-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_codec_test.cpp
More file actions
79 lines (66 loc) · 2.39 KB
/
Copy pathimage_codec_test.cpp
File metadata and controls
79 lines (66 loc) · 2.39 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
// Copyright 2026 Davide Faconti
// SPDX-License-Identifier: Apache-2.0
#include "pj_base/builtin/image_codec.hpp"
#include <gtest/gtest.h>
#include <cstdint>
#include <cstring>
#include <vector>
#include "protobuf_wire_test_helpers.hpp"
namespace PJ {
namespace {
using sdk::Image;
namespace pb = ::PJ::test_pb;
TEST(ImageCodecTest, SchemaName) {
EXPECT_EQ(kSchemaImage, "PJ.Image");
}
TEST(ImageCodecTest, EmptyBufferProducesError) {
EXPECT_FALSE(deserializeImage(nullptr, 0).has_value());
}
TEST(ImageCodecTest, RoundTripRawRGB8) {
Image in;
in.timestamp_ns = 9'000'000'000LL;
in.width = 2;
in.height = 2;
in.encoding = "rgb8";
in.row_step = 6; // 2 px * 3 bytes
in.is_bigendian = false;
in.frame_id = "camera_front";
const std::vector<uint8_t> pixels = {255, 0, 0, 0, 255, 0, 0, 0, 255, 128, 128, 128};
in.data = Span<const uint8_t>(pixels.data(), pixels.size());
const auto bytes = serializeImage(in);
auto out = deserializeImage(bytes.data(), bytes.size());
ASSERT_TRUE(out.has_value());
EXPECT_EQ(out->timestamp_ns, in.timestamp_ns);
EXPECT_EQ(out->width, in.width);
EXPECT_EQ(out->height, in.height);
EXPECT_EQ(out->encoding, in.encoding);
EXPECT_EQ(out->row_step, in.row_step);
EXPECT_FALSE(out->is_bigendian);
EXPECT_EQ(out->frame_id, "camera_front");
EXPECT_FALSE(out->compressed_depth_min.has_value());
EXPECT_FALSE(out->compressed_depth_max.has_value());
ASSERT_EQ(out->data.size(), pixels.size());
EXPECT_EQ(std::memcmp(out->data.data(), pixels.data(), pixels.size()), 0);
}
TEST(ImageCodecTest, RoundTripCompressedDepthWithRange) {
Image in;
in.timestamp_ns = 0;
in.width = 320;
in.height = 240;
in.encoding = "compressedDepth";
in.compressed_depth_min = 0.5f;
in.compressed_depth_max = 10.0f;
const std::vector<uint8_t> payload = {0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A};
in.data = Span<const uint8_t>(payload.data(), payload.size());
const auto bytes = serializeImage(in);
auto out = deserializeImage(bytes.data(), bytes.size());
ASSERT_TRUE(out.has_value());
EXPECT_EQ(out->encoding, "compressedDepth");
ASSERT_TRUE(out->compressed_depth_min.has_value());
ASSERT_TRUE(out->compressed_depth_max.has_value());
EXPECT_FLOAT_EQ(*out->compressed_depth_min, 0.5f);
EXPECT_FLOAT_EQ(*out->compressed_depth_max, 10.0f);
EXPECT_TRUE(out->frame_id.empty()); // unset frame_id round-trips as empty
}
} // namespace
} // namespace PJ