Gamma-correct rendering, part 5

Well, it's almost a year since writing part 4, and at the time I thought that was probably the end of the series. However, a recent CodeProject article prompted me to put fingers to keyboard once more and jot down some thoughts about the effect of gamma on the rendering of straight line segments. As I tried to explain in previous episodes, alpha blending in non-linear sRGB space causes errors that result in darkened, jagged-looking edges. The only way to achieve correct results is to perform the blending calculations in linear RGB space. To avoid noticeable quantization errors, a depth of 16 bits per pixel is required. Otherwise, pixel values must be converted "on the fly" from 8-bit sRGB to 16-bit linear RGB and back when blending.

For this article, I created a demo application to replicate the slanted lines from the AGG introduction and also the "wheel of lines" used in the CodeProject article. The app incorporates a slider to set the gamma level, as well as a checkbox to turn gamma correction on or off. First let's compare the slanted lines. Here's how they look with gamma correction disabled:

Pretty jagged looking, and very similar to the results obtained with OpenGL and Cairo in the CodeProject article. Now, here's how things look with gamma correction enabled:

The line edges certainly look a lot smoother, and overall quite similar to the picture in the AGG introduction. There's no mention of how that image was produced, but I'd guess it was rendered in 16-bit linear RGB then converted to sRGB.

Now let's have a look at the "wheel of lines". On the left is the uncorrected version; on the right is the gamma-correct version.

Notice how the lines in the uncorrected version look "heavy", and the near-horizontal and near-vertical lines are jagged-looking. The corrected version looks far better, though even then the near-horizontal and near-vertical lines may still not look quite right, but this is an inevitable result of trying to render detail at the same scale as the pixel grid. To avoid such grid-alignment artefacts, it may help to apply a mild blur effect to the final rendering, though of course this has a significant computational cost. Increasing the line thickness to 1.5 can improve the appearance considerably.

It's always fun to see the exaggerated effect of gamma-incorrect blending by using fully-saturated colours. If the "Monochrome" checkbox is deselected, the lines are rendered in full-intensity green on a magenta background. With gamma correction off, the results are truly horrific.