| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lopatin_a_sobel_operator/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/tbb.h> | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | |||
| 10 | #include "lopatin_a_sobel_operator/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace lopatin_a_sobel_operator { | ||
| 13 | |||
| 14 | const std::array<std::array<int, 3>, 3> kSobelX = {std::array<int, 3>{-1, 0, 1}, std::array<int, 3>{-2, 0, 2}, | ||
| 15 | std::array<int, 3>{-1, 0, 1}}; | ||
| 16 | |||
| 17 | const std::array<std::array<int, 3>, 3> kSobelY = {std::array<int, 3>{-1, -2, -1}, std::array<int, 3>{0, 0, 0}, | ||
| 18 | std::array<int, 3>{1, 2, 1}}; | ||
| 19 | |||
| 20 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | LopatinASobelOperatorTBB::LopatinASobelOperatorTBB(const InType &in) : h_(in.height), w_(in.width) { |
| 21 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 22 | GetInput() = in; | ||
| 23 | GetOutput().clear(); | ||
| 24 | 12 | } | |
| 25 | |||
| 26 | 12 | bool LopatinASobelOperatorTBB::ValidationImpl() { | |
| 27 | const auto &input = GetInput(); | ||
| 28 | 12 | return h_ * w_ == input.pixels.size(); | |
| 29 | } | ||
| 30 | |||
| 31 | 12 | bool LopatinASobelOperatorTBB::PreProcessingImpl() { | |
| 32 | 12 | GetOutput().resize(h_ * w_); | |
| 33 | 12 | return true; | |
| 34 | } | ||
| 35 | |||
| 36 | 12 | bool LopatinASobelOperatorTBB::RunImpl() { | |
| 37 | const auto &input = GetInput(); | ||
| 38 | 12 | const auto &input_data = input.pixels; | |
| 39 | auto &output = GetOutput(); | ||
| 40 | |||
| 41 | 48 | tbb::parallel_for(tbb::blocked_range<std::size_t>(1, h_ - 1), [&](const tbb::blocked_range<std::size_t> &r) { | |
| 42 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | for (std::size_t j = r.begin(); j < r.end(); ++j) { // processing only pixels with a full 3 x 3 neighborhood size |
| 43 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 36 times.
|
176 | for (std::size_t i = 1; i < w_ - 1; ++i) { |
| 44 | int gx = 0; | ||
| 45 | int gy = 0; | ||
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 420 times.
✓ Branch 1 taken 140 times.
|
560 | for (int ky = -1; ky <= 1; ++ky) { |
| 48 | 420 | std::uint8_t pixel = input_data[((j + ky) * w_) + (i - 1)]; | |
| 49 | 420 | gx += pixel * kSobelX.at(ky + 1).at(0); | |
| 50 | 420 | gy += pixel * kSobelY.at(ky + 1).at(0); | |
| 51 | |||
| 52 | 420 | pixel = input_data[((j + ky) * w_) + (i)]; | |
| 53 | 420 | gx += pixel * kSobelX.at(ky + 1).at(1); | |
| 54 | 420 | gy += pixel * kSobelY.at(ky + 1).at(1); | |
| 55 | |||
| 56 | 420 | pixel = input_data[((j + ky) * w_) + (i + 1)]; | |
| 57 | 420 | gx += pixel * kSobelX.at(ky + 1).at(2); | |
| 58 | 420 | gy += pixel * kSobelY.at(ky + 1).at(2); | |
| 59 | } | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 92 times.
|
140 | auto magnitude = static_cast<int>(round(std::sqrt((gx * gx) + (gy * gy)))); |
| 62 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 92 times.
|
188 | output[(j * w_) + i] = (magnitude > input.threshold) ? magnitude : 0; |
| 63 | } | ||
| 64 | } | ||
| 65 | 48 | }, tbb::simple_partitioner()); | |
| 66 | |||
| 67 | 12 | return true; | |
| 68 | } | ||
| 69 | |||
| 70 | 12 | bool LopatinASobelOperatorTBB::PostProcessingImpl() { | |
| 71 | 12 | return true; | |
| 72 | } | ||
| 73 | |||
| 74 | } // namespace lopatin_a_sobel_operator | ||
| 75 |