Skip to content

Latest commit

 

History

History
462 lines (413 loc) · 9.83 KB

IMG_xpm.c

File metadata and controls

462 lines (413 loc) · 9.83 KB
 
Nov 29, 2000
Nov 29, 2000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
IMGLIB: An example image loading library for use with SDL
Copyright (C) 1999 Sam Lantinga
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
5635-34 Springhouse Dr.
Pleasanton, CA 94588 (USA)
slouken@devolution.com
*/
/* This is an XPM image file loading framework */
Nov 30, 2000
Nov 30, 2000
27
#include <stdlib.h>
Nov 29, 2000
Nov 29, 2000
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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)
{
int is_XPM;
char magic[10];
is_XPM = 0;
if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
Dec 6, 2000
Dec 6, 2000
44
if(memcmp(magic, "/* XPM */", 9) == 0) {
Nov 29, 2000
Nov 29, 2000
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
is_XPM = 1;
}
}
return(is_XPM);
}
static char *SDL_RWgets(char *string, int maxlen, SDL_RWops *src)
{
int i;
for ( i=0; i<(maxlen-1); ++i ) {
if ( SDL_RWread(src, &string[i], 1, 1) <= 0 ) {
/* EOF or error */
if ( i == 0 ) {
/* Hmm, EOF on initial read, return NULL */
Dec 6, 2000
Dec 6, 2000
60
return NULL;
Nov 29, 2000
Nov 29, 2000
61
62
63
64
65
66
67
}
break;
}
/* In this case it's okay to use either '\r' or '\n'
as line separators because blank lines are just
ignored by the XPM format.
*/
Dec 6, 2000
Dec 6, 2000
68
if ( (string[i] == '\n') || (string[i] == '\r') ) {
Nov 29, 2000
Nov 29, 2000
69
70
71
break;
}
}
Dec 6, 2000
Dec 6, 2000
72
string[i] = '\0';
Nov 29, 2000
Nov 29, 2000
73
74
75
76
return(string);
}
/* Hash table to look up colors from pixel strings */
Dec 6, 2000
Dec 6, 2000
77
78
79
80
81
82
83
84
#define STARTING_HASH_SIZE 256
struct hash_entry {
char *key;
Uint32 color;
struct hash_entry *next;
};
Nov 29, 2000
Nov 29, 2000
85
struct color_hash {
Dec 6, 2000
Dec 6, 2000
86
87
88
89
90
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
91
92
};
Dec 6, 2000
Dec 6, 2000
93
static int hash_key(const char *key, int cpp, int size)
Nov 29, 2000
Nov 29, 2000
94
95
96
97
98
{
int hash;
hash = 0;
while ( cpp-- > 0 ) {
Dec 6, 2000
Dec 6, 2000
99
hash = hash * 33 + *key++;
Nov 29, 2000
Nov 29, 2000
100
}
Dec 6, 2000
Dec 6, 2000
101
return hash & (size - 1);
Nov 29, 2000
Nov 29, 2000
102
103
}
Dec 6, 2000
Dec 6, 2000
104
static struct color_hash *create_colorhash(int maxnum)
Nov 29, 2000
Nov 29, 2000
105
{
Dec 6, 2000
Dec 6, 2000
106
int bytes, s;
Nov 29, 2000
Nov 29, 2000
107
108
struct color_hash *hash;
Dec 6, 2000
Dec 6, 2000
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* 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
131
132
133
}
static int add_colorhash(struct color_hash *hash,
Dec 6, 2000
Dec 6, 2000
134
char *key, int cpp, Uint32 color)
Nov 29, 2000
Nov 29, 2000
135
{
Dec 6, 2000
Dec 6, 2000
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;
Nov 29, 2000
Nov 29, 2000
143
144
}
Dec 6, 2000
Dec 6, 2000
145
/* fast lookup that works if cpp == 1 */
Mar 2, 2001
Mar 2, 2001
146
#define QUICK_COLORHASH(hash, key) ((hash)->table[*(Uint8 *)(key)]->color)
Dec 6, 2000
Dec 6, 2000
147
148
static Uint32 get_colorhash(struct color_hash *hash, const char *key, int cpp)
Nov 29, 2000
Nov 29, 2000
149
{
Dec 6, 2000
Dec 6, 2000
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;
Nov 29, 2000
Nov 29, 2000
155
}
Dec 6, 2000
Dec 6, 2000
156
return 0; /* garbage in - garbage out */
Nov 29, 2000
Nov 29, 2000
157
158
159
160
}
static void free_colorhash(struct color_hash *hash)
{
Dec 6, 2000
Dec 6, 2000
161
162
163
164
if(hash && hash->table) {
free(hash->table);
free(hash->entries);
free(hash);
Nov 29, 2000
Nov 29, 2000
165
166
167
}
}
Dec 6, 2000
Dec 6, 2000
168
#define ARRAYSIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
Nov 29, 2000
Nov 29, 2000
169
Dec 6, 2000
Dec 6, 2000
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* convert colour spec to RGB (in 0xrrggbb format).
* return 1 if successful. may scribble on the colorspec buffer.
*/
static int color_to_rgb(char *spec, Uint32 *rgb)
{
/* 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];
++spec;
switch(strlen(spec)) {
Nov 29, 2000
Nov 29, 2000
190
case 3:
Dec 6, 2000
Dec 6, 2000
191
192
193
buf[0] = buf[1] = spec[0];
buf[2] = buf[3] = spec[1];
buf[4] = buf[5] = spec[2];
Nov 29, 2000
Nov 29, 2000
194
195
break;
case 6:
Dec 6, 2000
Dec 6, 2000
196
memcpy(buf, spec, 6);
Nov 29, 2000
Nov 29, 2000
197
198
break;
case 12:
Dec 6, 2000
Dec 6, 2000
199
200
201
202
203
204
buf[0] = spec[0];
buf[1] = spec[1];
buf[2] = spec[4];
buf[3] = spec[5];
buf[4] = spec[8];
buf[5] = spec[9];
Nov 29, 2000
Nov 29, 2000
205
break;
Dec 6, 2000
Dec 6, 2000
206
207
208
209
210
211
212
213
214
215
216
217
}
buf[6] = '\0';
*rgb = strtol(buf, NULL, 16);
return 1;
} else {
int i;
for(i = 0; i < ARRAYSIZE(known); i++)
if(IMG_string_equals(known[i].name, spec)) {
*rgb = known[i].rgb;
return 1;
}
return 0;
Nov 29, 2000
Nov 29, 2000
218
219
220
}
}
Dec 6, 2000
Dec 6, 2000
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
static char *skipspace(char *p)
{
while(isspace((unsigned char)*p))
++p;
return p;
}
static char *skipnonspace(char *p)
{
while(!isspace((unsigned char)*p) && *p)
++p;
return p;
}
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
Nov 29, 2000
Nov 29, 2000
239
240
241
242
243
/* Load a XPM type image from an SDL datasource */
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
{
SDL_Surface *image;
char line[1024];
Dec 6, 2000
Dec 6, 2000
244
char *here;
Nov 29, 2000
Nov 29, 2000
245
int index;
Dec 6, 2000
Dec 6, 2000
246
int x, y;
Nov 29, 2000
Nov 29, 2000
247
248
int w, h, ncolors, cpp;
int pixels_len;
Dec 6, 2000
Dec 6, 2000
249
char *pixels = NULL;
Nov 29, 2000
Nov 29, 2000
250
251
252
int indexed;
Uint8 *dst;
struct color_hash *colors;
Dec 6, 2000
Dec 6, 2000
253
254
255
SDL_Color *im_colors = NULL;
char *keystrings, *nextkey;
char *error = NULL;
Nov 29, 2000
Nov 29, 2000
256
257
258
/* Skip to the first string, which describes the image */
do {
Dec 6, 2000
Dec 6, 2000
259
260
here = SDL_RWgets(line, sizeof(line), src);
if ( !here ) {
Nov 29, 2000
Nov 29, 2000
261
262
263
IMG_SetError("Premature end of data");
return(NULL);
}
Dec 6, 2000
Dec 6, 2000
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
here = skipspace(here);
} while(*here != '"');
/*
* 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.
*/
if(sscanf(here + 1, "%d %d %d %d", &w, &h, &ncolors, &cpp) != 4
|| w <= 0 || h <= 0 || ncolors <= 0 || cpp <= 0) {
IMG_SetError("Invalid format description");
return(NULL);
}
Nov 29, 2000
Nov 29, 2000
280
Dec 6, 2000
Dec 6, 2000
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
keystrings = malloc(ncolors * cpp);
if(!keystrings) {
IMG_SetError("Out of memory");
free(pixels);
return NULL;
}
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?) */
free(pixels);
return(NULL);
}
Nov 29, 2000
Nov 29, 2000
306
307
/* Read the colors */
Dec 6, 2000
Dec 6, 2000
308
colors = create_colorhash(ncolors);
Nov 29, 2000
Nov 29, 2000
309
if ( ! colors ) {
Dec 6, 2000
Dec 6, 2000
310
311
error = "Out of memory";
goto done;
Nov 29, 2000
Nov 29, 2000
312
}
Dec 6, 2000
Dec 6, 2000
313
314
315
316
317
318
319
320
321
for(index = 0; index < ncolors; ++index ) {
char *key;
int len;
do {
here = SDL_RWgets(line, sizeof(line), src);
if(!here) {
error = "Premature end of data";
goto done;
Nov 29, 2000
Nov 29, 2000
322
}
Dec 6, 2000
Dec 6, 2000
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
here = skipspace(here);
} while(*here != '"');
++here;
len = strlen(here);
if(len < cpp + 7)
continue; /* cannot be a valid line */
key = here;
key[cpp] = '\0';
here += cpp + 1;
/* parse a colour definition */
for(;;) {
char nametype;
char *colname;
char delim;
Uint32 rgb;
here = skipspace(here);
nametype = *here;
here = skipnonspace(here);
here = skipspace(here);
colname = here;
while(*here && !isspace((unsigned char)*here)
&& *here != '"')
here++;
if(!*here) {
error = "color parse error";
goto done;
Nov 29, 2000
Nov 29, 2000
353
}
Dec 6, 2000
Dec 6, 2000
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
if(nametype == 's')
continue; /* skip symbolic colour names */
delim = *here;
*here = '\0';
if(delim)
here++;
if(!color_to_rgb(colname, &rgb))
continue;
memcpy(nextkey, key, cpp);
if(indexed) {
SDL_Color *c = im_colors + index;
c->r = rgb >> 16;
c->g = rgb >> 8;
c->b = rgb;
add_colorhash(colors, nextkey, cpp, index);
} else
add_colorhash(colors, nextkey, cpp, rgb);
nextkey += cpp;
if(rgb == 0xffffffff)
SDL_SetColorKey(image, SDL_SRCCOLORKEY,
indexed ? index : rgb);
break;
Nov 29, 2000
Nov 29, 2000
379
380
381
382
}
}
/* Read the pixels */
Dec 6, 2000
Dec 6, 2000
383
384
385
386
387
388
389
390
pixels_len = w * cpp;
pixels = malloc(MAX(pixels_len + 5, 20));
if(!pixels) {
error = "Out of memory";
goto done;
}
dst = image->pixels;
for (y = 0; y < h; ) {
Mar 2, 2001
Mar 2, 2001
391
char *s;
Dec 6, 2000
Dec 6, 2000
392
393
394
395
396
397
398
399
400
401
402
403
404
char c;
do {
if(SDL_RWread(src, &c, 1, 1) <= 0) {
error = "Premature end of data";
goto done;
}
} while(c == ' ');
if(c != '"') {
/* comment or empty line, skip it */
while(c != '\n' && c != '\r') {
if(SDL_RWread(src, &c, 1, 1) <= 0) {
error = "Premature end of data";
goto done;
Nov 29, 2000
Nov 29, 2000
405
406
}
}
Dec 6, 2000
Dec 6, 2000
407
continue;
Nov 29, 2000
Nov 29, 2000
408
}
Dec 6, 2000
Dec 6, 2000
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
if(SDL_RWread(src, pixels, pixels_len + 3, 1) <= 0) {
error = "Premature end of data";
goto done;
}
s = pixels;
if(indexed) {
/* optimization for some common cases */
if(cpp == 1)
for(x = 0; x < w; x++)
dst[x] = QUICK_COLORHASH(colors,
s + x);
else
for(x = 0; x < w; x++)
dst[x] = get_colorhash(colors,
s + x * cpp,
cpp);
} else {
for (x = 0; x < w; x++)
((Uint32*)dst)[x] = get_colorhash(colors,
s + x * cpp,
cpp);
}
dst += image->pitch;
y++;
Nov 29, 2000
Nov 29, 2000
433
}
Dec 6, 2000
Dec 6, 2000
434
Nov 29, 2000
Nov 29, 2000
435
done:
Dec 6, 2000
Dec 6, 2000
436
437
438
439
440
441
if(error) {
if(image)
SDL_FreeSurface(image);
image = NULL;
IMG_SetError(error);
}
Nov 29, 2000
Nov 29, 2000
442
free(pixels);
Dec 6, 2000
Dec 6, 2000
443
free(keystrings);
Nov 29, 2000
Nov 29, 2000
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
free_colorhash(colors);
return(image);
}
#else
/* See if an image is contained in a data source */
int IMG_isXPM(SDL_RWops *src)
{
return(0);
}
/* Load a XPM type image from an SDL datasource */
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_XPM */