Thursday, July 7, 2016

To Gamma or not to Gamma

Directly or indirectly, Gamma adjustment is part of most color vision simulation models. Since Gary W. Mayer and Donald P. Greenberg wrote "Color-Defective and Computer Graphics Displays" in 1988, the world of gamma has underwent numerous changes. This article explores the impact of the gamma value on colorblind simulation models, in the past and in the present. First, we need to understand the definition of gamma.

What is Gamma?

In "A Technical Introduction to Digital Video," Charles Poynton states:

"The nonlinearity of a CRT is remarkably similar to the inverse of the lightness sensitivity of human vision. Coding intensity into a gamma-corrected signal makes maximum perceptual use of the channel. If gamma correction were not already necessary for physical reasons at the CRT, we would have to invent it for perceptual reasons."

Electronic standards for gamma correction date back to the inception of color television. The days when television equipment used analog electronics. The television set used a CRT (Cathode Ray Tube), which had a non-linear output based on input voltage to the cathode. The gamma of a CRT is 2.35 to 2.55, with 2.5 being the standard value. The power relationship between input voltage and output in terms of lumens is:

output = input ^ gamma

Beyond looking at the gamma of an individual component, we can look at the gamma for the entire system. If the value of gamma is 1.0, then there is a linear relationship between the input and output. When viewing an image, the white objects in the surrounding environment would have the same brightness as those in the image. This is referred to as a "bright surround."

Movies are normally shown in a "dark surround." For this environment, a gamma of 1.5 provides a natural looking image. If the surrounding objects are visible in the room, then it is said to have a "dim surround." The corresponding gamma is 1.25. Thus, depending on the brightness of the surround environment, we need a system gamma of 1.0 to 1.5. The gamma for a system is the product of the gammas for each component in the system.

In an analog television system. The two major imaging components are the camera and television set. Thus, a camera needs a gamma of approximately 0.5 (in actuality, cameras have a gamma of 0.45). The PAL and NTSC television standard were designed to create a system gamma for dim surrounds, with the television set having a gamma of 2.5. For a reasonably short review of the technical details of gamma see Gamma FAQ - Frequently Asked Questions about Gamma by Charles Poynton.

Gamma and computers

When computers entered the world of color images, the standard color terminal was a CRT monitor with a gamma of 2.5. Color terminals underwent an evolution from 8 colors, to 16 colors, to 256 colors. The 256 color terminals did not really support 256 unique RGB colors. Rather, the first 16 colors were the same colors as the 16 color terminals, for backwards compatibility. These terminals only supported 216 RGB colors and 24 grayscale colors. While graphic cards in PCs actually supported 256 RGB colors, and were connected to monitors with a gamma of 2.5. This distinction is going to be important when evaluating CVD studies.

If we consider the PC being used in dim surroundings, we need a system gamma of 1.25. However, the system now has more components. The basic components are:
  •  camera gamma - image capture gamma
  • encoding gamma - image is encoded into a file
  • decoding gamma - extracting image from encoded file
  • lookup table (LUT) gamma - the gamma of the frame buffer (optional)
  • screen gamma - the gamma of the display device
We can group some components together, such that:
  • file gamma = camera gamma * encoding gamma
  • display gamma = LUT gamma * screen gamma
Thus,

system gamma = file gamma * decoding gamma * display gamma

The encoding gamma for JPEG files is 2.2. Each pixel in the file is encoded according to the following formula:

encoded pixel = camera pixel ^ 1/2.2

Decoding each pixel is then just a matter of:

decoded pixel = encoded pixel ^ 2.2

The gAMA field in a PNG file defines the gamma for the file. Although it may contain any value greater than or equal to 1.0, the current default value is 2.2.

The above components did not appear at once. It was an evolutionary process, in which operating systems and software development libraries adapted to color images. As discussed in the previous article, Chromatic Appearance Models (CAMs) and Chromatic Adaptation Transform (CAT) matrixes also evolved from their inception in 1976.

Gamma and color vision simulation

The first computer simulation model was done in 1988 by Gary W. Meyer and Donald P. Greenberg. In their paper, they use the acronym SML, instead of LMS. Their basic assumption is that SML color space is a linear transformation of the CIE XYZ color space. Meyer and Greenberg based their confusion points on a study by Esteves (see reference 4 in their paper). Some values were changed to eliminate negative results. My tests confirm that their algorithm never produces a negative value, although it occasionally produces an out-of-bounds positive value. The basis for their tests was an extended version of the Farnsworth-Munsell 100 Hue test.

While the Meyer and Greenberg study was published in 1988, Thomas Wolfmaier wrote a Java applet around 1999 that implemented their model. Two years later Matthew Wickline published his improved version of Wolfmaier's code. Thus, this code is referred to as MGWW.

In 1997, Hans Brettel, Francoise Vienot and John D. Mollon published Computerized simulation of color appearance for dichromats. Even though it was published 11 years earlier, Brettel-Vienot-Mollon do not reference the work of Meyer-Greenberg. They used a Hitachi color monitor driven by a Silicon Graphics 3x8-color graphics card (512 RGB colors). From the manufacturer of the card, we know that the computer was Unix based workstation. While they did not calibrate the gamma of the display, they did calibrate the colors within 2 nm. The major difference between BVM and MGWW is that BVM converts RGB to LMS and not CIE XYZ. The BVM algorithm makes no attempt to limit out-of-bound conditions for RGB values.

