-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.cpp
More file actions
68 lines (54 loc) · 2.04 KB
/
bitmap.cpp
File metadata and controls
68 lines (54 loc) · 2.04 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
#include "bitmap.h"
#include "iostream"
void StreamStatusChecker(std::ifstream& f) {
if (f.fail()) {
throw std::runtime_error("Could not read file.");
}
}
void StreamStatusChecker(std::ofstream& f) {
if (f.fail()) {
throw std::runtime_error("Could not write to file.");
}
}
void Bitmap::Open(const char* path_to_source) {
std::ifstream f;
f.open(path_to_source, std::ios::in);
StreamStatusChecker(f);
f.read(reinterpret_cast<char*>(&bmp_header_), sizeof(bmp_header_));
StreamStatusChecker(f);
f.read(reinterpret_cast<char*>(&bitmapInfoHeader_), sizeof(bitmapInfoHeader_));
StreamStatusChecker(f);
if (bitmapInfoHeader_.bits_per_pixel != 24 || bitmapInfoHeader_.color_planes != 1) {
throw std::runtime_error("Wrong file format.");
}
rows_num_ = bitmapInfoHeader_.height;
cols_num_ = bitmapInfoHeader_.width;
table_ = AllocateMatrix(rows_num_, cols_num_);
const uint32_t EXTRA = (4 - (bitmapInfoHeader_.width * 3) % 4) % 4;
for (size_t i = 0; i < bitmapInfoHeader_.height; ++i) {
for (size_t j = 0; j < bitmapInfoHeader_.width; ++j) {
Pixel pixel;
f.read(reinterpret_cast<char*>(&pixel), sizeof(pixel));
StreamStatusChecker(f);
this->operator()(i, j) = pixel;
}
f.seekg(EXTRA, std::ios_base::cur);
}
f.close();
}
void Bitmap::Save(const char* path_to_result) {
std::ofstream f;
f.open(path_to_result, std::ios::out);
StreamStatusChecker(f);
f.write(reinterpret_cast<char*>(&bmp_header_), sizeof(bmp_header_));
StreamStatusChecker(f);
f.write(reinterpret_cast<char*>(&bitmapInfoHeader_), sizeof(bitmapInfoHeader_));
const uint32_t EXTRA = (4 - (bitmapInfoHeader_.width * 3) % 4) % 4;
for (size_t i = 0; i < bitmapInfoHeader_.height; ++i) {
for (size_t j = 0; j < bitmapInfoHeader_.width; ++j) {
f.write(reinterpret_cast<char*>(&this->At(i, j)), sizeof(this->At(i, j)));
}
f.seekp(EXTRA, std::ios_base::cur);
}
f.close();
}