IMG_lbm.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 17:41:55 +0000
changeset 97 e1161bd417c4
parent 96 cada40fddd77
child 98 9f94c4674cc9
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999-2004 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    24 bits ILBM files support added by Marc Le Douarain (mavati AT club-internet
    29    POINT fr) in December 2002.
    30    EHB and HAM (specific Amiga graphic chip modes) support added by Marc Le Douarain
    31    (mavati AT club-internet POINT fr) in December 2003.
    32 */
    33 
    34 #include <stdio.h>
    35 #include <stdlib.h>
    36 #include <string.h>
    37 
    38 #include "SDL_endian.h"
    39 #include "SDL_image.h"
    40 
    41 #ifdef LOAD_LBM
    42 
    43 
    44 #define MAXCOLORS 256
    45 
    46 /* Structure for an IFF picture ( BMHD = Bitmap Header ) */
    47 
    48 typedef struct
    49 {
    50     Uint16 w, h;		/* width & height of the bitmap in pixels */
    51     Sint16 x, y;		/* screen coordinates of the bitmap */
    52     Uint8 planes;		/* number of planes of the bitmap */
    53     Uint8 mask;			/* mask type ( 0 => no mask ) */
    54     Uint8 tcomp;		/* compression type */
    55     Uint8 pad1;			/* dummy value, for padding */
    56     Uint16 tcolor;		/* transparent color */
    57     Uint8 xAspect,		/* pixel aspect ratio */
    58          yAspect;
    59     Sint16  Lpage;		/* width of the screen in pixels */
    60     Sint16  Hpage;		/* height of the screen in pixels */
    61 } BMHD;
    62 
    63 int IMG_isLBM( SDL_RWops *src )
    64 {
    65 	int   is_LBM;
    66 	Uint8 magic[4+4+4];
    67 
    68 	is_LBM = 0;
    69 	if ( SDL_RWread( src, magic, 4+4+4, 1 ) )
    70 	{
    71 		if ( !memcmp( magic, "FORM", 4 ) &&
    72 			( !memcmp( magic + 8, "PBM ", 4 ) ||
    73 			  !memcmp( magic + 8, "ILBM", 4 ) ) )
    74 		{
    75 			is_LBM = 1;
    76 		}
    77 	}
    78 	return( is_LBM );
    79 }
    80 
    81 SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
    82 {
    83 	SDL_Surface *Image;
    84 	Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    85 	Uint32      size, bytesloaded, nbcolors;
    86 	Uint32      i, j, bytesperline, nbplanes, plane, h;
    87 	Uint32      remainingbytes;
    88 	Uint32      width;
    89 	BMHD	      bmhd;
    90 	char        *error;
    91 	Uint8       flagHAM,flagEHB;
    92 
    93 	Image   = NULL;
    94 	error   = NULL;
    95 	MiniBuf = NULL;
    96 
    97 	if ( !SDL_RWread( src, id, 4, 1 ) )
    98 	{
    99 		error="error reading IFF chunk";
   100 		goto done;
   101 	}
   102 
   103 	/* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
   104 	if ( !SDL_RWread( src, &size, 4, 1 ) )
   105 	{
   106 		error="error reading IFF chunk size";
   107 		goto done;
   108 	}
   109 
   110 	/* As size is not used here, no need to swap it */
   111 
   112 	if ( memcmp( id, "FORM", 4 ) != 0 )
   113 	{
   114 		error="not a IFF file";
   115 		goto done;
   116 	}
   117 
   118 	if ( !SDL_RWread( src, id, 4, 1 ) )
   119 	{
   120 		error="error reading IFF chunk";
   121 		goto done;
   122 	}
   123 
   124 	pbm = 0;
   125 
   126 	/* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
   127 	if ( !memcmp( id, "PBM ", 4 ) ) pbm = 1;
   128 	else if ( memcmp( id, "ILBM", 4 ) )
   129 	{
   130 		error="not a IFF picture";
   131 		goto done;
   132 	}
   133 
   134 	nbcolors = 0;
   135 
   136 	memset( &bmhd, 0, sizeof( BMHD ) );
   137 	flagHAM = 0;
   138 	flagEHB = 0;
   139 
   140 	while ( memcmp( id, "BODY", 4 ) != 0 )
   141 	{
   142 		if ( !SDL_RWread( src, id, 4, 1 ) ) 
   143 		{
   144 			error="error reading IFF chunk";
   145 			goto done;
   146 		}
   147 
   148 		if ( !SDL_RWread( src, &size, 4, 1 ) )
   149 		{
   150 			error="error reading IFF chunk size";
   151 			goto done;
   152 		}
   153 
   154 		bytesloaded = 0;
   155 
   156 		size = SDL_SwapBE32( size );
   157 
   158 		if ( !memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
   159 		{
   160 			if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
   161 			{
   162 				error="error reading BMHD chunk";
   163 				goto done;
   164 			}
   165 
   166 			bytesloaded = sizeof( BMHD );
   167 
   168 			bmhd.w 		= SDL_SwapBE16( bmhd.w );
   169 			bmhd.h 		= SDL_SwapBE16( bmhd.h );
   170 			bmhd.x 		= SDL_SwapBE16( bmhd.x );
   171 			bmhd.y 		= SDL_SwapBE16( bmhd.y );
   172 			bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
   173 			bmhd.Lpage 	= SDL_SwapBE16( bmhd.Lpage );
   174 			bmhd.Hpage 	= SDL_SwapBE16( bmhd.Hpage );
   175 		}
   176 
   177 		if ( !memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
   178 		{
   179 			if ( !SDL_RWread( src, &colormap, size, 1 ) )
   180 			{
   181 				error="error reading CMAP chunk";
   182 				goto done;
   183 			}
   184 
   185 			bytesloaded = size;
   186 			nbcolors = size / 3;
   187 		}
   188 
   189 		if ( !memcmp( id, "CAMG", 4 ) ) /* Amiga ViewMode  */
   190 		{
   191 			Uint32 viewmodes;
   192 			if ( !SDL_RWread( src, &viewmodes, sizeof(viewmodes), 1 ) )
   193 			{
   194 				error="error reading CAMG chunk";
   195 				goto done;
   196 			}
   197 
   198 			bytesloaded = size;
   199 			viewmodes = SDL_SwapBE32( viewmodes );
   200 			if ( viewmodes & 0x0800 )
   201 				flagHAM = 1;
   202 			if ( viewmodes & 0x0080 )
   203 				flagEHB = 1;
   204 		}
   205 
   206 		if ( memcmp( id, "BODY", 4 ) )
   207 		{
   208 			if ( size & 1 )	++size;  	/* padding ! */
   209 			size -= bytesloaded;
   210 			/* skip the remaining bytes of this chunk */
   211 			if ( size )	SDL_RWseek( src, size, SEEK_CUR );
   212 		}
   213 	}
   214 
   215 	/* compute some usefull values, based on the bitmap header */
   216 
   217 	width = ( bmhd.w + 15 ) & 0xFFFFFFF0;  /* Width in pixels modulo 16 */
   218 
   219 	bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
   220 
   221 	nbplanes = bmhd.planes;
   222 
   223 	if ( pbm )                         /* File format : 'Packed Bitmap' */
   224 	{
   225 		bytesperline *= 8;
   226 		nbplanes = 1;
   227 	}
   228 
   229 	if ( bmhd.mask ) ++nbplanes;       /* There is a mask ( 'stencil' ) */
   230 
   231 	/* Allocate memory for a temporary buffer ( used for
   232            decompression/deinterleaving ) */
   233 
   234 	if ( ( MiniBuf = (void *)malloc( bytesperline * nbplanes ) ) == NULL )
   235 	{
   236 		error="no enough memory for temporary buffer";
   237 		goto done;
   238 	}
   239 
   240 	if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, (bmhd.planes==24 || flagHAM==1)?24:8, 0, 0, 0, 0 ) ) == NULL )
   241 	   goto done;
   242 
   243 	/* Update palette informations */
   244 
   245 	/* There is no palette in 24 bits ILBM file */
   246 	if ( nbcolors>0 && flagHAM==0 )
   247 	{
   248 		Image->format->palette->ncolors = nbcolors;
   249 
   250 		ptr = &colormap[0];
   251 
   252 		for ( i=0; i<nbcolors; i++ )
   253 		{
   254 			Image->format->palette->colors[i].r = *ptr++;
   255 			Image->format->palette->colors[i].g = *ptr++;
   256 			Image->format->palette->colors[i].b = *ptr++;
   257 		}
   258 
   259 		/* Amiga EHB mode (Extra-Half-Bright) */
   260 		/* 6 bitplanes mode with a 32 colors palette */
   261 		/* The 32 last colors are the same but divided by 2 */
   262 		/* Some Amiga pictures save 64 colors with 32 last wrong colors,
   263 		/* they shouldn't !, and here we overwrite these 32 bad colors. */
   264 		if ( (nbcolors==32 || flagEHB ) && (1<<bmhd.planes)==64 )
   265 		{
   266 			Image->format->palette->ncolors = 64;
   267 			ptr = &colormap[0];
   268 			for ( i=32; i<64; i++ )
   269 			{
   270 				Image->format->palette->colors[i].r = (*ptr++)/2;
   271 				Image->format->palette->colors[i].g = (*ptr++)/2;
   272 				Image->format->palette->colors[i].b = (*ptr++)/2;
   273 			}
   274 		}
   275 	}
   276 
   277 	/* Get the bitmap */
   278 
   279 	for ( h=0; h < bmhd.h; h++ )
   280 	{
   281 		/* uncompress the datas of each planes */
   282 
   283 		for ( plane=0; plane < nbplanes; plane++ )
   284 		{
   285 			ptr = MiniBuf + ( plane * bytesperline );
   286 
   287 			remainingbytes = bytesperline;
   288 
   289 			if ( bmhd.tcomp == 1 )	    /* Datas are compressed */
   290 			{
   291 				do
   292 				{
   293 					if ( !SDL_RWread( src, &count, 1, 1 ) )
   294 					{
   295 						error="error reading BODY chunk";
   296 						goto done;
   297 					}
   298 
   299 					if ( count & 0x80 )
   300 					{
   301 						count ^= 0xFF;
   302 						count += 2; /* now it */
   303 
   304 						if ( !SDL_RWread( src, &color, 1, 1 ) )
   305 						{
   306 						   error="error reading BODY chunk";
   307 							goto done;
   308 						}
   309 						memset( ptr, color, count );
   310 					}
   311 					else
   312 					{
   313 						++count;
   314 
   315 						if ( !SDL_RWread( src, ptr, count, 1 ) )
   316 						{
   317 						   error="error reading BODY chunk";
   318 							goto done;
   319 						}
   320 					}
   321 
   322 					ptr += count;
   323 					remainingbytes -= count;
   324 
   325 				} while ( remainingbytes > 0 );
   326 			}
   327 			else
   328 			{
   329 				if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
   330 				{
   331 					error="error reading BODY chunk";
   332 					goto done;
   333 				}
   334 			}
   335 		}
   336 
   337 		/* One line has been read, store it ! */
   338 
   339 		ptr = Image->pixels;
   340 		if ( nbplanes==24 || flagHAM==1 )
   341 			ptr += h * width * 3;
   342 		else
   343 			ptr += h * width;
   344 
   345 		if ( pbm )                 /* File format : 'Packed Bitmap' */
   346 		{
   347 		   memcpy( ptr, MiniBuf, width );
   348 		}
   349 		else		/* We have to un-interlace the bits ! */
   350 		{
   351 			if ( nbplanes!=24 && flagHAM==0 )
   352 			{
   353 				size = ( width + 7 ) / 8;
   354 
   355 				for ( i=0; i < size; i++ )
   356 				{
   357 					memset( ptr, 0, 8 );
   358 
   359 					for ( plane=0; plane < nbplanes; plane++ )
   360 					{
   361 						color = *( MiniBuf + i + ( plane * bytesperline ) );
   362 						msk = 0x80;
   363 
   364 						for ( j=0; j<8; j++ )
   365 						{
   366 							if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
   367 							else 	                    ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
   368 
   369 							msk >>= 1;
   370 						}
   371 					}
   372 					ptr += 8;
   373 				}
   374 			}
   375 			else
   376 			{
   377 				Uint32 finalcolor = 0;
   378 				size = ( width + 7 ) / 8;
   379 				/* 24 bitplanes ILBM : R0...R7,G0...G7,B0...B7 */
   380 				/* or HAM (6 bitplanes) or HAM8 (8 bitplanes) modes */
   381 				for ( i=0; i<width; i=i+8 )
   382 				{
   383 					Uint8 maskBit = 0x80;
   384 					for ( j=0; j<8; j++ )
   385 					{
   386 						Uint32 pixelcolor = 0;
   387 						Uint32 maskColor = 1;
   388 						Uint8 dataBody;
   389 						for ( plane=0; plane < nbplanes; plane++ )
   390 						{
   391 							dataBody = MiniBuf[ plane*size+i/8 ];
   392 							if ( dataBody&maskBit )
   393 								pixelcolor = pixelcolor | maskColor;
   394 							maskColor = maskColor<<1;
   395 						}
   396 						/* HAM : 12 bits RGB image (4 bits per color component) */
   397 						/* HAM8 : 18 bits RGB image (6 bits per color component) */
   398 						if ( flagHAM )
   399 						{
   400 							switch( pixelcolor>>(nbplanes-2) )
   401 							{
   402 								case 0: /* take direct color from palette */
   403 									finalcolor = colormap[ pixelcolor*3 ] + (colormap[ pixelcolor*3+1 ]<<8) + (colormap[ pixelcolor*3+2 ]<<16);
   404 									break;
   405 								case 1: /* modify only blue component */
   406 									finalcolor = finalcolor&0x00FFFF;
   407 									finalcolor = finalcolor | (pixelcolor<<(16+(10-nbplanes)));
   408 									break;
   409 								case 2: /* modify only red component */
   410 									finalcolor = finalcolor&0xFFFF00;
   411 									finalcolor = finalcolor | pixelcolor<<(10-nbplanes);
   412 									break;
   413 								case 3: /* modify only green component */
   414 									finalcolor = finalcolor&0xFF00FF;
   415 									finalcolor = finalcolor | (pixelcolor<<(8+(10-nbplanes)));
   416 									break;
   417 							}
   418 						}
   419 						else
   420 						{
   421 							finalcolor = pixelcolor;
   422 						}
   423 						if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
   424 						{
   425 							*ptr++ = finalcolor>>16;
   426 							*ptr++ = finalcolor>>8;
   427 							*ptr++ = finalcolor;
   428 						}
   429 						else
   430 						{
   431 							*ptr++ = finalcolor;
   432 							*ptr++ = finalcolor>>8;
   433 							*ptr++ = finalcolor>>16;
   434 						}
   435 
   436 						maskBit = maskBit>>1;
   437 					}
   438 				}
   439 			}
   440 		}
   441 	}
   442 
   443 done:
   444 
   445 	if ( MiniBuf ) free( MiniBuf );
   446 
   447 	if ( error )
   448 	{
   449 		IMG_SetError( error );
   450 		SDL_FreeSurface( Image );
   451 		Image = NULL;
   452 	}
   453 
   454 	return( Image );
   455 }
   456 
   457 #else /* LOAD_LBM */
   458 
   459 /* See if an image is contained in a data source */
   460 int IMG_isLBM(SDL_RWops *src)
   461 {
   462 	return(0);
   463 }
   464 
   465 /* Load an IFF type image from an SDL datasource */
   466 SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src)
   467 {
   468 	return(NULL);
   469 }
   470 
   471 #endif /* LOAD_LBM */