Debian Codegraph @

src/LercLib/Huffman.cpp

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 #include <algorithm>
25 #include <queue>
26 #include "Defines.h"cxx . . $ `<file>/src/LercLib/Defines.h`/
27 #include "Huffman.h"cxx . . $ `<file>/src/LercLib/Huffman.h`/
28 #include "BitStuffer2.h"cxx . . $ `<file>/src/LercLib/BitStuffer2.h`/
29
30 using namespace std;
31 USING_NAMESPACE_LERCcxx . . $ `src/LercLib/Defines.h:37:9`!
32
33 // -------------------------------------------------------------------------- ;
34
35 bool Huffmancxx . . $ LercNS/Huffman#::ComputeCodescxx . . $ LercNS/Huffman#ComputeCodes(13834f6f38354517).(const vectorcxx . . $ std/vector#<int>& histo)
36 {
37 if (histo.emptycxx . . $ std/vector#empty(3482b152b9333168).() || histo.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() >= m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.)
38 return false;
39
40 priority_queuecxx . . $ std/priority_queue#<Nodecxx . . $ LercNS/Huffman#Node#, vectorcxx . . $ std/vector#<Nodecxx . . $ LercNS/Huffman#Node#>, lesscxx . . $ std/less#<Nodecxx . . $ LercNS/Huffman#Node#> > pqcxx . . $ std/priority_queue#priority_queue(49f6e7a06ebc5aa8).;
41
42 int numNodes = 0;
43
44 int size = (int)histo.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
45 for (int i = 0; i < size; i++) // add all leaf nodes
46 if (histo[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i] > 0)
47 pq.pushcxx . . $ std/priority_queue#push(bfe8a576267fe2ec).(Nodecxx . . $ LercNS/Huffman#Node#Node(59a23d33d43e6eac).((short)i, histo[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i]));
48
49 if (pq.sizecxx . . $ std/priority_queue#size(71e6dd9020568ee8).() < 2) // histo has only 0 or 1 bin that is not empty; quit Huffman and give it to Lerc
50 return false;
51
52 while (pq.sizecxx . . $ std/priority_queue#size(71e6dd9020568ee8).() > 1) // build the Huffman tree
53 {
54 Nodecxx . . $ LercNS/Huffman#Node#* child0 = new Nodecxx . . $ LercNS/Huffman#Node#(pq.topcxx . . $ std/priority_queue#top(f3460657e6fa9e6a).());
55 numNodes++;
56 pq.popcxx . . $ std/priority_queue#pop(49f6e7a06ebc5aa8).();
57 Nodecxx . . $ LercNS/Huffman#Node#* child1 = new Nodecxx . . $ LercNS/Huffman#Node#(pq.topcxx . . $ std/priority_queue#top(f3460657e6fa9e6a).());
58 numNodes++;
59 pq.popcxx . . $ std/priority_queue#pop(49f6e7a06ebc5aa8).();
60 pq.pushcxx . . $ std/priority_queue#push(bfe8a576267fe2ec).(Nodecxx . . $ LercNS/Huffman#Node#Node(9ff489d7b1020b6a).(child0, child1));
61 }
62
63 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(size);
64 stdcxx . . $ std/::fillcxx . . $ std/fill(4a9bfaed88eccda7).(m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..begincxx . . $ std/vector#begin(55a6de06420d4903).(), m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..endcxx . . $ std/vector#end(55a6de06420d4903).(),
65 stdcxx . . $ std/pair#pair(14d64bafa7379ebc).::paircxx . . $ std/pair#<unsigned short, unsigned int>((short)0, 0));
66
67 if (!pq.topcxx . . $ std/priority_queue#top(f3460657e6fa9e6a).().TreeToLUTcxx . . $ LercNS/Huffman#Node#TreeToLUT(658b69b2a4ed5c4d).(0, 0, m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.)) // fill the LUT
68 return false;
69
70 //pq.top().FreeTree(numNodes); // Linux compiler complains
71 Nodecxx . . $ LercNS/Huffman#Node# nodeNonConst = pq.topcxx . . $ std/priority_queue#top(f3460657e6fa9e6a).();
72 nodeNonConst.FreeTreecxx . . $ LercNS/Huffman#Node#FreeTree(2afed6f74e8a6d90).(numNodes); // free all the nodes
73
74 if (numNodes != 0) // check the ref count
75 return false;
76
77 if (!ConvertCodesToCanonicalcxx . . $ LercNS/Huffman#ConvertCodesToCanonical(c9857a73d68e59ba).())
78 return false;
79
80 return true;
81 }
82
83 // -------------------------------------------------------------------------- ;
84
85 bool Huffmancxx . . $ LercNS/Huffman#::ComputeCompressedSizecxx . . $ LercNS/Huffman#ComputeCompressedSize(aaf3cc3253c7f648).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<int>& histo, int& numBytes, double& avgBpp) const
86 {
87 if (histo.emptycxx . . $ std/vector#empty(3482b152b9333168).() || histo.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() >= m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.)
88 return false;
89
90 numBytes = 0;
91 if (!ComputeNumBytesCodeTablecxx . . $ LercNS/Huffman#ComputeNumBytesCodeTable(792b76c179d3cac8).(numBytes)) // header and code table
92 return false;
93
94 int numBits = 0, numElem = 0;
95 int size = (int)histo.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
96 for (int i = 0; i < size; i++)
97 if (histo[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i] > 0)
98 {
99 numBits += histo[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i] * m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].firstcxx . . $ std/pair#first.;
100 numElem += histo[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i];
101 }
102
103 if (numElem == 0)
104 return false;
105
106 int numUInts = ((((numBits + 7) >> 3) + 3) >> 2) + 1; // add one more as the decode LUT can read ahead
107 numBytes += 4 * numUInts; // data huffman coded
108 avgBpp = 8 * numBytes / (double)numElem;
109
110 return true;
111 }
112
113 // -------------------------------------------------------------------------- ;
114
115 bool Huffmancxx . . $ LercNS/Huffman#::SetCodescxx . . $ LercNS/Huffman#SetCodes(8994584b4c5a0cd).(const vectorcxx . . $ std/vector#<paircxx . . $ std/pair#<unsigned short, unsigned int> >& codeTable)
116 {
117 if (codeTable.emptycxx . . $ std/vector#empty(3482b152b9333168).() || codeTable.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() >= m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.)
118 return false;
119
120 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable. =cxx . . $ std/vector#`operator=`(e6b68e61becad66f). codeTable;
121 return true;
122 }
123
124 // -------------------------------------------------------------------------- ;
125
126 bool Huffmancxx . . $ LercNS/Huffman#::WriteCodeTablecxx . . $ LercNS/Huffman#WriteCodeTable(18073134a042c744).(Bytecxx . . $ LercNS/Byte#** ppByte, int lerc2Version) const
127 {
128 if (!ppByte)
129 return false;
130
131 int i0, i1, maxLen;
132 if (!GetRangecxx . . $ LercNS/Huffman#GetRange(9f405b62d6bf9840).(i0, i1, maxLen))
133 return false;
134
135 int size = (int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
136 vectorcxx . . $ std/vector#<unsigned int> dataVeccxx . . $ std/vector#vector(229e4d6bd205de31).(i1 - i0, 0);
137
138 for (int i = i0; i < i1; i++)
139 {
140 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
141 dataVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i - i0] = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).k].firstcxx . . $ std/pair#first.;
142 }
143
144 // header
145 vectorcxx . . $ std/vector#<int> intVeccxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
146 intVec.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(4); // huffman version; 4 guarantees canonical codes
147 intVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(size);
148 intVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(i0); // code range
149 intVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(i1);
150
151 Bytecxx . . $ LercNS/Byte#* ptr = *ppByte;
152
153 size_t len = intVec.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() * sizeof(int);
154 memcpy(ptr, &intVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], len);
155 ptr += len;
156
157 BitStuffer2cxx . . $ LercNS/BitStuffer2# bitStuffer2cxx . . $ LercNS/BitStuffer2#BitStuffer2(49f6e7a06ebc5aa8).;
158 if (!bitStuffer2.EncodeSimplecxx . . $ LercNS/BitStuffer2#EncodeSimple(cf58339148bdbf5).(&ptr, dataVec, lerc2Version)) // code lengths, bit stuffed
159 return false;
160
161 if (!BitStuffCodescxx . . $ LercNS/Huffman#BitStuffCodes(b3c98f7c1c2dce59).(&ptr, i0, i1)) // variable length codes, bit stuffed
162 return false;
163
164 *ppByte = ptr;
165 return true;
166 }
167
168 // -------------------------------------------------------------------------- ;
169
170 bool Huffmancxx . . $ LercNS/Huffman#::ReadCodeTablecxx . . $ LercNS/Huffman#ReadCodeTable(93c08913fdc74d09).(const Bytecxx . . $ LercNS/Byte#** ppByte, size_t& nBytesRemainingInOut, int lerc2Version)
171 {
172 if (!ppByte || !(*ppByte))
173 return false;
174
175 const Bytecxx . . $ LercNS/Byte#* ptr = *ppByte;
176 size_t nBytesRemaining = nBytesRemainingInOut;
177
178 vectorcxx . . $ std/vector#<int> intVeccxx . . $ std/vector#vector(229e4d6bd205de31).(4, 0);
179 size_t len = intVec.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() * sizeof(int);
180
181 if (nBytesRemaining < len)
182 return false;
183
184 memcpy(&intVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], ptr, len);
185 ptr += len;
186 nBytesRemaining -= len;
187
188 int version = intVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
189
190 if (version < 2) // allow forward compatibility; for updates that break old decoders increase Lerc2 version number;
191 return false;
192
193 const int size = intVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).1];
194 const int i0 = intVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).2];
195 const int i1 = intVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).3];
196
197 if (i0 >= i1 || i0 < 0 || size < 0 || size > (int)m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.)
198 return false;
199
200 if (GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i0, size) >= size || GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i1 - 1, size) >= size)
201 return false;
202
203 try
204 {
205 vectorcxx . . $ std/vector#<unsigned int> dataVeccxx . . $ std/vector#vector(229e4d6bd205de31).(i1 - i0, 0);
206 BitStuffer2cxx . . $ LercNS/BitStuffer2# bitStuffer2cxx . . $ LercNS/BitStuffer2#BitStuffer2(49f6e7a06ebc5aa8).;
207 if (!bitStuffer2.Decodecxx . . $ LercNS/BitStuffer2#Decode(e9dc1ca41d64813e).(&ptr, nBytesRemaining, dataVec, dataVec.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(), lerc2Version)) // unstuff the code lengths
208 return false;
209
210 if (dataVec.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != static_cast<size_t>(i1 - i0))
211 return false;
212
213 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(size);
214 stdcxx . . $ std/::fillcxx . . $ std/fill(4a9bfaed88eccda7).(m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..begincxx . . $ std/vector#begin(55a6de06420d4903).(), m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..endcxx . . $ std/vector#end(55a6de06420d4903).(),
215 stdcxx . . $ std/pair#pair(14d64bafa7379ebc).::paircxx . . $ std/pair#<unsigned short, unsigned int>((short)0, 0));
216
217 for (int i = i0; i < i1; i++)
218 {
219 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
220 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].firstcxx . . $ std/pair#first. = (unsigned short)dataVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i - i0];
221 }
222
223 if (!BitUnStuffCodescxx . . $ LercNS/Huffman#BitUnStuffCodes(2c1933511a15bef0).(&ptr, nBytesRemaining, i0, i1)) // unstuff the codes
224 return false;
225
226 *ppByte = ptr;
227 nBytesRemainingInOut = nBytesRemaining;
228 return true;
229 }
230 catch (stdcxx . . $ std/::exception&)
231 {
232 return false;
233 }
234 }
235
236 // -------------------------------------------------------------------------- ;
237
238 bool Huffmancxx . . $ LercNS/Huffman#::BuildTreeFromCodescxx . . $ LercNS/Huffman#BuildTreeFromCodes(30ce46cb091eb21e).(int& numBitsLUT)
239 {
240 int i0 = 0, i1 = 0, maxLen = 0;
241 if (!GetRangecxx . . $ LercNS/Huffman#GetRange(9f405b62d6bf9840).(i0, i1, maxLen))
242 return false;
243
244 // build decode LUT using max of 12 bits
245 int size = (int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
246 int minNumZeroBits = 32;
247
248 bool bNeedTree = maxLen > m_maxNumBitsLUTcxx . . $ LercNS/Huffman#m_maxNumBitsLUT.;
249 numBitsLUT = mincxx . . $ std/min(2a338fce5842aa6a).(maxLen, m_maxNumBitsLUTcxx . . $ LercNS/Huffman#m_maxNumBitsLUT.);
250
251 int sizeLUT = 1 << numBitsLUT;
252
253 m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
254 m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT..assigncxx . . $ std/vector#assign(3804226c8c0acbc3).((size_t)sizeLUT, paircxx . . $ std/pair#pair(14d64bafa7379ebc).<short, short>((short)-1, (short)-1));
255
256 for (int i = i0; i < i1; i++)
257 {
258 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
259 int len = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].firstcxx . . $ std/pair#first.;
260
261 if (len == 0)
262 continue;
263
264 unsigned int code = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].secondcxx . . $ std/pair#second.;
265
266 if (len <= numBitsLUT)
267 {
268 code <<= (numBitsLUT - len);
269 unsigned int numEntries = 1 << (numBitsLUT - len);
270 paircxx . . $ std/pair#<short, short> entrycxx . . $ std/pair#pair(14d64bafa7379ebc).((short)len, (short)k);
271
272 for (unsigned int j = 0; j < numEntries; j++)
273 m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).code | j] =cxx . . $ std/pair#`operator=`(77b1c6289f83a70a). entry; // add the duplicates
274 }
275 else // for the codes too long for the LUT, count how many leading bits are 0
276 {
277 int shift = 1;
278 while (code >>= 1) shift++; // large canonical codes start with zero's
279 minNumZeroBits = mincxx . . $ std/min(2a338fce5842aa6a).(minNumZeroBits, len - shift);
280 }
281 }
282
283 m_numBitsToSkipInTreecxx . . $ LercNS/Huffman#m_numBitsToSkipInTree. = bNeedTree? minNumZeroBits : 0;
284
285 if (!bNeedTree) // decode LUT covers it all, no tree needed
286 return true;
287
288 //m_numBitsToSkipInTree = 0; // to disable skipping the 0 bits
289
290 ClearTreecxx . . $ LercNS/Huffman#ClearTree(49f6e7a06ebc5aa8).(); // if there
291
292 Nodecxx . . $ LercNS/Huffman#Node# emptyNodecxx . . $ LercNS/Huffman#Node#Node(59a23d33d43e6eac).((short)-1, 0);
293 m_rootcxx . . $ LercNS/Huffman#m_root. = new Nodecxx . . $ LercNS/Huffman#Node#(emptyNode);
294
295 for (int i = i0; i < i1; i++)
296 {
297 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
298 int len = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].firstcxx . . $ std/pair#first.;
299
300 if (len > 0 && len > numBitsLUT) // add only codes not in the decode LUT
301 {
302 unsigned int code = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].secondcxx . . $ std/pair#second.;
303 Nodecxx . . $ LercNS/Huffman#Node#* node = m_rootcxx . . $ LercNS/Huffman#m_root.;
304 int j = len - m_numBitsToSkipInTreecxx . . $ LercNS/Huffman#m_numBitsToSkipInTree.; // reduce len by number of leading 0 bits from above
305
306 while (--j >= 0) // go over the bits
307 {
308 if (code & (1 << j))
309 {
310 if (!node->child1cxx . . $ LercNS/Huffman#Node#child1.)
311 node->child1cxx . . $ LercNS/Huffman#Node#child1. = new Nodecxx . . $ LercNS/Huffman#Node#(emptyNode);
312
313 node = node->child1cxx . . $ LercNS/Huffman#Node#child1.;
314 }
315 else
316 {
317 if (!node->child0cxx . . $ LercNS/Huffman#Node#child0.)
318 node->child0cxx . . $ LercNS/Huffman#Node#child0. = new Nodecxx . . $ LercNS/Huffman#Node#(emptyNode);
319
320 node = node->child0cxx . . $ LercNS/Huffman#Node#child0.;
321 }
322
323 if (j == 0) // last bit, leaf node
324 node->valuecxx . . $ LercNS/Huffman#Node#value. = (short)k; // set the value
325 }
326 }
327 }
328
329 return true;
330 }
331
332 // -------------------------------------------------------------------------- ;
333
334 void Huffmancxx . . $ LercNS/Huffman#::Clearcxx . . $ LercNS/Huffman#Clear(49f6e7a06ebc5aa8).()
335 {
336 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
337 m_decodeLUTcxx . . $ LercNS/Huffman#m_decodeLUT..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
338 ClearTreecxx . . $ LercNS/Huffman#ClearTree(49f6e7a06ebc5aa8).();
339 }
340
341 // -------------------------------------------------------------------------- ;
342
343 void Huffmancxx . . $ LercNS/Huffman#::ClearTreecxx . . $ LercNS/Huffman#ClearTree(49f6e7a06ebc5aa8).()
344 {
345 if (m_rootcxx . . $ LercNS/Huffman#m_root.)
346 {
347 int n = 0;
348 m_rootcxx . . $ LercNS/Huffman#m_root.->FreeTreecxx . . $ LercNS/Huffman#Node#FreeTree(2afed6f74e8a6d90).(n);
349 delete m_rootcxx . . $ LercNS/Huffman#m_root.;
350 m_rootcxx . . $ LercNS/Huffman#m_root. = nullptr;
351 }
352 }
353
354 // -------------------------------------------------------------------------- ;
355 // -------------------------------------------------------------------------- ;
356
357 bool Huffmancxx . . $ LercNS/Huffman#::ComputeNumBytesCodeTablecxx . . $ LercNS/Huffman#ComputeNumBytesCodeTable(792b76c179d3cac8).(int& numBytes) const
358 {
359 int i0, i1, maxLen;
360 if (!GetRangecxx . . $ LercNS/Huffman#GetRange(9f405b62d6bf9840).(i0, i1, maxLen))
361 return false;
362
363 int size = (int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
364 int sum = 0;
365 for (int i = i0; i < i1; i++)
366 {
367 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
368 sum += m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).k].firstcxx . . $ std/pair#first.;
369 }
370
371 numBytes = 4 * sizeof(int); // version, size, first bin, (last + 1) bin
372
373 BitStuffer2cxx . . $ LercNS/BitStuffer2# bitStuffer2cxx . . $ LercNS/BitStuffer2#BitStuffer2(49f6e7a06ebc5aa8).;
374 numBytes += bitStuffer2.ComputeNumBytesNeededSimplecxx . . $ LercNS/BitStuffer2#ComputeNumBytesNeededSimple(83f10ac8959d5577).((unsigned int)(i1 - i0), (unsigned int)maxLen); // code lengths
375 int numUInts = (((sum + 7) >> 3) + 3) >> 2;
376 numBytes += 4 * numUInts; // byte array with the codes bit stuffed
377
378 return true;
379 }
380
381 // -------------------------------------------------------------------------- ;
382
383 bool Huffmancxx . . $ LercNS/Huffman#::GetRangecxx . . $ LercNS/Huffman#GetRange(9f405b62d6bf9840).(int& i0, int& i1, int& maxCodeLength) const
384 {
385 if (m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..emptycxx . . $ std/vector#empty(3482b152b9333168).() || m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() >= m_maxHistoSizecxx . . $ LercNS/Huffman#m_maxHistoSize.)
386 return false;
387
388 // first, check for peak somewhere in the middle with 0 stretches left and right
389 int size = (int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
390 {
391 int i = 0;
392 while (i < size && m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].firstcxx . . $ std/pair#first. == 0) i++;
393 i0 = i;
394 i = size - 1;
395 while (i >= 0 && m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].firstcxx . . $ std/pair#first. == 0) i--;
396 i1 = i + 1; // exclusive
397 }
398
399 if (i1 <= i0)
400 return false;
401
402 // second, cover the common case that the peak is close to 0
403 paircxx . . $ std/pair#<int, int> segmcxx . . $ std/pair#pair(14d64bafa7379ebc).(0, 0);
404 int j = 0;
405 while (j < size) // find the largest stretch of 0's, if any
406 {
407 while (j < size && m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).j].firstcxx . . $ std/pair#first. > 0) j++;
408 int k0 = j;
409 while (j < size && m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).j].firstcxx . . $ std/pair#first. == 0) j++;
410 int k1 = j;
411
412 if (k1 - k0 > segm.secondcxx . . $ std/pair#second.)
413 segm =cxx . . $ std/pair#`operator=`(225bd4d59bf12f87). paircxx . . $ std/pair#pair(14d64bafa7379ebc).<int, int>(k0, k1 - k0);
414 }
415
416 if (size - segm.secondcxx . . $ std/pair#second. < i1 - i0)
417 {
418 i0 = segm.firstcxx . . $ std/pair#first. + segm.secondcxx . . $ std/pair#second.;
419 i1 = segm.firstcxx . . $ std/pair#first. + size; // do wrap around
420 }
421
422 if (i1 <= i0)
423 return false;
424
425 int maxLen = 0;
426 for (int i = i0; i < i1; i++)
427 {
428 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
429 int len = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).k].firstcxx . . $ std/pair#first.;
430 maxLen = maxcxx . . $ std/max(2a338fce5842aa6a).(maxLen, len);
431 }
432
433 if (maxLen <= 0 || maxLen > 32)
434 return false;
435
436 maxCodeLength = maxLen;
437 return true;
438 }
439
440 // -------------------------------------------------------------------------- ;
441
442 bool Huffmancxx . . $ LercNS/Huffman#::BitStuffCodescxx . . $ LercNS/Huffman#BitStuffCodes(b3c98f7c1c2dce59).(Bytecxx . . $ LercNS/Byte#** ppByte, int i0, int i1) const
443 {
444 if (!ppByte)
445 return false;
446
447 int size = (int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
448 int bitPos = 0;
449
450 for (int i = i0; i < i1; i++)
451 {
452 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
453 int len = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).k].firstcxx . . $ std/pair#first.;
454 if (len > 0)
455 {
456 unsigned int val = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).k].secondcxx . . $ std/pair#second.;
457
458 if (!Huffmancxx . . $ LercNS/Huffman#::PushValuecxx . . $ LercNS/Huffman#PushValue(a42e7bfdfc4e36d8).(ppByte, bitPos, val, len))
459 return false;
460 }
461 }
462
463 size_t numUInts = (bitPos > 0 ? 1 : 0);
464 *ppByte += numUInts * sizeof(unsigned int);
465
466 return true;
467 }
468
469 // -------------------------------------------------------------------------- ;
470
471 bool Huffmancxx . . $ LercNS/Huffman#::BitUnStuffCodescxx . . $ LercNS/Huffman#BitUnStuffCodes(2c1933511a15bef0).(const Bytecxx . . $ LercNS/Byte#** ppByte, size_t& nBytesRemainingInOut, int i0, int i1)
472 {
473 if (!ppByte || !(*ppByte))
474 return false;
475
476 size_t nBytesRemaining = nBytesRemainingInOut;
477
478 const Bytecxx . . $ LercNS/Byte#* ptr0 = *ppByte;
479 const Bytecxx . . $ LercNS/Byte#* ptr = ptr0;
480
481 const size_t s4 = sizeof(unsigned int);
482
483 int size = (int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
484 int bitPos = 0;
485
486 for (int i = i0; i < i1; i++)
487 {
488 int k = GetIndexWrapAroundcxx . . $ LercNS/Huffman#GetIndexWrapAround(9b79fb6aee4c0440).(i, size);
489 int len = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].firstcxx . . $ std/pair#first.;
490 if (len > 0)
491 {
492 if (nBytesRemaining < s4 || len > 32)
493 return false;
494
495 unsigned int temp(0);
496 memcpy(&temp, ptr, s4);
497 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].secondcxx . . $ std/pair#second. = (temp << bitPos) >> (32 - len);
498
499 if (32 - bitPos >= len)
500 {
501 bitPos += len;
502 if (bitPos == 32)
503 {
504 bitPos = 0;
505 ptr += s4;
506 nBytesRemaining -= s4;
507 }
508 }
509 else
510 {
511 bitPos += len - 32;
512 ptr += s4;
513 nBytesRemaining -= s4;
514
515 if (nBytesRemaining < s4)
516 return false;
517
518 memcpy(&temp, ptr, s4);
519 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k].secondcxx . . $ std/pair#second. |= temp >> (32 - bitPos); // bitPos > 0
520 }
521 }
522 }
523
524 size_t len = (ptr - ptr0) + (bitPos > 0 ? s4 : 0);
525
526 if (nBytesRemainingInOut < len)
527 return false;
528
529 *ppByte += len;
530 nBytesRemainingInOut -= len;
531
532 if (nBytesRemaining != nBytesRemainingInOut
533 && nBytesRemaining != nBytesRemainingInOut + s4) // the real check
534 return false;
535
536 return true;
537 }
538
539 // -------------------------------------------------------------------------- ;
540
541 bool Huffmancxx . . $ LercNS/Huffman#::ConvertCodesToCanonicalcxx . . $ LercNS/Huffman#ConvertCodesToCanonical(c9857a73d68e59ba).()
542 {
543 // from the non canonical code book, create an array to be sorted in descending order:
544 // codeLength * tableSize - index
545
546 unsigned int tableSize = (unsigned int)m_codeTablecxx . . $ LercNS/Huffman#m_codeTable..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
547 vectorcxx . . $ std/vector#<paircxx . . $ std/pair#<int, unsigned int>> sortVeccxx . . $ std/vector#vector(229e4d6bd205de31).(tableSize, paircxx . . $ std/pair#pair(14d64bafa7379ebc).<int, unsigned int>(0, 0));
548
549 for (unsigned int i = 0; i < tableSize; i++)
550 if (m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].firstcxx . . $ std/pair#first. > 0)
551 sortVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] =cxx . . $ std/pair#`operator=`(225bd4d59bf12f87). paircxx . . $ std/pair#pair(14d64bafa7379ebc).<int, unsigned int>(m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].firstcxx . . $ std/pair#first. * tableSize - i, i);
552
553 stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e).(sortVec.begincxx . . $ std/vector#begin(55a6de06420d4903).(), sortVec.endcxx . . $ std/vector#end(55a6de06420d4903).(),
554 [](const paircxx . . $ std/pair#<int, unsigned int>& p0,
555 const paircxx . . $ std/pair#<int, unsigned int>& p1) { return p0.firstcxx . . $ std/pair#first. > p1.firstcxx . . $ std/pair#first.; });
556
557 // create canonical codes and assign to orig code table
558 unsigned int index = sortVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0].secondcxx . . $ std/pair#second.;
559 unsigned short codeLen = m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).index].firstcxx . . $ std/pair#first.; // max code length for this table
560 unsigned int i = 0, codeCanonical = 0;
561
562 while (i < tableSize && sortVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].firstcxx . . $ std/pair#first. > 0)
563 {
564 index = sortVec[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i++].secondcxx . . $ std/pair#second.;
565 short delta = codeLen - m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).index].firstcxx . . $ std/pair#first.; // difference of 2 consecutive code lengths, >= 0 as sorted
566 codeCanonical >>= delta;
567 codeLen -= delta;
568 m_codeTablecxx . . $ LercNS/Huffman#m_codeTable.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).index].secondcxx . . $ std/pair#second. = codeCanonical++;
569 }
570
571 return true;
572 }
573
574 // -------------------------------------------------------------------------- ;