h1

Photoshop gamma correction shader

January 22, 2009

After reproducing contrast, hue, saturation, brightness controls of Photoshop in pixel shaders, here is the gamma correction filter 🙂

gamma-correction-photoshop-levels

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:

gamma-correction-levels-before

Then after setting the gamma to 0.5 (it compresses the highlights and stretches the shadows):

gamma-correction-levels-after

And here it is after lowering the brightness (Image | Adjustments | Brightness/Contrast…):

gamma-correction-levels-after-brightness

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]):

gamma-correction-curves_tn

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).

gamma-correction-comparison

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.. :):

gamma-correction-comparison2

I added the code in my Photoshop Math (GLSL/HLSL) shaders.

Advertisement

9 comments

  1. Thanks!


  2. Thank you, some very useful, not to mention rare information.


  3. Hey, where’s the code, actually? Thanks!


  4. Just take your final color and run it through a pow(…) function . . .


  5. I am trying to implement gamma correction in Matlab.
    Would you elaborate more on the definition you’ve provided? Thank you.


  6. In MATLAB:
    pixels.^gamma


  7. Alright, thanks all! Actually, I found that imadjust in Matlab does all the magic.


  8. 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!


  9. […] -photoshop-levels mouaif.wordpress.com […]



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: