2 // Copyright 2005-2007 Adobe Systems Incorporated
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
8 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_IMAGE_VIEW_FACTORY_HPP
9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_IMAGE_VIEW_FACTORY_HPP
11 #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
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>
20 namespace boost { namespace gil {
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)
27 template <typename ResultView>
28 struct flipped_up_down_view_fn
30 using result_type = ResultView;
32 template <typename View>
33 auto operator()(View const& src) const -> result_type
35 return result_type{flipped_up_down_view(src)};
39 template <typename ResultView>
40 struct flipped_left_right_view_fn
42 using result_type = ResultView;
44 template <typename View>
45 auto operator()(View const& src) const -> result_type
47 return result_type{flipped_left_right_view(src)};
51 template <typename ResultView>
52 struct rotated90cw_view_fn
54 using result_type = ResultView;
56 template <typename View>
57 auto operator()(View const& src) const -> result_type
59 return result_type{rotated90cw_view(src)};
63 template <typename ResultView>
64 struct rotated90ccw_view_fn
66 using result_type = ResultView;
68 template <typename View>
69 auto operator()(View const& src) const -> result_type
71 return result_type{rotated90ccw_view(src)};
75 template <typename ResultView>
76 struct tranposed_view_fn
78 using result_type = ResultView;
80 template <typename View>
81 auto operator()(View const& src) const -> result_type
83 return result_type{tranposed_view(src)};
87 template <typename ResultView>
88 struct rotated180_view_fn
90 using result_type = ResultView;
92 template <typename View>
93 auto operator()(View const& src) const -> result_type
95 return result_type{rotated180_view(src)};
99 template <typename ResultView>
100 struct subimage_view_fn
102 using result_type = ResultView;
104 subimage_view_fn(point_t const& topleft, point_t const& dimensions)
105 : _topleft(topleft), _size2(dimensions)
108 template <typename View>
109 auto operator()(View const& src) const -> result_type
111 return result_type{subimage_view(src,_topleft,_size2)};
118 template <typename ResultView>
119 struct subsampled_view_fn
121 using result_type = ResultView;
123 subsampled_view_fn(point_t const& step) : _step(step) {}
125 template <typename View>
126 auto operator()(View const& src) const -> result_type
128 return result_type{subsampled_view(src,_step)};
134 template <typename ResultView>
135 struct nth_channel_view_fn
137 using result_type = ResultView;
139 nth_channel_view_fn(int n) : _n(n) {}
141 template <typename View>
142 auto operator()(View const& src) const -> result_type
144 return result_type(nth_channel_view(src,_n));
150 template <typename DstP, typename ResultView, typename CC = default_color_converter>
151 struct color_converted_view_fn
153 using result_type = ResultView;
155 color_converted_view_fn(CC cc = CC()): _cc(cc) {}
157 template <typename View>
158 auto operator()(View const& src) const -> result_type
160 return result_type{color_converted_view<DstP>(src, _cc)};
167 } // namespace detail
169 /// \ingroup ImageViewTransformationsFlipUD
170 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
171 template <typename Views>
173 auto flipped_up_down_view(any_image_view<Views> const& src)
174 -> typename dynamic_y_step_type<any_image_view<Views>>::type
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>());
180 /// \ingroup ImageViewTransformationsFlipLR
181 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
182 template <typename Views>
184 auto flipped_left_right_view(any_image_view<Views> const& src)
185 -> typename dynamic_x_step_type<any_image_view<Views>>::type
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>());
191 /// \ingroup ImageViewTransformationsTransposed
192 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
193 template <typename Views>
195 auto transposed_view(const any_image_view<Views>& src)
196 -> typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type
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>());
202 /// \ingroup ImageViewTransformations90CW
203 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
204 template <typename Views>
206 auto rotated90cw_view(const any_image_view<Views>& src)
207 -> typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type
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>());
213 /// \ingroup ImageViewTransformations90CCW
214 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
215 template <typename Views>
217 auto rotated90ccw_view(const any_image_view<Views>& src)
218 -> typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type
220 return apply_operation(src,detail::rotated90ccw_view_fn<typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type>());
223 /// \ingroup ImageViewTransformations180
224 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
225 template <typename Views>
227 auto rotated180_view(any_image_view<Views> const& src)
228 -> typename dynamic_xy_step_type<any_image_view<Views>>::type
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>());
234 /// \ingroup ImageViewTransformationsSubimage
235 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
236 template <typename Views>
239 any_image_view<Views> const& src,
240 point_t const& topleft,
241 point_t const& dimensions)
242 -> any_image_view<Views>
244 using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views>>;
245 return apply_operation(src, subimage_view_fn(topleft, dimensions));
248 /// \ingroup ImageViewTransformationsSubimage
249 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
250 template <typename Views>
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)
255 -> any_image_view<Views>
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)));
261 /// \ingroup ImageViewTransformationsSubsampled
262 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
263 template <typename Views>
265 auto subsampled_view(any_image_view<Views> const& src, point_t const& step)
266 -> typename dynamic_xy_step_type<any_image_view<Views>>::type
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));
273 /// \ingroup ImageViewTransformationsSubsampled
274 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
275 template <typename Views>
277 auto subsampled_view(any_image_view<Views> const& src, std::ptrdiff_t x_step, std::ptrdiff_t y_step)
278 -> typename dynamic_xy_step_type<any_image_view<Views>>::type
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)));
287 template <typename View>
288 struct get_nthchannel_type { using type = typename nth_channel_view_type<View>::type; };
290 template <typename Views>
291 struct views_get_nthchannel_type : mp11::mp_transform<get_nthchannel_type, Views> {};
293 } // namespace detail
295 /// \ingroup ImageViewTransformationsNthChannel
296 /// \brief Given a runtime source image view, returns the type of a runtime image view over a single channel of the source view
297 template <typename Views>
298 struct nth_channel_view_type<any_image_view<Views>>
300 using type = any_image_view<typename detail::views_get_nthchannel_type<Views>::type>;
303 /// \ingroup ImageViewTransformationsNthChannel
304 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
305 template <typename Views>
307 auto nth_channel_view(const any_image_view<Views>& src, int n)
308 -> typename nth_channel_view_type<any_image_view<Views>>::type
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));
316 template <typename View, typename DstP, typename CC>
317 struct get_ccv_type : color_converted_view_type<View, DstP, CC> {};
319 template <typename Views, typename DstP, typename CC>
320 struct views_get_ccv_type
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>;
331 using type = mp11::mp_transform<ccvt, Views>;
334 } // namespace detail
336 /// \ingroup ImageViewTransformationsColorConvert
337 /// \brief Returns the type of a runtime-specified view, color-converted to a given pixel type with user specified color converter
338 template <typename Views, typename DstP, typename CC>
339 struct color_converted_view_type<any_image_view<Views>,DstP,CC>
341 using type = any_image_view<typename detail::views_get_ccv_type<Views, DstP, CC>::type>;
344 /// \ingroup ImageViewTransformationsColorConvert
345 /// \brief overload of generic color_converted_view with user defined color-converter
346 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
347 template <typename DstP, typename Views, typename CC>
349 auto color_converted_view(const any_image_view<Views>& src, CC)
350 -> typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type
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>());
356 /// \ingroup ImageViewTransformationsColorConvert
357 /// \brief Returns the type of a runtime-specified view, color-converted to a given pixel type with the default coor converter
358 template <typename Views, typename DstP>
359 struct color_converted_view_type<any_image_view<Views>,DstP>
361 using type = any_image_view<typename detail::views_get_ccv_type<Views, DstP, default_color_converter>::type>;
364 /// \ingroup ImageViewTransformationsColorConvert
365 /// \brief overload of generic color_converted_view with the default color-converter
366 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
367 template <typename DstP, typename Views>
369 auto color_converted_view(any_image_view<Views> const& src)
370 -> typename color_converted_view_type<any_image_view<Views>, DstP>::type
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>());
376 /// \ingroup ImageViewTransformationsColorConvert
377 /// \brief overload of generic color_converted_view with user defined color-converter
378 /// These are workarounds for GCC 3.4, which thinks color_converted_view is ambiguous with the same method for templated views (in gil/image_view_factory.hpp)
379 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
380 template <typename DstP, typename Views, typename CC>
382 auto any_color_converted_view(const any_image_view<Views>& src, CC)
383 -> typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type
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>());
389 /// \ingroup ImageViewTransformationsColorConvert
390 /// \brief overload of generic color_converted_view with the default color-converter
391 /// These are workarounds for GCC 3.4, which thinks color_converted_view is ambiguous with the same method for templated views (in gil/image_view_factory.hpp)
392 /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
393 template <typename DstP, typename Views>
395 auto any_color_converted_view(const any_image_view<Views>& src)
396 -> typename color_converted_view_type<any_image_view<Views>, DstP>::type
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>());
404 }} // namespace boost::gil