IMG_png.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 186 5fbbeabf647f
parent 167 338c85983115
child 204 dcb38b0e78a6
permissions -rw-r--r--
Updated copyright date
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@186
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@121
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@121
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@121
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@121
    15
    You should have received a copy of the GNU Lesser General Public
slouken@121
    16
    License along with this library; if not, write to the Free Software
slouken@121
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* This is a PNG image file loading framework */
slouken@0
    24
slouken@0
    25
#include <stdlib.h>
slouken@0
    26
#include <stdio.h>
slouken@0
    27
slouken@0
    28
#include "SDL_image.h"
slouken@0
    29
slouken@0
    30
#ifdef LOAD_PNG
slouken@0
    31
slouken@0
    32
/*=============================================================================
slouken@0
    33
        File: SDL_png.c
slouken@0
    34
     Purpose: A PNG loader and saver for the SDL library      
slouken@0
    35
    Revision: 
slouken@0
    36
  Created by: Philippe Lavoie          (2 November 1998)
slouken@0
    37
              lavoie@zeus.genie.uottawa.ca
slouken@0
    38
 Modified by: 
slouken@0
    39
slouken@0
    40
 Copyright notice:
slouken@0
    41
          Copyright (C) 1998 Philippe Lavoie
slouken@0
    42
 
slouken@0
    43
          This library is free software; you can redistribute it and/or
slouken@0
    44
          modify it under the terms of the GNU Library General Public
slouken@0
    45
          License as published by the Free Software Foundation; either
slouken@0
    46
          version 2 of the License, or (at your option) any later version.
slouken@0
    47
 
slouken@0
    48
          This library is distributed in the hope that it will be useful,
slouken@0
    49
          but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    50
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    51
          Library General Public License for more details.
slouken@0
    52
 
slouken@0
    53
          You should have received a copy of the GNU Library General Public
slouken@0
    54
          License along with this library; if not, write to the Free
slouken@0
    55
          Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
slouken@0
    56
slouken@0
    57
    Comments: The load and save routine are basically the ones you can find
slouken@0
    58
             in the example.c file from the libpng distribution.
slouken@0
    59
slouken@0
    60
  Changes:
slouken@0
    61
    5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
slouken@0
    62
slouken@0
    63
=============================================================================*/
slouken@0
    64
slouken@0
    65
#include "SDL_endian.h"
slouken@0
    66
slouken@9
    67
#ifdef macintosh
slouken@9
    68
#define MACOS
slouken@9
    69
#endif
slouken@0
    70
#include <png.h>
slouken@0
    71
slouken@0
    72
slouken@143
    73
static struct {
slouken@143
    74
	int loaded;
slouken@143
    75
	void *handle;
slouken@143
    76
	png_infop (*png_create_info_struct) (png_structp png_ptr);
slouken@143
    77
	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);
slouken@143
    78
	void (*png_destroy_read_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr);
slouken@143
    79
	png_uint_32 (*png_get_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_method, int *compression_method, int *filter_method);
slouken@143
    80
	png_voidp (*png_get_io_ptr) (png_structp png_ptr);
slouken@143
    81
	png_uint_32 (*png_get_tRNS) (png_structp png_ptr, png_infop info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values);
slouken@143
    82
	png_uint_32 (*png_get_valid) (png_structp png_ptr, png_infop info_ptr, png_uint_32 flag);
slouken@143
    83
	void (*png_read_image) (png_structp png_ptr, png_bytepp image);
slouken@143
    84
	void (*png_read_info) (png_structp png_ptr, png_infop info_ptr);
slouken@143
    85
	void (*png_read_update_info) (png_structp png_ptr, png_infop info_ptr);
slouken@143
    86
	void (*png_set_expand) (png_structp png_ptr);
slouken@143
    87
	void (*png_set_gray_to_rgb) (png_structp png_ptr);
slouken@143
    88
	void (*png_set_packing) (png_structp png_ptr);
slouken@143
    89
	void (*png_set_read_fn) (png_structp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn);
