I'm trying to edit pixels of a Bitmap
(960x510), and I'm using the fastest method I could find. But it's still painfully slow (7 FPS).
unsafe
{
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
int bytesPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
byte* PtrFirstPixel = (byte*)bitmapData.Scan0;
for(int y = 0; y < heightInPixels; y++)
{
byte* currentLine = PtrFirstPixel + (y * bitmapData.Stride);
for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
{
for (int i = 0; i < Shaders.Count; i++)
{
rgba color = new rgba(currentLine[x], currentLine[x + 1], currentLine[x + 2], -1);
if (bytesPerPixel == 4) color.a = currentLine[x + 3];
Shaders[i].pixel(new xy(x * bytesPerPixel % bmp.Width, x * bytesPerPixel / bmp.Height), color);
currentLine[x] = (byte)color.r;
currentLine[x + 1] = (byte)color.g;
currentLine[x + 2] = (byte)color.b;
if (bytesPerPixel == 4) currentLine[x + 3] = (byte)color.a;
}
}
}
bmp.UnlockBits(bitmapData);
}
The method inside Shaders[i].pixel
changes the color.r
, color.g
, color.b
and color.a
values.
How can I increase performance?