Don't use linear light when enlarging with Lanczos et al

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?".
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

The notorious http://www.4p8.com/eric.brasseur/gamma.html makes it pretty clear that if you reduce the size of an image, you should resample through linear light (not sRGB), as also discussed here http://www.imagemagick.org/Usage/resize/#problems.

There is a physical basis to wanting to downsize in linear ("physical") light, as opposed to perceptual (sRGB et al) light: You want downsampling to mimick the physical process of "averaging" photons hitting one spot of your retina, and this averaging is almost certainly better modeled by averaging linear light values.

One may be tempted to infer that enlarging (upsampling) should also be done through linear light. I certainly made this inference myself. And it is wrong.

Although I have not checked things with sufficiently many ways of enlarging through linear light (using both different conversion methods and different linear light colourspaces), I feel pretty confident that the following statement will not come back to haunt me:
(Nicolas') Conjecture
If you are enlarging (specifically, if you are not significantly downsampling) an sRGB image with a filter that has significant negative lobes (like Lanczos or Catmull-Rom), do not go through linear light, even if your set up allows you to convert between sRGB and linear light with insignificant loss (with IM, anything but an 8-bit compile if you use linear RGB with sRGB primaries; with XYZ, stick to HDRI). You are better off resampling with "straight" sRGB values.

Short version: With a sharpening filter (Lanczos, Catmull-Rom etc), don't enlarge in linear light.
(The same probably holds with Adobe RGB and some of the other "perceptual" colourspaces.)

P.S. There has to be someone who noticed this before. If you have references (web, books or articles), please communicate them to me.

P.S. The above assumes that your image is reasonably close to being a "natural image" in which sharply focused foreground objects are generally darker than their background. If your sRGB image is better described as being "reverse video", you may or may not (I don't really know) be better off enlarging in linear light (or, at least, enlarging with a "positive" version of the image). An attempt at dealing with this caveat is discussed here: viewtopic.php?f=22&t=21415.

P.S. These conclusions were made comparing a "straight" IM enlarge with one that goes through linear RGB with sRGB primaries using the IM7 -colorspace command. At some point, I'll need to make sure that they are not specific to the comparison of these two toolchains.

P.S. I forgot to thank my graduate student Adam Turcotte. The above conjecture was formulated while trying to make sense of the results that are coming out of his Masters thesis work in progress EXQUIRES (https://github.com/aturcotte/exquires).
Last edited by NicolasRobidoux on 2012-07-19T10:45:51-07:00, edited 34 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with lanczos et al

Post by NicolasRobidoux »

Conjecture unchecked by experience:
Don't (blindly) sharpen in linear light.

(This one's trickier, since clearly someone who is sharpening wants "sharpening artifacts" :))
Last edited by NicolasRobidoux on 2012-07-21T07:06:11-07:00, edited 1 time in total.
User avatar
Dane Vandeputte
Posts: 14
Joined: 2012-07-01T18:26:53-07:00
Authentication code: 13
Location: Illinois, USA

Re: Don't use linear light when enlarging with lanczos et al

Post by Dane Vandeputte »

I fully agree that enlarging via linear light is not a good idea, at least not without some sort of artifact reduction. Even then, I personally haven't seen enough perceptible advantage to bother.

I think the reason behind this is that when upsampling, the interpolated values are generally not too different from the existing samples, except at discontinuities like edges (but if the discontinuities are large enough to matter, they tend to be plagued by ugly artifacts when using linear light). On the other hand, the result of a lowpass filter can generate values very different from the original samples when enough high frequencies are attenuated, thereby being much more affected by the gamma value.
Digital image processing and photography enthusiast :)
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with lanczos et al

Post by NicolasRobidoux »

Dane Vandeputte wrote:...
I think the reason behind this is that when upsampling, the interpolated values are generally not too different from the existing samples, except at discontinuities like edges (but if the discontinuities are large enough to matter, they tend to be plagued by ugly artifacts when using linear light).
...
Exactly my thinking.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

"Nicolas'" Conjecture may appear to contradict the general statement made in http://www.poynton.com/notes/colour_and ... tml#linear:
If you wish to simulate the physical world, linear-light coding is necessary.
The contradiction is only apparent:
Because producing "negative light", which can occur when using a filter with negative lobes, is definitely not physical---of course, these values are generally clamped, but the fact that things have to be clamped is also a sign that the operation is not "physical"--- and producing "whiter than white" (white "brighter" than the white point) is also of dubious "physicality", although less obviously so, when you use Lanczos 3 or Catmull Rom to resample, you have unwittingly introduced the "non-physical" into your toolchain. Which means that Charles Poynton's admonition is N/A.
Last edited by NicolasRobidoux on 2012-07-21T07:07:52-07:00, edited 1 time in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

viewtopic.php?f=22&t=21435&p=87862#p87862 suggests that maybe what I should have written is
"Don't use linear RGB with sRGB primaries when enlarging with Lanczos et al".
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Don't use linear light when enlarging with Lanczos et al

Post by anthony »

In summary. Linear colorspaces is correct. But you need to separate intensity from color handling.

Resizing using a linear LAB colorspace works But only in ImageMagick v6.7.8-2 onward as before this LAB color value storage was not quite linear!

I would say the same is true for composition as well.

The LAB storage problem was discovered when trying to blur/sharpen just an images intensity, rather than its colors.


PS: the reason this is only a problem when enlarging is when shrinking colors are merged and strong edges tend to have more colors merged so as to generally produce less of a ringing effect. It is also only really a problem on edges involving primary colors like red and blue. This is why ringing is often seen on edges involving a clear blue sky.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

Where did you guys find a definition for "linear Lab"? You realize that the "linearity" often attached to Lab refers to "perceptual linearity"? Standard Lab is not "physical light" by any means.
(I hope I'm not sticking my mouth on my foot in my scattered responses to Anthony's "Lab idea". What am I missing?)
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

anthony wrote:...It is also only really a problem on edges involving primary colors like red and blue. This is why ringing is often seen on edges involving a clear blue sky.
Although I don't have time for this right now, this is why I'd be curious to compare enlarging through linear RGB with sRGB primaries with enlarging through XYZ.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

The results of a quantitative re-enlargement study performed by my Masters student Adam Turcotte (almost ready to be filed) suggest that there is caveat to the above conjecture:
(Nicolas') Conjecture (caveat)
However, if you are re-enlarging images produced by downsampling directly through sRGB (instead of going through linear light), you will get more accurate results going through linear light, even if you use a filter without negative lobes.
My guess as to why this should be the case is that if the image was downsampled without using linear light, upsampling filter artifacts are the least of your worries. Your primary goal should be to correct the "crime" committed by downsampling directly with sRGB (or at least not make things worse).
P.S. I would think that what holds for sRGB generally holds for colorspaces which relate to linear light in a similar "gamma larger than 1" way, that is, "reserve much of your gamut for darks".
User avatar
Dane Vandeputte
Posts: 14
Joined: 2012-07-01T18:26:53-07:00
Authentication code: 13
Location: Illinois, USA

Re: Don't use linear light when enlarging with Lanczos et al

Post by Dane Vandeputte »

NicolasRobidoux wrote:Although I don't have time for this right now, this is why I'd be curious to compare enlarging through linear RGB with sRGB primaries with enlarging through XYZ.
Linear sRGB is related to XYZ through a simple matrix multiplication, so the results would be visually the same.
Digital image processing and photography enthusiast :)
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

Dane Vandeputte wrote:...
Linear sRGB is related to XYZ through a simple matrix multiplication, so the results would be visually the same.
Nonlinearity beware! I don't expect a huge difference, but I expect one.
User avatar
Dane Vandeputte
Posts: 14
Joined: 2012-07-01T18:26:53-07:00
Authentication code: 13
Location: Illinois, USA

Re: Don't use linear light when enlarging with Lanczos et al

Post by Dane Vandeputte »

NicolasRobidoux wrote:
Dane Vandeputte wrote:...
Linear sRGB is related to XYZ through a simple matrix multiplication, so the results would be visually the same.
Nonlinearity beware! I don't expect a huge difference, but I expect one.
Where is the nonlinearity?
Digital image processing and photography enthusiast :)
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Don't use linear light when enlarging with Lanczos et al

Post by NicolasRobidoux »

Dane Vandeputte wrote:...
Where is the nonlinearity?
Oops! Got my conversations crossed: I meant I'm curious to try XYZ with sigmoidalization (instead of linear RGB with sRGB primaries): viewtopic.php?f=22&t=21415.
Apologies.
User avatar
Dane Vandeputte
Posts: 14
Joined: 2012-07-01T18:26:53-07:00
Authentication code: 13
Location: Illinois, USA

Re: Don't use linear light when enlarging with Lanczos et al

Post by Dane Vandeputte »

NicolasRobidoux wrote:
Dane Vandeputte wrote:...
Where is the nonlinearity?
Oops! Got my conversations crossed: I meant I'm curious to try XYZ with sigmoidalization (instead of linear RGB with sRGB primaries): viewtopic.php?f=22&t=21415.
Apologies.
No problem. :)
Digital image processing and photography enthusiast :)
Post Reply