| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozov_n_sobels_filter/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | |||
| 9 | #include "morozov_n_sobels_filter/common/include/common.hpp" | ||
| 10 | #include "util/include/util.hpp" | ||
| 11 | |||
| 12 | namespace morozov_n_sobels_filter { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MorozovNSobelsFilterOMP::MorozovNSobelsFilterOMP(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | |||
| 18 | 20 | result_image_.height = in.height; | |
| 19 | 20 | result_image_.width = in.width; | |
| 20 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | result_image_.pixels.resize(result_image_.height * result_image_.width, 0); |
| 21 | 20 | } | |
| 22 | |||
| 23 | 20 | bool MorozovNSobelsFilterOMP::ValidationImpl() { | |
| 24 | const Image &input = GetInput(); | ||
| 25 |
3/6✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
|
20 | return (input.height == result_image_.height) && (input.width == result_image_.width) && |
| 26 | 20 | (input.pixels.size() == result_image_.pixels.size()); | |
| 27 | } | ||
| 28 | |||
| 29 | 20 | bool MorozovNSobelsFilterOMP::PreProcessingImpl() { | |
| 30 | 20 | return true; | |
| 31 | } | ||
| 32 | |||
| 33 | 20 | bool MorozovNSobelsFilterOMP::RunImpl() { | |
| 34 | const Image &input = GetInput(); | ||
| 35 | 20 | Filter(input); | |
| 36 | GetOutput() = result_image_; | ||
| 37 | 20 | return true; | |
| 38 | } | ||
| 39 | |||
| 40 | 20 | bool MorozovNSobelsFilterOMP::PostProcessingImpl() { | |
| 41 | 20 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 20 | void MorozovNSobelsFilterOMP::Filter(const Image &img) { | |
| 45 | 20 | #pragma omp parallel for schedule(static) default(none) shared(img) num_threads(ppc::util::GetNumThreads()) | |
| 46 | for (size_t id_y = 1; id_y < img.height - 1; id_y++) { | ||
| 47 | for (size_t id_x = 1; id_x < img.width - 1; id_x++) { | ||
| 48 | size_t pixel_id = (id_y * img.width) + id_x; | ||
| 49 | result_image_.pixels[pixel_id] = CalculateNewPixelColor(img, id_x, id_y); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | 20 | } | |
| 53 | |||
| 54 | 532 | uint8_t MorozovNSobelsFilterOMP::CalculateNewPixelColor(const Image &img, size_t x, size_t y) { | |
| 55 | constexpr int kRadX = 1; | ||
| 56 | constexpr int kRadY = 1; | ||
| 57 | 532 | constexpr size_t kZero = 0; | |
| 58 | |||
| 59 | int grad_x = 0; | ||
| 60 | int grad_y = 0; | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 1596 times.
✓ Branch 1 taken 532 times.
|
2128 | for (int row_offset = -kRadY; row_offset <= kRadY; row_offset++) { |
| 63 |
2/2✓ Branch 0 taken 4788 times.
✓ Branch 1 taken 1596 times.
|
6384 | for (int col_offset = -kRadX; col_offset <= kRadX; col_offset++) { |
| 64 | 4788 | size_t id_x = std::clamp(x + col_offset, kZero, img.width - 1); | |
| 65 | 4788 | size_t id_y = std::clamp(y + row_offset, kZero, img.height - 1); | |
| 66 | 4788 | size_t pixel_id = (id_y * img.width) + id_x; | |
| 67 | |||
| 68 | 4788 | grad_x += img.pixels[pixel_id] * kKernelX.at(row_offset + kRadY).at(col_offset + kRadX); | |
| 69 | 4788 | grad_y += img.pixels[pixel_id] * kKernelY.at(row_offset + kRadY).at(col_offset + kRadX); | |
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | 532 | int gradient = static_cast<int>(std::sqrt((grad_x * grad_x) + (grad_y * grad_y))); | |
| 74 | gradient = std::clamp(gradient, 0, 255); | ||
| 75 | |||
| 76 | 532 | return static_cast<uint8_t>(gradient); | |
| 77 | } | ||
| 78 | } // namespace morozov_n_sobels_filter | ||
| 79 |