src/video/SDL_bmp.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:25:42 +0000
branchSDL-1.2
changeset 4159 a1b03ba2fcd0
parent 1402 d910939febfa
child 4241 d3a210342761
permissions -rw-r--r--
Updated copyright date
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* 
slouken@0
    25
   Code to load and save surfaces in Windows BMP format.
slouken@0
    26
slouken@0
    27
   Why support BMP format?  Well, it's a native format for Windows, and
slouken@0
    28
   most image processing programs can read and write it.  It would be nice
slouken@0
    29
   to be able to have at least one image format that we can natively load
slouken@0
    30
   and save, and since PNG is so complex that it would bloat the library,
slouken@0
    31
   BMP is a good alternative. 
slouken@0
    32
slouken@0
    33
   This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
slouken@0
    34
*/
slouken@0
    35
slouken@0
    36
#include "SDL_video.h"
slouken@0
    37
#include "SDL_endian.h"
slouken@0
    38
slouken@0
    39
/* Compression encodings for BMP files */
slouken@0
    40
#ifndef BI_RGB
slouken@0
    41
#define BI_RGB		0
slouken@0
    42
#define BI_RLE8		1
slouken@0
    43
#define BI_RLE4		2
slouken@0
    44
#define BI_BITFIELDS	3
slouken@0
    45
#endif
slouken@0
    46
slouken@0
    47
slouken@0
    48
SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
slouken@0
    49
{
slouken@0
    50
	int was_error;
slouken@0
    51
	long fp_offset;
slouken@0
    52
	int bmpPitch;
slouken@0
    53
	int i, pad;
slouken@0
    54
	SDL_Surface *surface;
slouken@0
    55
	Uint32 Rmask;
slouken@0
    56
	Uint32 Gmask;
slouken@0
    57
	Uint32 Bmask;
slouken@0
    58
	SDL_Palette *palette;
slouken@0
    59
	Uint8 *bits;
slouken@0
    60
	int ExpandBMP;
slouken@0
    61
slouken@0
    62
	/* The Win32 BMP file header (14 bytes) */
slouken@0
    63
	char   magic[2];
slouken@0
    64
	Uint32 bfSize;
slouken@0
    65
	Uint16 bfReserved1;
slouken@0
    66
	Uint16 bfReserved2;
slouken@0
    67
	Uint32 bfOffBits;
slouken@0
    68
slouken@0
    69
	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@0
    70
	Uint32 biSize;
slouken@0
    71
	Sint32 biWidth;
slouken@0
    72
	Sint32 biHeight;
slouken@0
    73
	Uint16 biPlanes;
slouken@0
    74
	Uint16 biBitCount;
slouken@0
    75
	Uint32 biCompression;
slouken@0
    76
	Uint32 biSizeImage;
slouken@0
    77
	Sint32 biXPelsPerMeter;
slouken@0
    78
	Sint32 biYPelsPerMeter;
slouken@0
    79
	Uint32 biClrUsed;
slouken@0
    80
	Uint32 biClrImportant;
slouken@0
    81
slouken@0
    82
	/* Make sure we are passed a valid data source */
slouken@0
    83
	surface = NULL;
slouken@0
    84
	was_error = 0;
slouken@0
    85
	if ( src == NULL ) {
slouken@0
    86
		was_error = 1;
slouken@0
    87
		goto done;
slouken@0
    88
	}
slouken@0
    89
slouken@0
    90
	/* Read in the BMP file header */
slouken@0
    91
	fp_offset = SDL_RWtell(src);
slouken@0
    92
	SDL_ClearError();
slouken@0
    93
	if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
slouken@0
    94
		SDL_Error(SDL_EFREAD);
slouken@0
    95
		was_error = 1;
slouken@0
    96
		goto done;
slouken@0
    97
	}
slouken@1336
    98
	if ( SDL_strncmp(magic, "BM", 2) != 0 ) {
slouken@0
    99
		SDL_SetError("File is not a Windows BMP file");
slouken@0
   100
		was_error = 1;
slouken@0
   101
		goto done;
slouken@0
   102
	}
slouken@0
   103
	bfSize		= SDL_ReadLE32(src);
slouken@0
   104
	bfReserved1	= SDL_ReadLE16(src);
slouken@0
   105
	bfReserved2	= SDL_ReadLE16(src);
slouken@0
   106
	bfOffBits	= SDL_ReadLE32(src);
