bilinear point in percents

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Wolfy
Posts: 2
Joined: 2013-07-18T02:28:39-07:00
Authentication code: 6789

bilinear point in percents

Post by Wolfy »

Hello!
I am new to ImageMagick. I have this code

Code: Select all

convert t1-0.png -background white -flatten -alpha set -channel A -sparse-color Bilinear \"160,0 white  %w,0 transparent\"  t1-0.png
and i need to have percents but not pixels, something like this

Code: Select all

convert t1-0.png -background white -flatten -alpha set -channel A -sparse-color Bilinear \"50%,0 white  %w,0 transparent\"  t1-0.png
. But this gives unexpected results (not 50 percents). Please, help me
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: bilinear point in percents

Post by snibgo »

It would be good if all (x,y) coordinates could be given as percentages of (width,height). Sadly, they can't, and we have to do the necessary caculation in a script.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: bilinear point in percents

Post by GreenKoopa »

Instead of 50%, try %[fx:0.50*w]. In some places this works, others not (support is expanding). When it does, it is far more flexible that simple percents.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bilinear point in percents

Post by fmw42 »

GreenKoopa wrote:Instead of 50%, try %[fx:0.50*w]. In some places this works, others not (support is expanding). When it does, it is far more flexible that simple percents.
That should work for sparse-color as it does recognize % escapes. But if you want percent values, then use

%[fx:50*w/100],0 white

where 50 is from 50%. You cannot put % inside the fx expression. So strip the % from 50% and use 50.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bilinear point in percents

Post by fmw42 »

Actually rather than

%[fx:50*w/100],0 white

it should really be

%[fx:50*(w-1)/100],0 white

otherwise at 100% you would get w, which is outside the image by one pixel, since the last pixel corresponding to width is width-1.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: bilinear point in percents

Post by GreenKoopa »

Hopefully we helped with your initial problem Wolfy, so I'll continue on with this coordinate system question.

Some IM commands use 0,0 to mean the center of the top-left pixel (pixel coordinates), while others use 0,0 to mean the top-left edge of the image (top-left edge of the top-left pixel) (image coordinates). It is confusing (for me at least), but there are good reasons for it to be this way. Does -sparse-color uses pixel or image coordinates?
http://www.imagemagick.org/Usage/canvas ... _gradients
That documentation gives an example called gradient_math.png that states
Usage/canvas/#perfect_gradients wrote:If you were to look closely at this image you will find that the top and bottom most pixels are not white or black in color.
When I try this, my top most pixel is white. I don't know if something is awry or if this only serves to prove my confusion. Any ideas Fred and Anthony?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bilinear point in percents

Post by fmw42 »

I suspect that -sparse-color like the distort methods are using Image Coordinates.

If image coordinates are being used, then the command above should be what I wrote originally.

%[fx:50*w/100],0 white

But that won't make a gradient that is expected to go from full black to full white. One needs to convert the Image Coordinates to their corresponding Pixel appropriate coordinates by the 0.5 offset

But then my second version is not actually what is needed. What is correct according the the reference would be to start at

0.0.5 somecolor

and end at

0,%[fx:w-.5] someothercolor

or perhaps easier to use in code so that you are always using +0.5

0,%[fx:(w-1)+.5] someothercolor

This is essentially finding the center of the square pixel in Image coordinates for the first and last pixels in the horizontal dimension.

If the image is wide enough one probably won't see any difference visually and mathematically you would need to be pretty picky.

However I will defer to Anthony for corrections or a better explanation.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: bilinear point in percents

Post by GreenKoopa »

I reread the documentation and pinpointed why I was confused. The documentation implies that -sparse-color uses image coordinates. This seems proper to me, and intuitive since that makes all three of us correct on our first instinct. But the images within the documentation look like they were created using pixel coordinates. A quick experiment confirmed that this is the case.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: bilinear point in percents

Post by snibgo »

The text descriptions on that page don't correlate with the images or what IM actually does (or even what it did in old versions).
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: bilinear point in percents

Post by fmw42 »

Anthony will need to comment and clarify.
Wolfy
Posts: 2
Joined: 2013-07-18T02:28:39-07:00
Authentication code: 6789

Re: bilinear point in percents

Post by Wolfy »

This

Code: Select all

%[fx:50*w/100],0 white 
works fine for me! Thank you all very much! Imagemagick is awesome tool!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: bilinear point in percents

Post by anthony »

Sparse color and Distorts share the same code for converting input numbers coefficients (coords to color OR, coords to source coords, respectively). All coordinates are mathematical image coordinates. Percentages are not used.

