IMG_bmp.c
author couriersud
Tue, 13 Jan 2009 01:21:00 +0000
changeset 189 39d5ac4ec0f8
parent 186 5fbbeabf647f
child 202 8e1d88c68bda
permissions -rw-r--r--
Added ICO and CUR support to SDL_image
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@117
    50
	SDL_RWseek(src, start, 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;
couriersud@189
    73
	SDL_RWseek(src, start, 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@101
   188
	int 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@101
   199
	int ExpandBMP;
slouken@101
   200
slouken@101
   201
	/* The Win32 BMP file header (14 bytes) */
slouken@101
   202
	char   magic[2];
slouken@101
   203
	Uint32 bfSize;
slouken@101
   204
	Uint16 bfReserved1;
slouken@101
   205
	Uint16 bfReserved2;
slouken@101
   206
	Uint32 bfOffBits;
slouken@101
   207
slouken@101
   208
	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@101
   209
	Uint32 biSize;
slouken@101
   210
	Sint32 biWidth;
slouken@101
   211
	Sint32 biHeight;
slouken@101
   212
	Uint16 biPlanes;
slouken@101
   213
	Uint16 biBitCount;
slouken@101
   214
	Uint32 biCompression;
slouken@101
   215
	Uint32 biSizeImage;
slouken@101
   216
	Sint32 biXPelsPerMeter;
slouken@101
   217
	Sint32 biYPelsPerMeter;
slouken@101
   218
	Uint32 biClrUsed;
slouken@101
   219
	Uint32 biClrImportant;
slouken@101
   220
slouken@101
   221
	/* Make sure we are passed a valid data source */
slouken@101
   222
	surface = NULL;
slouken@101
   223
	was_error = 0;
slouken@101
   224
	if ( src == NULL ) {
slouken@101
   225
		was_error = 1;
slouken@101
   226
		goto done;
slouken@101
   227
	}
slouken@101
   228
slouken@101
   229
	/* Read in the BMP file header */
slouken@101
   230
	fp_offset = SDL_RWtell(src);
slouken@101
   231
	SDL_ClearError();
slouken@101
   232
	if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
slouken@101
   233
		SDL_Error(SDL_EFREAD);
slouken@101
   234
		was_error = 1;
slouken@101
   235
		goto done;
slouken@101
   236
	}
slouken@101
   237
	if ( strncmp(magic, "BM", 2) != 0 ) {
slouken@101
   238
		SDL_SetError("File is not a Windows BMP file");
slouken@101
   239
		was_error = 1;
slouken@101
   240
		goto done;
slouken@101
   241
	}
slouken@101
   242
	bfSize		= SDL_ReadLE32(src);
slouken@101
   243
	bfReserved1	= SDL_ReadLE16(src);
slouken@101
   244
	bfReserved2	= SDL_ReadLE16(src);
slouken@101
   245
	bfOffBits	= SDL_ReadLE32(src);
slouken@101
   246
slouken@101
   247
	/* Read the Win32 BITMAPINFOHEADER */
slouken@101
   248
	biSize		= SDL_ReadLE32(src);
slouken@101
   249
	if ( biSize == 12 ) {
slouken@101
   250
		biWidth		= (Uint32)SDL_ReadLE16(src);
slouken@101
   251
		biHeight	= (Uint32)SDL_ReadLE16(src);
slouken@101
   252
		biPlanes	= SDL_ReadLE16(src);
slouken@101
   253
		biBitCount	= SDL_ReadLE16(src);
slouken@101
   254
		biCompression	= BI_RGB;
slouken@101
   255
		biSizeImage	= 0;
slouken@101
   256
		biXPelsPerMeter	= 0;
slouken@101
   257
		biYPelsPerMeter	= 0;
slouken@101
   258
		biClrUsed	= 0;
slouken@101
   259
		biClrImportant	= 0;
slouken@101
   260
	} else {
slouken@101
   261
		biWidth		= SDL_ReadLE32(src);
slouken@101
   262
		biHeight	= SDL_ReadLE32(src);
slouken@101
   263
		biPlanes	= SDL_ReadLE16(src);
slouken@101
   264
		biBitCount	= SDL_ReadLE16(src);
slouken@101
   265
		biCompression	= SDL_ReadLE32(src);
slouken@101
   266
		biSizeImage	= SDL_ReadLE32(src);
slouken@101
   267
		biXPelsPerMeter	= SDL_ReadLE32(src);
slouken@101
   268
		biYPelsPerMeter	= SDL_ReadLE32(src);
slouken@101
   269
		biClrUsed	= SDL_ReadLE32(src);
slouken@101
   270
		biClrImportant	= SDL_ReadLE32(src);
slouken@101
   271
	}
slouken@101
   272
slouken@101
   273
	/* Check for read error */
slouken@101
   274
	if ( strcmp(SDL_GetError(), "") != 0 ) {
slouken@101
   275
		was_error = 1;
slouken@101
   276
		goto done;
slouken@101
   277
	}
slouken@101
   278
slouken@101
   279
	/* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
slouken@101
   280
	switch (biBitCount) {
slouken@101
   281
		case 1:
slouken@101
   282
		case 4:
slouken@101
   283
			ExpandBMP = biBitCount;
slouken@101
   284
			biBitCount = 8;
slouken@101
   285
			break;
slouken@101
   286
		default:
slouken@101
   287
			ExpandBMP = 0;
slouken@101
   288
			break;
slouken@101
   289
	}
slouken@101
   290
slouken@101
   291
	/* RLE4 and RLE8 BMP compression is supported */
slouken@106
   292
	Rmask = Gmask = Bmask = Amask = 0;
slouken@101
   293
	switch (biCompression) {
slouken@101
   294
		case BI_RGB:
slouken@101
   295
			/* If there are no masks, use the defaults */
slouken@101
   296
			if ( bfOffBits == (14+biSize) ) {
slouken@101
   297
				/* Default values for the BMP format */
slouken@101
   298
				switch (biBitCount) {
slouken@101
   299
					case 15:
slouken@101
   300
					case 16:
slouken@101
   301
						Rmask = 0x7C00;
slouken@101
   302
						Gmask = 0x03E0;
slouken@101
   303
						Bmask = 0x001F;
slouken@101
   304
						break;
slouken@101
   305
					case 24:
slouken@101
   306
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   307
					        Rmask = 0x000000FF;
slouken@101
   308
					        Gmask = 0x0000FF00;
slouken@101
   309
					        Bmask = 0x00FF0000;
slouken@106
   310
#else
slouken@106
   311
						Rmask = 0x00FF0000;
slouken@106
   312
						Gmask = 0x0000FF00;
slouken@106
   313
						Bmask = 0x000000FF;
slouken@106
   314
#endif
slouken@101
   315
						break;
slouken@101
   316
					case 32:
slouken@106
   317
						Amask = 0xFF000000;
slouken@101
   318
						Rmask = 0x00FF0000;
slouken@101
   319
						Gmask = 0x0000FF00;
slouken@101
   320
						Bmask = 0x000000FF;
slouken@101
   321
						break;
slouken@101
   322
					default:
slouken@101
   323
						break;
slouken@101
   324
				}
slouken@101
   325
				break;
slouken@101
   326
			}
slouken@101
   327
			/* Fall through -- read the RGB masks */
slouken@101
   328
slouken@101
   329
		default:
slouken@101
   330
			switch (biBitCount) {
slouken@101
   331
				case 15:
slouken@101
   332
				case 16:
slouken@101
   333
				case 32:
slouken@101
   334
					Rmask = SDL_ReadLE32(src);
slouken@101
   335
					Gmask = SDL_ReadLE32(src);
slouken@101
   336
					Bmask = SDL_ReadLE32(src);
slouken@106
   337
					Amask = SDL_ReadLE32(src);
slouken@101
   338
					break;
slouken@101
   339
				default:
slouken@101
   340
					break;
slouken@101
   341
			}
slouken@101
   342
			break;
slouken@101
   343
	}
slouken@101
   344
slouken@101
   345
	/* Create a compatible surface, note that the colors are RGB ordered */
slouken@101
   346
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@106
   347
			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, Amask);
