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_DYNAMIC_IO_H
13 #define GIL_DYNAMIC_IO_H
14 
15 /// \file
16 /// \brief  Generic io functions for dealing with dynamic images
17 //
18 /// \author Hailin Jin and Lubomir Bourdev \n
19 ///         Adobe Systems Incorporated
20 /// \date   2005-2007 \n Last updated May 30, 2006
21 
22 #include <boost/mpl/at.hpp>
23 #include <boost/mpl/size.hpp>
24 #include "../../gil_config.hpp"
25 #include "io_error.hpp"
26 #include "../dynamic_image/any_image.hpp"
27 
28 namespace boost { namespace gil {
29 
30 namespace detail {
31 
32 template <long N>
33 struct construct_matched_t {
34     template <typename Images,typename Pred>
applyboost::gil::detail::construct_matched_t35     static bool apply(any_image<Images>& im,Pred pred) {
36         if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
37             typename mpl::at_c<Images,N-1>::type x;
38             im.move_in(x);
39             return true;
40         } else return construct_matched_t<N-1>::apply(im,pred);
41     }
42 };
43 template <>
44 struct construct_matched_t<0> {
45     template <typename Images,typename Pred>
applyboost::gil::detail::construct_matched_t46     static bool apply(any_image<Images>&,Pred) {return false;}
47 };
48 
49 // A function object that can be passed to apply_operation.
50 // Given a predicate IsSupported taking a view type and returning an MPL boolean,
51 // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
52 template <typename IsSupported, typename OpClass>
53 class dynamic_io_fnobj {
54     OpClass* _op;
55 
56     template <typename View>
apply(const View & view,mpl::true_)57     void apply(const View& view,mpl::true_ ) {_op->apply(view);}
58     template <typename View>
apply(const View & view,mpl::false_)59     void apply(const View& view,mpl::false_) {io_error("dynamic_io: unsupported view type for the given file format");}
60 public:
dynamic_io_fnobj(OpClass * op)61     dynamic_io_fnobj(OpClass* op) : _op(op) {}
62 
63     typedef void result_type;
64 
65     template <typename View>
operator ()(const View & view)66     void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
67 };
68 
69 } // namespace detail
70 
71 /// \brief Within the any_image, constructs an image with the given dimensions
72 ///        and a type that satisfies the given predicate
73 template <typename Images,typename Pred>
construct_matched(any_image<Images> & im,Pred pred)74 inline bool construct_matched(any_image<Images>& im,Pred pred) {
75     return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
76 }
77 
78 } }  // namespace boost::gil
79 
80 #endif
81