Point Cloud Library (PCL)  1.14.1-dev
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 <iostream>
44 #include <vector>
45 #include <cmath>
46 #include <cstdint>
47 
48 #include <pcl/pcl_macros.h>
49 
50 namespace pcl
51 {
52 
53  using std::uint8_t;
54  using std::uint32_t;
55  using std::uint64_t;
56 
57  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
58  /** \brief @b AdaptiveRangeCoder compression class
59  * \note This class provides adaptive range coding functionality.
60  * \note Its symbol probability/frequency table is adaptively updated during encoding
61  * \note
62  * \author Julius Kammerl (julius@kammerl.de)
63  */
64  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66  {
67 
68  public:
69 
70  /** \brief Empty constructor. */
71  AdaptiveRangeCoder () = default;
72 
73  /** \brief Empty deconstructor. */
74  virtual
75  ~AdaptiveRangeCoder () = default;
76 
77  /** \brief Encode char vector to output stream
78  * \param inputByteVector_arg input vector
79  * \param outputByteStream_arg output stream containing compressed data
80  * \return amount of bytes written to output stream
81  */
82  unsigned long
83  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
84 
85  /** \brief Decode char stream to output vector
86  * \param inputByteStream_arg input stream of compressed data
87  * \param outputByteVector_arg decompressed output vector
88  * \return amount of bytes read from input stream
89  */
90  unsigned long
91  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
92 
93  protected:
94  using DWord = std::uint32_t; // 4 bytes
95 
96  private:
97  /** vector containing compressed data
98  */
99  std::vector<char> outputCharVector_;
100 
101  };
102 
103  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104  /** \brief @b StaticRangeCoder compression class
105  * \note This class provides static range coding functionality.
106  * \note Its symbol probability/frequency table is precomputed and encoded to the output stream
107  * \note
108  * \author Julius Kammerl (julius@kammerl.de)
109  */
110  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112  {
113  public:
114  /** \brief Constructor. */
116  cFreqTable_ (65537)
117  {
118  }
119 
120  /** \brief Empty deconstructor. */
121  virtual
122  ~StaticRangeCoder () = default;
123 
124  /** \brief Encode integer vector to output stream
125  * \param[in] inputIntVector_arg input vector
126  * \param[out] outputByterStream_arg output stream containing compressed data
127  * \return amount of bytes written to output stream
128  */
129  unsigned long
130  encodeIntVectorToStream (std::vector<unsigned int>& inputIntVector_arg, std::ostream& outputByterStream_arg);
131 
132  /** \brief Decode stream to output integer vector
133  * \param inputByteStream_arg input stream of compressed data
134  * \param outputIntVector_arg decompressed output vector
135  * \return amount of bytes read from input stream
136  */
137  unsigned long
138  decodeStreamToIntVector (std::istream& inputByteStream_arg, std::vector<unsigned int>& outputIntVector_arg);
139 
140  /** \brief Encode char vector to output stream
141  * \param inputByteVector_arg input vector
142  * \param outputByteStream_arg output stream containing compressed data
143  * \return amount of bytes written to output stream
144  */
145  unsigned long
146  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
147 
148  /** \brief Decode char stream to output vector
149  * \param inputByteStream_arg input stream of compressed data
150  * \param outputByteVector_arg decompressed output vector
151  * \return amount of bytes read from input stream
152  */
153  unsigned long
154  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
155 
156  protected:
157  using DWord = std::uint32_t; // 4 bytes
158 
159  private:
160  /** \brief Vector containing cumulative symbol frequency table. */
161  std::vector<std::uint64_t> cFreqTable_;
162 
163  /** \brief Vector containing compressed data. */
164  std::vector<char> outputCharVector_;
165 
166  };
167 }
168 
169 
170 //#include "impl/entropy_range_coder.hpp"
AdaptiveRangeCoder compression class
virtual ~AdaptiveRangeCoder()=default
Empty deconstructor.
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
AdaptiveRangeCoder()=default
Empty constructor.
StaticRangeCoder compression class
virtual ~StaticRangeCoder()=default
Empty deconstructor.
unsigned long decodeStreamToIntVector(std::istream &inputByteStream_arg, std::vector< unsigned int > &outputIntVector_arg)
Decode stream to output integer vector.
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.
unsigned long encodeIntVectorToStream(std::vector< unsigned int > &inputIntVector_arg, std::ostream &outputByterStream_arg)
Encode integer vector to output stream.
Defines all the PCL and non-PCL macros used.