compression.py
Go to the documentation of this file.
1 
2 import zlib
3 import struct
4 import numpy as np
5 
6 def compress(data):
7  assert data.ndim == 1 or data.ndim == 2
8 
9  dim1 = 1
10  if data.ndim == 1:
11  dim1 = 1
12  dim2 = len(data)
13  else:
14  dim1 = data.shape[0]
15  dim2 = data.shape[1]
16 
17  numpy_type_to_cvtype = {'uint8': 0, 'int8': 1, 'uint16': 2,
18  'int16': 3, 'int32': 4, 'float32': 5,
19  'float64': 6}
20 
21  compressed_data = bytearray(zlib.compress(data.tobytes()))
22  compressed_data.extend(struct.pack("iii", dim1, dim2, numpy_type_to_cvtype[data.dtype.name]))
23 
24  return compressed_data
25 
26 def uncompress(bytes):
27  cvtype_to_numpy_type = {0: 'uint8', 1: 'int8', 2: 'uint16',
28  3: 'int16', 4: 'int32', 5: 'float32',
29  6: 'float64'}
30  out = zlib.decompress(bytes[:len(bytes)-3*4])
31  rows, cols, datatype = struct.unpack_from("iii", bytes, offset=len(bytes)-3*4)
32  data = np.frombuffer(out, dtype=cvtype_to_numpy_type[datatype])
33  return data.reshape((rows, cols))
34 


rtabmap_ros
Author(s): Mathieu Labbe
autogenerated on Tue Jan 24 2023 04:04:40