| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lopatin_a_sobel_operator/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <omp.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 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace lopatin_a_sobel_operator { | ||
| 14 | |||
| 15 | const std::array<std::array<int, 3>, 3> kSobelX = {std::array<int, 3>{-1, 0, 1}, std::array<int, 3>{-2, 0, 2}, | ||
| 16 | std::array<int, 3>{-1, 0, 1}}; | ||
| 17 | |||
| 18 | const std::array<std::array<int, 3>, 3> kSobelY = {std::array<int, 3>{-1, -2, -1}, std::array<int, 3>{0, 0, 0}, | ||
| 19 | std::array<int, 3>{1, 2, 1}}; | ||
| 20 | |||
| 21 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | LopatinASobelOperatorOMP::LopatinASobelOperatorOMP(const InType &in) : h_(in.height), w_(in.width) { |
| 22 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 23 | GetInput() = in; | ||
| 24 | GetOutput().clear(); | ||
| 25 | 12 | } | |
| 26 | |||
| 27 | 12 | bool LopatinASobelOperatorOMP::ValidationImpl() { | |
| 28 | const auto &input = GetInput(); | ||
| 29 | 12 | return h_ * w_ == input.pixels.size(); | |
| 30 | } | ||
| 31 | |||
| 32 | 12 | bool LopatinASobelOperatorOMP::PreProcessingImpl() { | |
| 33 | 12 | GetOutput().resize(h_ * w_); | |
| 34 | 12 | return true; | |
| 35 | } | ||
| 36 | |||
| 37 | 12 | bool LopatinASobelOperatorOMP::RunImpl() { | |
| 38 | const auto &input = GetInput(); | ||
| 39 | 12 | const auto &input_data = input.pixels; | |
| 40 | auto &output = GetOutput(); | ||
| 41 | |||
| 42 | 12 | #pragma omp parallel for num_threads(ppc::util::GetNumThreads()) schedule(static) default(none) \ | |
| 43 | shared(kSobelX, kSobelY, input, input_data, output) | ||
| 44 | for (std::size_t j = 1; j < h_ - 1; ++j) { // processing only pixels with a full 3 x 3 neighborhood size | ||
| 45 | for (std::size_t i = 1; i < w_ - 1; ++i) { | ||
| 46 | int gx = 0; | ||
| 47 | int gy = 0; | ||
| 48 | |||
| 49 | for (int ky = -1; ky <= 1; ++ky) { | ||
| 50 | for (int kx = -1; kx <= 1; ++kx) { | ||
| 51 | std::uint8_t pixel = input_data[((j + ky) * w_) + (i + kx)]; | ||
| 52 | gx += pixel * kSobelX.at(ky + 1).at(kx + 1); | ||
| 53 | gy += pixel * kSobelY.at(ky + 1).at(kx + 1); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | auto magnitude = static_cast<int>(round(std::sqrt((gx * gx) + (gy * gy)))); | ||
| 58 | output[(j * w_) + i] = (magnitude > input.threshold) ? magnitude : 0; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | 12 | return true; | |
| 63 | } | ||
| 64 | |||
| 65 | 12 | bool LopatinASobelOperatorOMP::PostProcessingImpl() { | |
| 66 | 12 | return true; | |
| 67 | } | ||
| 68 | |||
| 69 | } // namespace lopatin_a_sobel_operator | ||
| 70 |