IMG_png.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 20:37:17 +0000
changeset 117 e613cf987897
parent 100 3feaaa4104f6
child 118 c5e736a47ad2
permissions -rw-r--r--
Fixed image type functions so they seek back to where they started
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@97
     3
    Copyright (C) 1999-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@53
    23
/* $Id$ */
slouken@53
    24
slouken@0
    25
/* This is a PNG image file loading framework */
slouken@0
    26
slouken@0
    27
#include <stdlib.h>
slouken@0
    28
#include <stdio.h>
slouken@0
    29
slouken@0
    30
#include "SDL_image.h"
slouken@0
    31
slouken@0
    32
#ifdef LOAD_PNG
slouken@0
    33
slouken@0
    34
/*=============================================================================
slouken@0
    35
        File: SDL_png.c
slouken@0
    36
     Purpose: A PNG loader and saver for the SDL library      
slouken@0
    37
    Revision: 
slouken@0
    38
  Created by: Philippe Lavoie          (2 November 1998)
slouken@0
    39
              lavoie@zeus.genie.uottawa.ca
slouken@0
    40
 Modified by: 
slouken@0
    41
slouken@0
    42
 Copyright notice:
slouken@0
    43
          Copyright (C) 1998 Philippe Lavoie
slouken@0
    44
 
slouken@0
    45
          This library is free software; you can redistribute it and/or
slouken@0
    46
          modify it under the terms of the GNU Library General Public
slouken@0
    47
          License as published by the Free Software Foundation; either
slouken@0
    48
          version 2 of the License, or (at your option) any later version.
slouken@0
    49
 
slouken@0
    50
          This library is distributed in the hope that it will be useful,
slouken@0
    51
          but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    52
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    53
          Library General Public License for more details.
slouken@0
    54
 
slouken@0
    55
          You should have received a copy of the GNU Library General Public
slouken@0
    56
          License along with this library; if not, write to the Free
slouken@0
    57
          Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
slouken@0
    58
slouken@0
    59
    Comments: The load and save routine are basically the ones you can find
slouken@0
    60
             in the example.c file from the libpng distribution.
slouken@0
    61
slouken@0
    62
  Changes:
slouken@0
    63
    5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
slouken@0
    64
slouken@0
    65
=============================================================================*/
slouken@0
    66
slouken@0
    67
#include "SDL_endian.h"
slouken@0
    68
slouken@9
    69
#ifdef macintosh
slouken@9
    70
#define MACOS
slouken@9
    71
#endif
slouken@0
    72
#include <png.h>
slouken@0
    73
slouken@0
    74
#define PNG_BYTES_TO_CHECK 4
slouken@0
    75
slouken@0
    76
/* See if an image is contained in a data source */
slouken@0
    77
int IMG_isPNG(SDL_RWops *src)
slouken@0
    78
{
slouken@117
    79
	int start;
slouken@117
    80
	int is_PNG;
slouken@117
    81
	unsigned char buf[PNG_BYTES_TO_CHECK];
slouken@0
    82
slouken@117
    83
	start = SDL_RWtell(src);
slouken@117
    84
	is_PNG = 0;
slouken@117
    85
	if ( SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK ) {
slouken@117
    86
		is_PNG = (png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK) == 0);
slouken@117
    87
	}
slouken@117
    88
	SDL_RWseek(src, start, SEEK_SET);
slouken@117
    89
	return(is_PNG);
slouken@0
    90
}
slouken@0
    91
slouken@0
    92
/* Load a PNG type image from an SDL datasource */
slouken@0
    93
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
slouken@0
    94
{
slouken@0
    95
	SDL_RWops *src;
slouken@0
    96
slouken@0
    97
	src = (SDL_RWops *)png_get_io_ptr(ctx);
slouken@0
    98
	SDL_RWread(src, area, size, 1);
slouken@0
    99
}
slouken@0
   100
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   101
{
slouken@7
   102
	SDL_Surface *volatile surface;
slouken@0
   103
	png_structp png_ptr;
slouken@0
   104
	png_infop info_ptr;
slouken@0
   105
	png_uint_32 width, height;
slouken@0
   106
	int bit_depth, color_type, interlace_type;
slouken@0
   107
	Uint32 Rmask;
slouken@0
   108
	Uint32 Gmask;
slouken@0
   109
	Uint32 Bmask;
slouken@0
   110
	Uint32 Amask;
slouken@0
   111
	SDL_Palette *palette;
slouken@7
   112
	png_bytep *volatile row_pointers;
slouken@0
   113
	int row, i;
slouken@7
   114
	volatile int ckey = -1;
slouken@0
   115
	png_color_16 *transv;
slouken@0
   116
slouken@98
   117
	if ( !src ) {
slouken@98
   118
		/* The error message has been set in SDL_RWFromFile */
slouken@98
   119
		return NULL;
slouken@98
   120
	}
slouken@98
   121
slouken@0
   122
	/* Initialize the data we will clean up when we're done */
slouken@0
   123
	png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
slouken@0
   124
slouken@0
   125
	/* Create the PNG loading context structure */
slouken@0
   126
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
slouken@0
   127
					  NULL,NULL,NULL);
