| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zaharov_g_linear_contrast_stretch/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <limits> | ||
| 7 | #include <thread> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "util/include/util.hpp" | ||
| 11 | #include "zaharov_g_linear_contrast_stretch/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace zaharov_g_linear_contrast_stretch { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | struct MinMax { | ||
| 18 | int min; | ||
| 19 | int max; | ||
| 20 | }; | ||
| 21 | |||
| 22 | std::size_t GetThreadCount(std::size_t size) { | ||
| 23 |
1/2✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
64 | const auto requested_threads = static_cast<std::size_t>(std::max(1, ppc::util::GetNumThreads())); |
| 24 | return std::min(size, requested_threads); | ||
| 25 | } | ||
| 26 | |||
| 27 | 32 | MinMax FindMinMax(const InType &input) { | |
| 28 | const std::size_t thread_count = GetThreadCount(input.size()); | ||
| 29 | std::vector<MinMax> local_minmax(thread_count, | ||
| 30 | 32 | {std::numeric_limits<uint8_t>::max(), std::numeric_limits<uint8_t>::min()}); | |
| 31 | 32 | std::vector<std::thread> threads; | |
| 32 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | threads.reserve(thread_count); |
| 33 | |||
| 34 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 32 times.
|
110 | for (std::size_t thread_index = 0; thread_index < thread_count; ++thread_index) { |
| 35 | 78 | const std::size_t begin = input.size() * thread_index / thread_count; | |
| 36 | 78 | const std::size_t end = input.size() * (thread_index + 1) / thread_count; | |
| 37 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | threads.emplace_back([&input, &local_minmax, begin, end, thread_index]() { |
| 38 | 78 | MinMax current{.min = std::numeric_limits<uint8_t>::max(), .max = std::numeric_limits<uint8_t>::min()}; | |
| 39 |
2/2✓ Branch 0 taken 10152 times.
✓ Branch 1 taken 78 times.
|
10230 | for (std::size_t i = begin; i < end; ++i) { |
| 40 | 10152 | const int value = static_cast<int>(input[i]); | |
| 41 | 10152 | current.min = std::min(current.min, value); | |
| 42 | 10152 | current.max = std::max(current.max, value); | |
| 43 | } | ||
| 44 | 78 | local_minmax[thread_index] = current; | |
| 45 | 78 | }); | |
| 46 | } | ||
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 32 times.
|
110 | for (auto &thread : threads) { |
| 49 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | thread.join(); |
| 50 | } | ||
| 51 | |||
| 52 | 32 | MinMax result{.min = std::numeric_limits<uint8_t>::max(), .max = std::numeric_limits<uint8_t>::min()}; | |
| 53 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 32 times.
|
110 | for (const auto ¤t : local_minmax) { |
| 54 | 78 | result.min = std::min(result.min, current.min); | |
| 55 | 78 | result.max = std::max(result.max, current.max); | |
| 56 | } | ||
| 57 | 32 | return result; | |
| 58 | 32 | } | |
| 59 | |||
| 60 | 78 | void StretchRange(const InType &input, OutType &output, std::size_t begin, std::size_t end, int min_el, int max_el) { | |
| 61 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 20 times.
|
78 | if (max_el > min_el) { |
| 62 | 58 | const int denom = max_el - min_el; | |
| 63 |
2/2✓ Branch 0 taken 10072 times.
✓ Branch 1 taken 58 times.
|
10130 | for (std::size_t i = begin; i < end; ++i) { |
| 64 | 10072 | const int value = (static_cast<int>(input[i]) - min_el) * std::numeric_limits<uint8_t>::max() / denom; | |
| 65 | 10072 | output[i] = static_cast<uint8_t>(std::clamp(value, 0, 255)); | |
| 66 | } | ||
| 67 | } else { | ||
| 68 | 20 | std::copy(input.begin() + static_cast<std::ptrdiff_t>(begin), input.begin() + static_cast<std::ptrdiff_t>(end), | |
| 69 | output.begin() + static_cast<std::ptrdiff_t>(begin)); | ||
| 70 | } | ||
| 71 | 78 | } | |
| 72 | |||
| 73 | } // namespace | ||
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | ZaharovGLinContrStrSTL::ZaharovGLinContrStrSTL(const InType &in) { |
| 76 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 77 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | GetInput() = in; |
| 78 | 32 | } | |
| 79 | |||
| 80 | 32 | bool ZaharovGLinContrStrSTL::ValidationImpl() { | |
| 81 | 32 | return !GetInput().empty(); | |
| 82 | } | ||
| 83 | |||
| 84 | 32 | bool ZaharovGLinContrStrSTL::PreProcessingImpl() { | |
| 85 | 32 | GetOutput().resize(GetInput().size()); | |
| 86 | 32 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 | 32 | bool ZaharovGLinContrStrSTL::RunImpl() { | |
| 90 | const InType &input = GetInput(); | ||
| 91 | OutType &output = GetOutput(); | ||
| 92 | |||
| 93 | 32 | const MinMax minmax = FindMinMax(input); | |
| 94 | const std::size_t thread_count = GetThreadCount(input.size()); | ||
| 95 | 32 | std::vector<std::thread> threads; | |
| 96 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | threads.reserve(thread_count); |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 32 times.
|
110 | for (std::size_t thread_index = 0; thread_index < thread_count; ++thread_index) { |
| 99 | 78 | const std::size_t begin = input.size() * thread_index / thread_count; | |
| 100 | 78 | const std::size_t end = input.size() * (thread_index + 1) / thread_count; | |
| 101 | 78 | threads.emplace_back( | |
| 102 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
156 | [&input, &output, begin, end, minmax]() { StretchRange(input, output, begin, end, minmax.min, minmax.max); }); |
| 103 | } | ||
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 32 times.
|
110 | for (auto &thread : threads) { |
| 106 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | thread.join(); |
| 107 | } | ||
| 108 | |||
| 109 | 32 | return true; | |
| 110 | 32 | } | |
| 111 | |||
| 112 | 32 | bool ZaharovGLinContrStrSTL::PostProcessingImpl() { | |
| 113 | 32 | return !GetOutput().empty(); | |
| 114 | } | ||
| 115 | |||
| 116 | } // namespace zaharov_g_linear_contrast_stretch | ||
| 117 |