Debian Codegraph @

gutils/gimagereadrgb.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gimage.h"
31
32 struct sgiheader {
33 short magic; /* Magic (SGI identification) number */
34 char format; /* Storage format (RLE=1 or VERBATIM=0) */
35 char bpc; /* Bytes per pixel channel (1or2 bytes) */
36 unsigned short dim; /* Number of dimensions (1,2,3) */
37 unsigned short width; /* Width of image in pixels X-size */
38 unsigned short height; /* Height of image in pixels Y-size */
39 unsigned short chans; /* Number of channels (1,3,4) Z-size */
40 long pixmin; /* Minimum pixel value (darkest) */
41 long pixmax; /* Maximum pixel value (brightest) */
42 char dummy[4]; /* Ignored */
43 char imagename[80]; /* Image name (0..79chars + '\0') */
44 long colormap; /* Colormap ID (0,1,2,3) */
45 char pad[404]; /* Ignored (total=512bytes) */
46 };
47
48 #define SGI_MAGIC 474
49 #define VERBATIM 0
50 #define RLE 1
51
52 static int getlong(FILE *fp, long *value) {
53 /* Get Big-Endian long (32bit int) value. Return 0 if okay, -1 if error */
54 int ch1, ch2, ch3, ch4;
55
56 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 || \
57 (ch3=fgetc(fp))<0 || (ch4=fgetc(fp))<0 ) {
58 *value=0;
59 return( -1 );
60 }
61 *value=(long)( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
62 return( 0 );
63 }
64
65 static int getshort(FILE *fp) {
66 /* Get Big-Endian short 16bit value. Return value if okay, -1 if error */
67 int ch1, ch2;
68
69 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 )
70 return( -1 );
71
72 return( (ch1<<8) | ch2 );
73 }
74
75 static int getsgiheader(struct sgiheader *head,FILE *fp) {
76 /* Get Header info. Return 0 if read input file okay, -1 if read error */
77 if ( (head->magic=getshort(fp))<0 || head->magic!=SGI_MAGIC || \
78 (head->format=fgetc(fp))<0 || \
79 (head->bpc=fgetc(fp))<0 || \
80 (head->dim=getshort(fp))==(unsigned short)-1 || \
81 (head->width=getshort(fp))==(unsigned short)-1 || \
82 (head->height=getshort(fp))==(unsigned short)-1|| \
83 (head->chans=getshort(fp))==(unsigned short)-1 || \
84 getlong(fp,&head->pixmin) || \
85 getlong(fp,&head->pixmax) || \
86 fread(head->dummy,sizeof(head->dummy),1,fp)<1 || \
87 fread(head->imagename,sizeof(head->imagename),1,fp)<1 || \
88 getlong(fp,&head->colormap) || \
89 fread(head->pad,sizeof(head->pad),1,fp)<1 )
90 return( -1 );
91
92 /* Check if header information okay (for us to use here ) */
93 if ( (head->format!=VERBATIM && head->format!=RLE) || \
94 (head->bpc!=1 && head->bpc!=2) || \
95 head->dim<1 || head->dim>3 || \
96 head->pixmax>65535 || (head->pixmax>255 && head->bpc==1) || \
97 (head->chans!=1 && head->chans!=3 && head->chans!=4) || \
98 head->pixmax<0 || head->pixmin<0 || head->pixmin>=head->pixmax || \
99 head->colormap!=0 )
100 return( -1 );
101
102 return( 0 );
103 }
104
105 static long scalecolor(struct sgiheader *header,long value) {
106 return( (value-header->pixmin)*255L/(header->pixmax-header->pixmin) );
107 }
108
109 static int readlongtab(FILE *fp,unsigned long *tab,long tablen) {
110 /* RLE table info, exit=0 if okay, return -1 if error */
111 long i;
112
113 for ( i=0; i<tablen; ++i )
114 if ( getlong(fp,(long *)&tab[i]) )
115 return( -1 ); /* had a read error */
116
117 return( 0 ); /* read everything okay */
118 }
119
120 static int find_scanline(FILE *fp,struct sgiheader *header,int cur,
121 unsigned long *starttab,unsigned char **ptrtab) {
122 /* Find and expand a scanline. Return 0 if okay, else -ve if error */
123 int ch = 0,i,cnt;
124 long val;
125 unsigned char *pt;
126
127 for ( i=0; i<cur; ++i )
128 if ( starttab[i]==starttab[cur] ) {
129 ptrtab[cur] = ptrtab[i];
130 return( 0 );
131 }
132 if ( (pt=ptrtab[cur]=(unsigned char *) malloc(header->width))==NULL ) {
133 NoMoreMemMessage();
134 return( -1 );
135 }
136 if ( fseek(fp,starttab[cur],0)!= 0 ) return( -2 );
137 while ( header->bpc==1 ) {
138 if ( (ch=fgetc(fp))<0 ) return( -2 );
139 if ( (cnt=(ch&0x7f))==0 ) return( 0 );
140 if ( ch&0x80 ) {
141 while ( --cnt>=0 ) {
142 if ( (ch=fgetc(fp))<0 ) return( -2 );
143 *pt++ = scalecolor(header,ch);
144 }
145 } else {
146 if ( (ch=fgetc(fp))<0 ) return( -2 );
147 ch = scalecolor(header,ch);
148 while ( --cnt>=0 )
149 *pt++ = ch;
150 }
151 }
152 while ( header->bpc!=1 ) {
153 if ( getlong(fp,&val) ) return( -2 );
154 if ( (cnt=(ch&0x7f))==0 ) return( 0 );
155 if ( ch&0x80 ) {
156 while ( --cnt>=0 ) {
157 if ( getlong(fp,&val) ) return( -2 );
158 *pt++ = scalecolor(header,val);
159 }
160 } else {
161 if ( getlong(fp,&val) ) return( -2 );
162 val = scalecolor(header,val);
163 while ( --cnt>=0 )
164 *pt++ = val;
165 }
166 }
167 return( -2 );
168 }
169
170 static void freeptrtab(unsigned char **ptrtab,long tot) {
171 long i,j;
172
173 if ( ptrtab!=NULL )
174 for ( i=0; i<tot; ++i )
175 if ( ptrtab[i]!=NULL ) {
176 for ( j=i+1; j<tot; ++j )
177 if ( ptrtab[j]==ptrtab[i] )
178 ptrtab[j] = NULL;
179 free(ptrtab[i]);
180 }
181 }
182
183 GImage *GImageReadRgb(char *filename) {
184 FILE *fp; /* source file */
185 struct sgiheader header;
186 int i,j,k;
187 unsigned char *pt, *end;
188 unsigned char *r=NULL,*g=NULL,*b=NULL,*a=NULL; /* Colors */
189 unsigned long *starttab=NULL /*, *lengthtab=NULL */;
190 unsigned char **ptrtab=NULL;
191 long tablen=0;
192 unsigned long *ipt, *iend;
193 GImage *ret = NULL;
194 struct _GImage *base;
195
196 if ( (fp=fopen(filename,"rb"))==NULL ) {
197 fprintf(stderr,"Can't open \"%s\"\n", filename);
198 return( NULL );
199 }
200
201 /* Check, and Get, Header information */
202 if ( getsgiheader(&header,fp) )
203 goto errorGImageReadRgbFile;
204
205 /* Create memory to hold image, exit with NULL if not enough memory */
206 if ( (ret=GImageCreate(header.dim==3?it_true:it_index,header.width,header.height))==NULL ) {
207 fclose(fp);
208 return( NULL );
209 }
210 base = ret->u.image;
211
212 if ( header.format==RLE ) {
213 /* Working with RLE image data*/
214
215 /* First, get offset table info */
216 tablen = header.height*header.chans;
217 if ( (starttab=(unsigned long *)calloc(1,tablen*sizeof(unsigned long)))==NULL || \
218 /*(lengthtab=(unsigned long *)calloc(1,tablen*sizeof(long)))==NULL || \ */
219 (ptrtab=(unsigned char **)calloc(1,tablen*sizeof(unsigned char *)))==NULL ) {
220 NoMoreMemMessage();
221 goto errorGImageReadRgbMem;
222 }
223 if ( readlongtab(fp,starttab,tablen) )
224 /* || readlongtab(fp,lengthtab,tablen) */
225 goto errorGImageReadRgbFile;
226
227 /* Next, get image data */
228 for ( i=0; i<tablen; ++i )
229 if ( (k=find_scanline(fp,&header,i,starttab,ptrtab)) ) {
230 if ( k==-1 ) goto errorGImageReadRgbMem; else goto errorGImageReadRgbFile;
231 }
232
233 /* Then, build image */
234 if ( header.chans==1 ) {
235 for ( i=0; i<header.height; ++i )
236 memcpy(base->data + (header.height-1-i)*base->bytes_per_line,ptrtab[i],header.width);
237 } else {
238 unsigned long *ipt;
239 for ( i=0; i<header.height; ++i ) {
240 ipt = (unsigned long *) (base->data + (header.height-1-i)*base->bytes_per_line);
241 for ( j=0; j<header.width; ++j )
242 *ipt++ = COLOR_CREATE(ptrtab[i][j],
243 ptrtab[i+header.height][j], ptrtab[i+2*header.height][j]);
244 }
245 }
246 freeptrtab(ptrtab,tablen);
247 free(ptrtab); free(starttab); /*free(lengthtab);*/
248 } else {
249 /* working with Verbatim image data*/
250 if ( header.chans==1 && header.bpc==1 ) {
251 for ( i=0; i<header.height; ++i ) {
252 fread(base->data + (header.height-1-i)*base->bytes_per_line,header.width,1,fp);
253 if ( header.pixmax!=255 ) {
254 pt = (unsigned char *) (base->data+(header.height-1-i)*base->bytes_per_line);
255 for ( end=pt+header.width; pt<end; ++pt )
256 *pt = (*pt*255)/header.pixmax;
257 }
258 }
259 } else if ( header.chans==1 ) {
260 for ( i=0; i<header.height; ++i ) {
261 pt = (unsigned char *) (base->data + (header.height-1-i)*base->bytes_per_line);
262 for ( end=pt+header.width; pt<end; ) {
263 if ( (k=getshort(fp))<0 ) goto errorGImageReadRgbFile;
264 *pt++ = (k*255L)/header.pixmax;
265 }
266 }
267 } else {
268 /* import RGB=3 or RGBA=4 Verbatim images */
269 unsigned char *rpt, *gpt, *bpt;
270 if ( (r=(unsigned char *) malloc(header.width*sizeof(unsigned char)))==NULL || \
271 (g=(unsigned char *) malloc(header.width*sizeof(unsigned char)))==NULL || \
272 (b=(unsigned char *) malloc(header.width*sizeof(unsigned char)))==NULL || \
273 (header.chans==4 && \
274 (a=(unsigned char *) malloc(header.width*sizeof(unsigned char)))==NULL) ) {
275 NoMoreMemMessage();
276 goto errorGImageReadRgbMem;
277 }
278 if ( header.bpc==1 ) {
279 for ( i=0; i<header.height; ++i ) {
280 if ( (fread(r,header.width,1,fp))<1 || \
281 (fread(g,header.width,1,fp))<1 || \
282 (fread(b,header.width,1,fp))<1 || \
283 (header.chans==4 && (fread(a,header.width,1,fp))<1) )
284 goto errorGImageReadRgbFile;
285 ipt = (unsigned long *) (base->data + (header.height-1-i)*base->bytes_per_line);
286 rpt = r; gpt = g; bpt = b;
287 for ( iend=ipt+header.width; ipt<iend; )
288 *ipt++ = COLOR_CREATE(*rpt++*255L/header.pixmax,
289 *gpt++*255L/header.pixmax,*bpt++*255L/header.pixmax);
290 }
291 } else {
292 for ( i=0; i<header.height; ++i ) {
293 for ( j=0; j<header.width; ++j ) {
294 if ( (k=getshort(fp))<0 ) goto errorGImageReadRgbFile;
295 r[j] = k*255L/header.pixmax;
296 }
297 for ( j=0; j<header.width; ++j ) {
298 if ( (k=getshort(fp))<0 ) goto errorGImageReadRgbFile;
299 g[j] = k*255L/header.pixmax;
300 }
301 for ( j=0; j<header.width; ++j ) {
302 if ( (k=getshort(fp))<0 ) goto errorGImageReadRgbFile;
303 b[j] = k*255L/header.pixmax;
304 }
305 if ( header.chans==4 ) {
306 fread(a,header.width,1,fp);
307 fread(a,header.width,1,fp);
308 }
309 ipt = (unsigned long *) (base->data + (header.height-1-i)*base->bytes_per_line);
310 rpt = r; gpt = g; bpt = b;
311 for ( iend=ipt+header.width; ipt<iend; )
312 *ipt++ = COLOR_CREATE(*rpt++,*gpt++,*bpt++);
313 }
314 }
315 free(r); free(g); free(b); free(a);
316 }
317 }
318 fclose(fp);
319 return( ret );
320
321 errorGImageReadRgbFile:
322 fprintf(stderr,"Bad input file \"%s\"\n",filename );
323 errorGImageReadRgbMem:
324 freeptrtab(ptrtab,tablen);
325 free(ptrtab); free(starttab); /*free(lengthtab);*/
326 free(r); free(g); free(b); free(a);
327 GImageDestroy(ret);
328 fclose(fp);
329 return( NULL );
330 }