slouken@143
    90
	void (*png_set_strip_16) (png_structp png_ptr);
slouken@143
    91
	int (*png_sig_cmp) (png_bytep sig, png_size_t start, png_size_t num_to_check);
slouken@143
    92
} lib;
slouken@143
    93
slouken@143
    94
#ifdef LOAD_PNG_DYNAMIC
slouken@143
    95
int IMG_InitPNG()
slouken@143
    96
{
slouken@143
    97
	if ( lib.loaded == 0 ) {
slouken@143
    98
		lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC);
slouken@143
    99
		if ( lib.handle == NULL ) {
slouken@143
   100
			return -1;
slouken@143
   101
		}
slouken@143
   102
		lib.png_create_info_struct =
slouken@143
   103
			(png_infop (*) (png_structp))
slouken@143
   104
			SDL_LoadFunction(lib.handle, "png_create_info_struct");
slouken@143
   105
		if ( lib.png_create_info_struct == NULL ) {
slouken@143
   106
			SDL_UnloadObject(lib.handle);
slouken@143
   107
			return -1;
slouken@143
   108
		}
slouken@143
   109
		lib.png_create_read_struct =
slouken@143
   110
			(png_structp (*) (png_const_charp, png_voidp, png_error_ptr, png_error_ptr))
slouken@143
   111
			SDL_LoadFunction(lib.handle, "png_create_read_struct");
slouken@143
   112
		if ( lib.png_create_read_struct == NULL ) {
slouken@143
   113
			SDL_UnloadObject(lib.handle);
slouken@143
   114
			return -1;
slouken@143
   115
		}
slouken@143
   116
		lib.png_destroy_read_struct =
slouken@143
   117
			(void (*) (png_structpp, png_infopp, png_infopp))
slouken@143
   118
			SDL_LoadFunction(lib.handle, "png_destroy_read_struct");
slouken@143
   119
		if ( lib.png_destroy_read_struct == NULL ) {
slouken@143
   120
			SDL_UnloadObject(lib.handle);
slouken@143
   121
			return -1;
slouken@143
   122
		}
slouken@143
   123
		lib.png_get_IHDR =
slouken@143
   124
			(png_uint_32 (*) (png_structp, png_infop, png_uint_32 *, png_uint_32 *, int *, int *, int *, int *, int *))
slouken@143
   125
			SDL_LoadFunction(lib.handle, "png_get_IHDR");
slouken@143
   126
		if ( lib.png_get_IHDR == NULL ) {
slouken@143
   127
			SDL_UnloadObject(lib.handle);
slouken@143
   128
			return -1;
slouken@143
   129
		}
slouken@143
   130
		lib.png_get_io_ptr =
slouken@143
   131
			(png_voidp (*) (png_structp))
slouken@143
   132
			SDL_LoadFunction(lib.handle, "png_get_io_ptr");
slouken@143
   133
		if ( lib.png_get_io_ptr == NULL ) {
slouken@143
   134
			SDL_UnloadObject(lib.handle);
slouken@143
   135
			return -1;
slouken@143
   136
		}
slouken@143
   137
		lib.png_get_tRNS =
slouken@143
   138
			(png_uint_32 (*) (png_structp, png_infop, png_bytep *, int *, png_color_16p *))
slouken@143
   139
			SDL_LoadFunction(lib.handle, "png_get_tRNS");
slouken@143
   140
		if ( lib.png_get_tRNS == NULL ) {
slouken@143
   141
			SDL_UnloadObject(lib.handle);
slouken@143
   142
			return -1;
slouken@143
   143
		}
slouken@143
   144
		lib.png_get_valid =
slouken@143
   145
			(png_uint_32 (*) (png_structp, png_infop, png_uint_32))
slouken@143
   146
			SDL_LoadFunction(lib.handle, "png_get_valid");
slouken@143
   147
		if ( lib.png_get_valid == NULL ) {
slouken@143
   148
			SDL_UnloadObject(lib.handle);
slouken@143
   149
			return -1;
slouken@143
   150
		}
slouken@143
   151
		lib.png_read_image =
slouken@143
   152
			(void (*) (png_structp, png_bytepp))
slouken@143
   153
			SDL_LoadFunction(lib.handle, "png_read_image");
slouken@143
   154
		if ( lib.png_read_image == NULL ) {
slouken@143
   155
			SDL_UnloadObject(lib.handle);
slouken@143
   156
			return -1;
slouken@143
   157
		}
slouken@143
   158
		lib.png_read_info =
slouken@143
   159
			(void (*) (png_structp, png_infop))
slouken@143
   160
			SDL_LoadFunction(lib.handle, "png_read_info");
slouken@143
   161
		if ( lib.png_read_info == NULL ) {
slouken@143
   162
			SDL_UnloadObject(lib.handle);
slouken@143
   163
			return -1;
slouken@143
   164
		}
slouken@143
   165
		lib.png_read_update_info =
slouken@143
   166
			(void (*) (png_structp, png_infop))
slouken@143
   167
			SDL_LoadFunction(lib.handle, "png_read_update_info");
slouken@143
   168
		if ( lib.png_read_update_info == NULL ) {
slouken@143
   169
			SDL_UnloadObject(lib.handle);
slouken@143
   170
			return -1;
slouken@143
   171
		}
slouken@143
   172
		lib.png_set_expand =
slouken@143
   173
			(void (*) (png_structp))
slouken@143
   174
			SDL_LoadFunction(lib.handle, "png_set_expand");
slouken@143
   175
		if ( lib.png_set_expand == NULL ) {
slouken@143
   176
			SDL_UnloadObject(lib.handle);
slouken@143
   177
			return -1;
slouken@143
   178
		}
slouken@143
   179
		lib.png_set_gray_to_rgb =
slouken@143
   180
			(void (*) (png_structp))
slouken@143
   181
			SDL_LoadFunction(lib.handle, "png_set_gray_to_rgb");
slouken@143
   182
		if ( lib.png_set_gray_to_rgb == NULL ) {
slouken@143
   183
			SDL_UnloadObject(lib.handle);
slouken@143
   184
			return -1;
slouken@143
   185
		}
slouken@143
   186
		lib.png_set_packing =
slouken@143
   187
			(void (*) (png_structp))
slouken@143
   188
			SDL_LoadFunction(lib.handle, "png_set_packing");
slouken@143
   189
		if ( lib.png_set_packing == NULL ) {
slouken@143
   190
			SDL_UnloadObject(lib.handle);
slouken@143
   191
			return -1;
slouken@143
   192
		}
slouken@143
   193
		lib.png_set_read_fn =
slouken@143
   194
			(void (*) (png_structp, png_voidp, png_rw_ptr))
slouken@143
   195
			SDL_LoadFunction(lib.handle, "png_set_read_fn");
slouken@143
   196
		if ( lib.png_set_read_fn == NULL ) {
slouken@143
   197
			SDL_UnloadObject(lib.handle);
slouken@143
   198
			return -1;
slouken@143
   199
		}
slouken@143
   200
		lib.png_set_strip_16 =
slouken@143
   201
			(void (*) (png_structp))
slouken@143
   202
			SDL_LoadFunction(lib.handle, "png_set_strip_16");
slouken@143
   203
		if ( lib.png_set_strip_16 == NULL ) {
slouken@143
   204
			SDL_UnloadObject(lib.handle);
slouken@143
   205
			return -1;
slouken@143
   206
		}
slouken@143
   207
		lib.png_sig_cmp =
slouken@143
   208
			(int (*) (png_bytep, png_size_t, png_size_t))
slouken@143
   209
			SDL_LoadFunction(lib.handle, "png_sig_cmp");
slouken@143
   210
		if ( lib.png_sig_cmp == NULL ) {
slouken@143
   211
			SDL_UnloadObject(lib.handle);
slouken@143
   212
			return -1;
slouken@143
   213
		}
slouken@143
   214
	}
