What every coder should know about Gamma Correction
Essential Gamma Correction Knowledge for Programmers
By John Novak (Adapted) Riding the Gaussian curve since 1979 Originally published: September 21, 2016
📋 A Quick Diagnostic Quiz
If you are currently writing—or intend to write—code that involves image processing, please go through the following checklist.
- I have no idea what "gamma correction" actually is.
-
Gamma is just a legacy issue from the CRT era; since we use LCDs now, it's irrelevant. - Gamma only matters for high-end print professionals who need perfect color accuracy; for general coding, it's negligible.
- I trust my operating system's graphics libraries to handle gamma automatically.
- I believe the library I'm using (e.g.,
OpenGL,DirectX, or a specific wrapper) handles gamma correctly. - I assume a pixel with an RGB value of
(128, 128, 128)emits exactly 50% of the light of a(255, 255, 255)pixel. - It is perfectly fine to load raw pixel data from a
.jpg,.png, or.gifand run mathematical algorithms directly on those values.
The Verdict: If you checked one or more of these boxes, there is a significant probability that your software is producing mathematically incorrect results.
Because these errors are often subtle, they might not be immediately obvious depending on your specific project, but they are there.
The "Invisible" Problem
It is a strange reality that gamma is almost entirely ignored by the average computer user and even by professional software engineers. This oversight is so pervasive that many modern photo editors, image viewers, and drawing tools still fail to implement gamma correctly.
Why is this so common?
The lack of awareness stems from a few sources:
- Academic Gaps: Most computer graphics textbooks barely mention gamma. Even my own university textbook ignored the practical implications of gamma handling.
- Vague Documentation: When it is mentioned, it's often described in abstract terms without concrete examples or visual demonstrations of what happens when you get it wrong.
- Confusing Resources: Online articles are often either too academic, filled with irrelevant tangents, or simply incorrect.
I personally hit this wall while developing my own ray tracer. I realized my understanding was superficial, and after spending days researching, I found that clear, comprehensive guides were surprisingly rare.
This article is my attempt to fill that void. I will explain gamma clearly, assuming you have no prior knowledge of the subject.
🛠️ Optimal Viewing Conditions
To ensure the visual examples in this guide work as intended, please follow these constraints:
- Hardware: Use a standard computer monitor (LCD or CRT).
- Avoid: Mobile phones or tablets, as their screen calibrations vary wildly.
- Environment: View this in a dimly lit room to avoid screen glare or external light interference.
Light Emission vs. Perceptual Brightness
To understand gamma, we must first distinguish between the physical amount of light emitted by a screen and how we perceive that light.
The Linear Experiment
Imagine an image consisting of vertical grey bars. In Figure 1, the amount of light energy increases by a constant, linear amount from one bar to the next.
# Conceptual Nim code for generating linear bars
for i in 0..255:
setPixel(i, color = RGB(i, i, i))
Now, look at Figure 2, where the bars are spaced differently.
Question: Which image looks like a smoother, more even gradation from black to white?
The Human Element
You will likely find that the second image looks more "linear" to your eyes. This is because human vision is non-linear. We do not perceive light intensity in a straight line; instead, our senses follow a power law.
| Type of Linearity | Description | Mathematical Nature |
|---|---|---|
| Physical | The actual energy emitted by the hardware. | Linear ( is constant) |
| Perceptual | How the human brain interprets that energy. | Non-linear (Power Law) |
In the first image, the physical difference between any two bars is constant:
However, because our eyes are more sensitive to changes in dark tones than in bright tones, a constant physical increase doesn't look constant. To make a gradient appear linear to a human, the physical light must increase according to a power law.
Visualizing the Flow
Physical vs. Perceptual Linearity
If we wanted to digitally capture a real-world object—such as a perfectly smooth grey gradient—we would encounter a conflict between how the world works and how we see it.
(The article continues to explore how this discrepancy leads to the need for gamma encoding and decoding...)