dynamic_io_new.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef BOOST_GIL_IO_DYNAMIC_IO_NEW_HPP
  10. #define BOOST_GIL_IO_DYNAMIC_IO_NEW_HPP
  11. /// \file
  12. /// \brief Generic io functions for dealing with dynamic images
  13. //
  14. /// \author Hailin Jin and Lubomir Bourdev \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated May 30, 2006
  17. #include <boost/mpl/at.hpp>
  18. #include <boost/mpl/size.hpp>
  19. #include <boost/gil/gil_config.hpp>
  20. #include <boost/gil/io/error.hpp>
  21. #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
  22. namespace boost { namespace gil {
  23. // need this for various meta functions.
  24. struct any_image_pixel_t {};
  25. struct any_image_channel_t {};
  26. struct any_image_color_space_t {};
  27. namespace detail {
  28. template <long N>
  29. struct construct_matched_t {
  30. template <typename Images,typename Pred>
  31. static bool apply(any_image<Images>& im,Pred pred) {
  32. if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
  33. typename mpl::at_c<Images,N-1>::type x;
  34. im.move_in(x);
  35. return true;
  36. } else return construct_matched_t<N-1>::apply(im,pred);
  37. }
  38. };
  39. template <>
  40. struct construct_matched_t<0> {
  41. template <typename Images,typename Pred>
  42. static bool apply(any_image<Images>&,Pred) {return false;}
  43. };
  44. // A function object that can be passed to apply_operation.
  45. // Given a predicate IsSupported taking a view type and returning an MPL boolean,
  46. // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
  47. template <typename IsSupported, typename OpClass>
  48. class dynamic_io_fnobj {
  49. OpClass* _op;
  50. template <typename View>
  51. void apply(const View& view,mpl::true_ ) {_op->apply(view);}
  52. template <typename View, typename Info >
  53. void apply( const View& view
  54. , const Info& info
  55. , const mpl::true_
  56. )
  57. {
  58. _op->apply( view, info );
  59. }
  60. template <typename View>
  61. void apply(const View& /* view */ ,mpl::false_) {io_error("dynamic_io: unsupported view type for the given file format");}
  62. template <typename View, typename Info >
  63. void apply( const View& /* view */
  64. , const Info& /* info */
  65. , const mpl::false_
  66. )
  67. {
  68. io_error( "dynamic_io: unsupported view type for the given file format" );
  69. }
  70. public:
  71. dynamic_io_fnobj(OpClass* op) : _op(op) {}
  72. typedef void result_type;
  73. template <typename View>
  74. void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
  75. template< typename View, typename Info >
  76. void operator()(const View& view, const Info& info )
  77. {
  78. apply( view
  79. , info
  80. , typename IsSupported::template apply< View >::type()
  81. );
  82. }
  83. };
  84. } // namespace detail
  85. /// \brief Within the any_image, constructs an image with the given dimensions
  86. /// and a type that satisfies the given predicate
  87. template <typename Images,typename Pred>
  88. inline bool construct_matched(any_image<Images>& im,Pred pred) {
  89. return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
  90. }
  91. } } // namespace boost::gil
  92. #endif