Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/io/include/pcl/compression/entropy_range_coder.h
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  *
00035  * Range Coder based on Dmitry Subbotin's carry-less implementation (http://www.compression.ru/ds/)
00036  * Added optimized symbol lookup and added implementation for static range coding (uses fixed precomputed frequency table)
00037  *
00038  * Author: Julius Kammerl (julius@kammerl.de)
00039  */
00040 
00041 #ifndef __PCL_IO_RANGECODING__
00042 #define __PCL_IO_RANGECODING__
00043 
00044 #include <map>
00045 #include <iostream>
00046 #include <vector>
00047 #include <string>
00048 #include <cmath>
00049 #include <algorithm>
00050 #include <stdio.h>
00051 #include <boost/cstdint.hpp>
00052 
00053 namespace pcl
00054 {
00055 
00056   using boost::uint8_t;
00057   using boost::uint32_t;
00058   using boost::uint64_t;
00059 
00060   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00061   /** \brief @b AdaptiveRangeCoder compression class
00062    *  \note This class provides adaptive range coding functionality.
00063    *  \note Its symbol probability/frequency table is adaptively updated during encoding
00064    *  \note
00065    *  \author Julius Kammerl (julius@kammerl.de)
00066    */
00067   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00068   class AdaptiveRangeCoder
00069   {
00070 
00071   public:
00072 
00073     /** \brief Empty constructor. */
00074     AdaptiveRangeCoder () : outputCharVector_ ()
00075     {
00076     }
00077 
00078     /** \brief Empty deconstructor. */
00079     virtual
00080     ~AdaptiveRangeCoder ()
00081     {
00082     }
00083 
00084     /** \brief Encode char vector to output stream
00085      * \param inputByteVector_arg input vector
00086      * \param outputByteStream_arg output stream containing compressed data
00087      * \return amount of bytes written to output stream
00088      */
00089     unsigned long
00090     encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
00091 
00092     /** \brief Decode char stream to output vector
00093      * \param inputByteStream_arg input stream of compressed data
00094      * \param outputByteVector_arg decompressed output vector
00095      * \return amount of bytes read from input stream
00096      */
00097     unsigned long
00098     decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
00099 
00100   protected:
00101     typedef boost::uint32_t DWord; // 4 bytes
00102 
00103   private:
00104     /** vector containing compressed data
00105      */
00106     std::vector<char> outputCharVector_;
00107 
00108   };
00109 
00110   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00111   /** \brief @b StaticRangeCoder compression class
00112    *  \note This class provides static range coding functionality.
00113    *  \note Its symbol probability/frequency table is precomputed and encoded to the output stream
00114    *  \note
00115    *  \author Julius Kammerl (julius@kammerl.de)
00116    */
00117   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00118   class StaticRangeCoder
00119   {
00120     public:
00121       /** \brief Constructor. */
00122       StaticRangeCoder () :
00123         cFreqTable_ (65537), outputCharVector_ ()
00124       {
00125       }
00126 
00127       /** \brief Empty deconstructor. */
00128       virtual
00129       ~StaticRangeCoder ()
00130       {
00131       }
00132 
00133       /** \brief Encode integer vector to output stream
00134         * \param[in] inputIntVector_arg input vector
00135         * \param[out] outputByterStream_arg output stream containing compressed data
00136         * \return amount of bytes written to output stream
00137         */
00138       unsigned long
00139       encodeIntVectorToStream (std::vector<unsigned int>& inputIntVector_arg, std::ostream& outputByterStream_arg);
00140 
00141       /** \brief Decode stream to output integer vector
00142        * \param inputByteStream_arg input stream of compressed data
00143        * \param outputIntVector_arg decompressed output vector
00144        * \return amount of bytes read from input stream
00145        */
00146       unsigned long
00147       decodeStreamToIntVector (std::istream& inputByteStream_arg, std::vector<unsigned int>& outputIntVector_arg);
00148 
00149       /** \brief Encode char vector to output stream
00150        * \param inputByteVector_arg input vector
00151        * \param outputByteStream_arg output stream containing compressed data
00152        * \return amount of bytes written to output stream
00153        */
00154       unsigned long
00155       encodeCharVectorToStream (const std::vector<char>& inputByteVector_arg, std::ostream& outputByteStream_arg);
00156 
00157       /** \brief Decode char stream to output vector
00158        * \param inputByteStream_arg input stream of compressed data
00159        * \param outputByteVector_arg decompressed output vector
00160        * \return amount of bytes read from input stream
00161        */
00162       unsigned long
00163       decodeStreamToCharVector (std::istream& inputByteStream_arg, std::vector<char>& outputByteVector_arg);
00164 
00165     protected:
00166       typedef boost::uint32_t DWord; // 4 bytes
00167 
00168       /** \brief Helper function to calculate the binary logarithm
00169        * \param n_arg: some value
00170        * \return binary logarithm (log2) of argument n_arg
00171        */
00172       inline double
00173       Log2 (double n_arg)
00174       {
00175         return log (n_arg) / log (2.0);
00176       }
00177 
00178     private:
00179       /** \brief Vector containing cumulative symbol frequency table. */
00180       std::vector<uint64_t> cFreqTable_;
00181 
00182       /** \brief Vector containing compressed data. */
00183       std::vector<char> outputCharVector_;
00184 
00185   };
00186 }
00187 
00188 
00189 //#include "impl/entropy_range_coder.hpp"
00190 
00191 #endif
00192