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
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
299 bool read(yarp::os::ConnectionReader& connection) override;
300
305 bool write(yarp::os::ConnectionWriter& connection) const override;
306
307 void setQuantum(size_t imgQuantum);
308
313 bool topIsLowIndex() const {
314 return topIsLow;
315 }
316
325 void setTopIsLowIndex(bool flag);
326
327
332 char **getRowArray() {
333 return data;
334 }
335
336 yarp::os::Type getReadType() const override {
337 return yarp::os::Type::byName("yarp/image");
338 }
339
340protected:
341
342 void setPixelCode(int imgPixelCode);
343
344 //pixelCode and pixelsSize should be linked together consistently.
345 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
346 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
347 void setPixelSize(size_t imgPixelSize);
348
349
350private:
352 static const std::map<YarpVocabPixelTypesEnum, size_t> pixelCode2Size;
353 size_t imgWidth, imgHeight, imgPixelSize, imgRowSize, imgQuantum;
354 int imgPixelCode;
355 bool topIsLow;
356
357 char **data;
358 void *implementation;
359
360 void synchronize();
361 void initialize();
362
363 void copyPixels(const unsigned char *src, size_t id1,
364 unsigned char *dest, size_t id2, size_t w, size_t h,
365 size_t imageSize, size_t quantum1, size_t quantum2,
366 bool topIsLow1, bool topIsLow2);
367};
368
369
375public:
376
377 void setPixelCode(int imgPixelCode) {
378 Image::setPixelCode(imgPixelCode);
379 }
380
381
382 void setPixelSize(size_t imgPixelSize) {
383 Image::setPixelSize(imgPixelSize);
384 //pixelCode and pixelsSize should be linked together consistently.
385 //since setPixelCode set also the corresponding pixelSize setPixelSize should not be used at all except for
386 //setting an arbitrary pixelSize with no corresponding pixel code (in that case the pixelCode will be set to -pixelSize).
387 }
388
389 void setQuantum(size_t imgQuantum) {
390 Image::setQuantum(imgQuantum);
391 }
392
393private:
394};
395
396
397
398
399#include <yarp/os/NetInt32.h>
400
401namespace yarp::sig {
402
406typedef unsigned char PixelMono;
407
411typedef yarp::os::NetUint16 PixelMono16;
412
416typedef yarp::os::NetInt32 PixelInt;
417
422struct YARP_sig_API PixelRgb
423{
424 unsigned char r {0};
425 unsigned char g {0};
426 unsigned char b {0};
427
428 PixelRgb() = default;
429 PixelRgb(unsigned char n_r,
430 unsigned char n_g,
431 unsigned char n_b) :
432 r(n_r),
433 g(n_g),
434 b(n_b)
435 {
436 }
437};
439
444struct YARP_sig_API PixelRgba
445{
446 PixelRgba() = default;
447 PixelRgba(unsigned char n_r,
448 unsigned char n_g,
449 unsigned char n_b,
450 unsigned char n_a) :
451 r(n_r),
452 g(n_g),
453 b(n_b),
454 a(n_a)
455 {
456 }
457
458 unsigned char r{0};
459 unsigned char g{0};
460 unsigned char b{0};
461 unsigned char a{0};
462};
464
469struct YARP_sig_API PixelBgra
470{
471 unsigned char b{0};
472 unsigned char g{0};
473 unsigned char r{0};
474 unsigned char a{0};
475
476 PixelBgra() = default;
477 PixelBgra(unsigned char n_r,
478 unsigned char n_g,
479 unsigned char n_b,
480 unsigned char n_a) :
481 b(n_b),
482 g(n_g),
483 r(n_r),
484 a(n_a)
485 {
486 }
487};
489
494struct YARP_sig_API PixelBgr
495{
496 unsigned char b{0};
497 unsigned char g{0};
498 unsigned char r{0};
499
500 PixelBgr() = default;
501 PixelBgr(unsigned char n_r, unsigned char n_g, unsigned char n_b) :
502 b(n_b),
503 g(n_g),
504 r(n_r)
505 {
506 }
507};
509
514struct YARP_sig_API PixelHsv
515{
516 unsigned char h{0};
517 unsigned char s{0};
518 unsigned char v{0};
519};
521
525typedef char PixelMonoSigned;
526
531struct YARP_sig_API PixelRgbSigned
532{
533 char r{0};
534 char g{0};
535 char b{0};
536};
538
542typedef float PixelFloat;
543
548struct YARP_sig_API PixelRgbFloat
549{
550 float r{0.0F};
551 float g{0.0F};
552 float b{0.0F};
553
554 PixelRgbFloat() = default;
555 PixelRgbFloat(float n_r,
556 float n_g,
557 float n_b) :
558 r(n_r),
559 g(n_g),
560 b(n_b)
561 {
562 }
563};
565
570struct YARP_sig_API PixelRgbInt
571{
575
576 PixelRgbInt() = default;
577 PixelRgbInt(int n_r,
578 int n_g,
579 int n_b) :
580 r(n_r),
581 g(n_g),
582 b(n_b)
583 {
584 }
585};
587
592struct PixelHsvFloat
593{
594 float h{0.0F};
595 float s{0.0F};
596 float v{0.0F};
597};
599} // namespace yarp::sig
600
601
614template <class T>
616{
617private:
618 T nullPixel;
619public:
621 nullPixel()
622 {
624 }
625
626 size_t getPixelSize() const override {
627 return sizeof(T);
628 }
629
630 int getPixelCode() const override;
631
632 inline T& pixel(size_t x, size_t y) {
633 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
634 }
635
636 inline T& pixel(size_t x, size_t y) const {
637 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
638 }
639
640 inline const T& operator()(size_t x, size_t y) const {
641 return pixel(x,y);
642 }
643
644 inline T& operator()(size_t x, size_t y) {
645 return pixel(x,y);
646 }
647
648 inline T& safePixel(size_t x, size_t y) {
649 if (!isPixel(x,y)) { return nullPixel; }
650 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
651 }
652
653 inline const T& safePixel(size_t x, size_t y) const {
654 if (!isPixel(x,y)) { return nullPixel; }
655 return *(reinterpret_cast<T*>(getPixelAddress(x,y)));
656 }
657};
658
659namespace yarp::sig {
660
661template<>
665
666template<>
670
671template<>
675
676template<>
680
681template<>
685
686template<>
690
691template<>
695
696template<>
700
701template<>
705
706template<>
710
711template<>
715
716template<>
720
721template<>
725
726template<>
730
731template<typename T>
732inline int ImageOf<T>::getPixelCode() const {
733 return -(static_cast<int>(sizeof(T)));
734}
735
736} // namespace yarp::sig
737
738#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:374
void setQuantum(size_t imgQuantum)
Definition Image.h:389
void setPixelCode(int imgPixelCode)
Definition Image.h:377
void setPixelSize(size_t imgPixelSize)
Definition Image.h:382
Typed image class.
Definition Image.h:616
T & safePixel(size_t x, size_t y)
Definition Image.h:648
T & pixel(size_t x, size_t y)
Definition Image.h:632
const T & operator()(size_t x, size_t y) const
Definition Image.h:640
int getPixelCode() const override
Gets pixel type identifier.
Definition Image.h:732
T & operator()(size_t x, size_t y)
Definition Image.h:644
size_t getPixelSize() const override
Gets pixel size in memory in bytes.
Definition Image.h:626
const T & safePixel(size_t x, size_t y) const
Definition Image.h:653
T & pixel(size_t x, size_t y) const
Definition Image.h:636
Base class for storing images.
Definition Image.h:79
bool swap(Image &alt)
swap operator.
Definition Image.cpp:670
Image & operator=(const Image &alt)
Assignment operator.
Definition Image.cpp:609
bool topIsLowIndex() const
Definition Image.h:313
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:451
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:509
void setPixelCode(int imgPixelCode)
Definition Image.cpp:440
bool move(Image &&alt) noexcept
move operator.
Definition Image.cpp:656
char ** getRowArray()
Get an array of pointers to the rows of the image.
Definition Image.h:332
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition Image.cpp:685
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:491
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:567
bool copy(const Image &alt)
Copy operator.
Definition Image.cpp:618
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:500
~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:336
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:461
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:395
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:390
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition Image.h:237
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