slouken@143
   215
	++lib.loaded;
slouken@143
   216
slouken@143
   217
	return 0;
slouken@143
   218
}
slouken@143
   219
void IMG_QuitPNG()
slouken@143
   220
{
slouken@143
   221
	if ( lib.loaded == 0 ) {
slouken@143
   222
		return;
slouken@143
   223
	}
slouken@143
   224
	if ( lib.loaded == 1 ) {
slouken@143
   225
		SDL_UnloadObject(lib.handle);
slouken@143
   226
	}
slouken@143
   227
	--lib.loaded;
slouken@143
   228
}
slouken@143
   229
#else
slouken@143
   230
int IMG_InitPNG()
slouken@143
   231
{
slouken@143
   232
	if ( lib.loaded == 0 ) {
slouken@143
   233
		lib.png_create_info_struct = png_create_info_struct;
slouken@143
   234
		lib.png_create_read_struct = png_create_read_struct;
slouken@143
   235
		lib.png_destroy_read_struct = png_destroy_read_struct;
slouken@143
   236
		lib.png_get_IHDR = png_get_IHDR;
slouken@143
   237
		lib.png_get_io_ptr = png_get_io_ptr;
slouken@143
   238
		lib.png_get_tRNS = png_get_tRNS;
slouken@143
   239
		lib.png_get_valid = png_get_valid;
slouken@143
   240
		lib.png_read_image = png_read_image;
slouken@143
   241
		lib.png_read_info = png_read_info;
slouken@143
   242
		lib.png_read_update_info = png_read_update_info;
slouken@143
   243
		lib.png_set_expand = png_set_expand;
slouken@143
   244
		lib.png_set_gray_to_rgb = png_set_gray_to_rgb;
slouken@143
   245
		lib.png_set_packing = png_set_packing;
slouken@143
   246
		lib.png_set_read_fn = png_set_read_fn;
slouken@143
   247
		lib.png_set_strip_16 = png_set_strip_16;
slouken@143
   248
		lib.png_sig_cmp = png_sig_cmp;
slouken@143
   249
	}
slouken@143
   250
	++lib.loaded;
slouken@146
   251
slouken@146
   252
	return 0;
slouken@143
   253
}
slouken@143
   254
