1 // Copyright (C) 2004 Davis E. King (davis@dlib.net) 2 // License: Boost Software License See LICENSE.txt for the full license. 3 #ifndef DLIB_ENTROPY_ENCODER_KERNEl_2_ 4 #define DLIB_ENTROPY_ENCODER_KERNEl_2_ 5 6 #include "../algs.h" 7 #include "entropy_encoder_kernel_abstract.h" 8 #include <iosfwd> 9 #include "../uintn.h" 10 11 namespace dlib 12 { 13 14 class entropy_encoder_kernel_2 15 { 16 /*! 17 GENERAL NOTES 18 this encoder is implemented using "range" coding 19 20 INITIAL VALUE 21 out == 0 22 initial_low == 0x00000001 (slightly more than zero) 23 initial_high == 0xffffffff (slightly less than one, 0.99999999976717) 24 low == initial_low 25 high == initial_high 26 27 CONVENTION 28 if (out != 0) 29 *out == get_stream() 30 true == stream_is_set() 31 streambuf == out->rdbuf() 32 else 33 false == stream_is_set() 34 35 36 low == the low end of the range used for arithmetic encoding. 37 this number is used as a 32bit fixed point real number. 38 the point is fixed just before the first bit, so it is 39 always in the range [0,1) 40 41 low is also never allowed to be zero to avoid overflow 42 in the calculation (high-low+1)/total. 43 44 high == the high end of the range - 1 used for arithmetic encoding. 45 this number is used as a 32bit fixed point real number. 46 the point is fixed just before the first bit, so when we 47 interpret high as a real number then it is always in the 48 range [0,1) 49 50 the range for arithmetic encoding is always 51 [low,high + 0.9999999...) the 0.9999999... is why 52 high == real upper range - 1 53 !*/ 54 55 public: 56 57 entropy_encoder_kernel_2 ( 58 ); 59 60 virtual ~entropy_encoder_kernel_2 ( 61 ); 62 63 void clear( 64 ); 65 66 void set_stream ( 67 std::ostream& out 68 ); 69 70 bool stream_is_set ( 71 ) const; 72 73 std::ostream& get_stream ( 74 ) const; 75 76 void encode ( 77 uint32 low_count, 78 uint32 high_count, 79 uint32 total 80 ); 81 82 private: 83 84 void flush ( 85 ); 86 /*! 87 requires 88 out != 0 (i.e. there is a stream object to flush the data to 89 !*/ 90 91 // restricted functions 92 entropy_encoder_kernel_2(entropy_encoder_kernel_2&); // copy constructor 93 entropy_encoder_kernel_2& operator=(entropy_encoder_kernel_2&); // assignment operator 94 95 // data members 96 const uint32 initial_low; 97 const uint32 initial_high; 98 std::ostream* out; 99 uint32 low; 100 uint32 high; 101 std::streambuf* streambuf; 102 103 }; 104 105 } 106 107 #ifdef NO_MAKEFILE 108 #include "entropy_encoder_kernel_2.cpp" 109 #endif 110 111 #endif // DLIB_ENTROPY_ENCODER_KERNEl_2_ 112 113