YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
zfpPortmonitor.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
6#include "zfpPortmonitor.h"
7
9#include <yarp/sig/Image.h>
10
11#include <cstring>
12#include <cmath>
13#include <algorithm>
14
15extern "C" {
16 #include "zfp.h"
17}
18
19using namespace yarp::os;
20using namespace yarp::sig;
21
22namespace {
24 "yarp.carrier.portmonitor.depthimage_compression_zfp",
28 nullptr)
29}
30
31
33{
34 shouldCompress = (options.find("sender_side").asBool());
35 compressed = nullptr;
36 decompressed = nullptr;
37 buffer = nullptr;
38 sizeToAllocate = 0;
39 sizeToAllocateB = 0;
40 return true;
41}
42
44{
45 if(compressed){
46 free(compressed);
47 compressed = nullptr;
48 }
49
50 if(buffer){
51 free(buffer);
52 buffer = nullptr;
53 }
54
55 if(decompressed){
56 free(decompressed);
57 decompressed = nullptr;
58 }
59}
60
62{
63 return false;
64}
65
67{
68 return false;
69}
70
72{
73 if(shouldCompress){
75 if(img == nullptr) {
76 yCError(ZFPMONITOR, "Expected type ImageOf<PixelFloat> in sender side, but got wrong data type!");
77 return false;
78 }
79 }
80 else{
81 Bottle* bt= thing.cast_as<Bottle>();
82 if(bt == nullptr){
83 yCError(ZFPMONITOR, "Expected type Bottle in receiver side, but got wrong data type!");
84 return false;
85 }
86
87 }
88
89
90 return true;
91}
92
94{
95
96 if(shouldCompress) {
98 // .... buffer, len
100 compress((float*)img->getRawImage(), compressed, sizeCompressed, img->width(),img->height(),1e-3);
101 if(!compressed){
102 yCError(ZFPMONITOR, "Failed to compress, exiting...");
103 return thing;
104 }
105 data.clear();
106 data.addInt32(img->width());
107 data.addInt32(img->height());
109 Value v(compressed, sizeCompressed);
110 data.add(v);
111 th.setPortWriter(&data);
112 }
113 else
114 {
115
117
118 int width=compressedbt->get(0).asInt32();
119 int height=compressedbt->get(1).asInt32();
120 int sizeCompressed=compressedbt->get(2).asInt32();
121 // cast thing to compressed.
122 decompress((float*)compressedbt->get(3).asBlob(), decompressed, sizeCompressed, width, height,1e-3);
123
124 if(!decompressed){
125 yCError(ZFPMONITOR, "Failed to decompress, exiting...");
126 return thing;
127 }
128 imageOut.resize(width,height);
129 memcpy(imageOut.getRawImage(),decompressed,width*height*4);
130 th.setPortWriter(&imageOut);
131
132 }
133
134 return th;
135}
136
137void ZfpMonitorObject::resizeF(float *&array, int newSize){
138 if(newSize>sizeToAllocate){
139 sizeToAllocate=newSize;
140 free(array);
141 array=(float*) malloc(sizeToAllocate);
142 }
143
144
145}
146void ZfpMonitorObject::resizeV(void *&array, int newSize){
147 if(newSize>sizeToAllocateB){
148 sizeToAllocateB=newSize;
149 free(array);
150 array=(void*) malloc(sizeToAllocateB);
151 }
152
153
154}
155
156int ZfpMonitorObject::compress(float* array, float* &compressed, int &zfpsize, int nx, int ny, float tolerance){
157 int status = 0; /* return value: 0 = success */
158 zfp_type type; /* array scalar type */
159 zfp_field* field; /* array meta data */
160 zfp_stream* zfp; /* compressed stream */
161 size_t bufsize; /* byte size of compressed buffer */
162 bitstream* stream; /* bit stream to write to or read from */
163
164 type = zfp_type_float;
165 field = zfp_field_2d(array, type, nx, ny);
166
167 /* allocate meta data for a compressed stream */
168 zfp = zfp_stream_open(nullptr);
169
170 /* set compression mode and parameters via one of three functions */
171 /* zfp_stream_set_rate(zfp, rate, type, 3, 0); */
172 /* zfp_stream_set_precision(zfp, precision); */
173 zfp_stream_set_accuracy(zfp, tolerance);
174
175 /* allocate buffer for compressed data */
177
178 resizeV(buffer,bufsize);
179
180 /* associate bit stream with allocated buffer */
181 stream = stream_open(buffer, bufsize);
184
185 /* compress entire array */
186 /* compress array and output compressed stream */
188 if (!zfpsize) {
189 yCError(ZFPMONITOR, "compression failed");
190 status = 1;
191 }
192
193 resizeF(compressed,zfpsize);
194 memcpy(compressed,(float*) stream_data(zfp->stream),zfpsize);
195
196 /* clean up */
199 stream_close(stream);
200
201 return status;
202}
203
204int ZfpMonitorObject::decompress(float* array, float* &decompressed, int zfpsize, int nx, int ny, float tolerance){
205 int status = 0; /* return value: 0 = success */
206 zfp_type type; /* array scalar type */
207 zfp_field* field; /* array meta data */
208 zfp_stream* zfp; /* compressed stream */
209 size_t bufsize; /* byte size of compressed buffer */
210 bitstream* stream; /* bit stream to write to or read from */
211
212 type = zfp_type_float;
213 resizeF(decompressed,nx*ny*sizeof(float));
214 field = zfp_field_2d(decompressed, type, nx, ny);
215
216 /* allocate meta data for a compressed stream */
217 zfp = zfp_stream_open(nullptr);
218
219 /* set compression mode and parameters via one of three functions */
220 /* zfp_stream_set_rate(zfp, rate, type, 3, 0); */
221 /* zfp_stream_set_precision(zfp, precision, type); */
222 zfp_stream_set_accuracy(zfp, tolerance);
223
224 /* allocate buffer for compressed data */
226 resizeV(buffer,bufsize);
227 memcpy(buffer,array,zfpsize);
228
229 /* associate bit stream with allocated buffer */
230 stream = stream_open(buffer, zfpsize);
233
234 /* read compressed stream and decompress array */
235 if (!zfp_decompress(zfp, field)) {
236 yCError(ZFPMONITOR, "decompression failed");
237 status = 1;
238 }
239
240 /* clean up */
243 stream_close(stream);
244
245 return status;
246
247}
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
int decompress(float *array, float *&decompressed, int zfpsize, int nx, int ny, float tolerance)
int compress(float *array, float *&compressed, int &zfpsize, int nx, int ny, float tolerance)
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
void resizeV(void *&array, int newSize)
void resizeF(float *&array, int newSize)
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
void destroy() override
This will be called when the portmonitor object destroyes.
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.
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
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
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.