IMG_bmp.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 10 Nov 2009 06:00:59 +0000
changeset 237 d1a4fae44bd0
parent 236 cce1251de477
child 247 7d5b00142b2a
permissions -rw-r--r--
In the process of tracking down bug #868, made ImageIO the default for building using command line tools, and cleaned up the ImageIO image loading code.
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@101
   343
				case 32:
slouken@101
   344
					Rmask = SDL_ReadLE32(src);
slouken@101
   345
					Gmask = SDL_ReadLE32(src);
slouken@101
   346
					Bmask = SDL_ReadLE32(src);
slouken@106
   347
					Amask = SDL_ReadLE32(src);
slouken@101
   348
					break;
slouken@101
   349
				default:
slouken@101
   350
					break;
slouken@101
   351
			}
slouken@101
   352
			break;
slouken@101
   353
	}
slouken@101
   354
slouken@101
   355
	/* Create a compatible surface, note that the colors are RGB ordered */
slouken@101
   356
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@106
   357
			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, Amask);
slouken@101
   358
	if ( surface == NULL ) {
slouken@202
   359
		was_error = SDL_TRUE;
slouken@101
   360
		goto done;
slouken@101
   361
	}
slouken@101
   362
slouken@101
   363
	/* Load the palette, if any */
slouken@101
   364
	palette = (surface->format)->palette;
slouken@101
   365
	if ( palette ) {
slouken@236
   366
		if ( SDL_RWseek(src, fp_offset+14+biSize, RW_SEEK_SET) < 0 ) {
slouken@151
   367
			SDL_Error(SDL_EFSEEK);
slouken@202
   368
			was_error = SDL_TRUE;
slouken@151
   369
			goto done;
slouken@151
   370
		}
slouken@151
   371
slouken@101
   372
		/*
slouken@101
   373
		| guich: always use 1<<bpp b/c some bitmaps can bring wrong information
slouken@101
   374
		| for colorsUsed
slouken@101
   375
		*/
slouken@101
   376
		/* if ( biClrUsed == 0 ) {  */
slouken@101
   377
		biClrUsed = 1 << biBitCount;
slouken@101
   378
		/* } */
slouken@101
   379
		if ( biSize == 12 ) {
slouken@101
   380
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   381
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   382
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   383
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   384
				palette->colors[i].unused = 0;
slouken@101
   385
			}	
slouken@101
   386
		} else {
slouken@101
   387
			for ( i = 0; i < (int)biClrUsed; ++i ) {
slouken@101
   388
				SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@101
   389
				SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@101
   390
				SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@101
   391
				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
slouken@101
   392
			}	
slouken@101
   393
		}
slouken@101
   394
		palette->ncolors = biClrUsed;
slouken@101
   395
	}
slouken@101
   396
slouken@101
   397
	/* Read the surface pixels.  Note that the bmp image is upside down */
slouken@236
   398
	if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
slouken@101
   399
		SDL_Error(SDL_EFSEEK);
slouken@202
   400
		was_error = SDL_TRUE;
slouken@101
   401
		goto done;
slouken@101
   402
	}
slouken@101
   403
	if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
slouken@101
   404
		was_error = readRlePixels(surface, src, biCompression == BI_RLE8);
slouken@101
   405
		if (was_error) SDL_SetError("Error reading from BMP");
slouken@101
   406
		goto done;
slouken@101
   407
	}
slouken@202
   408
	top = (Uint8 *)surface->pixels;
slouken@202
   409
	end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@101
   410
	switch (ExpandBMP) {
slouken@101
   411
		case 1:
slouken@101
   412
			bmpPitch = (biWidth + 7) >> 3;
slouken@101
   413
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   414
			break;
slouken@101
   415
		case 4:
slouken@101
   416
			bmpPitch = (biWidth + 1) >> 1;
slouken@101
   417
			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
slouken@101
   418
			break;
slouken@101
   419
		default:
slouken@101
   420
			pad  = ((surface->pitch%4) ?
slouken@101
   421
					(4-(surface->pitch%4)) : 0);
slouken@101
   422
			break;
slouken@101
   423
	}
slouken@202
   424
	if ( topDown ) {
slouken@202
   425
		bits = top;
slouken@202
   426
	} else {
slouken@202
   427
		bits = end - surface->pitch;
slouken@202
   428
	}
