Debian Codegraph @

src/LercLib/BitStuffer2.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 "Defines.h"
26 #include "BitStuffer2.h"
27
28 using namespace std;
29 USING_NAMESPACE_LERC
30
31 // -------------------------------------------------------------------------- ;
32
33 // if you change Encode(...) / Decode(...), don't forget to update ComputeNumBytesNeeded(...)
34
35 bool BitStuffer2::EncodeSimple(Byte** ppByte, const vector<unsigned int>& dataVec, int lerc2Version) const
36 {
37 if (!ppByte || dataVec.empty())
38 return false;
39
40 unsigned int maxElem = *max_element(dataVec.begin(), dataVec.end());
41 int numBits = 0;
42 while ((numBits < 32) && (maxElem >> numBits))
43 numBits++;
44
45 if (numBits >= 32)
46 return false;
47
48 Byte numBitsByte = (Byte)numBits;
49 unsigned int numElements = (unsigned int)dataVec.size();
50 unsigned int numUInts = (numElements * numBits + 31) / 32;
51
52 // use the upper 2 bits to encode the type used for numElements: Byte, ushort, or uint
53 int n = NumBytesUInt(numElements);
54 int bits67 = (n == 4) ? 0 : 3 - n;
55 numBitsByte |= bits67 << 6;
56
57 // bit5 = 0 means simple mode
58
59 **ppByte = numBitsByte;
60 (*ppByte)++;
61
62 if (!EncodeUInt(ppByte, numElements, n))
63 return false;
64
65 if (numUInts > 0) // numBits can be 0, then we only write the header
66 {
67 // now (0 < numBits < 32)
68 if (lerc2Version >= 3)
69 BitStuff(ppByte, dataVec, numBits);
70 else
71 BitStuff_Before_Lerc2v3(ppByte, dataVec, numBits);
72 }
73
74 return true;
75 }
76
77 // -------------------------------------------------------------------------- ;
78
79 bool BitStuffer2::EncodeLut(Byte** ppByte, const vector<pair<unsigned int, unsigned int>>& sortedDataVec, int lerc2Version) const
80 {
81 if (!ppByte || sortedDataVec.empty())
82 return false;
83
84 if (sortedDataVec[0].first != 0) // corresponds to min
85 return false;
86
87 // collect the different values for the lut
88 unsigned int numElem = (unsigned int)sortedDataVec.size();
89 unsigned int indexLut = 0;
90
91 m_tmpLutVec.resize(0); // omit the 0 throughout that corresponds to min
92 m_tmpIndexVec.assign(numElem, 0);
93
94 for (unsigned int i = 1; i < numElem; i++)
95 {
96 unsigned int prev = sortedDataVec[i - 1].first;
97 m_tmpIndexVec[sortedDataVec[i - 1].second] = indexLut;
98
99 if (sortedDataVec[i].first != prev)
100 {
101 m_tmpLutVec.push_back(sortedDataVec[i].first);
102 indexLut++;
103 }
104 }
105 m_tmpIndexVec[sortedDataVec[numElem - 1].second] = indexLut; // don't forget the last one
106
107 // write first 2 data elements same as simple, but bit5 set to 1
108 unsigned int maxElem = m_tmpLutVec.back();
109 int numBits = 0;
110 while ((numBits < 32) && (maxElem >> numBits))
111 numBits++;
112
113 if (numBits <= 0 || numBits >= 32) // ensures (0 < numBits < 32)
114 return false;
115
116 Byte numBitsByte = (Byte)numBits;
117
118 // use the upper 2 bits to encode the type used for numElem: byte, ushort, or uint
119 int n = NumBytesUInt(numElem);
120 int bits67 = (n == 4) ? 0 : 3 - n;
121 numBitsByte |= bits67 << 6;
122
123 numBitsByte |= (1 << 5); // bit 5 = 1 means lut mode
124
125 **ppByte = numBitsByte;
126 (*ppByte)++;
127
128 if (!EncodeUInt(ppByte, numElem, n)) // numElements = numIndexes to lut
129 return false;
130
131 unsigned int nLut = (unsigned int)m_tmpLutVec.size();
132 if (nLut < 1 || nLut >= 255) // ensures (0 < nBitsLut < 32) below
133 return false;
134
135 **ppByte = (Byte)nLut + 1; // size of lut, incl the 0
136 (*ppByte)++;
137
138 if (lerc2Version >= 3)
139 BitStuff(ppByte, m_tmpLutVec, numBits); // lut
140 else
141 BitStuff_Before_Lerc2v3(ppByte, m_tmpLutVec, numBits);
142
143 int nBitsLut = 0;
144 while (nLut >> nBitsLut) // indexes are in [0 .. nLut]
145 nBitsLut++;
146
147 if (lerc2Version >= 3)
148 BitStuff(ppByte, m_tmpIndexVec, nBitsLut); // indexes
149 else
150 BitStuff_Before_Lerc2v3(ppByte, m_tmpIndexVec, nBitsLut);
151
152 return true;
153 }
154
155 // -------------------------------------------------------------------------- ;
156
157 // if you change Encode(...) / Decode(...), don't forget to update ComputeNumBytesNeeded(...)
158
159 bool BitStuffer2::Decode(const Byte** ppByte, size_t& nBytesRemaining, vector<unsigned int>& dataVec, size_t maxElementCount, int lerc2Version) const
160 {
161 if (!ppByte || nBytesRemaining < 1)
162 return false;
163
164 Byte numBitsByte = **ppByte;
165 (*ppByte)++;
166 nBytesRemaining--;
167
168 int bits67 = numBitsByte >> 6;
169 int nb = (bits67 == 0) ? 4 : 3 - bits67;
170
171 bool doLut = (numBitsByte & (1 << 5)) ? true : false; // bit 5
172 numBitsByte &= 31; // bits 0-4;
173 int numBits = numBitsByte;
174
175 unsigned int numElements = 0;
176 if (!DecodeUInt(ppByte, nBytesRemaining, numElements, nb))
177 return false;
178 if (numElements > maxElementCount)
179 return false;
180
181 if (!doLut)
182 {
183 if (numBits > 0) // numBits can be 0
184 {
185 if (lerc2Version >= 3)
186 {
187 if (!BitUnStuff(ppByte, nBytesRemaining, dataVec, numElements, numBits))
188 return false;
189 }
190 else
191 {
192 if (!BitUnStuff_Before_Lerc2v3(ppByte, nBytesRemaining, dataVec, numElements, numBits))
193 return false;
194 }
195 }
196 }
197 else
198 {
199 if (numBits == 0) // fail gracefully in case of corrupted blob for old version <= 2 which had no checksum
200 return false;
201 if (nBytesRemaining < 1)
202 return false;
203
204 Byte nLutByte = **ppByte;
205 (*ppByte)++;
206 nBytesRemaining--;
207
208 int nLut = nLutByte - 1;
209
210 // unstuff lut w/o the 0
211 if (lerc2Version >= 3)
212 {
213 if (!BitUnStuff(ppByte, nBytesRemaining, m_tmpLutVec, nLut, numBits))
214 return false;
215 }
216 else
217 {
218 if (!BitUnStuff_Before_Lerc2v3(ppByte, nBytesRemaining, m_tmpLutVec, nLut, numBits))
219 return false;
220 }
221
222 int nBitsLut = 0;
223 while (nLut >> nBitsLut)
224 nBitsLut++;
225 if (nBitsLut == 0)
226 return false;
227
228 if (lerc2Version >= 3)
229 {
230 // unstuff indexes
231 if (!BitUnStuff(ppByte, nBytesRemaining, dataVec, numElements, nBitsLut))
232 return false;
233
234 // replace indexes by values
235 m_tmpLutVec.insert(m_tmpLutVec.begin(), 0); // put back in the 0
236 for (unsigned int i = 0; i < numElements; i++)
237 dataVec[i] = m_tmpLutVec[dataVec[i]];
238 }
239 else
240 {
241 // unstuff indexes
242 if (!BitUnStuff_Before_Lerc2v3(ppByte, nBytesRemaining, dataVec, numElements, nBitsLut))
243 return false;
244
245 // replace indexes by values
246 m_tmpLutVec.insert(m_tmpLutVec.begin(), 0); // put back in the 0
247 for (unsigned int i = 0; i < numElements; i++)
248 {
249 if (dataVec[i] >= m_tmpLutVec.size())
250 return false;
251
252 dataVec[i] = m_tmpLutVec[dataVec[i]];
253 }
254 }
255 }
256
257 return true;
258 }
259
260 // -------------------------------------------------------------------------- ;
261
262 unsigned int BitStuffer2::ComputeNumBytesNeededLut(const vector<pair<unsigned int, unsigned int> >& sortedDataVec, bool& doLut)
263 {
264 unsigned int maxElem = sortedDataVec.back().first;
265 unsigned int numElem = (unsigned int)sortedDataVec.size();
266
267 int numBits = 0;
268 while ((numBits < 32) && (maxElem >> numBits))
269 numBits++;
270 unsigned int numBytes = 1 + NumBytesUInt(numElem) + ((numElem * numBits + 7) >> 3);
271
272 // go through and count how often the value changes
273 int nLut = 0;
274 for (unsigned int i = 1; i < numElem; i++)
275 if (sortedDataVec[i].first != sortedDataVec[i - 1].first)
276 nLut++;
277
278 int nBitsLut = 0;
279 while (nLut >> nBitsLut)
280 nBitsLut++;
281
282 unsigned int numBitsTotalLut = nLut * numBits; // num bits w/o the 0
283 unsigned int numBytesLut = 1 + NumBytesUInt(numElem) + 1 + ((numBitsTotalLut + 7) >> 3) + ((numElem * nBitsLut + 7) >> 3);
284
285 doLut = numBytesLut < numBytes;
286 return min(numBytesLut, numBytes);
287 }
288
289 // -------------------------------------------------------------------------- ;
290 // -------------------------------------------------------------------------- ;
291
292 void BitStuffer2::BitStuff_Before_Lerc2v3(Byte** ppByte, const vector<unsigned int>& dataVec, int numBits)
293 {
294 unsigned int numElements = (unsigned int)dataVec.size();
295 unsigned int numUInts = (numElements * numBits + 31) / 32;
296 unsigned int numBytes = numUInts * sizeof(unsigned int);
297 unsigned int* arr = (unsigned int*)(*ppByte);
298
299 memset(arr, 0, numBytes);
300
301 // do the stuffing
302 const unsigned int* srcPtr = &dataVec[0];
303 unsigned int* dstPtr = arr;
304 int bitPos = 0;
305
306 for (unsigned int i = 0; i < numElements; i++)
307 {
308 if (32 - bitPos >= numBits)
309 {
310 unsigned int dstValue;
311 memcpy(&dstValue, dstPtr, sizeof(unsigned int));
312 dstValue |= (*srcPtr++) << (32 - bitPos - numBits);
313 memcpy(dstPtr, &dstValue, sizeof(unsigned int));
314 bitPos += numBits;
315 if (bitPos == 32) // shift >= 32 is undefined
316 {
317 bitPos = 0;
318 dstPtr++;
319 }
320 }
321 else
322 {
323 unsigned int dstValue;
324 int n = numBits - (32 - bitPos);
325 memcpy(&dstValue, dstPtr, sizeof(unsigned int));
326 dstValue |= (*srcPtr) >> n;
327 memcpy(dstPtr, &dstValue, sizeof(unsigned int));
328 dstPtr++;
329 memcpy(&dstValue, dstPtr, sizeof(unsigned int));
330 dstValue |= (*srcPtr++) << (32 - n);
331 memcpy(dstPtr, &dstValue, sizeof(unsigned int));
332 bitPos = n;
333 }
334 }
335
336 // save the 0-3 bytes not used in the last UInt
337 unsigned int numBytesNotNeeded = NumTailBytesNotNeeded(numElements, numBits);
338 unsigned int n = numBytesNotNeeded;
339 for (; n; --n)
340 {
341 unsigned int dstValue;
342 memcpy(&dstValue, dstPtr, sizeof(unsigned int));
343 dstValue >>= 8;
344 memcpy(dstPtr, &dstValue, sizeof(unsigned int));
345 }
346
347 *ppByte += numBytes - numBytesNotNeeded;
348 }
349
350 // -------------------------------------------------------------------------- ;
351
352 bool BitStuffer2::BitUnStuff_Before_Lerc2v3(const Byte** ppByte, size_t& nBytesRemaining,
353 vector<unsigned int>& dataVec, unsigned int numElements, int numBits) const
354 {
355 if (numElements == 0 || numBits >= 32)
356 return false;
357
358 unsigned long long numUIntsLL = ((unsigned long long)numElements * numBits + 31) / 32;
359 unsigned long long numBytesLL = numUIntsLL * sizeof(unsigned int);
360
361 size_t numUInts = (size_t)numUIntsLL;
362 size_t numBytes = (size_t)numBytesLL; // could theoretically overflow on 32 bit system
363
364 const unsigned int ntbnn = NumTailBytesNotNeeded(numElements, numBits);
365
366 if (numBytes != numBytesLL || nBytesRemaining + ntbnn < numBytes)
367 return false;
368
369 try
370 {
371 dataVec.resize(numElements, 0); // init with 0
372 m_tmpBitStuffVec.resize(numUInts);
373 }
374 catch (const std::exception&)
375 {
376 return false;
377 }
378
379 m_tmpBitStuffVec[numUInts - 1] = 0; // set last uint to 0
380
381 unsigned int nBytesToCopy = (numElements * numBits + 7) / 8;
382 memcpy(&m_tmpBitStuffVec[0], *ppByte, nBytesToCopy);
383
384 unsigned int* pLastULong = &m_tmpBitStuffVec[numUInts - 1];
385 for (unsigned int k = ntbnn; k > 0; k--)
386 *pLastULong <<= 8;
387
388 unsigned int* srcPtr = &m_tmpBitStuffVec[0];
389 unsigned int* dstPtr = &dataVec[0];
390 int bitPos = 0;
391
392 for (unsigned int i = 0; i < numElements; i++)
393 {
394 if (32 - bitPos >= numBits)
395 {
396 unsigned int val;
397 memcpy(&val, srcPtr, sizeof(unsigned int));
398 unsigned int n = val << bitPos;
399 *dstPtr++ = n >> (32 - numBits);
400 bitPos += numBits;
401
402 if (bitPos == 32) // shift >= 32 is undefined
403 {
404 bitPos = 0;
405 srcPtr++;
406 }
407 }
408 else
409 {
410 unsigned int val;
411 memcpy(&val, srcPtr, sizeof(unsigned int));
412 srcPtr++;
413 unsigned int n = val << bitPos;
414 *dstPtr = n >> (32 - numBits);
415 bitPos -= (32 - numBits);
416 memcpy(&val, srcPtr, sizeof(unsigned int));
417 *dstPtr++ |= val >> (32 - bitPos);
418 }
419 }
420
421 *ppByte += nBytesToCopy;
422 nBytesRemaining -= nBytesToCopy;
423
424 return true;
425 }
426
427 // -------------------------------------------------------------------------- ;
428
429 // starting with version Lerc2v3: integer >> into local uint buffer, plus final memcpy;
430 // note this function gets called only for (0 < numBits < 32)
431
432 void BitStuffer2::BitStuff(Byte** ppByte, const vector<unsigned int>& dataVec, int numBits) const
433 {
434 unsigned int numElements = (unsigned int)dataVec.size();
435 unsigned int numUInts = (numElements * numBits + 31) / 32;
436 unsigned int numBytes = numUInts * sizeof(unsigned int);
437
438 m_tmpBitStuffVec.resize(numUInts);
439 unsigned int* dstPtr = &m_tmpBitStuffVec[0];
440
441 memset(dstPtr, 0, numBytes);
442
443 // do the stuffing
444 const unsigned int* srcPtr = &dataVec[0];
445 int bitPos = 0;
446
447 for (unsigned int i = 0; i < numElements; i++)
448 {
449 if (32 - bitPos >= numBits) // 0 < numBits < 32
450 {
451 *dstPtr |= (*srcPtr++) << bitPos;
452 bitPos += numBits;
453 if (bitPos == 32) // shift >= 32 is undefined
454 {
455 dstPtr++;
456 bitPos = 0;
457 }
458 }
459 else
460 {
461 *dstPtr++ |= (*srcPtr) << bitPos;
462 *dstPtr |= (*srcPtr++) >> (32 - bitPos); // bitPos > 0 here, always
463 bitPos += numBits - 32;
464 }
465 }
466
467 // copy the bytes to the outgoing byte stream
468 size_t numBytesUsed = numBytes - NumTailBytesNotNeeded(numElements, numBits);
469 memcpy(*ppByte, &m_tmpBitStuffVec[0], numBytesUsed);
470
471 *ppByte += numBytesUsed;
472 }
473
474 // -------------------------------------------------------------------------- ;
475
476 bool BitStuffer2::BitUnStuff(const Byte** ppByte, size_t& nBytesRemaining, vector<unsigned int>& dataVec,
477 unsigned int numElements, int numBits) const
478 {
479 if (numElements == 0 || numBits >= 32)
480 return false;
481
482 unsigned long long numUIntsLL = ((unsigned long long)numElements * numBits + 31) / 32;
483 unsigned long long numBytesLL = numUIntsLL * sizeof(unsigned int);
484
485 size_t numUInts = (size_t)numUIntsLL;
486 size_t numBytes = (size_t)numBytesLL; // could theoretically overflow on 32 bit system
487
488 if (numBytes != numBytesLL)
489 return false;
490
491 // copy the bytes from the incoming byte stream so the data is 4 byte memory aligned
492 const size_t numBytesUsed = numBytes - NumTailBytesNotNeeded(numElements, numBits);
493
494 if (nBytesRemaining < numBytesUsed)
495 return false;
496
497 try
498 {
499 dataVec.resize(numElements);
500 m_tmpBitStuffVec.resize(numUInts);
501 }
502 catch (const std::exception&)
503 {
504 return false;
505 }
506
507 m_tmpBitStuffVec[numUInts - 1] = 0; // set last uint to 0
508
509 memcpy(&m_tmpBitStuffVec[0], *ppByte, numBytesUsed);
510
511 // do the un-stuffing
512 unsigned int* srcPtr = &m_tmpBitStuffVec[0];
513 unsigned int* dstPtr = &dataVec[0];
514 int bitPos = 0;
515 int nb = 32 - numBits;
516
517 for (unsigned int i = 0; i < numElements; i++)
518 {
519 if (nb - bitPos >= 0)
520 {
521 *dstPtr++ = ((*srcPtr) << (nb - bitPos)) >> nb;
522 bitPos += numBits;
523 if (bitPos == 32) // shift >= 32 is undefined
524 {
525 srcPtr++;
526 bitPos = 0;
527 }
528 }
529 else
530 {
531 *dstPtr = (*srcPtr++) >> bitPos;
532 *dstPtr++ |= ((*srcPtr) << (64 - numBits - bitPos)) >> nb;
533 bitPos -= nb;
534 }
535 }
536
537 *ppByte += numBytesUsed;
538 nBytesRemaining -= numBytesUsed;
539 return true;
540 }
541
542 // -------------------------------------------------------------------------- ;
543