Bitmap Gamma - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Bitmap Gamma

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System;/*from w  ww. ja  v  a2 s .co m*/

public class Main{
        public static bool Gamma(Bitmap b, double red, double green, double blue)
      {
         if (red < .2 || red > 5) return false;
         if (green < .2 || green > 5) return false;
         if (blue < .2 || blue > 5) return false;

         byte [] redGamma = new byte [256];
         byte [] greenGamma = new byte [256];
         byte [] blueGamma = new byte [256];

         for (int i = 0; i< 256; ++i)
         {
            redGamma[i] = (byte)Math.Min(255, (int)(( 255.0 * Math.Pow(i/255.0, 1.0/red)) + 0.5));
            greenGamma[i] = (byte)Math.Min(255, (int)(( 255.0 * Math.Pow(i/255.0, 1.0/green)) + 0.5));
            blueGamma[i] = (byte)Math.Min(255, (int)(( 255.0 * Math.Pow(i/255.0, 1.0/blue)) + 0.5));
         }

         // GDI+ still lies to us - the return format is BGR, NOT RGB.
         BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

         int stride = bmData.Stride;
         System.IntPtr Scan0 = bmData.Scan0;

         unsafe
         {
            byte * p = (byte *)(void *)Scan0;

            int nOffset = stride - b.Width*3;

            for(int y=0;y<b.Height;++y)
            {
               for(int x=0; x < b.Width; ++x )
               {
                  p[2] = redGamma[ p[2] ];
                  p[1] = greenGamma[ p[1] ];
                  p[0] = blueGamma[ p[0] ];

                  p += 3;
               }
               p += nOffset;
            }
         }

         b.UnlockBits(bmData);

         return true;
      }
}

Related Tutorials