| 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 0 taken 24 times.
✗ Branch 1 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 0 taken 24 times.
✗ Branch 1 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 | 145 | color_format_t sym_to_color_format(lbm_value v) { | |
| 101 | 145 | lbm_uint s = lbm_dec_sym(v); | |
| 102 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 110 times.
|
145 | if (s == symbol_indexed2) return indexed2; |
| 103 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 70 times.
|
110 | if (s == symbol_indexed4) return indexed4; |
| 104 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 50 times.
|
70 | 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 | 124 | static lbm_value image_buffer_lift(uint8_t *buf, color_format_t fmt, uint16_t width, uint16_t height) { | |
| 116 | 124 | lbm_value res = ENC_SYM_MERROR; | |
| 117 | 124 | lbm_uint size = image_dims_to_size_bytes(fmt, width, height); | |
| 118 |
1/2✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
|
124 | if ( lbm_lift_array(&res, (char*)buf, IMAGE_BUFFER_HEADER_SIZE + size)) { |
| 119 | 124 | buf[0] = (uint8_t)(width >> 8); | |
| 120 | 124 | buf[1] = (uint8_t)width; | |
| 121 | 124 | buf[2] = (uint8_t)(height >> 8); | |
| 122 | 124 | buf[3] = (uint8_t)height; | |
| 123 | 124 | buf[4] = color_format_to_byte(fmt); | |
| 124 | } | ||
| 125 | 124 | 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 0 taken 79 times.
✗ Branch 1 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 times.
✓ 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 0 taken 28 times.
✗ Branch 1 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 times.
|
5 | if (type == COLOR_PRE_X) { |
| 174 | 4 | color->type = COLOR_GRADIENT_X; | |
| 175 |
1/2✓ Branch 0 taken 1 times.
✗ 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 times.
✓ 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 | 124 | static lbm_value image_buffer_allocate(color_format_t fmt, uint16_t width, uint16_t height) { | |
| 198 | 124 | uint32_t size_bytes = image_dims_to_size_bytes(fmt, width, height); | |
| 199 | |||
| 200 | 124 | uint8_t *buf = lbm_malloc(IMAGE_BUFFER_HEADER_SIZE + size_bytes); | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
|
124 | if (!buf) { |
| 202 | ✗ | return ENC_SYM_MERROR; | |
| 203 | } | ||
| 204 | 124 | memset(buf, 0, size_bytes + IMAGE_BUFFER_HEADER_SIZE); | |
| 205 | 124 | lbm_value res = image_buffer_lift(buf, fmt, width, height); | |
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
|
124 | if (lbm_is_symbol(res)) { /* something is wrong, free */ |
| 207 | ✗ | lbm_free(buf); | |
| 208 | } | ||
| 209 | 124 | 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 times.
✗ 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 times.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 25 times.
|
105 | if (array && is_color_sized(array->size) |
| 242 |
1/2✓ Branch 0 taken 79 times.
✗ Branch 1 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 | 804 | static bool register_symbols(void) { | |
| 252 | 804 | bool res = true; | |
| 253 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("indexed2", &symbol_indexed2); |
| 254 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("indexed4", &symbol_indexed4); |
| 255 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("indexed16", &symbol_indexed16); |
| 256 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("rgb332", &symbol_rgb332); |
| 257 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("rgb565", &symbol_rgb565); |
| 258 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("rgb888", &symbol_rgb888); |
| 259 | |||
| 260 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("thickness", &symbol_thickness); |
| 261 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("filled", &symbol_filled); |
| 262 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("rounded", &symbol_rounded); |
| 263 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("dotted", &symbol_dotted); |
| 264 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("scale", &symbol_scale); |
| 265 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("rotate", &symbol_rotate); |
| 266 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("resolution", &symbol_resolution); |
| 267 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("tile", &symbol_tile); |
| 268 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("clip", &symbol_clip); |
| 269 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("palette", &symbol_palette); |
| 270 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("alpha-buffer", &symbol_alpha_buffer); |
| 271 | |||
| 272 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("regular", &symbol_regular); |
| 273 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("gradient_x", &symbol_gradient_x); |
| 274 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("gradient_y", &symbol_gradient_y); |
| 275 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("gradient_x_pre", &symbol_gradient_x_pre); |
| 276 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("gradient_y_pre", &symbol_gradient_y_pre); |
| 277 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("mirrored", &symbol_mirrored); |
| 278 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("repeat", &symbol_repeat); |
| 279 | |||
| 280 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("color-0", &symbol_color_0); |
| 281 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("color-1", &symbol_color_1); |
| 282 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("width", &symbol_width); |
| 283 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("offset", &symbol_offset); |
| 284 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("repeat-type", &symbol_repeat_type); |
| 285 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("alpha", &symbol_alpha); |
| 286 | |||
| 287 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("down", &symbol_down); |
| 288 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("up", &symbol_up); |
| 289 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("magnify", &symbol_magnify); |
| 290 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("spacing", &symbol_spacing); |
| 291 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("align", &symbol_align); |
| 292 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("left", &symbol_left); |
| 293 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("center", &symbol_center); |
| 294 |
2/4✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
✗ Branch 3 not taken.
|
804 | res = res && lbm_add_symbol_const("right", &symbol_right); |
| 295 | |||
| 296 | 804 | 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 0 taken 27 times.
✓ Branch 1 taken 151 times.
✗ Branch 2 not taken.
✓ Branch 3 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 0 taken 151 times.
✓ Branch 1 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 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 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 0 not taken.
✓ Branch 1 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 0 not taken.
✓ Branch 1 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 0 not taken.
✓ Branch 1 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 0 not taken.
✓ Branch 1 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 0 taken 4 times.
✗ Branch 1 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 times.
✓ Branch 1 taken 1 times.
|
2 | if (av == symbol_center) { *align = 1; return 1; } |
| 353 |
1/2✓ Branch 0 taken 1 times.
✗ 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 0 not taken.
✓ Branch 1 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 | 1321 | static img_args_t decode_args(lbm_value *args, lbm_uint argn, int num_expected) { | |
| 406 | img_args_t res; | ||
| 407 | 1321 | memset(&res, 0, sizeof(res)); | |
| 408 | 1321 | res.is_valid = false; | |
| 409 | |||
| 410 | lbm_array_header_t *arr; | ||
| 411 |
3/4✓ Branch 0 taken 1321 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1284 times.
✓ Branch 3 taken 37 times.
|
1321 | if (argn >= 1 && (arr = get_image_buffer(args[0]))) { |
| 412 | // at least one argument which is an image buffer. | ||
| 413 | 1284 | res.img.width = image_buffer_width((uint8_t*)arr->data); | |
| 414 | 1284 | res.img.height = image_buffer_height((uint8_t*)arr->data); | |
| 415 | 1284 | res.img.fmt = image_buffer_format((uint8_t*)arr->data); | |
| 416 | 1284 | res.img.mem_base = (uint8_t*)arr->data; | |
| 417 | 1284 | res.img.data = image_buffer_data((uint8_t*)arr->data); | |
| 418 | |||
| 419 | |||
| 420 | 1284 | int num_dec = 0; | |
| 421 |
2/2✓ Branch 0 taken 6255 times.
✓ Branch 1 taken 1258 times.
|
7513 | for (unsigned int i = 1;i < argn;i++) { |
| 422 |
4/4✓ Branch 0 taken 768 times.
✓ Branch 1 taken 5487 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 748 times.
|
6255 | 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 0 taken 5487 times.
✓ Branch 1 taken 748 times.
|
6235 | if (lbm_is_number(args[i])) { |
| 435 | 5487 | res.args[num_dec] = args[i]; | |
| 436 | 5487 | num_dec++; | |
| 437 | |||
| 438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5487 times.
|
5487 | if (num_dec >= ARG_MAX_NUM) { |
| 439 | ✗ | return res; | |
| 440 | } | ||
| 441 | } else { | ||
| 442 | 748 | lbm_value curr = args[i]; | |
| 443 | 748 | int attr_ind = 0; | |
| 444 | 748 | attr_t *attr_now = 0; | |
| 445 |
2/2✓ Branch 0 taken 1341 times.
✓ Branch 1 taken 746 times.
|
2087 | while (lbm_is_cons(curr)) { |
| 446 | 1341 | lbm_value arg = lbm_car(curr); | |
| 447 | |||
| 448 |
2/2✓ Branch 0 taken 748 times.
✓ Branch 1 taken 593 times.
|
1341 | if (attr_ind == 0) { |
| 449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 748 times.
|
748 | if (!lbm_is_symbol(arg)) { |
| 450 | ✗ | return res; | |
| 451 | } | ||
| 452 | |||
| 453 |
2/2✓ Branch 0 taken 386 times.
✓ Branch 1 taken 362 times.
|
748 | 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 0 taken 149 times.
✓ Branch 1 taken 213 times.
|
362 | } else if (lbm_dec_sym(arg) == symbol_filled) { |
| 457 | 149 | attr_now = &res.attr_filled; | |
| 458 | 149 | attr_now->arg_num = 0; | |
| 459 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 115 times.
|
213 | } 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 0 taken 28 times.
✓ Branch 1 taken 87 times.
|
115 | } 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 0 taken 17 times.
✓ Branch 1 taken 70 times.
|
87 | } 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 0 taken 11 times.
✓ Branch 1 taken 59 times.
|
70 | } 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 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | } else if (lbm_dec_sym(arg) == symbol_resolution) { |
| 474 | ✗ | attr_now = &res.attr_resolution; | |
| 475 | ✗ | attr_now->arg_num = 1; | |
| 476 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 46 times.
|
59 | } 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 0 taken 9 times.
✓ Branch 1 taken 37 times.
|
46 | } 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 0 taken 23 times.
✓ Branch 1 taken 14 times.
|
37 | } else if (lbm_dec_sym(arg) == symbol_palette) { |
| 485 | 23 | attr_now = &res.attr_palette; | |
| 486 | 23 | attr_now->arg_num = 1; | |
| 487 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
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 0 not taken.
✓ Branch 1 taken 1 times.
|
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 23 times.
✓ Branch 1 taken 570 times.
|
593 | if (attr_now == &res.attr_palette) { |
| 499 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | 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 0 taken 1 times.
✓ Branch 1 taken 569 times.
|
570 | } else if (!lbm_is_number(arg)) { |
| 507 | 1 | return res; | |
| 508 | } | ||
| 509 | |||
| 510 | 592 | attr_now->args[attr_ind - 1] = arg; | |
| 511 | } | ||
| 512 | |||
| 513 | 1339 | attr_ind++; | |
| 514 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1339 times.
|
1339 | if (attr_ind > (ATTR_MAX_ARGS + 1)) { |
| 515 | ✗ | return res; | |
| 516 | } | ||
| 517 | |||
| 518 | 1339 | 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 648 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 33 times.
|
746 | 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 742 times.
✓ Branch 1 taken 4 times.
|
746 | if ((attr_ind - 1) == attr_now->arg_num) { |
| 528 | 742 | attr_now->is_valid = true; | |
| 529 | } else { | ||
| 530 | 4 | return res; | |
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1258 times.
|
1258 | if (num_dec != num_expected) { |
| 535 | ✗ | return res; | |
| 536 | } | ||
| 537 | // I think this should go here ??? | ||
| 538 | 1258 | res.is_valid = true; | |
| 539 | } | ||
| 540 | 1295 | 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 0 not taken.
✓ Branch 1 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 | 130 | static lbm_value ext_image_buffer(lbm_value *args, lbm_uint argn) { | |
| 562 | 130 | lbm_value res = ENC_SYM_TERROR; | |
| 563 | 130 | bool args_ok = false; | |
| 564 | 130 | color_format_t fmt = indexed2; | |
| 565 | 130 | lbm_uint w = 0; | |
| 566 | 130 | lbm_uint h = 0; | |
| 567 | |||
| 568 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 129 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
131 | if (argn == 4 && |
| 569 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
2 | lbm_is_defrag_mem(args[0]) && |
| 570 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
2 | lbm_is_symbol(args[1]) && |
| 571 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 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 129 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
|
258 | } else if (argn == 3 && |
| 578 |
1/2✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
|
258 | lbm_is_symbol(args[0]) && |
| 579 |
1/2✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
|
258 | lbm_is_number(args[1]) && |
| 580 | 129 | lbm_is_number(args[2])) { | |
| 581 | 129 | fmt = sym_to_color_format(args[0]); | |
| 582 | 129 | w = lbm_dec_as_u32(args[1]); | |
| 583 | 129 | h = lbm_dec_as_u32(args[2]); | |
| 584 | 129 | args_ok = true; | |
| 585 | } | ||
| 586 | |||
| 587 |
9/12✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 126 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 125 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 125 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 125 times.
✗ Branch 11 not taken.
|
130 | if (args_ok && fmt != format_not_supported && w > 0 && h > 0 && w < MAX_WIDTH && h < MAX_HEIGHT) { |
| 588 |
2/2✓ Branch 0 taken 124 times.
✓ Branch 1 taken 1 times.
|
125 | if (argn == 3) { |
| 589 | 124 | 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 | 130 | 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 times.
|
26 | if (array) { |
| 605 | 25 | uint8_t *data = (uint8_t*)array->data; | |
| 606 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 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 times.
|
60 | if (argn >= 2 && argn <= 7 && |
| 618 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 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 0 taken 14 times.
✗ Branch 1 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 0 taken 14 times.
✗ Branch 1 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 0 taken 14 times.
✗ Branch 1 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 0 taken 10 times.
✗ Branch 1 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 0 taken 14 times.
✓ Branch 1 taken 15 times.
|
29 | if (lbm_dec_sym(args[0]) == symbol_regular) { |
| 675 | 14 | t = COLOR_REGULAR; | |
| 676 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
|
15 | } else if (lbm_dec_sym(args[0]) == symbol_gradient_x) { |
| 677 | 6 | t = COLOR_GRADIENT_X; | |
| 678 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | } else if (lbm_dec_sym(args[0]) == symbol_gradient_y) { |
| 679 | 3 | t = COLOR_GRADIENT_Y; | |
| 680 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 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 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
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 2 taken 22 times.
✓ Branch 3 taken 3 times.
|
25 | if (argn != 3 || !(color = get_color(args[0])) || // color assignment |
| 714 |
1/2✗ Branch 0 not taken.
✓ Branch 1 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 times.
✓ 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 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
6 | if (!lbm_is_number(args[2]) || !(is_regular || is_gradient)) { |
| 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 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
|
3 | if (!lbm_is_number(args[2]) || !is_gradient) { |
| 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 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
5 | if (!lbm_is_number(args[2]) || !is_gradient) { |
| 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 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
3 | if (!lbm_is_number(args[2]) || !(is_gradient || is_pre)) { |
| 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 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
3 | if (!lbm_is_symbol(args[2]) || !(is_gradient || is_pre)) { |
| 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 times.
|
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 2 taken 22 times.
✓ Branch 3 taken 1 times.
|
23 | if (argn != 2 || !(color = get_color(args[0])) || // color assignment |
| 773 |
1/2✗ Branch 0 not taken.
✓ Branch 1 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 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✗ 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 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✗ 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 times.
|
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 times.
|
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 times.
|
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 2 taken 6 times.
✓ Branch 3 taken 1 times.
|
7 | if (argn != 3 || !(color = get_color(args[0])) || |
| 816 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 4 times.
|
6 | !lbm_is_number(args[1]) || !lbm_is_number(args[2])) { |
| 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 times.
✓ 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 2 taken 13 times.
✓ Branch 3 taken 1 times.
|
14 | if (argn != 2 || !(color = get_color(args[0])) || |
| 835 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 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 times.
✓ 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 | 102 | static lbm_value ext_clear(lbm_value *args, lbm_uint argn) { | |
| 848 | |||
| 849 | 102 | lbm_value res = ENC_SYM_TERROR; | |
| 850 | lbm_array_header_t *arr; | ||
| 851 |
5/6✓ Branch 0 taken 95 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 95 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 99 times.
✓ Branch 5 taken 3 times.
|
204 | if ((argn == 1 || argn == 2) && |
| 852 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 7 times.
|
201 | (arr = get_image_buffer(args[0])) && // assignment |
| 853 |
1/2✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
|
92 | (argn != 2 || lbm_is_number(args[1]))) { // ( argn == 2 -> lbm_is_number(args[1])) |
| 854 | image_buffer_t img_buf; | ||
| 855 | 99 | img_buf.width = image_buffer_width((uint8_t*)arr->data); | |
| 856 | 99 | img_buf.height = image_buffer_height((uint8_t*)arr->data); | |
| 857 | 99 | img_buf.fmt = image_buffer_format((uint8_t*)arr->data); | |
| 858 | 99 | img_buf.mem_base = (uint8_t*)arr->data; | |
| 859 | 99 | img_buf.data = image_buffer_data((uint8_t*)arr->data); | |
| 860 | |||
| 861 | 99 | uint32_t color = 0; | |
| 862 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 7 times.
|
99 | if (argn == 2) { |
| 863 | 92 | color = lbm_dec_as_u32(args[1]); | |
| 864 | } | ||
| 865 | |||
| 866 | 99 | image_buffer_clear(&img_buf, color); | |
| 867 | 99 | res = ENC_SYM_TRUE; | |
| 868 | } | ||
| 869 | 102 | 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 | 214 | static lbm_value ext_getpixel(lbm_value *args, lbm_uint argn) { | |
| 887 | 214 | img_args_t arg_dec = decode_args(args, argn, 2); | |
| 888 | |||
| 889 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 190 times.
|
214 | if (!arg_dec.is_valid) { |
| 890 | 24 | return ENC_SYM_TERROR; | |
| 891 | } | ||
| 892 | |||
| 893 | 190 | 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 | 190 | 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 | 95 | static lbm_value ext_circle(lbm_value *args, lbm_uint argn) { | |
| 940 | 95 | img_args_t arg_dec = decode_args(args, argn, 4); | |
| 941 | |||
| 942 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 92 times.
|
95 | if (!arg_dec.is_valid) { |
| 943 | 3 | return ENC_SYM_TERROR; | |
| 944 | } | ||
| 945 | |||
| 946 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 63 times.
|
92 | if (arg_dec.attr_filled.is_valid) { |
| 947 | 29 | 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 times.
✓ 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 | 92 | 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 times.
✓ 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 | 121 | static lbm_value ext_rectangle(lbm_value *args, lbm_uint argn) { | |
| 1037 | 121 | img_args_t arg_dec = decode_args(args, argn, 5); | |
| 1038 | |||
| 1039 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 118 times.
|
121 | if (!arg_dec.is_valid) { |
| 1040 | 3 | return ENC_SYM_TERROR; | |
| 1041 | } | ||
| 1042 | |||
| 1043 | 118 | image_buffer_t *img = &arg_dec.img; | |
| 1044 | 118 | int x = lbm_dec_as_i32(arg_dec.args[0]); | |
| 1045 | 118 | int y = lbm_dec_as_i32(arg_dec.args[1]); | |
| 1046 | 118 | int width = lbm_dec_as_i32(arg_dec.args[2]); | |
| 1047 | 118 | int height = lbm_dec_as_i32(arg_dec.args[3]); | |
| 1048 | 118 | int rad = lbm_dec_as_i32(arg_dec.attr_rounded.args[0]); | |
| 1049 | 118 | int thickness = lbm_dec_as_i32(arg_dec.attr_thickness.args[0]); | |
| 1050 | 118 | uint32_t color = lbm_dec_as_u32(arg_dec.args[4]); | |
| 1051 | 118 | int dot1 = lbm_dec_as_i32(arg_dec.attr_dotted.args[0]); | |
| 1052 | 118 | int dot2 = lbm_dec_as_i32(arg_dec.attr_dotted.args[1]); | |
| 1053 | 118 | int resolution = lbm_dec_as_i32(arg_dec.attr_resolution.args[0]); | |
| 1054 | |||
| 1055 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 84 times.
|
118 | 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 | 84 | tinygfx_rectangle(img, | |
| 1063 | x, y, | ||
| 1064 | width, height, | ||
| 1065 | 84 | arg_dec.attr_filled.is_valid, | |
| 1066 | thickness, | ||
| 1067 | dot1, dot2, | ||
| 1068 | color); | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | 118 | 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 times.
✓ 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 2 taken 30 times.
✓ Branch 3 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 0 taken 16 times.
✓ Branch 1 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 0 taken 47 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
|
63 | while (lbm_is_cons(curr) && ci < 4) { |
| 1173 | 47 | lbm_value a = lbm_car(curr); | |
| 1174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 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 0 taken 16 times.
✗ Branch 1 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 0 not taken.
✓ Branch 1 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 0 taken 132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 3 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 0 taken 92 times.
✓ Branch 1 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 0 taken 1 times.
✓ Branch 1 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 0 taken 5 times.
✗ Branch 1 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 times.
✓ 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 0 taken 6 times.
✓ Branch 1 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 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 1 times.
✓ 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 | 264 | static int indexed_depth(color_format_t fmt) { | |
| 1260 |
4/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 86 times.
|
264 | switch (fmt) { |
| 1261 | 44 | case indexed2: return 2; | |
| 1262 | 109 | case indexed4: return 4; | |
| 1263 | 25 | 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 | 22 | static bool decode_palette_list(lbm_value lst, uint32_t *out, int max_len, int *out_len) { | |
| 1272 | 22 | int i = 0; | |
| 1273 | 22 | lbm_value curr = lst; | |
| 1274 |
2/2✓ Branch 0 taken 124 times.
✓ Branch 1 taken 21 times.
|
145 | while (lbm_is_cons(curr)) { |
| 1275 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 123 times.
|
124 | if (i >= max_len) return false; |
| 1276 | 123 | lbm_value arg = lbm_car(curr); | |
| 1277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
|
123 | if (!lbm_is_number(arg)) return false; |
| 1278 | 123 | out[i] = lbm_dec_as_u32(arg); | |
| 1279 | 123 | i++; | |
| 1280 | 123 | curr = lbm_cdr(curr); | |
| 1281 | } | ||
| 1282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (curr != ENC_SYM_NIL) return false; |
| 1283 | 21 | *out_len = i; | |
| 1284 | 21 | return true; | |
| 1285 | } | ||
| 1286 | |||
| 1287 | // TODO: Think about way to simplify the blit dispatch | ||
| 1288 | // based on transform or not. | ||
| 1289 | 141 | static lbm_value ext_blit(lbm_value *args, lbm_uint argn) { | |
| 1290 | 141 | img_args_t arg_dec = decode_args(args + 1, argn - 1, 3); | |
| 1291 | |||
| 1292 | 141 | lbm_value res = ENC_SYM_TERROR; | |
| 1293 | lbm_array_header_t *arr; | ||
| 1294 |
4/4✓ Branch 0 taken 135 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 3 times.
|
141 | if (arg_dec.is_valid && (arr = get_image_buffer(args[0]))) { //assignment |
| 1295 | image_buffer_t dest_buf; | ||
| 1296 | 132 | dest_buf.width = image_buffer_width((uint8_t*)arr->data); | |
| 1297 | 132 | dest_buf.height = image_buffer_height((uint8_t*)arr->data); | |
| 1298 | 132 | dest_buf.fmt = image_buffer_format((uint8_t*)arr->data); | |
| 1299 | 132 | dest_buf.mem_base = (uint8_t*)arr->data; | |
| 1300 | 132 | dest_buf.data = image_buffer_data((uint8_t*)arr->data); | |
| 1301 | |||
| 1302 | 132 | int dest_x = lbm_dec_as_i32(arg_dec.args[0]); | |
| 1303 | 132 | int dest_y = lbm_dec_as_i32(arg_dec.args[1]); | |
| 1304 | 132 | int32_t transparent_color = lbm_dec_as_i32(arg_dec.args[2]); | |
| 1305 | |||
| 1306 | 132 | int src_depth = indexed_depth(arg_dec.img.fmt); | |
| 1307 | 132 | int dest_depth = indexed_depth(dest_buf.fmt); | |
| 1308 | |||
| 1309 |
4/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 34 times.
|
132 | 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 | 21 | return ENC_SYM_EERROR; | |
| 1312 | } | ||
| 1313 | |||
| 1314 | uint32_t palette[16]; | ||
| 1315 | 128 | const uint32_t *palette_ptr = NULL; | |
| 1316 | 128 | int palette_len = 0; | |
| 1317 | |||
| 1318 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 106 times.
|
128 | if (arg_dec.attr_palette.is_valid) { |
| 1319 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | if (src_depth == 0 || |
| 1320 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1 times.
|
22 | !decode_palette_list(arg_dec.attr_palette.args[0], palette, 16, &palette_len) || |
| 1321 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 11 times.
|
21 | 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 | 11 | palette_ptr = palette; | |
| 1326 |
4/4✓ Branch 0 taken 72 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 66 times.
|
106 | } else if (src_depth != 0 && arg_dec.img.fmt != dest_buf.fmt) { |
| 1327 | 6 | lbm_set_error_reason("img-blit: blitting between differing indexed formats, or from indexed into rgb, requires a palette attribute."); | |
| 1328 | 6 | return ENC_SYM_EERROR; | |
| 1329 | } | ||
| 1330 | |||
| 1331 | 111 | uint8_t alpha = 255; | |
| 1332 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 98 times.
|
111 | if (arg_dec.attr_alpha.is_valid) { |
| 1333 | 13 | int32_t a = lbm_dec_as_i32(arg_dec.attr_alpha.args[0]); | |
| 1334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (a < 0) a = 0; |
| 1335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (a > 255) a = 255; |
| 1336 | 13 | alpha = (uint8_t)a; | |
| 1337 | } | ||
| 1338 | |||
| 1339 | image_buffer_t alpha_buf_img; | ||
| 1340 | 111 | image_buffer_t *alpha_buf_ptr = NULL; | |
| 1341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 111 times.
|
111 | if (arg_dec.attr_alpha_buffer.is_valid) { |
| 1342 | ✗ | lbm_array_header_t *abuf_arr = get_image_buffer(arg_dec.attr_alpha_buffer.args[0]); | |
| 1343 | ✗ | color_format_t abuf_fmt = abuf_arr ? image_buffer_format((uint8_t*)abuf_arr->data) : format_not_supported; | |
| 1344 | ✗ | uint16_t abuf_w = abuf_arr ? image_buffer_width((uint8_t*)abuf_arr->data) : 0; | |
| 1345 | ✗ | uint16_t abuf_h = abuf_arr ? image_buffer_height((uint8_t*)abuf_arr->data) : 0; | |
| 1346 | ✗ | if (!abuf_arr || | |
| 1347 | ✗ | (abuf_fmt != indexed2 && abuf_fmt != indexed4 && abuf_fmt != indexed16 && abuf_fmt != rgb332) || | |
| 1348 | ✗ | abuf_w != arg_dec.img.width || abuf_h != arg_dec.img.height) { | |
| 1349 | ✗ | 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."); | |
| 1350 | ✗ | return ENC_SYM_EERROR; | |
| 1351 | } | ||
| 1352 | ✗ | alpha_buf_img.width = abuf_w; | |
| 1353 | ✗ | alpha_buf_img.height = abuf_h; | |
| 1354 | ✗ | alpha_buf_img.fmt = abuf_fmt; | |
| 1355 | ✗ | alpha_buf_img.mem_base = (uint8_t*)abuf_arr->data; | |
| 1356 | ✗ | alpha_buf_img.data = image_buffer_data((uint8_t*)abuf_arr->data); | |
| 1357 | ✗ | alpha_buf_ptr = &alpha_buf_img; | |
| 1358 | } | ||
| 1359 | |||
| 1360 | 111 | blit_compose_t compose = { | |
| 1361 | .palette = palette_ptr, | ||
| 1362 | .palette_len = palette_len, | ||
| 1363 | .alpha = alpha, | ||
| 1364 | .alpha_buf = alpha_buf_ptr, | ||
| 1365 | }; | ||
| 1366 | 111 | const blit_compose_t *compose_ptr = | |
| 1367 |
5/6✓ Branch 0 taken 100 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 87 times.
|
111 | (palette_ptr || alpha != 255 || alpha_buf_ptr) ? &compose : NULL; |
| 1368 | |||
| 1369 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 38 times.
|
111 | if (!arg_dec.transform_attr_present) { |
| 1370 | 73 | tinygfx_blit(&dest_buf, &arg_dec.img, dest_x, dest_y, transparent_color, compose_ptr); | |
| 1371 | } else { | ||
| 1372 | 22 | blit_transform_t transform = { | |
| 1373 | 38 | .rot_x = lbm_dec_as_float(arg_dec.attr_rotate.args[0]), | |
| 1374 | 38 | .rot_y = lbm_dec_as_float(arg_dec.attr_rotate.args[1]), | |
| 1375 | 38 | .rot_angle = lbm_dec_as_float(arg_dec.attr_rotate.args[2]), | |
| 1376 |
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, |
| 1377 | 38 | .tile = arg_dec.attr_tile.is_valid, | |
| 1378 |
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, |
| 1379 |
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, |
| 1380 |
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, |
| 1381 |
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, |
| 1382 | }; | ||
| 1383 | |||
| 1384 | 38 | tinygfx_blit_transform(&dest_buf, &arg_dec.img, dest_x, dest_y, transform, transparent_color, compose_ptr); | |
| 1385 | } | ||
| 1386 | 111 | res = ENC_SYM_TRUE; | |
| 1387 | } | ||
| 1388 | 120 | return res; | |
| 1389 | } | ||
| 1390 | |||
| 1391 | ✗ | void display_dummy_reset(void) { | |
| 1392 | ✗ | return; | |
| 1393 | } | ||
| 1394 | |||
| 1395 | ✗ | void display_dummy_clear(uint32_t color) { | |
| 1396 | (void) color; | ||
| 1397 | ✗ | return; | |
| 1398 | } | ||
| 1399 | |||
| 1400 | ✗ | bool display_dummy_render_image(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) { | |
| 1401 | (void) img; | ||
| 1402 | (void) x; | ||
| 1403 | (void) y; | ||
| 1404 | (void) colors; | ||
| 1405 | ✗ | return false; | |
| 1406 | } | ||
| 1407 | |||
| 1408 | static bool(* volatile disp_render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors) = display_dummy_render_image; | ||
| 1409 | static void(* volatile disp_clear)(uint32_t color) = display_dummy_clear; | ||
| 1410 | static void(* volatile disp_reset)(void) = display_dummy_reset; | ||
| 1411 | |||
| 1412 | static char *msg_not_supported = "Command not supported or display driver not initialized"; | ||
| 1413 | |||
| 1414 | 1 | static lbm_value ext_disp_reset(lbm_value *args, lbm_uint argn) { | |
| 1415 | (void) args; | ||
| 1416 | (void) argn; | ||
| 1417 | |||
| 1418 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (disp_reset == NULL) { |
| 1419 | ✗ | lbm_set_error_reason(msg_not_supported); | |
| 1420 | ✗ | return ENC_SYM_EERROR; | |
| 1421 | } | ||
| 1422 | |||
| 1423 | 1 | disp_reset(); | |
| 1424 | |||
| 1425 | 1 | return ENC_SYM_TRUE; | |
| 1426 | } | ||
| 1427 | |||
| 1428 | 1 | static lbm_value ext_disp_clear(lbm_value *args, lbm_uint argn) { | |
| 1429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (disp_clear == NULL) { |
| 1430 | ✗ | lbm_set_error_reason(msg_not_supported); | |
| 1431 | ✗ | return ENC_SYM_EERROR; | |
| 1432 | } | ||
| 1433 | |||
| 1434 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (argn > 1) { |
| 1435 | ✗ | return ENC_SYM_TERROR; | |
| 1436 | } | ||
| 1437 | |||
| 1438 | 1 | uint32_t clear_color = 0; | |
| 1439 | |||
| 1440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (argn == 1) { |
| 1441 | ✗ | if (!lbm_is_number(args[0])) { | |
| 1442 | ✗ | return ENC_SYM_TERROR; | |
| 1443 | } | ||
| 1444 | |||
| 1445 | ✗ | clear_color = lbm_dec_as_u32(args[0]); | |
| 1446 | } | ||
| 1447 | |||
| 1448 | 1 | disp_clear(clear_color); | |
| 1449 | |||
| 1450 | 1 | return ENC_SYM_TRUE; | |
| 1451 | } | ||
| 1452 | |||
| 1453 | 118 | bool lbm_display_decode_color_list(lbm_value color_list, color_t *colors, int max_colors) { | |
| 1454 | 118 | memset(colors, 0, sizeof(color_t) * (size_t)max_colors); | |
| 1455 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
|
118 | if (!lbm_is_list(color_list)) { |
| 1456 | ✗ | return true; | |
| 1457 | } | ||
| 1458 | 118 | int i = 0; | |
| 1459 | 118 | lbm_value curr = color_list; | |
| 1460 |
3/4✓ Branch 0 taken 694 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 694 times.
✗ Branch 3 not taken.
|
812 | while (lbm_is_cons(curr) && i < max_colors) { |
| 1461 | 694 | lbm_value arg = lbm_car(curr); | |
| 1462 | color_t *color; | ||
| 1463 |
2/2✓ Branch 0 taken 678 times.
✓ Branch 1 taken 16 times.
|
694 | if (lbm_is_number(arg)) { |
| 1464 | 678 | colors[i].color1 = (int)lbm_dec_as_u32(arg); | |
| 1465 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | } else if ((color = get_color(arg))) { // color assignment |
| 1466 | 16 | colors[i] = *color; | |
| 1467 | } else { | ||
| 1468 | ✗ | return false; | |
| 1469 | } | ||
| 1470 | |||
| 1471 | 694 | curr = lbm_cdr(curr); | |
| 1472 | 694 | i++; | |
| 1473 | } | ||
| 1474 | 118 | return true; | |
| 1475 | } | ||
| 1476 | |||
| 1477 | 74 | static lbm_value ext_disp_render(lbm_value *args, lbm_uint argn) { | |
| 1478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (disp_render_image == NULL) { |
| 1479 | ✗ | lbm_set_error_reason(msg_not_supported); | |
| 1480 | ✗ | return ENC_SYM_EERROR; | |
| 1481 | } | ||
| 1482 | |||
| 1483 | 74 | lbm_value res = ENC_SYM_TERROR; | |
| 1484 | lbm_array_header_t *arr; | ||
| 1485 |
4/6✓ Branch 0 taken 60 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
|
148 | if ((argn == 3 || argn == 4) && |
| 1486 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
148 | (arr = get_image_buffer(args[0])) && |
| 1487 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
148 | lbm_is_number(args[1]) && |
| 1488 | 74 | lbm_is_number(args[2])) { | |
| 1489 | image_buffer_t img_buf; | ||
| 1490 | 74 | img_buf.fmt = image_buffer_format((uint8_t*)arr->data); | |
| 1491 | 74 | img_buf.width = image_buffer_width((uint8_t*)arr->data); | |
| 1492 | 74 | img_buf.height = image_buffer_height((uint8_t*)arr->data); | |
| 1493 | 74 | img_buf.mem_base = (uint8_t*)arr->data; | |
| 1494 | 74 | img_buf.data = image_buffer_data((uint8_t*)arr->data); | |
| 1495 | |||
| 1496 | color_t colors[16]; | ||
| 1497 |
3/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
|
74 | if (argn == 4 && !lbm_display_decode_color_list(args[3], colors, 16)) { |
| 1498 | ✗ | return ENC_SYM_TERROR; | |
| 1499 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 60 times.
|
74 | } else if (argn == 3) { |
| 1500 | 14 | memset(colors, 0, sizeof(color_t) * 16); | |
| 1501 | } | ||
| 1502 | |||
| 1503 | // img_buf is a stack allocated image_buffer_t. | ||
| 1504 | 74 | 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); | |
| 1505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (!render_res) { |
| 1506 | ✗ | lbm_set_error_reason("Could not render image. Check if the format and location is compatible with the display."); | |
| 1507 | ✗ | return ENC_SYM_EERROR; | |
| 1508 | } | ||
| 1509 | 74 | res = ENC_SYM_TRUE; | |
| 1510 | } | ||
| 1511 | 74 | return res; | |
| 1512 | } | ||
| 1513 | |||
| 1514 | // Jpg decoder | ||
| 1515 | |||
| 1516 | 3060 | int jpg_output_func ( /* 1:Ok, 0:Aborted */ | |
| 1517 | JDEC* jd, /* Decompression object */ | ||
| 1518 | void* bitmap, /* Bitmap data to be output */ | ||
| 1519 | JRECT* rect /* Rectangular region to output */ | ||
| 1520 | ) { | ||
| 1521 | 3060 | tinygfx_jpg_io_t *dev = (tinygfx_jpg_io_t*)jd->device; | |
| 1522 | |||
| 1523 | image_buffer_t img; | ||
| 1524 | 3060 | img.mem_base = (uint8_t*)bitmap; | |
| 1525 | 3060 | img.data = (uint8_t*)bitmap; | |
| 1526 | 3060 | img.width = (uint16_t)(rect->right - rect->left + 1); | |
| 1527 | 3060 | img.height = (uint16_t)(rect->bottom - rect->top + 1); | |
| 1528 | 3060 | img.fmt = rgb888; | |
| 1529 | |||
| 1530 | 3060 | disp_render_image(&img, (uint16_t)(rect->left + dev->ofs_x), (uint16_t)(rect->top + dev->ofs_y), 0); | |
| 1531 | |||
| 1532 | 3060 | return 1; | |
| 1533 | } | ||
| 1534 | |||
| 1535 | 2 | static lbm_value ext_disp_render_jpg(lbm_value *args, lbm_uint argn) { | |
| 1536 | |||
| 1537 | lbm_array_header_t *array; | ||
| 1538 | 2 | lbm_value res = ENC_SYM_TERROR; | |
| 1539 | |||
| 1540 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | if (argn == 3 && |
| 1541 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 | (array = lbm_dec_array_r(args[0])) && //asignment |
| 1542 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 | lbm_is_number(args[1]) && |
| 1543 | 2 | lbm_is_number(args[2])) { | |
| 1544 | |||
| 1545 | JDEC jd; | ||
| 1546 | void *jdwork; | ||
| 1547 | // make a bit of room before the buffer. | ||
| 1548 | 2 | const size_t sz_work = 4096 + IMAGE_BUFFER_HEADER_SIZE; | |
| 1549 | |||
| 1550 | 2 | jdwork = lbm_malloc(sz_work); | |
| 1551 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!jdwork) { |
| 1552 | ✗ | return ENC_SYM_MERROR; | |
| 1553 | } | ||
| 1554 | |||
| 1555 | |||
| 1556 | |||
| 1557 | tinygfx_jpg_io_t iodev; | ||
| 1558 | 2 | iodev.data = (uint8_t*)(array->data); | |
| 1559 | 2 | iodev.size = array->size; | |
| 1560 | 2 | iodev.pos = 0; | |
| 1561 | 2 | iodev.dest = NULL; | |
| 1562 | 2 | iodev.ofs_x = lbm_dec_as_i32(args[1]); | |
| 1563 | 2 | iodev.ofs_y = lbm_dec_as_i32(args[2]); | |
| 1564 | 2 | jd_prepare(&jd, tinygfx_jpg_input, jdwork, sz_work, &iodev); | |
| 1565 | 2 | jd_decomp(&jd, jpg_output_func, 0); | |
| 1566 | 2 | lbm_free(jdwork); | |
| 1567 | 2 | res = ENC_SYM_TRUE; | |
| 1568 | } | ||
| 1569 | 2 | return res; | |
| 1570 | } | ||
| 1571 | |||
| 1572 | 4 | static lbm_value ext_img_render_jpg(lbm_value *args, lbm_uint argn) { | |
| 1573 | |||
| 1574 | lbm_array_header_t *dest_arr; | ||
| 1575 | lbm_array_header_t *jpg_arr; | ||
| 1576 | 4 | lbm_value res = ENC_SYM_TERROR; | |
| 1577 | |||
| 1578 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
8 | if (argn == 4 && |
| 1579 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
7 | (dest_arr = get_image_buffer(args[0])) && //assignment |
| 1580 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
6 | (jpg_arr = lbm_dec_array_r(args[1])) && //assignment |
| 1581 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
6 | lbm_is_number(args[2]) && |
| 1582 | 3 | lbm_is_number(args[3])) { | |
| 1583 | |||
| 1584 | image_buffer_t dest_buf; | ||
| 1585 | 3 | dest_buf.width = image_buffer_width((uint8_t*)dest_arr->data); | |
| 1586 | 3 | dest_buf.height = image_buffer_height((uint8_t*)dest_arr->data); | |
| 1587 | 3 | dest_buf.fmt = image_buffer_format((uint8_t*)dest_arr->data); | |
| 1588 | 3 | dest_buf.mem_base = (uint8_t*)dest_arr->data; | |
| 1589 | 3 | dest_buf.data = image_buffer_data((uint8_t*)dest_arr->data); | |
| 1590 | |||
| 1591 | 3 | const size_t sz_work = 4096; | |
| 1592 | 3 | void *jdwork = lbm_malloc(sz_work); | |
| 1593 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!jdwork) { |
| 1594 | ✗ | return ENC_SYM_MERROR; | |
| 1595 | } | ||
| 1596 | |||
| 1597 | 9 | bool ok = tinygfx_decode_jpg(&dest_buf, | |
| 1598 | 3 | (uint8_t*)jpg_arr->data, jpg_arr->size, | |
| 1599 | 3 | lbm_dec_as_i32(args[2]), lbm_dec_as_i32(args[3]), | |
| 1600 | jdwork, sz_work); | ||
| 1601 | 3 | lbm_free(jdwork); | |
| 1602 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | res = ok ? ENC_SYM_TRUE : ENC_SYM_NIL; |
| 1603 | } | ||
| 1604 | 4 | return res; | |
| 1605 | } | ||
| 1606 | |||
| 1607 | 804 | void lbm_display_extensions_init(void) { | |
| 1608 | 804 | register_symbols(); | |
| 1609 | |||
| 1610 | 804 | disp_render_image = NULL; | |
| 1611 | 804 | disp_clear = NULL; | |
| 1612 | 804 | disp_reset = NULL; | |
| 1613 | |||
| 1614 | 804 | lbm_add_extension("img-buffer", ext_image_buffer); | |
| 1615 | 804 | lbm_add_extension("img-buffer?", ext_is_image_buffer); | |
| 1616 | 804 | lbm_add_extension("img-color", ext_color); | |
| 1617 | 804 | lbm_add_extension("img-color-set", ext_color_set); | |
| 1618 | 804 | lbm_add_extension("img-color-get", ext_color_get); | |
| 1619 | 804 | lbm_add_extension("img-color-setpre", ext_color_setpre); | |
| 1620 | 804 | lbm_add_extension("img-color-getpre", ext_color_getpre); | |
| 1621 | 804 | lbm_add_extension("img-dims", ext_image_dims); | |
| 1622 | 804 | lbm_add_extension("img-setpix", ext_putpixel); | |
| 1623 | 804 | lbm_add_extension("img-getpix", ext_getpixel); | |
| 1624 | 804 | lbm_add_extension("img-line", ext_line); | |
| 1625 | 804 | lbm_add_extension("img-text", ext_text); | |
| 1626 | 804 | lbm_add_extension("img-clear", ext_clear); | |
| 1627 | 804 | lbm_add_extension("img-circle", ext_circle); | |
| 1628 | 804 | lbm_add_extension("img-arc", ext_arc); | |
| 1629 | 804 | lbm_add_extension("img-circle-sector", ext_circle_sector); | |
| 1630 | 804 | lbm_add_extension("img-circle-segment", ext_circle_segment); | |
| 1631 | 804 | lbm_add_extension("img-rectangle", ext_rectangle); | |
| 1632 | 804 | lbm_add_extension("img-triangle", ext_triangle); | |
| 1633 | 804 | lbm_add_extension("img-blit", ext_blit); | |
| 1634 | |||
| 1635 | 804 | lbm_add_extension("disp-reset", ext_disp_reset); | |
| 1636 | 804 | lbm_add_extension("disp-clear", ext_disp_clear); | |
| 1637 | 804 | lbm_add_extension("disp-render", ext_disp_render); | |
| 1638 | 804 | lbm_add_extension("disp-render-jpg", ext_disp_render_jpg); | |
| 1639 | 804 | lbm_add_extension("img-render-jpg", ext_img_render_jpg); | |
| 1640 | 804 | } | |
| 1641 | |||
| 1642 | 90 | void lbm_display_extensions_set_callbacks( | |
| 1643 | bool(* volatile render_image)(image_buffer_t *img, uint16_t x, uint16_t y, color_t *colors), | ||
| 1644 | void(* volatile clear)(uint32_t color), | ||
| 1645 | void(* volatile reset)(void) | ||
| 1646 | ) { | ||
| 1647 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | disp_render_image = render_image ? render_image : display_dummy_render_image; |
| 1648 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | disp_clear = clear ? clear : display_dummy_clear; |
| 1649 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | disp_reset = reset ? reset : display_dummy_reset; |
| 1650 | 90 | } | |
| 1651 |