It is currently Thu Jun 25, 2026 10:32 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: A problem using a variable with vector-set! to set the foreground colo
PostPosted: Sat Dec 27, 2014 9:45 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
A script i’m attempting to write isn’t working as intended, i’ve condensed the problem into the coding below
----------------

(define range (make-vector 4))

(vector-set! range 0 '(255 0 0))
(vector-set! range 1 '(0 255 0))
(vector-set! range 2 '(0 0 255))
(vector-set! range 3 '(128 128 128))

(define vary 2)

; (set! vary 1.3333)
; (set! vary (round vary))

(gimp-context-set-foreground (vector-ref range vary))

----------------
As written the script works and the foreground colour is set to Blue

If the first semicolon is deleted, vary is set to a non integer and the script fails (as expected)

If the second semicolon is removed i expected the script to work, but it fails with the error

Error: vector-ref: argument 2 must be: non-negative integer

Where am i going wrong ?, any thoughts


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 11:02 am  (#2) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Your condensed version appears to behave correctly.

> (define range (make-vector 4))
range
> (vector-set! range 0 '(255 0 0))
#((255 0 0) () () ())
> (vector-set! range 1 '(0 255 0))
#((255 0 0) (0 255 0) () ())
> (vector-set! range 2 '(0 0 255))
#((255 0 0) (0 255 0) (0 0 255) ())
> (vector-set! range 3 '(128 128 128))
#((255 0 0) (0 255 0) (0 0 255) (128 128 128))
> (define vary 2)
vary
> (set! vary 1.3333)
1.3333
> (set! vary (round vary))
1.0
> (gimp-context-set-foreground (vector-ref range vary))
(#t)


Can you provide the "uncondensed" version? Perhaps the problem lies elsewhere.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 11:11 am  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
maybe round does not return an "exact" integer value (an exact integer is for instance 3 while 3.0 is not) in the current scheme implementation;
try to use the procedure inexact->exact : (set! vary (inexact->exact (round vary))) when you are in doubt

_________________
"Where am I ?"


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 11:39 am  (#4) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
PS: you may also use the function trunc to get an exact integer

_________________
"Where am I ?"


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 11:58 am  (#5) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
saulgoode wrote:
Your condensed version appears to behave correctly.

> (define range (make-vector 4))
range
> (vector-set! range 0 '(255 0 0))
#((255 0 0) () () ())
> (vector-set! range 1 '(0 255 0))
#((255 0 0) (0 255 0) () ())
> (vector-set! range 2 '(0 0 255))
#((255 0 0) (0 255 0) (0 0 255) ())
> (vector-set! range 3 '(128 128 128))
#((255 0 0) (0 255 0) (0 0 255) (128 128 128))
> (define vary 2)
vary
> (set! vary 1.3333)
1.3333
> (set! vary (round vary))
1.0
> (gimp-context-set-foreground (vector-ref range vary))
(#t)


Ah, now there is a difference, it works for you but not for me, after running through the SFC i get the error below

----------------

> (define range (make-vector 4))

(vector-set! range 0 '(255 0 0))
(vector-set! range 1 '(0 255 0))
(vector-set! range 2 '(0 0 255))
(vector-set! range 3 '(128 128 128))

(define vary 2)
range#( (255 0 0) () () () )#( (255 0 0) (0 255 0) () () )#( (255 0 0) (0 255 0) (0 0 255) () )#( (255 0 0) (0 255 0) (0 0 255) (128 128 128) )vary
> (set! vary 1.3333)
1.3333
> (set! vary (round vary))
1
> (gimp-context-set-foreground (vector-ref range vary))
Error: vector-ref: argument 2 must be: non-negative integer

----------------

saulgoode wrote:
Can you provide the "uncondensed" version? Perhaps the problem lies elsewhere.


I could provide the uncondensed version but it’s a bit large as the vector range is from

(vector-set! range 0 '(0 0 0))
(vector-set! range 1 '(1 1 1))

Through to

(vector-set! range 254 '(254 254 254))
(vector-set! range 255 '(255 255 255))

Quite a long list ?, do i just copy and paste it in


Last edited by Steve on Sat Dec 27, 2014 12:15 pm, edited 1 time in total.

Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 12:01 pm  (#6) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
dinasset wrote:
maybe round does not return an "exact" integer value (an exact integer is for instance 3 while 3.0 is not) in the current scheme implementation;
try to use the procedure inexact->exact : (set! vary (inexact->exact (round vary))) when you are in doubt



Thanks, that looks very interesting, will try it out

ps i've tried round, floor, ceiling and truncate but none worked


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 12:27 pm  (#7) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
dinasset wrote:
maybe round does not return an "exact" integer value (an exact integer is for instance 3 while 3.0 is not) in the current scheme implementation;

In Script-fu's implementation of Scheme, round and truncate both produce exact numbers (in TinyScheme, integers -- and only integers -- are exact unless manually coerced to inexact). This is not necessarily true of other implementations; for example in Guile, round and truncate can produce inexact integers while non-integer rational numbers may be exact.
dinasset wrote:
try to use the procedure inexact->exact : (set! vary (inexact->exact (round vary))) when you are in doubt

You are absolutely correct; the safe bet is to include the coercion, especially if your code is to run on different Scheme implementations.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 12:52 pm  (#8) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
Thanks dinasset and saulgoode

(set! vary (inexact->exact (round vary)))

has solved the problem, a good lesson learned

Thanks again


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 1:19 pm  (#9) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Interesting. On my system, round returns an exact integer:

> (exact? (round 1.3333))
#t


Which version of GIMP are you using? (I have 2.8.14) And what locale do you have set? (I am using en_US)

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 1:46 pm  (#10) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
Saul,
the problem IMO is as follows:
- the value returned from round is considered an exact integer but is interpreted in an incorrect manner by some filters.
Because round returns a value made by an integer PLUS a dot PLUS one (or maybe more) zero(es) that value is interpreted as an integer which includes the zero(es)
I made an experiment with the filter lew-engrave
- passing the result of a division (let's say 9/2) for the value line width using the scheme function quotient it gets the value 4 (OK)
- passing the result of the same division using the scheme function trunc it again gets the value 4 and works appropriately
- passing the result of the same division using the scheme function round it gets 4.0 which is accepted but interpreted as 40! hence the filter works badly

If you want, you can make your own experiment using the attached code

(define (script-fu-test-integer
            theImage
            theLayer
            Q T R)


(let* (
      (dividend 9)
                (divisor 2)
                (result 0)
                (theEngravedLayer1 0)
                (theEngravedLayer2 0)
       )

(if (= Q TRUE)
(begin
    (set! result (quotient dividend divisor))
    (gimp-message (number->string result))
    (lew-engrave theImage theLayer 0
                          result    ; line width
                          1.1       ; gamma
                          0 2 TRUE 1 FALSE )
    (gimp-message "OK using quotient")
))   
(set! theEngravedLayer1 (car (gimp-image-get-active-drawable theImage)))

(if (= T TRUE)
(begin
    (set! result (trunc (/ dividend divisor)))
    (gimp-message (number->string result))
    (lew-engrave theImage theEngravedLayer1 0
                          result    ; line width
                          1.1       ; gamma
                          0 2 TRUE 1 FALSE )
    (gimp-message "OK using trunc")
))   
(set! theEngravedLayer2 (car (gimp-image-get-active-drawable theImage)))

(if (= R TRUE)
(begin
    (set! result (round (/ dividend divisor)))
    (gimp-message (number->string result))
    (lew-engrave theImage theEngravedLayer2 0
                          result    ; line width
                          1.1       ; gamma
                          0 2 TRUE 1 FALSE )
    (gimp-message "OK using round")
))

(gimp-displays-flush)


) ; end let*
) ; end define

(script-fu-register "script-fu-test-integer"
            _"<Image>/Script-Fu/Test/test integer"
            "test integer"
            "aaa"
            "bbb"
            "ccc"
            "*"
            SF-IMAGE      "Image"     0
            SF-DRAWABLE   "Drawable"  0
            SF-TOGGLE     "Quotient"  TRUE
            SF-TOGGLE     "Trunc"     TRUE
            SF-TOGGLE     "Round"     TRUE
)

_________________
"Where am I ?"


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 1:58 pm  (#11) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
saulgoode wrote:
Interesting. On my system, round returns an exact integer:

> (exact? (round 1.3333))
#t


Which version of GIMP are you using? (I have 2.8.14) And what locale do you have set? (I am using en_US)


I’m using GIMP (2.6.11)

> (exact? (round 1.3333))
#f

Its interesting if a little troubling

Sorry don’t know what locale is set to or where to find this info


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 5:09 pm  (#12) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
dinasset wrote:
Saul,
the problem IMO is as follows:
- the value returned from round is considered an exact integer but is interpreted in an incorrect manner by some filters.
:
:
:
- passing the result of the same division using the scheme function round it gets 4.0 which is accepted but interpreted as 40! hence the filter works badly

I am wondering what you are seeing to indicate that the line width is interpreted as "40". If this is true, there is likely a bug in the engrave script. I have not delved into the script to find what might be wrong, but I do notice a misapprehension in the author's description of the SF-ADJUSTMENT statement.
Quote:
SF-ADJUSTMENT "Line width" '(25 3 127 1 5 0 1) ; List (start-value min-value max-value small-step large-step [int=0 or float=1] [slider=0 or roll-box=1])


The highlighted parameter has nothing to do with determining whether the result is an integer or a float, it is the number of fractional digits that should appear in the displayed number (e.g., 0="1", 1="1.0", 2="1.00", 3="1.000"). Regardless of that parameter's value, the number passed to the script is the fully accurate floating point value (it is never an integer).

Try the following code to see what I am struggling to convey (unless you already know all this):
(define (test x)
  (gimp-message (number->string x)))
(script-fu-register "test"
  "Test..."
  ""
  "Saul Goode"
  "Saul Goode"
  "December 2014"
  ""
  SF-ADJUSTMENT "Test" '(0 0 1 1 1 0 0)
  )
(script-fu-menu-register "test"
  "<Image>/Filters/Misc"
  )

Even though the displayed number is an integer -- either "0" or "1" depending upon which half of the slider you're on -- the value passed to the script is a full-precision floating point number ("0.3124142857", for example). If you change the SF-ADJUSTMENT configuration to '(0 0 1 1 1 4 0) then the displayed value will be shown accurate to a ten thousandth of a unit (e.g., "0.3124") but the script will still receive the fully accurate floating point value. It is up to the script to convert such arguments to their intended precision. If the script requires an integer then it should round or truncate the supplied floating point number.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sat Dec 27, 2014 11:49 pm  (#13) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
Hi Saul,
you write "I am wondering what you are seeing to indicate that the line width is interpreted as "40""
It was only a guess (wrong) after having seen the different results from my test script.
Here they are:
image obtained by using "quotient" (the values are in all cases 9 as dividend and 2 as divisor)
Attachment:
DSCF0731-crop_engraved-using-quotient.png
DSCF0731-crop_engraved-using-quotient.png [ 164.84 KiB | Viewed 2306 times ]

image obtained by using "trunc"
Attachment:
DSCF0731-crop_engraved-using-trunc.png
DSCF0731-crop_engraved-using-trunc.png [ 164.84 KiB | Viewed 2306 times ]

image obtained by using "round"
Attachment:
DSCF0731-crop_engraved-using-round.png
DSCF0731-crop_engraved-using-round.png [ 23.69 KiB | Viewed 2306 times ]


you may easily understand the reason of my guess (wrong)

but what is definitely true for me is:
while quotient and trunc return a pure integer without any appended fractional value, i.e. simply 4, round returns a value which is not a simple 4, maybe a float value, IDK

you are right when saying that the filter should take care of what it expects, but from a coder point of view who makes use of an existing filter I'm right in preferring to use trunc and not round, for safety... at least as long as the behaviours will not be equalized

_________________
"Where am I ?"


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sun Dec 28, 2014 1:01 am  (#14) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
dinasset wrote:
you are right when saying that the filter should take care of what it expects, but from a coder point of view who makes use of an existing filter I'm right in preferring to use trunc and not round, for safety... at least as long as the behaviours will not be equalized

To be honest, I don't recall ever using 'round' -- you will typically find expressions such as (truncate (+ x 0.5)) in my code instead. The problem with 'round' is that it follows the IEEE standard of rounding any midpoints to the nearest even ( 1.5 rounds to 2, while 2.5 rounds to ... 2). This is done with the intention of statistically averaging out the times you round up with the times you round down. This may be useful when doing accounting and such, but for most graphical operations I want consistency in the result -- if I have a set of points that I want to scale or offset, I do not want the calculated values for the end coordinates dependent upon whether the coordinates happen to be odd or even.

So in my opinion, your preference of avoiding 'round' is a wise one. If there is a problem with using 'round', the reason I've never encountered it is because I don't use the function.

P.S. As a minor issue, 'trunc' is deprecated in Script-fu (and non-existent in most Scheme implementations). You should probably switch to its replacement, 'truncate'.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: A problem using a variable with vector-set! to set the foreground
PostPosted: Sun Dec 28, 2014 1:30 am  (#15) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
thanks Saul.
But, in any case, don't you think that "round" should return a "pure" integer value?
By "pure" I mean having no decimals at all, not having decimals set to 0.
If even or odd doesn't matter (at the moment...) to me, but I don't understand why does "round" return a value which is not a pure integer (1, 2, 3, 4, ....)
What is your opinion on that?

_________________
"Where am I ?"


Top
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group