12 #ifndef GIL_CHANNEL_ALGORITHM_HPP
13 #define GIL_CHANNEL_ALGORITHM_HPP
26 #include <boost/config.hpp>
27 #include <boost/mpl/less.hpp>
28 #include <boost/mpl/integral_c.hpp>
29 #include <boost/mpl/greater.hpp>
30 #include <boost/type_traits.hpp>
38 namespace boost {
namespace gil {
49 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcIsGreater>
struct channel_converter_unsigned_integral;
50 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcLessThanDst,
bool SrcDivisible>
struct channel_converter_unsigned_integral_impl;
51 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcLessThanDst,
bool CannotFitInInteger>
struct channel_converter_unsigned_integral_nondivisible;
58 template <
typename Un
signedIntegralChannel>
59 struct unsigned_integral_max_value :
public mpl::integral_c<UnsignedIntegralChannel,std::numeric_limits<UnsignedIntegralChannel>::max()> {};
62 struct unsigned_integral_max_value<uint8_t> :
public mpl::integral_c<uint32_t,0xFF> {};
64 struct unsigned_integral_max_value<uint16_t> :
public mpl::integral_c<uint32_t,0xFFFF> {};
66 struct unsigned_integral_max_value<uint32_t> :
public mpl::integral_c<uintmax_t,0xFFFFFFFF> {};
70 struct unsigned_integral_max_value<packed_channel_value<K> >
71 :
public mpl::integral_c<typename packed_channel_value<K>::integer_t, (uint64_t(1)<<K)-1> {};
79 template <typename UnsignedIntegralChannel>
80 struct unsigned_integral_num_bits : public mpl::int_<sizeof(UnsignedIntegralChannel)*8> {};
83 struct unsigned_integral_num_bits<packed_channel_value<K> >
84 : public mpl::int_<K> {};
125 template <typename SrcChannelV, typename DstChannelV>
126 struct channel_converter_unsigned
127 : public detail::channel_converter_unsigned_impl<SrcChannelV,DstChannelV,is_integral<SrcChannelV>::value,is_integral<DstChannelV>::value> {};
131 template <typename T> struct channel_converter_unsigned<T,T> : public detail::identity<T> {};
141 template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral>
142 struct channel_converter_unsigned_impl {
143 typedef SrcChannelV argument_type;
144 typedef DstChannelV result_type;
145 DstChannelV operator()(SrcChannelV src) const {
146 return DstChannelV(channel_traits<DstChannelV>::min_value() +
147 (src - channel_traits<SrcChannelV>::min_value()) / channel_range<SrcChannelV>() * channel_range<DstChannelV>());
150 template <typename C>
151 static double channel_range() {
152 return double(channel_traits<C>::max_value()) - double(channel_traits<C>::min_value());
157 template <typename SrcChannelV, typename DstChannelV>
158 struct channel_converter_unsigned_impl<SrcChannelV,DstChannelV,true,true>
159 : public channel_converter_unsigned_integral<SrcChannelV,DstChannelV,
160 mpl::less<unsigned_integral_max_value<SrcChannelV>,unsigned_integral_max_value<DstChannelV> >::value > {};
167 template <typename SrcChannelV, typename DstChannelV>
168 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,true>
169 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,
170 !(unsigned_integral_max_value<DstChannelV>::value % unsigned_integral_max_value<SrcChannelV>::value) > {};
172 template <typename SrcChannelV, typename DstChannelV>
173 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,false>
174 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,
175 !(unsigned_integral_max_value<SrcChannelV>::value % unsigned_integral_max_value<DstChannelV>::value) > {};
185 template <typename SrcChannelV, typename DstChannelV>
186 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,true> {
187 DstChannelV operator()(SrcChannelV src) const {
188 typedef typename unsigned_integral_max_value<DstChannelV>::value_type integer_t;
189 static const integer_t mul = unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value;
190 return DstChannelV(src * mul);
197 template <typename SrcChannelV, typename DstChannelV>
198 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,true> {
199 DstChannelV operator()(SrcChannelV src) const {
200 typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t;
201 static const integer_t div = unsigned_integral_max_value<SrcChannelV>::value / unsigned_integral_max_value<DstChannelV>::value;
202 static const integer_t div2 = div/2;
203 return DstChannelV((src + div2) / div);
208 template <typename DstChannelV>
209 struct channel_converter_unsigned_integral_impl<uintmax_t,DstChannelV,false,true> {
210 DstChannelV operator()(uintmax_t src) const {
211 static const uintmax_t div = unsigned_integral_max_value<uint32_t>::value / unsigned_integral_max_value<DstChannelV>::value;
212 static const uintmax_t div2 = div/2;
213 if (src > unsigned_integral_max_value<uintmax_t>::value - div2)
214 return unsigned_integral_max_value<DstChannelV>::value;
215 return DstChannelV((src + div2) / div);
222 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst>
223 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,SrcLessThanDst,false>
224 : public channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,SrcLessThanDst,
226 mpl::plus<unsigned_integral_num_bits<SrcChannelV>,unsigned_integral_num_bits<DstChannelV> >,
227 unsigned_integral_num_bits<uintmax_t>
235 template <typename SrcChannelV, typename DstChannelV>
236 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
237 DstChannelV operator()(SrcChannelV src) const {
238 typedef typename base_channel_type<DstChannelV>::type dest_t;
239 return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
247 template <typename SrcChannelV, typename DstChannelV>
248 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,true> {
249 DstChannelV operator()(SrcChannelV src) const {
250 static const double mul = unsigned_integral_max_value<DstChannelV>::value / double(unsigned_integral_max_value<SrcChannelV>::value);
251 return DstChannelV(src * mul);
258 template <typename SrcChannelV, typename DstChannelV, bool CannotFit>
259 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,false,CannotFit> {
260 DstChannelV operator()(SrcChannelV src) const {
262 typedef typename detail::unsigned_integral_max_value< SrcChannelV >::value_type src_integer_t;
263 typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
265 static const double div = unsigned_integral_max_value<SrcChannelV>::value
266 / static_cast< double >( unsigned_integral_max_value<DstChannelV>::value );
268 static const src_integer_t div2 = static_cast< src_integer_t >( div / 2.0 );
270 return DstChannelV( static_cast< dst_integer_t >(( static_cast< double >( src + div2 ) / div )));
280 template <typename DstChannelV> struct channel_converter_unsigned<float32_t,DstChannelV> {
281 typedef float32_t argument_type;
282 typedef DstChannelV result_type;
283 DstChannelV operator()(float32_t x) const
285 typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
286 return DstChannelV( static_cast< dst_integer_t >(x*channel_traits<DstChannelV>::max_value()+0.5f ));
290 template <typename SrcChannelV> struct channel_converter_unsigned<SrcChannelV,float32_t> {
291 typedef float32_t argument_type;
292 typedef SrcChannelV result_type;
293 float32_t operator()(SrcChannelV x) const { return float32_t(x/float(channel_traits<SrcChannelV>::max_value())); }
296 template <> struct channel_converter_unsigned<float32_t,float32_t> {
297 typedef float32_t argument_type;
298 typedef float32_t result_type;
299 float32_t operator()(float32_t x) const { return x; }
304 template <> struct channel_converter_unsigned<uint32_t,float32_t> {
305 typedef uint32_t argument_type;
306 typedef float32_t result_type;
307 float32_t operator()(uint32_t x) const {
309 if (x>=channel_traits<uint32_t>::max_value()) return
channel_traits<float32_t>::max_value();
310 return float(x) / float(channel_traits<uint32_t>::max_value());
314 template <> struct channel_converter_unsigned<float32_t,uint32_t> {
316 typedef uint32_t result_type;
323 auto const result = x *
static_cast<float32_t::base_channel_t
>(max_value) + 0.5f;
324 return static_cast<uint32_t
>(result);
333 template <
typename ChannelValue>
334 struct channel_convert_to_unsigned :
public detail::identity<ChannelValue> {
335 typedef ChannelValue type;
338 template <>
struct channel_convert_to_unsigned<int8_t> {
339 typedef int8_t argument_type;
340 typedef uint8_t result_type;
341 typedef uint8_t type;
342 type operator()(int8_t val)
const {
343 return static_cast<uint8_t
>(
static_cast<uint32_t
>(val) + 128u);
347 template <>
struct channel_convert_to_unsigned<int16_t> {
348 typedef int16_t argument_type;
349 typedef uint16_t result_type;
350 typedef uint16_t type;
351 type operator()(int16_t val)
const {
352 return static_cast<uint16_t
>(
static_cast<uint32_t
>(val) + 32768u);
356 template <>
struct channel_convert_to_unsigned<int32_t> {
357 typedef int32_t argument_type;
358 typedef uint32_t result_type;
359 typedef uint32_t type;
360 type operator()(int32_t val)
const {
361 return static_cast<uint32_t
>(val)+(1u<<31);
368 template <
typename ChannelValue>
369 struct channel_convert_from_unsigned :
public detail::identity<ChannelValue> {
370 typedef ChannelValue type;
373 template <>
struct channel_convert_from_unsigned<int8_t> {
374 typedef uint8_t argument_type;
375 typedef int8_t result_type;
377 type operator()(uint8_t val)
const {
378 return static_cast<int8_t
>(
static_cast<int32_t
>(val) - 128);
382 template <>
struct channel_convert_from_unsigned<int16_t> {
383 typedef uint16_t argument_type;
384 typedef int16_t result_type;
385 typedef int16_t type;
386 type operator()(uint16_t val)
const {
387 return static_cast<int16_t
>(
static_cast<int32_t
>(val) - 32768);
391 template <>
struct channel_convert_from_unsigned<int32_t> {
392 typedef uint32_t argument_type;
393 typedef int32_t result_type;
394 typedef int32_t type;
395 type operator()(uint32_t val)
const {
396 return static_cast<int32_t
>(val - (1u<<31));
404 template <
typename SrcChannelV,
typename DstChannelV>
406 typedef SrcChannelV argument_type;
407 typedef DstChannelV result_type;
408 DstChannelV operator()(
const SrcChannelV& src)
const {
409 typedef detail::channel_convert_to_unsigned<SrcChannelV> to_unsigned;
410 typedef detail::channel_convert_from_unsigned<DstChannelV> from_unsigned;
411 typedef channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type> converter_unsigned;
412 return from_unsigned()(converter_unsigned()(to_unsigned()(src)));
418 template <
typename DstChannel,
typename SrcChannel>
429 template <
typename Ch1,
typename Ch2>
430 void operator()(
const Ch1& src, Ch2& dst)
const {
431 dst=channel_convert<Ch2>(src);
437 inline uint32_t div255(uint32_t in) { uint32_t tmp=in+128;
return (tmp + (tmp>>8))>>8; }
440 inline uint32_t div32768(uint32_t in) {
return (in+16384)>>15; }
459 template <
typename ChannelValue>
461 typedef ChannelValue first_argument_type;
462 typedef ChannelValue second_argument_type;
463 typedef ChannelValue result_type;
464 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
471 typedef uint8_t first_argument_type;
472 typedef uint8_t second_argument_type;
473 typedef uint8_t result_type;
474 uint8_t operator()(uint8_t a, uint8_t b)
const {
return uint8_t(detail::div255(uint32_t(a) * uint32_t(b))); }
479 typedef uint16_t first_argument_type;
480 typedef uint16_t second_argument_type;
481 typedef uint16_t result_type;
482 uint16_t operator()(uint16_t a, uint16_t b)
const {
return uint16_t((uint32_t(a) * uint32_t(b))/65535); }
494 template <
typename ChannelValue>
496 typedef ChannelValue first_argument_type;
497 typedef ChannelValue second_argument_type;
498 typedef ChannelValue result_type;
499 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
500 typedef detail::channel_convert_to_unsigned<ChannelValue> to_unsigned;
501 typedef detail::channel_convert_from_unsigned<ChannelValue> from_unsigned;
503 return from_unsigned()(multiplier_unsigned()(to_unsigned()(a), to_unsigned()(b)));
508 template <
typename Channel>
528 template <
typename Channel>
channel_traits< Channel >::value_type channel_invert(Channel x)
Default implementation. Provide overloads for performance.
Definition: channel_algorithm.hpp:531
A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept.
Definition: channel.hpp:156
channel_traits< DstChannel >::value_type channel_convert(const SrcChannel &src)
Converting from one channel type to another.
Definition: channel_algorithm.hpp:419
channel_traits< Channel >::value_type channel_multiply(Channel a, Channel b)
A function multiplying two channels. result = a * b / max_value.
Definition: channel_algorithm.hpp:509
identity taken from SGI STL.
Definition: utilities.hpp:265
Same as channel_converter, except it takes the destination channel by reference, which allows us to m...
Definition: channel_algorithm.hpp:428
A function object to multiply two channels. result = a * b / max_value.
Definition: channel_algorithm.hpp:495
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:460
Traits for channels. Contains the following members:
Definition: channel.hpp:114
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:48
A unary function object converting between channel types.
Definition: channel_algorithm.hpp:405