| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/graphics/pixmapimages.h" | ||
| 30 | #include "gate/strings.h" | ||
| 31 | #include "gate/results.h" | ||
| 32 | #include "gate/maps.h" | ||
| 33 | #include "gate/comparers.h" | ||
| 34 | #include "gate/debugging.h" | ||
| 35 | #include "gate/arrays.h" | ||
| 36 | |||
| 37 | ✗ | gate_result_t gate_pixmapimage_parse(char const* const* lines, gate_rasterimage_t* image, | |
| 38 | gate_int32_t* hotspot_x, gate_int32_t* hotspot_y) | ||
| 39 | { | ||
| 40 | gate_int64_t width, height, colors, depth, hx, hy, x, y; | ||
| 41 | gate_result_t ret; | ||
| 42 | char const* lineptr; | ||
| 43 | gate_size_t linelen; | ||
| 44 | gate_size_t result; | ||
| 45 | gate_size_t ndx; | ||
| 46 | gate_size_t col_count; | ||
| 47 | ✗ | gate_map_t colormap = GATE_INIT_EMPTY; | |
| 48 | gate_string_t line; | ||
| 49 | gate_size_t colmarkerlen; | ||
| 50 | gate_size_t coldescroffset; | ||
| 51 | gate_string_t colmarker; | ||
| 52 | gate_string_t coldescriptor; | ||
| 53 | gate_color_t color; | ||
| 54 | gate_color_t* colorptr; | ||
| 55 | gate_color_t const* currentcolor; | ||
| 56 | static gate_color_t const transparent_color = { { 1, 1, 1, 0 } }; | ||
| 57 | |||
| 58 | GATE_UNUSED_ARG(hotspot_x); | ||
| 59 | GATE_UNUSED_ARG(hotspot_y); | ||
| 60 | |||
| 61 | do | ||
| 62 | { | ||
| 63 | ✗ | if (lines == NULL) | |
| 64 | { | ||
| 65 | ✗ | ret = GATE_RESULT_NULLPOINTER; | |
| 66 | ✗ | break; | |
| 67 | } | ||
| 68 | ✗ | lineptr = lines[0]; | |
| 69 | ✗ | linelen = gate_str_length(lineptr); | |
| 70 | |||
| 71 | ✗ | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &width))) { ret = GATE_RESULT_INVALIDHEADER; break; }; | |
| 72 | ✗ | lineptr += result; linelen -= result; | |
| 73 | |||
| 74 | ✗ | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &height))) { ret = GATE_RESULT_INVALIDHEADER; break; }; | |
| 75 | ✗ | lineptr += result; linelen -= result; | |
| 76 | |||
| 77 | ✗ | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &colors))) { ret = GATE_RESULT_INVALIDHEADER; break; }; | |
| 78 | ✗ | lineptr += result; linelen -= result; | |
| 79 | ✗ | col_count = (gate_size_t)colors; | |
| 80 | |||
| 81 | ✗ | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &depth))) { ret = GATE_RESULT_INVALIDHEADER; break; }; | |
| 82 | ✗ | lineptr += result; linelen -= result; | |
| 83 | |||
| 84 | ✗ | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &hx))) { hx = 0; }; | |
| 85 | ✗ | lineptr += result; linelen -= result; | |
| 86 | |||
| 87 | ✗ | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &hy))) { hy = 0; }; | |
| 88 | ✗ | lineptr += result; linelen -= result; | |
| 89 | |||
| 90 | ✗ | if (NULL == gate_map_create(&colormap, &gate_compare_string, | |
| 91 | sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor, | ||
| 92 | sizeof(gate_color_t), NULL, NULL)) | ||
| 93 | { | ||
| 94 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 95 | ✗ | break; | |
| 96 | } | ||
| 97 | |||
| 98 | ✗ | ret = GATE_RESULT_OK; | |
| 99 | ✗ | ++lines; | |
| 100 | |||
| 101 | ✗ | colmarkerlen = (gate_size_t)depth; | |
| 102 | ✗ | coldescroffset = colmarkerlen + 1; | |
| 103 | |||
| 104 | /* build the color map that connects all text-tokens to a parsed color value */ | ||
| 105 | ✗ | for (ndx = 0; ndx != col_count; ++ndx, ++lines) | |
| 106 | { | ||
| 107 | ✗ | if (NULL == gate_string_create_static(&line, *lines)) | |
| 108 | { | ||
| 109 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 110 | ✗ | break; | |
| 111 | } | ||
| 112 | ✗ | if (gate_string_length(&line) <= coldescroffset) | |
| 113 | { | ||
| 114 | ✗ | ret = GATE_RESULT_INVALIDDATA; | |
| 115 | ✗ | break; | |
| 116 | } | ||
| 117 | |||
| 118 | ✗ | switch (line.str[coldescroffset]) | |
| 119 | { | ||
| 120 | ✗ | case 'c': | |
| 121 | case 'g': | ||
| 122 | { | ||
| 123 | ✗ | if (NULL == gate_string_substr(&colmarker, &line, 0, colmarkerlen)) | |
| 124 | { | ||
| 125 | ✗ | continue; | |
| 126 | } | ||
| 127 | ✗ | if (NULL == gate_string_substr(&coldescriptor, &line, coldescroffset + 2, GATE_STR_NPOS)) | |
| 128 | { | ||
| 129 | ✗ | gate_string_release(&colmarker); | |
| 130 | ✗ | continue; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | if (gate_str_starts_with_ic(coldescriptor.str, coldescriptor.length, "none", 4)) | |
| 134 | { | ||
| 135 | ✗ | gate_map_add(&colormap, &colmarker, &transparent_color); | |
| 136 | } | ||
| 137 | else | ||
| 138 | { | ||
| 139 | ✗ | if (gate_color_parse_hex(coldescriptor.str, coldescriptor.length, &color)) | |
| 140 | { | ||
| 141 | ✗ | gate_map_add(&colormap, &colmarker, &color); | |
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | ✗ | gate_string_release(&coldescriptor); | |
| 146 | ✗ | gate_string_release(&colmarker); | |
| 147 | ✗ | break; | |
| 148 | } | ||
| 149 | } | ||
| 150 | ✗ | gate_string_release(&line); | |
| 151 | } | ||
| 152 | |||
| 153 | ✗ | if (GATE_FAILED(ret)) | |
| 154 | { | ||
| 155 | ✗ | break; | |
| 156 | } | ||
| 157 | |||
| 158 | ✗ | if (NULL == gate_rasterimage_create(image, GATE_IMAGE_PIXELFORMAT_DEFAULT, | |
| 159 | (gate_uint32_t)width, (gate_uint32_t)height, NULL)) | ||
| 160 | { | ||
| 161 | |||
| 162 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 163 | ✗ | break; | |
| 164 | } | ||
| 165 | |||
| 166 | /* set pixels of image by finding the text token in the colormap */ | ||
| 167 | |||
| 168 | ✗ | colorptr = (gate_color_t*)gate_rasterimage_get_line_ptr(image, 0); | |
| 169 | ✗ | for (y = 0; y != height; ++y, ++lines) | |
| 170 | { | ||
| 171 | ✗ | if (NULL == gate_string_create_static(&line, *lines)) | |
| 172 | { | ||
| 173 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 174 | ✗ | break; | |
| 175 | } | ||
| 176 | |||
| 177 | ✗ | GATE_DEBUG_ASSERT(line.length == (gate_size_t)(width * depth)); | |
| 178 | |||
| 179 | ✗ | for (x = 0, ndx = 0; x != width; ++x, ++colorptr) | |
| 180 | { | ||
| 181 | |||
| 182 | ✗ | if (NULL != gate_string_substr(&colmarker, &line, ndx, (gate_size_t)depth)) | |
| 183 | { | ||
| 184 | ✗ | currentcolor = (gate_color_t const*)gate_map_get_value(&colormap, &colmarker); | |
| 185 | ✗ | if (currentcolor != NULL) | |
| 186 | { | ||
| 187 | ✗ | *colorptr = *currentcolor; | |
| 188 | } | ||
| 189 | ✗ | gate_string_release(&colmarker); | |
| 190 | } | ||
| 191 | ✗ | ndx += (gate_size_t)depth; | |
| 192 | } | ||
| 193 | |||
| 194 | ✗ | gate_string_release(&line); | |
| 195 | } | ||
| 196 | |||
| 197 | } while (0); | ||
| 198 | |||
| 199 | ✗ | gate_map_destroy(&colormap); | |
| 200 | |||
| 201 | ✗ | return ret; | |
| 202 | } | ||
| 203 | |||
| 204 | typedef enum pixmap_loader_state_enum | ||
| 205 | { | ||
| 206 | pls_header, | ||
| 207 | pls_info, | ||
| 208 | pls_palette, | ||
| 209 | pls_content, | ||
| 210 | pls_end, | ||
| 211 | pls_error, | ||
| 212 | } pixmap_loader_state_t; | ||
| 213 | |||
| 214 | typedef struct pixmap_info_class | ||
| 215 | { | ||
| 216 | unsigned width; | ||
| 217 | unsigned height; | ||
| 218 | unsigned colors; | ||
| 219 | unsigned bytes_per_pixel; | ||
| 220 | } pixmap_info_t; | ||
| 221 | |||
| 222 | 36 | static gate_string_t* decode_pixmap_line(gate_string_t* target, gate_string_t const* src) | |
| 223 | { | ||
| 224 | gate_size_t pos; | ||
| 225 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | if (!gate_string_starts_with_char(src, '\"')) |
| 226 | { | ||
| 227 | ✗ | return NULL; | |
| 228 | } | ||
| 229 | 36 | pos = gate_string_char_pos_last(src, '\"'); | |
| 230 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
|
36 | if ((GATE_STR_NPOS == pos) || (0 == pos)) |
| 231 | { | ||
| 232 | ✗ | return NULL; | |
| 233 | } | ||
| 234 | 36 | gate_string_substr(target, src, 1, pos - 1); | |
| 235 | 36 | return target; | |
| 236 | } | ||
| 237 | |||
| 238 | 1 | static pixmap_info_t* parse_info(pixmap_info_t* info, gate_string_t const* content) | |
| 239 | { | ||
| 240 | static gate_string_t const sep = GATE_STRING_INIT_STATIC(" "); | ||
| 241 | 1 | gate_string_t data = GATE_STRING_INIT_EMPTY; | |
| 242 | 1 | gate_string_t item = GATE_STRING_INIT_EMPTY; | |
| 243 | 1 | unsigned numbers[32] = { 0 }; | |
| 244 | 1 | unsigned ndx = 0; | |
| 245 | 1 | gate_bool_t continue_parse = true; | |
| 246 | gate_size_t len_parsed; | ||
| 247 | gate_uint64_t num; | ||
| 248 | |||
| 249 | 1 | gate_string_duplicate(&data, content); | |
| 250 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
5 | while (continue_parse && (ndx < sizeof(numbers) / sizeof(numbers[0]))) |
| 251 | { | ||
| 252 | 4 | len_parsed = gate_string_parse(&data, &sep, 0, &item, &data, false); | |
| 253 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (len_parsed > 0) |
| 254 | { | ||
| 255 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (0 != gate_string_parse_uint(&item, &num)) |
| 256 | { | ||
| 257 | 3 | numbers[ndx] = (unsigned)num; | |
| 258 | 3 | ++ndx; | |
| 259 | } | ||
| 260 | } | ||
| 261 | else | ||
| 262 | { | ||
| 263 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (0 != gate_string_parse_uint(&data, &num)) |
| 264 | { | ||
| 265 | 1 | numbers[ndx] = (unsigned)num; | |
| 266 | 1 | ++ndx; | |
| 267 | } | ||
| 268 | 1 | continue_parse = false; | |
| 269 | } | ||
| 270 | } | ||
| 271 | 1 | gate_string_release(&data); | |
| 272 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ndx >= 4) |
| 273 | { | ||
| 274 | 1 | info->width = numbers[0]; | |
| 275 | 1 | info->height = numbers[1]; | |
| 276 | 1 | info->colors = numbers[2]; | |
| 277 | 1 | info->bytes_per_pixel = numbers[3]; | |
| 278 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if ((info->width > 0) && (info->width <= 4096) |
| 279 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | && (info->height > 0) && (info->height <= 4096) |
| 280 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | && (info->colors > 0) && (info->colors <= 4096) |
| 281 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | && (info->bytes_per_pixel > 0) && (info->bytes_per_pixel <= 4) |
| 282 | ) | ||
| 283 | { | ||
| 284 | 1 | return info; | |
| 285 | } | ||
| 286 | } | ||
| 287 | ✗ | return NULL; | |
| 288 | } | ||
| 289 | |||
| 290 | typedef struct pixmap_color_class | ||
| 291 | { | ||
| 292 | char token[4]; | ||
| 293 | gate_color_t col; | ||
| 294 | } pixmap_color_t; | ||
| 295 | |||
| 296 | 3 | static void parse_color_token(gate_color_t* col, char const* str, gate_size_t len) | |
| 297 | { | ||
| 298 | 3 | GATE_DEBUG_ASSERT(col != NULL); | |
| 299 | |||
| 300 | 3 | gate_mem_clear(col, sizeof(gate_color_t)); | |
| 301 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
3 | if ((len >= 7) && (str != NULL) && (str[0] == '#')) |
| 302 | { | ||
| 303 | 2 | gate_str_parse_hex_byte(&str[1], &col->r); | |
| 304 | 2 | gate_str_parse_hex_byte(&str[3], &col->g); | |
| 305 | 2 | gate_str_parse_hex_byte(&str[5], &col->b); | |
| 306 | 2 | col->a = 255; | |
| 307 | } | ||
| 308 | 3 | } | |
| 309 | |||
| 310 | 3 | static pixmap_color_t* parse_color(pixmap_color_t* col, pixmap_info_t const* info, gate_string_t const* data) | |
| 311 | { | ||
| 312 | char type_chr; | ||
| 313 | 3 | gate_mem_clear(col, sizeof(pixmap_color_t)); | |
| 314 | 3 | gate_mem_copy(&col->token[0], data->str, info->bytes_per_pixel); | |
| 315 | 3 | type_chr = data->str[info->bytes_per_pixel + 1]; | |
| 316 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if ((type_chr == 'c') || (type_chr == 'g')) |
| 317 | { | ||
| 318 | 3 | parse_color_token(&col->col, &data->str[info->bytes_per_pixel + 3], data->length - info->bytes_per_pixel - 3); | |
| 319 | } | ||
| 320 | 3 | return col; | |
| 321 | } | ||
| 322 | |||
| 323 | 32 | static void parse_image_line(gate_color_t* ptr_pixels, pixmap_info_t const* info, gate_arraylist_t colors, gate_string_t const* data) | |
| 324 | { | ||
| 325 | 32 | gate_size_t len = gate_string_length(data) / info->bytes_per_pixel; | |
| 326 | gate_size_t ndx; | ||
| 327 | 32 | pixmap_color_t const* ptr_colentries = (pixmap_color_t const*)gate_arraylist_get(colors, 0); | |
| 328 | pixmap_color_t const* ptr_curentry; | ||
| 329 | 32 | gate_size_t colcount = gate_arraylist_length(colors); | |
| 330 | char const* ptr_curdata; | ||
| 331 | unsigned x; | ||
| 332 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (len > info->width) |
| 333 | { | ||
| 334 | ✗ | len = info->width; | |
| 335 | } | ||
| 336 | 32 | ptr_curdata = data->str; | |
| 337 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != len; ++x, ++ptr_pixels) |
| 338 | { | ||
| 339 | 2048 | ptr_curentry = ptr_colentries; | |
| 340 |
1/2✓ Branch 0 taken 6020 times.
✗ Branch 1 not taken.
|
6020 | for (ndx = 0; ndx != colcount; ++ndx, ++ptr_curentry) |
| 341 | { | ||
| 342 |
2/2✓ Branch 1 taken 2048 times.
✓ Branch 2 taken 3972 times.
|
6020 | if (0 == gate_mem_compare(ptr_curdata, ptr_curentry->token, info->bytes_per_pixel)) |
| 343 | { | ||
| 344 | 2048 | *ptr_pixels = ptr_curentry->col; | |
| 345 | 2048 | break; | |
| 346 | } | ||
| 347 | } | ||
| 348 | 2048 | ptr_curdata += info->bytes_per_pixel; | |
| 349 | } | ||
| 350 | 32 | } | |
| 351 | |||
| 352 | 1 | gate_result_t gate_pixmapimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags) | |
| 353 | { | ||
| 354 | 1 | gate_result_t result = GATE_RESULT_FAILED; | |
| 355 | char linebuffer[4096]; | ||
| 356 | gate_size_t linelen; | ||
| 357 | 1 | pixmap_loader_state_t state = pls_header; | |
| 358 | 1 | gate_string_t line = GATE_STRING_INIT_EMPTY; | |
| 359 | 1 | gate_string_t content = GATE_STRING_INIT_EMPTY; | |
| 360 | 1 | pixmap_info_t info = GATE_INIT_EMPTY; | |
| 361 | 1 | gate_arraylist_t colors = NULL; | |
| 362 | pixmap_color_t colmapping; | ||
| 363 | gate_color_t* ptr_pixel; | ||
| 364 | 1 | unsigned y = 0; | |
| 365 | GATE_UNUSED_ARG(flags); | ||
| 366 | |||
| 367 | do | ||
| 368 | { | ||
| 369 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!srcstream || !image) |
| 370 | { | ||
| 371 | ✗ | result = GATE_RESULT_INVALIDARG; | |
| 372 | ✗ | break; | |
| 373 | } | ||
| 374 | |||
| 375 | 1 | colors = gate_arraylist_create(sizeof(pixmap_color_t), NULL, 0, NULL, NULL); | |
| 376 | |||
| 377 | 1 | gate_mem_clear(image, sizeof(gate_rasterimage_t)); | |
| 378 | |||
| 379 |
3/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
39 | while ((state != pls_end) && (state != pls_error)) |
| 380 | { | ||
| 381 | 38 | result = gate_stream_read_line(srcstream, linebuffer, sizeof(linebuffer), &linelen); | |
| 382 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | GATE_BREAK_IF_FAILED(result); |
| 383 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (linelen == 0) |
| 384 | { | ||
| 385 | ✗ | break; | |
| 386 | } | ||
| 387 | |||
| 388 | 38 | gate_string_create_static_len(&line, linebuffer, linelen); | |
| 389 | 38 | gate_string_trim(&line, &line); | |
| 390 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
|
38 | if (0 == gate_string_length(&line)) |
| 391 | { | ||
| 392 | ✗ | continue; | |
| 393 | } | ||
| 394 |
3/4✓ Branch 1 taken 37 times.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 37 times.
|
38 | if (gate_string_starts_with_str(&line, "/*") || gate_string_ends_with_str(&line, "*/")) |
| 395 | { | ||
| 396 | 1 | continue; | |
| 397 | } | ||
| 398 | |||
| 399 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 32 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
37 | switch (state) |
| 400 | { | ||
| 401 | 1 | case pls_header: | |
| 402 | { | ||
| 403 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (gate_string_ends_with_char(&line, '{')) |
| 404 | { | ||
| 405 | /* header is skipped -> read info line */ | ||
| 406 | 1 | state = pls_info; | |
| 407 | } | ||
| 408 | 1 | break; | |
| 409 | } | ||
| 410 | 1 | case pls_info: | |
| 411 | { | ||
| 412 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == decode_pixmap_line(&content, &line)) |
| 413 | { | ||
| 414 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 415 | ✗ | state = pls_error; | |
| 416 | ✗ | break; | |
| 417 | } | ||
| 418 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == parse_info(&info, &content)) |
| 419 | { | ||
| 420 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 421 | ✗ | state = pls_error; | |
| 422 | ✗ | break; | |
| 423 | } | ||
| 424 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == gate_rasterimage_create(image, GATE_IMAGE_PIXELFORMAT_RGBA, info.width, info.height, NULL)) |
| 425 | { | ||
| 426 | ✗ | result = GATE_RESULT_OUTOFMEMORY; | |
| 427 | ✗ | state = pls_error; | |
| 428 | ✗ | break; | |
| 429 | } | ||
| 430 | 1 | state = pls_palette; | |
| 431 | 1 | break; | |
| 432 | } | ||
| 433 | 3 | case pls_palette: | |
| 434 | { | ||
| 435 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == decode_pixmap_line(&content, &line)) |
| 436 | { | ||
| 437 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 438 | ✗ | state = pls_error; | |
| 439 | ✗ | break; | |
| 440 | } | ||
| 441 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == parse_color(&colmapping, &info, &content)) |
| 442 | { | ||
| 443 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 444 | ✗ | state = pls_error; | |
| 445 | ✗ | break; | |
| 446 | } | ||
| 447 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == gate_arraylist_add(colors, &colmapping)) |
| 448 | { | ||
| 449 | ✗ | result = GATE_RESULT_OUTOFMEMORY; | |
| 450 | ✗ | state = pls_error; | |
| 451 | ✗ | break; | |
| 452 | } | ||
| 453 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if (gate_arraylist_length(colors) >= info.colors) |
| 454 | { | ||
| 455 | 1 | state = pls_content; | |
| 456 | } | ||
| 457 | 3 | break; | |
| 458 | } | ||
| 459 | 32 | case pls_content: | |
| 460 | { | ||
| 461 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (NULL == decode_pixmap_line(&content, &line)) |
| 462 | { | ||
| 463 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 464 | ✗ | state = pls_error; | |
| 465 | ✗ | break; | |
| 466 | } | ||
| 467 | 32 | ptr_pixel = (gate_color_t*)gate_rasterimage_get_line_ptr(image, y); | |
| 468 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (ptr_pixel) |
| 469 | { | ||
| 470 | 32 | parse_image_line(ptr_pixel, &info, colors, &content); | |
| 471 | } | ||
| 472 | 32 | ++y; | |
| 473 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
|
32 | if (y >= info.height) |
| 474 | { | ||
| 475 | 1 | state = pls_end; | |
| 476 | } | ||
| 477 | 32 | break; | |
| 478 | } | ||
| 479 | ✗ | case pls_end: | |
| 480 | case pls_error: | ||
| 481 | { | ||
| 482 | ✗ | break; | |
| 483 | } | ||
| 484 | } | ||
| 485 | 37 | gate_string_release(&content); | |
| 486 | } | ||
| 487 | |||
| 488 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(result); |
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (state != pls_end) |
| 490 | { | ||
| 491 | ✗ | gate_rasterimage_release(image); | |
| 492 | ✗ | result = GATE_RESULT_INVALIDSTATE; | |
| 493 | } | ||
| 494 | } while (0); | ||
| 495 | |||
| 496 | 1 | gate_arraylist_release(colors); | |
| 497 | |||
| 498 | 1 | return result; | |
| 499 | } | ||
| 500 | |||
| 501 | ✗ | static void copy_rgba(gate_uint8_t* dst, gate_uint8_t const* src, gate_size_t len) | |
| 502 | { | ||
| 503 | ✗ | gate_mem_copy(dst, src, len * 4); | |
| 504 | ✗ | } | |
| 505 | |||
| 506 | static gate_string_t const pix_chars = GATE_STRING_INIT_STATIC(" _.,:;~#=+-*/|^!$%&()[]{}<>@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); | ||
| 507 | |||
| 508 | 3 | static void print_color(gate_stream_t* strm, gate_uint32_t col, gate_uint16_t n) | |
| 509 | { | ||
| 510 | 3 | char const* ptr = gate_string_ptr(&pix_chars, 0); | |
| 511 | 3 | const gate_size_t ccount = gate_string_length(&pix_chars); | |
| 512 | 3 | gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)n / (gate_size_t)ccount); | |
| 513 | 3 | gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)n % (gate_size_t)ccount); | |
| 514 | gate_color_t c; | ||
| 515 | 3 | c.rgba = col; | |
| 516 | |||
| 517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ndx1 >= ccount) |
| 518 | { | ||
| 519 | ✗ | ndx1 = 0; | |
| 520 | } | ||
| 521 | |||
| 522 | 6 | gate_stream_print(strm, | |
| 523 | GATE_PRINT_CSTR, "\"", | ||
| 524 | 3 | GATE_PRINT_CHAR, ptr[ndx1], | |
| 525 | 3 | GATE_PRINT_CHAR, ptr[ndx2], | |
| 526 | GATE_PRINT_CSTR, " g ", | ||
| 527 | GATE_PRINT_END); | ||
| 528 | |||
| 529 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (c.a < 128) |
| 530 | { | ||
| 531 | 1 | gate_stream_print(strm, | |
| 532 | GATE_PRINT_CSTR, "None", | ||
| 533 | GATE_PRINT_END); | ||
| 534 | } | ||
| 535 | else | ||
| 536 | { | ||
| 537 | 6 | gate_stream_print(strm, | |
| 538 | GATE_PRINT_CSTR, "#", | ||
| 539 | 2 | GATE_PRINT_H8, c.r, | |
| 540 | 2 | GATE_PRINT_H8, c.g, | |
| 541 | 2 | GATE_PRINT_H8, c.b, | |
| 542 | GATE_PRINT_END); | ||
| 543 | } | ||
| 544 | |||
| 545 | 3 | gate_stream_print(strm, | |
| 546 | GATE_PRINT_CSTR, "\",\n", | ||
| 547 | GATE_PRINT_END); | ||
| 548 | 3 | } | |
| 549 | |||
| 550 | 1 | gate_result_t gate_pixmapimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags) | |
| 551 | { | ||
| 552 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 553 | 1 | gate_map_t col_map = GATE_INIT_EMPTY; | |
| 554 | 1 | char const* ptr = gate_string_ptr(&pix_chars, 0); | |
| 555 | 1 | const gate_size_t ccount = gate_string_length(&pix_chars); | |
| 556 | 1 | gate_uint16_t ncol = 0; | |
| 557 | unsigned y, x; | ||
| 558 | gate_uint8_t const* ptr_image_line; | ||
| 559 | 1 | gate_color_converter_t line_writer = NULL; | |
| 560 | gate_uint8_t buffer[4096]; | ||
| 561 | 1 | gate_uint8_t* ptr_buffer = &buffer[0]; | |
| 562 | gate_color_t const* ptr_pixel; | ||
| 563 | gate_uint16_t col_count; | ||
| 564 | gate_color_t c; | ||
| 565 | gate_map_iterator_t col_iter; | ||
| 566 | |||
| 567 | GATE_UNUSED_ARG(flags); | ||
| 568 | |||
| 569 | do | ||
| 570 | { | ||
| 571 |
1/11✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
1 | switch (image->pixel_format) |
| 572 | { | ||
| 573 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGBA: | |
| 574 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB32: line_writer = ©_rgba; break; | |
| 575 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGRA: | |
| 576 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR32: line_writer = &gate_color_convert_rgba_from_bgra_32; break; | |
| 577 | |||
| 578 | 1 | case GATE_IMAGE_PIXELFORMAT_RGB24: line_writer = &gate_color_convert_rgba_from_rgb_24; break; | |
| 579 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR24: line_writer = &gate_color_convert_rgba_from_bgr_24; break; | |
| 580 | |||
| 581 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB555: line_writer = &gate_color_convert_rgba_from_rgb_15; break; | |
| 582 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB565: line_writer = &gate_color_convert_rgba_from_rgb_16; break; | |
| 583 | ✗ | case GATE_IMAGE_PIXELFORMAT_ARGB4: line_writer = &gate_color_convert_rgba_from_argb_16; break; | |
| 584 | ✗ | case GATE_IMAGE_PIXELFORMAT_YUV2: line_writer = &gate_color_convert_rgba_from_yuv2_16; break; | |
| 585 | |||
| 586 | ✗ | case GATE_IMAGE_PIXELFORMAT_PAL8: line_writer = &gate_color_convert_rgba_from_pal_8; break; | |
| 587 | ✗ | case GATE_IMAGE_PIXELFORMAT_GRAY8: line_writer = &gate_color_convert_rgba_from_gray_8; break; | |
| 588 | ✗ | default: | |
| 589 | ✗ | break; | |
| 590 | } | ||
| 591 | |||
| 592 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (line_writer == NULL) |
| 593 | { | ||
| 594 | ✗ | ret = GATE_RESULT_NOTSUPPORTED; | |
| 595 | ✗ | break; | |
| 596 | } | ||
| 597 | |||
| 598 | 1 | c.rgba = 0; | |
| 599 | 1 | gate_map_create(&col_map, gate_compare_uint32, sizeof(gate_uint32_t), NULL, NULL, sizeof(gate_uint16_t), NULL, NULL); | |
| 600 | 1 | gate_map_add(&col_map, &c.rgba, &ncol); | |
| 601 | 1 | ++ncol; | |
| 602 | |||
| 603 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
| 604 | { | ||
| 605 | 32 | ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
| 606 | 32 | line_writer(ptr_buffer, ptr_image_line, image->width); | |
| 607 | 32 | ptr_pixel = (gate_color_t const*)ptr_buffer; | |
| 608 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != image->width; ++x) |
| 609 | { | ||
| 610 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2046 times.
|
2048 | if (NULL == gate_map_get(&col_map, &ptr_pixel[x].rgba)) |
| 611 | { | ||
| 612 | 2 | gate_map_add(&col_map, &ptr_pixel[x].rgba, &ncol); | |
| 613 | 2 | ++ncol; | |
| 614 | } | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | 1 | col_count = (gate_uint16_t)gate_map_count(&col_map); | |
| 619 | |||
| 620 | 3 | gate_stream_print(deststream, | |
| 621 | GATE_PRINT_CSTR, "/* XPM */\nstatic char const * gate_icon_brick_xpm[] = {\n\"", | ||
| 622 | 1 | GATE_PRINT_UI16, (gate_uint16_t)image->width, | |
| 623 | GATE_PRINT_CSTR, " ", | ||
| 624 | 1 | GATE_PRINT_UI16, (gate_uint16_t)image->height, | |
| 625 | GATE_PRINT_CSTR, " ", | ||
| 626 | GATE_PRINT_UI16, (gate_uint16_t)col_count, | ||
| 627 | GATE_PRINT_CSTR, " ", | ||
| 628 | GATE_PRINT_UI16, (gate_uint16_t)2, | ||
| 629 | GATE_PRINT_CSTR, "\",\n", | ||
| 630 | GATE_PRINT_END | ||
| 631 | ); | ||
| 632 | |||
| 633 | 1 | col_iter = gate_map_first(&col_map); | |
| 634 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | while (col_iter) |
| 635 | { | ||
| 636 | 3 | print_color(deststream, | |
| 637 | 3 | *(gate_uint32_t const*)gate_map_iterator_key(col_iter), | |
| 638 | 3 | *(gate_uint16_t const*)gate_map_iterator_value(col_iter)); | |
| 639 | 3 | col_iter = gate_map_iterator_next(col_iter); | |
| 640 | } | ||
| 641 | |||
| 642 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
| 643 | { | ||
| 644 | 32 | ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
| 645 | 32 | line_writer(ptr_buffer, ptr_image_line, image->width); | |
| 646 | 32 | ptr_pixel = (gate_color_t const*)ptr_buffer; | |
| 647 | 32 | gate_stream_print(deststream, GATE_PRINT_CSTR, "\"", GATE_PRINT_END); | |
| 648 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != image->width; ++x) |
| 649 | { | ||
| 650 | 2048 | gate_uint16_t const* ptr_n = gate_map_get_value(&col_map, &ptr_pixel[x].rgba); | |
| 651 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
|
2048 | if (ptr_n == NULL) |
| 652 | { | ||
| 653 | ✗ | gate_stream_print(deststream, GATE_PRINT_CSTR, " ", GATE_PRINT_END); | |
| 654 | } | ||
| 655 | else | ||
| 656 | { | ||
| 657 | 2048 | gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)*ptr_n / (gate_size_t)ccount); | |
| 658 | 2048 | gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)*ptr_n % (gate_size_t)ccount); | |
| 659 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
|
2048 | if (ndx1 >= ccount) |
| 660 | { | ||
| 661 | ✗ | ndx1 = 0; | |
| 662 | } | ||
| 663 | 4096 | gate_stream_print(deststream, | |
| 664 | 2048 | GATE_PRINT_CHAR, ptr[ndx1], | |
| 665 | 2048 | GATE_PRINT_CHAR, ptr[ndx2], | |
| 666 | GATE_PRINT_END); | ||
| 667 | } | ||
| 668 | } | ||
| 669 | 32 | gate_stream_print(deststream, GATE_PRINT_CSTR, "\",\n", GATE_PRINT_END); | |
| 670 | } | ||
| 671 | 1 | gate_stream_print(deststream, | |
| 672 | GATE_PRINT_CSTR, "};\n", | ||
| 673 | GATE_PRINT_END | ||
| 674 | ); | ||
| 675 | |||
| 676 | 1 | ret = GATE_RESULT_OK; | |
| 677 | |||
| 678 | } while (0); | ||
| 679 | |||
| 680 | 1 | gate_map_destroy(&col_map); | |
| 681 | 1 | return ret; | |
| 682 | } | ||
| 683 | |||
| 684 |