Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

IM 6.8.0.6 Q16 Mac OSX Snow Leopard

While researching for topic viewtopic.php?f=1&t=15564&start=15#p92944, I tried to check out the correspondence of LAB=50,0,0 and sRGB=119,119,119 per http://en.wikipedia.org/wiki/Middle_gray.

Using the color calculator at http://www.brucelindbloom.com/index.htm ... lator.html, it shows the above correspondence. That is if I put in LAB=50,0,0, I get RGB=0.466327,0.466327,0.466327, where I assume 50 is 50% in LAB and RGB is in range 0 to 1. So 0.466327*255=119. Alternately clicking the scale RGB checkbox after the calculation gives 118.9134. You can see this also at his example page at http://www.brucelindbloom.com/index.htm ... lator.html


But using IM, I get

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace LAB txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (32790,32769,32766) #801680017FFE cielab(50.0343%,50.0023%,49.9977%)

Thus sRGB=119,119,119 does not convert to LAB=50%,0%,0%, but seems to convert to LAB=50%,50%,50% using the txt: output format.

Am I doing something wrong here?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by magick »

We can reproduce the problem you posted and have a patch in ImageMagick 6.8.0-8 Beta available by sometime tomorrow. Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

I am not sure if this is part of the problem or a different issue.

If I use http://web.forret.com/tools/color.asp to convert rgb(119,119,119) to HSL and HSB, I get:
HSL=0%,0%,46.7%
HSB=0%,0%,46.7%

So in IM, if I do

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace HSB txt:
# ImageMagick pixel enumeration: 1,1,65535,hsb
0,0: ( 0, 0,12090) #000000002F3A hsb(0%,0%,18.4482%)

The above is incorrect!

But if I add -set colorspace RGB, then I get the correct values as follows

convert -size 1x1 xc:"rgb(119,119,119)" -set colorspace RGB -colorspace HSB txt:
# ImageMagick pixel enumeration: 1,1,65535,hsb
0,0: ( 0, 0,30583) #000000007777 hsb(0%,0%,46.6667%)

Now if I do the same converting to LAB, I must not add -set colorspace RGB in order to get the correct L value. Why is this?

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace LAB txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (32790,32769,32766) #801680017FFE cielab(50.0343%,50.0023%,49.9977%)

whereas the following is incorrect for L:

convert -size 1x1 xc:"rgb(119,119,119)" -set colorspace RGB -colorspace LAB txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (48479,32769,32765) #BD5F80017FFD cielab(73.9742%,50.0023%,49.9962%)


Please clarify or let me know that this is also part of the earlier fix?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by snibgo »

For the HSL calculation, this is consistent with my observation that "-equalize" in HSL space no longer equalises lightness. Instead, IM seems to convert the image to linear RGB, equalises the lightness in that space, then converts back to sRGB. I can prevent the conversion with "-set colorspace RGB".

In your example, I assume IM is doing its usual trick of assuming an image is in sRGB space unless told otherwise.

I know nothing about LAB.
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: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

snibgo wrote:For the HSL calculation, this is consistent with my observation that "-equalize" in HSL space no longer equalises lightness. Instead, IM seems to convert the image to linear RGB, equalises the lightness in that space, then converts back to sRGB. I can prevent the conversion with "-set colorspace RGB".

In your example, I assume IM is doing its usual trick of assuming an image is in sRGB space unless told otherwise.

I know nothing about LAB.

It is not the equalize but the change of colorspace. Each individual channels converted using -colorspace (and -channel or -separate) is considered as linear grayscale since IM 6.7.8.3. To keep them from getting converted to linear, you can use -set colorspace RGB, so that -colorspace thinks it already linear and does not convert from sRGB to linear. See viewtopic.php?f=4&t=21269
Last edited by fmw42 on 2012-11-27T20:24:49-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by snibgo »

Yes, indeed. I think we are saying the same thing.

And this is why, in your example ...

Code: Select all

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace HSB txt:
... IM is converting to RGB before converting to HSB, yielding hsb(0%,0%,18.4482%). I would call this correct (or at least "expected"), not incorrect.
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: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

snibgo wrote:Yes, indeed. I think we are saying the same thing.

And this is why, in your example ...

Code: Select all

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace HSB txt:
... IM is converting to RGB before converting to HSB, yielding hsb(0%,0%,18.4482%). I would call this correct (or at least "expected"), not incorrect.

That is a linear channel result and is correct according to the standards that define grayscale according to IM and the changes made recently. But most other systems do not consider linear channels for other colorspaces or even gray. So when I used the color converter web page, it produces non-linear channel values. Thus I had to add -set colorspace RGB to trick IM into thinking it was already linear and thus get non-linear results to match the color converter web site.

