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