YARP  2.3.72
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 #include <map>
16 
17 namespace yarp {
21  namespace sig {
22  class Image;
23  class FlexImage;
24  template <class T> class ImageOf;
25 
32  inline int PAD_BYTES (int len, int pad) {
33  const int rem = len % pad;
34  return (rem != 0) ? (pad - rem) : rem;
35  }
36  }
37 }
38 
39 // the image types partially reflect the IPL image types.
40 // There must be a pixel type for every ImageType entry.
42 {
44  VOCAB_PIXEL_MONO = VOCAB4('m','o','n','o'),
45  VOCAB_PIXEL_MONO16 = VOCAB4('m','o','1','6'),
46  VOCAB_PIXEL_RGB = VOCAB3('r','g','b'),
47  VOCAB_PIXEL_RGBA = VOCAB4('r','g','b','a'),
48  VOCAB_PIXEL_BGRA = VOCAB4(98/*'b'*/,'g','r','a'), /* SWIG BUG */
49  VOCAB_PIXEL_INT = VOCAB3('i','n','t'),
50  VOCAB_PIXEL_HSV = VOCAB3('h','s','v'),
51  VOCAB_PIXEL_BGR = VOCAB3(98/*'b'*/,'g','r'), /* SWIG BUG */
52  VOCAB_PIXEL_MONO_SIGNED = VOCAB4('s','i','g','n'),
53  VOCAB_PIXEL_RGB_SIGNED = VOCAB4('r','g','b','-'),
54  VOCAB_PIXEL_RGB_INT = VOCAB4('r','g','b','i'),
56  VOCAB_PIXEL_RGB_FLOAT = VOCAB4('r','g','b','.'),
57  VOCAB_PIXEL_HSV_FLOAT = VOCAB4('h','s','v','.'),
58  VOCAB_PIXEL_ENCODING_BAYER_GRBG8 = VOCAB4('g', 'r', 'b', 'g'), //grbg8
59  VOCAB_PIXEL_ENCODING_BAYER_GRBG16 = VOCAB4('g', 'r', '1', '6'), //grbg16
60  VOCAB_PIXEL_ENCODING_BAYER_BGGR8 = VOCAB4(98/*'b'*/, 'g', 'g', 'r'), //bggr8
61  VOCAB_PIXEL_ENCODING_BAYER_BGGR16 = VOCAB4(98/*'b'*/, 'g', '1', '6'), //bggr16
62  VOCAB_PIXEL_ENCODING_BAYER_GBRG8 = VOCAB4('g', 'b', 'r', 'g'), //gbrg8
63  VOCAB_PIXEL_ENCODING_BAYER_GBRG16 = VOCAB4('g', 'b', '1', '6'), //gbrg16
64  VOCAB_PIXEL_ENCODING_BAYER_RGGB8 = -VOCAB4('r', 'g', 'g', 'b'), //rggb8
65  VOCAB_PIXEL_ENCODING_BAYER_RGGB16 = VOCAB4('r', 'g', '1', '6'), //rggb16
66  VOCAB_PIXEL_YUV_420 = VOCAB4('y','u','v','a'),
67  VOCAB_PIXEL_YUV_444 = VOCAB4('y','u','v','b'),
68  VOCAB_PIXEL_YUV_422 = VOCAB4('y','u','v','c'),
69  VOCAB_PIXEL_YUV_411 = VOCAB4('y','u','v','d')
70 };
71 
81 class YARP_sig_API yarp::sig::Image : public yarp::os::Portable {
82 
83 public:
84 
89  Image();
90 
96  Image(const Image& alt);
97 
101  virtual ~Image();
102 
108  const Image& operator=(const Image& alt);
109 
110 
116  bool copy(const Image& alt);
117 
118 
127  bool copy(const Image& alt, int w, int h);
128 
129 
134  inline int width() const { return imgWidth; }
135 
140  inline int height() const { return imgHeight; }
141 
146  virtual int getPixelSize() const;
147 
154  virtual int getPixelCode() const;
155 
160  inline int getRowSize() const { return imgRowSize; }
161 
162 
167  inline int getQuantum() const { return imgQuantum; }
168 
173  inline int getPadding() const
174  {
175  const int ret=imgRowSize-imgWidth*imgPixelSize;
176  return ret;
177  }
178 
184  inline unsigned char *getRow(int r)
185  {
186  // should we check limits?
187  return (unsigned char *)(data[r]);
188  }
189 
196  inline const unsigned char *getRow(int r) const
197  {
198  // should we check limits?
199  return (const unsigned char *)(data[r]);
200  }
201 
208  inline unsigned char *getPixelAddress(int x, int y) const {
209  return (unsigned char *)(data[y] + x*imgPixelSize);
210  }
211 
218  inline bool isPixel(int x, int y) const {
219  return (x>=0 && y>=0 && x<imgWidth && y<imgHeight);
220  }
221 
225  void zero();
226 
236  void resize(int imgWidth, int imgHeight);
237 
243  void resize(const Image& alt) {
244  resize(alt.width(),alt.height());
245  }
246 
252  void setExternal(const void *data, int imgWidth, int imgHeight);
253 
258  unsigned char *getRawImage() const;
259 
264  int getRawImageSize() const;
265 
272  void *getIplImage();
273 
280  const void *getIplImage() const;
281 
293  void wrapIplImage(void *iplImage);
294 
295  //void wrapRawImage(void *buf, int imgWidth, int imgHeight);
296 
297 
302  virtual bool read(yarp::os::ConnectionReader& connection) override;
303 
308  virtual bool write(yarp::os::ConnectionWriter& connection) override;
309 
310  void setQuantum(int imgQuantum);
311 
316  bool topIsLowIndex() const {
317  return topIsLow;
318  }
319 
328  void setTopIsLowIndex(bool flag) {
329  topIsLow = flag;
330  }
331 
332 
337  char **getRowArray() {
338  return data;
339  }
340 
341  virtual yarp::os::Type getReadType() override {
342  return yarp::os::Type::byName("yarp/image");
343  }
344 
345 protected:
346 
347  void setPixelCode(int imgPixelCode);
348 
349  //pixelCode and pixelsSize should be linked together consistently.
350  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
351  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
352  void setPixelSize(int imgPixelSize);
353 
354 
355 private:
356  static const std::map<YarpVocabPixelTypesEnum, unsigned int> pixelCode2Size;
357  int imgWidth, imgHeight, imgPixelSize, imgRowSize, imgPixelCode, imgQuantum;
358  bool topIsLow;
359 
360  char **data;
362 
363  void synchronize();
364  void initialize();
365 
366  void copyPixels(const unsigned char *src, int id1,
367  unsigned char *dest, int id2, int w, int h,
368  int imageSize, int quantum1, int quantum2,
369  bool topIsLow1, bool topIsLow2);
370 };
371 
372 
377 class YARP_sig_API yarp::sig::FlexImage : public yarp::sig::Image {
378 public:
379 
380  void setPixelCode(int imgPixelCode) {
381  Image::setPixelCode(imgPixelCode);
382  }
383 
384 
385  void setPixelSize(int imgPixelSize) {
386  Image::setPixelSize(imgPixelSize);
387  //pixelCode and pixelsSize should be linked together consistently.
388  //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
389  //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
390  }
391 
392  void setQuantum(int imgQuantum) {
393  Image::setQuantum(imgQuantum);
394  }
395 
396 private:
397 };
398 
399 
400 
401 
402 #include <yarp/os/NetInt32.h>
403 
404 namespace yarp {
405  namespace sig {
406 
410  typedef unsigned char PixelMono;
411 
415  typedef yarp::os::NetUint16 PixelMono16;
416 
420  typedef yarp::os::NetInt32 PixelInt;
421 
425  YARP_BEGIN_PACK
426  struct YARP_sig_API PixelRgb
427  {
428  unsigned char r,g,b;
429 
430  PixelRgb() { r = g = b = 0; }
431  PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
432  { r = n_r; g = n_g; b = n_b; }
433  };
434  YARP_END_PACK
435 
439  YARP_BEGIN_PACK
440  struct YARP_sig_API PixelRgba
441  {
442  unsigned char r,g,b,a;
443 
444  PixelRgba() { r = g = b = a = 0; }
445  PixelRgba(unsigned char n_r, unsigned char n_g,
446  unsigned char n_b, unsigned char n_a)
447  { r = n_r; g = n_g; b = n_b; a = n_a; }
448  };
449  YARP_END_PACK
450 
454  YARP_BEGIN_PACK
455  struct YARP_sig_API PixelBgra
456  {
457  unsigned char b,g,r,a;
458 
459  PixelBgra() { r = g = b = a = 0; }
460  PixelBgra(unsigned char n_r, unsigned char n_g,
461  unsigned char n_b, unsigned char n_a)
462  { r = n_r; g = n_g; b = n_b; a = n_a; }
463  };
464  YARP_END_PACK
465 
469  YARP_BEGIN_PACK
470  struct YARP_sig_API PixelBgr
471  {
472  unsigned char b,g,r;
473  PixelBgr() { b = g = r = 0; }
474  PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
475  { r = n_r; g = n_g; b = n_b; }
476  };
477  YARP_END_PACK
478 
482  YARP_BEGIN_PACK
483  struct YARP_sig_API PixelHsv {
484  unsigned char h,s,v;
485  };
486  YARP_END_PACK
487 
491  typedef char PixelMonoSigned;
492 
496  YARP_BEGIN_PACK
497  struct YARP_sig_API PixelRgbSigned {
498  char r,g,b;
499  };
500  YARP_END_PACK
501 
505  typedef float PixelFloat;
506 
510  YARP_BEGIN_PACK
511  struct YARP_sig_API PixelRgbFloat {
512  float r,g,b;
513  PixelRgbFloat() { r = g = b = 0; }
514  PixelRgbFloat(float n_r, float n_g, float n_b)
515  { r = n_r; g = n_g; b = n_b; }
516  };
517  YARP_END_PACK
518 
522  YARP_BEGIN_PACK
523  struct YARP_sig_API PixelRgbInt {
524  yarp::os::NetInt32 r,g,b;
525  PixelRgbInt() { r = g = b = 0; }
526  PixelRgbInt(int n_r, int n_g, int n_b) {
527  r = n_r; g = n_g; b = n_b;
528  }
529  };
530  YARP_END_PACK
531 
535  YARP_BEGIN_PACK
536  struct PixelHsvFloat {
537  float h,s,v;
538  };
539  YARP_END_PACK
540 
541  }
542 }
543 
544 
557 template <class T>
558 class yarp::sig::ImageOf : public Image
559 {
560 private:
562 public:
563  ImageOf() : Image() {
564  setPixelCode(getPixelCode());
565  }
566 
567  virtual int getPixelSize() const override {
568  return sizeof(T);
569  }
570 
571  virtual int getPixelCode() const override;
572 
573  inline T& pixel(int x, int y) {
574  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
575  }
576 
577  inline T& pixel(int x, int y) const {
578  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
579  }
580 
581  inline const T& operator()(int x, int y) const {
582  return pixel(x,y);
583  }
584 
585  inline T& operator()(int x, int y) {
586  return pixel(x,y);
587  }
588 
589  inline T& safePixel(int x, int y) {
590  if (!isPixel(x,y)) { return nullPixel; }
591  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
592  }
593 
594  inline const T& safePixel(int x, int y) const {
595  if (!isPixel(x,y)) { return nullPixel; }
596  return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
597  }
598 };
599 
600 namespace yarp {
601 namespace sig {
602 
603 template<>
605  return VOCAB_PIXEL_MONO;
606 }
607 
608 template<>
610  return VOCAB_PIXEL_MONO16;
611 }
612 
613 template<>
615  return VOCAB_PIXEL_RGB;
616 }
617 
618 template<>
620  return VOCAB_PIXEL_RGBA;
621 }
622 
623 template<>
625  return VOCAB_PIXEL_HSV;
626 }
627 
628 template<>
630  return VOCAB_PIXEL_BGR;
631 }
632 
633 template<>
635  return VOCAB_PIXEL_BGRA;
636 }
637 
638 template<>
641 }
642 
643 template<>
645  return VOCAB_PIXEL_RGB_SIGNED;
646 }
647 
648 template<>
650  return VOCAB_PIXEL_MONO_FLOAT;
651 }
652 
653 template<>
655  return VOCAB_PIXEL_RGB_FLOAT;
656 }
657 
658 template<>
660  return VOCAB_PIXEL_RGB_INT;
661 }
662 
663 template<>
665  return VOCAB_PIXEL_HSV_FLOAT;
666 }
667 
668 template<>
670  return VOCAB_PIXEL_INT;
671 }
672 
673 template<typename T>
674 inline int ImageOf<T>::getPixelCode() const {
675  return -((int) sizeof(T));
676 }
677 
678 } // namespace sig
679 } // namespace yarp
680 
681 #endif // YARP_SIG_IMAGE_H
Floating point RGB pixel type.
Definition: Image.h:511
T & safePixel(int x, int y)
Definition: Image.h:589
virtual YARP_SSIZE_T read(const Bytes &b) override
unsigned char r
Definition: Image.h:457
Packed RGB pixel type.
Definition: Image.h:426
T & operator()(int x, int y)
Definition: Image.h:585
yarp::sig::ImageOf< yarp::sig::PixelBgr > Image
Definition: VfwGrabber.cpp:74
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:483
void setPixelSize(int imgPixelSize)
Definition: Image.h:385
Floating point HSV pixel type.
Definition: Image.h:536
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:196
Packed RGBA pixel type.
Definition: Image.h:440
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:523
const T & safePixel(int x, int y) const
Definition: Image.h:594
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:514
bool topIsLowIndex() const
Definition: Image.h:316
int width() const
Gets width of image in pixels.
Definition: Image.h:134
yarp::os::NetInt32 r
Definition: Image.h:524
char ** data
Definition: Image.h:360
virtual int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:674
The main, catch-all namespace for YARP.
YARP_END_PACK typedef float PixelFloat
Floating point pixel type.
Definition: Image.h:505
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:410
void setPixelSize(int imgPixelSize)
Definition: Image.cpp:489
unsigned char r
Definition: Image.h:442
Signed, packed RGB pixel type.
Definition: Image.h:497
void setPixelCode(int imgPixelCode)
Definition: Image.h:380
Image class with user control of representation details.
Definition: Image.h:377
unsigned char v
Definition: Image.h:484
Base class for storing images.
Definition: Image.h:81
An interface for writing to a network connection.
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:445
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:415
unsigned char * getPixelAddress(int x, int y) const
Get address of a pixel in memory.
Definition: Image.h:208
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:470
T & pixel(int x, int y) const
Definition: Image.h:577
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.h:328
#define VOCAB3(a, b, c)
Definition: Vocab.h:24
int getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:160
unsigned char * getRow(int r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:184
int getPadding() const
Returns the number of padding bytes.
Definition: Image.h:173
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:167
unsigned char r
Definition: Image.h:472
YARP_END_PACK typedef char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:491
Packed BGRA pixel type.
Definition: Image.h:455
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:526
bool isPixel(int x, int y) const
Check whether a coordinate lies within the image.
Definition: Image.h:218
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:420
void * implementation
Definition: Image.h:361
virtual int getPixelSize() const override
Gets pixel size in memory in bytes.
Definition: Image.h:567
void setQuantum(int imgQuantum)
Definition: Image.cpp:509
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:474
Typed image class.
Definition: Image.h:24
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:243
return ret
Definition: IplImage.cpp:876
virtual yarp::os::Type getReadType() override
Definition: Image.h:341
T & pixel(int x, int y)
Definition: Image.h:573
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:431
bool topIsLow
Definition: Image.h:358
int PAD_BYTES(int len, int pad)
computes the padding of YARP images.
Definition: Image.h:32
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:497
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:460
static const std::map< YarpVocabPixelTypesEnum, unsigned int > pixelCode2Size
Definition: Image.h:356
YarpVocabPixelTypesEnum
Definition: Image.h:41
int height() const
Gets height of image in pixels.
Definition: Image.h:140
unsigned char r
Definition: Image.h:428
const T & operator()(int x, int y) const
Definition: Image.h:581
void setQuantum(int imgQuantum)
Definition: Image.h:392
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:337
#define VOCAB4(a, b, c, d)
Definition: Vocab.h:23
virtual void write(const Bytes &b) override