YARP
Yet Another Robot Platform
Image.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_SIG_IMAGE_H
11 #define YARP_SIG_IMAGE_H
12 
13 #include <yarp/conf/system.h>
14 #include <yarp/os/NetUint16.h>
15 #include <yarp/os/Portable.h>
16 #include <yarp/os/Type.h>
17 #include <yarp/os/Vocab.h>
18 #include <yarp/sig/api.h>
19 #include <map>
20 
21 namespace yarp {
25  namespace sig {
26  class Image;
27  class FlexImage;
28  template <class T> class ImageOf;
29 
36  inline size_t PAD_BYTES (size_t len, size_t pad) {
37  const size_t rem = len % pad;
38  return (rem != 0) ? (pad - rem) : rem;
39  }
40  }
41 }
42 
43 // the image types partially reflect the IPL image types.
44 // There must be a pixel type for every ImageType entry.
46 {
74 };
75 
86 
87 public:
88 
93  Image();
94 
100  Image(const Image& alt);
101 
107  Image(Image&& other) noexcept;
108 
114  Image& operator=(const Image& alt);
115 
122  Image& operator=(Image &&other) noexcept;
123 
127  ~Image() override;
128 
129 
135  bool copy(const Image& alt);
136 
137 
146  bool copy(const Image& alt, size_t w, size_t h);
147 
148 
153  inline size_t width() const { return imgWidth; }
154 
159  inline size_t height() const { return imgHeight; }
160 
165  virtual size_t getPixelSize() const;
166 
173  virtual int getPixelCode() const;
174 
179  inline size_t getRowSize() const { return imgRowSize; }
180 
181 
186  inline size_t getQuantum() const { return imgQuantum; }
187 
192  inline size_t getPadding() const
193  {
194  const size_t ret=imgRowSize-imgWidth*imgPixelSize;
195  return ret;
196  }
197 
203  inline unsigned char *getRow(size_t r)
204  {
205  // should we check limits?
206  return reinterpret_cast<unsigned char *>(data[r]);
207  }
208 
215  inline const unsigned char *getRow(size_t r) const
216  {
217  // should we check limits?
218  return reinterpret_cast<const unsigned char *>(data[r]);
219  }
220 
227  inline unsigned char *getPixelAddress(size_t x, size_t y) const {
228  return reinterpret_cast<unsigned char *>(data[y] + x*imgPixelSize);
229  }
230 
237  inline bool isPixel(size_t x, size_t y) const {
238  return (x<imgWidth && y<imgHeight);
239  }
240 
244  void zero();
245 
255  void resize(size_t imgWidth, size_t imgHeight);
256 
262  void resize(const Image& alt) {
263  resize(alt.width(),alt.height());
264  }
265 
271  void setExternal(const void *data, size_t imgWidth, size_t imgHeight);
272 
277  unsigned char *getRawImage() const;
278 
283  size_t getRawImageSize() const;
284 
285 #ifndef YARP_NO_DEPRECATED // Since YARP 3.2.0
292  YARP_DEPRECATED_MSG("Use yarp::cv::toCvMat instead")
293  void *getIplImage();
294 
301  YARP_DEPRECATED_MSG("Use yarp::cv::toCvMat instead")
302  const void *getIplImage() const;
303 
315  YARP_DEPRECATED_MSG("Use yarp::cv::fromCvMat instead")
316  void wrapIplImage(void *iplImage);
317 #endif // YARP_NO_DEPRECATED
318 
319  //void wrapRawImage(void *buf, int imgWidth, int imgHeight);
320 
321 
326  bool read(yarp::os::ConnectionReader& connection) override;
327 
332  bool write(yarp::os::ConnectionWriter& connection) const override;
333 
334  void setQuantum(size_t imgQuantum);
335 
340  bool topIsLowIndex() const {
341  return topIsLow;
342  }
343 
352  void setTopIsLowIndex(bool flag) {
353  topIsLow = flag;
354  }
355 
356 
361  char **getRowArray() {
362  return data;
363  }
364 
365  yarp::os::Type getReadType() const override {
366  return yarp::os::Type::byName("yarp/image");
367  }
368 
369 protected:
370 
371  void setPixelCode(int imgPixelCode);
372 
373  //pixelCode and pixelsSize should be linked together consistently.
374  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
375  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
376  void setPixelSize(size_t imgPixelSize);
377 
378 
379 private:
381  static const std::map<YarpVocabPixelTypesEnum, size_t> pixelCode2Size;
382  size_t imgWidth, imgHeight, imgPixelSize, imgRowSize, imgQuantum;
383  int imgPixelCode;
384  bool topIsLow;
385 
386  char **data;
387  void *implementation;
388 
389  void synchronize();
390  void initialize();
391 
392  void copyPixels(const unsigned char *src, size_t id1,
393  unsigned char *dest, size_t id2, size_t w, size_t h,
394  size_t imageSize, size_t quantum1, size_t quantum2,
395  bool topIsLow1, bool topIsLow2);
396 };
397 
398 
404 public:
405 
406  void setPixelCode(int imgPixelCode) {
407  Image::setPixelCode(imgPixelCode);
408  }
409 
410 
411  void setPixelSize(size_t imgPixelSize) {
412  Image::setPixelSize(imgPixelSize);
413  //pixelCode and pixelsSize should be linked together consistently.
414  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
415  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
416  }
417 
418  void setQuantum(size_t imgQuantum) {
419  Image::setQuantum(imgQuantum);
420  }
421 
422 private:
423 };
424 
425 
426 
427 
428 #include <yarp/os/NetInt32.h>
429 
430 namespace yarp {
431  namespace sig {
432 
436  typedef unsigned char PixelMono;
437 
442 
447 
453  {
454  unsigned char r{0};
455  unsigned char g{0};
456  unsigned char b{0};
457 
458  PixelRgb() = default;
459  PixelRgb(unsigned char n_r,
460  unsigned char n_g,
461  unsigned char n_b) :
462  r(n_r),
463  g(n_g),
464  b(n_b)
465  {
466  }
467  };
469 
475  {
476  PixelRgba() = default;
477  PixelRgba(unsigned char n_r,
478  unsigned char n_g,
479  unsigned char n_b,
480  unsigned char n_a) :
481  r(n_r),
482  g(n_g),
483  b(n_b),
484  a(n_a)
485  {
486  }
487 
488  unsigned char r{0};
489  unsigned char g{0};
490  unsigned char b{0};
491  unsigned char a{0};
492  };
494 
500  {
501  unsigned char b{0};
502  unsigned char g{0};
503  unsigned char r{0};
504  unsigned char a{0};
505 
506  PixelBgra() = default;
507  PixelBgra(unsigned char n_r,
508  unsigned char n_g,
509  unsigned char n_b,
510  unsigned char n_a) :
511  b(n_b),
512  g(n_g),
513  r(n_r),
514  a(n_a)
515  {
516  }
517  };
519 
525  {
526  unsigned char b{0};
527  unsigned char g{0};
528  unsigned char r{0};
529 
530  PixelBgr() = default;
531  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b) :
532  b(n_b),
533  g(n_g),
534  r(n_r)
535  {
536  }
537  };
539 
545  {
546  unsigned char h{0};
547  unsigned char s{0};
548  unsigned char v{0};
549  };
551 
555  typedef char PixelMonoSigned;
556 
562  {
563  char r{0};
564  char g{0};
565  char b{0};
566  };
568 
572  typedef float PixelFloat;
573 
579  {
580  float r{0.0F};
581  float g{0.0F};
582  float b{0.0F};
583 
584  PixelRgbFloat() = default;
585  PixelRgbFloat(float n_r,
586  float n_g,
587  float n_b) :
588  r(n_r),
589  g(n_g),
590  b(n_b)
591  {
592  }
593  };
595 
601  {
605 
606  PixelRgbInt() = default;
607  PixelRgbInt(int n_r,
608  int n_g,
609  int n_b) :
610  r(n_r),
611  g(n_g),
612  b(n_b)
613  {
614  }
615  };
617 
623  {
624  float h{0.0F};
625  float s{0.0F};
626  float v{0.0F};
627  };
629  }
630 }
631 
632 
645 template <class T>
646 class yarp::sig::ImageOf : public Image
647 {
648 private:
649  T nullPixel;
650 public:
651  ImageOf() : Image(),
652  nullPixel()
653  {
655  }
656 
657  size_t getPixelSize() const override {
658  return sizeof(T);
659  }
660 
661  int getPixelCode() const override;
662 
663  inline T& pixel(size_t x, size_t y) {
664  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
665  }
666 
667  inline T& pixel(size_t x, size_t y) const {
668  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
669  }
670 
671  inline const T& operator()(size_t x, size_t y) const {
672  return pixel(x,y);
673  }
674 
675  inline T& operator()(size_t x, size_t y) {
676  return pixel(x,y);
677  }
678 
679  inline T& safePixel(size_t x, size_t y) {
680  if (!isPixel(x,y)) { return nullPixel; }
681  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
682  }
683 
684  inline const T& safePixel(size_t x, size_t y) const {
685  if (!isPixel(x,y)) { return nullPixel; }
686  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
687  }
688 };
689 
690 namespace yarp {
691 namespace sig {
692 
693 template<>
695  return VOCAB_PIXEL_MONO;
696 }
697 
698 template<>
700  return VOCAB_PIXEL_MONO16;
701 }
702 
703 template<>
705  return VOCAB_PIXEL_RGB;
706 }
707 
708 template<>
710  return VOCAB_PIXEL_RGBA;
711 }
712 
713 template<>
715  return VOCAB_PIXEL_HSV;
716 }
717 
718 template<>
720  return VOCAB_PIXEL_BGR;
721 }
722 
723 template<>
725  return VOCAB_PIXEL_BGRA;
726 }
727 
728 template<>
731 }
732 
733 template<>
735  return VOCAB_PIXEL_RGB_SIGNED;
736 }
737 
738 template<>
740  return VOCAB_PIXEL_MONO_FLOAT;
741 }
742 
743 template<>
745  return VOCAB_PIXEL_RGB_FLOAT;
746 }
747 
748 template<>
750  return VOCAB_PIXEL_RGB_INT;
751 }
752 
753 template<>
755  return VOCAB_PIXEL_HSV_FLOAT;
756 }
757 
758 template<>
760  return VOCAB_PIXEL_INT;
761 }
762 
763 template<typename T>
764 inline int ImageOf<T>::getPixelCode() const {
765  return -(static_cast<int>(sizeof(T)));
766 }
767 
768 } // namespace sig
769 } // namespace yarp
770 
771 #endif // YARP_SIG_IMAGE_H
bool ret
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
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:29
static Type byName(const char *name)
Definition: Type.cpp:174
Image class with user control of representation details.
Definition: Image.h:403
void setQuantum(size_t imgQuantum)
Definition: Image.h:418
void setPixelCode(int imgPixelCode)
Definition: Image.h:406
void setPixelSize(size_t imgPixelSize)
Definition: Image.h:411
Typed image class.
Definition: Image.h:647
const T & operator()(size_t x, size_t y) const
Definition: Image.h:671
T & pixel(size_t x, size_t y)
Definition: Image.h:663
T & pixel(size_t x, size_t y) const
Definition: Image.h:667
int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:764
T & safePixel(size_t x, size_t y)
Definition: Image.h:679
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition: Image.h:657
T & operator()(size_t x, size_t y)
Definition: Image.h:675
const T & safePixel(size_t x, size_t y) const
Definition: Image.h:684
Base class for storing images.
Definition: Image.h:85
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:227
Image & operator=(const Image &alt)
Assignment operator.
Definition: Image.cpp:833
bool topIsLowIndex() const
Definition: Image.h:340
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:262
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:510
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
size_t getPadding() const
Returns the number of padding bytes.
Definition: Image.h:192
bool read(yarp::os::ConnectionReader &connection) override
Read image from a connection.
Definition: Image.cpp:665
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:504
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:878
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:179
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:534
Image()
Default constructor.
Definition: Image.cpp:424
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition: Image.cpp:449
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.h:352
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
Definition: Image.cpp:772
bool copy(const Image &alt)
Copy operator.
Definition: Image.cpp:842
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:543
~Image() override
Destructor.
Definition: Image.cpp:441
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:203
void setPixelSize(size_t imgPixelSize)
Definition: Image.cpp:496
void * getIplImage()
Returns IPL/OpenCV view of image, if possible.
Definition: Image.cpp:553
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:466
yarp::os::Type getReadType() const override
Definition: Image.h:365
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:215
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition: Image.h:237
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:186
void zero()
Set all pixels to 0.
Definition: Image.cpp:459
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:361
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:454
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
YarpVocabPixelTypesEnum
Definition: Image.h:46
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
Definition: Image.h:65
@ VOCAB_PIXEL_YUV_420
Definition: Image.h:70
@ VOCAB_PIXEL_RGBA
Definition: Image.h:51
@ VOCAB_PIXEL_INT
Definition: Image.h:53
@ VOCAB_PIXEL_MONO16
Definition: Image.h:49
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
Definition: Image.h:64
@ VOCAB_PIXEL_YUV_444
Definition: Image.h:71
@ VOCAB_PIXEL_BGRA
Definition: Image.h:52
@ VOCAB_PIXEL_MONO_SIGNED
Definition: Image.h:56
@ VOCAB_PIXEL_BGR
Definition: Image.h:55
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:59
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
Definition: Image.h:68
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
Definition: Image.h:62
@ VOCAB_PIXEL_HSV_FLOAT
Definition: Image.h:61
@ VOCAB_PIXEL_YUV_422
Definition: Image.h:72
@ VOCAB_PIXEL_HSV
Definition: Image.h:54
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG16
Definition: Image.h:67
@ VOCAB_PIXEL_RGB_SIGNED
Definition: Image.h:57
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG16
Definition: Image.h:63
@ VOCAB_PIXEL_INVALID
Definition: Image.h:47
@ VOCAB_PIXEL_RGB_FLOAT
Definition: Image.h:60
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
Definition: Image.h:66
@ VOCAB_PIXEL_MONO
Definition: Image.h:48
@ VOCAB_PIXEL_RGB_INT
Definition: Image.h:58
@ VOCAB_PIXEL_YUV_411
Definition: Image.h:73
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB16
Definition: Image.h:69
@ VOCAB_PIXEL_RGB
Definition: Image.h:50
yarp::sig::ImageOf< T > fromCvMat(::cv::Mat &cvImage)
Convert a cv::Mat to a yarp::sig::ImageOf object.
Definition: Cv-inl.h:91
std::uint16_t NetUint16
Definition of the NetUint16 type.
Definition: NetUint16.h:33
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:33
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:555
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition: Image.h:36
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:441
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:436
float PixelFloat
Floating point pixel type.
Definition: Image.h:572
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:446
The main, catch-all namespace for YARP.
Definition: environment.h:25
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:525
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:531
Packed BGRA pixel type.
Definition: Image.h:500
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:507
Floating point HSV pixel type.
Definition: Image.h:623
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:545
Floating point RGB pixel type.
Definition: Image.h:579
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:585
Integer RGB pixel type.
Definition: Image.h:601
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:607
Signed, packed RGB pixel type.
Definition: Image.h:562
Packed RGB pixel type.
Definition: Image.h:453
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:459
Packed RGBA pixel type.
Definition: Image.h:475
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:477
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:194
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING
Suppress MSVC C4251 warning for the next line.
Definition: system.h:338
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:193
#define YARP_sig_API
Definition: api.h:19