void IMG_QuitPNG()
slouken@143
   255
{
slouken@143
   256
	if ( lib.loaded == 0 ) {
slouken@143
   257
		return;
slouken@143
   258
	}
slouken@143
   259
	if ( lib.loaded == 1 ) {
slouken@143
   260
	}
slouken@143
   261
	--lib.loaded;
slouken@143
   262
}
slouken@143
   263
#endif /* LOAD_PNG_DYNAMIC */
slouken@143
   264
slouken@0
   265
/* See if an image is contained in a data source */
slouken@0
   266
int IMG_isPNG(SDL_RWops *src)
slouken@0
   267
{
slouken@117
   268
	int start;
slouken@117
   269
	int is_PNG;
slouken@167
   270
	Uint8 magic[4];
slouken@0
   271
icculus@154
   272
	if ( !src )
icculus@154
   273
		return 0;
slouken@117
   274
	start = SDL_RWtell(src);
slouken@117
   275
	is_PNG = 0;
slouken@167
   276
	if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
slouken@167
   277
                if ( magic[0] == 0x89 &&
slouken@167
   278
                     magic[1] == 'P' &&
slouken@167
   279
                     magic[2] == 'N' &&
slouken@167
   280
                     magic[3] == 'G' ) {
slouken@166
   281
			is_PNG = 1;
slouken@166
   282
		}
slouken@117
   283
	}
slouken@117
   284
	SDL_RWseek(src, start, SEEK_SET);
slouken@117
   285
	return(is_PNG);
slouken@0
   286
}
slouken@0
   287
slouken@0
   288
