YARP  2.3.70.1
Yet Another Robot Platform
Image.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006, 2008 RobotCub Consortium
3  * Authors: Paul Fitzpatrick, Giorgio Metta
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
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/Portable.h>
12 #include <yarp/os/Vocab.h>
13 #include <yarp/os/NetUint16.h>
14 #include <yarp/sig/api.h>
15 
16 namespace yarp {
20  namespace sig {
21  class Image;
22  class FlexImage;
23  template <class T> class ImageOf;
24 
31  inline int PAD_BYTES (int len, int pad) {
32  const int rem = len % pad;
33  return (rem != 0) ? (pad - rem) : rem;
34  }
35  }
36 }
37 
38 
48 class YARP_sig_API yarp::sig::Image : public yarp::os::Portable {
49 
50 public:
51 
56  Image();
57 
63  Image(const Image& alt);
64 
68  virtual ~Image();
69 
75  const Image& operator=(const Image& alt);
76 
77 
83  bool copy(const Image& alt);
84 
85 
94  bool copy(const Image& alt, int w, int h);
95 
96 
101  inline int width() const { return imgWidth; }
102 
107  inline int height() const { return imgHeight; }
108 
113  virtual int getPixelSize() const;
114 
121  virtual int getPixelCode() const;
122 
127  inline int getRowSize() const { return imgRowSize; }
128 
129 
134  inline int getQuantum() const { return imgQuantum; }
135 
140  inline int getPadding() const
141  {
142  const int ret=imgRowSize-imgWidth*imgPixelSize;
143  return ret;
144  }
145 
151  inline unsigned char *getRow(int r)
152  {
153  // should we check limits?
154  return (unsigned char *)(data[r]);
155  }
156 
163  inline const unsigned char *getRow(int r) const
164  {
165  // should we check limits?
166  return (const unsigned char *)(data[r]);
167  }
168 
175  inline unsigned char *getPixelAddress(int x, int y) const {
176  return (unsigned char *)(data[y] + x*imgPixelSize);
177  }
178 
185  inline bool isPixel(int x, int y) const {
186  return (x>=0 && y>=0 && x<imgWidth && y<imgHeight);
187  }
188 
192  void zero();
193 
203  void resize(int imgWidth, int imgHeight);
204 
210  void resize(const Image& alt) {
211  resize(alt.width(),alt.height());
212  }
213 
219  void setExternal(const void *data, int imgWidth, int imgHeight);
220 
225  unsigned char *getRawImage() const;
226 
231  int getRawImageSize() const;
232 
239  void *getIplImage();
240 
247  const void *getIplImage() const;
248 
260  void wrapIplImage(void *iplImage);
261 
262  //void wrapRawImage(void *buf, int imgWidth, int imgHeight);
263 
264 
269  virtual bool read(yarp::os::ConnectionReader& connection) YARP_OVERRIDE;
270 
275  virtual bool write(yarp::os::ConnectionWriter& connection) YARP_OVERRIDE;
276 
277  void setQuantum(int imgQuantum);
278 
283  bool topIsLowIndex() const {
284  return topIsLow;
285  }
286 
295  void setTopIsLowIndex(bool flag) {
296  topIsLow = flag;
297  }
298 
299 
304  char **getRowArray() {
305  return data;
306  }
307 
308  virtual yarp::os::Type getReadType() YARP_OVERRIDE {
309  return yarp::os::Type::byName("yarp/image");
310  }
311 
312 protected:
313 
314  void setPixelCode(int imgPixelCode);
315 
316  void setPixelSize(int imgPixelSize);
317 
318 
319 private:
320 
321  int imgWidth, imgHeight, imgPixelSize, imgRowSize, imgPixelCode, imgQuantum;
322  bool topIsLow;
323 
324  char **data;
326 
327  void synchronize();
328  void initialize();
329 
330  void copyPixels(const unsigned char *src, int id1,
331  unsigned char *dest, int id2, int w, int h,
332  int imageSize, int quantum1, int quantum2,
333  bool topIsLow1, bool topIsLow2);
334 };
335 
336 
341 class YARP_sig_API yarp::sig::FlexImage : public yarp::sig::Image {
342 public:
343 
344  void setPixelCode(int imgPixelCode) {
345  Image::setPixelCode(imgPixelCode);
346  }
347 
348  void setPixelSize(int imgPixelSize) {
349  Image::setPixelSize(imgPixelSize);
350  }
351 
352  void setQuantum(int imgQuantum) {
353  Image::setQuantum(imgQuantum);
354  }
355 
356 private:
357 };
358 
359 
360 
361 // the image types partially reflect the IPL image types.
362 // There must be a pixel type for every ImageType entry.
364  {
366  VOCAB_PIXEL_MONO = VOCAB4('m','o','n','o'),
367  VOCAB_PIXEL_MONO16 = VOCAB4('m','o','1','6'),
368  VOCAB_PIXEL_RGB = VOCAB3('r','g','b'),
369  VOCAB_PIXEL_RGBA = VOCAB4('r','g','b','a'),
370  VOCAB_PIXEL_BGRA = VOCAB4(98/*'b'*/,'g','r','a'), /* SWIG BUG */
371  VOCAB_PIXEL_INT = VOCAB3('i','n','t'),
372  VOCAB_PIXEL_HSV = VOCAB3('h','s','v'),
373  VOCAB_PIXEL_BGR = VOCAB3(98/*'b'*/,'g','r'), /* SWIG BUG */
374  VOCAB_PIXEL_MONO_SIGNED = VOCAB4('s','i','g','n'),
375  VOCAB_PIXEL_RGB_SIGNED = VOCAB4('r','g','b','-'),
376  VOCAB_PIXEL_RGB_INT = VOCAB4('r','g','b','i'),
378  VOCAB_PIXEL_RGB_FLOAT = VOCAB4('r','g','b','.'),
379  VOCAB_PIXEL_HSV_FLOAT = VOCAB4('h','s','v','.'),
380  VOCAB_PIXEL_ENCODING_BAYER_GRBG8 = VOCAB4('g', 'r', 'b', 'g'), //grbg8
381  VOCAB_PIXEL_ENCODING_BAYER_GRBG16 = VOCAB4('g', 'r', '1', '6'), //grbg16
382  VOCAB_PIXEL_ENCODING_BAYER_BGGR8 = VOCAB4(98/*'b'*/, 'g', 'g', 'r'), //bggr8
383  VOCAB_PIXEL_ENCODING_BAYER_BGGR16 = VOCAB4(98/*'b'*/, 'g', '1', '6'), //bggr16
384  VOCAB_PIXEL_ENCODING_BAYER_GBRG8 = VOCAB4('g', 'b', 'r', 'g'), //gbrg8
385  VOCAB_PIXEL_ENCODING_BAYER_GBRG16 = VOCAB4('g', 'b', '1', '6'), //gbrg16
386  VOCAB_PIXEL_ENCODING_BAYER_RGGB8 = -VOCAB4('r', 'g', 'g', 'b'), //rggb8
387  VOCAB_PIXEL_ENCODING_BAYER_RGGB16 = VOCAB4('r', 'g', '1', '6'), //rggb16
388  VOCAB_PIXEL_YUV_420 = VOCAB4('y','u','v','a'),
389  VOCAB_PIXEL_YUV_444 = VOCAB4('y','u','v','b'),
390  VOCAB_PIXEL_YUV_422 = VOCAB4('y','u','v','c'),
391  VOCAB_PIXEL_YUV_411 = VOCAB4('y','u','v','d')
392  };
393 
394 
395 
396 
397 #include <yarp/os/NetInt32.h>
398 
399 namespace yarp {
400  namespace sig {
401 
405  typedef unsigned char PixelMono;
406 
410  typedef yarp::os::NetUint16 PixelMono16;
411 
415  typedef yarp::os::NetInt32 PixelInt;
416 
420  YARP_BEGIN_PACK
421  struct YARP_sig_API PixelRgb
422  {
423  unsigned char r,g,b;
424 
425  PixelRgb() { r = g = b = 0; }
426  PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
427  { r = n_r; g = n_g; b = n_b; }
428  };
429  YARP_END_PACK
430 
434  YARP_BEGIN_PACK
435  struct YARP_sig_API PixelRgba
436  {
437  unsigned char r,g,b,a;
438 
439  PixelRgba() { r = g = b = a = 0; }
440  PixelRgba(unsigned char n_r, unsigned char n_g,
441  unsigned char n_b, unsigned char n_a)
442  { r = n_r; g = n_g; b = n_b; a = n_a; }
443  };
444  YARP_END_PACK
445 
449  YARP_BEGIN_PACK
450  struct YARP_sig_API PixelBgra
451  {
452  unsigned char b,g,r,a;
453 
454  PixelBgra() { r = g = b = a = 0; }
455  PixelBgra(unsigned char n_r, unsigned char n_g,
456  unsigned char n_b, unsigned char n_a)
457  { r = n_r; g = n_g; b = n_b; a = n_a; }
458  };
459  YARP_END_PACK
460 
464  YARP_BEGIN_PACK
465  struct YARP_sig_API PixelBgr
466  {
467  unsigned char b,g,r;
468  PixelBgr() { b = g = r = 0; }
469  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
470  { r = n_r; g = n_g; b = n_b; }
471  };
472  YARP_END_PACK
473 
477  YARP_BEGIN_PACK
478  struct YARP_sig_API PixelHsv {
479  unsigned char h,s,v;
480  };
481  YARP_END_PACK
482 
486  typedef char PixelMonoSigned;
487 
491  YARP_BEGIN_PACK
492  struct YARP_sig_API PixelRgbSigned {
493  char r,g,b;
494  };
495  YARP_END_PACK
496 
500  typedef float PixelFloat;
501 
505  YARP_BEGIN_PACK
506  struct YARP_sig_API PixelRgbFloat {
507  float r,g,b;
508  PixelRgbFloat() { r = g = b = 0; }
509  PixelRgbFloat(float n_r, float n_g, float n_b)
510  { r = n_r; g = n_g; b = n_b; }
511  };
512  YARP_END_PACK
513 
517  YARP_BEGIN_PACK
518  struct YARP_sig_API PixelRgbInt {
519  yarp::os::NetInt32 r,g,b;
520  PixelRgbInt() { r = g = b = 0; }
521  PixelRgbInt(int n_r, int n_g, int n_b) {
522  r = n_r; g = n_g; b = n_b;
523  }
524  };
525  YARP_END_PACK
526 
530  YARP_BEGIN_PACK
531  struct PixelHsvFloat {
532  float h,s,v;
533  };
534  YARP_END_PACK
535 
536  }
537 }
538 
539 
552 template <class T>
553 class yarp::sig::ImageOf : public Image
554 {
555 private:
557 public:
558 
559  virtual int getPixelSize() const {
560  return sizeof(T);
561  }
562 
563  virtual int getPixelCode() const {
564  return -((int) sizeof(T));
565  }
566 
567  inline T& pixel(int x, int y) {
568  return *((T *)(getPixelAddress(x,y)));
569  }
570 
571  inline T& pixel(int x, int y) const {
572  return *((T *)(getPixelAddress(x,y)));
573  }
574 
575  inline const T& operator()(int x, int y) const {
576  return pixel(x,y);
577  }
578 
579  inline T& operator()(int x, int y) {
580  return pixel(x,y);
581  }
582 
583  inline T& safePixel(int x, int y) {
584  if (!isPixel(x,y)) { return nullPixel; }
585  return *((T *)(getPixelAddress(x,y)));
586  }
587 
588  inline const T& safePixel(int x, int y) const {
589  if (!isPixel(x,y)) { return nullPixel; }
590  return *((T *)(getPixelAddress(x,y)));
591  }
592 };
593 
594 namespace yarp {
595  namespace sig {
596 
597 #define YARPIMAGE_ASSOCIATE_TAG(tag,T) \
598 template<> \
599 class YARP_sig_API ImageOf<T> : public Image \
600 { \
601 private: \
602  T nullPixel; \
603 public: \
604 \
605  virtual int getPixelSize() const YARP_OVERRIDE { \
606  return sizeof(T); \
607  } \
608 \
609  virtual int getPixelCode() const YARP_OVERRIDE { \
610  return tag; \
611  } \
612 \
613  inline T& pixel(int x, int y) { \
614  return *((T *)(getPixelAddress(x,y))); \
615  } \
616 \
617  inline const T& pixel(int x, int y) const { \
618  return *((T *)(getPixelAddress(x,y))); \
619  } \
620 \
621  inline T& operator()(int x, int y) { \
622  return pixel(x,y); \
623  } \
624 \
625  inline const T& operator()(int x, int y) const { \
626  return pixel(x,y); \
627  } \
628 \
629  inline T& safePixel(int x, int y) { \
630  if (!isPixel(x,y)) { return nullPixel; } \
631  return *((T *)(getPixelAddress(x,y))); \
632  } \
633 \
634  inline const T& safePixel(int x, int y) const { \
635  if (!isPixel(x,y)) { return nullPixel; } \
636  return *((T *)(getPixelAddress(x,y))); \
637  } \
638 };
639 
640 
655 
656  }
657 }
658 
659 #undef YARPIMAGE_ASSOCIATE_TAG
660 
661 
662 #ifndef YARP_IMAGE_HEADER_CONTROL
663 #define YARP_IMAGE_HEADER_CONTROL
664 #endif
665 
666 #endif // YARP_SIG_IMAGE_H
Floating point RGB pixel type.
Definition: Image.h:506
#define YARPIMAGE_ASSOCIATE_TAG(tag, T)
Definition: Image.h:597
T & safePixel(int x, int y)
Definition: Image.h:583
virtual YARP_SSIZE_T read(const Bytes &b) override
unsigned char r
Definition: Image.h:452
dest b
Definition: ImageCopy.cpp:91
Packed RGB pixel type.
Definition: Image.h:421
T & operator()(int x, int y)
Definition: Image.h:579
yarp::sig::ImageOf< yarp::sig::PixelBgr > Image
Definition: VfwGrabber.cpp:74
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:478
void setPixelSize(int imgPixelSize)
Definition: Image.h:348
Floating point HSV pixel type.
Definition: Image.h:531
const unsigned char * getRow(int r) const
Get the address of a the first byte of a row in memory, const versions.
Definition: Image.h:163
Packed RGBA pixel type.
Definition: Image.h:435
static Type byName(const char *name)
Definition: Type.h:30
This is a base class for objects that can be both read from and be written to the YARP network...
Definition: Portable.h:26
Integer RGB pixel type.
Definition: Image.h:518
const T & safePixel(int x, int y) const
Definition: Image.h:588
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:509
dest a
Definition: ImageCopy.cpp:53
bool topIsLowIndex() const
Definition: Image.h:283
int width() const
Gets width of image in pixels.
Definition: Image.h:101
yarp::os::NetInt32 r
Definition: Image.h:519
char ** data
Definition: Image.h:324
* dest
Definition: ImageCopy.cpp:72
The main, catch-all namespace for YARP.
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.h:563
YARP_END_PACK typedef float PixelFloat
Floating point pixel type.
Definition: Image.h:500
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:405
void setPixelSize(int imgPixelSize)
Definition: Image.cpp:697
unsigned char r
Definition: Image.h:437
Signed, packed RGB pixel type.
Definition: Image.h:492
void setPixelCode(int imgPixelCode)
Definition: Image.h:344
Image class with user control of representation details.
Definition: Image.h:341
virtual yarp::os::Type getReadType() YARP_OVERRIDE
Definition: Image.h:308
unsigned char v
Definition: Image.h:479
Base class for storing images.
Definition: Image.h:48
An interface for writing to a network connection.
virtual int getPixelSize() const
Gets pixel size in memory in bytes.
Definition: Image.h:559
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:440
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:410
unsigned char * getPixelAddress(int x, int y) const
Get address of a pixel in memory.
Definition: Image.h:175
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:465
T & pixel(int x, int y) const
Definition: Image.h:571
dest g
Definition: ImageCopy.cpp:91
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.h:295
#define VOCAB3(a, b, c)
Definition: Vocab.h:24
int getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:127
unsigned char * getRow(int r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:151
int getPadding() const
Returns the number of padding bytes.
Definition: Image.h:140
An interface for reading from a network connection.
int getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:134
unsigned char r
Definition: Image.h:467
YARP_END_PACK typedef char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:486
Packed BGRA pixel type.
Definition: Image.h:450
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:521
bool isPixel(int x, int y) const
Check whether a coordinate lies within the image.
Definition: Image.h:185
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:415
void * implementation
Definition: Image.h:325
void setQuantum(int imgQuantum)
Definition: Image.cpp:702
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:469
Typed image class.
Definition: Image.h:23
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:210
dest h
Definition: ImageCopy.cpp:63
return ret
Definition: IplImage.cpp:876
SPECIAL_COPY_BEGIN dest r
Definition: ImageCopy.cpp:50
T & pixel(int x, int y)
Definition: Image.h:567
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:426
bool topIsLow
Definition: Image.h:322
int PAD_BYTES(int len, int pad)
computes the padding of YARP images.
Definition: Image.h:31
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:692
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:455
YarpVocabPixelTypesEnum
Definition: Image.h:363
int height() const
Gets height of image in pixels.
Definition: Image.h:107
unsigned char r
Definition: Image.h:423
const T & operator()(int x, int y) const
Definition: Image.h:575
void setQuantum(int imgQuantum)
Definition: Image.h:352
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:304
#define VOCAB4(a, b, c, d)
Definition: Vocab.h:23
virtual void write(const Bytes &b) override