buffer.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_genicam_api package.
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Heiko Hirschmueller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "buffer.h"
37 #include "stream.h"
38 
39 #include "gentl_wrapper.h"
40 #include "exception.h"
41 
42 namespace rcg
43 {
44 
45 namespace
46 {
47 
48 template<class T> inline T getBufferValue(const std::shared_ptr<const GenTLWrapper> &gentl,
49  void *stream, void *buffer, GenTL::BUFFER_INFO_CMD cmd)
50 {
51  T ret=0;
52 
54  size_t size=sizeof(T);
55 
56  if (stream != 0 && buffer != 0)
57  {
58  gentl->DSGetBufferInfo(stream, buffer, cmd, &type, &ret, &size);
59  }
60 
61  return ret;
62 }
63 
64 inline bool getBufferBool(const std::shared_ptr<const GenTLWrapper> &gentl,
65  void *stream, void *buffer, GenTL::BUFFER_INFO_CMD cmd)
66 {
67  bool8_t ret=0;
68 
70  size_t size=sizeof(ret);
71 
72  if (stream != 0 && buffer != 0)
73  {
74  gentl->DSGetBufferInfo(stream, buffer, cmd, &type, &ret, &size);
75  }
76 
77  return ret != 0;
78 }
79 
80 inline std::string getBufferString(const std::shared_ptr<const GenTLWrapper> &gentl,
81  void *stream, void *buffer, GenTL::BUFFER_INFO_CMD cmd)
82 {
83  std::string ret;
84 
86  char tmp[1024]="";
87  size_t size=sizeof(tmp);
88 
89  if (stream != 0 && buffer != 0)
90  {
91  if (gentl->DSGetBufferInfo(stream, buffer, cmd, &type, &ret, &size) == GenTL::GC_ERR_SUCCESS)
92  {
93  if (type == GenTL::INFO_DATATYPE_STRING)
94  {
95  ret=tmp;
96  }
97  }
98  }
99 
100  return ret;
101 }
102 
103 template<class T> inline T getBufferPartValue(const std::shared_ptr<const GenTLWrapper> &gentl,
104  void *stream, void *buffer, std::uint32_t part,
106 {
107  T ret=0;
108 
110  size_t size=sizeof(T);
111 
112  if (stream != 0 && buffer != 0)
113  {
114  gentl->DSGetBufferPartInfo(stream, buffer, part, cmd, &type, &ret, &size);
115  }
116 
117  return ret;
118 }
119 
120 }
121 
122 Buffer::Buffer(const std::shared_ptr<const GenTLWrapper> &_gentl, Stream *_parent)
123 {
124  parent=_parent;
125  gentl=_gentl;
126  buffer=0;
127  multipart=false;
128 }
129 
130 void Buffer::setHandle(void *handle)
131 {
132  buffer=handle;
133 
134  multipart=false;
135  if (buffer != 0)
136  {
137  multipart=getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
139  }
140 }
141 
142 uint32_t Buffer::getNumberOfParts() const
143 {
144  uint32_t ret=0;
145 
146  if (multipart)
147  {
148  gentl->DSGetNumBufferParts(parent->getHandle(), buffer, &ret);
149  }
150  else
151  {
152  size_t type=getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
154 
155  if (type != PAYLOAD_TYPE_CHUNK_ONLY)
156  {
157  ret=1;
158  }
159  }
160 
161  return ret;
162 }
163 
165 {
166  return getBufferValue<void *>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_BASE);
167 }
168 
169 size_t Buffer::getGlobalSize() const
170 {
171  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_SIZE);
172 }
173 
174 void *Buffer::getBase(std::uint32_t part) const
175 {
176  if (multipart)
177  {
178  return getBufferPartValue<void *>(gentl, parent->getHandle(), buffer, part,
180  }
181  else
182  {
183  void *ret=getBufferValue<void *>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_BASE);
184 
185  size_t offset=getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
187 
188  if (offset > 0)
189  {
190  ret=reinterpret_cast<char *>(ret)+offset;
191  }
192 
193  return ret;
194  }
195 }
196 
197 size_t Buffer::getSize(std::uint32_t part) const
198 {
199  if (multipart)
200  {
201  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
203  }
204  else
205  {
206  size_t size=getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
208 
209  size_t offset=getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
211 
212  return size-offset;
213  }
214 }
215 
216 void *Buffer::getUserPtr() const
217 {
218  return getBufferValue<void *>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_USER_PTR);
219 }
220 
221 uint64_t Buffer::getTimestamp() const
222 {
223  return getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer,
225 }
226 
227 bool Buffer::getNewData() const
228 {
229  return getBufferBool(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_NEW_DATA);
230 }
231 
233 {
234  return getBufferBool(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_IS_QUEUED);
235 }
236 
238 {
239  return getBufferBool(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_IS_ACQUIRING);
240 }
241 
243 {
244  return getBufferBool(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_IS_INCOMPLETE);
245 }
246 
247 std::string Buffer::getTLType() const
248 {
249  return getBufferString(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_TLTYPE);
250 }
251 
252 size_t Buffer::getSizeFilled() const
253 {
254  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
256 }
257 
258 size_t Buffer::getPartDataType(uint32_t part) const
259 {
260  if (multipart)
261  {
262  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
264  }
265  else
266  {
267  return 0;
268  }
269 }
270 
271 size_t Buffer::getWidth(std::uint32_t part) const
272 {
273  if (multipart)
274  {
275  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
277  }
278  else
279  {
280  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_WIDTH);
281  }
282 }
283 
284 size_t Buffer::getHeight(std::uint32_t part) const
285 {
286  if (multipart)
287  {
288  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
290  }
291  else
292  {
293  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_HEIGHT);
294  }
295 }
296 
297 size_t Buffer::getXOffset(std::uint32_t part) const
298 {
299  if (multipart)
300  {
301  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
303  }
304  else
305  {
306  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_XOFFSET);
307  }
308 }
309 
310 size_t Buffer::getYOffset(std::uint32_t part) const
311 {
312  if (multipart)
313  {
314  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
316  }
317  else
318  {
319  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_YOFFSET);
320  }
321 }
322 
323 size_t Buffer::getXPadding(std::uint32_t part) const
324 {
325  if (multipart)
326  {
327  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
329  }
330  else
331  {
332  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_XPADDING);
333  }
334 }
335 
336 size_t Buffer::getYPadding() const
337 {
338  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_YPADDING);
339 }
340 
341 uint64_t Buffer::getFrameID() const
342 {
343  return getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_FRAMEID);
344 }
345 
346 bool Buffer::getImagePresent(uint32_t part) const
347 {
348  if (multipart)
349  {
350  size_t type=getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
352 
353  bool ret;
354 
355  switch (type)
356  {
366  ret=true;
367  break;
368 
369  default:
370  ret=false;
371  break;
372  }
373 
374  return ret;
375  }
376  else
377  {
378  return getBufferBool(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_IMAGEPRESENT);
379  }
380 }
381 
383 {
384  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
386 }
387 
388 uint64_t Buffer::getPixelFormat(uint32_t part) const
389 {
390  if (multipart)
391  {
392  return getBufferPartValue<uint64_t>(gentl, parent->getHandle(), buffer, part,
394  }
395  else
396  {
397  return getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer,
399  }
400 }
401 
402 uint64_t Buffer::getPixelFormatNamespace(uint32_t part) const
403 {
404  if (multipart)
405  {
406  return getBufferPartValue<uint64_t>(gentl, parent->getHandle(), buffer, part,
408  }
409  else
410  {
411  return getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer,
413  }
414 }
415 
416 uint64_t Buffer::getPartSourceID(std::uint32_t part) const
417 {
418  if (multipart)
419  {
420  return getBufferPartValue<uint64_t>(gentl, parent->getHandle(), buffer, part,
422  }
423  else
424  {
425  return 0;
426  }
427 }
428 
429 size_t Buffer::getDeliveredImageHeight(uint32_t part) const
430 {
431  if (multipart)
432  {
433  return getBufferPartValue<size_t>(gentl, parent->getHandle(), buffer, part,
435  }
436  else
437  {
438  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
440  }
441 }
442 
444 {
445  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer,
447 }
448 
449 uint64_t Buffer::getChunkLayoutID() const
450 {
451  return getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer,
453 }
454 
455 std::string Buffer::getFilename() const
456 {
457  return getBufferString(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_FILENAME);
458 }
459 
461 {
462  bool ret=false;
463 
465  int32_t v;
466  size_t size=sizeof(v);
468 
469  if (parent->getHandle() != 0 && buffer != 0)
470  {
472  &type, &v, &size);
473 
474  if (err == GenTL::GC_ERR_SUCCESS && type == GenTL::INFO_DATATYPE_INT32 &&
476  {
477  ret=true;
478  }
479  }
480 
481  return ret;
482 }
483 
484 size_t Buffer::getDataSize() const
485 {
486  return getBufferValue<size_t>(gentl, parent->getHandle(), buffer, GenTL::BUFFER_INFO_DATA_SIZE);
487 }
488 
489 uint64_t Buffer::getTimestampNS() const
490 {
491  uint64_t ret=getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer,
493 
494  // if timestamp in nano seconds is not available, then compute it from
495  // timestamp and device frequency
496 
497  if (ret == 0)
498  {
499  const uint64_t ns_freq=1000000000ul;
500  uint64_t freq=parent->getParent()->getTimestampFrequency();
501 
502  if (freq == 0)
503  {
504  freq=ns_freq;
505  }
506 
507  ret=getBufferValue<uint64_t>(gentl, parent->getHandle(), buffer,
509 
510  if (freq != ns_freq)
511  {
512  ret=ret/freq*ns_freq+(ns_freq*(ret%freq))/freq;
513  }
514  }
515 
516  return ret;
517 }
518 
520 {
521  return getBufferBool(gentl, parent->getHandle(), buffer,
523 }
524 
526 {
527  return getBufferBool(gentl, parent->getHandle(), buffer,
529 }
530 
531 void *Buffer::getHandle() const
532 {
533  return buffer;
534 }
535 
537 {
538  int p=1;
539  char *c=reinterpret_cast<char *>(&p);
540 
541  if (c[0] == 1)
542  {
543  return false;
544  }
545 
546  return true;
547 }
548 
549 }
size_t getDeliveredImageHeight(std::uint32_t part) const
Returns the number of lines that are delivered in this buffer.
Definition: buffer.cc:429
size_t getDataSize() const
Returns the size of data intended to the written to the buffer the last time it has been filled...
Definition: buffer.cc:484
size_t getYPadding() const
Returns vertical padding of the data in the buffer in bytes.
Definition: buffer.cc:336
Stream * parent
Definition: buffer.h:460
bool multipart
Definition: buffer.h:463
size_t getSizeFilled() const
Returns the number of bytes written into the buffer last time it has been filled. ...
Definition: buffer.cc:252
std::shared_ptr< const GenTLWrapper > gentl
Definition: buffer.h:461
uint64_t getChunkLayoutID() const
Returns the chunk layout id, which serves as an indicator that the chunk layout has changed and the a...
Definition: buffer.cc:449
std::string getFilename() const
Returns the filename in case the payload contains a file.
Definition: buffer.cc:455
void * getHandle() const
Get internal stream handle.
Definition: buffer.cc:531
int32_t BUFFER_INFO_CMD
Definition: GenTL_v1_5.h:434
size_t getXPadding(std::uint32_t part) const
Returns horizontal padding of the data in the buffer in bytes.
Definition: buffer.cc:323
int32_t BUFFER_PART_INFO_CMD
Definition: GenTL_v1_5.h:454
bool getIsQueued() const
Signals if the buffer is associated to the input or output queue.
Definition: buffer.cc:232
std::shared_ptr< Device > getParent() const
Returns the pointer to the parent device object.
Definition: stream.cc:82
size_t getWidth(std::uint32_t part) const
Returns the width of the image in pixel.
Definition: buffer.cc:271
uint64_t getPartSourceID(std::uint32_t part) const
Returns the source id of the specified part.
Definition: buffer.cc:416
int32_t GC_ERROR
Definition: GenTL_v1_5.h:181
size_t getPayloadType() const
Returns the payload type according to PAYLOADTYPE_INFO_IDS.
Definition: buffer.cc:382
uint64_t getTimestamp() const
Returns the timestamp of the buffer.
Definition: buffer.cc:221
void * getGlobalBase() const
Returns the global base address of the buffer memory.
Definition: buffer.cc:164
void * getBase(std::uint32_t part) const
Returns the base address of the specified part of the multi-part buffer.
Definition: buffer.cc:174
void * buffer
Definition: buffer.h:462
uint64_t getPixelFormat(std::uint32_t part) const
Returns the pixel format of the specified part as defined in the PFNC.
Definition: buffer.cc:388
uint8_t bool8_t
Definition: GenTL_v1_5.h:105
size_t getDeliveredChunkPayloadSize() const
Returnes the delivered chung payload size.
Definition: buffer.cc:443
void * getHandle() const
Get internal stream handle.
Definition: stream.cc:471
size_t getPartDataType(uint32_t part) const
Returns the data type id of the specified part as defined in PARTDATATYPE_IDS.
Definition: buffer.cc:258
bool getContainsChunkdata() const
Returns if the buffer contains chunk data.
Definition: buffer.cc:525
uint64_t getTimestampNS() const
Returns the acquisition timestamp of the data in this buffer in ns.
Definition: buffer.cc:489
Buffer(const std::shared_ptr< const GenTLWrapper > &gentl, Stream *parent)
Constructs a buffer class as wrapper around a buffer handle.
Definition: buffer.cc:122
bool isBigEndian() const
Returns if the data is given as big or little endian.
Definition: buffer.cc:460
bool getDataLargerThanBuffer() const
Signals if the memory that was allocated for this buffer is too small.
Definition: buffer.cc:519
bool isHostBigEndian()
Definition: buffer.cc:536
size_t getGlobalSize() const
Returns the global size of the buffer.
Definition: buffer.cc:169
uint64_t getPixelFormatNamespace(std::uint32_t part) const
Returns the pixel format namespace, which preferably should be PIXELFORMAT_NAMESPACE_PFNC_32BIT.
Definition: buffer.cc:402
void * getUserPtr() const
Returns the private data pointer of the GenTL Consumer.
Definition: buffer.cc:216
Definition: buffer.cc:42
bool getIsAcquiring() const
Signals if the buffer is currently being filled with data.
Definition: buffer.cc:237
bool getImagePresent(std::uint32_t part) const
Returns if a 2D, 3D or confidence image is present in the specified part.
Definition: buffer.cc:346
bool getIsIncomplete() const
Signals if the buffer is incomplete due to an error.
Definition: buffer.cc:242
uint64_t getFrameID() const
Returns the sequentially incremented number of the frame.
Definition: buffer.cc:341
size_t getYOffset(std::uint32_t part) const
Returns the vertical offset of the data in the buffer in lines from the image origin to handle areas ...
Definition: buffer.cc:310
bool getNewData() const
Returns if the buffer contains new data.
Definition: buffer.cc:227
void setHandle(void *handle)
Set the buffer handle that this object should manage.
Definition: buffer.cc:130
size_t getXOffset(std::uint32_t part) const
Returns the horizontal offset of the data in the buffer in pixels from the image origin to handle are...
Definition: buffer.cc:297
size_t getHeight(std::uint32_t part) const
Returns the height of the image in pixel.
Definition: buffer.cc:284
int32_t INFO_DATATYPE
Definition: GenTL_v1_5.h:258
The stream class encapsulates a Genicam stream.
Definition: stream.h:55
size_t getSize(std::uint32_t part) const
Returns the size of the specified part of the mult-part buffer.
Definition: buffer.cc:197
std::uint32_t getNumberOfParts() const
Returns the number of parts, excluding chunk data.
Definition: buffer.cc:142
std::string getTLType() const
Returns the type the used transport layer.
Definition: buffer.cc:247


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Thu Jun 6 2019 19:10:53