IMG_tif.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 13:02:16 +0000
changeset 53 96b084473b47
parent 4 b1bb33e907f8
child 97 e1161bd417c4
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999, 2000, 2001  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 
   114 	/* turn off memory mapped access with the m flag */
   115 	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
   116 		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
   117 	if(!tiff)
   118 		return NULL;
   119 
   120 	/* Retrieve the dimensions of the image from the TIFF tags */
   121 	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
   122 	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
   123 
   124 	Rmask = 0x000000FF;
   125 	Gmask = 0x0000FF00;
   126 	Bmask = 0x00FF0000;
   127 	Amask = 0xFF000000;
   128 	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
   129 		Rmask, Gmask, Bmask, Amask);
   130 	if(!surface)
   131 		return NULL;
   132 	
   133 	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
   134 		return NULL;
   135 
   136 	/* libtiff loads the image upside-down, flip it back */
   137 	half = img_height / 2;
   138 	for(y = 0; y < half; y++)
   139 	{
   140 	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
   141 	        Uint32 *bot = (Uint32 *)surface->pixels
   142 		              + (img_height - y - 1) * surface->pitch/4;
   143 		for(x = 0; x < img_width; x++)
   144 		{
   145 		        Uint32 tmp = top[x];
   146 			top[x] = bot[x];
   147 			bot[x] = tmp;
   148 		}
   149 	}
   150 	TIFFClose(tiff);
   151 	
   152 	return surface;
   153 }
   154 
   155 #else
   156 
   157 /* See if an image is contained in a data source */
   158 int IMG_isTIF(SDL_RWops *src)
   159 {
   160 	return(0);
   161 }
   162 
   163 /* Load a TIFF type image from an SDL datasource */
   164 SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
   165 {
   166 	return(NULL);
   167 }
   168 
   169 #endif /* LOAD_TIF */