| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "vdovin_a_gauss_block/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <thread> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "util/include/util.hpp" | ||
| 10 | #include "vdovin_a_gauss_block/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace vdovin_a_gauss_block { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | constexpr int kChannels = 3; | ||
| 16 | constexpr int kKernelSize = 3; | ||
| 17 | constexpr int kKernelSum = 16; | ||
| 18 | constexpr std::array<std::array<int, kKernelSize>, kKernelSize> kKernel = {{{1, 2, 1}, {2, 4, 2}, {1, 2, 1}}}; | ||
| 19 | } // namespace | ||
| 20 | |||
| 21 | 80 | VdovinAGaussBlockSTL::VdovinAGaussBlockSTL(const InType &in) { | |
| 22 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 23 | 80 | GetInput() = in; | |
| 24 | GetOutput() = 0; | ||
| 25 | 80 | } | |
| 26 | |||
| 27 | 80 | bool VdovinAGaussBlockSTL::ValidationImpl() { | |
| 28 | 80 | return GetInput() >= 3; | |
| 29 | } | ||
| 30 | |||
| 31 | 80 | bool VdovinAGaussBlockSTL::PreProcessingImpl() { | |
| 32 | 80 | width_ = GetInput(); | |
| 33 | 80 | height_ = GetInput(); | |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (width_ < 3 || height_ < 3) { |
| 35 | input_image_.clear(); | ||
| 36 | output_image_.clear(); | ||
| 37 | ✗ | return false; | |
| 38 | } | ||
| 39 | 80 | int total = width_ * height_ * kChannels; | |
| 40 | 80 | input_image_.assign(total, 100); | |
| 41 | 80 | output_image_.assign(total, 0); | |
| 42 | 80 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 105592 | void VdovinAGaussBlockSTL::ApplyGaussianToPixel(int py, int px) { | |
| 46 |
2/2✓ Branch 0 taken 316776 times.
✓ Branch 1 taken 105592 times.
|
422368 | for (int ch = 0; ch < kChannels; ch++) { |
| 47 | int sum = 0; | ||
| 48 |
2/2✓ Branch 0 taken 950328 times.
✓ Branch 1 taken 316776 times.
|
1267104 | for (int ky = -1; ky <= 1; ky++) { |
| 49 |
2/2✓ Branch 0 taken 2850984 times.
✓ Branch 1 taken 950328 times.
|
3801312 | for (int kx = -1; kx <= 1; kx++) { |
| 50 | 2850984 | int ny = std::clamp(py + ky, 0, height_ - 1); | |
| 51 | 2850984 | int nx = std::clamp(px + kx, 0, width_ - 1); | |
| 52 | 2850984 | sum += input_image_[(((ny * width_) + nx) * kChannels) + ch] * kKernel.at(ky + 1).at(kx + 1); | |
| 53 | } | ||
| 54 | } | ||
| 55 |
1/2✓ Branch 0 taken 316776 times.
✗ Branch 1 not taken.
|
633552 | output_image_[(((py * width_) + px) * kChannels) + ch] = static_cast<uint8_t>(std::clamp(sum / kKernelSum, 0, 255)); |
| 56 | } | ||
| 57 | 105592 | } | |
| 58 | |||
| 59 | 200 | void VdovinAGaussBlockSTL::ProcessRows(int row_start, int row_end) { | |
| 60 |
2/2✓ Branch 0 taken 1704 times.
✓ Branch 1 taken 200 times.
|
1904 | for (int py = row_start; py < row_end; py++) { |
| 61 |
2/2✓ Branch 0 taken 105592 times.
✓ Branch 1 taken 1704 times.
|
107296 | for (int px = 0; px < width_; px++) { |
| 62 | 105592 | ApplyGaussianToPixel(py, px); | |
| 63 | } | ||
| 64 | } | ||
| 65 | 200 | } | |
| 66 | |||
| 67 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | bool VdovinAGaussBlockSTL::RunImpl() { |
| 68 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | if (input_image_.empty() || output_image_.empty()) { |
| 69 | return false; | ||
| 70 | } | ||
| 71 | |||
| 72 | 80 | int num_threads = ppc::util::GetNumThreads(); | |
| 73 | 80 | int rows_per_thread = height_ / num_threads; | |
| 74 | 80 | int remainder = height_ % num_threads; | |
| 75 | |||
| 76 | 80 | std::vector<std::thread> threads; | |
| 77 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | threads.reserve(num_threads); |
| 78 | |||
| 79 | 80 | int offset = 0; | |
| 80 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 80 times.
|
280 | for (int ti = 0; ti < num_threads; ti++) { |
| 81 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 56 times.
|
200 | int count = rows_per_thread + (ti < remainder ? 1 : 0); |
| 82 |
1/2✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
|
200 | threads.emplace_back(&VdovinAGaussBlockSTL::ProcessRows, this, offset, offset + count); |
| 83 | 200 | offset += count; | |
| 84 | } | ||
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 80 times.
|
280 | for (auto &th : threads) { |
| 87 |
1/2✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
|
200 | th.join(); |
| 88 | } | ||
| 89 | |||
| 90 | return true; | ||
| 91 | 80 | } | |
| 92 | |||
| 93 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | bool VdovinAGaussBlockSTL::PostProcessingImpl() { |
| 94 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (output_image_.empty()) { |
| 95 | return false; | ||
| 96 | } | ||
| 97 | auto total = static_cast<int64_t>(output_image_.size()); | ||
| 98 | if (total == 0) { | ||
| 99 | return false; | ||
| 100 | } | ||
| 101 | int64_t sum = 0; | ||
| 102 |
2/2✓ Branch 0 taken 316776 times.
✓ Branch 1 taken 80 times.
|
316856 | for (int64_t idx = 0; idx < total; idx++) { |
| 103 | 316776 | sum += output_image_[idx]; | |
| 104 | } | ||
| 105 | 80 | GetOutput() = static_cast<int>(sum / total); | |
| 106 | 80 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | } // namespace vdovin_a_gauss_block | ||
| 110 |