slouken@0
   107
slouken@0
   108
	/* Read the Win32 BITMAPINFOHEADER */
slouken@0
   109
	biSize		= SDL_ReadLE32(src);
slouken@0
   110
	if ( biSize == 12 ) {
slouken@0
   111
		biWidth		= (Uint32)SDL_ReadLE16(src);
slouken@0
   112
		biHeight	= (Uint32)SDL_ReadLE16(src);
slouken@0
   113
		biPlanes	= SDL_ReadLE16(src);
slouken@0
   114
		biBitCount	= SDL_ReadLE16(src);
slouken@0
   115
		biCompression	= BI_RGB;
slouken@0
   116
		biSizeImage	= 0;
slouken@0
   117
		biXPelsPerMeter	= 0;
slouken@0
   118
		biYPelsPerMeter	= 0;
slouken@0
   119
		biClrUsed	= 0;
slouken@0
   120
		biClrImportant	= 0;
slouken@0
   121
	} else {
slouken@0
   122
		biWidth		= SDL_ReadLE32(src);
slouken@0
   123
		biHeight	= SDL_ReadLE32(src);
slouken@0
   124
		biPlanes	= SDL_ReadLE16(src);
slouken@0
   125
		biBitCount	= SDL_ReadLE16(src);
slouken@0
   126
		biCompression	= SDL_ReadLE32(src);
slouken@0
   127
		biSizeImage	= SDL_ReadLE32(src);
slouken@0
   128
		biXPelsPerMeter	= SDL_ReadLE32(src);
slouken@0
   129
		biYPelsPerMeter	= SDL_ReadLE32(src);
slouken@0
   130
		biClrUsed	= SDL_ReadLE32(src);
slouken@0
   131
		biClrImportant	= SDL_ReadLE32(src);
slouken@0
   132
	}
slouken@0
   133
slouken@0
   134
	/* Check for read error */
slouken@1336
   135
	if ( SDL_strcmp(SDL_GetError(), "") != 0 ) {
slouken@0
   136
		was_error = 1;
slouken@0
   137
		goto done;
slouken@0
   138
	}
slouken@0
   139
slouken@0
   140
	/* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
slouken@0
   141
	switch (biBitCount) {
slouken@0
   142
		case 1:
slouken@0
   143
		case 4:
slouken@0
   144
			ExpandBMP = biBitCount;
slouken@0
   145
			biBitCount = 8;
slouken@0
   146
			break;
slouken@0
   147
		default:
slouken@0
   148
			ExpandBMP = 0;
slouken@0
   149
			break;
slouken@0
   150
	}
slouken@0
   151
slouken@0
   152
	/* We don't support any BMP compression right now */
slouken@0
   153
	Rmask = Gmask = Bmask = 0;
slouken@0
   154
	switch (biCompression) {
slouken@0
   155
		case BI_RGB:
slouken@0
   156
			/* If there are no masks, use the defaults */
slouken@0
   157
			if ( bfOffBits == (14+biSize) ) {
slouken@0
   158
				/* Default values for the BMP format */
slouken@0
   159
				switch (biBitCount) {
slouken@0
   160
					case 15:
slouken@0
   161
					case 16:
slouken@0
   162
						Rmask = 0x7C00;
slouken@0
   163
						Gmask = 0x03E0;
slouken@0
   164
						Bmask = 0x001F;
slouken@0
   165
						break;
slouken@0
   166
					case 24:
slouken@0
   167
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@0
   168
					        Rmask = 0x000000FF;
slouken@0
   169
					        Gmask = 0x0000FF00;
slouken@0
   170
					        Bmask = 0x00FF0000;
slouken@0
   171
						break;
slouken@0
   172
#endif
slouken@0
   173
					case 32:
slouken@0
   174
						Rmask = 0x00FF0000;
slouken@0
   175
						Gmask = 0x0000FF00;
slouken@0
   176
						Bmask = 0x000000FF;
slouken@0
   177
						break;
slouken@0
   178
					default:
slouken@0
   179
						break;
slouken@0
   180
				}
slouken@0
   181
				break;
slouken@0
   182
			}
slouken@0
   183
			/* Fall through -- read the RGB masks */
slouken@0
   184
slouken@0
   185
		case BI_BITFIELDS:
slouken@0
   186
			switch (biBitCount) {
slouken@0
   187
				case 15:
slouken@0
   188
				case 16:
slouken@0
   189
				case 32:
slouken@0
   190
					Rmask = SDL_ReadLE32(src);
slouken@0
   191
					Gmask = SDL_ReadLE32(src);
slouken@0
   192
					Bmask = SDL_ReadLE32(src);
slouken@0
   193
					break;
slouken@0
   194
				default:
slouken@0
   195
					break;
slouken@0
   196
			}
slouken@0
   197
			break;
slouken@0
   198
		default:
slouken@0
   199
			SDL_SetError("Compressed BMP files not supported");
slouken@0
   200
			was_error = 1;
slouken@0
   201
			goto done;
slouken@0
   202
	}
