IMG_lbm.c
changeset 368 8a61842d00ce
parent 367 b2aa197f6774
child 415 1700d607fce3
     1.1 --- a/IMG_lbm.c	Thu Apr 25 00:22:51 2013 -0700
     1.2 +++ b/IMG_lbm.c	Tue May 21 21:24:32 2013 -0700
     1.3 @@ -46,440 +46,440 @@
     1.4  
     1.5  typedef struct
     1.6  {
     1.7 -    Uint16 w, h;		/* width & height of the bitmap in pixels */
     1.8 -    Sint16 x, y;		/* screen coordinates of the bitmap */
     1.9 -    Uint8 planes;		/* number of planes of the bitmap */
    1.10 -    Uint8 mask;			/* mask type ( 0 => no mask ) */
    1.11 -    Uint8 tcomp;		/* compression type */
    1.12 -    Uint8 pad1;			/* dummy value, for padding */
    1.13 -    Uint16 tcolor;		/* transparent color */
    1.14 -    Uint8 xAspect,		/* pixel aspect ratio */
    1.15 +    Uint16 w, h;        /* width & height of the bitmap in pixels */
    1.16 +    Sint16 x, y;        /* screen coordinates of the bitmap */
    1.17 +    Uint8 planes;       /* number of planes of the bitmap */
    1.18 +    Uint8 mask;         /* mask type ( 0 => no mask ) */
    1.19 +    Uint8 tcomp;        /* compression type */
    1.20 +    Uint8 pad1;         /* dummy value, for padding */
    1.21 +    Uint16 tcolor;      /* transparent color */
    1.22 +    Uint8 xAspect,      /* pixel aspect ratio */
    1.23            yAspect;
    1.24 -    Sint16  Lpage;		/* width of the screen in pixels */
    1.25 -    Sint16  Hpage;		/* height of the screen in pixels */
    1.26 +    Sint16  Lpage;      /* width of the screen in pixels */
    1.27 +    Sint16  Hpage;      /* height of the screen in pixels */
    1.28  } BMHD;
    1.29  
    1.30  int IMG_isLBM( SDL_RWops *src )
    1.31  {
    1.32 -	Sint64 start;
    1.33 -	int   is_LBM;
    1.34 -	Uint8 magic[4+4+4];
    1.35 +    Sint64 start;
    1.36 +    int   is_LBM;
    1.37 +    Uint8 magic[4+4+4];
    1.38  
    1.39 -	if ( !src ) 
    1.40 -		return 0;
    1.41 -	start = SDL_RWtell(src);
    1.42 -	is_LBM = 0;
    1.43 -	if ( SDL_RWread( src, magic, sizeof(magic), 1 ) )
    1.44 -	{
    1.45 -		if ( !SDL_memcmp( magic, "FORM", 4 ) &&
    1.46 -			( !SDL_memcmp( magic + 8, "PBM ", 4 ) ||
    1.47 -			  !SDL_memcmp( magic + 8, "ILBM", 4 ) ) )
    1.48 -		{
    1.49 -			is_LBM = 1;
    1.50 -		}
    1.51 -	}
    1.52 -	SDL_RWseek(src, start, RW_SEEK_SET);
    1.53 -	return( is_LBM );
    1.54 +    if ( !src )
    1.55 +        return 0;
    1.56 +    start = SDL_RWtell(src);
    1.57 +    is_LBM = 0;
    1.58 +    if ( SDL_RWread( src, magic, sizeof(magic), 1 ) )
    1.59 +    {
    1.60 +        if ( !SDL_memcmp( magic, "FORM", 4 ) &&
    1.61 +            ( !SDL_memcmp( magic + 8, "PBM ", 4 ) ||
    1.62 +              !SDL_memcmp( magic + 8, "ILBM", 4 ) ) )
    1.63 +        {
    1.64 +            is_LBM = 1;
    1.65 +        }
    1.66 +    }
    1.67 +    SDL_RWseek(src, start, RW_SEEK_SET);
    1.68 +    return( is_LBM );
    1.69  }
    1.70  
    1.71  SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
    1.72  {
    1.73 -	Sint64 start;
    1.74 -	SDL_Surface *Image;
    1.75 -	Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    1.76 -	Uint32      size, bytesloaded, nbcolors;
    1.77 -	Uint32      i, j, bytesperline, nbplanes, stencil, plane, h;
    1.78 -	Uint32      remainingbytes;
    1.79 -	Uint32      width;
    1.80 -	BMHD	      bmhd;
    1.81 -	char        *error;
    1.82 -	Uint8       flagHAM,flagEHB;
    1.83 +    Sint64 start;
    1.84 +    SDL_Surface *Image;
    1.85 +    Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    1.86 +    Uint32      size, bytesloaded, nbcolors;
    1.87 +    Uint32      i, j, bytesperline, nbplanes, stencil, plane, h;
    1.88 +    Uint32      remainingbytes;
    1.89 +    Uint32      width;
    1.90 +    BMHD          bmhd;
    1.91 +    char        *error;
    1.92 +    Uint8       flagHAM,flagEHB;
    1.93  
    1.94 -	Image   = NULL;
    1.95 -	error   = NULL;
    1.96 -	MiniBuf = NULL;
    1.97 +    Image   = NULL;
    1.98 +    error   = NULL;
    1.99 +    MiniBuf = NULL;
   1.100  
   1.101 -	if ( !src ) {
   1.102 -		/* The error message has been set in SDL_RWFromFile */
   1.103 -		return NULL;
   1.104 -	}
   1.105 -	start = SDL_RWtell(src);
   1.106 +    if ( !src ) {
   1.107 +        /* The error message has been set in SDL_RWFromFile */
   1.108 +        return NULL;
   1.109 +    }
   1.110 +    start = SDL_RWtell(src);
   1.111  
   1.112 -	if ( !SDL_RWread( src, id, 4, 1 ) )
   1.113 -	{
   1.114 -		error="error reading IFF chunk";
   1.115 -		goto done;
   1.116 -	}
   1.117 +    if ( !SDL_RWread( src, id, 4, 1 ) )
   1.118 +    {
   1.119 +        error="error reading IFF chunk";
   1.120 +        goto done;
   1.121 +    }
   1.122  
   1.123 -	/* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
   1.124 -	if ( !SDL_RWread( src, &size, 4, 1 ) )
   1.125 -	{
   1.126 -		error="error reading IFF chunk size";
   1.127 -		goto done;
   1.128 -	}
   1.129 +    /* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
   1.130 +    if ( !SDL_RWread( src, &size, 4, 1 ) )
   1.131 +    {
   1.132 +        error="error reading IFF chunk size";
   1.133 +        goto done;
   1.134 +    }
   1.135  
   1.136 -	/* As size is not used here, no need to swap it */
   1.137 +    /* As size is not used here, no need to swap it */
   1.138  
   1.139 -	if ( memcmp( id, "FORM", 4 ) != 0 )
   1.140 -	{
   1.141 -		error="not a IFF file";
   1.142 -		goto done;
   1.143 -	}
   1.144 +    if ( memcmp( id, "FORM", 4 ) != 0 )
   1.145 +    {
   1.146 +        error="not a IFF file";
   1.147 +        goto done;
   1.148 +    }
   1.149  
   1.150 -	if ( !SDL_RWread( src, id, 4, 1 ) )
   1.151 -	{
   1.152 -		error="error reading IFF chunk";
   1.153 -		goto done;
   1.154 -	}
   1.155 +    if ( !SDL_RWread( src, id, 4, 1 ) )
   1.156 +    {
   1.157 +        error="error reading IFF chunk";
   1.158 +        goto done;
   1.159 +    }
   1.160  
   1.161 -	pbm = 0;
   1.162 +    pbm = 0;
   1.163  
   1.164 -	/* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
   1.165 -	if ( !SDL_memcmp( id, "PBM ", 4 ) ) pbm = 1;
   1.166 -	else if ( SDL_memcmp( id, "ILBM", 4 ) )
   1.167 -	{
   1.168 -		error="not a IFF picture";
   1.169 -		goto done;
   1.170 -	}
   1.171 +    /* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
   1.172 +    if ( !SDL_memcmp( id, "PBM ", 4 ) ) pbm = 1;
   1.173 +    else if ( SDL_memcmp( id, "ILBM", 4 ) )
   1.174 +    {
   1.175 +        error="not a IFF picture";
   1.176 +        goto done;
   1.177 +    }
   1.178  
   1.179 -	nbcolors = 0;
   1.180 +    nbcolors = 0;
   1.181  
   1.182 -	SDL_memset( &bmhd, 0, sizeof( BMHD ) );
   1.183 -	flagHAM = 0;
   1.184 -	flagEHB = 0;
   1.185 +    SDL_memset( &bmhd, 0, sizeof( BMHD ) );
   1.186 +    flagHAM = 0;
   1.187 +    flagEHB = 0;
   1.188  
   1.189 -	while ( SDL_memcmp( id, "BODY", 4 ) != 0 )
   1.190 -	{
   1.191 -		if ( !SDL_RWread( src, id, 4, 1 ) ) 
   1.192 -		{
   1.193 -			error="error reading IFF chunk";
   1.194 -			goto done;
   1.195 -		}
   1.196 +    while ( SDL_memcmp( id, "BODY", 4 ) != 0 )
   1.197 +    {
   1.198 +        if ( !SDL_RWread( src, id, 4, 1 ) )
   1.199 +        {
   1.200 +            error="error reading IFF chunk";
   1.201 +            goto done;
   1.202 +        }
   1.203  
   1.204 -		if ( !SDL_RWread( src, &size, 4, 1 ) )
   1.205 -		{
   1.206 -			error="error reading IFF chunk size";
   1.207 -			goto done;
   1.208 -		}
   1.209 +        if ( !SDL_RWread( src, &size, 4, 1 ) )
   1.210 +        {
   1.211 +            error="error reading IFF chunk size";
   1.212 +            goto done;
   1.213 +        }
   1.214  
   1.215 -		bytesloaded = 0;
   1.216 +        bytesloaded = 0;
   1.217  
   1.218 -		size = SDL_SwapBE32( size );
   1.219 +        size = SDL_SwapBE32( size );
   1.220  
   1.221 -		if ( !SDL_memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
   1.222 -		{
   1.223 -			if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
   1.224 -			{
   1.225 -				error="error reading BMHD chunk";
   1.226 -				goto done;
   1.227 -			}
   1.228 +        if ( !SDL_memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
   1.229 +        {
   1.230 +            if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
   1.231 +            {
   1.232 +                error="error reading BMHD chunk";
   1.233 +                goto done;
   1.234 +            }
   1.235  
   1.236 -			bytesloaded = sizeof( BMHD );
   1.237 +            bytesloaded = sizeof( BMHD );
   1.238  
   1.239 -			bmhd.w 		= SDL_SwapBE16( bmhd.w );
   1.240 -			bmhd.h 		= SDL_SwapBE16( bmhd.h );
   1.241 -			bmhd.x 		= SDL_SwapBE16( bmhd.x );
   1.242 -			bmhd.y 		= SDL_SwapBE16( bmhd.y );
   1.243 -			bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
   1.244 -			bmhd.Lpage 	= SDL_SwapBE16( bmhd.Lpage );
   1.245 -			bmhd.Hpage 	= SDL_SwapBE16( bmhd.Hpage );
   1.246 -		}
   1.247 +            bmhd.w      = SDL_SwapBE16( bmhd.w );
   1.248 +            bmhd.h      = SDL_SwapBE16( bmhd.h );
   1.249 +            bmhd.x      = SDL_SwapBE16( bmhd.x );
   1.250 +            bmhd.y      = SDL_SwapBE16( bmhd.y );
   1.251 +            bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
   1.252 +            bmhd.Lpage  = SDL_SwapBE16( bmhd.Lpage );
   1.253 +            bmhd.Hpage  = SDL_SwapBE16( bmhd.Hpage );
   1.254 +        }
   1.255  
   1.256 -		if ( !SDL_memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
   1.257 -		{
   1.258 -			if ( !SDL_RWread( src, &colormap, size, 1 ) )
   1.259 -			{
   1.260 -				error="error reading CMAP chunk";
   1.261 -				goto done;
   1.262 -			}
   1.263 +        if ( !SDL_memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
   1.264 +        {
   1.265 +            if ( !SDL_RWread( src, &colormap, size, 1 ) )
   1.266 +            {
   1.267 +                error="error reading CMAP chunk";
   1.268 +                goto done;
   1.269 +            }
   1.270  
   1.271 -			bytesloaded = size;
   1.272 -			nbcolors = size / 3;
   1.273 -		}
   1.274 +            bytesloaded = size;
   1.275 +            nbcolors = size / 3;
   1.276 +        }
   1.277  
   1.278 -		if ( !memcmp( id, "CAMG", 4 ) ) /* Amiga ViewMode  */
   1.279 -		{
   1.280 -			Uint32 viewmodes;
   1.281 -			if ( !SDL_RWread( src, &viewmodes, sizeof(viewmodes), 1 ) )
   1.282 -			{
   1.283 -				error="error reading CAMG chunk";
   1.284 -				goto done;
   1.285 -			}
   1.286 +        if ( !memcmp( id, "CAMG", 4 ) ) /* Amiga ViewMode  */
   1.287 +        {
   1.288 +            Uint32 viewmodes;
   1.289 +            if ( !SDL_RWread( src, &viewmodes, sizeof(viewmodes), 1 ) )
   1.290 +            {
   1.291 +                error="error reading CAMG chunk";
   1.292 +                goto done;
   1.293 +            }
   1.294  
   1.295 -			bytesloaded = size;
   1.296 -			viewmodes = SDL_SwapBE32( viewmodes );
   1.297 -			if ( viewmodes & 0x0800 )
   1.298 -				flagHAM = 1;
   1.299 -			if ( viewmodes & 0x0080 )
   1.300 -				flagEHB = 1;
   1.301 -		}
   1.302 +            bytesloaded = size;
   1.303 +            viewmodes = SDL_SwapBE32( viewmodes );
   1.304 +            if ( viewmodes & 0x0800 )
   1.305 +                flagHAM = 1;
   1.306 +            if ( viewmodes & 0x0080 )
   1.307 +                flagEHB = 1;
   1.308 +        }
   1.309  
   1.310 -		if ( SDL_memcmp( id, "BODY", 4 ) )
   1.311 -		{
   1.312 -			if ( size & 1 )	++size;  	/* padding ! */
   1.313 -			size -= bytesloaded;
   1.314 -			/* skip the remaining bytes of this chunk */
   1.315 -			if ( size )	SDL_RWseek( src, size, RW_SEEK_CUR );
   1.316 -		}
   1.317 -	}
   1.318 +        if ( SDL_memcmp( id, "BODY", 4 ) )
   1.319 +        {
   1.320 +            if ( size & 1 ) ++size;     /* padding ! */
   1.321 +            size -= bytesloaded;
   1.322 +            /* skip the remaining bytes of this chunk */
   1.323 +            if ( size ) SDL_RWseek( src, size, RW_SEEK_CUR );
   1.324 +        }
   1.325 +    }
   1.326  
   1.327 -	/* compute some usefull values, based on the bitmap header */
   1.328 +    /* compute some usefull values, based on the bitmap header */
   1.329  
   1.330 -	width = ( bmhd.w + 15 ) & 0xFFFFFFF0;  /* Width in pixels modulo 16 */
   1.331 +    width = ( bmhd.w + 15 ) & 0xFFFFFFF0;  /* Width in pixels modulo 16 */
   1.332  
   1.333 -	bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
   1.334 +    bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
   1.335  
   1.336 -	nbplanes = bmhd.planes;
   1.337 +    nbplanes = bmhd.planes;
   1.338  
   1.339 -	if ( pbm )                         /* File format : 'Packed Bitmap' */
   1.340 -	{
   1.341 -		bytesperline *= 8;
   1.342 -		nbplanes = 1;
   1.343 -	}
   1.344 +    if ( pbm )                         /* File format : 'Packed Bitmap' */
   1.345 +    {
   1.346 +        bytesperline *= 8;
   1.347 +        nbplanes = 1;
   1.348 +    }
   1.349  
   1.350 -	stencil = (bmhd.mask & 1);   /* There is a mask ( 'stencil' ) */
   1.351 +    stencil = (bmhd.mask & 1);   /* There is a mask ( 'stencil' ) */
   1.352  
   1.353 -	/* Allocate memory for a temporary buffer ( used for
   1.354 +    /* Allocate memory for a temporary buffer ( used for
   1.355             decompression/deinterleaving ) */
   1.356  
   1.357 -	MiniBuf = (Uint8 *)SDL_malloc( bytesperline * (nbplanes + stencil) );
   1.358 -	if ( MiniBuf == NULL )
   1.359 -	{
   1.360 -		error="no enough memory for temporary buffer";
   1.361 -		goto done;
   1.362 -	}
   1.363 +    MiniBuf = (Uint8 *)SDL_malloc( bytesperline * (nbplanes + stencil) );
   1.364 +    if ( MiniBuf == NULL )
   1.365 +    {
   1.366 +        error="no enough memory for temporary buffer";
   1.367 +        goto done;
   1.368 +    }
   1.369  
   1.370 -	if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, (bmhd.planes==24 || flagHAM==1)?24:8, 0, 0, 0, 0 ) ) == NULL )
   1.371 -	   goto done;
   1.372 +    if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, (bmhd.planes==24 || flagHAM==1)?24:8, 0, 0, 0, 0 ) ) == NULL )
   1.373 +       goto done;
   1.374  
   1.375 -	if ( bmhd.mask & 2 )               /* There is a transparent color */
   1.376 -		SDL_SetColorKey( Image, SDL_TRUE, bmhd.tcolor );
   1.377 +    if ( bmhd.mask & 2 )               /* There is a transparent color */
   1.378 +        SDL_SetColorKey( Image, SDL_TRUE, bmhd.tcolor );
   1.379  
   1.380 -	/* Update palette informations */
   1.381 +    /* Update palette informations */
   1.382  
   1.383 -	/* There is no palette in 24 bits ILBM file */
   1.384 -	if ( nbcolors>0 && flagHAM==0 )
   1.385 -	{
   1.386 -		/* FIXME: Should this include the stencil? See comment below */
   1.387 -		int nbrcolorsfinal = 1 << (nbplanes + stencil);
   1.388 -		ptr = &colormap[0];
   1.389 +    /* There is no palette in 24 bits ILBM file */
   1.390 +    if ( nbcolors>0 && flagHAM==0 )
   1.391 +    {
   1.392 +        /* FIXME: Should this include the stencil? See comment below */
   1.393 +        int nbrcolorsfinal = 1 << (nbplanes + stencil);
   1.394 +        ptr = &colormap[0];
   1.395  
   1.396 -		for ( i=0; i<nbcolors; i++ )
   1.397 -		{
   1.398 -			Image->format->palette->colors[i].r = *ptr++;
   1.399 -			Image->format->palette->colors[i].g = *ptr++;
   1.400 -			Image->format->palette->colors[i].b = *ptr++;
   1.401 -		}
   1.402 +        for ( i=0; i<nbcolors; i++ )
   1.403 +        {
   1.404 +            Image->format->palette->colors[i].r = *ptr++;
   1.405 +            Image->format->palette->colors[i].g = *ptr++;
   1.406 +            Image->format->palette->colors[i].b = *ptr++;
   1.407 +        }
   1.408  
   1.409 -		/* Amiga EHB mode (Extra-Half-Bright) */
   1.410 -		/* 6 bitplanes mode with a 32 colors palette */
   1.411 -		/* The 32 last colors are the same but divided by 2 */
   1.412 -		/* Some Amiga pictures save 64 colors with 32 last wrong colors, */
   1.413 -		/* they shouldn't !, and here we overwrite these 32 bad colors. */
   1.414 -		if ( (nbcolors==32 || flagEHB ) && (1<<bmhd.planes)==64 )
   1.415 -		{
   1.416 -			nbcolors = 64;
   1.417 -			ptr = &colormap[0];
   1.418 -			for ( i=32; i<64; i++ )
   1.419 -			{
   1.420 -				Image->format->palette->colors[i].r = (*ptr++)/2;
   1.421 -				Image->format->palette->colors[i].g = (*ptr++)/2;
   1.422 -				Image->format->palette->colors[i].b = (*ptr++)/2;
   1.423 -			}
   1.424 -		}
   1.425 +        /* Amiga EHB mode (Extra-Half-Bright) */
   1.426 +        /* 6 bitplanes mode with a 32 colors palette */
   1.427 +        /* The 32 last colors are the same but divided by 2 */
   1.428 +        /* Some Amiga pictures save 64 colors with 32 last wrong colors, */
   1.429 +        /* they shouldn't !, and here we overwrite these 32 bad colors. */
   1.430 +        if ( (nbcolors==32 || flagEHB ) && (1<<bmhd.planes)==64 )
   1.431 +        {
   1.432 +            nbcolors = 64;
   1.433 +            ptr = &colormap[0];
   1.434 +            for ( i=32; i<64; i++ )
   1.435 +            {
   1.436 +                Image->format->palette->colors[i].r = (*ptr++)/2;
   1.437 +                Image->format->palette->colors[i].g = (*ptr++)/2;
   1.438 +                Image->format->palette->colors[i].b = (*ptr++)/2;
   1.439 +            }
   1.440 +        }
   1.441  
   1.442 -		/* If nbcolors < 2^nbplanes, repeat the colormap */
   1.443 -		/* This happens when pictures have a stencil mask */
   1.444 -		if ( nbrcolorsfinal > (1<<bmhd.planes) ) {
   1.445 -			nbrcolorsfinal = (1<<bmhd.planes);
   1.446 -		}
   1.447 -		for ( i=nbcolors; i < (Uint32)nbrcolorsfinal; i++ )
   1.448 -		{
   1.449 -			Image->format->palette->colors[i].r = Image->format->palette->colors[i%nbcolors].r;
   1.450 -			Image->format->palette->colors[i].g = Image->format->palette->colors[i%nbcolors].g;
   1.451 -			Image->format->palette->colors[i].b = Image->format->palette->colors[i%nbcolors].b;
   1.452 -		}
   1.453 -		if ( !pbm )
   1.454 -			Image->format->palette->ncolors = nbrcolorsfinal;
   1.455 -	}
   1.456 +        /* If nbcolors < 2^nbplanes, repeat the colormap */
   1.457 +        /* This happens when pictures have a stencil mask */
   1.458 +        if ( nbrcolorsfinal > (1<<bmhd.planes) ) {
   1.459 +            nbrcolorsfinal = (1<<bmhd.planes);
   1.460 +        }
   1.461 +        for ( i=nbcolors; i < (Uint32)nbrcolorsfinal; i++ )
   1.462 +        {
   1.463 +            Image->format->palette->colors[i].r = Image->format->palette->colors[i%nbcolors].r;
   1.464 +            Image->format->palette->colors[i].g = Image->format->palette->colors[i%nbcolors].g;
   1.465 +            Image->format->palette->colors[i].b = Image->format->palette->colors[i%nbcolors].b;
   1.466 +        }
   1.467 +        if ( !pbm )
   1.468 +            Image->format->palette->ncolors = nbrcolorsfinal;
   1.469 +    }
   1.470  
   1.471 -	/* Get the bitmap */
   1.472 +    /* Get the bitmap */
   1.473  
   1.474 -	for ( h=0; h < bmhd.h; h++ )
   1.475 -	{
   1.476 -		/* uncompress the datas of each planes */
   1.477 +    for ( h=0; h < bmhd.h; h++ )
   1.478 +    {
   1.479 +        /* uncompress the datas of each planes */
   1.480  
   1.481 -		for ( plane=0; plane < (nbplanes+stencil); plane++ )
   1.482 -		{
   1.483 -			ptr = MiniBuf + ( plane * bytesperline );
   1.484 +        for ( plane=0; plane < (nbplanes+stencil); plane++ )
   1.485 +        {
   1.486 +            ptr = MiniBuf + ( plane * bytesperline );
   1.487  
   1.488 -			remainingbytes = bytesperline;
   1.489 +            remainingbytes = bytesperline;
   1.490  
   1.491 -			if ( bmhd.tcomp == 1 )	    /* Datas are compressed */
   1.492 -			{
   1.493 -				do
   1.494 -				{
   1.495 -					if ( !SDL_RWread( src, &count, 1, 1 ) )
   1.496 -					{
   1.497 -						error="error reading BODY chunk";
   1.498 -						goto done;
   1.499 -					}
   1.500 +            if ( bmhd.tcomp == 1 )      /* Datas are compressed */
   1.501 +            {
   1.502 +                do
   1.503 +                {
   1.504 +                    if ( !SDL_RWread( src, &count, 1, 1 ) )
   1.505 +                    {
   1.506 +                        error="error reading BODY chunk";
   1.507 +                        goto done;
   1.508 +                    }
   1.509  
   1.510 -					if ( count & 0x80 )
   1.511 -					{
   1.512 -						count ^= 0xFF;
   1.513 -						count += 2; /* now it */
   1.514 +                    if ( count & 0x80 )
   1.515 +                    {
   1.516 +                        count ^= 0xFF;
   1.517 +                        count += 2; /* now it */
   1.518  
   1.519 -						if ( ( count > remainingbytes ) || !SDL_RWread( src, &color, 1, 1 ) )
   1.520 -						{
   1.521 -							error="error reading BODY chunk";
   1.522 -							goto done;
   1.523 -						}
   1.524 -						SDL_memset( ptr, color, count );
   1.525 -					}
   1.526 -					else
   1.527 -					{
   1.528 -						++count;
   1.529 +                        if ( ( count > remainingbytes ) || !SDL_RWread( src, &color, 1, 1 ) )
   1.530 +                        {
   1.531 +                            error="error reading BODY chunk";
   1.532 +                            goto done;
   1.533 +                        }
   1.534 +                        SDL_memset( ptr, color, count );
   1.535 +                    }
   1.536 +                    else
   1.537 +                    {
   1.538 +                        ++count;
   1.539  
   1.540 -						if ( ( count > remainingbytes ) || !SDL_RWread( src, ptr, count, 1 ) )
   1.541 -						{
   1.542 -						   error="error reading BODY chunk";
   1.543 -							goto done;
   1.544 -						}
   1.545 -					}
   1.546 +                        if ( ( count > remainingbytes ) || !SDL_RWread( src, ptr, count, 1 ) )
   1.547 +                        {
   1.548 +                           error="error reading BODY chunk";
   1.549 +                            goto done;
   1.550 +                        }
   1.551 +                    }
   1.552  
   1.553 -					ptr += count;
   1.554 -					remainingbytes -= count;
   1.555 +                    ptr += count;
   1.556 +                    remainingbytes -= count;
   1.557  
   1.558 -				} while ( remainingbytes > 0 );
   1.559 -			}
   1.560 -			else
   1.561 -			{
   1.562 -				if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
   1.563 -				{
   1.564 -					error="error reading BODY chunk";
   1.565 -					goto done;
   1.566 -				}
   1.567 -			}
   1.568 -		}
   1.569 +                } while ( remainingbytes > 0 );
   1.570 +            }
   1.571 +            else
   1.572 +            {
   1.573 +                if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
   1.574 +                {
   1.575 +                    error="error reading BODY chunk";
   1.576 +                    goto done;
   1.577 +                }
   1.578 +            }
   1.579 +        }
   1.580  
   1.581 -		/* One line has been read, store it ! */
   1.582 +        /* One line has been read, store it ! */
   1.583  
   1.584 -		ptr = (Uint8 *)Image->pixels;
   1.585 -		if ( nbplanes==24 || flagHAM==1 )
   1.586 -			ptr += h * width * 3;
   1.587 -		else
   1.588 -			ptr += h * width;
   1.589 +        ptr = (Uint8 *)Image->pixels;
   1.590 +        if ( nbplanes==24 || flagHAM==1 )
   1.591 +            ptr += h * width * 3;
   1.592 +        else
   1.593 +            ptr += h * width;
   1.594  
   1.595 -		if ( pbm )                 /* File format : 'Packed Bitmap' */
   1.596 -		{
   1.597 -		   memcpy( ptr, MiniBuf, width );
   1.598 -		}
   1.599 -		else		/* We have to un-interlace the bits ! */
   1.600 -		{
   1.601 -			if ( nbplanes!=24 && flagHAM==0 )
   1.602 -			{
   1.603 -				size = ( width + 7 ) / 8;
   1.604 +        if ( pbm )                 /* File format : 'Packed Bitmap' */
   1.605 +        {
   1.606 +           memcpy( ptr, MiniBuf, width );
   1.607 +        }
   1.608 +        else        /* We have to un-interlace the bits ! */
   1.609 +        {
   1.610 +            if ( nbplanes!=24 && flagHAM==0 )
   1.611 +            {
   1.612 +                size = ( width + 7 ) / 8;
   1.613  
   1.614 -				for ( i=0; i < size; i++ )
   1.615 -				{
   1.616 -					memset( ptr, 0, 8 );
   1.617 +                for ( i=0; i < size; i++ )
   1.618 +                {
   1.619 +                    memset( ptr, 0, 8 );
   1.620  
   1.621 -					for ( plane=0; plane < (nbplanes + stencil); plane++ )
   1.622 -					{
   1.623 -						color = *( MiniBuf + i + ( plane * bytesperline ) );
   1.624 -						msk = 0x80;
   1.625 +                    for ( plane=0; plane < (nbplanes + stencil); plane++ )
   1.626 +                    {
   1.627 +                        color = *( MiniBuf + i + ( plane * bytesperline ) );
   1.628 +                        msk = 0x80;
   1.629  
   1.630 -						for ( j=0; j<8; j++ )
   1.631 -						{
   1.632 -							if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
   1.633 -							else 	                    ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
   1.634 +                        for ( j=0; j<8; j++ )
   1.635 +                        {
   1.636 +                            if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
   1.637 +                            else                        ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
   1.638  
   1.639 -							msk >>= 1;
   1.640 -						}
   1.641 -					}
   1.642 -					ptr += 8;
   1.643 -				}
   1.644 -			}
   1.645 -			else
   1.646 -			{
   1.647 -				Uint32 finalcolor = 0;
   1.648 -				size = ( width + 7 ) / 8;
   1.649 -				/* 24 bitplanes ILBM : R0...R7,G0...G7,B0...B7 */
   1.650 -				/* or HAM (6 bitplanes) or HAM8 (8 bitplanes) modes */
   1.651 -				for ( i=0; i<width; i=i+8 )
   1.652 -				{
   1.653 -					Uint8 maskBit = 0x80;
   1.654 -					for ( j=0; j<8; j++ )
   1.655 -					{
   1.656 -						Uint32 pixelcolor = 0;
   1.657 -						Uint32 maskColor = 1;
   1.658 -						Uint8 dataBody;
   1.659 -						for ( plane=0; plane < nbplanes; plane++ )
   1.660 -						{
   1.661 -							dataBody = MiniBuf[ plane*size+i/8 ];
   1.662 -							if ( dataBody&maskBit )
   1.663 -								pixelcolor = pixelcolor | maskColor;
   1.664 -							maskColor = maskColor<<1;
   1.665 -						}
   1.666 -						/* HAM : 12 bits RGB image (4 bits per color component) */
   1.667 -						/* HAM8 : 18 bits RGB image (6 bits per color component) */
   1.668 -						if ( flagHAM )
   1.669 -						{
   1.670 -							switch( pixelcolor>>(nbplanes-2) )
   1.671 -							{
   1.672 -								case 0: /* take direct color from palette */
   1.673 -									finalcolor = colormap[ pixelcolor*3 ] + (colormap[ pixelcolor*3+1 ]<<8) + (colormap[ pixelcolor*3+2 ]<<16);
   1.674 -									break;
   1.675 -								case 1: /* modify only blue component */
   1.676 -									finalcolor = finalcolor&0x00FFFF;
   1.677 -									finalcolor = finalcolor | (pixelcolor<<(16+(10-nbplanes)));
   1.678 -									break;
   1.679 -								case 2: /* modify only red component */
   1.680 -									finalcolor = finalcolor&0xFFFF00;
   1.681 -									finalcolor = finalcolor | pixelcolor<<(10-nbplanes);
   1.682 -									break;
   1.683 -								case 3: /* modify only green component */
   1.684 -									finalcolor = finalcolor&0xFF00FF;
   1.685 -									finalcolor = finalcolor | (pixelcolor<<(8+(10-nbplanes)));
   1.686 -									break;
   1.687 -							}
   1.688 -						}
   1.689 -						else
   1.690 -						{
   1.691 -							finalcolor = pixelcolor;
   1.692 -						}
   1.693 +                            msk >>= 1;
   1.694 +                        }
   1.695 +                    }
   1.696 +                    ptr += 8;
   1.697 +                }
   1.698 +            }
   1.699 +            else
   1.700 +            {
   1.701 +                Uint32 finalcolor = 0;
   1.702 +                size = ( width + 7 ) / 8;
   1.703 +                /* 24 bitplanes ILBM : R0...R7,G0...G7,B0...B7 */
   1.704 +                /* or HAM (6 bitplanes) or HAM8 (8 bitplanes) modes */
   1.705 +                for ( i=0; i<width; i=i+8 )
   1.706 +                {
   1.707 +                    Uint8 maskBit = 0x80;
   1.708 +                    for ( j=0; j<8; j++ )
   1.709 +                    {
   1.710 +                        Uint32 pixelcolor = 0;
   1.711 +                        Uint32 maskColor = 1;
   1.712 +                        Uint8 dataBody;
   1.713 +                        for ( plane=0; plane < nbplanes; plane++ )
   1.714 +                        {
   1.715 +                            dataBody = MiniBuf[ plane*size+i/8 ];
   1.716 +                            if ( dataBody&maskBit )
   1.717 +                                pixelcolor = pixelcolor | maskColor;
   1.718 +                            maskColor = maskColor<<1;
   1.719 +                        }
   1.720 +                        /* HAM : 12 bits RGB image (4 bits per color component) */
   1.721 +                        /* HAM8 : 18 bits RGB image (6 bits per color component) */
   1.722 +                        if ( flagHAM )
   1.723 +                        {
   1.724 +                            switch( pixelcolor>>(nbplanes-2) )
   1.725 +                            {
   1.726 +                                case 0: /* take direct color from palette */
   1.727 +                                    finalcolor = colormap[ pixelcolor*3 ] + (colormap[ pixelcolor*3+1 ]<<8) + (colormap[ pixelcolor*3+2 ]<<16);
   1.728 +                                    break;
   1.729 +                                case 1: /* modify only blue component */
   1.730 +                                    finalcolor = finalcolor&0x00FFFF;
   1.731 +                                    finalcolor = finalcolor | (pixelcolor<<(16+(10-nbplanes)));
   1.732 +                                    break;
   1.733 +                                case 2: /* modify only red component */
   1.734 +                                    finalcolor = finalcolor&0xFFFF00;
   1.735 +                                    finalcolor = finalcolor | pixelcolor<<(10-nbplanes);
   1.736 +                                    break;
   1.737 +                                case 3: /* modify only green component */
   1.738 +                                    finalcolor = finalcolor&0xFF00FF;
   1.739 +                                    finalcolor = finalcolor | (pixelcolor<<(8+(10-nbplanes)));
   1.740 +                                    break;
   1.741 +                            }
   1.742 +                        }
   1.743 +                        else
   1.744 +                        {
   1.745 +                            finalcolor = pixelcolor;
   1.746 +                        }
   1.747  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.748 -							*ptr++ = (Uint8)(finalcolor>>16);
   1.749 -							*ptr++ = (Uint8)(finalcolor>>8);
   1.750 -							*ptr++ = (Uint8)(finalcolor);
   1.751 +                            *ptr++ = (Uint8)(finalcolor>>16);
   1.752 +                            *ptr++ = (Uint8)(finalcolor>>8);
   1.753 +                            *ptr++ = (Uint8)(finalcolor);
   1.754  #else
   1.755 -						    *ptr++ = (Uint8)(finalcolor);
   1.756 -							*ptr++ = (Uint8)(finalcolor>>8);
   1.757 -							*ptr++ = (Uint8)(finalcolor>>16);
   1.758 +                            *ptr++ = (Uint8)(finalcolor);
   1.759 +                            *ptr++ = (Uint8)(finalcolor>>8);
   1.760 +                            *ptr++ = (Uint8)(finalcolor>>16);
   1.761  #endif
   1.762 -						maskBit = maskBit>>1;
   1.763 -					}
   1.764 -				}
   1.765 -			}
   1.766 -		}
   1.767 -	}
   1.768 +                        maskBit = maskBit>>1;
   1.769 +                    }
   1.770 +                }
   1.771 +            }
   1.772 +        }
   1.773 +    }
   1.774  
   1.775  done:
   1.776  
   1.777 -	if ( MiniBuf ) SDL_free( MiniBuf );
   1.778 +    if ( MiniBuf ) SDL_free( MiniBuf );
   1.779  
   1.780 -	if ( error )
   1.781 -	{
   1.782 -		SDL_RWseek(src, start, RW_SEEK_SET);
   1.783 -		if ( Image ) {
   1.784 -			SDL_FreeSurface( Image );
   1.785 -			Image = NULL;
   1.786 -		}
   1.787 -		IMG_SetError( error );
   1.788 -	}
   1.789 +    if ( error )
   1.790 +    {
   1.791 +        SDL_RWseek(src, start, RW_SEEK_SET);
   1.792 +        if ( Image ) {
   1.793 +            SDL_FreeSurface( Image );
   1.794 +            Image = NULL;
   1.795 +        }
   1.796 +        IMG_SetError( error );
   1.797 +    }
   1.798  
   1.799 -	return( Image );
   1.800 +    return( Image );
   1.801  }
   1.802  
   1.803  #else /* LOAD_LBM */
   1.804 @@ -487,13 +487,13 @@
   1.805  /* See if an image is contained in a data source */
   1.806  int IMG_isLBM(SDL_RWops *src)
   1.807  {
   1.808 -	return(0);
   1.809 +    return(0);
   1.810  }
   1.811  
   1.812  /* Load an IFF type image from an SDL datasource */
   1.813  SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src)
   1.814  {
   1.815 -	return(NULL);
   1.816 +    return(NULL);
   1.817  }
   1.818  
   1.819  #endif /* LOAD_LBM */