any_image.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_DYNAMICIMAGE_ANY_IMAGE_HPP
  10. #define GIL_DYNAMICIMAGE_ANY_IMAGE_HPP
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief Support for run-time instantiated images and image views
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. ///
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include "any_image_view.hpp"
  20. #include "../../image.hpp"
  21. //#ifdef _MSC_VER
  22. //#pragma warning(push)
  23. //#pragma warning(disable : 4244) // conversion from 'std::ptrdiff_t' to 'int', possible loss of data. even if we static-assert the two types are the same (on visual studio 8)
  24. //#endif
  25. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  26. #pragma warning(push)
  27. #pragma warning(disable:4512) //assignment operator could not be generated
  28. #endif
  29. namespace boost { namespace gil {
  30. namespace detail {
  31. template <typename T> struct get_view_t { typedef typename T::view_t type; };
  32. template <typename Images> struct images_get_views_t : public mpl::transform<Images, get_view_t<mpl::_1> > {};
  33. template <typename T> struct get_const_view_t { typedef typename T::const_view_t type; };
  34. template <typename Images> struct images_get_const_views_t : public mpl::transform<Images, get_const_view_t<mpl::_1> > {};
  35. struct recreate_image_fnobj {
  36. typedef void result_type;
  37. const point2<std::ptrdiff_t>& _dimensions;
  38. unsigned _alignment;
  39. recreate_image_fnobj(const point2<std::ptrdiff_t>& dims, unsigned alignment) : _dimensions(dims), _alignment(alignment) {}
  40. template <typename Image> result_type operator()(Image& img) const { img.recreate(_dimensions,_alignment); }
  41. };
  42. template <typename AnyView> // Models AnyViewConcept
  43. struct any_image_get_view {
  44. typedef AnyView result_type;
  45. template <typename Image> result_type operator()( Image& img) const { return result_type(view(img)); }
  46. };
  47. template <typename AnyConstView> // Models AnyConstViewConcept
  48. struct any_image_get_const_view {
  49. typedef AnyConstView result_type;
  50. template <typename Image> result_type operator()(const Image& img) const { return result_type(const_view(img)); }
  51. };
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////////////
  54. /// \ingroup ImageModel
  55. /// \brief Represents a run-time specified image. Note it does NOT model ImageConcept
  56. ///
  57. /// Represents an image whose type (color space, layout, planar/interleaved organization, etc) can be specified at run time.
  58. /// It is the runtime equivalent of \p image.
  59. /// Some of the requirements of ImageConcept, such as the \p value_type typedef cannot be fulfilled, since the language does not allow runtime type specification.
  60. /// Other requirements, such as access to the pixels, would be inefficient to provide. Thus \p any_image does not fully model ImageConcept.
  61. /// In particular, its \p view and \p const_view methods return \p any_image_view, which does not fully model ImageViewConcept. See \p any_image_view for more.
  62. ////////////////////////////////////////////////////////////////////////////////////////
  63. template <typename ImageTypes>
  64. class any_image : public variant<ImageTypes> {
  65. typedef variant<ImageTypes> parent_t;
  66. public:
  67. typedef any_image_view<typename detail::images_get_const_views_t<ImageTypes>::type> const_view_t;
  68. typedef any_image_view<typename detail::images_get_views_t<ImageTypes>::type> view_t;
  69. typedef std::ptrdiff_t x_coord_t;
  70. typedef std::ptrdiff_t y_coord_t;
  71. typedef point2<std::ptrdiff_t> point_t;
  72. any_image() : parent_t() {}
  73. template <typename T> explicit any_image(const T& obj) : parent_t(obj) {}
  74. template <typename T> explicit any_image(T& obj, bool do_swap) : parent_t(obj,do_swap) {}
  75. any_image(const any_image& v) : parent_t((const parent_t&)v) {}
  76. template <typename Types> any_image(const any_image<Types>& v) : parent_t((const variant<Types>&)v) {}
  77. template <typename T> any_image& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
  78. any_image& operator=(const any_image& v) { parent_t::operator=((const parent_t&)v); return *this;}
  79. template <typename Types> any_image& operator=(const any_image<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
  80. void recreate(const point_t& dims, unsigned alignment=1) { apply_operation(*this,detail::recreate_image_fnobj(dims,alignment)); }
  81. void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1) { recreate(point2<std::ptrdiff_t>(width,height),alignment); }
  82. std::size_t num_channels() const { return apply_operation(*this, detail::any_type_get_num_channels()); }
  83. point_t dimensions() const { return apply_operation(*this, detail::any_type_get_dimensions()); }
  84. x_coord_t width() const { return dimensions().x; }
  85. y_coord_t height() const { return dimensions().y; }
  86. };
  87. ///@{
  88. /// \name view, const_view
  89. /// \brief Get an image view from a run-time instantiated image
  90. /// \ingroup ImageModel
  91. /// \brief Returns the non-constant-pixel view of any image. The returned view is any view.
  92. template <typename Types> BOOST_FORCEINLINE // Models ImageVectorConcept
  93. typename any_image<Types>::view_t view(any_image<Types>& anyImage) {
  94. return apply_operation(anyImage, detail::any_image_get_view<typename any_image<Types>::view_t>());
  95. }
  96. /// \brief Returns the constant-pixel view of any image. The returned view is any view.
  97. template <typename Types> BOOST_FORCEINLINE // Models ImageVectorConcept
  98. typename any_image<Types>::const_view_t const_view(const any_image<Types>& anyImage) {
  99. return apply_operation(anyImage, detail::any_image_get_const_view<typename any_image<Types>::const_view_t>());
  100. }
  101. ///@}
  102. } } // namespace boost::gil
  103. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  104. #pragma warning(pop)
  105. #endif
  106. //#ifdef _MSC_VER
  107. //#pragma warning(pop)
  108. //#endif
  109. #endif