IMG_bmp.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 13:25:56 +0000
changeset 101 5c27212dbfc6
parent 97 e1161bd417c4
child 106 65e8cd587119
permissions -rw-r--r--
Pierre G. Richard - Fri, 30 Jul 2004 11:13:11 +0000 (UTC)
* Added support for RLE encoded BMP files
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@97
     3
    Copyright (C) 1999-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@53
    23
/* $Id$ */
slouken@53
    24
slouken@0
    25
/* This is a BMP image file loading framework */
slouken@0
    26
slouken@0
    27
#include <stdio.h>
slouken@0
    28
#include <string.h>
slouken@0
    29
slouken@0
    30
#include "SDL_image.h"
slouken@0
    31
slouken@0
    32
#ifdef LOAD_BMP
slouken@0
    33
slouken@0
    34
/* See if an image is contained in a data source */
slouken@0
    35
int IMG_isBMP(SDL_RWops *src)
slouken@0
    36
{
slouken@0
    37
	int is_BMP;
slouken@0
    38
	char magic[2];
slouken@0
    39
slouken@0
    40
	is_BMP = 0;
slouken@0
    41
	if ( SDL_RWread(src, magic, 2, 1) ) {
slouken@0
    42
		if ( strncmp(magic, "BM", 2) == 0 ) {
slouken@0
    43
			is_BMP = 1;
slouken@0
    44
		}
slouken@0
    45
	}
slouken@0
    46
	return(is_BMP);
slouken@0
    47
}
slouken@0
    48
slouken@101
    49
#include "SDL_error.h"
slouken@101
    50
#include "SDL_video.h"
slouken@101
    51
#include "SDL_endian.h"
slouken@101
    52
slouken@101
    53
/* Compression encodings for BMP files */
slouken@101
    54
#ifndef BI_RGB
slouken@101
    55
#define BI_RGB		0
slouken@101
    56
#define BI_RLE8		1
slouken@101
    57
#define BI_RLE4		2
slouken@101
    58
#define BI_BITFIELDS	3
slouken@101
    59
#endif
slouken@101
    60
slouken@101
    61
