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) { |
| 15 | *(reinterpret_cast<T *>(data)) = ival; |
| 16 | return data + kSize; |
| 17 | } |
| 18 | static inline T Read(const char **data) { |
| 19 | auto datum = *(reinterpret_cast<const T *>(*data)); |
| 20 | *data += kSize; |
| 21 | return datum; |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | using Int64Codec = IntCodec<uint64_t>; |
| 26 | using Int32Codec = IntCodec<uint32_t>; |
| 27 | using Int16Codec = IntCodec<uint16_t>; |
| 28 | |
| 29 | // Calculates bytes size of blob_list. This runs the encoding algorithm below |
| 30 | // to get the exact size that SerializeBlob will require. |
| 31 | // Consider just using SerializeBlobTo instead of these lower level routines. |
| 32 | size_t CalculateSize(const BlobList &blob_list); |
| 33 | // Serializes blob_list to data. Must be valid memory of size returned by |
| 34 | // PredictSize. |
| 35 | void SerializeBlob(const BlobList &blob_list, char *data); |
| 36 | |
| 37 | // Combines above to serialize to a string. |
| 38 | inline void SerializeBlobTo(const BlobList &blob_list, std::string *out) { |
| 39 | size_t len = CalculateSize(blob_list); |
| 40 | out->resize(len, 0); |
| 41 | SerializeBlob(blob_list, &(*out)[0]); |
| 42 | } |
| 43 | |
| 44 | // Parses a blob from data (Advancing data pointer by the size of the image). |
| 45 | const char *ParseBlobList(BlobList *blob_list, const char *data); |
| 46 | |
| 47 | } // namespace vision |
| 48 | } // namespace aos |
| 49 | |
| 50 | #endif // _AOS_VISION_BLOB_CODEC_H_ |