IMG_lbm.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 19 Dec 2003 18:04:48 +0000
changeset 95 7fcb46931afc
parent 84 b0beb6215ea2
child 96 cada40fddd77
permissions -rw-r--r--
Date: Fri, 19 Dec 2003 18:47:46 +0200
From: "Mike Gorchak"
Subject: SDL image

I've fixed NULL pointer pass as RWops argument in all calls in the SDL_Image library, found that when porting toppler game to QNX6, they're do not checking any return pointers and then game failed in the SDL_image library internals, instead of fail in the toppler internals ;) And I've fixed the QNX6 .qpg file for automative package building.
     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    24 bits ILBM files support added by Marc Le Douarain (mavati@club-internet.fr)
    29    in December 2002.
    30 */
    31 
    32 #include <stdio.h>
    33 #include <stdlib.h>
    34 #include <string.h>
    35 
    36 #include "SDL_endian.h"
    37 #include "SDL_image.h"
    38 
    39 #ifdef LOAD_LBM
    40 
    41 
    42 #define MAXCOLORS 256
    43 
    44 /* Structure for an IFF picture ( BMHD = Bitmap Header ) */
    45 
    46 typedef struct
    47 {
    48     Uint16 w, h;		/* width & height of the bitmap in pixels */
    49     Sint16 x, y;		/* screen coordinates of the bitmap */
    50     Uint8 planes;		/* number of planes of the bitmap */
    51     Uint8 mask;			/* mask type ( 0 => no mask ) */
    52     Uint8 tcomp;		/* compression type */
    53     Uint8 pad1;			/* dummy value, for padding */
    54     Uint16 tcolor;		/* transparent color */
    55     Uint8 xAspect,		/* pixel aspect ratio */
    56          yAspect;
    57     Sint16  Lpage;		/* width of the screen in pixels */
    58     Sint16  Hpage;		/* height of the screen in pixels */
    59 } BMHD;
    60 
    61 int IMG_isLBM( SDL_RWops *src )
    62 {
    63 	int   is_LBM;
    64 	Uint8 magic[4+4+4];
    65 
    66 	is_LBM = 0;
    67 	if ( SDL_RWread( src, magic, 4+4+4, 1 ) )
    68 	{
    69 		if ( !memcmp( magic, "FORM", 4 ) &&
    70 			( !memcmp( magic + 8, "PBM ", 4 ) ||
    71 			  !memcmp( magic + 8, "ILBM", 4 ) ) )
    72 		{
    73 			is_LBM = 1;
    74 		}
    75 	}
    76 	return( is_LBM );
    77 }
    78 
    79 SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
    80 {
    81 	SDL_Surface *Image;
    82 	Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    83 	Uint32      size, bytesloaded, nbcolors;
    84 	Uint32      i, j, bytesperline, nbplanes, plane, h;
    85 	Uint32      remainingbytes;
    86 	Uint32      width;
    87 	BMHD	      bmhd;
    88 	char        *error;
    89 
    90 	Image   = NULL;
    91 	error   = NULL;
    92 	MiniBuf = NULL;
    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, bmhd.planes==24?24:8, 0, 0, 0, 0 ) ) == NULL )
   219 	   goto done;
   220 
   221 	/* Update palette informations */
   222 
   223 	/* There is no palette in 24 bits ILBM file */
   224 	if ( nbcolors>0 )
   225 	{
   226 		Image->format->palette->ncolors = nbcolors;
   227 
   228 		ptr = &colormap[0];
   229 
   230 		for ( i=0; i<nbcolors; i++ )
   231 		{
   232 			Image->format->palette->colors[i].r = *ptr++;
   233 			Image->format->palette->colors[i].g = *ptr++;
   234 			Image->format->palette->colors[i].b = *ptr++;
   235 		}
   236 	}
   237 
   238 	/* Get the bitmap */
   239 
   240 	for ( h=0; h < bmhd.h; h++ )
   241 	{
   242 		/* uncompress the datas of each planes */
   243 
   244 		for ( plane=0; plane < nbplanes; plane++ )
   245 		{
   246 			ptr = MiniBuf + ( plane * bytesperline );
   247 
   248 			remainingbytes = bytesperline;
   249 
   250 			if ( bmhd.tcomp == 1 )	    /* Datas are compressed */
   251 			{
   252 				do
   253 				{
   254 					if ( !SDL_RWread( src, &count, 1, 1 ) )
   255 					{
   256 						error="error reading BODY chunk";
   257 						goto done;
   258 					}
   259 
   260 					if ( count & 0x80 )
   261 					{
   262 						count ^= 0xFF;
   263 						count += 2; /* now it */
   264 
   265 						if ( !SDL_RWread( src, &color, 1, 1 ) )
   266 						{
   267 						   error="error reading BODY chunk";
   268 							goto done;
   269 						}
   270 						memset( ptr, color, count );
   271 					}
   272 					else
   273 					{
   274 						++count;
   275 
   276 						if ( !SDL_RWread( src, ptr, count, 1 ) )
   277 						{
   278 						   error="error reading BODY chunk";
   279 							goto done;
   280 						}
   281 					}
   282 
   283 					ptr += count;
   284 					remainingbytes -= count;
   285 
   286 				} while ( remainingbytes > 0 );
   287 			}
   288 			else
   289 			{
   290 				if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
   291 				{
   292 					error="error reading BODY chunk";
   293 					goto done;
   294 				}
   295 			}
   296 		}
   297 
   298 		/* One line has been read, store it ! */
   299 
   300 		ptr = Image->pixels;
   301 		if ( nbplanes==24 )
   302 			ptr += h * width * 3;
   303 		else
   304 			ptr += h * width;
   305 
   306 		if ( pbm )                 /* File format : 'Packed Bitmap' */
   307 		{
   308 		   memcpy( ptr, MiniBuf, width );
   309 		}
   310 		else		/* We have to un-interlace the bits ! */
   311 		{
   312 			if ( nbplanes!=24 )
   313 			{
   314 				size = ( width + 7 ) / 8;
   315 
   316 				for ( i=0; i < size; i++ )
   317 				{
   318 					memset( ptr, 0, 8 );
   319 
   320 					for ( plane=0; plane < nbplanes; plane++ )
   321 					{
   322 						color = *( MiniBuf + i + ( plane * bytesperline ) );
   323 						msk = 0x80;
   324 
   325 						for ( j=0; j<8; j++ )
   326 						{
   327 							if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
   328 							else 	                    ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
   329 
   330 							msk >>= 1;
   331 						}
   332 					}
   333 					ptr += 8;
   334 				}
   335 			}
   336 			else
   337 			{
   338 				size = ( width + 7 ) / 8;
   339 				/* 24 bitplanes ILBM : R0...R7,G0...G7,B0...B7 */
   340 				for ( i=0; i<width; i=i+8 )
   341 				{
   342 					Uint8 maskBit = 0x80;
   343 					for ( j=0; j<8; j++ )
   344 					{
   345 						Uint32 color24 = 0;
   346 						Uint32 maskColor24 = 1;
   347 						Uint8 dataBody;
   348 						for ( plane=0; plane < nbplanes; plane++ )
   349 						{
   350 							dataBody = MiniBuf[ plane*size+i/8 ];
   351 							if ( dataBody&maskBit )
   352 								color24 = color24 | maskColor24;
   353 							maskColor24 = maskColor24<<1;
   354 						}
   355 						if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
   356 						{
   357 							*ptr++ = color24>>16;
   358 							*ptr++ = color24>>8;
   359 							*ptr++ = color24;
   360 						}
   361 						else
   362 						{
   363 							*ptr++ = color24;
   364 							*ptr++ = color24>>8;
   365 							*ptr++ = color24>>16;
   366 						}
   367 
   368 						maskBit = maskBit>>1;
   369 					}
   370 				}
   371 			}
   372 		}
   373 	}
   374 
   375 done:
   376 
   377 	if ( MiniBuf ) free( MiniBuf );
   378 
   379 	if ( error )
   380 	{
   381 		IMG_SetError( error );
   382 		SDL_FreeSurface( Image );
   383 		Image = NULL;
   384 	}
   385 
   386 	return( Image );
   387 }
   388 
   389 #else /* LOAD_LBM */
   390 
   391 /* See if an image is contained in a data source */
   392 int IMG_isLBM(SDL_RWops *src)
   393 {
   394 	return(0);
   395 }
   396 
   397 /* Load an IFF type image from an SDL datasource */
   398 SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src)
   399 {
   400 	return(NULL);
   401 }
   402 
   403 #endif /* LOAD_LBM */