Skip to content

Commit

Permalink
Merge pull request #621 from sdebionne/fix-620
Browse files Browse the repository at this point in the history
Fix for_each_pixel for non 1d iterable views
  • Loading branch information
sdebionne authored Mar 1, 2022
2 parents f4c70a8 + 2cc525a commit da0655f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/boost/gil/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
41 changes: 39 additions & 2 deletions test/core/algorithm/for_each_pixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
//
#include <boost/gil/algorithm.hpp>
#include <boost/gil/image.hpp>
#include <boost/gil/image_view_factory.hpp>

#include <boost/core/lightweight_test.hpp>

#include "test_utility_output_stream.hpp"

namespace gil = boost::gil;

void test_lambda_expression()
Expand All @@ -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();
}

0 comments on commit da0655f

Please sign in to comment.