| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lopatin_a_sobel_operator/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <functional> | ||
| 9 | #include <thread> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "lopatin_a_sobel_operator/common/include/common.hpp" | ||
| 13 | #include "util/include/util.hpp" | ||
| 14 | |||
| 15 | namespace lopatin_a_sobel_operator { | ||
| 16 | |||
| 17 | const std::array<std::array<int, 3>, 3> kSobelX = {std::array<int, 3>{-1, 0, 1}, std::array<int, 3>{-2, 0, 2}, | ||
| 18 | std::array<int, 3>{-1, 0, 1}}; | ||
| 19 | |||
| 20 | const std::array<std::array<int, 3>, 3> kSobelY = {std::array<int, 3>{-1, -2, -1}, std::array<int, 3>{0, 0, 0}, | ||
| 21 | std::array<int, 3>{1, 2, 1}}; | ||
| 22 | |||
| 23 | 54 | void LopatinASobelOperatorSTL::RunSobel(const Image &img, std::size_t start, std::size_t end, | |
| 24 | lopatin_a_sobel_operator::OutType &output) { | ||
| 25 | const auto &input_data = img.pixels; | ||
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 54 times.
|
126 | for (std::size_t j = start; j < end; ++j) { // processing only pixels with a full 3 x 3 neighborhood size |
| 28 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 72 times.
|
352 | for (std::size_t i = 1; i < img.width - 1; ++i) { |
| 29 | int gx = 0; | ||
| 30 | int gy = 0; | ||
| 31 | |||
| 32 |
2/2✓ Branch 0 taken 840 times.
✓ Branch 1 taken 280 times.
|
1120 | for (int ky = -1; ky <= 1; ++ky) { |
| 33 |
2/2✓ Branch 0 taken 2520 times.
✓ Branch 1 taken 840 times.
|
3360 | for (int kx = -1; kx <= 1; ++kx) { |
| 34 | 2520 | std::uint8_t pixel = input_data[((j + ky) * img.width) + (i + kx)]; | |
| 35 | 2520 | gx += pixel * kSobelX.at(ky + 1).at(kx + 1); | |
| 36 | 2520 | gy += pixel * kSobelY.at(ky + 1).at(kx + 1); | |
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 184 times.
|
280 | auto magnitude = static_cast<int>(round(std::sqrt((gx * gx) + (gy * gy)))); |
| 41 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 184 times.
|
376 | output[(j * img.width) + i] = (magnitude > img.threshold) ? magnitude : 0; |
| 42 | } | ||
| 43 | } | ||
| 44 | 54 | } | |
| 45 | |||
| 46 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | LopatinASobelOperatorSTL::LopatinASobelOperatorSTL(const InType &in) : h_(in.height), w_(in.width) { |
| 47 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 48 | GetInput() = in; | ||
| 49 | GetOutput().clear(); | ||
| 50 | 24 | } | |
| 51 | |||
| 52 | 24 | bool LopatinASobelOperatorSTL::ValidationImpl() { | |
| 53 | const auto &input = GetInput(); | ||
| 54 | 24 | return h_ * w_ == input.pixels.size(); | |
| 55 | } | ||
| 56 | |||
| 57 | 24 | bool LopatinASobelOperatorSTL::PreProcessingImpl() { | |
| 58 | 24 | GetOutput().resize(h_ * w_); | |
| 59 | 24 | return true; | |
| 60 | } | ||
| 61 | |||
| 62 | 24 | bool LopatinASobelOperatorSTL::RunImpl() { | |
| 63 | const auto &input = GetInput(); | ||
| 64 | auto &output = GetOutput(); | ||
| 65 | |||
| 66 | 24 | const auto rows = input.height - 2; | |
| 67 |
1/2✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | const auto n_threads = std::min(static_cast<std::size_t>(ppc::util::GetNumThreads()), rows); |
| 68 | |||
| 69 | 24 | std::vector<std::thread> threads; | |
| 70 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | threads.reserve(n_threads); |
| 71 | |||
| 72 | 24 | const std::size_t portion = rows / n_threads; | |
| 73 | 24 | const std::size_t tail = rows % n_threads; | |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 24 times.
|
70 | for (std::size_t i = 0; i < n_threads; ++i) { |
| 76 | 46 | std::size_t start = 1 + (i * portion); | |
| 77 | 46 | std::size_t end = 1 + ((i + 1) * portion); | |
| 78 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | threads.emplace_back(RunSobel, std::cref(input), start, end, std::ref(output)); |
| 79 | } | ||
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 24 times.
|
70 | for (auto &thread : threads) { |
| 82 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | if (thread.joinable()) { |
| 83 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | thread.join(); |
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
|
24 | if (tail != 0) { |
| 88 | 8 | std::size_t start = input.height - 1 - tail; | |
| 89 | 8 | RunSobel(input, start, input.height - 1, output); | |
| 90 | } | ||
| 91 | |||
| 92 | 24 | return true; | |
| 93 | 24 | } | |
| 94 | |||
| 95 | 24 | bool LopatinASobelOperatorSTL::PostProcessingImpl() { | |
| 96 | 24 | return true; | |
| 97 | } | ||
| 98 | |||
| 99 | } // namespace lopatin_a_sobel_operator | ||
| 100 |