IMG_tif.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 10 Aug 2000 06:05:39 +0000
changeset 0 76be7dab668c
child 4 b1bb33e907f8
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    IMGLIB:  An example image loading library for use with SDL
slouken@0
     3
    Copyright (C) 1999  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@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
slouken@0
    24
    5/29/2000: TIFF loader written. Mark Baker (mbaker@0x7a69.net)
slouken@0
    25
*/
slouken@0
    26
slouken@0
    27
slouken@0
    28
slouken@0
    29
/* This is a TIFF image file loading framework */
slouken@0
    30
slouken@0
    31
#include <stdio.h>
slouken@0
    32
slouken@0
    33
#include "SDL_image.h"
slouken@0
    34
slouken@0
    35
#ifdef LOAD_TIF
slouken@0
    36
slouken@0
    37
#include <tiffio.h>
slouken@0
    38
slouken@0
    39
slouken@0
    40
/*
slouken@0
    41
 * These are the thunking routine to use the SDL_RWops* routines from
slouken@0
    42
 * libtiff's internals.
slouken@0
    43
*/
slouken@0
    44
slouken@0
    45
static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
slouken@0
    46
{
slouken@0
    47
	return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
slouken@0
    48
}
slouken@0
    49
slouken@0
    50
static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
slouken@0
    51
{
slouken@0
    52
	return SDL_RWseek((SDL_RWops*)fd, offset, origin);
slouken@0
    53
}
slouken@0
    54
slouken@0
    55
static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
slouken@0
    56
{
slouken@0
    57
	return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
slouken@0
    58
}
slouken@0
    59
slouken@0
    60
static int tiff_close(thandle_t fd)
slouken@0
    61
{
slouken@0
    62
	/*
slouken@0
    63
	 * We don't want libtiff closing our SDL_RWops*, but if it's not given
slouken@0
    64
         * a routine to try, and if the image isn't a TIFF, it'll segfault.
slouken@0
    65
	 */
slouken@0
    66
	return 0;
slouken@0
    67
}
slouken@0
    68
slouken@0
    69
static toff_t tiff_size(thandle_t fd)
slouken@0
    70
{
slouken@0
    71
	Uint32 save_pos;
slouken@0
    72
	toff_t size;
slouken@0
    73
slouken@0
    74
	save_pos = SDL_RWtell((SDL_RWops*)fd);
slouken@0
    75
	SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
slouken@0
    76
        size = SDL_RWtell((SDL_RWops*)fd);
slouken@0
    77
	SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
slouken@0
    78
	return size;
slouken@0
    79
}
slouken@0
    80
slouken@0
    81
