IMG_xpm.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 22:26:04 +0000
changeset 118 c5e736a47ad2
parent 117 e613cf987897
child 121 1bf9c0c87374
permissions -rw-r--r--
If there's an error loading an image, seek back to the start of the image.
slouken@7
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@97
     3
    Copyright (C) 1999-2004 Sam Lantinga
slouken@7
     4
slouken@7
     5
    This library is free software; you can redistribute it and/or
slouken@7
     6
    modify it under the terms of the GNU Library General Public
slouken@7
     7
    License as published by the Free Software Foundation; either
slouken@7
     8
    version 2 of the License, or (at your option) any later version.
slouken@7
     9
slouken@7
    10
    This library is distributed in the hope that it will be useful,
slouken@7
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@7
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@7
    13
    Library General Public License for more details.
slouken@7
    14
slouken@7
    15
    You should have received a copy of the GNU Library General Public
slouken@7
    16
    License along with this library; if not, write to the Free
slouken@7
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@7
    18
slouken@7
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@7
    21
*/
slouken@7
    22
slouken@53
    23
/* $Id$ */
slouken@53
    24
slouken@35
    25
/*
slouken@35
    26
 * XPM (X PixMap) image loader:
slouken@35
    27
 *
slouken@35
    28
 * Supports the XPMv3 format, EXCEPT:
slouken@35
    29
 * - hotspot coordinates are ignored
slouken@35
    30
 * - only colour ('c') colour symbols are used
slouken@35
    31
 * - rgb.txt is not used (for portability), so only RGB colours
slouken@35
    32
 *   are recognized (#rrggbb etc) - only a few basic colour names are
slouken@35
    33
 *   handled
slouken@35
    34
 *
slouken@35
    35
 * The result is an 8bpp indexed surface if possible, otherwise 32bpp.
slouken@35
    36
 * The colourkey is correctly set if transparency is used.
slouken@35
    37
 * 
slouken@35
    38
 * Besides the standard API, also provides
slouken@35
    39
 *
slouken@35
    40
 *     SDL_Surface *IMG_ReadXPMFromArray(char **xpm)
slouken@35
    41
 *
slouken@35
    42
 * that reads the image data from an XPM file included in the C source.
slouken@35
    43
 *
slouken@35
    44
 * TODO: include rgb.txt here. The full table (from solaris 2.6) only
slouken@35
    45
 * requires about 13K in binary form.
slouken@35
    46
 */
slouken@7
    47
slouken@9
    48
#include <stdlib.h>
slouken@7
    49
#include <stdio.h>
slouken@7
    50
#include <string.h>
slouken@7
    51
#include <ctype.h>
slouken@7
    52
slouken@7
    53
#include "SDL_image.h"
slouken@7
    54
slouken@7
    55
#ifdef LOAD_XPM
slouken@7
    56
slouken@7
    57
/* See if an image is contained in a data source */
slouken@7
    58
int IMG_isXPM(SDL_RWops *src)
slouken@7
    59
{
slouken@117
    60
	int start;
slouken@117
    61
	int is_XPM;
slouken@35
    62
	char magic[9];
slouken@7
    63
slouken@117
    64
	start = SDL_RWtell(src);
slouken@117
    65
	is_XPM = 0;
slouken@117
    66
	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
slouken@117
    67
		if ( memcmp(magic, "/* XPM */", sizeof(magic)) == 0 ) {
slouken@117
    68
			is_XPM = 1;
slouken@117
    69
		}
slouken@117
    70
	}
slouken@117
    71
	SDL_RWseek(src, start, SEEK_SET);
slouken@117
    72
	return(is_XPM);
slouken@7
    73
}
slouken@7
    74
slouken@7
    75
/* Hash table to look up colors from pixel strings */
slouken@11
    76
#define STARTING_HASH_SIZE 256
slouken@11
    77
slouken@11
    78
struct hash_entry {
slouken@11
    79
	char *key;
slouken@11
    80
	Uint32 color;
slouken@11
    81
	struct hash_entry *next;
slouken@7
    82
};
slouken@7
    83
slouken@11
    84
