| 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 |
#include <fontforge-config.h> |
| 29 |
|
| 30 |
#include "gimage.h" |
| 31 |
|
| 32 |
struct sgiheader { |
| 33 |
short magic; |
| 34 |
char format; |
| 35 |
char bpc; |
| 36 |
unsigned short dim; |
| 37 |
unsigned short width; |
| 38 |
unsigned short height; |
| 39 |
unsigned short chans; |
| 40 |
long pixmin; |
| 41 |
long pixmax; |
| 42 |
char dummy[4]; |
| 43 |
char imagename[80]; |
| 44 |
long colormap; |
| 45 |
char pad[404]; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 111 |
long i; |
| 112 |
|
| 113 |
for ( i=0; i<tablen; ++i ) |
| 114 |
if ( getlong(fp,(long *)&tab[i]) ) |
| 115 |
return( -1 ); |
| 116 |
|
| 117 |
return( 0 ); |
| 118 |
} |
| 119 |
|
| 120 |
static int find_scanline(FILE *fp,struct sgiheader *header,int cur, |
| 121 |
unsigned long *starttab,unsigned char **ptrtab) { |
| 122 |
|
| 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; |
| 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; |
| 189 |
unsigned long *starttab=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 |
|
| 202 |
if ( getsgiheader(&header,fp) ) |
| 203 |
goto errorGImageReadRgbFile; |
| 204 |
|
| 205 |
|
| 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 |
|
| 214 |
|
| 215 |
|
| 216 |
tablen = header.height*header.chans; |
| 217 |
if ( (starttab=(unsigned long *)calloc(1,tablen*sizeof(unsigned long)))==NULL || \ |
| 218 |
|
| 219 |
(ptrtab=(unsigned char **)calloc(1,tablen*sizeof(unsigned char *)))==NULL ) { |
| 220 |
NoMoreMemMessage(); |
| 221 |
goto errorGImageReadRgbMem; |
| 222 |
} |
| 223 |
if ( readlongtab(fp,starttab,tablen) ) |
| 224 |
|
| 225 |
goto errorGImageReadRgbFile; |
| 226 |
|
| 227 |
|
| 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 |
|
| 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); |
| 248 |
} else { |
| 249 |
|
| 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 |
|
| 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); |
| 326 |
free(r); free(g); free(b); free(a); |
| 327 |
GImageDestroy(ret); |
| 328 |
fclose(fp); |
| 329 |
return( NULL ); |
| 330 |
} |