YARP
Yet Another Robot Platform
Image.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
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/NetUint16.h>
12#include <yarp/os/Portable.h>
13#include <yarp/os/Type.h>
14#include <yarp/os/Vocab.h>
15#include <yarp/sig/api.h>
16#include <map>
17
18namespace yarp::sig {
19class Image;
20class FlexImage;
21template <class T>
22class ImageOf;
23
30inline size_t PAD_BYTES(size_t len, size_t pad)
31{
32 const size_t rem = len % pad;
33 return (rem != 0) ? (pad - rem) : rem;
34}
35} // namespace yarp::sig
36
37// the image types partially reflect the IPL image types.
38// There must be a pixel type for every ImageType entry.
40{
68};
69
80
81public:
82
87 Image();
88
94 Image(const Image& alt);
95
101 Image(Image&& other) noexcept;
102
108 Image& operator=(const Image& alt);
109
116 Image& operator=(Image &&other) noexcept;
117
121 ~Image() override;
122
123
129 bool copy(const Image& alt);
130
131
140 bool copy(const Image& alt, size_t w, size_t h);
141
142
148 bool move(Image&& alt) noexcept;
149
150
156 bool swap(Image& alt);
157
158
163 inline size_t width() const { return imgWidth; }
164
169 inline size_t height() const { return imgHeight; }
170
175 virtual size_t getPixelSize() const;
176
183 virtual int getPixelCode() const;
184
189 inline size_t getRowSize() const { return imgRowSize; }
190
191
196 inline size_t getQuantum() const { return imgQuantum; }
197
202 inline size_t getPadding() const
203 {
204 const size_t ret=imgRowSize-imgWidth*imgPixelSize;
205 return ret;
206 }
207
213 inline unsigned char *getRow(size_t r)
214 {
215 // should we check limits?
216 return reinterpret_cast<unsigned char *>(data[r]);
217 }
218
225 inline const unsigned char *getRow(size_t r) const
226 {
227 // should we check limits?
228 return reinterpret_cast<const unsigned char *>(data[r]);
229 }
230
237 inline unsigned char *getPixelAddress(size_t x, size_t y) const {
238 return reinterpret_cast<unsigned char *>(data[y] + x*imgPixelSize);
239 }
240
247 inline bool isPixel(size_t x, size_t y) const {
248 return (x<imgWidth && y<imgHeight);
249 }
250
254 void zero();
255
265 void resize(size_t imgWidth, size_t imgHeight);
266
272 void resize(const Image& alt) {
273 resize(alt.width(),alt.height());
274 }
275
281 void setExternal(const void *data, size_t imgWidth, size_t imgHeight);
282
287 unsigned char *getRawImage() const;
288
293 size_t getRawImageSize() const;
294
295#ifndef YARP_NO_DEPRECATED // Since YARP 3.2.0
302 YARP_DEPRECATED_MSG("Use yarp::cv::toCvMat instead")
303 void *getIplImage();
304
311 YARP_DEPRECATED_MSG("Use yarp::cv::toCvMat instead")
312 const void *getIplImage() const;
313
325 YARP_DEPRECATED_MSG("Use yarp::cv::fromCvMat instead")
326 void wrapIplImage(void *iplImage);
327#endif // YARP_NO_DEPRECATED
328
329 //void wrapRawImage(void *buf, int imgWidth, int imgHeight);
330
331
336 bool read(yarp::os::ConnectionReader& connection) override;
337
342 bool write(yarp::os::ConnectionWriter& connection) const override;
343
344 void setQuantum(size_t imgQuantum);
345
350 bool topIsLowIndex() const {
351 return topIsLow;
352 }
353
362 void setTopIsLowIndex(bool flag);
363
364
369 char **getRowArray() {
370 return data;
371 }
372
373 yarp::os::Type getReadType() const override {
374 return yarp::os::Type::byName("yarp/image");
375 }
376
377protected:
378
379 void setPixelCode(int imgPixelCode);
380
381 //pixelCode and pixelsSize should be linked together consistently.
382 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
383 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
384 void setPixelSize(size_t imgPixelSize);
385
386
387private:
389 static const std::map<YarpVocabPixelTypesEnum, size_t> pixelCode2Size;
390 size_t imgWidth, imgHeight, imgPixelSize, imgRowSize, imgQuantum;
391 int imgPixelCode;
392 bool topIsLow;
393
394 char **data;
395 void *implementation;
396
397 void synchronize();
398 void initialize();
399
400 void copyPixels(const unsigned char *src, size_t id1,
401 unsigned char *dest, size_t id2, size_t w, size_t h,
402 size_t imageSize, size_t quantum1, size_t quantum2,
403 bool topIsLow1, bool topIsLow2);
404};
405
406
412public:
413
414 void setPixelCode(int imgPixelCode) {
415 Image::setPixelCode(imgPixelCode);
416 }
417
418
419 void setPixelSize(size_t imgPixelSize) {
420 Image::setPixelSize(imgPixelSize);
421 //pixelCode and pixelsSize should be linked together consistently.
422 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
423 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
424 }
425
426 void setQuantum(size_t imgQuantum) {
427 Image::setQuantum(imgQuantum);
428 }
429
430private:
431};
432
433
434
435
436#include <yarp/os/NetInt32.h>
437
438namespace yarp::sig {
439
443typedef unsigned char PixelMono;
444
449
454
460{
461 unsigned char r {0};
462 unsigned char g {0};
463 unsigned char b {0};
464
465 PixelRgb() = default;
466 PixelRgb(unsigned char n_r,
467 unsigned char n_g,
468 unsigned char n_b) :
469 r(n_r),
470 g(n_g),
471 b(n_b)
472 {
473 }
474};
476
482{
483 PixelRgba() = default;
484 PixelRgba(unsigned char n_r,
485 unsigned char n_g,
486 unsigned char n_b,
487 unsigned char n_a) :
488 r(n_r),
489 g(n_g),
490 b(n_b),
491 a(n_a)
492 {
493 }
494
495 unsigned char r{0};
496 unsigned char g{0};
497 unsigned char b{0};
498 unsigned char a{0};
499};
501
507{
508 unsigned char b{0};
509 unsigned char g{0};
510 unsigned char r{0};
511 unsigned char a{0};
512
513 PixelBgra() = default;
514 PixelBgra(unsigned char n_r,
515 unsigned char n_g,
516 unsigned char n_b,
517 unsigned char n_a) :
518 b(n_b),
519 g(n_g),
520 r(n_r),
521 a(n_a)
522 {
523 }
524};
526
532{
533 unsigned char b{0};
534 unsigned char g{0};
535 unsigned char r{0};
536
537 PixelBgr() = default;
538 PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b) :
539 b(n_b),
540 g(n_g),
541 r(n_r)
542 {
543 }
544};
546
552{
553 unsigned char h{0};
554 unsigned char s{0};
555 unsigned char v{0};
556};
558
562typedef char PixelMonoSigned;
563
569{
570 char r{0};
571 char g{0};
572 char b{0};
573};
575
579typedef float PixelFloat;
580
586{
587 float r{0.0F};
588 float g{0.0F};
589 float b{0.0F};
590
591 PixelRgbFloat() = default;
592 PixelRgbFloat(float n_r,
593 float n_g,
594 float n_b) :
595 r(n_r),
596 g(n_g),
597 b(n_b)
598 {
599 }
600};
602
608{
612
613 PixelRgbInt() = default;
614 PixelRgbInt(int n_r,
615 int n_g,
616 int n_b) :
617 r(n_r),
618 g(n_g),
619 b(n_b)
620 {
621 }
622};
624
630{
631 float h{0.0F};
632 float s{0.0F};
633 float v{0.0F};
634};
636} // namespace yarp::sig
637
638
651template <class T>
653{
654private:
655 T nullPixel;
656public:
658 nullPixel()
659 {
661 }
662
663 size_t getPixelSize() const override {
664 return sizeof(T);
665 }
666
667 int getPixelCode() const override;
668
669 inline T& pixel(size_t x, size_t y) {
670 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
671 }
672
673 inline T& pixel(size_t x, size_t y) const {
674 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
675 }
676
677 inline const T& operator()(size_t x, size_t y) const {
678 return pixel(x,y);
679 }
680
681 inline T& operator()(size_t x, size_t y) {
682 return pixel(x,y);
683 }
684
685 inline T& safePixel(size_t x, size_t y) {
686 if (!isPixel(x,y)) { return nullPixel; }
687 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
688 }
689
690 inline const T& safePixel(size_t x, size_t y) const {
691 if (!isPixel(x,y)) { return nullPixel; }
692 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
693 }
694};
695
696namespace yarp::sig {
697
698template<>
700 return VOCAB_PIXEL_MONO;
701}
702
703template<>
705 return VOCAB_PIXEL_MONO16;
706}
707
708template<>
710 return VOCAB_PIXEL_RGB;
711}
712
713template<>
715 return VOCAB_PIXEL_RGBA;
716}
717
718template<>
720 return VOCAB_PIXEL_HSV;
721}
722
723template<>
725 return VOCAB_PIXEL_BGR;
726}
727
728template<>
730 return VOCAB_PIXEL_BGRA;
731}
732
733template<>
736}
737
738template<>
741}
742
743template<>
746}
747
748template<>
751}
752
753template<>
755 return VOCAB_PIXEL_RGB_INT;
756}
757
758template<>
761}
762
763template<>
765 return VOCAB_PIXEL_INT;
766}
767
768template<typename T>
769inline int ImageOf<T>::getPixelCode() const {
770 return -(static_cast<int>(sizeof(T)));
771}
772
773} // namespace yarp::sig
774
775#endif // YARP_SIG_IMAGE_H
bool ret
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:17
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:25
static Type byName(const char *name)
Definition: Type.cpp:171
Image class with user control of representation details.
Definition: Image.h:411
void setQuantum(size_t imgQuantum)
Definition: Image.h:426
void setPixelCode(int imgPixelCode)
Definition: Image.h:414
void setPixelSize(size_t imgPixelSize)
Definition: Image.h:419
Typed image class.
Definition: Image.h:653
T & safePixel(size_t x, size_t y)
Definition: Image.h:685
T & pixel(size_t x, size_t y)
Definition: Image.h:669
const T & operator()(size_t x, size_t y) const
Definition: Image.h:677
int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:769
T & operator()(size_t x, size_t y)
Definition: Image.h:681
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition: Image.h:663
const T & safePixel(size_t x, size_t y) const
Definition: Image.h:690
T & pixel(size_t x, size_t y) const
Definition: Image.h:673
Base class for storing images.
Definition: Image.h:79
bool swap(Image &alt)
swap operator.
Definition: Image.cpp:889
Image & operator=(const Image &alt)
Assignment operator.
Definition: Image.cpp:828
bool topIsLowIndex() const
Definition: Image.h:350
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:213
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:272
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:502
size_t width() const
Gets width of image in pixels.
Definition: Image.h:163
size_t getPadding() const
Returns the number of padding bytes.
Definition: Image.h:202
bool read(yarp::os::ConnectionReader &connection) override
Read image from a connection.
Definition: Image.cpp:675
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:491
bool move(Image &&alt) noexcept
move operator.
Definition: Image.cpp:875
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition: Image.h:369
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:904
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:189
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:542
Image()
Default constructor.
Definition: Image.cpp:411
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition: Image.cpp:436
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
Definition: Image.cpp:786
bool copy(const Image &alt)
Copy operator.
Definition: Image.cpp:837
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
Definition: Image.cpp:551
~Image() override
Destructor.
Definition: Image.cpp:428
void setPixelSize(size_t imgPixelSize)
Definition: Image.cpp:483
void * getIplImage()
Returns IPL/OpenCV view of image, if possible.
Definition: Image.cpp:561
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition: Image.cpp:453
yarp::os::Type getReadType() const override
Definition: Image.h:373
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition: Image.h:247
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
Definition: Image.cpp:512
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:196
void zero()
Set all pixels to 0.
Definition: Image.cpp:446
size_t height() const
Gets height of image in pixels.
Definition: Image.h:169
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:225
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:441
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:237
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
YarpVocabPixelTypesEnum
Definition: Image.h:40
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
Definition: Image.h:59
@ VOCAB_PIXEL_YUV_420
Definition: Image.h:64
@ VOCAB_PIXEL_RGBA
Definition: Image.h:45
@ VOCAB_PIXEL_INT
Definition: Image.h:47
@ VOCAB_PIXEL_MONO16
Definition: Image.h:43
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
Definition: Image.h:58
@ VOCAB_PIXEL_YUV_444
Definition: Image.h:65
@ VOCAB_PIXEL_BGRA
Definition: Image.h:46
@ VOCAB_PIXEL_MONO_SIGNED
Definition: Image.h:50
@ VOCAB_PIXEL_BGR
Definition: Image.h:49
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:53
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
Definition: Image.h:62
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
Definition: Image.h:56
@ VOCAB_PIXEL_HSV_FLOAT
Definition: Image.h:55
@ VOCAB_PIXEL_YUV_422
Definition: Image.h:66
@ VOCAB_PIXEL_HSV
Definition: Image.h:48
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG16
Definition: Image.h:61
@ VOCAB_PIXEL_RGB_SIGNED
Definition: Image.h:51
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG16
Definition: Image.h:57
@ VOCAB_PIXEL_INVALID
Definition: Image.h:41
@ VOCAB_PIXEL_RGB_FLOAT
Definition: Image.h:54
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
Definition: Image.h:60
@ VOCAB_PIXEL_MONO
Definition: Image.h:42
@ VOCAB_PIXEL_RGB_INT
Definition: Image.h:52
@ VOCAB_PIXEL_YUV_411
Definition: Image.h:67
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB16
Definition: Image.h:63
@ VOCAB_PIXEL_RGB
Definition: Image.h:44
yarp::sig::ImageOf< T > fromCvMat(::cv::Mat &cvImage)
Convert a cv::Mat to a yarp::sig::ImageOf object.
Definition: Cv-inl.h:86
std::uint16_t NetUint16
Definition of the NetUint16 type.
Definition: NetUint16.h:29
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:29
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:27
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:562
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition: Image.h:30
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:448
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:443
float PixelFloat
Floating point pixel type.
Definition: Image.h:579
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:453
The main, catch-all namespace for YARP.
Definition: dirs.h:16
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:532
PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:538
Packed BGRA pixel type.
Definition: Image.h:507
PixelBgra(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:514
Floating point HSV pixel type.
Definition: Image.h:630
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:552
Floating point RGB pixel type.
Definition: Image.h:586
PixelRgbFloat(float n_r, float n_g, float n_b)
Definition: Image.h:592
Integer RGB pixel type.
Definition: Image.h:608
PixelRgbInt(int n_r, int n_g, int n_b)
Definition: Image.h:614
Signed, packed RGB pixel type.
Definition: Image.h:569
Packed RGB pixel type.
Definition: Image.h:460
PixelRgb(unsigned char n_r, unsigned char n_g, unsigned char n_b)
Definition: Image.h:466
Packed RGBA pixel type.
Definition: Image.h:482
PixelRgba(unsigned char n_r, unsigned char n_g, unsigned char n_b, unsigned char n_a)
Definition: Image.h:484
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:193
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING
Suppress MSVC C4251 warning for the next line.
Definition: system.h:337
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:192
#define YARP_sig_API
Definition: api.h:18