Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Convert RGB to Gray Scale without using Pointers

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Jan 2012CPOL 14.5K   1   5
You shouldn't rely on methods from the .NET classes (Bitmap::SetPixel, Bitmap::GetPixel, Color::FromArgb) in the inner loop as these are called intensively, million times per image, and their implementation are opaque to you.If their implementation is opaque to the compiler too, no code...

  1. You shouldn't rely on methods from the .NET classes (Bitmap::SetPixel, Bitmap::GetPixel, Color::FromArgb) in the inner loop as these are called intensively, million times per image, and their implementation are opaque to you.
  2. If their implementation is opaque to the compiler too, no code optimization can be done via inlining.


  3. Doing the linear combinations using floating-point arithmetic is overkill. Fixed-point is good enough. You will be penalized by the overhead of data type conversion and floating-point arithmetic.
  4. What's so scary with pointers ? Is the code below less readable?
  5. C#
    void GrayConversion()
    {
        Bitmap BMP = (Bitmap)pictureBox1.Image; // BMP is the source image
        int Width = BMP.Width, Height = BMP.Height;
        Bitmap GRAY = new Bitmap(Width, Height); // GRAY is the resultant image
    
        // Access the bitmap data
        BitmapData BMPData = BMP.LockBits(new Rectangle(0, 0, Width, Height), 
                   ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        BitmapData GRAYData = GRAY.LockBits(new Rectangle(0, 0, Width, Height), 
                   ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
    
        // Loop on the rows
        for (int i = 0; i < Height; i++)
        {
            unsafe
            {
                // Start-of-row addresses
                byte* Bmp = (byte*)BMPData.Scan0 + i * BMPData.Stride;
                byte* Gray = (byte*)GRAYData.Scan0 + i * GRAYData.Stride;
    
                // Loop on the pixels
                for (int j = 0; j < Width; j++, Bmp += 3, Gray += 3)
                {
                    // Extract the luminance of a source pixel using 14 bits fixed-point
                    byte Y = (byte)((4897 * Bmp[2] + 9617 * Bmp[1] + 1868 * Bmp[0]) >> 14);
    
                    // Assign it to the corresponding destination pixel
                    Gray[2] = Gray[1] = Gray[0] = Y;
                }
            }
        }
    
        // Release
        BMP.UnlockBits(BMPData);
        GRAY.UnlockBits(GRAYData);
    
        picGray.Image = GRAY;
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO VISION fOr VISION
Belgium Belgium
I fell into applied algorithmics at the age of 16 or so. This eventually brought me to develop machine vision software as a professional. This is Dreamland for algorithm lovers.

Comments and Discussions

 
GeneralYou are welcome. For the sake of readability, you can map a... Pin
YvesDaoust17-Jan-12 21:05
YvesDaoust17-Jan-12 21:05 
GeneralHi! Thanks for this. This is really something I missed out. ... Pin
Grasshopper.iics17-Jan-12 18:07
Grasshopper.iics17-Jan-12 18:07 
GeneralDISCLAIMER: someone inserted spaces on the left of the assig... Pin
YvesDaoust17-Jan-12 9:21
YvesDaoust17-Jan-12 9:21 
GeneralJust a note: you are naming your local variables starting wi... Pin
JoseMenendez17-Jan-12 8:22
JoseMenendez17-Jan-12 8:22 
GeneralRe: next time i'll sta Pin
YvesDaoust17-Jan-12 9:14
YvesDaoust17-Jan-12 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.