-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
106 lines (91 loc) · 2.96 KB
/
Copy pathImage.cpp
File metadata and controls
106 lines (91 loc) · 2.96 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
#include "Image.h"
int MakeCoordinateValid(int i, int up_bound) {
return std::min(up_bound - 1, std::max(0, i));
}
const BMPFileHeader& Image::GetFileHeader() const {
return file_header_;
}
const BMPImageHeader& Image::GetImageHeader() const {
return image_header_;
}
uint32_t Image::GetOffsetData() const {
return file_header_.offset_data;
}
int32_t Image::GetWidth() const {
return image_header_.width;
}
int32_t Image::GetHeight() const {
return image_header_.height;
}
Color Image::GetPixel(size_t i, size_t j) const {
return pixel_data_[i][j];
}
std::vector<std::vector<Color>> Image::GetPixelData() const {
return pixel_data_;
}
void Image::UpdateFileSize() {
uint32_t new_size = sizeof(file_header_) + sizeof(image_header_) + sizeof(pixel_data_);
new_size += ((4 - GetWidth() % 4) % 4) * GetHeight();
file_header_.file_size = new_size;
}
void Image::SetFileHeader(const BMPFileHeader& file_header) {
file_header_ = file_header;
}
void Image::SetImageHeader(const BMPImageHeader& image_header) {
image_header_ = image_header;
}
void Image::SetHeight(int32_t height) {
if (height > abs(image_header_.height)) {
return;
}
image_header_.height = height;
pixel_data_.resize(height);
UpdateFileSize();
}
void Image::SetWidth(int32_t width) {
if (width > abs(image_header_.width)) {
return;
}
image_header_.width = width;
for (size_t i = 0; i < static_cast<size_t>(image_header_.height); ++i) {
pixel_data_[i].resize(width);
}
UpdateFileSize();
}
void Image::SetPixel(size_t i, size_t j, const Color& color) {
pixel_data_[i][j] = color;
}
void Image::MultiplyByMatrix(const std::vector<std::vector<double>>& matrix, bool normalize) {
std::vector<std::vector<Color>> old_pixel_data = GetPixelData();
int n = static_cast<int>(matrix.size());
int m = static_cast<int>(matrix[0].size());
double sum = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
sum += matrix[i][j];
}
}
for (int i = 0; i < GetHeight(); ++i) {
for (int j = 0; j < GetWidth(); ++j) {
double blue = 0;
double green = 0;
double red = 0;
for (int i1 = 0; i1 < n; ++i1) {
for (int j1 = 0; j1 < m; ++j1) {
int i2 = MakeCoordinateValid(i + i1 - (n - 1) / 2, GetHeight());
int j2 = MakeCoordinateValid(j + j1 - (m - 1) / 2, GetWidth());
Color pixel = old_pixel_data[i2][j2];
blue += pixel.GetBlue() * matrix[i1][j1];
green += pixel.GetGreen() * matrix[i1][j1];
red += pixel.GetRed() * matrix[i1][j1];
}
}
if (normalize) {
blue /= sum;
green /= sum;
red /= sum;
}
SetPixel(i, j, {Standardize(blue), Standardize(green), Standardize(red)});
}
}
}