My point was that I get inconsistent results when doing the same things with LAB and using Lindbloom's converter, which is the "standard". I had to not add -set colorspace RGB in my IM convert. That was my concern. Is LAB different in terms of being linear or non-linear? Or is there some bug in IM?

P.S. Most people do not like viewing linear gray as it is too dark. It is probably the right thing when doing certain other processing, but the result should be converted back to non-linear for decent viewing. In my experience with my scripts, I almost always need to add -set colorspace RGB before any -colorspace ... -separate command to get results to match what I had before the change and also to appear correctly after processing. But that is just my experience and I am sure there are appropriate times to process in linear space for other colorspaces.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

magick wrote:We can reproduce the problem you posted and have a patch in ImageMagick 6.8.0-8 Beta available by sometime tomorrow. Thanks.
This appears to be corrected in the current version of 6.8.0.8 beta (ImageMagick-6.8.0-8.tar.xz 29-Nov-2012 07:43).

imb convert -size 1x1 xc:"rgb(119,119,119)" -colorspace LAB txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (32790, 1, 0) #801600010000 cielab(50.0343%,0.0015259%,-0.0030518%)


Thanks.


But why do I not need to use -set colorspace RGB when doing this with LAB, but I have to use it when converting to HSL,HSB

If I use http://web.forret.com/tools/color.asp to convert rgb(119,119,119) to HSL and HSB, I get:
HSL=0%,0%,46.7%
HSB=0%,0%,46.7%

So in IM, if I do

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace HSB txt:
# ImageMagick pixel enumeration: 1,1,65535,hsb
0,0: ( 0, 0,12090) #000000002F3A hsb(0%,0%,18.4482%)

The above is incorrect!

But if I add -set colorspace RGB, then I get the correct values as follows

convert -size 1x1 xc:"rgb(119,119,119)" -set colorspace RGB -colorspace HSB txt:
# ImageMagick pixel enumeration: 1,1,65535,hsb
0,0: ( 0, 0,30583) #000000007777 hsb(0%,0%,46.6667%)

Now if I do the same converting to LAB, I must not add -set colorspace RGB in order to get the correct L value. Why is this?

convert -size 1x1 xc:"rgb(119,119,119)" -colorspace LAB txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (32790,32769,32766) #801680017FFE cielab(50.0343%,50.0023%,49.9977%)

whereas the following is incorrect for L:

convert -size 1x1 xc:"rgb(119,119,119)" -set colorspace RGB -colorspace LAB txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (48479,32769,32765) #BD5F80017FFD cielab(73.9742%,50.0023%,49.9962%)


Can you clarify, please
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by magick »

This command gives you the expected results:
  • convert -size 1x1 xc:"icc-color(rgb,0.4666666666666667,0.4666666666666667,0.4666666666666667)" -colorspace HSB txt:
It would grand if rgb() returned linear RGB and srgb() non-linear RGB, however, we follow the SVG standard and it defines rgb() as non-linear.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

The issue is that conversion from sRGB color to LAB values is not consistent with conversion for example from sRGB colors to HSL or HSB given the same input. In the latter I must use -set colorspace RGB and I get results that match the color converters, but in the former (LAB) to get proper results I must leave off the -set colorspace LAB to match Lindbloom's color converter. So the issue is why the difference with LAB.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by magick »

Because Lab won't fit into an unsigned quantity. We need to add 0.5 to A & B to make it fit. We could avoid this step for HDRI but to keep it consistent we add 0.5 even for HDRI.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by fmw42 »

magick wrote:Because Lab won't fit into an unsigned quantity. We need to add 0.5 to A & B to make it fit. We could avoid this step for HDRI but to keep it consistent we add 0.5 even for HDRI.

How does that relate to using or not using -set colorspace RGB?

When convert to HSL/HSB, I have to use -set colorspace RGB. But when converting to LAB, if I add the same -set colorspace RGB, I get wrong values (all starting with sRGB color values). But leaving off the -set colorspace RGB for converting to LAB then does give the correct result.

There is an inconsistency here that I do not understand.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Possible bug LAB conversion or txt: IM 6.8.0.6 Q16

Post by magick »

All the colorspaces are linear other than sRGB. If you set the colorspace to RGB, the gamma function is not removed or applied when converted to another colorspace (since the source colorspace is then linear). Differences from expected results should be accounted for by the gamma function or for Lab the bias introduced to a & b.
Post Reply