| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "batushin_i_incr_contrast_with_lhs/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/tbb.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "batushin_i_incr_contrast_with_lhs/common/include/common.hpp" | ||
| 12 | #include "oneapi/tbb/parallel_for.h" | ||
| 13 | |||
| 14 | namespace batushin_i_incr_contrast_with_lhs { | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | BatushinIIncrContrastWithLhsTBB::BatushinIIncrContrastWithLhsTBB(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | GetInput() = in; |
| 19 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | GetOutput().resize(in.size()); |
| 20 | 40 | } | |
| 21 | |||
| 22 | 40 | bool BatushinIIncrContrastWithLhsTBB::ValidationImpl() { | |
| 23 | 40 | return !GetInput().empty(); | |
| 24 | } | ||
| 25 | |||
| 26 | 40 | bool BatushinIIncrContrastWithLhsTBB::PreProcessingImpl() { | |
| 27 | 40 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | namespace { | ||
| 31 | |||
| 32 | unsigned char NormalizePixel(unsigned char pixel, unsigned char min_val, double scale_factor) { | ||
| 33 | 108 | double normalized = static_cast<double>(pixel - min_val) * scale_factor; | |
| 34 | 108 | normalized = std::floor(normalized + 0.5); | |
| 35 | normalized = std::max(normalized, 0.0); | ||
| 36 | normalized = std::min(normalized, 255.0); | ||
| 37 | 108 | return static_cast<unsigned char>(normalized); | |
| 38 | } | ||
| 39 | |||
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | std::pair<unsigned char, unsigned char> FindMinMaxParallel(const std::vector<unsigned char> &data) { |
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (data.empty()) { |
| 42 | ✗ | return {0, 0}; | |
| 43 | } | ||
| 44 | |||
| 45 | auto result = tbb::parallel_reduce( | ||
| 46 | 40 | tbb::blocked_range<std::size_t>(0, data.size()), std::make_pair(data[0], data[0]), | |
| 47 | 40 | [&](const tbb::blocked_range<std::size_t> &r, std::pair<unsigned char, unsigned char> local) { | |
| 48 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 144 times.
|
288 | for (std::size_t i = r.begin(); i != r.end(); ++i) { |
| 49 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 93 times.
|
144 | local.first = std::min(local.first, data[i]); |
| 50 | 144 | local.second = std::max(local.second, data[i]); | |
| 51 | } | ||
| 52 | 144 | return local; | |
| 53 | 40 | }, [](const std::pair<unsigned char, unsigned char> &a, const std::pair<unsigned char, unsigned char> &b) { | |
| 54 | return std::make_pair(std::min(a.first, b.first), std::max(a.second, b.second)); | ||
| 55 | }); | ||
| 56 | |||
| 57 | 40 | return result; | |
| 58 | } | ||
| 59 | |||
| 60 | 24 | void NormalizeImage(const std::vector<unsigned char> &source, std::vector<unsigned char> &destination, | |
| 61 | unsigned char min_value, double scale_coefficient) { | ||
| 62 | 24 | tbb::parallel_for(tbb::blocked_range<std::size_t>(0, source.size()), | |
| 63 | 132 | [&](const tbb::blocked_range<std::size_t> &range) { | |
| 64 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 108 times.
|
216 | for (std::size_t i = range.begin(); i != range.end(); ++i) { |
| 65 | 108 | destination[i] = NormalizePixel(source[i], min_value, scale_coefficient); | |
| 66 | } | ||
| 67 | 108 | }); | |
| 68 | 24 | } | |
| 69 | |||
| 70 | void FillUniformImage(std::vector<unsigned char> &output, std::size_t size) { | ||
| 71 | 16 | tbb::parallel_for(tbb::blocked_range<std::size_t>(0, size), [&](const tbb::blocked_range<std::size_t> &range) { | |
| 72 |
2/4✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
72 | for (std::size_t i = range.begin(); i != range.end(); ++i) { |
| 73 | 36 | output[i] = 128; | |
| 74 | } | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | |||
| 78 | } // namespace | ||
| 79 | |||
| 80 | 40 | bool BatushinIIncrContrastWithLhsTBB::RunImpl() { | |
| 81 | const std::vector<unsigned char> &source = GetInput(); | ||
| 82 | std::vector<unsigned char> &destination = GetOutput(); | ||
| 83 | |||
| 84 | 40 | auto min_max = FindMinMaxParallel(source); | |
| 85 | 40 | unsigned char min_value = min_max.first; | |
| 86 | 40 | unsigned char max_value = min_max.second; | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 24 times.
|
40 | if (min_value == max_value) { |
| 89 | FillUniformImage(destination, source.size()); | ||
| 90 | 16 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 | 24 | const double scale_coefficient = 255.0 / static_cast<double>(max_value - min_value); | |
| 94 | 24 | destination.resize(source.size()); | |
| 95 | |||
| 96 | 24 | NormalizeImage(source, destination, min_value, scale_coefficient); | |
| 97 | |||
| 98 | return true; | ||
| 99 | } | ||
| 100 | |||
| 101 | 40 | bool BatushinIIncrContrastWithLhsTBB::PostProcessingImpl() { | |
| 102 | 40 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | } // namespace batushin_i_incr_contrast_with_lhs | ||
| 106 |