Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective way of making negative of image without external dlls

That is the solution to make a negative from a image in C# Windows Forms without any dlls and in a effective, fast way?

like image 638
Tom Smykowski Avatar asked Dec 09 '22 21:12

Tom Smykowski


1 Answers

The best way to do this is directly accessing the pixels with bitmap data.

Just to add some timing details:

Performing Negate on an 8 Megapixel Image (on a 2.4 Ghz Core 2 Duo):

  • SetPixel (~22 seconds) - 220 Times slower
  • Color Matrix, Matajon's method below (~750 milliseconds) - 7 times slower
  • Directly accesing the bitmap data (~100 milliseconds) - Fastest

So, if you can't have unsafe code, then Color Matrix is much better than SetPixel.

    public static void Negate(Bitmap image)
    {
        const int RED_PIXEL = 2;
        const int GREEN_PIXEL = 1;
        const int BLUE_PIXEL = 0;


        BitmapData bmData = currentImage.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);

        try
        {
            int stride = bmData.Stride;
            int bytesPerPixel = (currentImage.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4);

            unsafe
            {
                byte* pixel = (byte*)(void*)bmData.Scan0;
                int yMax = image.Height;
                int xMax = image.Width;

                for (int y = 0; y < yMax; y++)
                {
                    int yPos = y * stride;
                    for (int x = areaSize.X; x < xMax; x++)
                    {
                        int pos = yPos + (x * bytesPerPixel);

                        pixel[pos + RED_PIXEL] = (byte)(255 - pixel[pos + RED_PIXEL]);
                        pixel[pos + GREEN_PIXEL] = (byte)(255 - pixel[pos + GREEN_PIXEL]);
                        pixel[pos + BLUE_PIXEL] = (byte)(255 - pixel[pos + BLUE_PIXEL]);                                                    
                    }

                }
            }
        }
        finally
        {
            image.UnlockBits(bmData);
        }

    }

If you are interested, here is the code for Color Matrix:

    public static void Negate(Bitmap image)
    {

            Bitmap clone = (Bitmap) image.Clone();

            using (Graphics g = Graphics.FromImage(image))
            {

                // negation ColorMatrix
                ColorMatrix colorMatrix = new ColorMatrix(
                    new float[][]
                        {
                            new float[] {-1, 0, 0, 0, 0},
                            new float[] {0, -1, 0, 0, 0},
                            new float[] {0, 0, -1, 0, 0},
                            new float[] {0, 0, 0, 1, 0},
                            new float[] {0, 0, 0, 0, 1}
                        });

                ImageAttributes attributes = new ImageAttributes();

                attributes.SetColorMatrix(colorMatrix);

                g.DrawImage(clone, new Rectangle(0, 0, clone.Width, clone.Height),
                            0, 0, clone.Width, clone.Height, GraphicsUnit.Pixel, attributes);
           }
    }
like image 198
Kris Erickson Avatar answered Feb 15 '23 09:02

Kris Erickson