Debian Codegraph @

gutils/gimagereadxpm.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /* 2013apr11, additional fixes and error checks done, Jose Da Silva */
3 /*
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13
14 * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <fontforge-config.h>
30
31 #include "gimage.h"
32 /*#include "gxdrawP.h"*/
33 #include "utype.h"
34
35 /* X Pixmap format
36 These are c files, all the significant data resides in strings, everything
37 else may be ignored (well, the first line must be the comment / * XPM * /)
38
39 / * XPM * /
40 static char * plaid[] =
41 {
42 / * plaid pixmap * /
43 / * width height ncolors chars_per_pixel * /
44 "22 22 4 2 0 0 XPMEXT",
45 / * colors * /
46 " c red m white s light_color",
47 "Y c green m black s ines_in_mix",
48 "+ c yellow m white s lines_in_dark ",
49 "x m black s dark_color ",
50 / * pixels * /
51 "x x x x x x x x x x x x + x x x x x ",
52 " x x x x x x x x x x x x x x x x ",
53 "x x x x x x x x x x x x + x x x x x ",
54
55 The first string has 4 interesting numbers in it: width, height, ncolors,
56 characters per pixel, these may be followed by the hot spot location (2 nums)
57 or the text XPMEXT which means there are extensions later.
58
59 The next few strings (there will be ncolors of them) provide a mapping between
60 character(s) and colors. Above the " " combination maps to red on a color
61 display, white on monochrome and is called light_color.
62
63 After that come lines of pixels. Each line is width*chars_per_pixel long and
64 there are height of them.
65
66 color names may be: a name in the x database (ie sky blue), #xxxxxx (hex rgb),
67 %xxxxxx (hex hsb), None==transparent, "a symbolic name" and I don't know what
68 good they are.
69 */
70 /* There seems to be another, similar format XPM2 which is the same except
71 nothing is in a string. Example:
72 ! XPM2
73 64 64 2 1
74 . c #FFFFFFFFFFFF
75 + c #000000000000
76 .....................++.......+...+.............................
77 .+....................................+..++.....................
78 ..+.............................................................
79 .....+.................+........................................
80 ........++..................................+...................
81 ................................................................
82 ................................................................
83 ................................................+...............
84 ............................+...................................
85 */
86
87 static int getstring(unsigned char *buf,int sz,FILE *fp) {
88 /* get a string of text within "" marks and skip */
89 /* backslash sequences, or concatenated strings. */
90 int ch, incomment=0;
91
92 while ( (ch=getc(fp))>=0 ) {
93 if ( ch=='"' && !incomment )
94 break;
95 if ( !incomment && ch=='/' ) {
96 if ( (ch=getc(fp))<0 ) break;
97 if ( ch=='*' ) incomment=true;
98 else ungetc(ch,fp);
99 } else if ( incomment && ch=='*' ) {
100 if ( (ch=getc(fp))<0 ) break;
101 if ( ch=='/' ) incomment=false;
102 else ungetc(ch,fp);
103 }
104 }
105 if ( ch<0 )
106 return( 0 );
107
108 /* Get data within quote marks */
109 while ( --sz>0 && (ch=getc(fp))>=0 && ch!='"' )
110 *buf++ = ch;
111 if ( ch!='"' )
112 return( 0 );
113 *buf = '\0';
114 return( 1 );
115 }
116
117 static int gww_getline(unsigned char *buf,int sz,FILE *fp) {
118 /* get a single line of text (leave-out the '\n') */
119 int ch=0;
120 unsigned char *pt=buf;
121
122 while ( --sz>0 && (ch=getc(fp))>=0 && ch!='\n' && ch!='\r' )
123 *pt++ = ch;
124 if ( ch=='\r' && (ch=getc(fp))!='\n' )
125 ungetc(ch,fp);
126 *pt = '\0';
127 if ( ch<0 && pt==buf )
128 return( 0 );
129 return( 1 );
130 }
131
132 #define TRANS 0x1000000
133 union hash {
134 long color;
135 union hash *table;
136 };
137
138 static void freetab(union hash *tab, int nchars) {
139 int i;
140
141 if ( tab && nchars>1 ) {
142 for ( i=0; i<256; ++i )
143 if ( tab[i].table!=NULL )
144 freetab(tab[i].table,nchars-1);
145 }
146 free(tab);
147 }
148
149 static int fillupclut(Color *clut, union hash *tab,int index,int nchars) {
150 int i;
151
152 if ( nchars==1 ) {
153 for ( i=0; i<256; ++i )
154 if ( tab[i].color!=-1 ) {
155 if ( tab[i].color == TRANS ) {
156 clut[256] = index;
157 tab[i].color = 0;
158 }
159 clut[index] = tab[i].color;
160 tab[i].color = index++;
161 }
162 } else {
163 for ( i=0; i<256; ++i )
164 if ( tab[i].table!=NULL )
165 index = fillupclut(clut,tab[i].table,index,nchars-1);
166 }
167 return( index );
168 }
169
170 static long parsecol(char *start, char *end) {
171 long ret = -1;
172 int ch;
173
174 while ( !isspace(*start) && *start!='\0' ) ++start;
175 while ( isspace(*start) ) ++start;
176 while ( end>start && isspace(end[-1]) ) --end;
177 ch = *end; *end = '\0';
178
179 if ( strcmp(start,"None")==0 )
180 ret = TRANS; /* no_color==transparent */
181 else if ( *start=='#' || *start=='%' ) {
182 if ( end-start==4 ) {
183 sscanf(start+1,"%lx",&ret);
184 ret = ((ret&0xf00)<<12) | ((ret&0xf0)<<8) | ((ret&0xf)<<4);
185 } else if ( end-start==7 )
186 sscanf(start+1,"%lx",&ret);
187 else if ( end-start==13 ) {
188 int r,g,b;
189 sscanf(start+1,"%4x%4x%4x",&r,&g,&b);
190 ret = ((r>>8)<<16) | ((g>>8)<<8) | (b>>8);
191 }
192 if ( *start=='%' ) {
193 /* TODO! */
194 /* How do I translate from HSB to RGB???? */
195 ;
196 }
197 } else if ( strcmp(start,"white")==0 ) {
198 ret = COLOR_CREATE(255,255,255);
199 } else {
200 ret = 0;
201 }
202
203 *end = ch;
204 return ret;
205 }
206
207 static char *findnextkey(char *str) {
208 int oktostart=1;
209
210 while ( *str ) {
211 if ( isspace(*str)) oktostart=true;
212 else if ( oktostart ) {
213 if (( *str=='c' && isspace(str[1])) ||
214 (*str=='m' && isspace(str[1])) ||
215 (*str=='g' && isspace(str[1])) ||
216 (*str=='g' && str[1]=='4' && isspace(str[2])) ||
217 (*str=='s' && isspace(str[1])) )
218 return( str );
219 oktostart = false;
220 }
221 ++str;
222 }
223 return( str );
224 }
225
226 static long findcol(char *str) {
227 char *pt, *end;
228 const char *try_order = "cgm"; /* Try in this order to find something */
229
230 while ( *try_order ) {
231 pt = findnextkey(str);
232 while ( *pt ) {
233 end = findnextkey(pt+2);
234 if ( *pt==*try_order )
235 return( parsecol(pt,end) );
236 pt = end;
237 }
238 ++try_order;
239 }
240 return( 0 );
241 }
242
243 static union hash *parse_colors(FILE *fp,unsigned char *line, int lsiz, int ncols, int nchars,
244 int (*getdata)(unsigned char *,int,FILE *)) {
245 union hash *tab;
246 union hash *sub;
247 int i, j;
248
249 if ( (tab=(union hash *)malloc(256*sizeof(union hash)))==NULL ) {
250 NoMoreMemMessage();
251 return( NULL );
252 }
253
254 if ( nchars==1 )
255 memset(tab,-1,256*sizeof(union hash));
256 for ( i=0; i<ncols; ++i ) {
257 if ( !getdata(line,lsiz,fp) ) {
258 freetab(tab,nchars);
259 return( NULL );
260 }
261 sub = tab;
262 for ( j=0; j<nchars-1; ++j ) {
263 if ( sub[line[j]].table==NULL ) {
264 if ( (sub[line[j]].table=(union hash *)malloc(256*sizeof(union hash)))==NULL ) {
265 NoMoreMemMessage();
266 freetab(tab,nchars);
267 return( NULL );
268 }
269 if ( j==nchars-2 )
270 memset(sub[line[j]].table,-1,256*sizeof(union hash));
271 }
272 sub = sub[line[j]].table;
273 }
274 sub[line[j]].color = findcol((char *) line+j+1);
275 }
276 return( tab );
277 }
278
279 GImage *GImageReadXpm(char * filename) {
280 /* Import an *.xpm image, else cleanup and return NULL if error */
281 /* TODO: There is an XPM3 library that takes care of all cases. */
282 FILE *fp;
283 GImage *ret=NULL;
284 struct _GImage *base;
285 int width, height, cols, nchar;
286 unsigned char buf[80];
287 unsigned char *line, *lpt;
288 int y,j, lsiz;
289 union hash *tab, *sub;
290 unsigned char *pt, *end; unsigned long *ipt;
291 int (*getdata)(unsigned char *,int,FILE *) = NULL;
292
293 if ( (fp=fopen(filename,"r"))==NULL ) {
294 fprintf(stderr,"Can't open \"%s\"\n", filename);
295 return( NULL );
296 }
297
298 line=NULL; tab=NULL; nchar=0;
299 /* If file begins with XPM then read lines using getstring;() */
300 /* otherwise for XPM2 read lines using function gww_getline() */
301 if ( (fgets((char *)buf,sizeof(buf),fp))==NULL )
302 goto errorGImageReadXpm;
303 if ( strstr((char *) buf,"XPM2")!=NULL )
304 getdata = gww_getline;
305 else if ( strstr((char *)buf,"/*")!=NULL && strstr((char *)buf,"XPM")!=NULL && strstr((char *)buf,"*/")!=NULL )
306 getdata = getstring;
307
308 /* If no errors yet then go get width, height, colors, nchars */
309 if ( getdata==NULL ||
310 !getdata(buf,sizeof(buf),fp) ||
311 sscanf((char *)buf,"%d %d %d %d",&width,&height,&cols,&nchar)!=4 )
312 goto errorGImageReadXpm;
313
314 /* Prepare to fetch one graphic line at a time for conversion */
315 if ( (line=(unsigned char *)malloc((lsiz=nchar*width+20)*sizeof(unsigned char)))==NULL ) {
316 NoMoreMemMessage();
317 goto errorGImageReadXpmMem;
318 }
319
320 /* Fetch color table */
321 if ( (tab=parse_colors(fp,line,lsiz,cols,nchar,getdata))==NULL )
322 goto errorGImageReadXpmMem;
323
324 if ( cols<=256 ) {
325 Color clut[257];
326 clut[256] = COLOR_UNKNOWN;
327 fillupclut(clut,tab,0,nchar);
328 if ( (ret=GImageCreate(it_index,width,height))==NULL )
329 goto errorGImageReadXpmMem;
330 ret->u.image->clut->clut_len = cols;
331 memcpy(ret->u.image->clut->clut,clut,cols*sizeof(Color));
332 ret->u.image->trans = clut[256];
333 ret->u.image->clut->trans_index = clut[256];
334 } else {
335 if ( (ret=GImageCreate(it_true,width,height))==NULL )
336 goto errorGImageReadXpmMem;
337 ret->u.image->trans = TRANS; /* TRANS isn't a valid Color, but it fits in our 32 bit pixels */
338 }
339
340 /* Get image */
341 base = ret->u.image;
342 for ( y=0; y<height; ++y ) {
343 if ( !getdata(line,lsiz,fp))
344 goto errorGImageReadXpm;
345 pt = (uint8_t *) (base->data+y*base->bytes_per_line); ipt = NULL; end = pt+width;
346 if ( cols>256 )
347 ipt = (unsigned long *) pt;
348 for ( lpt=line; *line && pt<end; ) {
349 sub = tab;
350 for ( j=0; *lpt && j<nchar-1; ++j, ++lpt )
351 if ( sub!=NULL )
352 sub = sub[*lpt].table;
353 if ( sub!=NULL ) {
354 if ( cols<=256 )
355 *pt = sub[*lpt].color;
356 else
357 *ipt = sub[*lpt].color;
358 }
359 ++pt; ++ipt; ++lpt;
360 }
361 }
362 free(line);
363 freetab(tab,nchar);
364 fclose(fp);
365 return( ret );
366
367 errorGImageReadXpm:
368 fprintf(stderr,"Bad input file \"%s\"\n",filename );
369 errorGImageReadXpmMem:
370 GImageDestroy(ret);
371 free(line); freetab(tab,nchar);
372 fclose(fp);
373 return( NULL );
374 }