slouken@101
   348
	if ( surface == NULL ) {
slouken@101
   349
		was_error = 1;
slouken@101
   350
		goto done;
slouken@101
   351
	}
slouken@101
   352
slouken@101
   353
	/* Load the palette, if any */
slouken@101
   354
	palette = (surface->format)->palette;
slouken@101
   355
	if ( palette ) {
slouken@151
   356
		if ( SDL_RWseek(src, fp_offset+14+biSize, SEEK_SET) < 0 ) {
slouken@151
   357
			SDL_Error(SDL_EFSEEK);
slouken@151
   358
			was_error = 1;
slouken@151
   359
			goto done;
slouken@151
   360
		}
slouken@151
   361
slouken@101
   362
		/*
slouken@101
   363
		| guich: always use 1<<bpp b/c some bitmaps can bring wrong information
slouken@101
   364
		| for colorsUsed
slouken@101
   365
		*/
slouken@101
   366
		/* if ( biClrUsed == 0 ) {  */
slouken@101
   367
		biClrUsed = 1 << biBitCount;
slouken@101
   368
		/* } */
slouken@101
   369
		if ( biSize == 12 ) {
slouken@101
   370
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   371
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   372
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   373
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   374
				palette->colors[i].unused = 0;
slouken@101
   375
			}	
slouken@101
   376
		} else {
slouken@101
   377
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   378
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   379
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   380
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   381
				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
slouken@101
   382
			}	
