-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_filter.cpp
More file actions
37 lines (35 loc) · 1.4 KB
/
matrix_filter.cpp
File metadata and controls
37 lines (35 loc) · 1.4 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
#include "matrix_filter.hpp"
Picture MatrixFilter::operator()(const Picture& pic) const {
Picture res = pic;
for (ssize_t i = 0; i < static_cast<ssize_t>(pic.Height()); ++i) {
for (ssize_t j = 0; j < static_cast<ssize_t>(pic.Width()); ++j) {
Pixel p{0.0, 0.0, 0.0};
for (ssize_t index1 = -1; index1 <= 1; ++index1) {
for (ssize_t index2 = -1; index2 <= 1; ++index2) {
ssize_t i0 = i + index1;
ssize_t j0 = j + index2;
if (i0 < 0) {
i0 = 0;
}
if (i0 >= static_cast<ssize_t>(pic.Height())) {
i0 = static_cast<ssize_t>(pic.Height() - 1);
}
if (j0 < 0) {
j0 = 0;
}
if (j0 >= static_cast<ssize_t>(pic.Width())) {
j0 = static_cast<ssize_t>(pic.Width() - 1);
}
p += pic[{i0, j0}] * matrix_[{1 + index1, 1 + index2}];
}
}
p.red = std::min(1.0, std::max(0.0, p.red));
p.green = std::min(1.0, std::max(0.0, p.green));
p.blue = std::min(1.0, std::max(0.0, p.blue));
res[{i, j}] = p;
}
}
return res;
}
MatrixFilter::MatrixFilter(const Matrix<double>& matrix) : matrix_(matrix) {
}