Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/msdfgen/core/save-bmp.cpp
9902 views
1
2
#ifndef _CRT_SECURE_NO_WARNINGS
3
#define _CRT_SECURE_NO_WARNINGS
4
#endif
5
6
#include "save-bmp.h"
7
8
#include <cstdio>
9
10
#ifdef MSDFGEN_USE_CPP11
11
#include <cstdint>
12
#else
13
namespace msdfgen {
14
typedef int int32_t;
15
typedef unsigned uint32_t;
16
typedef unsigned short uint16_t;
17
typedef unsigned char uint8_t;
18
}
19
#endif
20
21
#include "pixel-conversion.hpp"
22
23
namespace msdfgen {
24
25
template <typename T>
26
static bool writeValue(FILE *file, T value) {
27
#ifdef __BIG_ENDIAN__
28
T reverse = 0;
29
for (int i = 0; i < sizeof(T); ++i) {
30
reverse <<= 8;
31
reverse |= value&T(0xff);
32
value >>= 8;
33
}
34
return fwrite(&reverse, sizeof(T), 1, file) == 1;
35
#else
36
return fwrite(&value, sizeof(T), 1, file) == 1;
37
#endif
38
}
39
40
static bool writeBmpHeader(FILE *file, int width, int height, int &paddedWidth) {
41
paddedWidth = (3*width+3)&~3;
42
const uint32_t bitmapStart = 54;
43
const uint32_t bitmapSize = paddedWidth*height;
44
const uint32_t fileSize = bitmapStart+bitmapSize;
45
46
writeValue<uint16_t>(file, 0x4d42u);
47
writeValue<uint32_t>(file, fileSize);
48
writeValue<uint16_t>(file, 0);
49
writeValue<uint16_t>(file, 0);
50
writeValue<uint32_t>(file, bitmapStart);
51
52
writeValue<uint32_t>(file, 40);
53
writeValue<int32_t>(file, width);
54
writeValue<int32_t>(file, height);
55
writeValue<uint16_t>(file, 1);
56
writeValue<uint16_t>(file, 24);
57
writeValue<uint32_t>(file, 0);
58
writeValue<uint32_t>(file, bitmapSize);
59
writeValue<uint32_t>(file, 2835);
60
writeValue<uint32_t>(file, 2835);
61
writeValue<uint32_t>(file, 0);
62
writeValue<uint32_t>(file, 0);
63
64
return true;
65
}
66
67
bool saveBmp(const BitmapConstRef<byte, 1> &bitmap, const char *filename) {
68
FILE *file = fopen(filename, "wb");
69
if (!file)
70
return false;
71
72
int paddedWidth;
73
writeBmpHeader(file, bitmap.width, bitmap.height, paddedWidth);
74
75
const uint8_t padding[4] = { };
76
int padLength = paddedWidth-3*bitmap.width;
77
for (int y = 0; y < bitmap.height; ++y) {
78
for (int x = 0; x < bitmap.width; ++x) {
79
uint8_t px = (uint8_t) *bitmap(x, y);
80
fwrite(&px, sizeof(uint8_t), 1, file);
81
fwrite(&px, sizeof(uint8_t), 1, file);
82
fwrite(&px, sizeof(uint8_t), 1, file);
83
}
84
fwrite(padding, 1, padLength, file);
85
}
86
87
return !fclose(file);
88
}
89
90
bool saveBmp(const BitmapConstRef<byte, 3> &bitmap, const char *filename) {
91
FILE *file = fopen(filename, "wb");
92
if (!file)
93
return false;
94
95
int paddedWidth;
96
writeBmpHeader(file, bitmap.width, bitmap.height, paddedWidth);
97
98
const uint8_t padding[4] = { };
99
int padLength = paddedWidth-3*bitmap.width;
100
for (int y = 0; y < bitmap.height; ++y) {
101
for (int x = 0; x < bitmap.width; ++x) {
102
uint8_t bgr[3] = {
103
(uint8_t) bitmap(x, y)[2],
104
(uint8_t) bitmap(x, y)[1],
105
(uint8_t) bitmap(x, y)[0]
106
};
107
fwrite(bgr, sizeof(uint8_t), 3, file);
108
}
109
fwrite(padding, 1, padLength, file);
110
}
111
112
return !fclose(file);
113
}
114
115
bool saveBmp(const BitmapConstRef<byte, 4> &bitmap, const char *filename) {
116
// RGBA not supported by the BMP format
117
return false;
118
}
119
120
bool saveBmp(const BitmapConstRef<float, 1> &bitmap, const char *filename) {
121
FILE *file = fopen(filename, "wb");
122
if (!file)
123
return false;
124
125
int paddedWidth;
126
writeBmpHeader(file, bitmap.width, bitmap.height, paddedWidth);
127
128
const uint8_t padding[4] = { };
129
int padLength = paddedWidth-3*bitmap.width;
130
for (int y = 0; y < bitmap.height; ++y) {
131
for (int x = 0; x < bitmap.width; ++x) {
132
uint8_t px = (uint8_t) pixelFloatToByte(*bitmap(x, y));
133
fwrite(&px, sizeof(uint8_t), 1, file);
134
fwrite(&px, sizeof(uint8_t), 1, file);
135
fwrite(&px, sizeof(uint8_t), 1, file);
136
}
137
fwrite(padding, 1, padLength, file);
138
}
139
140
return !fclose(file);
141
}
142
143
bool saveBmp(const BitmapConstRef<float, 3> &bitmap, const char *filename) {
144
FILE *file = fopen(filename, "wb");
145
if (!file)
146
return false;
147
148
int paddedWidth;
149
writeBmpHeader(file, bitmap.width, bitmap.height, paddedWidth);
150
151
const uint8_t padding[4] = { };
152
int padLength = paddedWidth-3*bitmap.width;
153
for (int y = 0; y < bitmap.height; ++y) {
154
for (int x = 0; x < bitmap.width; ++x) {
155
uint8_t bgr[3] = {
156
(uint8_t) pixelFloatToByte(bitmap(x, y)[2]),
157
(uint8_t) pixelFloatToByte(bitmap(x, y)[1]),
158
(uint8_t) pixelFloatToByte(bitmap(x, y)[0])
159
};
160
fwrite(bgr, sizeof(uint8_t), 3, file);
161
}
162
fwrite(padding, 1, padLength, file);
163
}
164
165
return !fclose(file);
166
}
167
168
bool saveBmp(const BitmapConstRef<float, 4> &bitmap, const char *filename) {
169
// RGBA not supported by the BMP format
170
return false;
171
}
172
173
}
174
175