slouken@101
   383
		}
slouken@101
   384
		palette->ncolors = biClrUsed;
slouken@101
   385
	}
slouken@101
   386
slouken@101
   387
	/* Read the surface pixels.  Note that the bmp image is upside down */
slouken@101
   388
	if ( SDL_RWseek(src, fp_offset+bfOffBits, SEEK_SET) < 0 ) {
slouken@101
   389
		SDL_Error(SDL_EFSEEK);
slouken@101
   390
		was_error = 1;
slouken@101
   391
		goto done;
slouken@101
   392
	}
slouken@101
   393
	if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
slouken@101
   394
		was_error = readRlePixels(surface, src, biCompression == BI_RLE8);
slouken@101
   395
		if (was_error) SDL_SetError("Error reading from BMP");
slouken@101
   396
		goto done;
slouken@101
   397
	}
slouken@101
   398
	bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@101
   399
	switch (ExpandBMP) {
slouken@101
   400
		case 1:
slouken@101
   401
			bmpPitch = (biWidth + 7) >> 3;
slouken@101
   402
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   403
			break;
slouken@101
   404
		case 4:
slouken@101
   405
			bmpPitch = (biWidth + 1) >> 1;
slouken@101
   406
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   407
			break;
slouken@101
   408
		default:
slouken@101
   409
			pad  = ((surface->pitch%4) ?
slouken@101
   410
					(4-(surface->pitch%4)) : 0);
slouken@101
   411
			break;
slouken@101
   412
	}
slouken@101
   413
	while ( bits > (Uint8 *)surface->pixels ) {
slouken@101
   414
		bits -= surface->pitch;
slouken@101
   415
		switch (ExpandBMP) {
slouken@101
   416
			case 1:
slouken@101
   417
			case 4: {
slouken@101
   418
			Uint8 pixel = 0;
slouken@101
   419
			int   shift = (8-ExpandBMP);
slouken@101
   420
			for ( i=0; i<surface->w; ++i ) {
slouken@101
   421
				if ( i%(8/ExpandBMP) == 0 ) {
slouken@101
   422
					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
slouken@101
   423
						SDL_SetError(
slouken@101
   424
					"Error reading from BMP");
slouken@101
   425
						was_error = 1;
slouken@101
   426
						goto done;
slouken@101
   427
					}
slouken@101
   428
				}
slouken@101
   429
				*(bits+i) = (pixel>>shift);
slouken@101
   430
				pixel <<= ExpandBMP;
slouken@101
   431
			} }
slouken@101
   432
			break;
slouken@101
   433
slouken@101
   434
			default:
slouken@101
   435
			if ( SDL_RWread(src, bits, 1, surface->pitch)
slouken@101
   436
							 != surface->pitch ) {
slouken@101
   437
				SDL_Error(SDL_EFREAD);
slouken@101
   438
				was_error = 1;
slouken@101
   439
				goto done;
slouken@101
   440
			}
slouken@101
   441
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   442
			/* Byte-swap the pixels if needed. Note that the 24bpp
slouken@101
   443
			   case has already been taken care of above. */
slouken@101
   444
			switch(biBitCount) {
slouken@101
   445
				case 15:
slouken@101
   446
				case 16: {
slouken@101
   447
				        Uint16 *pix = (Uint16 *)bits;
slouken@101
   448
					for(i = 0; i < surface->w; i++)
slouken@101
   449
					        pix[i] = SDL_Swap16(pix[i]);
slouken@101
   450
					break;
slouken@101
   451
				}
slouken@101
   452
slouken@101
   453
				case 32: {
slouken@101
   454
				        Uint32 *pix = (Uint32 *)bits;
slouken@101
   455
					for(i = 0; i < surface->w; i++)
slouken@101
   456
					        pix[i] = SDL_Swap32(pix[i]);
slouken@101
   457
					break;
slouken@101
   458
				}
slouken@101
   459
			}
slouken@101
   460
#endif
slouken@101
   461
			break;
slouken@101
   462
		}
slouken@101
   463
		/* Skip padding bytes, ugh */
slouken@101
   464
		if ( pad ) {
slouken@101
   465
			Uint8 padbyte;
slouken@101
   466
			for ( i=0; i<pad; ++i ) {
slouken@101
   467
				SDL_RWread(src, &padbyte, 1, 1);
slouken@101
   468
			}
slouken@101
   469
		}
slouken@101
   470
	}
slouken@101
   471
done:
slouken@101
   472
	if ( was_error ) {
slouken@118
   473
		if ( src ) {
slouken@118
   474
			SDL_RWseek(src, fp_offset, SEEK_SET);
slouken@118
   475
		}
slouken@101
   476
		if ( surface ) {
slouken@101
   477
			SDL_FreeSurface(surface);
slouken@101
   478
		}
slouken@101
   479
		surface = NULL;
slouken@101
   480
	}
slouken@101
   481
	if ( freesrc && src ) {
slouken@101
   482
		SDL_RWclose(src);
slouken@101
   483
	}
slouken@101
   484
	return(surface);
slouken@101
   485
}
slouken@101
   486
couriersud@189
   487
static Uint8
couriersud@189
   488
SDL_Read8(SDL_RWops * src)
couriersud@189
   489
{
couriersud@189
   490
    Uint8 value;
couriersud@189
   491
couriersud@189
   492
    SDL_RWread(src, &value, 1, 1);
couriersud@189
   493
    return (value);
couriersud@189
   494
}
couriersud@189
   495
couriersud@189
   496
static SDL_Surface *
couriersud@189
   497
