rvl_codec.cpp
Go to the documentation of this file.
1 // The following code is a C++ wrapper of the code presented by
2 // Andrew D. Wilson in "Fast Lossless Depth Image Compression" at SIGCHI'17.
3 // The original code is licensed under the MIT License.
4 
6 
8 
10 
11 void RvlCodec::EncodeVLE(int value) {
12  do {
13  int nibble = value & 0x7; // lower 3 bits
14  if (value >>= 3) nibble |= 0x8; // more to come
15  word_ <<= 4;
16  word_ |= nibble;
17  if (++nibblesWritten_ == 8) // output word
18  {
19  *pBuffer_++ = word_;
20  nibblesWritten_ = 0;
21  word_ = 0;
22  }
23  } while (value);
24 }
25 
27  unsigned int nibble;
28  int value = 0, bits = 29;
29  do {
30  if (!nibblesWritten_) {
31  word_ = *pBuffer_++; // load word
32  nibblesWritten_ = 8;
33  }
34  nibble = word_ & 0xf0000000;
35  value |= (nibble << 1) >> bits;
36  word_ <<= 4;
38  bits -= 3;
39  } while (nibble & 0x80000000);
40  return value;
41 }
42 
43 int RvlCodec::CompressRVL(const unsigned short* input, unsigned char* output,
44  int numPixels) {
45  buffer_ = pBuffer_ = (int*)output;
46  nibblesWritten_ = 0;
47  const unsigned short* end = input + numPixels;
48  unsigned short previous = 0;
49  while (input != end) {
50  int zeros = 0, nonzeros = 0;
51  for (; (input != end) && !*input; input++, zeros++)
52  ;
53  EncodeVLE(zeros); // number of zeros
54  for (const unsigned short* p = input; (p != end) && *p++; nonzeros++)
55  ;
56  EncodeVLE(nonzeros); // number of nonzeros
57  for (int i = 0; i < nonzeros; i++) {
58  unsigned short current = *input++;
59  int delta = current - previous;
60  int positive = (delta << 1) ^ (delta >> 31);
61  EncodeVLE(positive); // nonzero value
62  previous = current;
63  }
64  }
65  if (nibblesWritten_) // last few values
66  *pBuffer_++ = word_ << 4 * (8 - nibblesWritten_);
67  return int((unsigned char*)pBuffer_ - (unsigned char*)buffer_); // num bytes
68 }
69 
70 void RvlCodec::DecompressRVL(const unsigned char* input, unsigned short* output,
71  int numPixels) {
72  buffer_ = pBuffer_ = const_cast<int*>(reinterpret_cast<const int*>(input));
73  nibblesWritten_ = 0;
74  unsigned short current, previous = 0;
75  int numPixelsToDecode = numPixels;
76  while (numPixelsToDecode) {
77  int zeros = DecodeVLE(); // number of zeros
78  numPixelsToDecode -= zeros;
79  for (; zeros; zeros--) *output++ = 0;
80  int nonzeros = DecodeVLE(); // number of nonzeros
81  numPixelsToDecode -= nonzeros;
82  for (; nonzeros; nonzeros--) {
83  int positive = DecodeVLE(); // nonzero value
84  int delta = (positive >> 1) ^ -(positive & 1);
85  current = previous + delta;
86  *output++ = current;
87  previous = current;
88  }
89  }
90 }
91 
92 } // namespace compressed_depth_image_transport
void DecompressRVL(const unsigned char *input, unsigned short *output, int numPixels)
Definition: rvl_codec.cpp:70
int CompressRVL(const unsigned short *input, unsigned char *output, int numPixels)
Definition: rvl_codec.cpp:43


compressed_depth_image_transport
Author(s): Julius Kammerl
autogenerated on Fri Sep 20 2019 03:32:12