IMG_png.c
changeset 118 c5e736a47ad2
parent 117 e613cf987897
child 121 1bf9c0c87374
     1.1 --- a/IMG_png.c	Sat Feb 04 20:37:17 2006 +0000
     1.2 +++ b/IMG_png.c	Sat Feb 04 22:26:04 2006 +0000
     1.3 @@ -99,6 +99,8 @@
     1.4  }
     1.5  SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
     1.6  {
     1.7 +	int start;
     1.8 +	const char *error;
     1.9  	SDL_Surface *volatile surface;
    1.10  	png_structp png_ptr;
    1.11  	png_infop info_ptr;
    1.12 @@ -118,22 +120,24 @@
    1.13  		/* The error message has been set in SDL_RWFromFile */
    1.14  		return NULL;
    1.15  	}
    1.16 +	start = SDL_RWtell(src);
    1.17  
    1.18  	/* Initialize the data we will clean up when we're done */
    1.19 +	error = NULL;
    1.20  	png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
    1.21  
    1.22  	/* Create the PNG loading context structure */
    1.23  	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
    1.24  					  NULL,NULL,NULL);
    1.25  	if (png_ptr == NULL){
    1.26 -		IMG_SetError("Couldn't allocate memory for PNG file or incompatible PNG dll");
    1.27 +		error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
    1.28  		goto done;
    1.29  	}
    1.30  
    1.31  	 /* Allocate/initialize the memory for image information.  REQUIRED. */
    1.32  	info_ptr = png_create_info_struct(png_ptr);
    1.33  	if (info_ptr == NULL) {
    1.34 -		IMG_SetError("Couldn't create image information for PNG file");
    1.35 +		error = "Couldn't create image information for PNG file";
    1.36  		goto done;
    1.37  	}
    1.38  
    1.39 @@ -142,7 +146,7 @@
    1.40  	 * set up your own error handlers in png_create_read_struct() earlier.
    1.41  	 */
    1.42  	if ( setjmp(png_ptr->jmpbuf) ) {
    1.43 -		IMG_SetError("Error reading the PNG file.");
    1.44 +		error = "Error reading the PNG file.";
    1.45  		goto done;
    1.46  	}
    1.47  
    1.48 @@ -222,7 +226,7 @@
    1.49  	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
    1.50  			bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
    1.51  	if ( surface == NULL ) {
    1.52 -		IMG_SetError("Out of memory");
    1.53 +		error = "Out of memory";
    1.54  		goto done;
    1.55  	}
    1.56  
    1.57 @@ -239,9 +243,7 @@
    1.58  	/* Create the array of pointers to image data */
    1.59  	row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
    1.60  	if ( (row_pointers == NULL) ) {
    1.61 -		IMG_SetError("Out of memory");
    1.62 -		SDL_FreeSurface(surface);
    1.63 -		surface = NULL;
    1.64 +		error = "Out of memory";
    1.65  		goto done;
    1.66  	}
    1.67  	for (row = 0; row < (int)height; row++) {
    1.68 @@ -281,11 +283,22 @@
    1.69  	}
    1.70  
    1.71  done:	/* Clean up and return */
    1.72 -	png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0,
    1.73 +	if ( png_ptr ) {
    1.74 +		png_destroy_read_struct(&png_ptr,
    1.75 +		                        info_ptr ? &info_ptr : (png_infopp)0,
    1.76  								(png_infopp)0);
    1.77 +	}
    1.78  	if ( row_pointers ) {
    1.79  		free(row_pointers);
    1.80  	}
    1.81 +	if ( error ) {
    1.82 +		SDL_RWseek(src, start, SEEK_SET);
    1.83 +		if ( surface ) {
    1.84 +			SDL_FreeSurface(surface);
    1.85 +			surface = NULL;
    1.86 +		}
    1.87 +		IMG_SetError(error);
    1.88 +	}
    1.89  	return(surface); 
    1.90  }
    1.91