-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphics.cs
502 lines (435 loc) · 21.4 KB
/
Graphics.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Drawing;
using System.IO;
using System.Text;
using Cosmos.Core;
using Cosmos.System.Graphics;
namespace CosmosKernel1
{
unsafe class Graphics
{
public int[] raw;
public int width, height;
public int* address;
const int bytesPerPixel = 4;
static int transparent = Color.Transparent.ToArgb();
static string ASC16Base64 = "AAAAAAAAAAAAAAAAAAAAAAAAfoGlgYG9mYGBfgAAAAAAAH7/2///w+f//34AAAAAAAAAAGz+/v7+fDgQAAAAAAAAAAAQOHz+fDgQAAAAAAAAAAAYPDzn5+cYGDwAAAAAAAAAGDx+//9+GBg8AAAAAAAAAAAAABg8PBgAAAAAAAD////////nw8Pn////////AAAAAAA8ZkJCZjwAAAAAAP//////w5m9vZnD//////8AAB4OGjJ4zMzMzHgAAAAAAAA8ZmZmZjwYfhgYAAAAAAAAPzM/MDAwMHDw4AAAAAAAAH9jf2NjY2Nn5+bAAAAAAAAAGBjbPOc82xgYAAAAAACAwODw+P748ODAgAAAAAAAAgYOHj7+Ph4OBgIAAAAAAAAYPH4YGBh+PBgAAAAAAAAAZmZmZmZmZgBmZgAAAAAAAH/b29t7GxsbGxsAAAAAAHzGYDhsxsZsOAzGfAAAAAAAAAAAAAAA/v7+/gAAAAAAABg8fhgYGH48GH4AAAAAAAAYPH4YGBgYGBgYAAAAAAAAGBgYGBgYGH48GAAAAAAAAAAAABgM/gwYAAAAAAAAAAAAAAAwYP5gMAAAAAAAAAAAAAAAAMDAwP4AAAAAAAAAAAAAAChs/mwoAAAAAAAAAAAAABA4OHx8/v4AAAAAAAAAAAD+/nx8ODgQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYPDw8GBgYABgYAAAAAABmZmYkAAAAAAAAAAAAAAAAAABsbP5sbGz+bGwAAAAAGBh8xsLAfAYGhsZ8GBgAAAAAAADCxgwYMGDGhgAAAAAAADhsbDh23MzMzHYAAAAAADAwMGAAAAAAAAAAAAAAAAAADBgwMDAwM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H7b29t+AAAAAAAAAAAAAwZ+29vzfmDAAAAAAAAAHDBgYHxgYGAwHAAAAAAAAAB8xsbGxsbGxsYAAAAAAAAAAP4AAP4AAP4AAAAAAAAAAAAYGH4YGAAA/wAAAAAAAAAwGAwGDBgwAH4AAAAAAAAADBgwYDAYDAB+AAAAAAAADhsbGBgYGBgYGBgYGBgYGBgYGBgYGNjY2HAAAAAAAAAAABgYAH4AGBgAAAAAAAAAAAAAdtwAdtwAAAAAAAAAOGxsOAAAAAAAAAAAAAAAAAAAAAAAABgYAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAADwwMDAwM7GxsPBwAAAAAANhsbGxsbAAAAAAAAAAAAABw2DBgyPgAAAAAAAAAAAAAAAAAfHx8fHx8fAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";
static MemoryStream ASC16FontMS = new MemoryStream(Convert.FromBase64String(ASC16Base64));
public Graphics(int width, int height)
{
raw = new int[width * height];
fixed (int* pointer = raw)
{
address = pointer;
}
this.width = width;
this.height = height;
}
public void Clear(int color)
{
MemoryOperations.Fill((byte*)address, color, raw.Length * bytesPerPixel);
}
public void DrawImage(int x, int y, Image image)
{
fixed (int* imageRawAddress = image.rawData)
{
for (int h = 0; h < image.Height; h++)
{
if (x >= 0)
{
int w = (int)image.Width;
if (x + image.Width > width)
{
w = width - x;
}
MemoryOperations.Copy(address + (width * (h + y)) + x, imageRawAddress + (image.Width * h), w);
}
else
{
if (image.Width + x >= 0)
{
MemoryOperations.Copy(address + (width * (h + y)), imageRawAddress + (image.Width * h + x), (int)image.Width + x);
}
}
}
}
}
public void DrawImageWithAlpha(int x, int y, Image image)
{
for (int h = 0; h < image.Height; h++)
{
for (int w = 0; w < image.Width; w++)
{
//可能会导致错误,65536>>8 = 256,比255多1。
//No Colorspace
Color foreground = Color.FromArgb(image.rawData[image.Width * h + w]);
Color background = Color.FromArgb(GetPixel(x + w, y + h));
int alpha = foreground.A;
int inv_alpha = 255 - alpha;
int newR = (foreground.R * alpha + inv_alpha * background.R) >> 8;
int newG = (foreground.G * alpha + inv_alpha * background.G) >> 8;
int newB = (foreground.B * alpha + inv_alpha * background.B) >> 8;
Color newColor = Color.FromArgb(newR, newB, newG);
if (foreground.A != 0)
{
SetPixel(x + w, y + h, newColor.ToArgb());
}
}
}
}
public void DrawMyImage(string path)
{
}
public void SetPixel(int x, int y, int color)
{
if (x >= 0 && x <= width && y >= 0 && y <= height)
{
raw[width * y + x] = color;
}
}
public int GetPixel(int x, int y)
{
if (x >= 0 && x <= width && y >= 0 && y <= height)
{
return raw[width * y + x];
}
return transparent;
}
public void CopyFromGraphics(Graphics graphics, int x, int y)
{
for (int h = 0; h < height; h++)
{
int w = width;
if (graphics.width - x > width)
{
w = graphics.width - x;
}
MemoryOperations.Copy(address + width * h, graphics.address + graphics.width * (h + y) + x, w);
}
}
public void Blur(int intensity)
{
int[] _raw = raw;
for (int w = 0; w < width; w++)
{
for (int h = 0; h < height; h++)
{
long r = 0, g = 0, b = 0, a = 0;
int counter = 0;
for (int ww = w - intensity; ww < w + intensity; ww++)
{
for (int hh = h - intensity; hh < h + intensity; hh++)
{
if (ww >= 0 && hh >= 0 && ww < width && hh < height)
{
Color color = Color.FromArgb(_raw[width * hh + ww]);
r += color.R;
g += color.G;
b += color.B;
a += color.A;
counter++;
}
}
}
r /= counter;
g /= counter;
b /= counter;
a /= counter;
SetPixel(w, h, Color.FromArgb((int)a, (int)r, (int)g, (int)b).ToArgb());
}
}
}
public void DrawACSIIString(int x, int y, string s, int color)
{
string[] lines = s.Split('\n');
for (int l = 0; l < lines.Length; l++)
{
for (int c = 0; c < lines[l].Length; c++)
{
int offset = (Encoding.ASCII.GetBytes(lines[l][c].ToString())[0] & 0xFF) * 16;
ASC16FontMS.Seek(offset, SeekOrigin.Begin);
byte[] fontbuf = new byte[16];
ASC16FontMS.Read(fontbuf, 0, fontbuf.Length);
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 8; j++)
{
if ((fontbuf[i] & (0x80 >> j)) != 0)
{
SetPixel((x + j) + (c * 8), y + i + (l * 16), color);
}
}
}
}
}
}
public void Fill(int x, int y, int width, int height, int color)
{
for (int h = 0; h < height; h++)
{
if (x >= 0)
{
int w = width;
if (x + width > this.width)
{
w = this.width - x;
}
MemoryOperations.Fill(address + (this.width * (h + y)) + x, color, w);
}
else
{
if (width + x >= 0)
{
MemoryOperations.Fill(address + (this.width * (h + y)), color, width + x);
}
}
}
}
public void Flush(int x, int y, uint videoWidth, uint videoHeight, MemoryBlock videoMemory)
{
for (int h = 0; h < height; h++)
{
int offset = (int)((y + h) * videoWidth * bytesPerPixel + x * bytesPerPixel);
int w = width;
if (x + w > videoWidth)
{
w = (int)(videoWidth - x);
}
videoMemory.Copy(offset, raw, width * h, w);
}
}
public void DrawCircle(int x_center, int y_center, int radius, int color)
{
/*
ThrowIfCoordNotValid(x_center + radius, y_center);
ThrowIfCoordNotValid(x_center - radius, y_center);
ThrowIfCoordNotValid(x_center, y_center + radius);
ThrowIfCoordNotValid(x_center, y_center - radius);
*/
int x = radius;
int y = 0;
int e = 0;
while (x >= y)
{
SetPixel(x_center + x, y_center + y, color);
SetPixel(x_center + y, y_center + x, color);
SetPixel(x_center - y, y_center + x, color);
SetPixel(x_center - x, y_center + y, color);
SetPixel(x_center - x, y_center - y, color);
SetPixel(x_center - y, y_center - x, color);
SetPixel(x_center + y, y_center - x, color);
SetPixel(x_center + x, y_center - y, color);
y++;
if (e <= 0)
{
e += 2 * y + 1;
}
if (e > 0)
{
x--;
e -= 2 * x + 1;
}
}
}
public void DrawRectangle(int x, int y, int width, int height, int color)
{
/*
* we must draw four lines connecting any vertex of our rectangle to do this we first obtain the position of these
* vertex (we call these vertexes A, B, C, D as for geometric convention)
*/
/* The check of the validity of x and y are done in DrawLine() */
/* The vertex A is where x,y are */
int xa = x;
int ya = y;
/* The vertex B has the same y coordinate of A but x is moved of width pixels */
int xb = x + width;
int yb = y;
/* The vertex C has the same x coordiate of A but this time is y that is moved of height pixels */
int xc = x;
int yc = y + height;
/* The Vertex D has x moved of width pixels and y moved of height pixels */
int xd = x + width;
int yd = y + height;
/* Draw a line betwen A and B */
DrawLine(xa, ya, xb, yb, color);
/* Draw a line between A and C */
DrawLine(xa, ya, xc, yc, color);
/* Draw a line between B and D */
DrawLine(xb, yb, xd, yd, color);
/* Draw a line between C and D */
//DrawLine(xc, yc, xd, yd, color);
DrawLine(xc, yc, xd + 1, yd, color);
}
public void DrawLine(int x1, int y1, int x2, int y2, int color)
{
// trim the given line to fit inside the canvas boundries
TrimLine(ref x1, ref y1, ref x2, ref y2);
int dx, dy;
dx = x2 - x1; /* the horizontal distance of the line */
dy = y2 - y1; /* the vertical distance of the line */
if (dy == 0) /* The line is horizontal */
{
DrawHorizontalLine(color, dx, x1, y1);
/*
int minx = Math.Min(x1, x2);
int maxx = Math.Max(x1, x2);
for (int i = minx; i < maxx; i++)
{
DrawPoint(i, y1, color);
}
*/
return;
}
if (dx == 0) /* the line is vertical */
{
DrawVerticalLine(color, dy, x1, y1);
/*
int miny = Math.Min(y1, y2);
int maxy = Math.Max(y1, y2);
for (int i = miny; i < maxy; i++)
{
DrawPoint(x1, i, color);
}
*/
return;
}
/* the line is neither horizontal neither vertical, is diagonal then! */
DrawDiagonalLine(color, dx, dy, x1, y1);
}
#region DrawLine
private void DrawVerticalLine(int color, int dy, int x1, int y1)
{
int i;
for (i = 0; i < dy; i++)
{
SetPixel(x1, (y1 + i), color);
}
}
private void DrawHorizontalLine(int color, int dx, int x1, int y1)
{
uint i;
for (i = 0; i < dx; i++)
{
SetPixel(((int)(x1 + i)), y1, color);
}
}
protected void TrimLine(ref int x1, ref int y1, ref int x2, ref int y2)
{
// in case of vertical lines, no need to perform complex operations
if (x1 == x2)
{
x1 = Math.Min(width - 1, Math.Max(0, x1));
x2 = x1;
y1 = Math.Min(height - 1, Math.Max(0, y1));
y2 = Math.Min(height - 1, Math.Max(0, y2));
return;
}
// never attempt to remove this part,
// if we didn't calculate our new values as floats, we would end up with inaccurate output
float x1_out = x1, y1_out = y1;
float x2_out = x2, y2_out = y2;
// calculate the line slope, and the entercepted part of the y axis
float m = (y2_out - y1_out) / (x2_out - x1_out);
float c = y1_out - m * x1_out;
// handle x1
if (x1_out < 0)
{
x1_out = 0;
y1_out = c;
}
else if (x1_out >= width)
{
x1_out = width - 1;
y1_out = (width - 1) * m + c;
}
// handle x2
if (x2_out < 0)
{
x2_out = 0;
y2_out = c;
}
else if (x2_out >= width)
{
x2_out = width - 1;
y2_out = (width - 1) * m + c;
}
// handle y1
if (y1_out < 0)
{
x1_out = -c / m;
y1_out = 0;
}
else if (y1_out >= height)
{
x1_out = (height - 1 - c) / m;
y1_out = height - 1;
}
// handle y2
if (y2_out < 0)
{
x2_out = -c / m;
y2_out = 0;
}
else if (y2_out >= height)
{
x2_out = (height - 1 - c) / m;
y2_out = height - 1;
}
// final check, to avoid lines that are totally outside bounds
if (x1_out < 0 || x1_out >= width || y1_out < 0 || y1_out >= height)
{
x1_out = 0; x2_out = 0;
y1_out = 0; y2_out = 0;
}
if (x2_out < 0 || x2_out >= width || y2_out < 0 || y2_out >= height)
{
x1_out = 0; x2_out = 0;
y1_out = 0; y2_out = 0;
}
// replace inputs with new values
x1 = (int)x1_out; y1 = (int)y1_out;
x2 = (int)x2_out; y2 = (int)y2_out;
}
private void DrawDiagonalLine(int color, int dx, int dy, int x1, int y1)
{
int i, sdx, sdy, dxabs, dyabs, x, y, px, py;
dxabs = Math.Abs(dx);
dyabs = Math.Abs(dy);
sdx = Math.Sign(dx);
sdy = Math.Sign(dy);
x = dyabs >> 1;
y = dxabs >> 1;
px = x1;
py = y1;
if (dxabs >= dyabs) /* the line is more horizontal than vertical */
{
for (i = 0; i < dxabs; i++)
{
y += dyabs;
if (y >= dxabs)
{
y -= dxabs;
py += sdy;
}
px += sdx;
SetPixel(px, py, color);
}
}
else /* the line is more vertical than horizontal */
{
for (i = 0; i < dyabs; i++)
{
x += dxabs;
if (x >= dyabs)
{
x -= dyabs;
px += sdx;
}
py += sdy;
SetPixel(px, py, color);
}
}
}
#endregion
}
}