IMG_bmp.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 08 Nov 2009 17:13:36 +0000
changeset 236 cce1251de477
parent 202 8e1d88c68bda
child 237 d1a4fae44bd0
permissions -rw-r--r--
Use RW_SEEK constants for RWseek()
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@186
     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@121
     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@121
     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@121
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@121
    15
    You should have received a copy of the GNU Lesser General Public
slouken@121
    16
    License along with this library; if not, write to the Free Software
slouken@121
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* This is a BMP image file loading framework */
couriersud@189
    24
/* ICO/CUR file support is here as well since it uses similar internal
couriersud@189
    25
 * representation */
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
icculus@154
    41
	if ( !src )
icculus@154
    42
		return 0;
slouken@117
    43
	start = SDL_RWtell(src);
slouken@0
    44
	is_BMP = 0;
slouken@117
    45
	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
slouken@0
    46
		if ( strncmp(magic, "BM", 2) == 0 ) {
slouken@0
    47
			is_BMP = 1;
slouken@0
    48
		}
slouken@0
    49
	}
slouken@236
    50
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@0
    51
	return(is_BMP);
slouken@0
    52
}
slouken@0
    53
couriersud@189
    54
static int IMG_isICOCUR(SDL_RWops *src, int type)
couriersud@189
    55
{
couriersud@189
    56
	int start;
couriersud@189
    57
	int is_ICOCUR;
couriersud@189
    58
couriersud@189
    59
	/* The Win32 ICO file header (14 bytes) */
couriersud@189
    60
    Uint16 bfReserved;
couriersud@189
    61
    Uint16 bfType;
couriersud@189
    62
    Uint16 bfCount;
couriersud@189
    63
couriersud@189
    64
	if ( !src )
couriersud@189
    65
		return 0;
couriersud@189
    66
	start = SDL_RWtell(src);
couriersud@189
    67
	is_ICOCUR = 0;
couriersud@189
    68
    bfReserved = SDL_ReadLE16(src);
couriersud@189
    69
    bfType = SDL_ReadLE16(src);
couriersud@189
    70
    bfCount = SDL_ReadLE16(src);
couriersud@189
    71
    if ((bfReserved == 0) && (bfType == type) && (bfCount != 0)) 
couriersud@189
    72
    	is_ICOCUR = 1;
slouken@236
    73
	SDL_RWseek(src, start, RW_SEEK_SET);
couriersud@189
    74
couriersud@189
    75
	return (is_ICOCUR);
couriersud@189
    76
}
couriersud@189
    77
couriersud@189
    78
int IMG_isICO(SDL_RWops *src)
couriersud@189
    79
{
couriersud@189
    80
	return IMG_isICOCUR(src, 1);
couriersud@189
    81
}
couriersud@189
    82
couriersud@189
    83
int IMG_isCUR(SDL_RWops *src)
couriersud@189
    84
{
couriersud@189
    85
	return IMG_isICOCUR(src, 2);
couriersud@189
    86
}
couriersud@189
    87
slouken@101
    88
#include "SDL_error.h"
slouken@101
    89
#include "SDL_video.h"
slouken@101
    90
#include "SDL_endian.h"
slouken@101
    91
slouken@101
    92
/* Compression encodings for BMP files */
slouken@101
    93
#ifndef BI_RGB
slouken@101
    94
#define BI_RGB		0
slouken@101
    95
#define BI_RLE8		1
slouken@101
    96
#define BI_RLE4		2
slouken@101
    97
#define BI_BITFIELDS	3
slouken@101
    98
#endif
slouken@101
    99
slouken@101
   100