static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
slouken@101
    62
{
slouken@101
    63
	/*
slouken@101
    64
	| Sets the surface pixels from src.  A bmp image is upside down.
slouken@101
    65
	*/
slouken@101
    66
	int pitch = surface->pitch;
slouken@101
    67
	int height = surface->h;
slouken@101
    68
	Uint8 * bits = (Uint8 *)surface->pixels + ((height-1) * pitch);
slouken@101
    69
	int ofs = 0;
slouken@101
    70
	Uint8 ch;
slouken@101
    71
	Uint8 needsPad;
slouken@101
    72
slouken@101
    73
	for (;;) {
slouken@101
    74
		if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
    75
		/*
slouken@101
    76
		| encoded mode starts with a run length, and then a byte
slouken@101
    77
		| with two colour indexes to alternate between for the run
slouken@101
    78
		*/
slouken@101
    79
		if ( ch ) {
slouken@101
    80
			Uint8 pixel;
slouken@101
    81
			if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
slouken@101
    82
			if ( isRle8 ) {                 /* 256-color bitmap, compressed */
slouken@101
    83
				do {
slouken@101
    84
					bits[ofs++] = pixel;
slouken@101
    85
				} while (--ch);
slouken@101
    86
			}else {                         /* 16-color bitmap, compressed */
slouken@101
    87
				Uint8 pixel0 = pixel >> 4;
slouken@101
    88
				Uint8 pixel1 = pixel & 0x0F;
slouken@101
    89
				for (;;) {
slouken@101
    90
					bits[ofs++] = pixel0;     /* even count, high nibble */
slouken@101
    91
					if (!--ch) break;
slouken@101
    92
					bits[ofs++] = pixel1;     /* odd count, low nibble */
slouken@101
    93
					if (!--ch) break;
slouken@101
    94
				}
slouken@101
    95
			}
slouken@101
    96
		} else {
slouken@101
    97
			/*
slouken@101
    98
			| A leading zero is an escape; it may signal the end of the bitmap,
slouken@101
    99
			| a cursor move, or some absolute data.
slouken@101
   100
			| zero tag may be absolute mode or an escape
slouken@101
   101
			*/
slouken@101
   102
			if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   103
			switch (ch) {
slouken@101
   104
			case 0:                         /* end of line */
slouken@101
   105
				ofs = 0;
slouken@101
   106
				bits -= pitch;               /* go to previous */
slouken@101
   107
				break;
slouken@101
   108
			case 1:                         /* end of bitmap */
slouken@101
   109
				return 0;                    /* success! */
slouken@101
   110
			case 2:                         /* delta */
slouken@101
   111
				if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   112
				ofs += ch;
slouken@101
   113
				if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   114
				bits -= (ch * pitch);
slouken@101
   115
				break;
slouken@101
   116
			default:                        /* no compression */
slouken@101
   117
				if (isRle8) {
slouken@101
   118
					needsPad = ( ch & 1 );
slouken@101
   119
					do {
slouken@101
   120
						if ( !SDL_RWread(src, bits + ofs++, 1, 1) ) return 1;
slouken@101
   121
					} while (--ch);
slouken@101
   122
				} else {
slouken@101
   123
					needsPad = ( ((ch+1)>>1) & 1 ); /* (ch+1)>>1: bytes size */
slouken@101
   124
					for (;;) {
slouken@101
   125
						Uint8 pixel;
slouken@101
   126
						if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
slouken@101
   127
						bits[ofs++] = pixel >> 4;
slouken@101
   128
						if (!--ch) break;
slouken@101
   129
						bits[ofs++] = pixel & 0x0F;
slouken@101
   130
						if (!--ch) break;
slouken@101
   131
					}
slouken@101
   132
				}
slouken@101
   133
				/* pad at even boundary */
slouken@101
   134
				if ( needsPad && !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   135
				break;
slouken@101
   136
			}
slouken@101
   137
		}
slouken@101
   138
	}
slouken@101
   139
}
slouken@101
   140
slouken@101
   141
static SDL_Surface *LoadBMP_RW (SDL_RWops *src, int freesrc)
slouken@101
   142
{
slouken@101
   143
	int was_error;
slouken@101
   144
	long fp_offset;
slouken@101
   145
	int bmpPitch;
slouken@101
   146
	int i, pad;
slouken@101
   147
	SDL_Surface *surface;
slouken@101
   148
	Uint32 Rmask;
slouken@101
   149
	Uint32 Gmask;
slouken@101
   150
	Uint32 Bmask;
slouken@101
   151
	SDL_Palette *palette;
slouken@101
   152
	Uint8 *bits;
slouken@101
   153
	int ExpandBMP;
slouken@101
   154
slouken@101
   155
	/* The Win32 BMP file header (14 bytes) */
slouken@101
   156
	char   magic[2];
slouken@101
   157
	Uint32 bfSize;
slouken@101
   158
	Uint16 bfReserved1;
slouken@101
   159
	Uint16 bfReserved2;
slouken@101
   160
	Uint32 bfOffBits;
slouken@101
   161
slouken@101
   162
	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@101
   163
	Uint32 biSize;
slouken@101
   164
	Sint32 biWidth;
slouken@101
   165
	Sint32 biHeight;
slouken@101
   166
	Uint16 biPlanes;
slouken@101
   167
	Uint16 biBitCount;
slouken@101
   168
	Uint32 biCompression;
slouken@101
   169
	Uint32 biSizeImage;
slouken@101
   170
	Sint32 biXPelsPerMeter;
slouken@101
   171
	Sint32 biYPelsPerMeter;
slouken@101
   172
	Uint32 biClrUsed;
slouken@101
   173
	Uint32 biClrImportant;
slouken@101
   174
slouken@101
   175
	/* Make sure we are passed a valid data source */
slouken@101
   176
	surface = NULL;
slouken@101
   177
	was_error = 0;
slouken@101
   178
	if ( src == NULL ) {
slouken@101
   179
		was_error = 1;
slouken@101
   180
		goto done;
slouken@101
   181
	}
slouken@101
   182
slouken@101
   183
	/* Read in the BMP file header */
slouken@101
   184
	fp_offset = SDL_RWtell(src);
slouken@101
   185
	SDL_ClearError();
slouken@101
   186
	if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
slouken@101
   187
		SDL_Error(SDL_EFREAD);
slouken@101
   188
		was_error = 1;
slouken@101
   189
		goto done;
slouken@101
   190
	}
slouken@101
   191
	if ( strncmp(magic, "BM", 2) != 0 ) {
slouken@101
   192
		SDL_SetError("File is not a Windows BMP file");
slouken@101
   193
		was_error = 1;
slouken@101
   194
		goto done;
slouken@101
   195
	}
slouken@101
   196
	bfSize		= SDL_ReadLE32(src);
slouken@101
   197
	bfReserved1	= SDL_ReadLE16(src);
slouken@101
   198
	bfReserved2	= SDL_ReadLE16(src);
slouken@101
   199
	bfOffBits	= SDL_ReadLE32(src);
slouken@101
   200
slouken@101
   201
	/* Read the Win32 BITMAPINFOHEADER */
slouken@101
   202
	biSize		= SDL_ReadLE32(src);
slouken@101
   203
	if ( biSize == 12 ) {
slouken@101
   204
		biWidth		= (Uint32)SDL_ReadLE16(src);
slouken@101
   205
		biHeight	= (Uint32)SDL_ReadLE16(src);
slouken@101
   206
		biPlanes	= SDL_ReadLE16(src);
slouken@101
   207
		biBitCount	= SDL_ReadLE16(src);
slouken@101
   208
		biCompression	= BI_RGB;
slouken@101
   209
		biSizeImage	= 0;
slouken@101
   210
		biXPelsPerMeter	= 0;
slouken@101
   211
		biYPelsPerMeter	= 0;
slouken@101
   212
		biClrUsed	= 0;
slouken@101
   213
		biClrImportant	= 0;
slouken@101
   214
	} else {
slouken@101
   215
		biWidth		= SDL_ReadLE32(src);
slouken@101
   216
		biHeight	= SDL_ReadLE32(src);
slouken@101
   217
		biPlanes	= SDL_ReadLE16(src);
slouken@101
   218
		biBitCount	= SDL_ReadLE16(src);
slouken@101
   219
		biCompression	= SDL_ReadLE32(src);
slouken@101
   220
		biSizeImage	= SDL_ReadLE32(src);
slouken@101
   221
		biXPelsPerMeter	= SDL_ReadLE32(src);
slouken@101
   222
		biYPelsPerMeter	= SDL_ReadLE32(src);
slouken@101
   223
		biClrUsed	= SDL_ReadLE32(src);
slouken@101
   224
		biClrImportant	= SDL_ReadLE32(src);
slouken@101
   225
	}
slouken@101
   226
slouken@101
   227
	/* Check for read error */
slouken@101
   228
	if ( strcmp(SDL_GetError(), "") != 0 ) {
slouken@101
   229
		was_error = 1;
slouken@101
   230
		goto done;
slouken@101
   231
	}
slouken@101
   232
slouken@101
   233
	/* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
slouken@101
   234
	switch (biBitCount) {
slouken@101
   235
		case 1:
slouken@101
   236
		case 4:
slouken@101
   237
			ExpandBMP = biBitCount;
slouken@101
   238
			biBitCount = 8;
slouken@101
   239
			break;
slouken@101
   240
		default:
slouken@101
   241
			ExpandBMP = 0;
slouken@101
   242
			break;
slouken@101
   243
	}
slouken@101
   244
slouken@101
   245
	/* RLE4 and RLE8 BMP compression is supported */
slouken@101
   246
	Rmask = Gmask = Bmask = 0;
slouken@101
   247
	switch (biCompression) {
slouken@101
   248
		case BI_RGB:
slouken@101
   249
			/* If there are no masks, use the defaults */
slouken@101
   250
			if ( bfOffBits == (14+biSize) ) {
slouken@101
   251
				/* Default values for the BMP format */
slouken@101
   252
				switch (biBitCount) {
slouken@101
   253
					case 15:
slouken@101
   254
					case 16:
slouken@101
   255
						Rmask = 0x7C00;
slouken@101
   256
						Gmask = 0x03E0;
slouken@101
   257
						Bmask = 0x001F;
slouken@101
   258
						break;
slouken@101
   259
					case 24:
slouken@101
   260
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   261
					        Rmask = 0x000000FF;
slouken@101
   262
					        Gmask = 0x0000FF00;
slouken@101
   263
					        Bmask = 0x00FF0000;
slouken@101
   264
						break;
slouken@101
   265
#endif
slouken@101
   266
					case 32:
slouken@101
   267
						Rmask = 0x00FF0000;
slouken@101
   268
						Gmask = 0x0000FF00;
slouken@101
   269
						Bmask = 0x000000FF;
slouken@101
   270
						break;
slouken@101
   271
					default:
slouken@101
   272
						break;
slouken@101
   273
				}
slouken@101
   274
				break;
slouken@101
   275
			}
slouken@101
   276
			/* Fall through -- read the RGB masks */
slouken@101
   277
slouken@101
   278
		default:
slouken@101
   279
			switch (biBitCount) {
slouken@101
   280
				case 15:
slouken@101
   281
				case 16:
slouken@101
   282
				case 32:
slouken@101
   283
					Rmask = SDL_ReadLE32(src);
slouken@101
   284
					Gmask = SDL_ReadLE32(src);
slouken@101
   285
					Bmask = SDL_ReadLE32(src);
slouken@101
   286
					break;
slouken@101
   287
				default:
slouken@101
   288
					break;
slouken@101
   289
			}
slouken@101
   290
			break;
slouken@101
   291
	}
slouken@101
   292
slouken@101
   293
	/* Create a compatible surface, note that the colors are RGB ordered */
slouken@101
   294
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@101
   295
			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, 0);
