| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "fatehov_k_gaussian/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <functional> | ||
| 8 | #include <thread> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "fatehov_k_gaussian/common/include/common.hpp" | ||
| 12 | #include "util/include/util.hpp" | ||
| 13 | |||
| 14 | namespace fatehov_k_gaussian { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | 17400 | float ConvolvePixel(const Image &image, const std::vector<float> &kernel, int kernel_size, int half, int y_coord, | |
| 19 | int x_coord, int c_coord) { | ||
| 20 | 17400 | const int w = static_cast<int>(image.width); | |
| 21 | 17400 | const int h = static_cast<int>(image.height); | |
| 22 | 17400 | const int ch = static_cast<int>(image.channels); | |
| 23 | float res = 0.0F; | ||
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 121800 times.
✓ Branch 1 taken 17400 times.
|
139200 | for (int ky = -half; ky <= half; ++ky) { |
| 26 |
2/2✓ Branch 0 taken 852600 times.
✓ Branch 1 taken 121800 times.
|
974400 | for (int kx = -half; kx <= half; ++kx) { |
| 27 | 852600 | const int ny = std::clamp(y_coord + ky, 0, h - 1); | |
| 28 | 852600 | const int nx = std::clamp(x_coord + kx, 0, w - 1); | |
| 29 | 852600 | const float weight = kernel[((ky + half) * kernel_size) + (kx + half)]; | |
| 30 | 852600 | res += static_cast<float>(image.data[((ny * w + nx) * ch) + c_coord]) * weight; | |
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | 17400 | return res; | |
| 35 | } | ||
| 36 | |||
| 37 | 5800 | void ProcessPixel(const Image &src, Image &dst, const std::vector<float> &kernel, int kernel_size, int half, | |
| 38 | int y_coord, int x_coord) { | ||
| 39 | 5800 | const int ch = static_cast<int>(src.channels); | |
| 40 | 5800 | const int w = static_cast<int>(src.width); | |
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 17400 times.
✓ Branch 1 taken 5800 times.
|
23200 | for (int c_coord = 0; c_coord < ch; ++c_coord) { |
| 43 | 17400 | const float res = ConvolvePixel(src, kernel, kernel_size, half, y_coord, x_coord, c_coord); | |
| 44 | 17400 | dst.data[((y_coord * w + x_coord) * ch) + c_coord] = static_cast<uint8_t>(std::clamp(res, 0.0F, 255.0F)); | |
| 45 | } | ||
| 46 | 5800 | } | |
| 47 | |||
| 48 | 60 | void ProcessRows(const Image &src, Image &dst, const std::vector<float> &kernel, int kernel_size, int half, | |
| 49 | int row_begin, int row_end) { | ||
| 50 | 60 | const int w = static_cast<int>(src.width); | |
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 360 times.
✓ Branch 1 taken 60 times.
|
420 | for (int y_coord = row_begin; y_coord < row_end; ++y_coord) { |
| 53 |
2/2✓ Branch 0 taken 5800 times.
✓ Branch 1 taken 360 times.
|
6160 | for (int x_coord = 0; x_coord < w; ++x_coord) { |
| 54 | 5800 | ProcessPixel(src, dst, kernel, kernel_size, half, y_coord, x_coord); | |
| 55 | } | ||
| 56 | } | ||
| 57 | 60 | } | |
| 58 | |||
| 59 | } // namespace | ||
| 60 | |||
| 61 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | FatehovKGaussianSTL::FatehovKGaussianSTL(const InType &in) { |
| 62 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 63 | GetInput() = in; | ||
| 64 | 24 | } | |
| 65 | |||
| 66 | 24 | bool FatehovKGaussianSTL::ValidationImpl() { | |
| 67 | const auto &input = GetInput(); | ||
| 68 |
4/8✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 24 times.
|
24 | return input.image.width > 0 && input.image.height > 0 && input.image.channels > 0 && !input.image.data.empty() && |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | input.sigma > 0.0F; |
| 70 | } | ||
| 71 | |||
| 72 | 24 | bool FatehovKGaussianSTL::PreProcessingImpl() { | |
| 73 | const auto &input = GetInput(); | ||
| 74 | 24 | const float sigma = input.sigma; | |
| 75 | |||
| 76 | 24 | kernel_size_ = (2 * static_cast<int>(std::ceil(3.0F * sigma))) + 1; | |
| 77 | 24 | kernel_.resize(static_cast<std::size_t>(kernel_size_) * kernel_size_); | |
| 78 | |||
| 79 | 24 | const int half = kernel_size_ / 2; | |
| 80 | 24 | const float two_sigma_sq = 2.0F * sigma * sigma; | |
| 81 | float sum = 0.0F; | ||
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 24 times.
|
192 | for (int i = -half; i <= half; ++i) { |
| 84 |
2/2✓ Branch 0 taken 1176 times.
✓ Branch 1 taken 168 times.
|
1344 | for (int j = -half; j <= half; ++j) { |
| 85 | 1176 | const float val = std::exp(-(static_cast<float>((i * i) + (j * j))) / two_sigma_sq); | |
| 86 | 1176 | kernel_[((i + half) * kernel_size_) + (j + half)] = val; | |
| 87 | 1176 | sum += val; | |
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 1176 times.
✓ Branch 1 taken 24 times.
|
1200 | for (float &val : kernel_) { |
| 92 | 1176 | val /= sum; | |
| 93 | } | ||
| 94 | |||
| 95 | 24 | GetOutput() = Image(input.image.width, input.image.height, input.image.channels); | |
| 96 | |||
| 97 | 24 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | 24 | bool FatehovKGaussianSTL::RunImpl() { | |
| 101 | const auto &input = GetInput(); | ||
| 102 | auto &output = GetOutput(); | ||
| 103 | 24 | const int h = static_cast<int>(input.image.height); | |
| 104 | 24 | const int half = kernel_size_ / 2; | |
| 105 | 24 | const int kernel_size = kernel_size_; | |
| 106 | 24 | const auto &kernel = kernel_; | |
| 107 | 24 | const int num_threads = ppc::util::GetNumThreads(); | |
| 108 | |||
| 109 | 24 | std::vector<std::thread> threads(num_threads); | |
| 110 | 24 | const int rows_per_thread = h / num_threads; | |
| 111 | 24 | const int remainder = h % num_threads; | |
| 112 | |||
| 113 | 24 | int row_begin = 0; | |
| 114 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (int idx = 0; idx < num_threads; ++idx) { |
| 115 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 18 times.
|
60 | const int row_end = row_begin + rows_per_thread + (idx < remainder ? 1 : 0); |
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | threads[idx] = std::thread(ProcessRows, std::cref(input.image), std::ref(output), std::cref(kernel), kernel_size, |
| 117 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | half, row_begin, row_end); |
| 118 | 60 | row_begin = row_end; | |
| 119 | } | ||
| 120 | |||
| 121 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (auto &thread : threads) { |
| 122 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | thread.join(); |
| 123 | } | ||
| 124 | |||
| 125 | 24 | return true; | |
| 126 | 24 | } | |
| 127 | |||
| 128 | 24 | bool FatehovKGaussianSTL::PostProcessingImpl() { | |
| 129 | 24 | return true; | |
| 130 | } | ||
| 131 | |||
| 132 | } // namespace fatehov_k_gaussian | ||
| 133 |