static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
slouken@101
   101
{
slouken@101
   102
	/*
slouken@101
   103
	| Sets the surface pixels from src.  A bmp image is upside down.
slouken@101
   104
	*/
slouken@101
   105
	int pitch = surface->pitch;
slouken@101
   106
	int height = surface->h;
slouken@184
   107
	Uint8 *start = (Uint8 *)surface->pixels;
slouken@184
   108
	Uint8 *end = start + (height*pitch);
slouken@184
   109
	Uint8 *bits = end-pitch, *spot;
slouken@101
   110
	int ofs = 0;
slouken@101
   111
	Uint8 ch;
slouken@101
   112
	Uint8 needsPad;
slouken@101
   113
slouken@184
   114
#define COPY_PIXEL(x)	spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x)
slouken@184
   115
slouken@101
   116
	for (;;) {
slouken@101
   117
		if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   118
		/*
slouken@101
   119
		| encoded mode starts with a run length, and then a byte
slouken@101
   120
		| with two colour indexes to alternate between for the run
slouken@101
   121
		*/
slouken@101
   122
		if ( ch ) {
slouken@101
   123
			Uint8 pixel;
slouken@101
   124
			if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
slouken@101
   125
			if ( isRle8 ) {                 /* 256-color bitmap, compressed */
slouken@101
   126
				do {
slouken@184
   127
					COPY_PIXEL(pixel);
slouken@101
   128
				} while (--ch);
slouken@184
   129
			} else {                         /* 16-color bitmap, compressed */
slouken@101
   130
				Uint8 pixel0 = pixel >> 4;
slouken@101
   131
				Uint8 pixel1 = pixel & 0x0F;
slouken@101
   132
				for (;;) {
slouken@184
   133
					COPY_PIXEL(pixel0);	/* even count, high nibble */
slouken@101
   134
					if (!--ch) break;
slouken@184
   135
					COPY_PIXEL(pixel1);	/* odd count, low nibble */
slouken@101
   136
					if (!--ch) break;
slouken@101
   137
				}
slouken@101
   138
			}
slouken@101
   139
		} else {
slouken@101
   140
			/*
slouken@101
   141
			| A leading zero is an escape; it may signal the end of the bitmap,
slouken@101
   142
			| a cursor move, or some absolute data.
slouken@101
   143
			| zero tag may be absolute mode or an escape
slouken@101
   144
			*/
slouken@101
   145
			if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   146
			switch (ch) {
slouken@101
   147
			case 0:                         /* end of line */
slouken@101
   148
				ofs = 0;
slouken@101
   149
				bits -= pitch;               /* go to previous */
slouken@101
   150
				break;
slouken@101
   151
			case 1:                         /* end of bitmap */
slouken@101
   152
				return 0;                    /* success! */
slouken@101
   153
			case 2:                         /* delta */
slouken@101
   154
				if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   155
				ofs += ch;
slouken@101
   156
				if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   157
				bits -= (ch * pitch);
slouken@101
   158
				break;
slouken@101
   159
			default:                        /* no compression */
slouken@101
   160
				if (isRle8) {
slouken@101
   161
					needsPad = ( ch & 1 );
slouken@101
   162
					do {
slouken@184
   163
						Uint8 pixel;
slouken@184
   164
						if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
slouken@184
   165
						COPY_PIXEL(pixel);
slouken@101
   166
					} while (--ch);
slouken@101
   167
				} else {
slouken@101
   168
					needsPad = ( ((ch+1)>>1) & 1 ); /* (ch+1)>>1: bytes size */
slouken@101
   169
					for (;;) {
slouken@101
   170
						Uint8 pixel;
slouken@101
   171
						if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
slouken@184
   172
						COPY_PIXEL(pixel >> 4);
slouken@101
   173
						if (!--ch) break;
slouken@184
   174
						COPY_PIXEL(pixel & 0x0F);
slouken@101
   175
						if (!--ch) break;
slouken@101
   176
					}
slouken@101
   177
				}
slouken@101
   178
				/* pad at even boundary */
slouken@101
   179
				if ( needsPad && !SDL_RWread(src, &ch, 1, 1) ) return 1;
slouken@101
   180
				break;
slouken@101
   181
			}
slouken@101
   182
		}
slouken@101
   183
	}
slouken@101
   184
}
slouken@101
   185
slouken@101
   186
static SDL_Surface *LoadBMP_RW (SDL_RWops *src, int freesrc)
slouken@101
   187
{
slouken@202
   188
	SDL_bool was_error;
slouken@101
   189
	long fp_offset;
slouken@101
   190
	int bmpPitch;
slouken@101
   191
	int i, pad;
slouken@101
   192
	SDL_Surface *surface;
slouken@101
   193
	Uint32 Rmask;
slouken@101
   194
	Uint32 Gmask;
slouken@101
   195
	Uint32 Bmask;
slouken@106
   196
	Uint32 Amask;
slouken@101
   197
	SDL_Palette *palette;
slouken@101
   198
	Uint8 *bits;
slouken@202
   199
	Uint8 *top, *end;
slouken@202
   200
	SDL_bool topDown;
slouken@101
   201
	int ExpandBMP;
slouken@101
   202
slouken@101
   203
	/* The Win32 BMP file header (14 bytes) */
slouken@101
   204
	char   magic[2];
slouken@101
   205
	Uint32 bfSize;
slouken@101
   206
	Uint16 bfReserved1;
slouken@101
   207
	Uint16 bfReserved2;
slouken@101
   208
	Uint32 bfOffBits;
slouken@101
   209
slouken@101
   210
	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@101
   211
	Uint32 biSize;
slouken@101
   212
	Sint32 biWidth;
slouken@101
   213
	Sint32 biHeight;
slouken@101
   214
	Uint16 biPlanes;
slouken@101
   215
	Uint16 biBitCount;
slouken@101
   216
	Uint32 biCompression;
slouken@101
   217
	Uint32 biSizeImage;
slouken@101
   218
	Sint32 biXPelsPerMeter;
slouken@101
   219
	Sint32 biYPelsPerMeter;
slouken@101
   220
	Uint32 biClrUsed;
slouken@101
   221
	Uint32 biClrImportant;
slouken@101
   222
slouken@101
   223
	/* Make sure we are passed a valid data source */
slouken@101
   224
	surface = NULL;
slouken@202
   225
	was_error = SDL_FALSE;
slouken@101
   226
	if ( src == NULL ) {
slouken@202
   227
		was_error = SDL_TRUE;
slouken@101
   228
		goto done;
slouken@101
   229
	}
slouken@101
   230
slouken@101
   231
	/* Read in the BMP file header */
slouken@101
   232
	fp_offset = SDL_RWtell(src);
slouken@101
   233
	SDL_ClearError();
slouken@101
   234
	if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
slouken@101
   235
		SDL_Error(SDL_EFREAD);
slouken@202
   236
		was_error = SDL_TRUE;
slouken@101
   237
		goto done;
slouken@101
   238
	}
