43 #include <pcl/compression/entropy_range_coder.h>
52 std::ostream& outputByteStream_arg)
57 constexpr
DWord top =
static_cast<DWord>(1) << 24;
58 constexpr
DWord bottom =
static_cast<DWord>(1) << 16;
59 constexpr
DWord maxRange =
static_cast<DWord>(1) << 16;
61 auto input_size =
static_cast<unsigned> (inputByteVector_arg.size ());
64 outputCharVector_.clear ();
65 outputCharVector_.reserve (
sizeof(
char) * input_size);
67 unsigned int readPos = 0;
73 for (
unsigned int i = 0; i < 257; i++)
77 while (readPos < input_size)
80 std::uint8_t ch = inputByteVector_arg[readPos++];
83 low += freq[ch] * (range /= freq[256]);
84 range *= freq[ch + 1] - freq[ch];
87 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -
static_cast<int>(low) & (bottom - 1)), 1)))
89 char out =
static_cast<char> (low >> 24);
92 outputCharVector_.push_back (out);
96 for (
unsigned int j = ch + 1; j < 257; j++)
100 if (freq[256] >= maxRange)
103 for (
unsigned int f = 1; f <= 256; f++)
106 if (freq[f] <= freq[f - 1])
107 freq[f] = freq[f - 1] + 1;
114 for (
unsigned int i = 0; i < 4; i++)
116 char out =
static_cast<char> (low >> 24);
117 outputCharVector_.push_back (out);
122 outputByteStream_arg.write (outputCharVector_.data(), outputCharVector_.size ());
124 return (
static_cast<unsigned long> (outputCharVector_.size ()));
130 std::vector<char>& outputByteVector_arg)
135 constexpr
DWord top =
static_cast<DWord>(1) << 24;
136 constexpr
DWord bottom =
static_cast<DWord>(1) << 16;
137 constexpr
DWord maxRange =
static_cast<DWord>(1) << 16;
139 auto output_size =
static_cast<unsigned> (outputByteVector_arg.size ());
141 unsigned long streamByteCount = 0;
143 unsigned int outputBufPos = 0;
150 for (
unsigned int i = 0; i < 4; i++)
153 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
154 streamByteCount +=
sizeof(char);
159 for (
unsigned int i = 0; i <= 256; i++)
163 for (
unsigned int i = 0; i < output_size; i++)
165 std::uint8_t symbol = 0;
166 std::uint8_t sSize = 256 / 2;
169 DWord count = (
code - low) / (range /= freq[256]);
174 if (freq[symbol + sSize] <= count)
176 symbol =
static_cast<std::uint8_t
> (symbol + sSize);
182 outputByteVector_arg[outputBufPos++] = symbol;
185 low += freq[symbol] * range;
186 range *= freq[symbol + 1] - freq[symbol];
189 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -
static_cast<int>(low) & (bottom - 1)), 1)))
192 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
193 streamByteCount +=
sizeof(char);
200 for (
unsigned int j = symbol + 1; j < 257; j++)
204 if (freq[256] >= maxRange)
207 for (
unsigned int f = 1; f <= 256; f++)
210 if (freq[f] <= freq[f - 1])
211 freq[f] = freq[f - 1] + 1;
216 return (streamByteCount);
222 std::ostream& outputByteStream_arg)
225 constexpr std::uint64_t top =
static_cast<std::uint64_t
>(1) << 56;
226 constexpr std::uint64_t bottom =
static_cast<std::uint64_t
>(1) << 48;
227 constexpr std::uint64_t maxRange =
static_cast<std::uint64_t
>(1) << 48;
229 auto input_size =
static_cast<unsigned long> (inputIntVector_arg.size ());
232 outputCharVector_.clear ();
233 outputCharVector_.reserve ((
sizeof(
char) * input_size * 2));
235 std::uint64_t frequencyTableSize = 1;
237 unsigned int readPos = 0;
240 cFreqTable_[0] = cFreqTable_[1] = 0;
241 while (readPos < input_size)
243 unsigned int inputSymbol = inputIntVector_arg[readPos++];
245 if (inputSymbol + 1 >= frequencyTableSize)
248 std::uint64_t oldfrequencyTableSize;
249 oldfrequencyTableSize = frequencyTableSize;
254 frequencyTableSize <<= 1;
255 }
while (inputSymbol + 1 > frequencyTableSize);
257 if (cFreqTable_.size () < frequencyTableSize + 1)
260 cFreqTable_.resize (
static_cast<std::size_t
> (frequencyTableSize + 1));
264 std::fill_n(&cFreqTable_[oldfrequencyTableSize + 1],
265 frequencyTableSize - oldfrequencyTableSize, 0);
267 cFreqTable_[inputSymbol + 1]++;
269 frequencyTableSize++;
272 for (std::uint64_t f = 1; f < frequencyTableSize; f++)
274 cFreqTable_[f] = cFreqTable_[f - 1] + cFreqTable_[f];
275 if (cFreqTable_[f] <= cFreqTable_[f - 1])
276 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
280 while (cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)] >= maxRange)
282 for (std::size_t f = 1; f < cFreqTable_.size (); f++)
286 if (cFreqTable_[f] <= cFreqTable_[f - 1])
287 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
292 auto frequencyTableByteSize =
static_cast<std::uint8_t
> (std::ceil (
293 std::log2 (
static_cast<double> (cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)] + 1)) / 8.0));
296 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&frequencyTableSize),
sizeof(frequencyTableSize));
297 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
299 unsigned long streamByteCount =
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
302 for (std::uint64_t f = 1; f < frequencyTableSize; f++)
304 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&cFreqTable_[f]), frequencyTableByteSize);
305 streamByteCount += frequencyTableByteSize;
309 std::uint64_t low = 0;
310 auto range =
static_cast<std::uint64_t
> (-1);
313 while (readPos < input_size)
317 unsigned int inputsymbol = inputIntVector_arg[readPos++];
320 low += cFreqTable_[inputsymbol] * (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
321 range *= cFreqTable_[inputsymbol + 1] - cFreqTable_[inputsymbol];
324 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
326 char out =
static_cast<char> (low >> 56);
329 outputCharVector_.push_back (out);
335 for (
unsigned int i = 0; i < 8; i++)
337 char out =
static_cast<char> (low >> 56);
338 outputCharVector_.push_back (out);
343 outputByteStream_arg.write (outputCharVector_.data(), outputCharVector_.size ());
345 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
347 return (streamByteCount);
353 std::vector<unsigned int>& outputIntVector_arg)
356 constexpr std::uint64_t top =
static_cast<std::uint64_t
>(1) << 56;
357 constexpr std::uint64_t bottom =
static_cast<std::uint64_t
>(1) << 48;
359 std::uint64_t frequencyTableSize;
360 unsigned char frequencyTableByteSize;
362 unsigned int outputBufPos = 0;
363 std::size_t output_size = outputIntVector_arg.size ();
366 inputByteStream_arg.read (
reinterpret_cast<char*
> (&frequencyTableSize),
sizeof(frequencyTableSize));
367 inputByteStream_arg.read (
reinterpret_cast<char*
> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
369 unsigned long streamByteCount =
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
372 if (cFreqTable_.size () < frequencyTableSize)
374 cFreqTable_.resize (
static_cast<std::size_t
> (frequencyTableSize));
378 std::fill(cFreqTable_.begin(), cFreqTable_.end(), 0);
381 for (std::uint64_t f = 1; f < frequencyTableSize; f++)
383 inputByteStream_arg.read (
reinterpret_cast<char *
> (&cFreqTable_[f]), frequencyTableByteSize);
384 streamByteCount += frequencyTableByteSize;
388 std::uint64_t
code = 0;
389 std::uint64_t low = 0;
390 auto range =
static_cast<std::uint64_t
> (-1);
393 for (
unsigned int i = 0; i < 8; i++)
396 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
397 streamByteCount +=
sizeof(char);
402 for (std::size_t i = 0; i < output_size; i++)
404 std::uint64_t count = (
code - low) / (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
407 std::uint64_t symbol = 0;
408 std::uint64_t sSize = (frequencyTableSize - 1) / 2;
411 if (cFreqTable_[
static_cast<std::size_t
> (symbol + sSize)] <= count)
419 outputIntVector_arg[outputBufPos++] =
static_cast<unsigned int> (symbol);
422 low += cFreqTable_[
static_cast<std::size_t
> (symbol)] * range;
423 range *= cFreqTable_[
static_cast<std::size_t
> (symbol + 1)] - cFreqTable_[
static_cast<std::size_t
> (symbol)];
426 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
429 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
430 streamByteCount +=
sizeof(char);
437 return streamByteCount;
443 std::ostream& outputByteStream_arg)
448 constexpr
DWord top =
static_cast<DWord>(1) << 24;
449 constexpr
DWord bottom =
static_cast<DWord>(1) << 16;
450 constexpr
DWord maxRange =
static_cast<DWord>(1) << 16;
454 unsigned int input_size;
455 input_size =
static_cast<unsigned int> (inputByteVector_arg.size ());
458 outputCharVector_.clear ();
459 outputCharVector_.reserve (
sizeof(
char) * input_size);
461 std::uint64_t FreqHist[257]{};
464 unsigned int readPos = 0;
465 while (readPos < input_size)
467 auto symbol =
static_cast<std::uint8_t
> (inputByteVector_arg[readPos++]);
468 FreqHist[symbol + 1]++;
473 for (
int f = 1; f <= 256; f++)
475 freq[f] = freq[f - 1] +
static_cast<DWord> (FreqHist[f]);
476 if (freq[f] <= freq[f - 1])
477 freq[f] = freq[f - 1] + 1;
481 while (freq[256] >= maxRange)
483 for (
int f = 1; f <= 256; f++)
487 if (freq[f] <= freq[f - 1])
488 freq[f] = freq[f - 1] + 1;
493 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&freq[0]),
sizeof(freq));
494 unsigned long streamByteCount =
sizeof(freq);
499 range =
static_cast<DWord> (-1);
502 while (readPos < input_size)
505 std::uint8_t ch = inputByteVector_arg[readPos++];
508 low += freq[ch] * (range /= freq[256]);
509 range *= freq[ch + 1] - freq[ch];
512 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -
static_cast<int>(low) & (bottom - 1)), 1)))
514 char out =
static_cast<char> (low >> 24);
517 outputCharVector_.push_back (out);
523 for (
int i = 0; i < 4; i++)
525 char out =
static_cast<char> (low >> 24);
526 outputCharVector_.push_back (out);
531 outputByteStream_arg.write (outputCharVector_.data(), outputCharVector_.size ());
533 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
535 return (streamByteCount);
541 std::vector<char>& outputByteVector_arg)
546 constexpr
DWord top =
static_cast<DWord>(1) << 24;
547 constexpr
DWord bottom =
static_cast<DWord>(1) << 16;
552 unsigned int outputBufPos;
553 unsigned int output_size;
555 unsigned long streamByteCount;
559 output_size =
static_cast<unsigned int> (outputByteVector_arg.size ());
564 inputByteStream_arg.read (
reinterpret_cast<char*
> (&freq[0]),
sizeof(freq));
565 streamByteCount +=
sizeof(freq);
569 range =
static_cast<DWord> (-1);
572 for (
unsigned int i = 0; i < 4; i++)
575 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
576 streamByteCount +=
sizeof(char);
581 for (
unsigned int i = 0; i < output_size; i++)
584 std::uint8_t symbol = 0;
585 std::uint8_t sSize = 256 / 2;
587 DWord count = (
code - low) / (range /= freq[256]);
591 if (freq[symbol + sSize] <= count)
593 symbol =
static_cast<std::uint8_t
> (symbol + sSize);
599 outputByteVector_arg[outputBufPos++] = symbol;
601 low += freq[symbol] * range;
602 range *= freq[symbol + 1] - freq[symbol];
605 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -
static_cast<int>(low) & (bottom - 1)), 1)))
608 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
609 streamByteCount +=
sizeof(char);
617 return (streamByteCount);
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
unsigned long decodeStreamToIntVector(std::istream &inputByteStream_arg, std::vector< unsigned int > &outputIntVector_arg)
Decode stream to output integer vector.
unsigned long encodeCharVectorToStream(const std::vector< char > &inputByteVector_arg, std::ostream &outputByteStream_arg)
Encode char vector to output stream.
unsigned long decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
unsigned long encodeIntVectorToStream(std::vector< unsigned int > &inputIntVector_arg, std::ostream &outputByterStream_arg)
Encode integer vector to output stream.