slouken@0
   203
slouken@0
   204
	/* Create a compatible surface, note that the colors are RGB ordered */
slouken@0
   205
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@0
   206
			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, 0);
slouken@0
   207
	if ( surface == NULL ) {
slouken@0
   208
		was_error = 1;
slouken@0
   209
		goto done;
slouken@0
   210
	}
slouken@0
   211
slouken@0
   212
	/* Load the palette, if any */
slouken@0
   213
	palette = (surface->format)->palette;
slouken@0
   214
	if ( palette ) {
slouken@0
   215
		if ( biClrUsed == 0 ) {
slouken@0
   216
			biClrUsed = 1 << biBitCount;
slouken@0
   217
		}
slouken@0
   218
		if ( biSize == 12 ) {
slouken@0
   219
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@0
   220
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@0
   221
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@0
   222
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@0
   223
				palette->colors[i].unused = 0;
slouken@0
   224
			}	
slouken@0
   225
		} else {
slouken@0
   226
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@0
   227
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@0
   228
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@0
   229
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@0
   230
				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
slouken@0
   231
			}	
slouken@0
   232
		}
slouken@0
   233
		palette->ncolors = biClrUsed;
slouken@0
   234
	}
slouken@0
   235
slouken@0
   236
	/* Read the surface pixels.  Note that the bmp image is upside down */
slouken@1330
   237
	if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
slouken@0
   238
		SDL_Error(SDL_EFSEEK);
slouken@0
   239
		was_error = 1;
slouken@0
   240
		goto done;
slouken@0
   241
	}
slouken@0
   242
	bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@0
   243
	switch (ExpandBMP) {
slouken@0
   244
		case 1:
slouken@0
   245
			bmpPitch = (biWidth + 7) >> 3;
slouken@0
   246
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@0
   247
			break;
slouken@0
   248
		case 4:
slouken@0
   249
			bmpPitch = (biWidth + 1) >> 1;
slouken@0
   250
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@0
   251
			break;
slouken@0
   252
		default:
slouken@0
   253
			pad  = ((surface->pitch%4) ?
slouken@0
   254
					(4-(surface->pitch%4)) : 0);
slouken@0
   255
			break;
slouken@0
   256
	}
slouken@0
   257
	while ( bits > (Uint8 *)surface->pixels ) {
slouken@0
   258
		bits -= surface->pitch;
slouken@0
   259
		switch (ExpandBMP) {
slouken@0
   260
			case 1:
slouken@0
   261
			case 4: {
slouken@0
   262
			Uint8 pixel = 0;
slouken@0
   263
			int   shift = (8-ExpandBMP);
slouken@0
   264
			for ( i=0; i<surface->w; ++i ) {
slouken@0
   265
				if ( i%(8/ExpandBMP) == 0 ) {
slouken@0
   266
					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
slouken@0
   267
						SDL_SetError(
slouken@0
   268
					"Error reading from BMP");
slouken@0
   269
						was_error = 1;
slouken@0
   270
						goto done;
slouken@0
   271
					}
slouken@0
   272
				}
slouken@0
   273
				*(bits+i) = (pixel>>shift);
slouken@0
   274
				pixel <<= ExpandBMP;
slouken@0
   275
			} }
slouken@0
   276
			break;
slouken@0
   277
slouken@0
   278
			default:
slouken@0
   279
			if ( SDL_RWread(src, bits, 1, surface->pitch)
slouken@0
   280
							 != surface->pitch ) {
slouken@0
   281
				SDL_Error(SDL_EFREAD);
slouken@0
   282
				was_error = 1;
slouken@0
   283
				goto done;
slouken@0
   284
			}
slouken@0
   285
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@0
   286
			/* Byte-swap the pixels if needed. Note that the 24bpp
slouken@0
   287
			   case has already been taken care of above. */
slouken@0
   288
			switch(biBitCount) {
slouken@0
   289
				case 15:
slouken@0
   290
				case 16: {
slouken@0
   291
				        Uint16 *pix = (Uint16 *)bits;
slouken@0
   292
					for(i = 0; i < surface->w; i++)
slouken@0
   293
					        pix[i] = SDL_Swap16(pix[i]);
slouken@0
   294
					break;
slouken@0
   295
				}
slouken@0
   296
slouken@0
   297
				case 32: {
slouken@0
   298
				        Uint32 *pix = (Uint32 *)bits;
slouken@0
   299
					for(i = 0; i < surface->w; i++)
slouken@0
   300
					        pix[i] = SDL_Swap32(pix[i]);
slouken@0
   301
					break;
slouken@0
   302
				}
slouken@0
   303
			}
slouken@0
   304
#endif
slouken@0
   305
			break;
slouken@0
   306
		}