slouken@101
   239
	if ( strncmp(magic, "BM", 2) != 0 ) {
slouken@101
   240
		SDL_SetError("File is not a Windows BMP file");
slouken@202
   241
		was_error = SDL_TRUE;
slouken@101
   242
		goto done;
slouken@101
   243
	}
slouken@101
   244
	bfSize		= SDL_ReadLE32(src);
slouken@101
   245
	bfReserved1	= SDL_ReadLE16(src);
slouken@101
   246
	bfReserved2	= SDL_ReadLE16(src);
slouken@101
   247
	bfOffBits	= SDL_ReadLE32(src);
slouken@101
   248
slouken@101
   249
	/* Read the Win32 BITMAPINFOHEADER */
slouken@101
   250
	biSize		= SDL_ReadLE32(src);
slouken@101
   251
	if ( biSize == 12 ) {
slouken@101
   252
		biWidth		= (Uint32)SDL_ReadLE16(src);
slouken@101
   253
		biHeight	= (Uint32)SDL_ReadLE16(src);
slouken@101
   254
		biPlanes	= SDL_ReadLE16(src);
slouken@101
   255
		biBitCount	= SDL_ReadLE16(src);
slouken@101
   256
		biCompression	= BI_RGB;
slouken@101
   257
		biSizeImage	= 0;
slouken@101
   258
		biXPelsPerMeter	= 0;
slouken@101
   259
		biYPelsPerMeter	= 0;
slouken@101
   260
		biClrUsed	= 0;
slouken@101
   261
		biClrImportant	= 0;
slouken@101
   262
	} else {
slouken@101
   263
		biWidth		= SDL_ReadLE32(src);
slouken@101
   264
		biHeight	= SDL_ReadLE32(src);
slouken@101
   265
		biPlanes	= SDL_ReadLE16(src);
slouken@101
   266
		biBitCount	= SDL_ReadLE16(src);
slouken@101
   267
		biCompression	= SDL_ReadLE32(src);
slouken@101
   268
		biSizeImage	= SDL_ReadLE32(src);
slouken@101
   269
		biXPelsPerMeter	= SDL_ReadLE32(src);
slouken@101
   270
		biYPelsPerMeter	= SDL_ReadLE32(src);
slouken@101
   271
		biClrUsed	= SDL_ReadLE32(src);
slouken@101
   272
		biClrImportant	= SDL_ReadLE32(src);
slouken@101
   273
	}
slouken@202
   274
	if (biHeight < 0) {
slouken@202
   275
		topDown = SDL_TRUE;
slouken@202
   276
		biHeight = -biHeight;
slouken@202
   277
	} else {
slouken@202
   278
		topDown = SDL_FALSE;
slouken@202
   279
	}
slouken@101
   280
slouken@101
   281
	/* Check for read error */
slouken@101
   282
	if ( strcmp(SDL_GetError(), "") != 0 ) {
slouken@202
   283
		was_error = SDL_TRUE;
slouken@101
   284
		goto done;
slouken@101
   285
	}
slouken@101
   286
slouken@101
   287
	/* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
slouken@101
   288
	switch (biBitCount) {
slouken@101
   289
		case 1:
slouken@101
   290
		case 4:
slouken@101
   291
			ExpandBMP = biBitCount;
slouken@101
   292
			biBitCount = 8;
slouken@101
   293
			break;
slouken@101
   294
		default:
slouken@101
   295
			ExpandBMP = 0;
slouken@101
   296
			break;
slouken@101
   297
	}
slouken@101
   298
slouken@101
   299
	/* RLE4 and RLE8 BMP compression is supported */
slouken@106
   300
	Rmask = Gmask = Bmask = Amask = 0;
slouken@101
   301
	switch (biCompression) {
slouken@101
   302
		case BI_RGB:
slouken@101
   303
			/* If there are no masks, use the defaults */
slouken@101
   304
			if ( bfOffBits == (14+biSize) ) {
slouken@101
   305
				/* Default values for the BMP format */
slouken@101
   306
				switch (biBitCount) {
slouken@101
   307
					case 15:
slouken@101
   308
					case 16:
slouken@101
   309
						Rmask = 0x7C00;
slouken@101
   310
						Gmask = 0x03E0;
slouken@101
   311
						Bmask = 0x001F;
slouken@101
   312
						break;
slouken@101
   313
					case 24:
slouken@101
   314
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   315
					        Rmask = 0x000000FF;
slouken@101
   316
					        Gmask = 0x0000FF00;
slouken@101
   317
					        Bmask = 0x00FF0000;
slouken@106
   318
#else
slouken@106
   319
						Rmask = 0x00FF0000;
slouken@106
   320
						Gmask = 0x0000FF00;
slouken@106
   321
						Bmask = 0x000000FF;
slouken@106
   322
#endif
slouken@101
   323
						break;
slouken@101
   324
					case 32:
slouken@106
   325
						Amask = 0xFF000000;
slouken@101
   326
						Rmask = 0x00FF0000;
slouken@101
   327
						Gmask = 0x0000FF00;
slouken@101
   328
						Bmask = 0x000000FF;
slouken@101
   329
						break;
slouken@101
   330
					default:
slouken@101
   331
						break;
slouken@101
   332
				}
slouken@101
   333
				break;
slouken@101
   334
			}
slouken@101
   335
			/* Fall through -- read the RGB masks */
slouken@101
   336
slouken@101
   337
		default:
slouken@101
   338
			switch (biBitCount) {
slouken@101
   339
				case 15:
slouken@101
   340
				case 16:
slouken@101
   341
				case 32:
slouken@101
   342
					Rmask = SDL_ReadLE32(src);
slouken@101
   343
					Gmask = SDL_ReadLE32(src);
slouken@101
   344
					Bmask = SDL_ReadLE32(src);
slouken@106
   345
					Amask = SDL_ReadLE32(src);
slouken@101
   346
					break;
slouken@101
   347
				default:
slouken@101
   348
					break;
slouken@101
   349
			}
slouken@101
   350
			break;
slouken@101
   351
	}
