IMG_png.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 17:41:55 +0000
changeset 97 e1161bd417c4
parent 95 7fcb46931afc
child 98 9f94c4674cc9
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
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@22
    79
   unsigned char buf[PNG_BYTES_TO_CHECK];
slouken@0
    80
slouken@0
    81
   /* Read in the signature bytes */
slouken@0
    82
   if (SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK)
slouken@0
    83
      return 0;
slouken@0
    84
slouken@0
    85
   /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
slouken@0
    86
   return( !png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
slouken@0
    87
}
slouken@0
    88
slouken@0
    89
/* Load a PNG type image from an SDL datasource */
slouken@0
    90
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
slouken@0
    91
{
slouken@0
    92
	SDL_RWops *src;
slouken@0
    93
slouken@0
    94
	src = (SDL_RWops *)png_get_io_ptr(ctx);
slouken@0
    95
	SDL_RWread(src, area, size, 1);
slouken@0
    96
}
slouken@0
    97
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
    98
{
slouken@7
    99
	SDL_Surface *volatile surface;
slouken@0
   100
	png_structp png_ptr;
slouken@0
   101
	png_infop info_ptr;
slouken@0
   102
	png_uint_32 width, height;
slouken@0
   103
	int bit_depth, color_type, interlace_type;
slouken@0
   104
	Uint32 Rmask;
slouken@0
   105
	Uint32 Gmask;
slouken@0
   106
	Uint32 Bmask;
slouken@0
   107
	Uint32 Amask;
slouken@0
   108
	SDL_Palette *palette;
slouken@7
   109
	png_bytep *volatile row_pointers;
slouken@0
   110
	int row, i;
slouken@7
   111
	volatile int ckey = -1;
slouken@0
   112
	png_color_16 *transv;
slouken@0
   113
slouken@0
   114
	/* Initialize the data we will clean up when we're done */
slouken@0
   115
	png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
slouken@0
   116
slouken@0
   117
	/* Create the PNG loading context structure */
slouken@0
   118
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
slouken@0
   119
					  NULL,NULL,NULL);
slouken@0
   120
	if (png_ptr == NULL){
slouken@0
   121
		IMG_SetError("Couldn't allocate memory for PNG file");
slouken@0
   122
		goto done;
slouken@0
   123
	}
slouken@0
   124
slouken@0
   125
	 /* Allocate/initialize the memory for image information.  REQUIRED. */
slouken@0
   126
	info_ptr = png_create_info_struct(png_ptr);
slouken@0
   127
	if (info_ptr == NULL) {
slouken@0
   128
		IMG_SetError("Couldn't create image information for PNG file");
slouken@0
   129
		goto done;
slouken@0
   130
	}
slouken@0
   131
slouken@0
   132
	/* Set error handling if you are using setjmp/longjmp method (this is
slouken@0
   133
	 * the normal method of doing things with libpng).  REQUIRED unless you
slouken@0
   134
	 * set up your own error handlers in png_create_read_struct() earlier.
slouken@0
   135
	 */
slouken@0
   136
	if ( setjmp(png_ptr->jmpbuf) ) {
slouken@0
   137
		IMG_SetError("Error reading the PNG file.");
slouken@0
   138
		goto done;
slouken@0
   139
	}
slouken@0
   140
slouken@0
   141
	/* Set up the input control */
slouken@0
   142
	png_set_read_fn(png_ptr, src, png_read_data);
slouken@0
   143
slouken@0
   144
	/* Read PNG header info */
slouken@0
   145
	png_read_info(png_ptr, info_ptr);
slouken@0
   146
	png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@0
   147
			&color_type, &interlace_type, NULL, NULL);
slouken@0
   148
slouken@0
   149
	/* tell libpng to strip 16 bit/color files down to 8 bits/color */
slouken@0
   150
	png_set_strip_16(png_ptr) ;
slouken@0
   151
slouken@0
   152
	/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
slouken@0
   153
	 * byte into separate bytes (useful for paletted and grayscale images).
slouken@0
   154
	 */
slouken@0
   155
	png_set_packing(png_ptr);
slouken@0
   156
slouken@4
   157
	/* scale greyscale values to the range 0..255 */
slouken@4
   158
	if(color_type == PNG_COLOR_TYPE_GRAY)
slouken@4
   159
		png_set_expand(png_ptr);
slouken@0
   160
slouken@0
   161
	/* For images with a single "transparent colour", set colour key;
slouken@41
   162
	   if more than one index has transparency, or if partially transparent
slouken@41
   163
	   entries exist, use full alpha channel */
slouken@0
   164
	if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
slouken@0
   165
	        int num_trans;
slouken@0
   166
		Uint8 *trans;
slouken@0
   167
		png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
slouken@0
   168
			     &transv);
slouken@0
   169
		if(color_type == PNG_COLOR_TYPE_PALETTE) {
slouken@41
   170
		    /* Check if all tRNS entries are opaque except one */
slouken@41
   171
		    int i, t = -1;
slouken@41
   172
		    for(i = 0; i < num_trans; i++)
slouken@41
   173
			if(trans[i] == 0) {
slouken@41
   174
			    if(t >= 0)
slouken@41
   175
				break;
slouken@41
   176
			    t = i;
slouken@41
   177
			} else if(trans[i] != 255)
slouken@41
   178
			    break;
slouken@41
   179
		    if(i == num_trans) {
slouken@41
   180
			/* exactly one transparent index */
slouken@41
   181
			ckey = t;
slouken@41
   182
		    } else {
slouken@41
   183
			/* more than one transparent index, or translucency */
slouken@0
   184
			png_set_expand(png_ptr);
slouken@41
   185
		    }
slouken@0
   186
		} else
