| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "vdovin_a_gauss_block/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "vdovin_a_gauss_block/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace vdovin_a_gauss_block { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | constexpr int kChannels = 3; | ||
| 14 | constexpr int kKernelSize = 3; | ||
| 15 | constexpr int kKernelSum = 16; | ||
| 16 | constexpr int kBlockSize = 32; | ||
| 17 | constexpr std::array<std::array<int, kKernelSize>, kKernelSize> kKernel = {{{1, 2, 1}, {2, 4, 2}, {1, 2, 1}}}; | ||
| 18 | } // namespace | ||
| 19 | |||
| 20 | 40 | VdovinAGaussBlockOMP::VdovinAGaussBlockOMP(const InType &in) { | |
| 21 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 22 | 40 | GetInput() = in; | |
| 23 | GetOutput() = 0; | ||
| 24 | 40 | } | |
| 25 | |||
| 26 | 40 | bool VdovinAGaussBlockOMP::ValidationImpl() { | |
| 27 | 40 | return GetInput() >= 3; | |
| 28 | } | ||
| 29 | |||
| 30 | 40 | bool VdovinAGaussBlockOMP::PreProcessingImpl() { | |
| 31 | 40 | width_ = GetInput(); | |
| 32 | 40 | height_ = GetInput(); | |
| 33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (width_ < 3 || height_ < 3) { |
| 34 | input_image_.clear(); | ||
| 35 | output_image_.clear(); | ||
| 36 | ✗ | return false; | |
| 37 | } | ||
| 38 | 40 | int total = width_ * height_ * kChannels; | |
| 39 | 40 | input_image_.assign(total, 100); | |
| 40 | 40 | output_image_.assign(total, 0); | |
| 41 | 40 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 52796 | void VdovinAGaussBlockOMP::ApplyGaussianToPixel(int py, int px) { | |
| 45 |
2/2✓ Branch 0 taken 158388 times.
✓ Branch 1 taken 52796 times.
|
211184 | for (int ch = 0; ch < kChannels; ch++) { |
| 46 | int sum = 0; | ||
| 47 |
2/2✓ Branch 0 taken 475164 times.
✓ Branch 1 taken 158388 times.
|
633552 | for (int ky = -1; ky <= 1; ky++) { |
| 48 |
2/2✓ Branch 0 taken 1425492 times.
✓ Branch 1 taken 475164 times.
|
1900656 | for (int kx = -1; kx <= 1; kx++) { |
| 49 | 1425492 | int ny = std::clamp(py + ky, 0, height_ - 1); | |
| 50 | 1425492 | int nx = std::clamp(px + kx, 0, width_ - 1); | |
| 51 | 1425492 | sum += input_image_[(((ny * width_) + nx) * kChannels) + ch] * kKernel.at(ky + 1).at(kx + 1); | |
| 52 | } | ||
| 53 | } | ||
| 54 |
1/2✓ Branch 0 taken 158388 times.
✗ Branch 1 not taken.
|
316776 | output_image_[(((py * width_) + px) * kChannels) + ch] = static_cast<uint8_t>(std::clamp(sum / kKernelSum, 0, 255)); |
| 55 | } | ||
| 56 | 52796 | } | |
| 57 | |||
| 58 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool VdovinAGaussBlockOMP::RunImpl() { |
| 59 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | if (input_image_.empty() || output_image_.empty()) { |
| 60 | return false; | ||
| 61 | } | ||
| 62 | |||
| 63 | 40 | int num_blocks_y = (height_ + kBlockSize - 1) / kBlockSize; | |
| 64 | 40 | int num_blocks_x = (width_ + kBlockSize - 1) / kBlockSize; | |
| 65 | 40 | int total_blocks = num_blocks_y * num_blocks_x; | |
| 66 | |||
| 67 | 40 | #pragma omp parallel for schedule(static) default(none) shared(total_blocks, num_blocks_x) | |
| 68 | for (int bi = 0; bi < total_blocks; bi++) { | ||
| 69 | int by = (bi / num_blocks_x) * kBlockSize; | ||
| 70 | int bx = (bi % num_blocks_x) * kBlockSize; | ||
| 71 | int y_end = std::min(by + kBlockSize, height_); | ||
| 72 | int x_end = std::min(bx + kBlockSize, width_); | ||
| 73 | for (int py = by; py < y_end; py++) { | ||
| 74 | for (int px = bx; px < x_end; px++) { | ||
| 75 | ApplyGaussianToPixel(py, px); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | 40 | return true; | |
| 81 | } | ||
| 82 | |||
| 83 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool VdovinAGaussBlockOMP::PostProcessingImpl() { |
| 84 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (output_image_.empty()) { |
| 85 | return false; | ||
| 86 | } | ||
| 87 | auto total = static_cast<int64_t>(output_image_.size()); | ||
| 88 | if (total == 0) { | ||
| 89 | return false; | ||
| 90 | } | ||
| 91 | int64_t sum = 0; | ||
| 92 |
2/2✓ Branch 0 taken 158388 times.
✓ Branch 1 taken 40 times.
|
158428 | for (int64_t idx = 0; idx < total; idx++) { |
| 93 | 158388 | sum += output_image_[idx]; | |
| 94 | } | ||
| 95 | 40 | GetOutput() = static_cast<int>(sum / total); | |
| 96 | 40 | return true; | |
| 97 | } | ||
| 98 | |||
| 99 | } // namespace vdovin_a_gauss_block | ||
| 100 |