| 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 |
|
| 33 |
static void myputs(short s, FILE *file) { |
| 34 |
putc(s&0xff,file); |
| 35 |
putc(s>>8,file); |
| 36 |
} |
| 37 |
|
| 38 |
static void putl(short s, FILE *file) { |
| 39 |
putc(s&0xff,file); |
| 40 |
putc((s>>8)&0xff,file); |
| 41 |
putc((s>>16)&0xff,file); |
| 42 |
putc((s>>24)&0xff,file); |
| 43 |
} |
| 44 |
|
| 45 |
int GImageWrite_Bmp(GImage *gi, FILE *file) { |
| 46 |
struct _GImage *base = gi->list_len==0?gi->u.image:gi->u.images[0]; |
| 47 |
int headersize=40, preheadersize=14; |
| 48 |
int filesize, offset, imagesize; |
| 49 |
int bitsperpixel, clutsize, ncol; |
| 50 |
int row, col, i; |
| 51 |
|
| 52 |
if ( base->image_type == it_mono ) { |
| 53 |
ncol = 2; |
| 54 |
bitsperpixel = 1; |
| 55 |
clutsize = ncol*4; |
| 56 |
} else if ( base->image_type == it_index ) { |
| 57 |
ncol = base->clut->clut_len; |
| 58 |
if ( ncol<=16 ) |
| 59 |
bitsperpixel = 4; |
| 60 |
else |
| 61 |
bitsperpixel = 8; |
| 62 |
clutsize = ncol*4; |
| 63 |
} else { |
| 64 |
bitsperpixel = 24; |
| 65 |
clutsize = 0; |
| 66 |
ncol = 0; |
| 67 |
} |
| 68 |
imagesize = ((base->bytes_per_line + 3) & ~3U) * base->height; |
| 69 |
offset = preheadersize + headersize + clutsize; |
| 70 |
filesize = offset + imagesize; |
| 71 |
|
| 72 |
putc('B',file); |
| 73 |
putc('M',file); |
| 74 |
putl(filesize,file); |
| 75 |
myputs(0,file); |
| 76 |
myputs(0,file); |
| 77 |
putl(offset,file); |
| 78 |
putl(headersize,file); |
| 79 |
putl(base->width,file); |
| 80 |
putl(base->height,file); |
| 81 |
myputs(1,file); |
| 82 |
myputs(bitsperpixel,file); |
| 83 |
putl(0,file); |
| 84 |
putl(imagesize,file); |
| 85 |
putl(3000,file); |
| 86 |
putl(3000,file); |
| 87 |
putl(ncol,file); |
| 88 |
putl(0,file); |
| 89 |
|
| 90 |
if ( clutsize!=0 ) { |
| 91 |
int i; |
| 92 |
if ( base->clut!=NULL ) { |
| 93 |
for ( i=0; i<ncol; ++i ) { |
| 94 |
putc(COLOR_BLUE(base->clut->clut[i]),file); |
| 95 |
putc(COLOR_GREEN(base->clut->clut[i]),file); |
| 96 |
putc(COLOR_RED(base->clut->clut[i]),file); |
| 97 |
putc(0,file); |
| 98 |
} |
| 99 |
} else { |
| 100 |
putc(0,file); putc(0,file); putc(0,file); putc(0,file); |
| 101 |
putc(0xff,file); putc(0xff,file); putc(0xff,file); putc(0,file); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
for ( row = base->height-1; row>=0; --row ) { |
| 106 |
int pad=0; |
| 107 |
|
| 108 |
if ( bitsperpixel==24 ) { |
| 109 |
uint32_t *pt = (uint32_t *) (base->data+row*base->bytes_per_line); |
| 110 |
for ( col=0; col<base->width; ++col ) { |
| 111 |
putc(COLOR_BLUE(pt[col]),file); |
| 112 |
putc(COLOR_GREEN(pt[col]),file); |
| 113 |
putc(COLOR_RED(pt[col]),file); |
| 114 |
} |
| 115 |
pad = base->width&3; |
| 116 |
} else if ( bitsperpixel==8 ) { |
| 117 |
unsigned char *pt = (unsigned char *) (base->data+row*base->bytes_per_line); |
| 118 |
fwrite(pt,1,base->width,file); |
| 119 |
pad = 4-(base->width&3); |
| 120 |
} else if ( bitsperpixel==4 ) { |
| 121 |
unsigned char *pt = (unsigned char *) (base->data+row*base->bytes_per_line); |
| 122 |
for ( col=0; col<base->width/2; ++col ) { |
| 123 |
putc( (*pt<<4)|pt[1], file); |
| 124 |
pt += 2; |
| 125 |
} |
| 126 |
if ( base->width&1 ) |
| 127 |
putc(*pt<<4,file); |
| 128 |
pad = 4-(((base->width+1)>>1)&3); |
| 129 |
} else if ( bitsperpixel==1 ) { |
| 130 |
unsigned char *pt = (unsigned char *) (base->data+row*base->bytes_per_line); |
| 131 |
fwrite(pt,1,base->bytes_per_line,file); |
| 132 |
pad = 4-(base->bytes_per_line&3); |
| 133 |
} |
| 134 |
if ( pad&1 ) |
| 135 |
putc('\0',file); |
| 136 |
if ( pad&2 ) |
| 137 |
myputs(0,file); |
| 138 |
} |
| 139 |
fflush(file); |
| 140 |
i = ferror(file); |
| 141 |
return( !i ); |
| 142 |
} |
| 143 |
|
| 144 |
int GImageWriteBmp(GImage *gi, char *filename) { |
| 145 |
FILE *file; |
| 146 |
int ret; |
| 147 |
|
| 148 |
if ((file=fopen(filename,"wb"))==NULL ) |
| 149 |
return(false); |
| 150 |
ret = GImageWrite_Bmp(gi,file); |
| 151 |
fclose(file); |
| 152 |
return( ret ); |
| 153 |
} |