dynamic_images.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_EXTENSION_TOOLBOX_DYNAMIC_IMAGES_HPP
  10. #define BOOST_GIL_EXTENSION_TOOLBOX_DYNAMIC_IMAGES_HPP
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file dynamic_images.hpp
  13. /// \brief Generic io functions for dealing with dynamic images.
  14. /// \author Hailin Jin, Lubomir Bourdev, and Christian Henning \n
  15. ///
  16. /// \date 2012 \n
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include <boost/mpl/at.hpp>
  20. #include <boost/mpl/size.hpp>
  21. #include <boost/gil/gil_config.hpp>
  22. #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
  23. namespace boost { namespace gil {
  24. // need this for various meta functions.
  25. struct any_image_pixel_t {};
  26. struct any_image_channel_t {};
  27. struct any_image_color_space_t {};
  28. namespace detail {
  29. template <long N>
  30. struct construct_matched_t {
  31. template <typename Images,typename Pred>
  32. static bool apply(any_image<Images>& im,Pred pred) {
  33. if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
  34. typename mpl::at_c<Images,N-1>::type x;
  35. im.move_in(x);
  36. return true;
  37. } else return construct_matched_t<N-1>::apply(im,pred);
  38. }
  39. };
  40. template <>
  41. struct construct_matched_t<0> {
  42. template <typename Images,typename Pred>
  43. static bool apply(any_image<Images>&,Pred) {return false;}
  44. };
  45. // A function object that can be passed to apply_operation.
  46. // Given a predicate IsSupported taking a view type and returning an MPL boolean,
  47. // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
  48. template <typename IsSupported, typename OpClass>
  49. class dynamic_io_fnobj {
  50. OpClass* _op;
  51. template <typename View>
  52. void apply(const View& view,mpl::true_ ) {_op->apply(view);}
  53. template <typename View, typename Info >
  54. void apply( const View& view
  55. , const Info& info
  56. , const mpl::true_
  57. )
  58. {
  59. _op->apply( view, info );
  60. }
  61. template <typename View>
  62. void apply(const View& /* view */ ,mpl::false_)
  63. {
  64. throw std::ios_base::failure( "dynamic_io: unsupported view type for the given file format" );
  65. }
  66. template <typename View, typename Info >
  67. void apply( const View& /* view */
  68. , const Info& /* info */
  69. , const mpl::false_
  70. )
  71. {
  72. throw std::ios_base::failure( "dynamic_io: unsupported view type for the given file format" );
  73. }
  74. public:
  75. dynamic_io_fnobj(OpClass* op) : _op(op) {}
  76. typedef void result_type;
  77. template <typename View>
  78. void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
  79. template< typename View, typename Info >
  80. void operator()(const View& view, const Info& info )
  81. {
  82. apply( view
  83. , info
  84. , typename IsSupported::template apply< View >::type()
  85. );
  86. }
  87. };
  88. } // namespace detail
  89. /// \brief Within the any_image, constructs an image with the given dimensions
  90. /// and a type that satisfies the given predicate
  91. template <typename Images,typename Pred>
  92. inline bool construct_matched(any_image<Images>& im,Pred pred) {
  93. return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
  94. }
  95. template<>
  96. struct color_space_type< any_image_pixel_t >
  97. {
  98. typedef any_image_color_space_t type;
  99. };
  100. } } // namespace boost::gil
  101. #endif // BOOST_GIL_EXTENSION_TOOLBOX_DYNAMIC_IMAGES_HPP