dynamic_io.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 GIL_DYNAMIC_IO_H
  10. #define GIL_DYNAMIC_IO_H
  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 "../../gil_config.hpp"
  20. #include "io_error.hpp"
  21. #include "../dynamic_image/any_image.hpp"
  22. namespace boost { namespace gil {
  23. namespace detail {
  24. template <long N>
  25. struct construct_matched_t {
  26. template <typename Images,typename Pred>
  27. static bool apply(any_image<Images>& im,Pred pred) {
  28. if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
  29. typename mpl::at_c<Images,N-1>::type x;
  30. im.move_in(x);
  31. return true;
  32. } else return construct_matched_t<N-1>::apply(im,pred);
  33. }
  34. };
  35. template <>
  36. struct construct_matched_t<0> {
  37. template <typename Images,typename Pred>
  38. static bool apply(any_image<Images>&,Pred) {return false;}
  39. };
  40. // A function object that can be passed to apply_operation.
  41. // Given a predicate IsSupported taking a view type and returning an MPL boolean,
  42. // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
  43. template <typename IsSupported, typename OpClass>
  44. class dynamic_io_fnobj {
  45. OpClass* _op;
  46. template <typename View>
  47. void apply(const View& view,mpl::true_ ) {_op->apply(view);}
  48. template <typename View>
  49. void apply(const View& view,mpl::false_) {io_error("dynamic_io: unsupported view type for the given file format");}
  50. public:
  51. dynamic_io_fnobj(OpClass* op) : _op(op) {}
  52. typedef void result_type;
  53. template <typename View>
  54. void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
  55. };
  56. } // namespace detail
  57. /// \brief Within the any_image, constructs an image with the given dimensions
  58. /// and a type that satisfies the given predicate
  59. template <typename Images,typename Pred>
  60. inline bool construct_matched(any_image<Images>& im,Pred pred) {
  61. return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
  62. }
  63. } } // namespace boost::gil
  64. #endif