| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "pikhotskiy_r_vertical_gauss_filter/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | |||
| 7 | #include "pikhotskiy_r_vertical_gauss_filter/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace pikhotskiy_r_vertical_gauss_filter { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | constexpr int kKernelNorm = 16; | ||
| 13 | constexpr int kStripeDivider = 8; | ||
| 14 | |||
| 15 | constexpr int ClampIndex(int value, int upper_bound) noexcept { | ||
| 16 | ✗ | if (upper_bound <= 0) { | |
| 17 | return 0; | ||
| 18 | } | ||
| 19 | ✗ | if (value < 0) { | |
| 20 | return 0; | ||
| 21 | } | ||
| 22 | ✗ | if (value >= upper_bound) { | |
| 23 | ✗ | return upper_bound - 1; | |
| 24 | } | ||
| 25 | return value; | ||
| 26 | } | ||
| 27 | |||
| 28 | constexpr std::size_t ToLinearIndex(int x_pos, int y_pos, int width) noexcept { | ||
| 29 | ✗ | return (static_cast<std::size_t>(y_pos) * static_cast<std::size_t>(width)) + static_cast<std::size_t>(x_pos); | |
| 30 | } | ||
| 31 | |||
| 32 | std::uint8_t NormalizeAndRoundUp(int sum) { | ||
| 33 | ✗ | return static_cast<std::uint8_t>((sum + (kKernelNorm - 1)) / kKernelNorm); | |
| 34 | } | ||
| 35 | } // namespace | ||
| 36 | |||
| 37 | ✗ | PikhotskiyRVerticalGaussFilterSEQ::PikhotskiyRVerticalGaussFilterSEQ(const InType &in) { | |
| 38 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 39 | GetInput() = in; | ||
| 40 | ✗ | GetOutput() = OutType{}; | |
| 41 | ✗ | } | |
| 42 | |||
| 43 | ✗ | bool PikhotskiyRVerticalGaussFilterSEQ::ValidationImpl() { | |
| 44 | const auto &in = GetInput(); | ||
| 45 | |||
| 46 | ✗ | if (in.width <= 0 || in.height <= 0) { | |
| 47 | return false; | ||
| 48 | } | ||
| 49 | ✗ | const auto expected_size = static_cast<std::size_t>(in.width) * static_cast<std::size_t>(in.height); | |
| 50 | ✗ | return in.data.size() == expected_size; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | bool PikhotskiyRVerticalGaussFilterSEQ::PreProcessingImpl() { | |
| 54 | const auto &in = GetInput(); | ||
| 55 | ✗ | width_ = in.width; | |
| 56 | ✗ | height_ = in.height; | |
| 57 | ✗ | stripe_width_ = std::max(1, width_ / kStripeDivider); | |
| 58 | |||
| 59 | ✗ | source_ = in.data; | |
| 60 | ✗ | vertical_buffer_.assign(source_.size(), 0); | |
| 61 | ✗ | result_buffer_.assign(source_.size(), 0); | |
| 62 | ✗ | return true; | |
| 63 | } | ||
| 64 | |||
| 65 | ✗ | bool PikhotskiyRVerticalGaussFilterSEQ::RunImpl() { | |
| 66 | ✗ | const auto expected_size = static_cast<std::size_t>(width_) * static_cast<std::size_t>(height_); | |
| 67 | ✗ | if (width_ <= 0 || height_ <= 0 || source_.size() != expected_size || vertical_buffer_.size() != expected_size || | |
| 68 | result_buffer_.size() != expected_size) { | ||
| 69 | return false; | ||
| 70 | } | ||
| 71 | |||
| 72 | ✗ | for (int x_begin = 0; x_begin < width_; x_begin += stripe_width_) { | |
| 73 | ✗ | const int x_end = std::min(width_, x_begin + stripe_width_); | |
| 74 | ✗ | RunVerticalPassForStripe(x_begin, x_end); | |
| 75 | } | ||
| 76 | |||
| 77 | ✗ | for (int x_begin = 0; x_begin < width_; x_begin += stripe_width_) { | |
| 78 | ✗ | const int x_end = std::min(width_, x_begin + stripe_width_); | |
| 79 | ✗ | RunHorizontalPassForStripe(x_begin, x_end); | |
| 80 | } | ||
| 81 | |||
| 82 | return true; | ||
| 83 | } | ||
| 84 | |||
| 85 | ✗ | bool PikhotskiyRVerticalGaussFilterSEQ::PostProcessingImpl() { | |
| 86 | ✗ | GetOutput().width = width_; | |
| 87 | ✗ | GetOutput().height = height_; | |
| 88 | ✗ | GetOutput().data = result_buffer_; | |
| 89 | ✗ | return true; | |
| 90 | } | ||
| 91 | |||
| 92 | ✗ | void PikhotskiyRVerticalGaussFilterSEQ::RunVerticalPassForStripe(int x_begin, int x_end) { | |
| 93 | ✗ | for (int row = 0; row < height_; ++row) { | |
| 94 | ✗ | const int row_top = ClampIndex(row - 1, height_); | |
| 95 | ✗ | const int row_bottom = ClampIndex(row + 1, height_); | |
| 96 | |||
| 97 | ✗ | for (int col = x_begin; col < x_end; ++col) { | |
| 98 | ✗ | const std::size_t center = ToLinearIndex(col, row, width_); | |
| 99 | const std::size_t top = ToLinearIndex(col, row_top, width_); | ||
| 100 | const std::size_t bottom = ToLinearIndex(col, row_bottom, width_); | ||
| 101 | ✗ | vertical_buffer_[center] = | |
| 102 | ✗ | static_cast<int>(source_[top]) + (2 * static_cast<int>(source_[center])) + static_cast<int>(source_[bottom]); | |
| 103 | } | ||
| 104 | } | ||
| 105 | ✗ | } | |
| 106 | |||
| 107 | ✗ | void PikhotskiyRVerticalGaussFilterSEQ::RunHorizontalPassForStripe(int x_begin, int x_end) { | |
| 108 | ✗ | for (int row = 0; row < height_; ++row) { | |
| 109 | ✗ | for (int col = x_begin; col < x_end; ++col) { | |
| 110 | ✗ | const int col_left = ClampIndex(col - 1, width_); | |
| 111 | ✗ | const int col_right = ClampIndex(col + 1, width_); | |
| 112 | const std::size_t center = ToLinearIndex(col, row, width_); | ||
| 113 | const std::size_t left = ToLinearIndex(col_left, row, width_); | ||
| 114 | const std::size_t right = ToLinearIndex(col_right, row, width_); | ||
| 115 | ✗ | const int weighted_sum = vertical_buffer_[left] + (2 * vertical_buffer_[center]) + vertical_buffer_[right]; | |
| 116 | ✗ | result_buffer_[center] = NormalizeAndRoundUp(weighted_sum); | |
| 117 | } | ||
| 118 | } | ||
| 119 | ✗ | } | |
| 120 | |||
| 121 | } // namespace pikhotskiy_r_vertical_gauss_filter | ||
| 122 |