/* Load a PNG type image from an SDL datasource */
slouken@0
   289
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
slouken@0
   290
{
slouken@0
   291
	SDL_RWops *src;
slouken@0
   292
slouken@143
   293
	src = (SDL_RWops *)lib.png_get_io_ptr(ctx);
slouken@0
   294
	SDL_RWread(src, area, size, 1);
slouken@0
   295
}
slouken@0
   296
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   297
{
slouken@118
   298
	int start;
slouken@118
   299
	const char *error;
slouken@7
   300
	SDL_Surface *volatile surface;
slouken@0
   301
	png_structp png_ptr;
slouken@0
   302
	png_infop info_ptr;
slouken@0
   303
	png_uint_32 width, height;
slouken@0
   304
	int bit_depth, color_type, interlace_type;
slouken@0
   305
	Uint32 Rmask;
slouken@0
   306
	Uint32 Gmask;
slouken@0
   307
	Uint32 Bmask;
slouken@0
   308
	Uint32 Amask;
slouken@0
   309
	SDL_Palette *palette;
slouken@7
   310
	png_bytep *volatile row_pointers;
slouken@0
   311
	int row, i;
slouken@7
   312
	volatile int ckey = -1;
slouken@0
   313
	png_color_16 *transv;
slouken@0
   314
slouken@98
   315
	if ( !src ) {
slouken@98
   316
		/* The error message has been set in SDL_RWFromFile */
slouken@98
   317
		return NULL;
slouken@98
   318
	}
slouken@118
   319
	start = SDL_RWtell(src);
slouken@98
   320
slouken@143
   321
	if ( IMG_InitPNG() < 0 ) {
slouken@143
   322
		return NULL;
slouken@143
   323
	}
slouken@143
   324
slouken@0
   325
	/* Initialize the data we will clean up when we're done */
slouken@118
   326
	error = NULL;
slouken@0
   327
	png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
slouken@0
   328
slouken@0
   329
	/* Create the PNG loading context structure */
slouken@143
   330
	png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
slouken@0
   331
					  NULL,NULL,NULL);
slouken@0
   332
	if (png_ptr == NULL){
slouken@118
   333
		error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
slouken@0
   334
		goto done;
slouken@0
   335
	}
slouken@0
   336
slouken@0
   337
	 /* Allocate/initialize the memory for image information.  REQUIRED. */
slouken@143
   338
	info_ptr = lib.png_create_info_struct(png_ptr);
slouken@0
   339
	if (info_ptr == NULL) {
slouken@118
   340
		error = "Couldn't create image information for PNG file";
slouken@0
   341
		goto done;
slouken@0
   342
	}
slouken@0
   343
slouken@0
   344
	/* Set error handling if you are using setjmp/longjmp method (this is
slouken@0
   345
	 * the normal method of doing things with libpng).  REQUIRED unless you
slouken@0
   346
	 * set up your own error handlers in png_create_read_struct() earlier.
slouken@0
   347
	 */
slouken@0
   348
	if ( setjmp(png_ptr->jmpbuf) ) {
slouken@118
   349
		error = "Error reading the PNG file.";
slouken@0
   350
		goto done;
slouken@0
   351
	}
slouken@0
   352
slouken@0
   353
	/* Set up the input control */
slouken@143
   354
	lib.png_set_read_fn(png_ptr, src, png_read_data);
slouken@0
   355
slouken@0
   356
	/* Read PNG header info */
slouken@143
   357
	lib.png_read_info(png_ptr, info_ptr);
slouken@143
   358
	lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@0
   359
			&color_type, &interlace_type, NULL, NULL);
slouken@0
   360
slouken@0
   361
	/* tell libpng to strip 16 bit/color files down to 8 bits/color */
slouken@143
   362
	lib.png_set_strip_16(png_ptr) ;
slouken@0
   363
slouken@0
   364
	/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
slouken@0
   365
	 * byte into separate bytes (useful for paletted and grayscale images).
slouken@0
   366
	 */
slouken@143
   367
	lib.png_set_packing(png_ptr);
slouken@0
   368
slouken@4
   369
	/* scale greyscale values to the range 0..255 */
