diff --git a/include/boost/gil/algorithm.hpp b/include/boost/gil/algorithm.hpp index e19c7a73ed..e6bf78005e 100644 --- a/include/boost/gil/algorithm.hpp +++ b/include/boost/gil/algorithm.hpp @@ -880,7 +880,8 @@ F for_each_pixel(View const& view, F fun) else { for (std::ptrdiff_t y = 0; y < view.height(); ++y) - std::for_each(view.row_begin(y), view.row_end(y), fun); + for (auto begin = view.row_begin(y), end = view.row_end(y); begin != end; ++begin) + fun(*begin); return fun; } } diff --git a/test/core/algorithm/for_each_pixel.cpp b/test/core/algorithm/for_each_pixel.cpp index 5490b27f3a..25eb9c5cfd 100644 --- a/test/core/algorithm/for_each_pixel.cpp +++ b/test/core/algorithm/for_each_pixel.cpp @@ -7,11 +7,10 @@ // #include #include +#include #include -#include "test_utility_output_stream.hpp" - namespace gil = boost::gil; void test_lambda_expression() @@ -26,9 +25,47 @@ void test_lambda_expression() BOOST_TEST_EQ(sum, 2 * 2 * 128); } +struct accumulator +{ + void operator()(gil::gray8_pixel_t const& p) { + sum += gil::at_c<0>(p); + } + + int sum = 0; +}; + +void test_function_object_1d_traversable() +{ + gil::gray8_pixel_t const gray128(128); + gil::gray8_image_t image(2, 2, gray128); + + accumulator acc; + acc = gil::for_each_pixel( + gil::const_view(image), + acc + ); + BOOST_TEST_EQ(acc.sum, 2 * 2 * 128); +} + + +void test_function_object_not_1d_traversable() +{ + gil::gray8_pixel_t const gray128(128); + gil::gray8_image_t image(4, 4, gray128); + + accumulator acc; + acc = gil::for_each_pixel( + gil::subimage_view(gil::const_view(image), gil::point_t{1, 1}, gil::point_t{2, 2}), + acc + ); + BOOST_TEST_EQ(acc.sum, 2 * 2 * 128); +} + int main() { test_lambda_expression(); + test_function_object_1d_traversable(); + test_function_object_not_1d_traversable(); return ::boost::report_errors(); }