| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "buzulukski_d_gaus_gorizontal/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | |||
| 8 | #include "buzulukski_d_gaus_gorizontal/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace buzulukski_d_gaus_gorizontal { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | constexpr int kChannels = 3; | ||
| 14 | constexpr int kKernelSize = 3; | ||
| 15 | constexpr int kKernelSum = 16; | ||
| 16 | |||
| 17 | using KernelRow = std::array<int, kKernelSize>; | ||
| 18 | constexpr std::array<KernelRow, kKernelSize> kKernel = {{{1, 2, 1}, {2, 4, 2}, {1, 2, 1}}}; | ||
| 19 | } // namespace | ||
| 20 | |||
| 21 | ✗ | BuzulukskiDGausGorizontalSEQ::BuzulukskiDGausGorizontalSEQ(const InType &in) : BaseTask() { | |
| 22 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 23 | ✗ | GetInput() = in; | |
| 24 | GetOutput() = 0; | ||
| 25 | ✗ | } | |
| 26 | |||
| 27 | ✗ | bool BuzulukskiDGausGorizontalSEQ::ValidationImpl() { | |
| 28 | ✗ | return GetInput() >= kKernelSize; | |
| 29 | } | ||
| 30 | |||
| 31 | ✗ | bool BuzulukskiDGausGorizontalSEQ::PreProcessingImpl() { | |
| 32 | ✗ | width_ = GetInput(); | |
| 33 | ✗ | height_ = GetInput(); | |
| 34 | |||
| 35 | ✗ | if (width_ < kKernelSize) { | |
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 | ✗ | const auto total_size = static_cast<std::size_t>(width_) * static_cast<std::size_t>(height_) * kChannels; | |
| 40 | ✗ | input_image_.assign(total_size, static_cast<uint8_t>(100)); | |
| 41 | ✗ | output_image_.assign(total_size, 0); | |
| 42 | ✗ | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | ✗ | void BuzulukskiDGausGorizontalSEQ::ApplyGaussianToPixel(int py, int px) { | |
| 46 | ✗ | const auto u_width = static_cast<std::size_t>(width_); | |
| 47 | ✗ | const auto u_height = static_cast<std::size_t>(height_); | |
| 48 | const auto u_channels = static_cast<std::size_t>(kChannels); | ||
| 49 | |||
| 50 | ✗ | for (int ch = 0; ch < kChannels; ++ch) { | |
| 51 | int sum = 0; | ||
| 52 | ✗ | for (int ky = -1; ky <= 1; ++ky) { | |
| 53 | ✗ | for (int kx = -1; kx <= 1; ++kx) { | |
| 54 | ✗ | const int ny = std::clamp(py + ky, 0, static_cast<int>(u_height) - 1); | |
| 55 | ✗ | const int nx = std::clamp(px + kx, 0, static_cast<int>(u_width) - 1); | |
| 56 | |||
| 57 | ✗ | const auto idx = (((static_cast<std::size_t>(ny) * u_width) + static_cast<std::size_t>(nx)) * u_channels) + | |
| 58 | ✗ | static_cast<std::size_t>(ch); | |
| 59 | |||
| 60 | ✗ | const auto row_idx = static_cast<std::size_t>(ky) + 1; | |
| 61 | ✗ | const auto col_idx = static_cast<std::size_t>(kx) + 1; | |
| 62 | |||
| 63 | ✗ | sum += static_cast<int>(input_image_.at(idx)) * kKernel.at(row_idx).at(col_idx); | |
| 64 | } | ||
| 65 | } | ||
| 66 | ✗ | const auto out_idx = (((static_cast<std::size_t>(py) * u_width) + static_cast<std::size_t>(px)) * u_channels) + | |
| 67 | ✗ | static_cast<std::size_t>(ch); | |
| 68 | ✗ | output_image_.at(out_idx) = static_cast<uint8_t>(sum / kKernelSum); | |
| 69 | } | ||
| 70 | ✗ | } | |
| 71 | |||
| 72 | ✗ | bool BuzulukskiDGausGorizontalSEQ::RunImpl() { | |
| 73 | ✗ | for (int py = 0; py < height_; ++py) { | |
| 74 | ✗ | for (int px = 0; px < width_; ++px) { | |
| 75 | ✗ | ApplyGaussianToPixel(py, px); | |
| 76 | } | ||
| 77 | } | ||
| 78 | ✗ | return true; | |
| 79 | } | ||
| 80 | |||
| 81 | ✗ | bool BuzulukskiDGausGorizontalSEQ::PostProcessingImpl() { | |
| 82 | ✗ | if (output_image_.empty()) { | |
| 83 | return false; | ||
| 84 | } | ||
| 85 | |||
| 86 | int64_t total_sum = 0; | ||
| 87 | ✗ | for (const auto &val : output_image_) { | |
| 88 | ✗ | total_sum += static_cast<int64_t>(val); | |
| 89 | } | ||
| 90 | ✗ | GetOutput() = static_cast<int>(total_sum / static_cast<int64_t>(output_image_.size())); | |
| 91 | ✗ | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | } // namespace buzulukski_d_gaus_gorizontal | ||
| 95 |