Photoshop gamma correction shaderJanuary 22, 2009
After reproducing contrast, hue, saturation, brightness controls of Photoshop in pixel shaders, here is the gamma correction filter 🙂
There are 2 ways of changing gamma in Photoshop:
- Image | Adjustments | Exposure…
- Image | Adjustments | Levels… (or CTRL+L) and then move the midtone slider.
Gamma correction is not the same thing than Brightness at all, even if it can give the impression it is. For example here is the histogram of my original image:
Then after setting the gamma to 0.5 (it compresses the highlights and stretches the shadows):
And here it is after lowering the brightness (Image | Adjustments | Brightness/Contrast…):
Here you can see it clipped the values after some threshold in the shadows (and also in the highlights) and you’re loosing a lot of lighting information in this case. That’s actually why I wanted to have also a gamma control in my post-processing effects.
A little macro:
// Gamma from 9.99 to 0.1
#define GammaCorrection(color, gamma) pow(color, 1.0 / (gamma))
color = GammaCorrection(color, 0.1);
Here are the curves it produces with extreme values (limits of Photoshop [9.99, 0.1]):
So you see it stays in the same range and give a non-linear luminance (if correction value is different than 1). By the way I used this awesome web function grapher here.
By zooming and taking a very close look I noticed a few tiny differences with Photoshop in the shadows (seriously you need to toggle screenshot from shader and photoshopped image quickly and scan the image to see where it’s not the same). Photoshop on the left, shader version on the right (gamma = 0.1).
I also saw that the gamma in Photoshop (on the left) produced some banding artefacts somewhere, and it didn’t in my shader (on the right), well.. :):
I added the code in my Photoshop Math (GLSL/HLSL) shaders.