Re: When converting a grayscale image to color: result differs depending on -intensity setting: rec*luma vs. rec*luminan
Posted: 2017-08-26T09:51:15-07:00
I couldn't agree more.snibgo wrote: ↑2017-08-26T07:52:05-07:00 Storing the same data, or metadata, in multiple places isn't a good idea. What happens when the multiple values disagree with each other?
Pixel values may represent linear data, or not. This information is currently represented by metadata (a) colorspace, (b) gamma and (c) profile. Fairly recently we now have (d) intensity. Yikes. Four places!
I'd be fine with not having separate grayscale colorspaces, so long as it was clear what the colorspace means when there happens to be a single channel. For example if a single-channel image has what I would call a "nonhomogeneous" colorspace such as Luv, Lab, or LCh, how should we interpret that channel? For LChuv or LCHab, we could interpret it as, L, C, or H, which mean completely different things; in fact H is a hue angle ("linear" I suppose) and not compatible at all with being treated like L (a nonlinear luminance but not the same as the sRGB nonlinearity). If we going to truly support single-channel colorspaces corresponding to these, we'd probably want to start with "Lab_L" or "Luv_L" (if they're different -- I forget).snibgo wrote: ↑2017-08-26T07:52:05-07:00 Dabrosny asks: How should I flag a grayscale image as linear? It's a fair question.
I would add the converse question: When IM has an image in memory, what metadata tells me whether it is linear?
I'll add an important condition: the answer to the questions should not depend on whether the image happens to be grayscale. The image might have come from anywhere, or it might have been created as part of an IM command or program. Colourfullness is independent of linearity. Whether an image has one channel or three, and whether the three RGB channels happen to contain the same values or not, the data can be linear or non-linear.
If the answers do depend on whether the image happens to be grayscale, that raises difficulties about when the metadata is updated. Does this happen automatically, internally within IM? Whenever a pixel is updated in memory, does IM have to check whether the gray/colour status has changed so it can update the linear flag? That would be absurd. Or do we have to add this to all our scripts, to check whether each created file happens to be grayscale so we can set the metadata correctly? Also absurd.
So I conclude that a four-valued colorspace ("linear-colour", "non-linear-colour", "linear-gray", "non-linear-gray") is a bad idea, and would be a bad answer to either question.
But I'm happy to ignore those "advanced" colorspaces for the moment and focus on homogeneous colorspaces like RGB and sRGB, where all three channels can be treated in a similar way and all have the same [non]linearity.
I agree that if a single channel image has colorspace RGB or sRGB then it should be treated like a linear or nonlinear(sRGB-like) grayscale image.
I'm not sure if we should completely do away with grayscale colorspaces, because that would be a huge change (of mindset for one thing), but I understand that this would have benefits in terms of simplicity.
I don't want to go too far on this question, but as far as I know, a colorspace, or at least an ICC profile, would normally specify the number of channels and the interpretation of each channel. Usually defining each channel in terms of transformations in relation, ultimately, to XYZ (or possibly Lab). In the case of a single-channel colorspace or color profile like adobe's sGray.icc, that single channel's interpretation would probably be defined in terms of the linear luminance Y in CIE XYZ (could be L in Lab but I doubt it).
It follows for example from the formulas relating linear RGB and XYZ (CIE) that when you transform RGB to linear luminance Y, if you then replicate Y into three channels you can call it a linear RGB image and the transformation back to Y will indeed preserve the value of Y in round trip.
A linear RGB image having R=G=B literally represents the same image as a linear grayscale image with linear luminance ("Y" of XYZ if you like) having that same value, which means that if either of those images is transformed into a "reference" colorspace like XYZ or Lab, the result will literally be identical.
[edit: in this paragraph i corrected linear vs nonlinear]
Similarly, an sRGB image having three identical channels literally represents the same image as a nonlinear "sGray" image (same as what's stored in true single-channel jpeg) having that same value in its single channel.
I understand that you want to take advantage of relationships like this to use the same RGB and sRGB colorspaces to identify a grayscale image, understanding that they are the same as the multichannel images that you get by replicating the single channel.
The original "source" of of an image sets the colorspace or profile to one that matches the number of channels in the image.
Okay, but you will have to remember to -set colorspace RGB manually every time after you do "-grayscale rec709luminance: or "-intensity rec709luminance -colorspace gray", unless we modify these operations to do so for you.snibgo wrote: ↑2017-08-26T07:52:05-07:00 I don't claim IM's current scheme is good. It isn't. But I don't want to make a bad situation worse.
...
"How should I flag a grayscale image as linear?" Answer: the same way as for a colour image, use "-set colorspace RGB". I advise not to use "-intensity" except for specific operations, and unset it afterwards.
This doesn't work in general currently. In the simple case of an image that was created by "-intensity rec709luminance -colorspace gray" (or -grayscale rec709luminance), it makes no difference to IM what you might set the grayscale image's gamma to -- the interpretation of that grayscale image when you do something like -colorspace sRGB (including implicitly when writing to jpeg) will depend solely on the current intensity setting on that image (which will have been set by the grayscale operation to indicate linear or nonlinear), which is apparently by design. The result of -colorspace sRGB is completely unaffected by the gamma setting on the grayscale image.snibgo wrote: ↑2017-08-26T07:52:05-07:00 "When IM has an image in memory, what metadata tells me whether it is linear?" Check the colorspace. If it is RGB or sRGB, that answers the question. If it is "gray", then examine the gamma. If it is or isn't 1.0, that answers the question.
As far as I know, these methods work. If they don't, that's a bug.