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 
7 namespace rtabmap
8 {
9 
11 
12 void RvlCodec::EncodeVLE(int value)
13 {
14  do
15  {
16  int nibble = value & 0x7; // lower 3 bits
17  if (value >>= 3)
18  nibble |= 0x8; // more to come
19  word_ <<= 4;
20  word_ |= nibble;
21  if (++nibblesWritten_ == 8) // output word
22  {
23  *pBuffer_++ = word_;
24  nibblesWritten_ = 0;
25  word_ = 0;
26  }
27  } while (value);
28 }
29 
31 {
32  unsigned int nibble;
33  int value = 0, bits = 29;
34  do
35  {
36  if (!nibblesWritten_)
37  {
38  word_ = *pBuffer_++; // load word
39  nibblesWritten_ = 8;
40  }
41  nibble = word_ & 0xf0000000;
42  value |= (nibble << 1) >> bits;
43  word_ <<= 4;
45  bits -= 3;
46  } while (nibble & 0x80000000);
47  return value;
48 }
49 
50 int RvlCodec::CompressRVL(const uint16_t * input, unsigned char * output, int numPixels)
51 {
52  buffer_ = pBuffer_ = reinterpret_cast<int *>(output);
53  nibblesWritten_ = 0;
54  const uint16_t * end = input + numPixels;
55  uint16_t previous = 0;
56  while (input != end)
57  {
58  int zeros = 0, nonzeros = 0;
59  for (; (input != end) && !*input; input++, zeros++) {}
60  EncodeVLE(zeros); // number of zeros
61  for (const uint16_t * p = input; (p != end) && *p++; nonzeros++) {}
62  EncodeVLE(nonzeros); // number of nonzeros
63  for (int i = 0; i < nonzeros; i++)
64  {
65  uint16_t current = *input++;
66  int delta = current - previous;
67  int positive = (delta << 1) ^ (delta >> 31);
68  EncodeVLE(positive); // nonzero value
69  previous = current;
70  }
71  }
72  if (nibblesWritten_) // last few values
73  *pBuffer_++ = word_ << 4 * (8 - nibblesWritten_);
74  return static_cast<int>((unsigned char *)pBuffer_ - (unsigned char *)buffer_); // num bytes
75 }
76 
77 void RvlCodec::DecompressRVL(const unsigned char * input, uint16_t * output, int numPixels)
78 {
79  buffer_ = pBuffer_ = const_cast<int *>(reinterpret_cast<const int *>(input));
80  nibblesWritten_ = 0;
81  uint16_t current, previous = 0;
82  int numPixelsToDecode = numPixels;
83  while (numPixelsToDecode)
84  {
85  int zeros = DecodeVLE(); // number of zeros
86  numPixelsToDecode -= zeros;
87  for (; zeros; zeros--)
88  *output++ = 0;
89  int nonzeros = DecodeVLE(); // number of nonzeros
90  numPixelsToDecode -= nonzeros;
91  for (; nonzeros; nonzeros--)
92  {
93  int positive = DecodeVLE(); // nonzero value
94  int delta = (positive >> 1) ^ -(positive & 1);
95  current = previous + delta;
96  *output++ = current;
97  previous = current;
98  }
99  }
100 }
101 
102 } // namespace rtabmap
rtabmap::RvlCodec::RvlCodec
RvlCodec()
Definition: rvl_codec.cpp:10
bits
Map< const Array< unsigned char, sizeof(T), 1 > > bits(const T &x)
rtabmap::RvlCodec::pBuffer_
int * pBuffer_
Definition: rvl_codec.h:30
end
end
rtabmap::RvlCodec::nibblesWritten_
int nibblesWritten_
Definition: rvl_codec.h:32
rtabmap::RvlCodec::CompressRVL
int CompressRVL(const uint16_t *input, unsigned char *output, int numPixels)
Definition: rvl_codec.cpp:50
rtabmap::RvlCodec::DecodeVLE
int DecodeVLE()
Definition: rvl_codec.cpp:30
uint16_t
::uint16_t uint16_t
rtabmap::RvlCodec::DecompressRVL
void DecompressRVL(const unsigned char *input, uint16_t *output, int numPixels)
Definition: rvl_codec.cpp:77
delta
def delta(g0, g1)
p
Point3_ p(2)
rtabmap::RvlCodec::word_
int word_
Definition: rvl_codec.h:31
rvl_codec.h
rtabmap::RvlCodec::buffer_
int * buffer_
Definition: rvl_codec.h:29
rtabmap
Definition: CameraARCore.cpp:35
value
value
i
int i
rtabmap::RvlCodec::EncodeVLE
void EncodeVLE(int value)
Definition: rvl_codec.cpp:12


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Feb 13 2025 03:44:59