slouken@101
   352
slouken@101
   353
	/* Create a compatible surface, note that the colors are RGB ordered */
slouken@101
   354
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@106
   355
			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, Amask);
slouken@101
   356
	if ( surface == NULL ) {
slouken@202
   357
		was_error = SDL_TRUE;
slouken@101
   358
		goto done;
slouken@101
   359
	}
slouken@101
   360
slouken@101
   361
	/* Load the palette, if any */
slouken@101
   362
	palette = (surface->format)->palette;
slouken@101
   363
	if ( palette ) {
slouken@236
   364
		if ( SDL_RWseek(src, fp_offset+14+biSize, RW_SEEK_SET) < 0 ) {
slouken@151
   365
			SDL_Error(SDL_EFSEEK);
slouken@202
   366
			was_error = SDL_TRUE;
slouken@151
   367
			goto done;
slouken@151
   368
		}
slouken@151
   369
slouken@101
   370
		/*
slouken@101
   371
		| guich: always use 1<<bpp b/c some bitmaps can bring wrong information
slouken@101
   372
		| for colorsUsed
slouken@101
   373
		*/
slouken@101
   374
		/* if ( biClrUsed == 0 ) {  */
slouken@101
   375
		biClrUsed = 1 << biBitCount;
slouken@101
   376
		/* } */
slouken@101
   377
		if ( biSize == 12 ) {
slouken@101
   378
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   379
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   380
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   381
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   382
				palette->colors[i].unused = 0;
slouken@101
   383
			}	
slouken@101
   384
		} else {
slouken@101
   385
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   386
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   387
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   388
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   389
				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
slouken@101
   390
			}	
slouken@101
   391
		}
slouken@101
   392
		palette->ncolors = biClrUsed;
slouken@101
   393
	}
slouken@101
   394
slouken@101
   395
	/* Read the surface pixels.  Note that the bmp image is upside down */
slouken@236
   396
	if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
slouken@101
   397
		SDL_Error(SDL_EFSEEK);
slouken@202
   398
		was_error = SDL_TRUE;
slouken@101
   399
		goto done;
slouken@101
   400
	}
slouken@101
   401
	if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
slouken@101
   402
		was_error = readRlePixels(surface, src, biCompression == BI_RLE8);
slouken@101
   403
		if (was_error) SDL_SetError("Error reading from BMP");
slouken@101
   404
		goto done;
slouken@101
   405
	}
slouken@202
   406
	top = (Uint8 *)surface->pixels;
slouken@202
   407
	end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@101
   408
	switch (ExpandBMP) {
slouken@101
   409
		case 1:
slouken@101
   410
			bmpPitch = (biWidth + 7) >> 3;
slouken@101
   411
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   412
			break;
slouken@101
   413
		case 4:
slouken@101
   414
			bmpPitch = (biWidth + 1) >> 1;
slouken@101
   415
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   416
			break;
slouken@101
   417
		default:
slouken@101
   418
			pad  = ((surface->pitch%4) ?
slouken@101
   419
					(4-(surface->pitch%4)) : 0);
slouken@101
   420
			break;
slouken@101
   421
	}
slouken@202
   422
	if ( topDown ) {
slouken@202
   423
		bits = top;
slouken@202
   424
	} else {
slouken@202
   425
		bits = end - surface->pitch;
slouken@202
   426
	}
