Skip to content

Commit c063d1c

Browse files
authored
feat: Added for_each_pixel overload for any_image (#648)
Take functor by value instead of reference. Test cases to use the same fixture as the other any_image tests. Fixes #579
1 parent 3289fe0 commit c063d1c

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

include/boost/gil/extension/dynamic_image/algorithm.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ void fill_pixels(any_image_view<Types...> const& view, Value const& val)
230230
apply_operation(view, detail::fill_pixels_fn<Value>(val));
231231
}
232232

233+
namespace detail {
234+
235+
template <typename F>
236+
struct for_each_pixel_fn
237+
{
238+
for_each_pixel_fn(F&& fun) : fun_(std::move(fun)) {}
239+
240+
template <typename View>
241+
auto operator()(View const& view) -> F
242+
{
243+
return for_each_pixel(view, fun_);
244+
}
245+
246+
F fun_;
247+
};
248+
249+
} // namespace detail
250+
251+
/// \defgroup ImageViewSTLAlgorithmsForEachPixel for_each_pixel
252+
/// \ingroup ImageViewSTLAlgorithms
253+
/// \brief std::for_each for any image views
254+
///
255+
/// \ingroup ImageViewSTLAlgorithmsForEachPixel
256+
template <typename ...Types, typename F>
257+
auto for_each_pixel(any_image_view<Types...> const& view, F fun) -> F
258+
{
259+
return variant2::visit(detail::for_each_pixel_fn<F>(std::move(fun)), view);
260+
}
261+
233262
}} // namespace boost::gil
234263

235264
#endif

test/extension/dynamic_image/Jamfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ alias headers : [ generate_self_contained_headers extension/dynamic_image ] ;
1313
run any_image.cpp ;
1414
run any_image_view.cpp ;
1515
run subimage_view.cpp ;
16+
17+
build-project algorithm ;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Boost.GIL (Generic Image Library) - tests
2+
#
3+
# Copyright (c) 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
7+
# copy at http://www.boost.org/LICENSE_1_0.txt)
8+
9+
import testing ;
10+
11+
run for_each_pixel.cpp ;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Copyright 2022 Marco Langer
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+
#include <boost/gil/extension/dynamic_image/any_image.hpp>
9+
#include <boost/gil/extension/dynamic_image/algorithm.hpp>
10+
11+
#include <boost/core/lightweight_test.hpp>
12+
13+
#include "../test_fixture.hpp"
14+
#include "core/image/test_fixture.hpp"
15+
16+
namespace gil = boost::gil;
17+
namespace fixture = boost::gil::test::fixture;
18+
19+
struct accumulator
20+
{
21+
template <typename Pixel>
22+
void operator()(Pixel const& p)
23+
{
24+
sum += gil::at_c<0>(p);
25+
}
26+
27+
int sum{};
28+
};
29+
30+
struct test_for_each_pixel
31+
{
32+
template <typename Image>
33+
void operator()(Image const&)
34+
{
35+
using image_t = Image;
36+
fixture::dynamic_image image(fixture::create_image<image_t>(2, 2, 128));
37+
accumulator acc = gil::for_each_pixel(gil::const_view(image), accumulator());
38+
BOOST_TEST_EQ(acc.sum, 2 * 2 * 128);
39+
}
40+
41+
static void run()
42+
{
43+
boost::mp11::mp_for_each<fixture::image_types>(test_for_each_pixel{});
44+
}
45+
};
46+
47+
int main()
48+
{
49+
test_for_each_pixel::run();
50+
51+
return ::boost::report_errors();
52+
}

0 commit comments

Comments
 (0)