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);
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));
186 *((
int*)&
bytes[destLen+
sizeof(
int)]) =
data.cols;
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.");
248 int height = *((
int*)&
bytes[
size-3*
sizeof(
int)]);
249 int width = *((
int*)&
bytes[
size-2*
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.");
288 UASSERT(strMat.type() == CV_8SC1 && strMat.rows == 1);
289 return (
const char*)strMat.data;