Skip to content

Latest commit

 

History

History
677 lines (597 loc) · 24.1 KB

IMG_png.c

File metadata and controls

677 lines (597 loc) · 24.1 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Aug 10, 2000
Aug 10, 2000
20
21
22
23
24
25
*/
/* This is a PNG image file loading framework */
#include "SDL_image.h"
Nov 5, 2019
Nov 5, 2019
26
27
/* We'll always have PNG save support */
#define SAVE_PNG
Mar 17, 2019
Mar 17, 2019
28
Sep 12, 2017
Sep 12, 2017
29
#if !(defined(__APPLE__) || defined(SDL_IMAGE_USE_WIC_BACKEND)) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Jun 3, 2013
Jun 3, 2013
30
Aug 10, 2000
Aug 10, 2000
31
32
#ifdef LOAD_PNG
Nov 5, 2019
Nov 5, 2019
33
34
#define USE_LIBPNG
Nov 5, 2019
Nov 5, 2019
35
/* This code was originally written by Philippe Lavoie (2 November 1998) */
Nov 5, 2019
Nov 5, 2019
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
#include "SDL_endian.h"
#ifdef macintosh
#define MACOS
#endif
#include <png.h>
/* Check for the older version of libpng */
#if (PNG_LIBPNG_VER_MAJOR == 1)
#if (PNG_LIBPNG_VER_MINOR < 5)
#define LIBPNG_VERSION_12
typedef png_bytep png_const_bytep;
typedef png_color *png_const_colorp;
#endif
#if (PNG_LIBPNG_VER_MINOR < 4)
typedef png_structp png_const_structp;
typedef png_infop png_const_infop;
#endif
#if (PNG_LIBPNG_VER_MINOR < 6)
typedef png_structp png_structrp;
typedef png_infop png_inforp;
typedef png_const_structp png_const_structrp;
typedef png_const_infop png_const_inforp;
/* noconst15: version < 1.6 doesn't have const, >= 1.6 adds it */
/* noconst16: version < 1.6 does have const, >= 1.6 removes it */
typedef png_structp png_noconst15_structrp;
typedef png_inforp png_noconst15_inforp;
typedef png_const_inforp png_noconst16_inforp;
#else
typedef png_const_structp png_noconst15_structrp;
typedef png_const_inforp png_noconst15_inforp;
typedef png_inforp png_noconst16_inforp;
#endif
#else
typedef png_const_structp png_noconst15_structrp;
typedef png_const_inforp png_noconst15_inforp;
typedef png_inforp png_noconst16_inforp;
Nov 3, 2019
Nov 3, 2019
74
75
#endif
Nov 5, 2019
Nov 5, 2019
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
static struct {
int loaded;
void *handle;
png_infop (*png_create_info_struct) (png_noconst15_structrp png_ptr);
png_structp (*png_create_read_struct) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
void (*png_destroy_read_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr);
png_uint_32 (*png_get_IHDR) (png_noconst15_structrp png_ptr, png_noconst15_inforp info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_method, int *compression_method, int *filter_method);
png_voidp (*png_get_io_ptr) (png_noconst15_structrp png_ptr);
png_byte (*png_get_channels) (png_const_structrp png_ptr, png_const_inforp info_ptr);
png_uint_32 (*png_get_PLTE) (png_const_structrp png_ptr, png_noconst16_inforp info_ptr, png_colorp *palette, int *num_palette);
png_uint_32 (*png_get_tRNS) (png_const_structrp png_ptr, png_inforp info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values);
png_uint_32 (*png_get_valid) (png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 flag);
void (*png_read_image) (png_structrp png_ptr, png_bytepp image);
void (*png_read_info) (png_structrp png_ptr, png_inforp info_ptr);
void (*png_read_update_info) (png_structrp png_ptr, png_inforp info_ptr);
void (*png_set_expand) (png_structrp png_ptr);
void (*png_set_gray_to_rgb) (png_structrp png_ptr);
void (*png_set_packing) (png_structrp png_ptr);
void (*png_set_read_fn) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn);
void (*png_set_strip_16) (png_structrp png_ptr);
int (*png_set_interlace_handling) (png_structrp png_ptr);
int (*png_sig_cmp) (png_const_bytep sig, png_size_t start, png_size_t num_to_check);
#ifdef PNG_SETJMP_SUPPORTED
#ifndef LIBPNG_VERSION_12
jmp_buf* (*png_set_longjmp_fn) (png_structrp, png_longjmp_ptr, size_t);
#endif
#endif
#ifdef SAVE_PNG
png_structp (*png_create_write_struct) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
void (*png_destroy_write_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr);
void (*png_set_write_fn) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn);
void (*png_set_IHDR) (png_noconst15_structrp png_ptr, png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type);
void (*png_write_info) (png_structrp png_ptr, png_noconst15_inforp info_ptr);
void (*png_set_rows) (png_noconst15_structrp png_ptr, png_inforp info_ptr, png_bytepp row_pointers);
void (*png_write_png) (png_structrp png_ptr, png_inforp info_ptr, int transforms, png_voidp params);
void (*png_set_PLTE) (png_structrp png_ptr, png_inforp info_ptr, png_const_colorp palette, int num_palette);
#endif
} lib;
Sep 12, 2017
Sep 12, 2017
114
Nov 5, 2019
Nov 5, 2019
115
116
117
118
119
120
121
122
#ifdef LOAD_PNG_DYNAMIC
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = FUNC;
#endif
Oct 22, 2017
Oct 22, 2017
123
Sep 26, 2009
Sep 26, 2009
124
int IMG_InitPNG()
May 12, 2006
May 12, 2006
125
{
Nov 5, 2019
Nov 5, 2019
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
if ( lib.loaded == 0 ) {
#ifdef LOAD_PNG_DYNAMIC
lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
#endif
FUNCTION_LOADER(png_create_info_struct, png_infop (*) (png_noconst15_structrp png_ptr))
FUNCTION_LOADER(png_create_read_struct, png_structp (*) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn))
FUNCTION_LOADER(png_destroy_read_struct, void (*) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr))
FUNCTION_LOADER(png_get_IHDR, png_uint_32 (*) (png_noconst15_structrp png_ptr, png_noconst15_inforp info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_method, int *compression_method, int *filter_method))
FUNCTION_LOADER(png_get_io_ptr, png_voidp (*) (png_noconst15_structrp png_ptr))
FUNCTION_LOADER(png_get_channels, png_byte (*) (png_const_structrp png_ptr, png_const_inforp info_ptr))
FUNCTION_LOADER(png_get_PLTE, png_uint_32 (*) (png_const_structrp png_ptr, png_noconst16_inforp info_ptr, png_colorp *palette, int *num_palette))
FUNCTION_LOADER(png_get_tRNS, png_uint_32 (*) (png_const_structrp png_ptr, png_inforp info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values))
FUNCTION_LOADER(png_get_valid, png_uint_32 (*) (png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 flag))
FUNCTION_LOADER(png_read_image, void (*) (png_structrp png_ptr, png_bytepp image))
FUNCTION_LOADER(png_read_info, void (*) (png_structrp png_ptr, png_inforp info_ptr))
FUNCTION_LOADER(png_read_update_info, void (*) (png_structrp png_ptr, png_inforp info_ptr))
FUNCTION_LOADER(png_set_expand, void (*) (png_structrp png_ptr))
FUNCTION_LOADER(png_set_gray_to_rgb, void (*) (png_structrp png_ptr))
FUNCTION_LOADER(png_set_packing, void (*) (png_structrp png_ptr))
FUNCTION_LOADER(png_set_read_fn, void (*) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn))
FUNCTION_LOADER(png_set_strip_16, void (*) (png_structrp png_ptr))
FUNCTION_LOADER(png_set_interlace_handling, int (*) (png_structrp png_ptr))
FUNCTION_LOADER(png_sig_cmp, int (*) (png_const_bytep sig, png_size_t start, png_size_t num_to_check))
#ifdef PNG_SETJMP_SUPPORTED
#ifndef LIBPNG_VERSION_12
FUNCTION_LOADER(png_set_longjmp_fn, jmp_buf* (*) (png_structrp, png_longjmp_ptr, size_t))
#endif
#endif
#ifdef SAVE_PNG
FUNCTION_LOADER(png_create_write_struct, png_structp (*) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn))
FUNCTION_LOADER(png_destroy_write_struct, void (*) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr))
FUNCTION_LOADER(png_set_write_fn, void (*) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn))
FUNCTION_LOADER(png_set_IHDR, void (*) (png_noconst15_structrp png_ptr, png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type))
FUNCTION_LOADER(png_write_info, void (*) (png_structrp png_ptr, png_noconst15_inforp info_ptr))
FUNCTION_LOADER(png_set_rows, void (*) (png_noconst15_structrp png_ptr, png_inforp info_ptr, png_bytepp row_pointers))
FUNCTION_LOADER(png_write_png, void (*) (png_structrp png_ptr, png_inforp info_ptr, int transforms, png_voidp params))
FUNCTION_LOADER(png_set_PLTE, void (*) (png_structrp png_ptr, png_inforp info_ptr, png_const_colorp palette, int num_palette))
#endif
}
++lib.loaded;
May 22, 2013
May 22, 2013
170
return 0;
May 12, 2006
May 12, 2006
171
}
Sep 26, 2009
Sep 26, 2009
172
void IMG_QuitPNG()
May 12, 2006
May 12, 2006
173
{
Nov 5, 2019
Nov 5, 2019
174
175
176
177
178
179
180
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
#ifdef LOAD_PNG_DYNAMIC
SDL_UnloadObject(lib.handle);
#endif
Nov 3, 2019
Nov 3, 2019
181
}
Nov 5, 2019
Nov 5, 2019
182
--lib.loaded;
Nov 3, 2019
Nov 3, 2019
183
184
}
Nov 5, 2019
Nov 5, 2019
185
/* See if an image is contained in a data source */
Aug 10, 2000
Aug 10, 2000
186
187
int IMG_isPNG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
188
Sint64 start;
Nov 5, 2019
Nov 5, 2019
189
190
int is_PNG;
Uint8 magic[4];
Nov 1, 2019
Nov 1, 2019
191
Nov 5, 2019
Nov 5, 2019
192
193
if ( !src ) {
return 0;
Nov 3, 2019
Nov 3, 2019
194
195
}
Nov 5, 2019
Nov 5, 2019
196
197
198
199
200
201
202
203
start = SDL_RWtell(src);
is_PNG = 0;
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G' ) {
is_PNG = 1;
Nov 3, 2019
Nov 3, 2019
204
205
}
}
Nov 5, 2019
Nov 5, 2019
206
207
208
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_PNG);
}
Nov 3, 2019
Nov 3, 2019
209
Nov 5, 2019
Nov 5, 2019
210
211
212
213
/* Load a PNG type image from an SDL datasource */
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
{
SDL_RWops *src;
Nov 3, 2019
Nov 3, 2019
214
Nov 5, 2019
Nov 5, 2019
215
216
src = (SDL_RWops *)lib.png_get_io_ptr(ctx);
SDL_RWread(src, area, size, 1);
Nov 3, 2019
Nov 3, 2019
217
}
Aug 10, 2000
Aug 10, 2000
218
219
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
Nov 5, 2019
Nov 5, 2019
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
Sint64 start;
const char *error;
SDL_Surface *volatile surface;
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, num_channels;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
SDL_Palette *palette;
png_bytep *volatile row_pointers;
int row, i;
int ckey = -1;
png_color_16 *transv;
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
May 22, 2013
May 22, 2013
242
Feb 15, 2016
Feb 15, 2016
243
if ( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0 ) {
May 22, 2013
May 22, 2013
244
245
246
return NULL;
}
Nov 5, 2019
Nov 5, 2019
247
248
249
/* Initialize the data we will clean up when we're done */
error = NULL;
png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
May 22, 2013
May 22, 2013
250
Nov 5, 2019
Nov 5, 2019
251
252
253
254
255
256
/* Create the PNG loading context structure */
png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,NULL,NULL);
if (png_ptr == NULL){
error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
goto done;
May 22, 2013
May 22, 2013
257
258
}
Nov 5, 2019
Nov 5, 2019
259
260
261
262
263
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = lib.png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
error = "Couldn't create image information for PNG file";
goto done;
May 22, 2013
May 22, 2013
264
}
Nov 1, 2019
Nov 1, 2019
265
Nov 5, 2019
Nov 5, 2019
266
267
268
269
/* Set error handling if you are using setjmp/longjmp method (this is
* the normal method of doing things with libpng). REQUIRED unless you
* set up your own error handlers in png_create_read_struct() earlier.
*/
May 22, 2013
May 22, 2013
270
Nov 5, 2019
Nov 5, 2019
271
#ifdef PNG_SETJMP_SUPPORTED
Mar 5, 2020
Mar 5, 2020
272
273
274
#ifdef _MSC_VER
#pragma warning(disable:4611) /* warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable */
#endif
Nov 5, 2019
Nov 5, 2019
275
276
#ifndef LIBPNG_VERSION_12
if ( setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))) )
Nov 3, 2019
Nov 3, 2019
277
#else
Nov 5, 2019
Nov 5, 2019
278
if ( setjmp(png_ptr->jmpbuf) )
Nov 3, 2019
Nov 3, 2019
279
#endif
Nov 5, 2019
Nov 5, 2019
280
281
282
{
error = "Error reading the PNG file.";
goto done;
Nov 1, 2019
Nov 1, 2019
283
}
Nov 5, 2019
Nov 5, 2019
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
#endif
/* Set up the input control */
lib.png_set_read_fn(png_ptr, src, png_read_data);
/* Read PNG header info */
lib.png_read_info(png_ptr, info_ptr);
lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, &interlace_type, NULL, NULL);
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
lib.png_set_strip_16(png_ptr);
/* tell libpng to de-interlace (if the image is interlaced) */
lib.png_set_interlace_handling(png_ptr);
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
* byte into separate bytes (useful for paletted and grayscale images).
*/
lib.png_set_packing(png_ptr);
/* scale greyscale values to the range 0..255 */
if (color_type == PNG_COLOR_TYPE_GRAY)
lib.png_set_expand(png_ptr);
/* For images with a single "transparent colour", set colour key;
if more than one index has transparency, or if partially transparent
entries exist, use full alpha channel */
if (lib.png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
int num_trans;
Uint8 *trans;
lib.png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &transv);
if (color_type == PNG_COLOR_TYPE_PALETTE) {
/* Check if all tRNS entries are opaque except one */
int j, t = -1;
for (j = 0; j < num_trans; j++) {
if (trans[j] == 0) {
if (t >= 0) {
break;
}
t = j;
} else if (trans[j] != 255) {
break;
}
}
if (j == num_trans) {
/* exactly one transparent index */
ckey = t;
} else {
/* more than one transparent index, or translucency */
lib.png_set_expand(png_ptr);
Nov 1, 2019
Nov 1, 2019
334
}
Nov 5, 2019
Nov 5, 2019
335
336
} else {
ckey = 0; /* actual value will be set later */
Nov 1, 2019
Nov 1, 2019
337
}
May 22, 2013
May 22, 2013
338
339
}
Nov 5, 2019
Nov 5, 2019
340
341
if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
lib.png_set_gray_to_rgb(png_ptr);
Nov 1, 2019
Nov 1, 2019
342
Nov 5, 2019
Nov 5, 2019
343
lib.png_read_update_info(png_ptr, info_ptr);
Nov 1, 2019
Nov 1, 2019
344
Nov 5, 2019
Nov 5, 2019
345
346
lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, &interlace_type, NULL, NULL);
May 22, 2013
May 22, 2013
347
Nov 5, 2019
Nov 5, 2019
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* Allocate the SDL surface to hold the image */
Rmask = Gmask = Bmask = Amask = 0 ;
num_channels = lib.png_get_channels(png_ptr, info_ptr);
if ( num_channels >= 3 ) {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = (num_channels == 4) ? 0xFF000000 : 0;
#else
int s = (num_channels == 4) ? 0 : 8;
Rmask = 0xFF000000 >> s;
Gmask = 0x00FF0000 >> s;
Bmask = 0x0000FF00 >> s;
Amask = 0x000000FF >> s;
#endif
}
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
bit_depth*num_channels, Rmask,Gmask,Bmask,Amask);
if ( surface == NULL ) {
error = SDL_GetError();
goto done;
}
May 22, 2013
May 22, 2013
371
Nov 5, 2019
Nov 5, 2019
372
373
374
375
376
377
378
if (ckey != -1) {
if (color_type != PNG_COLOR_TYPE_PALETTE) {
/* FIXME: Should these be truncated or shifted down? */
ckey = SDL_MapRGB(surface->format,
(Uint8)transv->red,
(Uint8)transv->green,
(Uint8)transv->blue);
May 22, 2013
May 22, 2013
379
}
Nov 5, 2019
Nov 5, 2019
380
381
SDL_SetColorKey(surface, SDL_TRUE, ckey);
}
May 22, 2013
May 22, 2013
382
Nov 5, 2019
Nov 5, 2019
383
384
385
386
387
388
389
390
391
392
/* Create the array of pointers to image data */
row_pointers = (png_bytep*) SDL_malloc(sizeof(png_bytep)*height);
if (!row_pointers) {
error = "Out of memory";
goto done;
}
for (row = 0; row < (int)height; row++) {
row_pointers[row] = (png_bytep)
(Uint8 *)surface->pixels + row*surface->pitch;
}
Nov 1, 2019
Nov 1, 2019
393
Nov 5, 2019
Nov 5, 2019
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
/* Read the entire image in one go */
lib.png_read_image(png_ptr, row_pointers);
/* and we're done! (png_read_end() can be omitted if no processing of
* post-IDAT text/time/etc. is desired)
* In some cases it can't read PNG's created by some popular programs (ACDSEE),
* we do not want to process comments, so we omit png_read_end
lib.png_read_end(png_ptr, info_ptr);
*/
/* Load the palette, if any */
palette = surface->format->palette;
if ( palette ) {
int png_num_palette;
png_colorp png_palette;
lib.png_get_PLTE(png_ptr, info_ptr, &png_palette, &png_num_palette);
if (color_type == PNG_COLOR_TYPE_GRAY) {
palette->ncolors = 256;
for (i = 0; i < 256; i++) {
palette->colors[i].r = (Uint8)i;
palette->colors[i].g = (Uint8)i;
palette->colors[i].b = (Uint8)i;
}
} else if (png_num_palette > 0 ) {
palette->ncolors = png_num_palette;
for ( i=0; i<png_num_palette; ++i ) {
palette->colors[i].b = png_palette[i].blue;
palette->colors[i].g = png_palette[i].green;
palette->colors[i].r = png_palette[i].red;
}
May 22, 2013
May 22, 2013
425
426
}
}
Nov 1, 2019
Nov 1, 2019
427
Nov 5, 2019
Nov 5, 2019
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
done: /* Clean up and return */
if ( png_ptr ) {
lib.png_destroy_read_struct(&png_ptr,
info_ptr ? &info_ptr : (png_infopp)0,
(png_infopp)0);
}
if ( row_pointers ) {
SDL_free(row_pointers);
}
if ( error ) {
SDL_RWseek(src, start, RW_SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError("%s", error);
}
return(surface);
Aug 10, 2000
Aug 10, 2000
446
447
448
}
#else
Oct 9, 2019
Oct 9, 2019
449
450
451
#if _MSC_VER >= 1300
#pragma warning(disable : 4100) /* warning C4100: 'op' : unreferenced formal parameter */
#endif
Aug 10, 2000
Aug 10, 2000
452
Sep 26, 2009
Sep 26, 2009
453
454
int IMG_InitPNG()
{
May 22, 2013
May 22, 2013
455
IMG_SetError("PNG images are not supported");
Nov 5, 2019
Nov 5, 2019
456
return(-1);
Sep 26, 2009
Sep 26, 2009
457
458
459
460
461
462
}
void IMG_QuitPNG()
{
}
Aug 10, 2000
Aug 10, 2000
463
464
465
/* See if an image is contained in a data source */
int IMG_isPNG(SDL_RWops *src)
{
Nov 5, 2019
Nov 5, 2019
466
return(0);
Aug 10, 2000
Aug 10, 2000
467
468
469
470
471
}
/* Load a PNG type image from an SDL datasource */
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
Nov 5, 2019
Nov 5, 2019
472
return(NULL);
Aug 10, 2000
Aug 10, 2000
473
474
475
}
#endif /* LOAD_PNG */
Nov 10, 2009
Nov 10, 2009
476
477
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */
Jun 3, 2013
Jun 3, 2013
478
479
480
481
482
483
484
485
#ifdef SAVE_PNG
int IMG_SavePNG(SDL_Surface *surface, const char *file)
{
SDL_RWops *dst = SDL_RWFromFile(file, "wb");
if (dst) {
return IMG_SavePNG_RW(surface, dst, 1);
Nov 5, 2019
Nov 5, 2019
486
487
} else {
return -1;
Jun 3, 2013
Jun 3, 2013
488
489
490
}
}
Nov 5, 2019
Nov 5, 2019
491
492
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
static const Uint32 png_format = SDL_PIXELFORMAT_ABGR8888;
Jun 3, 2013
Jun 3, 2013
493
#else
Nov 5, 2019
Nov 5, 2019
494
495
496
497
498
499
500
501
502
503
504
505
506
static const Uint32 png_format = SDL_PIXELFORMAT_RGBA8888;
#endif
#ifdef USE_LIBPNG
static void png_write_data(png_structp png_ptr, png_bytep src, png_size_t size)
{
SDL_RWops *dst = (SDL_RWops *)lib.png_get_io_ptr(png_ptr);
SDL_RWwrite(dst, src, size, 1);
}
static void png_flush_data(png_structp png_ptr)
{
Mar 5, 2020
Mar 5, 2020
507
(void)png_ptr;
Nov 5, 2019
Nov 5, 2019
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
}
static int IMG_SavePNG_RW_libpng(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
if (dst) {
png_structp png_ptr;
png_infop info_ptr;
png_colorp color_ptr = NULL;
SDL_Surface *source = surface;
SDL_Palette *palette;
int png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
png_ptr = lib.png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
IMG_SetError("Couldn't allocate memory for PNG file or incompatible PNG dll");
return -1;
}
info_ptr = lib.png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
lib.png_destroy_write_struct(&png_ptr, NULL);
IMG_SetError("Couldn't create image information for PNG file");
return -1;
Sep 12, 2017
Sep 12, 2017
531
}
Nov 5, 2019
Nov 5, 2019
532
533
534
#ifdef PNG_SETJMP_SUPPORTED
#ifndef LIBPNG_VERSION_12
if (setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))))
Nov 3, 2019
Nov 3, 2019
535
#else
Nov 5, 2019
Nov 5, 2019
536
if (setjmp(png_ptr->jmpbuf))
Nov 3, 2019
Nov 3, 2019
537
#endif
Nov 5, 2019
Nov 5, 2019
538
539
540
541
542
#endif
{
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
IMG_SetError("Error writing the PNG file.");
return -1;
Nov 1, 2019
Nov 1, 2019
543
}
Nov 5, 2019
Nov 5, 2019
544
545
546
547
548
549
palette = surface->format->palette;
if (palette) {
const int ncolors = palette->ncolors;
int i;
Mar 5, 2020
Mar 5, 2020
550
color_ptr = (png_colorp)SDL_malloc(sizeof(png_colorp) * ncolors);
Nov 5, 2019
Nov 5, 2019
551
552
553
554
555
if (color_ptr == NULL)
{
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
IMG_SetError("Couldn't create palette for PNG file");
return -1;
Sep 12, 2017
Sep 12, 2017
556
}
Nov 5, 2019
Nov 5, 2019
557
558
559
560
for (i = 0; i < ncolors; i++) {
color_ptr[i].red = palette->colors[i].r;
color_ptr[i].green = palette->colors[i].g;
color_ptr[i].blue = palette->colors[i].b;
Nov 1, 2019
Nov 1, 2019
561
}
Nov 5, 2019
Nov 5, 2019
562
563
564
565
566
lib.png_set_PLTE(png_ptr, info_ptr, color_ptr, ncolors);
png_color_type = PNG_COLOR_TYPE_PALETTE;
}
else if (surface->format->format != png_format) {
source = SDL_ConvertSurfaceFormat(surface, png_format, 0);
Sep 12, 2017
Sep 12, 2017
567
568
}
Nov 5, 2019
Nov 5, 2019
569
lib.png_set_write_fn(png_ptr, dst, png_write_data, png_flush_data);
Sep 12, 2017
Sep 12, 2017
570
Nov 5, 2019
Nov 5, 2019
571
572
573
lib.png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h,
8, png_color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
Sep 12, 2017
Sep 12, 2017
574
Nov 5, 2019
Nov 5, 2019
575
576
577
if (source) {
png_bytep *row_pointers;
int row;
Sep 12, 2017
Sep 12, 2017
578
Nov 5, 2019
Nov 5, 2019
579
580
581
582
583
584
585
586
587
row_pointers = (png_bytep *) SDL_malloc(sizeof(png_bytep) * source->h);
if (!row_pointers) {
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
IMG_SetError("Out of memory");
return -1;
}
for (row = 0; row < (int)source->h; row++) {
row_pointers[row] = (png_bytep) (Uint8 *) source->pixels + row * source->pitch;
}
Sep 12, 2017
Sep 12, 2017
588
Nov 5, 2019
Nov 5, 2019
589
590
lib.png_set_rows(png_ptr, info_ptr, row_pointers);
lib.png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
Nov 5, 2017
Nov 5, 2017
591
Nov 5, 2019
Nov 5, 2019
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
SDL_free(row_pointers);
if (source != surface) {
SDL_FreeSurface(source);
}
}
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
if (color_ptr) {
SDL_free(color_ptr);
}
if (freedst) {
SDL_RWclose(dst);
}
} else {
IMG_SetError("Passed NULL dst");
return -1;
}
return 0;
Nov 1, 2019
Nov 1, 2019
609
}
Sep 12, 2017
Sep 12, 2017
610
Nov 5, 2019
Nov 5, 2019
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#endif /* USE_LIBPNG */
/* Replace C runtime functions with SDL C runtime functions for building on Windows */
#define MINIZ_NO_STDIO
#define MINIZ_NO_TIME
#define MINIZ_SDL_MALLOC
#define MZ_ASSERT(x) SDL_assert(x)
#undef memcpy
#define memcpy SDL_memcpy
#undef memset
#define memset SDL_memset
#define strlen SDL_strlen
#include "miniz.h"
static int IMG_SavePNG_RW_miniz(SDL_Surface *surface, SDL_RWops *dst, int freedst)
Sep 12, 2017
Sep 12, 2017
627
{
Nov 5, 2019
Nov 5, 2019
628
int result = -1;
Jun 3, 2013
Jun 3, 2013
629
Nov 5, 2019
Nov 5, 2019
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
if (dst) {
size_t size = 0;
void *png = NULL;
if (surface->format->format == png_format) {
png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, surface->format->BytesPerPixel, surface->pitch, &size);
} else {
SDL_Surface *cvt = SDL_ConvertSurfaceFormat(surface, png_format, 0);
if (cvt) {
png = tdefl_write_image_to_png_file_in_memory(cvt->pixels, cvt->w, cvt->h, cvt->format->BytesPerPixel, cvt->pitch, &size);
SDL_FreeSurface(cvt);
}
}
if (png) {
if (SDL_RWwrite(dst, png, size, 1)) {
result = 0;
}
SDL_free(png);
} else {
IMG_SetError("Failed to convert and save image");
}
if (freedst) {
SDL_RWclose(dst);
}
Nov 1, 2019
Nov 1, 2019
654
} else {
Nov 5, 2019
Nov 5, 2019
655
IMG_SetError("Passed NULL dst");
Jun 3, 2013
Jun 3, 2013
656
}
Nov 5, 2019
Nov 5, 2019
657
658
return result;
}
Jun 3, 2013
Jun 3, 2013
659
Nov 5, 2019
Nov 5, 2019
660
661
662
663
664
665
666
667
668
669
670
671
int IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
static int (*rw_func)(SDL_Surface *surface, SDL_RWops *dst, int freedst);
if (!rw_func)
{
#ifdef USE_LIBPNG
if (IMG_Init(IMG_INIT_PNG)) {
rw_func = IMG_SavePNG_RW_libpng;
} else
#endif
rw_func = IMG_SavePNG_RW_miniz;
Sep 12, 2017
Sep 12, 2017
672
673
}
Nov 5, 2019
Nov 5, 2019
674
return rw_func(surface, dst, freedst);
Sep 12, 2017
Sep 12, 2017
675
676
}
Jun 3, 2013
Jun 3, 2013
677
#endif /* SAVE_PNG */