Boost GIL


extension/dynamic_image/image_view_factory.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_IMAGE_VIEW_FACTORY_HPP
9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_IMAGE_VIEW_FACTORY_HPP
10 
11 #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
12 
13 #include <boost/gil/dynamic_step.hpp>
14 #include <boost/gil/image_view_factory.hpp>
15 #include <boost/gil/point.hpp>
16 #include <boost/gil/detail/mp11.hpp>
17 
18 #include <cstdint>
19 
20 namespace boost { namespace gil {
21 
22 // Methods for constructing any image views from other any image views
23 // Extends image view factory to runtime type-specified views (any_image_view)
24 
25 namespace detail {
26 
27 template <typename ResultView>
28 struct flipped_up_down_view_fn
29 {
30  using result_type = ResultView;
31 
32  template <typename View>
33  auto operator()(View const& src) const -> result_type
34  {
35  return result_type{flipped_up_down_view(src)};
36  }
37 };
38 
39 template <typename ResultView>
40 struct flipped_left_right_view_fn
41 {
42  using result_type = ResultView;
43 
44  template <typename View>
45  auto operator()(View const& src) const -> result_type
46  {
47  return result_type{flipped_left_right_view(src)};
48  }
49 };
50 
51 template <typename ResultView>
52 struct rotated90cw_view_fn
53 {
54  using result_type = ResultView;
55 
56  template <typename View>
57  auto operator()(View const& src) const -> result_type
58  {
59  return result_type{rotated90cw_view(src)};
60  }
61 };
62 
63 template <typename ResultView>
64 struct rotated90ccw_view_fn
65 {
66  using result_type = ResultView;
67 
68  template <typename View>
69  auto operator()(View const& src) const -> result_type
70  {
71  return result_type{rotated90ccw_view(src)};
72  }
73 };
74 
75 template <typename ResultView>
76 struct tranposed_view_fn
77 {
78  using result_type = ResultView;
79 
80  template <typename View>
81  auto operator()(View const& src) const -> result_type
82  {
83  return result_type{tranposed_view(src)};
84  }
85 };
86 
87 template <typename ResultView>
88 struct rotated180_view_fn
89 {
90  using result_type = ResultView;
91 
92  template <typename View>
93  auto operator()(View const& src) const -> result_type
94  {
95  return result_type{rotated180_view(src)};
96  }
97 };
98 
99 template <typename ResultView>
100 struct subimage_view_fn
101 {
102  using result_type = ResultView;
103 
104  subimage_view_fn(point_t const& topleft, point_t const& dimensions)
105  : _topleft(topleft), _size2(dimensions)
106  {}
107 
108  template <typename View>
109  auto operator()(View const& src) const -> result_type
110  {
111  return result_type{subimage_view(src,_topleft,_size2)};
112  }
113 
114  point_t _topleft;
115  point_t _size2;
116 };
117 
118 template <typename ResultView>
119 struct subsampled_view_fn
120 {
121  using result_type = ResultView;
122 
123  subsampled_view_fn(point_t const& step) : _step(step) {}
124 
125  template <typename View>
126  auto operator()(View const& src) const -> result_type
127  {
128  return result_type{subsampled_view(src,_step)};
129  }
130 
131  point_t _step;
132 };
133 
134 template <typename ResultView>
135 struct nth_channel_view_fn
136 {
137  using result_type = ResultView;
138 
139  nth_channel_view_fn(int n) : _n(n) {}
140 
141  template <typename View>
142  auto operator()(View const& src) const -> result_type
143  {
144  return result_type(nth_channel_view(src,_n));
145  }
146 
147  int _n;
148 };
149 
150 template <typename DstP, typename ResultView, typename CC = default_color_converter>
151 struct color_converted_view_fn
152 {
153  using result_type = ResultView;
154 
155  color_converted_view_fn(CC cc = CC()): _cc(cc) {}
156 
157  template <typename View>
158  auto operator()(View const& src) const -> result_type
159  {
160  return result_type{color_converted_view<DstP>(src, _cc)};
161  }
162 
163 private:
164  CC _cc;
165 };
166 
167 } // namespace detail
168 
171 template <typename Views>
172 inline
175 {
176  using result_view_t = typename dynamic_y_step_type<any_image_view<Views>>::type;
177  return apply_operation(src, detail::flipped_up_down_view_fn<result_view_t>());
178 }
179 
182 template <typename Views>
183 inline
186 {
187  using result_view_t = typename dynamic_x_step_type<any_image_view<Views>>::type;
188  return apply_operation(src, detail::flipped_left_right_view_fn<result_view_t>());
189 }
190 
193 template <typename Views>
194 inline
197 {
198  using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type;
199  return apply_operation(src, detail::tranposed_view_fn<result_view_t>());
200 }
201 
204 template <typename Views>
205 inline
208 {
209  using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type;
210  return apply_operation(src,detail::rotated90cw_view_fn<result_view_t>());
211 }
212 
215 template <typename Views>
216 inline
219 {
220  return apply_operation(src,detail::rotated90ccw_view_fn<typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type>());
221 }
222 
225 template <typename Views>
226 inline
229 {
230  using step_type = typename dynamic_xy_step_type<any_image_view<Views>>::type;
231  return apply_operation(src, detail::rotated180_view_fn<step_type>());
232 }
233 
236 template <typename Views>
237 inline
239  any_image_view<Views> const& src,
240  point_t const& topleft,
241  point_t const& dimensions)
243 {
244  using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views>>;
245  return apply_operation(src, subimage_view_fn(topleft, dimensions));
246 }
247 
250 template <typename Views>
251 inline
253  any_image_view<Views> const& src,
254  std::ptrdiff_t x_min, std::ptrdiff_t y_min, std::ptrdiff_t width, std::ptrdiff_t height)
256 {
257  using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views>>;
258  return apply_operation(src, subimage_view_fn(point_t(x_min, y_min),point_t(width, height)));
259 }
260 
263 template <typename Views>
264 inline
265 auto subsampled_view(any_image_view<Views> const& src, point_t const& step)
267 {
268  using step_type = typename dynamic_xy_step_type<any_image_view<Views>>::type;
269  using subsampled_view = detail::subsampled_view_fn<step_type>;
270  return apply_operation(src, subsampled_view(step));
271 }
272 
275 template <typename Views>
276 inline
277 auto subsampled_view(any_image_view<Views> const& src, std::ptrdiff_t x_step, std::ptrdiff_t y_step)
279 {
280  using step_type = typename dynamic_xy_step_type<any_image_view<Views>>::type;
281  using subsampled_view_fn = detail::subsampled_view_fn<step_type>;
282  return apply_operation(src, subsampled_view_fn(point_t(x_step, y_step)));
283 }
284 
285 namespace detail {
286 
287 template <typename View>
288 struct get_nthchannel_type { using type = typename nth_channel_view_type<View>::type; };
289 
290 template <typename Views>
291 struct views_get_nthchannel_type : mp11::mp_transform<get_nthchannel_type, Views> {};
292 
293 } // namespace detail
294 
297 template <typename Views>
299 {
301 };
302 
305 template <typename Views>
306 inline
309 {
310  using result_view_t = typename nth_channel_view_type<any_image_view<Views>>::type;
311  return apply_operation(src,detail::nth_channel_view_fn<result_view_t>(n));
312 }
313 
314 namespace detail {
315 
316 template <typename View, typename DstP, typename CC>
317 struct get_ccv_type : color_converted_view_type<View, DstP, CC> {};
318 
319 template <typename Views, typename DstP, typename CC>
320 struct views_get_ccv_type
321 {
322 private:
323  // FIXME: Remove class name injection with detail:: qualification
324  // Required as workaround for MP11 issue that treats unqualified metafunction
325  // in the class definition of the same name as the specialization (Peter Dimov):
326  // invalid template argument for template parameter 'F', expected a class template
327  template <typename T>
328  using ccvt = detail::get_ccv_type<T, DstP, CC>;
329 
330 public:
331  using type = mp11::mp_transform<ccvt, Views>;
332 };
333 
334 } // namespace detail
335 
338 template <typename Views, typename DstP, typename CC>
340 {
342 };
343 
347 template <typename DstP, typename Views, typename CC>
348 inline
350  -> typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type
351 {
352  using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type;
353  return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
354 }
355 
358 template <typename Views, typename DstP>
360 {
362 };
363 
367 template <typename DstP, typename Views>
368 inline
370  -> typename color_converted_view_type<any_image_view<Views>, DstP>::type
371 {
372  using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP>::type;
373  return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
374 }
375 
380 template <typename DstP, typename Views, typename CC>
381 inline
383  -> typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type
384 {
385  using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type;
386  return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
387 }
388 
393 template <typename DstP, typename Views>
394 inline
396  -> typename color_converted_view_type<any_image_view<Views>, DstP>::type
397 {
398  using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP>::type;
399  return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
400 }
401 
403 
404 }} // namespace boost::gil
405 
406 #endif
auto flipped_left_right_view(any_image_view< Views > const &src) -> typename dynamic_x_step_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:184
Returns the type of a transposed view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:51
auto nth_channel_view(const any_image_view< Views > &src, int n) -> typename nth_channel_view_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:307
auto color_converted_view(any_image_view< Views > const &src) -> typename color_converted_view_type< any_image_view< Views >, DstP >::type
overload of generic color_converted_view with the default color-converter
Definition: extension/dynamic_image/image_view_factory.hpp:369
Returns the type of a view that has a dynamic step along both X and Y.
Definition: dynamic_step.hpp:27
Given a source image view type View, returns the type of an image view over a single channel of ViewI...
Definition: image_view_factory.hpp:406
auto subimage_view(any_image_view< Views > const &src, std::ptrdiff_t x_min, std::ptrdiff_t y_min, std::ptrdiff_t width, std::ptrdiff_t height) -> any_image_view< Views >
Definition: extension/dynamic_image/image_view_factory.hpp:252
BOOST_FORCEINLINE auto apply_operation(variant< Types > &arg, UnaryOp op)
Invokes a generic mutable operation (represented as a unary function object) on a variant.
Definition: apply_operation.hpp:33
auto rotated90cw_view(const any_image_view< Views > &src) -> typename dynamic_xy_step_transposed_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:206
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition: image_view_factory.hpp:159
auto transposed_view(const any_image_view< Views > &src) -> typename dynamic_xy_step_transposed_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:195
auto flipped_up_down_view(any_image_view< Views > const &src) -> typename dynamic_y_step_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:173
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:66
auto any_color_converted_view(const any_image_view< Views > &src) -> typename color_converted_view_type< any_image_view< Views >, DstP >::type
overload of generic color_converted_view with the default color-converter These are workarounds for G...
Definition: extension/dynamic_image/image_view_factory.hpp:395
auto rotated90ccw_view(const any_image_view< Views > &src) -> typename dynamic_xy_step_transposed_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:217
auto subsampled_view(any_image_view< Views > const &src, std::ptrdiff_t x_step, std::ptrdiff_t y_step) -> typename dynamic_xy_step_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:277
Base template for types that model HasDynamicXStepTypeConcept.
Definition: dynamic_step.hpp:17
auto rotated180_view(any_image_view< Views > const &src) -> typename dynamic_xy_step_type< any_image_view< Views >>::type
Definition: extension/dynamic_image/image_view_factory.hpp:227