Point Cloud Library (PCL)  1.11.1
entropy_range_coder.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Range Coder based on Dmitry Subbotin's carry-less implementation (http://www.compression.ru/ds/)
36  * Added optimized symbol lookup and added implementation for static range coding (uses fixed precomputed frequency table)
37  *
38  * Author: Julius Kammerl (julius@kammerl.de)
39  */
40 
41 #pragma once
42 
43 #include <map>
44 #include <iostream>
45 #include <vector>
46 #include <string>
47 #include <cmath>
48 #include <algorithm>
49 #include <cstdio>
50 #include <cstdint>
51 
52 #include <pcl/pcl_macros.h>
53 
54 namespace pcl
55 {
56 
57  using std::uint8_t;
58  using std::uint32_t;
59  using std::uint64_t;
60 
61  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62  /** \brief @b AdaptiveRangeCoder compression class
63  * \note This class provides adaptive range coding functionality.
64  * \note Its symbol probability/frequency table is adaptively updated during encoding
65  * \note
66  * \author Julius Kammerl (julius@kammerl.de)
67  */
68  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70  {
71 
72  public:
73 
74  /** \brief Empty constructor. */
76  {
77  }
78 
79  /** \brief Empty deconstructor. */
80  virtual
82  {
83  }
84 
85  /** \brief Encode char vector to output stream
86  * \param inputByteVector_arg input vector
87  * \param outputByteStream_arg output stream containing compressed data
88  * \return amount of bytes written to output stream
89  */
90  unsigned long
91  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
92 
93  /** \brief Decode char stream to output vector
94  * \param inputByteStream_arg input stream of compressed data
95  * \param outputByteVector_arg decompressed output vector
96  * \return amount of bytes read from input stream
97  */
98  unsigned long
99  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
100 
101  protected:
102  using DWord = std::uint32_t; // 4 bytes
103 
104  private:
105  /** vector containing compressed data
106  */
107  std::vector<char> outputCharVector_;
108 
109  };
110 
111  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112  /** \brief @b StaticRangeCoder compression class
113  * \note This class provides static range coding functionality.
114  * \note Its symbol probability/frequency table is precomputed and encoded to the output stream
115  * \note
116  * \author Julius Kammerl (julius@kammerl.de)
117  */
118  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120  {
121  public:
122  /** \brief Constructor. */
124  cFreqTable_ (65537)
125  {
126  }
127 
128  /** \brief Empty deconstructor. */
129  virtual
131  {
132  }
133 
134  /** \brief Encode integer vector to output stream
135  * \param[in] inputIntVector_arg input vector
136  * \param[out] outputByterStream_arg output stream containing compressed data
137  * \return amount of bytes written to output stream
138  */
139  unsigned long
140  encodeIntVectorToStream (std::vector<unsigned int>& inputIntVector_arg, std::ostream& outputByterStream_arg);
141 
142  /** \brief Decode stream to output integer vector
143  * \param inputByteStream_arg input stream of compressed data
144  * \param outputIntVector_arg decompressed output vector
145  * \return amount of bytes read from input stream
146  */
147  unsigned long
148  decodeStreamToIntVector (std::istream& inputByteStream_arg, std::vector<unsigned int>& outputIntVector_arg);
149 
150  /** \brief Encode char vector to output stream
151  * \param inputByteVector_arg input vector
152  * \param outputByteStream_arg output stream containing compressed data
153  * \return amount of bytes written to output stream
154  */
155  unsigned long
156  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
157 
158  /** \brief Decode char stream to output vector
159  * \param inputByteStream_arg input stream of compressed data
160  * \param outputByteVector_arg decompressed output vector
161  * \return amount of bytes read from input stream
162  */
163  unsigned long
164  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
165 
166  protected:
167  using DWord = std::uint32_t; // 4 bytes
168 
169  /** \brief Helper function to calculate the binary logarithm
170  * \param n_arg: some value
171  * \return binary logarithm (log2) of argument n_arg
172  */
173  PCL_DEPRECATED(1, 12, "use std::log2 instead")
174  inline double
175  Log2 (double n_arg)
176  {
177  return std::log2 (n_arg);
178  }
179 
180  private:
181  /** \brief Vector containing cumulative symbol frequency table. */
182  std::vector<std::uint64_t> cFreqTable_;
183 
184  /** \brief Vector containing compressed data. */
185  std::vector<char> outputCharVector_;
186 
187  };
188 }
189 
190 
191 //#include "impl/entropy_range_coder.hpp"
std::uint64_t uint64_t
Definition: types.h:60
std::uint32_t uint32_t
Definition: types.h:58
AdaptiveRangeCoder compression class
unsigned long encodeIntVectorToStream(std::vector< unsigned int > &inputIntVector_arg, std::ostream &outputByterStream_arg)
Encode integer vector to output stream.
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
StaticRangeCoder()
Constructor.
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
StaticRangeCoder compression class
unsigned long decodeStreamToIntVector(std::istream &inputByteStream_arg, std::vector< unsigned int > &outputIntVector_arg)
Decode stream to output integer vector.
std::uint8_t uint8_t
Definition: types.h:54
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
#define PCL_DEPRECATED(Major, Minor, Message)
macro for compatibility across compilers and help remove old deprecated items for the Major...
Definition: pcl_macros.h:147
virtual ~StaticRangeCoder()
Empty deconstructor.
virtual ~AdaptiveRangeCoder()
Empty deconstructor.
AdaptiveRangeCoder()
Empty constructor.
Defines all the PCL and non-PCL macros used.
double Log2(double n_arg)
Helper function to calculate the binary logarithm.