slouken@0
   187
		    ckey = 0; /* actual value will be set later */
slouken@0
   188
	}
slouken@0
   189
slouken@4
   190
	if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
slouken@0
   191
		png_set_gray_to_rgb(png_ptr);
slouken@0
   192
slouken@0
   193
	png_read_update_info(png_ptr, info_ptr);
slouken@0
   194
slouken@0
   195
	png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@0
   196
			&color_type, &interlace_type, NULL, NULL);
slouken@0
   197
slouken@0
   198
	/* Allocate the SDL surface to hold the image */
slouken@0
   199
	Rmask = Gmask = Bmask = Amask = 0 ; 
slouken@0
   200
	if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
slouken@0
   201
		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
slouken@0
   202
			Rmask = 0x000000FF;
slouken@0
   203
			Gmask = 0x0000FF00;
slouken@0
   204
			Bmask = 0x00FF0000;
slouken@0
   205
			Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
slouken@0
   206
		} else {
slouken@0
   207
		        int s = (info_ptr->channels == 4) ? 0 : 8;
slouken@0
   208
			Rmask = 0xFF000000 >> s;
slouken@0
   209
			Gmask = 0x00FF0000 >> s;
slouken@0
   210
			Bmask = 0x0000FF00 >> s;
slouken@0
   211
			Amask = 0x000000FF >> s;
slouken@0
   212
		}
slouken@0
   213
	}
slouken@0
   214
	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
slouken@0
   215
			bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
slouken@0
   216
	if ( surface == NULL ) {
slouken@0
   217
		IMG_SetError("Out of memory");
slouken@0
   218
		goto done;
slouken@0
   219
	}
slouken@0
   220
slouken@0
   221
	if(ckey != -1) {
slouken@0
   222
	        if(color_type != PNG_COLOR_TYPE_PALETTE)
slouken@9
   223
			/* FIXME: Should these be truncated or shifted down? */
slouken@9
   224
		        ckey = SDL_MapRGB(surface->format,
slouken@9
   225
			                 (Uint8)transv->red,
slouken@9
   226
			                 (Uint8)transv->green,
slouken@9
   227
			                 (Uint8)transv->blue);
slouken@0
   228
	        SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
slouken@0
   229
	}
slouken@0
   230
slouken@0
   231
	/* Create the array of pointers to image data */
slouken@0
   232
	row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
slouken@0
   233
	if ( (row_pointers == NULL) ) {
slouken@0
   234
		IMG_SetError("Out of memory");
slouken@0
   235
		SDL_FreeSurface(surface);
slouken@0
   236
		surface = NULL;
slouken@0
   237
		goto done;
slouken@0
   238
	}
slouken@9
   239
	for (row = 0; row < (int)height; row++) {
slouken@0
   240
		row_pointers[row] = (png_bytep)
slouken@0
   241
				(Uint8 *)surface->pixels + row*surface->pitch;
slouken@0
   242
	}
slouken@0
   243
slouken@0
   244
	/* Read the entire image in one go */
slouken@0
   245
	png_read_image(png_ptr, row_pointers);
slouken@0
   246
slouken@0
   247
	/* read rest of file, get additional chunks in info_ptr - REQUIRED */
slouken@0
   248
	png_read_end(png_ptr, info_ptr);
slouken@0
   249
slouken@0
   250
	/* Load the palette, if any */
slouken@0
   251
	palette = surface->format->palette;
slouken@4
   252
	if ( palette ) {
slouken@4
   253
	    if(color_type == PNG_COLOR_TYPE_GRAY) {
slouken@4
   254
		palette->ncolors = 256;
slouken@4
   255
		for(i = 0; i < 256; i++) {
slouken@4
   256
		    palette->colors[i].r = i;
slouken@4
   257
		    palette->colors[i].g = i;
slouken@4
   258
		    palette->colors[i].b = i;
slouken@4
   259
		}
slouken@4
   260
	    } else if (info_ptr->num_palette > 0 ) {
slouken@0
   261
		palette->ncolors = info_ptr->num_palette; 
slouken@0
   262
		for( i=0; i<info_ptr->num_palette; ++i ) {
slouken@4
   263
		    palette->colors[i].b = info_ptr->palette[i].blue;
slouken@4
   264
		    palette->colors[i].g = info_ptr->palette[i].green;
slouken@4
   265
		    palette->colors[i].r = info_ptr->palette[i].red;
slouken@0
   266
		}
slouken@4
   267
	    }
slouken@0
   268
	}
slouken@0
   269
slouken@0
   270
done:	/* Clean up and return */
slouken@0
   271
	png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0,
slouken@0
   272
								(png_infopp)0);
slouken@0
   273
	if ( row_pointers ) {
slouken@0
   274
		free(row_pointers);
slouken@0
   275
	}
slouken@0
   276
	return(surface); 
slouken@0
   277
}
slouken@0
   278
slouken@0
   279
#else
slouken@0
   280
slouken@0
   281
/* See if an image is contained in a data source */
slouken@0
   282
int IMG_isPNG(SDL_RWops *src)
slouken@0
   283
{
slouken@0
   284
	return(0);
slouken@0
   285
}
slouken@0
   286
slouken@0
   287
/* Load a PNG type image from an SDL datasource */
slouken@0
   288
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   289
{
slouken@0
   290
	return(NULL);
slouken@0
   291
}
slouken@0
   292
slouken@0
   293
#endif /* LOAD_PNG */