Skip to content

Commit

Permalink
If there's an error loading an image, seek back to the start of the i…
Browse files Browse the repository at this point in the history
…mage.
  • Loading branch information
slouken committed Feb 4, 2006
1 parent b2ed3ce commit 289761a
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 48 deletions.
3 changes: 3 additions & 0 deletions IMG_bmp.c
Expand Up @@ -422,6 +422,9 @@ static SDL_Surface *LoadBMP_RW (SDL_RWops *src, int freesrc)
}
done:
if ( was_error ) {
if ( src ) {
SDL_RWseek(src, fp_offset, SEEK_SET);
}
if ( surface ) {
SDL_FreeSurface(surface);
}
Expand Down
8 changes: 7 additions & 1 deletion IMG_gif.c
Expand Up @@ -151,6 +151,7 @@ static Image *ReadImage(SDL_RWops * src, int len, int height, int,
Image *
IMG_LoadGIF_RW(SDL_RWops *src)
{
int start;
unsigned char buf[16];
unsigned char c;
unsigned char localColorMap[3][MAXCOLORMAPSIZE];
Expand All @@ -163,8 +164,10 @@ IMG_LoadGIF_RW(SDL_RWops *src)
Image *image = NULL;

if ( src == NULL ) {
goto done;
return NULL;
}
start = SDL_RWtell(src);

if (!ReadOK(src, buf, 6)) {
RWSetMsg("error reading magic number");
goto done;
Expand Down Expand Up @@ -262,6 +265,9 @@ IMG_LoadGIF_RW(SDL_RWops *src)
#endif

done:
if ( image == NULL ) {
SDL_RWseek(src, start, SEEK_SET);
}
return image;
}

Expand Down
15 changes: 10 additions & 5 deletions IMG_jpg.c
Expand Up @@ -202,6 +202,7 @@ static void output_no_message(j_common_ptr cinfo)
/* Load a JPEG type image from an SDL datasource */
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
{
int start;
struct jpeg_decompress_struct cinfo;
JSAMPROW rowptr[1];
SDL_Surface *volatile surface = NULL;
Expand All @@ -211,6 +212,7 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);

/* Create a decompression structure and load the JPEG header */
cinfo.err = jpeg_std_error(&jerr.errmgr);
Expand All @@ -219,8 +221,11 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
if(setjmp(jerr.escape)) {
/* If we get here, libjpeg found an error */
jpeg_destroy_decompress(&cinfo);
if ( surface != NULL ) {
SDL_FreeSurface(surface);
}
SDL_RWseek(src, start, SEEK_SET);
IMG_SetError("JPEG loading error");
SDL_FreeSurface(surface);
return NULL;
}

Expand Down Expand Up @@ -249,8 +254,10 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
#endif
0);
if ( surface == NULL ) {
jpeg_destroy_decompress(&cinfo);
SDL_RWseek(src, start, SEEK_SET);
IMG_SetError("Out of memory");
goto done;
return NULL;
}

/* Decompress the image */
Expand All @@ -261,10 +268,8 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
}
jpeg_finish_decompress(&cinfo);

/* Clean up and return */
done:
jpeg_destroy_decompress(&cinfo);

return(surface);
}

Expand Down
10 changes: 8 additions & 2 deletions IMG_lbm.c
Expand Up @@ -84,6 +84,7 @@ int IMG_isLBM( SDL_RWops *src )

SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
{
int start;
SDL_Surface *Image;
Uint8 id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
Uint32 size, bytesloaded, nbcolors;
Expand All @@ -102,6 +103,8 @@ SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);

if ( !SDL_RWread( src, id, 4, 1 ) )
{
error="error reading IFF chunk";
Expand Down Expand Up @@ -469,9 +472,12 @@ SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )

if ( error )
{
SDL_RWseek(src, start, SEEK_SET);
if ( Image ) {
SDL_FreeSurface( Image );
Image = NULL;
}
IMG_SetError( error );
SDL_FreeSurface( Image );
Image = NULL;
}

return( Image );
Expand Down
10 changes: 8 additions & 2 deletions IMG_pcx.c
Expand Up @@ -87,6 +87,7 @@ int IMG_isPCX(SDL_RWops *src)
/* Load a PCX type image from an SDL datasource */
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
{
int start;
struct PCXheader pcxh;
Uint32 Rmask;
Uint32 Gmask;
Expand All @@ -103,6 +104,8 @@ SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);

if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
error = "file truncated";
goto done;
Expand Down Expand Up @@ -235,9 +238,12 @@ SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
done:
free(buf);
if ( error ) {
SDL_FreeSurface(surface);
SDL_RWseek(src, start, SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError(error);
surface = NULL;
}
return(surface);
}
Expand Down
29 changes: 21 additions & 8 deletions IMG_png.c
Expand Up @@ -99,6 +99,8 @@ static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
}
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
{
int start;
const char *error;
SDL_Surface *volatile surface;
png_structp png_ptr;
png_infop info_ptr;
Expand All @@ -118,22 +120,24 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);

