20 comments

[ 2.0 ms ] story [ 47.5 ms ] thread
> Pixels with RGB values of (128, 128, 128) emit about half as much light as pixels with RGB values of (255, 255, 255).

Technically, this is not always incorrect, if your working color space is linear and 0 is no light. The problem only comes if you hand that same data to routines or surfaces expecting sRGB or another nonlinear color space (or one where 0 is not no light).

> On which image does the gradation appear more even? It’s the second one!

I felt the first one looked more even. On the first I could tell the difference between every two adjacent bars. On the second one I couldn't tell any difference between the first 4-5 bars.

I felt the same, and I saw multiple people did back in 2016 as well: https://news.ycombinator.com/item?id=12554887

Edit: Looking at the raw pixel data, the cells seem to be (0, 0, 0), (0, 0, 0), (1, 1, 1), (8, 8, 8), so there is indeed no practical difference between the first three.

for lot of image processing linearizing srgb is only half-way measure, and you might want to reach for a better colorspace altogether. cielab is obvious example, or maybe oklab/jzazbz/xyb/something else.
there isn't much that is "arcane" about gamma correction itself (as in, the math itself is really simple).

at the fundamental level, if a surface is illuminated with one lightbulb and we add another light bulb, the difference is exteremely noticeable to the human eye. if we add one more lightbulb to a surface that is already illuminated by a hundred light bulbs, there will be no perceptible difference. the exact response can be modeled with a pretty simple power law (with a modification in the low range, as the article mentions).

that's all there really is to "gamma correction". it's a hack that exploits this quirk of the human visual system in order to more efficiently allocate bits for encoding different "lightness" values.

all of the confusion and bugs stem from one or more of the systems in the chain that forms the final image, making an incorrect assumption about what the others are doing. it's a bit like coordinate spaces in that regard.

ICC profiles present a similar problem and remarkably Instagram, ostensibly a photo-sharing web-site, does not respect them when scaling images (or at least it didn't a few years ago when I started posting photos to it from my PC); when I uploaded full-resolution SLR-resolution originals the skin-tones would get all screwed up. If I exported at the final resolution, even with the same ICC profile, they were fine.
I wonder why we use something polynomial-based instead of something exponential-based like the decibel scale?
This feels like less of a gamma issue and more of a color space and monitor calibration issue. Possibly affected by the fact that oklab didn't exist when this article was written and the general awareness of color spaces was lower.
This article contains some pretty major errors, which is kind of surprising to see!

>The transformation used to represent the physically linear intensity data either generated synthetically via an algorithm or captured by a linear device (such as a CMOS of a digital camera or a scanner) with the discrete values of the perceptually linear scale is called gamma encoding.

This isn't super correct, and it underscores the biggest issue in this article:

sRGB (and its gamma encoding function) has absolutely nothing to do with perceptual linearity. sRGB is not perceptually linear! The original gamma encoding as far as I'm aware was made to compensate for the nonlinear transfer function of CRTs back in ye olde days. Its true that human vision is nonlinear, but sRGB is not a particularly good match to the perceptual linearity of human vision. Its a really common error to make, and leads to people wondering why we can't use sRGB to blend in if the reason why it was invented is because its perceptually linear

The article goes to compound on this mistake, which is why this is such a problematic misconception:

> Interestingly, Photoshop antialiases text using γ=1.42 by default, and this indeed seems to yield the best looking results (middle image). The reason for this is that most fonts have been designed for gamma-incorrect font rasterizers, hence if you use linear space (correctly), then the fonts will look thinner than they should.

This is where the mistakes start to add up

Consider what you're trying to achieve during antialiasing: when rasterising a line, lets say we discover that a pixel is only 40% covered and want to darken it. This means that we want our pixel's brightness to decrease by 40% to a human being. We don't want to emit 40% less light, because that's not what antialiasing is trying to achieve!

Both sRGB and linear colour are the wrong colour spaces to use. You want to blend in a perceptually linear colourspace, and photoshop's 1.42 gamma exponent probably maps better to human vision than 2.2 or 1.0 while being cheaper than a LUV conversion

>The standard gamma (γ) value to use in computer display systems is 2.2. The main reason for this is because a gamma of 2.2 approximately matches the power law sensitivity of human vision

The gamma transfer functions are also wrong. Its worth getting hung up on because it actually causes nontrivial errors, especially in the age of hardware accelerated sRGB conversions where doing it correctly is free

As you, say the original purpose of gamma was strictly to compensate the non-linearity of CRTs.

The reason why gamma has been preserved in digital television even after CRTs have become obsolete is that it happens to perform a dynamic range compression that allows the use of 8 bits for luminance or for color components without making too visible the steps between adjacent color values.

If you want to encode the color components linearly, you need to use more than 8 bits, preferably the FP16 format, which was originally introduced in GPUs especially for this purpose.

So today the only purpose of gamma is as a method of data size compression that is specific to images, by allowing the reduction of the number of bits per pixel, while keeping acceptable the degradation of the image quality.

It is probable that the standard gamma curves are not optimal for data compression, but the slight improvements in image quality that could have been attained with other curves are not worth the complications that would have been created by abandoning the compatibility with legacy recordings.

> [...] the only reason to use gamma encoding for digital images is because it allows us to store images more efficiently on a limited bit-length [...]

It's interesting how this part of the trade-off changes when using float16 for color components (as is common when HDR is involved) rather uint8.

Good timing that Safari 27 adds support for srgb-linear and display-p3-linear color spaces.

I thought you corrected gamma in the monitor settings, not in your application code. Am I wrong.
I work on the open-sourced Jedi Academy game engine, which is based on id tech 3. It has special logic to specifically adjust the gamma of the physical monitor during launch, then restore it upon exit.

This is why when these older games crashed on your PC, the monitor would look all washed out due to the manual gamma adjustments the game made that didn't get restored.

Nice! I remember being an early Nvidia adopter (RIVA 128, TNT) when all of my fellow LAN party buddies were Voodoo users, and I had to download a custom opengl32.dll which actually adjusted the gamma into tolerable levels so you could see what was going on in Quake.
This kind of thing is why you need a separate watchdog process that gets notified when the other process terminates.
For those interested in this topic, I found minutephysics' treatment of this subject to be really pleasant and easy to grok. I had a talk I gave at work once or twice discussing color conversion and I would play a 90 second snippet from this (attributed appropriately) as it would very quickly crash course people on the reasoning behind an EOTF in a really cute and layman's terms kind of way:

https://www.youtube.com/watch?v=LKnqECcg6Gw

> On which image does the gradation appear more even? It’s the second one!

Can’t reproduce. Tested on two monitors on my desk, designer-targeted Benq and cheap laptop. On the Benq, darkest 3 segments are indistinguishable, the 4-th one barely distinguishable. On the laptop, darkest 4 segments are indistinguishable, the 5-th barely distinguishable. However, on the “emitted light intensity” all bars are clearly visible.

> Image resizing

“Unsurprisingly, C gives the correct result” On my computers B very similar to A, just a tiny bit darker. While the “correct” C result is a lot lighter than A.

Also from the same section:

> B the result of resizing the pattern by 50% directly in sRGB-space (using bicubic interpolation)

Bicubic interpolation is only applicable when enraging images; downsampling is very different problem from interpolation.

(comment deleted)