GCC Code Coverage Report


Directory: ./
File: tasks/rysev_m_linear_filter_gauss_kernel/omp/src/ops_omp.cpp
Date: 2026-04-02 17:12:27
Exec Total Coverage
Lines: 52 54 96.3%
Functions: 6 7 85.7%
Branches: 23 32 71.9%

Line Branch Exec Source
1 #include "rysev_m_linear_filter_gauss_kernel/omp/include/ops_omp.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 261168 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 2350512 times.
✓ Branch 1 taken 261168 times.
2611680 for (const auto &ke : kKernelElements) {
36 2350512 int nr = row + ke.dr;
37 2350512 int nc = col + ke.dc;
38
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) {
39 2315904 std::size_t idx = (static_cast<std::size_t>(nr) * cols) + nc;
40 2315904 idx = (idx * channels) + channel;
41 2315904 sum += static_cast<float>(input[idx]) * ke.weight;
42 }
43 }
44 261168 return sum;
45 }
46
47 } // namespace
48
49 20 RysevMGaussFilterOMP::RysevMGaussFilterOMP(const InType &in) {
50 SetTypeOfTask(GetStaticTypeOfTask());
51 20 GetInput() = in;
52 GetOutput() = 0;
53 20 }
54
55 20 bool RysevMGaussFilterOMP::ValidationImpl() {
56
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;
57 }
58
59 20 bool RysevMGaussFilterOMP::PreProcessingImpl() {
60
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (GetInput() == 0) {
61 std::string abs_path =
62
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");
63 4 int w = 0;
64 4 int h = 0;
65
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 int ch = 0;
66
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);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (data == nullptr) {
68 return false;
69 }
70 4 width_ = w;
71 4 height_ = h;
72 4 channels_ = STBI_rgb;
73 4 std::ptrdiff_t total_pixels = static_cast<std::ptrdiff_t>(width_) * height_ * channels_;
74
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 input_image_.assign(data, data + total_pixels);
75
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 stbi_image_free(data);
76 } else {
77 int size = GetInput();
78 16 width_ = height_ = size;
79 16 channels_ = 3;
80 16 std::size_t total_pixels = static_cast<std::size_t>(width_) * height_ * channels_;
81 16 input_image_.resize(total_pixels);
82
83 16 std::mt19937 gen(static_cast<unsigned int>(GetInput()));
84 std::uniform_int_distribution<int> dist(0, 255);
85
2/2
✓ Branch 0 taken 261120 times.
✓ Branch 1 taken 16 times.
261136 for (auto &pixel : input_image_) {
86 261120 pixel = static_cast<uint8_t>(dist(gen));
87 }
88 }
89
90 20 output_image_.resize(input_image_.size(), 0);
91 20 return true;
92 }
93
94 void RysevMGaussFilterOMP::ApplyKernelToChannel(int channel, int rows, int cols) {
95 60 const auto &input = input_image_;
96 60 auto &output = output_image_;
97 60 int channels = channels_;
98
99 int row = 0;
100 60 #pragma omp parallel for default(none) shared(rows, cols, channels, input, output, channel) private(row)
101 for (row = 0; row < rows; ++row) {
102 for (int col = 0; col < cols; ++col) {
103 float sum = ComputePixelValue(row, col, channel, rows, cols, channels, input);
104 std::size_t out_idx = (static_cast<std::size_t>(row) * cols) + col;
105 out_idx = (out_idx * channels) + channel;
106 output[out_idx] = static_cast<uint8_t>(std::clamp(sum, 0.0F, 255.0F));
107 }
108 }
109 }
110
111 20 bool RysevMGaussFilterOMP::RunImpl() {
112 20 int rows = height_;
113 20 int cols = width_;
114 20 int ch = channels_;
115
116
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (int channel = 0; channel < ch; ++channel) {
117 ApplyKernelToChannel(channel, rows, cols);
118 }
119
120 20 return true;
121 }
122
123 20 bool RysevMGaussFilterOMP::PostProcessingImpl() {
124 int64_t total = 0;
125
2/2
✓ Branch 0 taken 261168 times.
✓ Branch 1 taken 20 times.
261188 for (uint8_t pixel : output_image_) {
126 261168 total += pixel;
127 }
128 20 GetOutput() = static_cast<int>(total);
129 20 return true;
130 }
131
132 } // namespace rysev_m_linear_filter_gauss_kernel
133