Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 1 | #ifndef _AOS_VISION_BLOB_CODEC_H_ |
| 2 | #define _AOS_VISION_BLOB_CODEC_H_ |
| 3 | |
Stephan Pleines | cc500b9 | 2024-05-30 10:58:40 -0700 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | #include <string.h> |
| 6 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "aos/vision/blob/range_image.h" |
| 10 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 11 | namespace aos::vision { |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 12 | |
| 13 | template <typename T> |
| 14 | struct IntCodec { |
| 15 | static constexpr size_t kSize = sizeof(T); |
| 16 | static inline char *Write(char *data, T ival) { |
Parker Schuh | 0ff777c | 2017-02-19 15:01:13 -0800 | [diff] [blame] | 17 | memcpy(data, &ival, sizeof(T)); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 18 | return data + kSize; |
| 19 | } |
| 20 | static inline T Read(const char **data) { |
Parker Schuh | 0ff777c | 2017-02-19 15:01:13 -0800 | [diff] [blame] | 21 | T datum; |
| 22 | memcpy(&datum, *data, sizeof(T)); |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 23 | *data += kSize; |
| 24 | return datum; |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | using Int64Codec = IntCodec<uint64_t>; |
| 29 | using Int32Codec = IntCodec<uint32_t>; |
| 30 | using Int16Codec = IntCodec<uint16_t>; |
| 31 | |
| 32 | // Calculates bytes size of blob_list. This runs the encoding algorithm below |
| 33 | // to get the exact size that SerializeBlob will require. |
| 34 | // Consider just using SerializeBlobTo instead of these lower level routines. |
| 35 | size_t CalculateSize(const BlobList &blob_list); |
| 36 | // Serializes blob_list to data. Must be valid memory of size returned by |
| 37 | // PredictSize. |
| 38 | void SerializeBlob(const BlobList &blob_list, char *data); |
| 39 | |
| 40 | // Combines above to serialize to a string. |
| 41 | inline void SerializeBlobTo(const BlobList &blob_list, std::string *out) { |
| 42 | size_t len = CalculateSize(blob_list); |
| 43 | out->resize(len, 0); |
| 44 | SerializeBlob(blob_list, &(*out)[0]); |
| 45 | } |
| 46 | |
| 47 | // Parses a blob from data (Advancing data pointer by the size of the image). |
| 48 | const char *ParseBlobList(BlobList *blob_list, const char *data); |
| 49 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 50 | } // namespace aos::vision |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 51 | |
| 52 | #endif // _AOS_VISION_BLOB_CODEC_H_ |