19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   Code to load and save surfaces in Windows BMP format.
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   Why support BMP format?  Well, it's a native format for Windows, and
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   most image processing programs can read and write it.  It would be nice
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   to be able to have at least one image format that we can natively load
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   and save, and since PNG is so complex that it would bloat the library,
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   BMP is a good alternative.
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_endian.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Compression encodings for BMP files */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef BI_RGB
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BI_RGB		0
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BI_RLE8		1
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BI_RLE4		2
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BI_BITFIELDS	3
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_bool was_error;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long fp_offset = 0;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bmpPitch;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, pad;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *surface;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 Rmask;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 Gmask;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 Bmask;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Palette *palette;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *bits;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *top, *end;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_bool topDown;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int ExpandBMP;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Win32 BMP file header (14 bytes) */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char   magic[2];
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 bfSize;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 bfReserved1;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 bfReserved2;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 bfOffBits;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biSize;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biWidth;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biHeight;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 biPlanes;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 biBitCount;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biCompression;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biSizeImage;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biXPelsPerMeter;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biYPelsPerMeter;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biClrUsed;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biClrImportant;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure we are passed a valid data source */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface = NULL;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	was_error = SDL_FALSE;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( src == NULL ) {
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = SDL_TRUE;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read in the BMP file header */
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fp_offset = SDL_RWtell(src);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_ClearError();
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Error(SDL_EFREAD);
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = SDL_TRUE;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_strncmp(magic, "BM", 2) != 0 ) {
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("File is not a Windows BMP file");
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = SDL_TRUE;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bfSize		= SDL_ReadLE32(src);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bfReserved1	= SDL_ReadLE16(src);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bfReserved2	= SDL_ReadLE16(src);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bfOffBits	= SDL_ReadLE32(src);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read the Win32 BITMAPINFOHEADER */
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	biSize		= SDL_ReadLE32(src);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( biSize == 12 ) {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biWidth		= (Uint32)SDL_ReadLE16(src);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biHeight	= (Uint32)SDL_ReadLE16(src);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biPlanes	= SDL_ReadLE16(src);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biBitCount	= SDL_ReadLE16(src);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biCompression	= BI_RGB;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biSizeImage	= 0;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biXPelsPerMeter	= 0;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biYPelsPerMeter	= 0;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biClrUsed	= 0;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biClrImportant	= 0;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biWidth		= SDL_ReadLE32(src);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biHeight	= SDL_ReadLE32(src);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biPlanes	= SDL_ReadLE16(src);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biBitCount	= SDL_ReadLE16(src);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biCompression	= SDL_ReadLE32(src);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biSizeImage	= SDL_ReadLE32(src);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biXPelsPerMeter	= SDL_ReadLE32(src);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biYPelsPerMeter	= SDL_ReadLE32(src);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biClrUsed	= SDL_ReadLE32(src);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biClrImportant	= SDL_ReadLE32(src);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* stop some compiler warnings. */
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) bfSize;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) bfReserved1;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) bfReserved2;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) biPlanes;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) biSizeImage;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) biXPelsPerMeter;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) biYPelsPerMeter;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) biClrImportant;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (biHeight < 0) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		topDown = SDL_TRUE;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biHeight = -biHeight;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		topDown = SDL_FALSE;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check for read error */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_strcmp(SDL_GetError(), "") != 0 ) {
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = SDL_TRUE;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (biBitCount) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 1:
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 4:
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ExpandBMP = biBitCount;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			biBitCount = 8;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ExpandBMP = 0;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We don't support any BMP compression right now */
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Rmask = Gmask = Bmask = 0;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (biCompression) {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case BI_RGB:
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* If there are no masks, use the defaults */
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( bfOffBits == (14+biSize) ) {
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Default values for the BMP format */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch (biBitCount) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case 15:
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case 16:
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Rmask = 0x7C00;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Gmask = 0x03E0;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Bmask = 0x001F;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case 24:
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_BIG_ENDIAN
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					        Rmask = 0x000000FF;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					        Gmask = 0x0000FF00;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					        Bmask = 0x00FF0000;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case 32:
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Rmask = 0x00FF0000;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Gmask = 0x0000FF00;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Bmask = 0x000000FF;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					default:
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Fall through -- read the RGB masks */
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case BI_BITFIELDS:
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch (biBitCount) {
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case 15:
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case 16:
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case 32:
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Rmask = SDL_ReadLE32(src);
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Gmask = SDL_ReadLE32(src);
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Bmask = SDL_ReadLE32(src);
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				default:
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Compressed BMP files not supported");
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			was_error = SDL_TRUE;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			goto done;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create a compatible surface, note that the colors are RGB ordered */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, 0);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface == NULL ) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = SDL_TRUE;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load the palette, if any */
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	palette = (surface->format)->palette;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( palette ) {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( biClrUsed == 0 ) {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			biClrUsed = 1 << biBitCount;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( biSize == 12 ) {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i = 0; i < (int)biClrUsed; ++i ) {
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].b, 1, 1);
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].g, 1, 1);
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].r, 1, 1);
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				palette->colors[i].unused = 0;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i = 0; i < (int)biClrUsed; ++i ) {
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].b, 1, 1);
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].g, 1, 1);
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].r, 1, 1);
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		palette->ncolors = biClrUsed;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read the surface pixels.  Note that the bmp image is upside down */
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Error(SDL_EFSEEK);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		was_error = SDL_TRUE;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	top = (Uint8 *)surface->pixels;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (ExpandBMP) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 1:
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bmpPitch = (biWidth + 7) >> 3;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 4:
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bmpPitch = (biWidth + 1) >> 1;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pad  = ((surface->pitch%4) ?
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					(4-(surface->pitch%4)) : 0);
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( topDown ) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bits = top;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bits = end - surface->pitch;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( bits >= top && bits < end ) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (ExpandBMP) {
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 1:
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 4: {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint8 pixel = 0;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int   shift = (8-ExpandBMP);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<surface->w; ++i ) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( i%(8/ExpandBMP) == 0 ) {
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						SDL_SetError(
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					"Error reading from BMP");
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						was_error = SDL_TRUE;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						goto done;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(bits+i) = (pixel>>shift);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel <<= ExpandBMP;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} }
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_RWread(src, bits, 1, surface->pitch)
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							 != surface->pitch ) {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Error(SDL_EFREAD);
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				was_error = SDL_TRUE;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				goto done;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_BIG_ENDIAN
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Byte-swap the pixels if needed. Note that the 24bpp
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   case has already been taken care of above. */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch(biBitCount) {
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case 15:
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case 16: {
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				        Uint16 *pix = (Uint16 *)bits;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for(i = 0; i < surface->w; i++)
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					        pix[i] = SDL_Swap16(pix[i]);
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case 32: {
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				        Uint32 *pix = (Uint32 *)bits;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for(i = 0; i < surface->w; i++)
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					        pix[i] = SDL_Swap32(pix[i]);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Skip padding bytes, ugh */
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pad ) {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint8 padbyte;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<pad; ++i ) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWread(src, &padbyte, 1, 1);
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( topDown ) {
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bits += surface->pitch;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bits -= surface->pitch;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldone:
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( was_error ) {
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( src ) {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_RWseek(src, fp_offset, RW_SEEK_SET);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface ) {
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_FreeSurface(surface);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface = NULL;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( freesrc && src ) {
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_RWclose(src);
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(surface);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SaveBMP_RW (SDL_Surface *saveme, SDL_RWops *dst, int freedst)
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long fp_offset;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, pad;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *surface;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *bits;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Win32 BMP file header (14 bytes) */
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char   magic[2] = { 'B', 'M' };
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 bfSize;
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 bfReserved1;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 bfReserved2;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 bfOffBits;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biSize;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biWidth;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biHeight;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 biPlanes;
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 biBitCount;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biCompression;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biSizeImage;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biXPelsPerMeter;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 biYPelsPerMeter;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biClrUsed;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 biClrImportant;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure we have somewhere to save */
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface = NULL;
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst ) {
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( saveme->format->palette ) {
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( saveme->format->BitsPerPixel == 8 ) {
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				surface = saveme;
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_SetError("%d bpp BMP files not supported",
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						saveme->format->BitsPerPixel);
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else if ( (saveme->format->BitsPerPixel == 24) &&
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_LIL_ENDIAN
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(saveme->format->Rmask == 0x00FF0000) &&
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(saveme->format->Gmask == 0x0000FF00) &&
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(saveme->format->Bmask == 0x000000FF)
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(saveme->format->Rmask == 0x000000FF) &&
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(saveme->format->Gmask == 0x0000FF00) &&
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(saveme->format->Bmask == 0x00FF0000)
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  ) {
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface = saveme;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Rect bounds;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Convert to 24 bits per pixel */
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					saveme->w, saveme->h, 24,
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_LIL_ENDIAN
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					0x00FF0000, 0x0000FF00, 0x000000FF,
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					0x000000FF, 0x0000FF00, 0x00FF0000,
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					0);
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( surface != NULL ) {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bounds.x = 0;
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bounds.y = 0;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bounds.w = saveme->w;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bounds.h = saveme->h;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( SDL_LowerBlit(saveme, &bounds, surface,
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							&bounds) < 0 ) {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_FreeSurface(surface);
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_SetError(
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					"Couldn't convert image to 24 bpp");
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					surface = NULL;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface && (SDL_LockSurface(surface) == 0) ) {
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const int bw = surface->w*surface->format->BytesPerPixel;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set the BMP file header values */
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bfSize = 0;		 /* We'll write this when we're done */
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bfReserved1 = 0;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bfReserved2 = 0;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bfOffBits = 0;		/* We'll write this when we're done */
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Write the BMP file header values */
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fp_offset = SDL_RWtell(dst);
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_ClearError();
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_RWwrite(dst, magic, 2, 1);
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, bfSize);
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE16(dst, bfReserved1);
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE16(dst, bfReserved2);
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, bfOffBits);
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set the BMP info values */
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biSize = 40;
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biWidth = surface->w;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biHeight = surface->h;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biPlanes = 1;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biBitCount = surface->format->BitsPerPixel;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biCompression = BI_RGB;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biSizeImage = surface->h*surface->pitch;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biXPelsPerMeter = 0;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biYPelsPerMeter = 0;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface->format->palette ) {
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			biClrUsed = surface->format->palette->ncolors;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			biClrUsed = 0;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		biClrImportant = 0;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Write the BMP info values */
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biSize);
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biWidth);
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biHeight);
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE16(dst, biPlanes);
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE16(dst, biBitCount);
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biCompression);
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biSizeImage);
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biXPelsPerMeter);
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biYPelsPerMeter);
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biClrUsed);
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, biClrImportant);
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Write the palette (in BGR color order) */
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface->format->palette ) {
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Color *colors;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int       ncolors;
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			colors = surface->format->palette->colors;
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ncolors = surface->format->palette->ncolors;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<ncolors; ++i ) {
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWwrite(dst, &colors[i].b, 1, 1);
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWwrite(dst, &colors[i].g, 1, 1);
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWwrite(dst, &colors[i].r, 1, 1);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_RWwrite(dst, &colors[i].unused, 1, 1);
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Write the bitmap offset */
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bfOffBits = SDL_RWtell(dst)-fp_offset;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_RWseek(dst, fp_offset+10, RW_SEEK_SET) < 0 ) {
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Error(SDL_EFSEEK);
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, bfOffBits);
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_RWseek(dst, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Error(SDL_EFSEEK);
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Write the bitmap image upside down */
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pad  = ((bw%4) ? (4-(bw%4)) : 0);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while ( bits > (Uint8 *)surface->pixels ) {
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bits -= surface->pitch;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_RWwrite(dst, bits, 1, bw) != bw) {
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Error(SDL_EFWRITE);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( pad ) {
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				const Uint8 padbyte = 0;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( i=0; i<pad; ++i ) {
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_RWwrite(dst, &padbyte, 1, 1);
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Write the BMP file size */
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bfSize = SDL_RWtell(dst)-fp_offset;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_RWseek(dst, fp_offset+2, RW_SEEK_SET) < 0 ) {
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Error(SDL_EFSEEK);
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_WriteLE32(dst, bfSize);
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_RWseek(dst, fp_offset+bfSize, RW_SEEK_SET) < 0 ) {
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Error(SDL_EFSEEK);
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Close it up.. */
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnlockSurface(surface);
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface != saveme ) {
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_FreeSurface(surface);
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( freedst && dst ) {
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_RWclose(dst);
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
550