
Photoshop gamma correction shader
January 22, 2009After reproducing contrast, hue, saturation, brightness controls of Photoshop in pixel shaders, here is the gamma correction filter 🙂
Photoshop
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.
Shader
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.
Thanks!
Thank you, some very useful, not to mention rare information.
Hey, where’s the code, actually? Thanks!
Just take your final color and run it through a pow(…) function . . .
I am trying to implement gamma correction in Matlab.
Would you elaborate more on the definition you’ve provided? Thank you.
In MATLAB:
pixels.^gamma
Alright, thanks all! Actually, I found that imadjust in Matlab does all the magic.
hey guys, does anyone have an idea how those calculation could be applied when working in L*a*b* mode? check out this formula for reference:
http://www.vbaccelerator.com/home/vb/code/vbmedia/image_processing/Gamma_Correction/article.asp
thanks!
[…] -photoshop-levels mouaif.wordpress.com […]