Gamma
This algorithm performs gamma correction of specified image in RGB color space.
public static void ApplyGamma(ref Bitmap bmp, double redComponent, double greenComponent, double blueComponent)
{
if (redComponent < 0.2 || redComponent > 5) return;
if (greenComponent < 0.2 || greenComponent > 5) return;
if (blueComponent < 0.2 || blueComponent > 5) return;
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
int stopAddress = (int)ptr + bmpData.Stride * bmpData.Height;
while ((int)ptr != stopAddress)
{
ptr[0] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(ptr[0] / 255.0, 1.0 / blueComponent)) + 0.5));
ptr[1] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(ptr[1] / 255.0, 1.0 / greenComponent)) + 0.5));
ptr[2] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(ptr[2] / 255.0, 1.0 / redComponent)) + 0.5));
ptr += 3;
}
}
bmp.UnlockBits(bmpData);
}
Example
Bitmap b = (Bitmap)Image.FromFile("rose.jpg");
ApplyGamma(ref b, 2, 1.6, 0.8);