I'm working on optimizing a program I'm working on, which currently reads byte data using lock bits, but writes pixel data using setPixel. So how do I actually modify the pixel data that I'm reading in? If I try setting pp, cp, or np, the method won't work (since it loops and needs pp, cp, and np to represent the pixel data), so I'm completely confused. Do I need to write to the pixel data to a byte[] and manipulate it, or what?
Here's a code sample:
BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int scaledPercent = (int)(Math.Round(percentageInt * 255)) - 47;
Debug.WriteLine("percent " + scaledPercent);
unsafe
{
Debug.WriteLine("Woah there, unsafe stuff");
byte* prevLine = (byte*)data.Scan0;
byte* currLine = prevLine + data.Stride;
byte* nextLine = currLine + data.Stride;
for (int y = 1; y < img.Height - 1; y++)
{
byte* pp = prevLine + 3;
byte* cp = currLine + 3;
byte* np = nextLine + 3;
for (int x = 1; x < img.Width - 1; x++)
{
if (IsEdgeOptimized(pp, cp, np, scaledPercent))
{
//Debug.WriteLine("x " + x + "y " + y);
img2.SetPixel(x, y, Color.Black);
}
else
{
img2.SetPixel(x, y, Color.White);
}
pp += 3; cp += 3; np += 3;
}
prevLine = currLine;
currLine = nextLine;
nextLine += data.Stride;
}
}
img.UnlockBits(data);
pictureBox2.Image = img2;