/* Initialize the data we will clean up when we're done */
error = NULL;
png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;

/* Create the PNG loading context structure */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,NULL,NULL);
if (png_ptr == NULL){
IMG_SetError("Couldn't allocate memory for PNG file or incompatible PNG dll");
error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
goto done;
}

/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
IMG_SetError("Couldn't create image information for PNG file");
error = "Couldn't create image information for PNG file";
goto done;
}

Expand All @@ -142,7 +146,7 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
* set up your own error handlers in png_create_read_struct() earlier.
*/
if ( setjmp(png_ptr->jmpbuf) ) {
IMG_SetError("Error reading the PNG file.");
error = "Error reading the PNG file.";
goto done;
}

Expand Down Expand Up @@ -222,7 +226,7 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
if ( surface == NULL ) {
IMG_SetError("Out of memory");
error = "Out of memory";
goto done;
}

Expand All @@ -239,9 +243,7 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
/* Create the array of pointers to image data */
row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
if ( (row_pointers == NULL) ) {
IMG_SetError("Out of memory");
SDL_FreeSurface(surface);
surface = NULL;
error = "Out of memory";
goto done;
}
for (row = 0; row < (int)height; row++) {
Expand Down Expand Up @@ -281,11 +283,22 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
}

done: /* Clean up and return */
png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0,
if ( png_ptr ) {
png_destroy_read_struct(&png_ptr,
info_ptr ? &info_ptr : (png_infopp)0,
(png_infopp)0);
}
if ( row_pointers ) {
free(row_pointers);
}
if ( error ) {
SDL_RWseek(src, start, SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError(error);
}
return(surface);
}

Expand Down
9 changes: 7 additions & 2 deletions IMG_pnm.c
Expand Up @@ -105,6 +105,7 @@ static int ReadNumber(SDL_RWops *src)

SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
{
int start;
SDL_Surface *surface = NULL;
int width, height;
int maxval, y, bpl;
Expand All @@ -121,6 +122,7 @@ SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);

SDL_RWread(src, magic, 2, 1);
kind = magic[1] - '1';
Expand Down Expand Up @@ -229,9 +231,12 @@ SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
done:
free(buf);
if(error) {
SDL_FreeSurface(surface);
SDL_RWseek(src, start, SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError(error);
surface = NULL;
}
return(surface);
}
Expand Down
30 changes: 19 additions & 11 deletions IMG_tga.c
Expand Up @@ -87,14 +87,15 @@ enum tga_type {
SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
{
int start;
const char *error = NULL;
struct TGAheader hdr;
int rle = 0;
int alpha = 0;
int indexed = 0;
int grey = 0;
int ckey = -1;
int ncols, w, h;
SDL_Surface *img;
SDL_Surface *img = NULL;
Uint32 rmask, gmask, bmask, amask;
Uint8 *dst;
int i;
Expand All @@ -107,18 +108,20 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
/* The error message has been set in SDL_RWFromFile */
return NULL;
}

start = SDL_RWtell(src);
if(!SDL_RWread(src, &hdr, sizeof(hdr), 1))

if(!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
error = "Error reading TGA data";
goto error;
}
ncols = LE16(hdr.cmap_len);
switch(hdr.type) {
case TGA_TYPE_RLE_INDEXED:
rle = 1;
/* fallthrough */
case TGA_TYPE_INDEXED:
if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
goto error;
goto unsupported;
indexed = 1;
break;

Expand All @@ -134,7 +137,7 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
/* fallthrough */
case TGA_TYPE_BW:
if(hdr.pixel_bits != 8)
goto error;
goto unsupported;
/* Treat greyscale as 8bpp indexed images */
indexed = grey = 1;
break;
Expand Down Expand Up @@ -195,6 +198,10 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
bpp * 8,
rmask, gmask, bmask, amask);
if(img == NULL) {
error = "Out of memory";
goto error;
}

if(hdr.has_cmap) {
int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
Expand Down Expand Up @@ -303,14 +310,15 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
}
return img;

error:
SDL_RWseek(src, start, SEEK_SET);
IMG_SetError("Error reading TGA data");
return NULL;

unsupported:
error = "Unsupported TGA format";

error:
SDL_RWseek(src, start, SEEK_SET);
IMG_SetError("unsupported TGA format");
if ( img ) {
SDL_FreeSurface(img);
}
IMG_SetError(error);
return NULL;
}

Expand Down

0 comments on commit 289761a

Please sign in to comment.