File: gimagereadxpm.c

package info (click to toggle)
fontforge 0.0.20120101%2Bgit-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,756 kB
  • sloc: ansic: 556,562; sh: 236; makefile: 162; xml: 11; python: 11
file content (352 lines) | stat: -rw-r--r-- 10,364 bytes parent folder | download
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
/* Copyright (C) 2000-2011 by George Williams */
/*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.

 * The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "gimage.h"
/*#include "gxdrawP.h"*/
#include "string.h"
#include "utype.h"

/* X Pixmap format
These are c files, all the significant data resides in strings, everything
else may be ignored (well, the first line must be the comment / * XPM * /)

    / * XPM * /
    static char * plaid[] =
    {
    / * plaid pixmap * /
    / * width height ncolors chars_per_pixel * /
    "22 22 4 2 0 0 XPMEXT",
    / * colors * /
    "   c red       m white  s light_color",
    "Y  c green     m black  s ines_in_mix",
    "+  c yellow    m white  s lines_in_dark ",
    "x              m black  s dark_color ",
    / * pixels * /
    "x   x   x x x   x   x x x x x x + x x x x x ",
    "  x   x   x   x   x   x x x x x x x x x x x ",
    "x   x   x x x   x   x x x x x x + x x x x x ",

The first string has 4 interesting numbers in it: width, height, ncolors,
characters per pixel, these may be followed by the hot spot location (2 nums)
or the text XPMEXT which means there are extensions later.

The next few strings (there will be ncolors of them) provide a mapping between
character(s) and colors.  Above the "  " combination maps to red on a color
display, white on monochrome and is called light_color.

After that come lines of pixels.  Each line is width*chars_per_pixel long and
there are height of them.

color names may be: a name in the x database (ie sky blue), #xxxxxx (hex rgb),
%xxxxxx (hex hsb), None==transparent, "a symbolic name" and I don't know what
good they are.
*/
/* There seems to be another, similar format XPM2 which is the same except
    nothing is in a string.  Example:
! XPM2  
64 64 2 1
  c #FFFFFFFFFFFF
. c #000000000000
                     ..       .   .                             
 .                                    .  ..                     
  .                                                             
     .                 .                                        
        ..                                  .                   
                                                                
                                                                
                                                .               
                            .                                   
*/

static long LookupXColorName(char *name) {
#if 0 && !defined( X_DISPLAY_MISSING )
    XColor ret;
    Display *display;

    if ( screen_display==NULL )
return( COLOR_UNKNOWN );

    display = ((GXDisplay *) screen_display)->display;
    if ( XParseColor(display,DefaultColormap(display,DefaultScreen(display)),
	    name,&ret))
return( ((ret.red>>8)<<16) | (ret.green&0xff00) | (ret.blue>>8) );
#endif	/* NO X */

return( COLOR_UNKNOWN );
}

/* Don't handle backslash sequences, nor concatenated strings */
static int getstring(unsigned char *buf, int sz, FILE *fp) {
    int ch, incomment=0;

    while ((ch=getc(fp))!=EOF ) {
	if ( ch=='"' && !incomment )
    break;
	if ( !incomment && ch=='/' ) {
	    ch = getc(fp);
	    if ( ch=='*' ) incomment=true;
	    else ungetc(ch,fp);
	} else if ( incomment && ch=='*' ) {
	    ch = getc(fp);
	    if ( ch=='/' ) incomment = false;
	    else ungetc(ch,fp);
	}
    }
    if ( ch==EOF )
return( 0 );
    while ( (ch=getc(fp))!=EOF && ch!='"' ) {
	if ( --sz>0 )
	    *buf++ = ch;
    }
    *buf = '\0';
return(1 );
}

static int gww_getline(unsigned char *buf, int sz, FILE *fp) {
    int ch;
    unsigned char *pt=buf;

    while ((ch=getc(fp))!=EOF && ch!='\n' && ch!='\r')
	*pt++ = ch;
    if ( ch=='\r' ) {
	if ((ch = getc(fp))!='\n' )
	    ungetc(ch,fp);
    }
    *pt = '\0';
    if ( ch==EOF && pt==buf )
return( 0 );
return(1 );
}

#define TRANS 0x1000000
union hash {
    long color;
    union hash *table;
};

static void freetab(union hash *tab, int nchars) {
    int i;

    if ( nchars>1 ) {
	for ( i=0; i<256; ++i )
	    if ( tab[i].table!=NULL )
		freetab(tab[i].table,nchars-1);
    }
    gfree(tab);
}
	
static int fillupclut(Color *clut, union hash *tab,int index,int nchars) {
    int i;

    if ( nchars==1 ) {
	for ( i=0; i<256; ++i )
	    if ( tab[i].color!=-1 ) {
		if ( tab[i].color == TRANS ) {
		    clut[256] = index;
		    tab[i].color = 0;
		}
		clut[index] = tab[i].color;
		tab[i].color = index++;
	    }
    } else {
	for ( i=0; i<256; ++i )
	    if ( tab[i].table!=NULL )
		index = fillupclut(clut,tab[i].table,index,nchars-1);
    }
return( index );
}
	    
