Boost GIL


channel_algorithm.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3 
4  Use, modification and distribution are subject to the Boost Software License,
5  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6  http://www.boost.org/LICENSE_1_0.txt).
7 
8  See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 /*************************************************************************************************/
11 
12 #ifndef GIL_CHANNEL_ALGORITHM_HPP
13 #define GIL_CHANNEL_ALGORITHM_HPP
14 
25 
26 #include <boost/config.hpp>
27 #include <boost/integer_traits.hpp>
28 #include <boost/mpl/less.hpp>
29 #include <boost/mpl/integral_c.hpp>
30 #include <boost/mpl/greater.hpp>
31 #include <boost/type_traits.hpp>
32 
33 #include "gil_config.hpp"
34 #include "channel.hpp"
35 
36 namespace boost { namespace gil {
37 
38 //#ifdef _MSC_VER
39 //#pragma warning(push)
40 //#pragma warning(disable: 4309) // disable truncation of constant value warning (using -1 to get the max value of an integral)
41 //#endif
42 
43 namespace detail {
44 
45 // some forward declarations
46 template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral> struct channel_converter_unsigned_impl;
47 template <typename SrcChannelV, typename DstChannelV, bool SrcIsGreater> struct channel_converter_unsigned_integral;
48 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst, bool SrcDivisible> struct channel_converter_unsigned_integral_impl;
49 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst, bool CannotFitInInteger> struct channel_converter_unsigned_integral_nondivisible;
50 
54 
55 
56 template <typename UnsignedIntegralChannel>
57 struct unsigned_integral_max_value : public mpl::integral_c<UnsignedIntegralChannel,integer_traits< UnsignedIntegralChannel>::const_max> {};
58 
59 template <>
60 struct unsigned_integral_max_value<uint8_t> : public mpl::integral_c<uint32_t,0xFF> {};
61 template <>
62 struct unsigned_integral_max_value<uint16_t> : public mpl::integral_c<uint32_t,0xFFFF> {};
63 template <>
64 struct unsigned_integral_max_value<uint32_t> : public mpl::integral_c<uintmax_t,0xFFFFFFFF> {};
65 
66 
67 template <int K>
68 struct unsigned_integral_max_value<packed_channel_value<K> >
69  : public mpl::integral_c<typename packed_channel_value<K>::integer_t, (uint64_t(1)<<K)-1> {};
70 
71 
72 
76 
77 template <typename UnsignedIntegralChannel>
78 struct unsigned_integral_num_bits : public mpl::int_<sizeof(UnsignedIntegralChannel)*8> {};
79 
80 template <int K>
81 struct unsigned_integral_num_bits<packed_channel_value<K> >
82  : public mpl::int_<K> {};
83 
84 } // namespace detail
85 
119 
123 template <typename SrcChannelV, typename DstChannelV> // Model ChannelValueConcept
124 struct channel_converter_unsigned
125  : public detail::channel_converter_unsigned_impl<SrcChannelV,DstChannelV,is_integral<SrcChannelV>::value,is_integral<DstChannelV>::value> {};
126 
127 
129 template <typename T> struct channel_converter_unsigned<T,T> : public detail::identity<T> {};
130 
131 
132 namespace detail {
133 
137 
139 template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral>
140 struct channel_converter_unsigned_impl : public std::unary_function<DstChannelV,SrcChannelV> {
141  DstChannelV operator()(SrcChannelV src) const {
142  return DstChannelV(channel_traits<DstChannelV>::min_value() +
143  (src - channel_traits<SrcChannelV>::min_value()) / channel_range<SrcChannelV>() * channel_range<DstChannelV>());
144  }
145 private:
146  template <typename C>
147  static double channel_range() {
148  return double(channel_traits<C>::max_value()) - double(channel_traits<C>::min_value());
149  }
150 };
151 
152 // When both the source and the destination are integral channels, perform a faster conversion
153 template <typename SrcChannelV, typename DstChannelV>
154 struct channel_converter_unsigned_impl<SrcChannelV,DstChannelV,true,true>
155  : public channel_converter_unsigned_integral<SrcChannelV,DstChannelV,
156  mpl::less<unsigned_integral_max_value<SrcChannelV>,unsigned_integral_max_value<DstChannelV> >::value > {};
157 
158 
162 
163 template <typename SrcChannelV, typename DstChannelV>
164 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,true>
165  : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,
166  !(unsigned_integral_max_value<DstChannelV>::value % unsigned_integral_max_value<SrcChannelV>::value) > {};
167 
168 template <typename SrcChannelV, typename DstChannelV>
169 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,false>
170  : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,
171  !(unsigned_integral_max_value<SrcChannelV>::value % unsigned_integral_max_value<DstChannelV>::value) > {};
172 
173 
177 
178 // Both source and destination are unsigned integral channels,
179 // the src max value is less than the dst max value,
180 // and the dst max value is divisible by the src max value
181 template <typename SrcChannelV, typename DstChannelV>
182 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,true> {
183  DstChannelV operator()(SrcChannelV src) const {
184  typedef typename unsigned_integral_max_value<DstChannelV>::value_type integer_t;
185  static const integer_t mul = unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value;
186  return DstChannelV(src * mul);
187  }
188 };
189 
190 // Both source and destination are unsigned integral channels,
191 // the dst max value is less than (or equal to) the src max value,
192 // and the src max value is divisible by the dst max value
193 template <typename SrcChannelV, typename DstChannelV>
194 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,true> {
195  DstChannelV operator()(SrcChannelV src) const {
196  typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t;
197  static const integer_t div = unsigned_integral_max_value<SrcChannelV>::value / unsigned_integral_max_value<DstChannelV>::value;
198  static const integer_t div2 = div/2;
199  return DstChannelV((src + div2) / div);
200  }
201 };
202 
203 // Prevent overflow for the largest integral type
204 template <typename DstChannelV>
205 struct channel_converter_unsigned_integral_impl<uintmax_t,DstChannelV,false,true> {
206  DstChannelV operator()(uintmax_t src) const {
207  static const uintmax_t div = unsigned_integral_max_value<bits32>::value / unsigned_integral_max_value<DstChannelV>::value;
208  static const uintmax_t div2 = div/2;
209  if (src > unsigned_integral_max_value<uintmax_t>::value - div2)
210  return unsigned_integral_max_value<DstChannelV>::value;
211  return DstChannelV((src + div2) / div);
212  }
213 };
214 
215 // Both source and destination are unsigned integral channels,
216 // and the dst max value is not divisible by the src max value
217 // See if you can represent the expression (src * dst_max) / src_max in integral form
218 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst>
219 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,SrcLessThanDst,false>
220  : public channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,SrcLessThanDst,
221  mpl::greater<
222  mpl::plus<unsigned_integral_num_bits<SrcChannelV>,unsigned_integral_num_bits<DstChannelV> >,
223  unsigned_integral_num_bits<uintmax_t>
224  >::value> {};
225 
226 
227 // Both source and destination are unsigned integral channels,
228 // the src max value is less than the dst max value,
229 // and the dst max value is not divisible by the src max value
230 // The expression (src * dst_max) / src_max fits in an integer
231 template <typename SrcChannelV, typename DstChannelV>
232 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
233  DstChannelV operator()(SrcChannelV src) const {
234  typedef typename base_channel_type<DstChannelV>::type dest_t;
235  return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
236  }
237 };
238 
239 // Both source and destination are unsigned integral channels,
240 // the src max value is less than the dst max value,
241 // and the dst max value is not divisible by the src max value
242 // The expression (src * dst_max) / src_max cannot fit in an integer (overflows). Use a double
243 template <typename SrcChannelV, typename DstChannelV>
244 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,true> {
245  DstChannelV operator()(SrcChannelV src) const {
246  static const double mul = unsigned_integral_max_value<DstChannelV>::value / double(unsigned_integral_max_value<SrcChannelV>::value);
247  return DstChannelV(src * mul);
248  }
249 };
250 
251 // Both source and destination are unsigned integral channels,
252 // the dst max value is less than (or equal to) the src max value,
253 // and the src max value is not divisible by the dst max value
254 template <typename SrcChannelV, typename DstChannelV, bool CannotFit>
255 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,false,CannotFit> {
256  DstChannelV operator()(SrcChannelV src) const {
257 
258  typedef typename detail::unsigned_integral_max_value< SrcChannelV >::value_type src_integer_t;
259  typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
260 
261  static const double div = unsigned_integral_max_value<SrcChannelV>::value
262  / static_cast< double >( unsigned_integral_max_value<DstChannelV>::value );
263 
264  static const src_integer_t div2 = static_cast< src_integer_t >( div / 2.0 );
265 
266  return DstChannelV( static_cast< dst_integer_t >(( static_cast< double >( src + div2 ) / div )));
267  }
268 };
269 
270 } // namespace detail
271 
275 
276 template <typename DstChannelV> struct channel_converter_unsigned<bits32f,DstChannelV> : public std::unary_function<bits32f,DstChannelV> {
277  DstChannelV operator()(bits32f x) const
278  {
279  typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
280  return DstChannelV( static_cast< dst_integer_t >(x*channel_traits<DstChannelV>::max_value()+0.5f ));
281  }
282 };
283 
284 template <typename SrcChannelV> struct channel_converter_unsigned<SrcChannelV,bits32f> : public std::unary_function<SrcChannelV,bits32f> {
285  bits32f operator()(SrcChannelV x) const { return bits32f(x/float(channel_traits<SrcChannelV>::max_value())); }
286 };
287 
288 template <> struct channel_converter_unsigned<bits32f,bits32f> : public std::unary_function<bits32f,bits32f> {
289  bits32f operator()(bits32f x) const { return x; }
290 };
291 
292 
294 template <> struct channel_converter_unsigned<bits32,bits32f> : public std::unary_function<bits32,bits32f> {
295  bits32f operator()(bits32 x) const {
296  // unfortunately without an explicit check it is possible to get a round-off error. We must ensure that max_value of bits32 matches max_value of bits32f
297  if (x>=channel_traits<bits32>::max_value()) return channel_traits<bits32f>::max_value();
298  return float(x) / float(channel_traits<bits32>::max_value());
299  }
300 };
302 template <> struct channel_converter_unsigned<bits32f,bits32> : public std::unary_function<bits32f,bits32> {
303  bits32 operator()(bits32f x) const {
304  // unfortunately without an explicit check it is possible to get a round-off error. We must ensure that max_value of bits32 matches max_value of bits32f
306  return bits32(x * channel_traits<bits32>::max_value() + 0.5f);
307  }
308 };
309 
311 
312 namespace detail {
313 // Converting from signed to unsigned integral channel.
314 // It is both a unary function, and a metafunction (thus requires the 'type' nested typedef, which equals result_type)
315 template <typename ChannelValue> // Model ChannelValueConcept
316 struct channel_convert_to_unsigned : public detail::identity<ChannelValue> {
317  typedef ChannelValue type;
318 };
319 
320 template <> struct channel_convert_to_unsigned<bits8s> : public std::unary_function<bits8s,bits8> {
321  typedef bits8 type;
322  type operator()(bits8s val) const { return val+128; }
323 };
324 
325 template <> struct channel_convert_to_unsigned<bits16s> : public std::unary_function<bits16s,bits16> {
326  typedef bits16 type;
327  type operator()(bits16s val) const { return val+32768; }
328 };
329 
330 template <> struct channel_convert_to_unsigned<bits32s> : public std::unary_function<bits32s,bits32> {
331  typedef bits32 type;
332  type operator()(bits32s x) const { return static_cast<bits32>(x+(1<<31)); }
333 };
334 
335 
336 // Converting from unsigned to signed integral channel
337 // It is both a unary function, and a metafunction (thus requires the 'type' nested typedef, which equals result_type)
338 template <typename ChannelValue> // Model ChannelValueConcept
339 struct channel_convert_from_unsigned : public detail::identity<ChannelValue> {
340  typedef ChannelValue type;
341 };
342 
343 template <> struct channel_convert_from_unsigned<bits8s> : public std::unary_function<bits8,bits8s> {
344  typedef bits8s type;
345  type operator()(bits8 val) const { return val-128; }
346 };
347 
348 template <> struct channel_convert_from_unsigned<bits16s> : public std::unary_function<bits16,bits16s> {
349  typedef bits16s type;
350  type operator()(bits16 val) const { return val-32768; }
351 };
352 
353 template <> struct channel_convert_from_unsigned<bits32s> : public std::unary_function<bits32,bits32s> {
354  typedef bits32s type;
355  type operator()(bits32 x) const { return static_cast<bits32s>(x-(1<<31)); }
356 };
357 
358 } // namespace detail
359 
362 template <typename SrcChannelV, typename DstChannelV> // Model ChannelValueConcept
363 struct channel_converter : public std::unary_function<SrcChannelV,DstChannelV> {
364  DstChannelV operator()(const SrcChannelV& src) const {
365  typedef detail::channel_convert_to_unsigned<SrcChannelV> to_unsigned;
366  typedef detail::channel_convert_from_unsigned<DstChannelV> from_unsigned;
367  typedef channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type> converter_unsigned;
368  return from_unsigned()(converter_unsigned()(to_unsigned()(src)));
369  }
370 };
371 
374 template <typename DstChannel, typename SrcChannel> // Model ChannelConcept (could be channel references)
375 inline typename channel_traits<DstChannel>::value_type channel_convert(const SrcChannel& src) {
377  typename channel_traits<DstChannel>::value_type>()(src);
378 }
379 
385  template <typename Ch1, typename Ch2>
386  void operator()(const Ch1& src, Ch2& dst) const {
387  dst=channel_convert<Ch2>(src);
388  }
389 };
390 
391 namespace detail {
392  // fast integer division by 255
393  inline uint32_t div255(uint32_t in) { uint32_t tmp=in+128; return (tmp + (tmp>>8))>>8; }
394 
395  // fast integer divison by 32768
396  inline uint32_t div32768(uint32_t in) { return (in+16384)>>15; }
397 }
398 
412 
415 template <typename ChannelValue>
416 struct channel_multiplier_unsigned : public std::binary_function<ChannelValue,ChannelValue,ChannelValue> {
417  ChannelValue operator()(ChannelValue a, ChannelValue b) const {
418  return ChannelValue(static_cast<typename base_channel_type<ChannelValue>::type>(a / double(channel_traits<ChannelValue>::max_value()) * b));
419  }
420 };
421 
423 template<> struct channel_multiplier_unsigned<bits8> : public std::binary_function<bits8,bits8,bits8> {
424  bits8 operator()(bits8 a, bits8 b) const { return bits8(detail::div255(uint32_t(a) * uint32_t(b))); }
425 };
426 
428 template<> struct channel_multiplier_unsigned<bits16> : public std::binary_function<bits16,bits16,bits16> {
429  bits16 operator()(bits16 a, bits16 b) const { return bits16((uint32_t(a) * uint32_t(b))/65535); }
430 };
431 
433 template<> struct channel_multiplier_unsigned<bits32f> : public std::binary_function<bits32f,bits32f,bits32f> {
434  bits32f operator()(bits32f a, bits32f b) const { return a*b; }
435 };
436 
438 template <typename ChannelValue>
439 struct channel_multiplier : public std::binary_function<ChannelValue, ChannelValue, ChannelValue> {
440  ChannelValue operator()(ChannelValue a, ChannelValue b) const {
441  typedef detail::channel_convert_to_unsigned<ChannelValue> to_unsigned;
442  typedef detail::channel_convert_from_unsigned<ChannelValue> from_unsigned;
444  return from_unsigned()(multiplier_unsigned()(to_unsigned()(a), to_unsigned()(b)));
445  }
446 };
447 
449 template <typename Channel> // Models ChannelConcept (could be a channel reference)
450 inline typename channel_traits<Channel>::value_type channel_multiply(Channel a, Channel b) {
452 }
454 
469 template <typename Channel> // Models ChannelConcept (could be a channel reference)
474 }
475 
476 //#ifdef _MSC_VER
477 //#pragma warning(pop)
478 //#endif
479 
480 } } // namespace boost::gil
481 
482 #endif
channel_traits< Channel >::value_type channel_invert(Channel x)
Default implementation. Provide overloads for performance.
Definition: channel_algorithm.hpp:472
A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept.
Definition: channel.hpp:155
channel_traits< DstChannel >::value_type channel_convert(const SrcChannel &src)
Converting from one channel type to another.
Definition: channel_algorithm.hpp:375
Channel utilities.
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:450
identity taken from SGI STL.
Definition: utilities.hpp:263
Same as channel_converter, except it takes the destination channel by reference, which allows us to m...
Definition: channel_algorithm.hpp:384
A function object to multiply two channels. result = a * b / max_value.
Definition: channel_algorithm.hpp:439
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:416
Traits for channels. Contains the following members:
Definition: channel.hpp:113
GIL configuration file.
This is the default implementation. Performance specializatons are provided.
Definition: channel_algorithm.hpp:46
A unary function object converting between channel types.
Definition: channel_algorithm.hpp:363