Skip to content

Commit

Permalink
* PNG and TIFF images are correctly identified even if dynamic libra…
Browse files Browse the repository at this point in the history
…ries

   to load them aren't available.
 * Fixed loading of TIFF images using libtiff 3.6
  • Loading branch information
slouken committed Jul 18, 2007
1 parent 0592c6c commit 901203e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
13 changes: 6 additions & 7 deletions IMG_png.c
Expand Up @@ -69,7 +69,6 @@
#endif
#include <png.h>

#define PNG_BYTES_TO_CHECK 4

static struct {
int loaded;
Expand Down Expand Up @@ -268,17 +267,17 @@ int IMG_isPNG(SDL_RWops *src)
{
int start;
int is_PNG;
unsigned char buf[PNG_BYTES_TO_CHECK];
Uint8 magic[4];

if ( !src )
return 0;
start = SDL_RWtell(src);
is_PNG = 0;
if ( SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK ) {
if ( buf[0] == 0x89 &&
buf[1] == 'P' &&
buf[2] == 'N' &&
buf[3] == 'G' ) {
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G' ) {
is_PNG = 1;
}
}
Expand Down
48 changes: 23 additions & 25 deletions IMG_tif.c
Expand Up @@ -152,6 +152,16 @@ static int tiff_close(thandle_t fd)
return 0;
}

static int tiff_map(thandle_t fd, tdata_t* pbase, toff_t* psize)
{
return (0);
}

static void tiff_unmap(thandle_t fd, tdata_t base, toff_t size)
{
return;
}

static toff_t tiff_size(thandle_t fd)
{
Uint32 save_pos;
Expand All @@ -168,37 +178,25 @@ int IMG_isTIF(SDL_RWops* src)
{
int start;
int is_TIF;
TIFF* tiff;
TIFFErrorHandler prev_handler;
Uint8 magic[4];

if ( IMG_InitTIF() < 0 ) {
return 0;
}
if ( !src )
return 0;
start = SDL_RWtell(src);
is_TIF = 0;

/* Suppress output from libtiff */
prev_handler = lib.TIFFSetErrorHandler(NULL);

/* Attempt to process the given file data */
/* turn off memory mapped access with the m flag */
tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);

/* Reset the default error handler, since it can be useful for info */
lib.TIFFSetErrorHandler(prev_handler);

/* If it's not a TIFF, then tiff will be NULL. */
if ( tiff ) {
is_TIF = 1;

/* Free up any dynamically allocated memory libtiff uses */
lib.TIFFClose(tiff);
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( (magic[0] == 'I' &&
magic[1] == 'I' &&
magic[2] == 0x2a &&
magic[3] == 0x00) ||
(magic[0] == 'M' &&
magic[1] == 'M' &&
magic[2] == 0x00 &&
magic[3] == 0x2a) ) {
is_TIF = 1;
}
}
SDL_RWseek(src, start, SEEK_SET);
IMG_QuitTIF();
return(is_TIF);
}

Expand All @@ -224,7 +222,7 @@ SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)

/* turn off memory mapped access with the m flag */
tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, tiff_map, tiff_unmap);
if(!tiff)
goto error;

Expand Down

0 comments on commit 901203e

Please sign in to comment.