LibMultiSense
LibMultiSense Documentation
Loading...
Searching...
No Matches
DisparityMessage.hh
Go to the documentation of this file.
1
38#ifndef LibMultiSense_DisparityMessage
39#define LibMultiSense_DisparityMessage
40
41#include <typeinfo>
42#include <cmath>
43
45
46namespace crl {
47namespace multisense {
48namespace details {
49namespace wire {
50
52public:
53
54 static CRL_CONSTEXPR IdType ID = ID_DATA_DISPARITY;
55 static CRL_CONSTEXPR VersionType VERSION = 1;
56
57 static CRL_CONSTEXPR uint8_t WIRE_BITS_PER_PIXEL = MULTISENSE_WIRE_BITS_PER_PIXEL;
58 static CRL_CONSTEXPR uint8_t WIRE_BYTE_ALIGNMENT = 3;
59 static CRL_CONSTEXPR uint8_t API_BITS_PER_PIXEL = MULTISENSE_API_BITS_PER_PIXEL; // after custom assemble()
60 static CRL_CONSTEXPR uint32_t META_LENGTH = 16; // packed, includes type/version
61
62#ifdef SENSORPOD_FIRMWARE
63 IdType id;
64 VersionType version;
65#endif // SENSORPOD_FIRMWARE
66
67 int64_t frameId;
68 uint16_t width;
69 uint16_t height;
70
72 :
73#ifdef SENSORPOD_FIRMWARE
74 id(ID),
75 version(VERSION),
76#endif // SENSORPOD_FIRMWARE
77 frameId(0),
78 width(0),
79 height(0) {};
80};
81
82#ifndef SENSORPOD_FIRMWARE
83
84class Disparity : public DisparityHeader {
85public:
86
87 void *dataP;
88
89 //
90 // Constructors
91
93 Disparity() : dataP(NULL) {};
94
95 //
96 // Serialization routine
97
98 template<class Archive>
99 void serialize(Archive& message,
100 const VersionType version)
101 {
102 (void) version;
103 message & frameId;
104 message & width;
105 message & height;
106
107 const uint32_t imageSize = static_cast<uint32_t> (std::ceil(((double) API_BITS_PER_PIXEL / 8.0) * width * height));
108
109 if (typeid(Archive) == typeid(utility::BufferStreamWriter)) {
110
111 message.write(dataP, imageSize);
112
113 } else {
114
115 dataP = message.peek();
116 message.seek(message.tell() + imageSize);
117 }
118 }
119
120 //
121 // UDP assembler
122
124 const uint8_t *dataP,
125 uint32_t offset,
126 uint32_t length)
127 {
128 //
129 // Special case, 1st packet contains header only. Firmware
130 // does not have to worry about the header length being
131 // byte-aligned on a WIRE_BITS_PER_PIXEL boundary
132
133 if (0 == offset) {
134 stream.seek(0);
135 stream.write(dataP, META_LENGTH);
136 return;
137 }
138
139 //
140 // The data section of each incoming packet is byte-aligned
141 // on a WIRE_BITS_PER_PIXEL boundary
142
143 const uint8_t *sP = dataP;
144 const uint32_t sourceOffset = offset - META_LENGTH;
145 const uint32_t count = (8 * length) / WIRE_BITS_PER_PIXEL;
146 const uint32_t destOffset = META_LENGTH + (((8 * sourceOffset) / WIRE_BITS_PER_PIXEL) *
147 (API_BITS_PER_PIXEL / 8));
148 //
149 // Seek to the proper location
150
151 stream.seek(destOffset);
152
153#if MULTISENSE_WIRE_BITS_PER_PIXEL == 12 && MULTISENSE_API_BITS_PER_PIXEL == 16
154 //
155 // This conversion is for (WIRE == 12bits), (API == 16bits, 1/16th pixel, unsigned integer)
156
157 uint16_t *dP = reinterpret_cast<uint16_t*>(stream.peek());
158
159 for(uint32_t i=0; i<count; i+=2, sP+=3) {
160 dP[i] = ((sP[0] ) | ((sP[1] & 0x0F) << 8));
161 dP[i+1] = ((sP[1] >> 4) | (sP[2] << 4) );
162 }
163
164#elif MULTISENSE_WIRE_BITS_PER_PIXEL == 12 && MULTISENSE_API_BITS_PER_PIXEL == 32
165 //
166 // This conversion is for (WIRE == 12bits), (API == 32bits, floating point)
167
168 float *dP = reinterpret_cast<float*>(stream.peek());
169
170 for(uint32_t i=0; i<count; i+=2, sP+=3) {
171
172 dP[i] = static_cast<float>((sP[0] ) | ((sP[1] & 0x0F) << 8)) / 16.0f;
173 dP[i+1] = static_cast<float>((sP[1] >> 4) | (sP[2] << 4) ) / 16.0f;
174 }
175
176#else
177#error MULTISENSE_WIRE_BITS_PER_PIXEL and MULTISENSE_API_BITS_PER_PIXEL not supported
178#endif
179
180 }
181};
182
183#endif // !SENSORPOD_FIRMWARE
184
185}}}} // namespaces
186
187#endif
Macros and symbols to help portability between different compiler versions.
#define CRL_CONSTEXPR
#define MULTISENSE_API_BITS_PER_PIXEL
Definition Protocol.hh:51
#define WIRE_HEADER_ATTRIBS_
Definition Protocol.hh:65
#define MULTISENSE_WIRE_BITS_PER_PIXEL
Definition Protocol.hh:47
virtual void write(const void *bufferP, std::size_t length)
static void assembler(utility::BufferStreamWriter &stream, const uint8_t *dataP, uint32_t offset, uint32_t length)
void serialize(Archive &message, const VersionType version)
Disparity(utility::BufferStreamReader &r, VersionType v)