slouken@0
   307
		/* Skip padding bytes, ugh */
slouken@0
   308
		if ( pad ) {
slouken@0
   309
			Uint8 padbyte;
slouken@0
   310
			for ( i=0; i<pad; ++i ) {
slouken@0
   311
				SDL_RWread(src, &padbyte, 1, 1);
slouken@0
   312
			}
slouken@0
   313
		}
slouken@0
   314
	}
slouken@0
   315
done:
slouken@0
   316
	if ( was_error ) {
slouken@1329
   317
		if ( src ) {
slouken@1330
   318
			SDL_RWseek(src, fp_offset, RW_SEEK_SET);
slouken@1329
   319
		}
slouken@0
   320
		if ( surface ) {
slouken@0
   321
			SDL_FreeSurface(surface);
slouken@0
   322
		}
slouken@0
   323
		surface = NULL;
slouken@0
   324
	}
slouken@0
   325
	if ( freesrc && src ) {
slouken@0
   326
		SDL_RWclose(src);
slouken@0
   327
	}
slouken@0
   328
	return(surface);
slouken@0
   329
}
slouken@0
   330
slouken@0
   331
int SDL_SaveBMP_RW (SDL_Surface *saveme, SDL_RWops *dst, int freedst)
slouken@0
   332
{
slouken@0
   333
	long fp_offset;
slouken@0
   334
	int i, pad;
slouken@0
   335
	SDL_Surface *surface;
slouken@0
   336
	Uint8 *bits;
slouken@0
   337
slouken@0
   338
	/* The Win32 BMP file header (14 bytes) */
slouken@0
   339
	char   magic[2] = { 'B', 'M' };
slouken@0
   340
	Uint32 bfSize;
slouken@0
   341
	Uint16 bfReserved1;
slouken@0
   342
	Uint16 bfReserved2;
slouken@0
   343
	Uint32 bfOffBits;
slouken@0
   344
slouken@0
   345
	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@0
   346
	Uint32 biSize;
slouken@0
   347
	Sint32 biWidth;
slouken@0
   348
	Sint32 biHeight;
slouken@0
   349
	Uint16 biPlanes;
slouken@0
   350
	Uint16 biBitCount;
slouken@0
   351
	Uint32 biCompression;
slouken@0
   352
	Uint32 biSizeImage;
slouken@0
   353
	Sint32 biXPelsPerMeter;
slouken@0
   354
	Sint32 biYPelsPerMeter;
slouken@0
   355
	Uint32 biClrUsed;
slouken@0
   356
	Uint32 biClrImportant;
slouken@0
   357
slouken@0
   358
	/* Make sure we have somewhere to save */
slouken@0
   359
	surface = NULL;
slouken@0
   360
	if ( dst ) {
slouken@0
   361
		if ( saveme->format->palette ) {
slouken@0
   362
			if ( saveme->format->BitsPerPixel == 8 ) {
slouken@0
   363
				surface = saveme;
slouken@0
   364
			} else {
slouken@0
   365
				SDL_SetError("%d bpp BMP files not supported",
slouken@0
   366
						saveme->format->BitsPerPixel);
slouken@0
   367
			}
slouken@0
   368
		}
slouken@0
   369
		else if ( (saveme->format->BitsPerPixel == 24) &&
slouken@0
   370
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   371
				(saveme->format->Rmask == 0x00FF0000) &&
slouken@0
   372
				(saveme->format->Gmask == 0x0000FF00) &&
slouken@0
   373
				(saveme->format->Bmask == 0x000000FF)
slouken@0
   374
#else
slouken@0
   375
				(saveme->format->Rmask == 0x000000FF) &&
slouken@0
   376
				(saveme->format->Gmask == 0x0000FF00) &&
slouken@0
   377
				(saveme->format->Bmask == 0x00FF0000)
slouken@0
   378
#endif
slouken@0
   379
			  ) {
slouken@0
   380
			surface = saveme;
slouken@0
   381
		} else {
slouken@0
   382
			SDL_Rect bounds;
slouken@0
   383
slouken@0
   384
			/* Convert to 24 bits per pixel */
slouken@0
   385
			surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@0
   386
					saveme->w, saveme->h, 24,
slouken@0
   387
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   388
					0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@0
   389
#else
slouken@0
   390
					0x000000FF, 0x0000FF00, 0x00FF0000,
slouken@0
   391
#endif
slouken@0
   392
					0);
slouken@0
   393
			if ( surface != NULL ) {
slouken@0
   394
				bounds.x = 0;
slouken@0
   395
				bounds.y = 0;
slouken@0
   396
				bounds.w = saveme->w;
slouken@0
   397
				bounds.h = saveme->h;
slouken@0
   398
				if ( SDL_LowerBlit(saveme, &bounds, surface,
slouken@0
   399
							&bounds) < 0 ) {
slouken@0
   400
					SDL_FreeSurface(surface);
slouken@0
   401
					SDL_SetError(
slouken@0
   402
					"Couldn't convert image to 24 bpp");
slouken@0
   403
					surface = NULL;
slouken@0
   404
				}
slouken@0
   405
			}
slouken@0
   406
		}
slouken@0
   407
	}
slouken@0
   408
slouken@0
   409
	if ( surface && (SDL_LockSurface(surface) == 0) ) {
slouken@1012
   410
		const int bw = surface->w*surface->format->BytesPerPixel;
slouken@1012
   411
slouken@0
   412
		/* Set the BMP file header values */
slouken@0
   413
		bfSize = 0;		 /* We'll write this when we're done */
slouken@0
   414
		bfReserved1 = 0;
slouken@0
   415
		bfReserved2 = 0;
slouken@0
   416
		bfOffBits = 0;		/* We'll write this when we're done */
slouken@0
   417
slouken@0
   418
		/* Write the BMP file header values */
slouken@0
   419
		fp_offset = SDL_RWtell(dst);
slouken@0
   420
		SDL_ClearError();
slouken@0
   421
		SDL_RWwrite(dst, magic, 2, 1);
slouken@0
   422
		SDL_WriteLE32(dst, bfSize);
slouken@0
   423
		SDL_WriteLE16(dst, bfReserved1);
slouken@0
   424
		SDL_WriteLE16(dst, bfReserved2);
slouken@0
   425
		SDL_WriteLE32(dst, bfOffBits);
slouken@0
   426
slouken@0
   427
		/* Set the BMP info values */
slouken@0
   428
		biSize = 40;
slouken@0
   429
		biWidth = surface->w;
slouken@0
   430
		biHeight = surface->h;
slouken@0
   431
		biPlanes = 1;
slouken@0
   432
		biBitCount = surface->format->BitsPerPixel;
slouken@0
   433
		biCompression = BI_RGB;
slouken@0
   434
		biSizeImage = surface->h*surface->pitch;
slouken@0
   435
		biXPelsPerMeter = 0;
slouken@0
   436
		biYPelsPerMeter = 0;
slouken@0
   437
		if ( surface->format->palette ) {
slouken@0
   438
			biClrUsed = surface->format->palette->ncolors;
slouken@0
   439
		} else {
slouken@0
   440
			biClrUsed = 0;
slouken@0
   441
		}
slouken@0
   442
		biClrImportant = 0;
slouken@0
   443
slouken@0
   444
		/* Write the BMP info values */
slouken@0
   445
		SDL_WriteLE32(dst, biSize);
slouken@0
   446
		SDL_WriteLE32(dst, biWidth);
slouken@0
   447
		SDL_WriteLE32(dst, biHeight);
slouken@0
   448
		SDL_WriteLE16(dst, biPlanes);
slouken@0
   449
		SDL_WriteLE16(dst, biBitCount);
slouken@0
   450
		SDL_WriteLE32(dst, biCompression);
slouken@0
   451
		SDL_WriteLE32(dst, biSizeImage);
slouken@0
   452
		SDL_WriteLE32(dst, biXPelsPerMeter);
slouken@0
   453
		SDL_WriteLE32(dst, biYPelsPerMeter);
slouken@0
   454
		SDL_WriteLE32(dst, biClrUsed);
slouken@0
   455
		SDL_WriteLE32(dst, biClrImportant);
slouken@0
   456
slouken@0
   457
		/* Write the palette (in BGR color order) */
slouken@0
   458
		if ( surface->format->palette ) {
slouken@0
   459
			SDL_Color *colors;
slouken@0
   460
			int       ncolors;
slouken@0
   461
slouken@0
   462
			colors = surface->format->palette->colors;
slouken@0
   463
			ncolors = surface->format->palette->ncolors;
slouken@0
   464
			for ( i=0; i<ncolors; ++i ) {
slouken@0
   465
				SDL_RWwrite(dst, &colors[i].b, 1, 1);
slouken@0
   466
				SDL_RWwrite(dst, &colors[i].g, 1, 1);
slouken@0
   467
				SDL_RWwrite(dst, &colors[i].r, 1, 1);
slouken@0
   468
				SDL_RWwrite(dst, &colors[i].unused, 1, 1);
slouken@0
   469
			}
slouken@0
   470
		}
slouken@0
   471
slouken@0
   472
		/* Write the bitmap offset */
slouken@0
   473
		bfOffBits = SDL_RWtell(dst)-fp_offset;
slouken@1330
   474
		if ( SDL_RWseek(dst, fp_offset+10, RW_SEEK_SET) < 0 ) {
slouken@0
   475
			SDL_Error(SDL_EFSEEK);
slouken@0
   476
		}
slouken@0
   477
		SDL_WriteLE32(dst, bfOffBits);
slouken@1330
   478
		if ( SDL_RWseek(dst, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
slouken@0
   479
			SDL_Error(SDL_EFSEEK);
slouken@0
   480
		}
slouken@0
   481
slouken@0
   482
		/* Write the bitmap image upside down */
slouken@0
   483
		bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@1012
   484
		pad  = ((bw%4) ? (4-(bw%4)) : 0);
slouken@0
   485
		while ( bits > (Uint8 *)surface->pixels ) {
slouken@0
   486
			bits -= surface->pitch;
slouken@1012
   487
			if ( SDL_RWwrite(dst, bits, 1, bw) != bw) {
slouken@0
   488
				SDL_Error(SDL_EFWRITE);
slouken@0
   489
				break;
slouken@0
   490
			}
slouken@0
   491
			if ( pad ) {
slouken@0
   492
				const Uint8 padbyte = 0;
slouken@0
   493
				for ( i=0; i<pad; ++i ) {
slouken@0
   494
					SDL_RWwrite(dst, &padbyte, 1, 1);
slouken@0
   495
				}
slouken@0
   496
			}
slouken@0
   497
		}
slouken@0
   498
slouken@0
   499
		/* Write the BMP file size */
slouken@0
   500
		bfSize = SDL_RWtell(dst)-fp_offset;
slouken@1330
   501
		if ( SDL_RWseek(dst, fp_offset+2, RW_SEEK_SET) < 0 ) {
slouken@0
   502
			SDL_Error(SDL_EFSEEK);
slouken@0
   503
		}
slouken@0
   504
		SDL_WriteLE32(dst, bfSize);
slouken@1330
   505
		if ( SDL_RWseek(dst, fp_offset+bfSize, RW_SEEK_SET) < 0 ) {
slouken@0
   506
			SDL_Error(SDL_EFSEEK);
slouken@0
   507
		}
slouken@0
   508
slouken@0
   509
		/* Close it up.. */
slouken@0
   510
		SDL_UnlockSurface(surface);
slouken@0
   511
		if ( surface != saveme ) {
slouken@0
   512
			SDL_FreeSurface(surface);
slouken@0
   513
		}
slouken@0
   514
	}
slouken@0
   515
slouken@0
   516
	if ( freedst && dst ) {
slouken@0
   517
		SDL_RWclose(dst);
slouken@0
   518
	}
slouken@1336
   519
	return((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
slouken@0
   520
}