blob: b8a8089a1a24a9de1e7e4fac7368ef68b4ab7be3 [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) {
Parker Schuh0ff777c2017-02-19 15:01:13 -080015 memcpy(data, &ival, sizeof(T));
Parker Schuh6691f192017-01-14 17:01:02 -080016 return data + kSize;
17 }
18 static inline T Read(const char **data) {
Parker Schuh0ff777c2017-02-19 15:01:13 -080019 T datum;
20 memcpy(&datum, *data, sizeof(T));
Parker Schuh6691f192017-01-14 17:01:02 -080021 *data += kSize;
22 return datum;
23 }
24};
25
26using Int64Codec = IntCodec<uint64_t>;
27using Int32Codec = IntCodec<uint32_t>;
28using 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.
33size_t CalculateSize(const BlobList &blob_list);
34// Serializes blob_list to data. Must be valid memory of size returned by
35// PredictSize.
36void SerializeBlob(const BlobList &blob_list, char *data);
37
38// Combines above to serialize to a string.
39inline 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).
46const char *ParseBlobList(BlobList *blob_list, const char *data);
47
48} // namespace vision
49} // namespace aos
50
51#endif // _AOS_VISION_BLOB_CODEC_H_