Gamma correction the third – distribution of brightness values on 8 bits

You are here: Nature Science Photography – Contrast – Gamma correction

The remarkable coincidence in the matter, which often causes confusion, is that the same value (or its inverse function) provides both the perceptually appropriate reproduction of the brightness values, the correction of the non-linearity of the screens, and the visually best possible utilization of the 8-bit coding of the digital data that is used today. This brings us to the third reason for the necessity of gamma correction: the optimal distribution of brightness values across 8 bits.

At this point, we will refer to the section „The minimum size of brightness differences“, which shows that we are able to perceive intensity differences in the range of 1%. This means that we recognize it when one brightness range is 1.01 times brighter than another. This is relatively constant over the average brightness range. If the brightness decreases, our ability to distinguish also decreases. There are several reasons for this. First, stray light from the brighter parts of the image obscures subtle differences in the darker ones. Second, lateral inhibition is lower in the darker areas, which reduces contrast ability. And thirdly, at low light levels, the basic activity of the photoreceptors (their basic noise, so to speak) comes into play more strongly, which represents a constant for the visual system that must be overcome in order for perception to occur. All three reasons together ensure that our ability to distinguish brightness values decreases by a factor of ten over a three-decade section of photopic vision (see figure 12 in the section about „Light and dark adaptation“).

If we apply the 1 % to a digital, i.e., binary-coded, image, we need log(2)/log(1.01) = 70 brightness levels per exposure step, which represents an intensity change by a factor of two. To store the brightness values of a channel without visible gradations, a linear scale requires 9,900 steps, or about 11 bits, with a contrast of 100:1. By default, however, we now work with only 8 bits per color channel because this makes the hardware cheaper. However, these 8 bits can only store 256 linear brightness gradations – far too few to make our visual apparatus believe that color or brightness transitions are really smooth when we distribute them linearly.

By encoding the brightness values in each color channel with the ^1/gamma function, we are doing nothing more than applying our discriminating ability, which varies with brightness, to the distribution of brightness values and distributing the 256 levels provided by the 8 bits in a visually optimal way. Let’s examine the detailed distribution of the code areas to visualize the differences between the coding variants.

Brightness in exposure stopsCode RangeCode RangeNumber of codesNumber of codes

8 bit linear8 bit gamma12 bit linear16 bit linear8 bit linear8 bit gamma12 bit linear16 bit linear
025625640966553612869204832768
-11281872048327686450102416384
-26413710241638432375128192
-332100512819216272564096
-4167325640968191282048
-58541282048415641024
-643964102421032512
-7229325121816256
-812116256068128
-9015812804464
-1001146403232
-110823202116
-12061160108

The first column shows the brightness in exposure stops relative to the maximum white. The number 0 indicates the brightest part of the image, while the values -1, -2, and so on indicate one level less or a reduction of the amount of light by a factor of 2.

The following four columns show how these brightness values are stored in four different coding techniques. In the linear functions, the code value is proportional to the light intensity, and each reduction by a factor of 2 results in an equally large reduction in the code values. The gamma function illustrates the outcome of quantizing light intensities into integer values using a power function with an exponent of 0.45 (1/2.2). This is the transfer function of the sRGB color space and is close to what video cameras do. Most digital cameras and scanners also output gamma-corrected data but do not always use the 0.45 exponent.

The 8-bit linear model allows for a maximum of 128 code values at the highest brightness level. That’s a lot, and you can expect to be able to do a lot of post-processing in this range without getting artifacts. The following level, between -1 and -2, has only 64 code values. This is still close to the computationally necessary 70, but leaves no more room for maneuver. One step further down, we have only 32 values. Theoretically, this number of steps is insufficient to guarantee a visually consistent transition, and if we isolate this area and watch it separately, we would likely perceive the brightness differences as a staircase. But as long as it is part of an overall image, the staircase is lost in our sensitivity, which decreases with intensity, and 32 code values is just about enough. Below this limit, however, the visual quality of the linear 8-bit coding deteriorates significantly. The following brightness levels have only 16, 8, 4, 2, and, last but not least, one code value. Therefore, this linear coding is realistically well-suited for storing images with a contrast of 3 or 4 exposure levels, and it also offers the advantage of very good resolution in the uppermost range.