static long parsecol(char *start, char *end) {
    long ret = -1;
    int ch;
    extern long LookupXColorName(char *);

    while ( !isspace(*start) && *start!='\0' ) ++start;
    while ( isspace(*start)) ++start;
    while ( end>start && isspace(end[-1])) --end;
    ch = *end; *end = '\0';

    if ( strcmp(start,"None")==0 )
	ret = TRANS;
    else if ( *start=='#' || *start=='%') {
	if ( end-start==4 ) {
	    sscanf(start+1,"%lx",&ret);
	    ret = ((ret&0xf00)<<12) | ((ret&0xf0)<<8) | ((ret&0xf)<<4);
	} else if ( end-start==7 )
	    sscanf(start+1,"%lx",&ret);
	else if ( end-start==13 ) {
	    int r,g,b;
	    sscanf(start+1,"%4x%4x%4x",&r,&g,&b);
	    ret = ((r>>8)<<16) | ((g>>8)<<8) | (b>>8);
	}
	if ( *start=='%' )
	    /* How do I translate from HSB to RGB???? */;
    } else if (( ret=LookupXColorName(start))!=-1 ) {
    } else if ( strcmp(start,"white")==0 ) {
	ret = COLOR_CREATE(255,255,255);
    } else {
	ret = 0;
    }

    *end = ch;
return ret;
}

static char *findnextkey(char *str) {
    int oktostart=1;

    while ( *str ) {
	if ( isspace(*str)) oktostart=true;
	else if ( oktostart ) {
	    if (( *str=='c' && isspace(str[1])) ||
		    (*str=='m' && isspace(str[1])) ||
		    (*str=='g' && isspace(str[1])) ||
		    (*str=='g' && str[1]=='4' && isspace(str[2])) ||
		    (*str=='s' && isspace(str[1])) )
return( str );
	    oktostart = false;
	}
	++str;
    }
return( str );
}

static long findcol(char *str) {
    char *pt, *end;
    char *try = "cgm";		/* Try in this order to find something */

    while ( *try ) {
	pt = findnextkey(str);
	while ( *pt ) {
	    end = findnextkey(pt+2);
	    if ( *pt==*try )
return( parsecol(pt,end));
	    pt = end;
	}
	++try;
    }
	
return 0;
}

static union hash *parse_colors(FILE *fp,unsigned char *line, int lsiz, int ncols, int nchars,
	int (*getdata)(unsigned char *,int,FILE *)) {
    union hash *tab = (union hash *) galloc(256*sizeof(union hash));
    union hash *sub;
    int i, j;

    if ( nchars==1 )
	memset(tab,-1,256*sizeof(union hash));
    for ( i=0; i<ncols; ++i ) {
	if ( !getdata(line,lsiz,fp)) {
	    freetab(tab,nchars);
return( NULL );
	}
	sub = tab;
	for ( j=0; j<nchars-1; ++j ) {
	    if ( sub[line[j]].table==NULL ) {
		sub[line[j]].table = (union hash *) galloc(256*sizeof(union hash));
		if ( j==nchars-2 )
		    memset(sub[line[j]].table,-1,256*sizeof(union hash));
	    }
	    sub = sub[line[j]].table;
	}
	sub[line[j]].color = findcol((char *) line+j+1);
    }
return( tab );
}

GImage *GImageReadXpm(char * filename) {
   FILE *fp;
   GImage *ret=NULL;
   struct _GImage *base;
   int width, height, cols, nchar;
   unsigned char buf[80];
   unsigned char *line, *lpt;
   int y,j, lsiz;
   union hash *tab, *sub;
   unsigned char *pt, *end; unsigned long *ipt;
   int (*getdata)(unsigned char *,int,FILE *) = NULL;

   fp = fopen(filename, "r");
   if (!fp)
return( NULL );

    fgets((char *) buf,sizeof(buf),fp);
    if ( strstr((char *) buf,"XPM2")!=NULL )
	getdata = gww_getline;
    else if ( strstr((char *) buf,"/*")!=NULL && strstr((char *) buf,"XPM")!=NULL && strstr((char *) buf,"*/")!=NULL )
	getdata = getstring;
    if ( getdata==NULL ||
	    !getdata( buf,sizeof(buf),fp) ||
	    sscanf((char *) buf,"%d %d %d %d", &width, &height, &cols, &nchar)!=4 ) {
	fclose(fp);
return( NULL );
    }
    line = galloc(lsiz = nchar*width+20);
    tab = parse_colors(fp,line,lsiz,cols,nchar,getdata);
    if ( cols<=256 ) {
	Color clut[257];
	clut[256] = COLOR_UNKNOWN;
	fillupclut(clut,tab,0,nchar);
	ret = GImageCreate(it_index,width,height);
	ret->u.image->clut->clut_len = cols;
	memcpy(ret->u.image->clut->clut,clut,cols*sizeof(Color));
	ret->u.image->trans = clut[256];
	ret->u.image->clut->trans_index = clut[256];
    } else {
	ret = GImageCreate(it_true,width,height);
	ret->u.image->trans = TRANS;		/* TRANS isn't a valid Color, but it fits in our 32 bit pixels */
    }
    base = ret->u.image;
    for ( y=0; y<height; ++y ) {
	if ( !getdata(line,lsiz,fp)) {
	    GImageDestroy(ret);
	    freetab(tab,nchar);
	    fclose(fp);
return( NULL );
	}
	pt = (uint8 *) (base->data+y*base->bytes_per_line); ipt = NULL; end = pt+width;
	if ( cols>256 )
	    ipt = (unsigned long *) pt;
	for ( lpt=line; *line && pt<end; ) {
	    sub = tab;
	    for ( j=0; *lpt && j<nchar-1; ++j, ++lpt )
		if ( sub!=NULL )
		    sub = sub[*lpt].table;
	    if ( sub!=NULL ) {
		if ( cols<=256 )
		    *pt = sub[*lpt].color;
		else
		    *ipt = sub[*lpt].color;
	    }
	    ++pt; ++ipt; ++lpt;
	}
    }
    freetab(tab,nchar);
    fclose(fp);
return( ret );
}