| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kolotukhin_a_gaussian_blur/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 "kolotukhin_a_gaussian_blur/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace kolotukhin_a_gaussian_blur { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | void GetThreadRange(size_t tid, size_t total, size_t num_t, size_t &begin, size_t &end) { | ||
| 18 | 80 | size_t chunk = total / num_t; | |
| 19 | 80 | begin = tid * chunk; | |
| 20 | 48 | end = (tid == num_t - 1) ? total : begin + chunk; | |
| 21 | } | ||
| 22 | |||
| 23 | } // namespace | ||
| 24 | |||
| 25 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | KolotukhinAGaussinBlureSTL::KolotukhinAGaussinBlureSTL(const InType &in) { |
| 26 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 27 | GetInput() = in; | ||
| 28 | GetOutput().clear(); | ||
| 29 | 32 | } | |
| 30 | |||
| 31 | 32 | bool KolotukhinAGaussinBlureSTL::ValidationImpl() { | |
| 32 | const auto &pixel_data = get<0>(GetInput()); | ||
| 33 | 32 | const auto img_width = get<1>(GetInput()); | |
| 34 | 32 | const auto img_height = get<2>(GetInput()); | |
| 35 | |||
| 36 | 32 | return static_cast<std::size_t>(img_height) * static_cast<std::size_t>(img_width) == pixel_data.size(); | |
| 37 | } | ||
| 38 | |||
| 39 | 32 | bool KolotukhinAGaussinBlureSTL::PreProcessingImpl() { | |
| 40 | 32 | const auto img_width = get<1>(GetInput()); | |
| 41 | 32 | const auto img_height = get<2>(GetInput()); | |
| 42 | |||
| 43 | 32 | GetOutput().assign(static_cast<std::size_t>(img_height) * static_cast<std::size_t>(img_width), 0); | |
| 44 | 32 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | bool KolotukhinAGaussinBlureSTL::RunImpl() { |
| 48 | const auto &pixel_data = get<0>(GetInput()); | ||
| 49 | 32 | const auto img_width = get<1>(GetInput()); | |
| 50 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | const auto img_height = get<2>(GetInput()); |
| 51 | |||
| 52 | const static std::array<std::array<int, 3>, 3> kKernel = {{{{1, 2, 1}}, {{2, 4, 2}}, {{1, 2, 1}}}}; | ||
| 53 | const static int kSum = 16; | ||
| 54 | |||
| 55 | auto &output = GetOutput(); | ||
| 56 | 32 | const std::size_t total_pixels = static_cast<std::size_t>(img_height) * static_cast<std::size_t>(img_width); | |
| 57 | |||
| 58 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (total_pixels == 0) { |
| 59 | return false; | ||
| 60 | } | ||
| 61 | |||
| 62 | 32 | const int num_threads = ppc::util::GetNumThreads(); | |
| 63 | 32 | std::vector<std::thread> threads; | |
| 64 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | threads.reserve(num_threads); |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 32 times.
|
112 | for (int tid = 0; tid < num_threads; tid++) { |
| 67 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | threads.emplace_back([&, tid]() { |
| 68 | std::size_t begin = 0; | ||
| 69 | std::size_t end = 0; | ||
| 70 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 32 times.
|
80 | GetThreadRange(tid, total_pixels, num_threads, begin, end); |
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 80 times.
|
392 | for (std::size_t idx = begin; idx < end; idx++) { |
| 73 | 312 | int row = static_cast<int>(idx / static_cast<std::size_t>(img_width)); | |
| 74 | 312 | int col = static_cast<int>(idx % static_cast<std::size_t>(img_width)); | |
| 75 | |||
| 76 | int acc = 0; | ||
| 77 |
2/2✓ Branch 0 taken 936 times.
✓ Branch 1 taken 312 times.
|
1248 | for (int dy = -1; dy <= 1; dy++) { |
| 78 |
2/2✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 936 times.
|
3744 | for (int dx = -1; dx <= 1; dx++) { |
| 79 | 2808 | std::uint8_t pixel = GetPixel(pixel_data, img_width, img_height, col + dx, row + dy); | |
| 80 | 2808 | acc += kKernel.at(1 + dy).at(1 + dx) * static_cast<int>(pixel); | |
| 81 | } | ||
| 82 | } | ||
| 83 | 312 | output[idx] = static_cast<std::uint8_t>(acc / kSum); | |
| 84 | } | ||
| 85 | 80 | }); | |
| 86 | } | ||
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 32 times.
|
112 | for (auto &th : threads) { |
| 89 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | th.join(); |
| 90 | } | ||
| 91 | |||
| 92 | return true; | ||
| 93 | 32 | } | |
| 94 | |||
| 95 | 2808 | std::uint8_t KolotukhinAGaussinBlureSTL::GetPixel(const std::vector<std::uint8_t> &pixel_data, int img_width, | |
| 96 | int img_height, int pos_x, int pos_y) { | ||
| 97 |
4/4✓ Branch 0 taken 288 times.
✓ Branch 1 taken 2520 times.
✓ Branch 2 taken 1944 times.
✓ Branch 3 taken 864 times.
|
3096 | std::size_t x = static_cast<std::size_t>(std::max(0, std::min(pos_x, img_width - 1))); |
| 98 |
4/4✓ Branch 0 taken 312 times.
✓ Branch 1 taken 2496 times.
✓ Branch 2 taken 1872 times.
✓ Branch 3 taken 936 times.
|
3120 | std::size_t y = static_cast<std::size_t>(std::max(0, std::min(pos_y, img_height - 1))); |
| 99 | 2808 | return pixel_data[(y * static_cast<std::size_t>(img_width)) + x]; | |
| 100 | } | ||
| 101 | |||
| 102 | 32 | bool KolotukhinAGaussinBlureSTL::PostProcessingImpl() { | |
| 103 | 32 | return true; | |
| 104 | } | ||
| 105 | |||
| 106 | } // namespace kolotukhin_a_gaussian_blur | ||
| 107 |