/
IMG_xpm.c
518 lines (465 loc) · 11.2 KB
1
/*
2
3
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
8
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
12
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
*/
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* 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.
*/
44
45
#include <stdlib.h>
46
47
48
49
50
51
52
53
54
55
56
#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)
{
57
58
int start;
int is_XPM;
59
char magic[9];
60
61
62
if ( !src )
return 0;
63
64
65
66
67
68
69
start = SDL_RWtell(src);
is_XPM = 0;
if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
if ( memcmp(magic, "/* XPM */", sizeof(magic)) == 0 ) {
is_XPM = 1;
}
}
70
SDL_RWseek(src, start, RW_SEEK_SET);
71
return(is_XPM);
72
73
74
}
/* Hash table to look up colors from pixel strings */
75
76
77
78
79
80
81
82
#define STARTING_HASH_SIZE 256
struct hash_entry {
char *key;
Uint32 color;
struct hash_entry *next;
};
83
struct color_hash {
84
85
86
87
88
struct hash_entry **table;
struct hash_entry *entries; /* array of all entries */
struct hash_entry *next_free;
int size;
int maxnum;
89
90
};
91
static int hash_key(const char *key, int cpp, int size)
92
93
94
95
96
{
int hash;
hash = 0;
while ( cpp-- > 0 ) {
97
hash = hash * 33 + *key++;
98
}
99
return hash & (size - 1);
100
101
}
102
static struct color_hash *create_colorhash(int maxnum)
103
{
104
int bytes, s;
105
106
struct color_hash *hash;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 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));
125
126
if(!hash->entries) {
free(hash->table);
127
return NULL;
128
}
129
130
hash->next_free = hash->entries;
return hash;
131
132
133
}
static int add_colorhash(struct color_hash *hash,
134
char *key, int cpp, Uint32 color)
135
{
136
137
138
139
140
141
142
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;
143
144
}
145
/* fast lookup that works if cpp == 1 */
146
#define QUICK_COLORHASH(hash, key) ((hash)->table[*(Uint8 *)(key)]->color)
147
148
static Uint32 get_colorhash(struct color_hash *hash, const char *key, int cpp)
149
{
150
151
152
153
154
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;
155
}
156
return 0; /* garbage in - garbage out */
157
158
159
160
}
static void free_colorhash(struct color_hash *hash)
{
161
162
163
164
if(hash && hash->table) {
free(hash->table);
free(hash->entries);
free(hash);
165
166
167
}
}
168
169
170
171
172
173
174
175
176
177
178
179
180
/* 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;
}
181
#define ARRAYSIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
182
183
184
/*
* convert colour spec to RGB (in 0xrrggbb format).
185
* return 1 if successful.
186
*/
187
static int color_to_rgb(char *spec, int speclen, Uint32 *rgb)
188
189
190
191
192
193
194
195
196
197
198
199
200
{
/* 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];
201
202
203
204
205
switch(speclen) {
case 4:
buf[0] = buf[1] = spec[1];
buf[2] = buf[3] = spec[2];
buf[4] = buf[5] = spec[3];
206
break;
207
208
case 7:
memcpy(buf, spec + 1, 6);
209
break;
210
211
212
213
214
215
216
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];
217
break;
218
219
220
221
222
223
224
}
buf[6] = '\0';
*rgb = strtol(buf, NULL, 16);
return 1;
} else {
int i;
for(i = 0; i < ARRAYSIZE(known); i++)
225
if(string_equal(known[i].name, spec, speclen)) {
226
227
228
229
*rgb = known[i].rgb;
return 1;
}
return 0;
230
231
232
}
}
233
234
235
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
236
237
238
239
240
241
242
243
244
245
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.
*/
246
static char *get_next_line(char ***lines, SDL_RWops *src, int len)
247
{
248
249
char *linebufnew;
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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;
265
linebufnew = realloc(linebuf, buflen);
266
267
if(!linebufnew) {
free(linebuf);
268
269
270
error = "Out of memory";
return NULL;
}
271
linebuf = linebufnew;
272
273
274
275
276
277
278
279
280
281
282
283
284
}
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;
285
linebufnew = realloc(linebuf, buflen);
286
287
if(!linebufnew) {
free(linebuf);
288
289
290
error = "Out of memory";
return NULL;
}
291
linebuf = linebufnew;
292
293
294
295
296
297
298
299
300
301
302
}
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;
}
303
304
}
305
306
307
308
309
#define SKIPSPACE(p) \
do { \
while(isspace((unsigned char)*(p))) \
++(p); \
} while(0)
310
311
312
313
314
315
316
317
318
#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)
319
{
320
int start = 0;
321
SDL_Surface *image = NULL;
322
int index;
323
int x, y;
324
325
326
int w, h, ncolors, cpp;
int indexed;
Uint8 *dst;
327
struct color_hash *colors = NULL;
328
SDL_Color *im_colors = NULL;
329
330
331
332
333
char *keystrings = NULL, *nextkey;
char *line;
char ***xpmlines = NULL;
int pixels_len;
334
335
336
337
error = NULL;
linebuf = NULL;
buflen = 0;
338
if(src) {
339
start = SDL_RWtell(src);
340
341
342
343
} else {
error = "src is NULL";
goto done;
}
344
345
346
347
348
349
350
if(xpm)
xpmlines = &xpm;
line = get_next_line(xpmlines, src, 0);
if(!line)
goto done;
351
352
353
354
355
356
357
358
359
/*
* 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.
*/
360
if(sscanf(line, "%d %d %d %d", &w, &h, &ncolors, &cpp) != 4
361
|| w <= 0 || h <= 0 || ncolors <= 0 || cpp <= 0) {
362
363
error = "Invalid format description";
goto done;
364
}
365
366
367
keystrings = malloc(ncolors * cpp);
if(!keystrings) {
368
369
error = "Out of memory";
goto done;
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
}
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?) */
387
goto done;
388
}
389
390
/* Read the colors */
391
colors = create_colorhash(ncolors);
392
if (!colors) {
393
394
error = "Out of memory";
goto done;
395
}
396
for(index = 0; index < ncolors; ++index ) {
397
398
399
400
char *p;
line = get_next_line(xpmlines, src, 0);
if(!line)
goto done;
401
402
p = line + cpp + 1;
403
404
405
406
407
/* parse a colour definition */
for(;;) {
char nametype;
char *colname;
408
409
410
411
412
Uint32 rgb, pixel;
SKIPSPACE(p);
if(!*p) {
error = "colour parse error";
413
goto done;
414
}
415
416
417
418
419
nametype = *p;
SKIPNONSPACE(p);
SKIPSPACE(p);
colname = p;
SKIPNONSPACE(p);
420
421
422
if(nametype == 's')
continue; /* skip symbolic colour names */
423
if(!color_to_rgb(colname, p - colname, &rgb))
424
425
continue;
426
memcpy(nextkey, line, cpp);
427
428
if(indexed) {
SDL_Color *c = im_colors + index;
429
430
431
c->r = (Uint8)(rgb >> 16);
c->g = (Uint8)(rgb >> 8);
c->b = (Uint8)(rgb);
432
pixel = index;
433
} else
434
435
pixel = rgb;
add_colorhash(colors, nextkey, cpp, pixel);
436
437
nextkey += cpp;
if(rgb == 0xffffffff)
438
SDL_SetColorKey(image, SDL_SRCCOLORKEY, pixel);
439
break;
440
441
442
443
}
}
/* Read the pixels */
444
445
pixels_len = w * cpp;
dst = image->pixels;
446
447
for(y = 0; y < h; y++) {
line = get_next_line(xpmlines, src, pixels_len);
448
449
450
451
if(indexed) {
/* optimization for some common cases */
if(cpp == 1)
for(x = 0; x < w; x++)
452
dst[x] = (Uint8)QUICK_COLORHASH(colors,
453
line + x);
454
455
else
for(x = 0; x < w; x++)
456
dst[x] = (Uint8)get_colorhash(colors,
457
line + x * cpp,
458
459
460
461
cpp);
} else {
for (x = 0; x < w; x++)
((Uint32*)dst)[x] = get_colorhash(colors,
462
line + x * cpp,
463
464
465
cpp);
}
dst += image->pitch;
466
}
467
468
done:
469
if(error) {
470
if ( src )
471
SDL_RWseek(src, start, RW_SEEK_SET);
472
473
474
475
if ( image ) {
SDL_FreeSurface(image);
image = NULL;
}
476
477
478
IMG_SetError(error);
}
free(keystrings);
479
free_colorhash(colors);
480
free(linebuf);
481
482
483
return(image);
}
484
485
486
/* Load a XPM type image from an RWops datasource */
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
{
487
488
489
490
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
491
492
493
494
495
496
497
498
499
return load_xpm(NULL, src);
}
SDL_Surface *IMG_ReadXPMFromArray(char **xpm)
{
return load_xpm(xpm, NULL);
}
#else /* not LOAD_XPM */
500
501
502
503
504
505
506
/* See if an image is contained in a data source */
int IMG_isXPM(SDL_RWops *src)
{
return(0);
}
507
508
509
510
511
512
513
/* Load a XPM type image from an SDL datasource */
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
{
return(NULL);
}
514
515
516
517
518
SDL_Surface *IMG_ReadXPMFromArray(char **xpm)
{
return NULL;
}
#endif /* not LOAD_XPM */