Debian Codegraph @

cvd/internal/scalar_convert.h

raw symbols SCIP
1 #ifndef CVD_SCALAR_CONVERT_H
2 #define CVD_SCALAR_CONVERT_H
3
4 #include <cvd/byte.h>
5 #include <cvd/internal/pixel_traits.h>
6
7 namespace CVD
8 {
9 namespace Pixel
10 {
11
12 namespace Internal
13 {
14
15 //When we convert, we want the following thing to happen:
16 //conv(max_low_precision_number) == max_high_precision_number
17 //Since max is all ones, rightshifting truncates, resulting in all ones
18 //Left shifting is more tricky.
19 //All remaining empty bits need to be filled with the higest bits of the low precision number
20 //That applies recursively (consider converting byte to ulong)
21 //As an illustration, imagine converting the 4 bit umber 1011 to a 10 bit number. The
22 //result should be:
23 //1011 -> 1011 1011 10
24 //
25 // In other words, it's:
26 // truncate( 1011 * 1000100010.00100010001000100010001000...
27 //
28 // Which is equal to multiplying by (high_precision_max + 1)/(low_precision_max)
29 // Where strict truncation occurs, ie 1.1111111... trucates to 1
30 //
31 template <class To, class From>
32 struct int_info
33 {
34 //Difference in number of bits used
35 static const int diff = traits<To>::bits_used - traits<From>::bits_used;
36
37 //Extra bits required to fill the space bits
38 //Number of complete copies required
39 static const int chunks = traits<To>::bits_used / traits<From>::bits_used;
40 //Number of extra bits
41 static const int extra_bits = traits<To>::bits_used % traits<From>::bits_used;
42 //Right shift required to leave extra bits behind:
43 static const int final_rshift = traits<From>::bits_used - extra_bits;
44
45 //Which way do we need to shift
46 static const int shift_dir = (diff == 0) ? 0 : ((diff > 0) ? 1 : -1);
47 };
48
49 template <class To, int num, int shift, int bits, int r_shift>
50 struct upshift
51 {
52 static To aggregate(To i)
53 {
54 return static_cast<typename traits<To>::element_type>(i << shift | upshift<To, num - 1, shift - bits, bits, r_shift>::aggregate(i));
55 }
56 };
57 template <class To, int shift, int bits, int r_shift>
58 struct upshift<To, 0, shift, bits, r_shift>
59 {
60 static To aggregate(To i)
61 {
62 return i >> r_shift;
63 }
64 };
65
66 template <class To, class From, int i = int_info<To, From>::shift_dir>
67 struct shift_convert
68 {
69 template <class D>
70 static To from(D f)
71 {
72 return static_cast<To>(f);
73 }
74 };
75
76 template <class To, class From>
77 struct shift_convert<To, From, 1>
78 {
79 typedef int_info<To, From> info;
80 template <class D>
81 static To from(D f)
82 {
83 //return static_cast<To>(f) << int_info<To,From>::diff;
84 return upshift<To, info::chunks, info::diff, traits<From>::bits_used, info::final_rshift>::aggregate(static_cast<To>(f));
85 }
86 };
87
88 template <class To, class From>
89 struct shift_convert<To, From, -1>
90 {
91 template <class D>
92 static To from(D f)
93 {
94 return static_cast<typename traits<To>::element_type>(f >> -int_info<To, From>::diff);
95 }
96 };
97
98 namespace Internal
99 {
100 //Trivial constexpr array since std::array isn't.
101 template <class T, int N>
102 struct trivial_array
103 {
104 T elems[N];
105
106 constexpr T& operator[](size_t i)
107 {
108 return elems[i];
109 }
110
111 constexpr const T& operator[](size_t i) const
112 {
113 return elems[i];
114 }
115 };
116
117 template <class S>
118 constexpr trivial_array<S, 512> buildLookupTable()
119 {
120 trivial_array<S, 512> table = {};
121 for(int i = 0; i <= 511; i++)
122 table[i] = (S)((i - 255) / 255.0);
123
124 return table;
125 }
126
127 constexpr static trivial_array<float, 512> float_for_byte = buildLookupTable<float>();
128 constexpr static trivial_array<double, 512> double_for_byte = buildLookupTable<double>();
129 }
130
131 inline float byte_to_float(int b) { return Internal::float_for_byte[b + 255]; }
132 inline double byte_to_double(int b) { return Internal::double_for_byte[b + 255]; }
133
134 //Convert a "D" to "To" scaled as if we are converting "From" type to a "To" type.
135 //Special code is invoked if both D and To are integral.
136 //FIXME: why is the test on "From", not "D"??
137 template <class From, class To, class D = From, bool int1 = traits<To>::integral&& traits<From>::integral, bool int2 = traits<D>::integral>
138 struct ScalarConvert
139 {
140 static inline To from(const D& from)
141 {
142
143 //Casts here to avoid conversion warnings since double can't represent uint64_t
144 static const double factor = static_cast<double>(traits<To>::max_intensity) / static_cast<double>(traits<From>::max_intensity);
145 //Cast to suppress conversion warning, since double can't represent uint64_t completely
146 auto s = static_cast<decltype(from * factor)>(from) * factor;
147 //Cast to suppress conversion warning for special scalar types, like bayer whose underling
148 //type is char, but casting to bayer hits the constructor without the cast
149 return static_cast<typename traits<To>::element_type>(s);
150 }
151 };
152
153 //If the input and output are integral, then use integer only scaling code.
154 template <class From, class To, class D>
155 struct ScalarConvert<From, To, D, true, true>
156 {
157 static inline To from(const D& f)
158 {
159 return shift_convert<To, From, int_info<To, From>::shift_dir>::from(f);
160 }
161 };
162
163 //If the destination is bool, then use != 0.
164 //Note two classes are needed here so that they are both more specialized than
165 //the integral conversion code (above) in order to avoid ambiguities.
166 template <class From, class D>
167 struct ScalarConvert<From, bool, D, true, true>
168 {
169 static inline bool from(const D& from)
170 {
171 return from != 0;
172 }
173 };
174 template <class From, class D>
175 struct ScalarConvert<From, bool, D, true, false>
176 {
177 static inline bool from(const D& from)
178 {
179 return from != 0;
180 }
181 };
182
183 //Lookup table conversion from byte to float.
184 //FIXME surely this can only work properly if D is also byte?
185 template <class D>
186 struct ScalarConvert<byte, float, D, false, true>
187 {
188 static inline float from(const D& from)
189 {
190 return byte_to_float(from);
191 }
192 };
193
194 //FIXME this is surely redundant
195 template <class D>
196 struct ScalarConvert<byte, float, D, false, false>
197 {
198 static inline float from(const D& from)
199 {
200 return static_cast<float>(from * (1.0 / 255.0));
201 }
202 };
203
204 template <class D>
205 struct ScalarConvert<byte, double, D, false, true>
206 {
207 static inline double from(const D& from)
208 {
209 return byte_to_double(from);
210 }
211 };
212 template <class D>
213 struct ScalarConvert<byte, double, D, false, false>
214 {
215 static inline double from(const D& from)
216 {
217 return from * (1.0 / 255.0);
218 }
219 };
220
221 inline double byte_float_to_float(double d)
222 {
223 return d * traits<double>::max_intensity / traits<byte>::max_intensity;
224 }
225
226 #if 0
227 template <> struct ScalarConvert<byte,float,float,false> {
228 static inline float from(const float& from) {
229 return byte_float_to_float(from);
230 }
231 };
232 template <> struct ScalarConvert<byte,double,float,false> {
233 static inline double from(const float& from) {
234 return byte_float_to_float(from);
235 }
236 };
237 template <> struct ScalarConvert<byte,float,double,false> {
238 static inline float from(const double& from) {
239 return byte_float_to_float(from);
240 }
241 };
242 template <> struct ScalarConvert<byte,double,double,false> {
243 static inline double from(const double& from) {
244 return byte_float_to_float(from);
245 }
246 };
247 #endif
248
249 }
250
251 template <class To, class From, class D>
252 inline To scalar_convert(const D& d) { return Internal::ScalarConvert<From, To, D>::from(d); }
253 //template <class To, class From> inline To scalar_convert(const From& d) { return Internal::ScalarConvert<From,To>::from(d); }
254
255 }
256 }
257 #endif