libvisiontransfer  8.1.0
imageset.h
1 /*******************************************************************************
2  * Copyright (c) 2020 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 
194  DEPRECATED("Only compatible with two-image sets: use setNumberOfImages() and setIndexOf() instead")
195  void setImageDisparityPair(bool dispPair);
196 
200  int getWidth() const {return width;}
201 
205  int getHeight() const {return height;}
206 
216  int getRowStride(int imageNumber) const {
217  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
218  return rowStride[imageNumber];
219  }
220 
229  int getRowStride(ImageType what) const {
230  int idx = getIndexOf(what, true);
231  return getRowStride(idx);
232  }
233 
243  ImageFormat getPixelFormat(int imageNumber) const {
244  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
245  return formats[imageNumber];
246  }
247 
257  int idx = getIndexOf(what, true);
258  return getPixelFormat(idx);
259  }
260 
270  unsigned char* getPixelData(int imageNumber) const {
271  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
272  return data[imageNumber];
273  }
274 
283  unsigned char* getPixelData(ImageType what) const {
284  int idx = getIndexOf(what, true);
285  return getPixelData(idx);
286  }
287 
291  const float* getQMatrix() const {
292  return qMatrix;
293  }
294 
298  unsigned int getSequenceNumber() const {return seqNum;}
299 
307  void getTimestamp(int& seconds, int& microsec) const {
308  seconds = timeSec;
309  microsec = timeMicrosec;
310  }
311 
320  void getDisparityRange(int& minimum, int& maximum) const {
321  minimum = minDisparity;
322  maximum = maxDisparity;
323  }
324 
328  int getSubpixelFactor() const {
329  return subpixelFactor;
330  }
331 
338  void writePgmFile(int imageNumber, const char* fileName) const;
339 
352  DEPRECATED("Only compatible with two-image sets: use hasImageType(ImageSet::IMAGE_DISPARITY) instead")
353  bool isImageDisparityPair() const {
354  return (getNumberOfImages()==2) && hasImageType(IMAGE_DISPARITY);
355  }
356 
360  void copyTo(ImageSet& dest);
361 
368  int getBytesPerPixel(int imageNumber) const {
369  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
370  return getBytesPerPixel(formats[imageNumber]);
371  }
372 
379  int getBitsPerPixel(int imageNumber) const {
380  assert(imageNumber >= 0 && imageNumber < getNumberOfImages());
381  return getBitsPerPixel(formats[imageNumber]);
382  }
383 
384  int getBitsPerPixel(ImageType what) const {
385  int idx = getIndexOf(what, true);
386  return getBitsPerPixel(idx);
387  }
388 
389  static int getBitsPerPixel(ImageFormat format);
390 
395  static int getBytesPerPixel(ImageFormat format);
396 
400  int getNumberOfImages() const {
401  return numberOfImages;
402  }
403 
407  void setNumberOfImages(int number) {
408  assert(number >= 1 && number <= MAX_SUPPORTED_IMAGES);
409  numberOfImages = number;
410  }
411 
415  ImageType getImageType(int imageNumber) const;
416 
425  int getIndexOf(ImageType what, bool throwIfNotFound=false) const;
426 
430  bool hasImageType(ImageType what) const {
431  return getIndexOf(what) >= 0;
432  }
433 
434 
442  void setIndexOf(ImageType what, int idx);
443 
444 
445 #ifdef CV_MAJOR_VERSION
446 
460  inline void toOpenCVImage(int imageNumber, cv::Mat& dest, bool convertRgbToBgr = true);
461 #endif
462 
463 private:
464  // No pimpl idiom here as almost everything is inlined.
465  int width;
466  int height;
467  int rowStride[MAX_SUPPORTED_IMAGES];
468  ImageFormat formats[MAX_SUPPORTED_IMAGES];
469  unsigned char* data[MAX_SUPPORTED_IMAGES];
470  const float* qMatrix;
471  int timeSec;
472  int timeMicrosec;
473  unsigned int seqNum;
474  int minDisparity;
475  int maxDisparity;
476  int subpixelFactor;
477  int* referenceCounter;
478  int numberOfImages;
479 
480  int indexLeftImage;
481  int indexRightImage;
482  int indexDisparityImage;
483 
484  void copyData(ImageSet& dest, const ImageSet& src, bool countRef);
485  void decrementReference();
486 };
487 
488 // For source compatibility
489 class DEPRECATED("Use ImageSet instead.") ImagePair: public ImageSet {
490 };
491 
492 } // namespace
493 
494 #include "visiontransfer/imageset-opencv.h"
495 #endif
496 
void setNumberOfImages(int number)
Sets the number of valid images in this set.
Definition: imageset.h:407
int getRowStride(int imageNumber) const
Returns the row stride for the pixel data of one image.
Definition: imageset.h:216
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:307
const float * getQMatrix() const
Returns a pointer to the disparity-to-depth mapping matrix q.
Definition: imageset.h:291
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:379
int getNumberOfImages() const
Returns the number of images in this set.
Definition: imageset.h:400
unsigned int getSequenceNumber() const
Returns the sequence number for this image set.
Definition: imageset.h:298
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:205
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:283
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
Definition: imageset.h:368
int getSubpixelFactor() const
Gets the subpixel factor for this image set.
Definition: imageset.h:328
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 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:320
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:270
bool hasImageType(ImageType what) const
Returns whether a left camera image is included in the enabled data.
Definition: imageset.h:430
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:243
int getRowStride(ImageType what) const
Returns the row stride for the pixel data of one image.
Definition: imageset.h:229
ImageFormat getPixelFormat(ImageType what) const
Returns the pixel format for the given image.
Definition: imageset.h:256
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:200
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