42 #ifndef __PCL_IO_RANGECODING__HPP
43 #define __PCL_IO_RANGECODING__HPP
45 #include <pcl/compression/entropy_range_coder.h>
54 std::ostream& outputByteStream_arg)
59 const DWord top =
static_cast<DWord> (1) << 24;
60 const DWord bottom =
static_cast<DWord> (1) << 16;
61 const DWord maxRange =
static_cast<DWord> (1) << 16;
63 unsigned int input_size =
static_cast<unsigned> (inputByteVector_arg.size ());
66 outputCharVector_.clear ();
67 outputCharVector_.reserve (
sizeof(
char) * input_size);
69 unsigned int readPos = 0;
75 for (
unsigned int i = 0; i < 257; i++)
79 while (readPos < input_size)
82 std::uint8_t ch = inputByteVector_arg[readPos++];
85 low += freq[ch] * (range /= freq[256]);
86 range *= freq[ch + 1] - freq[ch];
89 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
91 char out =
static_cast<char> (low >> 24);
94 outputCharVector_.push_back (out);
98 for (
unsigned int j = ch + 1; j < 257; j++)
102 if (freq[256] >= maxRange)
105 for (
unsigned int f = 1; f <= 256; f++)
108 if (freq[f] <= freq[f - 1])
109 freq[f] = freq[f - 1] + 1;
116 for (
unsigned int i = 0; i < 4; i++)
118 char out =
static_cast<char> (low >> 24);
119 outputCharVector_.push_back (out);
124 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
126 return (
static_cast<unsigned long> (outputCharVector_.size ()));
132 std::vector<char>& outputByteVector_arg)
137 const DWord top =
static_cast<DWord> (1) << 24;
138 const DWord bottom =
static_cast<DWord> (1) << 16;
139 const DWord maxRange =
static_cast<DWord> (1) << 16;
141 unsigned int output_size =
static_cast<unsigned> (outputByteVector_arg.size ());
143 unsigned long streamByteCount = 0;
145 unsigned int outputBufPos = 0;
152 for (
unsigned int i = 0; i < 4; i++)
155 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
156 streamByteCount +=
sizeof(char);
157 code = (code << 8) | ch;
161 for (
unsigned int i = 0; i <= 256; i++)
165 for (
unsigned int i = 0; i < output_size; i++)
167 std::uint8_t symbol = 0;
168 std::uint8_t sSize = 256 / 2;
171 DWord count = (code - low) / (range /= freq[256]);
176 if (freq[symbol + sSize] <= count)
178 symbol =
static_cast<std::uint8_t
> (symbol + sSize);
184 outputByteVector_arg[outputBufPos++] = symbol;
187 low += freq[symbol] * range;
188 range *= freq[symbol + 1] - freq[symbol];
191 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
194 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
195 streamByteCount +=
sizeof(char);
196 code = code << 8 | ch;
202 for (
unsigned int j = symbol + 1; j < 257; j++)
206 if (freq[256] >= maxRange)
209 for (
unsigned int f = 1; f <= 256; f++)
212 if (freq[f] <= freq[f - 1])
213 freq[f] = freq[f - 1] + 1;
218 return (streamByteCount);
224 std::ostream& outputByteStream_arg)
227 const std::uint64_t top =
static_cast<std::uint64_t
> (1) << 56;
228 const std::uint64_t bottom =
static_cast<std::uint64_t
> (1) << 48;
229 const std::uint64_t maxRange =
static_cast<std::uint64_t
> (1) << 48;
231 unsigned long input_size =
static_cast<unsigned long> (inputIntVector_arg.size ());
234 outputCharVector_.clear ();
235 outputCharVector_.reserve ((
sizeof(
char) * input_size * 2));
237 std::uint64_t frequencyTableSize = 1;
239 unsigned int readPos = 0;
242 cFreqTable_[0] = cFreqTable_[1] = 0;
243 while (readPos < input_size)
245 unsigned int inputSymbol = inputIntVector_arg[readPos++];
247 if (inputSymbol + 1 >= frequencyTableSize)
250 std::uint64_t oldfrequencyTableSize;
251 oldfrequencyTableSize = frequencyTableSize;
256 frequencyTableSize <<= 1;
257 }
while (inputSymbol + 1 > frequencyTableSize);
259 if (cFreqTable_.size () < frequencyTableSize + 1)
262 cFreqTable_.resize (
static_cast<std::size_t
> (frequencyTableSize + 1));
266 memset (&cFreqTable_[
static_cast<std::size_t
> (oldfrequencyTableSize + 1)], 0,
267 sizeof(std::uint64_t) *
static_cast<std::size_t
> (frequencyTableSize - oldfrequencyTableSize));
269 cFreqTable_[inputSymbol + 1]++;
271 frequencyTableSize++;
274 for (std::uint64_t f = 1; f < frequencyTableSize; f++)
276 cFreqTable_[f] = cFreqTable_[f - 1] + cFreqTable_[f];
277 if (cFreqTable_[f] <= cFreqTable_[f - 1])
278 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
282 while (cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)] >= maxRange)
284 for (std::size_t f = 1; f < cFreqTable_.size (); f++)
288 if (cFreqTable_[f] <= cFreqTable_[f - 1])
289 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
294 std::uint8_t frequencyTableByteSize =
static_cast<std::uint8_t
> (std::ceil (
295 std::log2 (
static_cast<double> (cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)] + 1)) / 8.0));
298 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&frequencyTableSize),
sizeof(frequencyTableSize));
299 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
301 unsigned long streamByteCount =
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
304 for (std::uint64_t f = 1; f < frequencyTableSize; f++)
306 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&cFreqTable_[f]), frequencyTableByteSize);
307 streamByteCount += frequencyTableByteSize;
311 std::uint64_t low = 0;
312 std::uint64_t range =
static_cast<std::uint64_t
> (-1);
315 while (readPos < input_size)
319 unsigned int inputsymbol = inputIntVector_arg[readPos++];
322 low += cFreqTable_[inputsymbol] * (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
323 range *= cFreqTable_[inputsymbol + 1] - cFreqTable_[inputsymbol];
326 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
328 char out =
static_cast<char> (low >> 56);
331 outputCharVector_.push_back (out);
337 for (
unsigned int i = 0; i < 8; i++)
339 char out =
static_cast<char> (low >> 56);
340 outputCharVector_.push_back (out);
345 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
347 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
349 return (streamByteCount);
355 std::vector<unsigned int>& outputIntVector_arg)
358 const std::uint64_t top =
static_cast<std::uint64_t
> (1) << 56;
359 const std::uint64_t bottom =
static_cast<std::uint64_t
> (1) << 48;
361 std::uint64_t frequencyTableSize;
362 unsigned char frequencyTableByteSize;
364 unsigned int outputBufPos = 0;
365 std::size_t output_size = outputIntVector_arg.size ();
368 inputByteStream_arg.read (
reinterpret_cast<char*
> (&frequencyTableSize),
sizeof(frequencyTableSize));
369 inputByteStream_arg.read (
reinterpret_cast<char*
> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
371 unsigned long streamByteCount =
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
374 if (cFreqTable_.size () < frequencyTableSize)
376 cFreqTable_.resize (
static_cast<std::size_t
> (frequencyTableSize));
380 memset (&cFreqTable_[0], 0,
sizeof(std::uint64_t) *
static_cast<std::size_t
> (frequencyTableSize));
383 for (std::uint64_t f = 1; f < frequencyTableSize; f++)
385 inputByteStream_arg.read (
reinterpret_cast<char *
> (&cFreqTable_[f]), frequencyTableByteSize);
386 streamByteCount += frequencyTableByteSize;
390 std::uint64_t code = 0;
391 std::uint64_t low = 0;
392 std::uint64_t range =
static_cast<std::uint64_t
> (-1);
395 for (
unsigned int i = 0; i < 8; i++)
398 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
399 streamByteCount +=
sizeof(char);
400 code = (code << 8) | ch;
404 for (std::size_t i = 0; i < output_size; i++)
406 std::uint64_t count = (code - low) / (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
409 std::uint64_t symbol = 0;
410 std::uint64_t sSize = (frequencyTableSize - 1) / 2;
413 if (cFreqTable_[
static_cast<std::size_t
> (symbol + sSize)] <= count)
421 outputIntVector_arg[outputBufPos++] =
static_cast<unsigned int> (symbol);
424 low += cFreqTable_[
static_cast<std::size_t
> (symbol)] * range;
425 range *= cFreqTable_[
static_cast<std::size_t
> (symbol + 1)] - cFreqTable_[
static_cast<std::size_t
> (symbol)];
428 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
431 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
432 streamByteCount +=
sizeof(char);
433 code = code << 8 | ch;
439 return streamByteCount;
445 std::ostream& outputByteStream_arg)
450 const DWord top =
static_cast<DWord> (1) << 24;
451 const DWord bottom =
static_cast<DWord> (1) << 16;
452 const DWord maxRange =
static_cast<DWord> (1) << 16;
456 unsigned int input_size;
457 input_size =
static_cast<unsigned int> (inputByteVector_arg.size ());
460 outputCharVector_.clear ();
461 outputCharVector_.reserve (
sizeof(
char) * input_size);
463 std::uint64_t FreqHist[257];
466 memset (FreqHist, 0,
sizeof(FreqHist));
467 unsigned int readPos = 0;
468 while (readPos < input_size)
470 std::uint8_t symbol =
static_cast<std::uint8_t
> (inputByteVector_arg[readPos++]);
471 FreqHist[symbol + 1]++;
476 for (
int f = 1; f <= 256; f++)
478 freq[f] = freq[f - 1] +
static_cast<DWord> (FreqHist[f]);
479 if (freq[f] <= freq[f - 1])
480 freq[f] = freq[f - 1] + 1;
484 while (freq[256] >= maxRange)
486 for (
int f = 1; f <= 256; f++)
490 if (freq[f] <= freq[f - 1])
491 freq[f] = freq[f - 1] + 1;
496 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&freq[0]),
sizeof(freq));
497 unsigned long streamByteCount =
sizeof(freq);
502 range =
static_cast<DWord> (-1);
505 while (readPos < input_size)
508 std::uint8_t ch = inputByteVector_arg[readPos++];
511 low += freq[ch] * (range /= freq[256]);
512 range *= freq[ch + 1] - freq[ch];
515 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
517 char out =
static_cast<char> (low >> 24);
520 outputCharVector_.push_back (out);
526 for (
int i = 0; i < 4; i++)
528 char out =
static_cast<char> (low >> 24);
529 outputCharVector_.push_back (out);
534 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
536 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
538 return (streamByteCount);
544 std::vector<char>& outputByteVector_arg)
549 const DWord top =
static_cast<DWord> (1) << 24;
550 const DWord bottom =
static_cast<DWord> (1) << 16;
555 unsigned int outputBufPos;
556 unsigned int output_size;
558 unsigned long streamByteCount;
562 output_size =
static_cast<unsigned int> (outputByteVector_arg.size ());
567 inputByteStream_arg.read (
reinterpret_cast<char*
> (&freq[0]),
sizeof(freq));
568 streamByteCount +=
sizeof(freq);
572 range =
static_cast<DWord> (-1);
575 for (
unsigned int i = 0; i < 4; i++)
578 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
579 streamByteCount +=
sizeof(char);
580 code = (code << 8) | ch;
584 for (
unsigned int i = 0; i < output_size; i++)
587 std::uint8_t symbol = 0;
588 std::uint8_t sSize = 256 / 2;
590 DWord count = (code - low) / (range /= freq[256]);
594 if (freq[symbol + sSize] <= count)
596 symbol =
static_cast<std::uint8_t
> (symbol + sSize);
602 outputByteVector_arg[outputBufPos++] = symbol;
604 low += freq[symbol] * range;
605 range *= freq[symbol + 1] - freq[symbol];
608 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
611 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
612 streamByteCount +=
sizeof(char);
613 code = code << 8 | ch;
620 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.