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