| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kopilov_d_vertical_gauss_filter/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <thread> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "kopilov_d_vertical_gauss_filter/common/include/common.hpp" | ||
| 12 | #include "util/include/util.hpp" | ||
| 13 | |||
| 14 | namespace kopilov_d_vertical_gauss_filter { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | const int kDivisor = 16; | ||
| 18 | const std::array<std::array<int, 3>, 3> kGaussKernel = {{{1, 2, 1}, {2, 4, 2}, {1, 2, 1}}}; | ||
| 19 | |||
| 20 | uint8_t GetPixelMirroredSTL(const std::vector<uint8_t> &src, int column, int row, int width, int height) { | ||
| 21 | int new_col = column; | ||
| 22 | int new_row = row; | ||
| 23 | 2808 | if (new_col < 0) { | |
| 24 | new_col = -new_col - 1; | ||
| 25 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 2184 times.
|
2496 | } else if (new_col >= width) { |
| 26 | 312 | new_col = (2 * width) - new_col - 1; | |
| 27 | } | ||
| 28 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 2496 times.
|
2808 | if (new_row < 0) { |
| 29 | new_row = -new_row - 1; | ||
| 30 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 2184 times.
|
2496 | } else if (new_row >= height) { |
| 31 | 312 | new_row = (2 * height) - new_row - 1; | |
| 32 | } | ||
| 33 | 2808 | auto idx = (static_cast<size_t>(new_row) * static_cast<size_t>(width)) + static_cast<size_t>(new_col); | |
| 34 | 2808 | return src[idx]; | |
| 35 | } | ||
| 36 | |||
| 37 | 312 | uint8_t ApplyGaussKernel(const std::vector<uint8_t> &src, int column, int row, int width, int height) { | |
| 38 | int pixel_sum = 0; | ||
| 39 |
2/2✓ Branch 0 taken 936 times.
✓ Branch 1 taken 312 times.
|
1248 | for (size_t kernel_y = 0; kernel_y < 3; ++kernel_y) { |
| 40 |
2/2✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 936 times.
|
3744 | for (size_t kernel_x = 0; kernel_x < 3; ++kernel_x) { |
| 41 | 2808 | int current_column = column + static_cast<int>(kernel_x) - 1; | |
| 42 | 2808 | int current_row = row + static_cast<int>(kernel_y) - 1; | |
| 43 | 2808 | pixel_sum += | |
| 44 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 2496 times.
|
5616 | kGaussKernel.at(kernel_y).at(kernel_x) * GetPixelMirroredSTL(src, current_column, current_row, width, height); |
| 45 | } | ||
| 46 | } | ||
| 47 | 312 | return static_cast<uint8_t>(pixel_sum / kDivisor); | |
| 48 | } | ||
| 49 | } // namespace | ||
| 50 | |||
| 51 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | KopilovDVerticalGaussFilterSTL::KopilovDVerticalGaussFilterSTL(const InType &in) { |
| 52 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 53 | GetInput() = in; | ||
| 54 | 40 | GetOutput() = OutType{}; | |
| 55 | 40 | } | |
| 56 | |||
| 57 | 40 | bool KopilovDVerticalGaussFilterSTL::ValidationImpl() { | |
| 58 | const auto &in = GetInput(); | ||
| 59 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | if (in.width <= 0 || in.height <= 0) { |
| 60 | return false; | ||
| 61 | } | ||
| 62 | 40 | return in.data.size() == static_cast<size_t>(in.width) * static_cast<size_t>(in.height); | |
| 63 | } | ||
| 64 | |||
| 65 | 40 | bool KopilovDVerticalGaussFilterSTL::PreProcessingImpl() { | |
| 66 | 40 | return true; | |
| 67 | } | ||
| 68 | |||
| 69 | 40 | bool KopilovDVerticalGaussFilterSTL::RunImpl() { | |
| 70 | const auto &in = GetInput(); | ||
| 71 | 40 | int width = in.width; | |
| 72 | 40 | int height = in.height; | |
| 73 | 40 | const std::vector<uint8_t> &matrix = in.data; | |
| 74 | 40 | std::vector<uint8_t> result(static_cast<size_t>(width) * static_cast<size_t>(height)); | |
| 75 | |||
| 76 |
2/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
|
40 | const int num_threads = ppc::util::GetNumThreads(); |
| 77 | const int actual_threads_count = std::min(num_threads, width); | ||
| 78 | |||
| 79 | 40 | std::vector<std::thread> threads; | |
| 80 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | threads.reserve(actual_threads_count); |
| 81 | |||
| 82 | 40 | const int cols_per_thread = width / actual_threads_count; | |
| 83 | 40 | const int remainder_cols = width % actual_threads_count; | |
| 84 | int start_column = 0; | ||
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 40 times.
|
118 | for (int thread_idx = 0; thread_idx < actual_threads_count; ++thread_idx) { |
| 87 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 6 times.
|
78 | int end_column = start_column + cols_per_thread + (thread_idx < remainder_cols ? 1 : 0); |
| 88 | |||
| 89 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | threads.emplace_back([&, start_column, end_column]() { |
| 90 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 78 times.
|
182 | for (int col_idx = start_column; col_idx < end_column; ++col_idx) { |
| 91 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 104 times.
|
416 | for (int row_idx = 0; row_idx < height; ++row_idx) { |
| 92 | 312 | auto out_idx = (static_cast<size_t>(row_idx) * static_cast<size_t>(width)) + static_cast<size_t>(col_idx); | |
| 93 | 312 | result[out_idx] = ApplyGaussKernel(matrix, col_idx, row_idx, width, height); | |
| 94 | } | ||
| 95 | } | ||
| 96 | 78 | }); | |
| 97 | start_column = end_column; | ||
| 98 | } | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 40 times.
|
118 | for (auto &thread_obj : threads) { |
| 101 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | thread_obj.join(); |
| 102 | } | ||
| 103 | |||
| 104 | 40 | GetOutput().width = width; | |
| 105 | 40 | GetOutput().height = height; | |
| 106 | 40 | GetOutput().data = std::move(result); | |
| 107 | 40 | return true; | |
| 108 | 40 | } | |
| 109 | |||
| 110 | 40 | bool KopilovDVerticalGaussFilterSTL::PostProcessingImpl() { | |
| 111 | 40 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | } // namespace kopilov_d_vertical_gauss_filter | ||
| 115 |