Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Created April 3, 2017 18:40
Show Gist options
  • Save Ni55aN/6d843ea8655928d967d8663372865e6a to your computer and use it in GitHub Desktop.
Save Ni55aN/6d843ea8655928d967d8663372865e6a to your computer and use it in GitHub Desktop.
delegate Color pixelMap(Color c);
delegate void pixelEach(Color c);
abstract class MyBitmap
{
protected Bitmap bmp;
public abstract Color getPixel(int x, int y);
public abstract void setPixel(int x, int y, Color color);
public abstract void Save();
public void each(pixelEach proc)
{
for (int i = 0; i < bmp.Width; ++i)
for (int j = 0; j < bmp.Height; ++j)
{
proc(getPixel(i, j));
}
}
public void map(pixelMap proc)
{
for (int i = 0; i < bmp.Width; ++i)
for (int j = 0; j < bmp.Height; ++j)
{
Color newval = proc(getPixel(i, j));
setPixel(i, j, newval);
}
}
}
unsafe class FastBitmap : MyBitmap
{
byte* imagePointer;
BitmapData data;
public FastBitmap(Bitmap bmp)
{
this.bmp = bmp;
data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
imagePointer = (byte*)data.Scan0;
}
public override Color getPixel(int x, int y)
{
return Color.FromArgb(imagePointer[data.Stride * y + x * 4], imagePointer[data.Stride * y + x * 4 + 1], imagePointer[data.Stride * y + x * 4 + 2]);
}
public override void setPixel(int x, int y, Color color)
{
imagePointer[data.Stride * y + x * 4] = color.R;
imagePointer[data.Stride * y + x * 4 + 1] = color.G;
imagePointer[data.Stride * y + x * 4 + 2] = color.B;
}
public override void Save()
{
bmp.UnlockBits(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment