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