-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBmpProc.cs
executable file
·186 lines (180 loc) · 5.72 KB
/
BmpProc.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Image Processing Utilities for C#2.0 (VC# 2005)
*
* Copyright junki, Jan, 2006 -
*
* http://junki.main.jp/
* http://code.junki.main.jp/
*
* how to use : see http://junki.main.jp/csgr/006Library1.htm
*
* This library is free for any non commercial usage. In the case of
* modifying and/or redistributing the code, it's obligate to retain
* the original copyright notice.
*
* Moified by Aont 08/14
* http://d.hatena.ne.jp/aont/
*/
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
namespace Aont
{
public unsafe class BmpProc : IDisposable
{
bool flagDispose = false;
protected Bitmap bitmap;
public readonly int Width, Height;
protected BitmapData bmpData;
protected byte* Scan0;
protected int Stride;
public readonly int DataLength;
public readonly PixelFormat Format;
protected BmpProc(Bitmap bitmap, PixelFormat Format)
{
this.bitmap = bitmap;
Width = bitmap.Width;
Height = bitmap.Height;
Rectangle rect = new Rectangle(0, 0, Width, Height);
bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, Format);
Scan0 = (byte*)bmpData.Scan0;
Stride = Math.Abs(bmpData.Stride);
DataLength = Stride * Height;
}
protected virtual void Dispose(bool flag)
{
if (!flagDispose)
{
if (flag)
{
bitmap.UnlockBits(bmpData);
}
this.flagDispose = true;
}
}
public virtual Color GetPixel(int x, int y) { throw new NotImplementedException(); }
public virtual void SetPixel(int x, int y, Color color) { throw new NotImplementedException(); }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~BmpProc()
{
Dispose(false);
}
}
public unsafe class BmpProc32 : BmpProc
{
public BmpProc32(Bitmap bitmap) : base(bitmap, PixelFormat.Format32bppArgb) { }
public Color this[int x, int y]
{
get
{
return Color.FromArgb(*(Int32*)&Scan0[Stride * y + x * 4]);
}
set
{
*(Int32*)&Scan0[Stride * y + x * 4] = value.ToArgb();
}
}
public override Color GetPixel(int x, int y)
{ return this[x, y]; }
public override void SetPixel(int x, int y, Color color)
{ this[x, y] = color; }
}
public unsafe class BmpProc24 : BmpProc
{
public BmpProc24(Bitmap bitmap) : base(bitmap, PixelFormat.Format24bppRgb) { }
public Color this[int x, int y]
{
get
{
byte* PixelB = &Scan0[Stride * y + x * 3];
return Color.FromArgb(PixelB[2], PixelB[1], *PixelB);
}
set
{
byte* PixelB = &Scan0[Stride * y + x * 3];
*PixelB = value.B;
PixelB[1] = value.G;
PixelB[2] = value.R;
}
}
public override Color GetPixel(int x, int y)
{ return this[x, y]; }
public override void SetPixel(int x, int y, Color color)
{ this[x, y] = color; }
}
public unsafe class BmpProc8 : BmpProc
{
public Color[] Palette { get { return bitmap.Palette.Entries; } }
public BmpProc8(Bitmap bitmap) : base(bitmap, PixelFormat.Format8bppIndexed) { }
public byte this[int x, int y]
{
get { return Scan0[Stride * y + x]; }
set { Scan0[Stride * y + x] = value; }
}
public byte this[int index]
{
get { return Scan0[index]; }
set { Scan0[index] = value; }
}
public override Color GetPixel(int x, int y)
{
return Palette[this[x, y]];
}
public override void SetPixel(int x, int y, Color color)
{
int i;
for (i = 0; i < 256; ++i)
{
if (Palette[i] == color)
break;
}
if (i < 256)
{
this[x, y] = (byte)i;
}
}
}
public unsafe class BmpProc1 : BmpProc
{
public BmpProc1(Bitmap bitmap) : base(bitmap, PixelFormat.Format1bppIndexed) { }
public bool this[int x, int y]
{
get
{
int xr;
int ofs = Stride * y + Math.DivRem(x, 8, out xr);
return ((Scan0[ofs] >> (7 - xr) & 1) == 1);
}
set
{
int xr;
int ofs = Stride * y + Math.DivRem(x, 8, out xr);
if (value)
Scan0[ofs] = (byte)(Scan0[ofs] | (1 << (7 - xr)));
else
Scan0[ofs] = (byte)(Scan0[ofs] & (~(1 << (7 - xr))));
}
}
public bool this[int index]
{
get { return ((*(Scan0 + index / 8) >> (7 - (index % 8)) & 1) == 1); }
set
{
int xr;
int ofs = Math.DivRem(index, 8, out xr);
if (value)
Scan0[ofs] = (byte)(Scan0[ofs] | (1 << (7 - xr)));
else
Scan0[ofs] = (byte)(Scan0[ofs] & (~(1 << (7 - xr))));
}
}
}
}