GCC Code Coverage Report


Directory: ../src/
File: extensions/display_extensions.c
Date: 2026-07-02 10:10:49
Coverage Exec Excl Total
Lines: 93.6% 1599 0 1708
Functions: 94.7% 71 0 75
Branches: 78.0% 1061 0 1361

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 1 taken 24 times.
✗ Branch 2 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 1 taken 24 times.
✗ Branch 2 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 93 color_format_t sym_to_color_format(lbm_value v) {
141 93 lbm_uint s = lbm_dec_sym(v);
142
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 66 times.
93 if (s == symbol_indexed2) return indexed2;
143
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 35 times.
66 if (s == symbol_indexed4) return indexed4;
144
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 20 times.
35 if (s == symbol_indexed16) return indexed16;
145
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (s == symbol_rgb332) return rgb332;
146
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (s == symbol_rgb565) return rgb565;
147
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 if (s == symbol_rgb888) return rgb888;
148 3 return format_not_supported;
149 }
150
151 2812 uint32_t image_dims_to_size_bytes(color_format_t fmt, uint16_t width, uint16_t height) {
152 2812 uint32_t num_pix = (uint32_t)width * (uint32_t)height;
153
7/7
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 2627 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 4 times.
2812 switch(fmt) {
154 113 case indexed2:
155
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 56 times.
113 if (num_pix % 8 != 0) return (num_pix / 8) + 1;
156 56 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 26 case rgb888:
172 26 return num_pix * 3;
173 4 default:
174 4 return 0;
175 }
176 }
177
178 72 static lbm_value image_buffer_lift(uint8_t *buf, color_format_t fmt, uint16_t width, uint16_t height) {
179 72 lbm_value res = ENC_SYM_MERROR;
180 72 lbm_uint size = image_dims_to_size_bytes(fmt, width, height);
181
1/2
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
72 if ( lbm_lift_array(&res, (char*)buf, IMAGE_BUFFER_HEADER_SIZE + size)) {
182 72 buf[0] = (uint8_t)(width >> 8);
183 72 buf[1] = (uint8_t)width;
184 72 buf[2] = (uint8_t)(height >> 8);
185 72 buf[3] = (uint8_t)height;
186 72 buf[4] = color_format_to_byte(fmt);
187 }
188 72 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 1 taken 71 times.
✗ Branch 2 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 time.
✓ 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 1 taken 26 times.
✗ Branch 2 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 time.
5 if (type == COLOR_PRE_X) {
236 4 color->type = COLOR_GRADIENT_X;
237
1/2
✓ Branch 0 taken 1 time.
✗ 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 time.
✓ 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 72 static lbm_value image_buffer_allocate(color_format_t fmt, uint16_t width, uint16_t height) {
260 72 uint32_t size_bytes = image_dims_to_size_bytes(fmt, width, height);
261
262 72 uint8_t *buf = lbm_malloc(IMAGE_BUFFER_HEADER_SIZE + size_bytes);
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (!buf) {
264 ✗ return ENC_SYM_MERROR;
265 }
266 72 memset(buf, 0, size_bytes + IMAGE_BUFFER_HEADER_SIZE);
267 72 lbm_value res = image_buffer_lift(buf, fmt, width, height);
268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 if (lbm_is_symbol(res)) { /* something is wrong, free */
269 ✗ lbm_free(buf);
270 }
271 72 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 time.
✗ 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 time.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 5 times.
77 if (array && is_color_sized(array->size)
304
1/2
✓ Branch 1 taken 71 times.
✗ Branch 2 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 378 static bool register_symbols(void) {
314 378 bool res = true;
315
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("indexed2", &symbol_indexed2);
316
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("indexed4", &symbol_indexed4);
317
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("indexed16", &symbol_indexed16);
318
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("rgb332", &symbol_rgb332);
319
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("rgb565", &symbol_rgb565);
320
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("rgb888", &symbol_rgb888);
321
322
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("thickness", &symbol_thickness);
323
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("filled", &symbol_filled);
324
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("rounded", &symbol_rounded);
325
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("dotted", &symbol_dotted);
326
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("scale", &symbol_scale);
327
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("rotate", &symbol_rotate);
328
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("resolution", &symbol_resolution);
329
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("tile", &symbol_tile);
330
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("clip", &symbol_clip);
331
332
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("regular", &symbol_regular);
333
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("gradient_x", &symbol_gradient_x);
334
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("gradient_y", &symbol_gradient_y);
335
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("gradient_x_pre", &symbol_gradient_x_pre);
336
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("gradient_y_pre", &symbol_gradient_y_pre);
337
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("mirrored", &symbol_mirrored);
338
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("repeat", &symbol_repeat);
339
340
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("color-0", &symbol_color_0);
341
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("color-1", &symbol_color_1);
342
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("width", &symbol_width);
343
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("offset", &symbol_offset);
344
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("repeat-type", &symbol_repeat_type);
345
346
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("down", &symbol_down);
347
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("up", &symbol_up);
348
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("magnify", &symbol_magnify);
349
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("spacing", &symbol_spacing);
350
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("align", &symbol_align);
351
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("left", &symbol_left);
352
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("center", &symbol_center);
353
2/4
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 378 times.
✗ Branch 4 not taken.
378 res = res && lbm_add_symbol_const("right", &symbol_right);
354
355 378 return res;
356 }
357
358 // Internal functions
359
360 973051 static int sign(int v) {
361
2/2
✓ Branch 0 taken 454132 times.
✓ Branch 1 taken 518919 times.
973051 if (v > 0) {
362 454132 return 1;
363
2/2
✓ Branch 0 taken 372779 times.
✓ Branch 1 taken 146140 times.
518919 } else if (v < 0) {
364 372779 return -1;
365 } else {
366 146140 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 201738 static int point_past_line(int x, int y, int line_start_x, int line_start_y, int line_end_x, int line_end_y) {
379 // source: https://stackoverflow.com/a/11908158/15507414
380
381 // this is not really a cross product, but whatever...
382 201738 int cross_prod = (x - line_start_x) * (line_end_y - line_start_y)
383 201738 - (y - line_start_y) * (line_end_x - line_start_x);
384
385
2/2
✓ Branch 0 taken 86264 times.
✓ Branch 1 taken 115474 times.
201738 if (cross_prod > 0) {
386 86264 return 1;
387
2/2
✓ Branch 0 taken 111536 times.
✓ Branch 1 taken 3938 times.
115474 } else if (cross_prod < 0) {
388 111536 return -1;
389 } else {
390 3938 return 0;
391 }
392 }
393
394 150802 static bool points_same_quadrant(int x0, int y0, int x1, int y1) {
395
4/4
✓ Branch 3 taken 90596 times.
✓ Branch 4 taken 3524 times.
✓ Branch 6 taken 30182 times.
✓ Branch 7 taken 60414 times.
244922 return (sign(x0) == sign(x1) || sign(x0) == 0 || sign(x1) == 0)
396
8/8
✓ Branch 0 taken 94120 times.
✓ Branch 1 taken 56682 times.
✓ Branch 4 taken 47454 times.
✓ Branch 5 taken 42934 times.
✓ Branch 7 taken 42513 times.
✓ Branch 8 taken 4941 times.
✓ Branch 10 taken 18408 times.
✓ Branch 11 taken 24105 times.
244922 && (sign(y0) == sign(y1) || sign(y0) == 0 || sign(y1) == 0);
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 22591162 void putpixel(image_buffer_t* img, int x_i, int y_i, uint32_t c) {
545 22591162 uint16_t w = img->width;
546 22591162 uint16_t h = img->height;
547 22591162 uint16_t x = (uint16_t)x_i; // negative numbers become really large.
548 22591162 uint16_t y = (uint16_t)y_i;
549
550
4/4
✓ Branch 0 taken 22553320 times.
✓ Branch 1 taken 37842 times.
✓ Branch 2 taken 20032982 times.
✓ Branch 3 taken 2520338 times.
22591162 if (x < w && y < h) {
551 20032982 color_format_t fmt = img->fmt;
552 20032982 uint8_t *data = img->data;
553
6/7
✓ Branch 0 taken 100226 times.
✓ Branch 1 taken 17820693 times.
✓ Branch 2 taken 2047628 times.
✓ Branch 3 taken 1730 times.
✓ Branch 4 taken 1943 times.
✓ Branch 5 taken 60762 times.
✗ Branch 6 not taken.
20032982 switch(fmt) {
554 100226 case indexed2: {
555 100226 uint32_t pos = (uint32_t)y * (uint32_t)w + (uint32_t)x;
556 100226 uint32_t byte = pos >> 3;
557 100226 uint32_t bit = 7 - (pos & 0x7);
558
2/2
✓ Branch 0 taken 9615 times.
✓ Branch 1 taken 90611 times.
100226 if (c) {
559 9615 data[byte] |= (uint8_t)(1 << bit);
560 } else {
561 90611 data[byte] &= (uint8_t)~(1 << bit);
562 }
563 100226 break;
564 }
565 17820693 case indexed4: {
566 17820693 uint32_t pos = (uint32_t)y*w + x;
567 17820693 uint32_t byte = pos >> 2;
568 17820693 uint32_t ix = 3 - (pos & 0x3);
569 17820693 data[byte] = (uint8_t)((uint8_t)(data[byte] & ~indexed4_mask[ix]) | (uint8_t)(c << indexed4_shift[ix]));
570 17820693 break;
571 }
572 2047628 case indexed16: {
573 2047628 uint32_t pos = (uint32_t)y*w + x;
574 2047628 uint32_t byte = pos >> 1;
575 2047628 uint32_t ix = 1 - (pos & 0x1);
576 2047628 data[byte] = (uint8_t)((uint8_t)(data[byte] & ~indexed16_mask[ix]) | (uint8_t)(c << indexed16_shift[ix]));
577 2047628 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 60762 case rgb888: {
592 60762 int pos = y*(w*3) + (x*3);
593 60762 data[pos] = (uint8_t)(c>>16);
594 60762 data[pos+1] = (uint8_t)(c>>8);
595 60762 data[pos+2] = (uint8_t)c;
596 60762 break;
597 }
598 ✗ default:
599 ✗ break;
600 }
601 }
602 22591162 }
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 159 static int parse_text_attr(lbm_value v, float *mag, int *spacing, int *align, int *rotation_deg) {
632
2/2
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 26 times.
159 if (!lbm_is_cons(v)) {
633 133 return 0;
634 }
635
636 26 lbm_value key = lbm_car(v);
637 26 lbm_value rest = lbm_cdr(v);
638
2/4
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
26 if (!lbm_is_symbol(key) || !lbm_is_cons(rest)) {
639 ✗ return 0;
640 }
641
642 26 lbm_value val = lbm_car(rest);
643
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 if (lbm_cdr(rest) != ENC_SYM_NIL) {
644 ✗ return -1;
645 }
646
647 26 lbm_uint sym = lbm_dec_sym(key);
648
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
26 if (sym == symbol_magnify || sym == symbol_scale) {
649
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (!lbm_is_number(val)) return -1;
650 15 float m = lbm_dec_as_float(val);
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (m < 0.1f) m = 0.1f;
652 15 *mag = m;
653 15 return 1;
654 }
655
656
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (sym == symbol_spacing) {
657
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!lbm_is_number(val)) return -1;
658 2 *spacing = lbm_dec_as_i32(val);
659 2 return 1;
660 }
661
662
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (sym == symbol_align) {
663
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 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
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (lbm_is_symbol(val)) {
671 4 lbm_uint av = lbm_dec_sym(val);
672
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (av == symbol_left) { *align = 0; return 1; }
673
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (av == symbol_center) { *align = 1; return 1; }
674
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 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
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (sym == symbol_rotate) {
687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (!lbm_is_number(val)) return -1;
688 5 int r = lbm_dec_as_i32(val);
689 5 *rotation_deg = r % 360;
690 5 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 483 times.
✓ Branch 1 taken 547 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 19228 times.
36154 if (thickness > 1) {
978 16926 fill_circle(img, x0, y0, thickness, c);
979 } else {
980 19228 putpixel(img, x0, y0, c);
981 }
982
983
4/4
✓ Branch 0 taken 11077 times.
✓ Branch 1 taken 25077 times.
✓ Branch 2 taken 520 times.
✓ Branch 3 taken 10557 times.
36154 if (x0 == x1 && y0 == y1) {
984 520 break;
985 }
986
2/2
✓ Branch 0 taken 23883 times.
✓ Branch 1 taken 11751 times.
35634 if ((error * 2) >= dy) {
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23883 times.
23883 if (x0 == x1) {
988 ✗ break;
989 }
990 23883 error += dy;
991 23883 x0 += sx;
992 }
993
2/2
✓ Branch 0 taken 15141 times.
✓ Branch 1 taken 20493 times.
35634 if ((error * 2) <= dx) {
994
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15114 times.
15141 if (y0 == y1) {
995 27 break;
996 }
997 15114 error += dx;
998 15114 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 time.
✓ 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 time.
✓ 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 12982 times.
✓ Branch 3 taken 21404 times.
42976 if (y > max_y || y < min_y) {
1124 21572 return;
1125 }
1126
1127 21404 int line_is_past_0 = point_past_line(x, y, 0, 0, cap0_x, cap0_y);
1128 21404 int line_is_past_1 = -point_past_line(x, y, 0, 0, cap1_x, cap1_y);
1129
1130 21404 bool in_cap0_quadrant = points_same_quadrant(
1131 x, y, cap0_x, cap0_y);
1132 21404 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 18606 times.
21404 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 14118 times.
✓ Branch 1 taken 4488 times.
✓ Branch 2 taken 9627 times.
✓ Branch 3 taken 4491 times.
18606 if (line_is_past_0 == 1 || line_is_past_1 == 1
1141
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 9531 times.
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 8 times.
9627 || (line_is_past_0 == 0 && !in_cap0_quadrant)
1142
4/4
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 9510 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 104 times.
9619 || (line_is_past_1 == 0 && !in_cap1_quadrant)) {
1143 8992 return;
1144 }
1145 }
1146
1147 12253 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 time.
✓ 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 time.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
28 if ((angle0 < M_3PI_2 && angle1 >= M_PI_2)
1197
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 time.
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 time.
✓ Branch 1 taken 9988 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
9989 || (line_is_past_0 == 0 && !in_cap0_quadrant)
1328
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9987 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
9988 || (line_is_past_1 == 0 && !in_cap1_quadrant)) {
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 // TODO: look into this:
1403 // end_is_past0!=0 is always true.
1404 // end_is_part1!=0 is always true.
1405 4035 bool slice_overlaps0 = start_is_past0 != end_is_past0
1406
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);
1407 4035 bool slice_overlaps1 = start_is_past1 != end_is_past1
1408
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);
1409
1410
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)
1411
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)
1412
5/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3671 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
3693 || (in_both_caps && !angle_is_closed && (
1413 ✗ (start_is_past0 == 1 && end_is_past0 == 1)
1414
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 || (start_is_past1 == 1 && end_is_past1 == 1)
1415 ))
1416
6/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3671 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 6 times.
3693 || (in_both_caps && angle_is_closed && (
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 (start_is_past0 == 1 && end_is_past0 == 1)
1418 ✗ && (start_is_past1 == 1 && end_is_past1 == 1)
1419 ))) {
1420 342 return;
1421 }
1422
1423 // The repetition in all these cases could probably be reduced...
1424
6/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3671 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 1 time.
3693 if ((in_both_caps && slice_overlaps0 && !slice_overlaps1)
1425
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)) {
1426 // intersect with cap line 0
1427
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) {
1428
2/2
✓ Branch 0 taken 9536 times.
✓ Branch 1 taken 666 times.
10202 while (start_is_past0 == 1) {
1429 9536 x_start += 1;
1430 9536 start_is_past0 = point_past_line(x_start, outer_y,
1431 0, 0,
1432 outer_x0, outer_y0);
1433 }
1434 } else {
1435
2/2
✓ Branch 0 taken 9128 times.
✓ Branch 1 taken 802 times.
9930 while (end_is_past0 == 1) {
1436 9128 x_end -= 1;
1437 9128 end_is_past0 = point_past_line(x_end, outer_y,
1438 0, 0,
1439 outer_x0, outer_y0);
1440 }
1441 }
1442
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)
1443
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)) {
1444 // intersect with cap line 1
1445
4/4
✓ Branch 0 taken 1250 times.
✓ Branch 1 taken 359 times.
✓ Branch 2 taken 1249 times.
✓ Branch 3 taken 1 time.
1609 if (start_is_past1 != -1 && end_is_past1 != 1) {
1446
2/2
✓ Branch 0 taken 14004 times.
✓ Branch 1 taken 1249 times.
15253 while (start_is_past1 == 1) {
1447 14004 x_start += 1;
1448 14004 start_is_past1 = -point_past_line(x_start, outer_y,
1449 0, 0,
1450 outer_x1, outer_y1);
1451 }
1452 } else {
1453
2/2
✓ Branch 0 taken 5076 times.
✓ Branch 1 taken 360 times.
5436 while (end_is_past1 == 1) {
1454 5076 x_end -= 1;
1455 5076 end_is_past1 = -point_past_line(x_end, outer_y,
1456 0, 0,
1457 outer_x1, outer_y1);
1458 }
1459 }
1460
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) {
1461 // intersect with both cap lines
1462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (angle0 < angle1) {
1463 ✗ if (angle0 < M_PI) {
1464 ✗ while (start_is_past1 == 1) {
1465 ✗ x_start += 1;
1466 ✗ start_is_past1 = -point_past_line(x_start, outer_y,
1467 0, 0,
1468 outer_x1, outer_y1);
1469 }
1470 ✗ while (end_is_past0 == 1) {
1471 ✗ x_end -= 1;
1472 ✗ end_is_past0 = point_past_line(x_end, outer_y,
1473 0, 0,
1474 outer_x0, outer_y0);
1475 }
1476 } else {
1477 ✗ while (start_is_past0 == 1) {
1478 ✗ x_start += 1;
1479 ✗ start_is_past0 = point_past_line(x_start, outer_y,
1480 0, 0,
1481 outer_x0, outer_y0);
1482 }
1483 ✗ while (end_is_past1 == 1) {
1484 ✗ x_end -= 1;
1485 ✗ end_is_past1 = -point_past_line(x_end, outer_y,
1486 0, 0,
1487 outer_x1, outer_y1);
1488 }
1489 }
1490 } else {
1491 // split the slice into two
1492
1493 14 slice_is_split = true;
1494
1495 14 int x_start1 = x_start;
1496 14 int x_end1 = x_end;
1497
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (angle0 < M_PI) {
1499 ✗ while (end_is_past0 == 1) {
1500 ✗ x_end -= 1;
1501 ✗ end_is_past0 = point_past_line(x_end, outer_y,
1502 0, 0,
1503 outer_x0, outer_y0);
1504 }
1505 ✗ while (start_is_past1 == 1) {
1506 ✗ x_start1 += 1;
1507 ✗ start_is_past1 = -point_past_line(x_start1, outer_y,
1508 0, 0,
1509 outer_x1, outer_y1);
1510 }
1511 } else {
1512
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 14 times.
328 while (end_is_past1 == 1) {
1513 314 x_end1 -= 1;
1514 314 end_is_past1 = -point_past_line(x_end1, outer_y,
1515 0, 0,
1516 outer_x1, outer_y1);
1517 }
1518
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 14 times.
376 while (start_is_past0 == 1) {
1519 362 x_start += 1;
1520 362 start_is_past0 = point_past_line(x_start, outer_y,
1521 0, 0,
1522 outer_x0, outer_y0);
1523 }
1524 }
1525
1526 14 x1 = x_start1;
1527 14 width1 = x_end1 + 1 - x_start1 ;
1528 }
1529 }
1530 3693 x = x_start;
1531 3693 width = x_end + 1 - x_start;
1532
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) {
1533 // the range from x_start to x_end is *inclusive*
1534 1298 int x_start = x;
1535 1298 int x_end = x_start + width - 1;
1536
1537 // when a point is "past" a line, it is on the wrong cleared side of it
1538 1298 int start_is_past = -point_past_line(x_start, outer_y,
1539 outer_x0, outer_y0, outer_x1, outer_y1);
1540 1298 int end_is_past = -point_past_line(x_end, outer_y,
1541 outer_x0, outer_y0, outer_x1, outer_y1);
1542
1543 1298 bool slice_overlaps = start_is_past != end_is_past
1544
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);
1545
1546
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) {
1547 455 return;
1548 }
1549
1550
2/2
✓ Branch 0 taken 610 times.
✓ Branch 1 taken 233 times.
843 if (slice_overlaps) {
1551
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) {
1552
2/2
✓ Branch 0 taken 5815 times.
✓ Branch 1 taken 285 times.
6100 while (start_is_past == 1) {
1553 5815 x_start += 1;
1554 5815 start_is_past = -point_past_line(x_start, outer_y,
1555 outer_x0, outer_y0, outer_x1, outer_y1);
1556 }
1557 } else {
1558
2/2
✓ Branch 0 taken 6497 times.
✓ Branch 1 taken 325 times.
6822 while (end_is_past == 1) {
1559 6497 x_end -= 1;
1560 6497 end_is_past = -point_past_line(x_end, outer_y,
1561 outer_x0, outer_y0, outer_x1, outer_y1);
1562 }
1563 }
1564 }
1565
1566 843 x = x_start;
1567 843 width = x_end + 1 - x_start;
1568 }
1569
1570 28252 h_line(img, c_x + x, c_y + outer_y, width, color);
1571
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28238 times.
28252 if (slice_is_split) {
1572 14 h_line(img, c_x + x1, c_y + outer_y, width1, color);
1573 }
1574 }
1575
1576 // TODO: Fix unwanted slice with angles 130 to 115 (I think, angles might be
1577 // slightly off).
1578 // TODO: Look into buggy rendering with angles around 180°-270°. This seems to
1579 // affect arcs, sectors, and segments likewise.
1580 545 static void arc(image_buffer_t *img, int c_x, int c_y, int radius, float angle0, float angle1,
1581 int thickness, bool rounded, bool filled, bool sector, bool segment, int dot1, int dot2, int resolution, uint32_t color) {
1582
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) {
1583 12 thickness /= 2;
1584
1585 12 radius -= thickness;
1586
1587
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (thickness == 0) {
1588 12 thickness = 1;
1589 }
1590
1591 12 generic_arc(img, c_x, c_y, radius, angle0, angle1, thickness, dot1, dot2, resolution, sector, segment, color);
1592
1593 12 return;
1594 }
1595
1596
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) {
1597 166 thin_arc(img, c_x, c_y, radius, angle0, angle1, sector, segment, color);
1598
1599 166 return;
1600 }
1601
1602
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 360 times.
367 if (radius == 0) {
1603 7 return;
1604 }
1605
1606 360 angle0 *= (float)M_PI / 180.0f;
1607 360 angle1 *= (float)M_PI / 180.0f;
1608 360 norm_angle_0_2pi(&angle0); // theses are probably unecessary?
1609 360 norm_angle_0_2pi(&angle1); // but who knows with floating point imprecision...
1610
1611
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 354 times.
360 if (angle0 == angle1) {
1612 6 return;
1613 }
1614
1615 bool angle_is_closed;
1616 // if the angle of the filled in part of the arc is greater than 180°
1617
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 102 times.
354 if (angle1 - angle0 > 0.0) {
1618 252 angle_is_closed = fabsf(angle1 - angle0) > M_PI;
1619 } else {
1620 102 angle_is_closed = fabsf(angle1 - angle0) < M_PI;
1621 }
1622
1623 // angles smaller than 1 degree seem to cause issues (with a radius of 62)
1624 // this is kinda ugly though, and it will probably still break at larger
1625 // radii or something...
1626
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
1627
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 9 times.
23 if (rounded) {
1628 14 float rad_f = (float)radius - ((float)thickness / 2.0f);
1629
1630 14 float angle = (angle0 + angle1) / 2.0f;
1631
1632 14 int cap_center_x = (int)floorf(cosf(angle) * rad_f);
1633 14 int cap_center_y = (int)floorf(sinf(angle) * rad_f);
1634
1635 14 fill_circle(img, c_x + cap_center_x, c_y + cap_center_y, thickness / 2, color);
1636 }
1637 23 return;
1638 }
1639
1640
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 329 times.
331 if (thickness >= radius) {
1641 2 filled = true;
1642 }
1643
1644 int radius_outer, radius_inner;
1645
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 254 times.
331 if (filled) {
1646 77 radius_outer = radius;
1647 77 radius_inner = 0;
1648 } else {
1649 254 radius_outer = radius;
1650 254 radius_inner = radius - thickness;
1651 }
1652 331 int radius_outer_dbl_sq = radius_outer * radius_outer * 4;
1653 331 int radius_inner_dbl_sq = radius_inner * radius_inner * 4;
1654
1655 331 float angle0_cos = cosf(angle0);
1656 331 float angle0_sin = sinf(angle0);
1657 331 float angle1_cos = cosf(angle1);
1658 331 float angle1_sin = sinf(angle1);
1659
1660 331 int outer_x0 = (int)(angle0_cos * (float)radius_outer);
1661 331 int outer_y0 = (int)(angle0_sin * (float)radius_outer);
1662
1663 331 int outer_x1 = (int)(angle1_cos * (float)radius_outer);
1664 331 int outer_y1 = (int)(angle1_sin * (float)radius_outer);
1665
1666 int inner_y0;
1667 int inner_y1;
1668
1669
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 254 times.
331 if (filled) {
1670 77 inner_y0 = 0;
1671
1672 77 inner_y1 = 0;
1673 } else {
1674 254 inner_y0 = (int)(angle0_sin * (float)radius_inner);
1675
1676 254 inner_y1 = (int)(angle1_sin * (float)radius_inner);
1677 }
1678
1679 331 int cap0_min_y = MIN(inner_y0, outer_y0);
1680 331 int cap0_max_y = MAX(inner_y0, outer_y0);
1681
1682 331 int cap1_min_y = MIN(inner_y1, outer_y1);
1683 331 int cap1_max_y = MAX(inner_y1, outer_y1);
1684
1685 // Highest and lowest (y coord wise) drawn line of the base arc (excluding
1686 // the circular end caps). This range is *inclusive*!
1687 // Note that these might be slightly off due to inconsistent rounding between
1688 // Bresenhamn's algorithm and point rotation. (I don't think the point about
1689 // Bresenhamn is relevant as we don't use it anymore. Still wouldn't trust
1690 // them completely though...)
1691 331 int min_y = MIN(outer_y0, MIN(outer_y1, MIN(inner_y0, inner_y1)));
1692 331 int max_y = MAX(outer_y0, MAX(outer_y1, MAX(inner_y0, inner_y1)));
1693
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 102 times.
331 if (angle0 < angle1) {
1694
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) {
1695 17 min_y = -radius_outer;
1696 17 max_y = radius_outer;
1697
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) {
1698 40 min_y = -radius_outer;
1699
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) {
1700 153 max_y = radius_outer;
1701 }
1702 } else {
1703
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)
1704
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1 time.
93 || (angle0 < M_PI_2)
1705
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 91 times.
92 || (angle1 > M_3PI_2)) {
1706 11 min_y = -radius_outer;
1707 11 max_y = radius_outer;
1708
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) {
1709 24 min_y = -radius_outer;
1710
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) {
1711 17 max_y = radius_outer;
1712 }
1713 }
1714
1715
2/2
✓ Branch 0 taken 16135 times.
✓ Branch 1 taken 331 times.
16466 for (int y = 0; y < radius_outer; y++) {
1716 16135 int y_dbl_offs = 2 * (y + 1);
1717 16135 int y_dbl_offs_sq = y_dbl_offs * y_dbl_offs;
1718
1719
1/2
✓ Branch 0 taken 223585 times.
✗ Branch 1 not taken.
223585 for (int x = -radius_outer; x <= 0; x++) {
1720 223585 int x_dbl_offs = 2 * (x + 1);
1721
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) {
1722 // This is horrible...
1723 16135 handle_arc_slice(img, x, y,
1724 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1725 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1726 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1727 16135 handle_arc_slice(img, -x - 1, y,
1728 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1729 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1730 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1731
1732 16135 handle_arc_slice(img, x, -y - 1,
1733 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1734 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1735 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1736 16135 handle_arc_slice(img, -x - 1, -y - 1,
1737 c_x, c_y, color, outer_x0, outer_y0, outer_x1, outer_y1,
1738 cap0_min_y, cap0_max_y, cap1_min_y, cap1_max_y, radius_outer, radius_inner, min_y, max_y,
1739 angle0, angle1, angle_is_closed, filled, segment, radius_inner_dbl_sq);
1740
1741 16135 break;
1742 }
1743 }
1744 }
1745
1746 // draw rounded line corners
1747
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) {
1748 34 float rad_f = (float)radius - ((float)thickness / 2.0f);
1749
1750 34 int cap0_center_x = (int)floorf(angle0_cos * rad_f);
1751 34 int cap0_center_y = (int)floorf(angle0_sin * rad_f);
1752
1753 34 int cap1_center_x = (int)floorf(angle1_cos * rad_f);
1754 34 int cap1_center_y = (int)floorf(angle1_sin * rad_f);
1755
1756 34 thickness /= 2;
1757
1758 34 fill_circle(img, c_x + cap0_center_x, c_y + cap0_center_y, thickness, color);
1759 34 fill_circle(img, c_x + cap1_center_x, c_y + cap1_center_y, thickness, color);
1760 }
1761
1762 // draw sector arc cap to center lines
1763 // (sectors are always rounded)
1764
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) {
1765 3 float rad_f = (float)radius - ((float)thickness / 2.0f);
1766
1767 3 int cap0_center_x = (int)floorf(angle0_cos * rad_f);
1768 3 int cap0_center_y = (int)floorf(angle0_sin * rad_f);
1769
1770 3 int cap1_center_x = (int)floorf(angle1_cos * rad_f);
1771 3 int cap1_center_y = (int)floorf(angle1_sin * rad_f);
1772
1773 3 thickness /= 2;
1774
1775 3 line(img, c_x + cap0_center_x, c_y + cap0_center_y,
1776 c_x, c_y, thickness, 0, 0, color);
1777 3 line(img, c_x + cap1_center_x, c_y + cap1_center_y,
1778 c_x, c_y, thickness, 0, 0, color);
1779 }
1780
1781
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) {
1782 200 float rad_f = (float)radius - ((float)thickness / 2.0f);
1783
1784 200 int cap0_center_x = (int)floorf(angle0_cos * rad_f);
1785 200 int cap0_center_y = (int)floorf(angle0_sin * rad_f);
1786
1787 200 int cap1_center_x = (int)floorf(angle1_cos * rad_f);
1788 200 int cap1_center_y = (int)floorf(angle1_sin * rad_f);
1789
1790 200 thickness /= 2;
1791
1792 200 line(img, c_x + cap0_center_x, c_y + cap0_center_y,
1793 c_x + cap1_center_x, c_y + cap1_center_y, thickness, 0, 0, color);
1794 }
1795 }
1796
1797 // orient: 0=normal, 1=up(90°CCW), 2=180°, 3=down(90°CW)
1798 851 static void img_putc(image_buffer_t *img, int x, int y, uint32_t *colors, int num_colors,
1799 uint8_t *font_data, uint8_t ch, int orient, float mag) {
1800 851 uint8_t w = font_data[0];
1801 851 uint8_t h = font_data[1];
1802 851 uint8_t char_num = font_data[2];
1803 851 uint8_t bits_per_pixel = font_data[3];
1804
1805 851 int pixels_per_byte = (int)(8 / bits_per_pixel);
1806 851 int bytes_per_char = (int)((w * h) / pixels_per_byte);
1807
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 742 times.
851 if ((w * h) % pixels_per_byte != 0) {
1808 109 bytes_per_char += 1;
1809 }
1810
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 851 times.
851 if (char_num == 10) {
1812 ✗ ch = (uint8_t)(ch - '0');
1813 } else {
1814 851 ch = (uint8_t)(ch - ' ');
1815 }
1816
1817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 851 times.
851 if (ch >= char_num) {
1818 ✗ return;
1819 }
1820
1821
2/2
✓ Branch 0 taken 312487 times.
✓ Branch 1 taken 851 times.
313338 for (int i = 0; i < w * h; i++) {
1822 312487 int x0 = i % w;
1823 312487 int y0 = i / w;
1824
1825 312487 int sx0 = (int)floorf((float)x0 * mag);
1826 312487 int sx1 = (int)floorf((float)(x0 + 1) * mag) - 1;
1827 312487 int sy0 = (int)floorf((float)y0 * mag);
1828 312487 int sy1 = (int)floorf((float)(y0 + 1) * mag) - 1;
1829
1830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312487 times.
312487 if (sx1 < sx0) sx1 = sx0;
1831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312487 times.
312487 if (sy1 < sy0) sy1 = sy0;
1832
1833 uint32_t color;
1834
2/2
✓ Branch 0 taken 217984 times.
✓ Branch 1 taken 94503 times.
312487 if (bits_per_pixel == 2) {
1835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217984 times.
217984 if (num_colors < 4) return;
1836 217984 uint8_t byte = font_data[4 + bytes_per_char * ch + (i / 4)];
1837 217984 uint8_t bit_pos = (uint8_t)(i % pixels_per_byte);
1838 217984 uint8_t pixel_value = (byte >> (bit_pos * 2)) & 0x03;
1839 217984 color = colors[pixel_value];
1840 } else {
1841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94503 times.
94503 if (num_colors < 1) return;
1842 94503 int32_t fg = (int32_t)colors[0];
1843
1/2
✓ Branch 0 taken 94503 times.
✗ Branch 1 not taken.
94503 int32_t bg = (num_colors > 1) ? (int32_t)colors[1] : -1;
1844 94503 uint8_t byte = font_data[4 + bytes_per_char * ch + (i / 8)];
1845 94503 uint8_t bit_pos = (uint8_t)(i % 8);
1846 94503 uint8_t bit = (uint8_t)(byte & (1 << bit_pos));
1847
3/4
✓ Branch 0 taken 80204 times.
✓ Branch 1 taken 14299 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80204 times.
94503 if (!bit && bg < 0) {
1848 ✗ continue;
1849 }
1850
2/2
✓ Branch 0 taken 14299 times.
✓ Branch 1 taken 80204 times.
94503 color = bit ? (uint32_t)fg : (uint32_t)bg;
1851 }
1852
1853
2/2
✓ Branch 0 taken 323394 times.
✓ Branch 1 taken 312487 times.
635881 for (int py = sy0; py <= sy1; py++) {
1854
2/2
✓ Branch 0 taken 368034 times.
✓ Branch 1 taken 323394 times.
691428 for (int px = sx0; px <= sx1; px++) {
1855
4/4
✓ Branch 0 taken 38160 times.
✓ Branch 1 taken 3360 times.
✓ Branch 2 taken 45792 times.
✓ Branch 3 taken 280722 times.
368034 switch (orient) {
1856 38160 case 1: putpixel(img, x + py, y - px, color); break;
1857 3360 case 2: putpixel(img, x - px, y - py, color); break;
1858 45792 case 3: putpixel(img, x - py, y + px, color); break;
1859 280722 default: putpixel(img, x + px, y + py, color); break;
1860 }
1861 }
1862 }
1863 }
1864 }
1865
1866 4386100 static inline void copy_pixel(
1867 image_buffer_t *img_dest,
1868 image_buffer_t *img_src,
1869 int dest_x, int dest_y,
1870 int src_x, int src_y,
1871 int src_w, int src_h,
1872 int transparent_color,
1873 bool tile
1874 ) {
1875
2/2
✓ Branch 0 taken 923300 times.
✓ Branch 1 taken 3462800 times.
4386100 if (tile) {
1876 923300 src_x = src_x % src_w;
1877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923300 times.
923300 if (src_x < 0) src_x = src_x + src_w;
1878 923300 src_y = src_y % src_h;
1879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923300 times.
923300 if (src_y < 0) src_y = src_y + src_h;
1880 }
1881
1882
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) {
1883 1013488 uint32_t p = getpixel(img_src, src_x, src_y);
1884
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) {
1885 945107 putpixel(img_dest, dest_x, dest_y, p);
1886 }
1887 }
1888 4386100 }
1889
1890 // Copy pixels from source to destination with transformations
1891 42 void blit(
1892 image_buffer_t *img_dest, // Destination image buffer
1893 image_buffer_t *img_src, // Source image buffer
1894 int dest_offset_x, int dest_offset_y, // Where on dest to start writing pixels
1895 float rot_x, float rot_y, // Coordinate in src to rotate around
1896 float rot_angle, // Rotation angle in degrees
1897 float scale, // Scale factor
1898 int32_t transparent_color, // Color that will not be drawn -1 to disable
1899 bool tile, // Tile src to fill dest
1900 int clip_x, int clip_y, // Clip start in dest
1901 int clip_w, int clip_h // Clip width and height
1902 ) {
1903
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 41 times.
42 if (scale == 0.0) return;
1904 41 int src_w = img_src->width;
1905 41 int src_h = img_src->height;
1906
1907 41 int dest_x_start = clip_x;
1908 41 int dest_y_start = clip_y;
1909 41 int dest_x_end = clip_w;
1910 41 int dest_y_end = clip_h;
1911
1912
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) {
1913
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 if (dest_offset_x > 0) dest_x_start += dest_offset_x;
1914
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 if (dest_offset_y > 0) dest_y_start += dest_offset_y;
1915
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (!tile) {
1916
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;
1917
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;
1918 }
1919
1920
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++) {
1921
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++) {
1922 866100 int src_x = dest_x - dest_offset_x;
1923 866100 int src_y = dest_y - dest_offset_y;
1924 866100 copy_pixel(img_dest, img_src, dest_x, dest_y, src_x, src_y, src_w, src_h, transparent_color, tile);
1925 }
1926 }
1927
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 } else if (rot_angle == 0.0) {
1928 10 rot_x *= scale;
1929 10 rot_y *= scale;
1930
1931 10 const int fp_scale = 1000;
1932
1933 10 int rot_x_x = (int)rot_x;
1934 10 int rot_y_i = (int)rot_y;
1935 10 int scale_i = (int)(scale * (float) fp_scale);
1936
1937
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++) {
1938
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++) {
1939 1720000 int src_x = (dest_x - dest_offset_x - rot_x_x) * fp_scale;
1940 1720000 int src_y = (dest_y - dest_offset_y - rot_y_i) * fp_scale;
1941
1942 1720000 src_x += rot_x_x * fp_scale;
1943 1720000 src_y += rot_y_i * fp_scale;
1944
1945 1720000 src_x /= scale_i;
1946 1720000 src_y /= scale_i;
1947 1720000 copy_pixel(img_dest, img_src, dest_x, dest_y, src_x, src_y, src_w, src_h, transparent_color, tile);
1948 }
1949 }
1950 } else {
1951 10 float sin_rot_angle = sinf(-rot_angle * (float)M_PI / 180.0f);
1952 10 float cos_rot_angle = cosf(-rot_angle * (float)M_PI / 180.0f);
1953
1954 10 rot_x *= scale;
1955 10 rot_y *= scale;
1956
1957 10 const int fp_scale = 1000;
1958
1959 10 int sin_rot_angle_i = (int)(sin_rot_angle * (float)fp_scale);
1960 10 int cos_rot_angle_i = (int)(cos_rot_angle * (float)fp_scale);
1961 10 int rot_x_i = (int)rot_x;
1962 10 int rot_y_i = (int)rot_y;
1963 10 int scale_i = (int)(scale * (float) fp_scale);
1964
1965
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++) {
1966
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++) {
1967 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;
1968 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;
1969
1970 1800000 src_x += rot_x_i * fp_scale;
1971 1800000 src_y += rot_y_i * fp_scale;
1972
1973 1800000 src_x /= scale_i;
1974 1800000 src_y /= scale_i;
1975 1800000 copy_pixel(img_dest, img_src, dest_x, dest_y, src_x, src_y, src_w, src_h, transparent_color, tile);
1976 }
1977 }
1978 }
1979 }
1980
1981 // Extensions
1982
1983 #define ATTR_MAX_ARGS 4
1984 #define ARG_MAX_NUM 8
1985
1986 typedef struct {
1987 bool is_valid;
1988 uint16_t arg_num;
1989 lbm_value args[ATTR_MAX_ARGS];
1990 } attr_t;
1991
1992 typedef struct {
1993 bool is_valid;
1994 image_buffer_t img;
1995 lbm_value args[ARG_MAX_NUM];
1996 attr_t attr_thickness;
1997 attr_t attr_filled;
1998 attr_t attr_rounded;
1999 attr_t attr_dotted;
2000 attr_t attr_scale;
2001 attr_t attr_rotate;
2002 attr_t attr_resolution;
2003 attr_t attr_tile;
2004 attr_t attr_clip;
2005 } img_args_t;
2006
2007 1085 static img_args_t decode_args(lbm_value *args, lbm_uint argn, int num_expected) {
2008 img_args_t res;
2009 1085 memset(&res, 0, sizeof(res));
2010 1085 res.is_valid = false;
2011
2012 lbm_array_header_t *arr;
2013
3/4
✓ Branch 0 taken 1085 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1048 times.
✓ Branch 4 taken 37 times.
1085 if (argn >= 1 && (arr = get_image_buffer(args[0]))) {
2014 // at least one argument which is an image buffer.
2015 1048 res.img.width = image_buffer_width((uint8_t*)arr->data);
2016 1048 res.img.height = image_buffer_height((uint8_t*)arr->data);
2017 1048 res.img.fmt = image_buffer_format((uint8_t*)arr->data);
2018 1048 res.img.mem_base = (uint8_t*)arr->data;
2019 1048 res.img.data = image_buffer_data((uint8_t*)arr->data);
2020
2021
2022 1048 int num_dec = 0;
2023
2/2
✓ Branch 0 taken 5391 times.
✓ Branch 1 taken 1022 times.
6413 for (unsigned int i = 1;i < argn;i++) {
2024
4/4
✓ Branch 1 taken 668 times.
✓ Branch 2 taken 4723 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 648 times.
5391 if (!lbm_is_number(args[i]) && !lbm_is_cons(args[i])) {
2025 20 return res;
2026 }
2027
2028
2/2
✓ Branch 1 taken 4723 times.
✓ Branch 2 taken 648 times.
5371 if (lbm_is_number(args[i])) {
2029 4723 res.args[num_dec] = args[i];
2030 4723 num_dec++;
2031
2032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4723 times.
4723 if (num_dec >= ARG_MAX_NUM) {
2033 ✗ return res;
2034 }
2035 } else {
2036 648 lbm_value curr = args[i];
2037 648 int attr_ind = 0;
2038 648 attr_t *attr_now = 0;
2039
2/2
✓ Branch 1 taken 1156 times.
✓ Branch 2 taken 646 times.
1802 while (lbm_is_cons(curr)) {
2040 1156 lbm_value arg = lbm_car(curr);
2041
2042
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 508 times.
1156 if (attr_ind == 0) {
2043
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 648 times.
648 if (!lbm_is_symbol(arg)) {
2044 ✗ return res;
2045 }
2046
2047
2/2
✓ Branch 1 taken 346 times.
✓ Branch 2 taken 302 times.
648 if (lbm_dec_sym(arg) == symbol_thickness) {
2048 346 attr_now = &res.attr_thickness;
2049 346 attr_now->arg_num = 1;
2050
2/2
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 168 times.
302 } else if (lbm_dec_sym(arg) == symbol_filled) {
2051 134 attr_now = &res.attr_filled;
2052 134 attr_now->arg_num = 0;
2053
2/2
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 77 times.
168 } else if (lbm_dec_sym(arg) == symbol_rounded) {
2054 91 attr_now = &res.attr_rounded;
2055 91 attr_now->arg_num = 1;
2056
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 49 times.
77 } else if (lbm_dec_sym(arg) == symbol_dotted) {
2057 28 attr_now = &res.attr_dotted;
2058 28 attr_now->arg_num = 2;
2059
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 34 times.
49 } else if (lbm_dec_sym(arg) == symbol_scale) {
2060 15 attr_now = &res.attr_scale;
2061 15 attr_now->arg_num = 1;
2062
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 23 times.
34 } else if (lbm_dec_sym(arg) == symbol_rotate) {
2063 11 attr_now = &res.attr_rotate;
2064 11 attr_now->arg_num = 3;
2065
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 } else if (lbm_dec_sym(arg) == symbol_resolution) {
2066 ✗ attr_now = &res.attr_resolution;
2067 ✗ attr_now->arg_num = 1;
2068
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 10 times.
23 } else if (lbm_dec_sym(arg) == symbol_tile) {
2069 13 attr_now = &res.attr_tile;
2070 13 attr_now->arg_num = 0;
2071
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 time.
10 } else if (lbm_dec_sym(arg) == symbol_clip) {
2072 9 attr_now = &res.attr_clip;
2073 9 attr_now->arg_num = 4;
2074 }
2075 else {
2076 1 return res;
2077 }
2078 } else {
2079
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 507 times.
508 if (!lbm_is_number(arg)) {
2080 1 return res;
2081 }
2082
2083 507 attr_now->args[attr_ind - 1] = arg;
2084 }
2085
2086 1154 attr_ind++;
2087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1154 times.
1154 if (attr_ind > (ATTR_MAX_ARGS + 1)) {
2088 ✗ return res;
2089 }
2090
2091 1154 curr = lbm_cdr(curr);
2092 }
2093
2094 // does this really compare the pointer addresses?
2095
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) {
2096 65 attr_now->arg_num = 0; // the `rounded` attribute may be empty
2097 }
2098
2099
2100
2/2
✓ Branch 0 taken 642 times.
✓ Branch 1 taken 4 times.
646 if ((attr_ind - 1) == attr_now->arg_num) {
2101 642 attr_now->is_valid = true;
2102 } else {
2103 4 return res;
2104 }
2105 }
2106 }
2107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
1022 if (num_dec != num_expected) {
2108 ✗ return res;
2109 }
2110 // I think this should go here ???
2111 1022 res.is_valid = true;
2112 }
2113 1059 return res;
2114 }
2115
2116 26 static lbm_value ext_image_dims(lbm_value *args, lbm_uint argn) {
2117 26 img_args_t arg_dec = decode_args(args, argn, 0);
2118
2119
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 if (!arg_dec.is_valid) {
2120 2 return ENC_SYM_TERROR;
2121 }
2122
2123 24 lbm_value dims = lbm_heap_allocate_list(2);
2124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (lbm_is_symbol(dims)) {
2125 ✗ return dims;
2126 }
2127 24 lbm_value curr = dims;
2128 24 lbm_set_car(curr, lbm_enc_i(arg_dec.img.width));
2129 24 curr = lbm_cdr(curr);
2130 24 lbm_set_car(curr, lbm_enc_i(arg_dec.img.height));
2131 24 return dims;
2132 }
2133
2134 78 static lbm_value ext_image_buffer(lbm_value *args, lbm_uint argn) {
2135 78 lbm_value res = ENC_SYM_TERROR;
2136 78 bool args_ok = false;
2137 78 color_format_t fmt = indexed2;
2138 78 lbm_uint w = 0;
2139 78 lbm_uint h = 0;
2140
2141
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
79 if (argn == 4 &&
2142
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_defrag_mem(args[0]) &&
2143
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_symbol(args[1]) &&
2144
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_number(args[2]) &&
2145 1 lbm_is_number(args[3])) {
2146 1 fmt = sym_to_color_format(args[1]);
2147 1 w = lbm_dec_as_u32(args[2]);
2148 1 h = lbm_dec_as_u32(args[3]);
2149 1 args_ok = true;
2150
2/4
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
154 } else if (argn == 3 &&
2151
1/2
✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
154 lbm_is_symbol(args[0]) &&
2152
1/2
✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
154 lbm_is_number(args[1]) &&
2153 77 lbm_is_number(args[2])) {
2154 77 fmt = sym_to_color_format(args[0]);
2155 77 w = lbm_dec_as_u32(args[1]);
2156 77 h = lbm_dec_as_u32(args[2]);
2157 77 args_ok = true;
2158 }
2159
2160
9/12
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 74 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 73 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 73 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 73 times.
✗ Branch 11 not taken.
78 if (args_ok && fmt != format_not_supported && w > 0 && h > 0 && w < MAX_WIDTH && h < MAX_HEIGHT) {
2161
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1 time.
73 if (argn == 3) {
2162 72 res = image_buffer_allocate(fmt, (uint16_t)w, (uint16_t)h);
2163 } else {
2164 1 res = image_buffer_allocate_dm((lbm_uint*)lbm_car(args[0]), fmt, (uint16_t)w, (uint16_t)h);
2165 }
2166 }
2167 78 return res;
2168 }
2169
2170
2171 20 static lbm_value ext_is_image_buffer(lbm_value *args, lbm_uint argn) {
2172 20 lbm_value res = ENC_SYM_TERROR;
2173
2174
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (argn == 1) {
2175 20 res = ENC_SYM_NIL;
2176 20 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
2177
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 time.
20 if (array) {
2178 19 uint8_t *data = (uint8_t*)array->data;
2179
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
19 if (image_buffer_is_valid(data, array->size)) {
2180 19 res = ENC_SYM_TRUE;;
2181 }
2182 }
2183 }
2184 20 return res;
2185 }
2186
2187 28 static lbm_value ext_color(lbm_value *args, lbm_uint argn) {
2188 28 lbm_value res = ENC_SYM_TERROR;
2189
2190
4/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 1 time.
56 if (argn >= 2 && argn <= 6 &&
2191
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
55 lbm_is_symbol(args[0]) &&
2192 27 lbm_is_number(args[1])) {
2193
2194 // Color1 and color2 are int in the struct and decoded as i32, why
2195 // where they stored in uint32_t?
2196 27 int32_t color1 = lbm_dec_as_i32(args[1]);
2197
2198 27 int32_t color2 = 0;
2199
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (argn >= 3) {
2200
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[2])) {
2201 14 color2 = lbm_dec_as_i32(args[2]);
2202 } else {
2203 ✗ return ENC_SYM_TERROR;
2204 }
2205 }
2206
2207 27 int32_t param1 = 0;
2208
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (argn >= 4) {
2209
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[3])) {
2210 14 param1 = lbm_dec_as_i32(args[3]);
2211 } else {
2212 ✗ return ENC_SYM_TERROR;
2213 }
2214 }
2215
2216 27 int32_t param2 = 0;
2217
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (argn >= 5) {
2218
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[4])) {
2219 14 param2 = lbm_dec_as_i32(args[4]);
2220 } else {
2221 ✗ return ENC_SYM_TERROR;
2222 }
2223 }
2224
2225 27 bool mirrored = false;
2226
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 17 times.
27 if (argn >= 6) {
2227
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (lbm_is_symbol(args[5])) {
2228 10 lbm_uint sym = lbm_dec_sym(args[5]);
2229
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 if (sym == symbol_repeat) {
2230 7 mirrored = false;
2231
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 } else if (sym == symbol_mirrored) {
2232 3 mirrored = true;
2233 } else {
2234 ✗ return ENC_SYM_TERROR;
2235 }
2236 } else {
2237 ✗ return ENC_SYM_TERROR;
2238 }
2239 }
2240
2241 COLOR_TYPE t;
2242
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 15 times.
27 if (lbm_dec_sym(args[0]) == symbol_regular) {
2243 12 t = COLOR_REGULAR;
2244
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 9 times.
15 } else if (lbm_dec_sym(args[0]) == symbol_gradient_x) {
2245 6 t = COLOR_GRADIENT_X;
2246
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
9 } else if (lbm_dec_sym(args[0]) == symbol_gradient_y) {
2247 3 t = COLOR_GRADIENT_Y;
2248
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
6 } else if (lbm_dec_sym(args[0]) == symbol_gradient_x_pre) {
2249 4 t = COLOR_PRE_X;
2250
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
2 } else if (lbm_dec_sym(args[0]) == symbol_gradient_y_pre) {
2251 1 t = COLOR_PRE_Y;
2252 } else {
2253 1 return ENC_SYM_TERROR;
2254 }
2255
2256 // Maybe check if param is in ranges first ?
2257 26 res = color_allocate(t, color1, color2, (uint16_t)param1, (uint16_t)param2, mirrored);
2258 }
2259
2260 27 return res;
2261 }
2262
2263 25 static lbm_value ext_color_set(lbm_value *args, lbm_uint argn) {
2264 color_t *color;
2265
3/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 3 times.
25 if (argn != 3 || !(color = get_color(args[0])) || // color assignment
2266
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 !lbm_is_symbol(args[1])) {
2267 3 return ENC_SYM_TERROR;
2268 }
2269
2270 22 bool is_regular = color->type == COLOR_REGULAR;
2271
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 10 times.
22 bool is_gradient = color->type == COLOR_GRADIENT_X || color->type == COLOR_GRADIENT_Y;
2272
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;
2273
2274 22 lbm_uint prop = lbm_dec_sym(args[1]);
2275
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 16 times.
22 if (prop == symbol_color_0) {
2276
5/6
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
6 if (!lbm_is_number(args[2]) || !(is_regular || is_gradient)) {
2277 2 return ENC_SYM_TERROR;
2278 }
2279 4 color->color1 = lbm_dec_as_i32(args[2]);
2280
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 13 times.
16 } else if (prop == symbol_color_1) {
2281
3/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
3 if (!lbm_is_number(args[2]) || !is_gradient) {
2282 2 return ENC_SYM_TERROR;
2283 }
2284 1 color->color2 = lbm_dec_as_i32(args[2]);
2285
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
13 } else if (prop == symbol_width) {
2286
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
5 if (!lbm_is_number(args[2]) || !is_gradient) {
2287 3 return ENC_SYM_TERROR;
2288 }
2289 2 color->param1 = (uint16_t)lbm_dec_as_u32(args[2]);
2290
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 } else if (prop == symbol_offset) {
2291
4/6
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
3 if (!lbm_is_number(args[2]) || !(is_gradient || is_pre)) {
2292 1 return ENC_SYM_TERROR;
2293 }
2294 2 color->param2 = (uint16_t)lbm_dec_as_u32(args[2]);
2295
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 } else if (prop == symbol_repeat_type) {
2296
2/6
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
3 if (!lbm_is_symbol(args[2]) || !(is_gradient || is_pre)) {
2297 ✗ return ENC_SYM_TERROR;
2298 }
2299 3 lbm_uint sym = lbm_dec_sym(args[2]);
2300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (sym == symbol_repeat) {
2301 ✗ color->mirrored = false;
2302
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 } else if (sym == symbol_mirrored) {
2303 2 color->mirrored = true;
2304 } else {
2305 1 return ENC_SYM_TERROR;
2306 }
2307 } else {
2308 2 return ENC_SYM_TERROR;
2309 }
2310
2311 11 return ENC_SYM_TRUE;
2312 }
2313
2314 23 static lbm_value ext_color_get(lbm_value *args, lbm_uint argn) {
2315 color_t *color;
2316
3/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 1 time.
23 if (argn != 2 || !(color = get_color(args[0])) || // color assignment
2317
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 !lbm_is_symbol(args[1])) {
2318 1 return ENC_SYM_TERROR;
2319 }
2320
2321
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;
2322
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;
2323
2324 22 lbm_uint prop = lbm_dec_sym(args[1]);
2325
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
22 if (prop == symbol_color_0) {
2326 // always allowed
2327 9 return lbm_enc_u32((uint32_t)color->color1);
2328
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 } else if (prop == symbol_color_1) {
2329
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
4 if (!is_gradient && !is_pre) {
2330 1 return ENC_SYM_TERROR;
2331 }
2332 3 return lbm_enc_u32((uint32_t)color->color2);
2333
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 } else if (prop == symbol_width) {
2334
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
5 if (!is_gradient && !is_pre) {
2335 1 return ENC_SYM_TERROR;
2336 }
2337 4 return lbm_enc_i32((int32_t)color->param1);
2338
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 } else if (prop == symbol_offset) {
2339
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) {
2340 ✗ return ENC_SYM_TERROR;
2341 }
2342 3 return lbm_enc_i32((int32_t)color->param2);
2343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 } else if (prop == symbol_repeat_type) {
2344 ✗ if (!is_gradient && !is_pre) {
2345 ✗ return ENC_SYM_TERROR;
2346 }
2347 ✗ return lbm_enc_sym(color->mirrored ? symbol_mirrored : symbol_repeat);
2348 } else {
2349 1 return ENC_SYM_TERROR;
2350 }
2351
2352 return ENC_SYM_TRUE;
2353 }
2354
2355 7 static lbm_value ext_color_setpre(lbm_value *args, lbm_uint argn) {
2356 color_t *color;
2357
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 time.
7 if (argn != 3 || !(color = get_color(args[0])) ||
2358
4/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 4 times.
6 !lbm_is_number(args[1]) || !lbm_is_number(args[2])) {
2359 3 return ENC_SYM_TERROR;
2360 }
2361
2362 4 uint32_t pos = lbm_dec_as_u32(args[1]);
2363 4 int new_color = lbm_dec_as_i32(args[2]);
2364
2365
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
4 if (color->precalc == 0 || pos >= COLOR_PRECALC_LEN) {
2366 1 return ENC_SYM_EERROR;
2367 }
2368
2369 3 color->precalc[pos] = (uint32_t)new_color;
2370
2371 3 return ENC_SYM_TRUE;
2372 }
2373
2374 14 static lbm_value ext_color_getpre(lbm_value *args, lbm_uint argn) {
2375 color_t *color;
2376
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 1 time.
14 if (argn != 2 || !(color = get_color(args[0])) ||
2377
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 12 times.
13 !lbm_is_number(args[1])) {
2378 2 return ENC_SYM_TERROR;
2379 }
2380 12 uint32_t pos = lbm_dec_as_u32(args[1]);
2381
2382
3/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 11 times.
12 if (color->precalc == 0 || pos >= COLOR_PRECALC_LEN) {
2383 1 return ENC_SYM_EERROR;
2384 }
2385
2386 11 return lbm_enc_u32(color->precalc[pos]);
2387 }
2388
2389 57 static lbm_value ext_clear(lbm_value *args, lbm_uint argn) {
2390
2391 57 lbm_value res = ENC_SYM_TERROR;
2392 lbm_array_header_t *arr;
2393
5/6
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 3 times.
114 if ((argn == 1 || argn == 2) &&
2394
2/2
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 1 time.
111 (arr = get_image_buffer(args[0])) && // assignment
2395
1/2
✓ Branch 1 taken 53 times.
✗ Branch 2 not taken.
53 (argn != 2 || lbm_is_number(args[1]))) { // ( argn == 2 -> lbm_is_number(args[1]))
2396 image_buffer_t img_buf;
2397 54 img_buf.width = image_buffer_width((uint8_t*)arr->data);
2398 54 img_buf.height = image_buffer_height((uint8_t*)arr->data);
2399 54 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2400 54 img_buf.mem_base = (uint8_t*)arr->data;
2401 54 img_buf.data = image_buffer_data((uint8_t*)arr->data);
2402
2403 54 uint32_t color = 0;
2404
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 1 time.
54 if (argn == 2) {
2405 53 color = lbm_dec_as_u32(args[1]);
2406 }
2407
2408 54 image_buffer_clear(&img_buf, color);
2409 54 res = ENC_SYM_TRUE;
2410 }
2411 57 return res;
2412 }
2413
2414 113 static lbm_value ext_putpixel(lbm_value *args, lbm_uint argn) {
2415 113 img_args_t arg_dec = decode_args(args, argn, 3);
2416
2417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (!arg_dec.is_valid) {
2418 ✗ return ENC_SYM_TERROR;
2419 }
2420
2421 113 putpixel(&arg_dec.img,
2422 lbm_dec_as_i32(arg_dec.args[0]),
2423 lbm_dec_as_i32(arg_dec.args[1]),
2424 lbm_dec_as_u32(arg_dec.args[2]));
2425 113 return ENC_SYM_TRUE;
2426 }
2427
2428 167 static lbm_value ext_getpixel(lbm_value *args, lbm_uint argn) {
2429 167 img_args_t arg_dec = decode_args(args, argn, 2);
2430
2431
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 143 times.
167 if (!arg_dec.is_valid) {
2432 24 return ENC_SYM_TERROR;
2433 }
2434
2435 143 uint32_t c = getpixel(&arg_dec.img,
2436 lbm_dec_as_i32(arg_dec.args[0]),
2437 lbm_dec_as_i32(arg_dec.args[1]));
2438 143 return lbm_enc_u32(c);
2439 }
2440
2441 // lisp args: img x1 y1 x2 y2 color opt-attr1 ... opt-attrN
2442 43 static lbm_value ext_line(lbm_value *args, lbm_uint argn) {
2443 43 img_args_t arg_dec = decode_args(args, argn, 5);
2444
2445
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 41 times.
43 if (!arg_dec.is_valid) {
2446 2 return ENC_SYM_TERROR;
2447 }
2448
2449 41 line(&arg_dec.img,
2450 lbm_dec_as_i32(arg_dec.args[0]),
2451 lbm_dec_as_i32(arg_dec.args[1]),
2452 lbm_dec_as_i32(arg_dec.args[2]),
2453 lbm_dec_as_i32(arg_dec.args[3]),
2454 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2455 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2456 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2457 lbm_dec_as_u32(arg_dec.args[4]));
2458
2459 41 return ENC_SYM_TRUE;
2460 }
2461
2462 // lisp args: img cx cy r color opt-attr1 ... opt-attrN
2463 83 static lbm_value ext_circle(lbm_value *args, lbm_uint argn) {
2464 83 img_args_t arg_dec = decode_args(args, argn, 4);
2465
2466
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 80 times.
83 if (!arg_dec.is_valid) {
2467 3 return ENC_SYM_TERROR;
2468 }
2469
2470
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 57 times.
80 if (arg_dec.attr_filled.is_valid) {
2471 23 fill_circle(&arg_dec.img,
2472 lbm_dec_as_i32(arg_dec.args[0]),
2473 lbm_dec_as_i32(arg_dec.args[1]),
2474 lbm_dec_as_i32(arg_dec.args[2]),
2475 lbm_dec_as_u32(arg_dec.args[3]));
2476
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 78 times.
80 } if (arg_dec.attr_dotted.is_valid) {
2477 4 arc(&arg_dec.img,
2478 lbm_dec_as_i32(arg_dec.args[0]),
2479 lbm_dec_as_i32(arg_dec.args[1]),
2480 lbm_dec_as_i32(arg_dec.args[2]),
2481 0, 359.9f,
2482 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2483 2 arg_dec.attr_rounded.is_valid, // currently does nothing as the line function doesn't support square ends.
2484 false,
2485 false, false,
2486 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2487 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2488 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2489 lbm_dec_as_u32(arg_dec.args[3]));
2490 } else {
2491 78 circle(&arg_dec.img,
2492 lbm_dec_as_i32(arg_dec.args[0]),
2493 lbm_dec_as_i32(arg_dec.args[1]),
2494 lbm_dec_as_i32(arg_dec.args[2]),
2495 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2496 lbm_dec_as_u32(arg_dec.args[3]));
2497 }
2498
2499 80 return ENC_SYM_TRUE;
2500 }
2501
2502 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
2503 137 static lbm_value ext_arc(lbm_value *args, lbm_uint argn) {
2504 137 img_args_t arg_dec = decode_args(args, argn, 6);
2505
2506
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 128 times.
137 if (!arg_dec.is_valid) {
2507 9 return ENC_SYM_TERROR;
2508 }
2509
2510 256 arc(&arg_dec.img,
2511 lbm_dec_as_i32(arg_dec.args[0]),
2512 lbm_dec_as_i32(arg_dec.args[1]),
2513 lbm_dec_as_i32(arg_dec.args[2]),
2514 lbm_dec_as_float(arg_dec.args[3]),
2515 lbm_dec_as_float(arg_dec.args[4]),
2516 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2517 128 arg_dec.attr_rounded.is_valid,
2518 128 arg_dec.attr_filled.is_valid,
2519 false, false,
2520 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2521 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2522 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2523 lbm_dec_as_u32(arg_dec.args[5]));
2524
2525 128 return ENC_SYM_TRUE;
2526 }
2527
2528 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
2529 51 static lbm_value ext_circle_sector(lbm_value *args, lbm_uint argn) {
2530 51 img_args_t arg_dec = decode_args(args, argn, 6);
2531
2532
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 50 times.
51 if (!arg_dec.is_valid) {
2533 1 return ENC_SYM_TERROR;
2534 }
2535
2536 100 arc(&arg_dec.img,
2537 lbm_dec_as_i32(arg_dec.args[0]),
2538 lbm_dec_as_i32(arg_dec.args[1]),
2539 lbm_dec_as_i32(arg_dec.args[2]),
2540 lbm_dec_as_float(arg_dec.args[3]),
2541 lbm_dec_as_float(arg_dec.args[4]),
2542 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2543 true,
2544 50 arg_dec.attr_filled.is_valid,
2545 true, false,
2546 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2547 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2548 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2549 lbm_dec_as_u32(arg_dec.args[5]));
2550
2551 50 return ENC_SYM_TRUE;
2552 }
2553
2554 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
2555 284 static lbm_value ext_circle_segment(lbm_value *args, lbm_uint argn) {
2556 284 img_args_t arg_dec = decode_args(args, argn, 6);
2557
2558
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 273 times.
284 if (!arg_dec.is_valid) {
2559 11 return ENC_SYM_TERROR;
2560 }
2561
2562 546 arc(&arg_dec.img,
2563 lbm_dec_as_i32(arg_dec.args[0]),
2564 lbm_dec_as_i32(arg_dec.args[1]),
2565 lbm_dec_as_i32(arg_dec.args[2]),
2566 lbm_dec_as_float(arg_dec.args[3]),
2567 lbm_dec_as_float(arg_dec.args[4]),
2568 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
2569 true,
2570 273 arg_dec.attr_filled.is_valid,
2571 false, true,
2572 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
2573 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
2574 lbm_dec_as_i32(arg_dec.attr_resolution.args[0]),
2575 lbm_dec_as_u32(arg_dec.args[5]));
2576
2577
2578 273 return ENC_SYM_TRUE;
2579 }
2580
2581 // lisp args: img x y width height color opt-attr1 ... opt-attrN
2582 108 static lbm_value ext_rectangle(lbm_value *args, lbm_uint argn) {
2583 108 img_args_t arg_dec = decode_args(args, argn, 5);
2584
2585
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 105 times.
108 if (!arg_dec.is_valid) {
2586 3 return ENC_SYM_TERROR;
2587 }
2588
2589 105 image_buffer_t *img = &arg_dec.img;
2590 105 int x = lbm_dec_as_i32(arg_dec.args[0]);
2591 105 int y = lbm_dec_as_i32(arg_dec.args[1]);
2592 105 int width = lbm_dec_as_i32(arg_dec.args[2]);
2593 105 int height = lbm_dec_as_i32(arg_dec.args[3]);
2594 105 int rad = lbm_dec_as_i32(arg_dec.attr_rounded.args[0]);
2595 105 int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]);
2596 105 uint32_t color = lbm_dec_as_u32(arg_dec.args[4]);
2597 105 int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]);
2598 105 int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]);
2599 105 int resolution = lbm_dec_as_i32(arg_dec.attr_resolution.args[0]);
2600
2601
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 78 times.
105 if (arg_dec.attr_rounded.is_valid) {
2602
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 23 times.
27 if (arg_dec.attr_filled.is_valid) {
2603 4 rectangle(img, x + rad, y, width - 2 * rad, rad, 1, 1, 0, 0, color);
2604 4 rectangle(img, x + rad, y + height - rad, width - 2 * rad, rad, 1, 1, 0, 0, color);
2605 4 rectangle(img, x, y + rad, width, height - 2 * rad, 1, 1, 0, 0, color);
2606 4 fill_circle(img, x + rad, y + rad, rad, color);
2607 4 fill_circle(img, x + rad, y + height - rad, rad, color);
2608 4 fill_circle(img, x + width - rad, y + rad, rad, color);
2609 4 fill_circle(img, x + width - rad, y + height - rad, rad, color);
2610 } else {
2611 // Remember to change these to use the rounded attribute,
2612 // when/if line supports it!
2613
2614 23 int line_thickness = thickness / 2;
2615 23 thickness = line_thickness * 2; // round it to even for consistency.
2616
2617 // top
2618 23 line(img, x + rad, y + line_thickness, x + width - rad, y + line_thickness, line_thickness, dot1, dot2, color);
2619 // bottom
2620 23 line(img, x + rad, y + height - line_thickness, x + width - rad, y + height - line_thickness, line_thickness, dot1, dot2, color);
2621 // left
2622 23 line(img, x + line_thickness, y + rad, x + line_thickness, y + height - rad, line_thickness, dot1, dot2, color);
2623 // right
2624 23 line(img, x + width - line_thickness, y + rad, x + width - line_thickness, y + height - rad, line_thickness, dot1, dot2, color);
2625
2626 // upper left
2627 23 arc(img, x + rad, y + rad, rad, 180, 270, thickness, false, false, false, false, dot1, dot2, resolution, color);
2628 // upper right
2629 23 arc(img, x + width - rad, y + rad, rad, 270, 0, thickness, false, false, false, false, dot1, dot2, resolution, color);
2630 // bottom left
2631 23 arc(img, x + rad, y + height - rad, rad, 90, 180, thickness, false, false, false, false, dot1, dot2, resolution, color);
2632 // bottom right
2633 23 arc(img, x + width - rad, y + height - rad, rad, 0, 90, thickness, false, false, false, false, dot1, dot2, resolution, color);
2634 }
2635 } else {
2636 78 rectangle(img,
2637 x, y,
2638 width, height,
2639 78 arg_dec.attr_filled.is_valid,
2640 thickness,
2641 dot1, dot2,
2642 color);
2643 }
2644
2645 105 return ENC_SYM_TRUE;
2646 }
2647
2648 // lisp args: img x1 y1 x2 y2 x3 y3 color opt-attr1 ... opt-attrN
2649 22 static lbm_value ext_triangle(lbm_value *args, lbm_uint argn) {
2650 22 img_args_t arg_dec = decode_args(args, argn, 7);
2651
2652
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (!arg_dec.is_valid) {
2653 2 return ENC_SYM_TERROR;
2654 }
2655
2656 20 image_buffer_t *img = &arg_dec.img;
2657 20 int x0 = lbm_dec_as_i32(arg_dec.args[0]);
2658 20 int y0 = lbm_dec_as_i32(arg_dec.args[1]);
2659 20 int x1 = lbm_dec_as_i32(arg_dec.args[2]);
2660 20 int y1 = lbm_dec_as_i32(arg_dec.args[3]);
2661 20 int x2 = lbm_dec_as_i32(arg_dec.args[4]);
2662 20 int y2 = lbm_dec_as_i32(arg_dec.args[5]);
2663 20 int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]);
2664 20 int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]);
2665 20 int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]);
2666 20 uint32_t color = lbm_dec_as_u32(arg_dec.args[6]);
2667
2668
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (arg_dec.attr_filled.is_valid) {
2669 2 fill_triangle(img, x0, y0, x1, y1, x2, y2, color);
2670 } else {
2671 18 line(img, x0, y0, x1, y1, thickness, dot1, dot2, color);
2672 18 line(img, x1, y1, x2, y2, thickness, dot1, dot2, color);
2673 18 line(img, x2, y2, x0, y0, thickness, dot1, dot2, color);
2674 }
2675
2676 20 return ENC_SYM_TRUE;
2677 }
2678
2679 // lisp args:
2680 // img x y fg bg font str [attrs] ['up|'down]
2681 // img x y fg bg str [attrs] ['up|'down] uses built-in retro5x7 font
2682 // img x y '(c0..c3) font str [attrs] ['up|'down] 4-color form for 2bpp fonts
2683 // attrs: '(magnify N) '(scale N) '(spacing N) '(align 'left|'center|'right) '(rotate deg)
2684 // orient: 0=normal 1=up/90CCW 2=180 3=down/90CW
2685 133 static lbm_value ext_text(lbm_value *args, lbm_uint argn) {
2686 133 int orient = 0;
2687
2688
3/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 105 times.
133 if (argn >= 6 && lbm_is_symbol(args[argn - 1])) {
2689 28 lbm_uint sym = lbm_dec_sym(args[argn - 1]);
2690
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 15 times.
28 if (sym == symbol_up) { orient = 1; argn--; }
2691
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 15 times.
28 if (sym == symbol_down) { orient = 3; argn--; }
2692 }
2693
2694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (argn < 6) return ENC_SYM_TERROR;
2695
2696 133 float txt_mag = 1.0f;
2697 133 int spacing = 0;
2698 133 int align = 0;
2699 133 int rot_deg = 0;
2700
2701 133 lbm_uint core_argn = argn;
2702
1/2
✓ Branch 0 taken 159 times.
✗ Branch 1 not taken.
159 while (core_argn > 0) {
2703 159 int r = parse_text_attr(args[core_argn - 1], &txt_mag, &spacing, &align, &rot_deg);
2704
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 133 times.
159 if (r == 1) { core_argn--; continue; }
2705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (r < 0) { return ENC_SYM_TERROR; }
2706 133 break;
2707 }
2708
2709
3/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 130 times.
133 if (core_argn < 6 || core_argn > 7) return ENC_SYM_TERROR;
2710
2711
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 127 times.
130 if (rot_deg != 0) {
2712 3 orient = (((rot_deg % 360) + 360) % 360) / 90;
2713 }
2714
2715 130 int x = lbm_dec_as_i32(args[1]);
2716 130 int y = lbm_dec_as_i32(args[2]);
2717
2718 130 int32_t colors[4] = {-1, -1, -1, -1};
2719 130 uint8_t *font_data = (uint8_t*)retro5x7;
2720 130 char *txt = NULL;
2721
2722
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 114 times.
130 if (lbm_is_cons(args[3])) {
2723 // color list form: img x y '(c0..cN) font str
2724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (core_argn != 6) return ENC_SYM_TERROR;
2725 16 lbm_value curr = args[3];
2726 16 int ci = 0;
2727
3/4
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 47 times.
✗ Branch 4 not taken.
63 while (lbm_is_cons(curr) && ci < 4) {
2728 47 lbm_value a = lbm_car(curr);
2729
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if (!lbm_is_number(a)) return ENC_SYM_TERROR;
2730 47 colors[ci++] = lbm_dec_as_i32(a);
2731 47 curr = lbm_cdr(curr);
2732 }
2733
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if (lbm_type_of_functional(args[4]) == LBM_TYPE_ARRAY) {
2734 16 lbm_array_header_t *fh = (lbm_array_header_t*)lbm_car(args[4]);
2735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (fh->size < 4) return ENC_SYM_TERROR;
2736 16 uint8_t *fd = (uint8_t*)fh->data;
2737 16 uint32_t need = ((uint32_t)fd[0] * fd[1] * fd[2] * fd[3] + 7) / 8;
2738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (fh->size - 4 < need) return ENC_SYM_TERROR;
2739 16 font_data = fd;
2740 }
2741 16 txt = lbm_dec_str(args[5]);
2742
2/4
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 114 times.
✗ Branch 5 not taken.
114 } else if (lbm_is_number(args[3]) && lbm_is_number(args[4])) {
2743 114 colors[0] = lbm_dec_as_i32(args[3]);
2744 114 colors[1] = lbm_dec_as_i32(args[4]);
2745
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 22 times.
114 if (core_argn == 7) {
2746
1/2
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
92 if (lbm_type_of_functional(args[5]) == LBM_TYPE_ARRAY) {
2747 92 lbm_array_header_t *fh = (lbm_array_header_t*)lbm_car(args[5]);
2748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (fh->size < 4) return ENC_SYM_TERROR;
2749 92 uint8_t *fd = (uint8_t*)fh->data;
2750 92 uint32_t need = ((uint32_t)fd[0] * fd[1] * fd[2] * fd[3] + 7) / 8;
2751
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 91 times.
92 if (fh->size - 4 < need) return ENC_SYM_TERROR;
2752 91 font_data = fd;
2753 } else {
2754 ✗ return ENC_SYM_TERROR;
2755 }
2756 91 txt = lbm_dec_str(args[6]);
2757 } else {
2758 22 txt = lbm_dec_str(args[5]);
2759 }
2760 } else {
2761 ✗ return ENC_SYM_TERROR;
2762 }
2763
2764
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 127 times.
129 if (!txt) return ENC_SYM_TERROR;
2765
2766
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 123 times.
127 if (!array_is_image_buffer(args[0])) return ENC_SYM_TERROR;
2767 123 lbm_array_header_t *arr = (lbm_array_header_t *)lbm_car(args[0]);
2768 image_buffer_t img_buf;
2769 123 img_buf.width = image_buffer_width((uint8_t*)arr->data);
2770 123 img_buf.height = image_buffer_height((uint8_t*)arr->data);
2771 123 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2772 123 img_buf.mem_base = (uint8_t*)arr->data;
2773 123 img_buf.data = image_buffer_data((uint8_t*)arr->data);
2774
2775 123 uint8_t w = font_data[0];
2776
2777 static const int8_t incx_tbl[4] = { 1, 0, -1, 0};
2778 static const int8_t incy_tbl[4] = { 0, -1, 0, 1};
2779 123 int incx = incx_tbl[orient];
2780 123 int incy = incy_tbl[orient];
2781
2782 123 float char_step_f = (float)w * txt_mag + (float)spacing;
2783 123 int char_step = (int)lroundf(char_step_f);
2784 123 int txt_len = (int)strlen(txt);
2785 123 int total = (int)lroundf((float)txt_len * char_step_f - (float)spacing);
2786
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 121 times.
123 int align_offset = (align == 1) ? total / 2 : (align == 2) ? total : 0;
2787
2788 123 int x0 = x - align_offset * incx;
2789 123 int y0 = y - align_offset * incy;
2790
2791
2/2
✓ Branch 0 taken 851 times.
✓ Branch 1 taken 123 times.
974 for (int ind = 0; txt[ind] != 0; ind++) {
2792 851 img_putc(&img_buf,
2793 851 x0 + ind * char_step * incx,
2794 851 y0 + ind * char_step * incy,
2795 (uint32_t *)colors,
2796 4,
2797 font_data,
2798 851 (uint8_t)txt[ind],
2799 orient,
2800 txt_mag);
2801 }
2802
2803 123 return ENC_SYM_TRUE;
2804 }
2805
2806 51 static lbm_value ext_blit(lbm_value *args, lbm_uint argn) {
2807 51 img_args_t arg_dec = decode_args(args + 1, argn - 1, 3);
2808
2809 51 lbm_value res = ENC_SYM_TERROR;
2810 lbm_array_header_t *arr;
2811
4/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 3 times.
51 if (arg_dec.is_valid && (arr = get_image_buffer(args[0]))) { //assignment
2812 image_buffer_t dest_buf;
2813 42 dest_buf.width = image_buffer_width((uint8_t*)arr->data);
2814 42 dest_buf.height = image_buffer_height((uint8_t*)arr->data);
2815 42 dest_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2816 42 dest_buf.mem_base = (uint8_t*)arr->data;
2817 42 dest_buf.data = image_buffer_data((uint8_t*)arr->data);
2818
2819 42 float scale = 1.0;
2820
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
42 if (arg_dec.attr_scale.is_valid) {
2821 14 scale = lbm_dec_as_float(arg_dec.attr_scale.args[0]);
2822 }
2823
2824 108 blit(
2825 &dest_buf,
2826 &arg_dec.img,
2827 lbm_dec_as_i32(arg_dec.args[0]),
2828 lbm_dec_as_i32(arg_dec.args[1]),
2829 lbm_dec_as_float(arg_dec.attr_rotate.args[0]),
2830 lbm_dec_as_float(arg_dec.attr_rotate.args[1]),
2831 lbm_dec_as_float(arg_dec.attr_rotate.args[2]),
2832 scale,
2833 lbm_dec_as_i32(arg_dec.args[2]),
2834 42 arg_dec.attr_tile.is_valid,
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[0]) : 0,
2836
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,
2837
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,
2838
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
2839 );
2840 42 res = ENC_SYM_TRUE;
2841 }
2842 51 return res;
2843 }
2844
2845 ✗ void display_dummy_reset(void) {
2846 ✗ return;
2847 }
2848
2849 ✗ void display_dummy_clear(uint32_t color) {
2850 (void) color;
2851 ✗ return;
2852 }
2853
2854 ✗ bool display_dummy_render_image(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) {
2855 (void) img;
2856 (void) x;
2857 (void) y;
2858 (void) colors;
2859 ✗ return false;
2860 }
2861
2862 static bool(* volatile disp_render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) = display_dummy_render_image;
2863 static void(* volatile disp_clear)(uint32_t color) = display_dummy_clear;
2864 static void(* volatile disp_reset)(void) = display_dummy_reset;
2865
2866 static char *msg_not_supported = "Command not supported or display driver not initialized";
2867
2868 1 static lbm_value ext_disp_reset(lbm_value *args, lbm_uint argn) {
2869 (void) args;
2870 (void) argn;
2871
2872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (disp_reset == NULL) {
2873 ✗ lbm_set_error_reason(msg_not_supported);
2874 ✗ return ENC_SYM_EERROR;
2875 }
2876
2877 1 disp_reset();
2878
2879 1 return ENC_SYM_TRUE;
2880 }
2881
2882 1 static lbm_value ext_disp_clear(lbm_value *args, lbm_uint argn) {
2883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (disp_clear == NULL) {
2884 ✗ lbm_set_error_reason(msg_not_supported);
2885 ✗ return ENC_SYM_EERROR;
2886 }
2887
2888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (argn > 1) {
2889 ✗ return ENC_SYM_TERROR;
2890 }
2891
2892 1 uint32_t clear_color = 0;
2893
2894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (argn == 1) {
2895 ✗ if (!lbm_is_number(args[0])) {
2896 ✗ return ENC_SYM_TERROR;
2897 }
2898
2899 ✗ clear_color = lbm_dec_as_u32(args[0]);
2900 }
2901
2902 1 disp_clear(clear_color);
2903
2904 1 return ENC_SYM_TRUE;
2905 }
2906
2907 52 static lbm_value ext_disp_render(lbm_value *args, lbm_uint argn) {
2908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (disp_render_image == NULL) {
2909 ✗ lbm_set_error_reason(msg_not_supported);
2910 ✗ return ENC_SYM_EERROR;
2911 }
2912
2913 52 lbm_value res = ENC_SYM_TERROR;
2914 lbm_array_header_t *arr;
2915
4/6
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
104 if ((argn == 3 || argn == 4) &&
2916
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
104 (arr = get_image_buffer(args[0])) &&
2917
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
104 lbm_is_number(args[1]) &&
2918 52 lbm_is_number(args[2])) {
2919 image_buffer_t img_buf;
2920 52 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
2921 52 img_buf.width = image_buffer_width((uint8_t*)arr->data);
2922 52 img_buf.height = image_buffer_height((uint8_t*)arr->data);
2923 52 img_buf.mem_base = (uint8_t*)arr->data;
2924 52 img_buf.data = image_buffer_data((uint8_t*)arr->data);
2925
2926 color_t colors[16];
2927 52 memset(colors, 0, sizeof(color_t) * 16);
2928
2929
3/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 47 times.
✗ Branch 4 not taken.
52 if (argn == 4 && lbm_is_list(args[3])) {
2930 47 int i = 0;
2931 47 lbm_value curr = args[3];
2932
3/4
✓ Branch 1 taken 300 times.
✓ Branch 2 taken 47 times.
✓ Branch 3 taken 300 times.
✗ Branch 4 not taken.
347 while (lbm_is_cons(curr) && i < 16) {
2933 300 lbm_value arg = lbm_car(curr);
2934 color_t *color;
2935
2/2
✓ Branch 1 taken 292 times.
✓ Branch 2 taken 8 times.
300 if (lbm_is_number(arg)) {
2936 292 colors[i].color1 = (int)lbm_dec_as_u32(arg);
2937
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 } else if ((color = get_color(arg))) { // color assignment
2938 8 colors[i] = *color;
2939 } else {
2940 ✗ return ENC_SYM_TERROR;
2941 }
2942
2943 300 curr = lbm_cdr(curr);
2944 300 i++;
2945 }
2946 }
2947
2948 // img_buf is a stack allocated image_buffer_t.
2949 52 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);
2950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (!render_res) {
2951 ✗ lbm_set_error_reason("Could not render image. Check if the format and location is compatible with the display.");
2952 ✗ return ENC_SYM_EERROR;
2953 }
2954 52 res = ENC_SYM_TRUE;
2955 }
2956 52 return res;
2957 }
2958
2959 // Jpg decoder
2960
2961 typedef struct {
2962 uint8_t *data;
2963 int pos;
2964 int size;
2965 int ofs_x;
2966 int ofs_y;
2967 } jpg_bufdef;
2968
2969 210 size_t jpg_input_func (JDEC* jd, uint8_t* buff, size_t ndata) {
2970 210 jpg_bufdef *dev = (jpg_bufdef*)jd->device;
2971
2972
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 208 times.
210 if ((int)ndata > (dev->size - dev->pos)) {
2973 2 ndata = (size_t)(dev->size - dev->pos);
2974 }
2975
2976
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 6 times.
210 if (buff) {
2977 204 memcpy(buff, dev->data + dev->pos, ndata);
2978 }
2979 210 dev->pos += (int)ndata;
2980 210 return ndata;
2981 }
2982
2983 3060 int jpg_output_func ( /* 1:Ok, 0:Aborted */
2984 JDEC* jd, /* Decompression object */
2985 void* bitmap, /* Bitmap data to be output */
2986 JRECT* rect /* Rectangular region to output */
2987 ) {
2988 3060 jpg_bufdef *dev = (jpg_bufdef*)jd->device;
2989
2990 image_buffer_t img;
2991 3060 img.mem_base = (uint8_t*)bitmap;
2992 3060 img.data = (uint8_t*)bitmap;
2993 3060 img.width = (uint16_t)(rect->right - rect->left + 1);
2994 3060 img.height = (uint16_t)(rect->bottom - rect->top + 1);
2995 3060 img.fmt = rgb888;
2996
2997 3060 disp_render_image(&img, (uint16_t)(rect->left + dev->ofs_x), (uint16_t)(rect->top + dev->ofs_y), 0);
2998
2999 3060 return 1;
3000 }
3001
3002 2 static lbm_value ext_disp_render_jpg(lbm_value *args, lbm_uint argn) {
3003
3004 lbm_array_header_t *array;
3005 2 lbm_value res = ENC_SYM_TERROR;
3006
3007
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 if (argn == 3 &&
3008
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 (array = lbm_dec_array_r(args[0])) && //asignment
3009
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 lbm_is_number(args[1]) &&
3010 2 lbm_is_number(args[2])) {
3011
3012 JDEC jd;
3013 void *jdwork;
3014 // make a bit of room before the buffer.
3015 2 const size_t sz_work = 4096 + IMAGE_BUFFER_HEADER_SIZE;
3016
3017 2 jdwork = lbm_malloc(sz_work);
3018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!jdwork) {
3019 ✗ return ENC_SYM_MERROR;
3020 }
3021
3022
3023
3024 jpg_bufdef iodev;
3025 2 iodev.data = (uint8_t*)(array->data);
3026 2 iodev.size = (int)array->size;
3027 2 iodev.pos = 0;
3028 2 iodev.ofs_x = lbm_dec_as_i32(args[1]);
3029 2 iodev.ofs_y = lbm_dec_as_i32(args[2]);
3030 2 jd_prepare(&jd, jpg_input_func, jdwork, sz_work, &iodev);
3031 2 jd_decomp(&jd, jpg_output_func, 0);
3032 2 lbm_free(jdwork);
3033 2 res = ENC_SYM_TRUE;
3034 }
3035 2 return res;
3036 }
3037
3038 378 void lbm_display_extensions_init(void) {
3039 378 register_symbols();
3040
3041 378 disp_render_image = NULL;
3042 378 disp_clear = NULL;
3043 378 disp_reset = NULL;
3044
3045 378 lbm_add_extension("img-buffer", ext_image_buffer);
3046 378 lbm_add_extension("img-buffer?", ext_is_image_buffer);
3047 378 lbm_add_extension("img-color", ext_color);
3048 378 lbm_add_extension("img-color-set", ext_color_set);
3049 378 lbm_add_extension("img-color-get", ext_color_get);
3050 378 lbm_add_extension("img-color-setpre", ext_color_setpre);
3051 378 lbm_add_extension("img-color-getpre", ext_color_getpre);
3052 378 lbm_add_extension("img-dims", ext_image_dims);
3053 378 lbm_add_extension("img-setpix", ext_putpixel);
3054 378 lbm_add_extension("img-getpix", ext_getpixel);
3055 378 lbm_add_extension("img-line", ext_line);
3056 378 lbm_add_extension("img-text", ext_text);
3057 378 lbm_add_extension("img-clear", ext_clear);
3058 378 lbm_add_extension("img-circle", ext_circle);
3059 378 lbm_add_extension("img-arc", ext_arc);
3060 378 lbm_add_extension("img-circle-sector", ext_circle_sector);
3061 378 lbm_add_extension("img-circle-segment", ext_circle_segment);
3062 378 lbm_add_extension("img-rectangle", ext_rectangle);
3063 378 lbm_add_extension("img-triangle", ext_triangle);
3064 378 lbm_add_extension("img-blit", ext_blit);
3065
3066 378 lbm_add_extension("disp-reset", ext_disp_reset);
3067 378 lbm_add_extension("disp-clear", ext_disp_clear);
3068 378 lbm_add_extension("disp-render", ext_disp_render);
3069 378 lbm_add_extension("disp-render-jpg", ext_disp_render_jpg);
3070 378 }
3071
3072 68 void lbm_display_extensions_set_callbacks(
3073 bool(* volatile render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors),
3074 void(* volatile clear)(uint32_t color),
3075 void(* volatile reset)(void)
3076 ) {
3077
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 disp_render_image = render_image ? render_image : display_dummy_render_image;
3078
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 disp_clear = clear ? clear : display_dummy_clear;
3079
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 disp_reset = reset ? reset : display_dummy_reset;
3080 68 }
3081