/
IMG_png.c
317 lines (268 loc) · 9.18 KB
1
/*
2
SDL_image: An example image loading library for use with SDL
3
Copyright (C) 1997-2006 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
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
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
*/
/* 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"
67
68
69
#ifdef macintosh
#define MACOS
#endif
70
71
72
73
74
75
76
#include <png.h>
#define PNG_BYTES_TO_CHECK 4
/* See if an image is contained in a data source */
int IMG_isPNG(SDL_RWops *src)
{
77
78
79
80
81
82
83
84
85
86
87
int start;
int is_PNG;
unsigned char buf[PNG_BYTES_TO_CHECK];
start = SDL_RWtell(src);
is_PNG = 0;
if ( SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK ) {
is_PNG = (png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK) == 0);
}
SDL_RWseek(src, start, SEEK_SET);
return(is_PNG);
88
89
90
91
92
93
94
95
96
97
98
99
}
/* 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;
src = (SDL_RWops *)png_get_io_ptr(ctx);
SDL_RWread(src, area, size, 1);
}
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
100
101
int start;
const char *error;
102
SDL_Surface *volatile surface;
103
104
105
106
107
108
109
110
111
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
SDL_Palette *palette;
112
png_bytep *volatile row_pointers;
113
int row, i;
114
volatile int ckey = -1;
115
116
png_color_16 *transv;
117
118
119
120
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
121
start = SDL_RWtell(src);
122
123
/* Initialize the data we will clean up when we're done */
124
error = NULL;
125
126
127
128
129
130
png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
/* Create the PNG loading context structure */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,NULL,NULL);
if (png_ptr == NULL){
131
error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
132
133
134
135
136
137
goto done;
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
138
error = "Couldn't create image information for PNG file";
139
140
141
142
143
144
145
146
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.
*/
if ( setjmp(png_ptr->jmpbuf) ) {
147
error = "Error reading the PNG file.";
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
goto done;
}
/* Set up the input control */
png_set_read_fn(png_ptr, src, png_read_data);
/* Read PNG header info */
png_read_info(png_ptr, info_ptr);
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 */
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).
*/
png_set_packing(png_ptr);
167
168
169
/* scale greyscale values to the range 0..255 */
if(color_type == PNG_COLOR_TYPE_GRAY)
png_set_expand(png_ptr);
170
171
/* For images with a single "transparent colour", set colour key;
172
173
if more than one index has transparency, or if partially transparent
entries exist, use full alpha channel */
174
175
176
177
178
179
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
int num_trans;
Uint8 *trans;
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
&transv);
if(color_type == PNG_COLOR_TYPE_PALETTE) {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* 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 */
194
png_set_expand(png_ptr);
195
}
196
197
198
199
} else
ckey = 0; /* actual value will be set later */
}
200
if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
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
png_set_gray_to_rgb(png_ptr);
png_read_update_info(png_ptr, info_ptr);
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 ;
if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
} else {
int s = (info_ptr->channels == 4) ? 0 : 8;
Rmask = 0xFF000000 >> s;
Gmask = 0x00FF0000 >> s;
Bmask = 0x0000FF00 >> s;
Amask = 0x000000FF >> s;
}
}
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
if ( surface == NULL ) {
227
error = "Out of memory";
228
229
230
231
232
goto done;
}
if(ckey != -1) {
if(color_type != PNG_COLOR_TYPE_PALETTE)
233
234
235
236
237
/* FIXME: Should these be truncated or shifted down? */
ckey = SDL_MapRGB(surface->format,
(Uint8)transv->red,
(Uint8)transv->green,
(Uint8)transv->blue);
238
239
240
241
242
243
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) ) {
244
error = "Out of memory";
245
246
goto done;
}
247
for (row = 0; row < (int)height; row++) {
248
249
250
251
252
253
254
row_pointers[row] = (png_bytep)
(Uint8 *)surface->pixels + row*surface->pitch;
}
/* Read the entire image in one go */
png_read_image(png_ptr, row_pointers);
255
256
257
258
259
/* 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
260
png_read_end(png_ptr, info_ptr);
261
*/
262
263
264
/* Load the palette, if any */
palette = surface->format->palette;
265
266
267
268
269
270
271
272
273
if ( palette ) {
if(color_type == PNG_COLOR_TYPE_GRAY) {
palette->ncolors = 256;
for(i = 0; i < 256; i++) {
palette->colors[i].r = i;
palette->colors[i].g = i;
palette->colors[i].b = i;
}
} else if (info_ptr->num_palette > 0 ) {
274
275
palette->ncolors = info_ptr->num_palette;
for( i=0; i<info_ptr->num_palette; ++i ) {
276
277
278
palette->colors[i].b = info_ptr->palette[i].blue;
palette->colors[i].g = info_ptr->palette[i].green;
palette->colors[i].r = info_ptr->palette[i].red;
279
}
280
}
281
282
283
}
done: /* Clean up and return */
284
285
286
if ( png_ptr ) {
png_destroy_read_struct(&png_ptr,
info_ptr ? &info_ptr : (png_infopp)0,
287
(png_infopp)0);
288
}
289
290
291
if ( row_pointers ) {
free(row_pointers);
}
292
293
294
295
296
297
298
299
if ( error ) {
SDL_RWseek(src, start, SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError(error);
}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
return(surface);
}
#else
/* 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 */