Created
April 3, 2017 18:40
-
-
Save Ni55aN/6d843ea8655928d967d8663372865e6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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