struct color_hash {
slouken@11
    85
	struct hash_entry **table;
slouken@11
    86
	struct hash_entry *entries; /* array of all entries */
slouken@11
    87
	struct hash_entry *next_free;
slouken@11
    88
	int size;
slouken@11
    89
	int maxnum;
slouken@11
    90
};
slouken@11
    91
slouken@11
    92
static int hash_key(const char *key, int cpp, int size)
slouken@7
    93
{
slouken@7
    94
	int hash;
slouken@7
    95
slouken@7
    96
	hash = 0;
slouken@7
    97
	while ( cpp-- > 0 ) {
slouken@11
    98
		hash = hash * 33 + *key++;
slouken@7
    99
	}
slouken@11
   100
	return hash & (size - 1);
slouken@7
   101
}
slouken@7
   102
slouken@11
   103
static struct color_hash *create_colorhash(int maxnum)
slouken@7
   104
{
slouken@11
   105
	int bytes, s;
slouken@7
   106
	struct color_hash *hash;
slouken@7
   107
slouken@11
   108
	/* we know how many entries we need, so we can allocate
slouken@11
   109
	   everything here */
slouken@11
   110
	hash = malloc(sizeof *hash);
slouken@11
   111
	if(!hash)
slouken@11
   112
		return NULL;
slouken@11
   113
slouken@11
   114
	/* use power-of-2 sized hash table for decoding speed */
slouken@11
   115
	for(s = STARTING_HASH_SIZE; s < maxnum; s <<= 1)
slouken@11
   116
		;
slouken@11
   117
	hash->size = s;
slouken@11
   118
	hash->maxnum = maxnum;
slouken@11
   119
	bytes = hash->size * sizeof(struct hash_entry **);
slouken@11
   120
	hash->entries = NULL;	/* in case malloc fails */
slouken@11
   121
	hash->table = malloc(bytes);
slouken@11
   122
	if(!hash->table)
slouken@11
   123
		return NULL;
slouken@11
   124
	memset(hash->table, 0, bytes);
slouken@11
   125
	hash->entries = malloc(maxnum * sizeof(struct hash_entry));
slouken@11
   126
	if(!hash->entries)
slouken@11
   127
		return NULL;
slouken@11
   128
	hash->next_free = hash->entries;
slouken@11
   129
	return hash;
slouken@7
   130
}
slouken@7
   131
slouken@7
   132
static int add_colorhash(struct color_hash *hash,
slouken@11
   133
                         char *key, int cpp, Uint32 color)
slouken@7
   134
{
slouken@11
   135
	int index = hash_key(key, cpp, hash->size);
slouken@11
   136
	struct hash_entry *e = hash->next_free++;
slouken@11
   137
	e->color = color;
slouken@11
   138
	e->key = key;
slouken@11
   139
	e->next = hash->table[index];
slouken@11
   140
	hash->table[index] = e;
slouken@11
   141
	return 1;
slouken@7
   142
}
slouken@7
   143
slouken@11
   144
/* fast lookup that works if cpp == 1 */
slouken@22
   145
#define QUICK_COLORHASH(hash, key) ((hash)->table[*(Uint8 *)(key)]->color)
slouken@11
   146
slouken@11
   147
static Uint32 get_colorhash(struct color_hash *hash, const char *key, int cpp)
slouken@7
   148
{
slouken@11
   149
	struct hash_entry *entry = hash->table[hash_key(key, cpp, hash->size)];
slouken@11
   150
	while(entry) {
slouken@11
   151
		if(memcmp(key, entry->key, cpp) == 0)
slouken@11
   152
			return entry->color;
slouken@11
   153
		entry = entry->next;
slouken@7
   154
	}
slouken@11
   155
	return 0;		/* garbage in - garbage out */
slouken@7
   156
}
slouken@7
   157
slouken@7
   158
static void free_colorhash(struct color_hash *hash)
slouken@7
   159
{
slouken@11
   160
	if(hash && hash->table) {
slouken@11
   161
		free(hash->table);
slouken@11
   162
		free(hash->entries);
slouken@11
   163
		free(hash);
slouken@7
   164
	}
slouken@7
   165
}
slouken@7
   166
