Point Cloud Library (PCL)  1.7.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 #ifndef __PCL_IO_RANGECODING__
42 #define __PCL_IO_RANGECODING__
43 
44 #include <map>
45 #include <iostream>
46 #include <vector>
47 #include <string>
48 #include <cmath>
49 #include <algorithm>
50 #include <stdio.h>
51 #include <boost/cstdint.hpp>
52 
53 namespace pcl
54 {
55 
56  using boost::uint8_t;
57  using boost::uint32_t;
58  using boost::uint64_t;
59 
60  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61  /** \brief @b AdaptiveRangeCoder compression class
62  * \note This class provides adaptive range coding functionality.
63  * \note Its symbol probability/frequency table is adaptively updated during encoding
64  * \note
65  * \author Julius Kammerl (julius@kammerl.de)
66  */
67  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69  {
70 
71  public:
72 
73  /** \brief Empty constructor. */
74  AdaptiveRangeCoder () : outputCharVector_ ()
75  {
76  }
77 
78  /** \brief Empty deconstructor. */
79  virtual
81  {
82  }
83 
84  /** \brief Encode char vector to output stream
85  * \param inputByteVector_arg input vector
86  * \param outputByteStream_arg output stream containing compressed data
87  * \return amount of bytes written to output stream
88  */
89  unsigned long
90  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
91 
92  /** \brief Decode char stream to output vector
93  * \param inputByteStream_arg input stream of compressed data
94  * \param outputByteVector_arg decompressed output vector
95  * \return amount of bytes read from input stream
96  */
97  unsigned long
98  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
99 
100  protected:
101  typedef boost::uint32_t DWord; // 4 bytes
102 
103  private:
104  /** vector containing compressed data
105  */
106  std::vector<char> outputCharVector_;
107 
108  };
109 
110  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111  /** \brief @b StaticRangeCoder compression class
112  * \note This class provides static range coding functionality.
113  * \note Its symbol probability/frequency table is precomputed and encoded to the output stream
114  * \note
115  * \author Julius Kammerl (julius@kammerl.de)
116  */
117  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
119  {
120  public:
121  /** \brief Constructor. */
123  cFreqTable_ (65537), outputCharVector_ ()
124  {
125  }
126 
127  /** \brief Empty deconstructor. */
128  virtual
130  {
131  }
132 
133  /** \brief Encode integer vector to output stream
134  * \param[in] inputIntVector_arg input vector
135  * \param[out] outputByterStream_arg output stream containing compressed data
136  * \return amount of bytes written to output stream
137  */
138  unsigned long
139  encodeIntVectorToStream (std::vector<unsigned int>& inputIntVector_arg, std::ostream& outputByterStream_arg);
140 
141  /** \brief Decode stream to output integer vector
142  * \param inputByteStream_arg input stream of compressed data
143  * \param outputIntVector_arg decompressed output vector
144  * \return amount of bytes read from input stream
145  */
146  unsigned long
147  decodeStreamToIntVector (std::istream& inputByteStream_arg, std::vector<unsigned int>& outputIntVector_arg);
148 
149  /** \brief Encode char vector to output stream
150  * \param inputByteVector_arg input vector
151  * \param outputByteStream_arg output stream containing compressed data
152  * \return amount of bytes written to output stream
153  */
154  unsigned long
155  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
156 
157  /** \brief Decode char stream to output vector
158  * \param inputByteStream_arg input stream of compressed data
159  * \param outputByteVector_arg decompressed output vector
160  * \return amount of bytes read from input stream
161  */
162  unsigned long
163  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
164 
165  protected:
166  typedef boost::uint32_t DWord; // 4 bytes
167 
168  /** \brief Helper function to calculate the binary logarithm
169  * \param n_arg: some value
170  * \return binary logarithm (log2) of argument n_arg
171  */
172  inline double
173  Log2 (double n_arg)
174  {
175  return log (n_arg) / log (2.0);
176  }
177 
178  private:
179  /** \brief Vector containing cumulative symbol frequency table. */
180  std::vector<uint64_t> cFreqTable_;
181 
182  /** \brief Vector containing compressed data. */
183  std::vector<char> outputCharVector_;
184 
185  };
186 }
187 
188 
189 //#include "impl/entropy_range_coder.hpp"
190 
191 #endif
192