| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "batkov_f_contrast_enh_lin_hist_stretch/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <oneapi/tbb.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <limits> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "batkov_f_contrast_enh_lin_hist_stretch/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace batkov_f_contrast_enh_lin_hist_stretch { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | 12 | std::pair<uint8_t, uint8_t> FindMinMaxParallel(const std::vector<uint8_t> &input) { | |
| 20 | using MinMax = std::pair<uint8_t, uint8_t>; | ||
| 21 | 12 | const MinMax identity{std::numeric_limits<uint8_t>::max(), std::numeric_limits<uint8_t>::min()}; | |
| 22 | |||
| 23 | 12 | return oneapi::tbb::parallel_reduce(oneapi::tbb::blocked_range<size_t>(0, input.size()), identity, | |
| 24 | 12 | [&](const oneapi::tbb::blocked_range<size_t> &r, MinMax mm) -> MinMax { | |
| 25 |
4/4✓ Branch 0 taken 352094 times.
✓ Branch 1 taken 415 times.
✓ Branch 2 taken 5696482 times.
✓ Branch 3 taken 4331 times.
|
6053322 | for (size_t i = r.begin(); i != r.end(); ++i) { |
| 26 | 6048576 | const uint8_t v = input[i]; | |
| 27 | mm.first = std::min(mm.first, v); | ||
| 28 | mm.second = std::max(mm.second, v); | ||
| 29 | } | ||
| 30 | return mm; | ||
| 31 | 12 | }, [](const MinMax &a, const MinMax &b) -> MinMax { | |
| 32 | return {std::min(a.first, b.first), std::max(a.second, b.second)}; | ||
| 33 | 12 | }); | |
| 34 | } | ||
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | std::pair<uint8_t, uint8_t> FindMinMax(const std::vector<uint8_t> &input, size_t parallel_threshold) { |
| 37 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (input.size() < parallel_threshold) { |
| 38 | 12 | const auto [min_it, max_it] = std::ranges::minmax_element(input.begin(), input.end()); | |
| 39 | 12 | return {*min_it, *max_it}; | |
| 40 | } | ||
| 41 | 12 | return FindMinMaxParallel(input); | |
| 42 | } | ||
| 43 | |||
| 44 | } // namespace | ||
| 45 | |||
| 46 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | BatkovFContrastEnhLinHistStretchTBB::BatkovFContrastEnhLinHistStretchTBB(const InType &in) { |
| 47 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 48 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetInput() = in; |
| 49 | 24 | } | |
| 50 | |||
| 51 | 24 | bool BatkovFContrastEnhLinHistStretchTBB::ValidationImpl() { | |
| 52 | 24 | return !GetInput().empty(); | |
| 53 | } | ||
| 54 | |||
| 55 | 24 | bool BatkovFContrastEnhLinHistStretchTBB::PreProcessingImpl() { | |
| 56 | 24 | GetOutput().resize(GetInput().size()); | |
| 57 | 24 | return true; | |
| 58 | } | ||
| 59 | |||
| 60 | 24 | bool BatkovFContrastEnhLinHistStretchTBB::RunImpl() { | |
| 61 | auto &input = GetInput(); | ||
| 62 | auto &output = GetOutput(); | ||
| 63 | |||
| 64 | constexpr size_t kParallelMinMaxThreshold = 100000; | ||
| 65 | 24 | const auto [min_el, max_el] = FindMinMax(input, kParallelMinMaxThreshold); | |
| 66 | |||
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (max_el == min_el) { |
| 68 | std::ranges::copy(input.begin(), input.end(), output.begin()); | ||
| 69 | ✗ | return true; | |
| 70 | } | ||
| 71 | |||
| 72 | 24 | const double a = 255.0 / static_cast<double>(max_el - min_el); | |
| 73 | 24 | const double b = -a * static_cast<double>(min_el); | |
| 74 | |||
| 75 | 24 | std::array<uint8_t, 256> lut{}; | |
| 76 |
2/2✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 24 times.
|
6168 | for (size_t i = 0; i < lut.size(); ++i) { |
| 77 | 6144 | const double new_pixel = (a * static_cast<double>(i)) + b; | |
| 78 | 6144 | lut.at(i) = static_cast<uint8_t>(std::clamp(new_pixel, 0.0, 255.0)); | |
| 79 | } | ||
| 80 | |||
| 81 | constexpr size_t kGrain = 1 << 17; | ||
| 82 | oneapi::tbb::static_partitioner part; | ||
| 83 | |||
| 84 | 24 | oneapi::tbb::parallel_for(oneapi::tbb::blocked_range<size_t>(0, input.size(), kGrain), | |
| 85 | 24 | [&](const oneapi::tbb::blocked_range<size_t> &r) { | |
| 86 | 44 | const uint8_t *src = input.data() + r.begin(); | |
| 87 | 44 | uint8_t *dst = output.data() + r.begin(); | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 6360720 times.
✓ Branch 1 taken 44 times.
|
6360764 | for (size_t i = 0; i < r.size(); ++i) { |
| 90 | 6360720 | dst[i] = lut.at(src[i]); | |
| 91 | } | ||
| 92 | }, part); | ||
| 93 | |||
| 94 | 24 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | 24 | bool BatkovFContrastEnhLinHistStretchTBB::PostProcessingImpl() { | |
| 98 | 24 | return !GetOutput().empty(); | |
| 99 | } | ||
| 100 | |||
| 101 | } // namespace batkov_f_contrast_enh_lin_hist_stretch | ||
| 102 |