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
345 char **data;
346 void *implementation;
347
348 void synchronize();
349 void initialize();
350
351 void copyPixels(const unsigned char *src, size_t id1,
352 unsigned char *dest, size_t id2, size_t w, size_t h,
353 size_t imageSize, size_t quantum1, size_t quantum2);
354};
355
356
362public:
363
364 void setPixelCode(int imgPixelCode) {
365 Image::setPixelCode(imgPixelCode);
366 }
367
368
369 void setPixelSize(size_t imgPixelSize) {
370 Image::setPixelSize(imgPixelSize);
371 //pixelCode and pixelsSize should be linked together consistently.
372 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
373 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
374 }
375
376 void setQuantum(size_t imgQuantum) {
377 Image::setQuantum(imgQuantum);
378 }
379
380private:
381};
382
383
384
385
386#include <yarp/os/NetInt32.h>
387
388namespace yarp::sig {
389
393typedef unsigned char PixelMono;
394
398typedef yarp::os::NetUint16 PixelMono16;
399
403typedef yarp::os::NetInt32 PixelInt;
404
409struct YARP_sig_API PixelRgb
410{
411 unsigned char r {0};
412 unsigned char g {0};
413 unsigned char b {0};
414
415 PixelRgb() = default;
416 PixelRgb(unsigned char n_r,
417 unsigned char n_g,
418 unsigned char n_b) :
419 r(n_r),
420 g(n_g),
421 b(n_b)
422 {
423 }
424};
426
431struct YARP_sig_API PixelRgba
432{
433 PixelRgba() = default;
434 PixelRgba(unsigned char n_r,
435 unsigned char n_g,
436 unsigned char n_b,
437 unsigned char n_a) :
438 r(n_r),
439 g(n_g),
440 b(n_b),
441 a(n_a)
442 {
443 }
444
445 unsigned char r{0};
446 unsigned char g{0};
447 unsigned char b{0};
448 unsigned char a{0};
449};
451
456struct YARP_sig_API PixelBgra
457{
458 unsigned char b{0};
459 unsigned char g{0};
460 unsigned char r{0};
461 unsigned char a{0};
462
463 PixelBgra() = default;
464 PixelBgra(unsigned char n_r,
465 unsigned char n_g,
466 unsigned char n_b,
467 unsigned char n_a) :
468 b(n_b),
469 g(n_g),
470 r(n_r),
471 a(n_a)
472 {
473 }
474};
476
481struct YARP_sig_API PixelBgr
482{
483 unsigned char b{0};
484 unsigned char g{0};
485 unsigned char r{0};
486
487 PixelBgr() = default;
488 PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b) :
489 b(n_b),
490 g(n_g),
491 r(n_r)
492 {
493 }
494};
496
501struct YARP_sig_API PixelHsv
502{
503 unsigned char h{0};
504 unsigned char s{0};
505 unsigned char v{0};
506};
508
512typedef char PixelMonoSigned;
513
518struct YARP_sig_API PixelRgbSigned
519{
520 char r{0};
521 char g{0};
522 char b{0};
523};
525
529typedef float PixelFloat;
530
535struct YARP_sig_API PixelRgbFloat
536{
537 float r{0.0F};
538 float g{0.0F};
539 float b{0.0F};
540
541 PixelRgbFloat() = default;
542 PixelRgbFloat(float n_r,
543 float n_g,
544 float n_b) :
545 r(n_r),
546 g(n_g),
547 b(n_b)
548 {
549 }
550};
552
557struct YARP_sig_API PixelRgbInt
558{
562
563 PixelRgbInt() = default;
564 PixelRgbInt(int n_r,
565 int n_g,
566 int n_b) :
567 r(n_r),
568 g(n_g),
569 b(n_b)
570 {
571 }
572};
574
579struct PixelHsvFloat
580{
581 float h{0.0F};
582 float s{0.0F};
583 float v{0.0F};
584};
586} // namespace yarp::sig
587
588
601template <class T>
603{
604private:
605 T nullPixel;
606public:
608 nullPixel()
609 {
611 }
612
613 size_t getPixelSize() const override {
614 return sizeof(T);
615 }
616
617 int getPixelCode() const override;
618
619 inline T& pixel(size_t x, size_t y) {
620 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
621 }
622
623 inline T& pixel(size_t x, size_t y) const {
624 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
625 }
626
627 inline const T& operator()(size_t x, size_t y) const {
628 return pixel(x,y);
629 }
630
631 inline T& operator()(size_t x, size_t y) {
632 return pixel(x,y);
633 }
634
635 inline T& safePixel(size_t x, size_t y) {
636 if (!isPixel(x,y)) { return nullPixel; }
637 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
638 }
639
640 inline const T& safePixel(size_t x, size_t y) const {
641 if (!isPixel(x,y)) { return nullPixel; }
642 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
643 }
644};
645
646namespace yarp::sig {
647
648template<>
652
653template<>
657
658template<>
662
663template<>
667
668template<>
672
673template<>
677
678template<>
682
683template<>
687
688template<>
692
693template<>
697
698template<>
702
703template<>
707
708template<>
712
713template<>
717
718template<typename T>
719inline int ImageOf<T>::getPixelCode() const {
720 return -(static_cast<int>(sizeof(T)));
721}
722
723} // namespace yarp::sig
724
725#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:361
void setQuantum(size_t imgQuantum)
Definition Image.h:376
void setPixelCode(int imgPixelCode)
Definition Image.h:364
void setPixelSize(size_t imgPixelSize)
Definition Image.h:369
Typed image class.
Definition Image.h:603
T & safePixel(size_t x, size_t y)
Definition Image.h:635
T & pixel(size_t x, size_t y)
Definition Image.h:619
const T & operator()(size_t x, size_t y) const
Definition Image.h:627
int getPixelCode() const override
Gets pixel type identifier.
Definition Image.h:719
T & operator()(size_t x, size_t y)
Definition Image.h:631
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition Image.h:613
const T & safePixel(size_t x, size_t y) const
Definition Image.h:640
T & pixel(size_t x, size_t y) const
Definition Image.h:623
Base class for storing images.
Definition Image.h:79
bool swap(Image &alt)
swap operator.
Definition Image.cpp:659
Image & operator=(const Image &alt)
Assignment operator.
Definition Image.cpp:602
bool operator==(const Image &alt) const
Comparison operator.
Definition Image.cpp:576
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:435
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:480
void setPixelCode(int imgPixelCode)
Definition Image.cpp:424
bool move(Image &&alt) noexcept
move operator.
Definition Image.cpp:645
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:674
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:462
Image()
Default constructor.
Definition Image.cpp:346
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition Image.cpp:370
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
Definition Image.cpp:535
bool copy(const Image &alt)
Copy operator.
Definition Image.cpp:611
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:471
~Image() override
Destructor.
Definition Image.cpp:362
void setPixelSize(size_t imgPixelSize)
Definition Image.cpp:416
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:387
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:380
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:375
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 Vocab32.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