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