-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMP_lab_1.cpp
More file actions
137 lines (112 loc) · 5.31 KB
/
Copy pathBMP_lab_1.cpp
File metadata and controls
137 lines (112 loc) · 5.31 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
#include "classbmp.h"
BMP::BMP(const char* fname) {
read(fname);
}
//считывание файла
void BMP::read(const char* fname) {
std::ifstream inp{ fname, std::ios_base::binary };
if (inp) {
inp.read((char*)&file_header, sizeof(file_header));
if (file_header.file_type != 0x4D42) {
throw std::runtime_error("Error!");
}
inp.read((char*)&bmp_info_header, sizeof(bmp_info_header));
//процесс перехода к пикселям
inp.seekg(file_header.offset_data, inp.beg);
if (bmp_info_header.bit_count == 24) {
bmp_info_header.size = sizeof(BMPInfoHeader);
file_header.offset_data = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
}
file_header.file_size = file_header.offset_data;
if (bmp_info_header.height < 0) {
throw std::runtime_error("Error");
}
data.resize(bmp_info_header.width * bmp_info_header.height * bmp_info_header.bit_count / 8);
//чётность отступа
if (bmp_info_header.width % 4 == 0) {
inp.read((char*)data.data(), data.size());
file_header.file_size += static_cast<uint32_t>(data.size());
}
else {
uint32_t row_stride = bmp_info_header.width * bmp_info_header.bit_count / 8;
uint32_t align_stride = 4;
uint32_t new_stride = bmp_info_header.width * bmp_info_header.bit_count / 8;
while (new_stride % align_stride != 0) {
new_stride++;
}
std::vector<uint8_t> padding_row(new_stride - row_stride);
for (int y = 0; y < bmp_info_header.height; ++y) {
inp.read((char*)(data.data() + row_stride * y), row_stride);
inp.read((char*)padding_row.data(), padding_row.size());
}
file_header.file_size += static_cast<uint32_t>(data.size()) + bmp_info_header.height * static_cast<uint32_t>(padding_row.size());
}
}
else {
throw std::runtime_error("Unable to open the input image file.");
}
}
//вот здесь записываются данные в файл
void BMP::write(const char* fname) {
std::ofstream of{ fname, std::ios_base::binary };
if (of) {
if (bmp_info_header.bit_count == 24) {
if (bmp_info_header.width % 4 == 0) {
of.write((const char*)&file_header, sizeof(file_header));
of.write((const char*)&bmp_info_header, sizeof(bmp_info_header));
of.write((const char*)data.data(), data.size());
}
else {
uint32_t align_stride = 4;
uint32_t new_stride = bmp_info_header.width * bmp_info_header.bit_count / 8;
while (new_stride % align_stride != 0) {
new_stride++;
}
std::vector<uint8_t> padding_row(new_stride - bmp_info_header.width * bmp_info_header.bit_count / 8);
of.write((const char*)&file_header, sizeof(file_header));
of.write((const char*)&bmp_info_header, sizeof(bmp_info_header));
for (int y = 0; y < bmp_info_header.height; ++y) {
of.write((const char*)(data.data() + bmp_info_header.width * bmp_info_header.bit_count / 8 * y), bmp_info_header.width * bmp_info_header.bit_count / 8);
of.write((const char*)padding_row.data(), padding_row.size());
}
}
}
else {
throw std::runtime_error("Only 24 bit format file");
}
}
else {
throw std::runtime_error("Can`t open file.");
}
}
//здесь показан поворот на 90 градусов по часовой стрелке
void BMP::rotate_clockwise_90() {
std::vector<uint8_t> new_data(data.size());
for (int y = 0; y < bmp_info_header.height; ++y) {
for (int x = 0; x < bmp_info_header.width; ++x) {
uint32_t src_index = 3 * (y * bmp_info_header.width + x);
uint32_t dest_index = 3 * ((bmp_info_header.width - x - 1) * bmp_info_header.height + y);
new_data[dest_index] = data[src_index];
new_data[dest_index + 1] = data[src_index + 1];
new_data[dest_index + 2] = data[src_index + 2];
}
}
data = std::move(new_data);
//изменение высоты и ширины
std::swap(bmp_info_header.width, bmp_info_header.height);
}
//здесь показан поворот на 90 градусов против часовой стрелки
void BMP::rotate_counterclockwise_90() {
std::vector<uint8_t> new_data(data.size());
for (int y = 0; y < bmp_info_header.height; ++y) {
for (int x = 0; x < bmp_info_header.width; ++x) {
uint32_t src_index = 3 * (y * bmp_info_header.width + x);
uint32_t dest_index = 3 * (x * bmp_info_header.height + (bmp_info_header.height - y - 1));
new_data[dest_index] = data[src_index];
new_data[dest_index + 1] = data[src_index + 1];
new_data[dest_index + 2] = data[src_index + 2];
}
}
data = std::move(new_data);
std::swap(bmp_info_header.width, bmp_info_header.height);
}