slouken@35
   167
/* portable case-insensitive string comparison */
slouken@35
   168
static int string_equal(const char *a, const char *b, int n)
slouken@35
   169
{
slouken@35
   170
	while(*a && *b && n) {
slouken@35
   171
		if(toupper((unsigned char)*a) != toupper((unsigned char)*b))
slouken@35
   172
			return 0;
slouken@35
   173
		a++;
slouken@35
   174
		b++;
slouken@35
   175
		n--;
slouken@35
   176
	}
slouken@35
   177
	return *a == *b;
slouken@35
   178
}
slouken@35
   179
slouken@11
   180
#define ARRAYSIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
slouken@11
   181
slouken@11
   182
/*
slouken@11
   183
 * convert colour spec to RGB (in 0xrrggbb format).
slouken@35
   184
 * return 1 if successful.
slouken@11
   185
 */
slouken@35
   186
static int color_to_rgb(char *spec, int speclen, Uint32 *rgb)
slouken@7
   187
{
slouken@11
   188
	/* poor man's rgb.txt */
slouken@11
   189
	static struct { char *name; Uint32 rgb; } known[] = {
slouken@11
   190
		{"none",  0xffffffff},
slouken@11
   191
		{"black", 0x00000000},
slouken@11
   192
		{"white", 0x00ffffff},
slouken@11
   193
		{"red",   0x00ff0000},
slouken@11
   194
		{"green", 0x0000ff00},
slouken@11
   195
		{"blue",  0x000000ff}
slouken@11
   196
	};
slouken@7
   197
slouken@11
   198
	if(spec[0] == '#') {
slouken@11
   199
		char buf[7];
slouken@35
   200
		switch(speclen) {
slouken@35
   201
		case 4:
slouken@35
   202
			buf[0] = buf[1] = spec[1];
slouken@35
   203
			buf[2] = buf[3] = spec[2];
slouken@35
   204
			buf[4] = buf[5] = spec[3];
slouken@7
   205
			break;
slouken@35
   206
		case 7:
slouken@35
   207
			memcpy(buf, spec + 1, 6);
slouken@7
   208
			break;
slouken@35
   209
		case 13:
slouken@35
   210
			buf[0] = spec[1];
slouken@35
   211
			buf[1] = spec[2];
slouken@35
   212
			buf[2] = spec[5];
slouken@35
   213
			buf[3] = spec[6];
slouken@35
   214
			buf[4] = spec[9];
slouken@35
   215
			buf[5] = spec[10];
slouken@7
   216
			break;
slouken@11
   217
		}
slouken@11
   218
		buf[6] = '\0';
slouken@11
   219
		*rgb = strtol(buf, NULL, 16);
slouken@11
   220
		return 1;
slouken@11
   221
	} else {
slouken@11
   222
		int i;
slouken@11
   223
		for(i = 0; i < ARRAYSIZE(known); i++)
slouken@35
   224
			if(string_equal(known[i].name, spec, speclen)) {
slouken@11
   225
				*rgb = known[i].rgb;
slouken@11
   226
				return 1;
slouken@11
   227
			}
slouken@11
   228
		return 0;
slouken@7
   229
	}
slouken@7
   230
}
slouken@7
   231
slouken@11
   232
#ifndef MAX
slouken@11
   233
#define MAX(a, b) ((a) > (b) ? (a) : (b))
slouken@11
   234
#endif
slouken@11
   235
slouken@35
   236
static char *linebuf;
slouken@35
   237
static int buflen;
slouken@35
   238
static char *error;
slouken@35
   239
slouken@35
   240
/*
slouken@35
   241
 * Read next line from the source.
slouken@35
   242
 * If len > 0, it's assumed to be at least len chars (for efficiency).
slouken@35
   243
 * Return NULL and set error upon EOF or parse error.
slouken@35
   244
 */
slouken@36
   245
