YARP  2.3.68+279-20170522.8+git3e63e13
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);
270 
275  virtual bool write(yarp::os::ConnectionWriter& connection);
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 
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  };
389 
390 
391 
392 
393 #include <yarp/os/NetInt32.h>
394 
395 namespace yarp {
396  namespace sig {
397 
401  typedef unsigned char PixelMono;
402 
406  typedef yarp::os::NetUint16 PixelMono16;
407 
411  typedef yarp::os::NetInt32 PixelInt;
412 
417  struct YARP_sig_API PixelRgb
418  {
419  unsigned char r,g,b;
420 
421  PixelRgb() { r = g = b = 0; }
422  PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
423  { r = n_r; g = n_g; b = n_b; }
424  };
426 
431  struct YARP_sig_API PixelRgba
432  {
433  unsigned char r,g,b,a;
434 
435  PixelRgba() { r = g = b = a = 0; }
436  PixelRgba(unsigned char n_r, unsigned char n_g,
437  unsigned char n_b, unsigned char n_a)
438  { r = n_r; g = n_g; b = n_b; a = n_a; }
439  };
441 
446  struct YARP_sig_API PixelBgra
447  {
448  unsigned char b,g,r,a;
449 
450  PixelBgra() { r = g = b = a = 0; }
451  PixelBgra(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 
461  struct YARP_sig_API PixelBgr
462  {
463  unsigned char b,g,r;
464  PixelBgr() { b = g = r = 0; }
465  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
466  { r = n_r; g = n_g; b = n_b; }
467  };
469 
474  struct YARP_sig_API PixelHsv {
475  unsigned char h,s,v;
476  };
478 
482  typedef char PixelMonoSigned;
483 
488  struct YARP_sig_API PixelRgbSigned {
489  char r,g,b;
490  };
492 
496  typedef float PixelFloat;
497 
502  struct YARP_sig_API PixelRgbFloat {
503  float r,g,b;
504  PixelRgbFloat() { r = g = b = 0; }
505  PixelRgbFloat(float n_r, float n_g, float n_b)
506  { r = n_r; g = n_g; b = n_b; }
507  };
509 
514  struct YARP_sig_API PixelRgbInt {
515  yarp::os::NetInt32 r,g,b;
516  PixelRgbInt() { r = g = b = 0; }
517  PixelRgbInt(int n_r, int n_g, int n_b) {
518  r = n_r; g = n_g; b = n_b;
519  }
520  };
522 
527  struct PixelHsvFloat {
528  float h,s,v;
529  };
531 
532  }
533 }
534 
535 
548 template <class T>
549 class yarp::sig::ImageOf : public Image
550 {
551 private:
553 public:
554 
555  virtual int getPixelSize() const {
556  return sizeof(T);
557  }
558 
559  virtual int getPixelCode() const {
560  return -((int) sizeof(T));
561  }
562 
563  inline T& pixel(int x, int y) {
564  return *((T *)(getPixelAddress(x,y)));
565  }
566 
567  inline T& pixel(int x, int y) const {
568  return *((T *)(getPixelAddress(x,y)));
569  }
570 
571  inline const T& operator()(int x, int y) const {
572  return pixel(x,y);
573  }
574 
575  inline T& operator()(int x, int y) {
576  return pixel(x,y);
577  }
578 
579  inline T& safePixel(int x, int y) {
580  if (!isPixel(x,y)) { return nullPixel; }
581  return *((T *)(getPixelAddress(x,y)));
582  }
583 
584  inline const T& safePixel(int x, int y) const {
585  if (!isPixel(x,y)) { return nullPixel; }
586  return *((T *)(getPixelAddress(x,y)));
587  }
588 };
589 
590 namespace yarp {
591  namespace sig {
592 
593 #define YARPIMAGE_ASSOCIATE_TAG(tag,T) \
594 template<> \
595 class YARP_sig_API ImageOf<T> : public Image \
596 { \
597 private: \
598  T nullPixel; \
599 public: \
600 \
601  virtual int getPixelSize() const { \
602  return sizeof(T); \
603  } \
604 \
605  virtual int getPixelCode() const { \
606  return tag; \
607  } \
608 \
609  inline T& pixel(int x, int y) { \
610  return *((T *)(getPixelAddress(x,y))); \
611  } \
612 \
613  inline const T& pixel(int x, int y) const { \
614  return *((T *)(getPixelAddress(x,y))); \
615  } \
616 \
617  inline T& operator()(int x, int y) { \
618  return pixel(x,y); \
619  } \
620 \
621  inline const T& operator()(int x, int y) const { \
622  return pixel(x,y); \
623  } \
624 \
625  inline T& safePixel(int x, int y) { \
626  if (!isPixel(x,y)) { return nullPixel; } \
627  return *((T *)(getPixelAddress(x,y))); \
628  } \
629 \
630  inline const T& safePixel(int x, int y) const { \
631  if (!isPixel(x,y)) { return nullPixel; } \
632  return *((T *)(getPixelAddress(x,y))); \
633  } \
634 };
635 
636 
651 
652  }
653 }
654 
655 #undef YARPIMAGE_ASSOCIATE_TAG
656 
657 
658 #ifndef YARP_IMAGE_HEADER_CONTROL
659 #define YARP_IMAGE_HEADER_CONTROL
660 #endif
661 
662 #endif // YARP_SIG_IMAGE_H
Floating point RGB pixel type.
Definition: Image.h:502
T & safePixel(int x, int y)
Definition: Image.h:579
unsigned char r
Definition: Image.h:448
dest b
Definition: ImageCopy.cpp:91
virtual yarp::os::Type getReadType()
Definition: Image.h:308
Packed RGB pixel type.
Definition: Image.h:417
T & operator()(int x, int y)
Definition: Image.h:575
yarp::sig::ImageOf< yarp::sig::PixelBgr > Image
Definition: VfwGrabber.cpp:74
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:206
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:474
void setPixelSize(int imgPixelSize)
Definition: Image.h:348
Floating point HSV pixel type.
Definition: Image.h:527
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:431
virtual YARP_SSIZE_T read(const Bytes &b)
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:514
const T & safePixel(int x, int y) const
Definition: Image.h:584
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:505
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
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:204
yarp::os::NetInt32 r
Definition: Image.h:515
char ** data
Definition: Image.h:324
* dest
Definition: ImageCopy.cpp:72
YarpVocabPixelTypesEnum
Definition: Image.h:363
The main, catch-all namespace for YARP.
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.h:559
YARP_END_PACK typedef float PixelFloat
Floating point pixel type.
Definition: Image.h:496
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:401
void setPixelSize(int imgPixelSize)
Definition: Image.cpp:697
unsigned char r
Definition: Image.h:433
Signed, packed RGB pixel type.
Definition: Image.h:488
void setPixelCode(int imgPixelCode)
Definition: Image.h:344
Image class with user control of representation details.
Definition: Image.h:341
unsigned char v
Definition: Image.h:475
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:555
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:436
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:406
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:461
T & pixel(int x, int y) const
Definition: Image.h:567
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
virtual void write(const Bytes &b)
#define YARPIMAGE_ASSOCIATE_TAG(tag, T)
Definition: Image.h:593
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:463
YARP_END_PACK typedef char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:482
Packed BGRA pixel type.
Definition: Image.h:446
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:517
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:411
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:465
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:563
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:422
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:451
int height() const
Gets height of image in pixels.
Definition: Image.h:107
unsigned char r
Definition: Image.h:419
const T & operator()(int x, int y) const
Definition: Image.h:571
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