-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp.h
More file actions
75 lines (60 loc) · 2.62 KB
/
bmp.h
File metadata and controls
75 lines (60 loc) · 2.62 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
#pragma once
#include <cstdint>
#include <fstream>
#include <iostream>
using namespace std;
#pragma pack(push, 1) // ensure the structures are packed
struct BMPHeader {
uint8_t file_type[2] = {'B', 'M'}; // always BM
uint32_t file_size; // file size, in bytes
uint32_t reserved = 0; // reserved, always 0
uint32_t data_offset; // where pixel data starts
};
struct BMPInfoHeader {
uint32_t size = sizeof(BMPInfoHeader); // this header size, in bytes
int32_t width; // image width, in pixels
int32_t height; // image height, in pixels
uint16_t num_color_planes = 1; // num of color planes, always 1
uint16_t color_depth = 24; // color depth, 8 bits per BGR colors = 24
uint32_t compression_method = 0; // compression method, uncompressed is 0
uint32_t raw_image_size = 0; // raw bitmap size in bytes, uncompressed is 0
int32_t horizontal_resolution = 2835; // pixel per meter, default 2835 = 72DPI
int32_t vertical_resolution = 2835; // pixel per meter, default 2835 = 72DPI
uint32_t colors_used = 0; // color table entries
uint32_t important_colors = 0; // important colors number, 0 for all
};
#pragma pack(pop)
struct BMPColor {
uint8_t red, green, blue;
};
void save_image(const vector<BMPColor>& image, int32_t width, int32_t height, const string& filename) {
if (width % 4 != 0) {
throw invalid_argument("Image width should be aligned to a multiple of 4");
}
ofstream file(filename, ios::binary);
if (!file.is_open()) {
throw runtime_error("Error opening file: " + filename);
}
BMPInfoHeader info_header;
BMPHeader bmp_header;
info_header.width = width;
info_header.height = height;
info_header.raw_image_size = width * height * 3;
bmp_header.data_offset = info_header.size + sizeof(BMPHeader);
bmp_header.file_size = bmp_header.data_offset + info_header.raw_image_size;
file.write((const char*)&bmp_header, sizeof(bmp_header));
file.write((const char*)&info_header, sizeof(info_header));
for (int32_t y = height - 1; y >= 0; y--) {
for (int32_t x = 0; x < width; x++) {
const BMPColor rgb = image[y * width + x];
// converting RGB image to BGR for bmp file format
file.put(rgb.blue);
file.put(rgb.green);
file.put(rgb.red);
}
}
file.close();
if (!file.good()) {
throw runtime_error("Error writing to file: " + filename);
}
}