slouken@202
   429
	while ( bits >= top && bits < end ) {
slouken@101
   430
		switch (ExpandBMP) {
slouken@101
   431
			case 1:
slouken@101
   432
			case 4: {
slouken@101
   433
			Uint8 pixel = 0;
slouken@101
   434
			int   shift = (8-ExpandBMP);
slouken@101
   435
			for ( i=0; i<surface->w; ++i ) {
slouken@101
   436
				if ( i%(8/ExpandBMP) == 0 ) {
slouken@101
   437
					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
slouken@101
   438
						SDL_SetError(
slouken@101
   439
					"Error reading from BMP");
slouken@202
   440
						was_error = SDL_TRUE;
slouken@101
   441
						goto done;
slouken@101
   442
					}
slouken@101
   443
				}
slouken@101
   444
				*(bits+i) = (pixel>>shift);
slouken@101
   445
				pixel <<= ExpandBMP;
slouken@101
   446
			} }
slouken@101
   447
			break;
slouken@101
   448
slouken@101
   449
			default:
slouken@101
   450
			if ( SDL_RWread(src, bits, 1, surface->pitch)
slouken@101
   451
							 != surface->pitch ) {
slouken@101
   452
				SDL_Error(SDL_EFREAD);
slouken@202
   453
				was_error = SDL_TRUE;
slouken@101
   454
				goto done;
slouken@101
   455
			}
slouken@101
   456
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@101
   457
			/* Byte-swap the pixels if needed. Note that the 24bpp
slouken@101
   458
			   case has already been taken care of above. */
slouken@101
   459
			switch(biBitCount) {
slouken@101
   460
				case 15:
slouken@101
   461
				case 16: {
slouken@101
   462
				        Uint16 *pix = (Uint16 *)bits;
slouken@101
   463
					for(i = 0; i < surface->w; i++)
slouken@101
   464
					        pix[i] = SDL_Swap16(pix[i]);
slouken@101
   465
					break;
slouken@101
   466
				}
slouken@101
   467
slouken@101
   468
				case 32: {
slouken@101
   469
				        Uint32 *pix = (Uint32 *)bits;
slouken@101
   470
					for(i = 0; i < surface->w; i++)
slouken@101
   471
					        pix[i] = SDL_Swap32(pix[i]);
slouken@101
   472
					break;
slouken@101
   473
				}
slouken@101
   474
			}
slouken@101
   475
#endif
slouken@101
   476
			break;
slouken@101
   477
		}
slouken@101
   478
		/* Skip padding bytes, ugh */
slouken@101
   479
		if ( pad ) {
slouken@101
   480
			Uint8 padbyte;
slouken@101
   481
			for ( i=0; i<pad; ++i ) {
slouken@101
   482
				SDL_RWread(src, &padbyte, 1, 1);
slouken@101
   483
			}
slouken@101
   484
		}
slouken@202
   485
		if ( topDown ) {
slouken@202
   486
			bits += surface->pitch;
slouken@202
   487
		} else {
slouken@202
   488
			bits -= surface->pitch;
slouken@202
   489
		}
slouken@101
   490
	}
slouken@101
   491
done:
slouken@101
   492
	if ( was_error ) {
slouken@118
   493
		if ( src ) {
slouken@236
   494
			SDL_RWseek(src, fp_offset, RW_SEEK_SET);
slouken@118
   495
		}
slouken@101
   496
		if ( surface ) {
slouken@101
   497
			SDL_FreeSurface(surface);
slouken@101
   498
		}
slouken@101
   499
		surface = NULL;
slouken@101
   500
	}
slouken@101
   501
	if ( freesrc && src ) {
slouken@101
   502
		SDL_RWclose(src);
slouken@101
   503
	}
slouken@101
   504
	return(surface);
slouken@101
   505
}
slouken@101
   506
couriersud@189
   507
static Uint8
couriersud@189
   508
SDL_Read8(SDL_RWops * src)
couriersud@189
   509
{
couriersud@189
   510
    Uint8 value;
couriersud@189
   511
couriersud@189
   512
    SDL_RWread(src, &value, 1, 1);
couriersud@189
   513
    return (value);
couriersud@189
   514
}
couriersud@189
   515
couriersud@189
   516
static SDL_Surface *
couriersud@189
   517
