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