slouken@4
   370
	if(color_type == PNG_COLOR_TYPE_GRAY)
slouken@143
   371
		lib.png_set_expand(png_ptr);
slouken@0
   372
slouken@0
   373
	/* For images with a single "transparent colour", set colour key;
slouken@41
   374
	   if more than one index has transparency, or if partially transparent
slouken@41
   375
	   entries exist, use full alpha channel */
slouken@143
   376
	if (lib.png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
slouken@0
   377
	        int num_trans;
slouken@0
   378
		Uint8 *trans;
slouken@143
   379
		lib.png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
slouken@0
   380
			     &transv);
slouken@0
   381
		if(color_type == PNG_COLOR_TYPE_PALETTE) {
slouken@41
   382
		    /* Check if all tRNS entries are opaque except one */
slouken@41
   383
		    int i, t = -1;
slouken@41
   384
		    for(i = 0; i < num_trans; i++)
slouken@41
   385
			if(trans[i] == 0) {
slouken@41
   386
			    if(t >= 0)
slouken@41
   387
				break;
slouken@41
   388
			    t = i;
slouken@41
   389
			} else if(trans[i] != 255)
slouken@41
   390
			    break;
slouken@41
   391
		    if(i == num_trans) {
slouken@41
   392
			/* exactly one transparent index */
slouken@41
   393
			ckey = t;
slouken@41
   394
		    } else {
slouken@41
   395
			/* more than one transparent index, or translucency */
slouken@143
   396
			lib.png_set_expand(png_ptr);
slouken@41
   397
		    }
slouken@0
   398
		} else
slouken@0
   399
		    ckey = 0; /* actual value will be set later */
slouken@0
   400
	}
slouken@0
   401
slouken@4
   402
	if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
slouken@143
   403
		lib.png_set_gray_to_rgb(png_ptr);
slouken@0
   404
slouken@143
   405
	lib.png_read_update_info(png_ptr, info_ptr);
slouken@0
   406
slouken@143
   407
	lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@0
   408
			&color_type, &interlace_type, NULL, NULL);
slouken@0
   409
slouken@0
   410
	/* Allocate the SDL surface to hold the image */
slouken@0
   411
	Rmask = Gmask = Bmask = Amask = 0 ; 
slouken@0
   412
	if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
slouken@0
   413
		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
slouken@0
   414
			Rmask = 0x000000FF;
slouken@0
   415
			Gmask = 0x0000FF00;
slouken@0
   416
			Bmask = 0x00FF0000;
slouken@0
   417
			Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
slouken@0
   418
		} else {
slouken@0
   419
		        int s = (info_ptr->channels == 4) ? 0 : 8;
slouken@0
   420
			Rmask = 0xFF000000 >> s;
slouken@0
   421
			Gmask = 0x00FF0000 >> s;
slouken@0
   422
			Bmask = 0x0000FF00 >> s;
slouken@0
   423
			Amask = 0x000000FF >> s;
slouken@0
   424
		}
slouken@0
   425
	}
slouken@0
   426
	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
slouken@0
   427
			bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
slouken@0
   428
	if ( surface == NULL ) {
slouken@118
   429
		error = "Out of memory";
slouken@0
   430
		goto done;
slouken@0
   431
	}
slouken@0
   432
slouken@0
   433
	if(ckey != -1) {
slouken@0
   434
	        if(color_type != PNG_COLOR_TYPE_PALETTE)
slouken@9
   435
			/* FIXME: Should these be truncated or shifted down? */
slouken@9
   436
		        ckey = SDL_MapRGB(surface->format,
slouken@9
   437
			                 (Uint8)transv->red,
slouken@9
   438
			                 (Uint8)transv->green,
slouken@9
   439
			                 (Uint8)transv->blue);
slouken@0
   440
	        SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
slouken@0
   441
	}
slouken@0
   442
slouken@0
   443
	/* Create the array of pointers to image data */
slouken@0
   444
	row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
