blob: d3ab72d852b9b348bbc143bdeb7829a1b5e3636c [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#include "aos/vision/blob/codec.h"
2
Stephan Pleinescc500b92024-05-30 10:58:40 -07003#include <stddef.h>
4
5#include <utility>
6#include <vector>
7
Stephan Pleinesf63bde82024-01-13 15:59:33 -08008namespace aos::vision {
Parker Schuh6691f192017-01-14 17:01:02 -08009
10size_t CalculateSize(const BlobList &blob_list) {
11 size_t count = Int16Codec::kSize;
12 for (const auto &blob : blob_list) {
13 count += 2 * Int16Codec::kSize;
14 for (int i = 0; i < static_cast<int>(blob.ranges().size()); ++i) {
15 count +=
16 Int16Codec::kSize + 2 * Int16Codec::kSize * blob.ranges()[i].size();
17 }
18 }
19 return count;
20}
21
22void SerializeBlob(const BlobList &blob_list, char *data) {
23 data = Int16Codec::Write(data, static_cast<uint16_t>(blob_list.size()));
24 for (const auto &blob : blob_list) {
25 data = Int16Codec::Write(data, static_cast<uint16_t>(blob.min_y()));
26 data = Int16Codec::Write(data, static_cast<uint16_t>(blob.ranges().size()));
27 for (int i = 0; i < (int)blob.ranges().size(); ++i) {
28 data = Int16Codec::Write(data,
29 static_cast<uint16_t>(blob.ranges()[i].size()));
30 for (const auto &range : blob.ranges()[i]) {
31 data = Int16Codec::Write(data, static_cast<uint16_t>(range.st));
32 data = Int16Codec::Write(data, static_cast<uint16_t>(range.ed));
33 }
34 }
35 }
36}
37
38const char *ParseBlobList(BlobList *blob_list, const char *data) {
39 int num_items = Int16Codec::Read(&data);
40 blob_list->clear();
41 blob_list->reserve(num_items);
42 for (int i = 0; i < num_items; ++i) {
43 std::vector<std::vector<ImageRange>> ranges_image;
44 int min_y = Int16Codec::Read(&data);
45 int num_ranges = Int16Codec::Read(&data);
46 ranges_image.reserve(num_ranges);
47 for (int j = 0; j < num_ranges; ++j) {
48 ranges_image.emplace_back();
49 auto &ranges = ranges_image.back();
50 int num_sub_ranges = Int16Codec::Read(&data);
51 for (int k = 0; k < num_sub_ranges; ++k) {
52 int st = Int16Codec::Read(&data);
53 int ed = Int16Codec::Read(&data);
54 ranges.emplace_back(st, ed);
55 }
56 }
57 blob_list->emplace_back(min_y, std::move(ranges_image));
58 }
59 return data;
60}
61
Stephan Pleinesf63bde82024-01-13 15:59:33 -080062} // namespace aos::vision