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