HOWEVER.. both allow the use of % escapes in IMv6 (just as all option arguments will be able to use them in IMv7) as such you can use percentages, or even a 'normalized image coordinates' (a value of 0.0 to 1.0 across the image) simply by using some %[FX:...] expressions.

For an example see...
The Fractured Mirror distortion map
http://www.imagemagick.org/Usage/mappin ... ace_mirror

Which generated random points (positions from 0 to 1) of specific colors, using a Voronoi Mapping.

The operators that use pixel coordinates are: Text Placement, Image Offsets, Flood Fills (whcih needs a actual pixel), and Draw (which also makes some sense). As such mapping between these may need some 0.5 value differences.

Distort sets image 'offsets' correctly (as integer pixel coordinates) for composition when using +distort.

generally if the value is typically integer, it is pixel coords. If floating point it is image coordinates. The Exception is Draw, which allows floating point, but is more typically used using integers.


Actually many of the examples for sparse color does not make correct use of image coordinates in its arguments, so as to align its results with that of 'drawn circles' or the 'original image overlays'. It probably should, but the results have been good enough without this 'perfection'. Only the examples involving 'perfect gradients' actually use correct image coordinates.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: bilinear point in percents

Post by GreenKoopa »

Is this correct output?

Code: Select all

convert -size 1x5 xc: -sparse-color Barycentric "0,0 white 0,5 black" txt:-
# ImageMagick pixel enumeration: 1,5,65535,srgb
0,0: (65535,65535,65535)  #FFFFFFFFFFFF  white
0,1: (52428,52428,52428)  #CCCCCCCCCCCC  grey80
0,2: (39321,39321,39321)  #999999999999  grey60
0,3: (26214,26214,26214)  #666666666666  grey40
0,4: (13107,13107,13107)  #333333333333  grey20

convert -size 1x5 xc: -sparse-color Barycentric "0,0.5 white 0,4.5 black" txt:-
# ImageMagick pixel enumeration: 1,5,65535,srgb
0,0: (65535,65535,65535)  #FFFFFFFFFFFF  white
0,1: (57343,57343,57343)  #DFFFDFFFDFFF  srgb(87.4998%,87.4998%,87.4998%)
0,2: (40959,40959,40959)  #9FFF9FFF9FFF  srgb(62.4994%,62.4994%,62.4994%)
0,3: (24576,24576,24576)  #600060006000  srgb(37.5006%,37.5006%,37.5006%)
0,4: ( 8192, 8192, 8192)  #200020002000  srgb(12.5002%,12.5002%,12.5002%)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: bilinear point in percents

Post by anthony »

No it is not right !!!

Also the result (using pixel coordinates)...

Code: Select all

convert -size 1x5 xc: -sparse-color Barycentric "0,0 white 0,4 black" txt:-
# ImageMagick pixel enumeration: 1,5,65535,srgb
0,0: (65535,65535,65535)  #FFFFFFFFFFFF  white
0,1: (49151,49151,49151)  #BFFFBFFFBFFF  srgb(74.9996%,74.9996%,74.9996%)
0,2: (32768,32768,32768)  #800080008000  srgb(50.0008%,50.0008%,50.0008%)
0,3: (16384,16384,16384)  #400040004000  srgb(25.0004%,25.0004%,25.0004%)
0,4: (    0,    0,    0)  #000000000000  black
shows that what result I should be getting using these image coordinates for pixel centers....

Code: Select all

convert -size 1x5 xc: -sparse-color Barycentric "0,0.5 white 0,4.5 black" txt:-
# ImageMagick pixel enumeration: 1,5,65535,srgb
0,0: (65535,65535,65535)  #FFFFFFFFFFFF  white
0,1: (57343,57343,57343)  #DFFFDFFFDFFF  srgb(87.4998%,87.4998%,87.4998%)
0,2: (40959,40959,40959)  #9FFF9FFF9FFF  srgb(62.4994%,62.4994%,62.4994%)
0,3: (24576,24576,24576)  #600060006000  srgb(37.5006%,37.5006%,37.5006%)
0,4: ( 8192, 8192, 8192)  #200020002000  srgb(12.5002%,12.5002%,12.5002%)
NOTE: white in this case is because of 'clipping', in HDRI the above value would be a 'ultra-white'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: bilinear point in percents

Post by GreenKoopa »

snibgo and I thought not. Sorry it took so long to demonstrate this clearly.
Post Reply