YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
LayeredImage.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <yarp/sig/Image.h>
8
9#include <yarp/os/Bottle.h>
12#include <yarp/os/Log.h>
13#include <yarp/os/Time.h>
14#include <yarp/os/Vocab.h>
15#include <yarp/os/LogStream.h>
16
18#include <yarp/sig/ImageUtils.h>
19
20#include <cstdio>
21#include <cstring>
22#include <string>
23#include <utility>
24#include <math.h>
25
26using namespace yarp::sig;
27using namespace yarp::os;
28
29inline void writeToConnection(const Image& img, ConnectionWriter& connection)
30{
32
33 imghdr.setFromImage(img);
34 size_t hdrsize = sizeof(imghdr);
35 connection.appendInt32(BOTTLE_TAG_BLOB);
36 connection.appendInt32(hdrsize);
37 connection.appendBlock((char*)(&imghdr), hdrsize);
38
39 size_t imgsize = img.getRawImageSize();
40 connection.appendInt32(BOTTLE_TAG_BLOB);
41 connection.appendInt32(imgsize);
42 connection.appendBlock((char*)(img.getRawImage()), imgsize);
43
44 return;
45}
46
47inline bool readFromConnection(FlexImage& dest, ConnectionReader& connection)
48{
49 bool ok = true;
51
52 connection.expectInt32();
53 size_t sizeData = connection.expectInt32();
54 ok &= connection.expectBlock((char*)(&imghdr), sizeData);
55 if (!ok) { return false; }
56 imghdr.setToImage(dest);
57
58 connection.expectInt32();
59 size_t sizeImg = connection.expectInt32();
60 size_t psizeImg = dest.getRawImageSize();
61 if (sizeImg != psizeImg)
62 {
63 return false;
64 }
65 unsigned char* pImg = dest.getRawImage();
66 ok &= connection.expectBlock((char*)pImg, sizeImg);
67
68 return ok;
69}
70
74
75
79
80
82{
84 layers.clear();
85}
86
88{
89 bool ok = true;
90
91 connection.convertTextMode();
92
93 // LIST OF ELEMENTS
94 connection.expectInt32();
95 size_t elems = connection.expectInt32();
96
97 //ELEMENT 1
98 connection.expectInt32();
99 size_t layersNum = connection.expectInt32();
100 if (elems != 1 + (1 * 2) + (layersNum*10))
101 {
102 return false;
103 }
104
105 // ELEMENT 2-3
106 ok &= readFromConnection(background, connection);
107
108 // ELEMENT 4-...
109 // each layer contains 8+2 elems
110 layers.clear();
111 for (size_t i = 0; i < layersNum; i++)
112 {
115
116 connection.expectInt32();
117 int32_t enable_val = connection.expectInt8(); //1
118 connection.expectInt32();
119 colorkey.enable = connection.expectInt8(); //2
120 connection.expectInt32();
121 colorkey.value = connection.expectInt32(); //3
122 connection.expectInt32();
123 alpha.enable = connection.expectInt8(); //4
124 connection.expectInt32();
125 alpha.value = connection.expectFloat32(); // 5
126 connection.expectInt32();
127 bool can_be_compressed = connection.expectInt8(); //6
128 connection.expectInt32();
129 int32_t offset_x = connection.expectInt32(); //7
130 connection.expectInt32();
131 int32_t offset_y = connection.expectInt32(); //8
132
134 ok &= readFromConnection(fleximg, connection); //9-10
135 yarp::sig::ImageLayer oneLayer(fleximg, enable_val, colorkey, alpha, can_be_compressed, offset_x, offset_y);
136 layers.emplace_back(oneLayer);
137 }
138
139 return true;
140}
141
142
144{
145 size_t layers_num = layers.size();
146
147 //LIST OF ELEMENTS
148 connection.appendInt32(BOTTLE_TAG_LIST);
149 connection.appendInt32(1+(1*2)+(layers_num*10));
150
151 //ELEMENT 1
152 connection.appendInt32(BOTTLE_TAG_INT32);
153 connection.appendInt32(layers_num);
154
155 // ELEMENT 2-3
156 writeToConnection(background, connection);
157
158 // ELEMENT 4-...
159 // each layer contains 8+2 elems
160 for (size_t i = 0; i < layers_num; i++)
161 {
162 connection.appendInt32(BOTTLE_TAG_INT8); //1
163 connection.appendInt8 (layers[i].enable);
164 connection.appendInt32(BOTTLE_TAG_INT8); //2
165 connection.appendInt8(layers[i].colorkey.enable);
166 connection.appendInt32(BOTTLE_TAG_INT32); //3
167 connection.appendInt32(layers[i].colorkey.value);
168 connection.appendInt32(BOTTLE_TAG_INT8); //4
169 connection.appendInt8(layers[i].alpha.enable);
170 connection.appendInt32(BOTTLE_TAG_FLOAT32); //5
171 connection.appendFloat32(layers[i].alpha.value);
172 connection.appendInt32(BOTTLE_TAG_INT8); //6
173 connection.appendInt8(layers[i].can_be_compressed);
174 connection.appendInt32(BOTTLE_TAG_INT32); //7
175 connection.appendInt32(layers[i].offset_x);
176 connection.appendInt32(BOTTLE_TAG_INT32); //8
177 connection.appendInt32(layers[i].offset_y);
178 writeToConnection(layers[i].layer, connection); // 9-10
179 }
180
181 connection.convertTextMode();
182 return !connection.isError();
183}
184
185
187 Portable()
188{
189 background = alt.background;
190 this->layers = alt.layers;
191}
192
194{
195}
196
198{
199 background = alt.background;
200 this->layers = alt.layers;
201 return *this;
202}
203
205{
206 size_t l1 = this->layers.size();
207 size_t l2 = alt.layers.size();
208
209 if (l1 != l2)
210 {
211 return false;
212 }
213
214 if (background != alt.background)
215 {
216 return false;
217 }
218
219 for (size_t i = 0; i < l1; i++)
220 {
221 if ((this->layers[i].enable != alt.layers[i].enable) ||
222 (this->layers[i].colorkey.enable != alt.layers[i].colorkey.enable) ||
223 (this->layers[i].colorkey.value != alt.layers[i].colorkey.value) ||
224 (this->layers[i].alpha.enable != alt.layers[i].alpha.enable) ||
225 (fabs(this->layers[i].alpha.value - alt.layers[i].alpha.value) > 0.001) ||
226 (this->layers[i].layer != alt.layers[i].layer) ||
227 (this->layers[i].can_be_compressed != alt.layers[i].can_be_compressed) ||
228 (this->layers[i].offset_x != alt.layers[i].offset_x) ||
229 (this->layers[i].offset_y != alt.layers[i].offset_y))
230 {
231 return false;
232 }
233 }
234
235 return true;
236}
237
239{
241
242 bool ret = true;
243 for (size_t i = 0; i < this->layers.size(); i++)
244 {
245 if (layers[i].enable == false)
246 {
247 continue;
248 }
249
250 ret &= yarp::sig::utils::sum(outimg, layers[i].layer, layers[i].colorkey.enable, layers[i].colorkey.value, layers[i].alpha.enable, layers[i].alpha.value, layers[i].offset_x, layers[i].offset_y);
251 }
252
253 return outimg;
254}
255
256LayeredImage::operator yarp::sig::FlexImage()
257{
258 return convert_to_flexImage();
259}
#define BOTTLE_TAG_INT8
Definition Bottle.h:19
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_BLOB
Definition Bottle.h:27
#define BOTTLE_TAG_LIST
Definition Bottle.h:28
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:24
bool readFromConnection(Image &dest, ImageNetworkHeader &header, ConnectionReader &connection)
This helper function groups code to avoid duplication.
Definition Image.cpp:39
bool ret
void writeToConnection(const Image &img, ConnectionWriter &connection)
bool readFromConnection(FlexImage &dest, ConnectionReader &connection)
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
virtual yarp::conf::float32_t expectFloat32()=0
Read a 32-bit floating point number from the network connection.
virtual std::int8_t expectInt8()=0
Read a 8-bit integer from the network connection.
An interface for writing to a network connection.
virtual bool isError() const =0
virtual void appendInt8(std::int8_t data)=0
Send a representation of a 8-bit integer to the network connection.
virtual void appendFloat32(yarp::conf::float32_t data)=0
Send a representation of a 32-bit floating point number to the network connection.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
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.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition Portable.h:25
Image class with user control of representation details.
Definition Image.h:363
A single layer of a layered image.
Byte order in image header for network transmission.
Base class for storing images.
Definition Image.h:79
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
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
void zero()
Set all pixels to 0.
Definition Image.cpp:395
A Layered Image, composed by a background and multiple layers.
yarp::sig::FlexImage convert_to_flexImage()
Conversion operator.
~LayeredImage() override
Destructor.
yarp::sig::FlexImage background
void clear()
Clear the layered Image.
bool read(yarp::os::ConnectionReader &connection) override
Read a LayeredImage from a connection.
std::vector< yarp::sig::ImageLayer > layers
bool write(yarp::os::ConnectionWriter &connection) const override
Write a LayeredImage to a connection.
LayeredImage & operator=(const LayeredImage &alt)
Assignment operator.
bool operator==(const LayeredImage &alt) const
Comparison operator.
LayeredImage()
Default constructor.
An interface to the operating system, including Port based communication.
bool sum(yarp::sig::Image &OutImg, const yarp::sig::Image &InImg, bool colorkey_enable, int colorkey, bool alpha_enable, float alpha, size_t off_x, size_t off_y)
applies an image on the top over another image.