-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.cpp
229 lines (182 loc) · 5.82 KB
/
bmp.cpp
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
#include <fstream>
#include <cstdio>
#include <cmath>
#include "bmp.hpp"
/* bits per pixel <= 8
BITMAPFILEHEADER 14 byte
BITMAPINFO 40 byte
2^bitsPerPixel * RGBQUAD // RGBQUAD = 4 bytes
array of pixels
*/
BMP::BMP() : width(info.biWidth), height(info.biHeight), bitCount(info.biBitCount),
header{0}, info{0}, flip(0), absHeight(0), bytesWidth(0), padding(0) {}
BMP::~BMP() {}
void BMP::initBmp() {
flip = (info.biHeight > 0) ? (info.biHeight - 1) : 0;
absHeight = abs(info.biHeight);
bytesWidth = (info.biWidth * info.biBitCount + 7) / 8; // width in bytes rounded up
padding = (4 - (bytesWidth % 4)) % 4;
header.bfType = 0x4D42;
header.bfReserved1 = 0;
header.bfReserved2 = 0;
info.biSize = sizeof(BITMAPINFO); // 40
//info.biWidth set by reference
//info.biHeight set by reference
info.biPlanes = 1;
//info.biBitCount set by reference
info.biCompression = 0;
info.biSizeImage = (((info.biBitCount * info.biWidth + 31) / 32) * 4) * absHeight;
info.biXPelsPerMeter = 0;
info.biYPelsPerMeter = 0;
info.biClrUsed = 1 << info.biBitCount; // pow(2, info.biBitCount);
info.biClrImportant = 0;
header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);
header.bfSize = header.bfOffBits + info.biSizeImage;
if (info.biBitCount <= 8) {
header.bfOffBits += info.biClrUsed * sizeof(RGBQUAD);
colors.resize(1 << info.biBitCount);
}
data.resize(absHeight);
for (uint32_t i = 0; i < data.size(); ++i)
data[i].resize(bytesWidth);
}
void BMP::read(const char* path) {
std::fstream file(path, std::ios::in | std::ios::binary);
if (!file.is_open())
return;
file.read((char*) &header, sizeof(BITMAPFILEHEADER));
if (header.bfType != 0x4d42)
return;
file.read((char*) &info, sizeof(BITMAPINFO));
if (info.biSize != 40 || info.biCompression != 0 || info.biPlanes != 1)
return;
if (info.biBitCount <= 8) {
colors.resize(1 << info.biBitCount); // info.biClrUsed can be 0
file.read((char*) &colors[0], colors.size() * sizeof(RGBQUAD));
}
file.seekg(header.bfOffBits, std::ios::beg);
flip = (info.biHeight > 0) ? (info.biHeight - 1) : 0;
absHeight = abs(info.biHeight);
bytesWidth = (info.biWidth * info.biBitCount + 7) / 8; // width in bytes rounded up
padding = (4 - (bytesWidth % 4)) % 4;
data.resize(absHeight);
for (uint32_t i = 0; i < data.size(); ++i)
data[i].resize(bytesWidth);
for (int32_t row = 0; row < absHeight; ++row) {
/*
std::vector<uint8_t> scanLine(bytesWidth);
file.read((char*) &scanLine[0], bytesWidth);
//for (uint16_t col = 0; col < bytesWidth; ++col)
//scanLine.push_back(file.get());
data.insert(data.begin(), scanLine);
*/
file.read((char*) &data[(int32_t)abs(flip - row)][0], bytesWidth);
file.seekg(padding, std::ios::cur);
}
file.close();
}
void BMP::write(const char* path) {
std::fstream file(path, std::ios::out | std::ios::binary);
if (!file.is_open())
return;
file.write((char*) &header, sizeof(BITMAPFILEHEADER));
file.write((char*) &info, sizeof(BITMAPINFO));
if (info.biBitCount <= 8)
file.write((char*) &colors[0], colors.size() * sizeof(RGBQUAD));
for (int32_t row = 0; row < absHeight; ++row) {
file.write((char*) &data[(int32_t)abs(flip - row)][0], bytesWidth);
/*
for (uint16_t col = 0; col < bytesWidth; ++col)
file.put(data[(int32_t)abs(flip - row)][col]);
*/
for (uint8_t i = 0; i < padding; ++i)
file.put(0x00);
}
file.close();
}
void BMP::setPixel(int32_t x, int32_t y, uint8_t value) {
switch (bitCount) {
case 1:
// 8 pixels per byte
/*
The pixel values are stored in each bit,
with the first (left-most) pixel in the most-significant bit of the first byte
*/
if (value & 1) {
data[y][x >> 3] |= 0x80 >> (x & 7);
//data[y][x >> 3] |= 1 << (7 - (x & 7));
//data[y][x >> 3] |= 1 << (~x & 7); // (1 * (~x & 7))
}
else {
data[y][x >> 3] &= ~(0x80 >> (x & 7));
//data[y][x >> 3] &= ~(1 << (7 - (x & 7)));
//data[y][x >> 3] &= ~(1 << (~x & 7)); // (1 * (~x & 7))
}
return;
case 2:
// 4 pixels per 1 byte, the left-most pixel being in the two most significant bits
for (uint8_t i = 0; i < bitCount; ++i) {
if (value & (1 << i)) {
data[y][x >> 2] |= (1 << i) << (6 - (2 * (x & 3)));
//data[y][x >> 2] |= (1 << i) << (2 * (~x & 3));
}
else {
data[y][x >> 2] &= ~((1 << i) << (6 - (2 * (x & 3))));
//data[y][x >> 2] &= ~((1 << i) << (2 * (~x & 3)));
}
}
return;
case 4:
// 2 pixels per 1 byte, the left-most pixel being in the more significant nibble
for (uint8_t i = 0; i < bitCount; ++i) {
if (value & (1 << i)) {
data[y][x >> 1] |= (1 << i) << (4 - (4 * (x & 1)));
//data[y][x >> 1] |= (1 << i) << (4 * (~x & 1));
}
else {
data[y][x >> 1] &= ~((1 << i) << (4 - (4 * (x & 1))));
//data[y][x >> 1] &= ~((1 << i) << (4 * (~x & 1)));
}
}
return;
case 8:
// 1 pixel per 1 byte
data[y][x] = value;
return;
default: return;
}
}
uint8_t BMP::readPixel(int32_t x, int32_t y) const {
switch (bitCount) {
case 1:
return (data[y][x >> 3] >> (7 - (x & 7))) & 1;
case 2:
return (data[y][x >> 2] >> (6 - (2 * (x & 3)))) & 0b11;
case 4:
return (data[y][x >> 1] >> (4 - (4 * (x & 1)))) & 0b1111;
case 8:
return data[y][x];
default: return 0;
}
}
const BITMAPFILEHEADER &BMP::getHeader() const {
return header;
}
const BITMAPINFO &BMP::getInfo() const {
return info;
}
int32_t BMP::getAbsHeight() const {
return absHeight;
}
int32_t BMP::getFlip() const {
return flip;
}
uint16_t BMP::getBytesWidth() const {
return bytesWidth;
}
std::vector<RGBQUAD> &BMP::getColors() {
return colors;
}
std::vector<std::vector<uint8_t>> &BMP::getData() {
return data;
}