GCC Code Coverage Report


Directory: ../src/
File: extensions/display_extensions.c
Date: 2026-02-08 10:07:39
Coverage Exec Excl Total
Lines: 94.1% 1543 0 1640
Functions: 94.6% 70 0 74
Branches: 80.2% 1026 0 1279

Line Branch Exec Source
1 /*
2 Copyright 2023 - 2025 Benjamin Vedder benjamin@vedder.se
3 Copyright 2023 - 2025 Joel Svensson svenssonjoel@yahoo.se
4 Copyright 2023 Rasmus Söderhielm rasmus.soderhielm@gmail.com
5 Copyright 2025 Joakim Lundborg joakim.lundborg@gmail.com
6
7 This file is part of LispBM. (Originally a part of the vesc_express FW)
8
9 LispBM is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 LispBM is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "tjpgd.h"
24
25 #include <math.h>
26
27 #include <extensions/display_extensions.h>
28 #include <lbm_utils.h>
29 #include <lbm_defrag_mem.h>
30 #include <lbm_cos_table.h>
31
32 #ifdef LBM_OPT_DISPLAY_EXTENSIONS_SIZE
33 #pragma GCC optimize ("-Os")
34 #endif
35 #ifdef LBM_OPT_DISPLAY_EXTENSIONS_SIZE_AGGRESSIVE
36 #pragma GCC optimize ("-Oz")
37 #endif
38
39 #define MAX_WIDTH 32000
40 #define MAX_HEIGHT 32000
41
42 1124 uint32_t lbm_display_rgb888_from_color(color_t color, int x, int y) {
43
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
✗ Branch 2 not taken.
1124 switch (color.type) {
44 ✗ case COLOR_REGULAR:
45 ✗ return (uint32_t)color.color1;
46
47 1124 case COLOR_GRADIENT_X:
48 case COLOR_GRADIENT_Y: {
49 uint32_t res;
50 1124 uint32_t r1 = (uint32_t)color.color1 >> 16;
51 1124 uint32_t g1 = (uint32_t)color.color1 >> 8 & 0xFF;
52 1124 uint32_t b1 = (uint32_t)color.color1 & 0xff;
53
54 1124 uint32_t r2 = (uint32_t)color.color2 >> 16;
55 1124 uint32_t g2 = (uint32_t)color.color2 >> 8 & 0xFF;
56 1124 uint32_t b2 = (uint32_t)color.color2 & 0xff;
57
58
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1074 times.
1124 int used_len = color.mirrored ? 256 : 128;
59
60
2/2
✓ Branch 0 taken 1094 times.
✓ Branch 1 taken 30 times.
1124 int pos = color.type == COLOR_GRADIENT_X ? x : y;
61 // int tab_pos = ((pos * 256) / color.param1 + color.param2) % 256;
62 1124 int tab_pos = (((pos - color.param2) * 256) / color.param1 / 2) % used_len;
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
1124 if (tab_pos < 0) {
64 ✗ tab_pos += used_len;
65 }
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
1124 uint32_t tab_val = (uint32_t)lbm_cos_tab_128[tab_pos <= 127 ? tab_pos : 128 - (tab_pos - 127)];
68
69 1124 uint32_t r = (r1 * tab_val + r2 * (255 - tab_val)) / 255;
70 1124 uint32_t g = (g1 * tab_val + g2 * (255 - tab_val)) / 255;
71 1124 uint32_t b = (b1 * tab_val + b2 * (255 - tab_val)) / 255;
72
73 1124 res = r << 16 | g << 8 | b;
74 1124 return res;
75 }
76
77 ✗ default:
78 ✗ return 0;
79 }
80 }
81
82 static lbm_uint symbol_indexed2 = 0;
83 static lbm_uint symbol_indexed4 = 0;
84 static lbm_uint symbol_indexed16 = 0;
85 static lbm_uint symbol_rgb332 = 0;
86 static lbm_uint symbol_rgb565 = 0;
87 static lbm_uint symbol_rgb888 = 0;
88
89 static lbm_uint symbol_thickness = 0;
90 static lbm_uint symbol_filled = 0;
91 static lbm_uint symbol_rounded = 0;
92 static lbm_uint symbol_dotted = 0;
93 static lbm_uint symbol_scale = 0;
94 static lbm_uint symbol_rotate = 0;
95 static lbm_uint symbol_resolution = 0;
96 static lbm_uint symbol_tile = 0;
97 static lbm_uint symbol_clip = 0;
98
99
100 static lbm_uint symbol_regular = 0;
101 static lbm_uint symbol_gradient_x = 0;
102 static lbm_uint symbol_gradient_y = 0;
103 static lbm_uint symbol_gradient_x_pre = 0;
104 static lbm_uint symbol_gradient_y_pre = 0;
105 static lbm_uint symbol_repeat = 0;
106 static lbm_uint symbol_mirrored = 0;
107
108 static lbm_uint symbol_color_0 = 0;
109 static lbm_uint symbol_color_1 = 0;
110 static lbm_uint symbol_width = 0;
111 static lbm_uint symbol_offset = 0;
112 static lbm_uint symbol_repeat_type = 0;
113
114 static lbm_uint symbol_down = 0;
115 static lbm_uint symbol_up = 0;
116
117 24 bool display_is_symbol_up(lbm_value v) {
118
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 if (lbm_is_symbol(v)) {
119 24 lbm_uint s = lbm_dec_sym(v);
120 24 return (s == symbol_up);
121 }
122 ✗ return false;
123 }
124
125 24 bool display_is_symbol_down(lbm_value v) {
126
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 if (lbm_is_symbol(v)) {
127 24 lbm_uint s = lbm_dec_sym(v);
128 24 return (s == symbol_down);
129 }
130 ✗ return false;
131 }
132
133 91 color_format_t sym_to_color_format(lbm_value v) {
134 91 lbm_uint s = lbm_dec_sym(v);
135
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 65 times.
91 if (s == symbol_indexed2) return indexed2;
136
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 34 times.
65 if (s == symbol_indexed4) return indexed4;
137
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 19 times.
34 if (s == symbol_indexed16) return indexed16;
138
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 if (s == symbol_rgb332) return rgb332;
139
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
15 if (s == symbol_rgb565) return rgb565;
140
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 if (s == symbol_rgb888) return rgb888;
141 3 return format_not_supported;
142 }
143
144 2808 uint32_t image_dims_to_size_bytes(color_format_t fmt, uint16_t width, uint16_t height) {
145 2808 uint32_t num_pix = (uint32_t)width * (uint32_t)height;
146
7/7
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 2627 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 4 times.
2808 switch(fmt) {
147 111 case indexed2:
148
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 54 times.
111 if (num_pix % 8 != 0) return (num_pix / 8) + 1;
149 54 else return (num_pix / 8);
150 break;
151 2627 case indexed4:
152
2/2
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 1894 times.
2627 if (num_pix % 4 != 0) return (num_pix / 4) + 1;
153 1894 else return (num_pix / 4);
154 break;
155 28 case indexed16: // Two pixels per byte
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (num_pix % 2 != 0) return (num_pix / 2) + 1;
157 28 else return (num_pix / 2);
158 8 case rgb332:
159 8 return num_pix;
160 break;
161 6 case rgb565:
162 6 return num_pix * 2;
163 break;
164 24 case rgb888:
165 24 return num_pix * 3;
166 4 default:
167 4 return 0;
168 }
169 }
170
171 70 static lbm_value image_buffer_lift(uint8_t *buf, color_format_t fmt, uint16_t width, uint16_t height) {
172 70 lbm_value res = ENC_SYM_MERROR;
173 70 lbm_uint size = image_dims_to_size_bytes(fmt, width, height);
174
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 if ( lbm_lift_array(&res, (char*)buf, IMAGE_BUFFER_HEADER_SIZE + size)) {
175 70 buf[0] = (uint8_t)(width >> 8);
176 70 buf[1] = (uint8_t)width;
177 70 buf[2] = (uint8_t)(height >> 8);
178 70 buf[3] = (uint8_t)height;
179 70 buf[4] = color_format_to_byte(fmt);
180 }
181 70 return res;
182 }
183
184 147 static inline bool is_color_sized(lbm_uint size) {
185 147 size_t color_size = sizeof(color_t);
186 147 return (size == color_size);
187 }
188
189 71 static inline bool is_color(uint8_t *data, lbm_uint size) {
190 71 bool res = false;
191
1/2
✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
71 if (is_color_sized(size)) {
192 71 color_t *color = (color_t*)data;
193 71 res = (color->magic == COLOR_MAGIC);
194 }
195 71 return res;
196 }
197
198
199 26 static lbm_value color_allocate(COLOR_TYPE type, int32_t color1, int32_t color2, uint16_t param1, uint16_t param2, bool mirrored) {
200 26 color_t *color = lbm_malloc(sizeof(color_t));
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!color) {
202 ✗ return ENC_SYM_MERROR;
203 }
204
205 26 uint32_t *pre = 0;
206
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 21 times.
26 if (type == COLOR_PRE_X || type == COLOR_PRE_Y) {
207 5 pre = lbm_malloc(COLOR_PRECALC_LEN * sizeof(uint32_t));
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!pre) {
209 ✗ lbm_free(color);
210 ✗ return ENC_SYM_MERROR;
211 }
212 }
213
214 26 lbm_value res = ENC_SYM_MERROR;
215
216
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 if (lbm_lift_array(&res, (char*)color, sizeof(color_t))) {
217 26 color->magic = COLOR_MAGIC;
218 26 color->type = type;
219 26 color->color1 = color1;
220 26 color->color2 = color2;
221 26 color->param1 = param1;
222 26 color->param2 = param2;
223 26 color->mirrored = mirrored;
224 26 color->precalc = pre;
225
226
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 21 times.
26 if (pre) {
227 5 COLOR_TYPE type_old = color->type;
228
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (type == COLOR_PRE_X) {
229 4 color->type = COLOR_GRADIENT_X;
230
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 } else if (type == COLOR_PRE_Y) {
231 1 color->type = COLOR_GRADIENT_Y;
232 }
233
234
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (color->param1 > COLOR_PRECALC_LEN) {
235 1 color->param1 = COLOR_PRECALC_LEN;
236 }
237
238
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 5 times.
1129 for (int i = 0;i < color->param1;i++) {
239 1124 pre[i] = lbm_display_rgb888_from_color(*color, i + color->param2, i + color->param2);
240 }
241
242 5 color->type = type_old;
243 }
244 } else {
245 ✗ lbm_free(pre);
246 ✗ lbm_free(color);
247 }
248
249 26 return res;
250 }
251
252 70 static lbm_value image_buffer_allocate(color_format_t fmt, uint16_t width, uint16_t height) {
253 70 uint32_t size_bytes = image_dims_to_size_bytes(fmt, width, height);
254
255 70 uint8_t *buf = lbm_malloc(IMAGE_BUFFER_HEADER_SIZE + size_bytes);
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (!buf) {
257 ✗ return ENC_SYM_MERROR;
258 }
259 70 memset(buf, 0, size_bytes + IMAGE_BUFFER_HEADER_SIZE);
260 70 lbm_value res = image_buffer_lift(buf, fmt, width, height);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
70 if (lbm_is_symbol(res)) { /* something is wrong, free */
262 ✗ lbm_free(buf);
263 }
264 70 return res;
265 }
266
267 1 static lbm_value image_buffer_allocate_dm(lbm_uint *dm, color_format_t fmt, uint16_t width, uint16_t height) {
268 1 uint32_t size_bytes = image_dims_to_size_bytes(fmt, width, height);
269
270 1 lbm_value res = lbm_defrag_mem_alloc(dm, IMAGE_BUFFER_HEADER_SIZE + size_bytes);
271 1 lbm_array_header_t *arr = lbm_dec_array_r(res);
272
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (arr) {
273 1 uint8_t *buf = (uint8_t*)arr->data;
274 1 buf[0] = (uint8_t)(width >> 8);
275 1 buf[1] = (uint8_t)width;
276 1 buf[2] = (uint8_t)(height >> 8);
277 1 buf[3] = (uint8_t)height;
278 1 buf[4] = color_format_to_byte(fmt);
279 }
280 1 return res;
281 }
282
283 // Exported interface
284 ✗ bool display_is_color(lbm_value v) {
285 ✗ lbm_array_header_t *array = lbm_dec_array_r(v);
286 ✗ bool res = false;
287 ✗ if (array && is_color_sized(array->size)) {
288 ✗ res = (is_color((uint8_t*)array->data, array->size));
289 }
290 ✗ return res;
291 }
292
293 77 static color_t *get_color(lbm_value v) {
294 77 color_t *res = NULL;
295 77 lbm_array_header_t *array = lbm_dec_array_r(v);
296
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 5 times.
77 if (array && is_color_sized(array->size)
297
1/2
✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
71 && (is_color((uint8_t*)array->data, array->size))) {
298 71 res = (color_t*)array->data;
299 }
300 77 return res;
301 }
302
303
304 // Register symbols
305
306 359 static bool register_symbols(void) {
307 359 bool res = true;
308
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("indexed2", &symbol_indexed2);
309
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("indexed4", &symbol_indexed4);
310
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("indexed16", &symbol_indexed16);
311
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("rgb332", &symbol_rgb332);
312
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("rgb565", &symbol_rgb565);
313
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("rgb888", &symbol_rgb888);
314
315
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("thickness", &symbol_thickness);
316
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("filled", &symbol_filled);
317
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("rounded", &symbol_rounded);
318
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("dotted", &symbol_dotted);
319
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("scale", &symbol_scale);
320
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("rotate", &symbol_rotate);
321
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("resolution", &symbol_resolution);
322
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("tile", &symbol_tile);
323
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("clip", &symbol_clip);
324
325
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("regular", &symbol_regular);
326
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("gradient_x", &symbol_gradient_x);
327
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("gradient_y", &symbol_gradient_y);
328
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("gradient_x_pre", &symbol_gradient_x_pre);
329
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("gradient_y_pre", &symbol_gradient_y_pre);
330
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("mirrored", &symbol_mirrored);
331
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("repeat", &symbol_repeat);
332
333
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("color-0", &symbol_color_0);
334
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("color-1", &symbol_color_1);
335
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("width", &symbol_width);
336
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("offset", &symbol_offset);
337
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("repeat-type", &symbol_repeat_type);
338
339
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("down", &symbol_down);
340
2/4
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 359 times.
✗ Branch 4 not taken.
359 res = res && lbm_add_symbol_const("up", &symbol_up);
341
342 359 return res;
343 }
344
345 // Internal functions
346
347 973051 static int sign(int v) {
348
2/2
✓ Branch 0 taken 454132 times.
✓ Branch 1 taken 518919 times.
973051 if (v > 0) {
349 454132 return 1;
350
2/2
✓ Branch 0 taken 372779 times.
✓ Branch 1 taken 146140 times.
518919 } else if (v < 0) {
351 372779 return -1;
352 } else {
353 146140 return 0;
354 }
355 }
356
357 // Geometry utility functions
358
359 // Checks if a point is past a line formed by the given end and start points.
360 // The returned value is 1 if it is past, -1 if it's on the other side of the
361 // line, or 0 if it's exactly on the line.
362 // Don't ask me what is considered the "positive" side of the line ;)
363 //
364 // It would probably be more logical if the sign of the result was flipped...
365 201738 static int point_past_line(int x, int y, int line_start_x, int line_start_y, int line_end_x, int line_end_y) {
366 // source: https://stackoverflow.com/a/11908158/15507414
367
368 // this is not really a cross product, but whatever...
369 201738 int cross_prod = (x - line_start_x) * (line_end_y - line_start_y)
370 201738 - (y - line_start_y) * (line_end_x - line_start_x);
371
372
2/2
✓ Branch 0 taken 86264 times.
✓ Branch 1 taken 115474 times.
201738 if (cross_prod > 0) {
373 86264 return 1;
374
2/2
✓ Branch 0 taken 111536 times.
✓ Branch 1 taken 3938 times.
115474 } else if (cross_prod < 0) {
375 111536 return -1;
376 } else {
377 3938 return 0;
378 }
379 }
380
381 150802 static bool points_same_quadrant(int x0, int y0, int x1, int y1) {
382
4/4
✓ Branch 3 taken 90596 times.
✓ Branch 4 taken 3524 times.
✓ Branch 6 taken 30182 times.
✓ Branch 7 taken 60414 times.
244922 return (sign(x0) == sign(x1) || sign(x0) == 0 || sign(x1) == 0)
383
8/8
✓ Branch 0 taken 94120 times.
✓ Branch 1 taken 56682 times.
✓ Branch 4 taken 47454 times.
✓ Branch 5 taken 42934 times.
✓ Branch 7 taken 42513 times.
✓ Branch 8 taken 4941 times.
✓ Branch 10 taken 18408 times.
✓ Branch 11 taken 24105 times.
244922 && (sign(y0) == sign(y1) || sign(y0) == 0 || sign(y1) == 0);
384 }
385
386 24 static inline void norm_angle(float *angle) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 while (*angle < -M_PI) { *angle += 2.0f * (float)M_PI; }
388
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 24 times.
34 while (*angle >= M_PI) { *angle -= 2.0f * (float)M_PI; }
389 24 }
390
391 1032 static inline void norm_angle_0_2pi(float *angle) {
392
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1032 times.
1043 while (*angle < 0) { *angle += 2.0f * (float)M_PI; }
393
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1032 times.
1064 while (*angle >= 2.0 * M_PI) { *angle -= 2.0f * (float)M_PI; }
394 1032 }
395
396 1737 static uint8_t rgb888to332(uint32_t rgb) {
397 1737 uint8_t r = (uint8_t)(rgb >> (16 + 5));
398 1737 uint8_t g = (uint8_t)(rgb >> (8 + 5));
399 1737 uint8_t b = (uint8_t)(rgb >> 6);
400 1737 r = (r & 0x7) << 5;
401 1737 g = (g & 0x7) << 2;
402 1737 b = (b & 0x3);
403 1737 uint8_t res_rgb332 = r | g | b;
404 1737 return res_rgb332;
405 }
406
407 1949 static uint16_t rgb888to565(uint32_t rgb) {
408 1949 uint16_t r = (uint16_t)(rgb >> (16 + 3));
409 1949 uint16_t g = (uint16_t)(rgb >> (8 + 2));
410 1949 uint16_t b = (uint16_t)(rgb >> 3);
411 1949 r = r << 11;
412 1949 g = (g & 0x3F) << 5;
413 1949 b = (b & 0x1F);
414 1949 uint16_t res_rgb565 = r | g | b;
415 1949 return res_rgb565;
416 }
417
418 // One problem with rgb332 is that
419 // if you take 3 most significant bits of 255 you get 7.
420 // There is no whole number that you can multiply 7 with to get 255.
421 // This is fundamental for any conversion from RGB888 that just uses the
422 // N < 8 most significant bits. And it means that conversion to this format
423 // and then back to rgb888 will not (without tricks) map highest intensity
424 // back to highest intensity.
425 //
426 // Another issue is that 2 bits (the blue channel) yields steps of 85 (255 / 3)
427 // while 3 bits yields steps of 36.4 (255 / 7)
428 //
429 // 36.4 72.8 109.3 145.7 182.1 218.6 254.99
430 // 85 170 255
431 //
432 // The multiples of 85 never coincide with the multiples of 36.4 except
433 // for at 0 and 255
434 16 static uint32_t rgb332to888(uint8_t rgb) {
435 16 uint32_t r = (uint32_t)((rgb>>5) & 0x7);
436 16 uint32_t g = (uint32_t)((rgb>>2) & 0x7);
437
438 // turn 2 bits into 3 having value 0 3 5 or 7
439 // so that 4 points match up when doing greyscale.
440 16 uint32_t b = (uint32_t)(rgb & 0x3);
441
442
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 b = (b > 0) ? (2 * b) + 1 : 0;
443
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 r = (r == 7) ? 255 : 36 * r; // 36 is an approximation (36.4)
444
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 g = (g == 7) ? 255 : 36 * g;
445
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 b = (b == 7) ? 255 : 36 * b;
446 16 uint32_t res_rgb888 = r << 16 | g << 8 | b;
447 16 return res_rgb888;
448 }
449
450 // RGB 565
451 // 2^5 = 32
452 // 2^6 = 64
453 // 255 / 31 = 8.226
454 // 255 / 63 = 4.18
455 // 0 1 2 3 4 5 6 7 8 ... 31 63
456 // 5 bits 0 8.226 16.45 24.67 32.9 41.13 49.35 57.58 65.81 ... 254.9
457 // 6 bits 0 4.047 8.09 12.14 16.19 20.24 24.29 28.33 32.38 ... 254.9
458 //
459 // For RGB 565 the 6 and 5 bit channels match up very nicely such
460 // index i in the 5 bit channel is equal to index (2 * i) in the 6 bit channel.
461 // RGB 565 will have nice grayscales.
462
463 22 static uint32_t rgb565to888(uint16_t rgb) {
464 22 uint32_t r = (uint32_t)(rgb >> 11);
465 22 uint32_t g = (uint32_t)((rgb >> 5) & 0x3F);
466 22 uint32_t b = (uint32_t)(rgb & 0x1F);
467 22 uint32_t res_rgb888 = r << (16 + 3) | g << (8 + 2) | b << 3;
468 22 return res_rgb888;
469 }
470
471 54 void image_buffer_clear(image_buffer_t *img, uint32_t cc) {
472 54 color_format_t fmt = img->fmt;
473 54 uint32_t w = img->width;
474 54 uint32_t h = img->height;
475 54 uint32_t img_size = w * h;
476 54 uint8_t *data = img->data;
477
6/7
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
54 switch (fmt) {
478 4 case indexed2: {
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 uint32_t bytes = (img_size / 8) + (img_size % 8 ? 1 : 0);
480
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 uint8_t c8 = (uint8_t)((cc & 1) ? 0xFF : 0x0);
481 4 memset(data, c8, bytes);
482 }
483 4 break;
484 12 case indexed4: {
485 static const uint8_t index4_table[4] = {0x00, 0x55, 0xAA, 0xFF};
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 uint32_t bytes = (img_size / 4) + (img_size % 4 ? 1 : 0);
487 12 uint8_t ix = (uint8_t)(cc & 0x3);
488 12 memset(data, index4_table[ix], bytes);
489 }
490 12 break;
491 18 case indexed16: {
492 18 uint32_t bytes = (img_size / 2) + (img_size % 2 ? 1 : 0);
493 18 uint8_t ix = (uint8_t)(cc & 0xF);
494 18 uint8_t color = (uint8_t)(ix | ix << 4); // create a color based on duplication of index
495 18 memset(data, color, bytes);
496 }
497 18 break;
498 7 case rgb332: {
499 7 memset(data, rgb888to332(cc), img_size);
500 }
501 7 break;
502 6 case rgb565: {
503 6 uint16_t c = rgb888to565(cc);
504 6 uint8_t *dp = (uint8_t*)data;
505
2/2
✓ Branch 0 taken 45000 times.
✓ Branch 1 taken 6 times.
45006 for (unsigned int i = 0; i < img_size/2; i +=2) {
506 45000 dp[i] = (uint8_t)(c >> 8);
507 45000 dp[i+1] = (uint8_t)c;
508 }
509 }
510 6 break;
511 7 case rgb888: {
512 7 uint8_t *dp = (uint8_t*)data;
513
2/2
✓ Branch 0 taken 200000 times.
✓ Branch 1 taken 7 times.
200007 for (unsigned int i = 0; i < img_size * 3; i+= 3) {
514 200000 dp[i] = (uint8_t)(cc >> 16);
515 200000 dp[i+1] = (uint8_t)(cc >> 8);
516 200000 dp[i+2] = (uint8_t)cc;
517 }
518 }
519 7 break;
520 ✗ default:
521 ✗ break;
522 }
523 54 }
524
525 static const uint8_t indexed4_mask[4] = {0x03, 0x0C, 0x30, 0xC0};
526 static const uint8_t indexed4_shift[4] = {0, 2, 4, 6};
527 static const uint8_t indexed16_mask[4] = {0x0F, 0xF0};
528 static const uint8_t indexed16_shift[4] = {0, 4};
529
530
531 22528472 void putpixel(image_buffer_t* img, int x_i, int y_i, uint32_t c) {
532 22528472 uint16_t w = img->width;
533 22528472 uint16_t h = img->height;
534 22528472 uint16_t x = (uint16_t)x_i; // negative numbers become really large.
535 22528472 uint16_t y = (uint16_t)y_i;
536
537
4/4
✓ Branch 0 taken 22490805 times.
✓ Branch 1 taken 37667 times.
✓ Branch 2 taken 19971027 times.
✓ Branch 3 taken 2519778 times.
22528472 if (x < w && y < h) {
538 19971027 color_format_t fmt = img->fmt;
539 19971027 uint8_t *data = img->data;
540
6/7
✓ Branch 0 taken 75266 times.
✓ Branch 1 taken 17820693 times.
✓ Branch 2 taken 2047628 times.
✓ Branch 3 taken 1730 times.
✓ Branch 4 taken 1943 times.
✓ Branch 5 taken 23767 times.
✗ Branch 6 not taken.
19971027 switch(fmt) {
541 75266 case indexed2: {
542 75266 uint32_t pos = (uint32_t)y * (uint32_t)w + (uint32_t)x;
543 75266 uint32_t byte = pos >> 3;
544 75266 uint32_t bit = 7 - (pos & 0x7);
545
2/2
✓ Branch 0 taken 6255 times.
✓ Branch 1 taken 69011 times.
75266 if (c) {
546 6255 data[byte] |= (uint8_t)(1 << bit);
547 } else {
548 69011 data[byte] &= (uint8_t)~(1 << bit);
549 }
550 75266 break;
551 }
552 17820693 case indexed4: {
553 17820693 uint32_t pos = (uint32_t)y*w + x;
554 17820693 uint32_t byte = pos >> 2;
555 17820693 uint32_t ix = 3 - (pos & 0x3);
556 17820693 data[byte] = (uint8_t)((uint8_t)(data[byte] & ~indexed4_mask[ix]) | (uint8_t)(c << indexed4_shift[ix]));
557 17820693 break;
558 }
559 2047628 case indexed16: {
560 2047628 uint32_t pos = (uint32_t)y*w + x;
561 2047628 uint32_t byte = pos >> 1;
562 2047628 uint32_t ix = 1 - (pos & 0x1);
563 2047628 data[byte] = (uint8_t)((uint8_t)(data[byte] & ~indexed16_mask[ix]) | (uint8_t)(c << indexed16_shift[ix]));
564 2047628 break;
565 }
566 1730 case rgb332: {
567 1730 int pos = y*w + x;
568 1730 data[pos] = rgb888to332(c);
569 1730 break;
570 }
571 1943 case rgb565: {
572 1943 int pos = y*(w<<1) + (x<<1) ;
573 1943 uint16_t color = rgb888to565(c);
574 1943 data[pos] = (uint8_t)(color >> 8);
575 1943 data[pos+1] = (uint8_t)color;
576 1943 break;
577 }
578 23767 case rgb888: {
579 23767 int pos = y*(w*3) + (x*3);
580 23767 data[pos] = (uint8_t)(c>>16);
581 23767 data[pos+1] = (uint8_t)(c>>8);
582 23767 data[pos+2] = (uint8_t)c;
583 23767 break;
584 }
585 ✗ default:
586 ✗ break;
587 }
588 }
589 22528472 }
590
591 1103765 uint32_t getpixel(image_buffer_t* img, int x_i, int y_i) {
592 1103765 uint16_t w = img->width;
593 1103765 uint16_t h = img->height;
594 1103765 uint16_t x = (uint16_t)x_i;
595 1103765 uint16_t y = (uint16_t)y_i;
596
597
3/4
✓ Branch 0 taken 1103753 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1103753 times.
✗ Branch 3 not taken.
1103765 if (x < w && y < h) {
598 1103753 color_format_t fmt = img->fmt;
599 1103753 uint8_t *data = img->data;
600
6/7
✓ Branch 0 taken 61017 times.
✓ Branch 1 taken 1042640 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 28 times.
✗ Branch 6 not taken.
1103753 switch(fmt) {
601 61017 case indexed2: {
602 61017 uint32_t pos = (uint32_t)y * w + x;
603 61017 uint32_t byte = pos >> 3;
604 61017 uint32_t bit = 7 - (pos & 0x7);
605 61017 return (uint32_t)(data[byte] >> bit) & 0x1;
606 }
607 1042640 case indexed4: {
608 1042640 uint32_t pos = (uint32_t)y*w + x;
609 1042640 uint32_t byte = pos >> 2;
610 1042640 uint32_t ix = 3 - (pos & 0x3);
611 1042640 return (uint32_t)((data[byte] & indexed4_mask[ix]) >> indexed4_shift[ix]);
612 }
613 30 case indexed16: {
614 30 uint32_t pos = (uint32_t)y*w + x;
615 30 uint32_t byte = pos >> 1;
616 30 uint32_t ix = 1 - (pos & 0x1);
617 30 return (uint32_t)((data[byte] & indexed16_mask[ix]) >> indexed16_shift[ix]);
618 }
619 16 case rgb332: {
620 16 int pos = y*w + x;
621 16 return rgb332to888(data[pos]);
622 }
623 22 case rgb565: {
624 22 int pos = y*(w<<1) + (x<<1);
625 22 uint16_t c = (uint16_t)(((uint16_t)data[pos] << 8) | (uint16_t)data[pos+1]);
626 22 return rgb565to888(c);
627 }
628 28 case rgb888: {
629 28 int pos = y*(w*3) + (x*3);
630 28 uint32_t r = data[pos];
631 28 uint32_t g = data[pos+1];
632 28 uint32_t b = data[pos+2];
633 28 return (r << 16 | g << 8 | b);
634 }
635 ✗ default:
636 ✗ break;
637 }
638 }
639 12 return 0;
640 }
641
642 342289 static void h_line(image_buffer_t* img, int x, int y, int len, uint32_t c) {
643
2/2
✓ Branch 0 taken 21197947 times.
✓ Branch 1 taken 342289 times.
21540236 for (int i = 0; i < len; i ++) {
644 21197947 putpixel(img, x+i, y, c);
645 }
646 342289 }
647
648 88 static void v_line(image_buffer_t* img, int x, int y, int len, uint32_t c) {
649
2/2
✓ Branch 0 taken 2840 times.
✓ Branch 1 taken 88 times.
2928 for (int i = 0; i < len; i ++) {
650 2840 putpixel(img, x, y+i, c);
651 }
652 88 }
653
654 17192 static void fill_circle(image_buffer_t *img, int x, int y, int radius, uint32_t color) {
655
6/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 2324 times.
✓ Branch 3 taken 3141 times.
✓ Branch 4 taken 2795 times.
✓ Branch 5 taken 8877 times.
17192 switch (radius) {
656 18 case 0:
657 18 break;
658
659 37 case 1:
660 37 putpixel(img, x - 1, y - 1, color);
661 37 putpixel(img, x, y - 1, color);
662 37 putpixel(img, x - 1, y, color);
663 37 putpixel(img, x, y, color);
664 37 break;
665
666 2324 case 2:
667 2324 h_line(img, x - 1, y - 2, 2, color);
668 2324 h_line(img, x - 2, y - 1, 4, color);
669 2324 h_line(img, x - 2, y, 4, color);
670 2324 h_line(img, x - 1, y + 1, 2, color);
671 2324 break;
672
673 3141 case 3:
674 3141 h_line(img, x - 2, y - 3, 4, color);
675 3141 h_line(img, x - 3, y - 2, 6, color);
676 3141 h_line(img, x - 3, y - 1, 6, color);
677 3141 h_line(img, x - 3, y, 6, color);
678 3141 h_line(img, x - 3, y + 1, 6, color);
679 3141 h_line(img, x - 2, y + 2, 4, color);
680 3141 break;
681
682 2795 case 4:
683 2795 h_line(img, x - 2, y - 4, 4, color);
684 2795 h_line(img, x - 3, y - 3, 6, color);
685 2795 h_line(img, x - 4, y - 2, 8, color);
686 2795 h_line(img, x - 4, y - 1, 8, color);
687 2795 h_line(img, x - 4, y, 8, color);
688 2795 h_line(img, x - 4, y + 1, 8, color);
689 2795 h_line(img, x - 3, y + 2, 6, color);
690 2795 h_line(img, x - 2, y + 3, 4, color);
691 2795 break;
692
693 8877 default: {
694 8877 int r_sq = radius * radius;
695
2/2
✓ Branch 0 taken 260341 times.
✓ Branch 1 taken 8877 times.
269218 for (int y1 = -radius; y1 <= radius; y1++) {
696
1/2
✓ Branch 0 taken 3291357 times.
✗ Branch 1 not taken.
3291357 for (int x1 = -radius; x1 <= radius; x1++) {
697
2/2
✓ Branch 0 taken 260341 times.
✓ Branch 1 taken 3031016 times.
3291357 if (x1 * x1 + y1 * y1 <= r_sq) {
698 // Compute the start and end position for x axis
699 260341 int x_left = x1;
700
4/4
✓ Branch 0 taken 20283832 times.
✓ Branch 1 taken 8877 times.
✓ Branch 2 taken 20032368 times.
✓ Branch 3 taken 251464 times.
20292709 while ((x1 + 1) <= radius && ((x1 + 1) * (x1 + 1) + y1 * y1) <= r_sq) {
701 20032368 x1++;
702 }
703 260341 int x_right = x1;
704
705 // Draw line at this level y
706 260341 int length = x_right - x_left + 1;
707 260341 h_line(img, x + x_left, y + y1, length, color);
708
709 // Break out of innter loop for this level y
710 260341 break;
711 }
712 }
713 }
714 8877 } break;
715 }
716 17192 }
717
718 // Circle helper function, to draw a circle with an inner and outer radius.
719 // Draws the slice at the given outer radius point.
720 2332 static void handle_circle_slice(int outer_x, int outer_y, image_buffer_t *img, int c_x, int c_y, int radius_inner, uint32_t color, int radius_inner_dbl_sq) {
721 int width;
722
723 bool slice_filled;
724
2/2
✓ Branch 0 taken 1166 times.
✓ Branch 1 taken 1166 times.
2332 if (outer_y < 0) {
725 1166 slice_filled = -outer_y > radius_inner;
726 } else {
727 1166 slice_filled = outer_y >= radius_inner;
728 }
729
730
2/2
✓ Branch 0 taken 716 times.
✓ Branch 1 taken 1616 times.
2332 if (slice_filled) {
731
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 358 times.
716 if (outer_x < 0) {
732 358 width = -outer_x;
733 } else {
734 358 width = outer_x + 1;
735 358 outer_x = 0;
736 }
737 } else {
738 1616 int cur_x = outer_x;
739
2/2
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 808 times.
1616 int delta = outer_x > 0 ? -1 : 1;
740
741 // TODO: this could probably be binary searched
742 1616 int y_dbl_off = outer_y * 2 + 1;
743 1616 int y_dbl_off_sq = y_dbl_off * y_dbl_off;
744 6848 while (true) {
745 8464 cur_x += delta;
746 8464 int x_dbl_off = cur_x * 2 + 1;
747
2/2
✓ Branch 0 taken 6848 times.
✓ Branch 1 taken 1616 times.
8464 if (x_dbl_off * x_dbl_off + y_dbl_off_sq <= radius_inner_dbl_sq
748
1/2
✓ Branch 0 taken 6848 times.
✗ Branch 1 not taken.
6848 || abs(cur_x) > 2000) { // failsafe
749 break;
750 }
751 }
752 1616 width = abs(cur_x - outer_x);
753
2/2
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 808 times.
1616 if (outer_x > 0) {
754 808 outer_x = cur_x + 1;
755 }
756 }
757
758 2332 h_line(img, outer_x + c_x, outer_y + c_y, width, color);
759 2332 }
760
761 // thickness extends inwards from the given radius circle
762 78 static void circle(image_buffer_t *img, int x, int y, int radius, int thickness, uint32_t color) {
763
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 30 times.
78 if (thickness <= 0) {
764 48 int x0 = 0;
765 48 int y0 = radius;
766 48 int d = 5 - 4*radius;
767 48 int da = 12;
768 48 int db = 20 - 8*radius;
769
770
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 48 times.
433 while (x0 < y0) {
771 385 putpixel(img, x + x0, y + y0, color);
772 385 putpixel(img, x + x0, y - y0, color);
773 385 putpixel(img, x - x0, y + y0, color);
774 385 putpixel(img, x - x0, y - y0, color);
775 385 putpixel(img, x + y0, y + x0, color);
776 385 putpixel(img, x + y0, y - x0, color);
777 385 putpixel(img, x - y0, y + x0, color);
778 385 putpixel(img, x - y0, y - x0, color);
779
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 170 times.
385 if (d < 0) { d = d + da; db = db+8; }
780 170 else { y0 = y0 - 1; d = d+db; db = db + 16; }
781 385 x0 = x0+1;
782 385 da = da + 8;
783 }
784 } else {
785 30 int radius_inner = radius - thickness;
786
787 30 int radius_outer_dbl_sq = radius * radius * 4;
788 30 int radius_inner_dbl_sq = radius_inner * radius_inner * 4;
789
790
2/2
✓ Branch 0 taken 583 times.
✓ Branch 1 taken 30 times.
613 for (int y0 = 0; y0 < radius; y0++) {
791 583 int y_dbl_offs = 2 * y0 + 1;
792 583 int y_dbl_offs_sq = y_dbl_offs * y_dbl_offs;
793
794
1/2
✓ Branch 0 taken 4572 times.
✗ Branch 1 not taken.
4572 for (int x0 = -radius; x0 <= 0; x0++) {
795 4572 int x_dbl_offs = 2 * x0 + 1;
796
2/2
✓ Branch 0 taken 583 times.
✓ Branch 1 taken 3989 times.
4572 if (x_dbl_offs * x_dbl_offs + y_dbl_offs_sq <= radius_outer_dbl_sq) {
797 // This is horrible...
798 583 handle_circle_slice(x0, y0,
799 img, x, y, radius_inner, color, radius_inner_dbl_sq);
800 583 handle_circle_slice(-x0 - 1, y0,
801 img, x, y, radius_inner, color, radius_inner_dbl_sq);
802 583 handle_circle_slice(x0, -y0 - 1,
803 img, x, y, radius_inner, color, radius_inner_dbl_sq);
804 583 handle_circle_slice(-x0 - 1, -y0 - 1,
805 img, x, y, radius_inner, color, radius_inner_dbl_sq);
806 583 break;
807 }
808 }
809 }
810 }
811 78 }
812
813 // Thickness extends outwards and inwards from the given line equally, resulting
814 // in double the total thickness.
815 // TODO: This should be more efficient
816 // http://homepages.enterprise.net/murphy/thickline/index.html
817 // https://github.com/ArminJo/STMF3-Discovery-Demos/blob/master/lib/BlueDisplay/LocalGUI/ThickLine.hpp
818 1030 static void line(image_buffer_t *img, int x0, int y0, int x1, int y1, int thickness, int dot1, int dot2, uint32_t c) {
819 1030 int dx = abs(x1 - x0);
820
2/2
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 674 times.
1030 int sx = x0 < x1 ? 1 : -1;
821 1030 int dy = -abs(y1 - y0);
822
2/2
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 665 times.
1030 int sy = y0 < y1 ? 1 : -1;
823 1030 int error = dx + dy;
824
825
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 547 times.
1030 if (dot1 > 0) {
826 // These are used to deal with consecutive calls with
827 // possibly overlapping pixels.
828 static int dotcnt = 0;
829 static int x_last = 0;
830 static int y_last = 0;
831
832 while (true) {
833
2/2
✓ Branch 0 taken 1704 times.
✓ Branch 1 taken 621 times.
2325 if (dotcnt <= dot1) {
834
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 1559 times.
1704 if (thickness > 1) {
835 145 fill_circle(img, x0, y0, thickness, c);
836 } else {
837 1559 putpixel(img, x0, y0, c);
838 }
839 }
840
841
4/4
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 1678 times.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 398 times.
2325 if (x0 != x_last || y0 != y_last) {
842 1927 dotcnt++;
843 }
844
845 2325 x_last = x0;
846 2325 y_last = y0;
847
848
2/2
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 2063 times.
2325 if (dotcnt >= (dot1 + dot2)) {
849 262 dotcnt = 0;
850 }
851
852
4/4
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 1658 times.
✓ Branch 2 taken 418 times.
✓ Branch 3 taken 249 times.
2325 if (x0 == x1 && y0 == y1) {
853 418 break;
854 }
855
2/2
✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 249 times.
1907 if ((error * 2) >= dy) {
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1658 times.
1658 if (x0 == x1) {
857 ✗ break;
858 }
859 1658 error += dy;
860 1658 x0 += sx;
861 }
862
2/2
✓ Branch 0 taken 751 times.
✓ Branch 1 taken 1156 times.
1907 if ((error * 2) <= dx) {
863
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 686 times.
751 if (y0 == y1) {
864 65 break;
865 }
866 686 error += dx;
867 686 y0 += sy;
868 }
869 }
870 } else {
871 while (true) {
872
2/2
✓ Branch 0 taken 16926 times.
✓ Branch 1 taken 19228 times.
36154 if (thickness > 1) {
873 16926 fill_circle(img, x0, y0, thickness, c);
874 } else {
875 19228 putpixel(img, x0, y0, c);
876 }
877
878
4/4
✓ Branch 0 taken 11077 times.
✓ Branch 1 taken 25077 times.
✓ Branch 2 taken 520 times.
✓ Branch 3 taken 10557 times.
36154 if (x0 == x1 && y0 == y1) {
879 520 break;
880 }
881
2/2
✓ Branch 0 taken 23883 times.
✓ Branch 1 taken 11751 times.
35634 if ((error * 2) >= dy) {
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23883 times.
23883 if (x0 == x1) {
883 ✗ break;
884 }
885 23883 error += dy;
886 23883 x0 += sx;
887 }
888
2/2
✓ Branch 0 taken 15141 times.
✓ Branch 1 taken 20493 times.
35634 if ((error * 2) <= dx) {
889
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15114 times.
15141 if (y0 == y1) {
890 27 break;
891 }
892 15114 error += dx;
893 15114 y0 += sy;
894 }
895 }
896 }
897 1030 }
898
899 // thickness extends inwards from the given rectangle edge.
900 90 static void rectangle(image_buffer_t *img, int x, int y, int width, int height,
901 bool fill, int thickness, int dot1, int dot2, uint32_t color) {
902 90 thickness /= 2;
903
904
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 68 times.
90 if (fill) {
905
2/2
✓ Branch 0 taken 760 times.
✓ Branch 1 taken 22 times.
782 for (int i = y; i < (y + height);i++) {
906 760 h_line(img, x, i, width, color);
907 }
908 } else {
909
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
68 if (thickness <= 0 && dot1 == 0) {
910 44 h_line(img, x, y, width, color);
911 44 h_line(img, x, y + height, width, color);
912 44 v_line(img, x, y, height, color);
913 44 v_line(img, x + width, y, height, color);
914 } else {
915 24 x += thickness;
916 24 y += thickness;
917 24 width -= thickness * 2;
918 24 height -= thickness * 2;
919 // top
920 24 line(img, x, y, x + width, y, thickness, dot1, dot2, color);
921 // bottom
922 24 line(img, x, y + height, x + width, y + height, thickness, dot1, dot2, color);
923 // left
924 24 line(img, x, y, x, y + height, thickness, dot1, dot2, color);
925 // right
926 24 line(img, x + width, y, x + width, y + height, thickness, dot1, dot2, color);
927 }
928 }
929 90 }
930
931 #define NMIN(a, b) ((a) < (b) ? (a) : (b))
932 #define NMAX(a, b) ((a) > (b) ? (a) : (b))
933
934 2 static void fill_triangle(image_buffer_t *img, int x0, int y0,
935 int x1, int y1, int x2, int y2, uint32_t color) {
936 2 int x_min = NMIN(x0, NMIN(x1, x2));
937 2 int x_max = NMAX(x0, NMAX(x1, x2));
938 2 int y_min = NMIN(y0, NMIN(y1, y2));
939 2 int y_max = NMAX(y0, NMAX(y1, y2));
940
941
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 2 times.
84 for (int y = y_min;y <= y_max;y++) {
942
2/2
✓ Branch 0 taken 5822 times.
✓ Branch 1 taken 82 times.
5904 for (int x = x_min;x <= x_max;x++) {
943 5822 int w0 = point_past_line(x, y, x1, y1, x2, y2);
944 5822 int w1 = point_past_line(x, y, x2, y2, x0, y0);
945 5822 int w2 = point_past_line(x, y, x0, y0, x1, y1);
946
947
5/6
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 5680 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 140 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
5822 if ((w0 >= 0 && w1 >= 0 && w2 >= 0)
948
5/6
✓ Branch 0 taken 5822 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4372 times.
✓ Branch 3 taken 1450 times.
✓ Branch 4 taken 2922 times.
✓ Branch 5 taken 1450 times.
5822 || (w0 <= 0 && w1 <= 0 && w2 <= 0)) {
949 2922 putpixel(img, x, y, color);
950 }
951 }
952 }
953 2 }
954
955 12 static void generic_arc(image_buffer_t *img, int x, int y, int rad, float ang_start, float ang_end,
956 int thickness, int dot1, int dot2, int res, bool sector, bool segment, uint32_t color) {
957 12 ang_start *= (float)M_PI / 180.0f;
958 12 ang_end *= (float)M_PI / 180.0f;
959
960 12 norm_angle(&ang_start);
961 12 norm_angle(&ang_end);
962
963 12 float ang_range = ang_end - ang_start;
964
965
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (ang_range < 0.0) {
966 6 ang_range += 2.0f * (float)M_PI;
967 }
968
969
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (res <= 0) {
970 12 res = 80;
971 }
972
973 12 float steps = ceilf((float)res * ang_range * (0.5f / (float)M_PI));
974
975 12 float ang_step = ang_range / steps;
976 12 float sa = sinf(ang_step);
977 12 float ca = cosf(ang_step);
978
979 12 float px_start = cosf(ang_start) * (float)rad;
980 12 float py_start = sinf(ang_start) * (float)rad;
981
982
983 12 float px = px_start;
984 12 float py = py_start;
985
986
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 12 times.
480 for (int i = 0;i < steps;i++) {
987 468 float px_before = px;
988 468 float py_before = py;
989
990 468 px = px * ca - py * sa;
991 468 py = py * ca + px_before * sa;
992
993 468 line(img, x + (int)px_before, y + (int)py_before,
994 468 x + (int)px, y + (int)py, thickness, dot1, dot2, color);
995 }
996
997
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 11 times.
12 if (sector) {
998 1 line(img, x + (int)px, y + (int)py,
999 x, y,
1000 thickness, dot1, dot2, color);
1001 1 line(img, x, y,
1002 1 x + (int)px_start, y + (int)py_start,
1003 thickness, dot1, dot2, color);
1004 }
1005
1006
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 11 times.
12 if (segment) {
1007 1 line(img, x + (int)px, y + (int)py,
1008 1 x + (int)px_start, y + (int)py_start,
1009 thickness, dot1, dot2, color);
1010 }
1011 12 }
1012
1013 // thin arc helper function
1014 // handles a single pixel in the complete circle, checking if the pixel is part
1015 // of the arc.
1016 42976 static void handle_thin_arc_pixel(image_buffer_t *img, int x, int y,
1017 int c_x, int c_y, int cap0_x, int cap0_y, int cap1_x, int cap1_y, int min_y, int max_y, bool angle_is_closed, uint32_t color) {
1018
4/4
✓ Branch 0 taken 34386 times.
✓ Branch 1 taken 8590 times.
✓ Branch 2 taken 12982 times.
✓ Branch 3 taken 21404 times.
42976 if (y > max_y || y < min_y) {
1019 21572 return;
1020 }
1021
1022 21404 int line_is_past_0 = point_past_line(x, y, 0, 0, cap0_x, cap0_y);
1023 21404 int line_is_past_1 = -point_past_line(x, y, 0, 0, cap1_x, cap1_y);
1024
1025 21404 bool in_cap0_quadrant = points_same_quadrant(
1026 x, y, cap0_x, cap0_y);
1027 21404 bool in_cap1_quadrant = points_same_quadrant(
1028 x, y, cap1_x, cap1_y);
1029
1030
2/2
✓ Branch 0 taken 2798 times.
✓ Branch 1 taken 18606 times.
21404 if (angle_is_closed) {
1031
4/4
✓ Branch 0 taken 590 times.
✓ Branch 1 taken 2208 times.
✓ Branch 2 taken 159 times.
✓ Branch 3 taken 431 times.
2798 if (line_is_past_0 == 1 && line_is_past_1 == 1) {
1032 159 return;
1033 }
1034 } else {
1035
4/4
✓ Branch 0 taken 14118 times.
✓ Branch 1 taken 4488 times.
✓ Branch 2 taken 9627 times.
✓ Branch 3 taken 4491 times.
18606 if (line_is_past_0 == 1 || line_is_past_1 == 1
1036
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 9531 times.
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 8 times.
9627 || (line_is_past_0 == 0 && !in_cap0_quadrant)
1037
4/4
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 9510 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 104 times.
9619 || (line_is_past_1 == 0 && !in_cap1_quadrant)) {
1038 8992 return;
1039 }
1040 }
1041
1042 12253 putpixel(img, c_x + x, c_y + y, color);
1043 }
1044
1045 // single pixel wide arc
1046 166 static void thin_arc(image_buffer_t *img, int c_x, int c_y, int radius, float angle0, float angle1, bool sector, bool segment, uint32_t color) {
1047
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 156 times.
166 if (radius == 0) {
1048 10 return;
1049 }
1050
1051 156 angle0 *= (float)M_PI / 180.0f;
1052 156 angle1 *= (float)M_PI / 180.0f;
1053 156 norm_angle_0_2pi(&angle0);
1054 156 norm_angle_0_2pi(&angle1);
1055
1056
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 146 times.
156 if (angle0 == angle1) {
1057 10 return;
1058 }
1059
1060 bool angle_is_closed;
1061 // if the angle of the filled in part of the arc is greater than 180°
1062 // honestly unsure if it'd be better if this was called angle_is_open
1063
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 28 times.
146 if (angle1 - angle0 > 0.0) {
1064 118 angle_is_closed = fabsf(angle1 - angle0) > M_PI;
1065 } else {
1066 28 angle_is_closed = fabsf(angle1 - angle0) < M_PI;
1067 }
1068
1069 146 int cap0_x = (int)(cosf(angle0) * (float)(radius));
1070 146 int cap0_y = (int)(sinf(angle0) * (float)(radius));
1071
1072 146 int cap1_x = (int)(cosf(angle1) * (float)(radius));
1073 146 int cap1_y = (int)(sinf(angle1) * (float)(radius));
1074
1075 // Highest and lowest (y coord wise) drawn line of the base arc (excluding
1076 // the circular end caps). This range is *inclusive*!
1077 // Note that these might be slightly off due to inconsistent rounding between
1078 // my circle drawing algorithm and point rotation.
1079 146 int min_y = MIN(cap0_y, cap1_y);
1080 146 int max_y = MAX(cap0_y, cap1_y);
1081
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 28 times.
146 if (angle0 < angle1) {
1082
4/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 70 times.
118 if (angle0 < M_PI_2 && angle1 >= M_3PI_2) {
1083 4 min_y = -radius;
1084 4 max_y = radius;
1085
4/4
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 90 times.
114 } else if (angle0 < M_3PI_2 && angle1 > M_3PI_2) {
1086 23 min_y = -radius;
1087
4/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 10 times.
91 } else if (angle0 < M_PI_2 && angle1 > M_PI_2) {
1088 60 max_y = radius;
1089 }
1090 } else {
1091
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
28 if ((angle0 < M_3PI_2 && angle1 >= M_PI_2)
1092
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 time.
28 || (angle0 < M_PI_2)
1093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 || (angle1 > M_3PI_2)) {
1094 1 min_y = -radius;
1095 1 max_y = radius;
1096
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
27 } else if (angle0 < M_3PI_2 && angle1 < M_PI_2) {
1097 ✗ min_y = -radius;
1098
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
27 } else if (angle0 > M_PI_2 && angle1 > M_PI_2) {
1099 ✗ max_y = radius;
1100 }
1101 }
1102
1103 146 int radius_dbl_sq = radius * radius * 4;
1104
1105 146 int last_x = 0;
1106
2/2
✓ Branch 0 taken 7639 times.
✓ Branch 1 taken 146 times.
7785 for (int y = radius - 1; y >= 0; y--) {
1107 7639 int y_dbl_offs = 2 * y + 1;
1108 7639 int y_dbl_offs_sq = y_dbl_offs * y_dbl_offs;
1109
1110
1/2
✓ Branch 0 taken 898574 times.
✗ Branch 1 not taken.
898574 for (int x = -radius; x <= 0; x++) {
1111 898574 int x_dbl_offs = 2 * x + 1;
1112
2/2
✓ Branch 0 taken 7639 times.
✓ Branch 1 taken 890935 times.
898574 if (x_dbl_offs * x_dbl_offs + y_dbl_offs_sq <= radius_dbl_sq) {
1113
2/2
✓ Branch 0 taken 6201 times.
✓ Branch 1 taken 1438 times.
7639 if (last_x - x < 2) {
1114 // This is horrible...
1115 6201 handle_thin_arc_pixel(img, x, y,
1116 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1117 6201 handle_thin_arc_pixel(img, -x - 1, y,
1118 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1119
1120 6201 handle_thin_arc_pixel(img, x, -y - 1,
1121 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1122 6201 handle_thin_arc_pixel(img, -x - 1, -y - 1,
1123 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1124 } else {
1125
2/2
✓ Branch 0 taken 4543 times.
✓ Branch 1 taken 1438 times.
5981 for (int x0 = x; x0 < last_x; x0++) {
1126 4543 handle_thin_arc_pixel(img, x0, y,
1127 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1128 4543 handle_thin_arc_pixel(img, -x0 - 1, y,
1129 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1130
1131 4543 handle_thin_arc_pixel(img, x0, -y - 1,
1132 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1133 4543 handle_thin_arc_pixel(img, -x0 - 1, -y - 1,
1134 c_x, c_y, cap0_x, cap0_y, cap1_x, cap1_y, min_y, max_y, angle_is_closed, color);
1135 }
1136 }
1137
1138 7639 last_x = x;
1139 7639 break;
1140 }
1141 }
1142 }
1143
1144
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 130 times.
146 if (sector) {
1145 16 line(img, c_x, c_y, c_x + cap0_x, c_y + cap0_y, 1, 0, 0, color);
1146 16 line(img, c_x, c_y, c_x + cap1_x, c_y + cap1_y, 1, 0, 0, color);
1147 }
1148
1149
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 108 times.
146 if (segment) {
1150 38 line(img, c_x + cap0_x, c_y + cap0_y, c_x + cap1_x, c_y + cap1_y, 1, 0, 0, color);
1151 }
1152 }
1153
1154 // arc helper function
1155 // handles a horizontal slice at the given outer arc point
1156 64540 static void handle_arc_slice(image_buffer_t *img, int outer_x, int outer_y, int c_x, int c_y, uint32_t color,
1157 int outer_x0, int outer_y0, int outer_x1, int outer_y1,
1158 int cap0_min_y, int cap0_max_y, int cap1_min_y, int cap1_max_y,
1159 int radius_outer, int radius_inner,
1160 int min_y, int max_y,
1161 float angle0, float angle1, bool angle_is_closed,
1162 bool filled, bool segment,
1163 int radius_inner_dbl_sq) {
1164 (void) radius_outer;
1165
4/4
✓ Branch 0 taken 56252 times.
✓ Branch 1 taken 8288 times.
✓ Branch 2 taken 20254 times.
✓ Branch 3 taken 35998 times.
64540 if (outer_y > max_y || outer_y < min_y) {
1166 28542 return;
1167 }
1168
1169 int line_is_past_0, line_is_past_1;
1170 35998 line_is_past_0 = point_past_line(outer_x, outer_y, 0, 0, outer_x0, outer_y0);
1171 35998 line_is_past_1 = -point_past_line(outer_x, outer_y, 0, 0, outer_x1, outer_y1);
1172
1173 35998 int outer_x_sign = sign(outer_x);
1174 35998 int outer_y_sign = sign(outer_y);
1175
1176 35998 int outer_x0_sign = sign(outer_x0);
1177 35998 int outer_y0_sign = sign(outer_y0);
1178
1179 35998 int outer_x1_sign = sign(outer_x1);
1180 35998 int outer_y1_sign = sign(outer_y1);
1181
1182 bool in_cap0, in_cap1, in_both_caps;
1183
4/4
✓ Branch 0 taken 22268 times.
✓ Branch 1 taken 13730 times.
✓ Branch 2 taken 1954 times.
✓ Branch 3 taken 20314 times.
35998 if (segment && filled) {
1184 3908 in_cap0 = outer_y <= MAX(outer_y0, outer_y1)
1185
4/4
✓ Branch 0 taken 1474 times.
✓ Branch 1 taken 480 times.
✓ Branch 2 taken 1298 times.
✓ Branch 3 taken 176 times.
1954 && outer_y >= MIN(outer_y0, outer_y1);
1186 1954 in_cap1 = false;
1187 }
1188
2/2
✓ Branch 0 taken 5526 times.
✓ Branch 1 taken 28518 times.
34044 else if (filled) {
1189 5526 in_cap0 = outer_y <= cap0_max_y
1190
2/2
✓ Branch 0 taken 828 times.
✓ Branch 1 taken 1746 times.
2574 && outer_x0_sign == outer_x_sign
1191
4/4
✓ Branch 0 taken 2574 times.
✓ Branch 1 taken 2952 times.
✓ Branch 2 taken 310 times.
✓ Branch 3 taken 518 times.
8100 && outer_y0_sign == outer_y_sign;
1192 5526 in_cap1 = outer_y <= cap1_max_y
1193
2/2
✓ Branch 0 taken 963 times.
✓ Branch 1 taken 1891 times.
2854 && outer_x1_sign == outer_x_sign
1194
4/4
✓ Branch 0 taken 2854 times.
✓ Branch 1 taken 2672 times.
✓ Branch 2 taken 670 times.
✓ Branch 3 taken 293 times.
8380 && outer_y1_sign == outer_y_sign;
1195 } else {
1196 28518 in_cap0 = outer_y >= cap0_min_y
1197
2/2
✓ Branch 0 taken 3376 times.
✓ Branch 1 taken 17574 times.
20950 && outer_y <= cap0_max_y
1198
4/4
✓ Branch 0 taken 20950 times.
✓ Branch 1 taken 7568 times.
✓ Branch 2 taken 1491 times.
✓ Branch 3 taken 1885 times.
49468 && outer_x_sign == outer_x0_sign;
1199 28518 in_cap1 = outer_y >= cap1_min_y
1200
2/2
✓ Branch 0 taken 3578 times.
✓ Branch 1 taken 10934 times.
14512 && outer_y <= cap1_max_y
1201
4/4
✓ Branch 0 taken 14512 times.
✓ Branch 1 taken 14006 times.
✓ Branch 2 taken 1586 times.
✓ Branch 3 taken 1992 times.
43030 && outer_x_sign == outer_x1_sign;
1202 }
1203
4/4
✓ Branch 0 taken 3099 times.
✓ Branch 1 taken 32899 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 3077 times.
35998 in_both_caps = in_cap0 && in_cap1;
1204
1205 35998 bool in_cap0_quadrant = points_same_quadrant(outer_x, outer_y, outer_x0, outer_y0);
1206 35998 bool in_cap1_quadrant = points_same_quadrant(outer_x, outer_y, outer_x1, outer_y1);
1207
1208 35998 bool caps_in_same_quadrant = points_same_quadrant(outer_x0, outer_y0, outer_x1, outer_y1);
1209
1210 // Check if slice is outside caps and drawn sections of the arc.
1211
4/4
✓ Branch 0 taken 32899 times.
✓ Branch 1 taken 3099 times.
✓ Branch 2 taken 30665 times.
✓ Branch 3 taken 2234 times.
35998 if (!in_cap0 && !in_cap1) {
1212
2/2
✓ Branch 0 taken 16214 times.
✓ Branch 1 taken 14451 times.
30665 if (angle_is_closed) {
1213
4/4
✓ Branch 0 taken 5879 times.
✓ Branch 1 taken 10335 times.
✓ Branch 2 taken 2486 times.
✓ Branch 3 taken 3393 times.
16214 if (line_is_past_0 == 1 && line_is_past_1 == 1
1214 // Failsafe for closed angles with a very small difference.
1215 // Otherwise a tiny section at the opposite side of the arc
1216 // might get skipped.
1217
4/6
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 2317 times.
✓ Branch 2 taken 169 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 169 times.
✗ Branch 5 not taken.
2486 && (!caps_in_same_quadrant || (in_cap0_quadrant && in_cap1_quadrant))) {
1218 2486 return;
1219 }
1220 } else {
1221
4/4
✓ Branch 0 taken 10850 times.
✓ Branch 1 taken 3601 times.
✓ Branch 2 taken 9989 times.
✓ Branch 3 taken 861 times.
14451 if (line_is_past_0 == 1 || line_is_past_1 == 1
1222
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9988 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
9989 || (line_is_past_0 == 0 && !in_cap0_quadrant)
1223
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9987 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
9988 || (line_is_past_1 == 0 && !in_cap1_quadrant)) {
1224 4463 return;
1225 }
1226 }
1227 }
1228
1229 // Find slice width if arc spanned the complete circle.
1230 int x, x1;
1231 29049 int width = 0;
1232 29049 int width1 = 0;
1233 29049 bool slice_is_split = false;
1234
1235 bool slice_filled;
1236
2/2
✓ Branch 0 taken 6229 times.
✓ Branch 1 taken 22820 times.
29049 if (filled) {
1237 6229 slice_filled = true;
1238 } else {
1239
2/2
✓ Branch 0 taken 8996 times.
✓ Branch 1 taken 13824 times.
22820 if (outer_y < 0) {
1240 8996 slice_filled = -outer_y > radius_inner;
1241 } else {
1242 13824 slice_filled = outer_y >= radius_inner;
1243 }
1244 }
1245
1246
2/2
✓ Branch 0 taken 11257 times.
✓ Branch 1 taken 17792 times.
29049 if (slice_filled) {
1247
2/2
✓ Branch 0 taken 5360 times.
✓ Branch 1 taken 5897 times.
11257 if (outer_x < 0) {
1248 5360 x = outer_x;
1249 5360 width = -x;
1250 } else {
1251 5897 x = 0;
1252 5897 width = outer_x + 1;
1253 }
1254 } else {
1255 17792 x = outer_x;
1256 17792 int cur_x = outer_x;
1257
2/2
✓ Branch 0 taken 10515 times.
✓ Branch 1 taken 7277 times.
17792 int delta = outer_x > 0 ? -1 : 1;
1258
1259 // TODO: this could probably be binary searched
1260 17792 int y_dbl_off = outer_y * 2 + 1;
1261 17792 int y_dbl_off_sq = y_dbl_off * y_dbl_off;
1262 239064 while (true) {
1263 256856 cur_x += delta;
1264 256856 int x_dbl_off = cur_x * 2 + 1;
1265
2/2
✓ Branch 0 taken 239064 times.
✓ Branch 1 taken 17792 times.
256856 if (x_dbl_off * x_dbl_off + y_dbl_off_sq <= radius_inner_dbl_sq
1266
1/2
✓ Branch 0 taken 239064 times.
✗ Branch 1 not taken.
239064 || abs(x) > 2000) { // failsafe
1267 break;
1268 }
1269 }
1270 17792 width = abs(cur_x - x);
1271
2/2
✓ Branch 0 taken 10515 times.
✓ Branch 1 taken 7277 times.
17792 if (outer_x > 0) {
1272 10515 x = cur_x + 1;
1273 }
1274 }
1275
1276 // Check which cap lines intersects this slice
1277
8/8
✓ Branch 0 taken 25950 times.
✓ Branch 1 taken 3099 times.
✓ Branch 2 taken 2234 times.
✓ Branch 3 taken 23716 times.
✓ Branch 4 taken 4048 times.
✓ Branch 5 taken 1285 times.
✓ Branch 6 taken 2750 times.
✓ Branch 7 taken 1298 times.
29049 if ((in_cap0 || in_cap1) && !(segment && filled)) {
1278 // the range from x_start to x_end is *inclusive*
1279 4035 int x_start = x;
1280 4035 int x_end = x_start + width - 1;
1281
1282 // when a point is "past" a line, it is on the wrong cleared side of it
1283 4035 int start_is_past0 = point_past_line(x_start, outer_y,
1284 0, 0,
1285 outer_x0, outer_y0);
1286 4035 int end_is_past0 = point_past_line(x_end, outer_y,
1287 0, 0,
1288 outer_x0, outer_y0);
1289
1290 4035 int start_is_past1 = -point_past_line(x_start, outer_y,
1291 0, 0,
1292 outer_x1, outer_y1);
1293 4035 int end_is_past1 = -point_past_line(x_end, outer_y,
1294 0, 0,
1295 outer_x1, outer_y1);
1296
1297 // TODO: look into this:
1298 // end_is_past0!=0 is always true.
1299 // end_is_part1!=0 is always true.
1300 4035 bool slice_overlaps0 = start_is_past0 != end_is_past0
1301
5/6
✓ Branch 0 taken 1655 times.
✓ Branch 1 taken 2380 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 1630 times.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
4035 && (start_is_past0 != 0 || end_is_past0 != 0);
1302 4035 bool slice_overlaps1 = start_is_past1 != end_is_past1
1303
5/6
✓ Branch 0 taken 1824 times.
✓ Branch 1 taken 2211 times.
✓ Branch 2 taken 83 times.
✓ Branch 3 taken 1741 times.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
4035 && (start_is_past1 != 0 || end_is_past1 != 0);
1304
1305
8/8
✓ Branch 0 taken 1801 times.
✓ Branch 1 taken 2234 times.
✓ Branch 2 taken 1779 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 768 times.
✓ Branch 5 taken 1011 times.
✓ Branch 6 taken 659 times.
✓ Branch 7 taken 109 times.
4035 if ((in_cap0 && !in_cap1 && start_is_past0 == 1 && end_is_past0 == 1)
1306
7/8
✓ Branch 0 taken 2234 times.
✓ Branch 1 taken 1692 times.
✓ Branch 2 taken 2234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1435 times.
✓ Branch 5 taken 799 times.
✓ Branch 6 taken 1202 times.
✓ Branch 7 taken 233 times.
3926 || (!in_cap0 && in_cap1 && start_is_past1 == 1 && end_is_past1 == 1)
1307
5/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3671 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
3693 || (in_both_caps && !angle_is_closed && (
1308 ✗ (start_is_past0 == 1 && end_is_past0 == 1)
1309
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 || (start_is_past1 == 1 && end_is_past1 == 1)
1310 ))
1311
6/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3671 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 6 times.
3693 || (in_both_caps && angle_is_closed && (
1312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 (start_is_past0 == 1 && end_is_past0 == 1)
1313 ✗ && (start_is_past1 == 1 && end_is_past1 == 1)
1314 ))) {
1315 342 return;
1316 }
1317
1318 // The repetition in all these cases could probably be reduced...
1319
6/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3671 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 1 time.
3693 if ((in_both_caps && slice_overlaps0 && !slice_overlaps1)
1320
6/6
✓ Branch 0 taken 1691 times.
✓ Branch 1 taken 2001 times.
✓ Branch 2 taken 1670 times.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 1467 times.
✓ Branch 5 taken 203 times.
3692 || (in_cap0 && !in_cap1 && slice_overlaps0)) {
1321 // intersect with cap line 0
1322
4/4
✓ Branch 0 taken 681 times.
✓ Branch 1 taken 787 times.
✓ Branch 2 taken 666 times.
✓ Branch 3 taken 15 times.
1468 if (start_is_past0 != -1 && end_is_past0 != 1) {
1323
2/2
✓ Branch 0 taken 9536 times.
✓ Branch 1 taken 666 times.
10202 while (start_is_past0 == 1) {
1324 9536 x_start += 1;
1325 9536 start_is_past0 = point_past_line(x_start, outer_y,
1326 0, 0,
1327 outer_x0, outer_y0);
1328 }
1329 } else {
1330
2/2
✓ Branch 0 taken 9128 times.
✓ Branch 1 taken 802 times.
9930 while (end_is_past0 == 1) {
1331 9128 x_end -= 1;
1332 9128 end_is_past0 = point_past_line(x_end, outer_y,
1333 0, 0,
1334 outer_x0, outer_y0);
1335 }
1336 }
1337
5/6
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2204 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
2225 } else if ((in_both_caps && !slice_overlaps0 && slice_overlaps1)
1338
5/6
✓ Branch 0 taken 2001 times.
✓ Branch 1 taken 224 times.
✓ Branch 2 taken 2001 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1609 times.
✓ Branch 5 taken 392 times.
2225 || (!in_cap0 && in_cap1 && slice_overlaps1)) {
1339 // intersect with cap line 1
1340
4/4
✓ Branch 0 taken 1250 times.
✓ Branch 1 taken 359 times.
✓ Branch 2 taken 1249 times.
✓ Branch 3 taken 1 time.
1609 if (start_is_past1 != -1 && end_is_past1 != 1) {
1341
2/2
✓ Branch 0 taken 14004 times.
✓ Branch 1 taken 1249 times.
15253 while (start_is_past1 == 1) {
1342 14004 x_start += 1;
1343 14004 start_is_past1 = -point_past_line(x_start, outer_y,
1344 0, 0,
1345 outer_x1, outer_y1);
1346 }
1347 } else {
1348
2/2
✓ Branch 0 taken 5076 times.
✓ Branch 1 taken 360 times.
5436 while (end_is_past1 == 1) {
1349 5076 x_end -= 1;
1350 5076 end_is_past1 = -point_past_line(x_end, outer_y,
1351 0, 0,
1352 outer_x1, outer_y1);
1353 }
1354 }
1355
5/6
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 595 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
616 } else if (in_both_caps && slice_overlaps0 && slice_overlaps1) {
1356 // intersect with both cap lines
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (angle0 < angle1) {
1358 ✗ if (angle0 < M_PI) {
1359 ✗ while (start_is_past1 == 1) {
1360 ✗ x_start += 1;
1361 ✗ start_is_past1 = -point_past_line(x_start, outer_y,
1362 0, 0,
1363 outer_x1, outer_y1);
1364 }
1365 ✗ while (end_is_past0 == 1) {
1366 ✗ x_end -= 1;
1367 ✗ end_is_past0 = point_past_line(x_end, outer_y,
1368 0, 0,
1369 outer_x0, outer_y0);
1370 }
1371 } else {
1372 ✗ while (start_is_past0 == 1) {
1373 ✗ x_start += 1;
1374 ✗ start_is_past0 = point_past_line(x_start, outer_y,
1375 0, 0,
1376 outer_x0, outer_y0);
1377 }
1378 ✗ while (end_is_past1 == 1) {
1379 ✗ x_end -= 1;
1380 ✗ end_is_past1 = -point_past_line(x_end, outer_y,
1381 0, 0,
1382 outer_x1, outer_y1);
1383 }
1384 }
1385 } else {
1386 // split the slice into two
1387
1388 14 slice_is_split = true;
1389
1390 14 int x_start1 = x_start;
1391 14 int x_end1 = x_end;
1392
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (angle0 < M_PI) {
1394 ✗ while (end_is_past0 == 1) {
1395 ✗ x_end -= 1;
1396 ✗ end_is_past0 = point_past_line(x_end, outer_y,
1397 0, 0,
1398 outer_x0, outer_y0);
1399 }
1400 ✗ while (start_is_past1 == 1) {
1401 ✗ x_start1 += 1;
1402 ✗ start_is_past1 = -point_past_line(x_start1, outer_y,
1403 0, 0,
1404 outer_x1, outer_y1);
1405 }
1406 } else {
1407
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 14 times.
328 while (end_is_past1 == 1) {
1408 314 x_end1 -= 1;
1409 314 end_is_past1 = -point_past_line(x_end1, outer_y,
1410 0, 0,
1411 outer_x1, outer_y1);
1412 }
1413
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 14 times.
376 while (start_is_past0 == 1) {
1414 362 x_start += 1;
1415 362 start_is_past0 = point_past_line(x_start, outer_y,
1416 0, 0,
1417 outer_x0, outer_y0);
1418 }
1419 }
1420
1421 14 x1 = x_start1;
1422 14 width1 = x_end1 + 1 - x_start1 ;
1423 }
1424 }
1425 3693 x = x_start;
1426 3693 width = x_end + 1 - x_start;
1427
4/6
✓ Branch 0 taken 1298 times.
✓ Branch 1 taken 23716 times.
✓ Branch 2 taken 1298 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1298 times.
✗ Branch 5 not taken.
25014 } else if (in_cap0 && segment && filled) {
1428 // the range from x_start to x_end is *inclusive*
1429 1298 int x_start = x;
1430 1298 int x_end = x_start + width - 1;
1431
1432 // when a point is "past" a line, it is on the wrong cleared side of it
1433 1298 int start_is_past = -point_past_line(x_start, outer_y,
1434 outer_x0, outer_y0, outer_x1, outer_y1);
1435 1298 int end_is_past = -point_past_line(x_end, outer_y,
1436 outer_x0, outer_y0, outer_x1, outer_y1);
1437
1438 1298 bool slice_overlaps = start_is_past != end_is_past
1439
5/6
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 688 times.
✓ Branch 2 taken 82 times.
✓ Branch 3 taken 528 times.
✓ Branch 4 taken 82 times.
✗ Branch 5 not taken.
1298 && (start_is_past != 0 || end_is_past != 0);
1440
1441
4/4
✓ Branch 0 taken 739 times.
✓ Branch 1 taken 559 times.
✓ Branch 2 taken 455 times.
✓ Branch 3 taken 284 times.
1298 if (start_is_past == 1 && end_is_past == 1) {
1442 455 return;
1443 }
1444
1445
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 233 times.
843 if (slice_overlaps) {
1446
4/4
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 244 times.
✓ Branch 2 taken 285 times.
✓ Branch 3 taken 81 times.
610 if (start_is_past != -1 && end_is_past != 1) {
1447
2/2
✓ Branch 0 taken 5815 times.
✓ Branch 1 taken 285 times.
6100 while (start_is_past == 1) {
1448 5815 x_start += 1;
1449 5815 start_is_past = -point_past_line(x_start, outer_y,
1450 outer_x0, outer_y0, outer_x1, outer_y1);
1451 }
1452 } else {
1453
2/2
✓ Branch 0 taken 6497 times.
✓ Branch 1 taken 325 times.
6822 while (end_is_past == 1) {
1454 6497 x_end -= 1;
1455 6497 end_is_past = -point_past_line(x_end, outer_y,
1456 outer_x0, outer_y0, outer_x1, outer_y1);
1457 }
1458 }
1459 }
1460
1461 843 x = x_start;
1462 843 width = x_end + 1 - x_start;
1463 }
1464
1465 28252 h_line(img, c_x + x, c_y + outer_y, width, color);
1466
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28238 times.
28252 if (slice_is_split) {
1467 14 h_line(img, c_x + x1, c_y + outer_y, width1, color);
1468 }
1469 }
1470
1471 // TODO: Fix unwanted slice with angles 130 to 115 (I think, angles might be
1472 // slightly off).
1473 // TODO: Look into buggy rendering with angles around 180°-270°. This seems to
1474 // affect arcs, sectors, and segments likewise.
1475 545 static void arc(image_buffer_t *img, int c_x, int c_y, int radius, float angle0, float angle1,
1476 int thickness, bool rounded, bool filled, bool sector, bool segment, int dot1, int dot2, int resolution, uint32_t color) {
1477
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 533 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
545 if (dot1 > 0 && !filled) {
1478 12 thickness /= 2;
1479
1480 12 radius -= thickness;
1481
1482
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (thickness == 0) {
1483 12 thickness = 1;
1484 }
1485
1486 12 generic_arc(img, c_x, c_y, radius, angle0, angle1, thickness, dot1, dot2, resolution, sector, segment, color);
1487
1488 12 return;
1489 }
1490
1491
4/4
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 273 times.
✓ Branch 2 taken 166 times.
✓ Branch 3 taken 94 times.
533 if (thickness <= 1 && !filled) {
1492 166 thin_arc(img, c_x, c_y, radius, angle0, angle1, sector, segment, color);
1493
1494 166 return;
1495 }
1496
1497
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 360 times.
367 if (radius == 0) {
1498 7 return;
1499 }
1500
1501 360 angle0 *= (float)M_PI / 180.0f;
1502 360 angle1 *= (float)M_PI / 180.0f;
1503 360 norm_angle_0_2pi(&angle0); // theses are probably unecessary?
1504 360 norm_angle_0_2pi(&angle1); // but who knows with floating point imprecision...
1505
1506
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 354 times.
360 if (angle0 == angle1) {
1507 6 return;
1508 }
1509
1510 bool angle_is_closed;
1511 // if the angle of the filled in part of the arc is greater than 180°
1512
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 102 times.
354 if (angle1 - angle0 > 0.0) {
1513 252 angle_is_closed = fabsf(angle1 - angle0) > M_PI;
1514 } else {
1515 102 angle_is_closed = fabsf(angle1 - angle0) < M_PI;
1516 }
1517
1518 // angles smaller than 1 degree seem to cause issues (with a radius of 62)
1519 // this is kinda ugly though, and it will probably still break at larger
1520 // radii or something...
1521
4/4
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 233 times.
354 if (!angle_is_closed && fabsf(angle1 - angle0) < 0.0174532925) { // one degree in radians
1522
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 9 times.
23 if (rounded) {
1523 14 float rad_f = (float)radius - ((float)thickness / 2.0f);
1524
1525 14 float angle = (angle0 + angle1) / 2.0f;
1526
1527 14 int cap_center_x = (int)floorf(cosf(angle) * rad_f);
1528 14 int cap_center_y = (int)floorf(sinf(angle) * rad_f);
1529
1530 14 fill_circle(img, c_x + cap_center_x, c_y + cap_center_y, thickness / 2, color);
1531 }
1532 23 return;
1533 }
1534
1535
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 329 times.
331 if (thickness >= radius) {
1536 2 filled = true;
1537 }
1538
1539 int radius_outer, radius_inner;
1540
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 254 times.
331 if (filled) {
1541 77 radius_outer = radius;
1542 77 radius_inner = 0;
1543 } else {
1544 254 radius_outer = radius;
1545 254 radius_inner = radius - thickness;
1546 }
1547 331 int radius_outer_dbl_sq = radius_outer * radius_outer * 4;
1548 331 int radius_inner_dbl_sq = radius_inner * radius_inner * 4;
1549
1550 331 float angle0_cos = cosf(angle0);
1551 331 float angle0_sin = sinf(angle0);
1552 331 float angle1_cos = cosf(angle1);
1553 331 float angle1_sin = sinf(angle1);
1554
1555 331 int outer_x0 = (int)(angle0_cos * (float)radius_outer);
1556 331 int outer_y0 = (int)(angle0_sin * (float)radius_outer);
1557
1558 331 int outer_x1 = (int)(angle1_cos * (float)radius_outer);
1559 331 int outer_y1 = (int)(angle1_sin * (float)radius_outer);
1560
1561 int inner_y0;
1562 int inner_y1;
1563
1564
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 254 times.
331 if (filled) {
1565 77 inner_y0 = 0;
1566
1567 77 inner_y1 = 0;
1568 } else {
1569 254 inner_y0 = (int)(angle0_sin * (float)radius_inner);
1570
1571 254 inner_y1 = (int)(angle1_sin * (float)radius_inner);
1572 }
1573
1574 331 int cap0_min_y = MIN(inner_y0, outer_y0);
1575 331 int cap0_max_y = MAX(inner_y0, outer_y0);
1576
1577 331 int cap1_min_y = MIN(inner_y1, outer_y1);
1578 331 int cap1_max_y = MAX(inner_y1, outer_y1);
1579
1580 // Highest and lowest (y coord wise) drawn line of the base arc (excluding
1581 // the circular end caps). This range is *inclusive*!
1582 // Note that these might be slightly off due to inconsistent rounding between
1583 // Bresenhamn's algorithm and point rotation. (I don't think the point about
1584 // Bresenhamn is relevant as we don't use it anymore. Still wouldn't trust
1585 // them completely though...)
1586 331 int min_y = MIN(outer_y0, MIN(outer_y1, MIN(inner_y0, inner_y1)));
1587 331 int max_y = MAX(outer_y0, MAX(outer_y1, MAX(inner_y0, inner_y1)));
1588
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 102 times.
331 if (angle0 < angle1) {
1589
4/4
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 162 times.
229 if (angle0 < M_PI_2 && angle1 >= M_3PI_2) {
1590 17 min_y = -radius_outer;
1591 17 max_y = radius_outer;
1592
3/4
✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 172 times.
212 } else if (angle0 < M_3PI_2 && angle1 > M_3PI_2) {
1593 40 min_y = -radius_outer;
1594
4/4
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 153 times.
✓ Branch 3 taken 9 times.
172 } else if (angle0 < M_PI_2 && angle1 > M_PI_2) {
1595 153 max_y = radius_outer;
1596 }
1597 } else {
1598
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 9 times.
102 if ((angle0 < M_3PI_2 && angle1 >= M_PI_2)
1599
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1 time.
93 || (angle0 < M_PI_2)
1600
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 91 times.
92 || (angle1 > M_3PI_2)) {
1601 11 min_y = -radius_outer;
1602 11 max_y = radius_outer;
1603
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
91 } else if (angle0 < M_3PI_2 && angle1 < M_PI_2) {
1604 24 min_y = -radius_outer;
1605
3/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 50 times.
67 } else if (angle0 > M_PI_2 && angle1 > M_PI_2) {
1606 17 max_y = radius_outer;
1607 }
1608 }
1609
1610
2/2
✓ Branch 0 taken 16135 times.
✓ Branch 1 taken 331 times.
16466 for (int y = 0; y < radius_outer; y++) {
1611 16135 int y_dbl_offs = 2 * (y + 1);
1612 16135 int y_dbl_offs_sq = y_dbl_offs * y_dbl_offs;
1613
1614
1/2
✓ Branch 0 taken 223585 times.
✗ Branch 1 not taken.
223585 for (int x = -radius_outer; x <= 0; x++) {
1615 223585 int x_dbl_offs = 2 * (x + 1);
1616
2/2
✓ Branch 0 taken 16135 times.
✓ Branch 1 taken 207450 times.
223585 if (x_dbl_offs * x_dbl_offs + y_dbl_offs_sq <= radius_outer_dbl_sq) {
1617 // This is horrible...
1618 16135 handle_arc_slice(img, x, y,
1619 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1620 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1621 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1622 16135 handle_arc_slice(img, -x - 1, y,
1623 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1624 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1625 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1626
1627 16135 handle_arc_slice(img, x, -y - 1,
1628 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1629 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1630 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1631 16135 handle_arc_slice(img, -x - 1, -y - 1,
1632 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1633 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1634 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1635
1636 16135 break;
1637 }
1638 }
1639 }
1640
1641 // draw rounded line corners
1642
8/8
✓ Branch 0 taken 285 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 237 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 234 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 200 times.
331 if (rounded && !filled && !sector && !segment) {
1643 34 float rad_f = (float)radius - ((float)thickness / 2.0f);
1644
1645 34 int cap0_center_x = (int)floorf(angle0_cos * rad_f);
1646 34 int cap0_center_y = (int)floorf(angle0_sin * rad_f);
1647
1648 34 int cap1_center_x = (int)floorf(angle1_cos * rad_f);
1649 34 int cap1_center_y = (int)floorf(angle1_sin * rad_f);
1650
1651 34 thickness /= 2;
1652
1653 34 fill_circle(img, c_x + cap0_center_x, c_y + cap0_center_y, thickness, color);
1654 34 fill_circle(img, c_x + cap1_center_x, c_y + cap1_center_y, thickness, color);
1655 }
1656
1657 // draw sector arc cap to center lines
1658 // (sectors are always rounded)
1659
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 23 times.
331 if (sector && !filled) {
1660 3 float rad_f = (float)radius - ((float)thickness / 2.0f);
1661
1662 3 int cap0_center_x = (int)floorf(angle0_cos * rad_f);
1663 3 int cap0_center_y = (int)floorf(angle0_sin * rad_f);
1664
1665 3 int cap1_center_x = (int)floorf(angle1_cos * rad_f);
1666 3 int cap1_center_y = (int)floorf(angle1_sin * rad_f);
1667
1668 3 thickness /= 2;
1669
1670 3 line(img, c_x + cap0_center_x, c_y + cap0_center_y,
1671 c_x, c_y, thickness, 0, 0, color);
1672 3 line(img, c_x + cap1_center_x, c_y + cap1_center_y,
1673 c_x, c_y, thickness, 0, 0, color);
1674 }
1675
1676
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 25 times.
331 if (segment && !filled) {
1677 200 float rad_f = (float)radius - ((float)thickness / 2.0f);
1678
1679 200 int cap0_center_x = (int)floorf(angle0_cos * rad_f);
1680 200 int cap0_center_y = (int)floorf(angle0_sin * rad_f);
1681
1682 200 int cap1_center_x = (int)floorf(angle1_cos * rad_f);
1683 200 int cap1_center_y = (int)floorf(angle1_sin * rad_f);
1684
1685 200 thickness /= 2;
1686
1687 200 line(img, c_x + cap0_center_x, c_y + cap0_center_y,
1688 c_x + cap1_center_x, c_y + cap1_center_y, thickness, 0, 0, color);
1689 }
1690 }
1691
1692 734 static void img_putc(image_buffer_t *img, int x, int y, uint32_t *colors, int num_colors,
1693 uint8_t *font_data, uint8_t ch, bool up, bool down) {
1694 734 uint8_t w = font_data[0];
1695 734 uint8_t h = font_data[1];
1696 734 uint8_t char_num = font_data[2];
1697 734 uint8_t bits_per_pixel = font_data[3];
1698
1699 734 int pixels_per_byte = (int)(8 / bits_per_pixel);
1700 734 int bytes_per_char = (int)((w * h) / pixels_per_byte);
1701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 734 times.
734 if ((w * h) % pixels_per_byte != 0) {
1702 ✗ bytes_per_char += 1;
1703 }
1704
1705 // There are some expectations on ch that are not documented here.
1706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 734 times.
734 if (char_num == 10) {
1707 ✗ ch = (uint8_t)(ch - '0');
1708 } else {
1709 734 ch = (uint8_t)(ch - ' ');
1710 }
1711
1712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 734 times.
734 if (ch >= char_num) {
1713 ✗ return;
1714 }
1715
1716
2/2
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 210 times.
734 if (bits_per_pixel == 2) {
1717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 524 times.
524 if (num_colors < 4) {
1718 ✗ return;
1719 }
1720
1721
2/2
✓ Branch 0 taken 217984 times.
✓ Branch 1 taken 524 times.
218508 for (int i = 0; i < w * h; i++) {
1722 217984 uint8_t byte = font_data[4 + bytes_per_char * ch + (i / 4)];
1723 217984 uint8_t bit_pos = (uint8_t)(i % pixels_per_byte);
1724 217984 uint8_t pixel_value = (byte >> (bit_pos * 2)) & 0x03;
1725 217984 int x0 = i % w;
1726 217984 int y0 = i / w;
1727
2/2
✓ Branch 0 taken 2912 times.
✓ Branch 1 taken 215072 times.
217984 if (up) {
1728 2912 putpixel(img, x + y0, y - x0, colors[pixel_value]);
1729
2/2
✓ Branch 0 taken 4576 times.
✓ Branch 1 taken 210496 times.
215072 } else if (down) {
1730 4576 putpixel(img, x - y0, y + x0, colors[pixel_value]);
1731 } else {
1732 210496 putpixel(img, x + x0, y + y0, colors[pixel_value]);
1733 }
1734 }
1735 } else {
1736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
210 if (num_colors < 1) {
1737 ✗ return;
1738 }
1739
1740 210 int32_t fg = (int32_t)colors[0];
1741 210 int32_t bg = -1;
1742
1743
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
210 if (num_colors > 1) {
1744 210 bg = (int32_t)colors[1];
1745 }
1746
1747
2/2
✓ Branch 0 taken 87360 times.
✓ Branch 1 taken 210 times.
87570 for (int i = 0; i < w * h; i++) {
1748 87360 uint8_t byte = font_data[4 + bytes_per_char * ch + (i / 8)];
1749 87360 uint8_t bit_pos = (uint8_t)(i % 8);
1750 87360 uint8_t bit = (uint8_t)(byte & (1 << bit_pos));
1751
3/4
✓ Branch 0 taken 74918 times.
✓ Branch 1 taken 12442 times.
✓ Branch 2 taken 74918 times.
✗ Branch 3 not taken.
87360 if (bit || bg >= 0) {
1752 87360 int x0 = i % w;
1753 87360 int y0 = i / w;
1754
1755
2/2
✓ Branch 0 taken 32448 times.
✓ Branch 1 taken 54912 times.
87360 if (up) {
1756
2/2
✓ Branch 0 taken 4433 times.
✓ Branch 1 taken 28015 times.
32448 putpixel(img, x + y0, y - x0, bit ? (uint32_t)fg : (uint32_t)bg);
1757
2/2
✓ Branch 0 taken 37856 times.
✓ Branch 1 taken 17056 times.
54912 } else if (down) {
1758
2/2
✓ Branch 0 taken 5566 times.
✓ Branch 1 taken 32290 times.
37856 putpixel(img, x - y0, y + x0, bit ? (uint32_t)fg : (uint32_t)bg);
1759 } else {
1760
2/2
✓ Branch 0 taken 2443 times.
✓ Branch 1 taken 14613 times.
17056 putpixel(img, x + x0, y + y0, bit ? (uint32_t)fg : (uint32_t)bg);
1761 }
1762 }
1763 }
1764 }
1765 }
1766
1767 4386100 static inline void copy_pixel(
1768 image_buffer_t *img_dest,
1769 image_buffer_t *img_src,
1770 int dest_x, int dest_y,
1771 int src_x, int src_y,
1772 int src_w, int src_h,
1773 int transparent_color,
1774 bool tile
1775 ) {
1776
2/2
✓ Branch 0 taken 923300 times.
✓ Branch 1 taken 3462800 times.
4386100 if (tile) {
1777 923300 src_x = src_x % src_w;
1778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923300 times.
923300 if (src_x < 0) src_x = src_x + src_w;
1779 923300 src_y = src_y % src_h;
1780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923300 times.
923300 if (src_y < 0) src_y = src_y + src_h;
1781 }
1782
1783
8/8
✓ Branch 0 taken 2553092 times.
✓ Branch 1 taken 1833008 times.
✓ Branch 2 taken 1223499 times.
✓ Branch 3 taken 1329593 times.
✓ Branch 4 taken 1130161 times.
✓ Branch 5 taken 93338 times.
✓ Branch 6 taken 1013488 times.
✓ Branch 7 taken 116673 times.
4386100 if (src_x >= 0 && src_x < src_w && src_y >= 0 && src_y < src_h) {
1784 1013488 uint32_t p = getpixel(img_src, src_x, src_y);
1785
4/4
✓ Branch 0 taken 122331 times.
✓ Branch 1 taken 891157 times.
✓ Branch 2 taken 53950 times.
✓ Branch 3 taken 68381 times.
1013488 if (transparent_color == -1 || p != (uint32_t)transparent_color) {
1786 945107 putpixel(img_dest, dest_x, dest_y, p);
1787 }
1788 }
1789 4386100 }
1790
1791 // Copy pixels from source to destination with transformations
1792 42 void blit(
1793 image_buffer_t *img_dest, // Destination image buffer
1794 image_buffer_t *img_src, // Source image buffer
1795 int dest_offset_x, int dest_offset_y, // Where on dest to start writing pixels
1796 float rot_x, float rot_y, // Coordinate in src to rotate around
1797 float rot_angle, // Rotation angle in degrees
1798 float scale, // Scale factor
1799 int32_t transparent_color, // Color that will not be drawn -1 to disable
1800 bool tile, // Tile src to fill dest
1801 int clip_x, int clip_y, // Clip start in dest
1802 int clip_w, int clip_h // Clip width and height
1803 ) {
1804
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 41 times.
42 if (scale == 0.0) return;
1805 41 int src_w = img_src->width;
1806 41 int src_h = img_src->height;
1807
1808 41 int dest_x_start = clip_x;
1809 41 int dest_y_start = clip_y;
1810 41 int dest_x_end = clip_w;
1811 41 int dest_y_end = clip_h;
1812
1813
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 10 times.
41 if (rot_angle == 0.0 && scale == 1.0) {
1814
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 if (dest_offset_x > 0) dest_x_start += dest_offset_x;
1815
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 if (dest_offset_y > 0) dest_y_start += dest_offset_y;
1816
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (!tile) {
1817
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
11 if ((dest_x_end - dest_offset_x) > src_w) dest_x_end = src_w + dest_offset_x;
1818
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
11 if ((dest_y_end - dest_offset_y) > src_h) dest_y_end = src_h + dest_offset_y;
1819 }
1820
1821
2/2
✓ Branch 0 taken 2415 times.
✓ Branch 1 taken 21 times.
2436 for (int dest_y = dest_y_start; dest_y < dest_y_end; dest_y++) {
1822
2/2
✓ Branch 0 taken 866100 times.
✓ Branch 1 taken 2415 times.
868515 for (int dest_x = dest_x_start; dest_x < dest_x_end; dest_x++) {
1823 866100 int src_x = dest_x - dest_offset_x;
1824 866100 int src_y = dest_y - dest_offset_y;
1825 866100 copy_pixel(img_dest, img_src, dest_x, dest_y, src_x, src_y, src_w, src_h, transparent_color, tile);
1826 }
1827 }
1828
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 } else if (rot_angle == 0.0) {
1829 10 rot_x *= scale;
1830 10 rot_y *= scale;
1831
1832 10 const int fp_scale = 1000;
1833
1834 10 int rot_x_x = (int)rot_x;
1835 10 int rot_y_i = (int)rot_y;
1836 10 int scale_i = (int)(scale * (float) fp_scale);
1837
1838
2/2
✓ Branch 0 taken 3500 times.
✓ Branch 1 taken 10 times.
3510 for (int dest_y = dest_y_start; dest_y < dest_y_end; dest_y++) {
1839
2/2
✓ Branch 0 taken 1720000 times.
✓ Branch 1 taken 3500 times.
1723500 for (int dest_x = dest_x_start; dest_x < dest_x_end; dest_x++) {
1840 1720000 int src_x = (dest_x - dest_offset_x - rot_x_x) * fp_scale;
1841 1720000 int src_y = (dest_y - dest_offset_y - rot_y_i) * fp_scale;
1842
1843 1720000 src_x += rot_x_x * fp_scale;
1844 1720000 src_y += rot_y_i * fp_scale;
1845
1846 1720000 src_x /= scale_i;
1847 1720000 src_y /= scale_i;
1848 1720000 copy_pixel(img_dest, img_src, dest_x, dest_y, src_x, src_y, src_w, src_h, transparent_color, tile);
1849 }
1850 }
1851 } else {
1852 10 float sin_rot_angle = sinf(-rot_angle * (float)M_PI / 180.0f);
1853 10 float cos_rot_angle = cosf(-rot_angle * (float)M_PI / 180.0f);
1854
1855 10 rot_x *= scale;
1856 10 rot_y *= scale;
1857
1858 10 const int fp_scale = 1000;
1859
1860 10 int sin_rot_angle_i = (int)(sin_rot_angle * (float)fp_scale);
1861 10 int cos_rot_angle_i = (int)(cos_rot_angle * (float)fp_scale);
1862 10 int rot_x_i = (int)rot_x;
1863 10 int rot_y_i = (int)rot_y;
1864 10 int scale_i = (int)(scale * (float) fp_scale);
1865
1866
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 10 times.
3610 for (int dest_y = dest_y_start; dest_y < dest_y_end; dest_y++) {
1867
2/2
✓ Branch 0 taken 1800000 times.
✓ Branch 1 taken 3600 times.
1803600 for (int dest_x = dest_x_start; dest_x < dest_x_end; dest_x++) {
1868 1800000 int src_x = (dest_x - dest_offset_x - rot_x_i) * cos_rot_angle_i + (dest_y - dest_offset_y - rot_y_i) * sin_rot_angle_i;
1869 1800000 int src_y = -(dest_x - dest_offset_x - rot_x_i) * sin_rot_angle_i + (dest_y - dest_offset_y - rot_y_i) * cos_rot_angle_i;
1870
1871 1800000 src_x += rot_x_i * fp_scale;
1872 1800000 src_y += rot_y_i * fp_scale;
1873
1874 1800000 src_x /= scale_i;
1875 1800000 src_y /= scale_i;
1876 1800000 copy_pixel(img_dest, img_src, dest_x, dest_y, src_x, src_y, src_w, src_h, transparent_color, tile);
1877 }
1878 }
1879 }
1880 }
1881
1882 // Extensions
1883
1884 #define ATTR_MAX_ARGS 4
1885 #define ARG_MAX_NUM 8
1886
1887 typedef struct {
1888 bool is_valid;
1889 uint16_t arg_num;
1890 lbm_value args[ATTR_MAX_ARGS];
1891 } attr_t;
1892
1893 typedef struct {
1894 bool is_valid;
1895 image_buffer_t img;
1896 lbm_value args[ARG_MAX_NUM];
1897 attr_t attr_thickness;
1898 attr_t attr_filled;
1899 attr_t attr_rounded;
1900 attr_t attr_dotted;
1901 attr_t attr_scale;
1902 attr_t attr_rotate;
1903 attr_t attr_resolution;
1904 attr_t attr_tile;
1905 attr_t attr_clip;
1906 } img_args_t;
1907
1908 1085 static img_args_t decode_args(lbm_value *args, lbm_uint argn, int num_expected) {
1909 img_args_t res;
1910 1085 memset(&res, 0, sizeof(res));
1911 1085 res.is_valid = false;
1912
1913 lbm_array_header_t *arr;
1914
3/4
✓ Branch 0 taken 1085 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1048 times.
✓ Branch 4 taken 37 times.
1085 if (argn >= 1 && (arr = get_image_buffer(args[0]))) {
1915 // at least one argument which is an image buffer.
1916 1048 res.img.width = image_buffer_width((uint8_t*)arr->data);
1917 1048 res.img.height = image_buffer_height((uint8_t*)arr->data);
1918 1048 res.img.fmt = image_buffer_format((uint8_t*)arr->data);
1919 1048 res.img.mem_base = (uint8_t*)arr->data;
1920 1048 res.img.data = image_buffer_data((uint8_t*)arr->data);
1921
1922
1923 1048 int num_dec = 0;
1924
2/2
✓ Branch 0 taken 5391 times.
✓ Branch 1 taken 1022 times.
6413 for (unsigned int i = 1;i < argn;i++) {
1925
4/4
✓ Branch 1 taken 668 times.
✓ Branch 2 taken 4723 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 648 times.
5391 if (!lbm_is_number(args[i]) && !lbm_is_cons(args[i])) {
1926 20 return res;
1927 }
1928
1929
2/2
✓ Branch 1 taken 4723 times.
✓ Branch 2 taken 648 times.
5371 if (lbm_is_number(args[i])) {
1930 4723 res.args[num_dec] = args[i];
1931 4723 num_dec++;
1932
1933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4723 times.
4723 if (num_dec > ARG_MAX_NUM) {
1934 ✗ return res;
1935 }
1936 } else {
1937 648 lbm_value curr = args[i];
1938 648 int attr_ind = 0;
1939 648 attr_t *attr_now = 0;
1940
2/2
✓ Branch 1 taken 1156 times.
✓ Branch 2 taken 646 times.
1802 while (lbm_is_cons(curr)) {
1941 1156 lbm_value arg = lbm_car(curr);
1942
1943
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 508 times.
1156 if (attr_ind == 0) {
1944
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 648 times.
648 if (!lbm_is_symbol(arg)) {
1945 ✗ return res;
1946 }
1947
1948
2/2
✓ Branch 1 taken 346 times.
✓ Branch 2 taken 302 times.
648 if (lbm_dec_sym(arg) == symbol_thickness) {
1949 346 attr_now = &res.attr_thickness;
1950 346 attr_now->arg_num = 1;
1951
2/2
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 168 times.
302 } else if (lbm_dec_sym(arg) == symbol_filled) {
1952 134 attr_now = &res.attr_filled;
1953 134 attr_now->arg_num = 0;
1954
2/2
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 77 times.
168 } else if (lbm_dec_sym(arg) == symbol_rounded) {
1955 91 attr_now = &res.attr_rounded;
1956 91 attr_now->arg_num = 1;
1957
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 49 times.
77 } else if (lbm_dec_sym(arg) == symbol_dotted) {
1958 28 attr_now = &res.attr_dotted;
1959 28 attr_now->arg_num = 2;
1960
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 34 times.
49 } else if (lbm_dec_sym(arg) == symbol_scale) {
1961 15 attr_now = &res.attr_scale;
1962 15 attr_now->arg_num = 1;
1963
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 23 times.
34 } else if (lbm_dec_sym(arg) == symbol_rotate) {
1964 11 attr_now = &res.attr_rotate;
1965 11 attr_now->arg_num = 3;
1966
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 } else if (lbm_dec_sym(arg) == symbol_resolution) {
1967 ✗ attr_now = &res.attr_resolution;
1968 ✗ attr_now->arg_num = 1;
1969
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 10 times.
23 } else if (lbm_dec_sym(arg) == symbol_tile) {
1970 13 attr_now = &res.attr_tile;
1971 13 attr_now->arg_num = 0;
1972
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 time.
10 } else if (lbm_dec_sym(arg) == symbol_clip) {
1973 9 attr_now = &res.attr_clip;
1974 9 attr_now->arg_num = 4;
1975 }
1976 else {
1977 1 return res;
1978 }
1979 } else {
1980
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 507 times.
508 if (!lbm_is_number(arg)) {
1981 1 return res;
1982 }
1983
1984 507 attr_now->args[attr_ind - 1] = arg;
1985 }
1986
1987 1154 attr_ind++;
1988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1154 times.
1154 if (attr_ind > (ATTR_MAX_ARGS + 1)) {
1989 ✗ return res;
1990 }
1991
1992 1154 curr = lbm_cdr(curr);
1993 }
1994
1995 // does this really compare the pointer addresses?
1996
4/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 555 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 26 times.
646 if (attr_now == &res.attr_rounded && attr_ind == 1) {
1997 65 attr_now->arg_num = 0; // the `rounded` attribute may be empty
1998 }
1999
2000
2001
2/2
✓ Branch 0 taken 642 times.
✓ Branch 1 taken 4 times.
646 if ((attr_ind - 1) == attr_now->arg_num) {
2002 642 attr_now->is_valid = true;
2003 } else {
2004 4 return res;
2005 }
2006 }
2007 }
2008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
1022 if (num_dec != num_expected) {
2009 ✗ return res;
2010 }
2011 // I think this should go here ???
2012 1022 res.is_valid = true;
2013 }
2014 1059 return res;
2015 }
2016
2017 26 static lbm_value ext_image_dims(lbm_value *args, lbm_uint argn) {
2018 26 img_args_t arg_dec = decode_args(args, argn, 0);
2019
2020
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 if (!arg_dec.is_valid) {
2021 2 return ENC_SYM_TERROR;
2022 }
2023
2024 24 lbm_value dims = lbm_heap_allocate_list(2);
2025
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (lbm_is_symbol(dims)) {
2026 ✗ return dims;
2027 }
2028 24 lbm_value curr = dims;
2029 24 lbm_set_car(curr, lbm_enc_i(arg_dec.img.width));
2030 24 curr = lbm_cdr(curr);
2031 24 lbm_set_car(curr, lbm_enc_i(arg_dec.img.height));
2032 24 return dims;
2033 }
2034
2035 76 static lbm_value ext_image_buffer(lbm_value *args, lbm_uint argn) {
2036 76 lbm_value res = ENC_SYM_TERROR;
2037 76 bool args_ok = false;
2038 76 color_format_t fmt = indexed2;
2039 76 lbm_uint w = 0;
2040 76 lbm_uint h = 0;
2041
2042
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
77 if (argn == 4 &&
2043
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_defrag_mem(args[0]) &&
2044
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_symbol(args[1]) &&
2045
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_number(args[2]) &&
2046 1 lbm_is_number(args[3])) {
2047 1 fmt = sym_to_color_format(args[1]);
2048 1 w = lbm_dec_as_u32(args[2]);
2049 1 h = lbm_dec_as_u32(args[3]);
2050 1 args_ok = true;
2051
2/4
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
150 } else if (argn == 3 &&
2052
1/2
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
150 lbm_is_symbol(args[0]) &&
2053
1/2
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
150 lbm_is_number(args[1]) &&
2054 75 lbm_is_number(args[2])) {
2055 75 fmt = sym_to_color_format(args[0]);
2056 75 w = lbm_dec_as_u32(args[1]);
2057 75 h = lbm_dec_as_u32(args[2]);
2058 75 args_ok = true;
2059 }
2060
2061
9/12
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 72 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 71 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 71 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 71 times.
✗ Branch 11 not taken.
76 if (args_ok && fmt != format_not_supported && w > 0 && h > 0 && w < MAX_WIDTH && h < MAX_HEIGHT) {
2062
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 1 time.
71 if (argn == 3) {
2063 70 res = image_buffer_allocate(fmt, (uint16_t)w, (uint16_t)h);
2064 } else {
2065 1 res = image_buffer_allocate_dm((lbm_uint*)lbm_car(args[0]), fmt, (uint16_t)w, (uint16_t)h);
2066 }
2067 }
2068 76 return res;
2069 }
2070
2071
2072 20 static lbm_value ext_is_image_buffer(lbm_value *args, lbm_uint argn) {
2073 20 lbm_value res = ENC_SYM_TERROR;
2074
2075
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (argn == 1) {
2076 20 res = ENC_SYM_NIL;
2077 20 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
2078
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 time.
20 if (array) {
2079 19 uint8_t *data = (uint8_t*)array->data;
2080
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 if (image_buffer_is_valid(data, array->size)) {
2081 19 res = ENC_SYM_TRUE;;
2082 }
2083 }
2084 }
2085 20 return res;
2086 }
2087
2088 28 static lbm_value ext_color(lbm_value *args, lbm_uint argn) {
2089 28 lbm_value res = ENC_SYM_TERROR;
2090
2091
4/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 1 time.
56 if (argn >= 2 && argn <= 6 &&
2092
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
55 lbm_is_symbol(args[0]) &&
2093 27 lbm_is_number(args[1])) {
2094
2095 // Color1 and color2 are int in the struct and decoded as i32, why
2096 // where they stored in uint32_t?
2097 27 int32_t color1 = lbm_dec_as_i32(args[1]);
2098
2099 27 int32_t color2 = 0;
2100
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (argn >= 3) {
2101
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[2])) {
2102 14 color2 = lbm_dec_as_i32(args[2]);
2103 } else {
2104 ✗ return ENC_SYM_TERROR;
2105 }
2106 }
2107
2108 27 int32_t param1 = 0;
2109
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (argn >= 4) {
2110
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[3])) {
2111 14 param1 = lbm_dec_as_i32(args[3]);
2112 } else {
2113 ✗ return ENC_SYM_TERROR;
2114 }
2115 }
2116
2117 27 int32_t param2 = 0;
2118
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (argn >= 5) {
2119
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[4])) {
2120 14 param2 = lbm_dec_as_i32(args[4]);
2121 } else {
2122 ✗ return ENC_SYM_TERROR;
2123 }
2124 }
2125
2126 27 bool mirrored = false;
2127
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 17 times.
27 if (argn >= 6) {
2128
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (lbm_is_symbol(args[5])) {
2129 10 lbm_uint sym = lbm_dec_sym(args[5]);
2130
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 if (sym == symbol_repeat) {
2131 7 mirrored = false;
2132
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 } else if (sym == symbol_mirrored) {
2133 3 mirrored = true;
2134 } else {
2135 ✗ return ENC_SYM_TERROR;
2136 }
2137 } else {
2138 ✗ return ENC_SYM_TERROR;
2139 }
2140 }
2141
2142 COLOR_TYPE t;
2143
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 15 times.
27 if (lbm_dec_sym(args[0]) == symbol_regular) {
2144 12 t = COLOR_REGULAR;
2145
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 9 times.
15 } else if (lbm_dec_sym(args[0]) == symbol_gradient_x) {
2146 6 t = COLOR_GRADIENT_X;
2147
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
9 } else if (lbm_dec_sym(args[0]) == symbol_gradient_y) {
2148 3 t = COLOR_GRADIENT_Y;
2149
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
6 } else if (lbm_dec_sym(args[0]) == symbol_gradient_x_pre) {
2150 4 t = COLOR_PRE_X;
2151
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
2 } else if (lbm_dec_sym(args[0]) == symbol_gradient_y_pre) {
2152 1 t = COLOR_PRE_Y;
2153 } else {
2154 1 return ENC_SYM_TERROR;
2155 }
2156
2157 // Maybe check if param is in ranges first ?
2158 26 res = color_allocate(t, color1, color2, (uint16_t)param1, (uint16_t)param2, mirrored);
2159 }
2160
2161 27 return res;
2162 }
2163
2164 25 static lbm_value ext_color_set(lbm_value *args, lbm_uint argn) {
2165 color_t *color;
2166
3/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 3 times.
25 if (argn != 3 || !(color = get_color(args[0])) || // color assignment
2167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 !lbm_is_symbol(args[1])) {
2168 3 return ENC_SYM_TERROR;
2169 }
2170
2171 22 bool is_regular = color->type == COLOR_REGULAR;
2172
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 10 times.
22 bool is_gradient = color->type == COLOR_GRADIENT_X || color->type == COLOR_GRADIENT_Y;
2173
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 bool is_pre = color->type == COLOR_PRE_X || color->type == COLOR_PRE_Y;
2174
2175 22 lbm_uint prop = lbm_dec_sym(args[1]);
2176
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 16 times.
22 if (prop == symbol_color_0) {
2177
5/6
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
6 if (!lbm_is_number(args[2]) || !(is_regular || is_gradient)) {
2178 2 return ENC_SYM_TERROR;
2179 }
2180 4 color->color1 = lbm_dec_as_i32(args[2]);
2181
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 13 times.
16 } else if (prop == symbol_color_1) {
2182
3/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
3 if (!lbm_is_number(args[2]) || !is_gradient) {
2183 2 return ENC_SYM_TERROR;
2184 }
2185 1 color->color2 = lbm_dec_as_i32(args[2]);
2186
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
13 } else if (prop == symbol_width) {
2187
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
5 if (!lbm_is_number(args[2]) || !is_gradient) {
2188 3 return ENC_SYM_TERROR;
2189 }
2190 2 color->param1 = (uint16_t)lbm_dec_as_u32(args[2]);
2191
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 } else if (prop == symbol_offset) {
2192
4/6
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
3 if (!lbm_is_number(args[2]) || !(is_gradient || is_pre)) {
2193 1 return ENC_SYM_TERROR;
2194 }
2195 2 color->param2 = (uint16_t)lbm_dec_as_u32(args[2]);
2196
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 } else if (prop == symbol_repeat_type) {
2197
2/6
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
3 if (!lbm_is_symbol(args[2]) || !(is_gradient || is_pre)) {
2198 ✗ return ENC_SYM_TERROR;
2199 }
2200 3 lbm_uint sym = lbm_dec_sym(args[2]);
2201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (sym == symbol_repeat) {
2202 ✗ color->mirrored = false;
2203
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 } else if (sym == symbol_mirrored) {
2204 2 color->mirrored = true;
2205 } else {
2206 1 return ENC_SYM_TERROR;
2207 }
2208 } else {
2209 2 return ENC_SYM_TERROR;
2210 }
2211
2212 11 return ENC_SYM_TRUE;
2213 }
2214
2215 23 static lbm_value ext_color_get(lbm_value *args, lbm_uint argn) {
2216 color_t *color;
2217
3/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 1 time.
23 if (argn != 2 || !(color = get_color(args[0])) || // color assignment
2218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 !lbm_is_symbol(args[1])) {
2219 1 return ENC_SYM_TERROR;
2220 }
2221
2222
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8 times.
22 bool is_gradient = color->type == COLOR_GRADIENT_X || color->type == COLOR_GRADIENT_Y;
2223
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 bool is_pre = color->type == COLOR_PRE_X || color->type == COLOR_PRE_Y;
2224
2225 22 lbm_uint prop = lbm_dec_sym(args[1]);
2226
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
22 if (prop == symbol_color_0) {
2227 // always allowed
2228 9 return lbm_enc_u32((uint32_t)color->color1);
2229
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 } else if (prop == symbol_color_1) {
2230
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
4 if (!is_gradient && !is_pre) {
2231 1 return ENC_SYM_TERROR;
2232 }
2233 3 return lbm_enc_u32((uint32_t)color->color2);
2234
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 } else if (prop == symbol_width) {
2235
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
5 if (!is_gradient && !is_pre) {
2236 1 return ENC_SYM_TERROR;
2237 }
2238 4 return lbm_enc_i32((int32_t)color->param1);
2239
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 } else if (prop == symbol_offset) {
2240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (!is_gradient && !is_pre) {
2241 ✗ return ENC_SYM_TERROR;
2242 }
2243 3 return lbm_enc_i32((int32_t)color->param2);
2244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 } else if (prop == symbol_repeat_type) {
2245 ✗ if (!is_gradient && !is_pre) {
2246 ✗ return ENC_SYM_TERROR;
2247 }
2248 ✗ return lbm_enc_sym(color->mirrored ? symbol_mirrored : symbol_repeat);
2249 } else {
2250 1 return ENC_SYM_TERROR;
2251 }
2252
2253 return ENC_SYM_TRUE;
2254 }
2255
2256 7 static lbm_value ext_color_setpre(lbm_value *args, lbm_uint argn) {
2257 color_t *color;
2258
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 time.
7 if (argn != 3 || !(color = get_color(args[0])) ||
2259
4/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 4 times.
6 !lbm_is_number(args[1]) || !lbm_is_number(args[2])) {
2260 3 return ENC_SYM_TERROR;
2261 }
2262
2263 4 uint32_t pos = lbm_dec_as_u32(args[1]);
2264 4 int new_color = lbm_dec_as_i32(args[2]);
2265
2266
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
4 if (color->precalc == 0 || pos >= COLOR_PRECALC_LEN) {
2267 1 return ENC_SYM_EERROR;
2268 }
2269
2270 3 color->precalc[pos] = (uint32_t)new_color;
2271
2272 3 return ENC_SYM_TRUE;
2273 }
2274
2275 14 static lbm_value ext_color_getpre(lbm_value *args, lbm_uint argn) {
2276 color_t *color;
2277
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 1 time.
14 if (argn != 2 || !(color = get_color(args[0])) ||
2278
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 12 times.
13 !lbm_is_number(args[1])) {
2279 2 return ENC_SYM_TERROR;
2280 }
2281 12 uint32_t pos = lbm_dec_as_u32(args[1]);
2282
2283
3/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 11 times.
12 if (color->precalc == 0 || pos >= COLOR_PRECALC_LEN) {
2284 1 return ENC_SYM_EERROR;
2285 }
2286
2287 11 return lbm_enc_u32(color->precalc[pos]);
2288 }
2289
2290 57 static lbm_value ext_clear(lbm_value *args, lbm_uint argn) {
2291
2292 57 lbm_value res = ENC_SYM_TERROR;
2293 lbm_array_header_t *arr;
2294
5/6
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 3 times.
114 if ((argn == 1 || argn == 2) &&
2295
2/2
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 1 time.
111 (arr = get_image_buffer(args[0])) && // assignment
2296
1/2
✓ Branch 1 taken 53 times.
✗ Branch 2 not taken.
53 (argn != 2 || lbm_is_number(args[1]))) { // ( argn == 2 -> lbm_is_number(args[1]))
2297 image_buffer_t img_buf;
2298 54 img_buf.width = image_buffer_width((uint8_t*)arr->data);
2299 54 img_buf.height = image_buffer_height((uint8_t*)arr->data);
2300 54 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2301 54 img_buf.mem_base = (uint8_t*)arr->data;
2302 54 img_buf.data = image_buffer_data((uint8_t*)arr->data);
2303
2304 54 uint32_t color = 0;
2305
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 1 time.
54 if (argn == 2) {
2306 53 color = lbm_dec_as_u32(args[1]);
2307 }
2308
2309 54 image_buffer_clear(&img_buf, color);
2310 54 res = ENC_SYM_TRUE;
2311 }
2312 57 return res;
2313 }
2314
2315 113 static lbm_value ext_putpixel(lbm_value *args, lbm_uint argn) {
2316 113 img_args_t arg_dec = decode_args(args, argn, 3);
2317
2318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (!arg_dec.is_valid) {
2319 ✗ return ENC_SYM_TERROR;
2320 }
2321
2322 113 putpixel(&arg_dec.img,
2323 lbm_dec_as_i32(arg_dec.args[0]),
2324 lbm_dec_as_i32(arg_dec.args[1]),
2325 lbm_dec_as_u32(arg_dec.args[2]));
2326 113 return ENC_SYM_TRUE;
2327 }
2328
2329 167 static lbm_value ext_getpixel(lbm_value *args, lbm_uint argn) {
2330 167 img_args_t arg_dec = decode_args(args, argn, 2);
2331
2332
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 143 times.
167 if (!arg_dec.is_valid) {
2333 24 return ENC_SYM_TERROR;
2334 }
2335
2336 143 uint32_t c = getpixel(&arg_dec.img,
2337 lbm_dec_as_i32(arg_dec.args[0]),
2338 lbm_dec_as_i32(arg_dec.args[1]));
2339 143 return lbm_enc_u32(c);
2340 }
2341
2342 // lisp args: img x1 y1 x2 y2 color opt-attr1 ... opt-attrN
2343 43 static lbm_value ext_line(lbm_value *args, lbm_uint argn) {
2344 43 img_args_t arg_dec = decode_args(args, argn, 5);
2345
2346
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 41 times.
43 if (!arg_dec.is_valid) {
2347 2 return ENC_SYM_TERROR;
2348 }
2349
2350 41 line(&arg_dec.img,
2351 lbm_dec_as_i32(arg_dec.args[0]),
2352 lbm_dec_as_i32(arg_dec.args[1]),
2353 lbm_dec_as_i32(arg_dec.args[2]),
2354 lbm_dec_as_i32(arg_dec.args[3]),
2355 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2356 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2357 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2358 lbm_dec_as_u32(arg_dec.args[4]));
2359
2360 41 return ENC_SYM_TRUE;
2361 }
2362
2363 // lisp args: img cx cy r color opt-attr1 ... opt-attrN
2364 83 static lbm_value ext_circle(lbm_value *args, lbm_uint argn) {
2365 83 img_args_t arg_dec = decode_args(args, argn, 4);
2366
2367
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 80 times.
83 if (!arg_dec.is_valid) {
2368 3 return ENC_SYM_TERROR;
2369 }
2370
2371
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 57 times.
80 if (arg_dec.attr_filled.is_valid) {
2372 23 fill_circle(&arg_dec.img,
2373 lbm_dec_as_i32(arg_dec.args[0]),
2374 lbm_dec_as_i32(arg_dec.args[1]),
2375 lbm_dec_as_i32(arg_dec.args[2]),
2376 lbm_dec_as_u32(arg_dec.args[3]));
2377
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 78 times.
80 } if (arg_dec.attr_dotted.is_valid) {
2378 4 arc(&arg_dec.img,
2379 lbm_dec_as_i32(arg_dec.args[0]),
2380 lbm_dec_as_i32(arg_dec.args[1]),
2381 lbm_dec_as_i32(arg_dec.args[2]),
2382 0, 359.9f,
2383 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2384 2 arg_dec.attr_rounded.is_valid, // currently does nothing as the line function doesn't support square ends.
2385 false,
2386 false, false,
2387 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2388 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2389 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2390 lbm_dec_as_u32(arg_dec.args[3]));
2391 } else {
2392 78 circle(&arg_dec.img,
2393 lbm_dec_as_i32(arg_dec.args[0]),
2394 lbm_dec_as_i32(arg_dec.args[1]),
2395 lbm_dec_as_i32(arg_dec.args[2]),
2396 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2397 lbm_dec_as_u32(arg_dec.args[3]));
2398 }
2399
2400 80 return ENC_SYM_TRUE;
2401 }
2402
2403 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
2404 137 static lbm_value ext_arc(lbm_value *args, lbm_uint argn) {
2405 137 img_args_t arg_dec = decode_args(args, argn, 6);
2406
2407
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 128 times.
137 if (!arg_dec.is_valid) {
2408 9 return ENC_SYM_TERROR;
2409 }
2410
2411 256 arc(&arg_dec.img,
2412 lbm_dec_as_i32(arg_dec.args[0]),
2413 lbm_dec_as_i32(arg_dec.args[1]),
2414 lbm_dec_as_i32(arg_dec.args[2]),
2415 lbm_dec_as_float(arg_dec.args[3]),
2416 lbm_dec_as_float(arg_dec.args[4]),
2417 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2418 128 arg_dec.attr_rounded.is_valid,
2419 128 arg_dec.attr_filled.is_valid,
2420 false, false,
2421 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2422 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2423 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2424 lbm_dec_as_u32(arg_dec.args[5]));
2425
2426 128 return ENC_SYM_TRUE;
2427 }
2428
2429 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
2430 51 static lbm_value ext_circle_sector(lbm_value *args, lbm_uint argn) {
2431 51 img_args_t arg_dec = decode_args(args, argn, 6);
2432
2433
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 50 times.
51 if (!arg_dec.is_valid) {
2434 1 return ENC_SYM_TERROR;
2435 }
2436
2437 100 arc(&arg_dec.img,
2438 lbm_dec_as_i32(arg_dec.args[0]),
2439 lbm_dec_as_i32(arg_dec.args[1]),
2440 lbm_dec_as_i32(arg_dec.args[2]),
2441 lbm_dec_as_float(arg_dec.args[3]),
2442 lbm_dec_as_float(arg_dec.args[4]),
2443 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2444 true,
2445 50 arg_dec.attr_filled.is_valid,
2446 true, false,
2447 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2448 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2449 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2450 lbm_dec_as_u32(arg_dec.args[5]));
2451
2452 50 return ENC_SYM_TRUE;
2453 }
2454
2455 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
2456 284 static lbm_value ext_circle_segment(lbm_value *args, lbm_uint argn) {
2457 284 img_args_t arg_dec = decode_args(args, argn, 6);
2458
2459
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 273 times.
284 if (!arg_dec.is_valid) {
2460 11 return ENC_SYM_TERROR;
2461 }
2462
2463 546 arc(&arg_dec.img,
2464 lbm_dec_as_i32(arg_dec.args[0]),
2465 lbm_dec_as_i32(arg_dec.args[1]),
2466 lbm_dec_as_i32(arg_dec.args[2]),
2467 lbm_dec_as_float(arg_dec.args[3]),
2468 lbm_dec_as_float(arg_dec.args[4]),
2469 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2470 true,
2471 273 arg_dec.attr_filled.is_valid,
2472 false, true,
2473 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2474 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2475 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2476 lbm_dec_as_u32(arg_dec.args[5]));
2477
2478
2479 273 return ENC_SYM_TRUE;
2480 }
2481
2482 // lisp args: img x y width height color opt-attr1 ... opt-attrN
2483 108 static lbm_value ext_rectangle(lbm_value *args, lbm_uint argn) {
2484 108 img_args_t arg_dec = decode_args(args, argn, 5);
2485
2486
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 105 times.
108 if (!arg_dec.is_valid) {
2487 3 return ENC_SYM_TERROR;
2488 }
2489
2490 105 image_buffer_t *img = &arg_dec.img;
2491 105 int x = lbm_dec_as_i32(arg_dec.args[0]);
2492 105 int y = lbm_dec_as_i32(arg_dec.args[1]);
2493 105 int width = lbm_dec_as_i32(arg_dec.args[2]);
2494 105 int height = lbm_dec_as_i32(arg_dec.args[3]);
2495 105 int rad = lbm_dec_as_i32(arg_dec.attr_rounded.args[0]);
2496 105 int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]);
2497 105 uint32_t color = lbm_dec_as_u32(arg_dec.args[4]);
2498 105 int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]);
2499 105 int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]);
2500 105 int resolution = lbm_dec_as_i32(arg_dec.attr_resolution.args[0]);
2501
2502
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 78 times.
105 if (arg_dec.attr_rounded.is_valid) {
2503
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 23 times.
27 if (arg_dec.attr_filled.is_valid) {
2504 4 rectangle(img, x + rad, y, width - 2 * rad, rad, 1, 1, 0, 0, color);
2505 4 rectangle(img, x + rad, y + height - rad, width - 2 * rad, rad, 1, 1, 0, 0, color);
2506 4 rectangle(img, x, y + rad, width, height - 2 * rad, 1, 1, 0, 0, color);
2507 4 fill_circle(img, x + rad, y + rad, rad, color);
2508 4 fill_circle(img, x + rad, y + height - rad, rad, color);
2509 4 fill_circle(img, x + width - rad, y + rad, rad, color);
2510 4 fill_circle(img, x + width - rad, y + height - rad, rad, color);
2511 } else {
2512 // Remember to change these to use the rounded attribute,
2513 // when/if line supports it!
2514
2515 23 int line_thickness = thickness / 2;
2516 23 thickness = line_thickness * 2; // round it to even for consistency.
2517
2518 // top
2519 23 line(img, x + rad, y + line_thickness, x + width - rad, y + line_thickness, line_thickness, dot1, dot2, color);
2520 // bottom
2521 23 line(img, x + rad, y + height - line_thickness, x + width - rad, y + height - line_thickness, line_thickness, dot1, dot2, color);
2522 // left
2523 23 line(img, x + line_thickness, y + rad, x + line_thickness, y + height - rad, line_thickness, dot1, dot2, color);
2524 // right
2525 23 line(img, x + width - line_thickness, y + rad, x + width - line_thickness, y + height - rad, line_thickness, dot1, dot2, color);
2526
2527 // upper left
2528 23 arc(img, x + rad, y + rad, rad, 180, 270, thickness, false, false, false, false, dot1, dot2, resolution, color);
2529 // upper right
2530 23 arc(img, x + width - rad, y + rad, rad, 270, 0, thickness, false, false, false, false, dot1, dot2, resolution, color);
2531 // bottom left
2532 23 arc(img, x + rad, y + height - rad, rad, 90, 180, thickness, false, false, false, false, dot1, dot2, resolution, color);
2533 // bottom right
2534 23 arc(img, x + width - rad, y + height - rad, rad, 0, 90, thickness, false, false, false, false, dot1, dot2, resolution, color);
2535 }
2536 } else {
2537 78 rectangle(img,
2538 x, y,
2539 width, height,
2540 78 arg_dec.attr_filled.is_valid,
2541 thickness,
2542 dot1, dot2,
2543 color);
2544 }
2545
2546 105 return ENC_SYM_TRUE;
2547 }
2548
2549 // lisp args: img x1 y1 x2 y2 x3 y3 color opt-attr1 ... opt-attrN
2550 22 static lbm_value ext_triangle(lbm_value *args, lbm_uint argn) {
2551 22 img_args_t arg_dec = decode_args(args, argn, 7);
2552
2553
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (!arg_dec.is_valid) {
2554 2 return ENC_SYM_TERROR;
2555 }
2556
2557 20 image_buffer_t *img = &arg_dec.img;
2558 20 int x0 = lbm_dec_as_i32(arg_dec.args[0]);
2559 20 int y0 = lbm_dec_as_i32(arg_dec.args[1]);
2560 20 int x1 = lbm_dec_as_i32(arg_dec.args[2]);
2561 20 int y1 = lbm_dec_as_i32(arg_dec.args[3]);
2562 20 int x2 = lbm_dec_as_i32(arg_dec.args[4]);
2563 20 int y2 = lbm_dec_as_i32(arg_dec.args[5]);
2564 20 int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]);
2565 20 int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]);
2566 20 int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]);
2567 20 uint32_t color = lbm_dec_as_u32(arg_dec.args[6]);
2568
2569
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (arg_dec.attr_filled.is_valid) {
2570 2 fill_triangle(img, x0, y0, x1, y1, x2, y2, color);
2571 } else {
2572 18 line(img, x0, y0, x1, y1, thickness, dot1, dot2, color);
2573 18 line(img, x1, y1, x2, y2, thickness, dot1, dot2, color);
2574 18 line(img, x2, y2, x0, y0, thickness, dot1, dot2, color);
2575 }
2576
2577 20 return ENC_SYM_TRUE;
2578 }
2579
2580 // lisp args: img x y fg bg font str
2581 107 static lbm_value ext_text(lbm_value *args, lbm_uint argn) {
2582 107 bool up = false;
2583 107 bool down = false;
2584
2585
4/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 63 times.
107 if (argn >= 7 && lbm_is_symbol(args[argn - 1])) {
2586
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 15 times.
28 if (lbm_dec_sym(args[argn - 1]) == symbol_up) {
2587 13 up = true;
2588 13 argn--;
2589 }
2590
2591
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 15 times.
28 if (lbm_dec_sym(args[argn - 1]) == symbol_down) {
2592 13 down = true;
2593 13 argn--;
2594 }
2595 }
2596
2597
4/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 88 times.
107 if (argn != 6 && argn != 7) {
2598 3 return ENC_SYM_TERROR;
2599 }
2600
2601 104 int x = lbm_dec_as_i32(args[1]);
2602 104 int y = lbm_dec_as_i32(args[2]);
2603
2604 104 int32_t colors[4] = {-1, -1, -1, -1}; // how big? int vs int32
2605
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 16 times.
104 if (argn == 7) {
2606
2/4
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 88 times.
88 if (!lbm_is_number(args[3]) || !lbm_is_number(args[4])) {
2607 ✗ return ENC_SYM_TERROR;
2608 }
2609 88 colors[0] = lbm_dec_as_i32(args[3]);
2610 88 colors[1] = lbm_dec_as_i32(args[4]);
2611 } else {
2612 16 lbm_value curr = args[3];
2613 16 int ind = 0;
2614
2/2
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 11 times.
58 while (lbm_is_cons(curr)) {
2615 47 lbm_value arg = lbm_car(curr);
2616
1/2
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
47 if (lbm_is_number(arg)) {
2617 47 colors[ind++] = lbm_dec_as_i32(arg);
2618 } else {
2619 ✗ return ENC_SYM_TERROR;
2620 }
2621
2622
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 42 times.
47 if (ind == 4) {
2623 5 break;
2624 }
2625
2626 42 curr = lbm_cdr(curr);
2627 }
2628 }
2629
2630
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (!array_is_image_buffer(args[0])) {
2631 4 return ENC_SYM_TERROR;
2632 }
2633 100 lbm_array_header_t *arr = (lbm_array_header_t *)lbm_car(args[0]);
2634 image_buffer_t img_buf;
2635 100 img_buf.width = image_buffer_width((uint8_t*)arr->data);
2636 100 img_buf.height = image_buffer_height((uint8_t*)arr->data);
2637 100 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2638 100 img_buf.mem_base = (uint8_t*)arr->data;
2639 100 img_buf.data = image_buffer_data((uint8_t*)arr->data);
2640
2641 100 lbm_array_header_t *font = 0;
2642 // Allow both const and non-const fonts.
2643
1/2
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
100 if (lbm_type_of_functional(args[argn - 2]) == LBM_TYPE_ARRAY) {
2644 100 font = (lbm_array_header_t *)lbm_car(args[argn - 2]);
2645 }
2646
2647 100 char *txt = lbm_dec_str(args[argn - 1]);
2648
2649
5/6
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 97 times.
100 if (!font || !txt || font->size < (4 + 5 * 5 * 10)) {
2650 3 return ENC_SYM_TERROR;
2651 }
2652
2653 97 uint8_t *font_data = (uint8_t*)font->data;
2654 97 uint8_t w = font_data[0];
2655 97 uint8_t h = font_data[1];
2656
2657 97 int incx = 1;
2658 97 int incy = 0;
2659
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 84 times.
97 if (up) {
2660 13 incx = 0;
2661 13 incy = -1;
2662
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 71 times.
84 } else if (down) {
2663 13 incx = 0;
2664 13 incy = 1;
2665 }
2666
2667 97 int ind = 0;
2668
2/2
✓ Branch 0 taken 734 times.
✓ Branch 1 taken 97 times.
831 while (txt[ind] != 0) {
2669 2202 img_putc(&img_buf,
2670
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 547 times.
734 x + ind * ((up || down) ? h : w) * incx,
2671
4/4
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 547 times.
✓ Branch 2 taken 649 times.
✓ Branch 3 taken 85 times.
734 y + ind * ((up || down) ? w : h) * incy,
2672 (uint32_t *)colors,
2673 4,
2674 font_data,
2675
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 85 times.
734 (uint8_t)txt[ind],
2676 up,
2677 down);
2678 734 ind++;
2679 }
2680
2681 97 return ENC_SYM_TRUE;
2682 }
2683
2684 51 static lbm_value ext_blit(lbm_value *args, lbm_uint argn) {
2685 51 img_args_t arg_dec = decode_args(args + 1, argn - 1, 3);
2686
2687 51 lbm_value res = ENC_SYM_TERROR;
2688 lbm_array_header_t *arr;
2689
4/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 3 times.
51 if (arg_dec.is_valid && (arr = get_image_buffer(args[0]))) { //assignment
2690 image_buffer_t dest_buf;
2691 42 dest_buf.width = image_buffer_width((uint8_t*)arr->data);
2692 42 dest_buf.height = image_buffer_height((uint8_t*)arr->data);
2693 42 dest_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2694 42 dest_buf.mem_base = (uint8_t*)arr->data;
2695 42 dest_buf.data = image_buffer_data((uint8_t*)arr->data);
2696
2697 42 float scale = 1.0;
2698
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
42 if (arg_dec.attr_scale.is_valid) {
2699 14 scale = lbm_dec_as_float(arg_dec.attr_scale.args[0]);
2700 }
2701
2702 108 blit(
2703 &dest_buf,
2704 &arg_dec.img,
2705 lbm_dec_as_i32(arg_dec.args[0]),
2706 lbm_dec_as_i32(arg_dec.args[1]),
2707 lbm_dec_as_float(arg_dec.attr_rotate.args[0]),
2708 lbm_dec_as_float(arg_dec.attr_rotate.args[1]),
2709 lbm_dec_as_float(arg_dec.attr_rotate.args[2]),
2710 scale,
2711 lbm_dec_as_i32(arg_dec.args[2]),
2712 42 arg_dec.attr_tile.is_valid,
2713
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33 times.
42 arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[0]) : 0,
2714
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33 times.
42 arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[1]) : 0,
2715
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33 times.
42 arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[2]) : dest_buf.width,
2716
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33 times.
42 arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[3]) : dest_buf.height
2717 );
2718 42 res = ENC_SYM_TRUE;
2719 }
2720 51 return res;
2721 }
2722
2723 ✗ void display_dummy_reset(void) {
2724 ✗ return;
2725 }
2726
2727 ✗ void display_dummy_clear(uint32_t color) {
2728 (void) color;
2729 ✗ return;
2730 }
2731
2732 ✗ bool display_dummy_render_image(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) {
2733 (void) img;
2734 (void) x;
2735 (void) y;
2736 (void) colors;
2737 ✗ return false;
2738 }
2739
2740 static bool(* volatile disp_render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) = display_dummy_render_image;
2741 static void(* volatile disp_clear)(uint32_t color) = display_dummy_clear;
2742 static void(* volatile disp_reset)(void) = display_dummy_reset;
2743
2744 static char *msg_not_supported = "Command not supported or display driver not initialized";
2745
2746 1 static lbm_value ext_disp_reset(lbm_value *args, lbm_uint argn) {
2747 (void) args;
2748 (void) argn;
2749
2750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (disp_reset == NULL) {
2751 ✗ lbm_set_error_reason(msg_not_supported);
2752 ✗ return ENC_SYM_EERROR;
2753 }
2754
2755 1 disp_reset();
2756
2757 1 return ENC_SYM_TRUE;
2758 }
2759
2760 1 static lbm_value ext_disp_clear(lbm_value *args, lbm_uint argn) {
2761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (disp_clear == NULL) {
2762 ✗ lbm_set_error_reason(msg_not_supported);
2763 ✗ return ENC_SYM_EERROR;
2764 }
2765
2766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (argn > 1) {
2767 ✗ return ENC_SYM_TERROR;
2768 }
2769
2770 1 uint32_t clear_color = 0;
2771
2772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (argn == 1) {
2773 ✗ if (!lbm_is_number(args[0])) {
2774 ✗ return ENC_SYM_TERROR;
2775 }
2776
2777 ✗ clear_color = lbm_dec_as_u32(args[0]);
2778 }
2779
2780 1 disp_clear(clear_color);
2781
2782 1 return ENC_SYM_TRUE;
2783 }
2784
2785 50 static lbm_value ext_disp_render(lbm_value *args, lbm_uint argn) {
2786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (disp_render_image == NULL) {
2787 ✗ lbm_set_error_reason(msg_not_supported);
2788 ✗ return ENC_SYM_EERROR;
2789 }
2790
2791 50 lbm_value res = ENC_SYM_TERROR;
2792 lbm_array_header_t *arr;
2793
4/6
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
100 if ((argn == 3 || argn == 4) &&
2794
1/2
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
100 (arr = get_image_buffer(args[0])) &&
2795
1/2
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
100 lbm_is_number(args[1]) &&
2796 50 lbm_is_number(args[2])) {
2797 image_buffer_t img_buf;
2798 50 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2799 50 img_buf.width = image_buffer_width((uint8_t*)arr->data);
2800 50 img_buf.height = image_buffer_height((uint8_t*)arr->data);
2801 50 img_buf.mem_base = (uint8_t*)arr->data;
2802 50 img_buf.data = image_buffer_data((uint8_t*)arr->data);
2803
2804 color_t colors[16];
2805 50 memset(colors, 0, sizeof(color_t) * 16);
2806
2807
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 45 times.
✗ Branch 4 not taken.
50 if (argn == 4 && lbm_is_list(args[3])) {
2808 45 int i = 0;
2809 45 lbm_value curr = args[3];
2810
3/4
✓ Branch 1 taken 298 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 298 times.
✗ Branch 4 not taken.
343 while (lbm_is_cons(curr) && i < 16) {
2811 298 lbm_value arg = lbm_car(curr);
2812 color_t *color;
2813
2/2
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 8 times.
298 if (lbm_is_number(arg)) {
2814 290 colors[i].color1 = (int)lbm_dec_as_u32(arg);
2815
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 } else if ((color = get_color(arg))) { // color assignment
2816 8 colors[i] = *color;
2817 } else {
2818 ✗ return ENC_SYM_TERROR;
2819 }
2820
2821 298 curr = lbm_cdr(curr);
2822 298 i++;
2823 }
2824 }
2825
2826 // img_buf is a stack allocated image_buffer_t.
2827 50 bool render_res = disp_render_image(&img_buf, (uint16_t)lbm_dec_as_u32(args[1]), (uint16_t)lbm_dec_as_u32(args[2]), colors);
2828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!render_res) {
2829 ✗ lbm_set_error_reason("Could not render image. Check if the format and location is compatible with the display.");
2830 ✗ return ENC_SYM_EERROR;
2831 }
2832 50 res = ENC_SYM_TRUE;
2833 }
2834 50 return res;
2835 }
2836
2837 // Jpg decoder
2838
2839 typedef struct {
2840 uint8_t *data;
2841 int pos;
2842 int size;
2843 int ofs_x;
2844 int ofs_y;
2845 } jpg_bufdef;
2846
2847 105 size_t jpg_input_func (JDEC* jd, uint8_t* buff, size_t ndata) {
2848 105 jpg_bufdef *dev = (jpg_bufdef*)jd->device;
2849
2850
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 104 times.
105 if ((int)ndata > (dev->size - dev->pos)) {
2851 1 ndata = (size_t)(dev->size - dev->pos);
2852 }
2853
2854
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 3 times.
105 if (buff) {
2855 102 memcpy(buff, dev->data + dev->pos, ndata);
2856 }
2857 105 dev->pos += (int)ndata;
2858 105 return ndata;
2859 }
2860
2861 1530 int jpg_output_func ( /* 1:Ok, 0:Aborted */
2862 JDEC* jd, /* Decompression object */
2863 void* bitmap, /* Bitmap data to be output */
2864 JRECT* rect /* Rectangular region to output */
2865 ) {
2866 1530 jpg_bufdef *dev = (jpg_bufdef*)jd->device;
2867
2868 image_buffer_t img;
2869 1530 img.mem_base = (uint8_t*)bitmap;
2870 1530 img.data = (uint8_t*)bitmap;
2871 1530 img.width = (uint16_t)(rect->right - rect->left + 1);
2872 1530 img.height = (uint16_t)(rect->bottom - rect->top + 1);
2873 1530 img.fmt = rgb888;
2874
2875 1530 disp_render_image(&img, (uint16_t)(rect->left + dev->ofs_x), (uint16_t)(rect->top + dev->ofs_y), 0);
2876
2877 1530 return 1;
2878 }
2879
2880 1 static lbm_value ext_disp_render_jpg(lbm_value *args, lbm_uint argn) {
2881
2882 lbm_array_header_t *array;
2883 1 lbm_value res = ENC_SYM_TERROR;
2884
2885
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (argn == 3 &&
2886
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 (array = lbm_dec_array_r(args[0])) && //asignment
2887
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_number(args[1]) &&
2888 1 lbm_is_number(args[2])) {
2889
2890 JDEC jd;
2891 void *jdwork;
2892 // make a bit of room before the buffer.
2893 1 const size_t sz_work = 4096 + IMAGE_BUFFER_HEADER_SIZE;
2894
2895 1 jdwork = lbm_malloc(sz_work);
2896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!jdwork) {
2897 ✗ return ENC_SYM_MERROR;
2898 }
2899
2900
2901
2902 jpg_bufdef iodev;
2903 1 iodev.data = (uint8_t*)(array->data);
2904 1 iodev.size = (int)array->size;
2905 1 iodev.pos = 0;
2906 1 iodev.ofs_x = lbm_dec_as_i32(args[1]);
2907 1 iodev.ofs_y = lbm_dec_as_i32(args[2]);
2908 1 jd_prepare(&jd, jpg_input_func, jdwork, sz_work + IMAGE_BUFFER_HEADER_SIZE, &iodev);
2909 1 jd_decomp(&jd, jpg_output_func, 0);
2910 1 lbm_free(jdwork);
2911 1 res = ENC_SYM_TRUE;
2912 }
2913 1 return res;
2914 }
2915
2916 359 void lbm_display_extensions_init(void) {
2917 359 register_symbols();
2918
2919 359 disp_render_image = NULL;
2920 359 disp_clear = NULL;
2921 359 disp_reset = NULL;
2922
2923 359 lbm_add_extension("img-buffer", ext_image_buffer);
2924 359 lbm_add_extension("img-buffer?", ext_is_image_buffer);
2925 359 lbm_add_extension("img-color", ext_color);
2926 359 lbm_add_extension("img-color-set", ext_color_set);
2927 359 lbm_add_extension("img-color-get", ext_color_get);
2928 359 lbm_add_extension("img-color-setpre", ext_color_setpre);
2929 359 lbm_add_extension("img-color-getpre", ext_color_getpre);
2930 359 lbm_add_extension("img-dims", ext_image_dims);
2931 359 lbm_add_extension("img-setpix", ext_putpixel);
2932 359 lbm_add_extension("img-getpix", ext_getpixel);
2933 359 lbm_add_extension("img-line", ext_line);
2934 359 lbm_add_extension("img-text", ext_text);
2935 359 lbm_add_extension("img-clear", ext_clear);
2936 359 lbm_add_extension("img-circle", ext_circle);
2937 359 lbm_add_extension("img-arc", ext_arc);
2938 359 lbm_add_extension("img-circle-sector", ext_circle_sector);
2939 359 lbm_add_extension("img-circle-segment", ext_circle_segment);
2940 359 lbm_add_extension("img-rectangle", ext_rectangle);
2941 359 lbm_add_extension("img-triangle", ext_triangle);
2942 359 lbm_add_extension("img-blit", ext_blit);
2943
2944 359 lbm_add_extension("disp-reset", ext_disp_reset);
2945 359 lbm_add_extension("disp-clear", ext_disp_clear);
2946 359 lbm_add_extension("disp-render", ext_disp_render);
2947 359 lbm_add_extension("disp-render-jpg", ext_disp_render_jpg);
2948 359 }
2949
2950 65 void lbm_display_extensions_set_callbacks(
2951 bool(* volatile render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors),
2952 void(* volatile clear)(uint32_t color),
2953 void(* volatile reset)(void)
2954 ) {
2955
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 disp_render_image = render_image ? render_image : display_dummy_render_image;
2956
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 disp_clear = clear ? clear : display_dummy_clear;
2957
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 disp_reset = reset ? reset : display_dummy_reset;
2958 65 }
2959