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