static char *get_next_line(char ***lines, SDL_RWops *src, int len)
slouken@7
   246
{
slouken@35
   247
	if(lines) {
slouken@35
   248
		return *(*lines)++;
slouken@35
   249
	} else {
slouken@35
   250
		char c;
slouken@35
   251
		int n;
slouken@35
   252
		do {
slouken@35
   253
			if(SDL_RWread(src, &c, 1, 1) <= 0) {
slouken@35
   254
				error = "Premature end of data";
slouken@35
   255
				return NULL;
slouken@35
   256
			}
slouken@35
   257
		} while(c != '"');
slouken@35
   258
		if(len) {
slouken@35
   259
			len += 4;	/* "\",\n\0" */
slouken@35
   260
			if(len > buflen){
slouken@35
   261
				buflen = len;
slouken@35
   262
				linebuf = realloc(linebuf, buflen);
slouken@35
   263
				if(!linebuf) {
slouken@35
   264
					error = "Out of memory";
slouken@35
   265
					return NULL;
slouken@35
   266
				}
slouken@35
   267
			}
slouken@35
   268
			if(SDL_RWread(src, linebuf, len - 1, 1) <= 0) {
slouken@35
   269
				error = "Premature end of data";
slouken@35
   270
				return NULL;
slouken@35
   271
			}
slouken@35
   272
			n = len - 2;
slouken@35
   273
		} else {
slouken@35
   274
			n = 0;
slouken@35
   275
			do {
slouken@35
   276
				if(n >= buflen - 1) {
slouken@35
   277
					if(buflen == 0)
slouken@35
   278
						buflen = 16;
slouken@35
   279
					buflen *= 2;
slouken@35
   280
					linebuf = realloc(linebuf, buflen);
slouken@35
   281
					if(!linebuf) {
slouken@35
   282
						error = "Out of memory";
slouken@35
   283
						return NULL;
slouken@35
   284
					}
slouken@35
   285
				}
slouken@35
   286
				if(SDL_RWread(src, linebuf + n, 1, 1) <= 0) {
slouken@35
   287
					error = "Premature end of data";
slouken@35
   288
					return NULL;
slouken@35
   289
				}
slouken@35
   290
			} while(linebuf[n++] != '"');
slouken@35
   291
			n--;
slouken@35
   292
		}
slouken@35
   293
		linebuf[n] = '\0';
slouken@35
   294
		return linebuf;
slouken@35
   295
	}
slouken@35
   296
}
slouken@35
   297
slouken@35
   298
#define SKIPSPACE(p)				\
slouken@35
   299
do {						\
slouken@35
   300
	while(isspace((unsigned char)*(p)))	\
slouken@35
   301
	      ++(p);				\
slouken@35
   302
} while(0)
slouken@35
   303
slouken@35
   304
#define SKIPNONSPACE(p)					\
slouken@35
   305
do {							\
slouken@35
   306
	while(!isspace((unsigned char)*(p)) && *p)	\
slouken@35
   307
	      ++(p);					\
slouken@35
   308
} while(0)
slouken@35
   309
slouken@35
   310
/* read XPM from either array or RWops */
slouken@35
   311
