Skip to content

Commit

Permalink
Fixed image type functions so they seek back to where they started
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 4, 2006
1 parent 9802897 commit b2ed3ce
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 60 deletions.
5 changes: 1 addition & 4 deletions IMG.c
Expand Up @@ -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.. */
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion IMG_bmp.c
Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion IMG_gif.c
Expand Up @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions IMG_jpg.c
Expand Up @@ -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) ) {
Expand All @@ -61,6 +63,7 @@ int IMG_isJPG(SDL_RWops *src)
}
}
}
SDL_RWseek(src, start, SEEK_SET);
return(is_JPG);
}

Expand Down
5 changes: 4 additions & 1 deletion IMG_lbm.c
Expand Up @@ -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 ) ||
Expand All @@ -76,6 +78,7 @@ int IMG_isLBM( SDL_RWops *src )
is_LBM = 1;
}
}
SDL_RWseek(src, start, SEEK_SET);
return( is_LBM );
}

Expand Down
3 changes: 3 additions & 0 deletions IMG_pcx.c
Expand Up @@ -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) &&
Expand All @@ -78,6 +80,7 @@ int IMG_isPCX(SDL_RWops *src)
is_PCX = 1;
}
}
SDL_RWseek(src, start, SEEK_SET);
return(is_PCX);
}

Expand Down
19 changes: 11 additions & 8 deletions IMG_png.c
Expand Up @@ -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 */
Expand Down
31 changes: 20 additions & 11 deletions IMG_pnm.c
Expand Up @@ -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 */
Expand Down
25 changes: 12 additions & 13 deletions IMG_tga.c
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -143,17 +140,15 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
break;

default:
unsupported();
return NULL;
goto unsupported;
}

bpp = (hdr.pixel_bits + 7) >> 3;
rmask = gmask = bmask = amask = 0;
switch(hdr.pixel_bits) {
case 8:
if(!indexed) {
unsupported();
return NULL;
goto unsupported;
}
break;

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions IMG_tif.c
Expand Up @@ -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);

Expand All @@ -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)
Expand Down
27 changes: 15 additions & 12 deletions IMG_xcf.c
Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions IMG_xpm.c
Expand Up @@ -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 */
Expand Down
4 changes: 3 additions & 1 deletion IMG_xxx.c
Expand Up @@ -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);
}

Expand Down

0 comments on commit b2ed3ce

Please sign in to comment.