YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
DepthImageZlibPortmonitor.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/LogStream.h>
10#include <yarp/sig/Image.h>
11
12#include <cstring>
13#include <cmath>
14#include <algorithm>
15
16#include <zlib.h>
17
18using namespace yarp::os;
19using namespace yarp::sig;
20
21namespace {
23 "yarp.carrier.portmonitor.depthimage_zlib",
27 nullptr)
28}
29
30
32{
33 m_shouldCompress = (options.find("sender_side").asBool());
34 return true;
35}
36
40
42{
43 return false;
44}
45
47{
48 return false;
49}
50
52{
53 if(m_shouldCompress)
54 {
55 //sender side / compressor
56 auto* b = thing.cast_as<ImageOf<PixelFloat>>();
57 if(b == nullptr)
58 {
59 yCError(DEPTHIMAGE_ZLIB_MONITOR, "Expected type ImageOf<PixelFloat> in sender side, but got wrong data type!");
60 return false;
61 }
62 }
63 else
64 {
65 //receiver side / decompressor
66 auto* b = thing.cast_as<Bottle>();
67 if(b == nullptr)
68 {
69 yCError(DEPTHIMAGE_ZLIB_MONITOR, "Expected type Bottle in receiver side, but got wrong data type!");
70 return false;
71 }
72 }
73 return true;
74}
75
77{
78 if(m_shouldCompress)
79 {
80 //sender side / compressor
81 //it receives an image, it sends a bottle to the network
82 auto* b = thing.cast_as<ImageOf<PixelFloat>>();
83
84 size_t sizeUncompressed= b->getRawImageSize();
85 const unsigned char* uncompressedData = b->getRawImage();
86
87 size_t sizeCompressed = (sizeUncompressed * 1.1) + 12;
88 unsigned char* compressedData = (unsigned char*) malloc (sizeCompressed);
89
91 if(!ret)
92 {
93 yCError(DEPTHIMAGE_ZLIB_MONITOR, "Failed to compress, exiting...");
95 return thing;
96 }
97
98 m_data.clear();
100 m_data.addInt32(b->width());
101 m_data.addInt32(b->height());
102 m_data.addInt32(sizeCompressed);
104 m_data.add(v);
105 m_th.setPortWriter(&m_data);
106
108 }
109 else
110 {
111 //receiver side / decompressor
112 //it receives a bottle from the network, it creates an image
113 Bottle* b= thing.cast_as<Bottle>();
114
115 size_t w = b->get(0).asInt32();
116 size_t h = b->get(1).asInt32();
117 size_t check_sizeCompressed = b->get(2).asInt32();
118 size_t check_sizeUncompressed = b->get(3).asInt32();
119 size_t sizeCompressed=b->get(4).asBlobLength();
120 const unsigned char* CompressedData = (const unsigned char*) b->get(4).asBlob();
121
122 size_t sizeUncompressed = w * h * 4;
125 {
126 yCError(DEPTHIMAGE_ZLIB_MONITOR, "Invalid data received: wrong blob size?");
127 return thing;
128 }
129 unsigned char* uncompressedData = (unsigned char*)malloc(sizeUncompressed);
130
132 if(!ret)
133 {
134 yCError(DEPTHIMAGE_ZLIB_MONITOR, "Failed to decompress, exiting...");
136 return thing;
137 }
138
139 m_imageOut.resize(w, h);
141 m_th.setPortWriter(&m_imageOut);
142
144 }
145
146 return m_th;
147}
148
149int DepthImageZlibMonitorObject::compressData(const unsigned char* in, const size_t& in_size, unsigned char* out, size_t& out_size)
150{
151 int z_result = compress((Bytef*)out, (uLongf*)&out_size, (Bytef*)in, in_size);
152 switch (z_result)
153 {
154 case Z_OK:
155 break;
156
157 case Z_MEM_ERROR:
158 yCError(DEPTHIMAGE_ZLIB_MONITOR, "zlib compression: out of memory");
159 return false;
160 break;
161
162 case Z_BUF_ERROR:
163 yCError(DEPTHIMAGE_ZLIB_MONITOR, "zlib compression: output buffer wasn't large enough");
164 return false;
165 break;
166 }
167
168 return true;
169}
170
171int DepthImageZlibMonitorObject::decompressData(const unsigned char* in, const size_t& in_size, unsigned char* out, size_t& out_size)
172{
173 int z_result = uncompress((Bytef*)out, (uLongf*)&out_size, (const Bytef*)in, in_size);
174 switch (z_result)
175 {
176 case Z_OK:
177 break;
178
179 case Z_MEM_ERROR:
180 yCError(DEPTHIMAGE_ZLIB_MONITOR, "zlib compression: out of memory");
181 return false;
182 break;
183
184 case Z_BUF_ERROR:
185 yCError(DEPTHIMAGE_ZLIB_MONITOR, "zlib compression: output buffer wasn't large enough");
186 return false;
187 break;
188
189 case Z_DATA_ERROR:
190 yCError(DEPTHIMAGE_ZLIB_MONITOR, "zlib compression: file contains corrupted data");
191 return false;
192 break;
193 }
194
195 return true;
196}
bool ret
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
void destroy() override
This will be called when the portmonitor object destroyes.
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
int decompressData(const unsigned char *in, const size_t &in_size, unsigned char *out, size_t &out_size)
int compressData(const unsigned char *in, const size_t &in_size, unsigned char *out, size_t &out_size)
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:309
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
A mini-server for performing network communication in the background.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:873
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:833
@ LogTypeReserved
Definition Log.h:98
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
void setPortWriter(yarp::os::PortWriter *writer)
Set the reference to a PortWriter object.
Definition Things.cpp:26
A single value (typically within a Bottle).
Definition Value.h:43
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
virtual size_t asBlobLength() const
Get binary data length.
Definition Value.cpp:267
virtual const char * asBlob() const
Get binary data value.
Definition Value.cpp:261
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
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
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.