Now let’s consider the gamma-encoded 8-bit variant. It assigns 69 code values to the highest brightness level. Visually, this is sufficient, but it leaves minimal opportunities for postprocessing. The second brightest level has 50 values at its disposal. This is close, but still manageable, given that our perception of brightness is diminishing. For all subsequent brightness levels, the gamma version offers a greater number of code values compared to the linear coding, making it a superior option for the darker range. Even at -8, we still have 8 levels available, and since the discrimination ability, as described above, decreases by a factor of 10, this is still enough and far more than the 1 level in the linear encoding. In order to achieve a comparable result with a linear variant, we must, as the 8th column shows, already fall back to 12 bits per channel. With 16 bits, we are then completely on the safe side, but, as with 12 bits, we place much higher demands on the hardware.

The differences become even clearer when we look at the practical figures for an image in which the shadows are 7 f/stops darker than the highlights, which is not a particularly extreme situation. In the case of linear 12-bit encoding, code 4,095 has the brightest spot, while code 32 has the darkest spot, which is only 1/128 as bright. This implies that the rounding process stores some neighboring image spots, which, in reality, were almost equally bright, as either integer values 32 or 33. So what was a very small difference in the original scene is pumped up to a bigger one in the digital representation because the difference in brightness is 3% (the area with the value 33 must be rendered 33/32 times as bright as its neighbor). In the brighter areas of the image, this 3% difference in brightness would be visible without question, but in a shadow area, it is lost in the vast majority of viewing situations. In this case, the use of linear 12-bit encoding prevents any visible staircasing from appearing in the image. However, if we reduce the number of bits, the problem becomes larger. At 11 bits, the difference increases to 6% and to 12% at 10 bits. If only 8 linearly encoded bits are available, our shadow area falls to the value 2, and after rounding, its neighbor may fall to 3. However, this value 3 is 50 % brighter than 2, and we can still perceive this difference even in the darkest shadow areas.

Now let’s try the gamma corrected 8-bit variant (^1/2.2 or ^0.45). Our image area, which is only 1/128 as bright as the maximum value, falls to the integer value 29, according to the calculation (255 * 1/128)^0.45. With correct rendering, this corresponds to a relative intensity of (29/255)^1/0.45) = .00798. An approximately equally bright neighboring pixel with the code value 30 is thus rendered with the relative intensity of (30/255)^(1/0.45) = .00860. This corresponds to an 8% difference in brightness, which is much less visible than the 50% difference of the linear variant and will not be noticed in the majority of viewing situations.

Naturally, as with any other aspect of life, there are trade-offs involved, as gamma encoding offers the benefit of improved shadow resolution at the expense of a decrease in code values in the highlights. Here, too, we take a look at the incorruptible mathematics to see how big the disadvantage really is.

With the linear 8-bit variant, we get levels in the bright parts of the image that are very close together. The relative intensity difference between 245 and 255, for example, is only 0.4%. Gamma correction doubles it to 0.9 %, but it is still below the 1 % limit that we can distinguish in the middle brightness range.

In the bright areas of the image, the linear 8-bit code provides ample resolving power and even allows for some flexibility, but it clearly performs poorly in the shadow areas. On the other hand, gamma-corrected 8 bits allow just enough resolving power in the highlights to prevent banding and enough in the shadows to prevent banding in the majority of cases. Therefore, gamma-encoded 8 bits are an acceptable lower limit, although there may be instances where artifacts are visible even at this level. In the majority of images, however, it works because the brightness staircases are lost in the noise and the fading sensitivity of our visual apparatus.

Next Gamma values in different color spaces

Main Contrast

Previous Gamma Correction the Second – Compensating the Monitor Properties

If you found this post useful and want to support the continuation of my writing without intrusive advertising, please consider supporting. Your assistance goes towards helping make the content on this website even better. If you’d like to make a one-time ‘tip’ and buy me a coffee, I have a Ko-Fi page. Your support means a lot. Thank you!

0 - 0

Thank You For Your Vote!

Sorry You have Already Voted!

Join the discussion

Pleased to meet you!

Since I started my first website in the year 2000, I’ve written and published ten books in the German language about photographing the amazing natural wonders of the American West, the details of our visual perception and its photography-related counterparts, and tried to shed some light on the immaterial concepts of quantum and chaos. Now all this material becomes freely accessible on this dedicated English website. I hope many of you find answers and inspiration there. My books are on www.buecherundbilder.de

If you feel that reading JCSCZEPEK.com is worth the price of a few coffees, I’d greatly appreciate your support via my Ko-Fi page. Every donation energizes me to keep the thing going.

Thank you!
Jörg

jcsczepek.com