Applying Gamma Correction to digital renderings allows to produce better looking images much faster, and is mandatory for a seamless compositing of rendered imagery and real footage.
This article is not an how-to tutorial, but rather a why-to. It dates back to 2005 but is still actual and the concepts apply to all renderers.
Gamma correction and linear rendering (aka "Linear Workflow")
Gamma Correction seems to be one of the most misunderstood topics in digital rendering. The fact that subjectively good images can be produced without Gamma Correction, coupled with some established practices in CGI world, led many artists to steer clear of Gamma Correction.
This old school scene with 1100 polygons and two spotlights won't win any award but demonstrates the point pretty well. From left to right, the first is the uncorrected rendering, the second is the gamma-corrected one, and the third is the reference photograph.
This not-so-short guide will introduce you to Gamma Correction in digital rendering. You'll learn why it is useful and how to make the most from it.
"Believe it or not, this was the way it was done 20 years ago, but the idea got lost along the way..."
Alvy Ray Smith, Microsoft Tech Memo, September 1995
I. The no-nonsense approach to Gamma
Gamma is a mathematical function, and most people know it is somehow related to the way a computer screen displays the colors and how the human eye perceives them.
Images which are not Gamma Corrected exhibit an harsh contrast with lack of detail in shadow and highlights areas. Also, physically correct lighting does not look right.
This happens because rendering calculations are performed in something called a linear color space. Which is just a fancy word to express a simple concept: if we multiply by two a pixel's value, the resulting pixel will be twice as bright. Sounds pretty logical.
However this is not how our eyes perceive colors. Twice the luminous intensity does not mean that we perceive twice as much brightness. In dark regions adding even a slight amount of light will be perceived as doubling the brightness. In lighter regions, a lot more light would be needed to achieve the same result.
To convert linear images as produced by the rendering software into tones suitable to our eyes a correction is needed. This Gamma Correction maps the linear intensities to a different representation that is finally perceived correctly.
Note: This has nothing to do with monitor gamma calibration. Even on a perfectly calibrated monitor, an uncorrected rendering appears wrong. Monitor gamma calibration is a separate step that compensates for monitor's own display characteristics but images are supposed to be in gamma space to be properly displayed.
Given that every software has a Gamma Correction filter or control, it would seem very easy to solve this issue.
Unfortunately simply applying Gamma Correction to an existing rendering will not produce a better looking image. Instead, the image will probably look washed out and lacking in contrast. This is where most people give up with Gamma Correction. What's supposed to make images look better totally ruins them.
Renderings have to be produced from the beginning with Gamma Correction in mind. An image that looks good without Gamma Correction, has already been artistically corrected with a lot of texture and lights tweaking. Adopting Gamma Correction improves the artist's workflow and produces results that are both visually and technically correct.
II. The boring but necessary demonstration of Gamma usefulness
As boring as they may look, the following images demonstrate how our eyes work. Both images are renderings of the same scene. A flat black surface with a white linear gradient starting at the center and growing outward. We expect the rendering to produce a smooth transition from light to darkness, similar to lighting a purely diffuse sphere from the observer's position.
The first image is the straight linear render, while the second one has a 2.2 Gamma correction applied.
This linear image seems to have a contrast peak both in the white center and in the dark outer region. It also looks smaller, while it is of the same size as the other one. Looking at its brightness histogram, we can verify it is indeed a linear ramp from black to white. But our eyes do not perceive it as such.
This gamma corrected version appears to be the smooth linear gradient we were expecting. Looking at its histogram, we can see that the distribution of its brightness values is not linear but follows a curve that peaks in the midtones.
Note: This may not yet be obvious, but the examples above show that each and every rendering algorithm, even physically based ones, will not appear to produce correct results unless Gamma Correction is applied. There is no way to "fix" the look of a linear rendering adding lights or tweaking colors, because the result of even the most simple calculations is distorted. It's exactly like trying to draw a curved line following a ruler. Next sections will deal with more practical examples.
III. Bring back real world lighting and textures
Much in the same way as the first example, correctly computed lighting values appear distorted to our eyes. In real world, lighting decreases with the inverse of the squared distance from the light source. This is known as the "inverse square law". We would expect that using real world formulas would produce photorealistic results.
But it doesn't, and many computer graphics book state that such a falloff is too harsh and should be avoided. Not surprisingly, the real world falloff is correct and appears visually pleasing if we gamma correct the renderings.
The following images are renderings of the same scene, with a point light placed 1 meter above a flat wood surface. Inverse squared distance falloff is turned on for the light. Ambient light is turned off.
This is the linear rendering, uncorrected. Looks more like a spotlight, with a strong dark falloff. Surely in real world light does not look so harsh. But let's try to compare this image's histogram with a math plot of the 1/d2 function. They look identical, and this demonstrates that this image is indeed physically correct.
What follows is the gamma corrected version of the same image. Looks much more like real world lighting, with light that quickly gets dimmer but does not abruptly disappear.
It may well be mistaken for a flash photograph of real wood. However it is not yet a properly gamma corrected image. The wood texture has been shot with a digital camera and therefore it has already a 2.2 Gamma Correction applied. This means that the wood texture's colors have been Gamma corrected twice. That's why the image looks washed out.
In the image to the right, the gamma correction applied to the wood texture has been undone applying an inverse gamma value to the texture.
To undo any gamma correction, simply take the reciprocal of the already applied gamma factor. For a gamma of 2.2, like most digital cameras, the correct value is 0.454545 (1.0 / 2.2) and in fact this fixes the washed out colors.
IV. Your long time lost friend: the ambient light
Without Gamma Correction, real world lighting falloffs look too dark. For the same reason, the ambient light used to simulate indirect lighting looks too bright and most textbooks suggest to turn it off completely.
The rationale behind this suggestion is that ambient light is a value that gets added equally to all surfaces, so it really does not help defining any shape: it only brightens the whole image.
Without Gamma Correction this is true. However, since gamma correcting an image is not a linear operation, ambient light reacts in a different way when corrected.
It will brighten a lot the darker areas of the image, because Gamma brings lower values much higher, but will impact well lit areas much less.
This image shows the differences in ambient light between a linear and a gamma corrected rendering.
The image on the left has been obtained subtracting a gamma corrected rendering with 5% white ambient light from a gamma corrected one with no ambient light.
The one on the right is the result of subtracting the linear rendering. As you can see, the difference is a flat silhouette, representing the uniform 5% added brightness.
Let me restate this again: when using gamma correction, ambient light is not flat anymore. It helps creating soft, GI-like, shading because it works as it was originally intended to work: as a coarse approximation of global Illumination.
The ambient-light shaded image above shows how ambient light approximates the look of a GI rendering. This is only possible because the ambient light is modulated by the gamma correction.
To take even more advantage of this nonlinear brightening, we can apply a trick dating back to the early days of computer graphics. We will model ambient light with a point light placed at the observer's position.
Such a light sees everything from the camera's position and therefore will never cast visible shadows.
This way the ambient light gets shaded both during rendering and when gamma correction is applied. As can be seen in the image below, the subjects gains a soft, GI like, shading. Let's see how we can improve on this, while keeping render times at a minimum.
This image is rendered without global illumination. There are no HDR maps, no ambient occlusion, no sub-surface scatterings. A few point lights are placed to provide non-uniform lighting and a strong ambient light term provides the GI look thanks to gamma correction.
V. References and links
In Favor of Back End Gamma Correction This article deals with VRML rendering but makes several important points about linear color space computations. It is co-authored by Gregory Ward, creator of the Radiance (HDR) format and of the Irradiance Caching algorithm (used by most renderers including Lightwave, Mental Ray and Photorealistic Renderman).
Why Render in Linear Floating Point? This short article deals with sRGB color space and shows how close to real world photography a simple 3D scene can be when pixel intensities are properly mapped. Also check the "Why store files in sRGB?" section from the main page. By Bill Spitzak of Digital Domain.
Gamma Correction Tech Memo Dating back to 1995, this Microsoft technical memo by Alvy Ray Smith of Pixar fame, is the real eye opener regarding gamma correction.
PNG (Portable Network Graphics) Specification This article is an appendix to PNG file format specifications. Deals with gamma correction from a more theoretical and mathematical point of view.
Max is a published author, has been producing commercial 3D animation since 1989, and his softwares have been sold in 31 countries around the world. He has taught compositing and animation both online and in classrooms. Anybody who has seen Big Bang Theory refers to him as Sheldon — he thinks it's a fair comparisons but doesn't quite get why it's supposed to be funny.