-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.cpp
79 lines (69 loc) · 2.99 KB
/
bitmap.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
77
78
79
//
// Created by Timofey Cherlenok on 19.03.2023.
//
#include "bitmap.h"
#include <stdio.h>
BitmapReaderWriter::BitmapReaderWriter(const char* file) {
exception_ = Status::OK;
FILE* f = fopen(file, "rb");
if (f == NULL) {
exception_ = Status::READ_FILE;
return;
}
fread(info_, sizeof(unsigned char), INFO_SIZE, f);
if (info_[0] != 'B' || info_[1] != 'M') {
exception_ = Status::NOT_BMP;
return;
}
int width = *reinterpret_cast<int*>(&info_[WIDTH_ST]);
int height = *reinterpret_cast<int*>(&info_[HEIGHT_ST]);
bitmap_.Resize(height, width, {0, 0, 0});
size_t row_padded = (width * 3 + 3) & (~3);
unsigned char* data = new unsigned char[row_padded];
for (size_t i = 0; i < height; ++i) {
fread(data, sizeof(unsigned char), row_padded, f);
for (size_t j = 0; j < width * 3; j += 3) {
bitmap_[bitmap_.GetRowsNum() - 1 - i][j / 3].r = data[j + 2];
bitmap_[bitmap_.GetRowsNum() - 1 - i][j / 3].g = data[j + 1];
bitmap_[bitmap_.GetRowsNum() - 1 - i][j / 3].b = data[j];
}
}
delete[] data;
fclose(f);
}
void BitmapReaderWriter::Write(const char* file) {
try {
size_t height = bitmap_.GetRowsNum();
size_t width = bitmap_.GetColsNum();
size_t row_padded = (width * 3 + 3) & (~3);
FILE* image_file = fopen(file, "wb");
size_t file_size = INFO_SIZE + (row_padded * height);
info_[FILE_SIZE_ST] = static_cast<unsigned char>(file_size);
info_[FILE_SIZE_ST + 1] = static_cast<unsigned char>(file_size >> MOVE);
info_[FILE_SIZE_ST + 2] = static_cast<unsigned char>(file_size >> MOVE * 2);
info_[FILE_SIZE_ST + 3] = static_cast<unsigned char>(file_size >> MOVE * 3);
info_[WIDTH_ST] = static_cast<unsigned char>(width);
info_[WIDTH_ST + 1] = static_cast<unsigned char>(width >> MOVE);
info_[WIDTH_ST + 2] = static_cast<unsigned char>(width >> MOVE * 2);
info_[WIDTH_ST + 3] = static_cast<unsigned char>(width >> MOVE * 3);
info_[HEIGHT_ST] = static_cast<unsigned char>(height);
info_[HEIGHT_ST + 1] = static_cast<unsigned char>(height >> MOVE);
info_[HEIGHT_ST + 2] = static_cast<unsigned char>(height >> MOVE * 2);
info_[HEIGHT_ST + 3] = static_cast<unsigned char>(height >> MOVE * 3);
fwrite(info_, 1, INFO_SIZE, image_file);
unsigned char* data = new unsigned char[row_padded];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width * 3; j += 3) {
data[j + 2] = bitmap_[bitmap_.GetRowsNum() - 1 - i][j / 3].r;
data[j + 1] = bitmap_[bitmap_.GetRowsNum() - 1 - i][j / 3].g;
data[j] = bitmap_[bitmap_.GetRowsNum() - 1 - i][j / 3].b;
}
fwrite(data, sizeof(unsigned char), row_padded, image_file);
}
delete[] data;
fclose(image_file);
} catch (std::exception e) {
exception_ = Status::WRITE_FILE;
return;
}
}