slouken@101
   296
	if ( surface == NULL ) {
slouken@101
   297
		was_error = 1;
slouken@101
   298
		goto done;
slouken@101
   299
	}
slouken@101
   300
slouken@101
   301
	/* Load the palette, if any */
slouken@101
   302
	palette = (surface->format)->palette;
slouken@101
   303
	if ( palette ) {
slouken@101
   304
		/*
slouken@101
   305
		| guich: always use 1<<bpp b/c some bitmaps can bring wrong information
slouken@101
   306
		| for colorsUsed
slouken@101
   307
		*/
slouken@101
   308
		/* if ( biClrUsed == 0 ) {  */
slouken@101
   309
		biClrUsed = 1 << biBitCount;
slouken@101
   310
		/* } */
slouken@101
   311
		if ( biSize == 12 ) {
slouken@101
   312
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   313
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   314
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   315
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   316
				palette->colors[i].unused = 0;
slouken@101
   317
			}	
slouken@101
   318
		} else {
slouken@101
   319
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   320
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   321
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   322
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   323
				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
slouken@101
   324
			}	
slouken@101
   325
		}
slouken@101
   326
		palette->ncolors = biClrUsed;
slouken@101
   327
	}
slouken@101
   328
slouken@101
   329
	/* Read the surface pixels.  Note that the bmp image is upside down */