LoadICOCUR_RW(SDL_RWops * src, int type, int freesrc)
couriersud@189
   518
{
slouken@202
   519
    SDL_bool was_error;
couriersud@189
   520
    long fp_offset;
couriersud@189
   521
    int bmpPitch;
couriersud@189
   522
    int i, pad;
couriersud@189
   523
    SDL_Surface *surface;
couriersud@189
   524
    Uint32 Rmask;
couriersud@189
   525
    Uint32 Gmask;
couriersud@189
   526
    Uint32 Bmask;
couriersud@189
   527
    Uint8 *bits;
couriersud@189
   528
    int ExpandBMP;
couriersud@189
   529
    int maxCol = 0;
couriersud@189
   530
    int icoOfs = 0;
couriersud@189
   531
    Uint32 palette[256];
couriersud@189
   532
couriersud@189
   533
    /* The Win32 ICO file header (14 bytes) */
couriersud@189
   534
    Uint16 bfReserved;
couriersud@189
   535
    Uint16 bfType;
couriersud@189
   536
    Uint16 bfCount;
couriersud@189
   537
couriersud@189
   538
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
couriersud@189
   539
    Uint32 biSize;
couriersud@189
   540
    Sint32 biWidth;
couriersud@189
   541
    Sint32 biHeight;
couriersud@189
   542
    Uint16 biPlanes;
couriersud@189
   543
    Uint16 biBitCount;
couriersud@189
   544
    Uint32 biCompression;
couriersud@189
   545
    Uint32 biSizeImage;
couriersud@189
   546
    Sint32 biXPelsPerMeter;
couriersud@189
   547
    Sint32 biYPelsPerMeter;
couriersud@189
   548
    Uint32 biClrUsed;
couriersud@189
   549
    Uint32 biClrImportant;
couriersud@189
   550
couriersud@189
   551
    /* Make sure we are passed a valid data source */
couriersud@189
   552
    surface = NULL;
slouken@202
   553
    was_error = SDL_FALSE;
couriersud@189
   554
    if (src == NULL) {
slouken@202
   555
        was_error = SDL_TRUE;
couriersud@189
   556
        goto done;
couriersud@189
   557
    }
couriersud@189
   558
couriersud@189
   559
    /* Read in the ICO file header */
couriersud@189
   560
    fp_offset = SDL_RWtell(src);
couriersud@189
   561
    SDL_ClearError();
couriersud@189
   562
couriersud@189
   563
    bfReserved = SDL_ReadLE16(src);
couriersud@189
   564
    bfType = SDL_ReadLE16(src);
couriersud@189
   565
    bfCount = SDL_ReadLE16(src);
couriersud@189
   566
    if ((bfReserved != 0) || (bfType != type) || (bfCount == 0)) {
couriersud@189
   567
        SDL_SetError("File is not a Windows %s file", type == 1 ? "ICO" : "CUR");
slouken@202
   568
        was_error = SDL_TRUE;
couriersud@189
   569
        goto done;
couriersud@189
   570
    }
couriersud@189
   571
couriersud@189
   572
    /* Read the Win32 Icon Directory */
couriersud@189
   573
    for (i = 0; i < bfCount; i++) {
couriersud@189
   574
        /* Icon Directory Entries */
couriersud@189
   575
        int bWidth = SDL_Read8(src);    /* Uint8, but 0 = 256 ! */
couriersud@189
   576
        int bHeight = SDL_Read8(src);   /* Uint8, but 0 = 256 ! */
couriersud@189
   577
        int bColorCount = SDL_Read8(src);       /* Uint8, but 0 = 256 ! */
couriersud@189
   578
        Uint8 bReserved = SDL_Read8(src);
couriersud@189
   579
        Uint16 wPlanes = SDL_ReadLE16(src);
couriersud@189
   580
        Uint16 wBitCount = SDL_ReadLE16(src);
couriersud@189
   581
        Uint32 dwBytesInRes = SDL_ReadLE32(src);
couriersud@189
   582
        Uint32 dwImageOffset = SDL_ReadLE32(src);
couriersud@189
   583
couriersud@189
   584
        if (!bWidth)
couriersud@189
   585
            bWidth = 256;
couriersud@189
   586
        if (!bHeight)
couriersud@189
   587
            bHeight = 256;
couriersud@189
   588
        if (!bColorCount)
couriersud@189
   589
            bColorCount = 256;
couriersud@189
   590
couriersud@189
   591
        //printf("%dx%d@%d - %08x\n", bWidth, bHeight, bColorCount, dwImageOffset);
couriersud@189
   592
        if (bColorCount > maxCol) {
couriersud@189
   593
            maxCol = bColorCount;
couriersud@189
   594
            icoOfs = dwImageOffset;
couriersud@189
   595
            //printf("marked\n");
couriersud@189
   596
        }
couriersud@189
   597
    }
couriersud@189
   598
couriersud@189
   599
    /* Advance to the DIB Data */
couriersud@189
   600
    if (SDL_RWseek(src, icoOfs, RW_SEEK_SET) < 0) {
couriersud@189
   601
        SDL_Error(SDL_EFSEEK);
slouken@202
   602
        was_error = SDL_TRUE;
couriersud@189
   603
        goto done;
couriersud@189
   604
    }
couriersud@189
   605
couriersud@189
   606
    /* Read the Win32 BITMAPINFOHEADER */
couriersud@189
   607
    biSize = SDL_ReadLE32(src);
couriersud@189
   608
    if (biSize == 40) {
couriersud@189
   609
        biWidth = SDL_ReadLE32(src);
couriersud@189
   610
        biHeight = SDL_ReadLE32(src);
couriersud@189
   611
        biPlanes = SDL_ReadLE16(src);
couriersud@189
   612
        biBitCount = SDL_ReadLE16(src);
couriersud@189
   613
        biCompression = SDL_ReadLE32(src);
couriersud@189
   614
        biSizeImage = SDL_ReadLE32(src);
couriersud@189
   615
        biXPelsPerMeter = SDL_ReadLE32(src);
couriersud@189
   616
        biYPelsPerMeter = SDL_ReadLE32(src);
couriersud@189
   617
        biClrUsed = SDL_ReadLE32(src);
couriersud@189
   618
        biClrImportant = SDL_ReadLE32(src);
couriersud@189
   619
    } else {
couriersud@189
   620
        SDL_SetError("Unsupported ICO bitmap format");
slouken@202
   621
        was_error = SDL_TRUE;
couriersud@189
   622
        goto done;
couriersud@189
   623
    }
couriersud@189
   624
couriersud@189
   625
    /* Check for read error */
couriersud@189
   626
    if (SDL_strcmp(SDL_GetError(), "") != 0) {
slouken@202
   627
        was_error = SDL_TRUE;
couriersud@189
   628
        goto done;
couriersud@189
   629
    }
couriersud@189
   630
couriersud@189
   631
    /* We don't support any BMP compression right now */
couriersud@189
   632
    switch (biCompression) {
couriersud@189
   633
    case BI_RGB:
couriersud@189
   634
        /* Default values for the BMP format */
couriersud@189
   635
        switch (biBitCount) {
couriersud@189
   636
        case 1:
couriersud@189
   637
        case 4:
couriersud@189
   638
            ExpandBMP = biBitCount;
couriersud@189
   639
            biBitCount = 8;
couriersud@189
   640
            break;
couriersud@189
   641
        case 8:
couriersud@189
   642
            ExpandBMP = 8;
couriersud@189
   643
            break;
couriersud@189
   644
        case 32:
couriersud@189
   645
            Rmask = 0x00FF0000;
couriersud@189
   646
            Gmask = 0x0000FF00;
couriersud@189
   647
            Bmask = 0x000000FF;
couriersud@189
   648
            ExpandBMP = 0;
couriersud@189
   649
            break;
couriersud@189
   650
        default:
couriersud@189
   651
            SDL_SetError("ICO file with unsupported bit count");
slouken@202
   652
            was_error = SDL_TRUE;
couriersud@189
   653
            goto done;
couriersud@189
   654
        }
couriersud@189
   655
        break;
couriersud@189
   656
    default:
couriersud@189
   657
        SDL_SetError("Compressed ICO files not supported");
slouken@202
   658
        was_error = SDL_TRUE;
couriersud@189
   659
        goto done;
couriersud@189
   660
    }
couriersud@189
   661
couriersud@189
   662
    /* Create a RGBA surface */
couriersud@189
   663
    biHeight = biHeight >> 1;
couriersud@189
   664
    //printf("%d x %d\n", biWidth, biHeight);
couriersud@189
   665
    surface =
couriersud@189
   666
        SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
couriersud@189
   667
                             0x0000FF00, 0x000000FF, 0xFF000000);
