| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "rysev_m_linear_filter_gauss_kernel/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <stb/stb_image.h> | ||
| 4 | #include <tbb/tbb.h> | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <array> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <cstdint> | ||
| 10 | #include <random> | ||
| 11 | #include <string> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | #include "rysev_m_linear_filter_gauss_kernel/common/include/common.hpp" | ||
| 15 | #include "util/include/util.hpp" | ||
| 16 | |||
| 17 | namespace rysev_m_linear_filter_gauss_kernel { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | struct KernelElement { | ||
| 21 | int dr; | ||
| 22 | int dc; | ||
| 23 | float weight; | ||
| 24 | }; | ||
| 25 | |||
| 26 | const std::array<KernelElement, 9> kKernelElements = { | ||
| 27 | {KernelElement{.dr = -1, .dc = -1, .weight = 1.0F / 16}, KernelElement{.dr = -1, .dc = 0, .weight = 2.0F / 16}, | ||
| 28 | KernelElement{.dr = -1, .dc = 1, .weight = 1.0F / 16}, KernelElement{.dr = 0, .dc = -1, .weight = 2.0F / 16}, | ||
| 29 | KernelElement{.dr = 0, .dc = 0, .weight = 4.0F / 16}, KernelElement{.dr = 0, .dc = 1, .weight = 2.0F / 16}, | ||
| 30 | KernelElement{.dr = 1, .dc = -1, .weight = 1.0F / 16}, KernelElement{.dr = 1, .dc = 0, .weight = 2.0F / 16}, | ||
| 31 | KernelElement{.dr = 1, .dc = 1, .weight = 1.0F / 16}}}; | ||
| 32 | |||
| 33 | 261168 | float ComputePixelValue(int row, int col, int channel, int rows, int cols, int channels, | |
| 34 | const std::vector<uint8_t> &input) { | ||
| 35 | float sum = 0.0F; | ||
| 36 |
2/2✓ Branch 0 taken 2350512 times.
✓ Branch 1 taken 261168 times.
|
2611680 | for (const auto &ke : kKernelElements) { |
| 37 | 2350512 | int nr = row + ke.dr; | |
| 38 | 2350512 | int nc = col + ke.dc; | |
| 39 |
4/4✓ Branch 0 taken 2333088 times.
✓ Branch 1 taken 17424 times.
✓ Branch 2 taken 2315904 times.
✓ Branch 3 taken 17184 times.
|
2350512 | if (nr >= 0 && nr < rows && nc >= 0 && nc < cols) { |
| 40 | 2315904 | std::size_t idx = (static_cast<std::size_t>(nr) * cols) + nc; | |
| 41 | 2315904 | idx = (idx * channels) + channel; | |
| 42 | 2315904 | sum += static_cast<float>(input[idx]) * ke.weight; | |
| 43 | } | ||
| 44 | } | ||
| 45 | 261168 | return sum; | |
| 46 | } | ||
| 47 | |||
| 48 | } // namespace | ||
| 49 | |||
| 50 | 20 | RysevMGaussFilterTBB::RysevMGaussFilterTBB(const InType &in) { | |
| 51 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 52 | 20 | GetInput() = in; | |
| 53 | GetOutput() = 0; | ||
| 54 | 20 | } | |
| 55 | |||
| 56 | 20 | bool RysevMGaussFilterTBB::ValidationImpl() { | |
| 57 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | return GetInput() >= 0 && GetOutput() == 0; |
| 58 | } | ||
| 59 | |||
| 60 | 20 | bool RysevMGaussFilterTBB::PreProcessingImpl() { | |
| 61 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
|
20 | if (GetInput() == 0) { |
| 62 | std::string abs_path = | ||
| 63 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
8 | ppc::util::GetAbsoluteTaskPath(std::string(PPC_ID_rysev_m_linear_filter_gauss_kernel), "pic.ppm"); |
| 64 | 4 | int w = 0; | |
| 65 | 4 | int h = 0; | |
| 66 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | int ch = 0; |
| 67 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | unsigned char *data = stbi_load(abs_path.c_str(), &w, &h, &ch, STBI_rgb); |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (data == nullptr) { |
| 69 | return false; | ||
| 70 | } | ||
| 71 | 4 | width_ = w; | |
| 72 | 4 | height_ = h; | |
| 73 | 4 | channels_ = STBI_rgb; | |
| 74 | 4 | std::ptrdiff_t total_pixels = static_cast<std::ptrdiff_t>(width_) * height_ * channels_; | |
| 75 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | input_image_.assign(data, data + total_pixels); |
| 76 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | stbi_image_free(data); |
| 77 | } else { | ||
| 78 | int size = GetInput(); | ||
| 79 | 16 | width_ = height_ = size; | |
| 80 | 16 | channels_ = 3; | |
| 81 | 16 | std::size_t total_pixels = static_cast<std::size_t>(width_) * height_ * channels_; | |
| 82 | 16 | input_image_.resize(total_pixels); | |
| 83 | |||
| 84 | 16 | std::mt19937 gen(static_cast<unsigned int>(GetInput())); | |
| 85 | std::uniform_int_distribution<int> dist(0, 255); | ||
| 86 |
2/2✓ Branch 0 taken 261120 times.
✓ Branch 1 taken 16 times.
|
261136 | for (auto &pixel : input_image_) { |
| 87 | 261120 | pixel = static_cast<uint8_t>(dist(gen)); | |
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | 20 | output_image_.resize(input_image_.size(), 0); | |
| 92 | 20 | return true; | |
| 93 | } | ||
| 94 | |||
| 95 | 60 | void RysevMGaussFilterTBB::ApplyKernelToChannel(int channel, int rows, int cols) { | |
| 96 | 60 | const auto &input = input_image_; | |
| 97 | 60 | auto &output = output_image_; | |
| 98 | 60 | int channels = channels_; | |
| 99 | |||
| 100 | 60 | tbb::parallel_for(0, rows, [&](int row) { | |
| 101 |
2/2✓ Branch 0 taken 261168 times.
✓ Branch 1 taken 2904 times.
|
264072 | for (int col = 0; col < cols; ++col) { |
| 102 | 261168 | float sum = ComputePixelValue(row, col, channel, rows, cols, channels, input); | |
| 103 | 261168 | std::size_t out_idx = (static_cast<std::size_t>(row) * cols) + col; | |
| 104 | 261168 | out_idx = (out_idx * channels) + channel; | |
| 105 | 261168 | output[out_idx] = static_cast<uint8_t>(std::clamp(sum, 0.0F, 255.0F)); | |
| 106 | } | ||
| 107 | 2904 | }); | |
| 108 | 60 | } | |
| 109 | |||
| 110 | 20 | bool RysevMGaussFilterTBB::RunImpl() { | |
| 111 | 20 | int rows = height_; | |
| 112 | 20 | int cols = width_; | |
| 113 | 20 | int ch = channels_; | |
| 114 | |||
| 115 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
|
80 | for (int channel = 0; channel < ch; ++channel) { |
| 116 | 60 | ApplyKernelToChannel(channel, rows, cols); | |
| 117 | } | ||
| 118 | |||
| 119 | 20 | return true; | |
| 120 | } | ||
| 121 | |||
| 122 | 20 | bool RysevMGaussFilterTBB::PostProcessingImpl() { | |
| 123 | int64_t total = 0; | ||
| 124 |
2/2✓ Branch 0 taken 261168 times.
✓ Branch 1 taken 20 times.
|
261188 | for (uint8_t pixel : output_image_) { |
| 125 | 261168 | total += pixel; | |
| 126 | } | ||
| 127 | 20 | GetOutput() = static_cast<int>(total); | |
| 128 | 20 | return true; | |
| 129 | } | ||
| 130 | |||
| 131 | } // namespace rysev_m_linear_filter_gauss_kernel | ||
| 132 |