YARP
Yet Another Robot Platform
Image.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_SIG_IMAGE_H
8 #define YARP_SIG_IMAGE_H
9 
10 #include <yarp/conf/system.h>
11 #include <yarp/os/NetUint16.h>
12 #include <yarp/os/Portable.h>
13 #include <yarp/os/Type.h>
14 #include <yarp/os/Vocab.h>
15 #include <yarp/sig/api.h>
16 #include <map>
17 
18 namespace yarp {
22  namespace sig {
23  class Image;
24  class FlexImage;
25  template <class T> class ImageOf;
26 
33  inline size_t PAD_BYTES (size_t len, size_t pad) {
34  const size_t rem = len % pad;
35  return (rem != 0) ? (pad - rem) : rem;
36  }
37  }
38 }
39 
40 // the image types partially reflect the IPL image types.
41 // There must be a pixel type for every ImageType entry.
43 {
71 };
72 
83 
84 public:
85 
90  Image();
91 
97  Image(const Image& alt);
98 
104  Image(Image&& other) noexcept;
105 
111  Image& operator=(const Image& alt);
112 
119  Image& operator=(Image &&other) noexcept;
120 
124  ~Image() override;
125 
126 
132  bool copy(const Image& alt);
133 
134 
143  bool copy(const Image& alt, size_t w, size_t h);
144 
145 
151  bool move(Image&& alt) noexcept;
152 
153 
159  bool swap(Image& alt);
160 
161 
166  inline size_t width() const { return imgWidth; }
167 
172  inline size_t height() const { return imgHeight; }
173 
178  virtual size_t getPixelSize() const;
179 
186  virtual int getPixelCode() const;
187 
192  inline size_t getRowSize() const { return imgRowSize; }
193 
194 
199  inline size_t getQuantum() const { return imgQuantum; }
200 
205  inline size_t getPadding() const
206  {
207  const size_t ret=imgRowSize-imgWidth*imgPixelSize;
208  return ret;
209  }
210 
216  inline unsigned char *getRow(size_t r)
217  {
218  // should we check limits?
219  return reinterpret_cast<unsigned char *>(data[r]);
220  }
221 
228  inline const unsigned char *getRow(size_t r) const
229  {
230  // should we check limits?
231  return reinterpret_cast<const unsigned char *>(data[r]);
232  }
233 
240  inline unsigned char *getPixelAddress(size_t x, size_t y) const {
241  return reinterpret_cast<unsigned char *>(data[y] + x*imgPixelSize);
242  }
243 
250  inline bool isPixel(size_t x, size_t y) const {
251  return (x<imgWidth && y<imgHeight);
252  }
253 
257  void zero();
258 
268  void resize(size_t imgWidth, size_t imgHeight);
269 
275  void resize(const Image& alt) {
276  resize(alt.width(),alt.height());
277  }
278 
284  void setExternal(const void *data, size_t imgWidth, size_t imgHeight);
285 
290  unsigned char *getRawImage() const;
291 
296  size_t getRawImageSize() const;
297 
298 #ifndef YARP_NO_DEPRECATED // Since YARP 3.2.0
305  YARP_DEPRECATED_MSG("Use yarp::cv::toCvMat instead")
306  void *getIplImage();
307 
314  YARP_DEPRECATED_MSG("Use yarp::cv::toCvMat instead")
315  const void *getIplImage() const;
316 
328  YARP_DEPRECATED_MSG("Use yarp::cv::fromCvMat instead")
329  void wrapIplImage(void *iplImage);
330 #endif // YARP_NO_DEPRECATED
331 
332  //void wrapRawImage(void *buf, int imgWidth, int imgHeight);
333 
334 
339  bool read(yarp::os::ConnectionReader& connection) override;
340 
345  bool write(yarp::os::ConnectionWriter& connection) const override;
346 
347  void setQuantum(size_t imgQuantum);
348 
353  bool topIsLowIndex() const {
354  return topIsLow;
355  }
356 
365  void setTopIsLowIndex(bool flag);
366 
367 
372  char **getRowArray() {
373  return data;
374  }
375 
376  yarp::os::Type getReadType() const override {
377  return yarp::os::Type::byName("yarp/image");
378  }
379 
380 protected:
381 
382  void setPixelCode(int imgPixelCode);
383 
384  //pixelCode and pixelsSize should be linked together consistently.
385  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
386  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
387  void setPixelSize(size_t imgPixelSize);
388 
389 
390 private:
392  static const std::map<YarpVocabPixelTypesEnum, size_t> pixelCode2Size;
393  size_t imgWidth, imgHeight, imgPixelSize, imgRowSize, imgQuantum;
394  int imgPixelCode;
395  bool topIsLow;
396 
397  char **data;
398  void *implementation;
399 
400  void synchronize();
401  void initialize();
402 
403  void copyPixels(const unsigned char *src, size_t id1,
404  unsigned char *dest, size_t id2, size_t w, size_t h,
405  size_t imageSize, size_t quantum1, size_t quantum2,
406  bool topIsLow1, bool topIsLow2);
407 };
408 
409 
415 public:
416 
417  void setPixelCode(int imgPixelCode) {
418  Image::setPixelCode(imgPixelCode);
419  }
420 
421 
422  void setPixelSize(size_t imgPixelSize) {
423  Image::setPixelSize(imgPixelSize);
424  //pixelCode and pixelsSize should be linked together consistently.
425  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
426  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
427  }
428 
429  void setQuantum(size_t imgQuantum) {
430  Image::setQuantum(imgQuantum);
431  }
432 
433 private:
434 };
435 
436 
437 
438 
439 #include <yarp/os/NetInt32.h>
440 
441 namespace yarp {
442  namespace sig {
443 
447  typedef unsigned char PixelMono;
448 
453 
458 
464  {
465  unsigned char r{0};
466  unsigned char g{0};
467  unsigned char b{0};
468 
469  PixelRgb() = default;
470  PixelRgb(unsigned char n_r,
471  unsigned char n_g,
472  unsigned char n_b) :
473  r(n_r),
474  g(n_g),
475  b(n_b)
476  {
477  }
478  };
480 
486  {
487  PixelRgba() = default;
488  PixelRgba(unsigned char n_r,
489  unsigned char n_g,
490  unsigned char n_b,
491  unsigned char n_a) :
492  r(n_r),
493  g(n_g),
494  b(n_b),
495  a(n_a)
496  {
497  }
498 
499  unsigned char r{0};
500  unsigned char g{0};
501  unsigned char b{0};
502  unsigned char a{0};
503  };
505 
511  {
512  unsigned char b{0};
513  unsigned char g{0};
514  unsigned char r{0};
515  unsigned char a{0};
516 
517  PixelBgra() = default;
518  PixelBgra(unsigned char n_r,
519  unsigned char n_g,
520  unsigned char n_b,
521  unsigned char n_a) :
522  b(n_b),
523  g(n_g),
524  r(n_r),
525  a(n_a)
526  {
527  }
528  };
530 
536  {
537  unsigned char b{0};
538  unsigned char g{0};
539  unsigned char r{0};
540 
541  PixelBgr() = default;
542  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b) :
543  b(n_b),
544  g(n_g),
545  r(n_r)
546  {
547  }
548  };
550 
556  {
557  unsigned char h{0};
558  unsigned char s{0};
559  unsigned char v{0};
560  };
562 
566  typedef char PixelMonoSigned;
567 
573  {
574  char r{0};
575  char g{0};
576  char b{0};
577  };
579 
583  typedef float PixelFloat;
584 
590  {
591  float r{0.0F};
592  float g{0.0F};
593  float b{0.0F};
594 
595  PixelRgbFloat() = default;
596  PixelRgbFloat(float n_r,
597  float n_g,
598  float n_b) :
599  r(n_r),
600  g(n_g),
601  b(n_b)
602  {
603  }
604  };
606 
612  {
616 
617  PixelRgbInt() = default;
618  PixelRgbInt(int n_r,
619  int n_g,
620  int n_b) :
621  r(n_r),
622  g(n_g),
623  b(n_b)
624  {
625  }
626  };
628 
634  {
635  float h{0.0F};
636  float s{0.0F};
637  float v{0.0F};
638  };
640  }
641 }
642 
643 
656 template <class T>
657 class yarp::sig::ImageOf : public Image
658 {
659 private:
660  T nullPixel;
661 public:
662  ImageOf() : Image(),
663  nullPixel()
664  {
666  }
667 
668  size_t getPixelSize() const override {
669  return sizeof(T);
670  }
671 
672  int getPixelCode() const override;
673 
674  inline T& pixel(size_t x, size_t y) {
675  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
676  }
677 
678  inline T& pixel(size_t x, size_t y) const {
679  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
680  }
681 
682  inline const T& operator()(size_t x, size_t y) const {
683  return pixel(x,y);
684  }
685 
686  inline T& operator()(size_t x, size_t y) {
687  return pixel(x,y);
688  }
689 
690  inline T& safePixel(size_t x, size_t y) {
691  if (!isPixel(x,y)) { return nullPixel; }
692  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
693  }
694 
695  inline const T& safePixel(size_t x, size_t y) const {
696  if (!isPixel(x,y)) { return nullPixel; }
697  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
698  }
699 };
700 
701 namespace yarp {
702 namespace sig {
703 
704 template<>
706  return VOCAB_PIXEL_MONO;
707 }
708 
709 template<>
711  return VOCAB_PIXEL_MONO16;
712 }
713 
714 template<>
716  return VOCAB_PIXEL_RGB;
717 }
718 
719 template<>
721  return VOCAB_PIXEL_RGBA;
722 }
723 
724 template<>
726  return VOCAB_PIXEL_HSV;
727 }
728 
729 template<>
731  return VOCAB_PIXEL_BGR;
732 }
733 
734 template<>
736  return VOCAB_PIXEL_BGRA;
737 }
738 
739 template<>
742 }
743 
744 template<>
746  return VOCAB_PIXEL_RGB_SIGNED;
747 }
748 
749 template<>
751  return VOCAB_PIXEL_MONO_FLOAT;
752 }
753 
754 template<>
756  return VOCAB_PIXEL_RGB_FLOAT;
757 }
758 
759 template<>
761  return VOCAB_PIXEL_RGB_INT;
762 }
763 
764 template<>
766  return VOCAB_PIXEL_HSV_FLOAT;
767 }
768 
769 template<>
771  return VOCAB_PIXEL_INT;
772 }
773 
774 template<typename T>
775 inline int ImageOf<T>::getPixelCode() const {
776  return -(static_cast<int>(sizeof(T)));
777 }
778 
779 } // namespace sig
780 } // namespace yarp
781 
782 #endif // YARP_SIG_IMAGE_H
bool ret
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:17
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
static Type byName(const char *name)
Definition: Type.cpp:171
Image class with user control of representation details.
Definition: Image.h:414
void setQuantum(size_t imgQuantum)
Definition: Image.h:429
void setPixelCode(int imgPixelCode)
Definition: Image.h:417
void setPixelSize(size_t imgPixelSize)
Definition: Image.h:422
Typed image class.
Definition: Image.h:658
const T & operator()(size_t x, size_t y) const
Definition: Image.h:682
T & pixel(size_t x, size_t y)
Definition: Image.h:674
T & pixel(size_t x, size_t y) const
Definition: Image.h:678
int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:775
T & safePixel(size_t x, size_t y)
Definition: Image.h:690
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition: Image.h:668
T & operator()(size_t x, size_t y)
Definition: Image.h:686
const T & safePixel(size_t x, size_t y) const
Definition: Image.h:695
Base class for storing images.
Definition: Image.h:82
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:240
bool swap(Image &alt)
swap operator.
Definition: Image.cpp:888
Image & operator=(const Image &alt)
Assignment operator.
Definition: Image.cpp:827
bool topIsLowIndex() const
Definition: Image.h:353
void resize(const Image &alt)
Reallocate the size of the image to match another, throwing away the actual content of the image.
Definition: Image.h:275
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:501
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
size_t getPadding() const
Returns the number of padding bytes.
Definition: Image.h:205
bool read(yarp::os::ConnectionReader &connection) override
Read image from a connection.
Definition: Image.cpp:674
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:490
bool move(Image &&alt) noexcept
move operator.
Definition: Image.cpp:874
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:903
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:192
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:541
Image()
Default constructor.
Definition: Image.cpp:410
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition: Image.cpp:435
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
Definition: Image.cpp:785
bool copy(const Image &alt)
Copy operator.
Definition: Image.cpp:836
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
Definition: Image.cpp:550
~Image() override
Destructor.
Definition: Image.cpp:427
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:216
void setPixelSize(size_t imgPixelSize)
Definition: Image.cpp:482
void * getIplImage()
Returns IPL/OpenCV view of image, if possible.
Definition: Image.cpp:560
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition: Image.cpp:452
yarp::os::Type getReadType() const override
Definition: Image.h:376
const unsigned char * getRow(size_t r) const
Get the address of a the first byte of a row in memory, const versions.
Definition: Image.h:228
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition: Image.h:250
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.cpp:511
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:199
void zero()
Set all pixels to 0.
Definition: Image.cpp:445
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:372
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:440
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
YarpVocabPixelTypesEnum
Definition: Image.h:43
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
Definition: Image.h:62
@ VOCAB_PIXEL_YUV_420
Definition: Image.h:67
@ VOCAB_PIXEL_RGBA
Definition: Image.h:48
@ VOCAB_PIXEL_INT
Definition: Image.h:50
@ VOCAB_PIXEL_MONO16
Definition: Image.h:46
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
Definition: Image.h:61
@ VOCAB_PIXEL_YUV_444
Definition: Image.h:68
@ VOCAB_PIXEL_BGRA
Definition: Image.h:49
@ VOCAB_PIXEL_MONO_SIGNED
Definition: Image.h:53
@ VOCAB_PIXEL_BGR
Definition: Image.h:52
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:56
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
Definition: Image.h:65
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
Definition: Image.h:59
@ VOCAB_PIXEL_HSV_FLOAT
Definition: Image.h:58
@ VOCAB_PIXEL_YUV_422
Definition: Image.h:69
@ VOCAB_PIXEL_HSV
Definition: Image.h:51
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG16
Definition: Image.h:64
@ VOCAB_PIXEL_RGB_SIGNED
Definition: Image.h:54
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG16
Definition: Image.h:60
@ VOCAB_PIXEL_INVALID
Definition: Image.h:44
@ VOCAB_PIXEL_RGB_FLOAT
Definition: Image.h:57
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
Definition: Image.h:63
@ VOCAB_PIXEL_MONO
Definition: Image.h:45
@ VOCAB_PIXEL_RGB_INT
Definition: Image.h:55
@ VOCAB_PIXEL_YUV_411
Definition: Image.h:70
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB16
Definition: Image.h:66
@ VOCAB_PIXEL_RGB
Definition: Image.h:47
yarp::sig::ImageOf< T > fromCvMat(::cv::Mat &cvImage)
Convert a cv::Mat to a yarp::sig::ImageOf object.
Definition: Cv-inl.h:88
std::uint16_t NetUint16
Definition of the NetUint16 type.
Definition: NetUint16.h:30
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:30
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:28
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:566
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition: Image.h:33
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:452
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:447
float PixelFloat
Floating point pixel type.
Definition: Image.h:583
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:457
The main, catch-all namespace for YARP.
Definition: dirs.h:16
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:536
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:542
Packed BGRA pixel type.
Definition: Image.h:511
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:518
Floating point HSV pixel type.
Definition: Image.h:634
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:556
Floating point RGB pixel type.
Definition: Image.h:590
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:596
Integer RGB pixel type.
Definition: Image.h:612
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:618
Signed, packed RGB pixel type.
Definition: Image.h:573
Packed RGB pixel type.
Definition: Image.h:464
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:470
Packed RGBA pixel type.
Definition: Image.h:486
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:488
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:191
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING
Suppress MSVC C4251 warning for the next line.
Definition: system.h:335
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:190
#define YARP_sig_API
Definition: api.h:18