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