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