| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozov_n_sobels_filter/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <thread> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "morozov_n_sobels_filter/common/include/common.hpp" | ||
| 13 | #include "util/include/util.hpp" | ||
| 14 | |||
| 15 | namespace morozov_n_sobels_filter { | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MorozovNSobelsFilterSTL::MorozovNSobelsFilterSTL(const InType &in) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 | GetInput() = in; | ||
| 20 | |||
| 21 | 40 | result_image_.height = in.height; | |
| 22 | 40 | result_image_.width = in.width; | |
| 23 |
1/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | result_image_.pixels.resize(result_image_.height * result_image_.width, 0); |
| 24 | 40 | } | |
| 25 | |||
| 26 | 40 | bool MorozovNSobelsFilterSTL::ValidationImpl() { | |
| 27 | const Image &input = GetInput(); | ||
| 28 |
3/6✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 40 times.
|
40 | return (input.height == result_image_.height) && (input.width == result_image_.width) && |
| 29 | 40 | (input.pixels.size() == result_image_.pixels.size()); | |
| 30 | } | ||
| 31 | |||
| 32 | 40 | bool MorozovNSobelsFilterSTL::PreProcessingImpl() { | |
| 33 | 40 | return true; | |
| 34 | } | ||
| 35 | |||
| 36 | 40 | bool MorozovNSobelsFilterSTL::RunImpl() { | |
| 37 | const Image &input = GetInput(); | ||
| 38 | |||
| 39 | 40 | const int k_num_threads = ppc::util::GetNumThreads(); | |
| 40 | 40 | std::vector<std::thread> threads; | |
| 41 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | threads.reserve((k_num_threads)); |
| 42 | |||
| 43 | size_t start_row = 1; | ||
| 44 | 40 | size_t end_row = input.height - 1; | |
| 45 | 40 | size_t total_rows = end_row - start_row; | |
| 46 | |||
| 47 | 40 | size_t rows_per_thread = total_rows / k_num_threads; | |
| 48 | 40 | size_t remaining_rows = total_rows % k_num_threads; | |
| 49 | |||
| 50 | size_t current_start = start_row; | ||
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 40 times.
|
140 | for (int i = 0; i < k_num_threads; i++) { |
| 53 | 100 | size_t num_rows = rows_per_thread + (std::cmp_less(i, remaining_rows) ? 1 : 0); | |
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 14 times.
|
100 | if (num_rows > 0) { |
| 56 |
1/2✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
|
172 | threads.emplace_back([this, &input, current_start, num_rows]() { this->Filter(input, current_start, num_rows); }); |
| 57 | } | ||
| 58 | |||
| 59 | 100 | current_start += num_rows; | |
| 60 | } | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 40 times.
|
126 | for (auto &thread : threads) { |
| 63 |
1/2✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 | if (thread.joinable()) { |
| 64 |
1/2✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
|
86 | thread.join(); |
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | GetOutput() = result_image_; | ||
| 69 | 40 | return true; | |
| 70 | 40 | } | |
| 71 | |||
| 72 | 40 | bool MorozovNSobelsFilterSTL::PostProcessingImpl() { | |
| 73 | 40 | return true; | |
| 74 | } | ||
| 75 | |||
| 76 | 86 | void MorozovNSobelsFilterSTL::Filter(const Image &img, size_t start_row, size_t num_rows) { | |
| 77 | 86 | size_t end_row = start_row + num_rows; | |
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 184 times.
✓ Branch 1 taken 86 times.
|
270 | for (size_t id_y = start_row; id_y < end_row; id_y++) { |
| 80 |
2/2✓ Branch 0 taken 1064 times.
✓ Branch 1 taken 184 times.
|
1248 | for (size_t id_x = 1; id_x < img.width - 1; id_x++) { |
| 81 | 1064 | size_t pixel_id = (id_y * img.width) + id_x; | |
| 82 | 1064 | result_image_.pixels[pixel_id] = CalculateNewPixelColor(img, id_x, id_y); | |
| 83 | } | ||
| 84 | } | ||
| 85 | 86 | } | |
| 86 | |||
| 87 | 1064 | uint8_t MorozovNSobelsFilterSTL::CalculateNewPixelColor(const Image &img, size_t x, size_t y) { | |
| 88 | constexpr int kRadX = 1; | ||
| 89 | constexpr int kRadY = 1; | ||
| 90 | 1064 | constexpr size_t kZero = 0; | |
| 91 | |||
| 92 | int grad_x = 0; | ||
| 93 | int grad_y = 0; | ||
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 3192 times.
✓ Branch 1 taken 1064 times.
|
4256 | for (int row_offset = -kRadY; row_offset <= kRadY; row_offset++) { |
| 96 |
2/2✓ Branch 0 taken 9576 times.
✓ Branch 1 taken 3192 times.
|
12768 | for (int col_offset = -kRadX; col_offset <= kRadX; col_offset++) { |
| 97 | 9576 | size_t id_x = std::clamp(x + col_offset, kZero, img.width - 1); | |
| 98 | 9576 | size_t id_y = std::clamp(y + row_offset, kZero, img.height - 1); | |
| 99 | 9576 | size_t pixel_id = (id_y * img.width) + id_x; | |
| 100 | |||
| 101 | 9576 | grad_x += img.pixels[pixel_id] * kKernelX.at(row_offset + kRadY).at(col_offset + kRadX); | |
| 102 | 9576 | grad_y += img.pixels[pixel_id] * kKernelY.at(row_offset + kRadY).at(col_offset + kRadX); | |
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | 1064 | int gradient = static_cast<int>(std::sqrt((grad_x * grad_x) + (grad_y * grad_y))); | |
| 107 | gradient = std::clamp(gradient, 0, 255); | ||
| 108 | |||
| 109 | 1064 | return static_cast<uint8_t>(gradient); | |
| 110 | } | ||
| 111 | } // namespace morozov_n_sobels_filter | ||
| 112 |