What every coder should know about gamma | John NovakTable of contents<br>A short quiz<br>If you have ever written, or are planning to write, any kind of code that<br>deals with image processing, you should complete the below quiz. If you have answered<br>one or more questions with a yes, there’s a high chance that your code is<br>doing the wrong thing and will produce incorrect results. This might not be<br>immediately obvious to you because these issues can be subtle and they’re<br>easier to spot in some problem domains than in others.<br>So here’s the quiz:<br>I don’t know what gamma correction is (duh!)<br>Gamma is a relic from the CRT display era; now that almost everyone uses<br>LCDs, it’s safe to ignore it.<br>Gamma is only relevant for graphics professionals working in the print<br>industry where accurate colour reproduction is of great<br>importance—for general image processing, it’s safe to ignore it.<br>I’m a game developer, I don’t need to know about gamma.<br>The graphics libraries of my operating system handle gamma correctly.1<br>The popular graphics library I’m using handles<br>gamma correctly.<br>Pixels with RGB values of (128, 128, 128) emit about half as much light as<br>pixels with RGB values of (255, 255, 255).<br>It is okay to just load pixel data from a popular image format (JPEG, PNG,<br>GIF etc.) into a buffer using some random library and run image processing<br>algorithms on the raw data directly.<br>Don’t feel bad if you have answered most with a yes! I would have given<br>a yes to most of these questions a week ago myself too. Somehow, the topic<br>of gamma is just under most computer users' radar (including programmers<br>writing commercial graphics software!), to the extent that most graphics<br>libraries, image viewers, photo editors and drawing software of today still<br>don’t get gamma right and produce incorrect results.<br>So keep on reading, and by the end of this article you’ll be more<br>knowledgeable about gamma than the vast majority of programmers!<br>The arcane art of gamma-correctness<br>Given that vision is arguably the most important sensory input channel for<br>human-computer interaction, it is quite surprising that gamma correction is<br>one of the least talked about subjects among programmers and it’s mentioned in<br>technical literature rather infrequently, including computer graphics texts.<br>The fact that most computer graphics textbooks don’t explicitly mention the<br>importance of correct gamma handling, or discuss it in practical terms, does<br>not help matters at all (my CG textbook from<br>uni falls squarely into<br>this category, I’ve just checked). Some books mention gamma correction in<br>passing in somewhat vague and abstract terms, but then provide neither<br>concrete real-world examples on how to do it properly, nor explain what the<br>implications of not doing it properly are, nor show image examples of<br>incorrect gamma handling.<br>I came across the need for correct gamma handling during writing my ray<br>tracer and I had to admit that my understanding of the<br>topic was rather superficial and incomplete. So I had spent a few days reading<br>up on it online, but it turned out that many articles about gamma are not much<br>help either, as many of them are too abstract and confusing, some contain too<br>many interesting but otherwise irrelevant details, and then some others lack<br>image examples or are just simply incorrect or hard to understand. Gamma is<br>not a terribly difficult concept to begin with, but for some mysterious reason<br>it’s not that trivial to find articles on it that are correct, complete and<br>explain the topic in a clear language.<br>What is gamma and why do we need it?<br>Alright, so this is my attempt to offer a comprehensive explanation of gamma,<br>focusing just on the most important aspects and assuming no prior knowledge of<br>it.<br>The image examples in this article assume that you are viewing this web page in<br>a modern browser on a computer monitor (CRT or LCD, doesn’t matter). Tablets<br>and phones are generally quite inaccurate compared to monitors, so try to<br>avoid those. You should be viewing the images in a dimly lit room, so no<br>direct lights or flare on your screen please.<br>Light emission vs perceptual brightness<br>Believe it or not, the difference of light energy emission between any two<br>neighbouring vertical bars in the below image is a constant. In other words,<br>the amount of light energy emitted by your screen increases by a constant<br>amount from bar to bar, left to right.<br>Figure 1 — Evenly-spaced greyscale bars in terms of emitted light<br>intensity (Nim source code)Now consider the following image:<br>Figure 2 — Evenly-spaced greyscale bars in terms of perceptual light<br>intensity (Nim source code)On which image does the gradation appear more even? It’s the second one! But<br>why is that so? We have just established that in the first image the bars are<br>evenly (linearly) spaced in terms of emitted light intensity between the<br>darkest black and brightest white your monitor is capable of...