Search code examples
c#multithreadingimageimage-processinggrayscale

Convert an image to grayscale parallel loop


I have written a code that converts image to grayscale. but the code only convert partial of it. I am trying to convert this code to a Parallel computation. I end up with bugs that I can not get my head around them. Any suggestion?

    private void button2_Click(object sender, EventArgs e)
    {

        Bitmap bmp = (Bitmap)pictureBox1.Image;
        unsafe {
            //get image dimension
            //int width = bmp.Width;
            //int height = bmp.Height;


            BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            //define variable
            int bpp = System.Drawing.Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
            int hip = bitmapData.Height;
            int wib = bitmapData.Width + bpp;

            //point to first pixel
            byte* PtrFirstPixel = (byte*)bitmapData.Scan0;
            //color of pixel
           // Color p;

            //grayscale

            Parallel.For(0, hip, y =>
            {
                byte* currentLine = PtrFirstPixel + (y * bitmapData.Stride);
                for (int x = 0; x < wib; x = x + bpp)
                {
                    //get pixel value
                    //p = bmp.GetPixel(x, y);

                    //extract pixel component ARGB
                    //int a = p.A;
                    //int r = p.R;
                    //int g = p.G;
                    // int b = p.B;
                    int b = currentLine[x];
                    int g = currentLine[x + 1];
                    int r = currentLine[x + 2];



                    //find average
                    int avg = (r + g + b) / 3;

                    //set new pixel value
                    // bmp.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg));
                    currentLine[x] = (byte)avg;
                    currentLine[x + 1] = (byte)avg;
                    currentLine[x + 2] = (byte)avg;


                }


            });

            bmp.UnlockBits(bitmapData);



            //load grayscale image in picturebox2
            //pictureBox2.Image = bmp;




        }
        pictureBox2.Image = bmp;

    }

my out put image


Solution

  • int wib = bitmapData.Width + bpp;
    

    should be:

    int wib = bitmapData.Width * bpp;
    

    You want the number of bytes which requires a multiply, not an add. There may be other issues, but this is definitely incorrect.