blob: c2bc30df7bc06969373e3ce791f5a1710201072c [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#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
8namespace aos {
9namespace vision {
10
11template <typename T>
12struct 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
25using Int64Codec = IntCodec<uint64_t>;
26using Int32Codec = IntCodec<uint32_t>;
27using 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.
32size_t CalculateSize(const BlobList &blob_list);
33// Serializes blob_list to data. Must be valid memory of size returned by
34// PredictSize.
35void SerializeBlob(const BlobList &blob_list, char *data);
36
37// Combines above to serialize to a string.
38inline 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).
45const char *ParseBlobList(BlobList *blob_list, const char *data);
46
47} // namespace vision
48} // namespace aos
49
50#endif // _AOS_VISION_BLOB_CODEC_H_