Debian Codegraph @

gutils/gimagereadras.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /* 2013jan30..feb5, 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
33 typedef struct _SunRaster {
34 long MagicNumber; /* Magic (identification) number */
35 long Width; /* Width of image in pixels */
36 long Height; /* Height of image in pixels */
37 long Depth; /* Number of bits per pixel */
38 long Length; /* Size of image data in bytes */
39 long Type; /* Type of raster file */
40 long ColorMapType; /* Type of color map */
41 long ColorMapLength; /* Size of the color map in bytes */
42 } SUNRASTER;
43
44 #define SUN_RAS_MAGIC 0x59a66a95
45 #define LITTLE_ENDIAN_MAGIC 0x956aa659 /* if we read this value, must byte swap */
46
47 enum types { TypeOld, TypeStandard, TypeByteEncoded, TypeRGB, TypeTIFF, TypeIFF };
48 enum cluts { ClutNone, ClutRGB, ClutRaw };
49
50 static int getlong(FILE *fp, long *value) {
51 /* Get Big-Endian long (32bit int) value. Return 0 if okay, -1 if error */
52 int ch1, ch2, ch3, ch4;
53
54 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 || \
55 (ch3=fgetc(fp))<0 || (ch4=fgetc(fp))<0 ) {
56 *value=0;
57 return( -1 );
58 }
59 *value=(long)( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
60 return( 0 );
61 }
62
63 static int getrasheader(SUNRASTER *head, FILE *fp) {
64 /* Get Header info. Return 0 if read input file okay, -1 if read error */
65 if ( getlong(fp,&head->MagicNumber) || \
66 (head->MagicNumber!=SUN_RAS_MAGIC && head->MagicNumber!=LITTLE_ENDIAN_MAGIC) || \
67 getlong(fp,&head->Width) || \
68 getlong(fp,&head->Height) || \
69 getlong(fp,&head->Depth) || \
70 getlong(fp,&head->Length) || \
71 getlong(fp,&head->Type) || \
72 getlong(fp,&head->ColorMapType) || \
73 getlong(fp,&head->ColorMapLength) )
74 return( -1 );
75
76 /* Check if header information okay (only try Big-Endian for now). */
77 if ( head->MagicNumber!=SUN_RAS_MAGIC || \
78 head->Type<0 || head->Type>TypeRGB || \
79 (head->ColorMapType!=ClutNone && head->ColorMapType!=ClutRGB) || \
80 (head->Depth!=1 && head->Depth!=8 && head->Depth!=24 && head->Depth!=32) || \
81 (head->Depth>=24 && head->ColorMapType!=ClutNone) || \
82 head->ColorMapLength>3*256 )
83 return( -1 );
84
85 return( 0 );
86 }
87
88 static GImage *ReadRasBitmap(GImage *ret,int width, int height, FILE *fp ) {
89 struct _GImage *base = ret->u.image;
90 int i,j,len;
91 unsigned char *pt, *buf;
92
93 len = ((width+15)/16)*2; /* pad out to 16 bits */
94 if ( (buf=(unsigned char *) malloc(len*sizeof(unsigned char)))==NULL ) {
95 NoMoreMemMessage();
96 GImageDestroy(ret);
97 return( NULL );
98 }
99
100 for ( i=0; i<height; ++i ) {
101 if ( fread(buf,len,1,fp)<1 ) {
102 free(buf);
103 GImageDestroy(ret);
104 return( NULL );
105 }
106 pt = (unsigned char *) (base->data + i*base->bytes_per_line);
107 for ( j=0; j<(width+7)>>3; ++j )
108 pt[j]=256-buf[j];
109 }
110 free(buf);
111 return( ret );
112 }
113
114 static GImage *ReadRas8Bit(GImage *ret,int width, int height, FILE *fp ) {
115 struct _GImage *base = ret->u.image;
116 int i;
117
118 for ( i=0; i<height; ++i ) {
119 if ( fread((base->data + i*base->bytes_per_line),width,1,fp)<1 ) {
120 goto errorReadRas8Bit;
121 }
122 if ( width&1 ) /* pad out to 16 bits */
123 if ( fgetc(fp)<0 )
124 goto errorReadRas8Bit;
125 }
126 return( ret );
127
128 errorReadRas8Bit:
129 GImageDestroy(ret);
130 return( NULL );
131 }
132
133 static GImage *ReadRas24Bit(GImage *ret,int width, int height, FILE *fp ) {
134 struct _GImage *base = ret->u.image;
135 int ch1,ch2,ch3=0;
136 int i;
137 long *ipt,*end;
138
139 for ( i=0; i<height; ++i ) {
140 for ( ipt = (long *) (base->data + i*base->bytes_per_line), end = ipt+width; ipt<end; ) {
141 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 || (ch3=fgetc(fp))<0 )
142 goto errorReadRas24Bit;
143 *ipt++ = COLOR_CREATE(ch3,ch2,ch1);
144 }
145 if ( width&1 ) /* pad out to 16 bits */
146 if ( fgetc(fp)<0 )
147 goto errorReadRas24Bit;
148 }
149 return( ret );
150
151 errorReadRas24Bit:
152 GImageDestroy(ret);
153 return( NULL );
154 }
155
156 static GImage *ReadRas32Bit(GImage *ret,int width, int height, FILE *fp ) {
157 struct _GImage *base = ret->u.image;
158 int ch1,ch2,ch3=0;
159 int i;
160 long *ipt, *end;
161
162 for ( i=0; i<height; ++i )
163 for ( ipt = (long *) (base->data + i*base->bytes_per_line), end = ipt+width; ipt<end; ) {
164 fgetc(fp); /* pad byte */
165 ch1 = fgetc(fp); ch2 = fgetc(fp); ch3 = fgetc(fp);
166 *ipt++ = COLOR_CREATE(ch3,ch2,ch1);
167 }
168 if ( ch3==EOF ) {
169 GImageDestroy(ret);
170 ret = NULL;
171 }
172 return ret;
173 }
174
175 static GImage *ReadRas24RBit(GImage *ret,int width, int height, FILE *fp ) {
176 struct _GImage *base = ret->u.image;
177 int ch1,ch2,ch3=0;
178 int i;
179 long *ipt,*end;
180
181 for ( i=0; i<height; ++i ) {
182 for ( ipt = (long *) (base->data + i*base->bytes_per_line), end = ipt+width; ipt<end; ) {
183 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 || (ch3=fgetc(fp))<0 )
184 goto errorReadRas24RBit;
185 *ipt++ = COLOR_CREATE(ch1,ch2,ch3);
186 }
187 if ( width&1 ) /* pad out to 16 bits */
188 if ( fgetc(fp)<0 )
189 goto errorReadRas24RBit;
190 }
191 return( ret );
192
193 errorReadRas24RBit:
194 GImageDestroy(ret);
195 return( NULL );
196 }
197
198 static GImage *ReadRas32RBit(GImage *ret,int width, int height, FILE *fp ) {
199 struct _GImage *base = ret->u.image;
200 int ch1,ch2,ch3=0;
201 int i;
202 long *ipt, *end;
203
204 for ( i=0; i<height; ++i )
205 for ( ipt = (long *) (base->data + i*base->bytes_per_line), end = ipt+width; ipt<end; ) {
206 fgetc(fp); /* pad byte */
207 ch1 = fgetc(fp); ch2 = fgetc(fp); ch3 = fgetc(fp);
208 *ipt++ = COLOR_CREATE(ch1,ch2,ch3);
209 }
210 if ( ch3==EOF ) {
211 GImageDestroy(ret);
212 ret = NULL;
213 }
214 return ret;
215 }
216
217 static GImage *ReadRle8Bit(GImage *ret,int width, int height, FILE *fp ) {
218 /* TODO: Make this an input filter that goes in front of other routines */
219 /* above so that in can be re-used by the different converters above. */
220 struct _GImage *base = ret->u.image;
221 int x,y,cnt,val = 0;
222 unsigned char *pt = NULL;
223
224 x=0; y=0; cnt=0;
225 while ( 1 ) {
226 while ( cnt && x ) {
227 --cnt; if ( --x || (width&1)-1 ) * pt++ = val;
228 }
229 if ( x==0 ) {
230 pt = (unsigned char *) (base->data + y*base->bytes_per_line);
231 if ( ++y>height )
232 return( ret );
233 x=((width+1)>>1)<<1;
234 }
235 if ( cnt==0 ) {
236 if ( (val=fgetc(fp))<0 ) goto errorReadRle8Bit;
237 cnt++;
238 if ( val==0x80 ) {
239 if ( (cnt=fgetc(fp))<0 ) goto errorReadRle8Bit;
240 if ( cnt++!=0 )
241 /* prepare to go insert 'val', 'cnt' times */
242 if ( (val=fgetc(fp))<0 ) goto errorReadRle8Bit;
243 }
244 }
245 }
246
247 errorReadRle8Bit:
248 GImageDestroy(ret);
249 return( NULL );
250 }
251
252 GImage *GImageReadRas(char *filename) {
253 /* Import a *.ras image (or *.im{1,8,24,32}), else return NULL if error */
254 FILE *fp; /* source file */
255 struct _SunRaster header;
256 GImage *ret = NULL;
257 struct _GImage *base;
258
259 if ( (fp=fopen(filename,"rb"))==NULL ) {
260 fprintf(stderr,"Can't open \"%s\"\n", filename);
261 return( NULL );
262 }
263
264 if ( getrasheader(&header,fp) )
265 goto errorGImageReadRas;
266
267 /* Create memory to hold image, exit with NULL if not enough memory */
268 if ( (header.Depth==1 && \
269 (ret=GImageCreate(it_bitmap,header.Width,header.Height))==NULL) || \
270 (header.Depth!=1 && \
271 (ret=GImageCreate(header.Depth==24?it_true:it_index,header.Width,header.Height))==NULL) ) {
272 fclose(fp);
273 return( NULL );
274 }
275
276 /* Convert *.ras ColorMap to one that FF can use */
277 base = ret->u.image;
278 if ( header.ColorMapLength!=0 && base->clut!=NULL ) {
279 unsigned char clutb[3*256]; int i,n;
280 if ( fread(clutb,header.ColorMapLength,1,fp)<1 )
281 goto errorGImageReadRas;
282 // if ( header.ColorMapType==ClutRaw ) {
283 // n = header.ColorMapLength;
284 // base->clut->clut_len = n;
285 // for ( i=0; i<n; ++i )
286 // base->clut->clut[i] = clutb[i];
287 // } else {
288 n = header.ColorMapLength/3;
289 base->clut->clut_len = n;
290 for ( i=0; i<n; ++i )
291 base->clut->clut[i] = COLOR_CREATE(clutb[i],clutb[i+n],clutb[i+2*n]);
292 // }
293 }
294
295 if ( header.Type==TypeOld || header.Type==TypeStandard ) { /* Synonymous */
296 if ( header.Depth==1 )
297 ret = ReadRasBitmap(ret,header.Width,header.Height,fp);
298 else if ( header.Depth==8 )
299 ret = ReadRas8Bit(ret,header.Width,header.Height,fp);
300 else if ( header.Depth==24 )
301 ret = ReadRas24Bit(ret,header.Width,header.Height,fp);
302 else
303 ret = ReadRas32Bit(ret,header.Width,header.Height,fp);
304 } else if ( header.Type==TypeRGB ) {
305 /* I think this type is the same as standard except rgb not bgr */
306 if ( header.Depth==1 )
307 ret = ReadRasBitmap(ret,header.Width,header.Height,fp);
308 else if ( header.Depth==8 )
309 ret = ReadRas8Bit(ret,header.Width,header.Height,fp);
310 else if ( header.Depth==24 )
311 ret = ReadRas24RBit(ret,header.Width,header.Height,fp);
312 else
313 ret = ReadRas32RBit(ret,header.Width,header.Height,fp);
314 } else if ( header.Type==TypeByteEncoded ) {
315 if ( header.Depth==8 )
316 ret = ReadRle8Bit(ret,header.Width,header.Height,fp);
317 else {
318 /* Don't bother with most rle formats */
319 /* TODO: if someone wants to do this - accept more formats */
320 fprintf(stderr,"Unsupported input file type\n");
321 goto errorGImageReadRas;
322 }
323 } else {
324 /* Don't bother with other formats */
325 /* TODO: if someone wants to do this - accept more formats */
326 fprintf(stderr,"Unsupported input file type\n");
327 goto errorGImageReadRas;
328 }
329 if ( ret!=NULL ) {
330 /* All okay if reached here, return converted image */
331 fclose(fp);
332 return( ret );
333 }
334
335 errorGImageReadRas:
336 fprintf(stderr,"Bad input file \"%s\"\n",filename );
337 GImageDestroy(ret);
338 fclose(fp);
339 return( NULL );
340 }