YARP
Yet Another Robot Platform
Image.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 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 int 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 
105  virtual ~Image();
106 
112  const Image& operator=(const Image& alt);
113 
114 
120  bool copy(const Image& alt);
121 
122 
131  bool copy(const Image& alt, size_t w, size_t h);
132 
133 
138  inline size_t width() const { return imgWidth; }
139 
144  inline size_t height() const { return imgHeight; }
145 
150  virtual size_t getPixelSize() const;
151 
158  virtual int getPixelCode() const;
159 
164  inline size_t getRowSize() const { return imgRowSize; }
165 
166 
171  inline size_t getQuantum() const { return imgQuantum; }
172 
177  inline size_t getPadding() const
178  {
179  const size_t ret=imgRowSize-imgWidth*imgPixelSize;
180  return ret;
181  }
182 
188  inline unsigned char *getRow(size_t r)
189  {
190  // should we check limits?
191  return (unsigned char *)(data[r]);
192  }
193 
200  inline const unsigned char *getRow(size_t r) const
201  {
202  // should we check limits?
203  return (const unsigned char *)(data[r]);
204  }
205 
212  inline unsigned char *getPixelAddress(size_t x, size_t y) const {
213  return (unsigned char *)(data[y] + x*imgPixelSize);
214  }
215 
222  inline bool isPixel(size_t x, size_t y) const {
223  return (x<imgWidth && y<imgHeight);
224  }
225 
229  void zero();
230 
240  void resize(size_t imgWidth, size_t imgHeight);
241 
247  void resize(const Image& alt) {
248  resize(alt.width(),alt.height());
249  }
250 
256  void setExternal(const void *data, size_t imgWidth, size_t imgHeight);
257 
262  unsigned char *getRawImage() const;
263 
268  size_t getRawImageSize() const;
269 
276  void *getIplImage();
277 
284  const void *getIplImage() const;
285 
297  void wrapIplImage(void *iplImage);
298 
299  //void wrapRawImage(void *buf, int imgWidth, int imgHeight);
300 
301 
306  virtual bool read(yarp::os::ConnectionReader& connection) override;
307 
312  virtual bool write(yarp::os::ConnectionWriter& connection) const override;
313 
314  void setQuantum(size_t imgQuantum);
315 
320  bool topIsLowIndex() const {
321  return topIsLow;
322  }
323 
332  void setTopIsLowIndex(bool flag) {
333  topIsLow = flag;
334  }
335 
336 
341  char **getRowArray() {
342  return data;
343  }
344 
345  virtual yarp::os::Type getReadType() const override {
346  return yarp::os::Type::byName("yarp/image");
347  }
348 
349 protected:
350 
351  void setPixelCode(int imgPixelCode);
352 
353  //pixelCode and pixelsSize should be linked together consistently.
354  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
355  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
356  void setPixelSize(size_t imgPixelSize);
357 
358 
359 private:
361  static const std::map<YarpVocabPixelTypesEnum, size_t> pixelCode2Size;
362  size_t imgWidth, imgHeight, imgPixelSize, imgRowSize, imgQuantum;
364  bool topIsLow;
365 
366  char **data;
368 
369  void synchronize();
370  void initialize();
371 
372  void copyPixels(const unsigned char *src, size_t id1,
373  unsigned char *dest, size_t id2, size_t w, size_t h,
374  size_t imageSize, size_t quantum1, size_t quantum2,
375  bool topIsLow1, bool topIsLow2);
376 };
377 
378 
384 public:
385 
386  void setPixelCode(int imgPixelCode) {
387  Image::setPixelCode(imgPixelCode);
388  }
389 
390 
391  void setPixelSize(size_t imgPixelSize) {
392  Image::setPixelSize(imgPixelSize);
393  //pixelCode and pixelsSize should be linked together consistently.
394  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
395  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
396  }
397 
398  void setQuantum(size_t imgQuantum) {
399  Image::setQuantum(imgQuantum);
400  }
401 
402 private:
403 };
404 
405 
406 
407 
408 #include <yarp/os/NetInt32.h>
409 
410 namespace yarp {
411  namespace sig {
412 
416  typedef unsigned char PixelMono;
417 
422 
427 
433  {
434  unsigned char r,g,b;
435 
436  PixelRgb() { r = g = b = 0; }
437  PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
438  { r = n_r; g = n_g; b = n_b; }
439  };
441 
447  {
448  unsigned char r,g,b,a;
449 
450  PixelRgba() { r = g = b = a = 0; }
451  PixelRgba(unsigned char n_r, unsigned char n_g,
452  unsigned char n_b, unsigned char n_a)
453  { r = n_r; g = n_g; b = n_b; a = n_a; }
454  };
456 
462  {
463  unsigned char b,g,r,a;
464 
465  PixelBgra() { r = g = b = a = 0; }
466  PixelBgra(unsigned char n_r, unsigned char n_g,
467  unsigned char n_b, unsigned char n_a)
468  { r = n_r; g = n_g; b = n_b; a = n_a; }
469  };
471 
477  {
478  unsigned char b,g,r;
479  PixelBgr() { b = g = r = 0; }
480  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
481  { r = n_r; g = n_g; b = n_b; }
482  };
484 
490  unsigned char h,s,v;
491  };
493 
497  typedef char PixelMonoSigned;
498 
504  char r,g,b;
505  };
507 
511  typedef float PixelFloat;
512 
518  float r,g,b;
519  PixelRgbFloat() { r = g = b = 0; }
520  PixelRgbFloat(float n_r, float n_g, float n_b)
521  { r = n_r; g = n_g; b = n_b; }
522  };
524 
531  PixelRgbInt() { r = g = b = 0; }
532  PixelRgbInt(int n_r, int n_g, int n_b) {
533  r = n_r; g = n_g; b = n_b;
534  }
535  };
537 
542  struct PixelHsvFloat {
543  float h,s,v;
544  };
546 
547  }
548 }
549 
550 
563 template <class T>
564 class yarp::sig::ImageOf : public Image
565 {
566 private:
568 public:
569  ImageOf() : Image(),
570  nullPixel()
571  {
572  setPixelCode(getPixelCode());
573  }
574 
575  virtual size_t getPixelSize() const override {
576  return sizeof(T);
577  }
578 
579  virtual int getPixelCode() const override;
580 
581  inline T& pixel(size_t x, size_t y) {
582  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
583  }
584 
585  inline T& pixel(size_t x, size_t y) const {
586  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
587  }
588 
589  inline const T& operator()(size_t x, size_t y) const {
590  return pixel(x,y);
591  }
592 
593  inline T& operator()(size_t x, size_t y) {
594  return pixel(x,y);
595  }
596 
597  inline T& safePixel(size_t x, size_t y) {
598  if (!isPixel(x,y)) { return nullPixel; }
599  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
600  }
601 
602  inline const T& safePixel(size_t x, size_t y) const {
603  if (!isPixel(x,y)) { return nullPixel; }
604  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
605  }
606 };
607 
608 namespace yarp {
609 namespace sig {
610 
611 template<>
613  return VOCAB_PIXEL_MONO;
614 }
615 
616 template<>
618  return VOCAB_PIXEL_MONO16;
619 }
620 
621 template<>
623  return VOCAB_PIXEL_RGB;
624 }
625 
626 template<>
628  return VOCAB_PIXEL_RGBA;
629 }
630 
631 template<>
633  return VOCAB_PIXEL_HSV;
634 }
635 
636 template<>
638  return VOCAB_PIXEL_BGR;
639 }
640 
641 template<>
643  return VOCAB_PIXEL_BGRA;
644 }
645 
646 template<>
649 }
650 
651 template<>
653  return VOCAB_PIXEL_RGB_SIGNED;
654 }
655 
656 template<>
658  return VOCAB_PIXEL_MONO_FLOAT;
659 }
660 
661 template<>
663  return VOCAB_PIXEL_RGB_FLOAT;
664 }
665 
666 template<>
668  return VOCAB_PIXEL_RGB_INT;
669 }
670 
671 template<>
673  return VOCAB_PIXEL_HSV_FLOAT;
674 }
675 
676 template<>
678  return VOCAB_PIXEL_INT;
679 }
680 
681 template<typename T>
682 inline int ImageOf<T>::getPixelCode() const {
683  return -((int) sizeof(T));
684 }
685 
686 } // namespace sig
687 } // namespace yarp
688 
689 #endif // YARP_SIG_IMAGE_H
Floating point RGB pixel type.
Definition: Image.h:517
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:430
unsigned char r
Definition: Image.h:463
Packed RGB pixel type.
Definition: Image.h:432
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:393
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:505
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition: Image.h:222
bool ret
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:201
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:489
Floating point HSV pixel type.
Definition: Image.h:542
Packed RGBA pixel type.
Definition: Image.h:446
float PixelFloat
Floating point pixel type.
Definition: Image.h:511
This is a base class for objects that can be both read from and be written to the YARP network...
Definition: Portable.h:27
Integer RGB pixel type.
Definition: Image.h:529
static Type byName(const char *name)
Definition: Type.cpp:179
virtual size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition: Image.h:575
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:164
int imgPixelCode
Definition: Image.h:363
T & pixel(size_t x, size_t y)
Definition: Image.h:581
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:520
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:171
bool topIsLowIndex() const
Definition: Image.h:320
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:200
yarp::os::NetInt32 r
Definition: Image.h:530
char ** data
Definition: Image.h:366
virtual int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:682
size_t width() const
Gets width of image in pixels.
Definition: Image.h:138
The main, catch-all namespace for YARP.
Definition: numeric.h:47
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:416
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:200
static const std::map< YarpVocabPixelTypesEnum, size_t > pixelCode2Size
Definition: Image.h:361
unsigned char r
Definition: Image.h:448
Signed, packed RGB pixel type.
Definition: Image.h:503
int PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition: Image.h:36
void setPixelCode(int imgPixelCode)
Definition: Image.h:386
Image class with user control of representation details.
Definition: Image.h:383
#define YARP_sig_API
Definition: api.h:19
unsigned char v
Definition: Image.h:490
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:32
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:212
bool initialize(int size, const int *amap)
Initialize the internal data and alloc memory.
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:451
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:421
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:188
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:476
size_t imgWidth
Definition: Image.h:362
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:497
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:32
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.h:332
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:43
void setQuantum(size_t imgQuantum)
Definition: Image.h:398
size_t height() const
Gets height of image in pixels.
Definition: Image.h:144
An interface for reading from a network connection.
size_t getPadding() const
Returns the number of padding bytes.
Definition: Image.h:177
unsigned char r
Definition: Image.h:478
yarp::rosmsg::sensor_msgs::Image Image
Definition: Image.h:24
Packed BGRA pixel type.
Definition: Image.h:461
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:532
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:426
void * implementation
Definition: Image.h:367
T & pixel(size_t x, size_t y) const
Definition: Image.h:585
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:480
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:247
void setPixelSize(size_t imgPixelSize)
Definition: Image.h:391
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:437
bool topIsLow
Definition: Image.h:364
virtual yarp::os::Type getReadType() const override
Definition: Image.h:345
const T & operator()(size_t x, size_t y) const
Definition: Image.h:589
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:499
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:466
T & safePixel(size_t x, size_t y)
Definition: Image.h:597
YarpVocabPixelTypesEnum
Definition: Image.h:45
unsigned char r
Definition: Image.h:434
void setPixelSize(size_t imgPixelSize)
Definition: Image.cpp:491
const T & safePixel(size_t x, size_t y) const
Definition: Image.h:602
T & operator()(size_t x, size_t y)
Definition: Image.h:593
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:341
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING
Suppress MSVC C4251 warning for the next line.
Definition: system.h:345