couriersud@189
   668
    if (surface == NULL) {
slouken@202
   669
        was_error = SDL_TRUE;
couriersud@189
   670
        goto done;
couriersud@189
   671
    }
couriersud@189
   672
couriersud@189
   673
    /* Load the palette, if any */
couriersud@189
   674
    //printf("bc %d bused %d\n", biBitCount, biClrUsed);
couriersud@189
   675
    if (biBitCount <= 8) {
couriersud@189
   676
        if (biClrUsed == 0) {
couriersud@189
   677
            biClrUsed = 1 << biBitCount;
couriersud@189
   678
        }
couriersud@189
   679
        for (i = 0; i < (int) biClrUsed; ++i) {
couriersud@189
   680
            SDL_RWread(src, &palette[i], 4, 1);
couriersud@189
   681
        }
couriersud@189
   682
    }
couriersud@189
   683
couriersud@189
   684
    /* Read the surface pixels.  Note that the bmp image is upside down */
couriersud@189
   685
    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
couriersud@189
   686
    switch (ExpandBMP) {
couriersud@189
   687
    case 1:
couriersud@189
   688
        bmpPitch = (biWidth + 7) >> 3;
couriersud@189
   689
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   690
        break;
couriersud@189
   691
    case 4:
couriersud@189
   692
        bmpPitch = (biWidth + 1) >> 1;
couriersud@189
   693
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   694
        break;
couriersud@189
   695
    case 8:
couriersud@189
   696
        bmpPitch = biWidth;
couriersud@189
   697
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   698
        break;
couriersud@189
   699
    default:
couriersud@189
   700
        bmpPitch = biWidth * 4;
couriersud@189
   701
        pad = 0;
couriersud@189
   702
        break;
couriersud@189
   703
    }
