IMG_tif.c
changeset 143 e01fee7bdf3b
parent 121 1bf9c0c87374
child 145 6d65a8d8b1d1
     1.1 --- a/IMG_tif.c	Fri May 12 02:58:31 2006 +0000
     1.2 +++ b/IMG_tif.c	Fri May 12 05:08:03 2006 +0000
     1.3 @@ -30,6 +30,97 @@
     1.4  
     1.5  #include <tiffio.h>
     1.6  
     1.7 +static struct {
     1.8 +	int loaded;
     1.9 +	void *handle;
    1.10 +	TIFF* (*TIFFClientOpen)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc);
    1.11 +	void (*TIFFClose)(TIFF*);
    1.12 +	int (*TIFFGetField)(TIFF*, ttag_t, ...);
    1.13 +	int (*TIFFReadRGBAImage)(TIFF*, uint32, uint32, uint32*, int);
    1.14 +	TIFFErrorHandler (*TIFFSetErrorHandler)(TIFFErrorHandler);
    1.15 +} lib;
    1.16 +
    1.17 +#ifdef LOAD_TIF_DYNAMIC
    1.18 +int IMG_InitTIF()
    1.19 +{
    1.20 +	if ( lib.loaded == 0 ) {
    1.21 +		lib.handle = SDL_LoadObject(LOAD_TIF_DYNAMIC);
    1.22 +		if ( lib.handle == NULL ) {
    1.23 +			return -1;
    1.24 +		}
    1.25 +		lib.TIFFClientOpen =
    1.26 +			(TIFF* (*)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc))
    1.27 +			SDL_LoadFunction(lib.handle, "TIFFClientOpen");
    1.28 +		if ( lib.TIFFClientOpen == NULL ) {
    1.29 +			SDL_UnloadObject(lib.handle);
    1.30 +			return -1;
    1.31 +		}
    1.32 +		lib.TIFFClose =
    1.33 +			(void (*)(TIFF*))
    1.34 +			SDL_LoadFunction(lib.handle, "TIFFClose");
    1.35 +		if ( lib.TIFFClose == NULL ) {
    1.36 +			SDL_UnloadObject(lib.handle);
    1.37 +			return -1;
    1.38 +		}
    1.39 +		lib.TIFFGetField =
    1.40 +			(int (*)(TIFF*, ttag_t, ...))
    1.41 +			SDL_LoadFunction(lib.handle, "TIFFGetField");
    1.42 +		if ( lib.TIFFGetField == NULL ) {
    1.43 +			SDL_UnloadObject(lib.handle);
    1.44 +			return -1;
    1.45 +		}
    1.46 +		lib.TIFFReadRGBAImage =
    1.47 +			(int (*)(TIFF*, uint32, uint32, uint32*, int))
    1.48 +			SDL_LoadFunction(lib.handle, "TIFFReadRGBAImage");
    1.49 +		if ( lib.TIFFReadRGBAImage == NULL ) {
    1.50 +			SDL_UnloadObject(lib.handle);
    1.51 +			return -1;
    1.52 +		}
    1.53 +		lib.TIFFSetErrorHandler =
    1.54 +			(TIFFErrorHandler (*)(TIFFErrorHandler))
    1.55 +			SDL_LoadFunction(lib.handle, "TIFFSetErrorHandler");
    1.56 +		if ( lib.TIFFSetErrorHandler == NULL ) {
    1.57 +			SDL_UnloadObject(lib.handle);
    1.58 +			return -1;
    1.59 +		}
    1.60 +	}
    1.61 +	++lib.loaded;
    1.62 +
    1.63 +	return 0;
    1.64 +}
    1.65 +void IMG_QuitTIF()
    1.66 +{
    1.67 +	if ( lib.loaded == 0 ) {
    1.68 +		return;
    1.69 +	}
    1.70 +	if ( lib.loaded == 1 ) {
    1.71 +		SDL_UnloadObject(lib.handle);
    1.72 +	}
    1.73 +	--lib.loaded;
    1.74 +}
    1.75 +#else
    1.76 +int IMG_InitTIF()
    1.77 +{
    1.78 +	if ( lib.loaded == 0 ) {
    1.79 +		lib.TIFFClientOpen = TIFFClientOpen;
    1.80 +		lib.TIFFClose = TIFFClose;
    1.81 +		lib.TIFFGetField = TIFFGetField;
    1.82 +		lib.TIFFReadRGBAImage = TIFFReadRGBAImage;
    1.83 +		lib.TIFFSetErrorHandler = TIFFSetErrorHandler;
    1.84 +	}
    1.85 +	++lib.loaded;
    1.86 +}
    1.87 +void IMG_QuitTIF()
    1.88 +{
    1.89 +	if ( lib.loaded == 0 ) {
    1.90 +		return;
    1.91 +	}
    1.92 +	if ( lib.loaded == 1 ) {
    1.93 +	}
    1.94 +	--lib.loaded;
    1.95 +}
    1.96 +#endif /* LOAD_TIF_DYNAMIC */
    1.97 +
    1.98  /*
    1.99   * These are the thunking routine to use the SDL_RWops* routines from
   1.100   * libtiff's internals.
   1.101 @@ -78,28 +169,32 @@
   1.102  	TIFF* tiff;
   1.103  	TIFFErrorHandler prev_handler;
   1.104  
   1.105 +	if ( IMG_InitTIF() < 0 ) {
   1.106 +		return 0;
   1.107 +	}
   1.108  	start = SDL_RWtell(src);
   1.109  	is_TIF = 0;
   1.110  
   1.111  	/* Suppress output from libtiff */
   1.112 -	prev_handler = TIFFSetErrorHandler(NULL);
   1.113 +	prev_handler = lib.TIFFSetErrorHandler(NULL);
   1.114  	
   1.115  	/* Attempt to process the given file data */
   1.116  	/* turn off memory mapped access with the m flag */
   1.117 -	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   1.118 +	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   1.119  		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   1.120  
   1.121  	/* Reset the default error handler, since it can be useful for info */
   1.122 -	TIFFSetErrorHandler(prev_handler);
   1.123 +	lib.TIFFSetErrorHandler(prev_handler);
   1.124  
   1.125  	/* If it's not a TIFF, then tiff will be NULL. */
   1.126  	if ( tiff ) {
   1.127  		is_TIF = 1;
   1.128  
   1.129  		/* Free up any dynamically allocated memory libtiff uses */
   1.130 -		TIFFClose(tiff);
   1.131 +		lib.TIFFClose(tiff);
   1.132  	}
   1.133  	SDL_RWseek(src, start, SEEK_SET);
   1.134 +	IMG_QuitTIF();
   1.135  	return(is_TIF);
   1.136  }
   1.137  
   1.138 @@ -119,15 +214,19 @@
   1.139  	}
   1.140  	start = SDL_RWtell(src);
   1.141  
   1.142 +	if ( IMG_InitTIF() < 0 ) {
   1.143 +		return NULL;
   1.144 +	}
   1.145 +
   1.146  	/* turn off memory mapped access with the m flag */
   1.147 -	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   1.148 +	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   1.149  		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   1.150  	if(!tiff)
   1.151  		goto error;
   1.152  
   1.153  	/* Retrieve the dimensions of the image from the TIFF tags */
   1.154 -	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   1.155 -	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   1.156 +	lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   1.157 +	lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   1.158  
   1.159  	Rmask = 0x000000FF;
   1.160  	Gmask = 0x0000FF00;
   1.161 @@ -138,7 +237,7 @@
   1.162  	if(!surface)
   1.163  		goto error;
   1.164  	
   1.165 -	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   1.166 +	if(!lib.TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   1.167  		goto error;
   1.168  
   1.169  	/* libtiff loads the image upside-down, flip it back */
   1.170 @@ -155,7 +254,8 @@
   1.171  			bot[x] = tmp;
   1.172  		}
   1.173  	}
   1.174 -	TIFFClose(tiff);
   1.175 +	lib.TIFFClose(tiff);
   1.176 +	IMG_QuitTIF();
   1.177  	
   1.178  	return surface;
   1.179  
   1.180 @@ -164,6 +264,7 @@
   1.181  	if ( surface ) {
   1.182  		SDL_FreeSurface(surface);
   1.183  	}
   1.184 +	IMG_QuitTIF();
   1.185  	return NULL;
   1.186  }
   1.187