Skip to content

Latest commit

 

History

History
486 lines (433 loc) · 10.5 KB

IMG_xpm.c

File metadata and controls

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