static SDL_Surface *load_xpm(char **xpm, SDL_RWops *src)
slouken@35
   312
{
slouken@118
   313
	int start;
slouken@35
   314
	SDL_Surface *image = NULL;
slouken@7
   315
	int index;
slouken@11
   316
	int x, y;
slouken@7
   317
	int w, h, ncolors, cpp;
slouken@7
   318
	int indexed;
slouken@7
   319
	Uint8 *dst;
slouken@35
   320
	struct color_hash *colors = NULL;
slouken@11
   321
	SDL_Color *im_colors = NULL;
slouken@35
   322
	char *keystrings = NULL, *nextkey;
slouken@35
   323
	char *line;
slouken@35
   324
	char ***xpmlines = NULL;
slouken@35
   325
	int pixels_len;
slouken@7
   326
slouken@38
   327
	error = NULL;
slouken@38
   328
	linebuf = NULL;
slouken@38
   329
	buflen = 0;
slouken@38
   330
slouken@118
   331
	start = SDL_RWtell(src);
slouken@118
   332
slouken@35
   333
	if(xpm)
slouken@35
   334
		xpmlines = &xpm;
slouken@35
   335
slouken@35
   336
	line = get_next_line(xpmlines, src, 0);
slouken@35
   337
	if(!line)
slouken@35
   338
		goto done;
slouken@11
   339
	/*
slouken@11
   340
	 * The header string of an XPMv3 image has the format
slouken@11
   341
	 *
slouken@11
   342
	 * <width> <height> <ncolors> <cpp> [ <hotspot_x> <hotspot_y> ]
slouken@11
   343
	 *
slouken@11
   344
	 * where the hotspot coords are intended for mouse cursors.
slouken@11
   345
	 * Right now we don't use the hotspots but it should be handled
slouken@11
   346
	 * one day.
slouken@11
   347
	 */
slouken@35
   348
	if(sscanf(line, "%d %d %d %d", &w, &h, &ncolors, &cpp) != 4
slouken@11
   349
	   || w <= 0 || h <= 0 || ncolors <= 0 || cpp <= 0) {
slouken@35
   350
		error = "Invalid format description";
slouken@35
   351
		goto done;
slouken@11
   352
	}
slouken@7
   353
slouken@11
   354
	keystrings = malloc(ncolors * cpp);
slouken@11
   355
	if(!keystrings) {
slouken@35
   356
		error = "Out of memory";
slouken@35
   357
		goto done;
slouken@11
   358
	}
slouken@11
   359
	nextkey = keystrings;
slouken@7
   360
slouken@11
   361
	/* Create the new surface */
slouken@11
   362
	if(ncolors <= 256) {
slouken@11
   363
		indexed = 1;
slouken@11
   364
		image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8,
slouken@11
   365
					     0, 0, 0, 0);
slouken@11
   366
		im_colors = image->format->palette->colors;
slouken@11
   367
		image->format->palette->ncolors = ncolors;
slouken@11
   368
	} else {
slouken@11
   369
		indexed = 0;
slouken@11
   370
		image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
slouken@11
   371
					     0xff0000, 0x00ff00, 0x0000ff, 0);
slouken@11
   372
	}
slouken@11
   373
	if(!image) {
slouken@11
   374
		/* Hmm, some SDL error (out of memory?) */
slouken@35
   375
		goto done;
slouken@11
   376
	}
slouken@7
   377
slouken@7
   378
	/* Read the colors */
slouken@11
   379
	colors = create_colorhash(ncolors);
slouken@35
   380
	if (!colors) {
slouken@11
   381
		error = "Out of memory";
slouken@11
   382
		goto done;
slouken@7
   383
	}
slouken@11
   384
	for(index = 0; index < ncolors; ++index ) {
slouken@35
   385
		char *p;
slouken@35
   386
		line = get_next_line(xpmlines, src, 0);
slouken@35
   387
		if(!line)
slouken@35
   388
			goto done;
slouken@11
   389
slouken@35
   390
		p = line + cpp + 1;
slouken@11
   391
slouken@11
   392
		/* parse a colour definition */
slouken@11
   393
		for(;;) {
slouken@11
   394
			char nametype;
slouken@11
   395
			char *colname;
slouken@35
   396
			Uint32 rgb, pixel;
slouken@11
   397
slouken@35
   398
			SKIPSPACE(p);
slouken@35
   399
			if(!*p) {
slouken@35
   400
				error = "colour parse error";
slouken@11
   401
				goto done;
slouken@7
   402
			}
slouken@35
   403
			nametype = *p;
slouken@35
   404
			SKIPNONSPACE(p);
slouken@35
   405
			SKIPSPACE(p);
slouken@35
   406
			colname = p;
slouken@35
   407
			SKIPNONSPACE(p);
slouken@11
   408
			if(nametype == 's')
slouken@11
   409
				continue;      /* skip symbolic colour names */
slouken@11
   410
slouken@35
   411
			if(!color_to_rgb(colname, p - colname, &rgb))
slouken@11
   412
				continue;
slouken@11
   413
slouken@35
   414
			memcpy(nextkey, line, cpp);
slouken@11
   415
			if(indexed) {
slouken@11
   416
				SDL_Color *c = im_colors + index;
slouken@11
   417
				c->r = rgb >> 16;
slouken@11
   418
				c->g = rgb >> 8;
slouken@11
   419
				c->b = rgb;
slouken@35
   420
				pixel = index;
slouken@11
   421
			} else
slouken@35
   422
				pixel = rgb;
slouken@35
   423
			add_colorhash(colors, nextkey, cpp, pixel);
slouken@11
   424
			nextkey += cpp;
slouken@11
   425
			if(rgb == 0xffffffff)
slouken@35
   426
				SDL_SetColorKey(image, SDL_SRCCOLORKEY, pixel);
slouken@11
   427
			break;
slouken@7
   428
		}
slouken@7
   429
	}
