-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterApplier.cpp
More file actions
134 lines (124 loc) · 5.51 KB
/
Copy pathFilterApplier.cpp
File metadata and controls
134 lines (124 loc) · 5.51 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
#include "FilterApplier.h"
Image FilterApplier::Apply(const IFilter& filter, Image& image) {
if (filter.GetType() == FilterType::Resize) {
unsigned int width = std::min(static_cast<unsigned int>(filter.GetRules()[0]), image.GetWidth());
unsigned int height = std::min(static_cast<unsigned int>(filter.GetRules()[1]), image.GetHeight());
return Resize(image, width, height);
} else if (filter.GetType() == FilterType::RGB) {
return RGB(filter, image);
} else if (filter.GetType() == FilterType::Matrix) {
return Matrix(image, filter.GetRules(), filter.GetThreshold());
} else if (filter.GetType() == FilterType::Edge) {
Image new_image = RGB(filter, image);
new_image = Matrix(new_image, filter.GetRules(), filter.GetThreshold());
return new_image;
} else if (filter.GetType() == FilterType::Gaus) {
unsigned int width = image.GetWidth();
unsigned int height = image.GetHeight();
Image new_image = Image(width, height);
float sigma = filter.GetThreshold();
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
float r = 0;
float g = 0;
float b = 0;
for (int y_shift : {-2, -1, 0, 1, 2}) {
for (int x_shift : {-2, -1, 0, 1, 2}) {
Color temp = image.GetColor(x + x_shift, y + y_shift);
r += temp.GetR() / (2 * M_PI * sigma * sigma) *
pow(M_E, -(pow(static_cast<int>(x) - static_cast<int>(x_shift), 2) +
pow(static_cast<int>(y) - static_cast<int>(y_shift), 2)) /
(2 * sigma * sigma));
g += temp.GetG() / (2 * M_PI * sigma * sigma) *
pow(M_E, -(pow(static_cast<int>(x) - static_cast<int>(x_shift), 2) +
pow(static_cast<int>(y) - static_cast<int>(y_shift), 2)) /
(2 * sigma * sigma));
b += temp.GetB() / (2 * M_PI * sigma * sigma) *
pow(M_E, -(pow(static_cast<int>(x) - static_cast<int>(x_shift), 2) +
pow(static_cast<int>(y) - static_cast<int>(y_shift), 2)) /
(2 * sigma * sigma));
}
}
new_image.SetColor(Color(r, g, b), x, y);
}
}
return new_image;
} else if (filter.GetType() == FilterType::Mirror) {
unsigned int width = image.GetWidth();
unsigned int height = image.GetHeight();
Image new_image = Image(width, height);
float parametr = filter.GetThreshold();
if (parametr != 0 and parametr != 1) {
std::cerr << "Неправильный параметр фитьтра отзеркаливания";
exit(1);
}
if (parametr == 1) {
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
new_image.SetColor(image.GetColor(x, y), width - 1 - x, y);
}
}
} else {
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
new_image.SetColor(image.GetColor(x, y), x, height - y - 1);
}
}
}
return new_image;
}
std::cerr << "Неправильный тип фильтра";
exit(1);
}
Image FilterApplier::Resize(Image& image, unsigned int width, unsigned int height) {
unsigned int shift = image.GetHeight() - height;
Image new_image = Image(width, height);
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
new_image.SetColor(image.GetColor(x, shift + y), x, y);
}
}
return new_image;
}
Image FilterApplier::RGB(const IFilter& filter, Image& image) {
unsigned int width = image.GetWidth();
unsigned int height = image.GetHeight();
Image new_image = Image(width, height);
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
new_image.SetColor(filter.GetColor(image.GetColor(x, y)), x, y);
}
}
return new_image;
}
Image FilterApplier::Matrix(Image& image, std::vector<float> matrix, float threshold) {
unsigned int width = image.GetWidth();
unsigned int height = image.GetHeight();
Image new_image = Image(width, height);
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
std::vector<Color> temp;
for (int y_shift : {-1, 0, 1}) {
for (int x_shift : {-1, 0, 1}) {
temp.push_back(image.GetColor(x + x_shift, y + y_shift));
}
}
float r = 0;
float g = 0;
float b = 0;
for (int i = 0; i < 9; ++i) {
r += matrix[i] * temp[i].GetR();
g += matrix[i] * temp[i].GetG();
b += matrix[i] * temp[i].GetB();
}
if (threshold == -1) {
new_image.SetColor(Color(r, g, b), x, y);
} else if (r > threshold) {
new_image.SetColor(Color(1, 1, 1), x, y);
} else {
new_image.SetColor(Color(0, 0, 0), x, y);
}
}
}
return new_image;
}