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)
64 const DWord top =
static_cast<DWord> (1) << 24;
65 const DWord bottom =
static_cast<DWord> (1) << 16;
66 const DWord maxRange =
static_cast<DWord> (1) << 16;
71 unsigned int input_size;
73 input_size =
static_cast<unsigned> (inputByteVector_arg.size ());
76 outputCharVector_.clear ();
77 outputCharVector_.reserve (
sizeof(
char) * input_size);
82 range =
static_cast<DWord> (-1);
85 for (i = 0; i < 257; i++)
89 while (readPos < input_size)
93 ch = inputByteVector_arg[readPos++];
96 low += freq[ch] * (range /= freq[256]);
97 range *= freq[ch + 1] - freq[ch];
100 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
102 out =
static_cast<char> (low >> 24);
105 outputCharVector_.push_back (out);
109 for (j = ch + 1; j < 257; j++)
113 if (freq[256] >= maxRange)
116 for (f = 1; f <= 256; f++)
119 if (freq[f] <= freq[f - 1])
120 freq[f] = freq[f - 1] + 1;
127 for (i = 0; i < 4; i++)
129 out =
static_cast<char> (low >> 24);
130 outputCharVector_.push_back (out);
135 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
137 return (static_cast<unsigned long> (outputCharVector_.size ()));
144 std::vector<char>& outputByteVector_arg)
148 unsigned int i, j, f;
151 const DWord top =
static_cast<DWord> (1) << 24;
152 const DWord bottom =
static_cast<DWord> (1) << 16;
153 const DWord maxRange =
static_cast<DWord> (1) << 16;
158 unsigned int outputBufPos;
159 unsigned int output_size =
static_cast<unsigned> (outputByteVector_arg.size ());
161 unsigned long streamByteCount;
169 range =
static_cast<DWord> (-1);
172 for (i = 0; i < 4; i++)
174 inputByteStream_arg.read (reinterpret_cast<char*> (&ch),
sizeof(
char));
175 streamByteCount +=
sizeof(char);
176 code = (code << 8) | ch;
180 for (i = 0; i <= 256; i++)
184 for (i = 0; i < output_size; i++)
187 uint8_t sSize = 256 / 2;
190 DWord count = (code - low) / (range /= freq[256]);
195 if (freq[symbol + sSize] <= count)
197 symbol =
static_cast<uint8_t
> (symbol + sSize);
203 outputByteVector_arg[outputBufPos++] = symbol;
206 low += freq[symbol] * range;
207 range *= freq[symbol + 1] - freq[symbol];
210 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
212 inputByteStream_arg.read (reinterpret_cast<char*> (&ch),
sizeof(
char));
213 streamByteCount +=
sizeof(char);
214 code = code << 8 | ch;
220 for (j = symbol + 1; j < 257; j++)
224 if (freq[256] >= maxRange)
227 for (f = 1; f <= 256; f++)
230 if (freq[f] <= freq[f - 1])
231 freq[f] = freq[f - 1] + 1;
236 return (streamByteCount);
243 std::ostream& outputByteStream_arg)
246 unsigned int inputsymbol;
250 uint64_t frequencyTableSize;
251 uint8_t frequencyTableByteSize;
254 const uint64_t top =
static_cast<uint64_t
> (1) << 56;
255 const uint64_t bottom =
static_cast<uint64_t
> (1) << 48;
256 const uint64_t maxRange =
static_cast<uint64_t
> (1) << 48;
258 unsigned long input_size =
static_cast<unsigned long> (inputIntVector_arg.size ());
261 unsigned int inputSymbol;
263 unsigned int readPos;
265 unsigned long streamByteCount;
270 outputCharVector_.clear ();
271 outputCharVector_.reserve ((
sizeof(
char) * input_size * 2));
273 frequencyTableSize = 1;
278 cFreqTable_[0] = cFreqTable_[1] = 0;
279 while (readPos < input_size)
281 inputSymbol = inputIntVector_arg[readPos++];
283 if (inputSymbol + 1 >= frequencyTableSize)
286 uint64_t oldfrequencyTableSize;
287 oldfrequencyTableSize = frequencyTableSize;
292 frequencyTableSize <<= 1;
293 }
while (inputSymbol + 1 > frequencyTableSize);
295 if (cFreqTable_.size () < frequencyTableSize + 1)
298 cFreqTable_.resize (static_cast<std::size_t> (frequencyTableSize + 1));
302 memset (&cFreqTable_[static_cast<std::size_t> (oldfrequencyTableSize + 1)], 0,
303 sizeof(uint64_t) * static_cast<std::size_t> (frequencyTableSize - oldfrequencyTableSize));
305 cFreqTable_[inputSymbol + 1]++;
307 frequencyTableSize++;
310 for (f = 1; f < frequencyTableSize; f++)
312 cFreqTable_[f] = cFreqTable_[f - 1] + cFreqTable_[f];
313 if (cFreqTable_[f] <= cFreqTable_[f - 1])
314 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
318 while (cFreqTable_[static_cast<std::size_t> (frequencyTableSize - 1)] >= maxRange)
320 for (f = 1; f < cFreqTable_.size (); f++)
324 if (cFreqTable_[f] <= cFreqTable_[f - 1])
325 cFreqTable_[f] = cFreqTable_[f - 1] + 1;
330 frequencyTableByteSize =
static_cast<uint8_t
> (ceil (
331 Log2 (static_cast<double> (cFreqTable_[static_cast<std::size_t> (frequencyTableSize - 1)])) / 8.0));
334 outputByteStream_arg.write (reinterpret_cast<const char*> (&frequencyTableSize),
sizeof(frequencyTableSize));
335 outputByteStream_arg.write (reinterpret_cast<const char*> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
337 streamByteCount +=
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
340 for (f = 1; f < frequencyTableSize; f++)
342 outputByteStream_arg.write (reinterpret_cast<const char*> (&cFreqTable_[f]), frequencyTableByteSize);
343 streamByteCount += frequencyTableByteSize;
348 range =
static_cast<uint64_t
> (-1);
351 while (readPos < input_size)
355 inputsymbol = inputIntVector_arg[readPos++];
358 low += cFreqTable_[inputsymbol] * (range /= cFreqTable_[
static_cast<std::size_t
> (frequencyTableSize - 1)]);
359 range *= cFreqTable_[inputsymbol + 1] - cFreqTable_[inputsymbol];
362 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
364 out =
static_cast<char> (low >> 56);
367 outputCharVector_.push_back (out);
373 for (i = 0; i < 8; i++)
375 out =
static_cast<char> (low >> 56);
376 outputCharVector_.push_back (out);
381 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
383 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
385 return (streamByteCount);
391 std::vector<unsigned int>& outputIntVector_arg)
397 const uint64_t top =
static_cast<uint64_t
> (1) << 56;
398 const uint64_t bottom =
static_cast<uint64_t
> (1) << 48;
403 unsigned int outputBufPos;
404 unsigned long output_size;
406 uint64_t frequencyTableSize;
407 unsigned char frequencyTableByteSize;
409 unsigned long streamByteCount;
414 output_size =
static_cast<unsigned long> (outputIntVector_arg.size ());
417 inputByteStream_arg.read (reinterpret_cast<char*> (&frequencyTableSize),
sizeof(frequencyTableSize));
418 inputByteStream_arg.read (reinterpret_cast<char*> (&frequencyTableByteSize),
sizeof(frequencyTableByteSize));
420 streamByteCount +=
sizeof(frequencyTableSize) +
sizeof(frequencyTableByteSize);
423 if (cFreqTable_.size () < frequencyTableSize)
425 cFreqTable_.resize (static_cast<std::size_t> (frequencyTableSize));
429 memset (&cFreqTable_[0], 0,
sizeof(uint64_t) * static_cast<std::size_t> (frequencyTableSize));
432 for (f = 1; f < frequencyTableSize; f++)
434 inputByteStream_arg.read (reinterpret_cast<char *> (&cFreqTable_[f]), frequencyTableByteSize);
435 streamByteCount += frequencyTableByteSize;
441 range =
static_cast<uint64_t
> (-1);
444 for (i = 0; i < 8; i++)
446 inputByteStream_arg.read (reinterpret_cast<char*> (&ch),
sizeof(
char));
447 streamByteCount +=
sizeof(char);
448 code = (code << 8) | ch;
452 for (i = 0; i < output_size; i++)
454 uint64_t count = (code - low) / (range /= cFreqTable_[static_cast<std::size_t> (frequencyTableSize - 1)]);
458 uint64_t sSize = (frequencyTableSize - 1) / 2;
461 if (cFreqTable_[static_cast<std::size_t> (symbol + sSize)] <= count)
469 outputIntVector_arg[outputBufPos++] =
static_cast<unsigned int> (symbol);
472 low += cFreqTable_[
static_cast<std::size_t
> (symbol)] * range;
473 range *= cFreqTable_[
static_cast<std::size_t
> (symbol + 1)] - cFreqTable_[static_cast<std::size_t> (symbol)];
476 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -low & (bottom - 1)), 1)))
478 inputByteStream_arg.read (reinterpret_cast<char*> (&ch),
sizeof(
char));
479 streamByteCount +=
sizeof(char);
480 code = code << 8 | ch;
487 return streamByteCount;
493 std::ostream& outputByteStream_arg)
501 const DWord top =
static_cast<DWord> (1) << 24;
502 const DWord bottom =
static_cast<DWord> (1) << 16;
503 const DWord maxRange =
static_cast<DWord> (1) << 16;
507 unsigned int input_size;
508 input_size =
static_cast<unsigned int> (inputByteVector_arg.size ());
510 unsigned int readPos;
512 unsigned long streamByteCount;
517 outputCharVector_.clear ();
518 outputCharVector_.reserve (
sizeof(
char) * input_size);
520 uint64_t FreqHist[257];
523 memset (FreqHist, 0,
sizeof(FreqHist));
525 while (readPos < input_size)
527 uint8_t symbol =
static_cast<uint8_t
> (inputByteVector_arg[readPos++]);
528 FreqHist[symbol + 1]++;
533 for (f = 1; f <= 256; f++)
535 freq[f] = freq[f - 1] +
static_cast<DWord> (FreqHist[f]);
536 if (freq[f] <= freq[f - 1])
537 freq[f] = freq[f - 1] + 1;
541 while (freq[256] >= maxRange)
543 for (f = 1; f <= 256; f++)
547 if (freq[f] <= freq[f - 1])
548 freq[f] = freq[f - 1] + 1;
553 outputByteStream_arg.write (reinterpret_cast<const char*> (&freq[0]),
sizeof(freq));
554 streamByteCount +=
sizeof(freq);
559 range =
static_cast<DWord> (-1);
562 while (readPos < input_size)
566 ch = inputByteVector_arg[readPos++];
569 low += freq[ch] * (range /= freq[256]);
570 range *= freq[ch + 1] - freq[ch];
573 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
575 out =
static_cast<char> (low >> 24);
578 outputCharVector_.push_back (out);
584 for (i = 0; i < 4; i++)
586 out =
static_cast<char> (low >> 24);
587 outputCharVector_.push_back (out);
592 outputByteStream_arg.write (&outputCharVector_[0], outputCharVector_.size ());
594 streamByteCount +=
static_cast<unsigned long> (outputCharVector_.size ());
596 return (streamByteCount);
602 std::vector<char>& outputByteVector_arg)
609 const DWord top =
static_cast<DWord> (1) << 24;
610 const DWord bottom =
static_cast<DWord> (1) << 16;
615 unsigned int outputBufPos;
616 unsigned int output_size;
618 unsigned long streamByteCount;
622 output_size =
static_cast<unsigned int> (outputByteVector_arg.size ());
627 inputByteStream_arg.read (reinterpret_cast<char*> (&freq[0]),
sizeof(freq));
628 streamByteCount +=
sizeof(freq);
632 range =
static_cast<DWord> (-1);
635 for (i = 0; i < 4; i++)
637 inputByteStream_arg.read (reinterpret_cast<char*> (&ch),
sizeof(
char));
638 streamByteCount +=
sizeof(char);
639 code = (code << 8) | ch;
643 for (i = 0; i < output_size; i++)
647 uint8_t sSize = 256 / 2;
649 DWord count = (code - low) / (range /= freq[256]);
653 if (freq[symbol + sSize] <= count)
655 symbol =
static_cast<uint8_t
> (symbol + sSize);
661 outputByteVector_arg[outputBufPos++] = symbol;
663 low += freq[symbol] * range;
664 range *= freq[symbol + 1] - freq[symbol];
667 while ((low ^ (low + range)) < top || ((range < bottom) && ((range = -int (low) & (bottom - 1)), 1)))
669 inputByteStream_arg.read (reinterpret_cast<char*> (&ch),
sizeof(
char));
670 streamByteCount +=
sizeof(char);
671 code = code << 8 | ch;
678 return (streamByteCount);
unsigned long encodeIntVectorToStream(std::vector< unsigned int > &inputIntVector_arg, std::ostream &outputByterStream_arg)
Encode integer vector to output stream.
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 decodeStreamToCharVector(std::istream &inputByteStream_arg, std::vector< char > &outputByteVector_arg)
Decode char stream to output vector.
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.