IMG_tif.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 20:37:17 +0000
changeset 117 e613cf987897
parent 98 9f94c4674cc9
child 118 c5e736a47ad2
permissions -rw-r--r--
Fixed image type functions so they seek back to where they started
     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 	TIFF* tiff;
   113 	SDL_Surface* surface = NULL;
   114 	Uint32 img_width, img_height;
   115 	Uint32 Rmask, Gmask, Bmask, Amask, mask;
   116 	Uint32 x, y;
   117 	Uint32 half;
   118 
   119 	if ( !src ) {
   120 		/* The error message has been set in SDL_RWFromFile */
   121 		return NULL;
   122 	}
   123 
   124 	/* turn off memory mapped access with the m flag */
   125 	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   126 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   127 	if(!tiff)
   128 		return NULL;
   129 
   130 	/* Retrieve the dimensions of the image from the TIFF tags */
   131 	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   132 	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   133 
   134 	Rmask = 0x000000FF;
   135 	Gmask = 0x0000FF00;
   136 	Bmask = 0x00FF0000;
   137 	Amask = 0xFF000000;
   138 	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
   139 		Rmask, Gmask, Bmask, Amask);
   140 	if(!surface)
   141 		return NULL;
   142 	
   143 	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   144 		return NULL;
   145 
   146 	/* libtiff loads the image upside-down, flip it back */
   147 	half = img_height / 2;
   148 	for(y = 0; y < half; y++)
   149 	{
   150 	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
   151 	        Uint32 *bot = (Uint32 *)surface->pixels
   152 		              + (img_height - y - 1) * surface->pitch/4;
   153 		for(x = 0; x < img_width; x++)
   154 		{
   155 		        Uint32 tmp = top[x];
   156 			top[x] = bot[x];
   157 			bot[x] = tmp;
   158 		}
   159 	}
   160 	TIFFClose(tiff);
   161 	
   162 	return surface;
   163 }
   164 
   165 #else
   166 
   167 /* See if an image is contained in a data source */
   168 int IMG_isTIF(SDL_RWops *src)
   169 {
   170 	return(0);
   171 }
   172 
   173 /* Load a TIFF type image from an SDL datasource */
   174 SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
   175 {
   176 	return(NULL);
   177 }
   178 
   179 #endif /* LOAD_TIF */