IMG_tif.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 22:04:38 +0000
changeset 98 9f94c4674cc9
parent 97 e1161bd417c4
child 117 e613cf987897
permissions -rw-r--r--
Added checks for NULL data sources to individual loaders.
Apps shouldn't be calling them, but they do. Naughty naughty! :)
     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 	TIFF* tiff;
    81 	TIFFErrorHandler prev_handler;
    82 
    83 	/* Suppress output from libtiff */
    84 	prev_handler = TIFFSetErrorHandler(NULL);
    85 	
    86 	/* Attempt to process the given file data */
    87 	/* turn off memory mapped access with the m flag */
    88 	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
    89 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
    90 
    91 	/* Reset the default error handler, since it can be useful for info */
    92 	TIFFSetErrorHandler(prev_handler);
    93 
    94 	/* If it's not a TIFF, then tiff will be NULL. */
    95 	if(!tiff)
    96 		return 0;
    97 
    98 	/* Free up any dynamically allocated memory libtiff uses */
    99 	TIFFClose(tiff);
   100 	
   101 	return 1;
   102 }
   103 
   104 SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
   105 {
   106 	TIFF* tiff;
   107 	SDL_Surface* surface = NULL;
   108 	Uint32 img_width, img_height;
   109 	Uint32 Rmask, Gmask, Bmask, Amask, mask;
   110 	Uint32 x, y;
   111 	Uint32 half;
   112 
   113 	if ( !src ) {
   114 		/* The error message has been set in SDL_RWFromFile */
   115 		return NULL;
   116 	}
   117 
   118 	/* turn off memory mapped access with the m flag */
   119 	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   120 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   121 	if(!tiff)
   122 		return NULL;
   123 
   124 	/* Retrieve the dimensions of the image from the TIFF tags */
   125 	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   126 	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   127 
   128 	Rmask = 0x000000FF;
   129 	Gmask = 0x0000FF00;
   130 	Bmask = 0x00FF0000;
   131 	Amask = 0xFF000000;
   132 	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
   133 		Rmask, Gmask, Bmask, Amask);
   134 	if(!surface)
   135 		return NULL;
   136 	
   137 	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   138 		return NULL;
   139 
   140 	/* libtiff loads the image upside-down, flip it back */
   141 	half = img_height / 2;
   142 	for(y = 0; y < half; y++)
   143 	{
   144 	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
   145 	        Uint32 *bot = (Uint32 *)surface->pixels
   146 		              + (img_height - y - 1) * surface->pitch/4;
   147 		for(x = 0; x < img_width; x++)
   148 		{
   149 		        Uint32 tmp = top[x];
   150 			top[x] = bot[x];
   151 			bot[x] = tmp;
   152 		}
   153 	}
   154 	TIFFClose(tiff);
   155 	
   156 	return surface;
   157 }
   158 
   159 #else
   160 
   161 /* See if an image is contained in a data source */
   162 int IMG_isTIF(SDL_RWops *src)
   163 {
   164 	return(0);
   165 }
   166 
   167 /* Load a TIFF type image from an SDL datasource */
   168 SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
   169 {
   170 	return(NULL);
   171 }
   172 
   173 #endif /* LOAD_TIF */