Debian Codegraph @

src/LercLib/Huffman.h

raw symbols SCIP
1 /*
2 Copyright 2015 - 2026 Esri
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 A local copy of the license and additional notices are located with the
17 source distribution at:
18
19 http://github.com/Esri/lerc/
20
21 Contributors: Thomas Maurer
22 */
23
24 #ifndef HUFFMAN_H
25 #define HUFFMAN_Hcxx . . $ `src/LercLib/Huffman.h:25:9`!
26
27 #include <vector>
28 #include <cstring>
29 #include <utility>
30 #include "Defines.h"cxx . . $ `<file>/src/LercLib/Defines.h`/
31
32 NAMESPACE_LERC_STARTcxx . . $ `src/LercLib/Defines.h:35:9`!
33
34 class Huffmancxx . . $ LercNS/Huffman#
35 {
36 public:
37 Huffmancxx . . $ LercNS/Huffman#Huffman(49f6e7a06ebc5aa8).() : m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.(1 << 15), m_maxNumBitsLUTcxx . . $ LercNS/Huffman#m_maxNumBitsLUT.(12), m_numBitsToSkipInTreecxx . . $ LercNS/Huffman#m_numBitsToSkipInTree.(0), m_rootcxx . . $ LercNS/Huffman#m_root.(nullptr) {}
38 ~cxx . . $ LercNS/Huffman#`~Huffman`(ced63f7c635d850d).Huffmancxx . . $ LercNS/Huffman#() { Clearcxx . . $ LercNS/Huffman#Clear(49f6e7a06ebc5aa8).(); }
39
40 // Limitation: We limit the max Huffman code length to 32 bit. If this happens, the function ComputeCodes()
41 // returns false. In that case don't use Huffman coding but Lerc only instead.
42 // This won't happen easily. For the worst case input maximizing the Huffman code length the counts in the
43 // histogram have to follow the Fibonacci sequence. Even then, for < 9,227,465 data values, 32 bit is
44 // the max Huffman code length possible.
45
46 bool ComputeCodescxx . . $ LercNS/Huffman#ComputeCodes(13834f6f38354517).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<int>& histo); // input histogram, size < 2^15
47
48 bool ComputeCompressedSizecxx . . $ LercNS/Huffman#ComputeCompressedSize(aaf3cc3253c7f648).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<int>& histo, int& numBytes, double& avgBpp) const;
49
50 // LUT of same size as histogram, each entry has length of Huffman bit code, and the bit code
51 const stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<unsigned short, unsigned int> >& GetCodescxx . . $ LercNS/Huffman#GetCodes(b040d847b6c149f9).() const { return m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.; }
52 bool SetCodescxx . . $ LercNS/Huffman#SetCodes(8994584b4c5a0cd).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<unsigned short, unsigned int> >& codeTable);
53
54 bool WriteCodeTablecxx . . $ LercNS/Huffman#WriteCodeTable(18073134a042c744).(Bytecxx . . $ LercNS/Byte#** ppByte, int lerc2Version) const;
55 bool ReadCodeTablecxx . . $ LercNS/Huffman#ReadCodeTable(93c08913fdc74d09).(const Bytecxx . . $ LercNS/Byte#** ppByte, size_t& nBytesRemaining, int lerc2Version);
56
57 bool BuildTreeFromCodescxx . . $ LercNS/Huffman#BuildTreeFromCodes(30ce46cb091eb21e).(int& numBitsLUT);
58 inline bool DecodeOneValuecxx . . $ LercNS/Huffman#DecodeOneValue(6cf2a5655d1a64b2).(const Bytecxx . . $ LercNS/Byte#** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const;
59 inline static bool PushValuecxx . . $ LercNS/Huffman#PushValue(a42e7bfdfc4e36d8).(Bytecxx . . $ LercNS/Byte#** ppByte, int& bitPos, unsigned int value, int len);
60 void Clearcxx . . $ LercNS/Huffman#Clear(49f6e7a06ebc5aa8).();
61
62 private:
63
64 struct Nodecxx . . $ LercNS/Huffman#Node#
65 {
66 int weightcxx . . $ LercNS/Huffman#Node#weight.;
67 short valuecxx . . $ LercNS/Huffman#Node#value.;
68 Nodecxx . . $ LercNS/Huffman#Node# *child0cxx . . $ LercNS/Huffman#Node#child0., *child1cxx . . $ LercNS/Huffman#Node#child1.;
69
70 Nodecxx . . $ LercNS/Huffman#Node#Node(59a23d33d43e6eac).(short val, int cnt) // new leaf node for val
71 {
72 valuecxx . . $ LercNS/Huffman#Node#value. = val;
73 weightcxx . . $ LercNS/Huffman#Node#weight. = -cnt;
74 child0cxx . . $ LercNS/Huffman#Node#child0. = child1cxx . . $ LercNS/Huffman#Node#child1. = nullptr;
75 }
76
77 Nodecxx . . $ LercNS/Huffman#Node#Node(9ff489d7b1020b6a).(Nodecxx . . $ LercNS/Huffman#Node#* c0, Nodecxx . . $ LercNS/Huffman#Node#* c1) // new internal node from children c0 and c1
78 {
79 valuecxx . . $ LercNS/Huffman#Node#value. = -1;
80 weightcxx . . $ LercNS/Huffman#Node#weight. = c0->weightcxx . . $ LercNS/Huffman#Node#weight. + c1->weightcxx . . $ LercNS/Huffman#Node#weight.;
81 child0cxx . . $ LercNS/Huffman#Node#child0. = c0;
82 child1cxx . . $ LercNS/Huffman#Node#child1. = c1;
83 }
84
85 bool operatorcxx . . $ LercNS/Huffman#Node#`operator<`(8a5d416830facd92). < (const Nodecxx . . $ LercNS/Huffman#Node#& other) const { return weightcxx . . $ LercNS/Huffman#Node#weight. < other.weightcxx . . $ LercNS/Huffman#Node#weight.; }
86
87 bool TreeToLUTcxx . . $ LercNS/Huffman#Node#TreeToLUT(658b69b2a4ed5c4d).(unsigned short numBits, unsigned int bits, stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<unsigned short, unsigned int> >& luTable) const
88 {
89 if (child0cxx . . $ LercNS/Huffman#Node#child0.)
90 {
91 if (numBits == 32 // the max huffman code length we allow
92 || !child0cxx . . $ LercNS/Huffman#Node#child0.->TreeToLUTcxx . . $ LercNS/Huffman#Node#TreeToLUT(658b69b2a4ed5c4d).(numBits + 1, (bits << 1) + 0, luTable)
93 || !child1cxx . . $ LercNS/Huffman#Node#child1.->TreeToLUTcxx . . $ LercNS/Huffman#Node#TreeToLUT(658b69b2a4ed5c4d).(numBits + 1, (bits << 1) + 1, luTable))
94 {
95 return false;
96 }
97 }
98 else
99 luTable[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).valuecxx . . $ LercNS/Huffman#Node#value.] =cxx . . $ std/pair#`operator=`(225bd4d59bf12f87). stdcxx . . $ std/pair#pair(14d64bafa7379ebc).::paircxx . . $ std/pair#<unsigned short, unsigned int>(numBits, bits);
100
101 return true;
102 }
103
104 void FreeTreecxx . . $ LercNS/Huffman#Node#FreeTree(2afed6f74e8a6d90).(int& n)
105 {
106 if (child0cxx . . $ LercNS/Huffman#Node#child0.)
107 {
108 child0cxx . . $ LercNS/Huffman#Node#child0.->FreeTreecxx . . $ LercNS/Huffman#Node#FreeTree(2afed6f74e8a6d90).(n);
109 delete child0cxx . . $ LercNS/Huffman#Node#child0.;
110 child0cxx . . $ LercNS/Huffman#Node#child0. = nullptr;
111 n--;
112 }
113 if (child1cxx . . $ LercNS/Huffman#Node#child1.)
114 {
115 child1cxx . . $ LercNS/Huffman#Node#child1.->FreeTreecxx . . $ LercNS/Huffman#Node#FreeTree(2afed6f74e8a6d90).(n);
116 delete child1cxx . . $ LercNS/Huffman#Node#child1.;
117 child1cxx . . $ LercNS/Huffman#Node#child1. = nullptr;
118 n--;
119 }
120 }
121 };
122
123 private:
124
125 size_t m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.;
126 stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<unsigned short, unsigned int> > m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.;
127 stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<short, short> > m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT.;
128 int m_maxNumBitsLUTcxx . . $ LercNS/Huffman#m_maxNumBitsLUT.;
129 int m_numBitsToSkipInTreecxx . . $ LercNS/Huffman#m_numBitsToSkipInTree.;
130 Nodecxx . . $ LercNS/Huffman#Node#* m_rootcxx . . $ LercNS/Huffman#m_root.;
131
132 static int GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(int i, int size) { return i - (i < size ? 0 : size); }
133
134 bool ComputeNumBytesCodeTablecxx . . $ LercNS/Huffman#ComputeNumBytesCodeTable(792b76c179d3cac8).(int& numBytes) const;
135 bool GetRangecxx . . $ LercNS/Huffman#GetRange(9f405b62d6bf9840).(int& i0, int& i1, int& maxCodeLength) const;
136 bool BitStuffCodescxx . . $ LercNS/Huffman#BitStuffCodes(b3c98f7c1c2dce59).(Bytecxx . . $ LercNS/Byte#** ppByte, int i0, int i1) const;
137 bool BitUnStuffCodescxx . . $ LercNS/Huffman#BitUnStuffCodes(2c1933511a15bef0).(const Bytecxx . . $ LercNS/Byte#** ppByte, size_t& nBytesRemaining, int i0, int i1);
138 bool ConvertCodesToCanonicalcxx . . $ LercNS/Huffman#ConvertCodesToCanonical(c9857a73d68e59ba).();
139 void ClearTreecxx . . $ LercNS/Huffman#ClearTree(49f6e7a06ebc5aa8).();
140 };
141
142 // -------------------------------------------------------------------------- ;
143
144 inline bool Huffmancxx . . $ LercNS/Huffman#::DecodeOneValuecxx . . $ LercNS/Huffman#DecodeOneValue(6cf2a5655d1a64b2).(const Bytecxx . . $ LercNS/Byte#** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const
145 {
146 const size_t s4 = sizeof(unsigned int);
147
148 if (!ppSrc || !(*ppSrc) || bitPos < 0 || bitPos >= 32 || nBytesRemaining < s4)
149 return false;
150
151 // first get the next (up to) 12 bits as a copy
152 unsigned int temp(0);
153 memcpy(&temp, *ppSrc, s4);
154 int valTmp = (temp << bitPos) >> (32 - numBitsLUT);
155
156 if (32 - bitPos < numBitsLUT)
157 {
158 if (nBytesRemaining < 2 * s4)
159 return false;
160
161 memcpy(&temp, *ppSrc + s4, s4);
162 valTmp |= temp >> (64 - bitPos - numBitsLUT);
163 }
164
165 if (m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).valTmp].firstcxx . . $ std/pair#first. >= 0) // if there, move the correct number of bits and done
166 {
167 value = m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).valTmp].secondcxx . . $ std/pair#second.;
168 bitPos += m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).valTmp].firstcxx . . $ std/pair#first.;
169 if (bitPos >= 32)
170 {
171 bitPos -= 32;
172 *ppSrc += s4;
173 nBytesRemaining -= s4;
174 }
175 return true;
176 }
177
178 // if not there, go through the tree (slower)
179 if (!m_rootcxx . . $ LercNS/Huffman#m_root.)
180 return false;
181
182 // skip leading 0 bits before entering the tree
183 bitPos += m_numBitsToSkipInTreecxx . . $ LercNS/Huffman#m_numBitsToSkipInTree.;
184 if (bitPos >= 32)
185 {
186 bitPos -= 32;
187 *ppSrc += s4;
188 nBytesRemaining -= s4;
189 }
190
191 const Nodecxx . . $ LercNS/Huffman#Node#* node = m_rootcxx . . $ LercNS/Huffman#m_root.;
192 value = -1;
193 while (value < 0 && nBytesRemaining >= s4)
194 {
195 memcpy(&temp, *ppSrc, s4);
196 int bit = (temp << bitPos) >> 31;
197 bitPos++;
198 if (bitPos == 32)
199 {
200 bitPos = 0;
201 *ppSrc += s4;
202 nBytesRemaining -= s4;
203 }
204
205 node = bit ? node->child1cxx . . $ LercNS/Huffman#Node#child1. : node->child0cxx . . $ LercNS/Huffman#Node#child0.;
206 if (!node)
207 return false;
208
209 if (node->valuecxx . . $ LercNS/Huffman#Node#value. >= 0) // reached a leaf node
210 value = node->valuecxx . . $ LercNS/Huffman#Node#value.;
211 }
212
213 return (value >= 0);
214 }
215
216 // -------------------------------------------------------------------------- ;
217
218 inline bool Huffmancxx . . $ LercNS/Huffman#::PushValuecxx . . $ LercNS/Huffman#PushValue(a42e7bfdfc4e36d8).(Bytecxx . . $ LercNS/Byte#** ppByte, int& bitPos, unsigned int value, int len)
219 {
220 const size_t s4 = sizeof(unsigned int);
221
222 if (!ppByte || !(*ppByte) || bitPos < 0 || bitPos >= 32 || len <= 0 || len > 32)
223 return false;
224
225 if (32 - bitPos >= len)
226 {
227 if (bitPos == 0)
228 memset(*ppByte, 0, s4);
229
230 unsigned int temp(0);
231 memcpy(&temp, *ppByte, s4);
232 temp |= value << (32 - bitPos - len);
233 memcpy(*ppByte, &temp, s4);
234
235 bitPos += len;
236 if (bitPos == 32)
237 {
238 bitPos = 0;
239 *ppByte += s4;
240 }
241 }
242 else
243 {
244 bitPos += len - 32;
245 unsigned int temp(0);
246 memcpy(&temp, *ppByte, s4);
247 temp |= value >> bitPos;
248 memcpy(*ppByte, &temp, s4);
249 *ppByte += s4;
250 temp = value << (32 - bitPos);
251 memcpy(*ppByte, &temp, s4);
252 }
253
254 return true;
255 }
256
257 // -------------------------------------------------------------------------- ;
258
259 NAMESPACE_LERC_ENDcxx . . $ `src/LercLib/Defines.h:36:9`!
260 #endif