slouken@101
   330
	if ( SDL_RWseek(src, fp_offset+bfOffBits, SEEK_SET) < 0 ) {
slouken@101
   331
		SDL_Error(SDL_EFSEEK);
slouken@101
   332
		was_error = 1;
slouken@101
   333
		goto done;
slouken@101
   334
	}
slouken@101
   335
	if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
slouken@101
   336
		was_error = readRlePixels(surface, src, biCompression == BI_RLE8);
slouken@101
   337
		if (was_error) SDL_SetError("Error reading from BMP");
slouken@101
   338
		goto done;
slouken@101
   339
	}
slouken@101
   340
	bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@101
   341
	switch (ExpandBMP) {
slouken@101
   342
		case 1:
slouken@101
   343
			bmpPitch = (biWidth + 7) >> 3;
slouken@101
   344
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   345
			break;
slouken@101
   346
		case 4:
slouken@101
   347
			bmpPitch = (biWidth + 1) >> 1;
slouken@101
   348
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   349
			break;
slouken@101
   350
		default:
slouken@101
   351
			pad  = ((surface->pitch%4) ?
slouken@101
   352
					(4-(surface->pitch%4)) : 0);
slouken@101
   353
			break;
slouken@101
   354
	}
slouken@101
   355
	while ( bits > (Uint8 *)surface->pixels ) {
slouken@101
   356
		bits -= surface->pitch;
slouken@101
   357
		switch (ExpandBMP) {
slouken@101
   358
			case 1:
slouken@101
   359
			case 4: {
slouken@101
   360
			Uint8 pixel = 0;
slouken@101
   361
			int   shift = (8-ExpandBMP);
slouken@101
   362
			for ( i=0; i<surface->w; ++i ) {
slouken@101
   363
				if ( i%(8/ExpandBMP) == 0 ) {
slouken@101
   364
					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
slouken@101
   365
						SDL_SetError(
slouken@101
   366
					"Error reading from BMP");
slouken@101
   367
						was_error = 1;
slouken@101
   368
						goto done;
slouken@101
   369
					}
slouken@101
   370
				}
slouken@101
   371
				*(bits+i) = (pixel>>shift);
slouken@101
   372
				pixel <<= ExpandBMP;
slouken@101
   373
			} }
slouken@101
   374
			break;
slouken@101
   375
slouken@101
   376
			default:
slouken@101
   377
			if ( SDL_RWread(src, bits, 1, surface->pitch)
slouken@101
   378
							 != surface->pitch ) {
slouken@101
   379
				SDL_Error(SDL_EFREAD);
slouken@101
   380
				was_error = 1;
slouken@101
   381
				goto done;
slouken@101
   382
			}
slouken@101
   383
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   384
			/* Byte-swap the pixels if needed. Note that the 24bpp
slouken@101
   385
			   case has already been taken care of above. */
slouken@101
   386
			switch(biBitCount) {
slouken@101
   387
				case 15:
slouken@101
   388
				case 16: {
slouken@101
   389
				        Uint16 *pix = (Uint16 *)bits;
slouken@101
   390
					for(i = 0; i < surface->w; i++)
slouken@101
   391
					        pix[i] = SDL_Swap16(pix[i]);
slouken@101
   392
					break;
slouken@101
   393
				}
slouken@101
   394
slouken@101
   395
				case 32: {
slouken@101
   396
				        Uint32 *pix = (Uint32 *)bits;
slouken@101
   397
					for(i = 0; i < surface->w; i++)
slouken@101
   398
					        pix[i] = SDL_Swap32(pix[i]);
slouken@101
   399
					break;
slouken@101
   400
				}
slouken@101
   401
			}
slouken@101
   402
#endif
slouken@101
   403
			break;
slouken@101
   404
		}
