IMG_tif.c
changeset 4 b1bb33e907f8
parent 0 76be7dab668c
child 53 96b084473b47
     1.1 --- a/IMG_tif.c	Thu Aug 10 06:15:57 2000 +0000
     1.2 +++ b/IMG_tif.c	Fri Sep 01 00:45:57 2000 +0000
     1.3 @@ -22,6 +22,9 @@
     1.4      slouken@devolution.com
     1.5  
     1.6      5/29/2000: TIFF loader written. Mark Baker (mbaker@0x7a69.net)
     1.7 +    2000-07-28: Fixed two off-by one bugs in reversal loop and made it work on
     1.8 +                big-endian machines (Mattias)
     1.9 +    2000-08-09: Removed alpha inversion (Mattias)
    1.10  */
    1.11  
    1.12  
    1.13 @@ -36,7 +39,6 @@
    1.14  
    1.15  #include <tiffio.h>
    1.16  
    1.17 -
    1.18  /*
    1.19   * These are the thunking routine to use the SDL_RWops* routines from
    1.20   * libtiff's internals.
    1.21 @@ -109,11 +111,9 @@
    1.22  	TIFF* tiff;
    1.23  	SDL_Surface* surface = NULL;
    1.24  	Uint32 img_width, img_height;
    1.25 -	Uint32 Rmask, Gmask, Bmask, Amask;
    1.26 +	Uint32 Rmask, Gmask, Bmask, Amask, mask;
    1.27  	Uint32 x, y;
    1.28  	Uint32 half;
    1.29 -	Uint32 tl_coord, bl_coord;
    1.30 -	Uint32 tl_pixel, bl_pixel;
    1.31  
    1.32  
    1.33  	/* turn off memory mapped access with the m flag */
    1.34 @@ -126,21 +126,10 @@
    1.35  	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
    1.36  	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
    1.37  
    1.38 -	if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
    1.39 -	{
    1.40 -		Rmask = 0x000000FF;
    1.41 -		Gmask = 0x0000FF00;
    1.42 -		Bmask = 0x00FF0000;
    1.43 -		Amask = 0xFF000000;
    1.44 -	}
    1.45 -	else
    1.46 -	{
    1.47 -		Rmask = 0xFF000000;
    1.48 -		Gmask = 0x00FF0000;
    1.49 -		Bmask = 0x0000FF00;
    1.50 -		Amask = 0x000000FF;
    1.51 -	}              
    1.52 -
    1.53 +	Rmask = 0x000000FF;
    1.54 +	Gmask = 0x0000FF00;
    1.55 +	Bmask = 0x00FF0000;
    1.56 +	Amask = 0xFF000000;
    1.57  	surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
    1.58  		Rmask, Gmask, Bmask, Amask);
    1.59  	if(!surface)
    1.60 @@ -149,36 +138,18 @@
    1.61  	if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
    1.62  		return NULL;
    1.63  
    1.64 -	/* 
    1.65 -	 * For this to make any sense, a little information is needed.
    1.66 -	 * Firstly, SDL uses more or less the opposite value for alpha as
    1.67 -	 * libtiff, so we need to invert the alpha channel of each pixel.
    1.68 -	 * To do this, I'm just xor it with the alpha mask.
    1.69 -	 *
    1.70 -	 * Secondly, libtiff loads the image more or less inverted.
    1.71 -	 * So the top left of the image ends up being the bottom left.
    1.72 -	 * To correct for this, we swap the pixels as we're inverting the 
    1.73 -	 * alpha channel
    1.74 -	*/
    1.75 -
    1.76 -	// We only need to loop 1/2 the height, because we're swapping values.
    1.77 +	/* libtiff loads the image upside-down, flip it back */
    1.78  	half = img_height / 2;
    1.79 -
    1.80 -	for(y = 0; y <= half; y++)
    1.81 +	for(y = 0; y < half; y++)
    1.82  	{
    1.83 +	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
    1.84 +	        Uint32 *bot = (Uint32 *)surface->pixels
    1.85 +		              + (img_height - y - 1) * surface->pitch/4;
    1.86  		for(x = 0; x < img_width; x++)
    1.87  		{
    1.88 -			tl_coord = y * img_width + x;
    1.89 -			bl_coord = (img_height - y - 1) * img_width + x;
    1.90 -
    1.91 -			/* Invert the value of the alpha channel, while copying the pixel */
    1.92 -			tl_pixel = *((Uint32*)surface->pixels + tl_coord) ^ Amask;
    1.93 -			bl_pixel = *((Uint32*)surface->pixels + bl_coord) ^ Amask;
    1.94 -
    1.95 -			/* Now invert the location of the pixel relative to the image */
    1.96 -			*((Uint32*)surface->pixels + bl_coord) = tl_pixel;
    1.97 -			*((Uint32*)surface->pixels + tl_coord) = bl_pixel;
    1.98 -
    1.99 +		        Uint32 tmp = top[x];
   1.100 +			top[x] = bot[x];
   1.101 +			bot[x] = tmp;
   1.102  		}
   1.103  	}
   1.104  	TIFFClose(tiff);