31 #include <opencv2/opencv.hpp> 39 uncompressedData_(mat),
41 image_(!format.empty()),
44 UASSERT(format.empty() || format.compare(
".png") == 0 || format.compare(
".jpg") == 0);
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(format, bgra, bytes);
113 cv::imencode(format, image, bytes);
122 std::vector<unsigned char> bytes =
compressImage(image, format);
125 return cv::Mat(1, (
int)bytes.size(), CV_8UC1, bytes.data()).clone();
135 #if CV_MAJOR_VERSION>2 || (CV_MAJOR_VERSION >=2 && CV_MINOR_VERSION >=4) 136 image = cv::imdecode(bytes, cv::IMREAD_UNCHANGED);
138 image = cv::imdecode(bytes, -1);
140 if(image.type() == CV_8UC4)
144 cv::Mat depth(image.size(), CV_32FC1);
145 memcpy(depth.data, image.data, image.total()*image.elemSize());
157 #if CV_MAJOR_VERSION>2 || (CV_MAJOR_VERSION >=2 && CV_MINOR_VERSION >=4) 158 image = cv::imdecode(bytes, cv::IMREAD_UNCHANGED);
160 image = cv::imdecode(bytes, -1);
162 if(image.type() == CV_8UC4)
164 image = cv::Mat(image.size(), CV_32FC1, image.data).clone();
172 std::vector<unsigned char> bytes;
175 uLong sourceLen = uLong(data.total())*uLong(data.elemSize());
176 uLong destLen = compressBound(sourceLen);
177 bytes.resize(destLen);
178 int errCode = compress(
179 (Bytef *)bytes.data(),
181 (
const Bytef *)data.data,
184 bytes.resize(destLen+3*
sizeof(
int));
185 *((
int*)&bytes[destLen]) = data.rows;
186 *((
int*)&bytes[destLen+
sizeof(
int)]) = data.cols;
187 *((
int*)&bytes[destLen+2*
sizeof(int)]) = data.type();
189 if(errCode == Z_MEM_ERROR)
191 UERROR(
"Z_MEM_ERROR : Insufficient memory.");
193 else if(errCode == Z_BUF_ERROR)
195 UERROR(
"Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
206 uLong sourceLen = uLong(data.total())*uLong(data.elemSize());
207 uLong destLen = compressBound(sourceLen);
208 bytes = cv::Mat(1, destLen+3*
sizeof(
int), CV_8UC1);
209 int errCode = compress(
212 (
const Bytef *)data.data,
214 bytes = cv::Mat(bytes, cv::Rect(0,0, destLen+3*
sizeof(
int), 1));
215 *((
int*)&bytes.data[destLen]) = data.rows;
216 *((
int*)&bytes.data[destLen+
sizeof(
int)]) = data.cols;
217 *((
int*)&bytes.data[destLen+2*
sizeof(int)]) = data.type();
219 if(errCode == Z_MEM_ERROR)
221 UERROR(
"Z_MEM_ERROR : Insufficient memory.");
223 else if(errCode == Z_BUF_ERROR)
225 UERROR(
"Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
233 UASSERT(bytes.empty() || bytes.type() == CV_8UC1);
245 if(bytes && size>=3*
sizeof(
int))
248 int height = *((
int*)&bytes[size-3*
sizeof(
int)]);
249 int width = *((
int*)&bytes[size-2*
sizeof(
int)]);
250 int type = *((
int*)&bytes[size-1*
sizeof(
int)]);
252 data = cv::Mat(height, width, type);
253 uLongf totalUncompressed = uLongf(data.total())*uLongf(data.elemSize());
255 int errCode = uncompress(
261 if(errCode == Z_MEM_ERROR)
263 UERROR(
"Z_MEM_ERROR : Insufficient memory.");
265 else if(errCode == Z_BUF_ERROR)
267 UERROR(
"Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
269 else if(errCode == Z_DATA_ERROR)
271 UERROR(
"Z_DATA_ERROR : The compressed data (referenced by source) was corrupted.");
280 return compressData2(cv::Mat(1, str.size()+1, CV_8SC1, (
void *)str.data()));
288 UASSERT(strMat.type() == CV_8SC1 && strMat.rows == 1);
289 return (
const char*)strMat.data;
cv::Mat uncompressedData_
cv::Mat RTABMAP_EXP uncompressData(const cv::Mat &bytes)
GLM_FUNC_DECL genType e()
Some conversion functions.
#define UASSERT(condition)
std::vector< unsigned char > RTABMAP_EXP compressImage(const cv::Mat &image, const std::string &format=".png")
std::vector< unsigned char > RTABMAP_EXP compressData(const cv::Mat &data)
cv::Mat RTABMAP_EXP compressData2(const cv::Mat &data)
CompressionThread(const cv::Mat &mat, const std::string &format="")
cv::Mat RTABMAP_EXP compressString(const std::string &str)
ULogger class and convenient macros.
std::string RTABMAP_EXP uncompressString(const cv::Mat &bytes)
cv::Mat RTABMAP_EXP uncompressImage(const cv::Mat &bytes)
cv::Mat RTABMAP_EXP compressImage2(const cv::Mat &image, const std::string &format=".png")