Debian Codegraph @

gutils/gimagereadbmp.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 #include "gimagebmpP.h"
32 GImage *_GImage_Create(enum image_type type, int32_t width, int32_t height);
33
34 static int getshort(FILE *fp) {
35 /* Get Little-Endian short 16bit value. Return value if okay, -1 if error */
36 int ch1, ch2;
37
38 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 )
39 return( -1 );
40
41 return( (ch2<<8) | ch1 );
42 }
43
44 static long getlong(FILE *fp, long *value) {
45 /* Get Little-Endian long 32bit int value. Return 0 if okay, -1 if error. */
46 int ch1, ch2, ch3, ch4;
47
48 if ( (ch1=fgetc(fp))<0 || (ch2=fgetc(fp))<0 || \
49 (ch3=fgetc(fp))<0 || (ch4=fgetc(fp))<0 ) {
50 *value=0;
51 return( -1 );
52 }
53 *value=(long)( (ch4<<24)|(ch3<<16)|(ch2<<8)|ch1 );
54 return( 0 );
55 }
56
57 static int bitshift(unsigned long mask) {
58 int off=0, len=0, bit;
59
60 if ( mask==0 )
61 return( 0 );
62 for ( off=0; !(mask&1); mask>>=1, ++off );
63 for ( len=0, bit=1; (mask&bit) && len<32; ++len, bit<<=1 );
64 return( off+(8-len) );
65 }
66
67 static int fillbmpheader(FILE *fp,struct bmpheader *head) {
68 /* Get BMPheader info. Return 0 if read the header okay, -1 if read error */
69 int i;
70 long temp;
71
72 if ( fgetc(fp)!='B' || getc(fp)!='M' || /* Bad format */ \
73 getlong(fp,&head->size) || \
74 (head->mbz1=getshort(fp))<0 || \
75 (head->mbz2=getshort(fp))<0 || \
76 getlong(fp,&head->offset) || \
77 getlong(fp,&head->headersize) )
78 return( -1 );
79
80 if ( head->headersize==12 ) { /* Windows 2.0 format, also OS/2 */
81 if ( (head->width=getshort(fp))<0 || \
82 (head->height=getshort(fp))<0 || \
83 (head->planes=getshort(fp))<0 || \
84 (head->bitsperpixel=getshort(fp))<0 )
85 return( -1 );
86 //head->colorsused=0;
87 //head->compression=0;
88 } else {
89 if ( getlong(fp,&head->width) || \
90 getlong(fp,&head->height) || \
91 (head->planes=getshort(fp))<0 || \
92 (head->bitsperpixel=getshort(fp))<0 || \
93 getlong(fp,&head->compression) || \
94 getlong(fp,&head->imagesize) || \
95 getlong(fp,&head->ignore1) || \
96 getlong(fp,&head->ignore2) || \
97 getlong(fp,&head->colorsused) || \
98 getlong(fp,&head->colorsimportant) )
99 return( -1 );
100 }
101 if ( head->height<0 )
102 head->height = -head->height;
103 else
104 head->invert = true;
105
106 if ( head->bitsperpixel!=1 && head->bitsperpixel!=4 && head->bitsperpixel!=8 &&
107 head->bitsperpixel!=16 && head->bitsperpixel!=24 && head->bitsperpixel!=32 )
108 return( -1 );
109
110 if ( head->compression==3 && ( head->bitsperpixel==16 || head->bitsperpixel==32 ) )
111 /* Good */;
112 else if ( head->compression==0 && ( head->bitsperpixel<=8 || head->bitsperpixel==24 || head->bitsperpixel==32 ) )
113 /* Good */;
114 else if ( head->compression==1 && head->bitsperpixel==8 )
115 /* Good */;
116 else if ( head->compression==2 && head->bitsperpixel==4 )
117 /* Good */;
118 else
119 return( -1 );
120
121 if ( head->colorsused==0 )
122 head->colorsused = 1<<head->bitsperpixel;
123 if ( head->bitsperpixel>=16 )
124 head->colorsused = 0;
125 if ( head->colorsused>(1<<head->bitsperpixel) )
126 return( -1 );
127
128 for ( i=0; i<head->colorsused; ++i ) {
129 int b,g,r;
130 if ( (b=fgetc(fp))<0 || (g=fgetc(fp))<0 || (r=fgetc(fp))<0 )
131 return( -1 );
132 head->clut[i]=COLOR_CREATE(r,g,b);
133 if ( head->headersize!=12 && fgetc(fp)<0 )
134 return( -1 );
135 }
136 if ( head->compression==3 || head->headersize==108 ) {
137 if ( getlong(fp,&head->red_mask) || \
138 getlong(fp,&head->green_mask) || \
139 getlong(fp,&head->blue_mask) )
140 return( -1 );
141 head->red_shift = bitshift(head->red_mask);
142 head->green_shift = bitshift(head->green_mask);
143 head->blue_shift = bitshift(head->blue_mask);
144 }
145
146 if ( head->headersize==108 && (
147 getlong(fp,&temp) || /* alpha_mask */ \
148 getlong(fp,&temp) || /* color space type */ \
149 getlong(fp,&temp) || /* redx */ \
150 getlong(fp,&temp) || /* redy */ \
151 getlong(fp,&temp) || /* redz */ \
152 getlong(fp,&temp) || /* greenx */ \
153 getlong(fp,&temp) || /* greeny */ \
154 getlong(fp,&temp) || /* greenz */ \
155 getlong(fp,&temp) || /* bluex */ \
156 getlong(fp,&temp) || /* bluey */ \
157 getlong(fp,&temp) || /* bluez */ \
158 getlong(fp,&temp) || /* gammared */ \
159 getlong(fp,&temp) || /* gammagreen */ \
160 getlong(fp,&temp) ) /* gammablue */ )
161 return( -1 );
162
163 return( 0 );
164 }
165
166 static int readpixels(FILE *file,struct bmpheader *head) {
167 int i,ii,j,ll,excess;
168
169 fseek(file,head->offset,0);
170
171 ll = head->width;
172 if ( head->bitsperpixel==8 && head->compression==0 ) {
173 excess = ((ll+3)/4)*4 -ll;
174 for ( i=0; i<head->height; ++i ) {
175 fread(head->byte_pixels+i*ll,1,ll,file);
176 for ( j=0; j<excess; ++j )
177 (void) getc(file);
178 }
179 } else if ( head->bitsperpixel==8 ) {
180 /* 8 bit RLE */
181 int ii = 0;
182 while ( ii<head->height*head->width ) {
183 int cnt = getc(file);
184 if (cnt < 0 || ii + cnt > head->height * head->width) {
185 return 0;
186 }
187 if ( cnt!=0 ) {
188 int ch = getc(file);
189 while ( --cnt>=0 )
190 head->byte_pixels[ii++] = ch;
191 } else {
192 cnt = getc(file);
193 if ( cnt>= 3 ) {
194 if (ii + cnt > head->height * head->width) {
195 return 0;
196 }
197 int odd = cnt&1;
198 while ( --cnt>=0 )
199 head->byte_pixels[ii++] = getc(file);
200 if ( odd )
201 getc(file);
202 } else if ( cnt==0 ) { /* end of scan line */
203 ii = ((ii+head->width-1)/head->width) * head->width;
204 } else if ( cnt==1 ) {
205 break;
206 } else if ( cnt==2 ) {
207 int x=getc(file);
208 int y = getc(file);
209 y += ii/head->width;
210 x += ii%head->width;
211 ii = y*head->width + x;
212 }
213 }
214 }
215 } else if ( head->bitsperpixel==4 && head->compression==0 ) {
216 excess = (ll+1)/2;
217 excess = ((excess+3)/4)*4 -excess;
218 for ( i=0; i<head->height; ++i ) {
219 ii = i*ll;
220 for ( j=0; j<((head->width+7)/8)*8; j+=8 ) {
221 int b1 = getc(file);
222 int b2 = getc(file);
223 int b3 = getc(file);
224 int b4 = getc(file);
225 head->byte_pixels[ii+j] = (b1>>4);
226 if ( j+1<ll ) head->byte_pixels[ii+j+1] = (b1&0xf);
227 if ( j+2<ll ) head->byte_pixels[ii+j+2] = (b2>>4);
228 if ( j+3<ll ) head->byte_pixels[ii+j+3] = (b2&0xf);
229 if ( j+4<ll ) head->byte_pixels[ii+j+4] = (b3>>4);
230 if ( j+5<ll ) head->byte_pixels[ii+j+5] = (b3&0xf);
231 if ( j+6<ll ) head->byte_pixels[ii+j+6] = (b4>>4);
232 if ( j+7<ll ) head->byte_pixels[ii+j+7] = (b4&0xf);
233 }
234 for ( j=0; j<excess; ++j )
235 (void) getc(file);
236 }
237 } else if ( head->bitsperpixel==4 ) {
238 /* 4 bit RLE */
239 int ii = 0;
240 while ( ii<head->height*head->width ) {
241 int cnt = getc(file);
242 if ( cnt!=0 ) {
243 int ch = getc(file);
244 while ( (cnt-=2)>=-1 ) {
245 head->byte_pixels[ii++] = ch>>4;
246 head->byte_pixels[ii++] = ch&0xf;
247 }
248 if ( cnt==-1 ) --ii;
249 } else {
250 cnt = getc(file);
251 if ( cnt>= 3 ) {
252 int odd = cnt&2;
253 while ( (cnt-=2)>=-1 ) {
254 int ch = getc(file);
255 head->byte_pixels[ii++] = ch>>4;
256 head->byte_pixels[ii++] = ch&0xf;
257 }
258 if ( cnt==-1 ) --ii;
259 if ( odd )
260 getc(file);
261 } else if ( cnt==0 ) { /* end of scan line */
262 ii = ((ii+head->width-1)/head->width) * head->width;
263 } else if ( cnt==1 ) {
264 break;
265 } else if ( cnt==2 ) {
266 int x=getc(file);
267 int y = getc(file);
268 y += ii/head->width;
269 x += ii%head->width;
270 ii = y*head->width + x;
271 }
272 }
273 }
274 } else if ( head->bitsperpixel==1 ) {
275 excess = (ll+7)/8;
276 excess = ((excess+3)/4)*4 -excess;
277 for ( i=0; i<head->height; ++i ) {
278 ii = i*((ll+7)/8);
279 for ( j=0; j<((head->width+7)/8); ++j ) {
280 head->byte_pixels[ii+j] = getc(file);
281 }
282 for ( j=0; j<excess; ++j )
283 (void) getc(file);
284 }
285 } else if ( head->bitsperpixel==24 ) {
286 excess = ((3*head->width+3)/4)*4 - 3*head->width;
287 for ( i=0; i<head->height; ++i ) {
288 ii = i*head->width;
289 for ( j=0; j<head->width; ++j ) {
290 int b = getc(file);
291 int g = getc(file);
292 int r = getc(file);
293 head->int32_pixels[ii+j] = COLOR_CREATE(r,g,b);
294 }
295 for ( j=0; j<excess; ++j )
296 (void) getc(file); /* ignore padding */
297 }
298 } else if ( head->bitsperpixel==32 && head->compression==0 ) {
299 for ( i=0; i<head->height; ++i ) {
300 ii = i*head->width;
301 for ( j=0; j<head->width; ++j ) {
302 int b,g,r;
303 b = getc(file);
304 g = getc(file);
305 r = getc(file);
306 (void) getc(file); /* Ignore the alpha channel */
307 head->int32_pixels[ii+j] = COLOR_CREATE(r,g,b);
308 }
309 }
310 } else if ( head->bitsperpixel==16 ) {
311 for ( i=0; i<head->height; ++i ) {
312 ii = i*head->width;
313 for ( j=0; j<head->width; ++j ) {
314 int pix = getshort(file);
315 head->int32_pixels[ii+j] = COLOR_CREATE((pix&head->red_mask)>>head->red_shift,
316 (pix&head->green_mask)>>head->green_shift,
317 (pix&head->blue_mask)>>head->blue_shift);
318 }
319 }
320 if ( head->width&1 )
321 getshort(file);
322 } else if ( head->bitsperpixel==32 ) {
323 for ( i=0; i<head->height; ++i ) {
324 ii = i*head->width;
325 for ( j=0; j<head->width; ++j ) {
326 long pix;
327 if ( getlong(file,&pix) )
328 return( 1 );
329 head->int32_pixels[ii+j] = COLOR_CREATE((pix&head->red_mask)>>head->red_shift,
330 (pix&head->green_mask)>>head->green_shift,
331 (pix&head->blue_mask)>>head->blue_shift);
332 }
333 }
334 }
335
336 if ( feof(file )) { /* Did we get an incomplete file? */
337 return( 0 );
338 }
339
340 return( 1 );
341 }
342
343 GImage *GImageRead_Bmp(FILE *file) {
344 /* Import a BMP image (based on file handle), cleanup & return NULL if error */
345 struct bmpheader bmp;
346 int i,l;
347 GImage *ret = NULL;
348 struct _GImage *base;
349
350 if ( file==NULL )
351 return( NULL );
352
353 /* First, read-in header information */
354 memset(&bmp,'\0',sizeof(bmp));
355 if ( fillbmpheader(file,&bmp) )
356 goto errorGImageReadBmp;
357
358 /* Create memory-space to read-in bmp file */
359 if ( (bmp.bitsperpixel>=16 && \
360 (bmp.int32_pixels=(uint32_t *)(malloc(bmp.height*bmp.width*sizeof(uint32_t))))==NULL) || \
361 (bmp.bitsperpixel==1 && \
362 (bmp.byte_pixels=(unsigned char *)(malloc(bmp.height*((bmp.width+7)/8)*sizeof(unsigned char))))==NULL) || \
363 (bmp.byte_pixels=(unsigned char *)(malloc(bmp.height*bmp.width*sizeof(unsigned char))))==NULL ) {
364 NoMoreMemMessage();
365 return( NULL );
366 }
367
368 if ( !readpixels(file,&bmp) )
369 goto errorGImageReadBmp;
370
371 if ( !bmp.invert ) {
372 if ( (ret=_GImage_Create(bmp.bitsperpixel>=16?it_true:bmp.bitsperpixel!=1?it_index:it_mono,
373 bmp.width, bmp.height))==NULL ) {
374 NoMoreMemMessage();
375 goto errorGImageMemBmp;
376 }
377 if ( bmp.bitsperpixel>=16 ) {
378 ret->u.image->data = (uint8_t *) bmp.int32_pixels;
379 } else if ( bmp.bitsperpixel!=1 ) {
380 ret->u.image->data = (uint8_t *) bmp.byte_pixels;
381 }
382 } else {
383 if ( bmp.bitsperpixel>=16 ) {
384 if ( (ret=GImageCreate(it_true,bmp.width, bmp.height))==NULL ) {
385 NoMoreMemMessage();
386 goto errorGImageMemBmp;
387 }
388 base = ret->u.image;
389 for ( i=0; i<bmp.height; ++i ) {
390 l = bmp.height-1-i;
391 memcpy(base->data+l*base->bytes_per_line,bmp.int32_pixels+i*bmp.width,bmp.width*sizeof(uint32_t));
392 }
393 free(bmp.int32_pixels);
394 } else if ( bmp.bitsperpixel!=1 ) {
395 if ( (ret=GImageCreate(it_index,bmp.width, bmp.height))==NULL ) {
396 NoMoreMemMessage();
397 goto errorGImageMemBmp;
398 }
399 base = ret->u.image;
400 for ( i=0; i<bmp.height; ++i ) {
401 l = bmp.height-1-i;
402 memcpy(base->data+l*base->bytes_per_line,bmp.byte_pixels+i*bmp.width,bmp.width);
403 }
404 free(bmp.byte_pixels);
405 } else {
406 if ( (ret=GImageCreate(it_mono,bmp.width, bmp.height))==NULL ) {
407 NoMoreMemMessage();
408 goto errorGImageMemBmp;
409 }
410 base = ret->u.image;
411 for ( i=0; i<bmp.height; ++i ) {
412 l = bmp.height-1-i;
413 memcpy(base->data+l*base->bytes_per_line,bmp.byte_pixels+i*base->bytes_per_line,base->bytes_per_line);
414 }
415 free(bmp.byte_pixels);
416 }
417 }
418 if ( ret->u.image->image_type==it_index ) {
419 ret->u.image->clut->clut_len = bmp.colorsused;
420 memcpy(ret->u.image->clut->clut,bmp.clut,bmp.colorsused*sizeof(Color));
421 ret->u.image->clut->trans_index = COLOR_UNKNOWN;
422 } else if ( ret->u.image->image_type==it_mono && bmp.colorsused!=0 ) {
423 if ( (ret->u.image->clut=(GClut *)(calloc(1,sizeof(GClut))))==NULL ) {
424 NoMoreMemMessage();
425 goto errorGImageMemBmp;
426 }
427 ret->u.image->clut->clut_len = bmp.colorsused;
428 memcpy(ret->u.image->clut->clut,bmp.clut,bmp.colorsused*sizeof(Color));
429 ret->u.image->clut->trans_index = COLOR_UNKNOWN;
430 }
431 return( ret );
432
433 errorGImageReadBmp:
434 fprintf(stderr,"Bad input file\n");
435 errorGImageMemBmp:
436 GImageDestroy(ret);
437 if ( bmp.bitsperpixel>=16 ) free(bmp.int32_pixels);
438 else free(bmp.byte_pixels);
439 return( NULL );
440 }
441
442 GImage *GImageReadBmp(char *filename) {
443 /* Import a BMP image, else cleanup and return NULL if error found */
444 FILE *file; /* source file */
445 GImage *ret;
446
447 if ( (file=fopen(filename,"rb"))==NULL ) {
448 fprintf(stderr,"Can't open \"%s\"\n", filename);
449 return( NULL );
450 }
451
452 ret = GImageRead_Bmp(file);
453 fclose(file);
454 return( ret );
455 }