slouken@202
   427
	while ( bits >= top && bits < end ) {
slouken@101
   428
		switch (ExpandBMP) {
slouken@101
   429
			case 1:
slouken@101
   430
			case 4: {
slouken@101
   431
			Uint8 pixel = 0;
slouken@101
   432
			int   shift = (8-ExpandBMP);
slouken@101
   433
			for ( i=0; i<surface->w; ++i ) {
slouken@101
   434
				if ( i%(8/ExpandBMP) == 0 ) {
slouken@101
   435
					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
slouken@101
   436
						SDL_SetError(
slouken@101
   437
					"Error reading from BMP");
slouken@202
   438
						was_error = SDL_TRUE;
slouken@101
   439
						goto done;
slouken@101
   440
					}
slouken@101
   441
				}
slouken@101
   442
				*(bits+i) = (pixel>>shift);
slouken@101
   443
				pixel <<= ExpandBMP;
slouken@101
   444
			} }
slouken@101
   445
			break;
slouken@101
   446
slouken@101
   447
			default:
slouken@101
   448
			if ( SDL_RWread(src, bits, 1, surface->pitch)
slouken@101
   449
							 != surface->pitch ) {
slouken@101
   450
				SDL_Error(SDL_EFREAD);
slouken@202
   451
				was_error = SDL_TRUE;
slouken@101
   452
				goto done;
slouken@101
   453
			}
slouken@101
   454
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   455
			/* Byte-swap the pixels if needed. Note that the 24bpp
slouken@101
   456
			   case has already been taken care of above. */
slouken@101
   457
			switch(biBitCount) {
slouken@101
   458
				case 15:
slouken@101
   459
				case 16: {
slouken@101
   460
				        Uint16 *pix = (Uint16 *)bits;
slouken@101
   461
					for(i = 0; i < surface->w; i++)
slouken@101
   462
					        pix[i] = SDL_Swap16(pix[i]);
slouken@101
   463
					break;
slouken@101
   464
				}
slouken@101
   465
slouken@101
   466
				case 32: {
slouken@101
   467
				        Uint32 *pix = (Uint32 *)bits;
slouken@101
   468
					for(i = 0; i < surface->w; i++)
slouken@101
   469
					        pix[i] = SDL_Swap32(pix[i]);
slouken@101
   470
					break;
slouken@101
   471
				}
slouken@101
   472
			}
slouken@101
   473
#endif
slouken@101
   474
			break;
slouken@101
   475
		}
slouken@101
   476
		/* Skip padding bytes, ugh */
slouken@101
   477
		if ( pad ) {
slouken@101
   478
			Uint8 padbyte;
slouken@101
   479
			for ( i=0; i<pad; ++i ) {
slouken@101
   480
				SDL_RWread(src, &padbyte, 1, 1);
slouken@101
   481
			}
slouken@101
   482
		}
slouken@202
   483
		if ( topDown ) {
slouken@202
   484
			bits += surface->pitch;
slouken@202
   485
		} else {
slouken@202
   486
			bits -= surface->pitch;
slouken@202
   487
		}
slouken@101
   488
	}
slouken@101
   489
done:
slouken@101
   490
	if ( was_error ) {
slouken@118
   491
		if ( src ) {
slouken@236
   492
			SDL_RWseek(src, fp_offset, RW_SEEK_SET);
slouken@118
   493
		}
slouken@101
   494
		if ( surface ) {
slouken@101
   495
			SDL_FreeSurface(surface);
slouken@101
   496
		}
slouken@101
   497
		surface = NULL;
slouken@101
   498
	}
slouken@101
   499
	if ( freesrc && src ) {
slouken@101
   500
		SDL_RWclose(src);
slouken@101
   501
	}
slouken@101
   502
	return(surface);
slouken@101
   503
}
slouken@101
   504
couriersud@189
   505
static Uint8
couriersud@189
   506
SDL_Read8(SDL_RWops * src)
couriersud@189
   507
{
couriersud@189
   508
    Uint8 value;
couriersud@189
   509
couriersud@189
   510
    SDL_RWread(src, &value, 1, 1);
couriersud@189
   511
    return (value);
couriersud@189
   512
}
couriersud@189
   513
couriersud@189
   514
static SDL_Surface *
couriersud@189
   515
