View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

depthquant.h
Go to the documentation of this file.
1
10#ifndef DEPTH_QUANT_H
11#define DEPTH_QUANT_H
12
13#include "export.h"
14
15#include <stdint.h>
16#include <cstdlib>
17
18namespace vistle {
19
24};
25
27template<int Precision, int BitsPerPixel, int ScaleBits = 0>
29 enum {
30 precision = Precision,
31
32 edge = 4, // only 4 works
33 scale_bits = ScaleBits,
34 bits_per_pixel = BitsPerPixel,
35 num_bytes = (BitsPerPixel * edge * edge) / 8
36 };
37
38 uint8_t bits[num_bytes];
39};
40
43
45template<int Precision>
47 uint8_t depth[2][Precision / 8];
48};
49
52
54template<int Precision, int BitsPerPixel, int ScaleBits = 0>
55struct DepthQuantize: public DepthQuantizeBits<Precision, BitsPerPixel, ScaleBits> {
56 uint8_t depth[2][Precision / 8];
57};
58
61
63template<class DepthQuantize>
64#ifdef __CUDACC__
65__device__ __host__
66#endif
67 inline void
68 setdepth(DepthQuantize &q, int idx, uint32_t value)
69{
70 for (unsigned i = 3; i > 0; --i) {
71 if (i <= sizeof(q.depth[idx]))
72 q.depth[idx][i - 1] = value & 0xff;
73 value >>= 8;
74 }
75}
76
78template<class DepthQuantize>
79#ifdef __CUDACC__
80__device__ __host__
81#endif
82 uint32_t
83 getdepth(const DepthQuantize &q, int idx);
84
86template<class DepthQuantize>
87#ifdef __CUDACC__
88__device__ __host__
89#endif
90 void
92{
93 for (unsigned i = 0; i < sizeof(q.bits); ++i) {
94 q.bits[i] = 0;
95 }
96}
97
99template<class DepthQuantize>
100#ifdef __CUDACC__
101__device__ __host__
102#endif
103 inline void
104 dq_setbit(DepthQuantize &q, int bit, unsigned value = 1)
105{
106 q.bits[bit >> 3] |= (value & 1) << (bit & 7);
107}
108
110template<class DepthQuantize>
111#ifdef __CUDACC__
112__device__ __host__
113#endif
114 void
116{
117 q.bits[bit >> 3] &= ~(1 << (bit & 7));
118}
119
121template<class DepthQuantize>
122#ifdef __CUDACC__
123__device__ __host__
124#endif
125 inline unsigned
126 dq_getbit(const DepthQuantize &q, int bit)
127{
128 return (q.bits[bit >> 3] >> (bit & 7)) & 1;
129}
130
132template<class DepthQuantize>
133#ifdef __CUDACC__
134__device__ __host__
135#endif
136 inline void
137 dq_setbits(DepthQuantize &q, int bit, int nbits, unsigned value, int block = 1)
138{
139#if 0
140 for (int i=bit; i<bit+nbits; ++i) {
141 dq_setbit(q, i, value&1);
142 value >>= 1;
143 }
144#else
145 const uint32_t mask = (1 << block) - 1;
146 for (int i = bit + nbits - 1; i >= bit; i -= block) {
147 q.bits[i >> 3] |= (value & mask) << (i & 7);
148 value >>= block;
149 }
150#endif
151}
152
154template<class DepthQuantize>
155#ifdef __CUDACC__
156__device__ __host__
157#endif
158 inline void
159 dq_setbits(DepthQuantize &q, uint64_t bits)
160{
161 for (int i = 0; i < DepthQuantize::num_bytes; ++i) {
162 //for (int i = DepthQuantize::num_bytes; i>=0; --i) {
163 q.bits[i] = bits & 0xff;
164 bits >>= 8;
165 }
166}
167
169template<class DepthQuantize>
170#ifdef __CUDACC__
171__device__ __host__
172#endif
173 inline unsigned
174 dq_getbits(const DepthQuantize &q, int bit, int nbits)
175{
176 unsigned v = 0;
177 for (int i = bit + nbits - 1; i >= bit; --i) {
178 v <<= 1;
179 v |= dq_getbit(q, i);
180 }
181 return v;
182}
183
185template<class DepthQuantize>
186#ifdef __CUDACC__
187__device__ __host__
188#endif
189 inline uint64_t
191{
192 uint64_t bits = q.bits[DepthQuantize::num_bytes - 1];
193 for (int i = DepthQuantize::num_bytes - 2; i >= 0; --i) {
194 bits <<= 8;
195 bits |= q.bits[i];
196 }
197 return bits;
198}
199
200template<DepthFormat format, int bpp>
201#ifdef __CUDACC__
202__device__ __host__
203#endif
204 inline uint32_t
205 get_depth(const unsigned char *img, int x, int y, int w, int h);
206
207template<>
208#ifdef __CUDACC__
209__device__ __host__
210#endif
211 inline uint32_t
212 get_depth<DepthFloat, 4>(const unsigned char *img, int x, int y, int w, int h)
213{
214 const float df = ((float *)img)[y * w + x];
215 uint32_t di = df * 0x00ffffffU;
216 if (di > 0x00ffffffU)
217 di = 0x00ffffffU;
218 return di;
219}
220
221template<>
222#ifdef __CUDACC__
223__device__ __host__
224#endif
225 inline uint32_t
226 get_depth<DepthRGBA, 4>(const unsigned char *img, int x, int y, int w, int h)
227{
228 const unsigned char *dp = &img[(y * w + x) * 4];
229 return ((dp[2] * 256 + dp[1]) * 256 + dp[0]);
230}
231
232template<>
233#ifdef __CUDACC__
234__device__ __host__
235#endif
236 inline uint32_t
237 get_depth<DepthInteger, 1>(const unsigned char *img, int x, int y, int w, int h)
238{
239 const unsigned char *dp = &img[(y * w + x) * 1];
240 return *dp * 0x0010101U;
241}
242
243template<>
244#ifdef __CUDACC__
245__device__ __host__
246#endif
247 inline uint32_t
248 get_depth<DepthInteger, 2>(const unsigned char *img, int x, int y, int w, int h)
249{
250 const unsigned char *dp = &img[(y * w + x) * 2];
251 return (dp[1] * 256 + dp[0]) * 256 + dp[1];
252}
253
254template<>
255#ifdef __CUDACC__
256__device__ __host__
257#endif
258 inline uint32_t
259 get_depth<DepthInteger, 3>(const unsigned char *img, int x, int y, int w, int h)
260{
261 const unsigned char *dp = &img[(y * w + x) * 3];
262 return ((dp[3] * 256 + dp[2]) * 256 + dp[1]);
263}
264
265template<>
266#ifdef __CUDACC__
267__device__ __host__
268#endif
269 inline uint32_t
270 get_depth<DepthInteger, 4>(const unsigned char *img, int x, int y, int w, int h)
271{
272 const unsigned char *dp = &img[(y * w + x) * 4];
273 return ((dp[3] * 256 + dp[2]) * 256 + dp[1]);
274}
275
277void V_RHREXPORT depthquant(char *quantbuf, const char *zbuf, DepthFormat format, int depthps, int x, int y, int width,
278 int height, int stride = -1);
279void V_RHREXPORT depthquant_planar(char *quantbuf, const char *zbuf, DepthFormat format, int depthps, int x, int y,
280 int width, int height, int stride = -1);
281
283size_t V_RHREXPORT depthquant_size(DepthFormat format, int depthps, int width, int height);
284
286void V_RHREXPORT depthdequant(char *zbuf, const char *quantbuf, DepthFormat format, int depthps, int tx, int dy,
287 int width, int height, int stride = -1);
288void V_RHREXPORT depthdequant_planar(char *zbuf, const char *quantbuf, DepthFormat format, int depthps, int tx, int dy,
289 int width, int height, int stride = -1);
290
291} // namespace vistle
292#endif
#define V_RHREXPORT
Definition: export.h:9
DepthQuantize< 16, 4, 0 > DepthQuantize16
Definition: depthquant.h:59
uint32_t get_depth< DepthFloat, 4 >(const unsigned char *img, int x, int y, int w, int h)
Definition: depthquant.h:212
DepthQuantizeBits< 16, 4, 0 > DepthQuantizeBits16
Definition: depthquant.h:41
uint32_t get_depth(const unsigned char *img, int x, int y, int w, int h)
unsigned dq_getbits(const DepthQuantize &q, int bit, int nbits)
get multiple bits from DepthQuantize struct
Definition: depthquant.h:174
void depthdequant_planar(char *zbuf, const char *quantbuf, DepthFormat format, int depthps, int dx, int dy, int width, int height, int stride)
Definition: depthquant.cpp:322
uint32_t get_depth< DepthInteger, 1 >(const unsigned char *img, int x, int y, int w, int h)
Definition: depthquant.h:237
DepthQuantizeBits< 24, 3, 4 > DepthQuantizeBits24
Definition: depthquant.h:42
uint32_t get_depth< DepthInteger, 3 >(const unsigned char *img, int x, int y, int w, int h)
Definition: depthquant.h:259
void depthquant_planar(char *quantbufS, const char *zbufS, DepthFormat format, int depthps, int x, int y, int width, int height, int stride)
Definition: depthquant.cpp:728
void depthquant(char *quantbufS, const char *zbufS, DepthFormat format, int depthps, int x, int y, int width, int height, int stride)
transform depth buffer into quantized values on 4x4 pixel tiles
Definition: depthquant.cpp:660
void setdepth(DepthQuantize &q, int idx, uint32_t value)
set depth bits for a single pixel
Definition: depthquant.h:68
DepthQuantizeMinMaxDepth< 24 > MinMaxDepth24
Definition: depthquant.h:51
DepthQuantizeMinMaxDepth< 16 > MinMaxDepth16
Definition: depthquant.h:50
void dq_clearbits(DepthQuantize &q)
zero depth bits for a 4x4 tile
Definition: depthquant.h:91
uint32_t get_depth< DepthRGBA, 4 >(const unsigned char *img, int x, int y, int w, int h)
Definition: depthquant.h:226
unsigned dq_getbit(const DepthQuantize &q, int bit)
retrieve a single bit from DepthQuantize struct
Definition: depthquant.h:126
uint8_t depth[2][Precision/8]
minimum and maximum depth data
Definition: depthquant.h:47
void depthdequant(char *zbuf, const char *quantbuf, DepthFormat format, int depthps, int dx, int dy, int width, int height, int stride)
reverse transformation done by depthquant
Definition: depthquant.cpp:281
DepthFormat
Definition: depthquant.h:20
@ DepthRGBA
Definition: depthquant.h:23
@ DepthFloat
Definition: depthquant.h:22
@ DepthInteger
Definition: depthquant.h:21
uint32_t get_depth< DepthInteger, 4 >(const unsigned char *img, int x, int y, int w, int h)
Definition: depthquant.h:270
size_t depthquant_size(DepthFormat format, int depthps, int width, int height)
return size required by depthquant for quantized image
Definition: depthquant.cpp:64
uint32_t getdepth(const DepthQuantize &q, int idx)
retrieve depth bits for a single pixel
DepthQuantize< 24, 3, 4 > DepthQuantize24
Definition: depthquant.h:60
void dq_clearbit(DepthQuantize &q, int bit)
clear a single bit in DepthQuantize struct
Definition: depthquant.h:115
void dq_setbit(DepthQuantize &q, int bit, unsigned value=1)
set a single bit in DepthQuantize struct
Definition: depthquant.h:104
void dq_setbits(DepthQuantize &q, int bit, int nbits, unsigned value, int block=1)
set multiple bits in DepthQuantize struct
Definition: depthquant.h:137
uint32_t get_depth< DepthInteger, 2 >(const unsigned char *img, int x, int y, int w, int h)
Definition: depthquant.h:248
min/max depth for a single 4x4 pixel tile (when storing depths and interpolation bits separately)
Definition: depthquant.h:46
quantized depth data for a single 4x4 pixel tile
Definition: depthquant.h:28
uint8_t bits[num_bytes]
per-pixel interpolation weights
Definition: depthquant.h:38
@ num_bytes
Definition: depthquant.h:35
@ edge
Definition: depthquant.h:32
@ precision
Definition: depthquant.h:30
@ scale_bits
Definition: depthquant.h:33
@ bits_per_pixel
Definition: depthquant.h:34
quantized depth data for a single 4x4 pixel tile
Definition: depthquant.h:55
uint8_t depth[2][Precision/8]
minimum and maximum depth data
Definition: depthquant.h:56