GCC Code Coverage Report


Directory: ./
File: tasks/ivanova_p_marking_components_on_binary_image/data/image_generator.hpp
Date: 2026-04-02 17:12:27
Exec Total Coverage
Lines: 81 90 90.0%
Functions: 16 16 100.0%
Branches: 136 148 91.9%

Line Branch Exec Source
1 #pragma once
2
3 #include <cstddef>
4 #include <cstdint>
5 #include <fstream>
6 #include <ios>
7 #include <string>
8
9 #include "ivanova_p_marking_components_on_binary_image/common/include/common.hpp"
10
11 namespace ivanova_p_marking_components_on_binary_image {
12 // Функция для загрузки изображения из текстового файла
13 96 inline Image LoadImageFromTxt(const std::string &filename) {
14 96 Image img;
15
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 std::ifstream file(filename);
16
17
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (!file.is_open()) {
18 img.width = 0;
19 img.height = 0;
20 return img;
21 }
22
23
2/4
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
96 file >> img.width >> img.height;
24
25
2/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
96 if (img.width <= 0 || img.height <= 0) {
26 img.width = 0;
27 img.height = 0;
28 return img;
29 }
30
31
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 img.data.resize(static_cast<size_t>(img.width) * static_cast<size_t>(img.height));
32
33
2/2
✓ Branch 0 taken 3384 times.
✓ Branch 1 taken 96 times.
3480 for (size_t i = 0; i < img.data.size(); ++i) {
34 3384 int pixel_value = 0;
35
2/4
✓ Branch 1 taken 3384 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3384 times.
3384 if (!(file >> pixel_value)) {
36 img.width = 0;
37 img.height = 0;
38 return img;
39 }
40 // Преобразуем в бинарное: если пиксель темный (< 128), то 1, иначе 0
41 // 0 - объект (черный), 255 - фон (белый)
42
2/2
✓ Branch 0 taken 2352 times.
✓ Branch 1 taken 1032 times.
5736 img.data[i] = (pixel_value < 128) ? 1 : 0;
43 }
44
45
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 file.close();
46 return img;
47 96 }
48
49 // Вспомогательные функции для создания тестовых изображений
50 namespace test_image_helpers {
51
52 inline bool IsPixelInTestCase1(int xx, int yy, int width, int height) {
53
6/6
✓ Branch 0 taken 117600 times.
✓ Branch 1 taken 60000 times.
✓ Branch 2 taken 87024 times.
✓ Branch 3 taken 30576 times.
✓ Branch 4 taken 29400 times.
✓ Branch 5 taken 57624 times.
177600 return xx > width / 4 && xx < (3 * width) / 4 && yy > height / 4 && yy < (3 * height) / 4;
54 }
55
56 240000 inline bool IsPixelInTestCase2(int xx, int yy, int width, int height) {
57
8/8
✓ Branch 0 taken 208800 times.
✓ Branch 1 taken 31200 times.
✓ Branch 2 taken 57600 times.
✓ Branch 3 taken 151200 times.
✓ Branch 4 taken 50112 times.
✓ Branch 5 taken 7488 times.
✓ Branch 6 taken 36288 times.
✓ Branch 7 taken 13824 times.
240000 return (xx > width / 8 && xx < (3 * width) / 8 && yy > height / 8 && yy < (3 * height) / 8) ||
58
8/8
✓ Branch 0 taken 88800 times.
✓ Branch 1 taken 137376 times.
✓ Branch 2 taken 57600 times.
✓ Branch 3 taken 31200 times.
✓ Branch 4 taken 21312 times.
✓ Branch 5 taken 36288 times.
✓ Branch 6 taken 13824 times.
✓ Branch 7 taken 7488 times.
226176 (xx > (5 * width) / 8 && xx < (7 * width) / 8 && yy > (5 * height) / 8 && yy < (7 * height) / 8);
59 }
60
61 240000 inline bool IsPixelInTestCase3(int xx, int yy, int width, int height) {
62
6/6
✓ Branch 0 taken 45600 times.
✓ Branch 1 taken 168000 times.
✓ Branch 2 taken 40584 times.
✓ Branch 3 taken 5016 times.
✓ Branch 4 taken 31920 times.
✓ Branch 5 taken 8664 times.
213600 return (xx > width / 10 && xx < (3 * width) / 10 && yy > height / 10 && yy < (3 * height) / 10) ||
63
10/10
✓ Branch 0 taken 213600 times.
✓ Branch 1 taken 26400 times.
✓ Branch 2 taken 141600 times.
✓ Branch 3 taken 89736 times.
✓ Branch 4 taken 45600 times.
✓ Branch 5 taken 96000 times.
✓ Branch 6 taken 26904 times.
✓ Branch 7 taken 18696 times.
✓ Branch 8 taken 8664 times.
✓ Branch 9 taken 18240 times.
444936 (xx > (4 * width) / 10 && xx < (6 * width) / 10 && yy > (4 * height) / 10 && yy < (6 * height) / 10) ||
64
8/8
✓ Branch 0 taken 69600 times.
✓ Branch 1 taken 153072 times.
✓ Branch 2 taken 45600 times.
✓ Branch 3 taken 24000 times.
✓ Branch 4 taken 13224 times.
✓ Branch 5 taken 32376 times.
✓ Branch 6 taken 8664 times.
✓ Branch 7 taken 4560 times.
222672 (xx > (7 * width) / 10 && xx < (9 * width) / 10 && yy > (7 * height) / 10 && yy < (9 * height) / 10);
65 }
66
67 240000 inline bool IsPixelInTestCase4(int xx, int yy, int width, int height) {
68
6/6
✓ Branch 0 taken 148800 times.
✓ Branch 1 taken 9600 times.
✓ Branch 2 taken 14208 times.
✓ Branch 3 taken 4992 times.
✓ Branch 4 taken 4800 times.
✓ Branch 5 taken 9408 times.
168000 return (xx > width / 3 && xx < ((width / 3) + 5) && yy > height / 4 && yy < (3 * height) / 4) ||
69
8/8
✓ Branch 0 taken 158400 times.
✓ Branch 1 taken 81600 times.
✓ Branch 2 taken 79200 times.
✓ Branch 3 taken 151200 times.
✓ Branch 4 taken 9600 times.
✓ Branch 5 taken 69600 times.
✓ Branch 6 taken 148992 times.
✓ Branch 7 taken 81600 times.
398592 (xx > (2 * width) / 3 && xx < (((2 * width) / 3) + 5) && yy > height / 4 && yy < (3 * height) / 4) ||
70
6/6
✓ Branch 0 taken 79392 times.
✓ Branch 1 taken 69600 times.
✓ Branch 2 taken 40296 times.
✓ Branch 3 taken 39096 times.
✓ Branch 4 taken 2088 times.
✓ Branch 5 taken 38208 times.
148992 (xx > width / 3 && xx < (((2 * width) / 3) + 5) && yy > ((height / 2) - 2) && yy < ((height / 2) + 2));
71 }
72
73 240000 inline bool IsPixelInTestCase7(int xx, int yy, int width, int height) {
74
4/4
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 235200 times.
✓ Branch 2 taken 4752 times.
✓ Branch 3 taken 48 times.
240000 return (xx == width / 2 && yy == height / 4) || (xx == (3 * width) / 4 && yy == height / 4) ||
75
10/10
✓ Branch 0 taken 237600 times.
✓ Branch 1 taken 2400 times.
✓ Branch 2 taken 2400 times.
✓ Branch 3 taken 237552 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 2376 times.
✓ Branch 6 taken 2376 times.
✓ Branch 7 taken 237552 times.
✓ Branch 8 taken 24 times.
✓ Branch 9 taken 2352 times.
479952 (xx == width / 4 && yy == (3 * height) / 4) || (xx == (3 * width) / 4 && yy == (3 * height) / 4);
76 }
77
78 inline bool IsPixelInTestCase8(int xx, int yy, int width, int height) {
79 240000 int cell_width = width / 3;
80 240000 int cell_height = height / 3;
81 240000 int local_x = xx % cell_width;
82 240000 int local_y = yy % cell_height;
83
4/4
✓ Branch 0 taken 108000 times.
✓ Branch 1 taken 64800 times.
✓ Branch 2 taken 77760 times.
✓ Branch 3 taken 30240 times.
172800 return local_x > cell_width / 4 && local_x < (3 * cell_width) / 4 && local_y > cell_height / 4 &&
84
2/2
✓ Branch 0 taken 29160 times.
✓ Branch 1 taken 48600 times.
77760 local_y < (3 * cell_height) / 4;
85 }
86
87 } // namespace test_image_helpers
88
89 // Вспомогательные функции для каждого тестового случая
90 namespace test_case_generators {
91
92
2/2
✓ Branch 0 taken 177600 times.
✓ Branch 1 taken 62400 times.
240000 inline uint8_t GetPixelForTestCase1(int xx, int yy, int width, int height) {
93 240000 return test_image_helpers::IsPixelInTestCase1(xx, yy, width, height) ? 1 : 0;
94 }
95
96 240000 inline uint8_t GetPixelForTestCase2(int xx, int yy, int width, int height) {
97
2/2
✓ Branch 0 taken 212352 times.
✓ Branch 1 taken 27648 times.
240000 return test_image_helpers::IsPixelInTestCase2(xx, yy, width, height) ? 1 : 0;
98 }
99
100 240000 inline uint8_t GetPixelForTestCase3(int xx, int yy, int width, int height) {
101
2/2
✓ Branch 0 taken 214008 times.
✓ Branch 1 taken 25992 times.
240000 return test_image_helpers::IsPixelInTestCase3(xx, yy, width, height) ? 1 : 0;
102 }
103
104 240000 inline uint8_t GetPixelForTestCase4(int xx, int yy, int width, int height) {
105
2/2
✓ Branch 0 taken 228504 times.
✓ Branch 1 taken 11496 times.
240000 return test_image_helpers::IsPixelInTestCase4(xx, yy, width, height) ? 1 : 0;
106 }
107
108 320000 inline uint8_t GetPixelForTestCase5(int /*xx*/, int /*yy*/, int /*width*/, int /*height*/) {
109 320000 return 0;
110 }
111
112 240000 inline uint8_t GetPixelForTestCase6(int xx, int yy, int width, int height) {
113
4/4
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 237600 times.
✓ Branch 2 taken 2376 times.
✓ Branch 3 taken 24 times.
240000 return (xx == width / 2 && yy == height / 2) ? 1 : 0;
114 }
115
116 240000 inline uint8_t GetPixelForTestCase7(int xx, int yy, int width, int height) {
117
2/2
✓ Branch 0 taken 239904 times.
✓ Branch 1 taken 96 times.
240000 return test_image_helpers::IsPixelInTestCase7(xx, yy, width, height) ? 1 : 0;
118 }
119
120
2/2
✓ Branch 0 taken 172800 times.
✓ Branch 1 taken 67200 times.
240000 inline uint8_t GetPixelForTestCase8(int xx, int yy, int width, int height) {
121 240000 return test_image_helpers::IsPixelInTestCase8(xx, yy, width, height) ? 1 : 0;
122 }
123
124 240000 inline uint8_t GetPixelForTestCase9(int xx, int yy, int width, int height) {
125 (void)xx;
126 (void)width;
127
2/2
✓ Branch 0 taken 237600 times.
✓ Branch 1 taken 2400 times.
240000 return (yy == height / 2) ? 1 : 0;
128 }
129
130 240000 inline uint8_t GetPixelForTestCase10(int xx, int yy, int width, int height) {
131 (void)yy;
132 (void)height;
133
2/2
✓ Branch 0 taken 237600 times.
✓ Branch 1 taken 2400 times.
240000 return (xx == width / 2) ? 1 : 0;
134 }
135
136 } // namespace test_case_generators
137
138 // Вспомогательная функция для создания тестовых изображений
139 248 inline Image CreateTestImage(int width, int height, int test_case) {
140 248 Image img;
141 248 img.width = width;
142 248 img.height = height;
143
1/2
✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
248 img.data.resize(static_cast<size_t>(width) * static_cast<size_t>(height));
144
145 // Выбираем функцию-генератор в зависимости от test_case
146 uint8_t (*generator)(int, int, int, int) = nullptr;
147
148
10/10
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 24 times.
✓ Branch 7 taken 24 times.
✓ Branch 8 taken 24 times.
✓ Branch 9 taken 24 times.
248 switch (test_case) {
149 case 1:
150 generator = test_case_generators::GetPixelForTestCase1;
151 break;
152 24 case 2:
153 generator = test_case_generators::GetPixelForTestCase2;
154 24 break;
155 24 case 3:
156 generator = test_case_generators::GetPixelForTestCase3;
157 24 break;
158 24 case 4:
159 generator = test_case_generators::GetPixelForTestCase4;
160 24 break;
161 case 5:
162 generator = test_case_generators::GetPixelForTestCase5;
163 break;
164 24 case 6:
165 generator = test_case_generators::GetPixelForTestCase6;
166 24 break;
167 24 case 7:
168 generator = test_case_generators::GetPixelForTestCase7;
169 24 break;
170 24 case 8:
171 generator = test_case_generators::GetPixelForTestCase8;
172 24 break;
173 24 case 9:
174 generator = test_case_generators::GetPixelForTestCase9;
175 24 break;
176 24 case 10:
177 generator = test_case_generators::GetPixelForTestCase10;
178 24 break;
179 default:
180 generator = test_case_generators::GetPixelForTestCase5; // фон по умолчанию
181 break;
182 }
183
184
2/2
✓ Branch 0 taken 24800 times.
✓ Branch 1 taken 248 times.
25048 for (int yy = 0; yy < height; ++yy) {
185
2/2
✓ Branch 0 taken 2480000 times.
✓ Branch 1 taken 24800 times.
2504800 for (int xx = 0; xx < width; ++xx) {
186 2480000 int idx = (yy * width) + xx;
187
1/2
✓ Branch 1 taken 2480000 times.
✗ Branch 2 not taken.
2480000 img.data[idx] = generator(xx, yy, width, height);
188 }
189 }
190
191 248 return img;
192 }
193 } // namespace ivanova_p_marking_components_on_binary_image
194