LoadICOCUR_RW(SDL_RWops * src, int type, int freesrc)
couriersud@189
   516
{
slouken@202
   517
    SDL_bool was_error;
couriersud@189
   518
    long fp_offset;
couriersud@189
   519
    int bmpPitch;
couriersud@189
   520
    int i, pad;
couriersud@189
   521
    SDL_Surface *surface;
couriersud@189
   522
    Uint32 Rmask;
couriersud@189
   523
    Uint32 Gmask;
couriersud@189
   524
    Uint32 Bmask;
couriersud@189
   525
    Uint8 *bits;
couriersud@189
   526
    int ExpandBMP;
couriersud@189
   527
    int maxCol = 0;
couriersud@189
   528
    int icoOfs = 0;
couriersud@189
   529
    Uint32 palette[256];
couriersud@189
   530
couriersud@189
   531
    /* The Win32 ICO file header (14 bytes) */
couriersud@189
   532
    Uint16 bfReserved;
couriersud@189
   533
    Uint16 bfType;
couriersud@189
   534
    Uint16 bfCount;
couriersud@189
   535
couriersud@189
   536
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
couriersud@189
   537
    Uint32 biSize;
couriersud@189
   538
    Sint32 biWidth;
couriersud@189
   539
    Sint32 biHeight;
couriersud@189
   540
    Uint16 biPlanes;
couriersud@189
   541
    Uint16 biBitCount;
couriersud@189
   542
    Uint32 biCompression;
couriersud@189
   543
    Uint32 biSizeImage;
couriersud@189
   544
    Sint32 biXPelsPerMeter;
couriersud@189
   545
    Sint32 biYPelsPerMeter;
couriersud@189
   546
    Uint32 biClrUsed;
couriersud@189
   547
    Uint32 biClrImportant;
couriersud@189
   548
couriersud@189
   549
    /* Make sure we are passed a valid data source */
couriersud@189
   550
    surface = NULL;
slouken@202
   551
    was_error = SDL_FALSE;
couriersud@189
   552
    if (src == NULL) {
slouken@202
   553
        was_error = SDL_TRUE;
couriersud@189
   554
        goto done;
couriersud@189
   555
    }
couriersud@189
   556
couriersud@189
   557
    /* Read in the ICO file header */
couriersud@189
   558
    fp_offset = SDL_RWtell(src);
couriersud@189
   559
    SDL_ClearError();
couriersud@189
   560
couriersud@189
   561
    bfReserved = SDL_ReadLE16(src);
couriersud@189
   562
    bfType = SDL_ReadLE16(src);
couriersud@189
   563
    bfCount = SDL_ReadLE16(src);
couriersud@189
   564
    if ((bfReserved != 0) || (bfType != type) || (bfCount == 0)) {
couriersud@189
   565
        SDL_SetError("File is not a Windows %s file", type == 1 ? "ICO" : "CUR");
slouken@202
   566
        was_error = SDL_TRUE;
couriersud@189
   567
        goto done;
couriersud@189
   568
    }
couriersud@189
   569
couriersud@189
   570
    /* Read the Win32 Icon Directory */
couriersud@189
   571
    for (i = 0; i < bfCount; i++) {
couriersud@189
   572
        /* Icon Directory Entries */
couriersud@189
   573
        int bWidth = SDL_Read8(src);    /* Uint8, but 0 = 256 ! */
couriersud@189
   574
        int bHeight = SDL_Read8(src);   /* Uint8, but 0 = 256 ! */
couriersud@189
   575
        int bColorCount = SDL_Read8(src);       /* Uint8, but 0 = 256 ! */
couriersud@189
   576
        Uint8 bReserved = SDL_Read8(src);
couriersud@189
   577
        Uint16 wPlanes = SDL_ReadLE16(src);
couriersud@189
   578
        Uint16 wBitCount = SDL_ReadLE16(src);
couriersud@189
   579
        Uint32 dwBytesInRes = SDL_ReadLE32(src);
couriersud@189
   580
        Uint32 dwImageOffset = SDL_ReadLE32(src);
couriersud@189
   581
couriersud@189
   582
        if (!bWidth)
couriersud@189
   583
            bWidth = 256;
couriersud@189
   584
        if (!bHeight)
couriersud@189
   585
            bHeight = 256;
couriersud@189
   586
        if (!bColorCount)
couriersud@189
   587
            bColorCount = 256;
couriersud@189
   588
couriersud@189
   589
        //printf("%dx%d@%d - %08x\n", bWidth, bHeight, bColorCount, dwImageOffset);
couriersud@189
   590
        if (bColorCount > maxCol) {
couriersud@189
   591
            maxCol = bColorCount;
couriersud@189
   592
            icoOfs = dwImageOffset;
couriersud@189
   593
            //printf("marked\n");
couriersud@189
   594
        }
couriersud@189
   595
    }
couriersud@189
   596
couriersud@189
   597
    /* Advance to the DIB Data */
couriersud@189
   598
    if (SDL_RWseek(src, icoOfs, RW_SEEK_SET) < 0) {
couriersud@189
   599
        SDL_Error(SDL_EFSEEK);
slouken@202
   600
        was_error = SDL_TRUE;
couriersud@189
   601
        goto done;
couriersud@189
   602
    }
couriersud@189
   603
couriersud@189
   604
    /* Read the Win32 BITMAPINFOHEADER */
couriersud@189
   605
    biSize = SDL_ReadLE32(src);
couriersud@189
   606
    if (biSize == 40) {
couriersud@189
   607
        biWidth = SDL_ReadLE32(src);
couriersud@189
   608
        biHeight = SDL_ReadLE32(src);
couriersud@189
   609
        biPlanes = SDL_ReadLE16(src);
couriersud@189
   610
        biBitCount = SDL_ReadLE16(src);
couriersud@189
   611
        biCompression = SDL_ReadLE32(src);
couriersud@189
   612
        biSizeImage = SDL_ReadLE32(src);
couriersud@189
   613
        biXPelsPerMeter = SDL_ReadLE32(src);
couriersud@189
   614
        biYPelsPerMeter = SDL_ReadLE32(src);
couriersud@189
   615
        biClrUsed = SDL_ReadLE32(src);
couriersud@189
   616
        biClrImportant = SDL_ReadLE32(src);
couriersud@189
   617
    } else {
couriersud@189
   618
        SDL_SetError("Unsupported ICO bitmap format");
slouken@202
   619
        was_error = SDL_TRUE;
couriersud@189
   620
        goto done;
couriersud@189
   621
    }
couriersud@189
   622
couriersud@189
   623
    /* Check for read error */
couriersud@189
   624
    if (SDL_strcmp(SDL_GetError(), "") != 0) {
slouken@202
   625
        was_error = SDL_TRUE;
couriersud@189
   626
        goto done;
couriersud@189
   627
    }
couriersud@189
   628
couriersud@189
   629
    /* We don't support any BMP compression right now */
couriersud@189
   630
    switch (biCompression) {
couriersud@189
   631
    case BI_RGB:
couriersud@189
   632
        /* Default values for the BMP format */
couriersud@189
   633
        switch (biBitCount) {
couriersud@189
   634
        case 1:
couriersud@189
   635
        case 4:
couriersud@189
   636
            ExpandBMP = biBitCount;
couriersud@189
   637
            biBitCount = 8;
couriersud@189
   638
            break;
couriersud@189
   639
        case 8:
couriersud@189
   640
            ExpandBMP = 8;
couriersud@189
   641
            break;
couriersud@189
   642
        case 32:
couriersud@189
   643
            Rmask = 0x00FF0000;
couriersud@189
   644
            Gmask = 0x0000FF00;
couriersud@189
   645
            Bmask = 0x000000FF;
couriersud@189
   646
            ExpandBMP = 0;
couriersud@189
   647
            break;
couriersud@189
   648
        default:
couriersud@189
   649
            SDL_SetError("ICO file with unsupported bit count");
slouken@202
   650
            was_error = SDL_TRUE;
couriersud@189
   651
            goto done;
couriersud@189
   652
        }
couriersud@189
   653
        break;
couriersud@189
   654
    default:
couriersud@189
   655
        SDL_SetError("Compressed ICO files not supported");
slouken@202
   656
        was_error = SDL_TRUE;
couriersud@189
   657
        goto done;
couriersud@189
   658
    }
couriersud@189
   659
couriersud@189
   660
    /* Create a RGBA surface */
couriersud@189
   661
    biHeight = biHeight >> 1;
couriersud@189
   662
    //printf("%d x %d\n", biWidth, biHeight);
couriersud@189
   663
    surface =
couriersud@189
   664
        SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
couriersud@189
   665
                             0x0000FF00, 0x000000FF, 0xFF000000);
