Skip to content

Latest commit

 

History

History
619 lines (561 loc) · 18.8 KB

IMG_png.c

File metadata and controls

619 lines (561 loc) · 18.8 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
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
*/
Nov 10, 2009
Nov 10, 2009
22
23
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Aug 10, 2000
Aug 10, 2000
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
/* This is a PNG image file loading framework */
#include <stdlib.h>
#include <stdio.h>
#include "SDL_image.h"
#ifdef LOAD_PNG
/*=============================================================================
File: SDL_png.c
Purpose: A PNG loader and saver for the SDL library
Revision:
Created by: Philippe Lavoie (2 November 1998)
lavoie@zeus.genie.uottawa.ca
Modified by:
Copyright notice:
Copyright (C) 1998 Philippe Lavoie
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.
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.
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
68
69
70
#ifdef macintosh
#define MACOS
#endif
Aug 10, 2000
Aug 10, 2000
71
72
#include <png.h>
Jan 20, 2011
Jan 20, 2011
73
/* Check for the older version of libpng */
Oct 23, 2018
Oct 23, 2018
74
#if (PNG_LIBPNG_VER_MAJOR == 1)
Oct 14, 2018
Oct 14, 2018
75
#if (PNG_LIBPNG_VER_MINOR < 5)
Jan 20, 2011
Jan 20, 2011
76
#define LIBPNG_VERSION_12
Oct 14, 2018
Oct 14, 2018
77
78
typedef png_bytep png_const_bytep;
#endif
Oct 23, 2018
Oct 23, 2018
79
80
81
82
#if (PNG_LIBPNG_VER_MINOR < 4)
typedef png_structp png_const_structp;
typedef png_infop png_const_infop;
#endif
Oct 14, 2018
Oct 14, 2018
83
84
85
#if (PNG_LIBPNG_VER_MINOR < 6)
typedef png_structp png_structrp;
typedef png_infop png_inforp;
Oct 23, 2018
Oct 23, 2018
86
87
88
89
90
91
92
93
94
95
96
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;
Oct 14, 2018
Oct 14, 2018
97
#endif
Oct 23, 2018
Oct 23, 2018
98
99
100
101
#else
typedef png_const_structp png_noconst15_structrp;
typedef png_const_inforp png_noconst15_inforp;
typedef png_inforp png_noconst16_inforp;
Jan 20, 2011
Jan 20, 2011
102
#endif
Aug 10, 2000
Aug 10, 2000
103
May 12, 2006
May 12, 2006
104
105
106
static struct {
int loaded;
void *handle;
Oct 23, 2018
Oct 23, 2018
107
png_infop (*png_create_info_struct) (png_noconst15_structrp png_ptr);
May 12, 2006
May 12, 2006
108
109
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);
Oct 23, 2018
Oct 23, 2018
110
111
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);
Oct 14, 2018
Oct 14, 2018
112
png_byte (*png_get_channels) (png_const_structrp png_ptr, png_const_inforp info_ptr);
Oct 23, 2018
Oct 23, 2018
113
png_uint_32 (*png_get_PLTE) (png_const_structrp png_ptr, png_noconst16_inforp info_ptr, png_colorp *palette, int *num_palette);
Oct 14, 2018
Oct 14, 2018
114
115
116
117
118
119
120
121
122
123
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);
Jul 10, 2019
Jul 10, 2019
124
int (*png_set_interlace_handling) (png_structrp png_ptr);
Oct 14, 2018
Oct 14, 2018
125
int (*png_sig_cmp) (png_const_bytep sig, png_size_t start, png_size_t num_to_check);
Jan 20, 2011
Jan 20, 2011
126
#ifndef LIBPNG_VERSION_12
Oct 14, 2018
Oct 14, 2018
127
jmp_buf* (*png_set_longjmp_fn) (png_structrp, png_longjmp_ptr, size_t);
Jan 20, 2011
Jan 20, 2011
128
#endif
May 12, 2006
May 12, 2006
129
130
131
} lib;
#ifdef LOAD_PNG_DYNAMIC
Sep 26, 2009
Sep 26, 2009
132
int IMG_InitPNG()
May 12, 2006
May 12, 2006
133
134
135
136
137
138
139
{
if ( lib.loaded == 0 ) {
lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
lib.png_create_info_struct =
Oct 23, 2018
Oct 23, 2018
140
(png_infop (*) (png_noconst15_structrp))
May 12, 2006
May 12, 2006
141
142
143
144
145
146
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 =
Oct 14, 2018
Oct 14, 2018
147
(png_structrp (*) (png_const_charp, png_voidp, png_error_ptr, png_error_ptr))
May 12, 2006
May 12, 2006
148
149
150
151
152
153
154
155
156
157
158
159
160
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 =
Oct 23, 2018
Oct 23, 2018
161
(png_uint_32 (*) (png_noconst15_structrp, png_noconst15_inforp, png_uint_32 *, png_uint_32 *, int *, int *, int *, int *, int *))
May 12, 2006
May 12, 2006
162
163
164
165
166
SDL_LoadFunction(lib.handle, "png_get_IHDR");
if ( lib.png_get_IHDR == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
Jan 20, 2011
Jan 20, 2011
167
lib.png_get_channels =
Oct 14, 2018
Oct 14, 2018
168
(png_byte (*) (png_const_structrp, png_const_inforp))
Jan 20, 2011
Jan 20, 2011
169
170
171
172
173
SDL_LoadFunction(lib.handle, "png_get_channels");
if ( lib.png_get_channels == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
May 12, 2006
May 12, 2006
174
lib.png_get_io_ptr =
Oct 23, 2018
Oct 23, 2018
175
(png_voidp (*) (png_noconst15_structrp))
May 12, 2006
May 12, 2006
176
177
178
179
180
SDL_LoadFunction(lib.handle, "png_get_io_ptr");
if ( lib.png_get_io_ptr == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
Jan 20, 2011
Jan 20, 2011
181
lib.png_get_PLTE =
Oct 23, 2018
Oct 23, 2018
182
(png_uint_32 (*) (png_const_structrp, png_noconst16_inforp, png_colorp *, int *))
Jan 20, 2011
Jan 20, 2011
183
184
185
186
187
SDL_LoadFunction(lib.handle, "png_get_PLTE");
if ( lib.png_get_PLTE == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
May 12, 2006
May 12, 2006
188
lib.png_get_tRNS =
Oct 14, 2018
Oct 14, 2018
189
(png_uint_32 (*) (png_const_structrp, png_inforp, png_bytep *, int *, png_color_16p *))
May 12, 2006
May 12, 2006
190
191
192
193
194
195
SDL_LoadFunction(lib.handle, "png_get_tRNS");
if ( lib.png_get_tRNS == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_get_valid =
Oct 14, 2018
Oct 14, 2018
196
(png_uint_32 (*) (png_const_structrp, png_const_inforp, png_uint_32))
May 12, 2006
May 12, 2006
197
198
199
200
201
202
SDL_LoadFunction(lib.handle, "png_get_valid");
if ( lib.png_get_valid == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_read_image =
Oct 14, 2018
Oct 14, 2018
203
(void (*) (png_structrp, png_bytepp))
May 12, 2006
May 12, 2006
204
205
206
207
208
209
SDL_LoadFunction(lib.handle, "png_read_image");
if ( lib.png_read_image == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_read_info =
Oct 14, 2018
Oct 14, 2018
210
(void (*) (png_structrp, png_inforp))
May 12, 2006
May 12, 2006
211
212
213
214
215
216
SDL_LoadFunction(lib.handle, "png_read_info");
if ( lib.png_read_info == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_read_update_info =
Oct 14, 2018
Oct 14, 2018
217
(void (*) (png_structrp, png_inforp))
May 12, 2006
May 12, 2006
218
219
220
221
222
223
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 =
Oct 14, 2018
Oct 14, 2018
224
(void (*) (png_structrp))
May 12, 2006
May 12, 2006
225
226
227
228
229
230
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 =
Oct 14, 2018
Oct 14, 2018
231
(void (*) (png_structrp))
May 12, 2006
May 12, 2006
232
233
234
235
236
237
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 =
Oct 14, 2018
Oct 14, 2018
238
(void (*) (png_structrp))
May 12, 2006
May 12, 2006
239
240
241
242
243
244
SDL_LoadFunction(lib.handle, "png_set_packing");
if ( lib.png_set_packing == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.png_set_read_fn =
Oct 14, 2018
Oct 14, 2018
245
(void (*) (png_structrp, png_voidp, png_rw_ptr))
May 12, 2006
May 12, 2006
246
247
248
249
250
251
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 =
Oct 14, 2018
Oct 14, 2018
252
(void (*) (png_structrp))
May 12, 2006
May 12, 2006
253
254
255
256
257
SDL_LoadFunction(lib.handle, "png_set_strip_16");
if ( lib.png_set_strip_16 == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
Oct 14, 2018
Oct 14, 2018
258
lib.png_set_interlace_handling =
Jul 10, 2019
Jul 10, 2019
259
(int (*) (png_structrp))
Oct 14, 2018
Oct 14, 2018
260
261
262
263
264
SDL_LoadFunction(lib.handle, "png_set_interlace_handling");
if ( lib.png_set_interlace_handling == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
May 12, 2006
May 12, 2006
265
lib.png_sig_cmp =
Oct 14, 2018
Oct 14, 2018
266
(int (*) (png_const_bytep, png_size_t, png_size_t))
May 12, 2006
May 12, 2006
267
268
269
270
271
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
272
#ifndef LIBPNG_VERSION_12
Jan 20, 2011
Jan 20, 2011
273
lib.png_set_longjmp_fn =
Oct 14, 2018
Oct 14, 2018
274
(jmp_buf * (*) (png_structrp, png_longjmp_ptr, size_t))
Jan 20, 2011
Jan 20, 2011
275
276
277
278
279
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
280
#endif
May 12, 2006
May 12, 2006
281
282
283
284
285
}
++lib.loaded;
return 0;
}
Sep 26, 2009
Sep 26, 2009
286
void IMG_QuitPNG()
May 12, 2006
May 12, 2006
287
288
289
290
291
292
293
294
295
296
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
SDL_UnloadObject(lib.handle);
}
--lib.loaded;
}
#else
Sep 26, 2009
Sep 26, 2009
297
int IMG_InitPNG()
May 12, 2006
May 12, 2006
298
299
300
301
302
303
{
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;
Jan 20, 2011
Jan 20, 2011
304
lib.png_get_channels = png_get_channels;
May 12, 2006
May 12, 2006
305
lib.png_get_io_ptr = png_get_io_ptr;
Jan 20, 2011
Jan 20, 2011
306
lib.png_get_PLTE = png_get_PLTE;
May 12, 2006
May 12, 2006
307
308
309
310
311
312
313
314
315
316
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;
Oct 14, 2018
Oct 14, 2018
317
lib.png_set_interlace_handling = png_set_interlace_handling;
May 12, 2006
May 12, 2006
318
lib.png_sig_cmp = png_sig_cmp;
Feb 8, 2011
Feb 8, 2011
319
320
321
#ifndef LIBPNG_VERSION_12
lib.png_set_longjmp_fn = png_set_longjmp_fn;
#endif
May 12, 2006
May 12, 2006
322
323
}
++lib.loaded;
May 13, 2006
May 13, 2006
324
325
return 0;
May 12, 2006
May 12, 2006
326
}
Sep 26, 2009
Sep 26, 2009
327
void IMG_QuitPNG()
May 12, 2006
May 12, 2006
328
329
330
331
332
333
334
335
336
337
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
}
--lib.loaded;
}
#endif /* LOAD_PNG_DYNAMIC */
Aug 10, 2000
Aug 10, 2000
338
339
340
/* See if an image is contained in a data source */
int IMG_isPNG(SDL_RWops *src)
{
Feb 4, 2006
Feb 4, 2006
341
342
int start;
int is_PNG;
Jul 18, 2007
Jul 18, 2007
343
Uint8 magic[4];
Feb 4, 2006
Feb 4, 2006
344
Feb 13, 2007
Feb 13, 2007
345
346
if ( !src )
return 0;
Feb 4, 2006
Feb 4, 2006
347
348
start = SDL_RWtell(src);
is_PNG = 0;
Jul 18, 2007
Jul 18, 2007
349
350
351
352
353
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G' ) {
Jul 18, 2007
Jul 18, 2007
354
355
is_PNG = 1;
}
Feb 4, 2006
Feb 4, 2006
356
}
Nov 8, 2009
Nov 8, 2009
357
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
358
return(is_PNG);
Aug 10, 2000
Aug 10, 2000
359
360
361
362
363
364
365
}
/* 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;
May 12, 2006
May 12, 2006
366
src = (SDL_RWops *)lib.png_get_io_ptr(ctx);
Aug 10, 2000
Aug 10, 2000
367
368
369
370
SDL_RWread(src, area, size, 1);
}
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
Feb 4, 2006
Feb 4, 2006
371
372
int start;
const char *error;
Nov 29, 2000
Nov 29, 2000
373
SDL_Surface *volatile surface;
Aug 10, 2000
Aug 10, 2000
374
375
376
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
Jan 4, 2012
Jan 4, 2012
377
int bit_depth, color_type, interlace_type, num_channels;
Aug 10, 2000
Aug 10, 2000
378
379
380
381
382
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
SDL_Palette *palette;
Nov 29, 2000
Nov 29, 2000
383
png_bytep *volatile row_pointers;
Aug 10, 2000
Aug 10, 2000
384
int row, i;
Nov 29, 2000
Nov 29, 2000
385
volatile int ckey = -1;
Aug 10, 2000
Aug 10, 2000
386
387
png_color_16 *transv;
Jan 4, 2004
Jan 4, 2004
388
389
390
391
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
392
start = SDL_RWtell(src);
Jan 4, 2004
Jan 4, 2004
393
Oct 16, 2018
Oct 16, 2018
394
if ( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0 ) {
May 12, 2006
May 12, 2006
395
396
397
return NULL;
}
Aug 10, 2000
Aug 10, 2000
398
/* Initialize the data we will clean up when we're done */
Feb 4, 2006
Feb 4, 2006
399
error = NULL;
Aug 10, 2000
Aug 10, 2000
400
401
402
png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
/* Create the PNG loading context structure */
May 12, 2006
May 12, 2006
403
png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
Aug 10, 2000
Aug 10, 2000
404
405
NULL,NULL,NULL);
if (png_ptr == NULL){
Feb 4, 2006
Feb 4, 2006
406
error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
Aug 10, 2000
Aug 10, 2000
407
408
409
410
goto done;
}
/* Allocate/initialize the memory for image information. REQUIRED. */
May 12, 2006
May 12, 2006
411
info_ptr = lib.png_create_info_struct(png_ptr);
Aug 10, 2000
Aug 10, 2000
412
if (info_ptr == NULL) {
Feb 4, 2006
Feb 4, 2006
413
error = "Couldn't create image information for PNG file";
Aug 10, 2000
Aug 10, 2000
414
415
416
417
418
419
420
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
421
422
423
424
425
426
#ifndef LIBPNG_VERSION_12
if ( setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))) )
#else
if ( setjmp(png_ptr->jmpbuf) )
#endif
{
Feb 4, 2006
Feb 4, 2006
427
error = "Error reading the PNG file.";
Aug 10, 2000
Aug 10, 2000
428
429
430
431
goto done;
}
/* Set up the input control */
May 12, 2006
May 12, 2006
432
lib.png_set_read_fn(png_ptr, src, png_read_data);
Aug 10, 2000
Aug 10, 2000
433
434
/* Read PNG header info */
May 12, 2006
May 12, 2006
435
436
lib.png_read_info(png_ptr, info_ptr);
lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
Aug 10, 2000
Aug 10, 2000
437
438
439
&color_type, &interlace_type, NULL, NULL);
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
May 12, 2006
May 12, 2006
440
lib.png_set_strip_16(png_ptr) ;
Aug 10, 2000
Aug 10, 2000
441
Oct 14, 2018
Oct 14, 2018
442
443
444
/* tell libpng to de-interlace (if the image is interlaced) */
lib.png_set_interlace_handling(png_ptr) ;
Aug 10, 2000
Aug 10, 2000
445
446
447
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
* byte into separate bytes (useful for paletted and grayscale images).
*/
May 12, 2006
May 12, 2006
448
lib.png_set_packing(png_ptr);
Aug 10, 2000
Aug 10, 2000
449
Sep 1, 2000
Sep 1, 2000
450
451
/* scale greyscale values to the range 0..255 */
if(color_type == PNG_COLOR_TYPE_GRAY)
May 12, 2006
May 12, 2006
452
lib.png_set_expand(png_ptr);
Aug 10, 2000
Aug 10, 2000
453
454
/* For images with a single "transparent colour", set colour key;
Jul 31, 2001
Jul 31, 2001
455
456
if more than one index has transparency, or if partially transparent
entries exist, use full alpha channel */
May 12, 2006
May 12, 2006
457
if (lib.png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
Aug 10, 2000
Aug 10, 2000
458
459
int num_trans;
Uint8 *trans;
May 12, 2006
May 12, 2006
460
lib.png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
Aug 10, 2000
Aug 10, 2000
461
462
&transv);
if(color_type == PNG_COLOR_TYPE_PALETTE) {
Jul 31, 2001
Jul 31, 2001
463
/* Check if all tRNS entries are opaque except one */
Oct 16, 2018
Oct 16, 2018
464
465
466
467
int j, t = -1;
for(j = 0; j < num_trans; j++) {
if(trans[j] == 0) {
if (t >= 0) {
Jul 31, 2001
Jul 31, 2001
468
break;
Oct 16, 2018
Oct 16, 2018
469
470
471
}
t = j;
} else if(trans[j] != 255) {
Jul 31, 2001
Jul 31, 2001
472
break;
Oct 16, 2018
Oct 16, 2018
473
474
475
}
}
if(j == num_trans) {
Jul 31, 2001
Jul 31, 2001
476
477
478
479
/* exactly one transparent index */
ckey = t;
} else {
/* more than one transparent index, or translucency */
May 12, 2006
May 12, 2006
480
lib.png_set_expand(png_ptr);
Jul 31, 2001
Jul 31, 2001
481
}
Aug 10, 2000
Aug 10, 2000
482
483
484
485
} else
ckey = 0; /* actual value will be set later */
}
Sep 1, 2000
Sep 1, 2000
486
if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
May 12, 2006
May 12, 2006
487
lib.png_set_gray_to_rgb(png_ptr);
Aug 10, 2000
Aug 10, 2000
488
May 12, 2006
May 12, 2006
489
lib.png_read_update_info(png_ptr, info_ptr);
Aug 10, 2000
Aug 10, 2000
490
May 12, 2006
May 12, 2006
491
lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
Aug 10, 2000
Aug 10, 2000
492
493
494
&color_type, &interlace_type, NULL, NULL);
/* Allocate the SDL surface to hold the image */
Jan 4, 2012
Jan 4, 2012
495
496
Rmask = Gmask = Bmask = Amask = 0 ;
num_channels = lib.png_get_channels(png_ptr, info_ptr);
Aug 10, 2000
Aug 10, 2000
497
498
499
500
501
if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Jan 4, 2012
Jan 4, 2012
502
Amask = (num_channels == 4) ? 0xFF000000 : 0;
Aug 10, 2000
Aug 10, 2000
503
} else {
Jan 4, 2012
Jan 4, 2012
504
int s = (num_channels == 4) ? 0 : 8;
Aug 10, 2000
Aug 10, 2000
505
506
507
508
509
510
511
Rmask = 0xFF000000 >> s;
Gmask = 0x00FF0000 >> s;
Bmask = 0x0000FF00 >> s;
Amask = 0x000000FF >> s;
}
}
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
Jan 4, 2012
Jan 4, 2012
512
bit_depth*num_channels, Rmask,Gmask,Bmask,Amask);
Aug 10, 2000
Aug 10, 2000
513
if ( surface == NULL ) {
Feb 4, 2006
Feb 4, 2006
514
error = "Out of memory";
Aug 10, 2000
Aug 10, 2000
515
516
517
518
519
goto done;
}
if(ckey != -1) {
if(color_type != PNG_COLOR_TYPE_PALETTE)
Nov 30, 2000
Nov 30, 2000
520
521
522
523
524
/* FIXME: Should these be truncated or shifted down? */
ckey = SDL_MapRGB(surface->format,
(Uint8)transv->red,
(Uint8)transv->green,
(Uint8)transv->blue);
Aug 10, 2000
Aug 10, 2000
525
526
527
528
529
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
}
/* Create the array of pointers to image data */
row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
Oct 16, 2018
Oct 16, 2018
530
if (row_pointers == NULL) {
Feb 4, 2006
Feb 4, 2006
531
error = "Out of memory";
Aug 10, 2000
Aug 10, 2000
532
533
goto done;
}
Nov 30, 2000
Nov 30, 2000
534
for (row = 0; row < (int)height; row++) {
Aug 10, 2000
Aug 10, 2000
535
536
537
538
539
row_pointers[row] = (png_bytep)
(Uint8 *)surface->pixels + row*surface->pitch;
}
/* Read the entire image in one go */
May 12, 2006
May 12, 2006
540
lib.png_read_image(png_ptr, row_pointers);
Aug 10, 2000
Aug 10, 2000
541
May 16, 2004
May 16, 2004
542
543
544
545
546
/* 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
May 12, 2006
May 12, 2006
547
lib.png_read_end(png_ptr, info_ptr);
May 16, 2004
May 16, 2004
548
*/
Aug 10, 2000
Aug 10, 2000
549
550
551
/* Load the palette, if any */
palette = surface->format->palette;
Sep 1, 2000
Sep 1, 2000
552
if ( palette ) {
Jan 20, 2011
Jan 20, 2011
553
554
555
int png_num_palette;
png_colorp png_palette;
lib.png_get_PLTE(png_ptr, info_ptr, &png_palette, &png_num_palette);
Sep 1, 2000
Sep 1, 2000
556
557
558
if(color_type == PNG_COLOR_TYPE_GRAY) {
palette->ncolors = 256;
for(i = 0; i < 256; i++) {
Oct 14, 2018
Oct 14, 2018
559
560
561
palette->colors[i].r = (Uint8)i;
palette->colors[i].g = (Uint8)i;
palette->colors[i].b = (Uint8)i;
Sep 1, 2000
Sep 1, 2000
562
}
Jan 20, 2011
Jan 20, 2011
563
} else if (png_num_palette > 0 ) {
Oct 16, 2018
Oct 16, 2018
564
palette->ncolors = png_num_palette;
Jan 20, 2011
Jan 20, 2011
565
566
567
568
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;
Aug 10, 2000
Aug 10, 2000
569
}
Sep 1, 2000
Sep 1, 2000
570
}
Aug 10, 2000
Aug 10, 2000
571
572
573
}
done: /* Clean up and return */
Feb 4, 2006
Feb 4, 2006
574
if ( png_ptr ) {
May 12, 2006
May 12, 2006
575
lib.png_destroy_read_struct(&png_ptr,
Feb 4, 2006
Feb 4, 2006
576
info_ptr ? &info_ptr : (png_infopp)0,
Aug 10, 2000
Aug 10, 2000
577
(png_infopp)0);
Feb 4, 2006
Feb 4, 2006
578
}
Aug 10, 2000
Aug 10, 2000
579
580
581
if ( row_pointers ) {
free(row_pointers);
}
Feb 4, 2006
Feb 4, 2006
582
if ( error ) {
Nov 8, 2009
Nov 8, 2009
583
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
584
585
586
587
588
589
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError(error);
}
Aug 10, 2000
Aug 10, 2000
590
591
592
593
594
return(surface);
}
#else
Sep 26, 2009
Sep 26, 2009
595
596
597
598
599
600
601
602
603
604
int IMG_InitPNG()
{
IMG_SetError("PNG images are not supported");
return(-1);
}
void IMG_QuitPNG()
{
}
Aug 10, 2000
Aug 10, 2000
605
606
607
608
609
610
611
612
613
614
615
616
617
/* See if an image is contained in a data source */
int IMG_isPNG(SDL_RWops *src)
{
return(0);
}
/* Load a PNG type image from an SDL datasource */
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_PNG */
Nov 10, 2009
Nov 10, 2009
618
619
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */