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