-
-
Notifications
You must be signed in to change notification settings - Fork 504
Expand file tree
/
Copy pathsave-bmp.cpp
More file actions
233 lines (194 loc) · 7.07 KB
/
save-bmp.cpp
File metadata and controls
233 lines (194 loc) · 7.07 KB
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
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "save-bmp.h"
#include <cstdio>
#ifdef MSDFGEN_USE_CPP11
#include <cstdint>
#else
namespace msdfgen {
typedef int int32_t;
typedef unsigned uint32_t;
typedef unsigned short uint16_t;
}
#endif
#include "pixel-conversion.hpp"
namespace msdfgen {
static const byte BMP_LINEAR_COLOR_SPACE_SPECIFICATION[48] = {
0xf8, 0xc2, 0x64, 0x1a, 0x08, 0x3d, 0x9b, 0x0d, 0x11, 0x36, 0x3c, 0x01,
0x1c, 0xeb, 0xe2, 0x16, 0x39, 0xd6, 0xc5, 0x2d, 0x09, 0xf9, 0xa0, 0x07,
0xdf, 0x4f, 0x8d, 0x0b, 0xc0, 0xec, 0x9e, 0x04, 0xf4, 0xfd, 0xd4, 0x3c,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
};
template <typename T>
bool writeLE(FILE *file, T value);
template <>
bool writeLE(FILE *f, uint16_t value) {
byte bytes[2] = { byte(value), byte(value>>8) };
return fwrite(bytes, 1, 2, f) == 2;
}
template <>
bool writeLE(FILE *f, uint32_t value) {
byte bytes[4] = { byte(value), byte(value>>8), byte(value>>16), byte(value>>24) };
return fwrite(bytes, 1, 4, f) == 4;
}
template <>
bool writeLE(FILE *f, int32_t value) {
byte bytes[4] = { byte(value), byte(value>>8), byte(value>>16), byte(value>>24) };
return fwrite(bytes, 1, 4, f) == 4;
}
static bool writeBmpHeader(FILE *file, int bytesPerPixel, int width, int height, int &paddedWidth) {
paddedWidth = (bytesPerPixel*width+3)&~3; // Ceil to multiple of 4 bytes
uint32_t colorTableEntries = bytesPerPixel == 1 ? 256 : 0;
uint32_t bitmapStart = 14+108+4*colorTableEntries;
uint32_t bitmapSize = paddedWidth*height;
uint32_t fileSize = bitmapStart+bitmapSize;
// BMP file header
writeLE<uint16_t>(file, 0x4d42u); // "BM"
writeLE<uint32_t>(file, fileSize);
writeLE<uint16_t>(file, 0);
writeLE<uint16_t>(file, 0);
writeLE<uint32_t>(file, bitmapStart);
// DIB header (BITMAPV4HEADER)
writeLE<uint32_t>(file, 108);
writeLE<int32_t>(file, width);
writeLE<int32_t>(file, height);
writeLE<uint16_t>(file, 1); // planes
writeLE<uint16_t>(file, uint16_t(8*bytesPerPixel));
writeLE<uint32_t>(file, bytesPerPixel == 4 ? 3 : 0); // 0 = BI_RGB, 3 = BI_BITFIELDS
writeLE<uint32_t>(file, bitmapSize);
writeLE<uint32_t>(file, 2835); // 72 dpi as pixels/meter
writeLE<uint32_t>(file, 2835);
writeLE<uint32_t>(file, colorTableEntries);
writeLE<uint32_t>(file, colorTableEntries);
writeLE<uint32_t>(file, 0x00ff0000u); // Red channel bit mask
writeLE<uint32_t>(file, 0x0000ff00u); // Green channel bit mask
writeLE<uint32_t>(file, 0x000000ffu); // Blue channel bit mask
writeLE<uint32_t>(file, bytesPerPixel == 4 ? 0xff000000u : 0u); // Alpha channel bit mask
writeLE<uint32_t>(file, 0); // LCS_CALIBRATED_RGB
fwrite(BMP_LINEAR_COLOR_SPACE_SPECIFICATION, 1, 48, file);
// Use indexed color for single channel - color table just lists grayscale colors (#000000, #010101, ... #FFFFFF)
if (bytesPerPixel == 1) {
for (uint32_t grayColor = 0; grayColor < 0x01000000u; grayColor += 0x00010101u)
writeLE<uint32_t>(file, grayColor|0xff000000u);
}
return true;
}
bool saveBmp(BitmapConstSection<byte, 1> bitmap, const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file)
return false;
int paddedWidth;
bitmap.reorient(Y_UPWARD);
writeBmpHeader(file, 1, bitmap.width, bitmap.height, paddedWidth);
byte padding[4] = { };
int padLength = paddedWidth-bitmap.width;
for (int y = 0; y < bitmap.height; ++y) {
fwrite(bitmap(0, y), sizeof(byte), bitmap.width, file);
fwrite(padding, 1, padLength, file);
}
return !fclose(file);
}
bool saveBmp(BitmapConstSection<byte, 3> bitmap, const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file)
return false;
int paddedWidth;
bitmap.reorient(Y_UPWARD);
writeBmpHeader(file, 3, bitmap.width, bitmap.height, paddedWidth);
byte padding[4] = { };
int padLength = paddedWidth-3*bitmap.width;
for (int y = 0; y < bitmap.height; ++y) {
for (int x = 0; x < bitmap.width; ++x) {
byte bgr[3] = {
bitmap(x, y)[2],
bitmap(x, y)[1],
bitmap(x, y)[0]
};
fwrite(bgr, sizeof(byte), 3, file);
}
fwrite(padding, 1, padLength, file);
}
return !fclose(file);
}
bool saveBmp(BitmapConstSection<byte, 4> bitmap, const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file)
return false;
int dummyPaddedWidth;
bitmap.reorient(Y_UPWARD);
writeBmpHeader(file, 4, bitmap.width, bitmap.height, dummyPaddedWidth);
for (int y = 0; y < bitmap.height; ++y) {
for (int x = 0; x < bitmap.width; ++x) {
byte bgra[4] = {
bitmap(x, y)[2],
bitmap(x, y)[1],
bitmap(x, y)[0],
bitmap(x, y)[3]
};
fwrite(bgra, sizeof(byte), 4, file);
}
}
return !fclose(file);
}
bool saveBmp(BitmapConstSection<float, 1> bitmap, const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file)
return false;
int paddedWidth;
bitmap.reorient(Y_UPWARD);
writeBmpHeader(file, 1, bitmap.width, bitmap.height, paddedWidth);
byte padding[4] = { };
int padLength = paddedWidth-bitmap.width;
for (int y = 0; y < bitmap.height; ++y) {
for (int x = 0; x < bitmap.width; ++x) {
byte px = pixelFloatToByte(*bitmap(x, y));
fwrite(&px, sizeof(byte), 1, file);
}
fwrite(padding, 1, padLength, file);
}
return !fclose(file);
}
bool saveBmp(BitmapConstSection<float, 3> bitmap, const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file)
return false;
int paddedWidth;
bitmap.reorient(Y_UPWARD);
writeBmpHeader(file, 3, bitmap.width, bitmap.height, paddedWidth);
byte padding[4] = { };
int padLength = paddedWidth-3*bitmap.width;
for (int y = 0; y < bitmap.height; ++y) {
for (int x = 0; x < bitmap.width; ++x) {
byte bgr[3] = {
pixelFloatToByte(bitmap(x, y)[2]),
pixelFloatToByte(bitmap(x, y)[1]),
pixelFloatToByte(bitmap(x, y)[0])
};
fwrite(bgr, sizeof(byte), 3, file);
}
fwrite(padding, 1, padLength, file);
}
return !fclose(file);
}
bool saveBmp(BitmapConstSection<float, 4> bitmap, const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file)
return false;
int dummyPaddedWidth;
bitmap.reorient(Y_UPWARD);
writeBmpHeader(file, 4, bitmap.width, bitmap.height, dummyPaddedWidth);
for (int y = 0; y < bitmap.height; ++y) {
for (int x = 0; x < bitmap.width; ++x) {
byte bgra[4] = {
pixelFloatToByte(bitmap(x, y)[2]),
pixelFloatToByte(bitmap(x, y)[1]),
pixelFloatToByte(bitmap(x, y)[0]),
pixelFloatToByte(bitmap(x, y)[3])
};
fwrite(bgra, sizeof(byte), 4, file);
}
}
return !fclose(file);
}
}