Generic Image Library: image_view_factory.hpp Source File

###  | #  |

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
20namespace 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
25namespace detail {
26
27template <typename ResultView>
28struct 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
39template <typename ResultView>
40struct 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
51template <typename ResultView>
52struct 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
63template <typename ResultView>
64struct 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
75template <typename ResultView>
76struct 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
87template <typename ResultView>
88struct 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
99template <typename ResultView>
100struct 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
118template <typename ResultView>
119struct 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
134template <typename ResultView>
135struct 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
150template <typename DstP, typename ResultView, typename CC = default_color_converter>
151struct 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
163private:
164 CC _cc;
165};
166
167} // namespace detail
168
171template <typename ...Views>
172inline
173 auto flipped_up_down_view(any_image_view<Views...> const& src)
174 -> typename dynamic_y_step_type<any_image_view<Views...>>::type
175{
176 using result_view_t = typename dynamic_y_step_type<any_image_view<Views...>>::type;
177 return variant2::visit(detail::flipped_up_down_view_fn<result_view_t>(), src);
178}
179
182template <typename ...Views>
183inline
184 auto flipped_left_right_view(any_image_view<Views...> const& src)
185 -> typename dynamic_x_step_type<any_image_view<Views...>>::type
186{
187 using result_view_t = typename dynamic_x_step_type<any_image_view<Views...>>::type;
188 return variant2::visit(detail::flipped_left_right_view_fn<result_view_t>(), src);
189}
190
193template <typename ...Views>
194inline
195 auto transposed_view(any_image_view<Views...> const& src)
196 -> typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type
197{
198 using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type;
199 return variant2::visit(detail::tranposed_view_fn<result_view_t>(), src);
200}
201
204template <typename ...Views>
205inline
206 auto rotated90cw_view(any_image_view<Views...> const& src)
207 -> typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type
208{
209 using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type;
210 return variant2::visit(detail::rotated90cw_view_fn<result_view_t>(), src);
211}
212
215template <typename ...Views>
216inline
217 auto rotated90ccw_view(any_image_view<Views...> const& src)
218 -> typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type
219{
220 using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type;
221 return variant2::visit(detail::rotated90ccw_view_fn<result_view_t>(), src);
222}
223
226template <typename ...Views>
227inline
228 auto rotated180_view(any_image_view<Views...> const& src)
229 -> typename dynamic_xy_step_type<any_image_view<Views...>>::type
230{
231 using result_view_t = typename dynamic_xy_step_type<any_image_view<Views...>>::type;
232 return variant2::visit(detail::rotated180_view_fn<result_view_t>(), src);
233}
234
237template <typename ...Views>
238inline
239 auto subimage_view(
240 any_image_view<Views...> const& src,
241 point_t const& topleft,
242 point_t const& dimensions)
243 -> any_image_view<Views...>
244{
245 using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views...>>;
246 return variant2::visit(subimage_view_fn(topleft, dimensions), src);
247}
248
251template <typename ...Views>
252inline
253 auto subimage_view(
254 any_image_view<Views...> const& src,
255 std::ptrdiff_t x_min, std::ptrdiff_t y_min, std::ptrdiff_t width, std::ptrdiff_t height)
256 -> any_image_view<Views...>
257{
258 using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views...>>;
259 return variant2::visit(subimage_view_fn(point_t(x_min, y_min),point_t(width, height)), src);
260}
261
264template <typename ...Views>
265inline
266 auto subsampled_view(any_image_view<Views...> const& src, point_t const& step)
267 -> typename dynamic_xy_step_type<any_image_view<Views...>>::type
268{
269 using step_type = typename dynamic_xy_step_type<any_image_view<Views...>>::type;
270 using subsampled_view = detail::subsampled_view_fn<step_type>;
271 return variant2::visit(subsampled_view(step), src);
272}
273
276template <typename ...Views>
277inline
278 auto subsampled_view(any_image_view<Views...> const& src, std::ptrdiff_t x_step, std::ptrdiff_t y_step)
279 -> typename dynamic_xy_step_type<any_image_view<Views...>>::type
280{
281 using step_type = typename dynamic_xy_step_type<any_image_view<Views...>>::type;
282 using subsampled_view_fn = detail::subsampled_view_fn<step_type>;
283 return variant2::visit(subsampled_view_fn(point_t(x_step, y_step)), src);
284}
285
286namespace detail {
287
288template <typename View>
289struct get_nthchannel_type { using type = typename nth_channel_view_type<View>::type; };
290
291template <typename Views>
292struct views_get_nthchannel_type : mp11::mp_transform<get_nthchannel_type, Views> {};
293
294} // namespace detail
295
298template <typename ...Views>
299 struct nth_channel_view_type<any_image_view<Views...>>
300{
301 using type = typename detail::views_get_nthchannel_type<any_image_view<Views...>>;
302};
303
306template <typename ...Views>
307inline
308 auto nth_channel_view(any_image_view<Views...> const& src, int n)
309 -> typename nth_channel_view_type<any_image_view<Views...>>::type
310{
311 using result_view_t = typename nth_channel_view_type<any_image_view<Views...>>::type;
312 return variant2::visit(detail::nth_channel_view_fn<result_view_t>(n), src);
313}
314
315namespace detail {
316
317template <typename Views, typename DstP, typename CC>
318struct views_get_ccv_type
319{
320private:
321 template <typename T>
322 using ccvt = typename color_converted_view_type<T, DstP, CC>::type;
323public:
324 using type = mp11::mp_transform<ccvt, Views>;
325};
326
327} // namespace detail
328
331template <typename ...Views, typename DstP, typename CC>
332 struct color_converted_view_type<any_image_view<Views...>,DstP,CC>
333{
334 //using type = any_image_view<typename detail::views_get_ccv_type<Views, DstP, CC>::type>;
335 using type = typename detail::views_get_ccv_type<any_image_view<Views...>, DstP, CC>::type;
336};
337
341template <typename DstP, typename ...Views, typename CC>
342inline
343 auto color_converted_view(any_image_view<Views...> const& src, CC cc)
344 -> typename color_converted_view_type<any_image_view<Views...>, DstP, CC>::type
345{
346 using cc_view_t = typename color_converted_view_type<any_image_view<Views...>, DstP, CC>::type;
347 return variant2::visit(detail::color_converted_view_fn<DstP, cc_view_t, CC>(cc), src);
348}
349
352template <typename ...Views, typename DstP>
353 struct color_converted_view_type<any_image_view<Views...>,DstP>
354{
355 using type = typename detail::views_get_ccv_type<any_image_view<Views...>, DstP, default_color_converter>::type;
356};
357
361template <typename DstP, typename ...Views>
362inline
363 auto color_converted_view(any_image_view<Views...> const& src)
364 -> typename color_converted_view_type<any_image_view<Views...>, DstP>::type
365{
366 using cc_view_t = typename color_converted_view_type<any_image_view<Views...>, DstP>::type;
367 return variant2::visit(detail::color_converted_view_fn<DstP, cc_view_t>(), src);
368}
369
374template <typename DstP, typename ...Views, typename CC>
375[[deprecated("Use color_converted_view(const any_image_view<Views...>& src, CC) instead.")]]
376inline
377 auto any_color_converted_view(const any_image_view<Views...>& src, CC cc)
378 -> typename color_converted_view_type<any_image_view<Views...>, DstP, CC>::type
379{
380 return color_converted_view(src, cc);
381}
382
387template <typename DstP, typename ...Views>
388[[deprecated("Use color_converted_view(any_image_view<Views...> const& src) instead.")]]
389inline
390 auto any_color_converted_view(const any_image_view<Views...>& src)
391 -> typename color_converted_view_type<any_image_view<Views...>, DstP>::type
392{
393 return color_converted_view(src);
394}
395
397
398}} // namespace boost::gil
399
400#endif
boost::gil::any_image_view
Represents a run-time specified image view. Models HasDynamicXStepTypeConcept, HasDynamicYStepTypeCon...
Definition any_image_view.hpp:76
boost::gil::any_color_converted_view
auto any_color_converted_view(const any_image_view< Views... > &src, CC cc) -> typename color_converted_view_type< any_image_view< Views... >, DstP, CC >::type
overload of generic color_converted_view with user defined color-converter These are workarounds for ...
Definition extension/dynamic_image/image_view_factory.hpp:377
boost::gil::color_converted_view
auto color_converted_view(View const &src, CC cc) -> typename color_converted_view_type< View, DstP, CC >::type
view of a different color space with a user defined color-converter
Definition image_view_factory.hpp:184
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition algorithm.hpp:36
boost::gil::type
boost::gil::color_converted_view_type
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition image_view_factory.hpp:176
boost::gil::default_color_converter
class for color-converting one pixel to another
Definition color_convert.hpp:328
boost::gil::dynamic_x_step_type
Base template for types that model HasDynamicXStepTypeConcept.
Definition dynamic_step.hpp:17
boost::gil::dynamic_xy_step_transposed_type
Returns the type of a transposed view that has a dynamic step along both X and Y.
Definition image_view_factory.hpp:52
boost::gil::dynamic_xy_step_type
Returns the type of a view that has a dynamic step along both X and Y.
Definition image_view_factory.hpp:46
boost::gil::dynamic_y_step_type
Base template for types that model HasDynamicYStepTypeConcept.
Definition dynamic_step.hpp:21
boost::gil::nth_channel_view_type
Given a source image view type View, returns the type of an image view over a single channel of View.
Definition image_view_factory.hpp:437