Skip to content

Latest commit

 

History

History
470 lines (433 loc) · 10 KB

IMG_xpm.c

File metadata and controls

470 lines (433 loc) · 10 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
353
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
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 */
#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) ) {
if ( strncmp(magic, "/* XPM */", 9) == 0 ) {
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 */
string = NULL;
}
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.
*/
if ( (string[i] == '\n') || (string[i] == '\n') ) {
break;
}
}
if ( string ) {
string[i] = '\0';
}
return(string);
}
/* Hash table to look up colors from pixel strings */
#define HASH_SIZE 256
struct color_hash {
struct hash_entry {
int keylen;
char *key;
Uint32 color;
struct hash_entry *next;
} *entries[HASH_SIZE];
};
static int hash_key(const char *key, int cpp)
{
int hash;
hash = 0;
while ( cpp-- > 0 ) {
hash += *key++;
}
return(hash%HASH_SIZE);
}
static struct color_hash *create_colorhash(void)
{
struct color_hash *hash;
hash = (struct color_hash *)malloc(sizeof *hash);
if ( hash ) {
memset(hash, 0, (sizeof *hash));
}
return(hash);
}
static int add_colorhash(struct color_hash *hash,
const char *key, int cpp, Uint32 color)
{
int hash_index;
struct hash_entry *prev, *entry;
/* Create the hash entry */
entry = (struct hash_entry *)malloc(sizeof *entry);
if ( ! entry ) {
return(0);
}
entry->keylen = cpp;
entry->key = strdup(key);
if ( ! entry->key ) {
free(entry);
return(0);
}
entry->color = color;
entry->next = NULL;
/* Add it to the hash table */
hash_index = hash_key(key, cpp);
for ( prev = hash->entries[hash_index];
prev && prev->next; prev = prev->next ) {
/* Go to the end of the list */ ;
}
if ( prev ) {
prev->next = entry;
} else {
hash->entries[hash_index] = entry;
}
return(1);
}
static int get_colorhash(struct color_hash *hash,
const char *key, int cpp, Uint32 *color)
{
int hash_index;
struct hash_entry *entry;
hash_index = hash_key(key, cpp);
for ( entry = hash->entries[hash_index]; entry; entry = entry->next ) {
if ( strncmp(key, entry->key, entry->keylen) == 0 ) {
*color = entry->color;
return(1);
}
}
return(0);
}
static void free_colorhash(struct color_hash *hash)
{
int i;
struct hash_entry *entry, *freeable;
for ( i=0; i<HASH_SIZE; ++i ) {
entry = hash->entries[i];
while ( entry ) {
freeable = entry;
entry = entry->next;
free(freeable->key);
free(freeable);
}
}
free(hash);
}
static int color_to_rgb(const char *colorspec, int *r, int *g, int *b)
{
char rbuf[3];
char gbuf[3];
char bbuf[3];
/* Handle monochrome black and white */
if ( strcasecmp(colorspec, "black") == 0 ) {
*r = 0;
*g = 0;
*b = 0;
return(1);
}
if ( strcasecmp(colorspec, "white") == 0 ) {
*r = 255;
*g = 255;
*b = 255;
return(1);
}
/* Normal hexidecimal color */
switch (strlen(colorspec)) {
case 3:
rbuf[0] = colorspec[0];
rbuf[1] = colorspec[0];
gbuf[0] = colorspec[1];
gbuf[1] = colorspec[1];
bbuf[0] = colorspec[2];
bbuf[1] = colorspec[2];
break;
case 6:
rbuf[0] = colorspec[0];
rbuf[1] = colorspec[1];
gbuf[0] = colorspec[2];
gbuf[1] = colorspec[3];
bbuf[0] = colorspec[4];
bbuf[1] = colorspec[5];
break;
case 12:
rbuf[0] = colorspec[0];
rbuf[1] = colorspec[1];
gbuf[0] = colorspec[4];
gbuf[1] = colorspec[5];
bbuf[0] = colorspec[8];
bbuf[1] = colorspec[9];
break;
default:
return(0);
}
rbuf[2] = '\0';
*r = (int)strtol(rbuf, NULL, 16);
gbuf[2] = '\0';
*g = (int)strtol(gbuf, NULL, 16);
bbuf[2] = '\0';
*b = (int)strtol(bbuf, NULL, 16);
return(1);
}
/* Load a XPM type image from an SDL datasource */
SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src)
{
SDL_Surface *image;
char line[1024];
char *here, *stop;
int index;
int i, x, y;
int w, h, ncolors, cpp;
int found;
int r, g, b;
Uint32 colorkey;
char *colorkey_string;
int pixels_len;
char *pixels;
int indexed;
Uint8 *dst;
Uint32 pixel;
struct color_hash *colors;
/* Skip to the first string, which describes the image */
image = NULL;
do {
here = SDL_RWgets(line, sizeof(line), src);
if ( ! here ) {
IMG_SetError("Premature end of data");
return(NULL);
}
if ( *here == '"' ) {
++here;
/* Skip to width */
while ( isspace(*here) ) ++here;
w = atoi(here);
while ( ! isspace(*here) ) ++here;
/* Skip to height */
while ( isspace(*here) ) ++here;
h = atoi(here);
while ( ! isspace(*here) ) ++here;
/* Skip to number of colors */
while ( isspace(*here) ) ++here;
ncolors = atoi(here);
while ( ! isspace(*here) ) ++here;
/* Skip to characters per pixel */
while ( isspace(*here) ) ++here;
cpp = atoi(here);
while ( ! isspace(*here) ) ++here;
/* Verify the parameters */
if ( !w || !h || !ncolors || !cpp ) {
IMG_SetError("Invalid format description");
return(NULL);
}
pixels_len = 1+w*cpp+1+1;
pixels = (char *)malloc(pixels_len);
if ( ! pixels ) {
IMG_SetError("Out of memory");
return(NULL);
}
/* Create the new surface */
if ( ncolors <= 256 ) {
indexed = 1;
image = SDL_CreateRGBSurface(SDL_SWSURFACE,
w, h, 8, 0, 0, 0, 0);
} else {
int rmask, gmask, bmask;
indexed = 0;
if ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) {
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
} else {
rmask = 0x00ff0000;
gmask = 0x0000ff00;
bmask = 0x000000ff;
}
image = SDL_CreateRGBSurface(SDL_SWSURFACE,
w, h, 32,
rmask, gmask, bmask, 0);
}
if ( ! image ) {
/* Hmm, some SDL error (out of memory?) */
free(pixels);
return(NULL);
}
}
} while ( ! image );
/* Read the colors */
colors = create_colorhash();
if ( ! colors ) {
SDL_FreeSurface(image);
free(pixels);
IMG_SetError("Out of memory");
return(NULL);
}
colorkey_string = NULL;
for ( index=0; index < ncolors; ++index ) {
here = SDL_RWgets(line, sizeof(line), src);
if ( ! here ) {
SDL_FreeSurface(image);
image = NULL;
IMG_SetError("Premature end of data");
goto done;
}
if ( *here == '"' ) {
const char *key;
++here;
/* Grab the pixel key */
key = here;
for ( i=0; i<cpp; ++i ) {
if ( ! *here++ ) {
/* Parse error */
continue;
}
}
if ( *here ) {
*here++ = '\0';
}
/* Find the color identifier */
found = 0;
while ( *here && ! found ) {
while ( isspace(*here) ) ++here;
if ( (*here != 'c') &&
(*here != 'g') &&
(*here != 'm') ) {
/* Skip color type */
while ( *here && !isspace(*here) )
++here;
/* Skip color name */
while ( isspace(*here) ) ++here;
while ( *here && !isspace(*here) )
++here;
continue;
}
++here;
while ( isspace(*here) ) ++here;
if ( strncasecmp(here, "None", 4) == 0 ) {
colorkey_string = strdup(key);
if ( indexed ) {
colorkey = (Uint32)index;
} else {
colorkey = 0xFFFFFFFF;
}
found = 1;
continue;
}
if ( *here == '#' ) {
++here;
}
while ( isspace(*here) ) ++here;
for ( stop=here; isalnum(*stop); ++stop ) {
/* Skip the pixel color */;
}
*stop++ = '\0';
found = color_to_rgb(here, &r, &g, &b);
if ( found ) {
if ( indexed ) {
SDL_Color *color;
color = &image->format->palette->colors[index];
color->r = (Uint8)r;
color->g = (Uint8)g;
color->b = (Uint8)b;
pixel = index;
} else {
pixel = (r<<16)|(g<<8)|b;
}
add_colorhash(colors, key, cpp, pixel);
}
*here = '\0';
}
if ( ! found ) {
/* Hum, couldn't parse a color.. */;
}
}
}
/* Read the pixels */
for ( y=0; y < h; ) {
here = SDL_RWgets(pixels, pixels_len, src);
if ( ! here ) {
SDL_FreeSurface(image);
image = NULL;
IMG_SetError("Premature end of data");
goto done;
}
if ( *here == '"' ) {
++here;
dst = (Uint8 *)image->pixels + y*image->pitch;
for ( x=0; x<w; ++x ) {
pixel = 0;
if ( colorkey_string &&
(strncmp(here,colorkey_string,cpp)==0) ) {
pixel = colorkey;
} else {
get_colorhash(colors, here,cpp, &pixel);
}
if ( indexed ) {
*dst++ = pixel;
} else {
*((Uint32 *)dst)++ = pixel;
}
for ( i=0; *here && i<cpp; ++i ) {
++here;
}
}
++y;
}
}
if ( colorkey_string ) {
SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
}
done:
free(pixels);
free_colorhash(colors);
if ( colorkey_string ) {
free(colorkey_string);
}
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 */