Boost GIL


dynamic_io_new.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_IO_DYNAMIC_IO_NEW_HPP
9 #define BOOST_GIL_IO_DYNAMIC_IO_NEW_HPP
10 
11 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
12 
13 #include <boost/gil/io/error.hpp>
14 
15 #include <boost/mpl/at.hpp>
16 #include <boost/mpl/size.hpp>
17 
18 namespace boost { namespace gil {
19 
20 namespace detail {
21 
22 template <long N>
23 struct construct_matched_t {
24  template <typename Images,typename Pred>
25  static bool apply(any_image<Images>& im,Pred pred) {
26  if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
27  typename mpl::at_c<Images,N-1>::type x;
28  im = std::move(x);
29  return true;
30  } else return construct_matched_t<N-1>::apply(im,pred);
31  }
32 };
33 template <>
34 struct construct_matched_t<0> {
35  template <typename Images,typename Pred>
36  static bool apply(any_image<Images>&,Pred) {return false;}
37 };
38 
39 // A function object that can be passed to apply_operation.
40 // Given a predicate IsSupported taking a view type and returning an MPL boolean,
41 // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
42 template <typename IsSupported, typename OpClass>
43 class dynamic_io_fnobj {
44  OpClass* _op;
45 
46  template <typename View>
47  void apply(const View& view,mpl::true_ ) {_op->apply(view);}
48 
49  template <typename View, typename Info >
50  void apply( const View& view
51  , const Info& info
52  , const mpl::true_
53  )
54  {
55  _op->apply( view, info );
56  }
57 
58  template <typename View>
59  void apply(const View& /* view */ ,mpl::false_) {io_error("dynamic_io: unsupported view type for the given file format");}
60 
61  template <typename View, typename Info >
62  void apply( const View& /* view */
63  , const Info& /* info */
64  , const mpl::false_
65  )
66  {
67  io_error( "dynamic_io: unsupported view type for the given file format" );
68  }
69 
70 public:
71  dynamic_io_fnobj(OpClass* op) : _op(op) {}
72 
73  using result_type = void;
74 
75  template <typename View>
76  void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
77 
78  template< typename View, typename Info >
79  void operator()(const View& view, const Info& info )
80  {
81  apply( view
82  , info
83  , typename IsSupported::template apply< View >::type()
84  );
85  }
86 
87 };
88 
89 } // namespace detail
90 
93 template <typename Images,typename Pred>
94 inline bool construct_matched(any_image<Images>& im,Pred pred) {
95  return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
96 }
97 
98 } } // namespace boost::gil
99 
100 #endif
Definition: algorithm.hpp:30
add_reference< E >::type at_c(detail::homogeneous_color_base< E, L, N > &p)
Provides mutable access to the K-th element, in physical order.
Definition: color_base.hpp:387
const image< Pixel, IsPlanar, Alloc >::view_t & view(image< Pixel, IsPlanar, Alloc > &img)
Returns the non-constant-pixel view of an image.
Definition: image.hpp:460