couriersud@189
   704
    while (bits > (Uint8 *) surface->pixels) {
couriersud@189
   705
        bits -= surface->pitch;
couriersud@189
   706
        switch (ExpandBMP) {
couriersud@189
   707
        case 1:
couriersud@189
   708
        case 4:
couriersud@189
   709
        case 8:
couriersud@189
   710
            {
couriersud@189
   711
                Uint8 pixel = 0;
couriersud@189
   712
                int shift = (8 - ExpandBMP);
couriersud@189
   713
                for (i = 0; i < surface->w; ++i) {
couriersud@189
   714
                    if (i % (8 / ExpandBMP) == 0) {
couriersud@189
   715
                        if (!SDL_RWread(src, &pixel, 1, 1)) {
couriersud@189
   716
                            SDL_SetError("Error reading from ICO");
slouken@202
   717
                            was_error = SDL_TRUE;
couriersud@189
   718
                            goto done;
couriersud@189
   719
                        }
couriersud@189
   720
                    }
couriersud@189
   721
                    *((Uint32 *) bits + i) = (palette[pixel >> shift]);
couriersud@189
   722
                    pixel <<= ExpandBMP;
couriersud@189
   723
                }
couriersud@189
   724
            }
couriersud@189
   725
            break;
couriersud@189
   726
couriersud@189
   727
        default:
couriersud@189
   728
            if (SDL_RWread(src, bits, 1, surface->pitch)
couriersud@189
   729
                != surface->pitch) {
couriersud@189
   730
                SDL_Error(SDL_EFREAD);
slouken@202
   731
                was_error = SDL_TRUE;
couriersud@189
   732
                goto done;
couriersud@189
   733
            }
couriersud@189
   734
            break;
couriersud@189
   735
        }
couriersud@189
   736
        /* Skip padding bytes, ugh */
couriersud@189
   737
        if (pad) {
couriersud@189
   738
            Uint8 padbyte;
couriersud@189
   739
            for (i = 0; i < pad; ++i) {
couriersud@189
   740
                SDL_RWread(src, &padbyte, 1, 1);
couriersud@189
   741
            }
couriersud@189
   742
        }
couriersud@189
   743
    }
couriersud@189
   744
    /* Read the mask pixels.  Note that the bmp image is upside down */
couriersud@189
   745
    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
couriersud@189
   746
    ExpandBMP = 1;
couriersud@189
   747
    bmpPitch = (biWidth + 7) >> 3;
couriersud@189
   748
    pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
