Boost GIL


any_image.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 // Copyright 2020 Samuel Debionne
4 //
5 // Distributed under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //
9 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
10 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
11 
12 #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
13 
14 #include <boost/gil/image.hpp>
15 #include <boost/gil/detail/mp11.hpp>
16 
17 #include <boost/config.hpp>
18 #include <boost/variant2/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 
86 
87 template <typename ...Images>
88 class any_image : public variant2::variant<Images...>
89 {
90  using parent_t = variant2::variant<Images...>;
91 
92 public:
93  using view_t = mp11::mp_rename<detail::images_get_views_t<any_image>, any_image_view>;
94  using const_view_t = mp11::mp_rename<detail::images_get_const_views_t<any_image>, any_image_view>;
95  using x_coord_t = std::ptrdiff_t;
96  using y_coord_t = std::ptrdiff_t;
98 
99  using parent_t::parent_t;
100 
101  any_image& operator=(any_image const& img)
102  {
103  parent_t::operator=((parent_t const&)img);
104  return *this;
105  }
106 
107  template <typename Image>
108  any_image& operator=(Image const& img)
109  {
110  parent_t::operator=(img);
111  return *this;
112  }
113 
114  template <typename ...OtherImages>
115  any_image& operator=(any_image<OtherImages...> const& img)
116  {
117  parent_t::operator=((typename variant2::variant<OtherImages...> const&)img);
118  return *this;
119  }
120 
121  void recreate(point_t const& dims, unsigned alignment=1)
122  {
123  variant2::visit(detail::recreate_image_fnobj(dims, alignment), *this);
124  }
125 
126  void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1)
127  {
128  recreate({ width, height }, alignment);
129  }
130 
131  std::size_t num_channels() const
132  {
133  return variant2::visit(detail::any_type_get_num_channels(), *this);
134  }
135 
136  point_t dimensions() const
137  {
138  return variant2::visit(detail::any_type_get_dimensions(), *this);
139  }
140 
141  x_coord_t width() const { return dimensions().x; }
142  y_coord_t height() const { return dimensions().y; }
143 };
144 
148 
150 
153 template <typename ...Images>
154 BOOST_FORCEINLINE
155 auto view(any_image<Images...>& img) -> typename any_image<Images...>::view_t
156 {
157  using view_t = typename any_image<Images...>::view_t;
158  return variant2::visit(detail::any_image_get_view<view_t>(), img);
159 }
160 
163 template <typename ...Images>
164 BOOST_FORCEINLINE
165 auto const_view(any_image<Images...> const& img) -> typename any_image<Images...>::const_view_t
166 {
167  using view_t = typename any_image<Images...>::const_view_t;
168  return variant2::visit(detail::any_image_get_const_view<view_t>(), img);
169 }
171 
172 }} // namespace boost::gil
173 
174 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
175 #pragma warning(pop)
176 #endif
177 
178 #endif
Represents a run-time specified image view. Models HasDynamicXStepTypeConcept, HasDynamicYStepTypeCon...
Definition: any_image_view.hpp:76
Represents a run-time specified image. Note it does NOT model ImageConcept.
Definition: any_image.hpp:89
BOOST_FORCEINLINE auto view(any_image< Images... > &img) -> typename any_image< Images... >::view_t
Returns the non-constant-pixel view of any image. The returned view is any view.
Definition: any_image.hpp:155
BOOST_FORCEINLINE auto const_view(any_image< Images... > const &img) -> typename any_image< Images... >::const_view_t
Returns the constant-pixel view of any image. The returned view is any view.
Definition: any_image.hpp:165
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Returns the number of channels of a pixel-based GIL construct.
Definition: pixel.hpp:54