blob: 5872b4462b7e4650a49ee9750778f84f23f1f22c [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08008namespace aos::vision {
Parker Schuh6691f192017-01-14 17:01:02 -08009
10template <typename T>
11struct IntCodec {
12 static constexpr size_t kSize = sizeof(T);
13 static inline char *Write(char *data, T ival) {
Parker Schuh0ff777c2017-02-19 15:01:13 -080014 memcpy(data, &ival, sizeof(T));
Parker Schuh6691f192017-01-14 17:01:02 -080015 return data + kSize;
16 }
17 static inline T Read(const char **data) {
Parker Schuh0ff777c2017-02-19 15:01:13 -080018 T datum;
19 memcpy(&datum, *data, sizeof(T));
Parker Schuh6691f192017-01-14 17:01:02 -080020 *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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080047} // namespace aos::vision
Parker Schuh6691f192017-01-14 17:01:02 -080048
49#endif // _AOS_VISION_BLOB_CODEC_H_