GCC Code Coverage Report


Directory: src/gate/
File: src/gate/graphics/bitmapimages.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 255 413 61.7%
Functions: 6 7 85.7%
Branches: 61 127 48.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2026, 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/bitmapimages.h"
30 #include "gate/results.h"
31 #include "gate/serializers.h"
32 #include "gate/debugging.h"
33 #include "gate/mathematics.h"
34 #include "gate/localbuffer.h"
35
36 typedef struct gate_bmp_header_class
37 {
38 gate_uint32_t header_sz;
39 gate_int32_t width;
40 gate_int32_t height;
41 gate_uint16_t nplanes;
42 gate_uint16_t bitspp;
43 gate_uint32_t compress_type;
44 gate_uint32_t bmp_bytesz;
45 gate_int32_t hres;
46 gate_int32_t vres;
47 gate_uint32_t ncolors;
48 gate_uint32_t nimpcolors;
49 } gate_bmp_header_t;
50
51
52
53 19 static gate_result_t load_bitmap_as_rgba_image(
54 gate_stream_t* srcstream,
55 gate_bmp_header_t const* ptr_header,
56 char const* palette,
57 gate_size_t linelen,
58 gate_rasterimage_t* image)
59 {
60 19 gate_result_t result = GATE_RESULT_FAILED;
61 19 const unsigned width = ptr_header->width < 0 ? (unsigned)(-ptr_header->width) : (unsigned)ptr_header->width;
62 19 const unsigned height = ptr_header->height < 0 ? (unsigned)(-ptr_header->height) : (unsigned)ptr_header->height;
63 gate_rasterimage_t raster;
64 gate_index_t lineadd;
65 gate_color_t* ptrpixel;
66 gate_color_t* ptrline;
67 unsigned x, y, z;
68 gate_size_t lenread;
69 gate_localbuffer_t buffer;
70 19 char* ptr_buffer = NULL;
71 gate_uint8_t const* ptrbytes;
72 gate_uint8_t const* ptrpal;
73 19 gate_bool_t all_alphas_are_zero = true;
74
75 do
76 {
77 19 gate_mem_clear(&raster, sizeof(raster));
78 19 ptr_buffer = (char*)gate_localbuffer_create(&buffer, linelen);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ptr_buffer == NULL)
80 {
81 ✗ result = GATE_RESULT_OUTOFMEMORY;
82 ✗ break;
83 }
84
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_RGBA, width, height, NULL))
86 {
87 ✗ result = GATE_RESULT_OUTOFMEMORY;
88 ✗ break;
89 }
90
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ptr_header->height < 0)
92 {
93 ✗ ptrpixel = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
94 ✗ lineadd = width;
95 }
96 else
97 {
98 19 ptrpixel = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, (height - 1));
99 19 lineadd = -(gate_index_t)width;
100 }
101
102 19 result = GATE_RESULT_OK;
103
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 19 times.
1299 for (y = 0; y != height; ++y)
104 {
105
106 1280 result = gate_stream_read_block(srcstream, ptr_buffer, linelen, &lenread);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 if (GATE_FAILED(result))
108 {
109 ✗ break;
110 }
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 if (lenread < linelen)
112 {
113 ✗ result = GATE_RESULT_ENDOFSTREAM;
114 ✗ break;
115 }
116 1280 ptrline = ptrpixel;
117 1280 ptrbytes = (gate_uint8_t const*)ptr_buffer;
118
119
2/2
✓ Branch 0 taken 102016 times.
✓ Branch 1 taken 1280 times.
103296 for (x = 0; x < width; ++x)
120 {
121
5/7
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 512 times.
✓ Branch 3 taken 1024 times.
✓ Branch 4 taken 8192 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 92160 times.
102016 switch (ptr_header->bitspp)
122 {
123 ✗ default:
124 {
125 ✗ ptrline->b = 0;
126 ✗ ptrline->g = 0;
127 ✗ ptrline->r = 0;
128 ✗ ptrline->a = 255;
129 ✗ ++ptrline;
130 ✗ break;
131 }
132 128 case 1:
133 {
134
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 128 times.
1152 for (z = 0; z != 8; ++z)
135 {
136
2/2
✓ Branch 0 taken 798 times.
✓ Branch 1 taken 226 times.
1024 if (*ptrbytes & (gate_uint8_t)(1 << (7 - z)))
137 {
138 798 ptrpal = (gate_uint8_t const*)&palette[4];
139 }
140 else
141 {
142 226 ptrpal = (gate_uint8_t const*)&palette[0];
143 }
144 1024 ptrline->b = ptrpal[0];
145 1024 ptrline->g = ptrpal[1];
146 1024 ptrline->r = ptrpal[2];
147 1024 ptrline->a = ptrpal[3];
148 1024 all_alphas_are_zero &= ((ptrline->a == 0) ? true : false);
149 1024 ++ptrline;
150 }
151 128 ++ptrbytes;
152 128 x += 7;
153 128 break;
154 }
155 512 case 4:
156 {
157 512 ptrpal = (gate_uint8_t const*)&palette[4 * (unsigned)(*ptrbytes >> 4)];
158 512 ptrline->b = ptrpal[0];
159 512 ptrline->g = ptrpal[1];
160 512 ptrline->r = ptrpal[2];
161 512 ptrline->a = ptrpal[3];
162 512 all_alphas_are_zero &= ((ptrline->a == 0) ? true : false);
163 512 ++ptrline;
164
165 512 ptrpal = (gate_uint8_t const*)&palette[4 * (unsigned)(*ptrbytes & 0x0f)];
166 512 ptrline->b = ptrpal[0];
167 512 ptrline->g = ptrpal[1];
168 512 ptrline->r = ptrpal[2];
169 512 ptrline->a = ptrpal[3];
170 512 all_alphas_are_zero &= ((ptrline->a == 0) ? true : false);
171 512 ++ptrline;
172
173 512 ++ptrbytes;
174 512 ++x;
175 512 break;
176 }
177 1024 case 8:
178 {
179 1024 ptrpal = (gate_uint8_t const*)&palette[4 * (gate_size_t)*ptrbytes];
180 1024 ++ptrbytes;
181 1024 ptrline->b = ptrpal[0];
182 1024 ptrline->g = ptrpal[1];
183 1024 ptrline->r = ptrpal[2];
184 1024 ptrline->a = ptrpal[3];
185 1024 all_alphas_are_zero &= ((ptrline->a == 0) ? true : false);
186 1024 ++ptrline;
187 1024 break;
188 }
189 8192 case 15:
190 case 16:
191 {
192 8192 ptrline->b = (ptrbytes[0] & 0x1f) << 3;
193 8192 ptrline->g = ((ptrbytes[0] >> 5) | ((ptrbytes[1] & 0x03) << 3)) << 3;
194 8192 ptrline->r = (ptrbytes[1] & 0x7c) << 1;
195 8192 ptrline->a = 255;
196 8192 ++ptrline;
197 8192 ptrbytes += 2;
198 8192 break;
199 }
200 ✗ case 24:
201 {
202 ✗ ptrline->b = *ptrbytes++;
203 ✗ ptrline->g = *ptrbytes++;
204 ✗ ptrline->r = *ptrbytes++;
205 ✗ ptrline->a = 255;
206 ✗ ++ptrline;
207 ✗ break;
208 }
209 92160 case 32:
210 {
211 92160 ptrline->b = *ptrbytes++;
212 92160 ptrline->g = *ptrbytes++;
213 92160 ptrline->r = *ptrbytes++;
214 92160 ptrline->a = *ptrbytes++;
215 92160 all_alphas_are_zero &= ((ptrline->a == 0) ? true : false);
216 92160 ++ptrline;
217 92160 break;
218 }
219 }
220 } /* for(x) */
221
222 1280 ptrpixel += lineadd;
223 } /* for(y) */
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 GATE_BREAK_IF_FAILED(result);
226
227
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 if (all_alphas_are_zero)
228 {
229 /* all alpha values are zero means, that
230 the soruce stream had no alpha-values stored
231 and all the reserved bits were zero.
232 Therfore we just patch all alpha-values to 255
233 */
234 5 gate_size_t pixelcount = (gate_size_t)raster.width * (gate_size_t)raster.height;
235 5 ptrpixel = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
236
2/2
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 5 times.
11269 while (pixelcount-- != 0)
237 {
238 11264 ptrpixel->a = 255;
239 11264 ++ptrpixel;
240 }
241 }
242
243 /* success case, transfer local image object into output object */
244 19 gate_mem_copy(image, &raster, sizeof(raster));
245 19 gate_mem_clear(&raster, sizeof(raster));
246 19 result = GATE_RESULT_OK;
247 } while (0);
248
249 19 gate_localbuffer_destroy(&buffer);
250 19 gate_rasterimage_release(&raster);
251
252 19 return result;
253 }
254
255
256 ✗ static gate_result_t load_bitmap_generic(
257 gate_stream_t* srcstream,
258 gate_bmp_header_t const* ptr_header,
259 char const* palette,
260 gate_size_t linelen,
261 gate_enumint_t pixel_format,
262 gate_rasterimage_t* image)
263 {
264 ✗ gate_result_t result = GATE_RESULT_FAILED;
265 ✗ const unsigned width = ptr_header->width < 0 ? (unsigned)(-ptr_header->width) : (unsigned)ptr_header->width;
266 ✗ const unsigned height = ptr_header->height < 0 ? (unsigned)(-ptr_header->height) : (unsigned)ptr_header->height;
267 gate_rasterimage_t raster;
268 unsigned x, y, z;
269 gate_size_t lenread;
270 gate_color_t current_pixel;
271 char buffer[8192];
272 ✗ char* ptr_buffer = &buffer[0];
273 gate_uint8_t const* ptrpal;
274
275 do
276 {
277 ✗ gate_mem_clear(&raster, sizeof(raster));
278
279 ✗ if (NULL == gate_rasterimage_create(&raster, pixel_format, width, height, NULL))
280 {
281 ✗ result = GATE_RESULT_OUTOFMEMORY;
282 ✗ break;
283 }
284
285 ✗ if (linelen > sizeof(buffer))
286 {
287 ✗ ptr_buffer = gate_mem_alloc(linelen);
288 ✗ if (NULL == ptr_buffer)
289 {
290 ✗ result = GATE_RESULT_OUTOFMEMORY;
291 ✗ break;
292 }
293 }
294
295 ✗ result = GATE_RESULT_OK;
296 ✗ for (y = 0; y != height; ++y)
297 {
298 ✗ const unsigned dest_y = (ptr_header->height < 0) ? y : height - y - 1;
299 ✗ gate_uint8_t const* ptr_line_bytes = (gate_uint8_t const*)ptr_buffer;
300
301 ✗ result = gate_stream_read_block(srcstream, ptr_buffer, linelen, &lenread);
302 ✗ if (GATE_FAILED(result))
303 {
304 ✗ break;
305 }
306 ✗ if (lenread < linelen)
307 {
308 ✗ result = GATE_RESULT_ENDOFSTREAM;
309 ✗ break;
310 }
311
312 ✗ for (x = 0; x < width; ++x)
313 {
314 ✗ switch (ptr_header->bitspp)
315 {
316 ✗ default:
317 {
318 ✗ current_pixel.b = 0;
319 ✗ current_pixel.g = 0;
320 ✗ current_pixel.r = 0;
321 ✗ current_pixel.a = 255;
322 ✗ ++ptr_line_bytes;
323 ✗ break;
324 }
325 ✗ case 1:
326 {
327 ✗ for (z = 0; z != 8; ++z)
328 {
329 ✗ if (*ptr_line_bytes & (gate_uint8_t)(1 << (7 - z)))
330 {
331 ✗ ptrpal = (gate_uint8_t const*)&palette[4];
332 }
333 else
334 {
335 ✗ ptrpal = (gate_uint8_t const*)&palette[0];
336 }
337 ✗ current_pixel.b = ptrpal[0];
338 ✗ current_pixel.g = ptrpal[1];
339 ✗ current_pixel.r = ptrpal[2];
340 ✗ current_pixel.a = 255;
341
342 ✗ gate_rasterimage_set_pixel(&raster, x + z, dest_y, &current_pixel);
343 }
344 ✗ ++ptr_line_bytes;
345 ✗ x += 7;
346 ✗ break;
347 }
348 ✗ case 4:
349 {
350 ✗ ptrpal = (gate_uint8_t const*)&palette[4 * (unsigned)(*ptr_line_bytes >> 4)];
351 ✗ current_pixel.b = ptrpal[0];
352 ✗ current_pixel.g = ptrpal[1];
353 ✗ current_pixel.r = ptrpal[2];
354 ✗ current_pixel.a = 255;
355 ✗ gate_rasterimage_set_pixel(&raster, x, dest_y, &current_pixel);
356 ✗ ++ptr_line_bytes;
357 ✗ ++x;
358
359 ✗ ptrpal = (gate_uint8_t const*)&palette[4 * (unsigned)(*ptr_line_bytes & 0x0f)];
360 ✗ current_pixel.b = ptrpal[0];
361 ✗ current_pixel.g = ptrpal[1];
362 ✗ current_pixel.r = ptrpal[2];
363 ✗ current_pixel.a = 255;
364 ✗ ++ptr_line_bytes;
365 ✗ break;
366 }
367 ✗ case 8:
368 {
369 ✗ ptrpal = (gate_uint8_t const*)&palette[4 * (gate_size_t)*ptr_line_bytes];
370 ✗ current_pixel.b = ptrpal[0];
371 ✗ current_pixel.g = ptrpal[1];
372 ✗ current_pixel.r = ptrpal[2];
373 ✗ current_pixel.a = 255;
374 ✗ ++ptr_line_bytes;
375 ✗ break;
376 }
377 ✗ case 15:
378 case 16:
379 {
380 ✗ current_pixel.b = (ptr_line_bytes[0] & 0x1f) << 3;
381 ✗ current_pixel.g = ((ptr_line_bytes[0] >> 5) | ((ptr_line_bytes[1] & 0x03) << 3)) << 3;
382 ✗ current_pixel.r = (ptr_line_bytes[1] & 0x7c) << 1;
383 ✗ current_pixel.a = 255;
384 ✗ ptr_line_bytes += 2;
385 ✗ break;
386 }
387 ✗ case 24:
388 {
389 ✗ current_pixel.b = ptr_line_bytes[0];
390 ✗ current_pixel.g = ptr_line_bytes[1];
391 ✗ current_pixel.r = ptr_line_bytes[2];
392 ✗ current_pixel.a = 255;
393 ✗ ptr_line_bytes += 3;
394 ✗ break;
395 }
396 ✗ case 32:
397 {
398 ✗ current_pixel.b = ptr_line_bytes[0];
399 ✗ current_pixel.g = ptr_line_bytes[1];
400 ✗ current_pixel.r = ptr_line_bytes[2];
401 ✗ current_pixel.a = ptr_line_bytes[3];
402 ✗ ptr_line_bytes += 4;
403 ✗ break;
404 }
405 }
406
407 ✗ gate_rasterimage_set_pixel(&raster, x, y, &current_pixel);
408 } /* for(x) */
409 } /* for(y) */
410 ✗ GATE_BREAK_IF_FAILED(result);
411
412 /* success case, transfer local image object into output object */
413 ✗ gate_mem_copy(image, &raster, sizeof(raster));
414 ✗ gate_mem_clear(&raster, sizeof(raster));
415 ✗ result = GATE_RESULT_OK;
416 } while (0);
417
418 ✗ if ((ptr_buffer != NULL) & (ptr_buffer != &buffer[0]))
419 {
420 ✗ gate_mem_dealloc(ptr_buffer);
421 }
422 ✗ gate_rasterimage_release(&raster);
423
424 ✗ return result;
425 }
426
427
428 19 gate_result_t gate_bitmapimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
429 {
430 static gate_size_t const header_min_size = 54;
431 char palette[1024];
432 19 char* ptr_buffer = &palette[0];
433 gate_result_t result;
434 gate_uint32_t filesize;
435 gate_uint32_t creatorid;
436 gate_uint32_t bmpoffset;
437 gate_size_t bufferused;
438 gate_bmp_header_t header;
439
440 gate_size_t linelen;
441 gate_size_t remain;
442 19 const gate_enumint_t load_pixel_format = flags & GATE_IMAGE_FLAG_DEPTHMASK;
443
444 (void)flags;
445
446 do
447 {
448 19 gate_rasterimage_create(image, GATE_IMAGE_PIXELFORMAT_DEFAULT, 0, 0, NULL);
449
450 19 result = gate_stream_read_block(srcstream, ptr_buffer, header_min_size, &bufferused);
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (GATE_FAILED(result))
452 {
453 ✗ result = GATE_RESULT_INVALIDINPUT;
454 ✗ break;
455 }
456
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 if (bufferused < header_min_size || (ptr_buffer[0] != 'B') || (ptr_buffer[1] != 'M'))
457 {
458 ✗ result = GATE_RESULT_INVALIDHEADER;
459 ✗ break;
460 }
461 19 gate_deserialize_uint32_l(&ptr_buffer[2], 4, &filesize);
462 19 gate_deserialize_uint32_l(&ptr_buffer[6], 4, &creatorid);
463 19 gate_deserialize_uint32_l(&ptr_buffer[10], 4, &bmpoffset);
464
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (bmpoffset < header_min_size)
466 {
467 ✗ result = GATE_RESULT_INVALIDHEADER;
468 ✗ break;
469 }
470
471 19 gate_deserialize_uint32_l(&ptr_buffer[14], 4, &header.header_sz);
472 19 gate_deserialize_int32_l(&ptr_buffer[18], 4, &header.width);
473 19 gate_deserialize_int32_l(&ptr_buffer[22], 4, &header.height);
474 19 gate_deserialize_uint16_l(&ptr_buffer[26], 2, &header.nplanes);
475 19 gate_deserialize_uint16_l(&ptr_buffer[28], 2, &header.bitspp);
476 19 gate_deserialize_uint32_l(&ptr_buffer[30], 4, &header.compress_type);
477 19 gate_deserialize_uint32_l(&ptr_buffer[34], 4, &header.bmp_bytesz);
478 19 gate_deserialize_int32_l(&ptr_buffer[38], 4, &header.hres);
479 19 gate_deserialize_int32_l(&ptr_buffer[42], 4, &header.vres);
480 19 gate_deserialize_uint32_l(&ptr_buffer[46], 4, &header.ncolors);
481 19 gate_deserialize_uint32_l(&ptr_buffer[50], 4, &header.nimpcolors);
482
483 19 result = GATE_RESULT_OK;
484
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 switch (header.compress_type)
485 {
486 19 case 0: /* BI_RGB */
487 {
488 /* supported */
489 19 break;
490 }
491 ✗ case 1: /* BI_RLE8 */
492 case 2: /* BI_RLE4 */
493 case 3: /* BI_BITFIELDS */
494 case 4: /* BI_JPEG */
495 case 5: /* BI_PNG */
496 default: /* unknown */
497 {
498 /* unsupported encoding */
499 ✗ result = GATE_RESULT_NOTSUPPORTED;
500 ✗ break;
501 }
502 }
503
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 GATE_BREAK_IF_FAILED(result);
505
506
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
19 if (bmpoffset > header_min_size)
507 {
508 3 result = gate_stream_read_block(srcstream, palette, bmpoffset - header_min_size, &bufferused);
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (GATE_FAILED(result))
510 {
511 ✗ result = GATE_RESULT_INVALIDHEADER;
512 ✗ break;
513 }
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (bufferused < (bmpoffset - header_min_size))
515 {
516 ✗ result = GATE_RESULT_INVALIDINPUT;
517 ✗ break;
518 }
519 }
520
521 19 linelen = 0;
522
523
5/7
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
✗ Branch 6 not taken.
19 switch (header.bitspp)
524 {
525 1 case 1:
526 {
527 1 linelen = (header.width + 7) / 8;
528 1 break;
529 }
530 1 case 4:
531 {
532 1 linelen = (header.width + 1) / 2;
533 1 break;
534 }
535 1 case 8:
536 {
537 1 linelen = header.width;
538 1 break;
539 }
540 2 case 15:
541 case 16:
542 {
543 2 linelen = (gate_size_t)gate_math_abs_i32(header.width) * 2;
544 2 break;
545 }
546 ✗ case 24:
547 {
548 ✗ linelen = (gate_size_t)gate_math_abs_i32(header.width) * 3;
549 ✗ break;
550 }
551 14 case 32:
552 {
553 14 linelen = (gate_size_t)gate_math_abs_i32(header.width) * 4;
554 14 break;
555 }
556 }
557
558 19 remain = linelen % 4;
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (remain != 0)
560 {
561 ✗ linelen += 4 - remain;
562 }
563
564
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
19 switch (load_pixel_format)
565 {
566 ✗ case GATE_IMAGE_FLAG_DEPTH_8GRAY: result = load_bitmap_generic(srcstream, &header, palette, linelen, GATE_IMAGE_PIXELFORMAT_GRAY8, image); break;
567 ✗ case GATE_IMAGE_FLAG_DEPTH_24RGB: result = load_bitmap_generic(srcstream, &header, palette, linelen, GATE_IMAGE_PIXELFORMAT_RGB24, image); break;
568 ✗ case GATE_IMAGE_FLAG_DEPTH_15RGB: result = load_bitmap_generic(srcstream, &header, palette, linelen, GATE_IMAGE_PIXELFORMAT_RGB555, image); break;
569 ✗ case GATE_IMAGE_FLAG_DEPTH_4COL:
570 ✗ case GATE_IMAGE_FLAG_DEPTH_8COL: result = load_bitmap_generic(srcstream, &header, palette, linelen, GATE_IMAGE_PIXELFORMAT_PAL8, image); break;
571 19 case GATE_IMAGE_FLAG_DEPTH_32RGBA:
572 case GATE_IMAGE_FLAG_DEPTH_DEF:
573 19 default: result = load_bitmap_as_rgba_image(srcstream, &header, palette, linelen, image); break;
574 }
575
576 } while (0);
577
578 19 return result;
579 }
580
581
582 128 static void GATE_CALL load_bgra_line(gate_uint8_t* bmp_buffer, gate_uint8_t const* ptr_line, gate_size_t length)
583 {
584 128 gate_mem_copy(bmp_buffer, ptr_line, length * 4);
585 128 }
586
587 64 static void GATE_CALL load_rgb15_line(gate_uint8_t* bmp_buffer, gate_uint8_t const* ptr_line, gate_size_t length)
588 {
589 64 gate_mem_copy(bmp_buffer, ptr_line, length * 2);
590 64 }
591
592 64 static void GATE_CALL load_rgb16_line(gate_uint8_t* bmp_buffer, gate_uint8_t const* ptr_line, gate_size_t length)
593 {
594 64 gate_uint16_t* dst555 = (gate_uint16_t*)bmp_buffer;
595 64 gate_uint16_t const* src565 = (gate_uint16_t const*)ptr_line;
596
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 64 times.
4160 for (; length > 0; --length, ++dst555, ++src565)
597 {
598 4096 const gate_uint16_t r = (*src565 >> 11) & 0x1f;
599 4096 const gate_uint16_t g = ((*src565 >> 5) & 0x3f) >> 1;
600 4096 const gate_uint16_t b = (*src565) & 0x1f;
601 4096 *dst555 = (r << 10) | (g << 5) | b;
602 }
603 64 }
604
605
606 16 gate_result_t gate_bitmapimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
607 {
608 gate_result_t result;
609 gate_size_t byteswritten;
610 char buffer[12288];
611 16 char* ptrbuffer = &buffer[0];
612
613 16 char header[54] = GATE_INIT_EMPTY;
614 gate_bmp_header_t bmp;
615 16 gate_size_t bmp_linelen = (gate_size_t)image->width * 4; /* 4 bytes per pixel */
616 16 const gate_size_t bmp_datasize = bmp_linelen * image->height;
617
618 16 const gate_uint32_t filesize = (gate_uint32_t)bmp_datasize + 54;
619 16 gate_uint32_t creator = 0;
620 16 gate_uint32_t offset = 54;
621
622 16 void(GATE_CALL *line_writer)(gate_uint8_t*, gate_uint8_t const*, gate_size_t) = NULL;
623
624 (void)flags;
625
626 16 bmp.header_sz = 40;
627 16 bmp.width = (gate_int32_t)image->width;
628 16 bmp.height = (gate_int32_t)image->height;
629 16 bmp.nplanes = 1;
630 16 bmp.bitspp = 32;
631 16 bmp.compress_type = 0; /* no compression BI_RGB */
632 16 bmp.bmp_bytesz = (gate_uint32_t)bmp_datasize;
633 16 bmp.hres = 0;
634 16 bmp.vres = 0;
635 16 bmp.ncolors = 0;
636 16 bmp.nimpcolors = 0;
637
638
639
10/11
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
16 switch (image->pixel_format)
640 {
641 5 case GATE_IMAGE_PIXELFORMAT_RGBA:
642 case GATE_IMAGE_PIXELFORMAT_RGB32:
643 5 line_writer = &gate_color_convert_bgra_from_rgba_32;
644 5 bmp.bitspp = 32;
645 5 break;
646 2 case GATE_IMAGE_PIXELFORMAT_BGRA:
647 case GATE_IMAGE_PIXELFORMAT_BGR32:
648 2 line_writer = &load_bgra_line;
649 2 bmp.bitspp = 32;
650 2 break;
651
652 2 case GATE_IMAGE_PIXELFORMAT_RGB24:
653 2 line_writer = &gate_color_convert_bgra_from_rgb_24;
654 2 bmp.bitspp = 32;
655 2 break;
656 1 case GATE_IMAGE_PIXELFORMAT_BGR24:
657 1 line_writer = &gate_color_convert_bgra_from_bgr_24;
658 1 bmp.bitspp = 32;
659 1 break;
660
661 1 case GATE_IMAGE_PIXELFORMAT_RGB555:
662 1 line_writer = &load_rgb15_line;
663 1 bmp.bitspp = 16;
664 1 bmp_linelen = (image->width * 2u + 3u) & ~3u;
665 1 break;
666 1 case GATE_IMAGE_PIXELFORMAT_RGB565:
667 1 line_writer = &load_rgb16_line;
668 1 bmp.bitspp = 16;
669 1 bmp_linelen = (image->width * 2u + 3u) & ~3u;
670 1 break;
671 1 case GATE_IMAGE_PIXELFORMAT_ARGB4:
672 1 line_writer = &gate_color_convert_bgra_from_argb_16;
673 1 break;
674 1 case GATE_IMAGE_PIXELFORMAT_YUV2:
675 1 line_writer = &gate_color_convert_bgra_from_yuv2_16;
676 1 break;
677
678 1 case GATE_IMAGE_PIXELFORMAT_PAL8:
679 1 line_writer = &gate_color_convert_bgra_from_pal_8;
680 1 break;
681 1 case GATE_IMAGE_PIXELFORMAT_GRAY8:
682 1 line_writer = &gate_color_convert_bgra_from_gray_8;
683 1 break;
684 ✗ default:
685 ✗ break;
686 }
687
688 16 header[0] = 'B';
689 16 header[1] = 'M';
690 16 gate_serialize_uint32_l(&header[2], 4, &filesize);
691 16 gate_serialize_uint32_l(&header[6], 4, &creator);
692 16 gate_serialize_uint32_l(&header[10], 4, &offset);
693
694 16 gate_serialize_uint32_l(&header[14], 4, &bmp.header_sz);
695 16 gate_serialize_int32_l(&header[18], 4, &bmp.width);
696 16 gate_serialize_int32_l(&header[22], 4, &bmp.height);
697 16 gate_serialize_uint16_l(&header[26], 2, &bmp.nplanes);
698 16 gate_serialize_uint16_l(&header[28], 2, &bmp.bitspp);
699 16 gate_serialize_uint32_l(&header[30], 4, &bmp.compress_type);
700 16 gate_serialize_uint32_l(&header[34], 4, &bmp.bmp_bytesz);
701
702 16 gate_serialize_int32_l(&header[38], 4, &bmp.hres);
703 16 gate_serialize_int32_l(&header[42], 4, &bmp.vres);
704 16 gate_serialize_uint32_l(&header[46], 4, &bmp.ncolors);
705 16 gate_serialize_uint32_l(&header[50], 4, &bmp.nimpcolors);
706
707 do
708 {
709 16 const gate_size_t img_linelen =
710 16 (gate_size_t)(image->width * (unsigned)image->bits_per_pixel / 8u + (unsigned)image->line_padding);
711 gate_uint8_t const* ptrline;
712 gate_uint32_t y;
713
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (line_writer == NULL)
715 {
716 ✗ result = GATE_RESULT_INVALIDCONTENT;
717 ✗ break;
718 }
719
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (bmp_linelen > sizeof(buffer))
721 {
722 ✗ ptrbuffer = gate_mem_alloc(bmp_linelen);
723 ✗ if (ptrbuffer == NULL)
724 {
725 ✗ result = GATE_RESULT_OUTOFMEMORY;
726 ✗ break;
727 }
728 }
729 16 gate_mem_clear(ptrbuffer, bmp_linelen);
730
731 16 result = gate_stream_write_block(deststream, header, sizeof(header), &byteswritten);
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (GATE_FAILED(result))
733 {
734 ✗ result = GATE_RESULT_INVALIDOUTPUT;
735 ✗ break;
736 }
737
738 16 ptrline = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, (image->height - 1));
739
740
2/2
✓ Branch 0 taken 1184 times.
✓ Branch 1 taken 16 times.
1200 for (y = 0; y != image->height; ++y)
741 {
742 1184 gate_uint8_t* ptrbyte = (gate_uint8_t*)ptrbuffer;
743 1184 line_writer(ptrbyte, ptrline, image->width);
744
745 1184 result = gate_stream_write_block(deststream, ptrbuffer, bmp_linelen, &byteswritten);
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1184 times.
1184 if (GATE_FAILED(result))
747 {
748 ✗ result = GATE_RESULT_INVALIDOUTPUT;
749 ✗ break;
750 }
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1184 times.
1184 GATE_DEBUG_ASSERT(bmp_linelen == byteswritten);
752
753 1184 ptrline -= img_linelen;
754 }
755 } while (0);
756
757
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if ((ptrbuffer != &buffer[0]) && (ptrbuffer != NULL))
758 {
759 ✗ gate_mem_dealloc(ptrbuffer);
760 }
761
762 16 return result;
763 }
764