Debian Codegraph @

gutils/gimagereadxbm.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /* 2013feb15, fileread and mem error checks, plus test for short, 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
33 static int ConvertXbmByte(int pixels) {
34 int i,val=0;
35
36 for ( i=0; i<8; ++i )
37 if ( pixels&(1<<i) )
38 val |= (0x80>>i);
39 return( val^0xff ); /* I default black the other way */
40 }
41
42 GImage *GImageReadXbm(char * filename) {
43 /* Import an *.xbm image, else return NULL if error */
44 FILE *file;
45 int width, height;
46 GImage *gi=NULL;
47 struct _GImage *base;
48 int ch,i,j,l = 0;
49 long pixels;
50 uint8_t *scanline;
51
52 if ( (file=fopen(filename,"r"))==NULL ) {
53 fprintf(stderr,"Can't open \"%s\"\n", filename);
54 return( NULL );
55 }
56
57 /* Get width and height */
58 if ( fscanf(file,"#define %*s %d\n",&width)!=1 || \
59 fscanf(file,"#define %*s %d\n",&height)!=1 )
60 goto errorGImageReadXbm;
61
62 /* Check for, and skip past hotspot */
63 if ( (ch=getc(file))<0 )
64 goto errorGImageReadXbm;
65 if ( ch=='#' ) {
66 if ( fscanf(file,"define %*s %*d\n")<0 || /* x hotspot */ \
67 fscanf(file,"#define %*s %*d\n")<0 ) /* y hotspot */
68 goto errorGImageReadXbm;
69 } else
70 if ( ungetc(ch,file)<0 )
71 goto errorGImageReadXbm;
72
73 /* Data starts after finding "static unsigned " or "static " */
74 if ( fscanf(file,"static ")<0 || (ch=getc(file))<0 )
75 goto errorGImageReadXbm;
76 if ( ch=='u' ) {
77 if ( fscanf(file,"nsigned ")<0 || (ch=getc(file))<0 )
78 goto errorGImageReadXbm;
79 /* TODO: Do we want to track signed/unsigned data? */
80 }
81
82 /* Data may be in char,short or long formats */
83 if ( ch=='c' ) {
84 if ( fscanf(file,"har %*s = {")<0 )
85 goto errorGImageReadXbm;
86 l=8;
87 } else if ( ch=='s' ) {
88 if ( fscanf(file,"hort %*s = {")<0 )
89 goto errorGImageReadXbm;
90 l=16;
91 } else if ( ch=='l' ) {
92 if ( fscanf(file,"ong %*s = {")<0 )
93 goto errorGImageReadXbm;
94 l=32;
95 }
96
97 /* Create memory to hold image, exit with NULL if not enough memory */
98 if ( (gi=GImageCreate(it_mono,width,height))==NULL )
99 goto errorGImageReadXbmMem;
100
101 /* Convert *.xbm graphic into one that FF can use */
102 base = gi->u.image;
103 for ( i=0; i<height; ++i ) {
104 scanline = base->data + i*base->bytes_per_line;
105 for ( j=0; j<base->bytes_per_line; ++j ) {
106 if ( fscanf(file," 0x%x",(unsigned *) &pixels)!=1 )
107 goto errorGImageReadXbm;
108 *scanline++ = ConvertXbmByte(pixels);
109 if ( l>8 && j+1<base->bytes_per_line ) {
110 *scanline++ = ConvertXbmByte(pixels>>8);
111 ++j;
112 }
113 if ( l>16 && j+1<base->bytes_per_line ) {
114 *scanline++ = ConvertXbmByte(pixels>>16);
115 ++j;
116 if ( j+1<base->bytes_per_line ) {
117 *scanline++ = ConvertXbmByte(pixels>>24);
118 ++j;
119 }
120 }
121 fscanf(file,",");
122 }
123 }
124 fclose(file);
125 return( gi );
126
127 errorGImageReadXbm:
128 fprintf(stderr,"Bad input file \"%s\"\n",filename );
129 errorGImageReadXbmMem:
130 GImageDestroy(gi);
131 fclose(file);
132 return( NULL );
133 }