What about Gamma?

Neither Meyer-Greenberg or Brettel-Vienot-Mollon mentioned gamma in their papers. Gamma is important to CVD models, because gamma shifts the confusion line. When gamma is increased the colors shift towards black. Conversely, reducing the value of gamma shifts the colors to white.

Further, brightness adjustments for the display device effects the gamma. Brighter screens reduce gamma, while darker screens increase gamma. Then you have operating systems, such as some Android devices, that allow for different screen modes. Both of these factors are handled by the LUT gamma, which is part of the display gamma. Changes to the display gamma, change the system gamma.

The CRT devices used be Meyer-Greenberg and Brettel-Vienot-Mollon had a gamma around 2.5. LCD screens have a gamma of 1.0. Apple Macintoshes originally had a system gamma 0.82, while Microsoft Windows and UNIX/Linux had a system gamma of 1.0. Starting with Snow Leopard (Mac OS 10.6, Macintoshes now have a system gamma of 1.0. Web browsers have also shifted to a system gamma of 1.0. By having a system gamma of 1.0, the OS is neutral. The display gamma now handles the brightness adjustments based on surrounding environment.

When working with images in a current high-level language, we work with a bitmap file. In Java, each pixel is represented by 4 16-bit fields (transparency, red, green, and blue), which is True-Color RGB. In most cases, we are working file-gamma * decode-gamma = 1.0. This is true for both JPEG and PNG files.

Gamma used in Source Code

Each pixel in an image is separated into its red, green, and blue values. For each value the following formula  is applied:

ColorIn =  OrigColor ^ (1 / gamma)

After shifting the color space for one of the CVD types, the following formula is applied to each color value in the pixel:

ColorOut = ColorIn ^ gamma

The Gimp image editor, popular on Linux, is based on the BVM model. The gamma value used in their code is 2.1. While the comments in the code reflect an understanding of gamma, the authors do not explain their choice. Although the Gimp code is based on Vischeck, the non-availability of Vischeck makes the Gimp code a common BVM implementation. However, implementations of Gimp often incorporate variations from the base Gimp implementation.

In his original code, Matthew Wickline used a gamma of 2.20. In a newer revised Javascript code, there is a option menu that provides for selecting values between 1.0 and 2.22 depending on the color terminal and operating system. In his original code, Wickline reverses the above two formulas. His later Javascript code uses a different approach for gamma, and just applies the following formula after simulation:

finalColor = ((colorOut / 255) ^ gamma) * 255

In his colorblind simulation models, Loren Petrich took a totally different approach. Petrich uses a gamma that ranges from gamma > 0 to gamma = 1. While his comments gamma of zero  represents no gamma, his formulas would lead to a divide by zero condition for a gamma of 0. His input and output conversions use precalculated values based on the following:

ColorIn = ((OrigColor + 0.5) / 256) ^ gamma
ColorOut = ((256 x ColorOut) / 65536) ^ (1 / gamma)

In Petrich's system, gamma = 0 is the gamma for bright surroundings. What is interesting is that Petrich uses the same formula for BVM and MGWW. While the general format of the gamma formulas follows the formulas used by Wickline, they are the opposite of the formulas used by BVM.

The hardest simulation models to evaluate are those that use pre-calculated values such as ImageJ. Without any comments, it is impossible to know if gamma played a role in the pre-calculations.

Conclusions

Digital imaging technology, both hardware and software, have undergone radical changes since the studies by Meyer-Greenberg and Brettel-Vienot-Mollon. Smartphone technology is reaching the point that we can view images in bright surrounds (gamma = 1). Movie theaters theaters have a gamma around 1.5, for dark viewing conditions. PC screens were designed for dim surrounding (gamma of 1.25). We know longer need to adjust colors for CRT monitors with a gamma of 2.5, as devices gamma is now handled by the framebuffer LUT.

The screen brightness control for Android outputs a value between 0 and 255, where 255 is equivalent to a gamma of 1, and 0 is equivalent to a gamma of 1.51. Since brightness and screen modes adjust the gamma, accurate simulation requires a gamma that matches the gamma of the screen brightness. Version 1.1.0, and higher, of my Colorblind Simulator app include a gamma setting feature, for which the default setting is 1.

The Pro edition allows the user to vary the gamma, with one additional feature. A setting of 1.51 creates a variable gamma based on the current brightness value. This feature works best when the Auto brightness setting  is disabled.

One of the critical requirements for accurate simulation is that the gamma adjustment on the input side cannot create out-of-bound conditions. In testing Wickline's code, I found a regular pattern for extreme out-of-bound conditions. After changing the gamma equations to match those used by BVM, the out-of-bound conditions on input disappeared. While BVM and MGWW still produce different results, those difference are based on other factors. As noted above, Petrich saw this same error in Wickline's code. However, his formulas tend to produce a high number of out-of-bound conditions for the output.

So far, every piece of code tested has required some corrections in gamma. Gamma is important, as the values are critical for accurate simulation. The equipment used today are not the same as the equipment used in the original studies. Operating  systems and software libraries now handle much of the gamma correction. The simulation code for CVD needs to keep current with an ever changing world. Gamma of today is not the same as the gamma of the past.

No comments:

Post a Comment