slouken@101
   405
		/* Skip padding bytes, ugh */
slouken@101
   406
		if ( pad ) {
slouken@101
   407
			Uint8 padbyte;
slouken@101
   408
			for ( i=0; i<pad; ++i ) {
slouken@101
   409
				SDL_RWread(src, &padbyte, 1, 1);
slouken@101
   410
			}
slouken@101
   411
		}
slouken@101
   412
	}
slouken@101
   413
done:
slouken@101
   414
	if ( was_error ) {
slouken@101
   415
		if ( surface ) {
slouken@101
   416
			SDL_FreeSurface(surface);
slouken@101
   417
		}
slouken@101
   418
		surface = NULL;
slouken@101
   419
	}
slouken@101
   420
	if ( freesrc && src ) {
slouken@101
   421
		SDL_RWclose(src);
slouken@101
   422
	}
slouken@101
   423
	return(surface);
slouken@101
   424
}
slouken@101
   425
slouken@0
   426
/* Load a BMP type image from an SDL datasource */
slouken@0
   427
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   428
{
slouken@101
   429
	return(LoadBMP_RW(src, 0));
slouken@0
   430
}
slouken@0
   431
slouken@0
   432
#else
slouken@0
   433
slouken@0
   434
/* See if an image is contained in a data source */
slouken@0
   435
int IMG_isBMP(SDL_RWops *src)
slouken@0
   436
{
slouken@0
   437
	return(0);
slouken@0
   438
}
slouken@0
   439
slouken@0
   440
/* Load a BMP type image from an SDL datasource */
slouken@0
   441
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   442
{
slouken@0
   443
	return(NULL);
slouken@0
   444
}
slouken@0
   445
slouken@0
   446
#endif /* LOAD_BMP */