IMG_tif.c
author Sam Lantinga <slouken@lokigames.com>
Fri, 01 Sep 2000 00:45:57 +0000
changeset 4 b1bb33e907f8
parent 0 76be7dab668c
child 53 96b084473b47
permissions -rw-r--r--
Mattias Engdeg�rd - Wed Aug 9 20:32:22 MET DST 2000
* Removed the alpha flipping, made IMG_InvertAlpha() a noop
* Fixed nonexisting PCX alpha support
* Some TIFF bugfixes
* PNG greyscale images are loaded as 8bpp with a greyscale palette
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@4
    25
    2000-07-28: Fixed two off-by one bugs in reversal loop and made it work on
slouken@4
    26
                big-endian machines (Mattias)
slouken@4
    27
    2000-08-09: Removed alpha inversion (Mattias)
slouken@0
    28
*/
slouken@0
    29
slouken@0
    30
slouken@0
    31
slouken@0
    32
/* This is a TIFF image file loading framework */
slouken@0
    33
slouken@0
    34
#include <stdio.h>
slouken@0
    35
slouken@0
    36
#include "SDL_image.h"
slouken@0
    37
slouken@0
    38
#ifdef LOAD_TIF
slouken@0
    39
slouken@0
    40
#include <tiffio.h>
slouken@0
    41
slouken@0
    42
/*
slouken@0
    43
 * These are the thunking routine to use the SDL_RWops* routines from
slouken@0
    44
 * libtiff's internals.
slouken@0
    45
*/
slouken@0
    46
slouken@0
    47
static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
slouken@0
    48
{
slouken@0
    49
	return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
slouken@0
    50
}
slouken@0
    51
slouken@0
    52
static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
slouken@0
    53
{
slouken@0
    54
	return SDL_RWseek((SDL_RWops*)fd, offset, origin);
slouken@0
    55
}
slouken@0
    56
slouken@0
    57
static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
slouken@0
    58
{
slouken@0
    59
	return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
slouken@0
    60
}
slouken@0
    61
slouken@0
    62
static int tiff_close(thandle_t fd)
slouken@0
    63
{
slouken@0
    64
	/*
slouken@0
    65
	 * We don't want libtiff closing our SDL_RWops*, but if it's not given
slouken@0
    66
         * a routine to try, and if the image isn't a TIFF, it'll segfault.
slouken@0
    67
	 */
slouken@0
    68
	return 0;
slouken@0
    69
}
slouken@0
    70
slouken@0
    71
static toff_t tiff_size(thandle_t fd)
slouken@0
    72
{
slouken@0
    73
	Uint32 save_pos;
slouken@0
    74
	toff_t size;
slouken@0
    75
slouken@0
    76
	save_pos = SDL_RWtell((SDL_RWops*)fd);
slouken@0
    77
	SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
slouken@0
    78
        size = SDL_RWtell((SDL_RWops*)fd);
slouken@0
    79
	SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
slouken@0
    80
	return size;
slouken@0
    81
}
slouken@0
    82
slouken@0
    83
int IMG_isTIF(SDL_RWops* src)
slouken@0
    84
{
slouken@0
    85
	TIFF* tiff;
slouken@0
    86
	TIFFErrorHandler prev_handler;
slouken@0
    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@0
   100
	if(!tiff)
slouken@0
   101
		return 0;
slouken@0
   102
slouken@0
   103
	/* Free up any dynamically allocated memory libtiff uses */
slouken@0
   104
	TIFFClose(tiff);
slouken@0
   105
	
slouken@0
   106
	return 1;
slouken@0
   107
}
slouken@0
   108
slouken@0
   109
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
slouken@0
   110
{
slouken@0
   111
	TIFF* tiff;
slouken@0
   112
	SDL_Surface* surface = NULL;
slouken@0
   113
	Uint32 img_width, img_height;
slouken@4
   114
	Uint32 Rmask, Gmask, Bmask, Amask, mask;
slouken@0
   115
	Uint32 x, y;
slouken@0
   116
	Uint32 half;
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@4
   129
	Rmask = 0x000000FF;
slouken@4
   130
	Gmask = 0x0000FF00;
slouken@4
   131
	Bmask = 0x00FF0000;
slouken@4
   132
	Amask = 0xFF000000;
slouken@0
   133
	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
slouken@0
   134
		Rmask, Gmask, Bmask, Amask);
slouken@0
   135
	if(!surface)
slouken@0
   136
		return NULL;
slouken@0
   137
	
slouken@0
   138
	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
slouken@0
   139
		return NULL;
slouken@0
   140
slouken@4
   141
	/* libtiff loads the image upside-down, flip it back */
slouken@0
   142
	half = img_height / 2;
slouken@4
   143
	for(y = 0; y < half; y++)
slouken@0
   144
	{
slouken@4
   145
	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
slouken@4
   146
	        Uint32 *bot = (Uint32 *)surface->pixels
slouken@4
   147
		              + (img_height - y - 1) * surface->pitch/4;
slouken@0
   148
		for(x = 0; x < img_width; x++)
slouken@0
   149
		{
slouken@4
   150
		        Uint32 tmp = top[x];
slouken@4
   151
			top[x] = bot[x];
slouken@4
   152
			bot[x] = tmp;
slouken@0
   153
		}
slouken@0
   154
	}
slouken@0
   155
	TIFFClose(tiff);
slouken@0
   156
	
slouken@0
   157
	return surface;
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
#else
slouken@0
   161
slouken@0
   162
/* See if an image is contained in a data source */
slouken@0
   163
int IMG_isTIF(SDL_RWops *src)
slouken@0
   164
{
slouken@0
   165
	return(0);
slouken@0
   166
}
slouken@0
   167
slouken@0
   168
/* Load a TIFF type image from an SDL datasource */
slouken@0
   169
SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
slouken@0
   170
{
slouken@0
   171
	return(NULL);
slouken@0
   172
}
slouken@0
   173
slouken@0
   174
#endif /* LOAD_TIF */