Debian Codegraph @

gutils/gimagewritebmp.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
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); /* filesize */
75 myputs(0,file); /* mbz1 */
76 myputs(0,file); /* mbz2 */
77 putl(offset,file); /* offset */
78 putl(headersize,file); /* headersize */
79 putl(base->width,file); /* width */
80 putl(base->height,file); /* height */
81 myputs(1,file); /* planes */
82 myputs(bitsperpixel,file); /* bitsperpixel */
83 putl(0,file); /* compression */
84 putl(imagesize,file); /* imagesize */
85 putl(3000,file); /* horizontal res, pixels/meter */
86 putl(3000,file); /* vertical res, pixels/meter */
87 putl(ncol,file); /* colours used */
88 putl(0,file); /* colours important */
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 ) /* pad to 4byte boundary */
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 }