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