-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.cc
More file actions
77 lines (63 loc) · 1.84 KB
/
bitmap.cc
File metadata and controls
77 lines (63 loc) · 1.84 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
#include <cstdint>
#include "bitmap.h"
namespace {
// The magic number is separate from the BitmapFileheader struct as it
// causes alignment issues. BitmapMagic should be read/written first
// followed by BitmapFileHeader.
struct BitmapMagic {
static const unsigned char magic[2];
};
const unsigned char BitmapMagic::magic[] = {'B', 'M'};
struct BitmapFileHeader {
uint32_t file_size;
uint16_t reserved1;
uint16_t reserved2;
uint32_t bmp_offset;
};
struct BitmapInfoHeader {
uint32_t header_size;
int32_t width;
int32_t height;
uint16_t num_color_planes;
uint16_t bits_per_pixel;
uint32_t compression_method;
uint32_t image_size;
int32_t horizontal_resolution;
int32_t vertical_resolution;
uint32_t num_colors;
uint32_t num_important_colors;
};
const int kPixelSize = 4; // (r, g, b, unused).
template <typename T>
inline bool WriteBytes(/* file */ const T& data) {
const unsigned char* p = reinterpret_cast<const unsigned char*>(&data);
for (decltype(sizeof(T)) i = 0; i < sizeof(T); ++i) {
/*Write(*/*p++/*)*/;
}
return true;
}
} // namespace
namespace raytracer {
bool BitmapFile::WriteToFile(const std::string& filename) const {
// Open file, if failed return false;
// contents_.reserve((width_ + padding) * height_);
if (width_ == 0 || height_ == 0)
return false;
BitmapMagic bitmap_magic;
// Must be zero-padded to end of 32-bit (4-byte) boundary.
const int padding = (width_ % 4) == 0 ? 0 : 4 - (width_ % 4);
const int actual_width = width_ + padding;
BitmapFileHeader bitmap_file_header = {
actual_width * height_ * kPixelSize,
0, 0
};
for (int i = 0; i < actual_width; ++i) {
if (i >= width_) {
// write 0
WriteBytes(0);
continue;
}
}
return true;
}
} // namespace raytracer