2 // Copyright 2005-2007 Adobe Systems Incorporated
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
8 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
11 #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
12 #include <boost/gil/extension/dynamic_image/apply_operation.hpp>
14 #include <boost/gil/image.hpp>
15 #include <boost/gil/detail/mp11.hpp>
17 #include <boost/config.hpp>
18 #include <boost/variant.hpp>
20 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
22 #pragma warning(disable:4512) //assignment operator could not be generated
25 namespace boost { namespace gil {
30 using get_view_t = typename T::view_t;
32 template <typename Images>
33 using images_get_views_t = mp11::mp_transform<get_view_t, Images>;
36 using get_const_view_t = typename T::const_view_t;
38 template <typename Images>
39 using images_get_const_views_t = mp11::mp_transform<get_const_view_t, Images>;
41 struct recreate_image_fnobj
43 using result_type = void;
44 point<std::ptrdiff_t> const& _dimensions;
47 recreate_image_fnobj(point<std::ptrdiff_t> const& dims, unsigned alignment)
48 : _dimensions(dims), _alignment(alignment)
51 template <typename Image>
52 result_type operator()(Image& img) const { img.recreate(_dimensions,_alignment); }
55 template <typename AnyView> // Models AnyViewConcept
56 struct any_image_get_view
58 using result_type = AnyView;
59 template <typename Image>
60 result_type operator()(Image& img) const
62 return result_type(view(img));
66 template <typename AnyConstView> // Models AnyConstViewConcept
67 struct any_image_get_const_view
69 using result_type = AnyConstView;
70 template <typename Image>
71 result_type operator()(Image const& img) const { return result_type{const_view(img)}; }
76 ////////////////////////////////////////////////////////////////////////////////////////
77 /// \ingroup ImageModel
78 /// \brief Represents a run-time specified image. Note it does NOT model ImageConcept
80 /// Represents an image whose type (color space, layout, planar/interleaved organization, etc) can be specified at run time.
81 /// It is the runtime equivalent of \p image.
82 /// 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.
83 /// Other requirements, such as access to the pixels, would be inefficient to provide. Thus \p any_image does not fully model ImageConcept.
84 /// 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.
85 ////////////////////////////////////////////////////////////////////////////////////////
87 template <typename Images>
88 class any_image : public make_variant_over<Images>::type
90 using parent_t = typename make_variant_over<Images>::type;
92 using view_t = any_image_view<detail::images_get_views_t<Images>>;
93 using const_view_t = any_image_view<detail::images_get_const_views_t<Images>>;
94 using x_coord_t = std::ptrdiff_t;
95 using y_coord_t = std::ptrdiff_t;
96 using point_t = point<std::ptrdiff_t>;
98 any_image() = default;
99 any_image(any_image const& img) : parent_t((parent_t const&)img) {}
101 template <typename Image>
102 explicit any_image(Image const& img) : parent_t(img) {}
104 template <typename Image>
105 explicit any_image(Image& img, bool do_swap) : parent_t(img, do_swap) {}
107 template <typename OtherImages>
108 any_image(any_image<OtherImages> const& img)
109 : parent_t((typename make_variant_over<OtherImages>::type const&)img)
112 any_image& operator=(any_image const& img)
114 parent_t::operator=((parent_t const&)img);
118 template <typename Image>
119 any_image& operator=(Image const& img)
121 parent_t::operator=(img);
125 template <typename OtherImages>
126 any_image& operator=(any_image<OtherImages> const& img)
128 parent_t::operator=((typename make_variant_over<OtherImages>::type const&)img);
132 void recreate(const point_t& dims, unsigned alignment=1)
134 apply_operation(*this, detail::recreate_image_fnobj(dims, alignment));
137 void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1)
139 recreate({ width, height }, alignment);
142 std::size_t num_channels() const
144 return apply_operation(*this, detail::any_type_get_num_channels());
147 point_t dimensions() const
149 return apply_operation(*this, detail::any_type_get_dimensions());
152 x_coord_t width() const { return dimensions().x; }
153 y_coord_t height() const { return dimensions().y; }
157 /// \name view, const_view
158 /// \brief Get an image view from a run-time instantiated image
160 /// \ingroup ImageModel
162 /// \brief Returns the non-constant-pixel view of any image. The returned view is any view.
163 /// \tparam Types Models ImageVectorConcept
164 template <typename Types>
166 auto view(any_image<Types>& img) -> typename any_image<Types>::view_t
168 using view_t = typename any_image<Types>::view_t;
169 return apply_operation(img, detail::any_image_get_view<view_t>());
172 /// \brief Returns the constant-pixel view of any image. The returned view is any view.
173 /// \tparam Types Models ImageVectorConcept
174 template <typename Types>
176 auto const_view(any_image<Types> const& img) -> typename any_image<Types>::const_view_t
178 using view_t = typename any_image<Types>::const_view_t;
179 return apply_operation(img, detail::any_image_get_const_view<view_t>());
183 }} // namespace boost::gil
185 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)