YARP
Yet Another Robot Platform
WireImage.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 YARP2_WIREIMAGE
8#define YARP2_WIREIMAGE
9
10#include <yarp/conf/system.h>
11#include <yarp/os/SizedWriter.h>
15#include <yarp/sig/Image.h>
16#include <cstdlib>
17#include <cstring>
18
20
21namespace yarp::wire_rep_utils {
22
23/*
24// layout for ROS:
25// Header header
26// uint32 seq --> +1
27// time stamp --> int32 secs, int32 nsecs (sync by time)
28// string frame_id --> string! argh / just pass it along
29// uint32 height
30// uint32 width
31// string encoding --> string! argh
32// uint8 is_bigendian
33// uint32 step
34// uint8[] data --> real payload
35*/
36
39public:
43};
45
58private:
59 RosImageStamp ros_seq_stamp;
60 yarp::os::ManagedBytes ros_const_header;
61 const yarp::sig::FlexImage *image;
62public:
64 ros_seq_stamp({0,0,0}),
65 image(nullptr)
66 {}
67
68 void init(const yarp::sig::FlexImage& img,
69 const std::string& frame) {
70 image = &img;
73 if (!pbuf) {
74 ::exit(1);
75 }
76 yarp::os::ConnectionWriter& buf = *pbuf;
78 // probably need to translate encoding format better, but at
79 // a guess "rgb" and "bgr" will work ok.
80 std::string encoding =
82 switch (image->getPixelCode()) {
83 case VOCAB_PIXEL_BGR:
84 encoding = "bgr8";
85 break;
86 case VOCAB_PIXEL_RGB:
87 encoding = "rgb8";
88 break;
90 encoding = "mono8";
91 break;
93 encoding = "mono16";
94 break;
96 encoding = "32FC1";
97 break;
98 }
99 buf.appendString(frame);
100 buf.appendInt32(image->height());
101 buf.appendInt32(image->width());
102 buf.appendString(encoding);
103 char is_bigendian = 0;
104 buf.appendBlock(&is_bigendian,1);
105 buf.appendInt32((image->width()*image->getPixelSize())+image->getPadding());
106 buf.appendInt32(image->getRawImageSize());
107 buf.getBuffer()->write(ss);
108 std::string hdr = ss.toString();
109 yarp::os::Bytes hdr_wrap((char*)hdr.c_str(),hdr.length());
110 ros_const_header = yarp::os::ManagedBytes(hdr_wrap);
111 ros_const_header.copy();
112 delete pbuf;
113 pbuf = 0 /*NULL*/;
114 }
115
116 void update(const yarp::sig::FlexImage *img, int seq, double t) {
117 // We should check if img properties have changed. But we don't.
118 ros_seq_stamp.seq = seq;
119 ros_seq_stamp.sec = (int)(t);
120 ros_seq_stamp.nsec = (int)((t-(int)t)*1e9);
121 }
122
123 size_t length() const override { return 3; }
124
125 size_t headerLength() const override { return 0; }
126
127 size_t length(size_t index) const override {
128 size_t result = 0;
129 switch (index) {
130 case 0:
131 result = sizeof(ros_seq_stamp);
132 break;
133 case 1:
134 result = ros_const_header.length();
135 break;
136 case 2:
137 result = image->getRawImageSize();
138 break;
139 default:
140 result = 0;
141 break;
142 }
143 return result;
144 }
145
146 const char *data(size_t index) const override {
147 const char *result = 0 /*NULL*/;
148 switch (index) {
149 case 0:
150 result = (const char *)(&ros_seq_stamp);
151 break;
152 case 1:
153 result = ros_const_header.get();
154 break;
155 case 2:
156 result = (const char *)(image->getRawImage());
157 break;
158 }
159 return result;
160 }
161
162 yarp::os::PortReader *getReplyHandler() override { return NULL; }
163
164 yarp::os::Portable *getReference() override { return NULL; }
165
166 bool dropRequested() override { return false; }
167
168 void startWrite() const override {}
169
170 void stopWrite() const override {}
171};
172
174private:
176public:
177 yarp::sig::FlexImage *checkForImage(yarp::os::SizedWriter& writer);
178};
179
180} // namespace yarp::wire_rep_utils
181
182#endif
float t
A simple abstraction for a block of bytes.
Definition: Bytes.h:24
An interface for writing to a network connection.
static ConnectionWriter * createBufferedConnectionWriter()
Create a connection writer implementation that stores to a buffer which can be read later using getBu...
virtual SizedWriter * getBuffer()=0
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
void appendString(const std::string &str)
Send a string to the network connection.
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:21
void copy()
Makes sure data block is owned, making a copy if necessary.
const char * get() const
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:25
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:32
virtual void write(OutputStream &os)
Definition: SizedWriter.cpp:16
An OutputStream that produces a string.
Image class with user control of representation details.
Definition: Image.h:411
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
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:542
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition: Image.cpp:436
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
size_t height() const
Gets height of image in pixels.
Definition: Image.h:169
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:441
As far as YARP is concerned, on the wire to/from ROS a raw image has:
Definition: WireImage.h:57
const char * data(size_t index) const override
Definition: WireImage.h:146
void stopWrite() const override
Call when all writing is finished.
Definition: WireImage.h:170
void init(const yarp::sig::FlexImage &img, const std::string &frame)
Definition: WireImage.h:68
yarp::os::PortReader * getReplyHandler() override
Definition: WireImage.h:162
void update(const yarp::sig::FlexImage *img, int seq, double t)
Definition: WireImage.h:116
size_t length(size_t index) const override
Definition: WireImage.h:127
yarp::os::Portable * getReference() override
Definition: WireImage.h:164
size_t headerLength() const override
Definition: WireImage.h:125
void startWrite() const override
Call when writing is about to begin.
Definition: WireImage.h:168
size_t length() const override
Definition: WireImage.h:123
@ VOCAB_PIXEL_MONO16
Definition: Image.h:43
@ VOCAB_PIXEL_BGR
Definition: Image.h:49
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:53
@ VOCAB_PIXEL_MONO
Definition: Image.h:42
@ VOCAB_PIXEL_RGB
Definition: Image.h:44
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:33
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:29
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:193
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:192
#define YARP_wire_rep_utils_API
Definition: api.h:12