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>
34 #include "promote_integral.hpp"
39 namespace boost {
namespace gil {
50 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcIsGreater>
struct channel_converter_unsigned_integral;
51 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcLessThanDst,
bool SrcDivisible>
struct channel_converter_unsigned_integral_impl;
52 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcLessThanDst,
bool CannotFitInInteger>
struct channel_converter_unsigned_integral_nondivisible;
59 template <
typename Un
signedIntegralChannel>
60 struct unsigned_integral_max_value :
public mpl::integral_c<UnsignedIntegralChannel,std::numeric_limits<UnsignedIntegralChannel>::max()> {};
63 struct unsigned_integral_max_value<uint8_t> :
public mpl::integral_c<uint32_t,0xFF> {};
65 struct unsigned_integral_max_value<uint16_t> :
public mpl::integral_c<uint32_t,0xFFFF> {};
67 struct unsigned_integral_max_value<uint32_t> :
public mpl::integral_c<uintmax_t,0xFFFFFFFF> {};
71 struct unsigned_integral_max_value<packed_channel_value<K> >
72 :
public mpl::integral_c<typename packed_channel_value<K>::integer_t, (uint64_t(1)<<K)-1> {};
80 template <typename UnsignedIntegralChannel>
81 struct unsigned_integral_num_bits : public mpl::int_<sizeof(UnsignedIntegralChannel)*8> {};
84 struct unsigned_integral_num_bits<packed_channel_value<K> >
85 : public mpl::int_<K> {};
126 template <typename SrcChannelV, typename DstChannelV>
127 struct channel_converter_unsigned
128 : public detail::channel_converter_unsigned_impl<SrcChannelV,DstChannelV,is_integral<SrcChannelV>::value,is_integral<DstChannelV>::value> {};
132 template <typename T> struct channel_converter_unsigned<T,T> : public detail::identity<T> {};
142 template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral>
143 struct channel_converter_unsigned_impl {
144 typedef SrcChannelV argument_type;
145 typedef DstChannelV result_type;
146 DstChannelV operator()(SrcChannelV src) const {
147 return DstChannelV(channel_traits<DstChannelV>::min_value() +
148 (src - channel_traits<SrcChannelV>::min_value()) / channel_range<SrcChannelV>() * channel_range<DstChannelV>());
151 template <typename C>
152 static double channel_range() {
153 return double(channel_traits<C>::max_value()) - double(channel_traits<C>::min_value());
158 template <typename SrcChannelV, typename DstChannelV>
159 struct channel_converter_unsigned_impl<SrcChannelV,DstChannelV,true,true>
160 : public channel_converter_unsigned_integral<SrcChannelV,DstChannelV,
161 mpl::less<unsigned_integral_max_value<SrcChannelV>,unsigned_integral_max_value<DstChannelV> >::value > {};
168 template <typename SrcChannelV, typename DstChannelV>
169 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,true>
170 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,
171 !(unsigned_integral_max_value<DstChannelV>::value % unsigned_integral_max_value<SrcChannelV>::value) > {};
173 template <typename SrcChannelV, typename DstChannelV>
174 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,false>
175 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,
176 !(unsigned_integral_max_value<SrcChannelV>::value % unsigned_integral_max_value<DstChannelV>::value) > {};
186 template <typename SrcChannelV, typename DstChannelV>
187 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,true> {
188 DstChannelV operator()(SrcChannelV src) const {
189 typedef typename unsigned_integral_max_value<DstChannelV>::value_type integer_t;
190 static const integer_t mul = unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value;
191 return DstChannelV(src * mul);
198 template <typename SrcChannelV, typename DstChannelV>
199 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,true> {
200 DstChannelV operator()(SrcChannelV src) const {
201 typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t;
202 static const integer_t div = unsigned_integral_max_value<SrcChannelV>::value / unsigned_integral_max_value<DstChannelV>::value;
203 static const integer_t div2 = div/2;
204 return DstChannelV((src + div2) / div);
209 template <typename DstChannelV>
210 struct channel_converter_unsigned_integral_impl<uintmax_t,DstChannelV,false,true> {
211 DstChannelV operator()(uintmax_t src) const {
212 static const uintmax_t div = unsigned_integral_max_value<uint32_t>::value / unsigned_integral_max_value<DstChannelV>::value;
213 static const uintmax_t div2 = div/2;
214 if (src > unsigned_integral_max_value<uintmax_t>::value - div2)
215 return unsigned_integral_max_value<DstChannelV>::value;
216 return DstChannelV((src + div2) / div);
223 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst>
224 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,SrcLessThanDst,false>
225 : public channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,SrcLessThanDst,
227 mpl::plus<unsigned_integral_num_bits<SrcChannelV>,unsigned_integral_num_bits<DstChannelV> >,
228 unsigned_integral_num_bits<uintmax_t>
236 template <typename SrcChannelV, typename DstChannelV>
237 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
238 DstChannelV operator()(SrcChannelV src) const {
239 typedef typename base_channel_type<DstChannelV>::type dest_t;
240 return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
248 template <typename SrcChannelV, typename DstChannelV>
249 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,true> {
250 DstChannelV operator()(SrcChannelV src) const {
251 static const double mul = unsigned_integral_max_value<DstChannelV>::value / double(unsigned_integral_max_value<SrcChannelV>::value);
252 return DstChannelV(src * mul);
259 template <typename SrcChannelV, typename DstChannelV, bool CannotFit>
260 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,false,CannotFit> {
261 DstChannelV operator()(SrcChannelV src) const {
263 typedef typename detail::unsigned_integral_max_value< SrcChannelV >::value_type src_integer_t;
264 typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
266 static const double div = unsigned_integral_max_value<SrcChannelV>::value
267 / static_cast< double >( unsigned_integral_max_value<DstChannelV>::value );
269 static const src_integer_t div2 = static_cast< src_integer_t >( div / 2.0 );
271 return DstChannelV( static_cast< dst_integer_t >(( static_cast< double >( src + div2 ) / div )));
281 template <typename DstChannelV> struct channel_converter_unsigned<float32_t,DstChannelV> {
282 typedef float32_t argument_type;
283 typedef DstChannelV result_type;
284 DstChannelV operator()(float32_t x) const
286 typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
287 return DstChannelV( static_cast< dst_integer_t >(x*channel_traits<DstChannelV>::max_value()+0.5f ));
291 template <typename SrcChannelV> struct channel_converter_unsigned<SrcChannelV,float32_t> {
292 typedef float32_t argument_type;
293 typedef SrcChannelV result_type;
294 float32_t operator()(SrcChannelV x) const { return float32_t(x/float(channel_traits<SrcChannelV>::max_value())); }
297 template <> struct channel_converter_unsigned<float32_t,float32_t> {
298 typedef float32_t argument_type;
299 typedef float32_t result_type;
300 float32_t operator()(float32_t x) const { return x; }
305 template <> struct channel_converter_unsigned<uint32_t,float32_t> {
306 typedef uint32_t argument_type;
307 typedef float32_t result_type;
308 float32_t operator()(uint32_t x) const {
310 if (x>=channel_traits<uint32_t>::max_value()) return
channel_traits<float32_t>::max_value();
311 return float(x) / float(channel_traits<uint32_t>::max_value());
315 template <> struct channel_converter_unsigned<float32_t,uint32_t> {
317 typedef uint32_t result_type;
324 auto const result = x *
static_cast<float32_t::base_channel_t
>(max_value) + 0.5f;
325 return static_cast<uint32_t
>(result);
334 template <
typename ChannelValue>
335 struct channel_convert_to_unsigned :
public detail::identity<ChannelValue> {
336 typedef ChannelValue type;
339 template <>
struct channel_convert_to_unsigned<int8_t> {
340 typedef int8_t argument_type;
341 typedef uint8_t result_type;
342 typedef uint8_t type;
343 type operator()(int8_t val)
const {
344 return static_cast<uint8_t
>(
static_cast<uint32_t
>(val) + 128u);
348 template <>
struct channel_convert_to_unsigned<int16_t> {
349 typedef int16_t argument_type;
350 typedef uint16_t result_type;
351 typedef uint16_t type;
352 type operator()(int16_t val)
const {
353 return static_cast<uint16_t
>(
static_cast<uint32_t
>(val) + 32768u);
357 template <>
struct channel_convert_to_unsigned<int32_t> {
358 typedef int32_t argument_type;
359 typedef uint32_t result_type;
360 typedef uint32_t type;
361 type operator()(int32_t val)
const {
362 return static_cast<uint32_t
>(val)+(1u<<31);
369 template <
typename ChannelValue>
370 struct channel_convert_from_unsigned :
public detail::identity<ChannelValue> {
371 typedef ChannelValue type;
374 template <>
struct channel_convert_from_unsigned<int8_t> {
375 typedef uint8_t argument_type;
376 typedef int8_t result_type;
378 type operator()(uint8_t val)
const {
379 return static_cast<int8_t
>(
static_cast<int32_t
>(val) - 128);
383 template <>
struct channel_convert_from_unsigned<int16_t> {
384 typedef uint16_t argument_type;
385 typedef int16_t result_type;
386 typedef int16_t type;
387 type operator()(uint16_t val)
const {
388 return static_cast<int16_t
>(
static_cast<int32_t
>(val) - 32768);
392 template <>
struct channel_convert_from_unsigned<int32_t> {
393 typedef uint32_t argument_type;
394 typedef int32_t result_type;
395 typedef int32_t type;
396 type operator()(uint32_t val)
const {
397 return static_cast<int32_t
>(val - (1u<<31));
405 template <
typename SrcChannelV,
typename DstChannelV>
407 typedef SrcChannelV argument_type;
408 typedef DstChannelV result_type;
409 DstChannelV operator()(
const SrcChannelV& src)
const {
410 typedef detail::channel_convert_to_unsigned<SrcChannelV> to_unsigned;
411 typedef detail::channel_convert_from_unsigned<DstChannelV> from_unsigned;
412 typedef channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type> converter_unsigned;
413 return from_unsigned()(converter_unsigned()(to_unsigned()(src)));
419 template <
typename DstChannel,
typename SrcChannel>
430 template <
typename Ch1,
typename Ch2>
431 void operator()(
const Ch1& src, Ch2& dst)
const {
432 dst=channel_convert<Ch2>(src);
438 inline uint32_t div255(uint32_t in) { uint32_t tmp=in+128;
return (tmp + (tmp>>8))>>8; }
441 inline uint32_t div32768(uint32_t in) {
return (in+16384)>>15; }
460 template <
typename ChannelValue>
462 typedef ChannelValue first_argument_type;
463 typedef ChannelValue second_argument_type;
464 typedef ChannelValue result_type;
465 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
472 typedef uint8_t first_argument_type;
473 typedef uint8_t second_argument_type;
474 typedef uint8_t result_type;
475 uint8_t operator()(uint8_t a, uint8_t b)
const {
return uint8_t(detail::div255(uint32_t(a) * uint32_t(b))); }
480 typedef uint16_t first_argument_type;
481 typedef uint16_t second_argument_type;
482 typedef uint16_t result_type;
483 uint16_t operator()(uint16_t a, uint16_t b)
const {
return uint16_t((uint32_t(a) * uint32_t(b))/65535); }
495 template <
typename ChannelValue>
497 typedef ChannelValue first_argument_type;
498 typedef ChannelValue second_argument_type;
499 typedef ChannelValue result_type;
500 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
501 typedef detail::channel_convert_to_unsigned<ChannelValue> to_unsigned;
502 typedef detail::channel_convert_from_unsigned<ChannelValue> from_unsigned;
504 return from_unsigned()(multiplier_unsigned()(to_unsigned()(a), to_unsigned()(b)));
509 template <
typename Channel>
529 template <
typename Channel>
534 using base_t =
typename base_channel_type<Channel>::type;
535 using promoted_t =
typename promote_integral<base_t>::type;
536 promoted_t
const promoted_x = x;
539 promoted_t
const promoted_inverted_x = promoted_max - promoted_x + promoted_min;
540 auto const inverted_x =
static_cast<base_t
>(promoted_inverted_x);
channel_traits< Channel >::value_type channel_invert(Channel x)
Default implementation. Provide overloads for performance.
Definition: channel_algorithm.hpp:532
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:420
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:510
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:429
A function object to multiply two channels. result = a * b / max_value.
Definition: channel_algorithm.hpp:496
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:461
Traits for channels. Contains the following members:
Definition: channel.hpp:114
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:49
A unary function object converting between channel types.
Definition: channel_algorithm.hpp:406