Boost GIL


any_image_view.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_VIEW_HPP
10 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_VIEW_HPP
11 
12 #include <boost/gil/dynamic_step.hpp>
13 #include <boost/gil/image.hpp>
14 #include <boost/gil/image_view.hpp>
15 #include <boost/gil/point.hpp>
16 #include <boost/gil/detail/mp11.hpp>
17 
18 #include <boost/variant2/variant.hpp>
19 
20 namespace boost { namespace gil {
21 
22 template <typename View>
23 struct dynamic_xy_step_transposed_type;
24 
25 namespace detail {
26 
27 template <typename View>
28 using get_const_t = typename View::const_t;
29 
30 template <typename Views>
31 using views_get_const_t = mp11::mp_transform<get_const_t, Views>;
32 
33 // works for both image_view and image
34 struct any_type_get_num_channels
35 {
36  using result_type = int;
37  template <typename T>
38  result_type operator()(const T&) const { return num_channels<T>::value; }
39 };
40 
41 // works for both image_view and image
42 struct any_type_get_dimensions
43 {
44  using result_type = point<std::ptrdiff_t>;
45  template <typename T>
46  result_type operator()(const T& v) const { return v.dimensions(); }
47 };
48 
49 // works for image_view
50 struct any_type_get_size
51 {
52  using result_type = std::size_t;
53  template <typename T>
54  result_type operator()(const T& v) const { return v.size(); }
55 };
56 
57 } // namespace detail
58 
73 
74 template <typename ...Views>
75 class any_image_view : public variant2::variant<Views...>
76 {
77  using parent_t = variant2::variant<Views...>;
78 
79 public:
80  using const_t = detail::views_get_const_t<any_image_view>;
81  using x_coord_t = std::ptrdiff_t;
82  using y_coord_t = std::ptrdiff_t;
84  using size_type = std::size_t;
85 
86  using parent_t::parent_t;
87 
88  any_image_view& operator=(any_image_view const& view)
89  {
90  parent_t::operator=((parent_t const&)view);
91  return *this;
92  }
93 
94  template <typename View>
95  any_image_view& operator=(View const& view)
96  {
97  parent_t::operator=(view);
98  return *this;
99  }
100 
101  template <typename ...OtherViews>
103  {
104  parent_t::operator=((variant2::variant<OtherViews...> const&)view);
105  return *this;
106  }
107 
108  std::size_t num_channels() const { return variant2::visit(detail::any_type_get_num_channels(), *this); }
109  point_t dimensions() const { return variant2::visit(detail::any_type_get_dimensions(), *this); }
110  size_type size() const { return variant2::visit(detail::any_type_get_size(), *this); }
111  x_coord_t width() const { return dimensions().x; }
112  y_coord_t height() const { return dimensions().y; }
113 };
114 
116 // HasDynamicXStepTypeConcept
118 
119 template <typename ...Views>
120 struct dynamic_x_step_type<any_image_view<Views...>>
121 {
122 private:
123  // FIXME: Remove class name injection with gil:: qualification
124  // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
125  // in the class definition of the same name as the specialization (Peter Dimov):
126  // invalid template argument for template parameter 'F', expected a class template
127  template <typename T>
128  using dynamic_step_view = typename gil::dynamic_x_step_type<T>::type;
129 
130 public:
131  using type = mp11::mp_transform<dynamic_step_view, any_image_view<Views...>>;
132 };
133 
135 // HasDynamicYStepTypeConcept
137 
138 template <typename ...Views>
139 struct dynamic_y_step_type<any_image_view<Views...>>
140 {
141 private:
142  // FIXME: Remove class name injection with gil:: qualification
143  // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
144  // in the class definition of the same name as the specialization (Peter Dimov):
145  // invalid template argument for template parameter 'F', expected a class template
146  template <typename T>
147  using dynamic_step_view = typename gil::dynamic_y_step_type<T>::type;
148 
149 public:
150  using type = mp11::mp_transform<dynamic_step_view, any_image_view<Views...>>;
151 };
152 
153 template <typename ...Views>
154 struct dynamic_xy_step_type<any_image_view<Views...>>
155 {
156 private:
157  // FIXME: Remove class name injection with gil:: qualification
158  // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
159  // in the class definition of the same name as the specialization (Peter Dimov):
160  // invalid template argument for template parameter 'F', expected a class template
161  template <typename T>
162  using dynamic_step_view = typename gil::dynamic_xy_step_type<T>::type;
163 
164 public:
165  using type = mp11::mp_transform<dynamic_step_view, any_image_view<Views...>>;
166 };
167 
168 template <typename ...Views>
169 struct dynamic_xy_step_transposed_type<any_image_view<Views...>>
170 {
171 private:
172  // FIXME: Remove class name injection with gil:: qualification
173  // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
174  // in the class definition of the same name as the specialization (Peter Dimov):
175  // invalid template argument for template parameter 'F', expected a class template
176  template <typename T>
177  using dynamic_step_view = typename gil::dynamic_xy_step_type<T>::type;
178 
179 public:
180  using type = mp11::mp_transform<dynamic_step_view, any_image_view<Views...>>;
181 };
182 
183 }} // namespace boost::gil
184 
185 #endif
Represents a run-time specified image view. Models HasDynamicXStepTypeConcept, HasDynamicYStepTypeCon...
Definition: any_image_view.hpp:76
auto view(image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::view_t const &
Returns the non-constant-pixel view of an image.
Definition: image.hpp:565
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Base template for types that model HasDynamicXStepTypeConcept.
Definition: dynamic_step.hpp:17
Base template for types that model HasDynamicYStepTypeConcept.
Definition: dynamic_step.hpp:21
Returns the number of channels of a pixel-based GIL construct.
Definition: pixel.hpp:54
Returns an integral constant type specifying the number of elements in a color base.
Definition: color_base_algorithm.hpp:42