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