YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
124 bool operator==(const Image& alt) const;
125
129 ~Image() override;
130
131
137 bool copy(const Image& alt);
138
139
148 bool copy(const Image& alt, size_t w, size_t h);
149
150
156 bool move(Image&& alt) noexcept;
157
158
164 bool swap(Image& alt);
165
166
171 inline size_t width() const { return imgWidth; }
172
177 inline size_t height() const { return imgHeight; }
178
183 virtual size_t getPixelSize() const;
184
191 virtual int getPixelCode() const;
192
197 inline size_t getRowSize() const { return imgRowSize; }
198
199
204 inline size_t getQuantum() const { return imgQuantum; }
205
210 inline size_t getPadding() const
211 {
212 const size_t ret=imgRowSize-imgWidth*imgPixelSize;
213 return ret;
214 }
215
221 inline unsigned char *getRow(size_t r)
222 {
223 // should we check limits?
224 return reinterpret_cast<unsigned char *>(data[r]);
225 }
226
233 inline const unsigned char *getRow(size_t r) const
234 {
235 // should we check limits?
236 return reinterpret_cast<const unsigned char *>(data[r]);
237 }
238
245 inline unsigned char *getPixelAddress(size_t x, size_t y) const {
246 return reinterpret_cast<unsigned char *>(data[y] + x*imgPixelSize);
247 }
248
255 inline bool isPixel(size_t x, size_t y) const {
256 return (x<imgWidth && y<imgHeight);
257 }
258
262 void zero();
263
273 void resize(size_t imgWidth, size_t imgHeight);
274
280 void resize(const Image& alt) {
281 resize(alt.width(),alt.height());
282 }
283
289 void setExternal(const void *data, size_t imgWidth, size_t imgHeight);
290
295 unsigned char *getRawImage() const;
296
301 size_t getRawImageSize() const;
302
307 bool read(yarp::os::ConnectionReader& connection) override;
308
313 bool write(yarp::os::ConnectionWriter& connection) const override;
314
315 void setQuantum(size_t imgQuantum);
316
321 char **getRowArray() {
322 return data;
323 }
324
325 yarp::os::Type getReadType() const override {
326 return yarp::os::Type::byName("yarp/image");
327 }
328
329protected:
330
331 void setPixelCode(int imgPixelCode);
332
333 //pixelCode and pixelsSize should be linked together consistently.
334 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
335 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
336 void setPixelSize(size_t imgPixelSize);
337
338
339private:
341 static const std::map<YarpVocabPixelTypesEnum, size_t> pixelCode2Size;
342 size_t imgWidth, imgHeight, imgPixelSize, imgRowSize, imgQuantum;
343 int imgPixelCode;
344 bool topIsLow;
345
346 char **data;
347 void *implementation;
348
349 void synchronize();
350 void initialize();
351
352 void copyPixels(const unsigned char *src, size_t id1,
353 unsigned char *dest, size_t id2, size_t w, size_t h,
354 size_t imageSize, size_t quantum1, size_t quantum2,
355 bool topIsLow1, bool topIsLow2);
356};
357
358
364public:
365
366 void setPixelCode(int imgPixelCode) {
367 Image::setPixelCode(imgPixelCode);
368 }
369
370
371 void setPixelSize(size_t imgPixelSize) {
372 Image::setPixelSize(imgPixelSize);
373 //pixelCode and pixelsSize should be linked together consistently.
374 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
375 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
376 }
377
378 void setQuantum(size_t imgQuantum) {
379 Image::setQuantum(imgQuantum);
380 }
381
382private:
383};
384
385
386
387
388#include <yarp/os/NetInt32.h>
389
390namespace yarp::sig {
391
395typedef unsigned char PixelMono;
396
400typedef yarp::os::NetUint16 PixelMono16;
401
405typedef yarp::os::NetInt32 PixelInt;
406
411struct YARP_sig_API PixelRgb
412{
413 unsigned char r {0};
414 unsigned char g {0};
415 unsigned char b {0};
416
417 PixelRgb() = default;
418 PixelRgb(unsigned char n_r,
419 unsigned char n_g,
420 unsigned char n_b) :
421 r(n_r),
422 g(n_g),
423 b(n_b)
424 {
425 }
426};
428
433struct YARP_sig_API PixelRgba
434{
435 PixelRgba() = default;
436 PixelRgba(unsigned char n_r,
437 unsigned char n_g,
438 unsigned char n_b,
439 unsigned char n_a) :
440 r(n_r),
441 g(n_g),
442 b(n_b),
443 a(n_a)
444 {
445 }
446
447 unsigned char r{0};
448 unsigned char g{0};
449 unsigned char b{0};
450 unsigned char a{0};
451};
453
458struct YARP_sig_API PixelBgra
459{
460 unsigned char b{0};
461 unsigned char g{0};
462 unsigned char r{0};
463 unsigned char a{0};
464
465 PixelBgra() = default;
466 PixelBgra(unsigned char n_r,
467 unsigned char n_g,
468 unsigned char n_b,
469 unsigned char n_a) :
470 b(n_b),
471 g(n_g),
472 r(n_r),
473 a(n_a)
474 {
475 }
476};
478
483struct YARP_sig_API PixelBgr
484{
485 unsigned char b{0};
486 unsigned char g{0};
487 unsigned char r{0};
488
489 PixelBgr() = default;
490 PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b) :
491 b(n_b),
492 g(n_g),
493 r(n_r)
494 {
495 }
496};
498
503struct YARP_sig_API PixelHsv
504{
505 unsigned char h{0};
506 unsigned char s{0};
507 unsigned char v{0};
508};
510
514typedef char PixelMonoSigned;
515
520struct YARP_sig_API PixelRgbSigned
521{
522 char r{0};
523 char g{0};
524 char b{0};
525};
527
531typedef float PixelFloat;
532
537struct YARP_sig_API PixelRgbFloat
538{
539 float r{0.0F};
540 float g{0.0F};
541 float b{0.0F};
542
543 PixelRgbFloat() = default;
544 PixelRgbFloat(float n_r,
545 float n_g,
546 float n_b) :
547 r(n_r),
548 g(n_g),
549 b(n_b)
550 {
551 }
552};
554
559struct YARP_sig_API PixelRgbInt
560{
564
565 PixelRgbInt() = default;
566 PixelRgbInt(int n_r,
567 int n_g,
568 int n_b) :
569 r(n_r),
570 g(n_g),
571 b(n_b)
572 {
573 }
574};
576
581struct PixelHsvFloat
582{
583 float h{0.0F};
584 float s{0.0F};
585 float v{0.0F};
586};
588} // namespace yarp::sig
589
590
603template <class T>
605{
606private:
607 T nullPixel;
608public:
610 nullPixel()
611 {
613 }
614
615 size_t getPixelSize() const override {
616 return sizeof(T);
617 }
618
619 int getPixelCode() const override;
620
621 inline T& pixel(size_t x, size_t y) {
622 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
623 }
624
625 inline T& pixel(size_t x, size_t y) const {
626 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
627 }
628
629 inline const T& operator()(size_t x, size_t y) const {
630 return pixel(x,y);
631 }
632
633 inline T& operator()(size_t x, size_t y) {
634 return pixel(x,y);
635 }
636
637 inline T& safePixel(size_t x, size_t y) {
638 if (!isPixel(x,y)) { return nullPixel; }
639 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
640 }
641
642 inline const T& safePixel(size_t x, size_t y) const {
643 if (!isPixel(x,y)) { return nullPixel; }
644 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
645 }
646};
647
648namespace yarp::sig {
649
650template<>
654
655template<>
659
660template<>
664
665template<>
669
670template<>
674
675template<>
679
680template<>
684
685template<>
689
690template<>
694
695template<>
699
700template<>
704
705template<>
709
710template<>
714
715template<>
719
720template<typename T>
721inline int ImageOf<T>::getPixelCode() const {
722 return -(static_cast<int>(sizeof(T)));
723}
724
725} // namespace yarp::sig
726
727#endif // YARP_SIG_IMAGE_H
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
bool ret
RandScalar * implementation(void *t)
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:363
void setQuantum(size_t imgQuantum)
Definition Image.h:378
void setPixelCode(int imgPixelCode)
Definition Image.h:366
void setPixelSize(size_t imgPixelSize)
Definition Image.h:371
Typed image class.
Definition Image.h:605
T & safePixel(size_t x, size_t y)
Definition Image.h:637
T & pixel(size_t x, size_t y)
Definition Image.h:621
const T & operator()(size_t x, size_t y) const
Definition Image.h:629
int getPixelCode() const override
Gets pixel type identifier.
Definition Image.h:721
T & operator()(size_t x, size_t y)
Definition Image.h:633
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition Image.h:615
const T & safePixel(size_t x, size_t y) const
Definition Image.h:642
T & pixel(size_t x, size_t y) const
Definition Image.h:625
Base class for storing images.
Definition Image.h:79
bool swap(Image &alt)
swap operator.
Definition Image.cpp:672
Image & operator=(const Image &alt)
Assignment operator.
Definition Image.cpp:615
bool operator==(const Image &alt) const
Comparison operator.
Definition Image.cpp:593
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition Image.h:221
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:280
void setQuantum(size_t imgQuantum)
Definition Image.cpp:451
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
size_t getPadding() const
Returns the number of padding bytes.
Definition Image.h:210
bool read(yarp::os::ConnectionReader &connection) override
Read image from a connection.
Definition Image.cpp:497
void setPixelCode(int imgPixelCode)
Definition Image.cpp:440
bool move(Image &&alt) noexcept
move operator.
Definition Image.cpp:658
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition Image.h:321
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition Image.cpp:687
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition Image.h:197
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
Image()
Default constructor.
Definition Image.cpp:360
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition Image.cpp:385
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
Definition Image.cpp:552
bool copy(const Image &alt)
Copy operator.
Definition Image.cpp:624
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:488
~Image() override
Destructor.
Definition Image.cpp:377
void setPixelSize(size_t imgPixelSize)
Definition Image.cpp:432
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:402
yarp::os::Type getReadType() const override
Definition Image.h:325
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition Image.h:255
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition Image.h:204
void zero()
Set all pixels to 0.
Definition Image.cpp:395
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
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:233
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:390
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition Image.h:245
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
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition Image.h:30
The main, catch-all namespace for YARP.
Definition dirs.h:16
#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