libvisiontransfer  9.0.3
imageset.h
1 /*******************************************************************************
2  * Copyright (c) 2021 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_IMAGESET_H
16 #define VISIONTRANSFER_IMAGESET_H
17 
18 #include <cassert>
19 #include <cstddef>
20 #include "visiontransfer/common.h"
21 
22 namespace visiontransfer {
23 
38 class VT_EXPORT ImageSet {
39 public:
40  static const int MAX_SUPPORTED_IMAGES = 3;
44  enum ImageFormat {
47 
50 
53  FORMAT_12_BIT_MONO
54  };
55 
60  FORMAT_8_BIT = FORMAT_8_BIT_MONO,
61  FORMAT_12_BIT = FORMAT_12_BIT_MONO
62  };
63 
67  enum ImageType {
68  IMAGE_UNDEFINED,
69  IMAGE_LEFT,
70  IMAGE_DISPARITY,
71  IMAGE_RIGHT,
72  };
73 
77  ImageSet();
78 
82  ImageSet(const ImageSet& other);
83 
84  ~ImageSet();
85  ImageSet& operator= (ImageSet const& other);
86 
90  void setWidth(int w) {width = w;}
91 
95  void setHeight(int h) {height = h;}
96 
104  void setRowStride(int imageNumber, int stride) {
105  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
106  rowStride[imageNumber] = stride;
107  }
108 
116  void setPixelFormat(int imageNumber, ImageFormat format) {
117  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
118  formats[imageNumber] = format;
119  }
120 
121 #ifndef DOXYGEN_SHOULD_SKIP_THIS
122  DEPRECATED("Use setPixelFormat(int, ImageFormat) instead") void setPixelFormat(int imageNumber, ImageFormat_Deprecated format) {
123  setPixelFormat(imageNumber, static_cast<ImageFormat>(format));
124  }
125 #endif
126 
134  void setPixelData(int imageNumber, unsigned char* pixelData) {
135  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
136  data[imageNumber] = pixelData;
137  }
138 
145  void setQMatrix(const float* q) {
146  qMatrix = q;
147  }
148 
152  void setSequenceNumber(unsigned int num) {
153  seqNum = num;
154  }
155 
163  void setTimestamp(int seconds, int microsec) {
164  timeSec = seconds;
165  timeMicrosec = microsec;
166  }
167 
175  void setDisparityRange(int minimum, int maximum) {
176  minDisparity = minimum;
177  maxDisparity = maximum;
178  }
179 
183  void setSubpixelFactor(int subpixFact) {
184  subpixelFactor = subpixFact;
185  }
186 
187 #ifndef DOXYGEN_SHOULD_SKIP_THIS
188 
195  DEPRECATED("Only compatible with two-image sets: use setNumberOfImages() and setIndexOf() instead")
196  void setImageDisparityPair(bool dispPair);
197 #endif
198 
202  int getWidth() const {return width;}
203 
207  int getHeight() const {return height;}
208 
218  int getRowStride(int imageNumber) const {
219  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
220  return rowStride[imageNumber];
221  }
222 
231  int getRowStride(ImageType what) const {
232  int idx = getIndexOf(what, true);
233  return getRowStride(idx);
234  }
235 
245  ImageFormat getPixelFormat(int imageNumber) const {
246  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
247  return formats[imageNumber];
248  }
249 
259  int idx = getIndexOf(what, true);
260  return getPixelFormat(idx);
261  }
262 
272  unsigned char* getPixelData(int imageNumber) const {
273  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
274  return data[imageNumber];
275  }
276 
285  unsigned char* getPixelData(ImageType what) const {
286  int idx = getIndexOf(what, true);
287  return getPixelData(idx);
288  }
289 
293  const float* getQMatrix() const {
294  return qMatrix;
295  }
296 
300  unsigned int getSequenceNumber() const {return seqNum;}
301 
309  void getTimestamp(int& seconds, int& microsec) const {
310  seconds = timeSec;
311  microsec = timeMicrosec;
312  }
313 
322  void getDisparityRange(int& minimum, int& maximum) const {
323  minimum = minDisparity;
324  maximum = maxDisparity;
325  }
326 
330  int getSubpixelFactor() const {
331  return subpixelFactor;
332  }
333 
340  void writePgmFile(int imageNumber, const char* fileName) const;
341 
342 #ifndef DOXYGEN_SHOULD_SKIP_THIS
343 
355  DEPRECATED("Only compatible with two-image sets: use hasImageType(ImageSet::IMAGE_DISPARITY) instead")
356  bool isImageDisparityPair() const {
357  return (getNumberOfImages()==2) && hasImageType(IMAGE_DISPARITY);
358  }
359 #endif
360 
364  void copyTo(ImageSet& dest);
365 
372  int getBytesPerPixel(int imageNumber) const {
373  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
374  return getBytesPerPixel(formats[imageNumber]);
375  }
376 
383  int getBitsPerPixel(int imageNumber) const {
384  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
385  return getBitsPerPixel(formats[imageNumber]);
386  }
387 
388  int getBitsPerPixel(ImageType what) const {
389  int idx = getIndexOf(what, true);
390  return getBitsPerPixel(idx);
391  }
392 
393  static int getBitsPerPixel(ImageFormat format);
394 
399  static int getBytesPerPixel(ImageFormat format);
400 
404  int getNumberOfImages() const {
405  return numberOfImages;
406  }
407 
411  void setNumberOfImages(int number) {
412  assert(number >= 1 && number <= MAX_SUPPORTED_IMAGES);
413  numberOfImages = number;
414  }
415 
419  ImageType getImageType(int imageNumber) const;
420 
429  int getIndexOf(ImageType what, bool throwIfNotFound=false) const;
430 
434  bool hasImageType(ImageType what) const {
435  return getIndexOf(what) >= 0;
436  }
437 
438 
446  void setIndexOf(ImageType what, int idx);
447 
448 
449 #ifdef CV_MAJOR_VERSION
450 
464  inline void toOpenCVImage(int imageNumber, cv::Mat& dest, bool convertRgbToBgr = true);
465 #endif
466 
472  void setExposureTime(int timeMicrosec) {
473  exposureTime = timeMicrosec;
474  }
475 
482  int getExposureTime() const {
483  return exposureTime;
484  }
485 
493  void setLastSyncPulse(int seconds, int microsec) {
494  lastSyncPulseSec = seconds;
495  lastSyncPulseMicrosec = microsec;
496  }
497 
505  void getLastSyncPulse(int& seconds, int& microsec) const {
506  seconds = lastSyncPulseSec;
507  microsec = lastSyncPulseMicrosec;
508  }
509 
510 private:
511  // No pimpl idiom here as almost everything is inlined.
512  int width;
513  int height;
514  int rowStride[MAX_SUPPORTED_IMAGES];
515  ImageFormat formats[MAX_SUPPORTED_IMAGES];
516  unsigned char* data[MAX_SUPPORTED_IMAGES];
517  const float* qMatrix;
518  int timeSec;
519  int timeMicrosec;
520  unsigned int seqNum;
521  int minDisparity;
522  int maxDisparity;
523  int subpixelFactor;
524  int* referenceCounter;
525  int numberOfImages;
526 
527  int indexLeftImage;
528  int indexRightImage;
529  int indexDisparityImage;
530 
531  int exposureTime;
532  int lastSyncPulseSec;
533  int lastSyncPulseMicrosec;
534 
535  void copyData(ImageSet& dest, const ImageSet& src, bool countRef);
536  void decrementReference();
537 };
538 
539 #ifndef DOXYGEN_SHOULD_SKIP_THIS
540 // For source compatibility
541 class DEPRECATED("Use ImageSet instead.") ImagePair: public ImageSet {
542 };
543 #endif
544 
545 } // namespace
546 
547 #include "visiontransfer/imageset-opencv.h"
548 #endif
void setNumberOfImages(int number)
Sets the number of valid images in this set.
Definition: imageset.h:411
int getRowStride(int imageNumber) const
Returns the row stride for the pixel data of one image.
Definition: imageset.h:218
void setTimestamp(int seconds, int microsec)
Sets the time at which this image set has been captured.
Definition: imageset.h:163
void setPixelData(int imageNumber, unsigned char *pixelData)
Sets the pixel data for the given image.
Definition: imageset.h:134
void getTimestamp(int &seconds, int &microsec) const
Returns the time at which this image set has been captured.
Definition: imageset.h:309
const float * getQMatrix() const
Returns a pointer to the disparity-to-depth mapping matrix q.
Definition: imageset.h:293
void setWidth(int w)
Sets a new width for both images.
Definition: imageset.h:90
int getBitsPerPixel(int imageNumber) const
Returns the number of bits that are required to store one image pixel.
Definition: imageset.h:383
int getNumberOfImages() const
Returns the number of images in this set.
Definition: imageset.h:404
void setExposureTime(int timeMicrosec)
Sets the exposure time that was used for capturing the image set.
Definition: imageset.h:472
int getExposureTime() const
Gets the exposure time in microseconds that was used for capturing the image set. ...
Definition: imageset.h:482
unsigned int getSequenceNumber() const
Returns the sequence number for this image set.
Definition: imageset.h:300
void setSequenceNumber(unsigned int num)
Sets the sequence number for this image set.
Definition: imageset.h:152
int getHeight() const
Returns the height of each image.
Definition: imageset.h:207
void setHeight(int h)
Sets a new width for both images.
Definition: imageset.h:95
unsigned char * getPixelData(ImageType what) const
Returns the pixel data for the given image.
Definition: imageset.h:285
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
Definition: imageset.h:372
int getSubpixelFactor() const
Gets the subpixel factor for this image set.
Definition: imageset.h:330
void setPixelFormat(int imageNumber, ImageFormat format)
Sets the pixel format for the given image.
Definition: imageset.h:116
void setRowStride(int imageNumber, int stride)
Sets a new row stride for the pixel data of one image.
Definition: imageset.h:104
void setLastSyncPulse(int seconds, int microsec)
Sets the timestamp of the last received sync pulse.
Definition: imageset.h:493
void getDisparityRange(int &minimum, int &maximum) const
Gets the value range for the disparity map contained in this image set. If the image set does not con...
Definition: imageset.h:322
ImageType
Supported image types.
Definition: imageset.h:67
ImageFormat
Image formats that can be transferred.
Definition: imageset.h:44
unsigned char * getPixelData(int imageNumber) const
Returns the pixel data for the given image.
Definition: imageset.h:272
bool hasImageType(ImageType what) const
Returns whether a left camera image is included in the enabled data.
Definition: imageset.h:434
A set of one to three images, but usually two (the left camera image and the disparity map)...
Definition: imageset.h:38
ImageFormat getPixelFormat(int imageNumber) const
Returns the pixel format for the given image.
Definition: imageset.h:245
void getLastSyncPulse(int &seconds, int &microsec) const
Gets the timestamp of the last received sync pulse.
Definition: imageset.h:505
int getRowStride(ImageType what) const
Returns the row stride for the pixel data of one image.
Definition: imageset.h:231
ImageFormat getPixelFormat(ImageType what) const
Returns the pixel format for the given image.
Definition: imageset.h:258
void setDisparityRange(int minimum, int maximum)
Sets the value range for the disparity map contained in this image set.
Definition: imageset.h:175
int getWidth() const
Returns the width of each image.
Definition: imageset.h:202
void setSubpixelFactor(int subpixFact)
Sets the subpixel factor for this image set.
Definition: imageset.h:183
void setQMatrix(const float *q)
Sets the pointer to the disparity-to-depth mapping matrix q.
Definition: imageset.h:145
Nerian Vision Technologies