IMG_lbm.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 17 Dec 2001 19:56:28 +0000
changeset 58 46862ddd4577
parent 53 96b084473b47
child 74 ab2b38e9a9e2
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999, 2000, 2001  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     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24  
    25 /* This is a ILBM image file loading framework
    26    Load IFF pictures, PBM & ILBM packing methods, with or without stencil
    27    Written by Daniel Morais ( Daniel@Morais.com ) in September 2001
    28 */
    29 
    30 #include <stdio.h>
    31 #include <stdlib.h>
    32 #include <string.h>
    33 
    34 #include "SDL_endian.h"
    35 #include "SDL_image.h"
    36 
    37 #ifdef LOAD_LBM
    38 
    39 
    40 #define MAXCOLORS 256
    41 
    42 /* Structure for an IFF picture ( BMHD = Bitmap Header ) */
    43 
    44 typedef struct
    45 {
    46     Uint16 w, h;		/* width & height of the bitmap in pixels */
    47     Sint16 x, y;		/* screen coordinates of the bitmap */
    48     Uint8 planes;		/* number of planes of the bitmap */
    49     Uint8 mask;			/* mask type ( 0 => no mask ) */
    50     Uint8 tcomp;		/* compression type */
    51     Uint8 pad1;			/* dummy value, for padding */
    52     Uint16 tcolor;		/* transparent color */
    53     Uint8 xAspect,		/* pixel aspect ratio */
    54          yAspect;
    55     Sint16  Lpage;		/* width of the screen in pixels */
    56     Sint16  Hpage;		/* height of the screen in pixels */
    57 } BMHD;
    58 
    59 int IMG_isLBM( SDL_RWops *src )
    60 {
    61 	int   is_LBM;
    62 	Uint8 magic[4+4+4];
    63 
    64 	is_LBM = 0;
    65 	if ( SDL_RWread( src, magic, 4+4+4, 1 ) ) 
    66 	{
    67 		if ( !memcmp( magic, "FORM", 4 ) && 
    68 			( !memcmp( magic + 8, "PBM ", 4 ) || 
    69 			  !memcmp( magic + 8, "ILBM", 4 ) ) ) 
    70 		{
    71 			is_LBM = 1;
    72 		}
    73 	}
    74 	return( is_LBM );
    75 }
    76 
    77 SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
    78 {
    79 	SDL_Surface *Image;
    80 	Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    81    Uint32      size, bytesloaded, nbcolors;
    82 	Uint32      i, j, bytesperline, nbplanes, plane, h;
    83 	Uint32      remainingbytes;
    84 	int         width;
    85 	BMHD	      bmhd;
    86 	char        *error;
    87 
    88 	Image   = NULL;
    89 	error   = NULL;
    90 	MiniBuf = NULL;
    91 
    92 	if ( src == NULL ) goto done;
    93 
    94 	if ( !SDL_RWread( src, id, 4, 1 ) ) 
    95 	{
    96 	   error="error reading IFF chunk";
    97 		goto done;
    98 	}
    99 
   100 	/* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
   101 	if ( !SDL_RWread( src, &size, 4, 1 ) )
   102 	{
   103 	   error="error reading IFF chunk size";
   104 		goto done;
   105 	}
   106 
   107 	/* As size is not used here, no need to swap it */
   108 	
   109 	if ( memcmp( id, "FORM", 4 ) != 0 ) 
   110 	{
   111 	   error="not a IFF file";
   112 		goto done;
   113 	}
   114 
   115 	if ( !SDL_RWread( src, id, 4, 1 ) ) 
   116 	{
   117 	   error="error reading IFF chunk";
   118 		goto done;
   119 	}
   120 
   121 	pbm = 0;
   122 
   123 	/* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
   124 	if ( !memcmp( id, "PBM ", 4 ) ) pbm = 1;
   125 	else if ( memcmp( id, "ILBM", 4 ) )
   126 	{
   127 	   error="not a IFF picture";
   128 		goto done;
   129 	}
   130 
   131 	nbcolors = 0;
   132 
   133 	memset( &bmhd, 0, sizeof( BMHD ) );
   134 
   135 	while ( memcmp( id, "BODY", 4 ) != 0 ) 
   136 	{
   137 		if ( !SDL_RWread( src, id, 4, 1 ) ) 
   138 		{
   139 	      error="error reading IFF chunk";
   140 			goto done;
   141 		}
   142 
   143 		if ( !SDL_RWread( src, &size, 4, 1 ) ) 
   144 		{
   145 	      error="error reading IFF chunk size";
   146 			goto done;
   147 		}
   148 
   149 		bytesloaded = 0;
   150 
   151 		size = SDL_SwapBE32( size );
   152 		
   153 		if ( !memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
   154 		{
   155 			if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
   156 			{
   157 			   error="error reading BMHD chunk";
   158 				goto done;
   159 			}
   160 
   161 			bytesloaded = sizeof( BMHD );
   162 
   163 			bmhd.w 		= SDL_SwapBE16( bmhd.w );
   164 			bmhd.h 		= SDL_SwapBE16( bmhd.h );
   165 			bmhd.x 		= SDL_SwapBE16( bmhd.x );
   166 			bmhd.y 		= SDL_SwapBE16( bmhd.y );
   167 			bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
   168 			bmhd.Lpage 	= SDL_SwapBE16( bmhd.Lpage );
   169 			bmhd.Hpage 	= SDL_SwapBE16( bmhd.Hpage );
   170 		}
   171 
   172 		if ( !memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
   173 		{
   174 			if ( !SDL_RWread( src, &colormap, size, 1 ) )
   175 			{
   176 			   error="error reading CMAP chunk";
   177 				goto done;
   178 			}
   179 
   180 			bytesloaded = size;
   181 			nbcolors = size / 3;
   182 		}
   183 
   184 		if ( memcmp( id, "BODY", 4 ) )
   185 		{
   186 		    if ( size & 1 )	++size;  	/* padding ! */
   187 			size -= bytesloaded;
   188 			/* skip the remaining bytes of this chunk */
   189 			if ( size )	SDL_RWseek( src, size, SEEK_CUR );
   190 		}
   191 	}
   192 
   193 	/* compute some usefull values, based on the bitmap header */
   194 
   195 	width = ( bmhd.w + 15 ) & 0xFFFFFFF0;  /* Width in pixels modulo 16 */
   196 
   197 	bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
   198 
   199 	nbplanes = bmhd.planes;
   200 
   201 	if ( pbm )                         /* File format : 'Packed Bitmap' */
   202 	{
   203 	   bytesperline *= 8;
   204 		nbplanes = 1;
   205 	}
   206 
   207 	if ( bmhd.mask ) ++nbplanes;       /* There is a mask ( 'stencil' ) */
   208 
   209 	/* Allocate memory for a temporary buffer ( used for
   210            decompression/deinterleaving ) */
   211 
   212 	if ( ( MiniBuf = (void *)malloc( bytesperline * nbplanes ) ) == NULL )
   213 	{
   214 	   error="no enough memory for temporary buffer";
   215 		goto done;
   216 	}
   217 
   218 	if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, 8, 0, 0, 0, 0 ) ) == NULL )
   219 	   goto done;
   220 
   221 	/* Update palette informations */
   222 
   223 	Image->format->palette->ncolors = nbcolors;
   224 
   225 	ptr = &colormap[0];
   226 
   227 	for ( i=0; i<nbcolors; i++ )
   228 	{
   229 	   Image->format->palette->colors[i].r = *ptr++;
   230 	   Image->format->palette->colors[i].g = *ptr++;
   231 	   Image->format->palette->colors[i].b = *ptr++;
   232 	}
   233 
   234 	/* Get the bitmap */
   235 
   236 	for ( h=0; h < bmhd.h; h++ )
   237 	{
   238 	    /* uncompress the datas of each planes */
   239 			  
   240 	   for ( plane=0; plane < nbplanes; plane++ )
   241 		{
   242 		   ptr = MiniBuf + ( plane * bytesperline );
   243 	
   244 			remainingbytes = bytesperline;
   245 	
   246 			if ( bmhd.tcomp == 1 )	    /* Datas are compressed */
   247 			{
   248 			   do
   249 				{
   250 				   if ( !SDL_RWread( src, &count, 1, 1 ) )
   251 					{
   252 					   error="error reading BODY chunk";
   253 						goto done;
   254 					}
   255 	
   256 					if ( count & 0x80 )
   257 					{
   258 					   count ^= 0xFF;
   259 					   count += 2; /* now it */
   260 						
   261 						if ( !SDL_RWread( src, &color, 1, 1 ) )
   262 						{
   263 						   error="error reading BODY chunk";
   264 							goto done;
   265 						}
   266 						memset( ptr, color, count );
   267 					}
   268 					else
   269 					{
   270 					   ++count;
   271 
   272 						if ( !SDL_RWread( src, ptr, count, 1 ) )
   273 						{
   274 						   error="error reading BODY chunk";
   275 							goto done;
   276 						}
   277 					}
   278 	
   279 					ptr += count;
   280 					remainingbytes -= count;
   281 	
   282 				} while ( remainingbytes > 0 );
   283 			}
   284 			else	
   285 			{
   286 			   if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
   287 				{
   288 				   error="error reading BODY chunk";
   289 					goto done;
   290 				}
   291 			}
   292 		}
   293 	
   294 	   /* One line has been read, store it ! */
   295 
   296 		ptr = Image->pixels;
   297 		ptr += h * width;
   298 	
   299 		if ( pbm )                 /* File format : 'Packed Bitmap' */
   300 		{
   301 		   memcpy( ptr, MiniBuf, width );
   302 		}
   303 		else		/* We have to un-interlace the bits ! */
   304 		{
   305 		   size = ( width + 7 ) / 8;
   306 	
   307 			for ( i=0; i < size; i++ )
   308 			{
   309 			   memset( ptr, 0, 8 );
   310 	
   311 				for ( plane=0; plane < nbplanes; plane++ )
   312 				{
   313 				   color = *( MiniBuf + i + ( plane * bytesperline ) );
   314 					msk = 0x80;
   315 	
   316 					for ( j=0; j<8; j++ )
   317 					{
   318 					   if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
   319 						else 	                    ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
   320 	
   321 						msk >>= 1;
   322 					}
   323 				}
   324 				ptr += 8;
   325 			}
   326 		}
   327 	}
   328 
   329 done:
   330 
   331 	if ( MiniBuf ) free( MiniBuf );
   332 
   333 	if ( error ) 
   334 	{
   335 		IMG_SetError( error );
   336 	   SDL_FreeSurface( Image );
   337 		Image = NULL;
   338 	}
   339 
   340 	return( Image );
   341 }
   342 
   343 #else /* LOAD_LBM */
   344 
   345 /* See if an image is contained in a data source */
   346 int IMG_isLBM(SDL_RWops *src)
   347 {
   348 	return(0);
   349 }
   350 
   351 /* Load an IFF type image from an SDL datasource */
   352 SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src)
   353 {
   354 	return(NULL);
   355 }
   356 
   357 #endif /* LOAD_LBM */