couriersud@189
   666
    if (surface == NULL) {
slouken@202
   667
        was_error = SDL_TRUE;
couriersud@189
   668
        goto done;
couriersud@189
   669
    }
couriersud@189
   670
couriersud@189
   671
    /* Load the palette, if any */
couriersud@189
   672
    //printf("bc %d bused %d\n", biBitCount, biClrUsed);
couriersud@189
   673
    if (biBitCount <= 8) {
couriersud@189
   674
        if (biClrUsed == 0) {
couriersud@189
   675
            biClrUsed = 1 << biBitCount;
couriersud@189
   676
        }
couriersud@189
   677
        for (i = 0; i < (int) biClrUsed; ++i) {
couriersud@189
   678
            SDL_RWread(src, &palette[i], 4, 1);
couriersud@189
   679
        }
couriersud@189
   680
    }
couriersud@189
   681
couriersud@189
   682
    /* Read the surface pixels.  Note that the bmp image is upside down */
couriersud@189
   683
    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
couriersud@189
   684
    switch (ExpandBMP) {
couriersud@189
   685
    case 1:
couriersud@189
   686
        bmpPitch = (biWidth + 7) >> 3;
couriersud@189
   687
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   688
        break;
couriersud@189
   689
    case 4:
couriersud@189
   690
        bmpPitch = (biWidth + 1) >> 1;
couriersud@189
   691
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   692
        break;
couriersud@189
   693
    case 8:
couriersud@189
   694
        bmpPitch = biWidth;
couriersud@189
   695
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   696
        break;
couriersud@189
   697
    default:
couriersud@189
   698
        bmpPitch = biWidth * 4;
couriersud@189
   699
        pad = 0;
couriersud@189
   700
        break;
couriersud@189
   701
    }
couriersud@189
   702
    while (bits > (Uint8 *) surface->pixels) {
couriersud@189
   703
        bits -= surface->pitch;
couriersud@189
   704
        switch (ExpandBMP) {
couriersud@189
   705
        case 1:
couriersud@189
   706
        case 4:
couriersud@189
   707
        case 8:
couriersud@189
   708
            {
couriersud@189
   709
                Uint8 pixel = 0;
couriersud@189
   710
                int shift = (8 - ExpandBMP);
couriersud@189
   711
                for (i = 0; i < surface->w; ++i) {
couriersud@189
   712
                    if (i % (8 / ExpandBMP) == 0) {
couriersud@189
   713
                        if (!SDL_RWread(src, &pixel, 1, 1)) {
couriersud@189
   714
                            SDL_SetError("Error reading from ICO");
slouken@202
   715
                            was_error = SDL_TRUE;
couriersud@189
   716
                            goto done;
couriersud@189
   717
                        }
couriersud@189
   718
                    }
couriersud@189
   719
                    *((Uint32 *) bits + i) = (palette[pixel >> shift]);
couriersud@189
   720
                    pixel <<= ExpandBMP;
couriersud@189
   721
                }
couriersud@189
   722
            }
couriersud@189
   723
            break;
couriersud@189
   724
couriersud@189
   725
        default:
couriersud@189
   726
            if (SDL_RWread(src, bits, 1, surface->pitch)
couriersud@189
   727
                != surface->pitch) {
couriersud@189
   728
                SDL_Error(SDL_EFREAD);
slouken@202
   729
                was_error = SDL_TRUE;
couriersud@189
   730
                goto done;
couriersud@189
   731
            }
couriersud@189
   732
            break;
couriersud@189
   733
        }
couriersud@189
   734
        /* Skip padding bytes, ugh */
couriersud@189
   735
        if (pad) {
couriersud@189
   736
            Uint8 padbyte;
couriersud@189
   737
            for (i = 0; i < pad; ++i) {
couriersud@189
   738
                SDL_RWread(src, &padbyte, 1, 1);
couriersud@189
   739
            }
couriersud@189
   740
        }
couriersud@189
   741
    }
