C# Tutorial: How To Use Gamma Correction


Andraz Krzisnik
C# Tutorial: How To Use Gamma Correction

Gamma correction is a process, which allows you to set the brightness of screen. You can often run into this setting in video games, where you set the brightness of your screen based on an image. This image is usually just an intensity ramp or a logo, for which you set the brightness, so you can just barely see it.
Gamma correction works by changing the values of each pixel individually. Equation by which the values are transformed is, what we call, a power law equation.

Equation looks as following

Gamma correction equation

S represents a certain pixel in the output image. C is a constant, that is commonly set to 1. R represents a pixel in the input image, which is in the same position as S pixel. And finally γ (gamma) exponent is the value we are most interested in, as it is responsible for changing the brightness of our image.
If we set gamma to be less than 1 (e.g. 0.3, 0.5, 0.04), then our output image will appear brighter as before. And in the case we set it to be more than 1 (e.g. 1.5, 2.2, 25), our output image will become darker.

Gamma correction function in C#

private Bitmap GammaCorrection(Bitmap img, double gamma, double c = 1d)
{
    int width = img.Width;
    int height = img.Height;
    BitmapData srcData = img.LockBits(new Rectangle(0, 0, width, height),
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    int bytes = srcData.Stride * srcData.Height;
    byte[] buffer = new byte[bytes];
    byte[] result = new byte[bytes];
    Marshal.Copy(srcData.Scan0, buffer, 0, bytes);
    img.UnlockBits(srcData);
    int current = 0;
    int cChannels = 3;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            current = y * srcData.Stride + x * 4;
            for (int i = 0; i < cChannels; i++)
            {
                double range = (double)buffer[current + i] / 255;
                double correction = c * Math.Pow(range, gamma);
                result[current + i] = (byte)(correction * 255);
            }
            result[current + 3] = 255;
        }
    }
    Bitmap resImg = new Bitmap(width, height);
    BitmapData resData = resImg.LockBits(new Rectangle(0, 0, width, height),
        ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    Marshal.Copy(result, 0, resData.Scan0, bytes);
    resImg.UnlockBits(resData);
    return resImg;
}

Entire project is available for download

Download Project

Example

 

Show Comments (24)

Comments

  • http://ker.li/livestreaming445519

    https://epochabuse.com is very informative, bookmarked

    • Article Author
  • cheap bouncy castles to buy

    Great weblog here! Additionally your website lots up fast!
    What host are you the usage of? Can I am getting your affiliate hyperlink
    for your host? I want my site loaded up as fast as
    yours lol

    • Article Author
  • test

    Hi there it’s me, I am also visiting this site daily, this web
    site is genuinely good and the visitors are actually sharing good thoughts.

    • Article Author

Related Articles

Morphological Processes

How To Make Thinning In Image Processing Work With C#

Thinning is a morphological operation in image processing, which eats away at the foregorund pixels and leaves only lines shaping objects.

Posted on by Andraz Krzisnik
Color Image Processing

How To Use Color Slicing In Image Processing With C#

Color slicing is a color image processing technique, which only shows colors in a certain color space making objects stand out.

Posted on by Andraz Krzisnik