LoadICOCUR_RW(SDL_RWops * src, int type, int freesrc)
couriersud@189
   498
{
couriersud@189
   499
    int was_error;
couriersud@189
   500
    long fp_offset;
couriersud@189
   501
    int bmpPitch;
couriersud@189
   502
    int i, pad;
couriersud@189
   503
    SDL_Surface *surface;
couriersud@189
   504
    Uint32 Rmask;
couriersud@189
   505
    Uint32 Gmask;
couriersud@189
   506
    Uint32 Bmask;
couriersud@189
   507
    Uint8 *bits;
couriersud@189
   508
    int ExpandBMP;
couriersud@189
   509
    int maxCol = 0;
couriersud@189
   510
    int icoOfs = 0;
couriersud@189
   511
    Uint32 palette[256];
couriersud@189
   512
couriersud@189
   513
    /* The Win32 ICO file header (14 bytes) */
couriersud@189
   514
    Uint16 bfReserved;
couriersud@189
   515
    Uint16 bfType;
couriersud@189
   516
    Uint16 bfCount;
couriersud@189
   517
couriersud@189
   518
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
couriersud@189
   519
    Uint32 biSize;
couriersud@189
   520
    Sint32 biWidth;
couriersud@189
   521
    Sint32 biHeight;
couriersud@189
   522
    Uint16 biPlanes;
couriersud@189
   523
    Uint16 biBitCount;
couriersud@189
   524
    Uint32 biCompression;
couriersud@189
   525
    Uint32 biSizeImage;
couriersud@189
   526
    Sint32 biXPelsPerMeter;
couriersud@189
   527
    Sint32 biYPelsPerMeter;
couriersud@189
   528
    Uint32 biClrUsed;
couriersud@189
   529
    Uint32 biClrImportant;
couriersud@189
   530
couriersud@189
   531
    /* Make sure we are passed a valid data source */
couriersud@189
   532
    surface = NULL;
couriersud@189
   533
    was_error = 0;
couriersud@189
   534
    if (src == NULL) {
couriersud@189
   535
        was_error = 1;
couriersud@189
   536
        goto done;
couriersud@189
   537
    }
couriersud@189
   538
couriersud@189
   539
    /* Read in the ICO file header */
couriersud@189
   540
    fp_offset = SDL_RWtell(src);
couriersud@189
   541
    SDL_ClearError();
couriersud@189
   542
couriersud@189
   543
    bfReserved = SDL_ReadLE16(src);
couriersud@189
   544
    bfType = SDL_ReadLE16(src);
couriersud@189
   545
    bfCount = SDL_ReadLE16(src);
couriersud@189
   546
    if ((bfReserved != 0) || (bfType != type) || (bfCount == 0)) {
couriersud@189
   547
        SDL_SetError("File is not a Windows %s file", type == 1 ? "ICO" : "CUR");
couriersud@189
   548
        was_error = 1;
couriersud@189
   549
        goto done;
couriersud@189
   550
    }
couriersud@189
   551
couriersud@189
   552
    /* Read the Win32 Icon Directory */
couriersud@189
   553
    for (i = 0; i < bfCount; i++) {
couriersud@189
   554
        /* Icon Directory Entries */
couriersud@189
   555
        int bWidth = SDL_Read8(src);    /* Uint8, but 0 = 256 ! */
couriersud@189
   556
        int bHeight = SDL_Read8(src);   /* Uint8, but 0 = 256 ! */
couriersud@189
   557
        int bColorCount = SDL_Read8(src);       /* Uint8, but 0 = 256 ! */
couriersud@189
   558
        Uint8 bReserved = SDL_Read8(src);
couriersud@189
   559
        Uint16 wPlanes = SDL_ReadLE16(src);
couriersud@189
   560
        Uint16 wBitCount = SDL_ReadLE16(src);
couriersud@189
   561
        Uint32 dwBytesInRes = SDL_ReadLE32(src);
couriersud@189
   562
        Uint32 dwImageOffset = SDL_ReadLE32(src);
couriersud@189
   563
couriersud@189
   564
        if (!bWidth)
couriersud@189
   565
            bWidth = 256;
couriersud@189
   566
        if (!bHeight)
couriersud@189
   567
            bHeight = 256;
couriersud@189
   568
        if (!bColorCount)
couriersud@189
   569
            bColorCount = 256;
couriersud@189
   570
couriersud@189
   571
        //printf("%dx%d@%d - %08x\n", bWidth, bHeight, bColorCount, dwImageOffset);
couriersud@189
   572
        if (bColorCount > maxCol) {
couriersud@189
   573
            maxCol = bColorCount;
couriersud@189
   574
            icoOfs = dwImageOffset;
couriersud@189
   575
            //printf("marked\n");
couriersud@189
   576
        }
couriersud@189
   577
    }
couriersud@189
   578
couriersud@189
   579
    /* Advance to the DIB Data */
couriersud@189
   580
    if (SDL_RWseek(src, icoOfs, RW_SEEK_SET) < 0) {
couriersud@189
   581
        SDL_Error(SDL_EFSEEK);
couriersud@189
   582
        was_error = 1;
couriersud@189
   583
        goto done;
couriersud@189
   584
    }
couriersud@189
   585
couriersud@189
   586
    /* Read the Win32 BITMAPINFOHEADER */
couriersud@189
   587
    biSize = SDL_ReadLE32(src);
couriersud@189
   588
    if (biSize == 40) {
couriersud@189
   589
        biWidth = SDL_ReadLE32(src);
couriersud@189
   590
        biHeight = SDL_ReadLE32(src);
couriersud@189
   591
        biPlanes = SDL_ReadLE16(src);
couriersud@189
   592
        biBitCount = SDL_ReadLE16(src);
couriersud@189
   593
        biCompression = SDL_ReadLE32(src);
couriersud@189
   594
        biSizeImage = SDL_ReadLE32(src);
couriersud@189
   595
        biXPelsPerMeter = SDL_ReadLE32(src);
couriersud@189
   596
        biYPelsPerMeter = SDL_ReadLE32(src);
couriersud@189
   597
        biClrUsed = SDL_ReadLE32(src);
couriersud@189
   598
        biClrImportant = SDL_ReadLE32(src);
couriersud@189
   599
    } else {
couriersud@189
   600
        SDL_SetError("Unsupported ICO bitmap format");
couriersud@189
   601
        was_error = 1;
couriersud@189
   602
        goto done;
couriersud@189
   603
    }
couriersud@189
   604
couriersud@189
   605
    /* Check for read error */
couriersud@189
   606
    if (SDL_strcmp(SDL_GetError(), "") != 0) {
couriersud@189
   607
        was_error = 1;
couriersud@189
   608
        goto done;
couriersud@189
   609
    }
couriersud@189
   610
couriersud@189
   611
    /* We don't support any BMP compression right now */
couriersud@189
   612
    switch (biCompression) {
couriersud@189
   613
    case BI_RGB:
couriersud@189
   614
        /* Default values for the BMP format */
couriersud@189
   615
        switch (biBitCount) {
couriersud@189
   616
        case 1:
couriersud@189
   617
        case 4:
couriersud@189
   618
            ExpandBMP = biBitCount;
couriersud@189
   619
            biBitCount = 8;
couriersud@189
   620
            break;
couriersud@189
   621
        case 8:
couriersud@189
   622
            ExpandBMP = 8;
couriersud@189
   623
            break;
couriersud@189
   624
        case 32:
couriersud@189
   625
            Rmask = 0x00FF0000;
couriersud@189
   626
            Gmask = 0x0000FF00;
couriersud@189
   627
            Bmask = 0x000000FF;
couriersud@189
   628
            ExpandBMP = 0;
couriersud@189
   629
            break;
couriersud@189
   630
        default:
couriersud@189
   631
            SDL_SetError("ICO file with unsupported bit count");
couriersud@189
   632
            was_error = 1;
couriersud@189
   633
            goto done;
couriersud@189
   634
        }
couriersud@189
   635
        break;
couriersud@189
   636
    default:
couriersud@189
   637
        SDL_SetError("Compressed ICO files not supported");
couriersud@189
   638
        was_error = 1;
couriersud@189
   639
        goto done;
couriersud@189
   640
    }
couriersud@189
   641
couriersud@189
   642
    /* Create a RGBA surface */
couriersud@189
   643
    biHeight = biHeight >> 1;
couriersud@189
   644
    //printf("%d x %d\n", biWidth, biHeight);
couriersud@189
   645
    surface =
couriersud@189
   646
        SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
couriersud@189
   647
                             0x0000FF00, 0x000000FF, 0xFF000000);
couriersud@189
   648
    if (surface == NULL) {
couriersud@189
   649
        was_error = 1;
couriersud@189
   650
        goto done;
couriersud@189
   651
    }
couriersud@189
   652
couriersud@189
   653
    /* Load the palette, if any */
couriersud@189
   654
    //printf("bc %d bused %d\n", biBitCount, biClrUsed);
couriersud@189
   655
    if (biBitCount <= 8) {
couriersud@189
   656
        if (biClrUsed == 0) {
couriersud@189
   657
            biClrUsed = 1 << biBitCount;
couriersud@189
   658
        }
couriersud@189
   659
        for (i = 0; i < (int) biClrUsed; ++i) {
couriersud@189
   660
            SDL_RWread(src, &palette[i], 4, 1);
couriersud@189
   661
        }
couriersud@189
   662
    }
couriersud@189
   663
couriersud@189
   664
    /* Read the surface pixels.  Note that the bmp image is upside down */
couriersud@189
   665
    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
couriersud@189
   666
    switch (ExpandBMP) {
couriersud@189
   667
    case 1:
couriersud@189
   668
        bmpPitch = (biWidth + 7) >> 3;
couriersud@189
   669
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   670
        break;
couriersud@189
   671
    case 4:
couriersud@189
   672
        bmpPitch = (biWidth + 1) >> 1;
couriersud@189
   673
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   674
        break;
couriersud@189
   675
    case 8:
couriersud@189
   676
        bmpPitch = biWidth;
couriersud@189
   677
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   678
        break;
couriersud@189
   679
    default:
couriersud@189
   680
        bmpPitch = biWidth * 4;
couriersud@189
   681
        pad = 0;
couriersud@189
   682
        break;
couriersud@189
   683
    }
