1 /*
2 ===============================================================================
3 
4   FILE:  integercompressor.hpp
5 
6   CONTENTS:
7 
8     This compressor provides three different contexts for encoding integer
9     numbers whose range may lie anywhere between 1 and 31 bits, which is
10     specified with the SetPrecision function.
11 
12     The compressor encodes two things:
13 
14       (1) the number k of miss-predicted low-order bits and
15       (2) the k-bit number that corrects the missprediction
16 
17     The k-bit number is usually coded broken in two chunks. The highest
18     bits are compressed using an arithmetic range table. The lower bits
19     are stored raw without predicive coding. How many of the higher bits
20     are compressed can be specified with bits_high. The default is 8.
21 
22   PROGRAMMERS:
23 
24     martin.isenburg@rapidlasso.com  -  http://rapidlasso.com
25 
26   COPYRIGHT:
27 
28     (c) 2005-2014, martin isenburg, rapidlasso - fast tools to catch reality
29 
30     This is free software; you can redistribute and/or modify it under the
31     terms of the GNU Lesser General Licence as published by the Free Software
32     Foundation. See the COPYING file for more information.
33 
34     This software is distributed WITHOUT ANY WARRANTY and without even the
35     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 
37   CHANGE HISTORY:
38 
39      6 September 2014 -- removed inheritance of EntropyEncoder and EntropyDecoder
40     10 January 2011 -- licensing change for LGPL release and liblas integration
41     10 December 2010 -- unified for all entropy coders at Baeckerei Schaefer
42     31 October 2009 -- switched from the Rangecoder to the Entropycoder
43     30 September 2005 -- now splitting the corrector into raw and compressed bits
44     13 July 2005 -- created after returning with many mosquito bites from OBX
45 
46 ===============================================================================
47 */
48 #ifndef INTEGER_COMPRESSOR_HPP
49 #define INTEGER_COMPRESSOR_HPP
50 
51 #include "arithmeticencoder.hpp"
52 #include "arithmeticdecoder.hpp"
53 
54 class IntegerCompressor
55 {
56 public:
57 
58   // Constructor & Deconstructor
59   IntegerCompressor(ArithmeticEncoder* enc, U32 bits=16, U32 contexts=1, U32 bits_high=8, U32 range=0);
60   IntegerCompressor(ArithmeticDecoder* dec, U32 bits=16, U32 contexts=1, U32 bits_high=8, U32 range=0);
61   ~IntegerCompressor();
62 
63   // Manage Compressor
64   void initCompressor();
65   void compress(I32 iPred, I32 iReal, U32 context=0);
66 
67   // Manage Decompressor
68   void initDecompressor();
69   I32 decompress(I32 iPred, U32 context=0);
70 
71   // Get the k corrector bits from the last compress/decompress call
getK() const72   U32 getK() const {return k;};
73 
74 private:
75   void writeCorrector(I32 c, ArithmeticModel* model);
76   I32 readCorrector(ArithmeticModel* model);
77 
78   U32 k;
79 
80   U32 contexts;
81   U32 bits_high;
82 
83   U32 bits;
84   U32 range;
85 
86   U32 corr_bits;
87   U32 corr_range;
88   I32 corr_min;
89   I32 corr_max;
90 
91   ArithmeticEncoder* enc;
92   ArithmeticDecoder* dec;
93 
94   ArithmeticModel** mBits;
95 
96   ArithmeticModel** mCorrector;
97 
98 #ifdef CREATE_HISTOGRAMS
99   int** corr_histogram;
100 #endif
101 };
102 
103 #endif
104