Image scaling and gamma correction

Generally speaking, if you want good results when you scale an image, you should do your calculations using numbers that are related in a linear way to the true brightness of the samples (as in number of photons per second), rather than the apparent brightness (as in this color looks about twice as bright as that color).

The problem is, the numbers usually used to encode sample values (e.g. the [0 to 255] values available on standard computer monitors) do not meet this criterion.

This is not a simple thing to explain, and Eric Brasseur has already done of good job of it, so I’m not going to duplicate his efforts.

The rule is seemingly simple: always convert the samples to a linear colorspace before doing image processing. My ImageWorsener utility does that automatically.

OriginalCorrect gammaIncorrect gamma

Interactive demo



Catch #1

The biggest problem with using correct gamma is that it can be much slower. The exponential functions involved are relatively difficult for computers to calculate, and may have gotchas. Using lookup tables can help a lot, but you may have to trade some accuracy for speed. And partial transparency can be a problem if you’re using a graphics subsystem that doesn’t know how to process it correctly.

Catch #2

Images of dark text or line drawings, on a light background, appear darker if you use incorrect gamma. That’s usually a desirable quality. Even though the incorrect image may not represent the original image as faithfully, it can sometimes still look better.

Correct gammaIncorrect gamma

Interactive demo

Catch #3

Consider this simple image of a dark blue square. Let’s enlarge it the wrong way and the right way, and compare:

OriginalCorrect gammaIncorrect gamma

To make the problem more obvious, I used a slightly unusual resampling filter: an 8-lobed Lanczos filter.

When we resize it correctly, the “ringing” artifacts in the light colored region are slightly lessened, but the artifacts in dark square are much worse! Did we do something wrong?

No; that really is what can happen when you resize an image according to the prevailing best practices.

As far as I know, there is no simple solution to this problem. The real problem is that the mathematical formulas typically used for resampling have no knowledge of how the human visual system works. They don’t “know” that differences in brightness are far more visible in dark-colored regions than in light-colored regions. We’d need to have the algorithm take that fact into account at a fundmental level. I suppose that such algorithms do exist, but that’s getting beyond my level of knowledge.

You might be thinking that maybe the “incorrect” way is actually the correct way to enlarge an image, because the numbers used correspond well to human perception of brightness. Let’s test that theory. Here’s an image I made using Eric’s gamma grayator utility, enlarged using both methods:

OriginalCorrect gammaIncorrect gamma

In this case it’s clear that the “correct” image really is the correct one. The incorrect one is too dull and gray. So, unfortunately, that theory is false, and the problem remains.

Opinions differ on whether you should use a linear colorspace when enlarging images. I would lean toward using it when possible, because it’s more theoretically correct. But I can’t deny that, when increasing the size by a large amount, it often makes images look worse to the human eye.