-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBitmapDrawing.cs
More file actions
417 lines (348 loc) · 13.8 KB
/
BitmapDrawing.cs
File metadata and controls
417 lines (348 loc) · 13.8 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
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
using System;
using UnityEngine;
using System.Collections.Generic;
/**
The MIT License (MIT)
Copyright (c) 2014 Lauri Hosio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace ProtoTurtle.BitmapDrawing
{
/// <summary>
/// A extension class for UnityEngine.Texture2D that provides a bitmap drawing API.
/// Provides drawing methods line Line, Circle, Rectangle etc.
/// This class uses a convention of having the 0,0 point at the left-top corner!</summary>
/// <remarks>
/// Check this out for more cool middleware stuff:
/// http://prototurtle.com
///
/// </remarks>
public static class BitmapDrawingExtensions
{
/// <summary>
/// Draws a pixel just like SetPixel except 0,0 is the left top corner.</summary>
public static void DrawPixel(this Texture2D texture, int x, int y, Color color)
{
if (x < 0 || x > texture.width || y < 0 || y > texture.height)
{
return;
}
texture.SetPixel(x, TransformToLeftTop_y(y, texture.height), color);
}
/// <summary>
/// Draws a pixel just like SetPixel except 0,0 is the left top corner.
/// Takes the width and height as parameters - faster for calling this in a loop.
/// </summary>
/// <param name="width">Width of the target bitmap</param>
/// <param name="height">Height of the target bitmap</param>
public static void DrawPixel(this Texture2D texture, int x, int y, int width, int height, Color color)
{
if (x < 0 || x > width || y < 0 || y > height)
{
return;
}
texture.SetPixel(x, TransformToLeftTop_y(y, height), color);
}
/// <summary>
/// Draws a circle with the midpoint being x0, x1.
/// Implementation of Bresenham's circle algorithm
/// </summary>
public static void DrawCircle(this Texture2D texture, int x, int y, int radius, Color color)
{
Circle(texture, x, y, radius, color, false);
}
/// <summary>
/// Draws a filled circle with the midpoint being x0, x1.
/// Implementation of Bresenham's circle algorithm
/// </summary>
public static void DrawFilledCircle(this Texture2D texture, int x, int y, int radius, Color color)
{
Circle(texture, x, y, radius, color, true);
}
private static void Circle(Texture2D texture, int x, int y, int radius, Color color, bool filled = false)
{
int cx = radius;
int cy = 0;
int radiusError = 1 - cx;
while (cx >= cy)
{
if (!filled)
{
PlotCircle(texture, cx, x, cy, y, color);
}
else
{
ScanLineCircle(texture, cx, x, cy, y, color);
}
cy++;
if (radiusError < 0)
{
radiusError += 2 * cy + 1;
}
else
{
cx--;
radiusError += 2 * (cy - cx + 1);
}
}
}
private static void PlotCircle(Texture2D texture, int cx, int x, int cy, int y, Color color)
{
texture.DrawPixel(cx + x, cy + y, color); // Point in octant 1...
texture.DrawPixel(cy + x, cx + y, color);
texture.DrawPixel(-cx + x, cy + y, color);
texture.DrawPixel(-cy + x, cx + y, color);
texture.DrawPixel(-cx + x, -cy + y, color);
texture.DrawPixel(-cy + x, -cx + y, color);
texture.DrawPixel(cx + x, -cy + y, color);
texture.DrawPixel(cy + x, -cx + y, color); // ... point in octant 8
}
// Draw scanlines from opposite sides of the circle on y-scanlines instead of just plotting pixels
// at the right coordinates
private static void ScanLineCircle(Texture2D texture, int cx, int x, int cy, int y, Color color)
{
//texture.DrawPixel(cx + x, cy + y, color);
//texture.DrawPixel(-cx + x, cy + y, color);
texture.DrawLine(cx + x, cy + y, -cx + x, cy + y, color);
//texture.DrawPixel(cy + x, cx + y, color);
//texture.DrawPixel(-cy + x, cx + y, color);
texture.DrawLine(cy + x, cx + y, -cy + x, cx + y, color);
//texture.DrawPixel(-cx + x, -cy + y, color);
//texture.DrawPixel(cx + x, -cy + y, color);
texture.DrawLine(-cx + x, -cy + y, cx + x, -cy + y, color);
//texture.DrawPixel(-cy + x, -cx + y, color);
//texture.DrawPixel(cy + x, -cx + y, color);
texture.DrawLine(-cy + x, -cx + y, cy + x, -cx + y, color);
}
/// <summary>
/// Starts a flood fill at point startX, startY.
/// This is a pretty slow flood fill, biggest bottle neck is comparing two colors which happens
/// a lot. Should be a way to make it much faster.
/// O(n) space. n = width*height - makes a copy of the bitmap temporarily in the memory
/// </summary>
/// <param name="texture"></param>
/// <param name="startX"></param>
/// <param name="startY"></param>
/// <param name="newColor"></param>
public static void FloodFill(this Texture2D texture, int startX, int startY, Color newColor)
{
Point start = new Point(startX, TransformToLeftTop_y(startY, texture.height));
Flat2DArray copyBmp = new Flat2DArray(texture.height, texture.width, texture.GetPixels());
Color originalColor = texture.GetPixel(start.X, start.Y);
int width = texture.width;
int height = texture.height;
if (originalColor == newColor)
{
return;
}
copyBmp[start.X, start.Y] = newColor;
Queue<Point> openNodes = new Queue<Point>();
openNodes.Enqueue(start);
int i = 0;
// TODO: remove this
// emergency switch so it doesn't hang if something goes wrong
int emergency = width*height;
while (openNodes.Count > 0)
{
i++;
if (i > emergency)
{
return;
}
Point current = openNodes.Dequeue();
int x = current.X;
int y = current.Y;
if (x > 0)
{
if (copyBmp[x - 1, y] == originalColor)
{
copyBmp[x - 1, y] = newColor;
openNodes.Enqueue(new Point(x - 1, y));
}
}
if (x < width - 1)
{
if (copyBmp[x + 1, y] == originalColor)
{
copyBmp[x + 1, y] = newColor;
openNodes.Enqueue(new Point(x + 1, y));
}
}
if (y > 0)
{
if (copyBmp[x, y - 1] == originalColor)
{
copyBmp[x, y - 1] = newColor;
openNodes.Enqueue(new Point(x, y - 1));
}
}
if (y < height - 1)
{
if (copyBmp[x, y + 1] == originalColor)
{
copyBmp[x, y + 1] = newColor;
openNodes.Enqueue(new Point(x, y + 1));
}
}
}
texture.SetPixels(copyBmp.data);
}
// Could be its own file
private class Flat2DArray
{
public Color[] data;
private readonly int height;
private readonly int width;
public Flat2DArray(int height, int width, Color[] data)
{
this.height = height;
this.width = width;
this.data = data;
}
public Color this[int x, int y]
{
get
{
return data[x + y * width];
}
set
{
data[x + y * width] = value;
}
}
}
private struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
}
/// <summary>
/// Draws a rectangle
/// </summary>
public static void DrawRectangle(this Texture2D texture, Rect rectangle, Color color)
{
int x = (int)rectangle.x;
int y = (int)rectangle.y;
int height = (int)rectangle.height;
int width = (int)rectangle.width;
// top left to bottom left
texture.DrawLine(x, y, x, y + height, color);
// bottom left to bottom right
texture.DrawLine(x, y + height, x + width, y + height, color);
// bottom right to top right
texture.DrawLine(x + width, y + height, x + width, y, color);
// top right to top left
texture.DrawLine(x + width, y, x, y, color);
}
/// <summary>
/// Fills the given rectangle area with a solid color.</summary>
public static void DrawFilledRectangle(this Texture2D texture, Rect rectangle, Color color)
{
Color[] colorsArray = new Color[(int)rectangle.width * (int)rectangle.height];
for (int i = 0; i < colorsArray.Length; i++)
{
colorsArray[i] = color;
}
int transformedY = TransformToLeftTop_y(rectangle.y, texture.height) - (int)rectangle.height;
texture.SetPixels((int)rectangle.x, transformedY,
(int)rectangle.width, (int)rectangle.height, colorsArray);
}
public static void DrawLine(this Texture2D texture, Vector3 start, Vector3 end, Color color)
{
Line(texture, (int)start.x, (int)start.y, (int)end.x, (int)end.y, color);
}
public static void DrawLine(this Texture2D texture, Vector2 start, Vector2 end, Color color)
{
Line(texture, (int)start.x, (int)start.y, (int)end.x, (int)end.y, color);
}
/// <summary>
/// Draws a line between two points. Implementation of Bresenham's line algorithm.
/// </summary>
/// <param name="x0">x of the start point</param>
/// <param name="y0">y of the start point</param>
/// <param name="x1">x of the end point</param>
/// <param name="y1">y of the end point</param>
public static void DrawLine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color)
{
Line(texture, x0, y0, x1, y1, color);
}
private static void Line(Texture2D texture, int x0, int y0, int x1, int y1, Color color)
{
int width = texture.width;
int height = texture.height;
bool isSteep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
if (isSteep)
{
Swap(ref x0, ref y0);
Swap(ref x1, ref y1);
}
if (x0 > x1)
{
Swap(ref x0, ref x1);
Swap(ref y0, ref y1);
}
int deltax = x1 - x0;
int deltay = Math.Abs(y1 - y0);
int error = deltax / 2;
int ystep;
int y = y0;
if (y0 < y1)
ystep = 1;
else
ystep = -1;
for (int x = x0; x < x1; x++)
{
if (isSteep)
texture.DrawPixel(y, x, width, height, color);
else
texture.DrawPixel(x, y, width, height, color);
error = error - deltay;
if (error < 0)
{
y = y + ystep;
error = error + deltax;
}
}
}
/// <summary>
/// Swap two ints by reference.
/// </summary>
private static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
/// <summary>
/// Transforms a point in the texture plane so that 0,0 points at left-top corner.</summary>
private static int TransformToLeftTop_y(int y, int height)
{
return height - y;
}
/// <summary>
/// Transforms a point in the texture plane so that 0,0 points at left-top corner.</summary>
private static int TransformToLeftTop_y(float y, int height)
{
return height - (int)y;
}
}
}