GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/display_extensions.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 1567 1708 91.7%
Functions: 71 75 94.7%
Branches: 1031 1361 75.8%

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