| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "pankov_gauss_filter/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "pankov_gauss_filter/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace pankov_gauss_filter { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | inline int ClampInt(int v, int lo, int hi) { | ||
| 17 | return std::max(lo, std::min(v, hi)); | ||
| 18 | } | ||
| 19 | |||
| 20 | inline std::uint8_t ClampToByte(int v) { | ||
| 21 |
1/2✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 | v = std::max(0, std::min(v, 255)); |
| 22 | 464 | return static_cast<std::uint8_t>(v); | |
| 23 | } | ||
| 24 | |||
| 25 | constexpr std::array<std::array<int, 3>, 3> kGaussianKernel3x3 = {{{{1, 2, 1}}, {{2, 4, 2}}, {{1, 2, 1}}}}; | ||
| 26 | constexpr int kGaussianDiv = 16; | ||
| 27 | |||
| 28 | 464 | std::uint8_t ConvolveGaussian3x3Clamp(const Image &image, int row, int col, int channel) { | |
| 29 | 464 | const int width = image.width; | |
| 30 | 464 | const int height = image.height; | |
| 31 | 464 | const int channels = image.channels; | |
| 32 | |||
| 33 | int acc = 0; | ||
| 34 |
2/2✓ Branch 0 taken 1392 times.
✓ Branch 1 taken 464 times.
|
1856 | for (std::size_t kernel_row = 0; kernel_row < 3; ++kernel_row) { |
| 35 | 1392 | const int d_row = static_cast<int>(kernel_row) - 1; | |
| 36 | 1392 | const int src_row = ClampInt(row + d_row, 0, height - 1); | |
| 37 |
2/2✓ Branch 0 taken 4176 times.
✓ Branch 1 taken 1392 times.
|
5568 | for (std::size_t kernel_col = 0; kernel_col < 3; ++kernel_col) { |
| 38 | 4176 | const int d_col = static_cast<int>(kernel_col) - 1; | |
| 39 | 4176 | const int src_col = ClampInt(col + d_col, 0, width - 1); | |
| 40 | 4176 | const int weight = kGaussianKernel3x3.at(kernel_row).at(kernel_col); | |
| 41 | 4176 | const std::size_t idx = | |
| 42 | 4176 | ((static_cast<std::size_t>(src_row) * static_cast<std::size_t>(width) + static_cast<std::size_t>(src_col)) * | |
| 43 | 4176 | static_cast<std::size_t>(channels)) + | |
| 44 | 4176 | static_cast<std::size_t>(channel); | |
| 45 | 4176 | acc += weight * static_cast<int>(image.data[idx]); | |
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
|
464 | const int rounded = (acc + (kGaussianDiv / 2)) / kGaussianDiv; |
| 50 | 464 | return ClampToByte(rounded); | |
| 51 | } | ||
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | PankovGaussFilterSEQ::PankovGaussFilterSEQ(const InType &in) { |
| 56 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 57 | InType temp(in); | ||
| 58 | 40 | std::swap(GetInput(), temp); | |
| 59 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
|
80 | GetOutput() = OutType{}; |
| 60 | 40 | } | |
| 61 | |||
| 62 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool PankovGaussFilterSEQ::ValidationImpl() { |
| 63 | const auto &in = GetInput(); | ||
| 64 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (!GetOutput().data.empty()) { |
| 65 | return false; | ||
| 66 | } | ||
| 67 |
3/6✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
|
40 | if (in.width < 0 || in.height < 0 || in.channels < 0) { |
| 68 | return false; | ||
| 69 | } | ||
| 70 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
|
40 | if (in.width == 0 || in.height == 0) { |
| 71 | ✗ | return in.data.empty(); | |
| 72 | } | ||
| 73 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (in.channels == 0) { |
| 74 | return false; | ||
| 75 | } | ||
| 76 | 40 | const auto expected = | |
| 77 | 40 | static_cast<std::size_t>(in.width) * static_cast<std::size_t>(in.height) * static_cast<std::size_t>(in.channels); | |
| 78 | 40 | return in.data.size() == expected; | |
| 79 | } | ||
| 80 | |||
| 81 | 40 | bool PankovGaussFilterSEQ::PreProcessingImpl() { | |
| 82 | const auto &in = GetInput(); | ||
| 83 | auto &out = GetOutput(); | ||
| 84 | 40 | out.width = in.width; | |
| 85 | 40 | out.height = in.height; | |
| 86 | 40 | out.channels = in.channels; | |
| 87 | 40 | const auto total = | |
| 88 | 40 | static_cast<std::size_t>(in.width) * static_cast<std::size_t>(in.height) * static_cast<std::size_t>(in.channels); | |
| 89 | 40 | out.data.assign(total, 0); | |
| 90 | 40 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool PankovGaussFilterSEQ::RunImpl() { |
| 94 | const auto &in = GetInput(); | ||
| 95 | auto &out = GetOutput(); | ||
| 96 | |||
| 97 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | if (in.width == 0 || in.height == 0) { |
| 98 | return true; | ||
| 99 | } | ||
| 100 | |||
| 101 | const int width = in.width; | ||
| 102 | const int height = in.height; | ||
| 103 | 40 | const int channels = in.channels; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 40 times.
|
136 | for (int row = 0; row < height; ++row) { |
| 106 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 96 times.
|
432 | for (int col = 0; col < width; ++col) { |
| 107 |
2/2✓ Branch 0 taken 464 times.
✓ Branch 1 taken 336 times.
|
800 | for (int channel = 0; channel < channels; ++channel) { |
| 108 | 464 | const std::size_t out_idx = | |
| 109 | 464 | ((static_cast<std::size_t>(row) * static_cast<std::size_t>(width) + static_cast<std::size_t>(col)) * | |
| 110 | 464 | static_cast<std::size_t>(channels)) + | |
| 111 | 464 | static_cast<std::size_t>(channel); | |
| 112 | 464 | out.data[out_idx] = ConvolveGaussian3x3Clamp(in, row, col, channel); | |
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | return true; | ||
| 118 | } | ||
| 119 | |||
| 120 | 40 | bool PankovGaussFilterSEQ::PostProcessingImpl() { | |
| 121 | const auto &out = GetOutput(); | ||
| 122 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
|
40 | if (out.width == 0 || out.height == 0) { |
| 123 | ✗ | return out.data.empty(); | |
| 124 | } | ||
| 125 | 40 | return !out.data.empty(); | |
| 126 | } | ||
| 127 | |||
| 128 | } // namespace pankov_gauss_filter | ||
| 129 |