| 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 |
#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 |
|
| 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 |
|
| 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 |
|
| 69 |
int i; |
| 70 |
long temp; |
| 71 |
|
| 72 |
if ( fgetc(fp)!='B' || getc(fp)!='M' || \ |
| 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 ) { |
| 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 |
|
| 87 |
|
| 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 |
; |
| 112 |
else if ( head->compression==0 && ( head->bitsperpixel<=8 || head->bitsperpixel==24 || head->bitsperpixel==32 ) ) |
| 113 |
; |
| 114 |
else if ( head->compression==1 && head->bitsperpixel==8 ) |
| 115 |
; |
| 116 |
else if ( head->compression==2 && head->bitsperpixel==4 ) |
| 117 |
; |
| 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) || \ |
| 148 |
getlong(fp,&temp) || \ |
| 149 |
getlong(fp,&temp) || \ |
| 150 |
getlong(fp,&temp) || \ |
| 151 |
getlong(fp,&temp) || \ |
| 152 |
getlong(fp,&temp) || \ |
| 153 |
getlong(fp,&temp) || \ |
| 154 |
getlong(fp,&temp) || \ |
| 155 |
getlong(fp,&temp) || \ |
| 156 |
getlong(fp,&temp) || \ |
| 157 |
getlong(fp,&temp) || \ |
| 158 |
getlong(fp,&temp) || \ |
| 159 |
getlong(fp,&temp) || \ |
| 160 |
getlong(fp,&temp) ) ) |
| 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 |
|
| 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 ) { |
| 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 |
|
| 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 ) { |
| 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); |
| 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); |
| 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 )) { |
| 337 |
return( 0 ); |
| 338 |
} |
| 339 |
|
| 340 |
return( 1 ); |
| 341 |
} |
| 342 |
|
| 343 |
GImage *GImageRead_Bmp(FILE *file) { |
| 344 |
|
| 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 |
|
| 354 |
memset(&bmp,'\0',sizeof(bmp)); |
| 355 |
if ( fillbmpheader(file,&bmp) ) |
| 356 |
goto errorGImageReadBmp; |
| 357 |
|
| 358 |
|
| 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 |
|
| 444 |
FILE *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 |
} |