TuttleOFX  1
dynamic_images.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003 
00004     Use, modification and distribution are subject to the Boost Software License,
00005     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
00006     http://www.boost.org/LICENSE_1_0.txt).
00007 
00008     See http://opensource.adobe.com/gil for most recent version including documentation.
00009 */
00010 /*************************************************************************************************/
00011 
00012 #ifndef BOOST_GIL_EXTENSION_TOOLBOX_DYNAMIC_IMAGES_HPP
00013 #define BOOST_GIL_EXTENSION_TOOLBOX_DYNAMIC_IMAGES_HPP
00014 
00015 /// \file
00016 /// \brief  Generic io functions for dealing with dynamic images
00017 //
00018 /// \author Hailin Jin and Lubomir Bourdev \n
00019 ///         Adobe Systems Incorporated
00020 /// \date   2005-2007 \n Last updated May 30, 2006
00021 
00022 #include <boost/mpl/at.hpp>
00023 #include <boost/mpl/size.hpp>
00024 #include <boost/gil/gil_config.hpp>
00025 #include <boost/gil/extension/io_new/detail/io_error.hpp>
00026 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
00027 
00028 namespace boost { namespace gil {
00029 
00030 // need this for various meta functions.
00031 struct any_image_pixel_t       {};
00032 struct any_image_channel_t     {};
00033 struct any_image_color_space_t {};
00034 
00035 namespace detail {
00036 
00037 template <long N>
00038 struct construct_matched_t {
00039     template <typename Images,typename Pred>
00040     static bool apply(any_image<Images>& im,Pred pred) {
00041         if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
00042             typename mpl::at_c<Images,N-1>::type x;
00043             im.move_in(x);
00044             return true;
00045         } else return construct_matched_t<N-1>::apply(im,pred);
00046     }
00047 };
00048 template <>
00049 struct construct_matched_t<0> {
00050     template <typename Images,typename Pred>
00051     static bool apply(any_image<Images>&,Pred) {return false;}
00052 };
00053 
00054 // A function object that can be passed to apply_operation.
00055 // Given a predicate IsSupported taking a view type and returning an MPL boolean,
00056 // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
00057 template <typename IsSupported, typename OpClass>
00058 class dynamic_io_fnobj {
00059     OpClass* _op;
00060 
00061     template <typename View>
00062     void apply(const View& view,mpl::true_ ) {_op->apply(view);}
00063 
00064     template <typename View, typename Info >
00065     void apply( const View& view
00066               , const Info& info
00067               , const mpl::true_
00068               )
00069     {
00070         _op->apply( view, info );
00071     }
00072 
00073     template <typename View>
00074     void apply(const View& /* view */ ,mpl::false_) {io_error("dynamic_io: unsupported view type for the given file format");}
00075 
00076     template <typename View, typename Info >
00077     void apply( const View& /* view */
00078               , const Info& /* info */
00079               , const mpl::false_
00080               )
00081     {
00082         io_error( "dynamic_io: unsupported view type for the given file format" );
00083     }
00084 
00085 public:
00086     dynamic_io_fnobj(OpClass* op) : _op(op) {}
00087 
00088     typedef void result_type;
00089 
00090     template <typename View>
00091     void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
00092 
00093     template< typename View, typename Info >
00094     void operator()(const View& view, const Info& info )
00095     {
00096         apply( view
00097              , info
00098              , typename IsSupported::template apply< View >::type()
00099              );
00100     }
00101 
00102 };
00103 
00104 } // namespace detail
00105 
00106 /// \brief Within the any_image, constructs an image with the given dimensions
00107 ///        and a type that satisfies the given predicate
00108 template <typename Images,typename Pred>
00109 inline bool construct_matched(any_image<Images>& im,Pred pred) {
00110     return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
00111 }
00112 
00113 } }  // namespace boost::gil
00114 
00115 #endif // BOOST_GIL_EXTENSION_TOOLBOX_DYNAMIC_IMAGES_HPP