Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for_each_pixel for non 1d iterable views #621

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/boost/gil/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,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();
}