couriersud@189
   749
    while (bits > (Uint8 *) surface->pixels) {
couriersud@189
   750
        Uint8 pixel = 0;
couriersud@189
   751
        int shift = (8 - ExpandBMP);
couriersud@189
   752
couriersud@189
   753
        bits -= surface->pitch;
couriersud@189
   754
        for (i = 0; i < surface->w; ++i) {
couriersud@189
   755
            if (i % (8 / ExpandBMP) == 0) {
couriersud@189
   756
                if (!SDL_RWread(src, &pixel, 1, 1)) {
couriersud@189
   757
                    SDL_SetError("Error reading from ICO");
slouken@202
   758
                    was_error = SDL_TRUE;
couriersud@189
   759
                    goto done;
couriersud@189
   760
                }
couriersud@189
   761
            }
couriersud@189
   762
            *((Uint32 *) bits + i) |= ((pixel >> shift) ? 0 : 0xFF000000);
couriersud@189
   763
            pixel <<= ExpandBMP;
couriersud@189
   764
        }
couriersud@189
   765
        /* Skip padding bytes, ugh */
couriersud@189
   766
        if (pad) {
couriersud@189
   767
            Uint8 padbyte;
couriersud@189
   768
            for (i = 0; i < pad; ++i) {
couriersud@189
   769
                SDL_RWread(src, &padbyte, 1, 1);
couriersud@189
   770
            }
couriersud@189
   771
        }
couriersud@189
   772
    }
couriersud@189
   773
  done:
couriersud@189
   774
    if (was_error) {
couriersud@189
   775
        if (src) {
couriersud@189
   776
            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
couriersud@189
   777
        }
couriersud@189
   778
        if (surface) {
couriersud@189
   779
            SDL_FreeSurface(surface);
couriersud@189
   780
        }
couriersud@189
   781
        surface = NULL;
couriersud@189
   782
    }
couriersud@189
   783
    if (freesrc && src) {
couriersud@189
   784
        SDL_RWclose(src);
couriersud@189
   785
    }
couriersud@189
   786
    return (surface);
couriersud@189
   787
}
couriersud@189
   788
slouken@0
   789
/* Load a BMP type image from an SDL datasource */
slouken@0
   790
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   791
{
slouken@101
   792
	return(LoadBMP_RW(src, 0));
slouken@0
   793
}
slouken@0
   794
couriersud@189
   795
/* Load a ICO type image from an SDL datasource */
couriersud@189
   796
SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
couriersud@189
   797
{
couriersud@189
   798
	return(LoadICOCUR_RW(src, 1, 0));
couriersud@189
   799
}
couriersud@189
   800
couriersud@189
   801
/* Load a CUR type image from an SDL datasource */
couriersud@189
   802
SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
couriersud@189
   803
{
couriersud@189
   804
	return(LoadICOCUR_RW(src, 2, 0));
couriersud@189
   805
}
couriersud@189
   806
slouken@0
   807
#else
slouken@0
   808
slouken@0
   809
/* See if an image is contained in a data source */
slouken@0
   810
int IMG_isBMP(SDL_RWops *src)
slouken@0
   811
{
slouken@0
   812
	return(0);
slouken@0
   813
}
slouken@0
   814
couriersud@189
   815
int IMG_isICO(SDL_RWops *src)
couriersud@189
   816
{
couriersud@189
   817
	return(0);
couriersud@189
   818
}
couriersud@189
   819
couriersud@189
   820
int IMG_isCUR(SDL_RWops *src)
couriersud@189
   821
{
couriersud@189
   822
	return(0);
couriersud@189
   823
}
couriersud@189
   824
slouken@0
   825
/* Load a BMP type image from an SDL datasource */
slouken@0
   826
SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
slouken@0
   827
{
slouken@0
   828
	return(NULL);
slouken@0
   829
}
slouken@0
   830
couriersud@189
   831
/* Load a BMP type image from an SDL datasource */
couriersud@189
   832
SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
couriersud@189
   833
{
couriersud@189
   834
	return(NULL);
couriersud@189
   835
}
couriersud@189
   836
couriersud@189
   837
/* Load a BMP type image from an SDL datasource */
couriersud@189
   838
SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
couriersud@189
   839
{
couriersud@189
   840
	return(NULL);
couriersud@189
   841
}
couriersud@189
   842
slouken@0
   843
#endif /* LOAD_BMP */
slouken@237
   844
slouken@237
   845
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */