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