-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_codec.cpp
More file actions
157 lines (141 loc) · 3.93 KB
/
Copy pathimage_codec.cpp
File metadata and controls
157 lines (141 loc) · 3.93 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
// Copyright 2026 Davide Faconti
// SPDX-License-Identifier: Apache-2.0
#include "pj_base/builtin/image_codec.hpp"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "geometry_codec.hpp"
#include "protobuf_wire.hpp"
namespace PJ {
namespace {
using builtin_wire::parseFields;
using builtin_wire::Reader;
using builtin_wire::Tag;
using builtin_wire::WireType;
using builtin_wire::Writer;
using sdk::Image;
bool readBytesIntoImage(Reader& reader, Image& out) {
const uint8_t* data = nullptr;
size_t size = 0;
if (!reader.readBytes(data, size)) {
return false;
}
auto owned = std::make_shared<std::vector<uint8_t>>(data, data + size);
out.data = Span<const uint8_t>(owned->data(), owned->size());
out.anchor = owned;
return true;
}
} // namespace
std::vector<uint8_t> serializeImage(const Image& image) {
std::vector<uint8_t> out;
Writer writer(out);
writer.message(1, [&](Writer& nested) { builtin_wire::writeTimestamp(nested, image.timestamp_ns); });
writer.varint(2, image.width);
writer.varint(3, image.height);
writer.string(4, image.encoding);
writer.varint(5, image.row_step);
writer.varint(6, image.is_bigendian ? 1u : 0u);
writer.bytes(7, image.data.data(), image.data.size());
if (image.compressed_depth_min.has_value()) {
writer.floatField(8, *image.compressed_depth_min);
}
if (image.compressed_depth_max.has_value()) {
writer.floatField(9, *image.compressed_depth_max);
}
writer.string(10, image.frame_id);
return out;
}
Expected<sdk::Image> deserializeImage(const uint8_t* data, size_t size) {
if (data == nullptr || size == 0) {
return unexpected(std::string("Image wire: empty buffer"));
}
Reader reader(data, size);
sdk::Image image;
const bool ok = parseFields(reader, [&](Tag tag, Reader& r) {
switch (tag.field) {
case 1:
return tag.type == WireType::kLengthDelimited && builtin_wire::readTimestampMessage(r, image.timestamp_ns);
case 2: {
if (tag.type != WireType::kVarint) {
return false;
}
uint64_t v = 0;
if (!r.readVarint(v)) {
return false;
}
image.width = static_cast<uint32_t>(v);
return true;
}
case 3: {
if (tag.type != WireType::kVarint) {
return false;
}
uint64_t v = 0;
if (!r.readVarint(v)) {
return false;
}
image.height = static_cast<uint32_t>(v);
return true;
}
case 4:
return tag.type == WireType::kLengthDelimited && r.readString(image.encoding);
case 5: {
if (tag.type != WireType::kVarint) {
return false;
}
uint64_t v = 0;
if (!r.readVarint(v)) {
return false;
}
image.row_step = static_cast<uint32_t>(v);
return true;
}
case 6: {
if (tag.type != WireType::kVarint) {
return false;
}
uint64_t v = 0;
if (!r.readVarint(v)) {
return false;
}
image.is_bigendian = (v != 0);
return true;
}
case 7:
return tag.type == WireType::kLengthDelimited && readBytesIntoImage(r, image);
case 8: {
if (tag.type != WireType::kFixed32) {
return false;
}
float v = 0.0f;
if (!r.readFloat(v)) {
return false;
}
image.compressed_depth_min = v;
return true;
}
case 9: {
if (tag.type != WireType::kFixed32) {
return false;
}
float v = 0.0f;
if (!r.readFloat(v)) {
return false;
}
image.compressed_depth_max = v;
return true;
}
case 10:
return tag.type == WireType::kLengthDelimited && r.readString(image.frame_id);
default:
return false;
}
});
if (!ok) {
return unexpected(std::string("Image wire: decode failed"));
}
return image;
}
} // namespace PJ