scenepic 1.1.0
3D Visualization Made Easy
compression.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#ifndef _SCENEPIC_COMPRESSION_H_
5#define _SCENEPIC_COMPRESSION_H_
6
7#include "matrix.h"
8#include "zip.h"
9
10#include <Eigen/Core>
11#include <cstdint>
12#include <vector>
13
14namespace scenepic
15{
20 template<typename Derived>
21 std::vector<std::uint8_t> compress_matrix(const Derived& matrix)
22 {
23 const std::uint8_t* source_buf =
24 reinterpret_cast<const std::uint8_t*>(matrix.data());
25 auto dest_len = sizeof(typename Derived::Scalar) * matrix.size();
26 std::vector<std::uint8_t> deflate_bytes = deflate(source_buf, dest_len);
27 dest_len = deflate_bytes.size();
28 deflate_bytes.resize(deflate_bytes.size() + 5);
29 std::uint32_t* rows_ptr =
30 reinterpret_cast<std::uint32_t*>(deflate_bytes.data() + dest_len);
31 *rows_ptr = static_cast<std::uint32_t>(matrix.rows());
32 deflate_bytes[dest_len + 4] = static_cast<std::uint8_t>(matrix.cols());
33
34 return deflate_bytes;
35 }
36
41 template<typename Derived>
42 Derived decompress_matrix(const std::vector<std::uint8_t>& buffer)
43 {
44 const std::uint32_t* rows_ptr =
45 reinterpret_cast<const std::uint32_t*>(buffer.data() + buffer.size() - 5);
46 Eigen::Index rows = *rows_ptr;
47 Eigen::Index cols = *buffer.rbegin();
48 std::vector<std::uint8_t> inflate_bytes = inflate(
49 buffer.data(),
50 buffer.size() - 5,
51 rows * cols * sizeof(typename Derived::Scalar));
52 Eigen::Map<Derived> matrix_map(
53 reinterpret_cast<typename Derived::Scalar*>(inflate_bytes.data()),
54 rows,
55 cols);
56
57 Derived matrix = matrix_map;
58 return matrix;
59 }
60} // namespace scenepic
61
62#endif
Definition: audio_track.h:14
std::vector< std::uint8_t > compress_matrix(const Derived &matrix)
Compress a matrix.
Definition: compression.h:21
Derived decompress_matrix(const std::vector< std::uint8_t > &buffer)
Decompress a matrix compressed by the "compress" method.
Definition: compression.h:42
std::vector< std::uint8_t > deflate(const std::uint8_t *data, std::size_t data_length)
Deflate a byte array.
std::vector< std::uint8_t > inflate(const std::uint8_t *data, std::size_t source_length, std::size_t dest_length)
Inflates a byte array created by.