Implementation of 2D to 1D Array Formula in Image Processing

public void Invert(ref Bitmap bmp)
{

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

int total_size = newData.Stride * newData.Height;
byte[] g_PixBytes = new byte[total_size + 1];
byte[] g_OutBytes = new byte[total_size + 1];

Marshal.Copy(newData.Scan0, g_PixBytes, 0, total_size);

 

for (int y = 0; y < bmp.Height; y++)
{

for (int x = 0; x < bmp.Width; x++)
{

int pix = y * newData.Stride + 3 * x;               //2D to 1D Array Formula

g_OutBytes[pix + 2] = (byte)(255 – g_PixBytes[pix + 2]);
g_OutBytes[pix + 1] = (byte)(255 – g_PixBytes[pix + 1]);
g_OutBytes[pix + 0] = (byte)(255 – g_PixBytes[pix + 0]);
}
}

Marshal.Copy(g_OutBytes, 0, newData.Scan0, total_size);

bmp.UnlockBits(newData);

g_PixBytes = null;
g_OutBytes = null;

}

//Simple image invert filter by C# programming language.

Updated: —

Leave a Reply

Your email address will not be published. Required fields are marked *