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