Boost GIL


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