8 #ifndef BOOST_GIL_GIL_CHANNEL_ALGORITHM_HPP
9 #define BOOST_GIL_GIL_CHANNEL_ALGORITHM_HPP
11 #include <boost/gil/channel.hpp>
12 #include <boost/gil/promote_integral.hpp>
13 #include <boost/gil/typedefs.hpp>
15 #include <boost/mpl/less.hpp>
16 #include <boost/mpl/integral_c.hpp>
17 #include <boost/mpl/greater.hpp>
18 #include <boost/type_traits.hpp>
22 namespace boost {
namespace gil {
28 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcIsGreater>
struct channel_converter_unsigned_integral;
29 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcLessThanDst,
bool SrcDivisible>
struct channel_converter_unsigned_integral_impl;
30 template <
typename SrcChannelV,
typename DstChannelV,
bool SrcLessThanDst,
bool CannotFitInInteger>
struct channel_converter_unsigned_integral_nondivisible;
37 template <
typename Un
signedIntegralChannel>
38 struct unsigned_integral_max_value :
public mpl::integral_c<UnsignedIntegralChannel,std::numeric_limits<UnsignedIntegralChannel>::max()> {};
41 struct unsigned_integral_max_value<uint8_t> :
public mpl::integral_c<uint32_t,0xFF> {};
43 struct unsigned_integral_max_value<uint16_t> :
public mpl::integral_c<uint32_t,0xFFFF> {};
45 struct unsigned_integral_max_value<uint32_t> :
public mpl::integral_c<uintmax_t,0xFFFFFFFF> {};
49 struct unsigned_integral_max_value<packed_channel_value<K> >
50 :
public mpl::integral_c<typename packed_channel_value<K>::integer_t, (uint64_t(1)<<K)-1> {};
58 template <typename UnsignedIntegralChannel>
59 struct unsigned_integral_num_bits : public mpl::int_<sizeof(UnsignedIntegralChannel)*8> {};
62 struct unsigned_integral_num_bits<packed_channel_value<K> >
63 : public mpl::int_<K> {};
104 template <typename SrcChannelV, typename DstChannelV>
105 struct channel_converter_unsigned
106 : public detail::channel_converter_unsigned_impl<SrcChannelV,DstChannelV,is_integral<SrcChannelV>::value,is_integral<DstChannelV>::value> {};
110 template <typename T> struct channel_converter_unsigned<T,T> : public detail::identity<T> {};
120 template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral>
121 struct channel_converter_unsigned_impl {
122 typedef SrcChannelV argument_type;
123 typedef DstChannelV result_type;
124 DstChannelV operator()(SrcChannelV src) const {
125 return DstChannelV(channel_traits<DstChannelV>::min_value() +
126 (src - channel_traits<SrcChannelV>::min_value()) / channel_range<SrcChannelV>() * channel_range<DstChannelV>());
129 template <typename C>
130 static double channel_range() {
131 return double(channel_traits<C>::max_value()) - double(channel_traits<C>::min_value());
136 template <typename SrcChannelV, typename DstChannelV>
137 struct channel_converter_unsigned_impl<SrcChannelV,DstChannelV,true,true>
138 : public channel_converter_unsigned_integral<SrcChannelV,DstChannelV,
139 mpl::less<unsigned_integral_max_value<SrcChannelV>,unsigned_integral_max_value<DstChannelV> >::value > {};
146 template <typename SrcChannelV, typename DstChannelV>
147 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,true>
148 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,
149 !(unsigned_integral_max_value<DstChannelV>::value % unsigned_integral_max_value<SrcChannelV>::value) > {};
151 template <typename SrcChannelV, typename DstChannelV>
152 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,false>
153 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,
154 !(unsigned_integral_max_value<SrcChannelV>::value % unsigned_integral_max_value<DstChannelV>::value) > {};
164 template <typename SrcChannelV, typename DstChannelV>
165 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,true> {
166 DstChannelV operator()(SrcChannelV src) const {
167 typedef typename unsigned_integral_max_value<DstChannelV>::value_type integer_t;
168 static const integer_t mul = unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value;
169 return DstChannelV(src * mul);
176 template <typename SrcChannelV, typename DstChannelV>
177 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,true> {
178 DstChannelV operator()(SrcChannelV src) const {
179 typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t;
180 static const integer_t div = unsigned_integral_max_value<SrcChannelV>::value / unsigned_integral_max_value<DstChannelV>::value;
181 static const integer_t div2 = div/2;
182 return DstChannelV((src + div2) / div);
187 template <typename DstChannelV>
188 struct channel_converter_unsigned_integral_impl<uintmax_t,DstChannelV,false,true> {
189 DstChannelV operator()(uintmax_t src) const {
190 static const uintmax_t div = unsigned_integral_max_value<uint32_t>::value / unsigned_integral_max_value<DstChannelV>::value;
191 static const uintmax_t div2 = div/2;
192 if (src > unsigned_integral_max_value<uintmax_t>::value - div2)
193 return unsigned_integral_max_value<DstChannelV>::value;
194 return DstChannelV((src + div2) / div);
201 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst>
202 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,SrcLessThanDst,false>
203 : public channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,SrcLessThanDst,
205 mpl::plus<unsigned_integral_num_bits<SrcChannelV>,unsigned_integral_num_bits<DstChannelV> >,
206 unsigned_integral_num_bits<uintmax_t>
214 template <typename SrcChannelV, typename DstChannelV>
215 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
216 DstChannelV operator()(SrcChannelV src) const {
217 typedef typename base_channel_type<DstChannelV>::type dest_t;
218 return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
226 template <typename SrcChannelV, typename DstChannelV>
227 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,true> {
228 DstChannelV operator()(SrcChannelV src) const {
229 static const double mul = unsigned_integral_max_value<DstChannelV>::value / double(unsigned_integral_max_value<SrcChannelV>::value);
230 return DstChannelV(src * mul);
237 template <typename SrcChannelV, typename DstChannelV, bool CannotFit>
238 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,false,CannotFit> {
239 DstChannelV operator()(SrcChannelV src) const {
241 typedef typename detail::unsigned_integral_max_value< SrcChannelV >::value_type src_integer_t;
242 typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
244 static const double div = unsigned_integral_max_value<SrcChannelV>::value
245 / static_cast< double >( unsigned_integral_max_value<DstChannelV>::value );
247 static const src_integer_t div2 = static_cast< src_integer_t >( div / 2.0 );
249 return DstChannelV( static_cast< dst_integer_t >(( static_cast< double >( src + div2 ) / div )));
259 template <typename DstChannelV> struct channel_converter_unsigned<float32_t,DstChannelV> {
260 typedef float32_t argument_type;
261 typedef DstChannelV result_type;
262 DstChannelV operator()(float32_t x) const
264 typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
265 return DstChannelV( static_cast< dst_integer_t >(x*channel_traits<DstChannelV>::max_value()+0.5f ));
269 template <typename SrcChannelV> struct channel_converter_unsigned<SrcChannelV,float32_t> {
270 typedef float32_t argument_type;
271 typedef SrcChannelV result_type;
272 float32_t operator()(SrcChannelV x) const { return float32_t(x/float(channel_traits<SrcChannelV>::max_value())); }
275 template <> struct channel_converter_unsigned<float32_t,float32_t> {
276 typedef float32_t argument_type;
277 typedef float32_t result_type;
278 float32_t operator()(float32_t x) const { return x; }
283 template <> struct channel_converter_unsigned<uint32_t,float32_t> {
284 typedef uint32_t argument_type;
285 typedef float32_t result_type;
286 float32_t operator()(uint32_t x) const {
288 if (x>=channel_traits<uint32_t>::max_value()) return channel_traits<float32_t>::max_value();
289 return float(x) / float(channel_traits<uint32_t>::max_value());
293 template <> struct channel_converter_unsigned<float32_t,uint32_t> {
295 typedef uint32_t result_type;
298 if (x>=channel_traits<float32_t>::max_value())
299 return channel_traits<uint32_t>::max_value();
301 auto const max_value = channel_traits<uint32_t>::max_value();
302 auto const result = x *
static_cast<float32_t::base_channel_t
>(max_value) + 0.5f;
303 return static_cast<uint32_t
>(result);
312 template <
typename ChannelValue>
313 struct channel_convert_to_unsigned :
public detail::identity<ChannelValue> {
314 typedef ChannelValue type;
317 template <>
struct channel_convert_to_unsigned<int8_t> {
318 typedef int8_t argument_type;
319 typedef uint8_t result_type;
320 typedef uint8_t type;
321 type operator()(int8_t val)
const {
322 return static_cast<uint8_t
>(
static_cast<uint32_t
>(val) + 128u);
326 template <>
struct channel_convert_to_unsigned<int16_t> {
327 typedef int16_t argument_type;
328 typedef uint16_t result_type;
329 typedef uint16_t type;
330 type operator()(int16_t val)
const {
331 return static_cast<uint16_t
>(
static_cast<uint32_t
>(val) + 32768u);
335 template <>
struct channel_convert_to_unsigned<int32_t> {
336 typedef int32_t argument_type;
337 typedef uint32_t result_type;
338 typedef uint32_t type;
339 type operator()(int32_t val)
const {
340 return static_cast<uint32_t
>(val)+(1u<<31);
347 template <
typename ChannelValue>
348 struct channel_convert_from_unsigned :
public detail::identity<ChannelValue> {
349 typedef ChannelValue type;
352 template <>
struct channel_convert_from_unsigned<int8_t> {
353 typedef uint8_t argument_type;
354 typedef int8_t result_type;
356 type operator()(uint8_t val)
const {
357 return static_cast<int8_t
>(
static_cast<int32_t
>(val) - 128);
361 template <>
struct channel_convert_from_unsigned<int16_t> {
362 typedef uint16_t argument_type;
363 typedef int16_t result_type;
364 typedef int16_t type;
365 type operator()(uint16_t val)
const {
366 return static_cast<int16_t
>(
static_cast<int32_t
>(val) - 32768);
370 template <>
struct channel_convert_from_unsigned<int32_t> {
371 typedef uint32_t argument_type;
372 typedef int32_t result_type;
373 typedef int32_t type;
374 type operator()(uint32_t val)
const {
375 return static_cast<int32_t
>(val - (1u<<31));
383 template <
typename SrcChannelV,
typename DstChannelV>
385 typedef SrcChannelV argument_type;
386 typedef DstChannelV result_type;
387 DstChannelV operator()(
const SrcChannelV& src)
const {
388 typedef detail::channel_convert_to_unsigned<SrcChannelV> to_unsigned;
389 typedef detail::channel_convert_from_unsigned<DstChannelV> from_unsigned;
390 typedef channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type> converter_unsigned;
391 return from_unsigned()(converter_unsigned()(to_unsigned()(src)));
397 template <
typename DstChannel,
typename SrcChannel>
398 inline typename channel_traits<DstChannel>::value_type
channel_convert(
const SrcChannel& src) {
400 typename channel_traits<DstChannel>::value_type>()(src);
408 template <
typename Ch1,
typename Ch2>
409 void operator()(
const Ch1& src, Ch2& dst)
const {
410 dst=channel_convert<Ch2>(src);
416 inline uint32_t div255(uint32_t in) { uint32_t tmp=in+128;
return (tmp + (tmp>>8))>>8; }
419 inline uint32_t div32768(uint32_t in) {
return (in+16384)>>15; }
438 template <
typename ChannelValue>
440 typedef ChannelValue first_argument_type;
441 typedef ChannelValue second_argument_type;
442 typedef ChannelValue result_type;
443 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
444 return ChannelValue(
static_cast<typename base_channel_type<ChannelValue>::type
>(a /
double(channel_traits<ChannelValue>::max_value()) * b));
450 typedef uint8_t first_argument_type;
451 typedef uint8_t second_argument_type;
452 typedef uint8_t result_type;
453 uint8_t operator()(uint8_t a, uint8_t b)
const {
return uint8_t(detail::div255(uint32_t(a) * uint32_t(b))); }
458 typedef uint16_t first_argument_type;
459 typedef uint16_t second_argument_type;
460 typedef uint16_t result_type;
461 uint16_t operator()(uint16_t a, uint16_t b)
const {
return uint16_t((uint32_t(a) * uint32_t(b))/65535); }
473 template <
typename ChannelValue>
475 typedef ChannelValue first_argument_type;
476 typedef ChannelValue second_argument_type;
477 typedef ChannelValue result_type;
478 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
479 typedef detail::channel_convert_to_unsigned<ChannelValue> to_unsigned;
480 typedef detail::channel_convert_from_unsigned<ChannelValue> from_unsigned;
482 return from_unsigned()(multiplier_unsigned()(to_unsigned()(a), to_unsigned()(b)));
487 template <
typename Channel>
488 inline typename channel_traits<Channel>::value_type
channel_multiply(Channel a, Channel b) {
507 template <
typename Channel>
512 using base_t =
typename base_channel_type<Channel>::type;
513 using promoted_t =
typename promote_integral<base_t>::type;
514 promoted_t
const promoted_x = x;
515 promoted_t
const promoted_max = channel_traits<Channel>::max_value();
516 promoted_t
const promoted_min = channel_traits<Channel>::min_value();
517 promoted_t
const promoted_inverted_x = promoted_max - promoted_x + promoted_min;
518 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:510
channel_traits< DstChannel >::value_type channel_convert(const SrcChannel &src)
Converting from one channel type to another.
Definition: channel_algorithm.hpp:398
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:488
identity taken from SGI STL.
Definition: utilities.hpp:193
scoped_channel_value< float, float_point_zero< float >, float_point_one< float >> float32_t
32-bit floating point channel type with range [0.0f ... 1.0f]. Models ChannelValueConcept ...
Definition: typedefs.hpp:124
Same as channel_converter, except it takes the destination channel by reference, which allows us to m...
Definition: channel_algorithm.hpp:407
A function object to multiply two channels. result = a * b / max_value.
Definition: channel_algorithm.hpp:474
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:439
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:27
A unary function object converting between channel types.
Definition: channel_algorithm.hpp:384