Point Cloud Library (PCL)  1.10.0
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 namespace pcl
53 {
54 
55  using std::uint8_t;
56  using std::uint32_t;
57  using std::uint64_t;
58 
59  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60  /** \brief @b AdaptiveRangeCoder compression class
61  * \note This class provides adaptive range coding functionality.
62  * \note Its symbol probability/frequency table is adaptively updated during encoding
63  * \note
64  * \author Julius Kammerl (julius@kammerl.de)
65  */
66  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68  {
69 
70  public:
71 
72  /** \brief Empty constructor. */
74  {
75  }
76 
77  /** \brief Empty deconstructor. */
78  virtual
80  {
81  }
82 
83  /** \brief Encode char vector to output stream
84  * \param inputByteVector_arg input vector
85  * \param outputByteStream_arg output stream containing compressed data
86  * \return amount of bytes written to output stream
87  */
88  unsigned long
89  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
90 
91  /** \brief Decode char stream to output vector
92  * \param inputByteStream_arg input stream of compressed data
93  * \param outputByteVector_arg decompressed output vector
94  * \return amount of bytes read from input stream
95  */
96  unsigned long
97  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
98 
99  protected:
100  using DWord = std::uint32_t; // 4 bytes
101 
102  private:
103  /** vector containing compressed data
104  */
105  std::vector<char> outputCharVector_;
106 
107  };
108 
109  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110  /** \brief @b StaticRangeCoder compression class
111  * \note This class provides static range coding functionality.
112  * \note Its symbol probability/frequency table is precomputed and encoded to the output stream
113  * \note
114  * \author Julius Kammerl (julius@kammerl.de)
115  */
116  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
118  {
119  public:
120  /** \brief Constructor. */
122  cFreqTable_ (65537)
123  {
124  }
125 
126  /** \brief Empty deconstructor. */
127  virtual
129  {
130  }
131 
132  /** \brief Encode integer vector to output stream
133  * \param[in] inputIntVector_arg input vector
134  * \param[out] outputByterStream_arg output stream containing compressed data
135  * \return amount of bytes written to output stream
136  */
137  unsigned long
138  encodeIntVectorToStream (std::vector<unsigned int>& inputIntVector_arg, std::ostream& outputByterStream_arg);
139 
140  /** \brief Decode stream to output integer vector
141  * \param inputByteStream_arg input stream of compressed data
142  * \param outputIntVector_arg decompressed output vector
143  * \return amount of bytes read from input stream
144  */
145  unsigned long
146  decodeStreamToIntVector (std::istream& inputByteStream_arg, std::vector<unsigned int>& outputIntVector_arg);
147 
148  /** \brief Encode char vector to output stream
149  * \param inputByteVector_arg input vector
150  * \param outputByteStream_arg output stream containing compressed data
151  * \return amount of bytes written to output stream
152  */
153  unsigned long
154  encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
155 
156  /** \brief Decode char stream to output vector
157  * \param inputByteStream_arg input stream of compressed data
158  * \param outputByteVector_arg decompressed output vector
159  * \return amount of bytes read from input stream
160  */
161  unsigned long
162  decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
163 
164  protected:
165  using DWord = std::uint32_t; // 4 bytes
166 
167  /** \brief Helper function to calculate the binary logarithm
168  * \param n_arg: some value
169  * \return binary logarithm (log2) of argument n_arg
170  */
171  [[deprecated("use std::log2 instead")]]
172  inline double
173  Log2 (double n_arg)
174  {
175  return std::log2 (n_arg);
176  }
177 
178  private:
179  /** \brief Vector containing cumulative symbol frequency table. */
180  std::vector<std::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"
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::StaticRangeCoder
StaticRangeCoder compression class
Definition: entropy_range_coder.h:117
pcl::uint32_t
std::uint32_t uint32_t
Definition: pcl_macros.h:96
pcl::StaticRangeCoder::~StaticRangeCoder
virtual ~StaticRangeCoder()
Empty deconstructor.
Definition: entropy_range_coder.h:128
pcl::AdaptiveRangeCoder::DWord
std::uint32_t DWord
Definition: entropy_range_coder.h:100
pcl::StaticRangeCoder::decodeStreamToIntVector
unsigned long decodeStreamToIntVector(std::istream &inputByteStream_arg, std::vector< unsigned int > &outputIntVector_arg)
Decode stream to output integer vector.
Definition: entropy_range_coder.hpp:356
pcl::AdaptiveRangeCoder::encodeCharVectorToStream
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
Definition: entropy_range_coder.hpp:55
pcl::StaticRangeCoder::DWord
std::uint32_t DWord
Definition: entropy_range_coder.h:165
pcl::StaticRangeCoder::Log2
double Log2(double n_arg)
Helper function to calculate the binary logarithm.
Definition: entropy_range_coder.h:173
pcl::AdaptiveRangeCoder::~AdaptiveRangeCoder
virtual ~AdaptiveRangeCoder()
Empty deconstructor.
Definition: entropy_range_coder.h:79
pcl::StaticRangeCoder::decodeStreamToCharVector
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
Definition: entropy_range_coder.hpp:545
pcl::StaticRangeCoder::StaticRangeCoder
StaticRangeCoder()
Constructor.
Definition: entropy_range_coder.h:121
pcl::uint8_t
std::uint8_t uint8_t
Definition: pcl_macros.h:92
pcl::AdaptiveRangeCoder
AdaptiveRangeCoder compression class
Definition: entropy_range_coder.h:67
pcl::AdaptiveRangeCoder::decodeStreamToCharVector
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
Definition: entropy_range_coder.hpp:133
pcl::AdaptiveRangeCoder::AdaptiveRangeCoder
AdaptiveRangeCoder()
Empty constructor.
Definition: entropy_range_coder.h:73
pcl::StaticRangeCoder::encodeIntVectorToStream
unsigned long encodeIntVectorToStream(std::vector< unsigned int > &inputIntVector_arg, std::ostream &outputByterStream_arg)
Encode integer vector to output stream.
Definition: entropy_range_coder.hpp:225
pcl::StaticRangeCoder::encodeCharVectorToStream
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
Definition: entropy_range_coder.hpp:446
pcl::uint64_t
std::uint64_t uint64_t
Definition: pcl_macros.h:98