Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Voodooman
Posts: 1
Joined: 2017-07-29T04:33:39-07:00
Authentication code: 1151

Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post by Voodooman » 2017-07-29T04:49:35-07:00

So, I wanted to generate gradient texture sampler for shader, did this:

magick -size 256x1 -define gradient:angle=90 gradient:#000000-#00ff00 linear_gradient_green.png

Supposed result should be color from 0 to 255 where pixel X coord equal to color number.
From 0 to 128 everything is correct, 0=0 and 128=128, but things starting to get funny from X=129 which for some reason still has same color 128 as previous pixel, and this end with entire second half of range shifted by 1 pixel and 255=254.

Please, fix this miscalculation of gradient, it may be not very noticeable by eye, but when you use texture for sampling colors in shader and when instead of pure white you got shade of grey, it gets ugly and ruins color calibrations.

snibgo
Posts: 9253
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post by snibgo » 2017-07-29T06:16:22-07:00

Please say what version of IM you have, on what platform. What Q-number is your version?

I confirm the bug for v7.0.1-0 and v6.9.5-3. The workaround for v6 is to use the older form of gradient:

Code: Select all

convert -size 1x256 gradient:#000000-#00ff00 -rotate -90 out.png
The workaround doesn't work for v7.0.1-0.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 451
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post by GeeMack » 2017-07-29T07:58:15-07:00

Voodooman wrote:
2017-07-29T04:49:35-07:00
Supposed result should be color from 0 to 255 where pixel X coord equal to color number.
Using IM 7.0.6-3 HDRI on Windows 10 64. When I run this command...

Code: Select all

magick -size 256x1 -define gradient:angle=90 gradient:#000000-#00ff00 -unique-colors info:
The output is this...

Code: Select all

gradient:#000000-#00ff00 XC 256x1 256x1+0+0 16-bit sRGB 0.000u 0:00.000
This shows 256 unique colors in the 256x1 gradient image, which should mean there are no duplicates.

snibgo
Posts: 9253
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post by snibgo » 2017-07-29T08:40:58-07:00

Assuming you are using Q16, there might be 256 unique colours, but they might be the wrong colours.

The OP may be using Q16, but using 8-bit software to read the values, which confuses everyone.
snibgo's IM pages: im.snibgo.com

snibgo
Posts: 9253
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post by snibgo » 2017-07-29T08:48:25-07:00

Here is a simpler example. First, a correct result:

Code: Select all

f:\web\im>%IM%convert -size 1x16 gradient:black-white txt:
# ImageMagick pixel enumeration: 1,16,65535,srgb
0,0: (0,0,0)  #000000000000  black
0,1: (4369,4369,4369)  #111111111111  srgb(17,17,17)
0,2: (8738,8738,8738)  #222222222222  srgb(34,34,34)
0,3: (13107,13107,13107)  #333333333333  grey20
0,4: (17476,17476,17476)  #444444444444  srgb(68,68,68)
0,5: (21845,21845,21845)  #555555555555  srgb(85,85,85)
0,6: (26214,26214,26214)  #666666666666  grey40
0,7: (30583,30583,30583)  #777777777777  srgb(119,119,119)
0,8: (34952,34952,34952)  #888888888888  srgb(136,136,136)
0,9: (39321,39321,39321)  #999999999999  grey60
0,10: (43690,43690,43690)  #AAAAAAAAAAAA  srgb(170,170,170)
0,11: (48059,48059,48059)  #BBBBBBBBBBBB  srgb(187,187,187)
0,12: (52428,52428,52428)  #CCCCCCCCCCCC  grey80
0,13: (56797,56797,56797)  #DDDDDDDDDDDD  srgb(221,221,221)
0,14: (61166,61166,61166)  #EEEEEEEEEEEE  srgb(238,238,238)
0,15: (65535,65535,65535)  #FFFFFFFFFFFF  white
Now, a bad result, that doesn't even reach white.

Code: Select all

f:\web\im>%IM%convert -size 16x1 -define gradient:angle=90 gradient:black-white txt:
# ImageMagick pixel enumeration: 16,1,65535,srgb
0,0: (0,0,0)  #000000000000  black
1,0: (4096,4096,4096)  #100010001000  srgb(6%,6%,6%)
2,0: (8192,8192,8192)  #200020002000  srgb(13%,13%,13%)
3,0: (12288,12288,12288)  #300030003000  srgb(19%,19%,19%)
4,0: (16384,16384,16384)  #400040004000  srgb(25%,25%,25%)
5,0: (20480,20480,20480)  #500050005000  srgb(31%,31%,31%)
6,0: (24576,24576,24576)  #600060006000  srgb(38%,38%,38%)
7,0: (28672,28672,28672)  #700070007000  srgb(44%,44%,44%)
8,0: (32768,32768,32768)  #800080008000  srgb(50%,50%,50%)
9,0: (36863,36863,36863)  #8FFF8FFF8FFF  srgb(56%,56%,56%)
10,0: (40959,40959,40959)  #9FFF9FFF9FFF  srgb(62%,62%,62%)
11,0: (45055,45055,45055)  #AFFFAFFFAFFF  srgb(69%,69%,69%)
12,0: (49151,49151,49151)  #BFFFBFFFBFFF  srgb(75%,75%,75%)
13,0: (53247,53247,53247)  #CFFFCFFFCFFF  srgb(81%,81%,81%)
14,0: (57343,57343,57343)  #DFFFDFFFDFFF  srgb(87%,87%,87%)
15,0: (61439,61439,61439)  #EFFFEFFFEFFF  srgb(94%,94%,94%)
But this with v6.9.5-3 (on Windows 8.1).
snibgo's IM pages: im.snibgo.com

User avatar
magick
Site Admin
Posts: 10684
Joined: 2003-05-31T11:32:55-07:00

Re: Gradient BUG - 128 of 255 repeats twice, shifts whole gradient by 1

Post by magick » 2017-07-29T09:05:10-07:00

Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ http://www.imagemagick.org/download/beta/ by sometime tomorrow.

Post Reply