slouken@0
   128
	if (png_ptr == NULL){
slouken@100
   129
		IMG_SetError("Couldn't allocate memory for PNG file or incompatible PNG dll");
slouken@0
   130
		goto done;
slouken@0
   131
	}
slouken@0
   132
slouken@0
   133
	 /* Allocate/initialize the memory for image information.  REQUIRED. */
slouken@0
   134
	info_ptr = png_create_info_struct(png_ptr);
slouken@0
   135
	if (info_ptr == NULL) {
slouken@0
   136
		IMG_SetError("Couldn't create image information for PNG file");
slouken@0
   137
		goto done;
slouken@0
   138
	}
slouken@0
   139
slouken@0
   140
	/* Set error handling if you are using setjmp/longjmp method (this is
slouken@0
   141
	 * the normal method of doing things with libpng).  REQUIRED unless you
slouken@0
   142
	 * set up your own error handlers in png_create_read_struct() earlier.
slouken@0
   143
	 */
slouken@0
   144
	if ( setjmp(png_ptr->jmpbuf) ) {
slouken@0
   145
		IMG_SetError("Error reading the PNG file.");
slouken@0
   146
		goto done;
slouken@0
   147
	}
slouken@0
   148
slouken@0
   149
	/* Set up the input control */
slouken@0
   150
	png_set_read_fn(png_ptr, src, png_read_data);
slouken@0
   151
slouken@0
   152
	/* Read PNG header info */
slouken@0
   153
	png_read_info(png_ptr, info_ptr);
slouken@0
   154
	png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@0
   155
			&color_type, &interlace_type, NULL, NULL);
slouken@0
   156
slouken@0
   157
	/* tell libpng to strip 16 bit/color files down to 8 bits/color */
slouken@0
   158
	png_set_strip_16(png_ptr) ;
slouken@0
   159
slouken@0
   160
	/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
slouken@0
   161
	 * byte into separate bytes (useful for paletted and grayscale images).
slouken@0
   162
	 */
slouken@0
   163
	png_set_packing(png_ptr);
slouken@0
   164
slouken@4
   165
	/* scale greyscale values to the range 0..255 */
slouken@4
   166
	if(color_type == PNG_COLOR_TYPE_GRAY)
slouken@4
   167
		png_set_expand(png_ptr);
slouken@0
   168
slouken@0
   169
	/* For images with a single "transparent colour", set colour key;
slouken@41
   170
	   if more than one index has transparency, or if partially transparent
slouken@41
   171
	   entries exist, use full alpha channel */
slouken@0
   172
	if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
slouken@0
   173
	        int num_trans;
slouken@0
   174
		Uint8 *trans;
slouken@0
   175
		png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
slouken@0
   176
			     &transv);
slouken@0
   177
		if(color_type == PNG_COLOR_TYPE_PALETTE) {
slouken@41
   178
		    /* Check if all tRNS entries are opaque except one */
slouken@41
   179
		    int i, t = -1;
slouken@41
   180
		    for(i = 0; i < num_trans; i++)
slouken@41
   181
			if(trans[i] == 0) {
slouken@41
   182
			    if(t >= 0)
slouken@41
   183
				break;
slouken@41
   184
			    t = i;
slouken@41
   185
			} else if(trans[i] != 255)
slouken@41
   186
			    break;
slouken@41
   187
		    if(i == num_trans) {
slouken@41
   188
			/* exactly one transparent index */
slouken@41
   189
			ckey = t;
slouken@41
   190
		    } else {
slouken@41
   191
			/* more than one transparent index, or translucency */
slouken@0
   192
			png_set_expand(png_ptr);
slouken@41
   193
		    }
slouken@0
   194
		} else
slouken@0
   195
		    ckey = 0; /* actual value will be set later */
slouken@0
   196
	}
slouken@0
   197
slouken@4
   198
	if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
slouken@0
   199
		png_set_gray_to_rgb(png_ptr);
slouken@0
   200
slouken@0
   201
	png_read_update_info(png_ptr, info_ptr);
slouken@0
   202
slouken@0
   203
	png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@0
   204
			&color_type, &interlace_type, NULL, NULL);
slouken@0
   205
slouken@0
   206
	/* Allocate the SDL surface to hold the image */
slouken@0
   207
	Rmask = Gmask = Bmask = Amask = 0 ; 
slouken@0
   208
	if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
slouken@0
   209
		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
slouken@0
   210
			Rmask = 0x000000FF;
slouken@0
   211
			Gmask = 0x0000FF00;
slouken@0
   212
			Bmask = 0x00FF0000;
