Compression.cpp
Go to the documentation of this file.
1 /*
2  * Compression.cpp
3  *
4  * Created on: Sep 10, 2018
5  * Author: labm2414
6  */
7 
8 #include <Compression.h>
9 #include <zlib.h>
11 
12 namespace find_object {
13 
14 std::vector<unsigned char> compressData(const cv::Mat & data)
15 {
16  std::vector<unsigned char> bytes;
17  if(!data.empty())
18  {
19  uLong sourceLen = uLong(data.total())*uLong(data.elemSize());
20  uLong destLen = compressBound(sourceLen);
21  bytes.resize(destLen);
22  int errCode = compress(
23  (Bytef *)bytes.data(),
24  &destLen,
25  (const Bytef *)data.data,
26  sourceLen);
27 
28  bytes.resize(destLen+3*sizeof(int));
29  *((int*)&bytes[destLen]) = data.rows;
30  *((int*)&bytes[destLen+sizeof(int)]) = data.cols;
31  *((int*)&bytes[destLen+2*sizeof(int)]) = data.type();
32 
33  if(errCode == Z_MEM_ERROR)
34  {
35  UERROR("Z_MEM_ERROR : Insufficient memory.");
36  }
37  else if(errCode == Z_BUF_ERROR)
38  {
39  UERROR("Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
40  }
41  }
42  return bytes;
43 }
44 
45 cv::Mat uncompressData(const unsigned char * bytes, unsigned long size)
46 {
47  cv::Mat data;
48  if(bytes && size>=3*sizeof(int))
49  {
50  //last 3 int elements are matrix size and type
51  int height = *((int*)&bytes[size-3*sizeof(int)]);
52  int width = *((int*)&bytes[size-2*sizeof(int)]);
53  int type = *((int*)&bytes[size-1*sizeof(int)]);
54 
55  data = cv::Mat(height, width, type);
56  uLongf totalUncompressed = uLongf(data.total())*uLongf(data.elemSize());
57 
58  int errCode = uncompress(
59  (Bytef*)data.data,
60  &totalUncompressed,
61  (const Bytef*)bytes,
62  uLong(size));
63 
64  if(errCode == Z_MEM_ERROR)
65  {
66  UERROR("Z_MEM_ERROR : Insufficient memory.");
67  }
68  else if(errCode == Z_BUF_ERROR)
69  {
70  UERROR("Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
71  }
72  else if(errCode == Z_DATA_ERROR)
73  {
74  UERROR("Z_DATA_ERROR : The compressed data (referenced by source) was corrupted.");
75  }
76  }
77  return data;
78 }
79 } /* namespace find_object */
cv::Mat uncompressData(const unsigned char *bytes, unsigned long size)
Definition: Compression.cpp:45
std::vector< unsigned char > compressData(const cv::Mat &data)
Definition: Compression.cpp:14
#define UERROR(...)
ULogger class and convenient macros.
def compress(data)


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:09