| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "krykov_e_sobel_op/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <array> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <thread> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "krykov_e_sobel_op/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace krykov_e_sobel_op { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | 96 | int ComputeMagnitude(const std::vector<int> &gray, int w, int row, int col, | |
| 15 | const std::array<std::array<int, 3>, 3> &gx_kernel, | ||
| 16 | const std::array<std::array<int, 3>, 3> &gy_kernel) { | ||
| 17 | int gx = 0; | ||
| 18 | int gy = 0; | ||
| 19 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 96 times.
|
384 | for (int ky = -1; ky <= 1; ++ky) { |
| 20 |
2/2✓ Branch 0 taken 864 times.
✓ Branch 1 taken 288 times.
|
1152 | for (int kx = -1; kx <= 1; ++kx) { |
| 21 | 864 | int pixel = gray[((row + ky) * w) + (col + kx)]; | |
| 22 | 864 | gx += pixel * gx_kernel.at(ky + 1).at(kx + 1); | |
| 23 | 864 | gy += pixel * gy_kernel.at(ky + 1).at(kx + 1); | |
| 24 | } | ||
| 25 | } | ||
| 26 | 96 | return static_cast<int>(std::sqrt(static_cast<double>((gx * gx) + (gy * gy)))); | |
| 27 | } | ||
| 28 | } // namespace | ||
| 29 | |||
| 30 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | KrykovESobelOpSTL::KrykovESobelOpSTL(const InType &in) { |
| 31 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 32 | GetInput() = in; | ||
| 33 | GetOutput().clear(); | ||
| 34 | 24 | } | |
| 35 | |||
| 36 | 24 | bool KrykovESobelOpSTL::ValidationImpl() { | |
| 37 | const auto &img = GetInput(); | ||
| 38 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | return img.width > 2 && img.height > 2 && static_cast<int>(img.data.size()) == img.width * img.height; |
| 39 | } | ||
| 40 | |||
| 41 | 24 | bool KrykovESobelOpSTL::PreProcessingImpl() { | |
| 42 | const auto &img = GetInput(); | ||
| 43 | |||
| 44 | 24 | width_ = img.width; | |
| 45 | 24 | height_ = img.height; | |
| 46 | |||
| 47 | 24 | grayscale_.resize(static_cast<size_t>(width_) * static_cast<size_t>(height_)); | |
| 48 | // RGB → grayscale | ||
| 49 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 24 times.
|
408 | for (int i = 0; i < width_ * height_; ++i) { |
| 50 | 384 | const Pixel &p = img.data[i]; | |
| 51 | 384 | grayscale_[i] = static_cast<int>((0.299 * p.r) + (0.587 * p.g) + (0.114 * p.b)); | |
| 52 | } | ||
| 53 | 24 | GetOutput().assign(static_cast<size_t>(width_) * static_cast<size_t>(height_), 0); | |
| 54 | 24 | return true; | |
| 55 | } | ||
| 56 | |||
| 57 | 24 | bool KrykovESobelOpSTL::RunImpl() { | |
| 58 | 24 | const std::array<std::array<int, 3>, 3> gx_kernel = {{{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}}; | |
| 59 | 24 | const std::array<std::array<int, 3>, 3> gy_kernel = {{{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}}; | |
| 60 | |||
| 61 | auto &output = GetOutput(); | ||
| 62 | 24 | const auto &gray = grayscale_; | |
| 63 | 24 | const int h = height_; | |
| 64 | 24 | const int w = width_; | |
| 65 | |||
| 66 | 24 | unsigned int num_threads = std::thread::hardware_concurrency(); | |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (num_threads == 0) { |
| 68 | num_threads = 2; | ||
| 69 | } | ||
| 70 | |||
| 71 | 24 | const auto total_rows = static_cast<unsigned int>(h - 2); | |
| 72 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (total_rows == 0) { |
| 73 | return true; | ||
| 74 | } | ||
| 75 | |||
| 76 | 24 | const unsigned int rows_per_thread = total_rows / num_threads; | |
| 77 | 24 | const unsigned int remainder = total_rows % num_threads; | |
| 78 | |||
| 79 | 24 | std::vector<std::thread> threads; | |
| 80 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | threads.reserve(num_threads); |
| 81 | |||
| 82 | unsigned int next_start = 1; | ||
| 83 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
|
120 | for (unsigned int i = 0; i < num_threads; ++i) { |
| 84 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
|
96 | unsigned int chunk_rows = rows_per_thread + (i < remainder ? 1 : 0); |
| 85 | unsigned int start_row = next_start; | ||
| 86 | 96 | unsigned int end_row = start_row + chunk_rows; | |
| 87 | next_start = end_row; | ||
| 88 | |||
| 89 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | threads.emplace_back([&, start_row, end_row]() { |
| 90 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 96 times.
|
144 | for (unsigned int row = start_row; row < end_row; ++row) { |
| 91 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (int col = 1; col < w - 1; ++col) { |
| 92 | 96 | output[(row * w) + col] = ComputeMagnitude(gray, w, static_cast<int>(row), col, gx_kernel, gy_kernel); | |
| 93 | } | ||
| 94 | } | ||
| 95 | 96 | }); | |
| 96 | } | ||
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
|
120 | for (auto &t : threads) { |
| 99 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | t.join(); |
| 100 | } | ||
| 101 | |||
| 102 | return true; | ||
| 103 | 24 | } | |
| 104 | |||
| 105 | 24 | bool KrykovESobelOpSTL::PostProcessingImpl() { | |
| 106 | 24 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | } // namespace krykov_e_sobel_op | ||
| 110 |