IMG_tif.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 13 Feb 2007 10:09:17 +0000
changeset 154 201cc5c1b373
parent 146 aaa5876f41ab
child 167 338c85983115
permissions -rw-r--r--
SDL_image shouldn't dereference a rwops if it's NULL.

Fixes Bugzilla #284.
     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 	return 0;
   114 }
   115 void IMG_QuitTIF()
   116 {
   117 	if ( lib.loaded == 0 ) {
   118 		return;
   119 	}
   120 	if ( lib.loaded == 1 ) {
   121 	}
   122 	--lib.loaded;
   123 }
   124 #endif /* LOAD_TIF_DYNAMIC */
   125 
   126 /*
   127  * These are the thunking routine to use the SDL_RWops* routines from
   128  * libtiff's internals.
   129 */
   130 
   131 static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
   132 {
   133 	return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
   134 }
   135 
   136 static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
   137 {
   138 	return SDL_RWseek((SDL_RWops*)fd, offset, origin);
   139 }
   140 
   141 static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
   142 {
   143 	return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
   144 }
   145 
   146 static int tiff_close(thandle_t fd)
   147 {
   148 	/*
   149 	 * We don't want libtiff closing our SDL_RWops*, but if it's not given
   150          * a routine to try, and if the image isn't a TIFF, it'll segfault.
   151 	 */
   152 	return 0;
   153 }
   154 
   155 static toff_t tiff_size(thandle_t fd)
   156 {
   157 	Uint32 save_pos;
   158 	toff_t size;
   159 
   160 	save_pos = SDL_RWtell((SDL_RWops*)fd);
   161 	SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
   162         size = SDL_RWtell((SDL_RWops*)fd);
   163 	SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
   164 	return size;
   165 }
   166 
   167 int IMG_isTIF(SDL_RWops* src)
   168 {
   169 	int start;
   170 	int is_TIF;
   171 	TIFF* tiff;
   172 	TIFFErrorHandler prev_handler;
   173 
   174 	if ( IMG_InitTIF() < 0 ) {
   175 		return 0;
   176 	}
   177 	if ( !src )
   178 		return 0;
   179 	start = SDL_RWtell(src);
   180 	is_TIF = 0;
   181 
   182 	/* Suppress output from libtiff */
   183 	prev_handler = lib.TIFFSetErrorHandler(NULL);
   184 	
   185 	/* Attempt to process the given file data */
   186 	/* turn off memory mapped access with the m flag */
   187 	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   188 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   189 
   190 	/* Reset the default error handler, since it can be useful for info */
   191 	lib.TIFFSetErrorHandler(prev_handler);
   192 
   193 	/* If it's not a TIFF, then tiff will be NULL. */
   194 	if ( tiff ) {
   195 		is_TIF = 1;
   196 
   197 		/* Free up any dynamically allocated memory libtiff uses */
   198 		lib.TIFFClose(tiff);
   199 	}
   200 	SDL_RWseek(src, start, SEEK_SET);
   201 	IMG_QuitTIF();
   202 	return(is_TIF);
   203 }
   204 
   205 SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
   206 {
   207 	int start;
   208 	TIFF* tiff;
   209 	SDL_Surface* surface = NULL;
   210 	Uint32 img_width, img_height;
   211 	Uint32 Rmask, Gmask, Bmask, Amask;
   212 	Uint32 x, y;
   213 	Uint32 half;
   214 
   215 	if ( !src ) {
   216 		/* The error message has been set in SDL_RWFromFile */
   217 		return NULL;
   218 	}
   219 	start = SDL_RWtell(src);
   220 
   221 	if ( IMG_InitTIF() < 0 ) {
   222 		return NULL;
   223 	}
   224 
   225 	/* turn off memory mapped access with the m flag */
   226 	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   227 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   228 	if(!tiff)
   229 		goto error;
   230 
   231 	/* Retrieve the dimensions of the image from the TIFF tags */
   232 	lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   233 	lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   234 
   235 	Rmask = 0x000000FF;
   236 	Gmask = 0x0000FF00;
   237 	Bmask = 0x00FF0000;
   238 	Amask = 0xFF000000;
   239 	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
   240 		Rmask, Gmask, Bmask, Amask);
   241 	if(!surface)
   242 		goto error;
   243 	
   244 	if(!lib.TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   245 		goto error;
   246 
   247 	/* libtiff loads the image upside-down, flip it back */
   248 	half = img_height / 2;
   249 	for(y = 0; y < half; y++)
   250 	{
   251 	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
   252 	        Uint32 *bot = (Uint32 *)surface->pixels
   253 		              + (img_height - y - 1) * surface->pitch/4;
   254 		for(x = 0; x < img_width; x++)
   255 		{
   256 		        Uint32 tmp = top[x];
   257 			top[x] = bot[x];
   258 			bot[x] = tmp;
   259 		}
   260 	}
   261 	lib.TIFFClose(tiff);
   262 	IMG_QuitTIF();
   263 	
   264 	return surface;
   265 
   266 error:
   267 	SDL_RWseek(src, start, SEEK_SET);
   268 	if ( surface ) {
   269 		SDL_FreeSurface(surface);
   270 	}
   271 	IMG_QuitTIF();
   272 	return NULL;
   273 }
   274 
   275 #else
   276 
   277 /* See if an image is contained in a data source */
   278 int IMG_isTIF(SDL_RWops *src)
   279 {
   280 	return(0);
   281 }
   282 
   283 /* Load a TIFF type image from an SDL datasource */
   284 SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
   285 {
   286 	return(NULL);
   287 }
   288 
   289 #endif /* LOAD_TIF */