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