slouken@0
   213
			Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
slouken@0
   214
		} else {
slouken@0
   215
		        int s = (info_ptr->channels == 4) ? 0 : 8;
slouken@0
   216
			Rmask = 0xFF000000 >> s;
slouken@0
   217
			Gmask = 0x00FF0000 >> s;
slouken@0
   218
			Bmask = 0x0000FF00 >> s;
slouken@0
   219
			Amask = 0x000000FF >> s;
slouken@0
   220
		}
slouken@0
   221
	}
slouken@0
   222
	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
slouken@0
   223
			bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
slouken@0
   224
	if ( surface == NULL ) {
slouken@0
   225
		IMG_SetError("Out of memory");
slouken@0
   226
		goto done;
slouken@0
   227
	}
slouken@0
   228
slouken@0
   229
	if(ckey != -1) {
slouken@0
   230
	        if(color_type != PNG_COLOR_TYPE_PALETTE)
slouken@9
   231
			/* FIXME: Should these be truncated or shifted down? */
slouken@9
   232
		        ckey = SDL_MapRGB(surface->format,
slouken@9
   233
			                 (Uint8)transv->red,
slouken@9
   234
			                 (Uint8)transv->green,
slouken@9
   235
			                 (Uint8)transv->blue);
slouken@0
   236
	        SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
slouken@0
   237
	}
slouken@0
   238
slouken@0
   239
	/* Create the array of pointers to image data */
slouken@0
   240
	row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
slouken@0
   241
	if ( (row_pointers == NULL) ) {
slouken@0
   242
		IMG_SetError("Out of memory");
slouken@0
   243
		SDL_FreeSurface(surface);
slouken@0
   244
		surface = NULL;
slouken@0
   245
		goto done;
slouken@0
   246
	}
slouken@9
   247
	for (row = 0; row < (int)height; row++) {
slouken@0
   248
		row_pointers[row] = (png_bytep)
slouken@0
   249
				(Uint8 *)surface->pixels + row*surface->pitch;
slouken@0
   250
	}
slouken@0
   251
slouken@0
   252
	/* Read the entire image in one go */
slouken@0
   253
	png_read_image(png_ptr, row_pointers);
slouken@0
   254
slouken@100
   255
	/* and we're done!  (png_read_end() can be omitted if no processing of
slouken@100
   256
	 * post-IDAT text/time/etc. is desired)
slouken@100
   257
	 * In some cases it can't read PNG's created by some popular programs (ACDSEE),
slouken@100
   258
	 * we do not want to process comments, so we omit png_read_end
slouken@100
   259
slouken@0
   260
	png_read_end(png_ptr, info_ptr);
slouken@100
   261
	*/
slouken@0
   262
slouken@0
   263
	/* Load the palette, if any */
slouken@0
   264
	palette = surface->format->palette;
slouken@4
   265
	if ( palette ) {
slouken@4
   266
	    if(color_type == PNG_COLOR_TYPE_GRAY) {
slouken@4
   267
		palette->ncolors = 256;
slouken@4
   268
		for(i = 0; i < 256; i++) {
slouken@4
   269
		    palette->colors[i].r = i;
slouken@4
   270
		    palette->colors[i].g = i;
slouken@4
   271
		    palette->colors[i].b = i;
slouken@4
   272
		}
slouken@4
   273
	    } else if (info_ptr->num_palette > 0 ) {
slouken@0
   274
		palette->ncolors = info_ptr->num_palette; 
slouken@0
   275
		for( i=0; i<info_ptr->num_palette; ++i ) {
slouken@4
   276
		    palette->colors[i].b = info_ptr->palette[i].blue;
slouken@4
   277
		    palette->colors[i].g = info_ptr->palette[i].green;
slouken@4
   278
		    palette->colors[i].r = info_ptr->palette[i].red;
slouken@0
   279
		}
slouken@4
   280
	    }
slouken@0
   281
	}
slouken@0
   282
slouken@0
   283
done:	/* Clean up and return */
slouken@0
   284
	png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0,
slouken@0
   285
								(png_infopp)0);
slouken@0
   286
	if ( row_pointers ) {
slouken@0
   287
		free(row_pointers);
slouken@0
   288
	}
slouken@0
   289
	return(surface); 
slouken@0
   290
}
slouken@0
   291
slouken@0
   292
#else
slouken@0
   293
slouken@0
   294
/* See if an image is contained in a data source */
slouken@0
   295
int IMG_isPNG(SDL_RWops *src)
slouken@0
   296
{
slouken@0
   297
	return(0);
slouken@0
   298
}
slouken@0
   299
slouken@0
   300
/* Load a PNG type image from an SDL datasource */
slouken@0
   301
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   302
{
slouken@0
   303
	return(NULL);
slouken@0
   304
}
slouken@0
   305
slouken@0
   306
#endif /* LOAD_PNG */