any_image.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
  9. #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
  10. #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
  11. #include <boost/gil/extension/dynamic_image/apply_operation.hpp>
  12. #include <boost/gil/image.hpp>
  13. #include <boost/config.hpp>
  14. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  15. #pragma warning(push)
  16. #pragma warning(disable:4512) //assignment operator could not be generated
  17. #endif
  18. namespace boost { namespace gil {
  19. namespace detail {
  20. template <typename T> struct get_view_t { using type = typename T::view_t; };
  21. template <typename Images> struct images_get_views_t : public mpl::transform<Images, get_view_t<mpl::_1> > {};
  22. template <typename T> struct get_const_view_t { using type = typename T::const_view_t; };
  23. template <typename Images> struct images_get_const_views_t : public mpl::transform<Images, get_const_view_t<mpl::_1> > {};
  24. struct recreate_image_fnobj
  25. {
  26. using result_type = void;
  27. point<std::ptrdiff_t> const& _dimensions;
  28. unsigned _alignment;
  29. recreate_image_fnobj(point<std::ptrdiff_t> const& dims, unsigned alignment) : _dimensions(dims), _alignment(alignment) {}
  30. template <typename Image>
  31. result_type operator()(Image& img) const { img.recreate(_dimensions,_alignment); }
  32. };
  33. template <typename AnyView> // Models AnyViewConcept
  34. struct any_image_get_view {
  35. using result_type = AnyView;
  36. template <typename Image> result_type operator()( Image& img) const { return result_type(view(img)); }
  37. };
  38. template <typename AnyConstView> // Models AnyConstViewConcept
  39. struct any_image_get_const_view {
  40. using result_type = AnyConstView;
  41. template <typename Image> result_type operator()(const Image& img) const { return result_type(const_view(img)); }
  42. };
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////////////
  45. /// \ingroup ImageModel
  46. /// \brief Represents a run-time specified image. Note it does NOT model ImageConcept
  47. ///
  48. /// Represents an image whose type (color space, layout, planar/interleaved organization, etc) can be specified at run time.
  49. /// It is the runtime equivalent of \p image.
  50. /// Some of the requirements of ImageConcept, such as the \p value_type alias cannot be fulfilled, since the language does not allow runtime type specification.
  51. /// Other requirements, such as access to the pixels, would be inefficient to provide. Thus \p any_image does not fully model ImageConcept.
  52. /// 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.
  53. ////////////////////////////////////////////////////////////////////////////////////////
  54. template <typename ImageTypes>
  55. class any_image : public make_variant_over<ImageTypes>::type {
  56. using parent_t = typename make_variant_over<ImageTypes>::type;
  57. public:
  58. using const_view_t = any_image_view<typename detail::images_get_const_views_t<ImageTypes>::type>;
  59. using view_t = any_image_view<typename detail::images_get_views_t<ImageTypes>::type>;
  60. using x_coord_t = std::ptrdiff_t;
  61. using y_coord_t = std::ptrdiff_t;
  62. using point_t = point<std::ptrdiff_t>;
  63. any_image() : parent_t() {}
  64. template <typename T> explicit any_image(const T& obj) : parent_t(obj) {}
  65. template <typename T> explicit any_image(T& obj, bool do_swap) : parent_t(obj,do_swap) {}
  66. any_image(const any_image& v) : parent_t((const parent_t&)v) {}
  67. template <typename Types> any_image(const any_image<Types>& v) : parent_t((const typename make_variant_over<Types>::type&)v) {}
  68. template <typename T> any_image& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
  69. any_image& operator=(const any_image& v) { parent_t::operator=((const parent_t&)v); return *this;}
  70. template <typename Types> any_image& operator=(const any_image<Types>& v) { parent_t::operator=((const typename make_variant_over<Types>::type&)v); return *this;}
  71. void recreate(const point_t& dims, unsigned alignment=1)
  72. {
  73. apply_operation(*this,detail::recreate_image_fnobj(dims,alignment));
  74. }
  75. void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1)
  76. {
  77. recreate({width, height}, alignment);
  78. }
  79. std::size_t num_channels() const { return apply_operation(*this, detail::any_type_get_num_channels()); }
  80. point_t dimensions() const { return apply_operation(*this, detail::any_type_get_dimensions()); }
  81. x_coord_t width() const { return dimensions().x; }
  82. y_coord_t height() const { return dimensions().y; }
  83. };
  84. ///@{
  85. /// \name view, const_view
  86. /// \brief Get an image view from a run-time instantiated image
  87. /// \ingroup ImageModel
  88. /// \brief Returns the non-constant-pixel view of any image. The returned view is any view.
  89. template <typename Types> BOOST_FORCEINLINE // Models ImageVectorConcept
  90. typename any_image<Types>::view_t view(any_image<Types>& anyImage) {
  91. return apply_operation(anyImage, detail::any_image_get_view<typename any_image<Types>::view_t>());
  92. }
  93. /// \brief Returns the constant-pixel view of any image. The returned view is any view.
  94. template <typename Types> BOOST_FORCEINLINE // Models ImageVectorConcept
  95. typename any_image<Types>::const_view_t const_view(const any_image<Types>& anyImage) {
  96. return apply_operation(anyImage, detail::any_image_get_const_view<typename any_image<Types>::const_view_t>());
  97. }
  98. ///@}
  99. }} // namespace boost::gil
  100. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  101. #pragma warning(pop)
  102. #endif
  103. #endif