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