Skip to content

Latest commit

 

History

History
852 lines (766 loc) · 28.9 KB

IMG_png.c

File metadata and controls

852 lines (766 loc) · 28.9 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 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 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"
Sep 12, 2017
Sep 12, 2017
26
#if !(defined(__APPLE__) || defined(SDL_IMAGE_USE_WIC_BACKEND)) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Jun 3, 2013
Jun 3, 2013
27
Aug 10, 2000
Aug 10, 2000
28
29
#ifdef LOAD_PNG
Sep 12, 2017
Sep 12, 2017
30
31
#define USE_LIBPNG
Aug 10, 2000
Aug 10, 2000
32
33
/*=============================================================================
File: SDL_png.c
May 22, 2013
May 22, 2013
34
35
Purpose: A PNG loader and saver for the SDL library
Revision:
Aug 10, 2000
Aug 10, 2000
36
37
Created by: Philippe Lavoie (2 November 1998)
lavoie@zeus.genie.uottawa.ca
May 22, 2013
May 22, 2013
38
Modified by:
Aug 10, 2000
Aug 10, 2000
39
40
41
Copyright notice:
Copyright (C) 1998 Philippe Lavoie
May 22, 2013
May 22, 2013
42
Aug 10, 2000
Aug 10, 2000
43
44
45
46
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
May 22, 2013
May 22, 2013
47
Aug 10, 2000
Aug 10, 2000
48
49
50
51
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
May 22, 2013
May 22, 2013
52
Aug 10, 2000
Aug 10, 2000
53
54
55
56
57
58
59
60
61
62
63
64
65
66
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Comments: The load and save routine are basically the ones you can find
in the example.c file from the libpng distribution.
Changes:
5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
=============================================================================*/
#include "SDL_endian.h"
Nov 30, 2000
Nov 30, 2000
67
68
69
#ifdef macintosh
#define MACOS
#endif
Aug 10, 2000
Aug 10, 2000
70
71
#include <png.h>
Jan 20, 2011
Jan 20, 2011
72
/* Check for the older version of libpng */
Jun 1, 2013
Jun 1, 2013
73
#if (PNG_LIBPNG_VER_MAJOR == 1)
Oct 12, 2013
Oct 12, 2013
74
#if (PNG_LIBPNG_VER_MINOR < 5)
Jan 20, 2011
Jan 20, 2011
75
#define LIBPNG_VERSION_12
Jul 11, 2014
Jul 11, 2014
76
typedef png_bytep png_const_bytep;
Jun 1, 2013
Jun 1, 2013
77
78
#endif
#if (PNG_LIBPNG_VER_MINOR < 6)
May 28, 2013
May 28, 2013
79
80
typedef png_structp png_const_structrp;
typedef png_infop png_const_inforp;
Jun 1, 2013
Jun 1, 2013
81
#endif
Jan 20, 2011
Jan 20, 2011
82
#endif
Aug 10, 2000
Aug 10, 2000
83
May 12, 2006
May 12, 2006
84
static struct {
May 22, 2013
May 22, 2013
85
86
int loaded;
void *handle;
May 28, 2013
May 28, 2013
87
png_infop (*png_create_info_struct) (png_const_structrp png_ptr);
May 22, 2013
May 22, 2013
88
89
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);
May 28, 2013
May 28, 2013
90
91
92
93
94
95
png_uint_32 (*png_get_IHDR) (png_const_structrp png_ptr, png_const_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_const_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_infop info_ptr, png_colorp *palette, int *num_palette);
png_uint_32 (*png_get_tRNS) (png_const_structrp png_ptr, png_infop 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);
May 22, 2013
May 22, 2013
96
97
98
99
100
101
102
103
void (*png_read_image) (png_structp png_ptr, png_bytepp image);
void (*png_read_info) (png_structp png_ptr, png_infop info_ptr);
void (*png_read_update_info) (png_structp png_ptr, png_infop info_ptr);
void (*png_set_expand) (png_structp png_ptr);
void (*png_set_gray_to_rgb) (png_structp png_ptr);
void (*png_set_packing) (png_structp png_ptr);
void (*png_set_read_fn) (png_structp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn);
void (*png_set_strip_16) (png_structp png_ptr);
May 28, 2013
May 28, 2013
104
int (*png_sig_cmp) (png_const_bytep sig, png_size_t start, png_size_t num_to_check);
Jan 20, 2011
Jan 20, 2011
105
#ifndef LIBPNG_VERSION_12
May 22, 2013
May 22, 2013
106
jmp_buf* (*png_set_longjmp_fn) (png_structp, png_longjmp_ptr, size_t);
Jan 20, 2011
Jan 20, 2011
107
#endif
Sep 12, 2017
Sep 12, 2017
108
109
110
111
112
113
114
115
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_structp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn);
void (*png_set_IHDR) (png_structp png_ptr, png_infop 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_structp png_ptr, png_infop info_ptr);
void (*png_set_rows) (png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers);
void (*png_write_png) (png_structp png_ptr, png_infop info_ptr, int transforms, png_voidp params);
void (*png_set_PLTE) (png_structp png_ptr, png_infop info_ptr, png_colorp palette, int num_palette);
May 12, 2006
May 12, 2006
116
117
118
} lib;
#ifdef LOAD_PNG_DYNAMIC
Sep 26, 2009
Sep 26, 2009
119
int IMG_InitPNG()
May 12, 2006
May 12, 2006
120
{
May 22, 2013
May 22, 2013
121
122
123
124
125
126
if ( lib.loaded == 0 ) {
lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
lib.png_create_info_struct =
May 28, 2013
May 28, 2013
127
(png_infop (*) (png_const_structrp))
May 22, 2013
May 22, 2013
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
SDL_LoadFunction(lib.handle, "png_create_info_struct");
if ( lib.png_create_info_struct == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_create_read_struct =
(png_structp (*) (png_const_charp, png_voidp, png_error_ptr, png_error_ptr))
SDL_LoadFunction(lib.handle, "png_create_read_struct");
if ( lib.png_create_read_struct == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_destroy_read_struct =
(void (*) (png_structpp, png_infopp, png_infopp))
SDL_LoadFunction(lib.handle, "png_destroy_read_struct");
if ( lib.png_destroy_read_struct == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_IHDR =
May 28, 2013
May 28, 2013
148
(png_uint_32 (*) (png_const_structrp, png_const_inforp, png_uint_32 *, png_uint_32 *, int *, int *, int *, int *, int *))
May 22, 2013
May 22, 2013
149
150
151
152
153
154
SDL_LoadFunction(lib.handle, "png_get_IHDR");
if ( lib.png_get_IHDR == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_channels =
May 28, 2013
May 28, 2013
155
(png_byte (*) (png_const_structrp, png_const_inforp))
May 22, 2013
May 22, 2013
156
157
158
159
160
161
SDL_LoadFunction(lib.handle, "png_get_channels");
if ( lib.png_get_channels == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_io_ptr =
May 28, 2013
May 28, 2013
162
(png_voidp (*) (png_const_structrp))
May 22, 2013
May 22, 2013
163
164
165
166
167
168
SDL_LoadFunction(lib.handle, "png_get_io_ptr");
if ( lib.png_get_io_ptr == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_PLTE =
May 28, 2013
May 28, 2013
169
(png_uint_32 (*) (png_const_structrp, png_infop, png_colorp *, int *))
May 22, 2013
May 22, 2013
170
171
172
173
174
175
SDL_LoadFunction(lib.handle, "png_get_PLTE");
if ( lib.png_get_PLTE == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_tRNS =
May 28, 2013
May 28, 2013
176
(png_uint_32 (*) (png_const_structrp, png_infop, png_bytep *, int *, png_color_16p *))
May 22, 2013
May 22, 2013
177
178
179
180
181
182
SDL_LoadFunction(lib.handle, "png_get_tRNS");
if ( lib.png_get_tRNS == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_valid =
May 28, 2013
May 28, 2013
183
(png_uint_32 (*) (png_const_structrp, png_const_inforp, png_uint_32))
May 22, 2013
May 22, 2013
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
SDL_LoadFunction(lib.handle, "png_get_valid");
if ( lib.png_get_valid == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_read_image =
(void (*) (png_structp, png_bytepp))
SDL_LoadFunction(lib.handle, "png_read_image");
if ( lib.png_read_image == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_read_info =
(void (*) (png_structp, png_infop))
SDL_LoadFunction(lib.handle, "png_read_info");
if ( lib.png_read_info == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_read_update_info =
(void (*) (png_structp, png_infop))
SDL_LoadFunction(lib.handle, "png_read_update_info");
if ( lib.png_read_update_info == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_expand =
(void (*) (png_structp))
SDL_LoadFunction(lib.handle, "png_set_expand");
if ( lib.png_set_expand == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_gray_to_rgb =
(void (*) (png_structp))
SDL_LoadFunction(lib.handle, "png_set_gray_to_rgb");
if ( lib.png_set_gray_to_rgb == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_packing =
(void (*) (png_structp))
SDL_LoadFunction(lib.handle, "png_set_packing");
if ( lib.png_set_packing == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_read_fn =
(void (*) (png_structp, png_voidp, png_rw_ptr))
SDL_LoadFunction(lib.handle, "png_set_read_fn");
if ( lib.png_set_read_fn == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_strip_16 =
(void (*) (png_structp))
SDL_LoadFunction(lib.handle, "png_set_strip_16");
if ( lib.png_set_strip_16 == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_sig_cmp =
May 28, 2013
May 28, 2013
246
(int (*) (png_const_bytep, png_size_t, png_size_t))
May 22, 2013
May 22, 2013
247
248
249
250
251
SDL_LoadFunction(lib.handle, "png_sig_cmp");
if ( lib.png_sig_cmp == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
Jan 20, 2011
Jan 20, 2011
252
#ifndef LIBPNG_VERSION_12
May 22, 2013
May 22, 2013
253
254
255
256
257
258
259
lib.png_set_longjmp_fn =
(jmp_buf * (*) (png_structp, png_longjmp_ptr, size_t))
SDL_LoadFunction(lib.handle, "png_set_longjmp_fn");
if ( lib.png_set_longjmp_fn == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
Jan 20, 2011
Jan 20, 2011
260
#endif
Sep 12, 2017
Sep 12, 2017
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
lib.png_create_write_struct =
(png_structp (*) (png_const_charp, png_voidp, png_error_ptr, png_error_ptr))
SDL_LoadFunction(lib.handle, "png_create_write_struct");
if ( lib.png_create_write_struct == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_destroy_write_struct =
(void (*) (png_structpp, png_infopp))
SDL_LoadFunction(lib.handle, "png_destroy_write_struct");
if ( lib.png_destroy_write_struct == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_write_fn =
(void (*) (png_structp, png_voidp, png_rw_ptr, png_flush_ptr))
SDL_LoadFunction(lib.handle, "png_set_write_fn");
if ( lib.png_set_write_fn == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_IHDR =
(void (*) (png_structp, png_infop, png_uint_32, png_uint_32, int, int, int, int, int))
SDL_LoadFunction(lib.handle, "png_set_IHDR");
if ( lib.png_set_IHDR == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_write_info =
(void (*) (png_structp, png_infop))
SDL_LoadFunction(lib.handle, "png_write_info");
if ( lib.png_write_info == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_rows =
(void (*) (png_structp, png_infop, png_bytepp))
SDL_LoadFunction(lib.handle, "png_set_rows");
if ( lib.png_set_rows == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_write_png =
(void (*) (png_structp, png_infop, int, png_voidp))
SDL_LoadFunction(lib.handle, "png_write_png");
if ( lib.png_write_png == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_PLTE =
(void (*) (png_structp, png_infop, png_colorp, int))
SDL_LoadFunction(lib.handle, "png_set_PLTE");
if ( lib.png_set_PLTE == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
May 22, 2013
May 22, 2013
317
318
}
++lib.loaded;
May 12, 2006
May 12, 2006
319
May 22, 2013
May 22, 2013
320
return 0;
May 12, 2006
May 12, 2006
321
}
Sep 26, 2009
Sep 26, 2009
322
void IMG_QuitPNG()
May 12, 2006
May 12, 2006
323
{
May 22, 2013
May 22, 2013
324
325
326
327
328
329
330
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
SDL_UnloadObject(lib.handle);
}
--lib.loaded;
May 12, 2006
May 12, 2006
331
332
}
#else
Sep 26, 2009
Sep 26, 2009
333
int IMG_InitPNG()
May 12, 2006
May 12, 2006
334
{
May 22, 2013
May 22, 2013
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
if ( lib.loaded == 0 ) {
lib.png_create_info_struct = png_create_info_struct;
lib.png_create_read_struct = png_create_read_struct;
lib.png_destroy_read_struct = png_destroy_read_struct;
lib.png_get_IHDR = png_get_IHDR;
lib.png_get_channels = png_get_channels;
lib.png_get_io_ptr = png_get_io_ptr;
lib.png_get_PLTE = png_get_PLTE;
lib.png_get_tRNS = png_get_tRNS;
lib.png_get_valid = png_get_valid;
lib.png_read_image = png_read_image;
lib.png_read_info = png_read_info;
lib.png_read_update_info = png_read_update_info;
lib.png_set_expand = png_set_expand;
lib.png_set_gray_to_rgb = png_set_gray_to_rgb;
lib.png_set_packing = png_set_packing;
lib.png_set_read_fn = png_set_read_fn;
lib.png_set_strip_16 = png_set_strip_16;
lib.png_sig_cmp = png_sig_cmp;
Feb 8, 2011
Feb 8, 2011
354
#ifndef LIBPNG_VERSION_12
May 22, 2013
May 22, 2013
355
lib.png_set_longjmp_fn = png_set_longjmp_fn;
Feb 8, 2011
Feb 8, 2011
356
#endif
Sep 12, 2017
Sep 12, 2017
357
358
359
360
361
362
363
364
lib.png_create_write_struct = png_create_write_struct;
lib.png_destroy_write_struct = png_destroy_write_struct;
lib.png_set_write_fn = png_set_write_fn;
lib.png_set_IHDR = png_set_IHDR;
lib.png_write_info = png_write_info;
lib.png_set_rows = png_set_rows;
lib.png_write_png = png_write_png;
lib.png_set_PLTE = png_set_PLTE;
May 22, 2013
May 22, 2013
365
366
}
++lib.loaded;
May 13, 2006
May 13, 2006
367
May 22, 2013
May 22, 2013
368
return 0;
May 12, 2006
May 12, 2006
369
}
Sep 26, 2009
Sep 26, 2009
370
void IMG_QuitPNG()
May 12, 2006
May 12, 2006
371
{
May 22, 2013
May 22, 2013
372
373
374
375
376
377
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
}
--lib.loaded;
May 12, 2006
May 12, 2006
378
379
380
}
#endif /* LOAD_PNG_DYNAMIC */
Aug 10, 2000
Aug 10, 2000
381
382
383
/* See if an image is contained in a data source */
int IMG_isPNG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
384
385
386
387
Sint64 start;
int is_PNG;
Uint8 magic[4];
Jun 3, 2013
Jun 3, 2013
388
if ( !src ) {
May 22, 2013
May 22, 2013
389
return 0;
Jun 3, 2013
Jun 3, 2013
390
391
}
May 22, 2013
May 22, 2013
392
393
394
start = SDL_RWtell(src);
is_PNG = 0;
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
Jun 3, 2013
Jun 3, 2013
395
396
397
398
if ( magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G' ) {
May 22, 2013
May 22, 2013
399
400
401
402
403
is_PNG = 1;
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_PNG);
Aug 10, 2000
Aug 10, 2000
404
405
406
407
408
}
/* Load a PNG type image from an SDL datasource */
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
{
May 22, 2013
May 22, 2013
409
SDL_RWops *src;
Aug 10, 2000
Aug 10, 2000
410
May 22, 2013
May 22, 2013
411
412
src = (SDL_RWops *)lib.png_get_io_ptr(ctx);
SDL_RWread(src, area, size, 1);
Aug 10, 2000
Aug 10, 2000
413
414
415
}
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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);
Feb 15, 2016
Feb 15, 2016
439
if ( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0 ) {
May 22, 2013
May 22, 2013
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
return NULL;
}
/* Initialize the data we will clean up when we're done */
error = NULL;
png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
/* 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;
}
/* 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;
}
/* 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.
*/
Jan 20, 2011
Jan 20, 2011
466
#ifndef LIBPNG_VERSION_12
May 22, 2013
May 22, 2013
467
if ( setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))) )
Jan 20, 2011
Jan 20, 2011
468
#else
May 22, 2013
May 22, 2013
469
if ( setjmp(png_ptr->jmpbuf) )
Jan 20, 2011
Jan 20, 2011
470
#endif
May 22, 2013
May 22, 2013
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
{
error = "Error reading the PNG file.";
goto done;
}
/* 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) ;
/* 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)) {
Jun 3, 2013
Jun 3, 2013
500
int num_trans;
May 22, 2013
May 22, 2013
501
Uint8 *trans;
Jun 3, 2013
Jun 3, 2013
502
lib.png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &transv);
May 22, 2013
May 22, 2013
503
504
505
506
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++) {
Jun 3, 2013
Jun 3, 2013
507
508
509
if (trans[j] == 0) {
if (t >= 0) {
break;
Mar 23, 2013
Mar 23, 2013
510
}
Jun 3, 2013
Jun 3, 2013
511
512
513
514
515
t = j;
} else if (trans[j] != 255) {
break;
}
}
May 22, 2013
May 22, 2013
516
if (j == num_trans) {
Jun 3, 2013
Jun 3, 2013
517
518
/* exactly one transparent index */
ckey = t;
May 22, 2013
May 22, 2013
519
} else {
Jun 3, 2013
Jun 3, 2013
520
521
/* more than one transparent index, or translucency */
lib.png_set_expand(png_ptr);
May 22, 2013
May 22, 2013
522
}
Jun 3, 2013
Jun 3, 2013
523
} else {
May 22, 2013
May 22, 2013
524
ckey = 0; /* actual value will be set later */
Jun 3, 2013
Jun 3, 2013
525
}
May 22, 2013
May 22, 2013
526
527
528
529
530
531
532
533
534
535
536
537
538
539
}
if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
lib.png_set_gray_to_rgb(png_ptr);
lib.png_read_update_info(png_ptr, info_ptr);
lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, &interlace_type, NULL, NULL);
/* 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 ) {
Feb 3, 2013
Feb 3, 2013
540
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Jun 3, 2013
Jun 3, 2013
541
542
543
544
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = (num_channels == 4) ? 0xFF000000 : 0;
Feb 3, 2013
Feb 3, 2013
545
#else
Jun 3, 2013
Jun 3, 2013
546
547
548
549
550
int s = (num_channels == 4) ? 0 : 8;
Rmask = 0xFF000000 >> s;
Gmask = 0x00FF0000 >> s;
Bmask = 0x0000FF00 >> s;
Amask = 0x000000FF >> s;
Feb 3, 2013
Feb 3, 2013
551
#endif
May 22, 2013
May 22, 2013
552
553
554
555
556
557
558
559
560
}
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
bit_depth*num_channels, Rmask,Gmask,Bmask,Amask);
if ( surface == NULL ) {
error = SDL_GetError();
goto done;
}
if (ckey != -1) {
Jun 3, 2013
Jun 3, 2013
561
if (color_type != PNG_COLOR_TYPE_PALETTE) {
May 22, 2013
May 22, 2013
562
/* FIXME: Should these be truncated or shifted down? */
Jun 3, 2013
Jun 3, 2013
563
564
565
566
567
568
ckey = SDL_MapRGB(surface->format,
(Uint8)transv->red,
(Uint8)transv->green,
(Uint8)transv->blue);
}
SDL_SetColorKey(surface, SDL_TRUE, ckey);
May 22, 2013
May 22, 2013
569
570
571
572
}
/* Create the array of pointers to image data */
row_pointers = (png_bytep*) SDL_malloc(sizeof(png_bytep)*height);
Sep 7, 2013
Sep 7, 2013
573
if (!row_pointers) {
May 22, 2013
May 22, 2013
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
error = "Out of memory";
goto done;
}
for (row = 0; row < (int)height; row++) {
row_pointers[row] = (png_bytep)
(Uint8 *)surface->pixels + row*surface->pitch;
}
/* 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) {
Jun 3, 2013
Jun 3, 2013
600
601
602
603
604
605
palette->ncolors = 256;
for (i = 0; i < 256; i++) {
palette->colors[i].r = i;
palette->colors[i].g = i;
palette->colors[i].b = i;
}
May 22, 2013
May 22, 2013
606
} else if (png_num_palette > 0 ) {
Jun 3, 2013
Jun 3, 2013
607
608
609
610
611
612
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
}
}
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;
}
Jun 15, 2014
Jun 15, 2014
631
IMG_SetError("%s", error);
May 22, 2013
May 22, 2013
632
633
}
return(surface);
Aug 10, 2000
Aug 10, 2000
634
635
636
637
}
#else
Sep 26, 2009
Sep 26, 2009
638
639
int IMG_InitPNG()
{
May 22, 2013
May 22, 2013
640
641
IMG_SetError("PNG images are not supported");
return(-1);
Sep 26, 2009
Sep 26, 2009
642
643
644
645
646
647
}
void IMG_QuitPNG()
{
}
Aug 10, 2000
Aug 10, 2000
648
649
650
/* See if an image is contained in a data source */
int IMG_isPNG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
651
return(0);
Aug 10, 2000
Aug 10, 2000
652
653
654
655
656
}
/* Load a PNG type image from an SDL datasource */
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
657
return(NULL);
Aug 10, 2000
Aug 10, 2000
658
659
660
}
#endif /* LOAD_PNG */
Nov 10, 2009
Nov 10, 2009
661
662
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */
Jun 3, 2013
Jun 3, 2013
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/* We'll always have PNG save support */
#define SAVE_PNG
#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);
} else {
return -1;
}
}
Sep 12, 2017
Sep 12, 2017
679
680
681
682
683
684
685
686
687
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
static const Uint32 png_format = SDL_PIXELFORMAT_ABGR8888;
#else
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)
Jun 3, 2013
Jun 3, 2013
688
{
Sep 12, 2017
Sep 12, 2017
689
690
691
692
693
694
695
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)
{
}
Jun 3, 2013
Jun 3, 2013
696
Sep 12, 2017
Sep 12, 2017
697
698
static int IMG_SavePNG_RW_libpng(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
Jun 3, 2013
Jun 3, 2013
699
if (dst) {
Sep 12, 2017
Sep 12, 2017
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
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) {
SDL_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);
SDL_SetError("Couldn't create image information for PNG file");
return -1;
}
#ifndef LIBPNG_VERSION_12
if (setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))))
Jun 3, 2013
Jun 3, 2013
722
#else
Sep 12, 2017
Sep 12, 2017
723
if (setjmp(png_ptr->jmpbuf))
Jun 3, 2013
Jun 3, 2013
724
#endif
Sep 12, 2017
Sep 12, 2017
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
{
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
SDL_SetError("Error writing the PNG file.");
return -1;
}
palette = surface->format->palette;
if (palette) {
const int ncolors = palette->ncolors;
int i;
color_ptr = SDL_malloc(sizeof(png_colorp) * ncolors);
if (color_ptr == NULL)
{
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
SDL_SetError("Couldn't create palette for PNG file");
return -1;
}
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;
}
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);
}
lib.png_set_write_fn(png_ptr, dst, png_write_data, png_flush_data);
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);
if (source) {
png_bytep *row_pointers;
int row;
row_pointers = (png_bytep *) SDL_malloc(sizeof(png_bytep) * source->h);
if (!row_pointers) {
lib.png_destroy_write_struct(&png_ptr, &info_ptr);
SDL_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;
}
lib.png_set_rows(png_ptr, info_ptr, row_pointers);
lib.png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
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 {
SDL_SetError("Passed NULL dst");
return -1;
}
return 0;
}
#endif /* USE_LIBPNG */
#include "miniz.h"
static int IMG_SavePNG_RW_miniz(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
int result = -1;
if (dst) {
Jun 3, 2013
Jun 3, 2013
806
size_t size;
Sep 28, 2013
Sep 28, 2013
807
void *png = NULL;
Jun 3, 2013
Jun 3, 2013
808
809
if (surface->format->format == png_format) {
Oct 14, 2013
Oct 14, 2013
810
png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, surface->format->BytesPerPixel, surface->pitch, &size);
Jun 3, 2013
Jun 3, 2013
811
812
813
} else {
SDL_Surface *cvt = SDL_ConvertSurfaceFormat(surface, png_format, 0);
if (cvt) {
Oct 14, 2013
Oct 14, 2013
814
png = tdefl_write_image_to_png_file_in_memory(cvt->pixels, cvt->w, cvt->h, cvt->format->BytesPerPixel, cvt->pitch, &size);
Jun 3, 2013
Jun 3, 2013
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
SDL_FreeSurface(cvt);
}
}
if (png) {
if (SDL_RWwrite(dst, png, size, 1)) {
result = 0;
}
SDL_free(png);
} else {
SDL_SetError("Failed to convert and save image");
}
if (freedst) {
SDL_RWclose(dst);
}
} else {
SDL_SetError("Passed NULL dst");
}
return result;
}
Sep 12, 2017
Sep 12, 2017
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
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;
}
return rw_func(surface, dst, freedst);
}
Jun 3, 2013
Jun 3, 2013
852
#endif /* SAVE_PNG */