| 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 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
template <class To, class From> |
| 32 |
struct int_info |
| 33 |
{ |
| 34 |
|
| 35 |
static const int diff = traits<To>::bits_used - traits<From>::bits_used; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static const int chunks = traits<To>::bits_used / traits<From>::bits_used; |
| 40 |
|
| 41 |
static const int extra_bits = traits<To>::bits_used % traits<From>::bits_used; |
| 42 |
|
| 43 |
static const int final_rshift = traits<From>::bits_used - extra_bits; |
| 44 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 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 |
|
| 144 |
static const double factor = static_cast<double>(traits<To>::max_intensity) / static_cast<double>(traits<From>::max_intensity); |
| 145 |
|
| 146 |
auto s = static_cast<decltype(from * factor)>(from) * factor; |
| 147 |
|
| 148 |
|
| 149 |
return static_cast<typename traits<To>::element_type>(s); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 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 |
|
| 164 |
|
| 165 |
|
| 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 |
|
| 184 |
|
| 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 |
|
| 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 |
|
| 254 |
|
| 255 |
} |
| 256 |
} |
| 257 |
#endif |