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