slouken@0
   445
	if ( (row_pointers == NULL) ) {
slouken@118
   446
		error = "Out of memory";
slouken@0
   447
		goto done;
slouken@0
   448
	}
slouken@9
   449
	for (row = 0; row < (int)height; row++) {
slouken@0
   450
		row_pointers[row] = (png_bytep)
slouken@0
   451
				(Uint8 *)surface->pixels + row*surface->pitch;
slouken@0
   452
	}
slouken@0
   453
slouken@0
   454
	/* Read the entire image in one go */
slouken@143
   455
	lib.png_read_image(png_ptr, row_pointers);
slouken@0
   456
slouken@100
   457
	/* and we're done!  (png_read_end() can be omitted if no processing of
slouken@100
   458
	 * post-IDAT text/time/etc. is desired)
slouken@100
   459
	 * In some cases it can't read PNG's created by some popular programs (ACDSEE),
slouken@100
   460
	 * we do not want to process comments, so we omit png_read_end
slouken@100
   461
slouken@143
   462
	lib.png_read_end(png_ptr, info_ptr);
slouken@100
   463
	*/
slouken@0
   464
slouken@0
   465
	/* Load the palette, if any */
slouken@0
   466
	palette = surface->format->palette;
slouken@4
   467
	if ( palette ) {
slouken@4
   468
	    if(color_type == PNG_COLOR_TYPE_GRAY) {
slouken@4
   469
		palette->ncolors = 256;
slouken@4
   470
		for(i = 0; i < 256; i++) {
slouken@4
   471
		    palette->colors[i].r = i;
slouken@4
   472
		    palette->colors[i].g = i;
slouken@4
   473
		    palette->colors[i].b = i;
slouken@4
   474
		}
slouken@4
   475
	    } else if (info_ptr->num_palette > 0 ) {
slouken@0
   476
		palette->ncolors = info_ptr->num_palette; 
slouken@0
   477
		for( i=0; i<info_ptr->num_palette; ++i ) {
slouken@4
   478
		    palette->colors[i].b = info_ptr->palette[i].blue;
slouken@4
   479
		    palette->colors[i].g = info_ptr->palette[i].green;
slouken@4
   480
		    palette->colors[i].r = info_ptr->palette[i].red;
slouken@0
   481
		}
slouken@4
   482
	    }
slouken@0
   483
	}
slouken@0
   484
slouken@0
   485
done:	/* Clean up and return */
slouken@118
   486
	if ( png_ptr ) {
slouken@143
   487
		lib.png_destroy_read_struct(&png_ptr,
slouken@118
   488
		                        info_ptr ? &info_ptr : (png_infopp)0,
slouken@0
   489
								(png_infopp)0);
slouken@118
   490
	}
slouken@0
   491
	if ( row_pointers ) {
slouken@0
   492
		free(row_pointers);
slouken@0
   493
	}
slouken@118
   494
	if ( error ) {
slouken@118
   495
		SDL_RWseek(src, start, SEEK_SET);
slouken@118
   496
		if ( surface ) {
slouken@118
   497
			SDL_FreeSurface(surface);
slouken@118
   498
			surface = NULL;
slouken@118
   499
		}
slouken@143
   500
		IMG_QuitPNG();
slouken@118
   501
		IMG_SetError(error);
slouken@143
   502
	} else {
slouken@143
   503
		IMG_QuitPNG();
slouken@118
   504
	}
slouken@0
   505
	return(surface); 
slouken@0
   506
}
slouken@0
   507
slouken@0
   508
#else
slouken@0
   509
slouken@0
   510
/* See if an image is contained in a data source */
slouken@0
   511
int IMG_isPNG(SDL_RWops *src)
slouken@0
   512
{
slouken@0
   513
	return(0);
slouken@0
   514
}
slouken@0
   515
slouken@0
   516
/* Load a PNG type image from an SDL datasource */
slouken@0
   517
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   518
{
slouken@0
   519
	return(NULL);
slouken@0
   520
}
slouken@0
   521
slouken@0
   522
#endif /* LOAD_PNG */