| 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 | 1 | 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_result_t ret; | ||
| 41 | 1 | gate_map_t colormap = GATE_INIT_EMPTY; | |
| 42 | static gate_color_t const transparent_color = { { 1, 1, 1, 0 } }; | ||
| 43 | |||
| 44 | GATE_UNUSED_ARG(hotspot_x); | ||
| 45 | GATE_UNUSED_ARG(hotspot_y); | ||
| 46 | |||
| 47 | do | ||
| 48 | { | ||
| 49 | gate_int64_t width, height, colors, depth, hx, hy, x, y; | ||
| 50 | char const* lineptr; | ||
| 51 | gate_size_t linelen; | ||
| 52 | gate_size_t result; | ||
| 53 | gate_size_t ndx; | ||
| 54 | gate_size_t col_count; | ||
| 55 | gate_string_t line; | ||
| 56 | gate_string_t colmarker; | ||
| 57 | gate_size_t colmarkerlen; | ||
| 58 | gate_size_t coldescroffset; | ||
| 59 | gate_string_t coldescriptor; | ||
| 60 | gate_color_t* colorptr; | ||
| 61 | |||
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (lines == NULL) |
| 63 | { | ||
| 64 | ✗ | ret = GATE_RESULT_NULLPOINTER; | |
| 65 | ✗ | break; | |
| 66 | } | ||
| 67 | 1 | lineptr = lines[0]; | |
| 68 | 1 | linelen = gate_str_length(lineptr); | |
| 69 | |||
| 70 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &width))) { ret = GATE_RESULT_INVALIDHEADER; break; }; |
| 71 | 1 | lineptr += result; linelen -= result; | |
| 72 | |||
| 73 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &height))) { ret = GATE_RESULT_INVALIDHEADER; break; }; |
| 74 | 1 | lineptr += result; linelen -= result; | |
| 75 | |||
| 76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &colors))) { ret = GATE_RESULT_INVALIDHEADER; break; }; |
| 77 | 1 | lineptr += result; linelen -= result; | |
| 78 | 1 | col_count = (gate_size_t)colors; | |
| 79 | |||
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &depth))) { ret = GATE_RESULT_INVALIDHEADER; break; }; |
| 81 | 1 | lineptr += result; linelen -= result; | |
| 82 | |||
| 83 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &hx))) { hx = 0; }; |
| 84 | 1 | lineptr += result; linelen -= result; | |
| 85 | |||
| 86 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (0 == (result = gate_str_parse_int64(lineptr, linelen, &hy))) { hy = 0; }; |
| 87 | 1 | lineptr += result; linelen -= result; | |
| 88 | |||
| 89 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == gate_map_create(&colormap, &gate_compare_string, |
| 90 | sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor, | ||
| 91 | sizeof(gate_color_t), NULL, NULL)) | ||
| 92 | { | ||
| 93 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 94 | ✗ | break; | |
| 95 | } | ||
| 96 | |||
| 97 | 1 | ret = GATE_RESULT_OK; | |
| 98 | 1 | ++lines; | |
| 99 | |||
| 100 | 1 | colmarkerlen = (gate_size_t)depth; | |
| 101 | 1 | coldescroffset = colmarkerlen + 1; | |
| 102 | |||
| 103 | /* build the color map that connects all text-tokens to a parsed color value */ | ||
| 104 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 1 times.
|
91 | for (ndx = 0; ndx != col_count; ++ndx, ++lines) |
| 105 | { | ||
| 106 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
90 | if (NULL == gate_string_create_static(&line, *lines)) |
| 107 | { | ||
| 108 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 109 | ✗ | break; | |
| 110 | } | ||
| 111 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
90 | if (gate_string_length(&line) <= coldescroffset) |
| 112 | { | ||
| 113 | ✗ | ret = GATE_RESULT_INVALIDDATA; | |
| 114 | ✗ | break; | |
| 115 | } | ||
| 116 | |||
| 117 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | switch (line.str[coldescroffset]) |
| 118 | { | ||
| 119 | 90 | case 'c': | |
| 120 | case 'g': | ||
| 121 | { | ||
| 122 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
90 | if (NULL == gate_string_substr(&colmarker, &line, 0, colmarkerlen)) |
| 123 | { | ||
| 124 | ✗ | continue; | |
| 125 | } | ||
| 126 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
90 | if (NULL == gate_string_substr(&coldescriptor, &line, coldescroffset + 2, GATE_STR_NPOS)) |
| 127 | { | ||
| 128 | ✗ | gate_string_release(&colmarker); | |
| 129 | ✗ | continue; | |
| 130 | } | ||
| 131 | |||
| 132 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 89 times.
|
90 | if (gate_str_starts_with_ic(coldescriptor.str, coldescriptor.length, "none", 4)) |
| 133 | { | ||
| 134 | 1 | gate_map_add(&colormap, &colmarker, &transparent_color); | |
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | gate_color_t color; | ||
| 139 |
1/2✓ Branch 1 taken 89 times.
✗ Branch 2 not taken.
|
89 | if (gate_color_parse_hex(coldescriptor.str, coldescriptor.length, &color)) |
| 140 | { | ||
| 141 | 89 | gate_map_add(&colormap, &colmarker, &color); | |
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | 90 | gate_string_release(&coldescriptor); | |
| 146 | 90 | gate_string_release(&colmarker); | |
| 147 | 90 | break; | |
| 148 | } | ||
| 149 | } | ||
| 150 | 90 | gate_string_release(&line); | |
| 151 | } | ||
| 152 | |||
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (GATE_FAILED(ret)) |
| 154 | { | ||
| 155 | ✗ | break; | |
| 156 | } | ||
| 157 | |||
| 158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | 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 | 1 | colorptr = (gate_color_t*)gate_rasterimage_get_line_ptr(image, 0); | |
| 169 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
|
17 | for (y = 0; y != height; ++y, ++lines) |
| 170 | { | ||
| 171 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (NULL == gate_string_create_static(&line, *lines)) |
| 172 | { | ||
| 173 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 174 | ✗ | break; | |
| 175 | } | ||
| 176 | |||
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | GATE_DEBUG_ASSERT(line.length == (gate_size_t)(width * depth)); |
| 178 | |||
| 179 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 16 times.
|
272 | for (x = 0, ndx = 0; x != width; ++x, ++colorptr) |
| 180 | { | ||
| 181 | |||
| 182 |
1/2✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
|
256 | if (NULL != gate_string_substr(&colmarker, &line, ndx, (gate_size_t)depth)) |
| 183 | { | ||
| 184 | 256 | gate_color_t const* currentcolor = (gate_color_t const*)gate_map_get_value(&colormap, &colmarker); | |
| 185 |
1/2✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
|
256 | if (currentcolor != NULL) |
| 186 | { | ||
| 187 | 256 | *colorptr = *currentcolor; | |
| 188 | } | ||
| 189 | 256 | gate_string_release(&colmarker); | |
| 190 | } | ||
| 191 | 256 | ndx += (gate_size_t)depth; | |
| 192 | } | ||
| 193 | |||
| 194 | 16 | gate_string_release(&line); | |
| 195 | } | ||
| 196 | |||
| 197 | } while (0); | ||
| 198 | |||
| 199 | 1 | gate_map_destroy(&colormap); | |
| 200 | |||
| 201 | 1 | 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 | unsigned numbers[32] = { 0 }; | |
| 243 | 1 | unsigned ndx = 0; | |
| 244 | 1 | gate_bool_t continue_parse = true; | |
| 245 | |||
| 246 | 1 | gate_string_duplicate(&data, content); | |
| 247 |
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]))) |
| 248 | { | ||
| 249 | 4 | gate_string_t item = GATE_STRING_INIT_EMPTY; | |
| 250 | gate_uint64_t num; | ||
| 251 | 4 | gate_size_t len_parsed = gate_string_parse(&data, &sep, 0, &item, &data, false); | |
| 252 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (len_parsed > 0) |
| 253 | { | ||
| 254 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (0 != gate_string_parse_uint(&item, &num)) |
| 255 | { | ||
| 256 | 3 | numbers[ndx] = (unsigned)num; | |
| 257 | 3 | ++ndx; | |
| 258 | } | ||
| 259 | } | ||
| 260 | else | ||
| 261 | { | ||
| 262 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (0 != gate_string_parse_uint(&data, &num)) |
| 263 | { | ||
| 264 | 1 | numbers[ndx] = (unsigned)num; | |
| 265 | 1 | ++ndx; | |
| 266 | } | ||
| 267 | 1 | continue_parse = false; | |
| 268 | } | ||
| 269 | } | ||
| 270 | 1 | gate_string_release(&data); | |
| 271 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ndx >= 4) |
| 272 | { | ||
| 273 | 1 | info->width = numbers[0]; | |
| 274 | 1 | info->height = numbers[1]; | |
| 275 | 1 | info->colors = numbers[2]; | |
| 276 | 1 | info->bytes_per_pixel = numbers[3]; | |
| 277 |
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) |
| 278 |
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) |
| 279 |
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) |
| 280 |
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) |
| 281 | ) | ||
| 282 | { | ||
| 283 | 1 | return info; | |
| 284 | } | ||
| 285 | } | ||
| 286 | ✗ | return NULL; | |
| 287 | } | ||
| 288 | |||
| 289 | typedef struct pixmap_color_class | ||
| 290 | { | ||
| 291 | char token[4]; | ||
| 292 | gate_color_t col; | ||
| 293 | } pixmap_color_t; | ||
| 294 | |||
| 295 | 3 | static void parse_color_token(gate_color_t* col, char const* str, gate_size_t len) | |
| 296 | { | ||
| 297 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATE_DEBUG_ASSERT(col != NULL); |
| 298 | |||
| 299 | 3 | gate_mem_clear(col, sizeof(gate_color_t)); | |
| 300 |
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] == '#')) |
| 301 | { | ||
| 302 | 2 | gate_str_parse_hex_byte(&str[1], &col->r); | |
| 303 | 2 | gate_str_parse_hex_byte(&str[3], &col->g); | |
| 304 | 2 | gate_str_parse_hex_byte(&str[5], &col->b); | |
| 305 | 2 | col->a = 255; | |
| 306 | } | ||
| 307 | 3 | } | |
| 308 | |||
| 309 | 3 | static pixmap_color_t* parse_color(pixmap_color_t* col, pixmap_info_t const* info, gate_string_t const* data) | |
| 310 | { | ||
| 311 | char type_chr; | ||
| 312 | 3 | gate_mem_clear(col, sizeof(pixmap_color_t)); | |
| 313 | 3 | gate_mem_copy(&col->token[0], data->str, info->bytes_per_pixel); | |
| 314 | 3 | type_chr = data->str[info->bytes_per_pixel + 1]; | |
| 315 |
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')) |
| 316 | { | ||
| 317 | 3 | parse_color_token(&col->col, &data->str[info->bytes_per_pixel + 3], data->length - info->bytes_per_pixel - 3); | |
| 318 | } | ||
| 319 | 3 | return col; | |
| 320 | } | ||
| 321 | |||
| 322 | 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) | |
| 323 | { | ||
| 324 | 32 | char const* ptr_curdata = gate_string_ptr(data, 0); | |
| 325 | 32 | gate_size_t len = gate_string_length(data) / info->bytes_per_pixel; | |
| 326 | 32 | pixmap_color_t const* ptr_colentries = (pixmap_color_t const*)gate_arraylist_get(colors, 0); | |
| 327 | 32 | gate_size_t const colcount = gate_arraylist_length(colors); | |
| 328 | unsigned x; | ||
| 329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (len > info->width) |
| 330 | { | ||
| 331 | ✗ | len = info->width; | |
| 332 | } | ||
| 333 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != len; ++x, ++ptr_pixels) |
| 334 | { | ||
| 335 | gate_size_t ndx; | ||
| 336 | 2048 | pixmap_color_t const* ptr_curentry = ptr_colentries; | |
| 337 |
1/2✓ Branch 0 taken 6020 times.
✗ Branch 1 not taken.
|
6020 | for (ndx = 0; ndx != colcount; ++ndx, ++ptr_curentry) |
| 338 | { | ||
| 339 |
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)) |
| 340 | { | ||
| 341 | 2048 | *ptr_pixels = ptr_curentry->col; | |
| 342 | 2048 | break; | |
| 343 | } | ||
| 344 | } | ||
| 345 | 2048 | ptr_curdata += info->bytes_per_pixel; | |
| 346 | } | ||
| 347 | 32 | } | |
| 348 | |||
| 349 | 1 | gate_result_t gate_pixmapimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags) | |
| 350 | { | ||
| 351 | 1 | gate_result_t result = GATE_RESULT_FAILED; | |
| 352 | 1 | gate_arraylist_t colors = NULL; | |
| 353 | GATE_UNUSED_ARG(flags); | ||
| 354 | |||
| 355 | do | ||
| 356 | { | ||
| 357 | char linebuffer[4096]; | ||
| 358 | 1 | pixmap_info_t info = GATE_INIT_EMPTY; | |
| 359 | 1 | pixmap_loader_state_t state = pls_header; | |
| 360 | 1 | unsigned y = 0; | |
| 361 | |||
| 362 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!srcstream || !image) |
| 363 | { | ||
| 364 | ✗ | result = GATE_RESULT_INVALIDARG; | |
| 365 | ✗ | break; | |
| 366 | } | ||
| 367 | |||
| 368 | 1 | colors = gate_arraylist_create(sizeof(pixmap_color_t), NULL, 0, NULL, NULL); | |
| 369 | |||
| 370 | 1 | gate_mem_clear(image, sizeof(gate_rasterimage_t)); | |
| 371 | |||
| 372 |
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)) |
| 373 | { | ||
| 374 | gate_size_t linelen; | ||
| 375 | 38 | gate_string_t line = GATE_STRING_INIT_EMPTY; | |
| 376 | 38 | gate_string_t content = GATE_STRING_INIT_EMPTY; | |
| 377 | |||
| 378 | 38 | result = gate_stream_read_line(srcstream, linebuffer, sizeof(linebuffer), &linelen); | |
| 379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | GATE_BREAK_IF_FAILED(result); |
| 380 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (linelen == 0) |
| 381 | { | ||
| 382 | ✗ | break; | |
| 383 | } | ||
| 384 | |||
| 385 | 38 | gate_string_create_static_len(&line, linebuffer, linelen); | |
| 386 | 38 | gate_string_trim(&line, &line); | |
| 387 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
|
38 | if (0 == gate_string_length(&line)) |
| 388 | { | ||
| 389 | 1 | continue; | |
| 390 | } | ||
| 391 |
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, "*/")) |
| 392 | { | ||
| 393 | 1 | continue; | |
| 394 | } | ||
| 395 | |||
| 396 |
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) |
| 397 | { | ||
| 398 | 1 | case pls_header: | |
| 399 | { | ||
| 400 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (gate_string_ends_with_char(&line, '{')) |
| 401 | { | ||
| 402 | /* header is skipped -> read info line */ | ||
| 403 | 1 | state = pls_info; | |
| 404 | } | ||
| 405 | 1 | break; | |
| 406 | } | ||
| 407 | 1 | case pls_info: | |
| 408 | { | ||
| 409 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == decode_pixmap_line(&content, &line)) |
| 410 | { | ||
| 411 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 412 | ✗ | state = pls_error; | |
| 413 | ✗ | break; | |
| 414 | } | ||
| 415 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == parse_info(&info, &content)) |
| 416 | { | ||
| 417 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 418 | ✗ | state = pls_error; | |
| 419 | ✗ | break; | |
| 420 | } | ||
| 421 |
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)) |
| 422 | { | ||
| 423 | ✗ | result = GATE_RESULT_OUTOFMEMORY; | |
| 424 | ✗ | state = pls_error; | |
| 425 | ✗ | break; | |
| 426 | } | ||
| 427 | 1 | state = pls_palette; | |
| 428 | 1 | break; | |
| 429 | } | ||
| 430 | 3 | case pls_palette: | |
| 431 | { | ||
| 432 | pixmap_color_t colmapping; | ||
| 433 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == decode_pixmap_line(&content, &line)) |
| 434 | { | ||
| 435 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 436 | ✗ | state = pls_error; | |
| 437 | ✗ | break; | |
| 438 | } | ||
| 439 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == parse_color(&colmapping, &info, &content)) |
| 440 | { | ||
| 441 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 442 | ✗ | state = pls_error; | |
| 443 | ✗ | break; | |
| 444 | } | ||
| 445 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == gate_arraylist_add(colors, &colmapping)) |
| 446 | { | ||
| 447 | ✗ | result = GATE_RESULT_OUTOFMEMORY; | |
| 448 | ✗ | state = pls_error; | |
| 449 | ✗ | break; | |
| 450 | } | ||
| 451 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if (gate_arraylist_length(colors) >= info.colors) |
| 452 | { | ||
| 453 | 1 | state = pls_content; | |
| 454 | } | ||
| 455 | 3 | break; | |
| 456 | } | ||
| 457 | 32 | case pls_content: | |
| 458 | { | ||
| 459 | gate_color_t* ptr_pixel; | ||
| 460 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (NULL == decode_pixmap_line(&content, &line)) |
| 461 | { | ||
| 462 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 463 | ✗ | state = pls_error; | |
| 464 | ✗ | break; | |
| 465 | } | ||
| 466 | 32 | ptr_pixel = (gate_color_t*)gate_rasterimage_get_line_ptr(image, y); | |
| 467 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (ptr_pixel) |
| 468 | { | ||
| 469 | 32 | parse_image_line(ptr_pixel, &info, colors, &content); | |
| 470 | } | ||
| 471 | 32 | ++y; | |
| 472 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
|
32 | if (y >= info.height) |
| 473 | { | ||
| 474 | 1 | state = pls_end; | |
| 475 | } | ||
| 476 | 32 | break; | |
| 477 | } | ||
| 478 | ✗ | case pls_end: | |
| 479 | case pls_error: | ||
| 480 | { | ||
| 481 | ✗ | break; | |
| 482 | } | ||
| 483 | } | ||
| 484 | 37 | gate_string_release(&content); | |
| 485 | } | ||
| 486 | |||
| 487 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(result); |
| 488 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (state != pls_end) |
| 489 | { | ||
| 490 | ✗ | gate_rasterimage_release(image); | |
| 491 | ✗ | result = GATE_RESULT_INVALIDSTATE; | |
| 492 | } | ||
| 493 | } while (0); | ||
| 494 | |||
| 495 | 1 | gate_arraylist_release(colors); | |
| 496 | |||
| 497 | 1 | return result; | |
| 498 | } | ||
| 499 | |||
| 500 | ✗ | static void copy_rgba(gate_uint8_t* dst, gate_uint8_t const* src, gate_size_t len) | |
| 501 | { | ||
| 502 | ✗ | gate_mem_copy(dst, src, len * 4); | |
| 503 | ✗ | } | |
| 504 | |||
| 505 | static gate_string_t const pix_chars = GATE_STRING_INIT_STATIC(" _.,:;~#=+-*/|^!$%&()[]{}<>@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); | ||
| 506 | |||
| 507 | 3 | static void print_color(gate_stream_t* strm, gate_uint32_t col, gate_uint16_t n) | |
| 508 | { | ||
| 509 | 3 | char const* ptr = gate_string_ptr(&pix_chars, 0); | |
| 510 | 3 | const gate_size_t ccount = gate_string_length(&pix_chars); | |
| 511 | 3 | gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)n / (gate_size_t)ccount); | |
| 512 | 3 | gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)n % (gate_size_t)ccount); | |
| 513 | gate_color_t c; | ||
| 514 | 3 | c.rgba = col; | |
| 515 | |||
| 516 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ndx1 >= ccount) |
| 517 | { | ||
| 518 | ✗ | ndx1 = 0; | |
| 519 | } | ||
| 520 | |||
| 521 | 6 | gate_stream_print(strm, | |
| 522 | GATE_PRINT_CSTR, "\"", | ||
| 523 | 3 | GATE_PRINT_CHAR, ptr[ndx1], | |
| 524 | 3 | GATE_PRINT_CHAR, ptr[ndx2], | |
| 525 | GATE_PRINT_CSTR, " g ", | ||
| 526 | GATE_PRINT_END); | ||
| 527 | |||
| 528 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (c.a < 128) |
| 529 | { | ||
| 530 | 1 | gate_stream_print(strm, | |
| 531 | GATE_PRINT_CSTR, "None", | ||
| 532 | GATE_PRINT_END); | ||
| 533 | } | ||
| 534 | else | ||
| 535 | { | ||
| 536 | 6 | gate_stream_print(strm, | |
| 537 | GATE_PRINT_CSTR, "#", | ||
| 538 | 2 | GATE_PRINT_H8, c.r, | |
| 539 | 2 | GATE_PRINT_H8, c.g, | |
| 540 | 2 | GATE_PRINT_H8, c.b, | |
| 541 | GATE_PRINT_END); | ||
| 542 | } | ||
| 543 | |||
| 544 | 3 | gate_stream_print(strm, | |
| 545 | GATE_PRINT_CSTR, "\",\n", | ||
| 546 | GATE_PRINT_END); | ||
| 547 | 3 | } | |
| 548 | |||
| 549 | 1 | gate_result_t gate_pixmapimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags) | |
| 550 | { | ||
| 551 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 552 | 1 | gate_map_t col_map = GATE_INIT_EMPTY; | |
| 553 | GATE_UNUSED_ARG(flags); | ||
| 554 | |||
| 555 | do | ||
| 556 | { | ||
| 557 | 1 | char const* ptr = gate_string_ptr(&pix_chars, 0); | |
| 558 | 1 | const gate_size_t ccount = gate_string_length(&pix_chars); | |
| 559 | 1 | gate_uint16_t ncol = 0; | |
| 560 | unsigned y, x; | ||
| 561 | gate_uint8_t const* ptr_image_line; | ||
| 562 | 1 | gate_color_converter_t line_writer = NULL; | |
| 563 | gate_uint8_t buffer[4096]; | ||
| 564 | 1 | gate_uint8_t* ptr_buffer = &buffer[0]; | |
| 565 | gate_color_t const* ptr_pixel; | ||
| 566 | gate_uint16_t col_count; | ||
| 567 | gate_color_t c; | ||
| 568 | gate_map_iterator_t col_iter; | ||
| 569 | |||
| 570 |
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) |
| 571 | { | ||
| 572 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGBA: | |
| 573 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB32: line_writer = ©_rgba; break; | |
| 574 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGRA: | |
| 575 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR32: line_writer = &gate_color_convert_rgba_from_bgra_32; break; | |
| 576 | |||
| 577 | 1 | case GATE_IMAGE_PIXELFORMAT_RGB24: line_writer = &gate_color_convert_rgba_from_rgb_24; break; | |
| 578 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR24: line_writer = &gate_color_convert_rgba_from_bgr_24; break; | |
| 579 | |||
| 580 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB555: line_writer = &gate_color_convert_rgba_from_rgb_15; break; | |
| 581 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB565: line_writer = &gate_color_convert_rgba_from_rgb_16; break; | |
| 582 | ✗ | case GATE_IMAGE_PIXELFORMAT_ARGB4: line_writer = &gate_color_convert_rgba_from_argb_16; break; | |
| 583 | ✗ | case GATE_IMAGE_PIXELFORMAT_YUV2: line_writer = &gate_color_convert_rgba_from_yuv2_16; break; | |
| 584 | |||
| 585 | ✗ | case GATE_IMAGE_PIXELFORMAT_PAL8: line_writer = &gate_color_convert_rgba_from_pal_8; break; | |
| 586 | ✗ | case GATE_IMAGE_PIXELFORMAT_GRAY8: line_writer = &gate_color_convert_rgba_from_gray_8; break; | |
| 587 | ✗ | default: break; | |
| 588 | } | ||
| 589 | |||
| 590 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (line_writer == NULL) |
| 591 | { | ||
| 592 | ✗ | ret = GATE_RESULT_NOTSUPPORTED; | |
| 593 | ✗ | break; | |
| 594 | } | ||
| 595 | |||
| 596 | 1 | c.rgba = 0; | |
| 597 | 1 | gate_map_create(&col_map, gate_compare_uint32, sizeof(gate_uint32_t), NULL, NULL, sizeof(gate_uint16_t), NULL, NULL); | |
| 598 | 1 | gate_map_add(&col_map, &c.rgba, &ncol); | |
| 599 | 1 | ++ncol; | |
| 600 | |||
| 601 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
| 602 | { | ||
| 603 | 32 | ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
| 604 | 32 | line_writer(ptr_buffer, ptr_image_line, image->width); | |
| 605 | 32 | ptr_pixel = (gate_color_t const*)ptr_buffer; | |
| 606 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != image->width; ++x) |
| 607 | { | ||
| 608 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2046 times.
|
2048 | if (NULL == gate_map_get(&col_map, &ptr_pixel[x].rgba)) |
| 609 | { | ||
| 610 | 2 | gate_map_add(&col_map, &ptr_pixel[x].rgba, &ncol); | |
| 611 | 2 | ++ncol; | |
| 612 | } | ||
| 613 | } | ||
| 614 | } | ||
| 615 | |||
| 616 | 1 | col_count = (gate_uint16_t)gate_map_count(&col_map); | |
| 617 | |||
| 618 | 3 | gate_stream_print(deststream, | |
| 619 | GATE_PRINT_CSTR, "/* XPM */\nstatic char const * gate_icon_brick_xpm[] = {\n\"", | ||
| 620 | 1 | GATE_PRINT_UI16, (gate_uint16_t)image->width, | |
| 621 | GATE_PRINT_CSTR, " ", | ||
| 622 | 1 | GATE_PRINT_UI16, (gate_uint16_t)image->height, | |
| 623 | GATE_PRINT_CSTR, " ", | ||
| 624 | GATE_PRINT_UI16, (gate_uint16_t)col_count, | ||
| 625 | GATE_PRINT_CSTR, " ", | ||
| 626 | GATE_PRINT_UI16, (gate_uint16_t)2, | ||
| 627 | GATE_PRINT_CSTR, "\",\n", | ||
| 628 | GATE_PRINT_END | ||
| 629 | ); | ||
| 630 | |||
| 631 | 1 | col_iter = gate_map_first(&col_map); | |
| 632 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | while (col_iter) |
| 633 | { | ||
| 634 | 3 | print_color(deststream, | |
| 635 | 3 | *(gate_uint32_t const*)gate_map_iterator_key(col_iter), | |
| 636 | 3 | *(gate_uint16_t const*)gate_map_iterator_value(col_iter)); | |
| 637 | 3 | col_iter = gate_map_iterator_next(col_iter); | |
| 638 | } | ||
| 639 | |||
| 640 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
| 641 | { | ||
| 642 | 32 | ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
| 643 | 32 | line_writer(ptr_buffer, ptr_image_line, image->width); | |
| 644 | 32 | ptr_pixel = (gate_color_t const*)ptr_buffer; | |
| 645 | 32 | gate_stream_print(deststream, GATE_PRINT_CSTR, "\"", GATE_PRINT_END); | |
| 646 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != image->width; ++x) |
| 647 | { | ||
| 648 | 2048 | gate_uint16_t const* ptr_n = gate_map_get_value(&col_map, &ptr_pixel[x].rgba); | |
| 649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
|
2048 | if (ptr_n == NULL) |
| 650 | { | ||
| 651 | ✗ | gate_stream_print(deststream, GATE_PRINT_CSTR, " ", GATE_PRINT_END); | |
| 652 | } | ||
| 653 | else | ||
| 654 | { | ||
| 655 | 2048 | gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)*ptr_n / (gate_size_t)ccount); | |
| 656 | 2048 | gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)*ptr_n % (gate_size_t)ccount); | |
| 657 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
|
2048 | if (ndx1 >= ccount) |
| 658 | { | ||
| 659 | ✗ | ndx1 = 0; | |
| 660 | } | ||
| 661 | 4096 | gate_stream_print(deststream, | |
| 662 | 2048 | GATE_PRINT_CHAR, ptr[ndx1], | |
| 663 | 2048 | GATE_PRINT_CHAR, ptr[ndx2], | |
| 664 | GATE_PRINT_END); | ||
| 665 | } | ||
| 666 | } | ||
| 667 | 32 | gate_stream_print(deststream, GATE_PRINT_CSTR, "\",\n", GATE_PRINT_END); | |
| 668 | } | ||
| 669 | 1 | gate_stream_print(deststream, | |
| 670 | GATE_PRINT_CSTR, "};\n", | ||
| 671 | GATE_PRINT_END | ||
| 672 | ); | ||
| 673 | |||
| 674 | 1 | ret = GATE_RESULT_OK; | |
| 675 | |||
| 676 | } while (0); | ||
| 677 | |||
| 678 | 1 | gate_map_destroy(&col_map); | |
| 679 | 1 | return ret; | |
| 680 | } | ||
| 681 | |||
| 682 |