couriersud@189
   742
    /* Read the mask pixels.  Note that the bmp image is upside down */
couriersud@189
   743
    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
couriersud@189
   744
    ExpandBMP = 1;
couriersud@189
   745
    bmpPitch = (biWidth + 7) >> 3;
couriersud@189
   746
    pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   747
    while (bits > (Uint8 *) surface->pixels) {
couriersud@189
   748
        Uint8 pixel = 0;
couriersud@189
   749
        int shift = (8 - ExpandBMP);
couriersud@189
   750
couriersud@189
   751
        bits -= surface->pitch;
couriersud@189
   752
        for (i = 0; i < surface->w; ++i) {
couriersud@189
   753
            if (i % (8 / ExpandBMP) == 0) {
couriersud@189
   754
                if (!SDL_RWread(src, &pixel, 1, 1)) {
couriersud@189
   755
                    SDL_SetError("Error reading from ICO");
slouken@202
   756
                    was_error = SDL_TRUE;
couriersud@189
   757
                    goto done;
couriersud@189
   758
                }
couriersud@189
   759
            }
couriersud@189
   760
            *((Uint32 *) bits + i) |= ((pixel >> shift) ? 0 : 0xFF000000);
couriersud@189
   761
            pixel <<= ExpandBMP;
couriersud@189
   762
        }
couriersud@189
   763
        /* Skip padding bytes, ugh */
couriersud@189
   764
        if (pad) {
couriersud@189
   765
            Uint8 padbyte;
couriersud@189
   766
            for (i = 0; i < pad; ++i) {
couriersud@189
   767
                SDL_RWread(src, &padbyte, 1, 1);
couriersud@189
   768
            }
couriersud@189
   769
        }
couriersud@189
   770
    }
couriersud@189
   771
  done:
couriersud@189
   772
    if (was_error) {
couriersud@189
   773
        if (src) {
couriersud@189
   774
            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
couriersud@189
   775
        }
couriersud@189
   776
        if (surface) {
couriersud@189
   777
            SDL_FreeSurface(surface);
couriersud@189
   778
        }
couriersud@189
   779
        surface = NULL;
couriersud@189
   780
    }
couriersud@189
   781
    if (freesrc && src) {
couriersud@189
   782
        SDL_RWclose(src);
couriersud@189
   783
    }
couriersud@189
   784
    return (surface);
couriersud@189
   785
}
couriersud@189
   786
slouken@0
   787
/* Load a BMP type image from an SDL datasource */
slouken@0
   788
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   789
{
slouken@101
   790
	return(LoadBMP_RW(src, 0));
slouken@0
   791
}
slouken@0
   792
couriersud@189
   793
/* Load a ICO type image from an SDL datasource */
couriersud@189
   794
SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
couriersud@189
   795
{
couriersud@189
   796
	return(LoadICOCUR_RW(src, 1, 0));
couriersud@189
   797
}
couriersud@189
   798
couriersud@189
   799
/* Load a CUR type image from an SDL datasource */
couriersud@189
   800
SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
couriersud@189
   801
{
couriersud@189
   802
	return(LoadICOCUR_RW(src, 2, 0));
couriersud@189
   803
}
couriersud@189
   804
slouken@0
   805
#else
slouken@0
   806
slouken@0
   807
/* See if an image is contained in a data source */
slouken@0
   808
int IMG_isBMP(SDL_RWops *src)
slouken@0
   809
{
slouken@0
   810
	return(0);
slouken@0
   811
}
slouken@0
   812
couriersud@189
   813
int IMG_isICO(SDL_RWops *src)
couriersud@189
   814
{
couriersud@189
   815
	return(0);
couriersud@189
   816
}
couriersud@189
   817
couriersud@189
   818
int IMG_isCUR(SDL_RWops *src)
couriersud@189
   819
{
couriersud@189
   820
	return(0);
couriersud@189
   821
}
couriersud@189
   822
slouken@0
   823
/* Load a BMP type image from an SDL datasource */
slouken@0
   824
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   825
{
slouken@0
   826
	return(NULL);
slouken@0
   827
}
slouken@0
   828
couriersud@189
   829
/* Load a BMP type image from an SDL datasource */
couriersud@189
   830
SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
couriersud@189
   831
{
couriersud@189
   832
	return(NULL);
couriersud@189
   833
}
couriersud@189
   834
couriersud@189
   835
/* Load a BMP type image from an SDL datasource */
couriersud@189
   836
SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
couriersud@189
   837
{
couriersud@189
   838
	return(NULL);
couriersud@189
   839
}
couriersud@189
   840
slouken@0
   841
#endif /* LOAD_BMP */