Fixed image type functions so they seek back to where they started
authorSam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 20:37:17 +0000
changeset 117e613cf987897
parent 116 df32aeaf0b10
child 118 c5e736a47ad2
Fixed image type functions so they seek back to where they started
IMG.c
IMG_bmp.c
IMG_gif.c
IMG_jpg.c
IMG_lbm.c
IMG_pcx.c
IMG_png.c
IMG_pnm.c
IMG_tga.c
IMG_tif.c
IMG_xcf.c
IMG_xpm.c
IMG_xxx.c
     1.1 --- a/IMG.c	Sat Feb 04 04:53:36 2006 +0000
     1.2 +++ b/IMG.c	Sat Feb 04 20:37:17 2006 +0000
     1.3 @@ -96,7 +96,7 @@
     1.4  /* Load an image from an SDL datasource, optionally specifying the type */
     1.5  SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
     1.6  {
     1.7 -	int i, start;
     1.8 +	int i;
     1.9  	SDL_Surface *image;
    1.10  
    1.11  	/* Make sure there is something to do.. */
    1.12 @@ -114,11 +114,9 @@
    1.13  	}
    1.14  
    1.15  	/* Detect the type of image being loaded */
    1.16 -	start = SDL_RWtell(src);
    1.17  	image = NULL;
    1.18  	for ( i=0; i < ARRAYSIZE(supported); ++i ) {
    1.19  		if(supported[i].is) {
    1.20 -			SDL_RWseek(src, start, SEEK_SET);
    1.21  			if(!supported[i].is(src))
    1.22  				continue;
    1.23  		} else {
    1.24 @@ -131,7 +129,6 @@
    1.25  		fprintf(stderr, "IMGLIB: Loading image as %s\n",
    1.26  			supported[i].type);
    1.27  #endif
    1.28 -		SDL_RWseek(src, start, SEEK_SET);
    1.29  		image = supported[i].load(src);
    1.30  		if(freesrc)
    1.31  			SDL_RWclose(src);
     2.1 --- a/IMG_bmp.c	Sat Feb 04 04:53:36 2006 +0000
     2.2 +++ b/IMG_bmp.c	Sat Feb 04 20:37:17 2006 +0000
     2.3 @@ -34,15 +34,18 @@
     2.4  /* See if an image is contained in a data source */
     2.5  int IMG_isBMP(SDL_RWops *src)
     2.6  {
     2.7 +	int start;
     2.8  	int is_BMP;
     2.9  	char magic[2];
    2.10  
    2.11 +	start = SDL_RWtell(src);
    2.12  	is_BMP = 0;
    2.13 -	if ( SDL_RWread(src, magic, 2, 1) ) {
    2.14 +	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
    2.15  		if ( strncmp(magic, "BM", 2) == 0 ) {
    2.16  			is_BMP = 1;
    2.17  		}
    2.18  	}
    2.19 +	SDL_RWseek(src, start, SEEK_SET);
    2.20  	return(is_BMP);
    2.21  }
    2.22  
     3.1 --- a/IMG_gif.c	Sat Feb 04 04:53:36 2006 +0000
     3.2 +++ b/IMG_gif.c	Sat Feb 04 20:37:17 2006 +0000
     3.3 @@ -34,17 +34,20 @@
     3.4  /* See if an image is contained in a data source */
     3.5  int IMG_isGIF(SDL_RWops *src)
     3.6  {
     3.7 +	int start;
     3.8  	int is_GIF;
     3.9  	char magic[6];
    3.10  
    3.11 +	start = SDL_RWtell(src);
    3.12  	is_GIF = 0;
    3.13 -	if ( SDL_RWread(src, magic, 6, 1) ) {
    3.14 +	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
    3.15  		if ( (strncmp(magic, "GIF", 3) == 0) &&
    3.16  		     ((memcmp(magic + 3, "87a", 3) == 0) ||
    3.17  		      (memcmp(magic + 3, "89a", 3) == 0)) ) {
    3.18  			is_GIF = 1;
    3.19  		}
    3.20  	}
    3.21 +	SDL_RWseek(src, start, SEEK_SET);
    3.22  	return(is_GIF);
    3.23  }
    3.24  
     4.1 --- a/IMG_jpg.c	Sat Feb 04 04:53:36 2006 +0000
     4.2 +++ b/IMG_jpg.c	Sat Feb 04 20:37:17 2006 +0000
     4.3 @@ -40,9 +40,11 @@
     4.4  /* See if an image is contained in a data source */
     4.5  int IMG_isJPG(SDL_RWops *src)
     4.6  {
     4.7 +	int start;
     4.8  	int is_JPG;
     4.9  	Uint8 magic[4];
    4.10  
    4.11 +	start = SDL_RWtell(src);
    4.12  	is_JPG = 0;
    4.13  	if ( SDL_RWread(src, magic, 2, 1) ) {
    4.14  		if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
    4.15 @@ -61,6 +63,7 @@
    4.16  			}
    4.17  		}
    4.18  	}
    4.19 +	SDL_RWseek(src, start, SEEK_SET);
    4.20  	return(is_JPG);
    4.21  }
    4.22  
     5.1 --- a/IMG_lbm.c	Sat Feb 04 04:53:36 2006 +0000
     5.2 +++ b/IMG_lbm.c	Sat Feb 04 20:37:17 2006 +0000
     5.3 @@ -63,11 +63,13 @@
     5.4  
     5.5  int IMG_isLBM( SDL_RWops *src )
     5.6  {
     5.7 +	int start;
     5.8  	int   is_LBM;
     5.9  	Uint8 magic[4+4+4];
    5.10  
    5.11 +	start = SDL_RWtell(src);
    5.12  	is_LBM = 0;
    5.13 -	if ( SDL_RWread( src, magic, 4+4+4, 1 ) )
    5.14 +	if ( SDL_RWread( src, magic, sizeof(magic), 1 ) )
    5.15  	{
    5.16  		if ( !memcmp( magic, "FORM", 4 ) &&
    5.17  			( !memcmp( magic + 8, "PBM ", 4 ) ||
    5.18 @@ -76,6 +78,7 @@
    5.19  			is_LBM = 1;
    5.20  		}
    5.21  	}
    5.22 +	SDL_RWseek(src, start, SEEK_SET);
    5.23  	return( is_LBM );
    5.24  }
    5.25  
     6.1 --- a/IMG_pcx.c	Sat Feb 04 04:53:36 2006 +0000
     6.2 +++ b/IMG_pcx.c	Sat Feb 04 20:37:17 2006 +0000
     6.3 @@ -64,12 +64,14 @@
     6.4  /* See if an image is contained in a data source */
     6.5  int IMG_isPCX(SDL_RWops *src)
     6.6  {
     6.7 +	int start;
     6.8  	int is_PCX;
     6.9  	const int ZSoft_Manufacturer = 10;
    6.10  	const int PC_Paintbrush_Version = 5;
    6.11  	const int PCX_RunLength_Encoding = 1;
    6.12  	struct PCXheader pcxh;
    6.13  
    6.14 +	start = SDL_RWtell(src);
    6.15  	is_PCX = 0;
    6.16  	if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
    6.17  		if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
    6.18 @@ -78,6 +80,7 @@
    6.19  			is_PCX = 1;
    6.20  		}
    6.21  	}
    6.22 +	SDL_RWseek(src, start, SEEK_SET);
    6.23  	return(is_PCX);
    6.24  }
    6.25  
     7.1 --- a/IMG_png.c	Sat Feb 04 04:53:36 2006 +0000
     7.2 +++ b/IMG_png.c	Sat Feb 04 20:37:17 2006 +0000
     7.3 @@ -76,14 +76,17 @@
     7.4  /* See if an image is contained in a data source */
     7.5  int IMG_isPNG(SDL_RWops *src)
     7.6  {
     7.7 -   unsigned char buf[PNG_BYTES_TO_CHECK];
     7.8 +	int start;
     7.9 +	int is_PNG;
    7.10 +	unsigned char buf[PNG_BYTES_TO_CHECK];
    7.11  
    7.12 -   /* Read in the signature bytes */
    7.13 -   if (SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK)
    7.14 -      return 0;
    7.15 -
    7.16 -   /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
    7.17 -   return( !png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
    7.18 +	start = SDL_RWtell(src);
    7.19 +	is_PNG = 0;
    7.20 +	if ( SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK ) {
    7.21 +		is_PNG = (png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK) == 0);
    7.22 +	}
    7.23 +	SDL_RWseek(src, start, SEEK_SET);
    7.24 +	return(is_PNG);
    7.25  }
    7.26  
    7.27  /* Load a PNG type image from an SDL datasource */
     8.1 --- a/IMG_pnm.c	Sat Feb 04 04:53:36 2006 +0000
     8.2 +++ b/IMG_pnm.c	Sat Feb 04 20:37:17 2006 +0000
     8.3 @@ -42,19 +42,28 @@
     8.4  /* See if an image is contained in a data source */
     8.5  int IMG_isPNM(SDL_RWops *src)
     8.6  {
     8.7 +	int start;
     8.8 +	int is_PNM;
     8.9  	char magic[2];
    8.10  
    8.11 -	/*
    8.12 -	 * PNM magic signatures:
    8.13 -	 * P1	PBM, ascii format
    8.14 -	 * P2	PGM, ascii format
    8.15 -	 * P3	PPM, ascii format
    8.16 -	 * P4	PBM, binary format
    8.17 -	 * P5	PGM, binary format
    8.18 -	 * P6	PPM, binary format
    8.19 -	 */
    8.20 -	return (SDL_RWread(src, magic, 2, 1)
    8.21 -		&& magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6');
    8.22 +	start = SDL_RWtell(src);
    8.23 +	is_PNM = 0;
    8.24 +	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
    8.25 +		/*
    8.26 +		 * PNM magic signatures:
    8.27 +		 * P1	PBM, ascii format
    8.28 +		 * P2	PGM, ascii format
    8.29 +		 * P3	PPM, ascii format
    8.30 +		 * P4	PBM, binary format
    8.31 +		 * P5	PGM, binary format
    8.32 +		 * P6	PPM, binary format
    8.33 +		 */
    8.34 +		if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
    8.35 +			is_PNM = 1;
    8.36 +		}
    8.37 +	}
    8.38 +	SDL_RWseek(src, start, SEEK_SET);
    8.39 +	return(is_PNM);
    8.40  }
    8.41  
    8.42  /* read a non-negative integer from the source. return -1 upon error */
     9.1 --- a/IMG_tga.c	Sat Feb 04 04:53:36 2006 +0000
     9.2 +++ b/IMG_tga.c	Sat Feb 04 20:37:17 2006 +0000
     9.3 @@ -83,14 +83,10 @@
     9.4  #define LE16(p) ((p)[0] + ((p)[1] << 8))
     9.5  #define SETLE16(p, v) ((p)[0] = (v), (p)[1] = (v) >> 8)
     9.6  
     9.7 -static void unsupported(void)
     9.8 -{
     9.9 -    IMG_SetError("unsupported TGA format");
    9.10 -}
    9.11 -
    9.12  /* Load a TGA type image from an SDL datasource */
    9.13  SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
    9.14  {
    9.15 +    int start;
    9.16      struct TGAheader hdr;
    9.17      int rle = 0;
    9.18      int alpha = 0;
    9.19 @@ -112,6 +108,7 @@
    9.20          return NULL;
    9.21      }
    9.22  
    9.23 +    start = SDL_RWtell(src);
    9.24      if(!SDL_RWread(src, &hdr, sizeof(hdr), 1))
    9.25  	goto error;
    9.26      ncols = LE16(hdr.cmap_len);
    9.27 @@ -143,8 +140,7 @@
    9.28  	break;
    9.29  
    9.30      default:
    9.31 -        unsupported();
    9.32 -	return NULL;
    9.33 +        goto unsupported;
    9.34      }
    9.35  
    9.36      bpp = (hdr.pixel_bits + 7) >> 3;
    9.37 @@ -152,8 +148,7 @@
    9.38      switch(hdr.pixel_bits) {
    9.39      case 8:
    9.40  	if(!indexed) {
    9.41 -	    unsupported();
    9.42 -	    return NULL;
    9.43 +            goto unsupported;
    9.44  	}
    9.45  	break;
    9.46  
    9.47 @@ -185,14 +180,12 @@
    9.48  	break;
    9.49  
    9.50      default:
    9.51 -	unsupported();
    9.52 -	return NULL;
    9.53 +        goto unsupported;
    9.54      }
    9.55  
    9.56      if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
    9.57         || hdr.flags & TGA_ORIGIN_RIGHT) {
    9.58 -	unsupported();
    9.59 -	return NULL;
    9.60 +        goto unsupported;
    9.61      }
    9.62      
    9.63      SDL_RWseek(src, hdr.infolen, SEEK_CUR); /* skip info field */
    9.64 @@ -311,8 +304,14 @@
    9.65      return img;
    9.66  
    9.67  error:
    9.68 +    SDL_RWseek(src, start, SEEK_SET);
    9.69      IMG_SetError("Error reading TGA data");
    9.70      return NULL;
    9.71 +
    9.72 +unsupported:
    9.73 +    SDL_RWseek(src, start, SEEK_SET);
    9.74 +    IMG_SetError("unsupported TGA format");
    9.75 +    return NULL;
    9.76  }
    9.77  
    9.78  #else
    10.1 --- a/IMG_tif.c	Sat Feb 04 04:53:36 2006 +0000
    10.2 +++ b/IMG_tif.c	Sat Feb 04 20:37:17 2006 +0000
    10.3 @@ -77,9 +77,14 @@
    10.4  
    10.5  int IMG_isTIF(SDL_RWops* src)
    10.6  {
    10.7 +	int start;
    10.8 +	int is_TIF;
    10.9  	TIFF* tiff;
   10.10  	TIFFErrorHandler prev_handler;
   10.11  
   10.12 +	start = SDL_RWtell(src);
   10.13 +	is_TIF = 0;
   10.14 +
   10.15  	/* Suppress output from libtiff */
   10.16  	prev_handler = TIFFSetErrorHandler(NULL);
   10.17  	
   10.18 @@ -92,13 +97,14 @@
   10.19  	TIFFSetErrorHandler(prev_handler);
   10.20  
   10.21  	/* If it's not a TIFF, then tiff will be NULL. */
   10.22 -	if(!tiff)
   10.23 -		return 0;
   10.24 +	if ( tiff ) {
   10.25 +		is_TIF = 1;
   10.26  
   10.27 -	/* Free up any dynamically allocated memory libtiff uses */
   10.28 -	TIFFClose(tiff);
   10.29 -	
   10.30 -	return 1;
   10.31 +		/* Free up any dynamically allocated memory libtiff uses */
   10.32 +		TIFFClose(tiff);
   10.33 +	}
   10.34 +	SDL_RWseek(src, start, SEEK_SET);
   10.35 +	return(is_TIF);
   10.36  }
   10.37  
   10.38  SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
    11.1 --- a/IMG_xcf.c	Sat Feb 04 04:53:36 2006 +0000
    11.2 +++ b/IMG_xcf.c	Sat Feb 04 20:37:17 2006 +0000
    11.3 @@ -209,18 +209,21 @@
    11.4  
    11.5  
    11.6  /* See if an image is contained in a data source */
    11.7 -int IMG_isXCF(SDL_RWops *src) {
    11.8 -  int is_XCF;
    11.9 -  char magic[14];
   11.10 +int IMG_isXCF(SDL_RWops *src)
   11.11 +{
   11.12 +	int start;
   11.13 +	int is_XCF;
   11.14 +	char magic[14];
   11.15  
   11.16 -  is_XCF = 0;
   11.17 -  if ( SDL_RWread(src, magic, 14, 1) ) {
   11.18 -    if (strncmp(magic, "gimp xcf ", 9) == 0) {
   11.19 -      is_XCF = 1;
   11.20 -    }
   11.21 -  }
   11.22 -
   11.23 -  return(is_XCF);
   11.24 +	start = SDL_RWtell(src);
   11.25 +	is_XCF = 0;
   11.26 +	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
   11.27 +		if (strncmp(magic, "gimp xcf ", 9) == 0) {
   11.28 +			is_XCF = 1;
   11.29 +		}
   11.30 +	}
   11.31 +	SDL_RWseek(src, start, SEEK_SET);
   11.32 +	return(is_XCF);
   11.33  }
   11.34  
   11.35  static char * read_string (SDL_RWops * src) {
    12.1 --- a/IMG_xpm.c	Sat Feb 04 04:53:36 2006 +0000
    12.2 +++ b/IMG_xpm.c	Sat Feb 04 20:37:17 2006 +0000
    12.3 @@ -57,10 +57,19 @@
    12.4  /* See if an image is contained in a data source */
    12.5  int IMG_isXPM(SDL_RWops *src)
    12.6  {
    12.7 +	int start;
    12.8 +	int is_XPM;
    12.9  	char magic[9];
   12.10  
   12.11 -	return (SDL_RWread(src, magic, sizeof(magic), 1)
   12.12 -		&& memcmp(magic, "/* XPM */", 9) == 0);
   12.13 +	start = SDL_RWtell(src);
   12.14 +	is_XPM = 0;
   12.15 +	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
   12.16 +		if ( memcmp(magic, "/* XPM */", sizeof(magic)) == 0 ) {
   12.17 +			is_XPM = 1;
   12.18 +		}
   12.19 +	}
   12.20 +	SDL_RWseek(src, start, SEEK_SET);
   12.21 +	return(is_XPM);
   12.22  }
   12.23  
   12.24  /* Hash table to look up colors from pixel strings */
    13.1 --- a/IMG_xxx.c	Sat Feb 04 04:53:36 2006 +0000
    13.2 +++ b/IMG_xxx.c	Sat Feb 04 20:37:17 2006 +0000
    13.3 @@ -33,10 +33,12 @@
    13.4  /* See if an image is contained in a data source */
    13.5  int IMG_isXXX(SDL_RWops *src)
    13.6  {
    13.7 +	int start;
    13.8  	int is_XXX;
    13.9  
   13.10 +	start = SDL_RWtell(src);
   13.11  	is_XXX = 0;
   13.12 -
   13.13 +	SDL_RWseek(src, start, SEEK_SET);
   13.14  	return(is_XXX);
   13.15  }
   13.16