couriersud@189
   684
    while (bits > (Uint8 *) surface->pixels) {
couriersud@189
   685
        bits -= surface->pitch;
couriersud@189
   686
        switch (ExpandBMP) {
couriersud@189
   687
        case 1:
couriersud@189
   688
        case 4:
couriersud@189
   689
        case 8:
couriersud@189
   690
            {
couriersud@189
   691
                Uint8 pixel = 0;
couriersud@189
   692
                int shift = (8 - ExpandBMP);
couriersud@189
   693
                for (i = 0; i < surface->w; ++i) {
couriersud@189
   694
                    if (i % (8 / ExpandBMP) == 0) {
couriersud@189
   695
                        if (!SDL_RWread(src, &pixel, 1, 1)) {
couriersud@189
   696
                            SDL_SetError("Error reading from ICO");
couriersud@189
   697
                            was_error = 1;
couriersud@189
   698
                            goto done;
couriersud@189
   699
                        }
couriersud@189
   700
                    }
couriersud@189
   701
                    *((Uint32 *) bits + i) = (palette[pixel >> shift]);
couriersud@189
   702
                    pixel <<= ExpandBMP;
couriersud@189
   703
                }
couriersud@189
   704
            }
couriersud@189
   705
            break;
couriersud@189
   706
couriersud@189
   707
        default:
couriersud@189
   708
            if (SDL_RWread(src, bits, 1, surface->pitch)
couriersud@189
   709
                != surface->pitch) {
couriersud@189
   710
                SDL_Error(SDL_EFREAD);
couriersud@189
   711
                was_error = 1;
couriersud@189
   712
                goto done;
couriersud@189
   713
            }
couriersud@189
   714
            break;
couriersud@189
   715
        }
couriersud@189
   716
        /* Skip padding bytes, ugh */
couriersud@189
   717
        if (pad) {
couriersud@189
   718
            Uint8 padbyte;
couriersud@189
   719
            for (i = 0; i < pad; ++i) {
couriersud@189
   720
                SDL_RWread(src, &padbyte, 1, 1);
couriersud@189
   721
            }
couriersud@189
   722
        }
couriersud@189
   723
    }
couriersud@189
   724
    /* Read the mask pixels.  Note that the bmp image is upside down */
couriersud@189
   725
    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
couriersud@189
   726
    ExpandBMP = 1;
couriersud@189
   727
    bmpPitch = (biWidth + 7) >> 3;
