31 #include <opencv2/opencv.hpp>
39 uncompressedData_(
mat),
48 compressedData_(
bytes),
85 catch (cv::Exception &
e) {
86 UERROR(
"Exception while compressing/uncompressing data: %s",
e.what());
100 std::vector<unsigned char>
compressImage(
const cv::Mat & image,
const std::string & format)
102 std::vector<unsigned char>
bytes;
105 if(image.type() == CV_32FC1)
108 cv::Mat bgra(image.size(), CV_8UC4, image.data);
109 cv::imencode(
".png", bgra,
bytes);
113 bytes = {
'D',
'E',
'P',
'T',
'H',
'R',
'V',
'L'};
114 int numPixels = image.rows * image.cols;
116 bytes.resize(3 * numPixels + 20);
139 return cv::Mat(1, (
int)
bytes.size(), CV_8UC1,
bytes.data()).clone();
154 image = cv::Mat(
rows,
cols, CV_16UC1);
160 #if CV_MAJOR_VERSION>2 || (CV_MAJOR_VERSION >=2 && CV_MINOR_VERSION >=4)
161 image = cv::imdecode(
bytes, cv::IMREAD_UNCHANGED);
163 image = cv::imdecode(
bytes, -1);
165 if(image.type() == CV_8UC4)
169 cv::Mat depth(image.size(), CV_32FC1);
170 memcpy(depth.data, image.data, image.total()*image.elemSize());
188 image = cv::Mat(
rows,
cols, CV_16UC1);
194 #if CV_MAJOR_VERSION>2 || (CV_MAJOR_VERSION >=2 && CV_MINOR_VERSION >=4)
195 image = cv::imdecode(
bytes, cv::IMREAD_UNCHANGED);
197 image = cv::imdecode(
bytes, -1);
199 if(image.type() == CV_8UC4)
201 image = cv::Mat(image.size(), CV_32FC1, image.data).clone();
210 std::vector<unsigned char>
bytes;
213 uLong sourceLen = uLong(
data.total())*uLong(
data.elemSize());
214 uLong destLen = compressBound(sourceLen);
215 bytes.resize(destLen);
216 int errCode = compress(
217 (Bytef *)
bytes.data(),
219 (
const Bytef *)
data.data,
222 bytes.resize(destLen+3*
sizeof(
int));
224 *((
int*)&
bytes[destLen+
sizeof(
int)]) =
data.cols;
227 if(errCode == Z_MEM_ERROR)
229 UERROR(
"Z_MEM_ERROR : Insufficient memory.");
231 else if(errCode == Z_BUF_ERROR)
233 UERROR(
"Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
244 uLong sourceLen = uLong(
data.total())*uLong(
data.elemSize());
245 uLong destLen = compressBound(sourceLen);
246 bytes = cv::Mat(1, destLen+3*
sizeof(
int), CV_8UC1);
247 int errCode = compress(
250 (
const Bytef *)
data.data,
252 bytes = cv::Mat(
bytes, cv::Rect(0,0, destLen+3*
sizeof(
int), 1));
253 *((
int*)&
bytes.data[destLen]) =
data.rows;
254 *((
int*)&
bytes.data[destLen+
sizeof(
int)]) =
data.cols;
255 *((
int*)&
bytes.data[destLen+2*
sizeof(
int)]) =
data.type();
257 if(errCode == Z_MEM_ERROR)
259 UERROR(
"Z_MEM_ERROR : Insufficient memory.");
261 else if(errCode == Z_BUF_ERROR)
263 UERROR(
"Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
286 int height = *((
int*)&
bytes[
size-3*
sizeof(
int)]);
287 int width = *((
int*)&
bytes[
size-2*
sizeof(
int)]);
290 data = cv::Mat(height, width,
type);
291 uLongf totalUncompressed = uLongf(
data.total())*uLongf(
data.elemSize());
293 int errCode = uncompress(
299 if(errCode == Z_MEM_ERROR)
301 UERROR(
"Z_MEM_ERROR : Insufficient memory.");
303 else if(errCode == Z_BUF_ERROR)
305 UERROR(
"Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
307 else if(errCode == Z_DATA_ERROR)
309 UERROR(
"Z_DATA_ERROR : The compressed data (referenced by source) was corrupted.");
326 UASSERT(strMat.type() == CV_8SC1 && strMat.rows == 1);
327 return (
const char*)strMat.data;
346 std::vector<unsigned char> signature(maxlen);
347 memcpy(&signature[0],
bytes, maxlen);
348 if (std::string(signature.begin(), signature.end()) ==
"DEPTHRVL")