Adjusting Gamma of an Image in C#

Gamma correction of an image.
Apr 23 2009 by James Craig

I covered how to adjust the brightness of an image last week. Today I'm going to talk about a subject that is very similar but at the same time fairly different: Gamma. Gamma determines the intensity of the light produced by a CRT... You see back in the day of CRT TVs (early portion of it anyway as this was discovered fairly early), it was thought that the intensity of the color produced was proportional to the input voltage. However that wasn't the case. Instead they found that TVs that used same voltage showed colors in completely different intensities. What it turned out is that the intensity was dictated by the input voltage raised to the power of gamma. Gamma being pretty much being a random (as far as your concerned) number between .2 and 5, and usually centering around 2.5. What that means is your green, while a nice shade of green, may not be as green as my green, which is totally greener...

In order to combat this, most devices use gamma correction. This is done by creating an array of values that have the "corrected" colors as they should be displayed. So for instance if you have a pixel with a green value of 3, you'd look in the array in the third slot to see what value it should really be. Right about now you're wishing I'd shut up and show you how to implement it already, so here you go:

 /// <summary>
/// Adjusts the Gamma
/// </summary>
/// <param name="OriginalImage">Image to change</param>
/// <param name="Value">Used to build the gamma ramp (usually .2 to 5)</param>
/// <returns>A bitmap object</returns>
public static Bitmap AdjustGamma(Bitmap OriginalImage, float Value)
{
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
BitmapData NewData = Image.LockImage(NewBitmap);
BitmapData OldData = Image.LockImage(OriginalImage);
int NewPixelSize = Image.GetPixelSize(NewData);
int OldPixelSize = Image.GetPixelSize(OldData);

int\[\] RedRamp = new int\[256\];
int\[\] GreenRamp = new int\[256\];
int\[\] BlueRamp = new int\[256\];
for (int x = 0; x < 256; ++x)
{
RedRamp\[x\] = MathHelper.Clamp((int)((255.0 \* System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
GreenRamp\[x\] = MathHelper.Clamp((int)((255.0 \* System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
BlueRamp\[x\] = MathHelper.Clamp((int)((255.0 \* System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
}

for (int x = 0; x < NewBitmap.Width; ++x)
{
for (int y = 0; y < NewBitmap.Height; ++y)
{
Color Pixel = Image.GetPixel(OldData, x, y, OldPixelSize);
int Red = RedRamp\[Pixel.R\];
int Green = GreenRamp\[Pixel.G\];
int Blue = BlueRamp\[Pixel.B\];
Image.SetPixel(NewData, x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);
}
}

Image.UnlockImage(NewBitmap, NewData);
Image.UnlockImage(OriginalImage, OldData);
return NewBitmap;
}

Note that the code above uses a couple of functions from my utility library, mainly Image.LockImage, UnlockImage, etc. Most of them can be removed or replaced with the built in GetPixel/SetPixel functions (or you can simply download my library and get a ton of extra code along with it). Anyway, the formula that is used is fairly simple to determine the value in the arrays:

 Value=((x/255)^(1/InputValue))+0.5

Where x/255 simulates the voltage (if we were talking about CRTs) and 1/InputValue being the gamma. You may also notice that it calls a function called Clamp:

 private static int Clamp(int Value, int Max, int Min)
{
Value = Value > Max ? Max : Value;
Value = Value < Min ? Min : Value;
return Value;
}

The clamp function just makes sure that we're between 0 and 255 (inclusive). Anyway, that's all there is to implementing it. Just call it, sending in your image and the gamma value (once again, your best results will be between .2 and 5). The function will in turn send back a new, gamma corrected image. So try it out, leave feedback, and happy coding.