Daniel Morais - Sun Sep 23 16:32:13 PDT 2001
authorSam Lantinga <slouken@libsdl.org>
Sun, 23 Sep 2001 23:33:06 +0000
changeset 4545736a3044c0
parent 44 8e4ebbd9893d
child 46 c08be76c14ef
Daniel Morais - Sun Sep 23 16:32:13 PDT 2001
* Added support for the IFF (LBM) image format
CHANGES
IMG.c
IMG_lbm.c
Makefile.am
README
SDL_image.h
VisualC.zip
configure.in
     1.1 --- a/CHANGES	Mon Aug 20 02:42:46 2001 +0000
     1.2 +++ b/CHANGES	Sun Sep 23 23:33:06 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.2.1:
     1.6 +Daniel Morais - Sun Sep 23 16:32:13 PDT 2001
     1.7 + * Added support for the IFF (LBM) image format
     1.8  Sam Lantinga - Sun Aug 19 01:51:44 PDT 2001
     1.9   * Added Project Builder projects for building MacOS X framework
    1.10  Mattias Engdegård - Tue Jul 31 04:32:29 PDT 2001
     2.1 --- a/IMG.c	Mon Aug 20 02:42:46 2001 +0000
     2.2 +++ b/IMG.c	Sun Sep 23 23:33:06 2001 +0000
     2.3 @@ -48,6 +48,7 @@
     2.4  	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
     2.5  	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
     2.6  	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
     2.7 +	{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
     2.8  	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW }
     2.9  };
    2.10  
    2.11 @@ -68,7 +69,7 @@
    2.12  }
    2.13  
    2.14  /* Portable case-insensitive string compare function */
    2.15 -static int IMG_string_equals(const char *str1, const char *str2)
    2.16 +int IMG_string_equals(const char *str1, const char *str2)
    2.17  {
    2.18  	while ( *str1 && *str2 ) {
    2.19  		if ( toupper((unsigned char)*str1) !=
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/IMG_lbm.c	Sun Sep 23 23:33:06 2001 +0000
     3.3 @@ -0,0 +1,369 @@
     3.4 +/*
     3.5 +    IMGLIB:  An example image loading library for use with SDL
     3.6 +    Copyright (C) 1999  Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Library General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Library General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Library General Public
    3.19 +    License along with this library; if not, write to the Free
    3.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    5635-34 Springhouse Dr.
    3.24 +    Pleasanton, CA 94588 (USA)
    3.25 +    slouken@devolution.com
    3.26 +*/
    3.27 + 
    3.28 +/* This is a ILBM image file loading framework
    3.29 +   Load IFF pictures, PBM & ILBM packing methods, with or without stencil
    3.30 +   Written by Daniel Morais ( Daniel@Morais.com ) in September 2001
    3.31 +*/
    3.32 +
    3.33 +#include <stdio.h>
    3.34 +#include <stdlib.h>
    3.35 +
    3.36 +#include "SDL_endian.h"
    3.37 +#include "SDL_image.h"
    3.38 +
    3.39 +#ifdef LOAD_LBM
    3.40 +
    3.41 +
    3.42 +//===========================================================================
    3.43 +// DEFINES
    3.44 +//===========================================================================
    3.45 +
    3.46 +#define MAXCOLORS 256
    3.47 +
    3.48 +//===========================================================================
    3.49 +// STRUCTURES
    3.50 +//===========================================================================
    3.51 +
    3.52 +// Structure for an IFF picture ( BMHD = Bitmap Header )
    3.53 +
    3.54 +typedef struct
    3.55 +{
    3.56 +   Uint16 w, h;		// width & height of the bitmap in pixels
    3.57 +   Sint16 x, y;      // screen coordinates of the bitmap
    3.58 +   Uint8 planes;     // number of planes of the bitmap
    3.59 +   Uint8 mask;       // mask type ( 0 => no mask )
    3.60 +   Uint8 tcomp;      // compression type
    3.61 +   Uint8 pad1;       // dummy value, for padding
    3.62 +   Uint16 tcolor;    // transparent color
    3.63 +   Uint8 xAspect,    // pixel aspect ratio
    3.64 +         yAspect;
    3.65 +   Sint16  Lpage;		// width of the screen in pixels
    3.66 +   Sint16  Hpage;		// height of the screen in pixels
    3.67 +
    3.68 +} BMHD;
    3.69 +
    3.70 +//===========================================================================
    3.71 +// See if an image is contained in a data source
    3.72 +
    3.73 +int IMG_isLBM( SDL_RWops *src )
    3.74 +{
    3.75 +	int   is_LBM;
    3.76 +	Uint8 magic[4+4+4];
    3.77 +
    3.78 +	is_LBM = 0;
    3.79 +	if ( SDL_RWread( src, magic, 4+4+4, 1 ) ) 
    3.80 +	{
    3.81 +		if ( !memcmp( magic, "FORM", 4 ) && 
    3.82 +			( !memcmp( magic + 8, "PBM ", 4 ) || 
    3.83 +			  !memcmp( magic + 8, "ILBM", 4 ) ) ) 
    3.84 +		{
    3.85 +			is_LBM = 1;
    3.86 +		}
    3.87 +	}
    3.88 +	return( is_LBM );
    3.89 +}
    3.90 +
    3.91 +//===========================================================================
    3.92 +// Load a IFF type image from an SDL datasource
    3.93 +
    3.94 +SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
    3.95 +{
    3.96 +	SDL_Surface *Image;
    3.97 +	Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    3.98 +   Uint32      size, bytesloaded, nbcolors;
    3.99 +	Uint32      i, j, bytesperline, nbplanes, plane, h;
   3.100 +	Uint32      remainingbytes;
   3.101 +	int         width;
   3.102 +	BMHD	      bmhd;
   3.103 +	char        *error;
   3.104 +
   3.105 +	Image   = NULL;
   3.106 +	error   = NULL;
   3.107 +	MiniBuf = NULL;
   3.108 +
   3.109 +	if ( src == NULL ) goto done;
   3.110 +
   3.111 +	if ( !SDL_RWread( src, id, 4, 1 ) ) 
   3.112 +	{
   3.113 +	   error="error reading IFF chunk";
   3.114 +		goto done;
   3.115 +	}
   3.116 +
   3.117 +	if ( !SDL_RWread( src, &size, 4, 1 ) ) // Should be the size of the file minus 4+4 ( 'FORM'+size )
   3.118 +	{
   3.119 +	   error="error reading IFF chunk size";
   3.120 +		goto done;
   3.121 +	}
   3.122 +
   3.123 +	// As size is not used here, no need to swap it
   3.124 +	
   3.125 +	if ( memcmp( id, "FORM", 4 ) != 0 ) 
   3.126 +	{
   3.127 +	   error="not a IFF file";
   3.128 +		goto done;
   3.129 +	}
   3.130 +
   3.131 +	if ( !SDL_RWread( src, id, 4, 1 ) ) 
   3.132 +	{
   3.133 +	   error="error reading IFF chunk";
   3.134 +		goto done;
   3.135 +	}
   3.136 +
   3.137 +	pbm = 0;
   3.138 +
   3.139 +	if ( !memcmp( id, "PBM ", 4 ) ) pbm = 1; // File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap
   3.140 +	else if ( memcmp( id, "ILBM", 4 ) )
   3.141 +	{
   3.142 +	   error="not a IFF picture";
   3.143 +		goto done;
   3.144 +	}
   3.145 +
   3.146 +	nbcolors = 0;
   3.147 +
   3.148 +	memset( &bmhd, 0, sizeof( BMHD ) );
   3.149 +
   3.150 +	while ( memcmp( id, "BODY", 4 ) != 0 ) 
   3.151 +	{
   3.152 +		if ( !SDL_RWread( src, id, 4, 1 ) ) 
   3.153 +		{
   3.154 +	      error="error reading IFF chunk";
   3.155 +			goto done;
   3.156 +		}
   3.157 +
   3.158 +		if ( !SDL_RWread( src, &size, 4, 1 ) ) 
   3.159 +		{
   3.160 +	      error="error reading IFF chunk size";
   3.161 +			goto done;
   3.162 +		}
   3.163 +
   3.164 +		bytesloaded = 0;
   3.165 +
   3.166 +		size = SDL_SwapBE32( size );
   3.167 +		
   3.168 +		if ( !memcmp( id, "BMHD", 4 ) ) // Bitmap header
   3.169 +		{
   3.170 +			if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
   3.171 +			{
   3.172 +			   error="error reading BMHD chunk";
   3.173 +				goto done;
   3.174 +			}
   3.175 +
   3.176 +			bytesloaded = sizeof( BMHD );
   3.177 +
   3.178 +			bmhd.w 		= SDL_SwapBE16( bmhd.w );
   3.179 +			bmhd.h 		= SDL_SwapBE16( bmhd.h );
   3.180 +			bmhd.x 		= SDL_SwapBE16( bmhd.x );
   3.181 +			bmhd.y 		= SDL_SwapBE16( bmhd.y );
   3.182 +			bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
   3.183 +			bmhd.Lpage 	= SDL_SwapBE16( bmhd.Lpage );
   3.184 +			bmhd.Hpage 	= SDL_SwapBE16( bmhd.Hpage );
   3.185 +		}
   3.186 +
   3.187 +		if ( !memcmp( id, "CMAP", 4 ) ) // palette ( Color Map )
   3.188 +		{
   3.189 +			if ( !SDL_RWread( src, &colormap, size, 1 ) )
   3.190 +			{
   3.191 +			   error="error reading CMAP chunk";
   3.192 +				goto done;
   3.193 +			}
   3.194 +
   3.195 +			bytesloaded = size;
   3.196 +			nbcolors = size / 3;
   3.197 +		}
   3.198 +
   3.199 +		if ( memcmp( id, "BODY", 4 ) )
   3.200 +		{
   3.201 +		   if ( size & 1 )	++size;  	// padding !
   3.202 +			size -= bytesloaded;
   3.203 +			if ( size )	SDL_RWseek( src, size, SEEK_CUR ); // skip the remaining bytes of this chunk
   3.204 +		}
   3.205 +	}
   3.206 +
   3.207 +	// compute some usefull values, based on the bitmap header
   3.208 +
   3.209 +	width = ( bmhd.w + 15 ) & 0xFFFFFFF0;        // Width in pixels modulo 16
   3.210 +
   3.211 +	bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;	// Number of bytes per line
   3.212 +
   3.213 +	nbplanes = bmhd.planes;                      // Number of planes
   3.214 +
   3.215 +	if ( pbm )                                   // File format : 'Packed Bitmap'
   3.216 +	{
   3.217 +	   bytesperline *= 8;
   3.218 +		nbplanes = 1;
   3.219 +	}
   3.220 +
   3.221 +	if ( bmhd.mask ) ++nbplanes;                 // There is a mask ( 'stencil' )
   3.222 +
   3.223 +	// Allocate memory for a temporary buffer ( used for decompression/deinterleaving )
   3.224 +
   3.225 +	if ( ( MiniBuf = (void *)malloc( bytesperline * nbplanes ) ) == NULL )
   3.226 +	{
   3.227 +	   error="no enough memory for temporary buffer";
   3.228 +		goto done;
   3.229 +	}
   3.230 +
   3.231 +	// Create the surface
   3.232 +
   3.233 +	if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, 8, 0, 0, 0, 0 ) ) == NULL )
   3.234 +	   goto done;
   3.235 +
   3.236 +	// Update palette informations
   3.237 +
   3.238 +	Image->format->palette->ncolors = nbcolors;
   3.239 +
   3.240 +	ptr = &colormap[0];
   3.241 +
   3.242 +	for ( i=0; i<nbcolors; i++ )
   3.243 +	{
   3.244 +	   Image->format->palette->colors[i].r = *ptr++;
   3.245 +	   Image->format->palette->colors[i].g = *ptr++;
   3.246 +	   Image->format->palette->colors[i].b = *ptr++;
   3.247 +	}
   3.248 +
   3.249 +	// Get the bitmap
   3.250 +
   3.251 +	for ( h=0; h < bmhd.h; h++ )
   3.252 +	{
   3.253 +		// uncompress the datas of each planes
   3.254 +			  
   3.255 +	   for ( plane=0; plane < nbplanes; plane++ )
   3.256 +		{
   3.257 +		   ptr = MiniBuf + ( plane * bytesperline );
   3.258 +	
   3.259 +			remainingbytes = bytesperline;
   3.260 +	
   3.261 +			if ( bmhd.tcomp == 1 )			// Datas are compressed
   3.262 +			{
   3.263 +			   do
   3.264 +				{
   3.265 +				   if ( !SDL_RWread( src, &count, 1, 1 ) )
   3.266 +					{
   3.267 +					   error="error reading BODY chunk";
   3.268 +						goto done;
   3.269 +					}
   3.270 +	
   3.271 +					if ( count & 0x80 )
   3.272 +					{
   3.273 +					   count ^= 0xFF;
   3.274 +						count += 2; // now it
   3.275 +						
   3.276 +						if ( !SDL_RWread( src, &color, 1, 1 ) )
   3.277 +						{
   3.278 +						   error="error reading BODY chunk";
   3.279 +							goto done;
   3.280 +						}
   3.281 +						memset( ptr, color, count );
   3.282 +					}
   3.283 +					else
   3.284 +					{
   3.285 +					   ++count;
   3.286 +
   3.287 +						if ( !SDL_RWread( src, ptr, count, 1 ) )
   3.288 +						{
   3.289 +						   error="error reading BODY chunk";
   3.290 +							goto done;
   3.291 +						}
   3.292 +					}
   3.293 +	
   3.294 +					ptr += count;
   3.295 +					remainingbytes -= count;
   3.296 +	
   3.297 +				} while ( remainingbytes > 0 );
   3.298 +			}
   3.299 +			else	
   3.300 +			{
   3.301 +			   if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
   3.302 +				{
   3.303 +				   error="error reading BODY chunk";
   3.304 +					goto done;
   3.305 +				}
   3.306 +			}
   3.307 +		}
   3.308 +	
   3.309 +		// One line has been read, store it !
   3.310 +
   3.311 +		ptr = Image->pixels;
   3.312 +		ptr += h * width;
   3.313 +	
   3.314 +		if ( pbm )                 // File format : 'Packed Bitmap'
   3.315 +		{
   3.316 +		   memcpy( ptr, MiniBuf, width );
   3.317 +		}
   3.318 +		else							   // We have to un-interlace the bits !
   3.319 +		{
   3.320 +		   size = ( width + 7 ) / 8;
   3.321 +	
   3.322 +			for ( i=0; i < size; i++ )
   3.323 +			{
   3.324 +			   memset( ptr, 0, 8 );
   3.325 +	
   3.326 +				for ( plane=0; plane < nbplanes; plane++ )
   3.327 +				{
   3.328 +				   color = *( MiniBuf + i + ( plane * bytesperline ) );
   3.329 +					msk = 0x80;
   3.330 +	
   3.331 +					for ( j=0; j<8; j++ )
   3.332 +					{
   3.333 +					   if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
   3.334 +						else 	                    ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
   3.335 +	
   3.336 +						msk >>= 1;
   3.337 +					}
   3.338 +				}
   3.339 +				ptr += 8;
   3.340 +			}
   3.341 +		}
   3.342 +	}
   3.343 +
   3.344 +done:
   3.345 +
   3.346 +	if ( MiniBuf ) free( MiniBuf );
   3.347 +
   3.348 +	if ( error ) 
   3.349 +	{
   3.350 +		IMG_SetError( error );
   3.351 +	   SDL_FreeSurface( Image );
   3.352 +		Image = NULL;
   3.353 +	}
   3.354 +
   3.355 +	return( Image );
   3.356 +}
   3.357 +
   3.358 +#else /* LOAD_LBM */
   3.359 +
   3.360 +/* See if an image is contained in a data source */
   3.361 +int IMG_isLBM(SDL_RWops *src)
   3.362 +{
   3.363 +	return(0);
   3.364 +}
   3.365 +
   3.366 +/* Load an IFF type image from an SDL datasource */
   3.367 +SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src)
   3.368 +{
   3.369 +	return(NULL);
   3.370 +}
   3.371 +
   3.372 +#endif /* LOAD_LBM */
     4.1 --- a/Makefile.am	Mon Aug 20 02:42:46 2001 +0000
     4.2 +++ b/Makefile.am	Sun Sep 23 23:33:06 2001 +0000
     4.3 @@ -11,6 +11,7 @@
     4.4  	IMG_bmp.c		\
     4.5  	IMG_gif.c		\
     4.6  	IMG_jpg.c		\
     4.7 +	IMG_lbm.c		\
     4.8  	IMG_pcx.c		\
     4.9  	IMG_png.c		\
    4.10  	IMG_pnm.c		\
     5.1 --- a/README	Mon Aug 20 02:42:46 2001 +0000
     5.2 +++ b/README	Sun Sep 23 23:33:06 2001 +0000
     5.3 @@ -5,7 +5,7 @@
     5.4  http://www.libsdl.org/projects/SDL_image/
     5.5  
     5.6  This is a simple library to load images of various formats as SDL surfaces.
     5.7 -This library supports BMP, PNM (PPM/PGM/PBM), XPM, PCX, GIF, JPEG, PNG,
     5.8 +This library supports BMP, PNM (PPM/PGM/PBM), XPM, LBM, PCX, GIF, JPEG, PNG,
     5.9  TGA, and TIFF formats.
    5.10  
    5.11  API:
     6.1 --- a/SDL_image.h	Mon Aug 20 02:42:46 2001 +0000
     6.2 +++ b/SDL_image.h	Sun Sep 23 23:33:06 2001 +0000
     6.3 @@ -64,6 +64,7 @@
     6.4  extern DECLSPEC int IMG_isJPG(SDL_RWops *src);
     6.5  extern DECLSPEC int IMG_isTIF(SDL_RWops *src);
     6.6  extern DECLSPEC int IMG_isPNG(SDL_RWops *src);
     6.7 +extern DECLSPEC int IMG_isLBM(SDL_RWops *src);
     6.8  
     6.9  /* Individual loading functions */
    6.10  extern DECLSPEC SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src);
    6.11 @@ -76,6 +77,7 @@
    6.12  extern DECLSPEC SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src);
    6.13  extern DECLSPEC SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src);
    6.14  extern DECLSPEC SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src);
    6.15 +extern DECLSPEC SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src);
    6.16  
    6.17  extern DECLSPEC SDL_Surface *IMG_ReadXPMFromArray(char **xpm);
    6.18  
    6.19 @@ -83,6 +85,9 @@
    6.20  #define IMG_SetError	SDL_SetError
    6.21  #define IMG_GetError	SDL_GetError
    6.22  
    6.23 +/* used internally, NOT an exported function */
    6.24 +extern int IMG_string_equals(const char *str1, const char *str2);
    6.25 +
    6.26  /* Ends C function definitions when using C++ */
    6.27  #ifdef __cplusplus
    6.28  }
     7.1 Binary file VisualC.zip has changed
     8.1 --- a/configure.in	Mon Aug 20 02:42:46 2001 +0000
     8.2 +++ b/configure.in	Sun Sep 23 23:33:06 2001 +0000
     8.3 @@ -111,6 +111,12 @@
     8.4          AC_MSG_WARN([JPG image loading disabled])
     8.5      fi
     8.6  fi
     8.7 +AC_ARG_ENABLE(lbm,
     8.8 +[  --enable-lbm            support loading LBM images [default=yes]],
     8.9 +              , enable_lbm=yes)
    8.10 +if test x$enable_lbm = xyes; then
    8.11 +    CFLAGS="$CFLAGS -DLOAD_LBM"
    8.12 +fi
    8.13  AC_ARG_ENABLE(pcx,
    8.14  [  --enable-pcx            support loading PCX images [default=yes]],
    8.15                , enable_pcx=yes)