-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMP_export.cpp
76 lines (57 loc) · 1.17 KB
/
BMP_export.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
//
// Created by marc on 13/08/2021.
//
struct col {
char r, g, b;
};
const int headerSize = 14;
const int DIBSize = 40;
FILE* fp;
inline void printInt(int v) {
char* cp = (char*) (&v);
fputc(*(cp + 0), fp);
fputc(*(cp + 1), fp);
fputc(*(cp + 2), fp);
fputc(*(cp + 3), fp);
}
inline void printInt16(int v) {
char* cp = (char*) (&v);
fputc(*(cp + 0), fp);
fputc(*(cp + 1), fp);
}
inline void printBIH(int width, int height, int pixelsSize) {
printInt(DIBSize);
printInt(width);
printInt(height);
printInt16(1);
printInt16(24);
printInt(0);
printInt(pixelsSize);
printInt(0);
printInt(0);
printInt(0);
printInt(0);
}
inline void dumpPixels(col* px, int num) {
for (int i = 0; i < num; i++) {
col pix = px[i];
fputc(pix.b, fp);
fputc(pix.g, fp);
fputc(pix.r, fp);
}
}
void export_image(col* pixels, int width, int height) {
const int pixelsSize = width*height*3;
fp = fopen("test.bmp", "wb");
fputc('B', fp);
fputc('M', fp);
printInt(headerSize + DIBSize + pixelsSize);
fputc(0, fp);
fputc(0, fp);
fputc(0, fp);
fputc(0, fp);
printInt(headerSize + DIBSize);
printBIH(width, height, pixelsSize);
dumpPixels(pixels, width*height);
fclose(fp);
}