-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageGraphics.cpp
More file actions
377 lines (309 loc) · 10.2 KB
/
ImageGraphics.cpp
File metadata and controls
377 lines (309 loc) · 10.2 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
/*
* SmartMatrix Library - Methods for interacting with background layer
*
* Copyright (c) 2014 Louis Beaudoin (Pixelmatix)
*
* 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.
*
* Code from MatrixGraphics.cpp was modified substantially by Craig A. Lindley for use in the
* Painter application. This code writes into an image data array using color indices instead
* of writing into a backing buffer using rgb24 values.
*/
#include <arduino.h>
const int WIDTH = 32;
const int HEIGHT = 32;
extern byte image[WIDTH][HEIGHT];
void drawPixelIntoImage(int x, int y, byte colorIndex) {
image[x][y] = colorIndex;
}
#define SWAPint(X,Y) { int temp = X; X = Y; Y = temp; }
// x0, x1, and y must be in bounds
void drawHardwareHLine(int x0, int x1, int y, byte colorIndex) {
for (int i = x0; i <= x1; i++) {
image[i][y] = colorIndex;
}
}
// x, y0, and y1 must be in bounds
void drawHardwareVLine(int x, int y0, int y1, byte colorIndex) {
for (int i = y0; i <= y1; i++) {
image[x][i] = colorIndex;
}
}
void drawFastHLine(int x0, int x1, int y, byte colorIndex) {
// make sure line goes from x0 to x1
if (x1 < x0) {
SWAPint(x1, x0);
}
// check for completely out of bounds line
if (x1 < 0 || x0 >= WIDTH || y < 0 || y >= HEIGHT) {
return;
}
// truncate if partially out of bounds
if (x0 < 0) {
x0 = 0;
}
if (x1 >= WIDTH) {
x1 = WIDTH - 1;
}
drawHardwareHLine(x0, x1, y, colorIndex);
}
void drawFastVLine(int x, int y0, int y1, byte colorIndex) {
// make sure line goes from y0 to y1
if (y1 < y0) {
SWAPint(y1, y0);
}
// check for completely out of bounds line
if (y1 < 0 || y0 >= HEIGHT || x < 0 || x >= WIDTH) {
return;
}
// truncate if partially out of bounds
if (y0 < 0) {
y0 = 0;
}
if (y1 >= HEIGHT) {
y1 = HEIGHT - 1;
}
drawHardwareVLine(x, y0, y1, colorIndex);
}
void bresteepline(int x3, int y3, int x4, int y4, byte colorIndex) {
// if point x3, y3 is on the right side of point x4, y4, change them
if ((x3 - x4) > 0) {
bresteepline(x4, y4, x3, y3, colorIndex);
return;
}
int x = x3, y = y3, sum = x4 - x3, Dx = 2 * (x4 - x3), Dy = abs(2 * (y4 - y3));
int prirastokDy = ((y4 - y3) > 0) ? 1 : -1;
for (int i = 0; i <= x4 - x3; i++) {
drawPixelIntoImage(y, x, colorIndex);
x++;
sum -= Dy;
if (sum < 0) {
y = y + prirastokDy;
sum += Dx;
}
}
}
// algorithm from http://www.netgraphics.sk/bresenham-algorithm-for-a-line
void drawLineIntoImage(int x1, int y1, int x2, int y2, byte colorIndex) {
// if point x1, y1 is on the right side of point x2, y2, change them
if ((x1 - x2) > 0) {
drawLineIntoImage(x2, y2, x1, y1, colorIndex);
return;
}
// test inclination of line
// function Math.abs(y) defines absolute value y
if (abs(y2 - y1) > abs(x2 - x1)) {
// line and y axis angle is less then 45 degrees
// that swhy go on the next procedure
bresteepline(y1, x1, y2, x2, colorIndex);
return;
}
// line and x axis angle is less then 45 degrees, so x is guiding
// auxiliary variables
int x = x1, y = y1, sum = x2 - x1, Dx = 2 * (x2 - x1), Dy = abs(2 * (y2 - y1));
int prirastokDy = ((y2 - y1) > 0) ? 1 : -1;
// draw line
for (int i = 0; i <= x2 - x1; i++) {
drawPixelIntoImage(x, y, colorIndex);
x++;
sum -= Dy;
if (sum < 0) {
y = y + prirastokDy;
sum += Dx;
}
}
}
// algorithm from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
void drawCircleIntoImage(int x0, int y0, int radius, byte colorIndex) {
int a = radius, b = 0;
int radiusError = 1 - a;
if (radius == 0)
return;
while (a >= b) {
drawPixelIntoImage(a + x0, b + y0, colorIndex);
drawPixelIntoImage(b + x0, a + y0, colorIndex);
drawPixelIntoImage(-a + x0, b + y0, colorIndex);
drawPixelIntoImage(-b + x0, a + y0, colorIndex);
drawPixelIntoImage(-a + x0, -b + y0, colorIndex);
drawPixelIntoImage(-b + x0, -a + y0, colorIndex);
drawPixelIntoImage(a + x0, -b + y0, colorIndex);
drawPixelIntoImage(b + x0, -a + y0, colorIndex);
b++;
if (radiusError < 0) {
radiusError += 2 * b + 1;
}
else {
a--;
radiusError += 2 * (b - a + 1);
}
}
}
// algorithm from drawCircle rearranged with hlines drawn between points on the raidus
void fillCircleIntoImage(int x0, int y0, int radius, byte colorIndex) {
int a = radius, b = 0;
int radiusError = 1 - a;
if (radius == 0) {
return;
}
// only draw one line per row, skipping the top and bottom
bool hlineDrawn = true;
while (a >= b) {
// this pair sweeps from horizontal center down
drawFastHLine((a - 1) + x0, (-a + 1) + x0, b + y0, colorIndex);
// this pair sweeps from horizontal center up
drawFastHLine((a - 1) + x0, (-a + 1) + x0, -b + y0, colorIndex);
if (b > 1 && !hlineDrawn) {
drawFastHLine((b - 1) + x0, (-b + 1) + x0, a + y0, colorIndex);
drawFastHLine((b - 1) + x0, (-b + 1) + x0, -a + y0, colorIndex);
hlineDrawn = true;
}
b++;
if (radiusError < 0) {
radiusError += 2 * b + 1;
}
else {
a--;
hlineDrawn = false;
radiusError += 2 * (b - a + 1);
}
}
}
void drawRectIntoImage(int x0, int y0, int x1, int y1, byte colorIndex) {
drawFastHLine(x0, x1, y0, colorIndex);
drawFastHLine(x0, x1, y1, colorIndex);
drawFastVLine(x0, y0, y1, colorIndex);
drawFastVLine(x1, y0, y1, colorIndex);
}
void fillRectIntoImage(int x0, int y0, int x1, int y1, byte colorIndex) {
for (int i = y0; i <= y1; i++) {
drawFastHLine(x0, x1, i, colorIndex);
}
}
void drawTriangleIntoImage(int x1, int y1, int x2, int y2, int x3, int y3, byte colorIndex) {
drawLineIntoImage(x1, y1, x2, y2, colorIndex);
drawLineIntoImage(x2, y2, x3, y3, colorIndex);
drawLineIntoImage(x1, y1, x3, y3, colorIndex);
}
// Code from http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
void fillFlatSideTriangleInt(int x1, int y1, int x2, int y2, int x3, int y3, byte colorIndex) {
int t1x, t2x, t1y, t2y;
bool changed1 = false;
bool changed2 = false;
int8_t signx1, signx2, signy1, signy2, dx1, dy1, dx2, dy2;
int i;
int8_t e1, e2;
t1x = t2x = x1;
t1y = t2y = y1; // Starting points
dx1 = abs(x2 - x1);
dy1 = abs(y2 - y1);
dx2 = abs(x3 - x1);
dy2 = abs(y3 - y1);
if (x2 - x1 < 0) {
signx1 = -1;
}
else signx1 = 1;
if (x3 - x1 < 0) {
signx2 = -1;
}
else signx2 = 1;
if (y2 - y1 < 0) {
signy1 = -1;
}
else signy1 = 1;
if (y3 - y1 < 0) {
signy2 = -1;
}
else signy2 = 1;
if (dy1 > dx1) { // swap values
SWAPint(dx1, dy1);
changed1 = true;
}
if (dy2 > dx2) { // swap values
SWAPint(dy2, dx2);
changed2 = true;
}
e1 = 2 * dy1 - dx1;
e2 = 2 * dy2 - dx2;
for (i = 0; i <= dx1; i++)
{
drawFastHLine(t1x, t2x, t1y, colorIndex);
while (e1 >= 0)
{
if (changed1)
t1x += signx1;
else
t1y += signy1;
e1 = e1 - 2 * dx1;
}
if (changed1)
t1y += signy1;
else
t1x += signx1;
e1 = e1 + 2 * dy1;
/* here we rendered the next point on line 1 so follow now line 2
* until we are on the same y-value as line 1.
*/
while (t2y != t1y)
{
while (e2 >= 0)
{
if (changed2)
t2x += signx2;
else
t2y += signy2;
e2 = e2 - 2 * dx2;
}
if (changed2)
t2y += signy2;
else
t2x += signx2;
e2 = e2 + 2 * dy2;
}
}
}
// Code from http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
void fillTriangleIntoImage(int x1, int y1, int x2, int y2, int x3, int y3, byte colorIndex) {
// Sort vertices
if (y1 > y2) {
SWAPint(y1, y2);
SWAPint(x1, x2);
}
if (y1 > y3) {
SWAPint(y1, y3);
SWAPint(x1, x3);
}
if (y2 > y3) {
SWAPint(y2, y3);
SWAPint(x2, x3);
}
if (y2 == y3) {
fillFlatSideTriangleInt(x1, y1, x2, y2, x3, y3, colorIndex);
}
/* check for trivial case of top-flat triangle */
else if (y1 == y2) {
fillFlatSideTriangleInt(x3, y3, x1, y1, x2, y2, colorIndex);
}
else {
/* general case - split the triangle in a topflat and bottom-flat one */
int16_t xtmp, ytmp;
xtmp = (int)(x1 + ((float)(y2 - y1) / (float)(y3 - y1)) * (x3 - x1));
ytmp = y2;
fillFlatSideTriangleInt(x1, y1, x2, y2, xtmp, ytmp, colorIndex);
fillFlatSideTriangleInt(x3, y3, x2, y2, xtmp, ytmp, colorIndex);
}
}