Generic Image Library: algorithm.hpp Source File

###  | #  |

extension/dynamic_image/algorithm.hpp
Go to the documentation of this file.
1//
2// Copyright 2005-2007 Adobe Systems Incorporated
3// Copyright 2022 Marco Langer <langer.m86 at gmail dot com>
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_ALGORITHM_HPP
10#define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ALGORITHM_HPP
11
12#include <boost/gil/extension/dynamic_image/any_image.hpp>
13
14#include <boost/gil/algorithm.hpp>
15
16#include <boost/variant2/variant.hpp>
17
18#include <functional>
19#include <utility>
20
29
30namespace boost { namespace gil {
31
32namespace detail {
33
34struct equal_pixels_fn : binary_operation_obj<equal_pixels_fn, bool>
35{
36 template <typename V1, typename V2>
37 BOOST_FORCEINLINE
38 bool apply_compatible(V1 const& v1, V2 const& v2) const
39 {
40 return equal_pixels(v1, v2);
41 }
42};
43
44} // namespace detail
45
49template <typename ...Types, typename View>
50 auto equal_pixels(any_image_view<Types...> const& src, View const& dst) -> bool
51{
52 return variant2::visit(
53 std::bind(detail::equal_pixels_fn(), std::placeholders::_1, dst),
54 src);
55}
56
60template <typename View, typename ...Types>
61 auto equal_pixels(View const& src, any_image_view<Types...> const& dst) -> bool
62{
63 return variant2::visit(
64 std::bind(detail::equal_pixels_fn(), src, std::placeholders::_1),
65 dst);
66}
67
71template <typename ...Types1, typename ...Types2>
72 auto equal_pixels(any_image_view<Types1...> const& src, any_image_view<Types2...> const& dst) -> bool
73{
74 return variant2::visit(detail::equal_pixels_fn(), src, dst);
75}
76
77namespace detail {
78
79struct copy_pixels_fn : public binary_operation_obj<copy_pixels_fn>
80{
81 template <typename View1, typename View2>
82 BOOST_FORCEINLINE
83 void apply_compatible(View1 const& src, View2 const& dst) const
84 {
85 copy_pixels(src,dst);
86 }
87};
88
89} // namespace detail
90
94template <typename ...Types, typename View>
95 void copy_pixels(any_image_view<Types...> const& src, View const& dst)
96{
97 variant2::visit(std::bind(detail::copy_pixels_fn(), std::placeholders::_1, dst), src);
98}
99
103template <typename ...Types, typename View>
104 void copy_pixels(View const& src, any_image_view<Types...> const& dst)
105{
106 variant2::visit(std::bind(detail::copy_pixels_fn(), src, std::placeholders::_1), dst);
107}
108
112template <typename ...Types1, typename ...Types2>
113 void copy_pixels(any_image_view<Types1...> const& src, any_image_view<Types2...> const& dst)
114{
115 variant2::visit(detail::copy_pixels_fn(), src, dst);
116}
117
118//forward declaration for default_color_converter (see full definition in color_convert.hpp)
119struct default_color_converter;
120
125template <typename ...Types, typename View, typename CC>
126 void copy_and_convert_pixels(any_image_view<Types...> const& src, View const& dst, CC cc)
127{
128 using cc_fn = detail::copy_and_convert_pixels_fn<CC>;
129 variant2::visit(std::bind(cc_fn{cc}, std::placeholders::_1, dst), src);
130}
131
135template <typename ...Types, typename View>
136 void copy_and_convert_pixels(any_image_view<Types...> const& src, View const& dst)
137{
138 using cc_fn = detail::copy_and_convert_pixels_fn<default_color_converter>;
139 variant2::visit(std::bind(cc_fn{}, std::placeholders::_1, dst), src);
140}
141
146template <typename View, typename ...Types, typename CC>
147 void copy_and_convert_pixels(View const& src, any_image_view<Types...> const& dst, CC cc)
148{
149 using cc_fn = detail::copy_and_convert_pixels_fn<CC>;
150 variant2::visit(std::bind(cc_fn{cc}, src, std::placeholders::_1), dst);
151}
152
156template <typename View, typename ...Types>
157 void copy_and_convert_pixels(View const& src, any_image_view<Types...> const& dst)
158{
159 using cc_fn = detail::copy_and_convert_pixels_fn<default_color_converter>;
160 variant2::visit(std::bind(cc_fn{}, src, std::placeholders::_1), dst);
161}
162
167template <typename ...Types1, typename ...Types2, typename CC>
168 void copy_and_convert_pixels(
169 any_image_view<Types1...> const& src,
170 any_image_view<Types2...> const& dst, CC cc)
171{
172 variant2::visit(detail::copy_and_convert_pixels_fn<CC>(cc), src, dst);
173}
174
178template <typename ...Types1, typename ...Types2>
179 void copy_and_convert_pixels(
180 any_image_view<Types1...> const& src,
181 any_image_view<Types2...> const& dst)
182{
183 variant2::visit(
184 detail::copy_and_convert_pixels_fn<default_color_converter>(), src, dst);
185}
186
187namespace detail {
188
189template <bool IsCompatible>
190struct fill_pixels_fn1
191{
192 template <typename V, typename Value>
193 static void apply(V const& src, Value const& val) { fill_pixels(src, val); }
194};
195
196// copy_pixels invoked on incompatible images
197template <>
198struct fill_pixels_fn1<false>
199{
200 template <typename V, typename Value>
201 static void apply(V const&, Value const&) { throw std::bad_cast();}
202};
203
204template <typename Value>
205struct fill_pixels_fn
206{
207 fill_pixels_fn(Value const& val) : val_(val) {}
208
209 using result_type = void;
210 template <typename V>
211 result_type operator()(V const& view) const
212 {
213 fill_pixels_fn1
214 <
215 pixels_are_compatible
216 <
217 typename V::value_type,
218 Value
219 >::value
220 >::apply(view, val_);
221 }
222
223 Value val_;
224};
225
226} // namespace detail
227
231template <typename ...Types, typename Value>
232 void fill_pixels(any_image_view<Types...> const& view, Value const& val)
233{
234 variant2::visit(detail::fill_pixels_fn<Value>(val), view);
235}
236
237namespace detail {
238
239template <typename F>
240struct for_each_pixel_fn
241{
242 for_each_pixel_fn(F&& fun) : fun_(std::move(fun)) {}
243
244 template <typename View>
245 auto operator()(View const& view) -> F
246 {
247 return for_each_pixel(view, fun_);
248 }
249
250 F fun_;
251};
252
253} // namespace detail
254
260template <typename ...Types, typename F>
261auto for_each_pixel(any_image_view<Types...> const& view, F fun) -> F
262{
263 return variant2::visit(detail::for_each_pixel_fn<F>(std::move(fun)), view);
264}
265
266}} // namespace boost::gil
267
268#endif
boost::gil::any_image_view
Represents a run-time specified image view. Models HasDynamicXStepTypeConcept, HasDynamicYStepTypeCon...
Definition any_image_view.hpp:76
boost::gil::view
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
boost::gil::copy_pixels
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition algorithm.hpp:292
boost::gil::equal_pixels
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition algorithm.hpp:1109
boost::gil::fill_pixels
BOOST_FORCEINLINE void fill_pixels(View const &view, Value const &value)
std::fill for image views
Definition algorithm.hpp:420
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition algorithm.hpp:36