-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLFontBitmap.cs
More file actions
323 lines (251 loc) · 10.9 KB
/
GLFontBitmap.cs
File metadata and controls
323 lines (251 loc) · 10.9 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace GLGUI
{
public class GLFontBitmap
{
public Bitmap bitmap;
public BitmapData bitmapData;
public GLFontBitmap(string filePath)
{
bitmap = new Bitmap(filePath);
bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
}
public GLFontBitmap(Bitmap bitmap)
{
this.bitmap = bitmap;
bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
}
public void Clear32(byte r, byte g, byte b, byte a)
{
unsafe
{
byte* sourcePtr = (byte*)(bitmapData.Scan0);
for (int i = 0; i < bitmapData.Height; i++)
{
for (int j = 0; j < bitmapData.Width; j++)
{
*(sourcePtr) = b;
*(sourcePtr + 1) = g;
*(sourcePtr + 2) = r;
*(sourcePtr + 3) = a;
sourcePtr += 4;
}
sourcePtr += bitmapData.Stride - bitmapData.Width * 4; //move to the end of the line (past unused space)
}
}
}
/// <summary>
/// Returns try if the given pixel is empty (i.e. black)
/// </summary>
public static unsafe bool EmptyPixel(BitmapData bitmapData, int px, int py)
{
byte* addr = (byte*)(bitmapData.Scan0) + bitmapData.Stride * py + px * 3;
return (*addr == 0 && *(addr + 1) == 0 && *(addr + 2) == 0);
}
/// <summary>
/// Returns try if the given pixel is empty (i.e. alpha is zero)
/// </summary>
public static unsafe bool EmptyAlphaPixel(BitmapData bitmapData, int px, int py, byte alphaEmptyPixelTolerance)
{
byte* addr = (byte*)(bitmapData.Scan0) + bitmapData.Stride * py + px * 4;
return (*(addr + 3) <= alphaEmptyPixelTolerance);
}
/// <summary>
/// Blits a block of a bitmap data from source to destination, using the luminance of the source to determine the
/// alpha of the target. Source must be 24-bit, target must be 32-bit.
/// </summary>
public static void BlitMask(BitmapData source, BitmapData target, int srcPx, int srcPy, int srcW, int srcH, int px, int py)
{
int sourceBpp = 3;
int targetBpp = 4;
int targetStartX, targetEndX;
int targetStartY, targetEndY;
int copyW, copyH;
targetStartX = Math.Max(px, 0);
targetEndX = Math.Min(px + srcW, target.Width);
targetStartY = Math.Max(py, 0);
targetEndY = Math.Min(py + srcH, target.Height);
copyW = targetEndX - targetStartX;
copyH = targetEndY - targetStartY;
if (copyW < 0)
{
return;
}
if (copyH < 0)
{
return;
}
int sourceStartX = srcPx + targetStartX - px;
int sourceStartY = srcPy + targetStartY - py;
unsafe
{
byte* sourcePtr = (byte*)(source.Scan0);
byte* targetPtr = (byte*)(target.Scan0);
byte* targetY = targetPtr + targetStartY * target.Stride;
byte* sourceY = sourcePtr + sourceStartY * source.Stride;
for (int y = 0; y < copyH; y++, targetY += target.Stride, sourceY += source.Stride)
{
byte* targetOffset = targetY + targetStartX * targetBpp;
byte* sourceOffset = sourceY + sourceStartX * sourceBpp;
for (int x = 0; x < copyW; x++, targetOffset += targetBpp, sourceOffset += sourceBpp)
{
int lume = *(sourceOffset) + *(sourceOffset + 1) + *(sourceOffset + 2);
lume /= 3;
if (lume > 255)
lume = 255;
*(targetOffset + 3) = (byte)lume;
}
}
}
}
/// <summary>
/// Blits from source to target. Both source and target must be 32-bit
/// </summary>
public static void Blit(BitmapData source, BitmapData target, Rectangle sourceRect, int px, int py)
{
Blit(source, target, sourceRect.X, sourceRect.Y, sourceRect.Width, sourceRect.Height, px, py);
}
/// <summary>
/// Blits from source to target. Both source and target must be 32-bit
/// </summary>
public static void Blit(BitmapData source, BitmapData target, int srcPx, int srcPy, int srcW, int srcH, int destX, int destY)
{
int bpp = 4;
int targetStartX, targetEndX;
int targetStartY, targetEndY;
int copyW, copyH;
targetStartX = Math.Max(destX, 0);
targetEndX = Math.Min(destX + srcW, target.Width);
targetStartY = Math.Max(destY, 0);
targetEndY = Math.Min(destY + srcH, target.Height);
copyW = targetEndX - targetStartX;
copyH = targetEndY - targetStartY;
if (copyW < 0)
{
return;
}
if (copyH < 0)
{
return;
}
int sourceStartX = srcPx + targetStartX - destX;
int sourceStartY = srcPy + targetStartY - destY;
unsafe
{
byte* sourcePtr = (byte*)(source.Scan0);
byte* targetPtr = (byte*)(target.Scan0);
byte* targetY = targetPtr + targetStartY * target.Stride;
byte* sourceY = sourcePtr + sourceStartY * source.Stride;
for (int y = 0; y < copyH; y++, targetY += target.Stride, sourceY += source.Stride)
{
byte* targetOffset = targetY + targetStartX * bpp;
byte* sourceOffset = sourceY + sourceStartX * bpp;
for (int x = 0; x < copyW*bpp; x++, targetOffset ++, sourceOffset ++)
*(targetOffset) = *(sourceOffset);
}
}
}
public unsafe void PutPixel32(int px, int py, byte r, byte g, byte b, byte a)
{
byte* addr = (byte*)(bitmapData.Scan0) + bitmapData.Stride * py + px * 4;
*addr = b;
*(addr + 1) = g;
*(addr + 2) = r;
*(addr + 3) = a;
}
public unsafe void GetPixel32(int px, int py, ref byte r, ref byte g, ref byte b, ref byte a)
{
byte* addr = (byte*)(bitmapData.Scan0) + bitmapData.Stride * py + px * 4;
b = *addr;
g = *(addr + 1);
r = *(addr + 2);
a = *(addr + 3);
}
public void DownScale32(int newWidth, int newHeight)
{
GLFontBitmap newBitmap = new GLFontBitmap(new Bitmap(newWidth, newHeight, bitmap.PixelFormat));
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
throw new Exception("DownsScale32 only works on 32 bit images");
float xscale = (float)bitmapData.Width / newWidth;
float yscale = (float)bitmapData.Height / newHeight;
byte r = 0, g = 0, b = 0, a = 0;
float summedR = 0f;
float summedG = 0f;
float summedB = 0f;
float summedA = 0f;
int left, right, top, bottom; //the area of old pixels covered by the new bitmap
float targetStartX, targetEndX;
float targetStartY, targetEndY;
float leftF, rightF, topF, bottomF; //edges of new pixel in old pixel coords
float weight;
float weightScale = xscale * yscale;
float totalColourWeight = 0f;
for (int m = 0; m < newHeight; m++)
{
for (int n = 0; n < newWidth; n++)
{
leftF = n * xscale;
rightF = (n + 1) * xscale;
topF = m * yscale;
bottomF = (m + 1) * yscale;
left = (int)leftF;
right = (int)rightF;
top = (int)topF;
bottom = (int)bottomF;
if (left < 0) left = 0;
if (top < 0) top = 0;
if (right >= bitmapData.Width) right = bitmapData.Width - 1;
if (bottom >= bitmapData.Height) bottom = bitmapData.Height - 1;
summedR = 0f;
summedG = 0f;
summedB = 0f;
summedA = 0f;
totalColourWeight = 0f;
for (int j = top; j <= bottom; j++)
{
for (int i = left; i <= right; i++)
{
targetStartX = Math.Max(leftF, i);
targetEndX = Math.Min(rightF, i + 1);
targetStartY = Math.Max(topF, j);
targetEndY = Math.Min(bottomF, j + 1);
weight = (targetEndX - targetStartX) * (targetEndY - targetStartY);
GetPixel32(i, j, ref r, ref g, ref b, ref a);
summedA += weight * a;
if (a != 0)
{
summedR += weight * r;
summedG += weight * g;
summedB += weight * b;
totalColourWeight += weight;
}
}
}
summedR /= totalColourWeight;
summedG /= totalColourWeight;
summedB /= totalColourWeight;
summedA /= weightScale;
if (summedR < 0) summedR = 0f;
if (summedG < 0) summedG = 0f;
if (summedB < 0) summedB = 0f;
if (summedA < 0) summedA = 0f;
if (summedR >= 256) summedR = 255;
if (summedG >= 256) summedG = 255;
if (summedB >= 256) summedB = 255;
if (summedA >= 256) summedA = 255;
newBitmap.PutPixel32(n, m, (byte)summedR, (byte)summedG, (byte)summedB, (byte)summedA);
}
}
this.Free();
this.bitmap = newBitmap.bitmap;
this.bitmapData = newBitmap.bitmapData;
}
public void Free()
{
bitmap.UnlockBits(bitmapData);
bitmap.Dispose();
}
}
}