IMG_jpg.c
changeset 118 c5e736a47ad2
parent 117 e613cf987897
child 121 1bf9c0c87374
     1.1 --- a/IMG_jpg.c	Sat Feb 04 20:37:17 2006 +0000
     1.2 +++ b/IMG_jpg.c	Sat Feb 04 22:26:04 2006 +0000
     1.3 @@ -202,6 +202,7 @@
     1.4  /* Load a JPEG type image from an SDL datasource */
     1.5  SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
     1.6  {
     1.7 +	int start;
     1.8  	struct jpeg_decompress_struct cinfo;
     1.9  	JSAMPROW rowptr[1];
    1.10  	SDL_Surface *volatile surface = NULL;
    1.11 @@ -211,6 +212,7 @@
    1.12  		/* The error message has been set in SDL_RWFromFile */
    1.13  		return NULL;
    1.14  	}
    1.15 +	start = SDL_RWtell(src);
    1.16  
    1.17  	/* Create a decompression structure and load the JPEG header */
    1.18  	cinfo.err = jpeg_std_error(&jerr.errmgr);
    1.19 @@ -219,8 +221,11 @@
    1.20  	if(setjmp(jerr.escape)) {
    1.21  		/* If we get here, libjpeg found an error */
    1.22  		jpeg_destroy_decompress(&cinfo);
    1.23 +		if ( surface != NULL ) {
    1.24 +			SDL_FreeSurface(surface);
    1.25 +		}
    1.26 +		SDL_RWseek(src, start, SEEK_SET);
    1.27  		IMG_SetError("JPEG loading error");
    1.28 -		SDL_FreeSurface(surface);
    1.29  		return NULL;
    1.30  	}
    1.31  
    1.32 @@ -249,8 +254,10 @@
    1.33  #endif
    1.34  				   0);
    1.35  	if ( surface == NULL ) {
    1.36 +		jpeg_destroy_decompress(&cinfo);
    1.37 +		SDL_RWseek(src, start, SEEK_SET);
    1.38  		IMG_SetError("Out of memory");
    1.39 -		goto done;
    1.40 +		return NULL;
    1.41  	}
    1.42  
    1.43  	/* Decompress the image */
    1.44 @@ -261,10 +268,8 @@
    1.45  		jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
    1.46  	}
    1.47  	jpeg_finish_decompress(&cinfo);
    1.48 +	jpeg_destroy_decompress(&cinfo);
    1.49  
    1.50 -	/* Clean up and return */
    1.51 -done:
    1.52 -	jpeg_destroy_decompress(&cinfo);
    1.53  	return(surface);
    1.54  }
    1.55