GCC Code Coverage Report


Directory: ../src/
File: extensions/display_extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 87.1% 827 0 950
Functions: 92.0% 46 0 50
Branches: 64.5% 537 0 832

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 <math.h>
24 #include <string.h>
25
26 #include <extensions/display_extensions.h>
27 #include <lbm_utils.h>
28 #include <lbm_defrag_mem.h>
29
30 #ifdef LBM_OPT_DISPLAY_EXTENSIONS_SIZE
31 #pragma GCC optimize ("-Os")
32 #endif
33 #ifdef LBM_OPT_DISPLAY_EXTENSIONS_SIZE_AGGRESSIVE
34 #pragma GCC optimize ("-Oz")
35 #endif
36
37 #define MAX_WIDTH 32000
38 #define MAX_HEIGHT 32000
39
40 static lbm_uint symbol_indexed2 = 0;
41 static lbm_uint symbol_indexed4 = 0;
42 static lbm_uint symbol_indexed16 = 0;
43 static lbm_uint symbol_rgb332 = 0;
44 static lbm_uint symbol_rgb565 = 0;
45 static lbm_uint symbol_rgb888 = 0;
46
47 static lbm_uint symbol_thickness = 0;
48 static lbm_uint symbol_filled = 0;
49 static lbm_uint symbol_rounded = 0;
50 static lbm_uint symbol_dotted = 0;
51 static lbm_uint symbol_scale = 0;
52 static lbm_uint symbol_rotate = 0;
53 static lbm_uint symbol_resolution = 0;
54 static lbm_uint symbol_tile = 0;
55 static lbm_uint symbol_clip = 0;
56 static lbm_uint symbol_palette = 0;
57 static lbm_uint symbol_alpha_buffer = 0;
58
59
60 static lbm_uint symbol_regular = 0;
61 static lbm_uint symbol_gradient_x = 0;
62 static lbm_uint symbol_gradient_y = 0;
63 static lbm_uint symbol_gradient_x_pre = 0;
64 static lbm_uint symbol_gradient_y_pre = 0;
65 static lbm_uint symbol_repeat = 0;
66 static lbm_uint symbol_mirrored = 0;
67
68 static lbm_uint symbol_color_0 = 0;
69 static lbm_uint symbol_color_1 = 0;
70 static lbm_uint symbol_width = 0;
71 static lbm_uint symbol_offset = 0;
72 static lbm_uint symbol_repeat_type = 0;
73 static lbm_uint symbol_alpha = 0;
74
75 static lbm_uint symbol_down = 0;
76 static lbm_uint symbol_up = 0;
77 static lbm_uint symbol_magnify = 0;
78 static lbm_uint symbol_spacing = 0;
79 static lbm_uint symbol_align = 0;
80 static lbm_uint symbol_left = 0;
81 static lbm_uint symbol_center = 0;
82 static lbm_uint symbol_right = 0;
83
84 24 bool display_is_symbol_up(lbm_value v) {
85
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 if (lbm_is_symbol(v)) {
86 24 lbm_uint s = lbm_dec_sym(v);
87 24 return (s == symbol_up);
88 }
89 ✗ return false;
90 }
91
92 24 bool display_is_symbol_down(lbm_value v) {
93
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 if (lbm_is_symbol(v)) {
94 24 lbm_uint s = lbm_dec_sym(v);
95 24 return (s == symbol_down);
96 }
97 ✗ return false;
98 }
99
100 152 color_format_t sym_to_color_format(lbm_value v) {
101 152 lbm_uint s = lbm_dec_sym(v);
102
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 115 times.
152 if (s == symbol_indexed2) return indexed2;
103
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 73 times.
115 if (s == symbol_indexed4) return indexed4;
104
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 50 times.
73 if (s == symbol_indexed16) return indexed16;
105
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 43 times.
50 if (s == symbol_rgb332) return rgb332;
106
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 35 times.
43 if (s == symbol_rgb565) return rgb565;
107
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 3 times.
35 if (s == symbol_rgb888) return rgb888;
108 3 return format_not_supported;
109 }
110
111 #define COLOR_MAGIC (uint32_t)0x4C4F4300
112
113 #define COLOR_PRECALC_LEN 512
114
115 131 static lbm_value image_buffer_lift(uint8_t *buf, color_format_t fmt, uint16_t width, uint16_t height) {
116 131 lbm_value res = ENC_SYM_MERROR;
117 131 lbm_uint size = image_dims_to_size_bytes(fmt, width, height);
118
1/2
✓ Branch 1 taken 131 times.
✗ Branch 2 not taken.
131 if ( lbm_lift_array(&res, (char*)buf, IMAGE_BUFFER_HEADER_SIZE + size)) {
119 131 buf[0] = (uint8_t)(width >> 8);
120 131 buf[1] = (uint8_t)width;
121 131 buf[2] = (uint8_t)(height >> 8);
122 131 buf[3] = (uint8_t)height;
123 131 buf[4] = color_format_to_byte(fmt);
124 }
125 131 return res;
126 }
127
128 183 static inline bool is_color_sized(lbm_uint size) {
129 183 size_t color_size = sizeof(color_t);
130 183 return (size == color_size);
131 }
132
133 79 static inline bool is_color(uint8_t *data, lbm_uint size) {
134 79 bool res = false;
135
1/2
✓ Branch 1 taken 79 times.
✗ Branch 2 not taken.
79 if (is_color_sized(size)) {
136 79 color_t *color = (color_t*)data;
137 79 res = (color->magic == COLOR_MAGIC);
138 }
139 79 return res;
140 }
141
142
143 28 static lbm_value color_allocate(COLOR_TYPE type, int32_t color1, int32_t color2, uint16_t param1, uint16_t param2, bool mirrored, uint8_t alpha) {
144 28 color_t *color = lbm_malloc(sizeof(color_t));
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!color) {
146 ✗ return ENC_SYM_MERROR;
147 }
148
149 28 uint32_t *pre = 0;
150
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 23 times.
28 if (type == COLOR_PRE_X || type == COLOR_PRE_Y) {
151 5 pre = lbm_malloc(COLOR_PRECALC_LEN * sizeof(uint32_t));
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!pre) {
153 ✗ lbm_free(color);
154 ✗ return ENC_SYM_MERROR;
155 }
156 }
157
158 28 lbm_value res = ENC_SYM_MERROR;
159
160
1/2
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
28 if (lbm_lift_array(&res, (char*)color, sizeof(color_t))) {
161 28 color->magic = COLOR_MAGIC;
162 28 color->type = type;
163 28 color->color1 = color1;
164 28 color->color2 = color2;
165 28 color->param1 = param1;
166 28 color->param2 = param2;
167 28 color->mirrored = mirrored;
168 28 color->alpha = alpha;
169 28 color->precalc = pre;
170
171
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 if (pre) {
172 5 COLOR_TYPE type_old = color->type;
173
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (type == COLOR_PRE_X) {
174 4 color->type = COLOR_GRADIENT_X;
175
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 } else if (type == COLOR_PRE_Y) {
176 1 color->type = COLOR_GRADIENT_Y;
177 }
178
179
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (color->param1 > COLOR_PRECALC_LEN) {
180 1 color->param1 = COLOR_PRECALC_LEN;
181 }
182
183
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 5 times.
1129 for (int i = 0;i < color->param1;i++) {
184 1124 pre[i] = lbm_display_rgb888_from_color(*color, i + color->param2, i + color->param2);
185 }
186
187 5 color->type = type_old;
188 }
189 } else {
190 ✗ lbm_free(pre);
191 ✗ lbm_free(color);
192 }
193
194 28 return res;
195 }
196
197 131 static lbm_value image_buffer_allocate(color_format_t fmt, uint16_t width, uint16_t height) {
198 131 uint32_t size_bytes = image_dims_to_size_bytes(fmt, width, height);
199
200 131 uint8_t *buf = lbm_malloc(IMAGE_BUFFER_HEADER_SIZE + size_bytes);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (!buf) {
202 ✗ return ENC_SYM_MERROR;
203 }
204 131 memset(buf, 0, size_bytes + IMAGE_BUFFER_HEADER_SIZE);
205 131 lbm_value res = image_buffer_lift(buf, fmt, width, height);
206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 131 times.
131 if (lbm_is_symbol(res)) { /* something is wrong, free */
207 ✗ lbm_free(buf);
208 }
209 131 return res;
210 }
211
212 1 static lbm_value image_buffer_allocate_dm(lbm_uint *dm, color_format_t fmt, uint16_t width, uint16_t height) {
213 1 uint32_t size_bytes = image_dims_to_size_bytes(fmt, width, height);
214
215 1 lbm_value res = lbm_defrag_mem_alloc(dm, IMAGE_BUFFER_HEADER_SIZE + size_bytes);
216 1 lbm_array_header_t *arr = lbm_dec_array_r(res);
217
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (arr) {
218 1 uint8_t *buf = (uint8_t*)arr->data;
219 1 buf[0] = (uint8_t)(width >> 8);
220 1 buf[1] = (uint8_t)width;
221 1 buf[2] = (uint8_t)(height >> 8);
222 1 buf[3] = (uint8_t)height;
223 1 buf[4] = color_format_to_byte(fmt);
224 }
225 1 return res;
226 }
227
228 // Exported interface
229 ✗ bool display_is_color(lbm_value v) {
230 ✗ lbm_array_header_t *array = lbm_dec_array_r(v);
231 ✗ bool res = false;
232 ✗ if (array && is_color_sized(array->size)) {
233 ✗ res = (is_color((uint8_t*)array->data, array->size));
234 }
235 ✗ return res;
236 }
237
238 105 static color_t *get_color(lbm_value v) {
239 105 color_t *res = NULL;
240 105 lbm_array_header_t *array = lbm_dec_array_r(v);
241
4/4
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 79 times.
✓ Branch 4 taken 25 times.
105 if (array && is_color_sized(array->size)
242
1/2
✓ Branch 1 taken 79 times.
✗ Branch 2 not taken.
79 && (is_color((uint8_t*)array->data, array->size))) {
243 79 res = (color_t*)array->data;
244 }
245 105 return res;
246 }
247
248
249 // Register symbols
250
251 818 static bool register_symbols(void) {
252 818 bool res = true;
253
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("indexed2", &symbol_indexed2);
254
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("indexed4", &symbol_indexed4);
255
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("indexed16", &symbol_indexed16);
256
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("rgb332", &symbol_rgb332);
257
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("rgb565", &symbol_rgb565);
258
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("rgb888", &symbol_rgb888);
259
260
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("thickness", &symbol_thickness);
261
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("filled", &symbol_filled);
262
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("rounded", &symbol_rounded);
263
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("dotted", &symbol_dotted);
264
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("scale", &symbol_scale);
265
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("rotate", &symbol_rotate);
266
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("resolution", &symbol_resolution);
267
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("tile", &symbol_tile);
268
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("clip", &symbol_clip);
269
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("palette", &symbol_palette);
270
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("alpha-buffer", &symbol_alpha_buffer);
271
272
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("regular", &symbol_regular);
273
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("gradient_x", &symbol_gradient_x);
274
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("gradient_y", &symbol_gradient_y);
275
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("gradient_x_pre", &symbol_gradient_x_pre);
276
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("gradient_y_pre", &symbol_gradient_y_pre);
277
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("mirrored", &symbol_mirrored);
278
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("repeat", &symbol_repeat);
279
280
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("color-0", &symbol_color_0);
281
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("color-1", &symbol_color_1);
282
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("width", &symbol_width);
283
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("offset", &symbol_offset);
284
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("repeat-type", &symbol_repeat_type);
285
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("alpha", &symbol_alpha);
286
287
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("down", &symbol_down);
288
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("up", &symbol_up);
289
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("magnify", &symbol_magnify);
290
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("spacing", &symbol_spacing);
291
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("align", &symbol_align);
292
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("left", &symbol_left);
293
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("center", &symbol_center);
294
2/4
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 818 times.
✗ Branch 4 not taken.
818 res = res && lbm_add_symbol_const("right", &symbol_right);
295
296 818 return res;
297 }
298
299 #include "tinygfx_fonts.h"
300
301 // returns: 1 parsed, 0 not an attr list, -1 malformed/invalid
302 178 static int parse_text_attr(lbm_value v, float *mag, int *spacing, int *align, int *rotation_deg) {
303
3/4
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 151 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
178 if (lbm_is_cons(v) && lbm_car(v) == ENC_SYM_QUOTE) {
304 ✗ lbm_value quoted = lbm_cdr(v);
305 ✗ if (!lbm_is_cons(quoted) || lbm_cdr(quoted) != ENC_SYM_NIL) {
306 ✗ return -1;
307 }
308 ✗ v = lbm_car(quoted);
309 }
310
311
2/2
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 27 times.
178 if (!lbm_is_cons(v)) {
312 151 return 0;
313 }
314
315 27 lbm_value key = lbm_car(v);
316 27 lbm_value rest = lbm_cdr(v);
317
2/4
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 27 times.
27 if (!lbm_is_symbol(key) || !lbm_is_cons(rest)) {
318 ✗ return 0;
319 }
320
321 27 lbm_value val = lbm_car(rest);
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (lbm_cdr(rest) != ENC_SYM_NIL) {
323 ✗ return -1;
324 }
325
326 27 lbm_uint sym = lbm_dec_sym(key);
327
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
27 if (sym == symbol_magnify || sym == symbol_scale) {
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (!lbm_is_number(val)) return -1;
329 16 float m = lbm_dec_as_float(val);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (m < 0.1f) m = 0.1f;
331 16 *mag = m;
332 16 return 1;
333 }
334
335
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (sym == symbol_spacing) {
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!lbm_is_number(val)) return -1;
337 2 *spacing = lbm_dec_as_i32(val);
338 2 return 1;
339 }
340
341
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (sym == symbol_align) {
342
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (lbm_is_number(val)) {
343 ✗ int a = lbm_dec_as_i32(val);
344 ✗ if (a < 0 || a > 2) return -1;
345 ✗ *align = a;
346 ✗ return 1;
347 }
348
349
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (lbm_is_symbol(val)) {
350 4 lbm_uint av = lbm_dec_sym(val);
351
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (av == symbol_left) { *align = 0; return 1; }
352
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (av == symbol_center) { *align = 1; return 1; }
353
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (av == symbol_right) { *align = 2; return 1; }
354 ✗ return -1;
355 }
356
357 ✗ char *astr = lbm_dec_str(val);
358 ✗ if (!astr) return -1;
359 ✗ if (strcmp(astr, "left") == 0) { *align = 0; return 1; }
360 ✗ if (strcmp(astr, "center") == 0) { *align = 1; return 1; }
361 ✗ if (strcmp(astr, "right") == 0) { *align = 2; return 1; }
362 ✗ return -1;
363 }
364
365
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (sym == symbol_rotate) {
366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (!lbm_is_number(val)) return -1;
367 5 int r = lbm_dec_as_i32(val);
368 5 *rotation_deg = r % 360;
369 5 return 1;
370 }
371
372 ✗ return 0;
373 }
374
375 // Extensions
376
377 #define ATTR_MAX_ARGS 4
378 #define ARG_MAX_NUM 8
379
380 typedef struct {
381 bool is_valid;
382 uint16_t arg_num;
383 lbm_value args[ATTR_MAX_ARGS];
384 } attr_t;
385
386 typedef struct {
387 bool is_valid;
388 image_buffer_t img;
389 lbm_value args[ARG_MAX_NUM];
390 attr_t attr_thickness;
391 attr_t attr_filled;
392 attr_t attr_rounded;
393 attr_t attr_dotted;
394 attr_t attr_scale;
395 attr_t attr_rotate;
396 attr_t attr_resolution;
397 attr_t attr_tile;
398 attr_t attr_clip;
399 attr_t attr_palette;
400 attr_t attr_alpha;
401 attr_t attr_alpha_buffer;
402 bool transform_attr_present;
403 } img_args_t;
404
405 1343 static img_args_t decode_args(lbm_value *args, lbm_uint argn, int num_expected) {
406 img_args_t res;
407 1343 memset(&res, 0, sizeof(res));
408 1343 res.is_valid = false;
409
410 lbm_array_header_t *arr;
411
3/4
✓ Branch 0 taken 1343 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1306 times.
✓ Branch 4 taken 37 times.
1343 if (argn >= 1 && (arr = get_image_buffer(args[0]))) {
412 // at least one argument which is an image buffer.
413 1306 res.img.width = image_buffer_width((uint8_t*)arr->data);
414 1306 res.img.height = image_buffer_height((uint8_t*)arr->data);
415 1306 res.img.fmt = image_buffer_format((uint8_t*)arr->data);
416 1306 res.img.mem_base = (uint8_t*)arr->data;
417 1306 res.img.data = image_buffer_data((uint8_t*)arr->data);
418
419
420 1306 int num_dec = 0;
421
2/2
✓ Branch 0 taken 6321 times.
✓ Branch 1 taken 1280 times.
7601 for (unsigned int i = 1;i < argn;i++) {
422
4/4
✓ Branch 1 taken 773 times.
✓ Branch 2 taken 5548 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 753 times.
6321 if (!lbm_is_number(args[i]) && !lbm_is_cons(args[i])) {
423 20 color_t *clr = get_color(args[i]);
424
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 if (clr && clr->type == COLOR_REGULAR &&
425 ✗ num_dec == num_expected - 1) {
426 ✗ res.args[num_dec] = lbm_enc_u32((uint32_t)clr->color1);
427 ✗ num_dec++;
428 ✗ if (num_dec >= ARG_MAX_NUM) return res;
429 ✗ continue;
430 }
431 20 return res;
432 }
433
434
2/2
✓ Branch 1 taken 5548 times.
✓ Branch 2 taken 753 times.
6301 if (lbm_is_number(args[i])) {
435 5548 res.args[num_dec] = args[i];
436 5548 num_dec++;
437
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5548 times.
5548 if (num_dec >= ARG_MAX_NUM) {
439 ✗ return res;
440 }
441 } else {
442 753 lbm_value curr = args[i];
443 753 int attr_ind = 0;
444 753 attr_t *attr_now = 0;
445
2/2
✓ Branch 1 taken 1347 times.
✓ Branch 2 taken 751 times.
2098 while (lbm_is_cons(curr)) {
446 1347 lbm_value arg = lbm_car(curr);
447
448
2/2
✓ Branch 0 taken 753 times.
✓ Branch 1 taken 594 times.
1347 if (attr_ind == 0) {
449
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 753 times.
753 if (!lbm_is_symbol(arg)) {
450 ✗ return res;
451 }
452
453
2/2
✓ Branch 1 taken 386 times.
✓ Branch 2 taken 367 times.
753 if (lbm_dec_sym(arg) == symbol_thickness) {
454 386 attr_now = &res.attr_thickness;
455 386 attr_now->arg_num = 1;
456
2/2
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 214 times.
367 } else if (lbm_dec_sym(arg) == symbol_filled) {
457 153 attr_now = &res.attr_filled;
458 153 attr_now->arg_num = 0;
459
2/2
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 116 times.
214 } else if (lbm_dec_sym(arg) == symbol_rounded) {
460 98 attr_now = &res.attr_rounded;
461 98 attr_now->arg_num = 1;
462
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 88 times.
116 } else if (lbm_dec_sym(arg) == symbol_dotted) {
463 28 attr_now = &res.attr_dotted;
464 28 attr_now->arg_num = 2;
465
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 71 times.
88 } else if (lbm_dec_sym(arg) == symbol_scale) {
466 17 attr_now = &res.attr_scale;
467 17 attr_now->arg_num = 1;
468 17 res.transform_attr_present = true;
469
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 60 times.
71 } else if (lbm_dec_sym(arg) == symbol_rotate) {
470 11 attr_now = &res.attr_rotate;
471 11 attr_now->arg_num = 3;
472 11 res.transform_attr_present = true;
473
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 } else if (lbm_dec_sym(arg) == symbol_resolution) {
474 ✗ attr_now = &res.attr_resolution;
475 ✗ attr_now->arg_num = 1;
476
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 47 times.
60 } else if (lbm_dec_sym(arg) == symbol_tile) {
477 13 attr_now = &res.attr_tile;
478 13 attr_now->arg_num = 0;
479 13 res.transform_attr_present = true;
480
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 38 times.
47 } else if (lbm_dec_sym(arg) == symbol_clip) {
481 9 attr_now = &res.attr_clip;
482 9 attr_now->arg_num = 4;
483 9 res.transform_attr_present = true;
484
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 14 times.
38 } else if (lbm_dec_sym(arg) == symbol_palette) {
485 24 attr_now = &res.attr_palette;
486 24 attr_now->arg_num = 1;
487
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 time.
14 } else if (lbm_dec_sym(arg) == symbol_alpha) {
488 13 attr_now = &res.attr_alpha;
489 13 attr_now->arg_num = 1;
490
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 } else if (lbm_dec_sym(arg) == symbol_alpha_buffer) {
491 ✗ attr_now = &res.attr_alpha_buffer;
492 ✗ attr_now->arg_num = 1;
493 }
494 else {
495 1 return res;
496 }
497 } else {
498
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 570 times.
594 if (attr_now == &res.attr_palette) {
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (!lbm_is_list(arg)) {
500 ✗ return res;
501 }
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 570 times.
570 } else if (attr_now == &res.attr_alpha_buffer) {
503 ✗ if (!lbm_dec_array_r(arg)) {
504 ✗ return res;
505 }
506
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 569 times.
570 } else if (!lbm_is_number(arg)) {
507 1 return res;
508 }
509
510 593 attr_now->args[attr_ind - 1] = arg;
511 }
512
513 1345 attr_ind++;
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1345 times.
1345 if (attr_ind > (ATTR_MAX_ARGS + 1)) {
515 ✗ return res;
516 }
517
518 1345 curr = lbm_cdr(curr);
519 }
520
521 // does this really compare the pointer addresses?
522
4/4
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 653 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 33 times.
751 if (attr_now == &res.attr_rounded && attr_ind == 1) {
523 65 attr_now->arg_num = 0; // the `rounded` attribute may be empty
524 }
525
526
527
2/2
✓ Branch 0 taken 747 times.
✓ Branch 1 taken 4 times.
751 if ((attr_ind - 1) == attr_now->arg_num) {
528 747 attr_now->is_valid = true;
529 } else {
530 4 return res;
531 }
532 }
533 }
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 if (num_dec != num_expected) {
535 ✗ return res;
536 }
537 // I think this should go here ???
538 1280 res.is_valid = true;
539 }
540 1317 return res;
541 }
542
543 32 static lbm_value ext_image_dims(lbm_value *args, lbm_uint argn) {
544 32 img_args_t arg_dec = decode_args(args, argn, 0);
545
546
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
32 if (!arg_dec.is_valid) {
547 2 return ENC_SYM_TERROR;
548 }
549
550 30 lbm_value dims = lbm_heap_allocate_list(2);
551
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (lbm_is_symbol(dims)) {
552 ✗ return dims;
553 }
554 30 lbm_value curr = dims;
555 30 lbm_set_car(curr, lbm_enc_i(arg_dec.img.width));
556 30 curr = lbm_cdr(curr);
557 30 lbm_set_car(curr, lbm_enc_i(arg_dec.img.height));
558 30 return dims;
559 }
560
561 137 static lbm_value ext_image_buffer(lbm_value *args, lbm_uint argn) {
562 137 lbm_value res = ENC_SYM_TERROR;
563 137 bool args_ok = false;
564 137 color_format_t fmt = indexed2;
565 137 lbm_uint w = 0;
566 137 lbm_uint h = 0;
567
568
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
138 if (argn == 4 &&
569
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_defrag_mem(args[0]) &&
570
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_symbol(args[1]) &&
571
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
2 lbm_is_number(args[2]) &&
572 1 lbm_is_number(args[3])) {
573 1 fmt = sym_to_color_format(args[1]);
574 1 w = lbm_dec_as_u32(args[2]);
575 1 h = lbm_dec_as_u32(args[3]);
576 1 args_ok = true;
577
2/4
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
272 } else if (argn == 3 &&
578
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
272 lbm_is_symbol(args[0]) &&
579
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
272 lbm_is_number(args[1]) &&
580 136 lbm_is_number(args[2])) {
581 136 fmt = sym_to_color_format(args[0]);
582 136 w = lbm_dec_as_u32(args[1]);
583 136 h = lbm_dec_as_u32(args[2]);
584 136 args_ok = true;
585 }
586
587
9/12
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 133 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 132 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 132 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 132 times.
✗ Branch 11 not taken.
137 if (args_ok && fmt != format_not_supported && w > 0 && h > 0 && w < MAX_WIDTH && h < MAX_HEIGHT) {
588
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1 time.
132 if (argn == 3) {
589 131 res = image_buffer_allocate(fmt, (uint16_t)w, (uint16_t)h);
590 } else {
591 1 res = image_buffer_allocate_dm((lbm_uint*)lbm_car(args[0]), fmt, (uint16_t)w, (uint16_t)h);
592 }
593 }
594 137 return res;
595 }
596
597
598 26 static lbm_value ext_is_image_buffer(lbm_value *args, lbm_uint argn) {
599 26 lbm_value res = ENC_SYM_TERROR;
600
601
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (argn == 1) {
602 26 res = ENC_SYM_NIL;
603 26 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
604
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 time.
26 if (array) {
605 25 uint8_t *data = (uint8_t*)array->data;
606
1/2
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
25 if (image_buffer_is_valid(data, array->size)) {
607 25 res = ENC_SYM_TRUE;;
608 }
609 }
610 }
611 26 return res;
612 }
613
614 30 static lbm_value ext_color(lbm_value *args, lbm_uint argn) {
615 30 lbm_value res = ENC_SYM_TERROR;
616
617
4/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 1 time.
60 if (argn >= 2 && argn <= 7 &&
618
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
59 lbm_is_symbol(args[0]) &&
619 29 lbm_is_number(args[1])) {
620
621 // Color1 and color2 are int in the struct and decoded as i32, why
622 // where they stored in uint32_t?
623 29 int32_t color1 = lbm_dec_as_i32(args[1]);
624
625 29 int32_t color2 = 0;
626
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 15 times.
29 if (argn >= 3) {
627
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[2])) {
628 14 color2 = lbm_dec_as_i32(args[2]);
629 } else {
630 ✗ return ENC_SYM_TERROR;
631 }
632 }
633
634 29 int32_t param1 = 0;
635
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 15 times.
29 if (argn >= 4) {
636
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[3])) {
637 14 param1 = lbm_dec_as_i32(args[3]);
638 } else {
639 ✗ return ENC_SYM_TERROR;
640 }
641 }
642
643 29 int32_t param2 = 0;
644
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 15 times.
29 if (argn >= 5) {
645
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (lbm_is_number(args[4])) {
646 14 param2 = lbm_dec_as_i32(args[4]);
647 } else {
648 ✗ return ENC_SYM_TERROR;
649 }
650 }
651
652 29 bool mirrored = false;
653 29 lbm_uint alpha_arg = argn;
654
655
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 19 times.
29 if (argn >= 6) {
656
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (lbm_is_symbol(args[5])) {
657 10 lbm_uint sym = lbm_dec_sym(args[5]);
658
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 if (sym == symbol_repeat) {
659 7 mirrored = false;
660
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 } else if (sym == symbol_mirrored) {
661 3 mirrored = true;
662 } else {
663 ✗ return ENC_SYM_TERROR;
664 }
665 10 alpha_arg = 6;
666 ✗ } else if (argn == 6 && lbm_is_number(args[5])) {
667 ✗ alpha_arg = 5;
668 } else {
669 ✗ return ENC_SYM_TERROR;
670 }
671 }
672
673 COLOR_TYPE t;
674
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 15 times.
29 if (lbm_dec_sym(args[0]) == symbol_regular) {
675 14 t = COLOR_REGULAR;
676
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 9 times.
15 } else if (lbm_dec_sym(args[0]) == symbol_gradient_x) {
677 6 t = COLOR_GRADIENT_X;
678
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
9 } else if (lbm_dec_sym(args[0]) == symbol_gradient_y) {
679 3 t = COLOR_GRADIENT_Y;
680
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
6 } else if (lbm_dec_sym(args[0]) == symbol_gradient_x_pre) {
681 4 t = COLOR_PRE_X;
682
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
2 } else if (lbm_dec_sym(args[0]) == symbol_gradient_y_pre) {
683 1 t = COLOR_PRE_Y;
684 } else {
685 1 return ENC_SYM_TERROR;
686 }
687
688
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
28 if (t == COLOR_REGULAR && argn == 3) {
689 ✗ alpha_arg = 2;
690 }
691
692 28 uint8_t alpha = 255;
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (alpha_arg < argn) {
694 ✗ if (lbm_is_number(args[alpha_arg])) {
695 ✗ int32_t a = lbm_dec_as_i32(args[alpha_arg]);
696 ✗ if (a < 0) a = 0;
697 ✗ if (a > 255) a = 255;
698 ✗ alpha = (uint8_t)a;
699 } else {
700 ✗ return ENC_SYM_TERROR;
701 }
702 }
703
704 // Maybe check if param is in ranges first ?
705 28 res = color_allocate(t, color1, color2, (uint16_t)param1, (uint16_t)param2, mirrored, alpha);
706 }
707
708 29 return res;
709 }
710
711 25 static lbm_value ext_color_set(lbm_value *args, lbm_uint argn) {
712 color_t *color;
713
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
714
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 !lbm_is_symbol(args[1])) {
715 3 return ENC_SYM_TERROR;
716 }
717
718 22 bool is_regular = color->type == COLOR_REGULAR;
719
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;
720
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;
721
722 22 lbm_uint prop = lbm_dec_sym(args[1]);
723
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 16 times.
22 if (prop == symbol_color_0) {
724
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)) {
725 2 return ENC_SYM_TERROR;
726 }
727 4 color->color1 = lbm_dec_as_i32(args[2]);
728
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 13 times.
16 } else if (prop == symbol_color_1) {
729
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) {
730 2 return ENC_SYM_TERROR;
731 }
732 1 color->color2 = lbm_dec_as_i32(args[2]);
733
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
13 } else if (prop == symbol_width) {
734
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) {
735 3 return ENC_SYM_TERROR;
736 }
737 2 color->param1 = (uint16_t)lbm_dec_as_u32(args[2]);
738
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 } else if (prop == symbol_offset) {
739
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)) {
740 1 return ENC_SYM_TERROR;
741 }
742 2 color->param2 = (uint16_t)lbm_dec_as_u32(args[2]);
743
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 } else if (prop == symbol_repeat_type) {
744
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)) {
745 ✗ return ENC_SYM_TERROR;
746 }
747 3 lbm_uint sym = lbm_dec_sym(args[2]);
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (sym == symbol_repeat) {
749 ✗ color->mirrored = false;
750
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 } else if (sym == symbol_mirrored) {
751 2 color->mirrored = true;
752 } else {
753 1 return ENC_SYM_TERROR;
754 }
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (prop == symbol_alpha) {
756 ✗ if (!lbm_is_number(args[2])) {
757 ✗ return ENC_SYM_TERROR;
758 }
759 ✗ int32_t a = lbm_dec_as_i32(args[2]);
760 ✗ if (a < 0) a = 0;
761 ✗ if (a > 255) a = 255;
762 ✗ color->alpha = (uint8_t)a;
763 } else {
764 2 return ENC_SYM_TERROR;
765 }
766
767 11 return ENC_SYM_TRUE;
768 }
769
770 23 static lbm_value ext_color_get(lbm_value *args, lbm_uint argn) {
771 color_t *color;
772
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
773
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 !lbm_is_symbol(args[1])) {
774 1 return ENC_SYM_TERROR;
775 }
776
777
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;
778
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;
779
780 22 lbm_uint prop = lbm_dec_sym(args[1]);
781
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
22 if (prop == symbol_color_0) {
782 // always allowed
783 9 return lbm_enc_u32((uint32_t)color->color1);
784
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 } else if (prop == symbol_color_1) {
785
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) {
786 1 return ENC_SYM_TERROR;
787 }
788 3 return lbm_enc_u32((uint32_t)color->color2);
789
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 } else if (prop == symbol_width) {
790
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) {
791 1 return ENC_SYM_TERROR;
792 }
793 4 return lbm_enc_i32((int32_t)color->param1);
794
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 } else if (prop == symbol_offset) {
795
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) {
796 ✗ return ENC_SYM_TERROR;
797 }
798 3 return lbm_enc_i32((int32_t)color->param2);
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 } else if (prop == symbol_repeat_type) {
800 ✗ if (!is_gradient && !is_pre) {
801 ✗ return ENC_SYM_TERROR;
802 }
803 ✗ return lbm_enc_sym(color->mirrored ? symbol_mirrored : symbol_repeat);
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 } else if (prop == symbol_alpha) {
805 ✗ return lbm_enc_u32((uint32_t)color->alpha);
806 } else {
807 1 return ENC_SYM_TERROR;
808 }
809
810 return ENC_SYM_TRUE;
811 }
812
813 7 static lbm_value ext_color_setpre(lbm_value *args, lbm_uint argn) {
814 color_t *color;
815
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])) ||
816
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])) {
817 3 return ENC_SYM_TERROR;
818 }
819
820 4 uint32_t pos = lbm_dec_as_u32(args[1]);
821 4 int new_color = lbm_dec_as_i32(args[2]);
822
823
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) {
824 1 return ENC_SYM_EERROR;
825 }
826
827 3 color->precalc[pos] = (uint32_t)new_color;
828
829 3 return ENC_SYM_TRUE;
830 }
831
832 14 static lbm_value ext_color_getpre(lbm_value *args, lbm_uint argn) {
833 color_t *color;
834
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])) ||
835
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 12 times.
13 !lbm_is_number(args[1])) {
836 2 return ENC_SYM_TERROR;
837 }
838 12 uint32_t pos = lbm_dec_as_u32(args[1]);
839
840
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) {
841 1 return ENC_SYM_EERROR;
842 }
843
844 11 return lbm_enc_u32(color->precalc[pos]);
845 }
846
847 111 static lbm_value ext_clear(lbm_value *args, lbm_uint argn) {
848
849 111 lbm_value res = ENC_SYM_TERROR;
850 lbm_array_header_t *arr;
851
5/6
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 3 times.
222 if ((argn == 1 || argn == 2) &&
852
2/2
✓ Branch 1 taken 101 times.
✓ Branch 2 taken 7 times.
219 (arr = get_image_buffer(args[0])) && // assignment
853
1/2
✓ Branch 1 taken 101 times.
✗ Branch 2 not taken.
101 (argn != 2 || lbm_is_number(args[1]))) { // ( argn == 2 -> lbm_is_number(args[1]))
854 image_buffer_t img_buf;
855 108 img_buf.width = image_buffer_width((uint8_t*)arr->data);
856 108 img_buf.height = image_buffer_height((uint8_t*)arr->data);
857 108 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
858 108 img_buf.mem_base = (uint8_t*)arr->data;
859 108 img_buf.data = image_buffer_data((uint8_t*)arr->data);
860
861 108 uint32_t color = 0;
862
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 7 times.
108 if (argn == 2) {
863 101 color = lbm_dec_as_u32(args[1]);
864 }
865
866 108 image_buffer_clear(&img_buf, color);
867 108 res = ENC_SYM_TRUE;
868 }
869 111 return res;
870 }
871
872 145 static lbm_value ext_putpixel(lbm_value *args, lbm_uint argn) {
873 145 img_args_t arg_dec = decode_args(args, argn, 3);
874
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145 times.
145 if (!arg_dec.is_valid) {
876 ✗ return ENC_SYM_TERROR;
877 }
878
879 145 putpixel(&arg_dec.img,
880 lbm_dec_as_i32(arg_dec.args[0]),
881 lbm_dec_as_i32(arg_dec.args[1]),
882 lbm_dec_as_u32(arg_dec.args[2]));
883 145 return ENC_SYM_TRUE;
884 }
885
886 226 static lbm_value ext_getpixel(lbm_value *args, lbm_uint argn) {
887 226 img_args_t arg_dec = decode_args(args, argn, 2);
888
889
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 202 times.
226 if (!arg_dec.is_valid) {
890 24 return ENC_SYM_TERROR;
891 }
892
893 202 uint32_t c = getpixel(&arg_dec.img,
894 lbm_dec_as_i32(arg_dec.args[0]),
895 lbm_dec_as_i32(arg_dec.args[1]));
896 202 return lbm_enc_u32(c);
897 }
898
899 // lisp args: img x1 y1 x2 y2 color opt-attr1 ... opt-attrN
900 71 static lbm_value ext_line(lbm_value *args, lbm_uint argn) {
901 71 img_args_t arg_dec = decode_args(args, argn, 5);
902
903
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 69 times.
71 if (!arg_dec.is_valid) {
904 2 return ENC_SYM_TERROR;
905 }
906
907 69 tinygfx_line(&arg_dec.img,
908 lbm_dec_as_i32(arg_dec.args[0]),
909 lbm_dec_as_i32(arg_dec.args[1]),
910 lbm_dec_as_i32(arg_dec.args[2]),
911 lbm_dec_as_i32(arg_dec.args[3]),
912 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
913 lbm_dec_as_i32(arg_dec.attr_dotted.args[0]),
914 lbm_dec_as_i32(arg_dec.attr_dotted.args[1]),
915 lbm_dec_as_u32(arg_dec.args[4]));
916
917 69 return ENC_SYM_TRUE;
918 }
919
920 // Shared by ext_arc/ext_circle_sector/ext_circle_segment, which decode
921 // identical attribute sets and only differ in sector/segment and which
922 // args[] slot holds the color.
923 457 static arc_params_t make_arc_params(const img_args_t *a, bool rounded, bool sector, bool segment, lbm_value color_arg) {
924 2285 arc_params_t p = {
925 457 .thickness = lbm_dec_as_i32(a->attr_thickness.args[0]),
926 .rounded = rounded,
927 457 .filled = a->attr_filled.is_valid,
928 .sector = sector,
929 .segment = segment,
930 457 .dot1 = lbm_dec_as_i32(a->attr_dotted.args[0]),
931 457 .dot2 = lbm_dec_as_i32(a->attr_dotted.args[1]),
932 457 .resolution = lbm_dec_as_i32(a->attr_resolution.args[0]),
933 457 .color = lbm_dec_as_u32(color_arg),
934 };
935 457 return p;
936 }
937
938 // lisp args: img cx cy r color opt-attr1 ... opt-attrN
939 96 static lbm_value ext_circle(lbm_value *args, lbm_uint argn) {
940 96 img_args_t arg_dec = decode_args(args, argn, 4);
941
942
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 93 times.
96 if (!arg_dec.is_valid) {
943 3 return ENC_SYM_TERROR;
944 }
945
946
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 63 times.
93 if (arg_dec.attr_filled.is_valid) {
947 30 tinygfx_fill_circle(&arg_dec.img,
948 lbm_dec_as_i32(arg_dec.args[0]),
949 lbm_dec_as_i32(arg_dec.args[1]),
950 lbm_dec_as_i32(arg_dec.args[2]),
951 lbm_dec_as_u32(arg_dec.args[3]));
952
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 62 times.
63 } else if (arg_dec.attr_dotted.is_valid) {
953 // rounded here currently does nothing as the line function doesn't
954 // support square ends.
955 1 arc_params_t p = make_arc_params(&arg_dec, arg_dec.attr_rounded.is_valid, false, false, arg_dec.args[3]);
956 1 tinygfx_arc(&arg_dec.img,
957 lbm_dec_as_i32(arg_dec.args[0]),
958 lbm_dec_as_i32(arg_dec.args[1]),
959 lbm_dec_as_i32(arg_dec.args[2]),
960 0, 359.9f,
961 &p);
962 } else {
963 62 tinygfx_circle(&arg_dec.img,
964 lbm_dec_as_i32(arg_dec.args[0]),
965 lbm_dec_as_i32(arg_dec.args[1]),
966 lbm_dec_as_i32(arg_dec.args[2]),
967 lbm_dec_as_i32(arg_dec.attr_thickness.args[0]),
968 lbm_dec_as_u32(arg_dec.args[3]));
969 }
970
971 93 return ENC_SYM_TRUE;
972 }
973
974 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
975 137 static lbm_value ext_arc(lbm_value *args, lbm_uint argn) {
976 137 img_args_t arg_dec = decode_args(args, argn, 6);
977
978
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 128 times.
137 if (!arg_dec.is_valid) {
979 9 return ENC_SYM_TERROR;
980 }
981
982 128 arc_params_t p = make_arc_params(&arg_dec, arg_dec.attr_rounded.is_valid, false, false, arg_dec.args[5]);
983 128 tinygfx_arc(&arg_dec.img,
984 lbm_dec_as_i32(arg_dec.args[0]),
985 lbm_dec_as_i32(arg_dec.args[1]),
986 lbm_dec_as_i32(arg_dec.args[2]),
987 lbm_dec_as_float(arg_dec.args[3]),
988 lbm_dec_as_float(arg_dec.args[4]),
989 &p);
990
991 128 return ENC_SYM_TRUE;
992 }
993
994 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
995 56 static lbm_value ext_circle_sector(lbm_value *args, lbm_uint argn) {
996 56 img_args_t arg_dec = decode_args(args, argn, 6);
997
998
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 55 times.
56 if (!arg_dec.is_valid) {
999 1 return ENC_SYM_TERROR;
1000 }
1001
1002 55 arc_params_t p = make_arc_params(&arg_dec, true, true, false, arg_dec.args[5]);
1003 55 tinygfx_arc(&arg_dec.img,
1004 lbm_dec_as_i32(arg_dec.args[0]),
1005 lbm_dec_as_i32(arg_dec.args[1]),
1006 lbm_dec_as_i32(arg_dec.args[2]),
1007 lbm_dec_as_float(arg_dec.args[3]),
1008 lbm_dec_as_float(arg_dec.args[4]),
1009 &p);
1010
1011 55 return ENC_SYM_TRUE;
1012 }
1013
1014 // lisp args: img cx cy r ang-s ang-e color opt-attr1 ... opt-attrN
1015 284 static lbm_value ext_circle_segment(lbm_value *args, lbm_uint argn) {
1016 284 img_args_t arg_dec = decode_args(args, argn, 6);
1017
1018
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 273 times.
284 if (!arg_dec.is_valid) {
1019 11 return ENC_SYM_TERROR;
1020 }
1021
1022 273 arc_params_t p = make_arc_params(&arg_dec, true, false, true, arg_dec.args[5]);
1023 273 tinygfx_arc(&arg_dec.img,
1024 lbm_dec_as_i32(arg_dec.args[0]),
1025 lbm_dec_as_i32(arg_dec.args[1]),
1026 lbm_dec_as_i32(arg_dec.args[2]),
1027 lbm_dec_as_float(arg_dec.args[3]),
1028 lbm_dec_as_float(arg_dec.args[4]),
1029 &p);
1030
1031
1032 273 return ENC_SYM_TRUE;
1033 }
1034
1035 // lisp args: img x y width height color opt-attr1 ... opt-attrN
1036 124 static lbm_value ext_rectangle(lbm_value *args, lbm_uint argn) {
1037 124 img_args_t arg_dec = decode_args(args, argn, 5);
1038
1039
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 121 times.
124 if (!arg_dec.is_valid) {
1040 3 return ENC_SYM_TERROR;
1041 }
1042
1043 121 image_buffer_t *img = &arg_dec.img;
1044 121 int x = lbm_dec_as_i32(arg_dec.args[0]);
1045 121 int y = lbm_dec_as_i32(arg_dec.args[1]);
1046 121 int width = lbm_dec_as_i32(arg_dec.args[2]);
1047 121 int height = lbm_dec_as_i32(arg_dec.args[3]);
1048 121 int rad = lbm_dec_as_i32(arg_dec.attr_rounded.args[0]);
1049 121 int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]);
1050 121 uint32_t color = lbm_dec_as_u32(arg_dec.args[4]);
1051 121 int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]);
1052 121 int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]);
1053 121 int resolution = lbm_dec_as_i32(arg_dec.attr_resolution.args[0]);
1054
1055
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 87 times.
121 if (arg_dec.attr_rounded.is_valid) {
1056
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
34 if (arg_dec.attr_filled.is_valid) {
1057 4 tinygfx_fill_rounded_rectangle(img, x, y, width, height, rad, color);
1058 } else {
1059 30 tinygfx_rounded_rectangle(img, x, y, width, height, rad, thickness, dot1, dot2, resolution, color);
1060 }
1061 } else {
1062 87 tinygfx_rectangle(img,
1063 x, y,
1064 width, height,
1065 87 arg_dec.attr_filled.is_valid,
1066 thickness,
1067 dot1, dot2,
1068 color);
1069 }
1070
1071 121 return ENC_SYM_TRUE;
1072 }
1073
1074 // lisp args: img x1 y1 x2 y2 x3 y3 color opt-attr1 ... opt-attrN
1075 25 static lbm_value ext_triangle(lbm_value *args, lbm_uint argn) {
1076 25 img_args_t arg_dec = decode_args(args, argn, 7);
1077
1078
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 23 times.
25 if (!arg_dec.is_valid) {
1079 2 return ENC_SYM_TERROR;
1080 }
1081
1082 23 image_buffer_t *img = &arg_dec.img;
1083 23 int x0 = lbm_dec_as_i32(arg_dec.args[0]);
1084 23 int y0 = lbm_dec_as_i32(arg_dec.args[1]);
1085 23 int x1 = lbm_dec_as_i32(arg_dec.args[2]);
1086 23 int y1 = lbm_dec_as_i32(arg_dec.args[3]);
1087 23 int x2 = lbm_dec_as_i32(arg_dec.args[4]);
1088 23 int y2 = lbm_dec_as_i32(arg_dec.args[5]);
1089 23 int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]);
1090 23 int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]);
1091 23 int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]);
1092 23 uint32_t color = lbm_dec_as_u32(arg_dec.args[6]);
1093
1094
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 if (arg_dec.attr_filled.is_valid) {
1095 5 tinygfx_fill_triangle(img, x0, y0, x1, y1, x2, y2, color);
1096 } else {
1097 18 tinygfx_line(img, x0, y0, x1, y1, thickness, dot1, dot2, color);
1098 18 tinygfx_line(img, x1, y1, x2, y2, thickness, dot1, dot2, color);
1099 18 tinygfx_line(img, x2, y2, x0, y0, thickness, dot1, dot2, color);
1100 }
1101
1102 23 return ENC_SYM_TRUE;
1103 }
1104
1105 108 static bool font_data_is_valid(const lbm_array_header_t *font) {
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (font->size < 4) {
1107 ✗ return false;
1108 }
1109
1110 108 const uint8_t *font_data = (const uint8_t*)font->data;
1111 108 uint8_t bits_per_pixel = font_data[3];
1112
5/8
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 72 times.
✓ Branch 7 taken 36 times.
108 if (font_data[0] == 0 || font_data[1] == 0 || font_data[2] == 0 ||
1113
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 71 times.
72 (bits_per_pixel != 1 && bits_per_pixel != 2)) {
1114 1 return false;
1115 }
1116
1117 107 uint32_t bytes_per_char =
1118 107 ((uint32_t)font_data[0] * font_data[1] * bits_per_pixel + 7) / 8;
1119 107 uint32_t required_size = 4 + bytes_per_char * font_data[2];
1120 107 return font->size >= required_size;
1121 }
1122
1123 // lisp args:
1124 // img x y fg bg font str [attrs] ['up|'down]
1125 // img x y fg bg font-id str [attrs] ['up|'down] uses a compiled-in font
1126 // img x y fg bg str [attrs] ['up|'down] uses TINYGFX_DEFAULT_FONT_ID
1127 // img x y '(c0..c3) font str [attrs] ['up|'down] 4-color form for 2bpp fonts
1128 // attrs: '(magnify N) '(scale N) '(spacing N) '(align 'left|'center|'right) '(rotate deg)
1129 // orient: 0=normal 1=up/90CCW 2=180 3=down/90CW
1130 151 static lbm_value ext_text(lbm_value *args, lbm_uint argn) {
1131 151 int orient = 0;
1132
1133
3/4
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 121 times.
151 if (argn >= 6 && lbm_is_symbol(args[argn - 1])) {
1134 30 lbm_uint sym = lbm_dec_sym(args[argn - 1]);
1135
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 16 times.
30 if (sym == symbol_up) { orient = 1; argn--; }
1136
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 16 times.
30 if (sym == symbol_down) { orient = 3; argn--; }
1137 }
1138
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 if (argn < 6) return ENC_SYM_TERROR;
1140
1141 151 float txt_mag = 1.0f;
1142 151 int spacing = 0;
1143 151 int align = 0;
1144 151 int rot_deg = 0;
1145
1146 151 lbm_uint core_argn = argn;
1147
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 while (core_argn > 0) {
1148 178 int r = parse_text_attr(args[core_argn - 1], &txt_mag, &spacing, &align, &rot_deg);
1149
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 151 times.
178 if (r == 1) { core_argn--; continue; }
1150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 if (r < 0) { return ENC_SYM_TERROR; }
1151 151 break;
1152 }
1153
1154
4/4
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 97 times.
151 if (core_argn != 6 && core_argn != 7) return ENC_SYM_TERROR;
1155
1156
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 145 times.
148 if (rot_deg != 0) {
1157 3 orient = (((rot_deg % 360) + 360) % 360) / 90;
1158 }
1159
1160 148 int x = lbm_dec_as_i32(args[1]);
1161 148 int y = lbm_dec_as_i32(args[2]);
1162
1163 148 int32_t colors[4] = {-1, -1, -1, -1};
1164 148 const uint8_t *font_data = NULL;
1165 148 char *txt = NULL;
1166
1167
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 132 times.
148 if (lbm_is_cons(args[3])) {
1168 // color list form: img x y '(c0..cN) font str
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (core_argn != 6) return ENC_SYM_TERROR;
1170 16 lbm_value curr = args[3];
1171 16 int ci = 0;
1172
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) {
1173 47 lbm_value a = lbm_car(curr);
1174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if (!lbm_is_number(a)) return ENC_SYM_TERROR;
1175 47 colors[ci++] = lbm_dec_as_i32(a);
1176 47 curr = lbm_cdr(curr);
1177 }
1178
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if (lbm_type_of_functional(args[4]) == LBM_TYPE_ARRAY) {
1179 16 lbm_array_header_t *fh = (lbm_array_header_t*)lbm_car(args[4]);
1180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (!font_data_is_valid(fh)) return ENC_SYM_TERROR;
1181 16 const uint8_t *fd = (const uint8_t*)fh->data;
1182 16 font_data = fd;
1183 ✗ } else if (lbm_is_number(args[4])) {
1184 ✗ font_data = tinygfx_get_builtin_font(lbm_dec_as_i32(args[4]));
1185 ✗ if (!font_data) return ENC_SYM_TERROR;
1186 } else {
1187 ✗ return ENC_SYM_TERROR;
1188 }
1189 16 txt = lbm_dec_str(args[5]);
1190
2/4
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 132 times.
✗ Branch 5 not taken.
132 } else if (lbm_is_number(args[3]) && lbm_is_number(args[4])) {
1191 132 colors[0] = lbm_dec_as_i32(args[3]);
1192 132 colors[1] = lbm_dec_as_i32(args[4]);
1193
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 35 times.
132 if (core_argn == 7) {
1194
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 5 times.
97 if (lbm_type_of_functional(args[5]) == LBM_TYPE_ARRAY) {
1195 92 lbm_array_header_t *fh = (lbm_array_header_t*)lbm_car(args[5]);
1196
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 91 times.
92 if (!font_data_is_valid(fh)) return ENC_SYM_TERROR;
1197 91 const uint8_t *fd = (const uint8_t*)fh->data;
1198 91 font_data = fd;
1199
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 } else if (lbm_is_number(args[5])) {
1200 5 font_data = tinygfx_get_builtin_font(lbm_dec_as_i32(args[5]));
1201
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (!font_data) return ENC_SYM_TERROR;
1202 } else {
1203 ✗ return ENC_SYM_TERROR;
1204 }
1205 95 txt = lbm_dec_str(args[6]);
1206 } else {
1207 35 font_data = tinygfx_get_builtin_font(TINYGFX_DEFAULT_FONT_ID);
1208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!font_data) return ENC_SYM_TERROR;
1209 35 txt = lbm_dec_str(args[5]);
1210 }
1211 } else {
1212 ✗ return ENC_SYM_TERROR;
1213 }
1214
1215
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 143 times.
146 if (!txt) return ENC_SYM_TERROR;
1216
1217
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 137 times.
143 if (!array_is_image_buffer(args[0])) return ENC_SYM_TERROR;
1218 137 lbm_array_header_t *arr = (lbm_array_header_t *)lbm_car(args[0]);
1219 image_buffer_t img_buf;
1220 137 img_buf.width = image_buffer_width((uint8_t*)arr->data);
1221 137 img_buf.height = image_buffer_height((uint8_t*)arr->data);
1222 137 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
1223 137 img_buf.mem_base = (uint8_t*)arr->data;
1224 137 img_buf.data = image_buffer_data((uint8_t*)arr->data);
1225
1226 137 uint8_t w = font_data[0];
1227
1228 static const int8_t incx_tbl[4] = { 1, 0, -1, 0};
1229 static const int8_t incy_tbl[4] = { 0, -1, 0, 1};
1230 137 int incx = incx_tbl[orient];
1231 137 int incy = incy_tbl[orient];
1232
1233 137 float char_step_f = (float)w * txt_mag + (float)spacing;
1234 137 int char_step = (int)lroundf(char_step_f);
1235 137 int txt_len = (int)strlen(txt);
1236 137 int total = (int)lroundf((float)txt_len * char_step_f - (float)spacing);
1237
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 135 times.
137 int align_offset = (align == 1) ? total / 2 : (align == 2) ? total : 0;
1238
1239 137 int x0 = x - align_offset * incx;
1240 137 int y0 = y - align_offset * incy;
1241
1242
2/2
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 137 times.
1088 for (int ind = 0; txt[ind] != 0; ind++) {
1243 951 tinygfx_img_putc(&img_buf,
1244 951 x0 + ind * char_step * incx,
1245 951 y0 + ind * char_step * incy,
1246 (uint32_t *)colors,
1247 4,
1248 font_data,
1249 951 (uint8_t)txt[ind],
1250 orient,
1251 txt_mag);
1252 }
1253
1254 137 return ENC_SYM_TRUE;
1255 }
1256
1257
1258 // Number of distinct index values an indexed format can hold, 0 if fmt isn't indexed.
1259 276 static int indexed_depth(color_format_t fmt) {
1260
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 86 times.
276 switch (fmt) {
1261 48 case indexed2: return 2;
1262 114 case indexed4: return 4;
1263 28 case indexed16: return 16;
1264 86 default: return 0;
1265 }
1266 }
1267
1268 // Decodes a plain numeric list (as opposed to lbm_display_decode_color_list's
1269 // color-or-number list) into out[], up to max_len entries. Reports the exact
1270 // decoded length via out_len so callers can validate it against a required size.
1271 23 static bool decode_palette_list(lbm_value lst, uint32_t *out, int max_len, int *out_len) {
1272 23 int i = 0;
1273 23 lbm_value curr = lst;
1274
2/2
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 22 times.
148 while (lbm_is_cons(curr)) {
1275
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 125 times.
126 if (i >= max_len) return false;
1276 125 lbm_value arg = lbm_car(curr);
1277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 125 times.
125 if (!lbm_is_number(arg)) return false;
1278 125 out[i] = lbm_dec_as_u32(arg);
1279 125 i++;
1280 125 curr = lbm_cdr(curr);
1281 }
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (curr != ENC_SYM_NIL) return false;
1283 22 *out_len = i;
1284 22 return true;
1285 }
1286
1287 // TODO: Think about way to simplify the blit dispatch
1288 // based on transform or not.
1289 147 static lbm_value ext_blit(lbm_value *args, lbm_uint argn) {
1290 147 img_args_t arg_dec = decode_args(args + 1, argn - 1, 3);
1291
1292 147 lbm_value res = ENC_SYM_TERROR;
1293 lbm_array_header_t *arr;
1294
4/4
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 138 times.
✓ Branch 4 taken 3 times.
147 if (arg_dec.is_valid && (arr = get_image_buffer(args[0]))) { //assignment
1295 image_buffer_t dest_buf;
1296 138 dest_buf.width = image_buffer_width((uint8_t*)arr->data);
1297 138 dest_buf.height = image_buffer_height((uint8_t*)arr->data);
1298 138 dest_buf.fmt = image_buffer_format((uint8_t*)arr->data);
1299 138 dest_buf.mem_base = (uint8_t*)arr->data;
1300 138 dest_buf.data = image_buffer_data((uint8_t*)arr->data);
1301
1302 138 int dest_x = lbm_dec_as_i32(arg_dec.args[0]);
1303 138 int dest_y = lbm_dec_as_i32(arg_dec.args[1]);
1304 138 int32_t transparent_color = lbm_dec_as_i32(arg_dec.args[2]);
1305
1306 138 int src_depth = indexed_depth(arg_dec.img.fmt);
1307 138 int dest_depth = indexed_depth(dest_buf.fmt);
1308
1309
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 34 times.
138 if (src_depth == 0 && dest_depth != 0) {
1310 4 lbm_set_error_reason("img-blit: an rgb source cannot be blitted into an indexed destination.");
1311 22 return ENC_SYM_EERROR;
1312 }
1313
1314 uint32_t palette[16];
1315 134 const uint32_t *palette_ptr = NULL;
1316 134 int palette_len = 0;
1317
1318
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 111 times.
134 if (arg_dec.attr_palette.is_valid) {
1319
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (src_depth == 0 ||
1320
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 1 time.
23 !decode_palette_list(arg_dec.attr_palette.args[0], palette, 16, &palette_len) ||
1321
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12 times.
22 palette_len != src_depth) {
1322 11 lbm_set_error_reason("img-blit: palette must be a list with exactly as many entries as the source image's color space.");
1323 11 return ENC_SYM_EERROR;
1324 }
1325 12 palette_ptr = palette;
1326
6/6
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 70 times.
✓ Branch 5 taken 4 times.
111 } else if (src_depth != 0 && dest_depth != 0 && dest_depth >= src_depth) {
1327 // Indexed source into an indexed dest with at least as many colors:
1328 // source index i is a valid dest index unchanged, so the
1329 // getpixel/putpixel fallback below (no compose) is already an
1330 // identity mapping and needs no palette.
1331
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
41 } else if (src_depth != 0 && arg_dec.img.fmt != dest_buf.fmt) {
1332 7 lbm_set_error_reason("img-blit: needs a palette attribute here.");
1333 7 return ENC_SYM_EERROR;
1334 }
1335
1336 116 uint8_t alpha = 255;
1337
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 103 times.
116 if (arg_dec.attr_alpha.is_valid) {
1338 13 int32_t a = lbm_dec_as_i32(arg_dec.attr_alpha.args[0]);
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (a < 0) a = 0;
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (a > 255) a = 255;
1341 13 alpha = (uint8_t)a;
1342 }
1343
1344 image_buffer_t alpha_buf_img;
1345 116 image_buffer_t *alpha_buf_ptr = NULL;
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (arg_dec.attr_alpha_buffer.is_valid) {
1347 ✗ lbm_array_header_t *abuf_arr = get_image_buffer(arg_dec.attr_alpha_buffer.args[0]);
1348 ✗ color_format_t abuf_fmt = abuf_arr ? image_buffer_format((uint8_t*)abuf_arr->data) : format_not_supported;
1349 ✗ uint16_t abuf_w = abuf_arr ? image_buffer_width((uint8_t*)abuf_arr->data) : 0;
1350 ✗ uint16_t abuf_h = abuf_arr ? image_buffer_height((uint8_t*)abuf_arr->data) : 0;
1351 ✗ if (!abuf_arr ||
1352 ✗ (abuf_fmt != indexed2 && abuf_fmt != indexed4 && abuf_fmt != indexed16 && abuf_fmt != rgb332) ||
1353 ✗ abuf_w != arg_dec.img.width || abuf_h != arg_dec.img.height) {
1354 ✗ lbm_set_error_reason("img-blit: alpha-buffer must be an indexed2/4/16 or rgb332 image buffer with the same dimensions as the source.");
1355 ✗ return ENC_SYM_EERROR;
1356 }
1357 ✗ alpha_buf_img.width = abuf_w;
1358 ✗ alpha_buf_img.height = abuf_h;
1359 ✗ alpha_buf_img.fmt = abuf_fmt;
1360 ✗ alpha_buf_img.mem_base = (uint8_t*)abuf_arr->data;
1361 ✗ alpha_buf_img.data = image_buffer_data((uint8_t*)abuf_arr->data);
1362 ✗ alpha_buf_ptr = &alpha_buf_img;
1363 }
1364
1365 116 blit_compose_t compose = {
1366 .palette = palette_ptr,
1367 .palette_len = palette_len,
1368 .alpha = alpha,
1369 .alpha_buf = alpha_buf_ptr,
1370 };
1371 116 const blit_compose_t *compose_ptr =
1372
5/6
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 91 times.
116 (palette_ptr || alpha != 255 || alpha_buf_ptr) ? &compose : NULL;
1373
1374
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 38 times.
116 if (!arg_dec.transform_attr_present) {
1375 78 tinygfx_blit(&dest_buf, &arg_dec.img, dest_x, dest_y, transparent_color, compose_ptr);
1376 } else {
1377 22 blit_transform_t transform = {
1378 38 .rot_x = lbm_dec_as_float(arg_dec.attr_rotate.args[0]),
1379 38 .rot_y = lbm_dec_as_float(arg_dec.attr_rotate.args[1]),
1380 38 .rot_angle = lbm_dec_as_float(arg_dec.attr_rotate.args[2]),
1381
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 22 times.
38 .scale = arg_dec.attr_scale.is_valid ? lbm_dec_as_float(arg_dec.attr_scale.args[0]) : 1.0f,
1382 38 .tile = arg_dec.attr_tile.is_valid,
1383
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 29 times.
38 .clip_x = arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[0]) : 0,
1384
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 29 times.
38 .clip_y = arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[1]) : 0,
1385
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 29 times.
38 .clip_w = arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[2]) : dest_buf.width,
1386
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 29 times.
38 .clip_h = arg_dec.attr_clip.is_valid ? lbm_dec_as_i32(arg_dec.attr_clip.args[3]) : dest_buf.height,
1387 };
1388
1389 38 tinygfx_blit_transform(&dest_buf, &arg_dec.img, dest_x, dest_y, transform, transparent_color, compose_ptr);
1390 }
1391 116 res = ENC_SYM_TRUE;
1392 }
1393 125 return res;
1394 }
1395
1396 ✗ void display_dummy_reset(void) {
1397 ✗ return;
1398 }
1399
1400 ✗ void display_dummy_clear(uint32_t color) {
1401 (void) color;
1402 ✗ return;
1403 }
1404
1405 ✗ bool display_dummy_render_image(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) {
1406 (void) img;
1407 (void) x;
1408 (void) y;
1409 (void) colors;
1410 ✗ return false;
1411 }
1412
1413 static bool(* volatile disp_render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) = display_dummy_render_image;
1414 static void(* volatile disp_clear)(uint32_t color) = display_dummy_clear;
1415 static void(* volatile disp_reset)(void) = display_dummy_reset;
1416
1417 static char *msg_not_supported = "Command not supported or display driver not initialized";
1418
1419 1 static lbm_value ext_disp_reset(lbm_value *args, lbm_uint argn) {
1420 (void) args;
1421 (void) argn;
1422
1423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (disp_reset == NULL) {
1424 ✗ lbm_set_error_reason(msg_not_supported);
1425 ✗ return ENC_SYM_EERROR;
1426 }
1427
1428 1 disp_reset();
1429
1430 1 return ENC_SYM_TRUE;
1431 }
1432
1433 1 static lbm_value ext_disp_clear(lbm_value *args, lbm_uint argn) {
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (disp_clear == NULL) {
1435 ✗ lbm_set_error_reason(msg_not_supported);
1436 ✗ return ENC_SYM_EERROR;
1437 }
1438
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (argn > 1) {
1440 ✗ return ENC_SYM_TERROR;
1441 }
1442
1443 1 uint32_t clear_color = 0;
1444
1445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (argn == 1) {
1446 ✗ if (!lbm_is_number(args[0])) {
1447 ✗ return ENC_SYM_TERROR;
1448 }
1449
1450 ✗ clear_color = lbm_dec_as_u32(args[0]);
1451 }
1452
1453 1 disp_clear(clear_color);
1454
1455 1 return ENC_SYM_TRUE;
1456 }
1457
1458 120 bool lbm_display_decode_color_list(lbm_value color_list, color_t *colors, int max_colors) {
1459 120 memset(colors, 0, sizeof(color_t) * (size_t)max_colors);
1460
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 if (!lbm_is_list(color_list)) {
1461 ✗ return true;
1462 }
1463 120 int i = 0;
1464 120 lbm_value curr = color_list;
1465
3/4
✓ Branch 1 taken 702 times.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 702 times.
✗ Branch 4 not taken.
822 while (lbm_is_cons(curr) && i < max_colors) {
1466 702 lbm_value arg = lbm_car(curr);
1467 color_t *color;
1468
2/2
✓ Branch 1 taken 686 times.
✓ Branch 2 taken 16 times.
702 if (lbm_is_number(arg)) {
1469 686 colors[i].color1 = (int)lbm_dec_as_u32(arg);
1470
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 } else if ((color = get_color(arg))) { // color assignment
1471 16 colors[i] = *color;
1472 } else {
1473 ✗ return false;
1474 }
1475
1476 702 curr = lbm_cdr(curr);
1477 702 i++;
1478 }
1479 120 return true;
1480 }
1481
1482 75 static lbm_value ext_disp_render(lbm_value *args, lbm_uint argn) {
1483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (disp_render_image == NULL) {
1484 ✗ lbm_set_error_reason(msg_not_supported);
1485 ✗ return ENC_SYM_EERROR;
1486 }
1487
1488 75 lbm_value res = ENC_SYM_TERROR;
1489 lbm_array_header_t *arr;
1490
4/6
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
150 if ((argn == 3 || argn == 4) &&
1491
1/2
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
150 (arr = get_image_buffer(args[0])) &&
1492
1/2
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
150 lbm_is_number(args[1]) &&
1493 75 lbm_is_number(args[2])) {
1494 image_buffer_t img_buf;
1495 75 img_buf.fmt = image_buffer_format((uint8_t*)arr->data);
1496 75 img_buf.width = image_buffer_width((uint8_t*)arr->data);
1497 75 img_buf.height = image_buffer_height((uint8_t*)arr->data);
1498 75 img_buf.mem_base = (uint8_t*)arr->data;
1499 75 img_buf.data = image_buffer_data((uint8_t*)arr->data);
1500
1501 color_t colors[16];
1502
3/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
75 if (argn == 4 && !lbm_display_decode_color_list(args[3], colors, 16)) {
1503 ✗ return ENC_SYM_TERROR;
1504
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 61 times.
75 } else if (argn == 3) {
1505 14 memset(colors, 0, sizeof(color_t) * 16);
1506 }
1507
1508 // img_buf is a stack allocated image_buffer_t.
1509 75 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);
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (!render_res) {
1511 ✗ lbm_set_error_reason("Could not render image. Check if the format and location is compatible with the display.");
1512 ✗ return ENC_SYM_EERROR;
1513 }
1514 75 res = ENC_SYM_TRUE;
1515 }
1516 75 return res;
1517 }
1518
1519 // Jpg decoder
1520
1521 3060 int jpg_output_func ( /* 1:Ok, 0:Aborted */
1522 JDEC* jd, /* Decompression object */
1523 void* bitmap, /* Bitmap data to be output */
1524 JRECT* rect /* Rectangular region to output */
1525 ) {
1526 3060 tinygfx_jpg_io_t *dev = (tinygfx_jpg_io_t*)jd->device;
1527
1528 image_buffer_t img;
1529 3060 img.mem_base = (uint8_t*)bitmap;
1530 3060 img.data = (uint8_t*)bitmap;
1531 3060 img.width = (uint16_t)(rect->right - rect->left + 1);
1532 3060 img.height = (uint16_t)(rect->bottom - rect->top + 1);
1533 3060 img.fmt = rgb888;
1534
1535 3060 disp_render_image(&img, (uint16_t)(rect->left + dev->ofs_x), (uint16_t)(rect->top + dev->ofs_y), 0);
1536
1537 3060 return 1;
1538 }
1539
1540 2 static lbm_value ext_disp_render_jpg(lbm_value *args, lbm_uint argn) {
1541
1542 lbm_array_header_t *array;
1543 2 lbm_value res = ENC_SYM_TERROR;
1544
1545
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 if (argn == 3 &&
1546
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 (array = lbm_dec_array_r(args[0])) && //asignment
1547
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 lbm_is_number(args[1]) &&
1548 2 lbm_is_number(args[2])) {
1549
1550 JDEC jd;
1551 void *jdwork;
1552 // make a bit of room before the buffer.
1553 2 const size_t sz_work = 4096 + IMAGE_BUFFER_HEADER_SIZE;
1554
1555 2 jdwork = lbm_malloc(sz_work);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!jdwork) {
1557 ✗ return ENC_SYM_MERROR;
1558 }
1559
1560
1561
1562 tinygfx_jpg_io_t iodev;
1563 2 iodev.data = (uint8_t*)(array->data);
1564 2 iodev.size = array->size;
1565 2 iodev.pos = 0;
1566 2 iodev.dest = NULL;
1567 2 iodev.ofs_x = lbm_dec_as_i32(args[1]);
1568 2 iodev.ofs_y = lbm_dec_as_i32(args[2]);
1569 2 jd_prepare(&jd, tinygfx_jpg_input, jdwork, sz_work, &iodev);
1570 2 jd_decomp(&jd, jpg_output_func, 0);
1571 2 lbm_free(jdwork);
1572 2 res = ENC_SYM_TRUE;
1573 }
1574 2 return res;
1575 }
1576
1577 4 static lbm_value ext_img_render_jpg(lbm_value *args, lbm_uint argn) {
1578
1579 lbm_array_header_t *dest_arr;
1580 lbm_array_header_t *jpg_arr;
1581 4 lbm_value res = ENC_SYM_TERROR;
1582
1583
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 time.
8 if (argn == 4 &&
1584
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
7 (dest_arr = get_image_buffer(args[0])) && //assignment
1585
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 (jpg_arr = lbm_dec_array_r(args[1])) && //assignment
1586
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 lbm_is_number(args[2]) &&
1587 3 lbm_is_number(args[3])) {
1588
1589 image_buffer_t dest_buf;
1590 3 dest_buf.width = image_buffer_width((uint8_t*)dest_arr->data);
1591 3 dest_buf.height = image_buffer_height((uint8_t*)dest_arr->data);
1592 3 dest_buf.fmt = image_buffer_format((uint8_t*)dest_arr->data);
1593 3 dest_buf.mem_base = (uint8_t*)dest_arr->data;
1594 3 dest_buf.data = image_buffer_data((uint8_t*)dest_arr->data);
1595
1596 3 const size_t sz_work = 4096;
1597 3 void *jdwork = lbm_malloc(sz_work);
1598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!jdwork) {
1599 ✗ return ENC_SYM_MERROR;
1600 }
1601
1602 9 bool ok = tinygfx_decode_jpg(&dest_buf,
1603 3 (uint8_t*)jpg_arr->data, jpg_arr->size,
1604 3 lbm_dec_as_i32(args[2]), lbm_dec_as_i32(args[3]),
1605 jdwork, sz_work);
1606 3 lbm_free(jdwork);
1607
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 res = ok ? ENC_SYM_TRUE : ENC_SYM_NIL;
1608 }
1609 4 return res;
1610 }
1611
1612 818 void lbm_display_extensions_init(void) {
1613 818 register_symbols();
1614
1615 818 disp_render_image = NULL;
1616 818 disp_clear = NULL;
1617 818 disp_reset = NULL;
1618
1619 818 lbm_add_extension("img-buffer", ext_image_buffer);
1620 818 lbm_add_extension("img-buffer?", ext_is_image_buffer);
1621 818 lbm_add_extension("img-color", ext_color);
1622 818 lbm_add_extension("img-color-set", ext_color_set);
1623 818 lbm_add_extension("img-color-get", ext_color_get);
1624 818 lbm_add_extension("img-color-setpre", ext_color_setpre);
1625 818 lbm_add_extension("img-color-getpre", ext_color_getpre);
1626 818 lbm_add_extension("img-dims", ext_image_dims);
1627 818 lbm_add_extension("img-setpix", ext_putpixel);
1628 818 lbm_add_extension("img-getpix", ext_getpixel);
1629 818 lbm_add_extension("img-line", ext_line);
1630 818 lbm_add_extension("img-text", ext_text);
1631 818 lbm_add_extension("img-clear", ext_clear);
1632 818 lbm_add_extension("img-circle", ext_circle);
1633 818 lbm_add_extension("img-arc", ext_arc);
1634 818 lbm_add_extension("img-circle-sector", ext_circle_sector);
1635 818 lbm_add_extension("img-circle-segment", ext_circle_segment);
1636 818 lbm_add_extension("img-rectangle", ext_rectangle);
1637 818 lbm_add_extension("img-triangle", ext_triangle);
1638 818 lbm_add_extension("img-blit", ext_blit);
1639
1640 818 lbm_add_extension("disp-reset", ext_disp_reset);
1641 818 lbm_add_extension("disp-clear", ext_disp_clear);
1642 818 lbm_add_extension("disp-render", ext_disp_render);
1643 818 lbm_add_extension("disp-render-jpg", ext_disp_render_jpg);
1644 818 lbm_add_extension("img-render-jpg", ext_img_render_jpg);
1645 818 }
1646
1647 91 void lbm_display_extensions_set_callbacks(
1648 bool(* volatile render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors),
1649 void(* volatile clear)(uint32_t color),
1650 void(* volatile reset)(void)
1651 ) {
1652
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 disp_render_image = render_image ? render_image : display_dummy_render_image;
1653
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 disp_clear = clear ? clear : display_dummy_clear;
1654
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 disp_reset = reset ? reset : display_dummy_reset;
1655 91 }
1656