IMG_tif.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 12 May 2006 19:06:09 +0000
changeset 145 6d65a8d8b1d1
parent 143 e01fee7bdf3b
child 146 aaa5876f41ab
permissions -rw-r--r--
Updated Visual C++ project with dynamic image library loading
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* This is a TIFF image file loading framework */
    24 
    25 #include <stdio.h>
    26 
    27 #include "SDL_image.h"
    28 
    29 #ifdef LOAD_TIF
    30 
    31 #include <tiffio.h>
    32 
    33 static struct {
    34 	int loaded;
    35 	void *handle;
    36 	TIFF* (*TIFFClientOpen)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc);
    37 	void (*TIFFClose)(TIFF*);
    38 	int (*TIFFGetField)(TIFF*, ttag_t, ...);
    39 	int (*TIFFReadRGBAImage)(TIFF*, uint32, uint32, uint32*, int);
    40 	TIFFErrorHandler (*TIFFSetErrorHandler)(TIFFErrorHandler);
    41 } lib;
    42 
    43 #ifdef LOAD_TIF_DYNAMIC
    44 int IMG_InitTIF()
    45 {
    46 	if ( lib.loaded == 0 ) {
    47 		lib.handle = SDL_LoadObject(LOAD_TIF_DYNAMIC);
    48 		if ( lib.handle == NULL ) {
    49 			return -1;
    50 		}
    51 		lib.TIFFClientOpen =
    52 			(TIFF* (*)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc))
    53 			SDL_LoadFunction(lib.handle, "TIFFClientOpen");
    54 		if ( lib.TIFFClientOpen == NULL ) {
    55 			SDL_UnloadObject(lib.handle);
    56 			return -1;
    57 		}
    58 		lib.TIFFClose =
    59 			(void (*)(TIFF*))
    60 			SDL_LoadFunction(lib.handle, "TIFFClose");
    61 		if ( lib.TIFFClose == NULL ) {
    62 			SDL_UnloadObject(lib.handle);
    63 			return -1;
    64 		}
    65 		lib.TIFFGetField =
    66 			(int (*)(TIFF*, ttag_t, ...))
    67 			SDL_LoadFunction(lib.handle, "TIFFGetField");
    68 		if ( lib.TIFFGetField == NULL ) {
    69 			SDL_UnloadObject(lib.handle);
    70 			return -1;
    71 		}
    72 		lib.TIFFReadRGBAImage =
    73 			(int (*)(TIFF*, uint32, uint32, uint32*, int))
    74 			SDL_LoadFunction(lib.handle, "TIFFReadRGBAImage");
    75 		if ( lib.TIFFReadRGBAImage == NULL ) {
    76 			SDL_UnloadObject(lib.handle);
    77 			return -1;
    78 		}
    79 		lib.TIFFSetErrorHandler =
    80 			(TIFFErrorHandler (*)(TIFFErrorHandler))
    81 			SDL_LoadFunction(lib.handle, "TIFFSetErrorHandler");
    82 		if ( lib.TIFFSetErrorHandler == NULL ) {
    83 			SDL_UnloadObject(lib.handle);
    84 			return -1;
    85 		}
    86 	}
    87 	++lib.loaded;
    88 
    89 	return 0;
    90 }
    91 void IMG_QuitTIF()
    92 {
    93 	if ( lib.loaded == 0 ) {
    94 		return;
    95 	}
    96 	if ( lib.loaded == 1 ) {
    97 		SDL_UnloadObject(lib.handle);
    98 	}
    99 	--lib.loaded;
   100 }
   101 #else
   102 int IMG_InitTIF()
   103 {
   104 	if ( lib.loaded == 0 ) {
   105 		lib.TIFFClientOpen = TIFFClientOpen;
   106 		lib.TIFFClose = TIFFClose;
   107 		lib.TIFFGetField = TIFFGetField;
   108 		lib.TIFFReadRGBAImage = TIFFReadRGBAImage;
   109 		lib.TIFFSetErrorHandler = TIFFSetErrorHandler;
   110 	}
   111 	++lib.loaded;
   112 }
   113 void IMG_QuitTIF()
   114 {
   115 	if ( lib.loaded == 0 ) {
   116 		return;
   117 	}
   118 	if ( lib.loaded == 1 ) {
   119 	}
   120 	--lib.loaded;
   121 }
   122 #endif /* LOAD_TIF_DYNAMIC */
   123 
   124 /*
   125  * These are the thunking routine to use the SDL_RWops* routines from
   126  * libtiff's internals.
   127 */
   128 
   129 static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
   130 {
   131 	return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
   132 }
   133 
   134 static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
   135 {
   136 	return SDL_RWseek((SDL_RWops*)fd, offset, origin);
   137 }
   138 
   139 static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
   140 {
   141 	return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
   142 }
   143 
   144 static int tiff_close(thandle_t fd)
   145 {
   146 	/*
   147 	 * We don't want libtiff closing our SDL_RWops*, but if it's not given
   148          * a routine to try, and if the image isn't a TIFF, it'll segfault.
   149 	 */
   150 	return 0;
   151 }
   152 
   153 static toff_t tiff_size(thandle_t fd)
   154 {
   155 	Uint32 save_pos;
   156 	toff_t size;
   157 
   158 	save_pos = SDL_RWtell((SDL_RWops*)fd);
   159 	SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
   160         size = SDL_RWtell((SDL_RWops*)fd);
   161 	SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
   162 	return size;
   163 }
   164 
   165 int IMG_isTIF(SDL_RWops* src)
   166 {
   167 	int start;
   168 	int is_TIF;
   169 	TIFF* tiff;
   170 	TIFFErrorHandler prev_handler;
   171 
   172 	if ( IMG_InitTIF() < 0 ) {
   173 		return 0;
   174 	}
   175 	start = SDL_RWtell(src);
   176 	is_TIF = 0;
   177 
   178 	/* Suppress output from libtiff */
   179 	prev_handler = lib.TIFFSetErrorHandler(NULL);
   180 	
   181 	/* Attempt to process the given file data */
   182 	/* turn off memory mapped access with the m flag */
   183 	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   184 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   185 
   186 	/* Reset the default error handler, since it can be useful for info */
   187 	lib.TIFFSetErrorHandler(prev_handler);
   188 
   189 	/* If it's not a TIFF, then tiff will be NULL. */
   190 	if ( tiff ) {
   191 		is_TIF = 1;
   192 
   193 		/* Free up any dynamically allocated memory libtiff uses */
   194 		lib.TIFFClose(tiff);
   195 	}
   196 	SDL_RWseek(src, start, SEEK_SET);
   197 	IMG_QuitTIF();
   198 	return(is_TIF);
   199 }
   200 
   201 SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
   202 {
   203 	int start;
   204 	TIFF* tiff;
   205 	SDL_Surface* surface = NULL;
   206 	Uint32 img_width, img_height;
   207 	Uint32 Rmask, Gmask, Bmask, Amask;
   208 	Uint32 x, y;
   209 	Uint32 half;
   210 
   211 	if ( !src ) {
   212 		/* The error message has been set in SDL_RWFromFile */
   213 		return NULL;
   214 	}
   215 	start = SDL_RWtell(src);
   216 
   217 	if ( IMG_InitTIF() < 0 ) {
   218 		return NULL;
   219 	}
   220 
   221 	/* turn off memory mapped access with the m flag */
   222 	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   223 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   224 	if(!tiff)
   225 		goto error;
   226 
   227 	/* Retrieve the dimensions of the image from the TIFF tags */
   228 	lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   229 	lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   230 
   231 	Rmask = 0x000000FF;
   232 	Gmask = 0x0000FF00;
   233 	Bmask = 0x00FF0000;
   234 	Amask = 0xFF000000;
   235 	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
   236 		Rmask, Gmask, Bmask, Amask);
   237 	if(!surface)
   238 		goto error;
   239 	
   240 	if(!lib.TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   241 		goto error;
   242 
   243 	/* libtiff loads the image upside-down, flip it back */
   244 	half = img_height / 2;
   245 	for(y = 0; y < half; y++)
   246 	{
   247 	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
   248 	        Uint32 *bot = (Uint32 *)surface->pixels
   249 		              + (img_height - y - 1) * surface->pitch/4;
   250 		for(x = 0; x < img_width; x++)
   251 		{
   252 		        Uint32 tmp = top[x];
   253 			top[x] = bot[x];
   254 			bot[x] = tmp;
   255 		}
   256 	}
   257 	lib.TIFFClose(tiff);
   258 	IMG_QuitTIF();
   259 	
   260 	return surface;
   261 
   262 error:
   263 	SDL_RWseek(src, start, SEEK_SET);
   264 	if ( surface ) {
   265 		SDL_FreeSurface(surface);
   266 	}
   267 	IMG_QuitTIF();
   268 	return NULL;
   269 }
   270 
   271 #else
   272 
   273 /* See if an image is contained in a data source */
   274 int IMG_isTIF(SDL_RWops *src)
   275 {
   276 	return(0);
   277 }
   278 
   279 /* Load a TIFF type image from an SDL datasource */
   280 SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
   281 {
   282 	return(NULL);
   283 }
   284 
   285 #endif /* LOAD_TIF */