1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/graphics/pngimages.h"
#include "gate/results.h"
#include "gate/debugging.h"
#if defined(GATE_EXTLIB_LIBPNG)
# define GATE_GRAPHICS_PNG_USE_LIBPNG 1
#else
# define GATE_GRAPHICS_PNG_NO_IMPL 1
#endif
#if defined(GATE_GRAPHICS_PNG_USE_LIBPNG)
#if !defined(GATE_SYS_WINCE) || defined(GATE_COMPILER_MSVC)
# define GATE_GRAPHICS_PNG_SETJMP 1
#endif
#include "png.h"
typedef struct gate_png_param_class
{
gate_stream_t* stream;
gate_result_t result;
} gate_png_param_t;
static void gate_png_stream_read_data(png_structp png, png_bytep data, png_size_t length)
{
gate_png_param_t* user_param = (gate_png_param_t*)png_get_io_ptr(png);
gate_size_t bytesreturned;
gate_result_t result;
result = gate_stream_read_block(user_param->stream, (char*)data, length, &bytesreturned);
if (GATE_FAILED(result))
{
user_param->result = result;
}
else
{
if (bytesreturned != length)
{
user_param->result = GATE_RESULT_ENDOFSTREAM;
}
}
}
static void gate_png_stream_write_data(png_structp png, png_bytep data, png_size_t length)
{
gate_png_param_t* user_param = (gate_png_param_t*)png_get_io_ptr(png);
gate_size_t byteswritten;
gate_result_t result;
result = gate_stream_write_block(user_param->stream, (char const*)data, length, &byteswritten);
if (GATE_FAILED(result))
{
user_param->result = result;
}
else
{
if (byteswritten != length)
{
user_param->result = GATE_RESULT_ENDOFSTREAM;
}
}
}
static void gate_png_stream_flush(png_structp png)
{
gate_png_param_t* user_param = (gate_png_param_t*)png_get_io_ptr(png);
gate_stream_flush(user_param->stream);
}
static void gate_png_error_handler(png_structp png, png_const_charp msg)
{
void* error_ptr = png_get_error_ptr(png);
GATE_UNUSED_ARG(msg);
if (error_ptr == NULL)
{
GATE_DEBUG_BREAKPOINT;
}
else
{
#if defined(GATE_GRAPHICS_PNG_SETJMP)
jmp_buf* jmpbuffer = (jmp_buf*)(error_ptr);
longjmp(*jmpbuffer, 1);
#endif
}
}
static void gate_png_warning_handler(png_structp png, png_const_charp msg)
{
(void)png;
(void)msg;
}
gate_result_t gate_pngimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
{
gate_result_t ret;
gate_rasterimage_t raster;
gate_png_param_t user_param;
#if defined(GATE_GRAPHICS_PNG_SETJMP)
jmp_buf jmpbuffer;
#endif
png_structp png = NULL;
png_infop png_info = NULL;
png_infop png_info_end = NULL;
unsigned char png_interlace_type;<--- The scope of the variable 'png_interlace_type' can be reduced. [+]The scope of the variable 'png_interlace_type' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned char png_color_type;<--- The scope of the variable 'png_color_type' can be reduced. [+]The scope of the variable 'png_color_type' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned char png_bit_depth;<--- The scope of the variable 'png_bit_depth' can be reduced. [+]The scope of the variable 'png_bit_depth' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t linebytes;
png_byte buffer[4096 * 3];
png_byte* ptrbuffer = &buffer[0];
gate_color_t* ptrpixels;
gate_uint32_t x, y;
png_bytep ptrbytes;
png_colorp png_palette;
int png_palette_size;
int png_number_of_passes;
int ndx;
user_param.stream = srcstream;
user_param.result = GATE_RESULT_OK;
gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_DEFAULT, 0, 0, NULL);
do
{
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
png_info = png_create_info_struct(png);
png_info_end = png_create_info_struct(png);
if (png_info == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
#if defined(GATE_GRAPHICS_PNG_SETJMP)
if (setjmp(jmpbuffer) != 0)
{
ret = GATE_RESULT_CRITICALERROR;
break;
}
png_set_error_fn(png, &jmpbuffer, &gate_png_error_handler, &gate_png_warning_handler);
#endif
png_set_read_fn(png, (png_voidp)&user_param, &gate_png_stream_read_data);
png_read_info(png, png_info);
if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_DEFAULT,
png_get_image_width(png, png_info),
png_get_image_height(png, png_info), NULL))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
png_interlace_type = png_get_interlace_type(png, png_info);
png_color_type = png_get_color_type(png, png_info);
if (png_interlace_type == PNG_INTERLACE_ADAM7)
{
/* skip smaller preview images */
linebytes = png_get_rowbytes(png, png_info);
if (linebytes > sizeof(buffer))
{
ptrbuffer = gate_mem_alloc(linebytes);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
png_number_of_passes = png_set_interlace_handling(png);
for (ndx = 1; ndx != png_number_of_passes; ++ndx)
{
for (y = 0; y != raster.height; ++y)
{
ptrbytes = (png_bytep)ptrbuffer;
png_read_row(png, ptrbuffer, NULL);
}
}
if (ptrbuffer != &buffer[0])
{
gate_mem_dealloc(ptrbuffer);
ptrbuffer = &buffer[0];
}
}
ret = GATE_RESULT_OK;
png_bit_depth = png_get_bit_depth(png, png_info);
switch (png_color_type)
{
case PNG_COLOR_TYPE_PALETTE:
{ // we have a palette
if (png_bit_depth < 8)
{
png_set_packing(png);
}
if (png_bit_depth > 8)
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
linebytes = raster.width;
if (linebytes > sizeof(buffer))
{
ptrbuffer = gate_mem_alloc(linebytes);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
png_get_PLTE(png, png_info, &png_palette, &png_palette_size);
for (y = 0; y < raster.height; ++y)
{
ptrbytes = (png_bytep)ptrbuffer;
png_read_row(png, ptrbuffer, NULL);
for (x = 0; x < raster.width; ++x)
{
if (*ptrbytes < png_palette_size)
{
ptrpixels->r = (gate_uint8_t)(png_palette[*ptrbytes].red);
ptrpixels->g = (gate_uint8_t)(png_palette[*ptrbytes].green);
ptrpixels->b = (gate_uint8_t)(png_palette[*ptrbytes].blue);
}
else
{
ptrpixels->r = 255;
ptrpixels->g = 255;
ptrpixels->b = 255;
}
ptrpixels->a = 255;
++ptrpixels;
++ptrbytes;
}
}
png_read_end(png, png_info_end);
break;
}
case PNG_COLOR_TYPE_RGB:
{
png_set_strip_16(png);
linebytes = png_get_rowbytes(png, png_info);
if (linebytes > sizeof(buffer))
{
ptrbuffer = gate_mem_alloc(linebytes);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
for (y = 0; y < raster.height; ++y)
{
ptrbytes = (png_bytep)ptrbuffer;
png_read_row(png, ptrbuffer, NULL);
for (x = 0; x < raster.width; ++x)
{
ptrpixels->r = (gate_uint8_t) * (ptrbytes++);
ptrpixels->g = (gate_uint8_t) * (ptrbytes++);
ptrpixels->b = (gate_uint8_t) * (ptrbytes++);
ptrpixels->a = 255;
++ptrpixels;
}
}
png_read_end(png, png_info_end);
break;
}
case PNG_COLOR_TYPE_RGB_ALPHA:
{
png_set_strip_16(png);
linebytes = png_get_rowbytes(png, png_info);
if (linebytes > sizeof(buffer))
{
ptrbuffer = gate_mem_alloc(linebytes);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
for (y = 0; y < raster.height; ++y)
{
ptrbytes = (png_bytep)ptrbuffer;
png_read_row(png, ptrbuffer, NULL);
for (x = 0; x < raster.width; ++x)
{
ptrpixels->r = (gate_uint8_t) * (ptrbytes++);
ptrpixels->g = (gate_uint8_t) * (ptrbytes++);
ptrpixels->b = (gate_uint8_t) * (ptrbytes++);
ptrpixels->a = (gate_uint8_t) * (ptrbytes++);
++ptrpixels;
}
}
png_read_end(png, png_info_end);
break;
}
case PNG_COLOR_TYPE_GRAY:
{
if (png_bit_depth > 8)
{
linebytes = png_get_rowbytes(png, png_info);
if (linebytes > sizeof(buffer))
{
ptrbuffer = gate_mem_alloc(linebytes);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
for (y = 0; y < raster.height; ++y)
{
ptrbytes = (png_bytep)ptrbuffer;
png_read_row(png, ptrbuffer, NULL);
for (x = 0; x < raster.width; ++x)
{
ptrpixels->r = (gate_uint8_t)*ptrbytes;
ptrpixels->g = (gate_uint8_t)*ptrbytes;
ptrpixels->b = (gate_uint8_t)*ptrbytes;
ptrpixels->a = 255;
++ptrpixels;
ptrbytes += 2;
}
}
}
else
{
if (png_bit_depth < 8)
{
png_set_expand(png);
}
linebytes = png_get_rowbytes(png, png_info);
if (linebytes > sizeof(buffer))
{
ptrbuffer = gate_mem_alloc(linebytes);
if (ptrbuffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
ptrpixels = (gate_color_t*)gate_rasterimage_get_line_ptr(&raster, 0);
for (y = 0; y < raster.height; ++y)
{
ptrbytes = (png_bytep)ptrbuffer;
png_read_row(png, ptrbuffer, NULL);
for (x = 0; x < raster.width; ++x)
{
ptrpixels->r = (gate_uint8_t) * (ptrbytes);
ptrpixels->g = (gate_uint8_t) * (ptrbytes);
ptrpixels->b = (gate_uint8_t) * (ptrbytes);
ptrpixels->a = (gate_uint8_t) * (ptrbytes);
++ptrpixels;
++ptrbytes;
}
}
}
png_read_end(png, png_info_end);
break;
}
default:
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
} // switch(info->color_type)
if (GATE_SUCCEEDED(ret))
{
gate_mem_copy(image, &raster, sizeof(raster));
gate_mem_clear(&raster, sizeof(raster));
}
} while (0);
if (png != NULL)
{
png_destroy_read_struct(&png, &png_info, &png_info_end);
}
if ((ptrbuffer != &buffer[0]) && (ptrbuffer != NULL))
{
gate_mem_dealloc(ptrbuffer);
}
gate_rasterimage_release(&raster);
return ret;
}
static gate_bool_t gate_png_equals_color(png_color const* pngcolor, gate_color_t const* gatecolor)
{
return (pngcolor->red == gatecolor->r)
&& (pngcolor->green == gatecolor->g)
&& (pngcolor->blue == gatecolor->b)
;
}
static void copy_rgba(gate_uint8_t* dst, gate_uint8_t const* src, gate_size_t pixel_count)
{
gate_mem_copy(dst, src, pixel_count * 4);
}
gate_result_t gate_pngimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
{
gate_result_t ret = GATE_RESULT_OK;
gate_png_param_t user_param;
png_structp png = NULL;
png_infop png_info = NULL;
#if defined(GATE_GRAPHICS_PNG_SETJMP)
jmp_buf jmpbuffer;
#endif
png_color png_palette[256];
png_byte buffer[4096];
gate_uint8_t* ptr_buffer = NULL;
gate_uint8_t const* ptr_line;
gate_size_t buffer_required_len;
int p;
int png_palette_size = 0;
png_const_colorp ptrpalette = NULL;
gate_uint32_t imagesize;
gate_color_t const* ptrpixels;
gate_bool_t found;
unsigned x, y;
void(*line_writer)(gate_uint8_t*, gate_uint8_t const*, gate_size_t) = NULL;
do
{
if ((image->width == 0) || (image->height == 0) || (image->data == NULL))
{
ret = GATE_RESULT_INVALIDDATA;
break;
}
switch (image->pixel_format)
{
case GATE_IMAGE_PIXELFORMAT_RGBA:
case GATE_IMAGE_PIXELFORMAT_RGB32: line_writer = ©_rgba; break;
case GATE_IMAGE_PIXELFORMAT_BGRA:
case GATE_IMAGE_PIXELFORMAT_BGR32: line_writer = &gate_color_convert_rgba_from_bgra_32; break;
case GATE_IMAGE_PIXELFORMAT_RGB24: line_writer = &gate_color_convert_rgba_from_rgb_24; break;
case GATE_IMAGE_PIXELFORMAT_BGR24: line_writer = &gate_color_convert_rgba_from_bgr_24; break;
case GATE_IMAGE_PIXELFORMAT_RGB555: line_writer = &gate_color_convert_rgba_from_rgb_15; break;
case GATE_IMAGE_PIXELFORMAT_RGB565: line_writer = &gate_color_convert_rgba_from_rgb_16; break;
case GATE_IMAGE_PIXELFORMAT_ARGB4: line_writer = &gate_color_convert_rgba_from_argb_16; break;
case GATE_IMAGE_PIXELFORMAT_YUV2: line_writer = &gate_color_convert_rgba_from_yuv2_16; break;
case GATE_IMAGE_PIXELFORMAT_PAL8: line_writer = &gate_color_convert_rgba_from_pal_8; break;
case GATE_IMAGE_PIXELFORMAT_GRAY8: line_writer = &gate_color_convert_rgba_from_gray_8; break;
default:
break;
}
if (line_writer == NULL)
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
buffer_required_len = (gate_size_t)image->width * 4u;
if (buffer_required_len <= sizeof(buffer))
{
ptr_buffer = &buffer[0];
}
else
{
ptr_buffer = gate_mem_alloc(buffer_required_len);
if (ptr_buffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
imagesize = image->width * image->height;
user_param.stream = deststream;
user_param.result = GATE_RESULT_OK;
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL)
{
ret = GATE_RESULT_FAILED;
break;
}
png_info = png_create_info_struct(png);
if (png_info == NULL)
{
ret = GATE_RESULT_INVALIDHEADER;
break;
}
#if defined(GATE_GRAPHICS_PNG_SETJMP)
if (setjmp(jmpbuffer) != 0)
{
ret = GATE_RESULT_FAILED;
break;
}
png_set_error_fn(png, &jmpbuffer, &gate_png_error_handler, &gate_png_warning_handler);
#endif
png_set_write_fn(png, &user_param, &gate_png_stream_write_data, &gate_png_stream_flush);
if (GATE_FLAG_ENABLED(flags, GATE_IMAGE_FLAG_PALETTE))
{
ptrpixels = (gate_color_t const*)gate_rasterimage_get_line_ptr(image, 0);
ptrpalette = &png_palette[0];
for (y = 0; (y != image->height) && (ptrpalette != NULL); ++y)
{
ptr_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y);
line_writer(ptr_buffer, ptr_line, image->width);
ptrpixels = (gate_color_t const*)ptr_buffer;
for (x = 0; x != image->width; ++x)
{
found = false;
for (p = 0; p != png_palette_size; ++p)
{
if (gate_png_equals_color(&png_palette[p], &ptrpixels[x]))
{
found = true;
break;
}
}
if (!found)
{
png_palette[png_palette_size].red = ptrpixels[x].r;
png_palette[png_palette_size].green = ptrpixels[x].g;
png_palette[png_palette_size].blue = ptrpixels[x].b;
++png_palette_size;
if (png_palette_size >= 256)
{
ptrpalette = NULL;
break;
}
}
}
}
}
if (ptrpalette != NULL)
{
/* write an indexed palette image */
png_set_IHDR(png, png_info, image->width, image->height, 8, PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_PLTE(png, png_info, ptrpalette, png_palette_size);
png_write_info(png, png_info);
for (y = 0; y != image->height; ++y)
{
ptr_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, 0);
line_writer(ptr_buffer, ptr_line, image->width);
for (x = 0; x != image->width; ++x)
{
for (p = 0; p != png_palette_size; ++p)
{
if (gate_png_equals_color(&png_palette[p], (gate_color_t const*)&ptr_buffer[x * 4]))
{
ptr_buffer[x] = (png_byte)p;
break;
}
}
}
png_write_row(png, ptr_buffer);
}
}
else
{
if (line_writer == NULL)
{
ret = GATE_RESULT_NOTSUPPORTED;
}
else
{
png_set_IHDR(png, png_info, image->width, image->height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, png_info);
for (y = 0; y != image->height; ++y)
{
ptr_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y);
line_writer(ptr_buffer, ptr_line, image->width);
png_write_row(png, (png_bytep)ptr_buffer);
}
}
}
png_write_end(png, png_info);
} while (0);
if (ptr_buffer && (ptr_buffer != &buffer[0]))
{
gate_mem_dealloc(ptr_buffer);
}
if (png != NULL)
{
png_destroy_write_struct(&png, &png_info);
}
return ret;
}
#endif /* GATE_GRAPHICS_PNG_USE_LIBPNG */
#if defined(GATE_GRAPHICS_PNG_NO_IMPL)
gate_result_t gate_pngimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
{
(void)srcstream;
(void)image;
(void)flags;
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_pngimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
{
(void)image;
(void)deststream;
(void)flags;
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif /* GATE_GRAPHICS_PNG_NO_IMPL */
|