int IMG_isTIF(SDL_RWops* src)
slouken@0
    82
{
slouken@0
    83
	TIFF* tiff;
slouken@0
    84
	TIFFErrorHandler prev_handler;
slouken@0
    85
slouken@0
    86
	/* Suppress output from libtiff */
slouken@0
    87
	prev_handler = TIFFSetErrorHandler(NULL);
slouken@0
    88
	
slouken@0
    89
	/* Attempt to process the given file data */
slouken@0
    90
	/* turn off memory mapped access with the m flag */
slouken@0
    91
	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
slouken@0
    92
		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
slouken@0
    93
slouken@0
    94
	/* Reset the default error handler, since it can be useful for info */
slouken@0
    95
	TIFFSetErrorHandler(prev_handler);
slouken@0
    96
slouken@0
    97
	/* If it's not a TIFF, then tiff will be NULL. */
slouken@0
    98
	if(!tiff)
slouken@0
    99
		return 0;
slouken@0
   100
slouken@0
   101
	/* Free up any dynamically allocated memory libtiff uses */
slouken@0
   102
	TIFFClose(tiff);
slouken@0
   103
	
slouken@0
   104
	return 1;
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
slouken@0
   108
{
slouken@0
   109
	TIFF* tiff;
slouken@0
   110
	SDL_Surface* surface = NULL;
slouken@0
   111
	Uint32 img_width, img_height;
slouken@0
   112
	Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
   113
	Uint32 x, y;
slouken@0
   114
	Uint32 half;
slouken@0
   115
	Uint32 tl_coord, bl_coord;
slouken@0
   116
	Uint32 tl_pixel, bl_pixel;
slouken@0
   117
slouken@0
   118
slouken@0
   119
	/* turn off memory mapped access with the m flag */
slouken@0
   120
	tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
slouken@0
   121
		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
slouken@0
   122
	if(!tiff)
slouken@0
   123
		return NULL;
slouken@0
   124
slouken@0
   125
	/* Retrieve the dimensions of the image from the TIFF tags */
slouken@0
   126
	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
slouken@0
   127
	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
slouken@0
   128
slouken@0
   129
	if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
slouken@0
   130
	{
slouken@0
   131
		Rmask = 0x000000FF;
slouken@0
   132
		Gmask = 0x0000FF00;
slouken@0
   133
		Bmask = 0x00FF0000;
slouken@0
   134
		Amask = 0xFF000000;
slouken@0
   135
	}
slouken@0
   136
	else
slouken@0
   137
	{
slouken@0
   138
		Rmask = 0xFF000000;
slouken@0
   139
		Gmask = 0x00FF0000;
slouken@0
   140
		Bmask = 0x0000FF00;
slouken@0
   141
		Amask = 0x000000FF;
slouken@0
   142
	}              
slouken@0
   143
slouken@0
   144
	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
slouken@0
   145
		Rmask, Gmask, Bmask, Amask);
slouken@0
   146
	if(!surface)
slouken@0
   147
		return NULL;
slouken@0
   148
	
slouken@0
   149
	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
slouken@0
   150
		return NULL;
slouken@0
   151
slouken@0
   152
	/* 
slouken@0
   153
	 * For this to make any sense, a little information is needed.
slouken@0
   154
	 * Firstly, SDL uses more or less the opposite value for alpha as
slouken@0
   155
	 * libtiff, so we need to invert the alpha channel of each pixel.
slouken@0
   156
	 * To do this, I'm just xor it with the alpha mask.
slouken@0
   157
	 *
slouken@0
   158
	 * Secondly, libtiff loads the image more or less inverted.
slouken@0
   159
	 * So the top left of the image ends up being the bottom left.
slouken@0
   160
	 * To correct for this, we swap the pixels as we're inverting the 
slouken@0
   161
	 * alpha channel
slouken@0
   162
	*/
slouken@0
   163
slouken@0
   164
	// We only need to loop 1/2 the height, because we're swapping values.
slouken@0
   165
	half = img_height / 2;
slouken@0
   166
slouken@0
   167
	for(y = 0; y <= half; y++)
slouken@0
   168
	{
slouken@0
   169
		for(x = 0; x < img_width; x++)
slouken@0
   170
		{
slouken@0
   171
			tl_coord = y * img_width + x;
slouken@0
   172
			bl_coord = (img_height - y - 1) * img_width + x;
slouken@0
   173
slouken@0
   174
			/* Invert the value of the alpha channel, while copying the pixel */
slouken@0
   175
			tl_pixel = *((Uint32*)surface->pixels + tl_coord) ^ Amask;
slouken@0
   176
			bl_pixel = *((Uint32*)surface->pixels + bl_coord) ^ Amask;
slouken@0
   177
slouken@0
   178
			/* Now invert the location of the pixel relative to the image */
slouken@0
   179
			*((Uint32*)surface->pixels + bl_coord) = tl_pixel;
slouken@0
   180
			*((Uint32*)surface->pixels + tl_coord) = bl_pixel;
slouken@0
   181
slouken@0
   182
		}
slouken@0
   183
	}
slouken@0
   184
	TIFFClose(tiff);
slouken@0
   185
	
slouken@0
   186
	return surface;
slouken@0
   187
}
slouken@0
   188
slouken@0
   189
#else
slouken@0
   190
slouken@0
   191
/* See if an image is contained in a data source */
slouken@0
   192
int IMG_isTIF(SDL_RWops *src)
slouken@0
   193
{
slouken@0
   194
	return(0);
slouken@0
   195
}
slouken@0
   196
slouken@0
   197
/* Load a TIFF type image from an SDL datasource */
slouken@0
   198
SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
slouken@0
   199
{
slouken@0
   200
	return(NULL);
slouken@0
   201
}
slouken@0
   202
slouken@0
   203
#endif /* LOAD_TIF */