-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_annotations_codec_test.cpp
More file actions
300 lines (258 loc) · 10.4 KB
/
Copy pathimage_annotations_codec_test.cpp
File metadata and controls
300 lines (258 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2026 Davide Faconti
// SPDX-License-Identifier: Apache-2.0
#include "pj_base/builtin/image_annotations_codec.hpp"
#include <gtest/gtest.h>
#include <cstdint>
#include <vector>
#include "protobuf_wire_test_helpers.hpp"
namespace PJ {
namespace {
using sdk::AnnotationTopology;
using sdk::CircleAnnotation;
using sdk::ColorRGBA;
using sdk::ImageAnnotations;
using sdk::Point2;
using sdk::PointsAnnotation;
using sdk::TextAnnotation;
namespace pb = ::PJ::test_pb;
// Decode the bytes produced by serializeImageAnnotations back into an
// sdk::ImageAnnotations.
sdk::ImageAnnotations roundTrip(const sdk::ImageAnnotations& input) {
auto bytes = serializeImageAnnotations(input);
auto result = deserializeImageAnnotations(bytes.data(), bytes.size());
EXPECT_TRUE(result.has_value());
return *result;
}
// Compare two ColorRGBA values allowing 1-LSB drift on each channel from the
// double-quantization round-trip (uint8 -> double in [0,1] -> uint8).
::testing::AssertionResult colorEq(const ColorRGBA& a, const ColorRGBA& b) {
auto near = [](uint8_t x, uint8_t y) { return x > y ? (x - y) <= 1 : (y - x) <= 1; };
if (near(a.r, b.r) && near(a.g, b.g) && near(a.b, b.b) && near(a.a, b.a)) {
return ::testing::AssertionSuccess();
}
return ::testing::AssertionFailure() << "Color mismatch: got {" << +b.r << "," << +b.g << "," << +b.b << "," << +b.a
<< "}, expected {" << +a.r << "," << +a.g << "," << +a.b << "," << +a.a << "}";
}
// -----------------------------------------------------------------------------
// 1. Empty input produces empty bytes
// -----------------------------------------------------------------------------
TEST(ImageAnnotationCodecTest, EmptyAnnotationProducesEmptyBytes) {
sdk::ImageAnnotations ia;
auto bytes = serializeImageAnnotations(ia);
EXPECT_TRUE(bytes.empty());
}
// -----------------------------------------------------------------------------
// 2. Golden-byte test: pins the wire format itself, not just round-trip behavior.
// -----------------------------------------------------------------------------
TEST(ImageAnnotationCodecTest, GoldenBytes_SinglePointsAnnotation) {
// Build the canonical input: one PointsAnnotation, kLineLoop, two points,
// outline color = pure red (255, 0, 0, 255), fill = transparent default,
// thickness = 2.0.
sdk::ImageAnnotations ia;
PointsAnnotation pa;
pa.topology = AnnotationTopology::kLineLoop;
pa.points = {{1.0, 2.0}, {3.0, 4.0}};
pa.color = {255, 0, 0, 255};
pa.fill_color = {0, 0, 0, 0};
pa.thickness = 2.0;
ia.points.push_back(std::move(pa));
// Hand-build the expected byte sequence using the standalone pb helpers.
// This is the inverse encoding the reader expects, so getting it byte-for-
// byte right is the strongest available proof.
// Point2 sub-messages.
std::vector<uint8_t> p1;
pb::appendTag(p1, 1, 1);
pb::appendDouble(p1, 1.0);
pb::appendTag(p1, 2, 1);
pb::appendDouble(p1, 2.0);
std::vector<uint8_t> p2;
pb::appendTag(p2, 1, 1);
pb::appendDouble(p2, 3.0);
pb::appendTag(p2, 2, 1);
pb::appendDouble(p2, 4.0);
// Color sub-messages.
std::vector<uint8_t> outline_color;
pb::appendTag(outline_color, 1, 1);
pb::appendDouble(outline_color, 1.0); // r = 255/255
pb::appendTag(outline_color, 2, 1);
pb::appendDouble(outline_color, 0.0);
pb::appendTag(outline_color, 3, 1);
pb::appendDouble(outline_color, 0.0);
pb::appendTag(outline_color, 4, 1);
pb::appendDouble(outline_color, 1.0); // a = 255/255
std::vector<uint8_t> fill_color;
pb::appendTag(fill_color, 1, 1);
pb::appendDouble(fill_color, 0.0);
pb::appendTag(fill_color, 2, 1);
pb::appendDouble(fill_color, 0.0);
pb::appendTag(fill_color, 3, 1);
pb::appendDouble(fill_color, 0.0);
pb::appendTag(fill_color, 4, 1);
pb::appendDouble(fill_color, 0.0);
// PointsAnnotation body.
std::vector<uint8_t> pa_body;
pb::appendTag(pa_body, 2, 0);
pb::appendVarint(pa_body, 2); // kLineLoop = 2
pb::appendTag(pa_body, 3, 2);
pb::appendLenDelim(pa_body, p1);
pb::appendTag(pa_body, 3, 2);
pb::appendLenDelim(pa_body, p2);
pb::appendTag(pa_body, 4, 2);
pb::appendLenDelim(pa_body, outline_color);
pb::appendTag(pa_body, 6, 2);
pb::appendLenDelim(pa_body, fill_color);
pb::appendTag(pa_body, 7, 1);
pb::appendDouble(pa_body, 2.0);
// Top-level ImageAnnotations: one PointsAnnotation at field 2.
std::vector<uint8_t> expected;
pb::appendTag(expected, 2, 2);
pb::appendLenDelim(expected, pa_body);
auto actual = serializeImageAnnotations(ia);
EXPECT_EQ(actual, expected) << "wire format mismatch";
}
// -----------------------------------------------------------------------------
// 3. Round-trip tests: build, serialize, read, compare.
// -----------------------------------------------------------------------------
TEST(ImageAnnotationCodecTest, RoundTrip_LineLoopFourPoints) {
sdk::ImageAnnotations in;
PointsAnnotation pa;
pa.topology = AnnotationTopology::kLineLoop;
pa.points = {{75.0, 185.0}, {125.0, 185.0}, {125.0, 215.0}, {75.0, 215.0}};
pa.color = {0, 255, 0, 255};
pa.fill_color = {0, 0, 0, 0};
pa.thickness = 2.5;
in.points.push_back(std::move(pa));
auto out = roundTrip(in);
ASSERT_EQ(out.points.size(), 1u);
EXPECT_EQ(out.points[0].topology, AnnotationTopology::kLineLoop);
EXPECT_EQ(out.points[0].points, in.points[0].points);
EXPECT_TRUE(colorEq(in.points[0].color, out.points[0].color));
EXPECT_TRUE(colorEq(in.points[0].fill_color, out.points[0].fill_color));
EXPECT_DOUBLE_EQ(out.points[0].thickness, 2.5);
}
TEST(ImageAnnotationCodecTest, RoundTrip_AllTopologies) {
for (auto topology :
{AnnotationTopology::kPoints, AnnotationTopology::kLineList, AnnotationTopology::kLineStrip,
AnnotationTopology::kLineLoop}) {
sdk::ImageAnnotations in;
PointsAnnotation pa;
pa.topology = topology;
pa.points = {{0.0, 0.0}, {10.0, 10.0}};
pa.color = {0, 255, 0, 255};
pa.fill_color = {0, 0, 0, 0};
pa.thickness = 2.0;
in.points.push_back(std::move(pa));
auto out = roundTrip(in);
ASSERT_EQ(out.points.size(), 1u) << "topology=" << static_cast<int>(topology);
EXPECT_EQ(out.points[0].topology, topology);
}
}
TEST(ImageAnnotationCodecTest, RoundTrip_CirclePreservesDiameterRadiusInverse) {
sdk::ImageAnnotations in;
CircleAnnotation ca;
ca.center = {50.0, 60.0};
ca.radius = 10.0; // wire emits diameter = 20; reader halves back to 10.
ca.thickness = 1.5;
ca.color = {0, 255, 0, 255};
ca.fill_color = {255, 0, 0, 128}; // semi-transparent red
in.circles.push_back(std::move(ca));
auto out = roundTrip(in);
ASSERT_EQ(out.circles.size(), 1u);
EXPECT_DOUBLE_EQ(out.circles[0].center.x, 50.0);
EXPECT_DOUBLE_EQ(out.circles[0].center.y, 60.0);
EXPECT_DOUBLE_EQ(out.circles[0].radius, 10.0);
EXPECT_DOUBLE_EQ(out.circles[0].thickness, 1.5);
EXPECT_TRUE(colorEq(in.circles[0].color, out.circles[0].color));
EXPECT_TRUE(colorEq(in.circles[0].fill_color, out.circles[0].fill_color));
}
TEST(ImageAnnotationCodecTest, RoundTrip_TextUtf8) {
sdk::ImageAnnotations in;
TextAnnotation ta;
ta.position = {320.5, 240.25};
ta.font_size = 14.0;
ta.color = {255, 255, 255, 255};
ta.text = "person 0.95 \xe2\x80\x94 \xc3\xa1\xc3\xa9\xc3\xad"; // UTF-8 text
in.texts.push_back(std::move(ta));
auto out = roundTrip(in);
ASSERT_EQ(out.texts.size(), 1u);
EXPECT_EQ(out.texts[0].text, in.texts[0].text);
EXPECT_DOUBLE_EQ(out.texts[0].position.x, 320.5);
EXPECT_DOUBLE_EQ(out.texts[0].position.y, 240.25);
EXPECT_DOUBLE_EQ(out.texts[0].font_size, 14.0);
EXPECT_TRUE(colorEq(in.texts[0].color, out.texts[0].color));
}
TEST(ImageAnnotationCodecTest, RoundTrip_FullImageAnnotationAllThreeKinds) {
sdk::ImageAnnotations in;
// Two points annotations.
PointsAnnotation pa1;
pa1.topology = AnnotationTopology::kLineLoop;
pa1.points = {{0.0, 0.0}, {100.0, 0.0}, {100.0, 100.0}, {0.0, 100.0}};
pa1.color = {255, 128, 64, 255};
pa1.thickness = 3.0;
in.points.push_back(pa1);
PointsAnnotation pa2;
pa2.topology = AnnotationTopology::kLineStrip;
pa2.points = {{50.0, 50.0}, {150.0, 100.0}, {200.0, 200.0}};
pa2.color = {64, 255, 128, 200};
pa2.thickness = 1.0;
in.points.push_back(pa2);
// One circle.
CircleAnnotation ca;
ca.center = {320.0, 240.0};
ca.radius = 5.0;
ca.thickness = 2.0;
ca.color = {0, 0, 255, 255};
ca.fill_color = {0, 0, 255, 64};
in.circles.push_back(ca);
// One text.
TextAnnotation ta;
ta.position = {10.0, 10.0};
ta.font_size = 12.0;
ta.color = {255, 255, 0, 255};
ta.text = "label";
in.texts.push_back(ta);
auto out = roundTrip(in);
ASSERT_EQ(out.points.size(), 2u);
ASSERT_EQ(out.circles.size(), 1u);
ASSERT_EQ(out.texts.size(), 1u);
EXPECT_EQ(out.points[0].topology, AnnotationTopology::kLineLoop);
EXPECT_EQ(out.points[1].topology, AnnotationTopology::kLineStrip);
EXPECT_EQ(out.points[0].points.size(), 4u);
EXPECT_EQ(out.points[1].points.size(), 3u);
EXPECT_DOUBLE_EQ(out.circles[0].radius, 5.0);
EXPECT_EQ(out.texts[0].text, "label");
}
TEST(ImageAnnotationCodecTest, EmptyColorsVectorDoesNotInjectDefaultEntry) {
// A PointsAnnotation with empty `colors` must round-trip to empty `colors`.
// If the writer emitted a default Color for the empty vector, the reader
// would push a phantom entry, breaking per-vertex coloring semantics.
sdk::ImageAnnotations in;
PointsAnnotation pa;
pa.topology = AnnotationTopology::kPoints;
pa.points = {{1.0, 1.0}, {2.0, 2.0}};
pa.colors = {}; // explicitly empty
pa.color = {0, 255, 0, 255};
pa.thickness = 2.0;
in.points.push_back(std::move(pa));
auto out = roundTrip(in);
ASSERT_EQ(out.points.size(), 1u);
EXPECT_TRUE(out.points[0].colors.empty()) << "writer must not emit phantom field-5 entries";
}
TEST(ImageAnnotationCodecTest, RoundTrip_PerVertexColors) {
sdk::ImageAnnotations in;
PointsAnnotation pa;
pa.topology = AnnotationTopology::kLineStrip;
pa.points = {{0.0, 0.0}, {10.0, 10.0}, {20.0, 0.0}};
pa.colors = {{255, 0, 0, 255}, {0, 255, 0, 255}, {0, 0, 255, 255}};
pa.color = {255, 255, 255, 255};
pa.thickness = 2.0;
in.points.push_back(std::move(pa));
auto out = roundTrip(in);
ASSERT_EQ(out.points.size(), 1u);
ASSERT_EQ(out.points[0].colors.size(), 3u);
EXPECT_TRUE(colorEq(in.points[0].colors[0], out.points[0].colors[0]));
EXPECT_TRUE(colorEq(in.points[0].colors[1], out.points[0].colors[1]));
EXPECT_TRUE(colorEq(in.points[0].colors[2], out.points[0].colors[2]));
}
} // namespace
} // namespace PJ