Boost GIL


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 
30 namespace boost { namespace gil {
31 
32 namespace detail {
33 
34 struct 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 
49 template <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 
60 template <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 
71 template <typename ...Types1, typename ...Types2>
73 {
74  return variant2::visit(detail::equal_pixels_fn(), src, dst);
75 }
76 
77 namespace detail {
78 
79 struct 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 
94 template <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 
103 template <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 
112 template <typename ...Types1, typename ...Types2>
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)
119 struct default_color_converter;
120 
125 template <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 
135 template <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 
146 template <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 
156 template <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 
167 template <typename ...Types1, typename ...Types2, typename CC>
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 
178 template <typename ...Types1, typename ...Types2>
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 
187 namespace detail {
188 
189 template <bool IsCompatible>
190 struct 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
197 template <>
198 struct 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 
204 template <typename Value>
205 struct 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 
231 template <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 
237 namespace detail {
238 
239 template <typename F>
240 struct 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 
260 template <typename ...Types, typename F>
261 auto 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
Represents a run-time specified image view. Models HasDynamicXStepTypeConcept, HasDynamicYStepTypeCon...
Definition: any_image_view.hpp:76
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
void copy_and_convert_pixels(any_image_view< Types1... > const &src, any_image_view< Types2... > const &dst)
Definition: extension/dynamic_image/algorithm.hpp:179
void copy_pixels(any_image_view< Types1... > const &src, any_image_view< Types2... > const &dst)
Definition: extension/dynamic_image/algorithm.hpp:113
auto equal_pixels(any_image_view< Types1... > const &src, any_image_view< Types2... > const &dst) -> bool
Definition: extension/dynamic_image/algorithm.hpp:72
void fill_pixels(any_image_view< Types... > const &view, Value const &val)
fill_pixels for any image view. The pixel to fill with must be compatible with the current view
Definition: extension/dynamic_image/algorithm.hpp:232
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36