-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMPArrayConvert.cpp
More file actions
165 lines (123 loc) · 4.33 KB
/
Copy pathBMPArrayConvert.cpp
File metadata and controls
165 lines (123 loc) · 4.33 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
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
/*
* Created December 2020
* Author: Luke Sequeira
*
* Copyright (c) 2020 Luke Sequeira
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
typedef unsigned char ubyte; //unsigned char
typedef unsigned int uint; //unsigned int
typedef std::vector<std::vector<uint>> i_mat; //integer matrix
typedef std::vector<uint> i_vec; //integer array
void printList(i_mat arrs){
for(int i = 0; i < arrs.size(); i++){
for(int j = 0; j < (arrs[i]).size(); j++){
std::cout << arrs[i][j] << ", ";
}
std::cout << std::endl;
}
return;
}
void intTOChar(ubyte* out, size_t in, int bytes){
for (int i = 0; i < bytes; i++){
out[i] = in;
in = in >> 8; //move a over 8 bits
}
}
bool arrayToBMP(std::string filePath, i_mat r, i_mat g, i_mat b){ //takes pixel arrays and converts it to a BMP file.
std::ofstream writeBMP;
writeBMP.open(filePath); //Open file
//write file header (BITMAPFILEHEADER) -> all data is little endian except 'BM' header (14 bytes)
writeBMP << 'B' << 'M'; //write BM header to file (indicating this is a bitmap file) (2 bytes)
//write file size (4 bytes)
uint32_t file_size = 54 + (3 * r.size() * r[0].size()); //there are 54 bytes in the header section and and 3 * len * width bytes in raw data section
ubyte* cfs = new ubyte[4];
intTOChar(cfs, file_size, 4);
for (int i = 0; i < 4; i++)
{
writeBMP << cfs[i];
}
delete[] cfs;
//reserved bytes dependant on the program. Initialized to zero (2 * 2 bytes = 4 bytes)
for (int i = 0; i < 4; i++)
{
writeBMP << (ubyte) 0; //write 4 zeros
}
//offset (distance from first byte to data. Equal to 54 == 0x36 by default) (4 bytes)
writeBMP << (ubyte) 54;
for (int i = 0; i < 3; i++)
{
writeBMP << (ubyte) 0; //write 3 zeros
}
//DIB header (BITMAPINFOHEADER) - Information header (40 bytes)
//size of Info header (4 bytes)
writeBMP << (ubyte) 40; //info header is 40 bytes
for (int i = 0; i < 3; i++)
{
writeBMP << (ubyte) 0; //write 3 zeros
}
//width of file in pixels (4 bytes).
//Followed by height of file in pixels (4 bytes)
uint32_t width = r.size();
uint32_t height = r[0].size();
ubyte *cwd = new ubyte[4]; //width char array
ubyte *chd = new ubyte[4]; //height char array
intTOChar(cwd, width, 4);
intTOChar(chd, height, 4);
for (int i = 0; i < 4; i++)
{
writeBMP << cwd[i];
}
for (int i = 0; i < 4; i++)
{
writeBMP << chd[i];
}
//Planes == 1 (2 bytes)
writeBMP << (ubyte) 1;
writeBMP << (ubyte) 0;
//Bits per Pixel. We set it to 24 for bit RGB color (2 bytes)
writeBMP << (ubyte) 24; //24 bit RGB color (~16M possible colors)
writeBMP << (ubyte) 0;
//Compression Used. Usually set to 0 (no compression) (4 bytes)
for (int i = 0; i < 4; i++)
{
writeBMP << (ubyte) 0; //write 4 zeros
}
//Compressed Image Size (can be set to 0 if compression == 0) (4 bytes)
for (int i = 0; i < 4; i++)
{
writeBMP << (ubyte) 0; //write 4 zeros
}
//The following; Pixels per meter X, Pixels per meter Y, Num Colors Used, and num important colors will all be initialized to 0. Each is 4 bytes (4 * 4 bytes = 16 bytes)
for (int i = 0; i < 16; i++)
{
writeBMP << (ubyte) 0; //write 4 zeros
}
//Pixel Raw Data (This must be padded to make data a multiple of 4 bytes) (bottom to top; left to right)
//Data is in BGR format
//Calculate padding
int padding = (4 - ((r[0].size() * 3) % 4)) % 4;
//write data
for (int i = r.size() - 1; i >= 0; i--) //bottom to top
{
for (int j = 0; j < r[0].size(); j++) //left to right
{
writeBMP << (ubyte) (b[i][j]); //write blue
writeBMP << (ubyte) (g[i][j]); //write green
writeBMP << (ubyte) (r[i][j]); //write red
}
for (int k = 0; k < padding; k++)
{
writeBMP << (ubyte) 0; //pad with appropriate amount of zeros
}
}
return true; //successful
}
int main(){
i_mat rc = {{255,255,255,0,0,0,0},{255,255,255,0,0,0,0},{255,255,255,0,0,0,0},{255,255,255,0,0,0,0},{255,255,255,0,0,0,0},{255,255,255,0,0,0,0},{255,255,255,0,0,0,0}};
arrayToBMP("test.bmp", rc, rc, rc);
return 0;
}