-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage.hpp
More file actions
56 lines (50 loc) · 1.82 KB
/
image.hpp
File metadata and controls
56 lines (50 loc) · 1.82 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
#include<string>
#include<fstream>
inline void save_bmp( const float *pixels, const int width, const int height, const std::string &filename )
{
std::ofstream ofs( filename, std::ios::binary );
const int row_bytes = ((width * 3 + 3) >> 2) << 2;
const short bfType = 0x4d42;
ofs.write((char*)&bfType, 2);
const int bfSize = 14 + 40 + row_bytes * height;
ofs.write((char*)&bfSize, 4);
const short bfReserved1 = 0;
ofs.write((char*)&bfReserved1, 2);
const short bfReserved2 = 0;
ofs.write((char*)&bfReserved2, 2);
const int bfOffBits = 14 + 40;
ofs.write((char*)&bfOffBits, 4);
const int biSize = 40;
ofs.write((char*)&biSize, 4);
const int biWidth = width;
ofs.write((char*)&biWidth, 4);
const int biHeight = height;
ofs.write((char*)&biHeight, 4);
const short biPlanes = 1;
ofs.write((char*)&biPlanes, 2);
const short biBitCount = 24;
ofs.write((char*)&biBitCount, 2);
const int biCompression = 0;
ofs.write((char*)&biCompression, 4);
const int biSizeImage = 0;
ofs.write((char*)&biSizeImage, 4);
const int biXPelsPerMeter = 0;
ofs.write((char*)&biXPelsPerMeter, 4);
const int biYPelsPerMeter = 0;
ofs.write((char*)&biYPelsPerMeter, 4);
const int biClrUsed = 0;
ofs.write((char*)&biClrUsed, 4);
const int biClrImportant = 0;
ofs.write((char*)&biClrImportant, 4);
std::vector<unsigned char> scanline(
row_bytes
);
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
scanline[3 * x + 0] = std::max( 0, std::min( 255, int( 255 * pow( pixels[ 3 * ( y * width + x ) + 2 ], 1.f / 2.2f ) ) ) );
scanline[3 * x + 1] = std::max( 0, std::min( 255, int( 255 * pow( pixels[ 3 * ( y * width + x ) + 1 ], 1.f / 2.2f ) ) ) );
scanline[3 * x + 2] = std::max( 0, std::min( 255, int( 255 * pow( pixels[ 3 * ( y * width + x ) + 0 ], 1.f / 2.2f ) ) ) );
}
ofs.write((char*)scanline.data(), row_bytes);
}
}