-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblur_filter.cpp
More file actions
64 lines (61 loc) · 2.19 KB
/
Copy pathblur_filter.cpp
File metadata and controls
64 lines (61 loc) · 2.19 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
#include "blur_filter.hpp"
#include <cmath>
#include "constants.hpp"
double Gaussian(double x, double sigma) {
return std::exp(-std::pow(x, 2) / 2 / sigma / sigma) / std::sqrt(2 * M_PI) / sigma;
}
BlurFilter::BlurFilter(double sigma) : sigma_(sigma) {
}
Picture BlurFilter::operator()(const Picture& pic) const {
std::vector<double> kernel{};
double sum = 0;
ssize_t kernel_size = static_cast<ssize_t>(BLUR_DELTA * sigma_) + 1;
for (size_t i = 0; i < static_cast<size_t>(kernel_size); ++i) {
double x = Gaussian(static_cast<double>(i), sigma_);
kernel.push_back(x);
sum += x;
}
sum -= kernel[0];
sum *= 2;
sum += kernel[0];
for (auto& elem: kernel) {
elem /= sum;
}
Picture res = pic;
for (ssize_t x0 = 0; static_cast<size_t>(x0) < res.Height(); ++x0) {
for (ssize_t y0 = 0; static_cast<size_t>(y0) < res.Width(); ++y0) {
Pixel p = {0.0, 0.0, 0.0};
for (ssize_t x = x0 - kernel_size; x <= x0 + kernel_size; ++x) {
ssize_t index = std::max(0L, std::min(static_cast<ssize_t>(res.Height() - 1), x));
p += pic[{index, y0}] * kernel[std::abs(x - x0)];
if (p > Pixel(1)) {
p = Pixel(1);
break;
}
}
res[{x0, y0}] = p;
if (res[{x0, y0}] > Pixel(1)) {
res[{x0, y0}] = Pixel(1);
}
}
}
Picture res_res = res;
for (ssize_t x0 = 0; static_cast<size_t>(x0) < res.Height(); ++x0) {
for (ssize_t y0 = 0; static_cast<size_t>(y0) < res.Width(); ++y0) {
Pixel p = {0.0, 0.0, 0.0};
for (ssize_t y = y0 - kernel_size; y <= y0 + kernel_size; ++y) {
ssize_t index = std::max(0L, std::min(static_cast<ssize_t>(res.Width() - 1), y));
p += res[{x0, index}] * kernel[std::abs(y - y0)];
if (p > Pixel(1)) {
p = Pixel(1);
break;
}
}
res_res[{x0, y0}] = p;
if (res_res[{x0, y0}] > Pixel(1)) {
res_res[{x0, y0}] = Pixel(1);
}
}
}
return res_res;
}