42 #ifndef __PCL_IO_RANGECODING__HPP
43 #define __PCL_IO_RANGECODING__HPP
45 #include <pcl/compression/entropy_range_coder.h>
56 std::ostream& outputByteStream_arg)
61 const DWord top =
static_cast<DWord> (1) << 24;
62 const DWord bottom =
static_cast<DWord> (1) << 16;
63 const DWord maxRange =
static_cast<DWord> (1) << 16;
65 unsigned int input_size =
static_cast<unsigned> (inputByteVector_arg.size ());
68 outputCharVector_.clear ();
69 outputCharVector_.reserve (
sizeof(
char) * input_size);
71 unsigned int readPos = 0;
77 for (
unsigned int i = 0; i < 257; i++)
81 while (readPos < input_size)
87 low += freq[ch] * (range /= freq[256]);
88 range *= freq[ch + 1] - freq[ch];
91 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
93 char out =
static_cast<char> (low >> 24);
96 outputCharVector_.push_back (out);
100 for (
unsigned int j = ch + 1; j < 257; j++)
104 if (freq[256] >= maxRange)
107 for (
unsigned int f = 1; f <= 256; f++)
110 if (freq[f] <= freq[f - 1])
111 freq[f] = freq[f - 1] + 1;
118 for (
unsigned int i = 0; i < 4; i++)
120 char out =
static_cast<char> (low >> 24);
121 outputCharVector_.push_back (out);
126 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
128 return (
static_cast<unsigned long> (outputCharVector_.size ()));
134 std::vector<char>& outputByteVector_arg)
139 const DWord top =
static_cast<DWord> (1) << 24;
140 const DWord bottom =
static_cast<DWord> (1) << 16;
141 const DWord maxRange =
static_cast<DWord> (1) << 16;
143 unsigned int output_size =
static_cast<unsigned> (outputByteVector_arg.size ());
145 unsigned long streamByteCount = 0;
147 unsigned int outputBufPos = 0;
154 for (
unsigned int i = 0; i < 4; i++)
157 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
158 streamByteCount +=
sizeof(char);
159 code = (code << 8) | ch;
163 for (
unsigned int i = 0; i <= 256; i++)
167 for (
unsigned int i = 0; i < output_size; i++)
173 DWord count = (code - low) / (range /= freq[256]);
178 if (freq[symbol + sSize] <= count)
186 outputByteVector_arg[outputBufPos++] = symbol;
189 low += freq[symbol] * range;
190 range *= freq[symbol + 1] - freq[symbol];
193 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
196 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
197 streamByteCount +=
sizeof(char);
198 code = code << 8 | ch;
204 for (
unsigned int j = symbol + 1; j < 257; j++)
208 if (freq[256] >= maxRange)
211 for (
unsigned int f = 1; f <= 256; f++)
214 if (freq[f] <= freq[f - 1])
215 freq[f] = freq[f - 1] + 1;
220 return (streamByteCount);
226 std::ostream& outputByteStream_arg)
233 unsigned long input_size =
static_cast<unsigned long> (inputIntVector_arg.size ());
236 outputCharVector_.clear ();
237 outputCharVector_.reserve ((
sizeof(
char) * input_size * 2));
241 unsigned int readPos = 0;
244 cFreqTable_[0] = cFreqTable_[1] = 0;
245 while (readPos < input_size)
247 unsigned int inputSymbol = inputIntVector_arg[readPos++];
249 if (inputSymbol + 1 >= frequencyTableSize)
253 oldfrequencyTableSize = frequencyTableSize;
258 frequencyTableSize <<= 1;
259 }
while (inputSymbol + 1 > frequencyTableSize);
261 if (cFreqTable_.size () < frequencyTableSize + 1)
264 cFreqTable_.resize (
static_cast<std::size_t
> (frequencyTableSize + 1));
268 memset (&cFreqTable_[
static_cast<std::size_t
> (oldfrequencyTableSize + 1)], 0,
269 sizeof(
std::uint64_t) *
static_cast<std::size_t
> (frequencyTableSize - oldfrequencyTableSize));
271 cFreqTable_[inputSymbol + 1]++;
273 frequencyTableSize++;
278 cFreqTable_[f] = cFreqTable_[f - 1] + cFreqTable_[f];
279 if (cFreqTable_[f] <= cFreqTable_[f - 1])
280 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
284 while (cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)] >= maxRange)
286 for (std::size_t f = 1; f < cFreqTable_.size (); f++)
290 if (cFreqTable_[f] <= cFreqTable_[f - 1])
291 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
297 std::log2 (
static_cast<double> (cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)] + 1)) / 8.0));
300 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&frequencyTableSize),
sizeof(frequencyTableSize));
301 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
303 unsigned long streamByteCount =
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
308 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&cFreqTable_[f]), frequencyTableByteSize);
309 streamByteCount += frequencyTableByteSize;
317 while (readPos < input_size)
321 unsigned int inputsymbol = inputIntVector_arg[readPos++];
324 low += cFreqTable_[inputsymbol] * (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
325 range *= cFreqTable_[inputsymbol + 1] - cFreqTable_[inputsymbol];
328 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
330 char out =
static_cast<char> (low >> 56);
333 outputCharVector_.push_back (out);
339 for (
unsigned int i = 0; i < 8; i++)
341 char out =
static_cast<char> (low >> 56);
342 outputCharVector_.push_back (out);
347 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
349 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
351 return (streamByteCount);
357 std::vector<unsigned int>& outputIntVector_arg)
364 unsigned char frequencyTableByteSize;
366 unsigned int outputBufPos = 0;
367 std::size_t output_size = outputIntVector_arg.size ();
370 inputByteStream_arg.read (
reinterpret_cast<char*
> (&frequencyTableSize),
sizeof(frequencyTableSize));
371 inputByteStream_arg.read (
reinterpret_cast<char*
> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
373 unsigned long streamByteCount =
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
376 if (cFreqTable_.size () < frequencyTableSize)
378 cFreqTable_.resize (
static_cast<std::size_t
> (frequencyTableSize));
382 memset (&cFreqTable_[0], 0,
sizeof(
std::uint64_t) *
static_cast<std::size_t
> (frequencyTableSize));
387 inputByteStream_arg.read (
reinterpret_cast<char *
> (&cFreqTable_[f]), frequencyTableByteSize);
388 streamByteCount += frequencyTableByteSize;
397 for (
unsigned int i = 0; i < 8; i++)
400 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
401 streamByteCount +=
sizeof(char);
402 code = (code << 8) | ch;
406 for (std::size_t i = 0; i < output_size; i++)
408 std::uint64_t count = (code - low) / (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
415 if (cFreqTable_[
static_cast<std::size_t
> (symbol + sSize)] <= count)
423 outputIntVector_arg[outputBufPos++] =
static_cast<unsigned int> (symbol);
426 low += cFreqTable_[
static_cast<std::size_t
> (symbol)] * range;
427 range *= cFreqTable_[
static_cast<std::size_t
> (symbol + 1)] - cFreqTable_[
static_cast<std::size_t
> (symbol)];
430 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
433 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
434 streamByteCount +=
sizeof(char);
435 code = code << 8 | ch;
441 return streamByteCount;
447 std::ostream& outputByteStream_arg)
452 const DWord top =
static_cast<DWord> (1) << 24;
453 const DWord bottom =
static_cast<DWord> (1) << 16;
454 const DWord maxRange =
static_cast<DWord> (1) << 16;
458 unsigned int input_size;
459 input_size =
static_cast<unsigned int> (inputByteVector_arg.size ());
462 outputCharVector_.clear ();
463 outputCharVector_.reserve (
sizeof(
char) * input_size);
468 memset (FreqHist, 0,
sizeof(FreqHist));
469 unsigned int readPos = 0;
470 while (readPos < input_size)
473 FreqHist[symbol + 1]++;
478 for (
int f = 1; f <= 256; f++)
480 freq[f] = freq[f - 1] +
static_cast<DWord> (FreqHist[f]);
481 if (freq[f] <= freq[f - 1])
482 freq[f] = freq[f - 1] + 1;
486 while (freq[256] >= maxRange)
488 for (
int f = 1; f <= 256; f++)
492 if (freq[f] <= freq[f - 1])
493 freq[f] = freq[f - 1] + 1;
498 outputByteStream_arg.write (
reinterpret_cast<const char*
> (&freq[0]),
sizeof(freq));
499 unsigned long streamByteCount =
sizeof(freq);
504 range =
static_cast<DWord> (-1);
507 while (readPos < input_size)
513 low += freq[ch] * (range /= freq[256]);
514 range *= freq[ch + 1] - freq[ch];
517 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
519 char out =
static_cast<char> (low >> 24);
522 outputCharVector_.push_back (out);
528 for (
int i = 0; i < 4; i++)
530 char out =
static_cast<char> (low >> 24);
531 outputCharVector_.push_back (out);
536 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
538 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
540 return (streamByteCount);
546 std::vector<char>& outputByteVector_arg)
551 const DWord top =
static_cast<DWord> (1) << 24;
552 const DWord bottom =
static_cast<DWord> (1) << 16;
557 unsigned int outputBufPos;
558 unsigned int output_size;
560 unsigned long streamByteCount;
564 output_size =
static_cast<unsigned int> (outputByteVector_arg.size ());
569 inputByteStream_arg.read (
reinterpret_cast<char*
> (&freq[0]),
sizeof(freq));
570 streamByteCount +=
sizeof(freq);
574 range =
static_cast<DWord> (-1);
577 for (
unsigned int i = 0; i < 4; i++)
580 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
581 streamByteCount +=
sizeof(char);
582 code = (code << 8) | ch;
586 for (
unsigned int i = 0; i < output_size; i++)
592 DWord count = (code - low) / (range /= freq[256]);
596 if (freq[symbol + sSize] <= count)
604 outputByteVector_arg[outputBufPos++] = symbol;
606 low += freq[symbol] * range;
607 range *= freq[symbol + 1] - freq[symbol];
610 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
613 inputByteStream_arg.read (
reinterpret_cast<char*
> (&ch),
sizeof(char));
614 streamByteCount +=
sizeof(char);
615 code = code << 8 | ch;
622 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.