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 using argument_type = SrcChannelV;
123 using result_type = DstChannelV;
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 using integer_t = typename unsigned_integral_max_value<DstChannelV>::value_type;
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 using integer_t = typename unsigned_integral_max_value<SrcChannelV>::value_type;
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 using dest_t = typename base_channel_type<DstChannelV>::type;
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 using src_integer_t = typename detail::unsigned_integral_max_value<SrcChannelV>::value_type;
242 using dst_integer_t = typename detail::unsigned_integral_max_value<DstChannelV>::value_type;
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 using argument_type = float32_t;
261 using result_type = DstChannelV;
262 DstChannelV operator()(float32_t x) const
264 using dst_integer_t = typename detail::unsigned_integral_max_value<DstChannelV>::value_type;
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 using argument_type = float32_t;
271 using result_type = SrcChannelV;
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 using argument_type = float32_t;
277 using result_type = float32_t;
278 float32_t operator()(float32_t x) const { return x; }
283 template <> struct channel_converter_unsigned<uint32_t,float32_t> {
284 using argument_type = uint32_t;
285 using result_type = float32_t;
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 using result_type = uint32_t;
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 using type = ChannelValue;
317 template <>
struct channel_convert_to_unsigned<int8_t> {
318 using argument_type = int8_t;
319 using result_type = uint8_t;
320 using type = uint8_t;
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 using argument_type = int16_t;
328 using result_type = uint16_t;
329 using type = uint16_t;
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 using argument_type = int32_t;
337 using result_type = uint32_t;
338 using type = uint32_t;
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 using type = ChannelValue;
352 template <>
struct channel_convert_from_unsigned<int8_t> {
353 using argument_type = uint8_t;
354 using result_type = int8_t;
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 using argument_type = uint16_t;
363 using result_type = int16_t;
364 using type = int16_t;
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 using argument_type = uint32_t;
372 using result_type = int32_t;
373 using type = int32_t;
374 type operator()(uint32_t val)
const {
375 return static_cast<int32_t
>(val - (1u<<31));
383 template <
typename SrcChannelV,
typename DstChannelV>
385 using argument_type = SrcChannelV;
386 using result_type = DstChannelV;
387 DstChannelV operator()(
const SrcChannelV& src)
const {
388 using to_unsigned = detail::channel_convert_to_unsigned<SrcChannelV>;
389 using from_unsigned = detail::channel_convert_from_unsigned<DstChannelV>;
390 using converter_unsigned = channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type>;
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 using first_argument_type = ChannelValue;
441 using second_argument_type = ChannelValue;
442 using result_type = ChannelValue;
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 using first_argument_type = uint8_t;
451 using second_argument_type = uint8_t;
452 using result_type = uint8_t;
453 uint8_t operator()(uint8_t a, uint8_t b)
const {
return uint8_t(detail::div255(uint32_t(a) * uint32_t(b))); }
458 using first_argument_type = uint16_t;
459 using second_argument_type = uint16_t;
460 using result_type = uint16_t;
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 using first_argument_type = ChannelValue;
476 using second_argument_type = ChannelValue;
477 using result_type = ChannelValue;
478 ChannelValue operator()(ChannelValue a, ChannelValue b)
const {
479 using to_unsigned = detail::channel_convert_to_unsigned<ChannelValue>;
480 using from_unsigned = detail::channel_convert_from_unsigned<ChannelValue>;
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