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_mem_clear(col, sizeof(gate_color_t)); | |
299 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (str[0] == '#') |
300 | { | ||
301 | 2 | gate_str_parse_hex_byte(&str[1], &col->r); | |
302 | 2 | gate_str_parse_hex_byte(&str[3], &col->g); | |
303 | 2 | gate_str_parse_hex_byte(&str[5], &col->b); | |
304 | 2 | col->a = 255; | |
305 | } | ||
306 | 3 | } | |
307 | |||
308 | 3 | static pixmap_color_t* parse_color(pixmap_color_t* col, pixmap_info_t const* info, gate_string_t const* data) | |
309 | { | ||
310 | char type_chr; | ||
311 | 3 | gate_mem_clear(col, sizeof(pixmap_color_t)); | |
312 | 3 | gate_mem_copy(&col->token[0], data->str, info->bytes_per_pixel); | |
313 | 3 | type_chr = data->str[info->bytes_per_pixel + 1]; | |
314 |
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')) |
315 | { | ||
316 | 3 | parse_color_token(&col->col, &data->str[info->bytes_per_pixel + 3], data->length - info->bytes_per_pixel - 3); | |
317 | } | ||
318 | 3 | return col; | |
319 | } | ||
320 | |||
321 | 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) | |
322 | { | ||
323 | 32 | gate_size_t len = gate_string_length(data) / info->bytes_per_pixel; | |
324 | gate_size_t ndx; | ||
325 | 32 | pixmap_color_t const* ptr_colentries = (pixmap_color_t const*)gate_arraylist_get(colors, 0); | |
326 | pixmap_color_t const* ptr_curentry; | ||
327 | 32 | gate_size_t colcount = gate_arraylist_length(colors); | |
328 | char const* ptr_curdata; | ||
329 | unsigned x; | ||
330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (len > info->width) |
331 | { | ||
332 | ✗ | len = info->width; | |
333 | } | ||
334 | 32 | ptr_curdata = data->str; | |
335 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != len; ++x, ++ptr_pixels) |
336 | { | ||
337 | 2048 | ptr_curentry = ptr_colentries; | |
338 |
1/2✓ Branch 0 taken 6020 times.
✗ Branch 1 not taken.
|
6020 | for (ndx = 0; ndx != colcount; ++ndx, ++ptr_curentry) |
339 | { | ||
340 |
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)) |
341 | { | ||
342 | 2048 | *ptr_pixels = ptr_curentry->col; | |
343 | 2048 | break; | |
344 | } | ||
345 | } | ||
346 | 2048 | ptr_curdata += info->bytes_per_pixel; | |
347 | } | ||
348 | 32 | } | |
349 | |||
350 | 1 | gate_result_t gate_pixmapimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags) | |
351 | { | ||
352 | 1 | gate_result_t result = GATE_RESULT_FAILED; | |
353 | char linebuffer[4096]; | ||
354 | gate_size_t linelen; | ||
355 | 1 | pixmap_loader_state_t state = pls_header; | |
356 | 1 | gate_string_t line = GATE_STRING_INIT_EMPTY; | |
357 | 1 | gate_string_t content = GATE_STRING_INIT_EMPTY; | |
358 | 1 | pixmap_info_t info = GATE_INIT_EMPTY; | |
359 | 1 | gate_arraylist_t colors = NULL; | |
360 | pixmap_color_t colmapping; | ||
361 | gate_color_t* ptr_pixel; | ||
362 | 1 | unsigned y = 0; | |
363 | GATE_UNUSED_ARG(flags); | ||
364 | |||
365 | do | ||
366 | { | ||
367 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!srcstream || !image) |
368 | { | ||
369 | ✗ | result = GATE_RESULT_INVALIDARG; | |
370 | ✗ | break; | |
371 | } | ||
372 | |||
373 | 1 | colors = gate_arraylist_create(sizeof(pixmap_color_t), NULL, 0, NULL, NULL); | |
374 | |||
375 | 1 | gate_mem_clear(image, sizeof(gate_rasterimage_t)); | |
376 | |||
377 |
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)) |
378 | { | ||
379 | 38 | result = gate_stream_read_line(srcstream, linebuffer, sizeof(linebuffer), &linelen); | |
380 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | GATE_BREAK_IF_FAILED(result); |
381 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (linelen == 0) |
382 | { | ||
383 | ✗ | break; | |
384 | } | ||
385 | |||
386 | 38 | gate_string_create_static_len(&line, linebuffer, linelen); | |
387 | 38 | gate_string_trim(&line, &line); | |
388 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
|
38 | if (0 == gate_string_length(&line)) |
389 | { | ||
390 | ✗ | continue; | |
391 | } | ||
392 |
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, "*/")) |
393 | { | ||
394 | 1 | continue; | |
395 | } | ||
396 | |||
397 |
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) |
398 | { | ||
399 | 1 | case pls_header: | |
400 | { | ||
401 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (gate_string_ends_with_char(&line, '{')) |
402 | { | ||
403 | /* header is skipped -> read info line */ | ||
404 | 1 | state = pls_info; | |
405 | } | ||
406 | 1 | break; | |
407 | } | ||
408 | 1 | case pls_info: | |
409 | { | ||
410 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == decode_pixmap_line(&content, &line)) |
411 | { | ||
412 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
413 | ✗ | state = pls_error; | |
414 | ✗ | break; | |
415 | } | ||
416 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == parse_info(&info, &content)) |
417 | { | ||
418 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
419 | ✗ | state = pls_error; | |
420 | ✗ | break; | |
421 | } | ||
422 |
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)) |
423 | { | ||
424 | ✗ | result = GATE_RESULT_OUTOFMEMORY; | |
425 | ✗ | state = pls_error; | |
426 | ✗ | break; | |
427 | } | ||
428 | 1 | state = pls_palette; | |
429 | 1 | break; | |
430 | } | ||
431 | 3 | case pls_palette: | |
432 | { | ||
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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (NULL == decode_pixmap_line(&content, &line)) |
460 | { | ||
461 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
462 | ✗ | state = pls_error; | |
463 | ✗ | break; | |
464 | } | ||
465 | 32 | ptr_pixel = (gate_color_t*)gate_rasterimage_get_line_ptr(image, y); | |
466 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (ptr_pixel) |
467 | { | ||
468 | 32 | parse_image_line(ptr_pixel, &info, colors, &content); | |
469 | } | ||
470 | 32 | ++y; | |
471 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
|
32 | if (y >= info.height) |
472 | { | ||
473 | 1 | state = pls_end; | |
474 | } | ||
475 | 32 | break; | |
476 | } | ||
477 | ✗ | case pls_end: | |
478 | case pls_error: | ||
479 | { | ||
480 | ✗ | break; | |
481 | } | ||
482 | } | ||
483 | 37 | gate_string_release(&content); | |
484 | } | ||
485 | |||
486 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(result); |
487 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (state != pls_end) |
488 | { | ||
489 | ✗ | gate_rasterimage_release(image); | |
490 | ✗ | result = GATE_RESULT_INVALIDSTATE; | |
491 | } | ||
492 | } while (0); | ||
493 | |||
494 | 1 | gate_arraylist_release(colors); | |
495 | |||
496 | 1 | return result; | |
497 | } | ||
498 | |||
499 | ✗ | static void copy_rgba(gate_uint8_t* dst, gate_uint8_t const* src, gate_size_t len) | |
500 | { | ||
501 | ✗ | gate_mem_copy(dst, src, len * 4); | |
502 | ✗ | } | |
503 | |||
504 | static gate_string_t const pix_chars = GATE_STRING_INIT_STATIC(" _.,:;~#=+-*/|^!$%&()[]{}<>@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); | ||
505 | |||
506 | 3 | static void print_color(gate_stream_t* strm, gate_uint32_t col, gate_uint16_t n) | |
507 | { | ||
508 | 3 | char const* ptr = gate_string_ptr(&pix_chars, 0); | |
509 | 3 | const gate_size_t ccount = gate_string_length(&pix_chars); | |
510 | 3 | gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)n / (gate_size_t)ccount); | |
511 | 3 | gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)n % (gate_size_t)ccount); | |
512 | gate_color_t c; | ||
513 | 3 | c.rgba = col; | |
514 | |||
515 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ndx1 >= ccount) |
516 | { | ||
517 | ✗ | ndx1 = 0; | |
518 | } | ||
519 | |||
520 | 6 | gate_stream_print(strm, | |
521 | GATE_PRINT_CSTR, "\"", | ||
522 | 3 | GATE_PRINT_CHAR, ptr[ndx1], | |
523 | 3 | GATE_PRINT_CHAR, ptr[ndx2], | |
524 | GATE_PRINT_CSTR, " g ", | ||
525 | GATE_PRINT_END); | ||
526 | |||
527 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (c.a < 128) |
528 | { | ||
529 | 1 | gate_stream_print(strm, | |
530 | GATE_PRINT_CSTR, "None", | ||
531 | GATE_PRINT_END); | ||
532 | } | ||
533 | else | ||
534 | { | ||
535 | 6 | gate_stream_print(strm, | |
536 | GATE_PRINT_CSTR, "#", | ||
537 | 2 | GATE_PRINT_H8, c.r, | |
538 | 2 | GATE_PRINT_H8, c.g, | |
539 | 2 | GATE_PRINT_H8, c.b, | |
540 | GATE_PRINT_END); | ||
541 | } | ||
542 | |||
543 | 3 | gate_stream_print(strm, | |
544 | GATE_PRINT_CSTR, "\",\n", | ||
545 | GATE_PRINT_END); | ||
546 | 3 | } | |
547 | |||
548 | 1 | gate_result_t gate_pixmapimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags) | |
549 | { | ||
550 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
551 | 1 | gate_map_t col_map = GATE_INIT_EMPTY; | |
552 | 1 | char const* ptr = gate_string_ptr(&pix_chars, 0); | |
553 | 1 | const gate_size_t ccount = gate_string_length(&pix_chars); | |
554 | 1 | gate_uint16_t ncol = 0; | |
555 | unsigned y, x; | ||
556 | gate_uint8_t const* ptr_image_line; | ||
557 | 1 | gate_color_converter_t line_writer = NULL; | |
558 | gate_uint8_t buffer[4096]; | ||
559 | 1 | gate_uint8_t* ptr_buffer = &buffer[0]; | |
560 | gate_color_t const* ptr_pixel; | ||
561 | gate_uint16_t col_count; | ||
562 | gate_color_t c; | ||
563 | gate_map_iterator_t col_iter; | ||
564 | |||
565 | do | ||
566 | { | ||
567 |
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) |
568 | { | ||
569 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGBA: | |
570 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB32: line_writer = ©_rgba; break; | |
571 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGRA: | |
572 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR32: line_writer = &gate_color_convert_rgba_from_bgra_32; break; | |
573 | |||
574 | 1 | case GATE_IMAGE_PIXELFORMAT_RGB24: line_writer = &gate_color_convert_rgba_from_rgb_24; break; | |
575 | ✗ | case GATE_IMAGE_PIXELFORMAT_BGR24: line_writer = &gate_color_convert_rgba_from_bgr_24; break; | |
576 | |||
577 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB555: line_writer = &gate_color_convert_rgba_from_rgb_15; break; | |
578 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGB565: line_writer = &gate_color_convert_rgba_from_rgb_16; break; | |
579 | ✗ | case GATE_IMAGE_PIXELFORMAT_ARGB4: line_writer = &gate_color_convert_rgba_from_argb_16; break; | |
580 | ✗ | case GATE_IMAGE_PIXELFORMAT_YUV2: line_writer = &gate_color_convert_rgba_from_yuv2_16; break; | |
581 | |||
582 | ✗ | case GATE_IMAGE_PIXELFORMAT_PAL8: line_writer = &gate_color_convert_rgba_from_pal_8; break; | |
583 | ✗ | case GATE_IMAGE_PIXELFORMAT_GRAY8: line_writer = &gate_color_convert_rgba_from_gray_8; break; | |
584 | ✗ | default: | |
585 | ✗ | break; | |
586 | } | ||
587 | |||
588 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (line_writer == NULL) |
589 | { | ||
590 | ✗ | ret = GATE_RESULT_NOTSUPPORTED; | |
591 | ✗ | break; | |
592 | } | ||
593 | |||
594 | 1 | c.rgba = 0; | |
595 | 1 | gate_map_create(&col_map, gate_compare_uint32, sizeof(gate_uint32_t), NULL, NULL, sizeof(gate_uint16_t), NULL, NULL); | |
596 | 1 | gate_map_add(&col_map, &c.rgba, &ncol); | |
597 | 1 | ++ncol; | |
598 | |||
599 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
600 | { | ||
601 | 32 | ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
602 | 32 | line_writer(ptr_buffer, ptr_image_line, image->width); | |
603 | 32 | ptr_pixel = (gate_color_t const*)ptr_buffer; | |
604 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != image->width; ++x) |
605 | { | ||
606 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2046 times.
|
2048 | if (NULL == gate_map_get(&col_map, &ptr_pixel[x].rgba)) |
607 | { | ||
608 | 2 | gate_map_add(&col_map, &ptr_pixel[x].rgba, &ncol); | |
609 | 2 | ++ncol; | |
610 | } | ||
611 | } | ||
612 | } | ||
613 | |||
614 | 1 | col_count = (gate_uint16_t)gate_map_count(&col_map); | |
615 | |||
616 | 3 | gate_stream_print(deststream, | |
617 | GATE_PRINT_CSTR, "/* XPM */\nstatic char const * gate_icon_brick_xpm[] = {\n\"", | ||
618 | 1 | GATE_PRINT_UI16, (gate_uint16_t)image->width, | |
619 | GATE_PRINT_CSTR, " ", | ||
620 | 1 | GATE_PRINT_UI16, (gate_uint16_t)image->height, | |
621 | GATE_PRINT_CSTR, " ", | ||
622 | GATE_PRINT_UI16, (gate_uint16_t)col_count, | ||
623 | GATE_PRINT_CSTR, " ", | ||
624 | GATE_PRINT_UI16, (gate_uint16_t)2, | ||
625 | GATE_PRINT_CSTR, "\",\n", | ||
626 | GATE_PRINT_END | ||
627 | ); | ||
628 | |||
629 | 1 | col_iter = gate_map_first(&col_map); | |
630 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | while (col_iter) |
631 | { | ||
632 | 3 | print_color(deststream, | |
633 | 3 | *(gate_uint32_t const*)gate_map_iterator_key(col_iter), | |
634 | 3 | *(gate_uint16_t const*)gate_map_iterator_value(col_iter)); | |
635 | 3 | col_iter = gate_map_iterator_next(col_iter); | |
636 | } | ||
637 | |||
638 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | for (y = 0; y != image->height; ++y) |
639 | { | ||
640 | 32 | ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y); | |
641 | 32 | line_writer(ptr_buffer, ptr_image_line, image->width); | |
642 | 32 | ptr_pixel = (gate_color_t const*)ptr_buffer; | |
643 | 32 | gate_stream_print(deststream, GATE_PRINT_CSTR, "\"", GATE_PRINT_END); | |
644 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
|
2080 | for (x = 0; x != image->width; ++x) |
645 | { | ||
646 | 2048 | gate_uint16_t const* ptr_n = gate_map_get_value(&col_map, &ptr_pixel[x].rgba); | |
647 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
|
2048 | if (ptr_n == NULL) |
648 | { | ||
649 | ✗ | gate_stream_print(deststream, GATE_PRINT_CSTR, " ", GATE_PRINT_END); | |
650 | } | ||
651 | else | ||
652 | { | ||
653 | 2048 | gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)*ptr_n / (gate_size_t)ccount); | |
654 | 2048 | gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)*ptr_n % (gate_size_t)ccount); | |
655 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
|
2048 | if (ndx1 >= ccount) |
656 | { | ||
657 | ✗ | ndx1 = 0; | |
658 | } | ||
659 | 4096 | gate_stream_print(deststream, | |
660 | 2048 | GATE_PRINT_CHAR, ptr[ndx1], | |
661 | 2048 | GATE_PRINT_CHAR, ptr[ndx2], | |
662 | GATE_PRINT_END); | ||
663 | } | ||
664 | } | ||
665 | 32 | gate_stream_print(deststream, GATE_PRINT_CSTR, "\",\n", GATE_PRINT_END); | |
666 | } | ||
667 | 1 | gate_stream_print(deststream, | |
668 | GATE_PRINT_CSTR, "};\n", | ||
669 | GATE_PRINT_END | ||
670 | ); | ||
671 | |||
672 | 1 | ret = GATE_RESULT_OK; | |
673 | |||
674 | } while (0); | ||
675 | |||
676 | 1 | gate_map_destroy(&col_map); | |
677 | 1 | return ret; | |
678 | } | ||
679 | |||
680 |