diff --git a/include/boost/gil/extension/dynamic_image/algorithm.hpp b/include/boost/gil/extension/dynamic_image/algorithm.hpp index e63e1f9825..75a4aedb7f 100644 --- a/include/boost/gil/extension/dynamic_image/algorithm.hpp +++ b/include/boost/gil/extension/dynamic_image/algorithm.hpp @@ -230,6 +230,35 @@ void fill_pixels(any_image_view const& view, Value const& val) apply_operation(view, detail::fill_pixels_fn(val)); } +namespace detail { + +template +struct for_each_pixel_fn +{ + for_each_pixel_fn(F&& fun) : fun_(std::move(fun)) {} + + template + auto operator()(View const& view) -> F + { + return for_each_pixel(view, fun_); + } + + F fun_; +}; + +} // namespace detail + +/// \defgroup ImageViewSTLAlgorithmsForEachPixel for_each_pixel +/// \ingroup ImageViewSTLAlgorithms +/// \brief std::for_each for any image views +/// +/// \ingroup ImageViewSTLAlgorithmsForEachPixel +template +auto for_each_pixel(any_image_view const& view, F fun) -> F +{ + return variant2::visit(detail::for_each_pixel_fn(std::move(fun)), view); +} + }} // namespace boost::gil #endif diff --git a/test/extension/dynamic_image/Jamfile b/test/extension/dynamic_image/Jamfile index 63189265e4..4d78b7554b 100644 --- a/test/extension/dynamic_image/Jamfile +++ b/test/extension/dynamic_image/Jamfile @@ -13,3 +13,5 @@ alias headers : [ generate_self_contained_headers extension/dynamic_image ] ; run any_image.cpp ; run any_image_view.cpp ; run subimage_view.cpp ; + +build-project algorithm ; diff --git a/test/extension/dynamic_image/algorithm/Jamfile b/test/extension/dynamic_image/algorithm/Jamfile new file mode 100644 index 0000000000..cdca8c65e8 --- /dev/null +++ b/test/extension/dynamic_image/algorithm/Jamfile @@ -0,0 +1,11 @@ +# Boost.GIL (Generic Image Library) - tests +# +# Copyright (c) 2022 Marco Langer +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or +# copy at http://www.boost.org/LICENSE_1_0.txt) + +import testing ; + +run for_each_pixel.cpp ; diff --git a/test/extension/dynamic_image/algorithm/for_each_pixel.cpp b/test/extension/dynamic_image/algorithm/for_each_pixel.cpp new file mode 100644 index 0000000000..67a947cac2 --- /dev/null +++ b/test/extension/dynamic_image/algorithm/for_each_pixel.cpp @@ -0,0 +1,52 @@ +// +// Copyright 2022 Marco Langer +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +#include +#include + +#include + +#include "../test_fixture.hpp" +#include "core/image/test_fixture.hpp" + +namespace gil = boost::gil; +namespace fixture = boost::gil::test::fixture; + +struct accumulator +{ + template + void operator()(Pixel const& p) + { + sum += gil::at_c<0>(p); + } + + int sum{}; +}; + +struct test_for_each_pixel +{ + template + void operator()(Image const&) + { + using image_t = Image; + fixture::dynamic_image image(fixture::create_image(2, 2, 128)); + accumulator acc = gil::for_each_pixel(gil::const_view(image), accumulator()); + BOOST_TEST_EQ(acc.sum, 2 * 2 * 128); + } + + static void run() + { + boost::mp11::mp_for_each(test_for_each_pixel{}); + } +}; + +int main() +{ + test_for_each_pixel::run(); + + return ::boost::report_errors(); +} \ No newline at end of file