forked from ghewgill/xearth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.c
247 lines (210 loc) · 5.96 KB
/
bmp.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* bmp.c
* greg hewgill
* september 2009
*
* Copyright (C) 1989, 1990, 1993-1995, 1999 Kirk Lauritz Johnson
*
* Parts of the source code (as marked) are:
* Copyright (C) 1989, 1990, 1991 by Jim Frost
* Copyright (C) 1992 by Jamie Zawinski <[email protected]>
*
* Permission to use, copy, modify and freely distribute xearth for
* non-commercial and not-for-profit purposes is hereby granted
* without fee, provided that both the above copyright notice and this
* permission notice appear in all copies and in supporting
* documentation.
*
* Unisys Corporation holds worldwide patent rights on the Lempel Zev
* Welch (LZW) compression technique employed in the CompuServe GIF
* image file format as well as in other formats. Unisys has made it
* clear, however, that it does not require licensing or fees to be
* paid for freely distributed, non-commercial applications (such as
* xearth) that employ LZW/GIF technology. Those wishing further
* information about licensing the LZW patent should contact Unisys
* directly at ([email protected]) or by writing to
*
* Unisys Corporation
* Welch Licensing Department
* M/S-C1SW19
* P.O. Box 500
* Blue Bell, PA 19424
*
* The author makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xearth.h"
#include "kljcpyrt.h"
static void bmp_setup _P((void));
static int bmp_row _P((u_char *));
static void bmp_cleanup _P((void));
static void bmp_truecolor_setup _P((void));
static int bmp_truecolor_row _P((u_char *));
static void bmp_truecolor_cleanup _P((void));
#pragma pack(push, 1)
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef long LONG;
typedef unsigned long DWORD;
#define BI_RGB (0)
struct __attribute__((__packed__)) BITMAPFILEHEADER {
char bfType[2];
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
};
struct __attribute__((__packed__)) BITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
};
struct __attribute__((__packed__)) RGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
};
#pragma pack(pop)
static u16or32 *dith;
void bmp_output()
{
compute_positions();
scan_map();
do_dots();
if (num_colors > 256)
{
bmp_truecolor_setup();
render(bmp_truecolor_row);
bmp_truecolor_cleanup();
}
else
{
bmp_setup();
render(bmp_row);
bmp_cleanup();
}
}
static void bmp_setup()
{
int i;
struct BITMAPFILEHEADER bmf;
struct BITMAPINFOHEADER bmi;
dither_setup(num_colors);
dith = (u16or32 *) malloc((unsigned) sizeof(u16or32) * wdth);
assert(dith != NULL);
assert(sizeof(struct BITMAPFILEHEADER) == 14);
assert(sizeof(struct BITMAPINFOHEADER) == 40);
bmf.bfType[0] = 'B';
bmf.bfType[1] = 'M';
bmf.bfSize = sizeof(struct BITMAPFILEHEADER)
+ sizeof(struct BITMAPINFOHEADER)
+ num_colors * sizeof(struct RGBQUAD)
+ hght * 4 * ((wdth + 3) / 4);
bmf.bfReserved1 = 0;
bmf.bfReserved2 = 0;
bmf.bfOffBits = sizeof(struct BITMAPFILEHEADER)
+ sizeof(struct BITMAPINFOHEADER)
+ num_colors * sizeof(struct RGBQUAD);
fwrite(&bmf, 1, sizeof(bmf), stdout);
bmi.biSize = sizeof(bmi);
bmi.biWidth = wdth;
bmi.biHeight = -hght;
bmi.biPlanes = 1;
bmi.biBitCount = 8;
bmi.biCompression = BI_RGB;
bmi.biSizeImage = hght * 4 * ((wdth + 3) / 4);
bmi.biXPelsPerMeter = 0;
bmi.biYPelsPerMeter = 0;
bmi.biClrUsed = num_colors;
bmi.biClrImportant = num_colors;
fwrite(&bmi, 1, sizeof(bmi), stdout);
for (i=0; i<dither_ncolors; i++)
{
struct RGBQUAD rgb;
rgb.rgbBlue = dither_colormap[i*3+2];
rgb.rgbGreen = dither_colormap[i*3+1];
rgb.rgbRed = dither_colormap[i*3+0];
rgb.rgbReserved = 0;
fwrite(&rgb, 1, sizeof(rgb), stdout);
}
}
static int bmp_row(row)
u_char *row;
{
int i, i_lim;
u16or32 *tmp;
tmp = dith;
dither_row(row, tmp);
i_lim = 4 * ((wdth + 3) / 4);
for (i = 0; i < i_lim; i++)
fwrite(tmp+i, 1, 1, stdout);
return 0;
}
static void bmp_cleanup()
{
dither_cleanup();
free(dith);
}
static void bmp_truecolor_setup()
{
struct BITMAPFILEHEADER bmf;
struct BITMAPINFOHEADER bmi;
assert(sizeof(struct BITMAPFILEHEADER) == 14);
assert(sizeof(struct BITMAPINFOHEADER) == 40);
bmf.bfType[0] = 'B';
bmf.bfType[1] = 'M';
bmf.bfSize = sizeof(struct BITMAPFILEHEADER)
+ sizeof(struct BITMAPINFOHEADER)
+ hght * 4 * ((3 * wdth + 3) / 4);
bmf.bfReserved1 = 0;
bmf.bfReserved2 = 0;
bmf.bfOffBits = sizeof(struct BITMAPFILEHEADER)
+ sizeof(struct BITMAPINFOHEADER);
fwrite(&bmf, 1, sizeof(bmf), stdout);
bmi.biSize = sizeof(bmi);
bmi.biWidth = wdth;
bmi.biHeight = -hght;
bmi.biPlanes = 1;
bmi.biBitCount = 24;
bmi.biCompression = BI_RGB;
bmi.biSizeImage = hght * 4 * ((3 * wdth + 3) / 4);
bmi.biXPelsPerMeter = 0;
bmi.biYPelsPerMeter = 0;
bmi.biClrUsed = 0;
bmi.biClrImportant = 0;
fwrite(&bmi, 1, sizeof(bmi), stdout);
}
static int bmp_truecolor_row(row)
u_char *row;
{
int i, i_lim;
i_lim = 4 * ((wdth + 3) / 4);
for (i = 0; i < i_lim; i++)
{
fwrite(row+3*i+2, 1, 1, stdout);
fwrite(row+3*i+1, 1, 1, stdout);
fwrite(row+3*i+0, 1, 1, stdout);
}
return 0;
}
static void bmp_truecolor_cleanup()
{
}