YARP
Yet Another Robot Platform
Image.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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 
115  Image& operator=(Image &&other) noexcept;
116 
120  virtual ~Image();
121 
127  const Image& operator=(const Image& alt);
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 (unsigned char *)(data[r]);
207  }
208 
215  inline const unsigned char *getRow(size_t r) const
216  {
217  // should we check limits?
218  return (const unsigned char *)(data[r]);
219  }
220 
227  inline unsigned char *getPixelAddress(size_t x, size_t y) const {
228  return (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
286 
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,g,b;
455 
456  PixelRgb() { r = g = b = 0; }
457  PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
458  { r = n_r; g = n_g; b = n_b; }
459  };
461 
467  {
468  unsigned char r,g,b,a;
469 
470  PixelRgba() { r = g = b = a = 0; }
471  PixelRgba(unsigned char n_r, unsigned char n_g,
472  unsigned char n_b, unsigned char n_a)
473  { r = n_r; g = n_g; b = n_b; a = n_a; }
474  };
476 
482  {
483  unsigned char b,g,r,a;
484 
485  PixelBgra() { r = g = b = a = 0; }
486  PixelBgra(unsigned char n_r, unsigned char n_g,
487  unsigned char n_b, unsigned char n_a)
488  { r = n_r; g = n_g; b = n_b; a = n_a; }
489  };
491 
497  {
498  unsigned char b,g,r;
499  PixelBgr() { b = g = r = 0; }
500  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
501  { r = n_r; g = n_g; b = n_b; }
502  };
504 
510  unsigned char h,s,v;
511  };
513 
517  typedef char PixelMonoSigned;
518 
524  char r,g,b;
525  };
527 
531  typedef float PixelFloat;
532 
538  float r,g,b;
539  PixelRgbFloat() { r = g = b = 0; }
540  PixelRgbFloat(float n_r, float n_g, float n_b)
541  { r = n_r; g = n_g; b = n_b; }
542  };
544 
551  PixelRgbInt() { r = g = b = 0; }
552  PixelRgbInt(int n_r, int n_g, int n_b) {
553  r = n_r; g = n_g; b = n_b;
554  }
555  };
557 
562  struct PixelHsvFloat {
563  float h,s,v;
564  };
566 
567  }
568 }
569 
570 
583 template <class T>
584 class yarp::sig::ImageOf : public Image
585 {
586 private:
587  T nullPixel;
588 public:
589  ImageOf() : Image(),
590  nullPixel()
591  {
592  setPixelCode(getPixelCode());
593  }
594 
595  size_t getPixelSize() const override {
596  return sizeof(T);
597  }
598 
599  int getPixelCode() const override;
600 
601  inline T& pixel(size_t x, size_t y) {
602  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
603  }
604 
605  inline T& pixel(size_t x, size_t y) const {
606  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
607  }
608 
609  inline const T& operator()(size_t x, size_t y) const {
610  return pixel(x,y);
611  }
612 
613  inline T& operator()(size_t x, size_t y) {
614  return pixel(x,y);
615  }
616 
617  inline T& safePixel(size_t x, size_t y) {
618  if (!isPixel(x,y)) { return nullPixel; }
619  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
620  }
621 
622  inline const T& safePixel(size_t x, size_t y) const {
623  if (!isPixel(x,y)) { return nullPixel; }
624  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
625  }
626 };
627 
628 namespace yarp {
629 namespace sig {
630 
631 template<>
633  return VOCAB_PIXEL_MONO;
634 }
635 
636 template<>
638  return VOCAB_PIXEL_MONO16;
639 }
640 
641 template<>
643  return VOCAB_PIXEL_RGB;
644 }
645 
646 template<>
648  return VOCAB_PIXEL_RGBA;
649 }
650 
651 template<>
653  return VOCAB_PIXEL_HSV;
654 }
655 
656 template<>
658  return VOCAB_PIXEL_BGR;
659 }
660 
661 template<>
663  return VOCAB_PIXEL_BGRA;
664 }
665 
666 template<>
669 }
670 
671 template<>
673  return VOCAB_PIXEL_RGB_SIGNED;
674 }
675 
676 template<>
678  return VOCAB_PIXEL_MONO_FLOAT;
679 }
680 
681 template<>
683  return VOCAB_PIXEL_RGB_FLOAT;
684 }
685 
686 template<>
688  return VOCAB_PIXEL_RGB_INT;
689 }
690 
691 template<>
693  return VOCAB_PIXEL_HSV_FLOAT;
694 }
695 
696 template<>
698  return VOCAB_PIXEL_INT;
699 }
700 
701 template<typename T>
702 inline int ImageOf<T>::getPixelCode() const {
703  return -((int) sizeof(T));
704 }
705 
706 } // namespace sig
707 } // namespace yarp
708 
709 #endif // YARP_SIG_IMAGE_H
Floating point RGB pixel type.
Definition: Image.h:537
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:553
unsigned char r
Definition: Image.h:483
Packed RGB pixel type.
Definition: Image.h:452
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:516
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:506
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition: Image.h:237
bool ret
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:194
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:509
Floating point HSV pixel type.
Definition: Image.h:562
Packed RGBA pixel type.
Definition: Image.h:466
float PixelFloat
Floating point pixel type.
Definition: Image.h:531
This is a base class for objects that can be both read from and be written to the YARP network...
Definition: Portable.h:28
Integer RGB pixel type.
Definition: Image.h:549
static Type byName(const char *name)
Definition: Type.cpp:174
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:179
T & pixel(size_t x, size_t y)
Definition: Image.h:601
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:540
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:186
bool topIsLowIndex() const
Definition: Image.h:340
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:193
yarp::os::NetInt32 r
Definition: Image.h:550
int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:702
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
The main, catch-all namespace for YARP.
Definition: numeric.h:47
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:436
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
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition: Image.h:595
unsigned char r
Definition: Image.h:468
Signed, packed RGB pixel type.
Definition: Image.h:523
void setPixelCode(int imgPixelCode)
Definition: Image.h:406
Image class with user control of representation details.
Definition: Image.h:403
#define YARP_sig_API
Definition: api.h:19
unsigned char v
Definition: Image.h:510
Base class for storing images.
Definition: Image.h:85
An interface for writing to a network connection.
std::uint16_t NetUint16
Definition of the NetUint16 type.
Definition: NetUint16.h:33
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:227
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:471
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:441
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:203
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:496
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:517
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:33
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.h:352
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
void setQuantum(size_t imgQuantum)
Definition: Image.h:418
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
An interface for reading from a network connection.
size_t getPadding() const
Returns the number of padding bytes.
Definition: Image.h:192
unsigned char r
Definition: Image.h:498
yarp::rosmsg::sensor_msgs::Image Image
Definition: Image.h:24
Packed BGRA pixel type.
Definition: Image.h:481
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition: Image.h:36
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:552
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:446
T & pixel(size_t x, size_t y) const
Definition: Image.h:605
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:500
Typed image class.
Definition: Image.h:28
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
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
yarp::os::Type getReadType() const override
Definition: Image.h:365
void setPixelSize(size_t imgPixelSize)
Definition: Image.h:411
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:457
const T & operator()(size_t x, size_t y) const
Definition: Image.h:609
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:500
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:486
T & safePixel(size_t x, size_t y)
Definition: Image.h:617
YarpVocabPixelTypesEnum
Definition: Image.h:45
unsigned char r
Definition: Image.h:454
void setPixelSize(size_t imgPixelSize)
Definition: Image.cpp:492
const T & safePixel(size_t x, size_t y) const
Definition: Image.h:622
T & operator()(size_t x, size_t y)
Definition: Image.h:613
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:361
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2852
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING
Suppress MSVC C4251 warning for the next line.
Definition: system.h:338