Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / gil / extension / dynamic_image / any_image.hpp
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
11 #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
12 #include <boost/gil/extension/dynamic_image/apply_operation.hpp>
13
14 #include <boost/gil/image.hpp>
15 #include <boost/gil/detail/mp11.hpp>
16
17 #include <boost/config.hpp>
18 #include <boost/variant.hpp>
19
20 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
21 #pragma warning(push)
22 #pragma warning(disable:4512) //assignment operator could not be generated
23 #endif
24
25 namespace boost { namespace gil {
26
27 namespace detail {
28
29 template <typename T>
30 using get_view_t = typename T::view_t;
31
32 template <typename Images>
33 using images_get_views_t = mp11::mp_transform<get_view_t, Images>;
34
35 template <typename T>
36 using get_const_view_t = typename T::const_view_t;
37
38 template <typename Images>
39 using images_get_const_views_t = mp11::mp_transform<get_const_view_t, Images>;
40
41 struct recreate_image_fnobj
42 {
43     using result_type = void;
44     point<std::ptrdiff_t> const& _dimensions;
45     unsigned _alignment;
46
47     recreate_image_fnobj(point<std::ptrdiff_t> const& dims, unsigned alignment)
48         : _dimensions(dims), _alignment(alignment)
49     {}
50
51     template <typename Image>
52     result_type operator()(Image& img) const { img.recreate(_dimensions,_alignment); }
53 };
54
55 template <typename AnyView>  // Models AnyViewConcept
56 struct any_image_get_view
57 {
58     using result_type = AnyView;
59     template <typename Image>
60     result_type operator()(Image& img) const
61     {
62         return result_type(view(img));
63     }
64 };
65
66 template <typename AnyConstView>  // Models AnyConstViewConcept
67 struct any_image_get_const_view
68 {
69     using result_type = AnyConstView;
70     template <typename Image>
71     result_type operator()(Image const& img) const { return result_type{const_view(img)}; }
72 };
73
74 } // namespce detail
75
76 ////////////////////////////////////////////////////////////////////////////////////////
77 /// \ingroup ImageModel
78 /// \brief Represents a run-time specified image. Note it does NOT model ImageConcept
79 ///
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 ////////////////////////////////////////////////////////////////////////////////////////
86
87 template <typename Images>
88 class any_image : public make_variant_over<Images>::type
89 {
90     using parent_t = typename make_variant_over<Images>::type;
91 public:
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>;
97
98     any_image() = default;
99     any_image(any_image const& img) : parent_t((parent_t const&)img) {}
100
101     template <typename Image>
102     explicit any_image(Image const& img) : parent_t(img) {}
103
104     template <typename Image>
105     explicit any_image(Image& img, bool do_swap) : parent_t(img, do_swap) {}
106
107     template <typename OtherImages>
108     any_image(any_image<OtherImages> const& img)
109         : parent_t((typename make_variant_over<OtherImages>::type const&)img)
110     {}
111
112     any_image& operator=(any_image const& img)
113     {
114         parent_t::operator=((parent_t const&)img);
115         return *this;
116     }
117
118     template <typename Image>
119     any_image& operator=(Image const& img)
120     {
121         parent_t::operator=(img);
122         return *this;
123     }
124
125     template <typename OtherImages>
126     any_image& operator=(any_image<OtherImages> const& img)
127     {
128             parent_t::operator=((typename make_variant_over<OtherImages>::type const&)img);
129             return *this;
130     }
131
132     void recreate(const point_t& dims, unsigned alignment=1)
133     {
134         apply_operation(*this, detail::recreate_image_fnobj(dims, alignment));
135     }
136
137     void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1)
138     {
139         recreate({ width, height }, alignment);
140     }
141
142     std::size_t num_channels() const
143     {
144         return apply_operation(*this, detail::any_type_get_num_channels());
145     }
146
147     point_t dimensions() const
148     {
149         return apply_operation(*this, detail::any_type_get_dimensions());
150     }
151
152     x_coord_t width()  const { return dimensions().x; }
153     y_coord_t height() const { return dimensions().y; }
154 };
155
156 ///@{
157 /// \name view, const_view
158 /// \brief Get an image view from a run-time instantiated image
159
160 /// \ingroup ImageModel
161
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>
165 BOOST_FORCEINLINE
166 auto view(any_image<Types>& img) -> typename any_image<Types>::view_t
167 {
168     using view_t = typename any_image<Types>::view_t;
169     return apply_operation(img, detail::any_image_get_view<view_t>());
170 }
171
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>
175 BOOST_FORCEINLINE
176 auto const_view(any_image<Types> const& img) -> typename any_image<Types>::const_view_t
177 {
178     using view_t = typename any_image<Types>::const_view_t;
179     return apply_operation(img, detail::any_image_get_const_view<view_t>());
180 }
181 ///@}
182
183 }}  // namespace boost::gil
184
185 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
186 #pragma warning(pop)
187 #endif
188
189 #endif