From b2ed3ceaacfe5aeeb7383f1f7a7bef8467b5aede Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 4 Feb 2006 20:37:17 +0000 Subject: [PATCH] Fixed image type functions so they seek back to where they started --- IMG.c | 5 +---- IMG_bmp.c | 5 ++++- IMG_gif.c | 5 ++++- IMG_jpg.c | 3 +++ IMG_lbm.c | 5 ++++- IMG_pcx.c | 3 +++ IMG_png.c | 19 +++++++++++-------- IMG_pnm.c | 31 ++++++++++++++++++++----------- IMG_tga.c | 25 ++++++++++++------------- IMG_tif.c | 18 ++++++++++++------ IMG_xcf.c | 27 +++++++++++++++------------ IMG_xpm.c | 13 +++++++++++-- IMG_xxx.c | 4 +++- 13 files changed, 103 insertions(+), 60 deletions(-) diff --git a/IMG.c b/IMG.c index 26ce6821..ea1797f8 100644 --- a/IMG.c +++ b/IMG.c @@ -96,7 +96,7 @@ static int IMG_string_equals(const char *str1, const char *str2) /* Load an image from an SDL datasource, optionally specifying the type */ SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type) { - int i, start; + int i; SDL_Surface *image; /* Make sure there is something to do.. */ @@ -114,11 +114,9 @@ SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type) } /* Detect the type of image being loaded */ - start = SDL_RWtell(src); image = NULL; for ( i=0; i < ARRAYSIZE(supported); ++i ) { if(supported[i].is) { - SDL_RWseek(src, start, SEEK_SET); if(!supported[i].is(src)) continue; } else { @@ -131,7 +129,6 @@ SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type) fprintf(stderr, "IMGLIB: Loading image as %s\n", supported[i].type); #endif - SDL_RWseek(src, start, SEEK_SET); image = supported[i].load(src); if(freesrc) SDL_RWclose(src); diff --git a/IMG_bmp.c b/IMG_bmp.c index e8769fd6..e70a532b 100644 --- a/IMG_bmp.c +++ b/IMG_bmp.c @@ -34,15 +34,18 @@ /* See if an image is contained in a data source */ int IMG_isBMP(SDL_RWops *src) { + int start; int is_BMP; char magic[2]; + start = SDL_RWtell(src); is_BMP = 0; - if ( SDL_RWread(src, magic, 2, 1) ) { + if ( SDL_RWread(src, magic, sizeof(magic), 1) ) { if ( strncmp(magic, "BM", 2) == 0 ) { is_BMP = 1; } } + SDL_RWseek(src, start, SEEK_SET); return(is_BMP); } diff --git a/IMG_gif.c b/IMG_gif.c index 68ff112b..5e30b6cf 100644 --- a/IMG_gif.c +++ b/IMG_gif.c @@ -34,17 +34,20 @@ /* See if an image is contained in a data source */ int IMG_isGIF(SDL_RWops *src) { + int start; int is_GIF; char magic[6]; + start = SDL_RWtell(src); is_GIF = 0; - if ( SDL_RWread(src, magic, 6, 1) ) { + if ( SDL_RWread(src, magic, sizeof(magic), 1) ) { if ( (strncmp(magic, "GIF", 3) == 0) && ((memcmp(magic + 3, "87a", 3) == 0) || (memcmp(magic + 3, "89a", 3) == 0)) ) { is_GIF = 1; } } + SDL_RWseek(src, start, SEEK_SET); return(is_GIF); } diff --git a/IMG_jpg.c b/IMG_jpg.c index ff0ceebc..4916ac53 100644 --- a/IMG_jpg.c +++ b/IMG_jpg.c @@ -40,9 +40,11 @@ /* See if an image is contained in a data source */ int IMG_isJPG(SDL_RWops *src) { + int start; int is_JPG; Uint8 magic[4]; + start = SDL_RWtell(src); is_JPG = 0; if ( SDL_RWread(src, magic, 2, 1) ) { if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) { @@ -61,6 +63,7 @@ int IMG_isJPG(SDL_RWops *src) } } } + SDL_RWseek(src, start, SEEK_SET); return(is_JPG); } diff --git a/IMG_lbm.c b/IMG_lbm.c index 4df84074..578f6c7c 100644 --- a/IMG_lbm.c +++ b/IMG_lbm.c @@ -63,11 +63,13 @@ typedef struct int IMG_isLBM( SDL_RWops *src ) { + int start; int is_LBM; Uint8 magic[4+4+4]; + start = SDL_RWtell(src); is_LBM = 0; - if ( SDL_RWread( src, magic, 4+4+4, 1 ) ) + if ( SDL_RWread( src, magic, sizeof(magic), 1 ) ) { if ( !memcmp( magic, "FORM", 4 ) && ( !memcmp( magic + 8, "PBM ", 4 ) || @@ -76,6 +78,7 @@ int IMG_isLBM( SDL_RWops *src ) is_LBM = 1; } } + SDL_RWseek(src, start, SEEK_SET); return( is_LBM ); } diff --git a/IMG_pcx.c b/IMG_pcx.c index 8df1e5b2..52ba3d4c 100644 --- a/IMG_pcx.c +++ b/IMG_pcx.c @@ -64,12 +64,14 @@ struct PCXheader { /* See if an image is contained in a data source */ int IMG_isPCX(SDL_RWops *src) { + int start; int is_PCX; const int ZSoft_Manufacturer = 10; const int PC_Paintbrush_Version = 5; const int PCX_RunLength_Encoding = 1; struct PCXheader pcxh; + start = SDL_RWtell(src); is_PCX = 0; if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) { if ( (pcxh.Manufacturer == ZSoft_Manufacturer) && @@ -78,6 +80,7 @@ int IMG_isPCX(SDL_RWops *src) is_PCX = 1; } } + SDL_RWseek(src, start, SEEK_SET); return(is_PCX); } diff --git a/IMG_png.c b/IMG_png.c index 0aaad032..ed763ceb 100644 --- a/IMG_png.c +++ b/IMG_png.c @@ -76,14 +76,17 @@ /* See if an image is contained in a data source */ int IMG_isPNG(SDL_RWops *src) { - unsigned char buf[PNG_BYTES_TO_CHECK]; - - /* Read in the signature bytes */ - if (SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK) - return 0; - - /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */ - return( !png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); + int start; + int is_PNG; + unsigned char buf[PNG_BYTES_TO_CHECK]; + + start = SDL_RWtell(src); + is_PNG = 0; + if ( SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK ) { + is_PNG = (png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK) == 0); + } + SDL_RWseek(src, start, SEEK_SET); + return(is_PNG); } /* Load a PNG type image from an SDL datasource */ diff --git a/IMG_pnm.c b/IMG_pnm.c index ee3febc4..bcef664c 100644 --- a/IMG_pnm.c +++ b/IMG_pnm.c @@ -42,19 +42,28 @@ /* See if an image is contained in a data source */ int IMG_isPNM(SDL_RWops *src) { + int start; + int is_PNM; char magic[2]; - /* - * PNM magic signatures: - * P1 PBM, ascii format - * P2 PGM, ascii format - * P3 PPM, ascii format - * P4 PBM, binary format - * P5 PGM, binary format - * P6 PPM, binary format - */ - return (SDL_RWread(src, magic, 2, 1) - && magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6'); + start = SDL_RWtell(src); + is_PNM = 0; + if ( SDL_RWread(src, magic, sizeof(magic), 1) ) { + /* + * PNM magic signatures: + * P1 PBM, ascii format + * P2 PGM, ascii format + * P3 PPM, ascii format + * P4 PBM, binary format + * P5 PGM, binary format + * P6 PPM, binary format + */ + if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) { + is_PNM = 1; + } + } + SDL_RWseek(src, start, SEEK_SET); + return(is_PNM); } /* read a non-negative integer from the source. return -1 upon error */ diff --git a/IMG_tga.c b/IMG_tga.c index 97ac78f3..6481bccb 100644 --- a/IMG_tga.c +++ b/IMG_tga.c @@ -83,14 +83,10 @@ enum tga_type { #define LE16(p) ((p)[0] + ((p)[1] << 8)) #define SETLE16(p, v) ((p)[0] = (v), (p)[1] = (v) >> 8) -static void unsupported(void) -{ - IMG_SetError("unsupported TGA format"); -} - /* Load a TGA type image from an SDL datasource */ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src) { + int start; struct TGAheader hdr; int rle = 0; int alpha = 0; @@ -112,6 +108,7 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src) return NULL; } + start = SDL_RWtell(src); if(!SDL_RWread(src, &hdr, sizeof(hdr), 1)) goto error; ncols = LE16(hdr.cmap_len); @@ -143,8 +140,7 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src) break; default: - unsupported(); - return NULL; + goto unsupported; } bpp = (hdr.pixel_bits + 7) >> 3; @@ -152,8 +148,7 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src) switch(hdr.pixel_bits) { case 8: if(!indexed) { - unsupported(); - return NULL; + goto unsupported; } break; @@ -185,14 +180,12 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src) break; default: - unsupported(); - return NULL; + goto unsupported; } if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE || hdr.flags & TGA_ORIGIN_RIGHT) { - unsupported(); - return NULL; + goto unsupported; } SDL_RWseek(src, hdr.infolen, SEEK_CUR); /* skip info field */ @@ -311,8 +304,14 @@ 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: + SDL_RWseek(src, start, SEEK_SET); + IMG_SetError("unsupported TGA format"); + return NULL; } #else diff --git a/IMG_tif.c b/IMG_tif.c index 2b28343a..a4f58d79 100644 --- a/IMG_tif.c +++ b/IMG_tif.c @@ -77,9 +77,14 @@ static toff_t tiff_size(thandle_t fd) int IMG_isTIF(SDL_RWops* src) { + int start; + int is_TIF; TIFF* tiff; TIFFErrorHandler prev_handler; + start = SDL_RWtell(src); + is_TIF = 0; + /* Suppress output from libtiff */ prev_handler = TIFFSetErrorHandler(NULL); @@ -92,13 +97,14 @@ int IMG_isTIF(SDL_RWops* src) TIFFSetErrorHandler(prev_handler); /* If it's not a TIFF, then tiff will be NULL. */ - if(!tiff) - return 0; + if ( tiff ) { + is_TIF = 1; - /* Free up any dynamically allocated memory libtiff uses */ - TIFFClose(tiff); - - return 1; + /* Free up any dynamically allocated memory libtiff uses */ + TIFFClose(tiff); + } + SDL_RWseek(src, start, SEEK_SET); + return(is_TIF); } SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src) diff --git a/IMG_xcf.c b/IMG_xcf.c index 135ef4e7..73433bc9 100644 --- a/IMG_xcf.c +++ b/IMG_xcf.c @@ -209,18 +209,21 @@ typedef unsigned char * (* load_tile_type) (SDL_RWops *, Uint32, int, int, int); /* See if an image is contained in a data source */ -int IMG_isXCF(SDL_RWops *src) { - int is_XCF; - char magic[14]; - - is_XCF = 0; - if ( SDL_RWread(src, magic, 14, 1) ) { - if (strncmp(magic, "gimp xcf ", 9) == 0) { - is_XCF = 1; - } - } - - return(is_XCF); +int IMG_isXCF(SDL_RWops *src) +{ + int start; + int is_XCF; + char magic[14]; + + start = SDL_RWtell(src); + is_XCF = 0; + if ( SDL_RWread(src, magic, sizeof(magic), 1) ) { + if (strncmp(magic, "gimp xcf ", 9) == 0) { + is_XCF = 1; + } + } + SDL_RWseek(src, start, SEEK_SET); + return(is_XCF); } static char * read_string (SDL_RWops * src) { diff --git a/IMG_xpm.c b/IMG_xpm.c index c2311f81..79a3c8f0 100644 --- a/IMG_xpm.c +++ b/IMG_xpm.c @@ -57,10 +57,19 @@ /* See if an image is contained in a data source */ int IMG_isXPM(SDL_RWops *src) { + int start; + int is_XPM; char magic[9]; - return (SDL_RWread(src, magic, sizeof(magic), 1) - && memcmp(magic, "/* XPM */", 9) == 0); + start = SDL_RWtell(src); + is_XPM = 0; + if ( SDL_RWread(src, magic, sizeof(magic), 1) ) { + if ( memcmp(magic, "/* XPM */", sizeof(magic)) == 0 ) { + is_XPM = 1; + } + } + SDL_RWseek(src, start, SEEK_SET); + return(is_XPM); } /* Hash table to look up colors from pixel strings */ diff --git a/IMG_xxx.c b/IMG_xxx.c index 10de62ff..590af52b 100644 --- a/IMG_xxx.c +++ b/IMG_xxx.c @@ -33,10 +33,12 @@ /* See if an image is contained in a data source */ int IMG_isXXX(SDL_RWops *src) { + int start; int is_XXX; + start = SDL_RWtell(src); is_XXX = 0; - + SDL_RWseek(src, start, SEEK_SET); return(is_XXX); }