DisparityMessage.h
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 
46 namespace crl {
47 namespace multisense {
48 namespace details {
49 namespace wire {
50 
52 public:
53 
55  static CRL_CONSTEXPR VersionType VERSION = 1;
56 
57  static CRL_CONSTEXPR uint8_t WIRE_BITS_PER_PIXEL = 12;
58  static CRL_CONSTEXPR uint8_t WIRE_BYTE_ALIGNMENT = 3;
59  static CRL_CONSTEXPR uint8_t API_BITS_PER_PIXEL = 16; // 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 
84 class Disparity : public DisparityHeader {
85 public:
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  message & frameId;
103  message & width;
104  message & height;
105 
106  const uint32_t imageSize = static_cast<uint32_t> (std::ceil(((double) API_BITS_PER_PIXEL / 8.0) * width * height));
107 
108  if (typeid(Archive) == typeid(utility::BufferStreamWriter)) {
109 
110  message.write(dataP, imageSize);
111 
112  } else {
113 
114  dataP = message.peek();
115  message.seek(message.tell() + imageSize);
116  }
117  }
118 
119  //
120  // UDP assembler
121 
123  const uint8_t *dataP,
124  uint32_t offset,
125  uint32_t length)
126  {
127  //
128  // Special case, 1st packet contains header only. Firmware
129  // does not have to worry about the header length being
130  // byte-aligned on a WIRE_BITS_PER_PIXEL boundary
131 
132  if (0 == offset) {
133  stream.seek(0);
134  stream.write(dataP, META_LENGTH);
135  return;
136  }
137 
138  //
139  // The data section of each incoming packet is byte-aligned
140  // on a WIRE_BITS_PER_PIXEL boundary
141 
142  const uint8_t *sP = dataP;
143  const uint32_t sourceOffset = offset - META_LENGTH;
144  const uint32_t count = (8 * length) / WIRE_BITS_PER_PIXEL;
145  const uint32_t destOffset = META_LENGTH + (((8 * sourceOffset) / WIRE_BITS_PER_PIXEL) *
146  (API_BITS_PER_PIXEL / 8));
147  //
148  // Seek to the proper location
149 
150  stream.seek(destOffset);
151 
152  //
153  // This conversion is for (WIRE == 12bits), (API == 16bits, 1/16th pixel, unsigned integer)
154 
155  if (12 == WIRE_BITS_PER_PIXEL && 16 == API_BITS_PER_PIXEL) {
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  //
165  // This conversion is for (WIRE == 12bits), (API == 32bits, floating point)
166 
167  } else if (12 == WIRE_BITS_PER_PIXEL && 32 == API_BITS_PER_PIXEL) {
168 
169  float *dP = reinterpret_cast<float*>(stream.peek());
170 
171  for(uint32_t i=0; i<count; i+=2, sP+=3) {
172 
173  dP[i] = static_cast<float>((sP[0] ) | ((sP[1] & 0x0F) << 8)) / 16.0f;
174  dP[i+1] = static_cast<float>((sP[1] >> 4) | (sP[2] << 4) ) / 16.0f;
175  }
176 
177  } else
178  CRL_EXCEPTION("(wire==%dbits, api=%dbits) not supported",
179  WIRE_BITS_PER_PIXEL, API_BITS_PER_PIXEL);
180  }
181 };
182 
183 #endif // !SENSORPOD_FIRMWARE
184 
185 }}}}; // namespaces
186 
187 #endif
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:71
virtual void write(const void *bufferP, std::size_t length)
f
Disparity(utility::BufferStreamReader &r, VersionType v)
std::string * frameId(M &m)
static CRL_CONSTEXPR IdType ID_DATA_DISPARITY
Definition: Protocol.h:195
void serialize(Stream &stream, const T &t)
Definition: channel.cc:56
#define WIRE_HEADER_ATTRIBS_
Definition: Protocol.h:55
void serialize(Archive &message, const VersionType version)
static void assembler(utility::BufferStreamWriter &stream, const uint8_t *dataP, uint32_t offset, uint32_t length)
#define CRL_CONSTEXPR
Definition: Portability.hh:38


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46