| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gutyansky_a_img_contrast_incr/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <limits> | ||
| 7 | #include <thread> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "gutyansky_a_img_contrast_incr/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace gutyansky_a_img_contrast_incr { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | void WaitAll(std::vector<std::thread> &threads) { | ||
| 17 |
6/6✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 320 times.
✓ Branch 5 taken 80 times.
|
800 | for (auto &thread : threads) { |
| 18 |
3/6✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 128 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 320 times.
✗ Branch 8 not taken.
|
640 | thread.join(); |
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | 80 | std::pair<uint8_t, uint8_t> ComputeBounds(const std::vector<uint8_t> &input) { | |
| 23 | 80 | const size_t sz = input.size(); | |
| 24 | 80 | const uint32_t num_threads = std::max(1U, std::thread::hardware_concurrency()); | |
| 25 | 80 | const size_t chunk_sz = (sz + num_threads - 1) / num_threads; | |
| 26 | |||
| 27 | 80 | std::vector<std::thread> threads; | |
| 28 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | threads.reserve(num_threads); |
| 29 | |||
| 30 | std::vector<std::pair<uint8_t, uint8_t>> local_res( | ||
| 31 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | num_threads, std::make_pair(static_cast<uint8_t>(255), static_cast<uint8_t>(0))); |
| 32 | |||
| 33 |
2/2✓ Branch 0 taken 320 times.
✓ Branch 1 taken 80 times.
|
400 | for (uint32_t tid = 0; tid < num_threads; tid++) { |
| 34 | 320 | size_t from = tid * chunk_sz; | |
| 35 |
1/2✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
|
320 | size_t to = std::min(sz, from + chunk_sz); |
| 36 | |||
| 37 |
1/4✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
320 | threads.emplace_back([&local_res, &input, from, to, tid]() { |
| 38 | 320 | uint8_t local_min = 255; | |
| 39 | 320 | uint8_t local_max = 0; | |
| 40 | |||
| 41 |
2/2✓ Branch 0 taken 296 times.
✓ Branch 1 taken 320 times.
|
616 | for (size_t i = from; i < to; ++i) { |
| 42 |
4/4✓ Branch 0 taken 184 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 72 times.
|
480 | local_min = std::min(local_min, input[i]); |
| 43 | 296 | local_max = std::max(local_max, input[i]); | |
| 44 | } | ||
| 45 | |||
| 46 | 320 | local_res[tid] = std::make_pair(local_min, local_max); | |
| 47 | 320 | }); | |
| 48 | } | ||
| 49 | |||
| 50 | WaitAll(threads); | ||
| 51 | |||
| 52 | 80 | uint8_t lower_bound = 255; | |
| 53 | 80 | uint8_t upper_bound = 0; | |
| 54 |
2/2✓ Branch 0 taken 320 times.
✓ Branch 1 taken 80 times.
|
400 | for (const auto &result : local_res) { |
| 55 | 320 | lower_bound = std::min(lower_bound, result.first); | |
| 56 | 320 | upper_bound = std::max(upper_bound, result.second); | |
| 57 | } | ||
| 58 | |||
| 59 | 80 | return std::make_pair(lower_bound, upper_bound); | |
| 60 | 80 | } | |
| 61 | |||
| 62 | 32 | void CopyMultiThread(const std::vector<uint8_t> &input, std::vector<uint8_t> &output) { | |
| 63 | 32 | const size_t sz = input.size(); | |
| 64 | 32 | const uint32_t num_threads = std::max(1U, std::thread::hardware_concurrency()); | |
| 65 | 32 | const size_t chunk_sz = (sz + num_threads - 1) / num_threads; | |
| 66 | |||
| 67 | 32 | std::vector<std::thread> threads; | |
| 68 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | threads.reserve(num_threads); |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
|
160 | for (uint32_t tid = 0; tid < num_threads; tid++) { |
| 71 | 128 | size_t from = tid * chunk_sz; | |
| 72 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | size_t to = std::min(from + chunk_sz, sz); |
| 73 | |||
| 74 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | threads.emplace_back([&output, &input, from, to]() { |
| 75 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 128 times.
|
192 | for (size_t idx = from; idx < to; ++idx) { |
| 76 | 64 | output[idx] = input[idx]; | |
| 77 | } | ||
| 78 | }); | ||
| 79 | } | ||
| 80 | |||
| 81 | WaitAll(threads); | ||
| 82 | 32 | } | |
| 83 | |||
| 84 | 48 | void TransformMultiThread(const std::vector<uint8_t> &input, std::vector<uint8_t> &output, uint8_t lower_bound, | |
| 85 | uint8_t delta) { | ||
| 86 | 48 | const size_t sz = input.size(); | |
| 87 | 48 | const uint32_t num_threads = std::max(1U, std::thread::hardware_concurrency()); | |
| 88 | 48 | const size_t chunk_sz = (sz + num_threads - 1) / num_threads; | |
| 89 | |||
| 90 | 48 | std::vector<std::thread> threads; | |
| 91 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | threads.reserve(num_threads); |
| 92 | |||
| 93 | constexpr uint16_t kMaxUint8 = std::numeric_limits<uint8_t>::max(); | ||
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
|
240 | for (unsigned int tid = 0; tid < num_threads; tid++) { |
| 96 | 192 | size_t from = tid * chunk_sz; | |
| 97 |
1/2✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
|
192 | size_t to = std::min(from + chunk_sz, sz); |
| 98 | |||
| 99 |
1/2✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
|
192 | threads.emplace_back([&output, &input, from, to, lower_bound, delta]() { |
| 100 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 192 times.
|
424 | for (size_t idx = from; idx < to; ++idx) { |
| 101 | 232 | uint16_t old_value = input[idx]; | |
| 102 | 232 | uint16_t new_value = (kMaxUint8 * (old_value - lower_bound)) / delta; | |
| 103 | 232 | output[idx] = static_cast<uint8_t>(new_value); | |
| 104 | } | ||
| 105 | }); | ||
| 106 | } | ||
| 107 | |||
| 108 | WaitAll(threads); | ||
| 109 | 48 | } | |
| 110 | |||
| 111 | } // namespace | ||
| 112 | |||
| 113 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | GutyanskyAImgContrastIncrSTL::GutyanskyAImgContrastIncrSTL(const InType &in) { |
| 114 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 115 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | GetInput() = in; |
| 116 | GetOutput() = {}; | ||
| 117 | 80 | } | |
| 118 | |||
| 119 | 80 | bool GutyanskyAImgContrastIncrSTL::ValidationImpl() { | |
| 120 | 80 | return !GetInput().empty(); | |
| 121 | } | ||
| 122 | |||
| 123 | 80 | bool GutyanskyAImgContrastIncrSTL::PreProcessingImpl() { | |
| 124 | 80 | GetOutput().resize(GetInput().size()); | |
| 125 | 80 | return true; | |
| 126 | } | ||
| 127 | |||
| 128 | 80 | bool GutyanskyAImgContrastIncrSTL::RunImpl() { | |
| 129 | const auto &input = GetInput(); | ||
| 130 | auto &output = GetOutput(); | ||
| 131 | |||
| 132 | 80 | const auto bounds = ComputeBounds(input); | |
| 133 | 80 | const uint8_t delta = bounds.second - bounds.first; | |
| 134 | |||
| 135 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 48 times.
|
80 | if (delta == 0) { |
| 136 | 32 | CopyMultiThread(input, output); | |
| 137 | } else { | ||
| 138 | 48 | TransformMultiThread(input, output, bounds.first, delta); | |
| 139 | } | ||
| 140 | |||
| 141 | 80 | return true; | |
| 142 | } | ||
| 143 | |||
| 144 | 80 | bool GutyanskyAImgContrastIncrSTL::PostProcessingImpl() { | |
| 145 | 80 | return true; | |
| 146 | } | ||
| 147 | |||
| 148 | } // namespace gutyansky_a_img_contrast_incr | ||
| 149 |