couriersud@189
   728
    pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   729
    while (bits > (Uint8 *) surface->pixels) {
couriersud@189
   730
        Uint8 pixel = 0;
couriersud@189
   731
        int shift = (8 - ExpandBMP);
couriersud@189
   732
couriersud@189
   733
        bits -= surface->pitch;
couriersud@189
   734
        for (i = 0; i < surface->w; ++i) {
couriersud@189
   735
            if (i % (8 / ExpandBMP) == 0) {
couriersud@189
   736
                if (!SDL_RWread(src, &pixel, 1, 1)) {
couriersud@189
   737
                    SDL_SetError("Error reading from ICO");
couriersud@189
   738
                    was_error = 1;
couriersud@189
   739
                    goto done;
couriersud@189
   740
                }
couriersud@189
   741
            }
couriersud@189
   742
            *((Uint32 *) bits + i) |= ((pixel >> shift) ? 0 : 0xFF000000);
couriersud@189
   743
            pixel <<= ExpandBMP;
couriersud@189
   744
        }
couriersud@189
   745
        /* Skip padding bytes, ugh */
couriersud@189
   746
        if (pad) {
couriersud@189
   747
            Uint8 padbyte;
couriersud@189
   748
            for (i = 0; i < pad; ++i) {
couriersud@189
   749
                SDL_RWread(src, &padbyte, 1, 1);
couriersud@189
   750
            }
couriersud@189
   751
        }
couriersud@189
   752
    }
couriersud@189
   753
  done:
couriersud@189
   754
    if (was_error) {
couriersud@189
   755
        if (src) {
couriersud@189
   756
            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
couriersud@189
   757
        }
couriersud@189
   758
        if (surface) {
couriersud@189
   759
            SDL_FreeSurface(surface);
couriersud@189
   760
        }
couriersud@189
   761
        surface = NULL;
couriersud@189
   762
    }
couriersud@189
   763
    if (freesrc && src) {
couriersud@189
   764
        SDL_RWclose(src);
couriersud@189
   765
    }
couriersud@189
   766
    return (surface);
couriersud@189
   767
}
couriersud@189
   768
slouken@0
   769
/* Load a BMP type image from an SDL datasource */
slouken@0
   770
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   771
{
slouken@101
   772
	return(LoadBMP_RW(src, 0));
slouken@0
   773
}
slouken@0
   774
couriersud@189
   775
/* Load a ICO type image from an SDL datasource */
couriersud@189
   776
SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
couriersud@189
   777
{
couriersud@189
   778
	return(LoadICOCUR_RW(src, 1, 0));
couriersud@189
   779
}
couriersud@189
   780
couriersud@189
   781
/* Load a CUR type image from an SDL datasource */
couriersud@189
   782
SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
couriersud@189
   783
{
couriersud@189
   784
	return(LoadICOCUR_RW(src, 2, 0));
couriersud@189
   785
}
couriersud@189
   786
slouken@0
   787
#else
slouken@0
   788
slouken@0
   789
/* See if an image is contained in a data source */
slouken@0
   790
int IMG_isBMP(SDL_RWops *src)
slouken@0
   791
{
slouken@0
   792
	return(0);
slouken@0
   793
}
slouken@0
   794
couriersud@189
   795
int IMG_isICO(SDL_RWops *src)
couriersud@189
   796
{
couriersud@189
   797
	return(0);
couriersud@189
   798
}
couriersud@189
   799
couriersud@189
   800
int IMG_isCUR(SDL_RWops *src)
couriersud@189
   801
{
couriersud@189
   802
	return(0);
couriersud@189
   803
}
couriersud@189
   804
slouken@0
   805
/* Load a BMP type image from an SDL datasource */
slouken@0
   806
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   807
{
slouken@0
   808
	return(NULL);
slouken@0
   809
}
slouken@0
   810
couriersud@189
   811
/* Load a BMP type image from an SDL datasource */
couriersud@189
   812
SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
couriersud@189
   813
{
couriersud@189
   814
	return(NULL);
couriersud@189
   815
}
couriersud@189
   816
couriersud@189
   817
/* Load a BMP type image from an SDL datasource */
couriersud@189
   818
SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
couriersud@189
   819
{
couriersud@189
   820
	return(NULL);
couriersud@189
   821
}
couriersud@189
   822
slouken@0
   823
#endif /* LOAD_BMP */