| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "nikolaev_d_block_linear_image_filtering/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <thread> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "nikolaev_d_block_linear_image_filtering/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace nikolaev_d_block_linear_image_filtering { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | NikolaevDBlockLinearImageFilteringSTL::NikolaevDBlockLinearImageFilteringSTL(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | GetInput() = in; | ||
| 18 | 40 | GetOutput() = std::vector<uint8_t>(); | |
| 19 | 40 | } | |
| 20 | |||
| 21 | 40 | bool NikolaevDBlockLinearImageFilteringSTL::ValidationImpl() { | |
| 22 | 40 | const auto img_width = std::get<0>(GetInput()); | |
| 23 | 40 | const auto img_height = std::get<1>(GetInput()); | |
| 24 | const auto &pixel_data = std::get<2>(GetInput()); | ||
| 25 | |||
| 26 | 40 | return static_cast<std::size_t>(img_width) * static_cast<std::size_t>(img_height) * 3 == pixel_data.size(); | |
| 27 | } | ||
| 28 | |||
| 29 | 40 | bool NikolaevDBlockLinearImageFilteringSTL::PreProcessingImpl() { | |
| 30 | 40 | return true; | |
| 31 | } | ||
| 32 | |||
| 33 | ✗ | std::uint8_t NikolaevDBlockLinearImageFilteringSTL::GetPixel(const std::vector<uint8_t> &data, int w, int h, int nx, | |
| 34 | int ny, int ch) { | ||
| 35 | 3888 | int ix = std::clamp(nx, 0, w - 1); | |
| 36 | 3888 | int iy = std::clamp(ny, 0, h - 1); | |
| 37 | 3888 | return data[((iy * w + ix) * 3) + ch]; | |
| 38 | } | ||
| 39 | |||
| 40 | 432 | std::uint8_t NikolaevDBlockLinearImageFilteringSTL::ApplyKernel(const std::vector<uint8_t> &src, int w, int h, int nx, | |
| 41 | int ny, int ch) { | ||
| 42 | static constexpr std::array<std::array<int, 3>, 3> kKernel = {{{1, 2, 1}, {2, 4, 2}, {1, 2, 1}}}; | ||
| 43 | int acc = 0; | ||
| 44 |
2/2✓ Branch 0 taken 1296 times.
✓ Branch 1 taken 432 times.
|
1728 | for (int ky = -1; ky <= 1; ++ky) { |
| 45 |
2/2✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 1296 times.
|
5184 | for (int kx = -1; kx <= 1; ++kx) { |
| 46 | 3888 | acc += GetPixel(src, w, h, nx + kx, ny + ky, ch) * kKernel.at(ky + 1).at(kx + 1); | |
| 47 | } | ||
| 48 | } | ||
| 49 |
1/2✓ Branch 0 taken 432 times.
✗ Branch 1 not taken.
|
432 | return static_cast<uint8_t>(std::clamp((acc + 8) / 16, 0, 255)); |
| 50 | } | ||
| 51 | |||
| 52 | 56 | void NikolaevDBlockLinearImageFilteringSTL::ProcessRows(const std::vector<uint8_t> &src, int width, int height, | |
| 53 | int start_row, int end_row) { | ||
| 54 | auto &dst = GetOutput(); | ||
| 55 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 56 times.
|
120 | for (int ny = start_row; ny < end_row; ++ny) { |
| 56 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 64 times.
|
208 | for (int nx = 0; nx < width; ++nx) { |
| 57 |
2/2✓ Branch 0 taken 432 times.
✓ Branch 1 taken 144 times.
|
576 | for (int ch = 0; ch < 3; ++ch) { |
| 58 | 432 | dst[((ny * width + nx) * 3) + ch] = ApplyKernel(src, width, height, nx, ny, ch); | |
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | 56 | } | |
| 63 | |||
| 64 | 40 | bool NikolaevDBlockLinearImageFilteringSTL::RunImpl() { | |
| 65 | 40 | const int width = std::get<0>(GetInput()); | |
| 66 | 40 | const int height = std::get<1>(GetInput()); | |
| 67 | const auto &src = std::get<2>(GetInput()); | ||
| 68 | |||
| 69 | auto &dst = GetOutput(); | ||
| 70 | 40 | dst.assign(src.size(), 0); | |
| 71 | |||
| 72 | 40 | int num_threads = ppc::util::GetNumThreads(); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (num_threads == 0) { |
| 74 | ✗ | num_threads = 4; | |
| 75 | } | ||
| 76 | |||
| 77 | 40 | num_threads = std::min(height, num_threads); | |
| 78 | |||
| 79 | 40 | std::vector<std::thread> threads; | |
| 80 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | threads.reserve(num_threads); |
| 81 | |||
| 82 | 40 | int rows_per_thread = height / num_threads; | |
| 83 | 40 | int remainder = height % num_threads; | |
| 84 | |||
| 85 | int start_row = 0; | ||
| 86 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 40 times.
|
96 | for (int i = 0; i < num_threads; ++i) { |
| 87 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 2 times.
|
56 | int end_row = start_row + rows_per_thread + (i < remainder ? 1 : 0); |
| 88 | 56 | threads.emplace_back( | |
| 89 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
112 | [this, &src, width, height, start_row, end_row]() { ProcessRows(src, width, height, start_row, end_row); }); |
| 90 | |||
| 91 | start_row = end_row; | ||
| 92 | } | ||
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 40 times.
|
96 | for (auto &t : threads) { |
| 95 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | if (t.joinable()) { |
| 96 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | t.join(); |
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | 40 | return true; | |
| 101 | 40 | } | |
| 102 | |||
| 103 | 40 | bool NikolaevDBlockLinearImageFilteringSTL::PostProcessingImpl() { | |
| 104 | 40 | return true; | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace nikolaev_d_block_linear_image_filtering | ||
| 108 |