| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 ); |
| 40 |
} |
| 41 |
|
| 42 |
GImage *GImageReadXbm(char * filename) { |
| 43 |
|
| 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 |
|
| 58 |
if ( fscanf(file,"#define %*s %d\n",&width)!=1 || \ |
| 59 |
fscanf(file,"#define %*s %d\n",&height)!=1 ) |
| 60 |
goto errorGImageReadXbm; |
| 61 |
|
| 62 |
|
| 63 |
if ( (ch=getc(file))<0 ) |
| 64 |
goto errorGImageReadXbm; |
| 65 |
if ( ch=='#' ) { |
| 66 |
if ( fscanf(file,"define %*s %*d\n")<0 || \ |
| 67 |
fscanf(file,"#define %*s %*d\n")<0 ) |
| 68 |
goto errorGImageReadXbm; |
| 69 |
} else |
| 70 |
if ( ungetc(ch,file)<0 ) |
| 71 |
goto errorGImageReadXbm; |
| 72 |
|
| 73 |
|
| 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 |
|
| 80 |
} |
| 81 |
|
| 82 |
|
| 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 |
|
| 98 |
if ( (gi=GImageCreate(it_mono,width,height))==NULL ) |
| 99 |
goto errorGImageReadXbmMem; |
| 100 |
|
| 101 |
|
| 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 |
} |