Dabrosny wrote: ↑2017-08-30T16:05:42-07:00
In order to understand what -intensity does and how it replaces the old nonlinear and linear grayscale colorspaces, I did some experimentation and this "-intensity" documentation entry is based on what I believe is the actual behavior of IM 7 (at least in 7.0.6 x64 q16 HDRI).
See my preceding post on this thread for a list of questions that I felt this needed to answer.
I included a form of Snibgo's idea related to the possible use of sRGB and RGB colorspaces even for single-channel grayscale images.
I don't know whether perhaps some of what I'm documenting here is unintended behavior.
(I haven't yet tried to change the documentation for the -grayscale operation because its behavior has an inconsistency that I'll probably submit as a bug report later, rather than merely documenting its current behavior.)
This isn't formatted like the actual documentation, but you get the idea.
------------------------------------------------------------------------------
-intensity
method
set image attribute specifying the method to generate intensity values from a non-Gray image or pixel, and that identifies a Gray image as linear or nonlinear
This sets the intensity attribute of the current image(s), and also sets a global default for any further images lacking this attribute themselves. The form +intensity unassigns the intensity.
On a color image (non-Gray colorspace), this attribute determines the formula that will be used by -colorspace Gray to convert to grayscale, and in other situations where an operator needs to calculate a single grayscale value for some purpose, from the image's red, green, and blue pixel components.
The following methods are available (R,G,B indicate linear channels while R',G',B' indicate nonlinear):
Rec709Luminance converts to RGB and then uses 0.212656R + 0.715158G + 0.072186B to get a
linear grayscale (equal to luminance Y as in XYZ colorspace)
Rec601Luminance converts to RGB and then uses 0.298839R + 0.586811G + 0.114350B to get a
linear grayscale
Rec709Luma converts to sRGB and then uses 0.212656R' + 0.715158G' + 0.072186B' to get a
nonlinear grayscale
Rec601Luma converts to sRGB and then uses 0.298839R' + 0.586811G'+ 0.114350B' to get a
nonlinear grayscale
If the image has no intensity attribute, the global intensity set by the most recent -intensity is used. If there is no global intensity assigned, then the Rec709Luma nonlinear grayscale formula is currently used by default.
The -colorspace Gray conversion on an existing non-Gray image will set an appropriate gamma of 1 (linear) or 0.454545 (nonlinear) as well as retaining the intensity attribute on the resulting Gray image, so that further operations on this Gray image will know whether to treat it as linear or nonlinear. (Note: if a nonlinear image were ever treated as linear or vice-versa, then conversion back to a non-Gray colorspace would be incorrect, and conversion to an external file format (such as JPEG) would create an image file that would be too dark or too light when viewed.)
Whereas a non-Gray image is treated by IM as having nonlinear or linear values based solely on its colorspace irrespective of gamma (or of intensity), a Gray image will be treated as having linear values only if its intensity attribute is Rec709Luminance (or Rec601Luminance)
and its Gamma attribute is exactly 1. For all other combinations of intensity and gamma value (or lacking one or both), the Gray image will be treated as nonlinear (like sRGB).
Using -intensity (or +intensity) on an existing Gray image can change the interpretation of the image's values (as linear vs. nonlinear) without a corresponding change in those values themselves, much like -set colorspace changes the interpretation of a non-Gray image's values without changing those values. You should also -set gamma (1 for linear or 0.454545 for nonlinear) to be consistent with the new intensity, because, unlike -set colorspace, setting the intensity does not change gamma. If you set a linear intensity on a Gray image but don't also set the gamma to precisely 1, then IM will continue to treat the image as nonlinear. But if you set a nonlinear intensity (or remove the intensity) for a Gray image, it will be treated as nonlinear (like sRGB) irrespective of gamma.
While there is no operator that will convert between linear and nonlinear Gray images, an alternative is to use -colorspace RGB or -colorspace sRGB, which will keep the image as a single channel initially (and replicate it into three channels when needed by a further operation), although no longer identified as Gray colorspace. For many output file formats such as JPEG and PNG, the image will (for default settings) automatically be saved as a single-channel grayscale image if all three color channels are equal throughout the image, and if read back into IM this will become Gray colorspace and threfore subject to the behavior documented here for Gray rather than that of sRGB or RGB.
The following intensity methods are mathematical in nature and will use the current value in the image's respective r,g,b channel regardless of its colorspace or whether these channels are linear or nonlinear.
Brightness max(r, g, b )
Lightness (min(r, g, b) + max(r, g, b)) / 2.0
Average (r + g + b) / 3.0
MS (r^2 + g^2 + b^2) / 3.0
RMS sqrt( (r^2 + g^2 + g^2) / 3.0 )
These methods are often used for other purposes, such as generating a grayscale difference image between two color images (using -compose 'Difference' composition.
For example The 'MS' (Mean Squared) setting is good for minimizing color error comparisions. While... The method 'RMS' (Root Mean Squared) for example is appropriate for calculating color vector distance, from a color difference image. This is equivalent to the color only component of the -fuzz factor color compare setting.
See also -grayscale which applies one of the above formulas directly to convert an image to Gray without setting the global default intensity.
To print a complete list of possible pixel intensity setting methods, use -list intensity.
Operators affected by the -intensity setting of a non-Gray image include:
-adaptive-blur
-adaptive-sharpen
-black-threshold
-clut (when mapping grayscale CLUT image to alpha channel if set by -channels)
-colors for gray colorspace
-compose {LightenIntensity, DarkenIntensity, CopyOpacity, CopyBlack}
-contrast-stretch
-distort {ErodeIntensity, DilateIntensity}
-normalize
-random-threshold
-selective-blur
-shade
-threshold
-tint
-white-threshold