-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFastBitmap.cs
89 lines (67 loc) · 2.15 KB
/
FastBitmap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace GodLesZ.Library {
public unsafe class FastBitmap : IDisposable {
public struct PixelData {
public byte blue;
public byte green;
public byte red;
public byte alpha;
public override string ToString() {
return "(" + alpha.ToString() + ", " + red.ToString() + ", " + green.ToString() + ", " + blue.ToString() + ")";
}
}
protected Bitmap mWorkingBitmap = null;
protected int mWidth = 0;
protected BitmapData mBitmapData = null;
protected byte* mPointer = null;
protected PixelData* mCurrentPixelData = null;
public byte* Pointer {
get { return mPointer; }
}
public FastBitmap(Bitmap inputBitmap)
: this(inputBitmap, false) {
}
public FastBitmap(Bitmap inputBitmap, bool lockImage) {
mWorkingBitmap = inputBitmap;
if (lockImage == true) {
LockImage();
}
}
public void Dispose() {
UnlockImage();
}
public void LockImage() {
Rectangle bounds = new Rectangle(Point.Empty, mWorkingBitmap.Size);
mWidth = (int)(bounds.Width * sizeof(PixelData));
if (mWidth % 4 != 0)
mWidth = 4 * (mWidth / 4 + 1);
// Lock Image
mBitmapData = mWorkingBitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
mPointer = (Byte*)mBitmapData.Scan0.ToPointer();
}
public void UnlockImage() {
if (mBitmapData != null) {
mWorkingBitmap.UnlockBits(mBitmapData);
mBitmapData = null;
mPointer = null;
}
}
public Color GetPixel(int x, int y) {
mCurrentPixelData = (PixelData*)(Pointer + y * mWidth + x * sizeof(PixelData));
return Color.FromArgb(mCurrentPixelData->alpha, mCurrentPixelData->red, mCurrentPixelData->green, mCurrentPixelData->blue);
}
public Color GetPixelNext() {
mCurrentPixelData++;
return Color.FromArgb(mCurrentPixelData->alpha, mCurrentPixelData->red, mCurrentPixelData->green, mCurrentPixelData->blue);
}
public void SetPixel(int x, int y, Color color) {
PixelData* data = (PixelData*)(Pointer + (y * mWidth) + (x * sizeof(PixelData)));
data->alpha = color.A;
data->red = color.R;
data->green = color.G;
data->blue = color.B;
}
}
}