slouken@7
   430
slouken@7
   431
	/* Read the pixels */
slouken@11
   432
	pixels_len = w * cpp;
slouken@11
   433
	dst = image->pixels;
slouken@35
   434
	for(y = 0; y < h; y++) {
slouken@35
   435
		line = get_next_line(xpmlines, src, pixels_len);
slouken@11
   436
		if(indexed) {
slouken@11
   437
			/* optimization for some common cases */
slouken@11
   438
			if(cpp == 1)
slouken@11
   439
				for(x = 0; x < w; x++)
slouken@11
   440
					dst[x] = QUICK_COLORHASH(colors,
slouken@35
   441
								 line + x);
slouken@11
   442
			else
slouken@11
   443
				for(x = 0; x < w; x++)
slouken@11
   444
					dst[x] = get_colorhash(colors,
slouken@35
   445
							       line + x * cpp,
slouken@11
   446
							       cpp);
slouken@11
   447
		} else {
slouken@11
   448
			for (x = 0; x < w; x++)
slouken@11
   449
				((Uint32*)dst)[x] = get_colorhash(colors,
slouken@35
   450
								line + x * cpp,
slouken@11
   451
								  cpp);
slouken@7
   452
		}
slouken@11
   453
		dst += image->pitch;
slouken@7
   454
	}
slouken@11
   455
slouken@11
   456
done:
slouken@11
   457
	if(error) {
slouken@118
   458
		SDL_RWseek(src, start, SEEK_SET);
slouken@118
   459
		if ( image ) {
slouken@118
   460
			SDL_FreeSurface(image);
slouken@118
   461
			image = NULL;
slouken@118
   462
		}
slouken@11
   463
		IMG_SetError(error);
slouken@7
   464
	}
slouken@11
   465
	free(keystrings);
slouken@7
   466
	free_colorhash(colors);
slouken@35
   467
	free(linebuf);
slouken@7
   468
	return(image);
slouken@7
   469
}
slouken@7
   470
slouken@35
   471
/* Load a XPM type image from an RWops datasource */
slouken@35
   472
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
slouken@35
   473
{
slouken@98
   474
	if ( !src ) {
slouken@98
   475
		/* The error message has been set in SDL_RWFromFile */
slouken@98
   476
		return NULL;
slouken@98
   477
	}
slouken@35
   478
	return load_xpm(NULL, src);
slouken@35
   479
}
slouken@35
   480
slouken@35
   481
SDL_Surface *IMG_ReadXPMFromArray(char **xpm)
slouken@35
   482
{
slouken@35
   483
	return load_xpm(xpm, NULL);
slouken@35
   484
}
slouken@35
   485
slouken@35
   486
#else  /* not LOAD_XPM */
slouken@7
   487
slouken@7
   488
/* See if an image is contained in a data source */
slouken@7
   489
int IMG_isXPM(SDL_RWops *src)
slouken@7
   490
{
slouken@7
   491
	return(0);
slouken@7
   492
}
slouken@7
   493
slouken@35
   494
slouken@7
   495
/* Load a XPM type image from an SDL datasource */
slouken@7
   496
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
slouken@7
   497
{
slouken@7
   498
	return(NULL);
slouken@7
   499
}
slouken@7
   500
slouken@35
   501
SDL_Surface *IMG_ReadXPMFromArray(char **xpm)
slouken@35
   502
{
slouken@35
   503
    return NULL;
slouken@35
   504
}
slouken@35
   505
#endif /* not LOAD_XPM */