IMG_tif.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 22:26:04 +0000
changeset 118 c5e736a47ad2
parent 117 e613cf987897
child 121 1bf9c0c87374
permissions -rw-r--r--
If there's an error loading an image, seek back to the start of the image.
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 
    26 
    27 /* This is a TIFF image file loading framework */
    28 
    29 #include <stdio.h>
    30 
    31 #include "SDL_image.h"
    32 
    33 #ifdef LOAD_TIF
    34 
    35 #include <tiffio.h>
    36 
    37 /*
    38  * These are the thunking routine to use the SDL_RWops* routines from
    39  * libtiff's internals.
    40 */
    41 
    42 static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
    43 {
    44 	return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
    45 }
    46 
    47 static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
    48 {
    49 	return SDL_RWseek((SDL_RWops*)fd, offset, origin);
    50 }
    51 
    52 static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
    53 {
    54 	return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
    55 }
    56 
    57 static int tiff_close(thandle_t fd)
    58 {
    59 	/*
    60 	 * We don't want libtiff closing our SDL_RWops*, but if it's not given
    61          * a routine to try, and if the image isn't a TIFF, it'll segfault.
    62 	 */
    63 	return 0;
    64 }
    65 
    66 static toff_t tiff_size(thandle_t fd)
    67 {
    68 	Uint32 save_pos;
    69 	toff_t size;
    70 
    71 	save_pos = SDL_RWtell((SDL_RWops*)fd);
    72 	SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
    73         size = SDL_RWtell((SDL_RWops*)fd);
    74 	SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
    75 	return size;
    76 }
    77 
    78 int IMG_isTIF(SDL_RWops* src)
    79 {
    80 	int start;
    81 	int is_TIF;
    82 	TIFF* tiff;
    83 	TIFFErrorHandler prev_handler;
    84 
    85 	start = SDL_RWtell(src);
    86 	is_TIF = 0;
    87 
    88 	/* Suppress output from libtiff */
    89 	prev_handler = TIFFSetErrorHandler(NULL);
    90 	
    91 	/* Attempt to process the given file data */
    92 	/* turn off memory mapped access with the m flag */
    93 	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
    94 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
    95 
    96 	/* Reset the default error handler, since it can be useful for info */
    97 	TIFFSetErrorHandler(prev_handler);
    98 
    99 	/* If it's not a TIFF, then tiff will be NULL. */
   100 	if ( tiff ) {
   101 		is_TIF = 1;
   102 
   103 		/* Free up any dynamically allocated memory libtiff uses */
   104 		TIFFClose(tiff);
   105 	}
   106 	SDL_RWseek(src, start, SEEK_SET);
   107 	return(is_TIF);
   108 }
   109 
   110 SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
   111 {
   112 	int start;
   113 	TIFF* tiff;
   114 	SDL_Surface* surface = NULL;
   115 	Uint32 img_width, img_height;
   116 	Uint32 Rmask, Gmask, Bmask, Amask, mask;
   117 	Uint32 x, y;
   118 	Uint32 half;
   119 
   120 	if ( !src ) {
   121 		/* The error message has been set in SDL_RWFromFile */
   122 		return NULL;
   123 	}
   124 	start = SDL_RWtell(src);
   125 
   126 	/* turn off memory mapped access with the m flag */
   127 	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   128 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   129 	if(!tiff)
   130 		goto error;
   131 
   132 	/* Retrieve the dimensions of the image from the TIFF tags */
   133 	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   134 	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   135 
   136 	Rmask = 0x000000FF;
   137 	Gmask = 0x0000FF00;
   138 	Bmask = 0x00FF0000;
   139 	Amask = 0xFF000000;
   140 	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
   141 		Rmask, Gmask, Bmask, Amask);
   142 	if(!surface)
   143 		goto error;
   144 	
   145 	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   146 		goto error;
   147 
   148 	/* libtiff loads the image upside-down, flip it back */
   149 	half = img_height / 2;
   150 	for(y = 0; y < half; y++)
   151 	{
   152 	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
   153 	        Uint32 *bot = (Uint32 *)surface->pixels
   154 		              + (img_height - y - 1) * surface->pitch/4;
   155 		for(x = 0; x < img_width; x++)
   156 		{
   157 		        Uint32 tmp = top[x];
   158 			top[x] = bot[x];
   159 			bot[x] = tmp;
   160 		}
   161 	}
   162 	TIFFClose(tiff);
   163 	
   164 	return surface;
   165 
   166 error:
   167 	SDL_RWseek(src, start, SEEK_SET);
   168 	if ( surface ) {
   169 		SDL_FreeSurface(surface);
   170 	}
   171 	return NULL;
   172 }
   173 
   174 #else
   175 
   176 /* See if an image is contained in a data source */
   177 int IMG_isTIF(SDL_RWops *src)
   178 {
   179 	return(0);
   180 }
   181 
   182 /* Load a TIFF type image from an SDL datasource */
   183 SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
   184 {
   185 	return(NULL);
   186 }
   187 
   188 #endif /* LOAD_TIF */