-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp.cpp
More file actions
63 lines (53 loc) · 1.99 KB
/
bmp.cpp
File metadata and controls
63 lines (53 loc) · 1.99 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
#include "bmp.h"
#include "math.h"
namespace bmp {
struct __attribute__((packed)) BmpHeader {
uint16_t signature;
uint32_t size;
uint16_t res1;
uint16_t res2;
uint32_t offset;
};
struct __attribute__((packed)) DibHeader {
uint32_t size;
int32_t width;
int32_t height;
uint16_t numColorPlanes;
uint16_t numBitsPerPixel;
uint32_t compression;
uint32_t imageSize;
int32_t horResolution;
int32_t verResolution;
uint32_t numColors;
uint32_t numImportantColors;
};
Image read(std::ifstream& in) {
BmpHeader header{};
DibHeader dibHeader{};
in.read((char *) &header, sizeof(header));
in.read((char *) &dibHeader, sizeof(dibHeader));
if (dibHeader.size != 40) {
throw std::invalid_argument("Unsupported DIB type.");
}
int skip = header.offset - header.size - dibHeader.size;
in.ignore(skip);
unsigned int channels = dibHeader.numBitsPerPixel / 8u;
int rowSize = ((dibHeader.width * dibHeader.numBitsPerPixel + 31) / 32) * 4;
int rowSkip = rowSize - (dibHeader.width * dibHeader.numBitsPerPixel / 8);
std::vector<uint8_t> data;
data.resize(dibHeader.height * dibHeader.width * channels);
uint8_t pixel;
for (unsigned int row = 0; row < dibHeader.height; row++) {
for (unsigned int col = 0; col < dibHeader.width; col++) {
for (unsigned int channel = 0; channel < channels; channel++) {
in.read((char *) &pixel, 1);
unsigned int index = show::math::index(dibHeader.width, channels,
dibHeader.height - 1 - row, col, channel);
data[index] = pixel;
}
}
in.ignore(rowSkip);
}
return Image(dibHeader.height, dibHeader.width, channels, std::move(data));
}
}