Skip to content

Commit

Permalink
feat: Added for_each_pixel overload for any_image (#648)
Browse files Browse the repository at this point in the history
Take functor by value instead of reference.
Test cases to use the same fixture as the other any_image tests.
Fixes #579
  • Loading branch information
marco-langer committed Apr 30, 2022
1 parent 3289fe0 commit c063d1c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
29 changes: 29 additions & 0 deletions include/boost/gil/extension/dynamic_image/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,35 @@ void fill_pixels(any_image_view<Types...> const& view, Value const& val)
apply_operation(view, detail::fill_pixels_fn<Value>(val));
}

namespace detail {

template <typename F>
struct for_each_pixel_fn
{
for_each_pixel_fn(F&& fun) : fun_(std::move(fun)) {}

template <typename View>
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 <typename ...Types, typename F>
auto for_each_pixel(any_image_view<Types...> const& view, F fun) -> F
{
return variant2::visit(detail::for_each_pixel_fn<F>(std::move(fun)), view);
}

}} // namespace boost::gil

#endif
2 changes: 2 additions & 0 deletions test/extension/dynamic_image/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
11 changes: 11 additions & 0 deletions test/extension/dynamic_image/algorithm/Jamfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Boost.GIL (Generic Image Library) - tests
#
# Copyright (c) 2022 Marco Langer <langer.m86 at gmail dot com>
#
# 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 ;
52 changes: 52 additions & 0 deletions test/extension/dynamic_image/algorithm/for_each_pixel.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/gil/extension/dynamic_image/any_image.hpp>
#include <boost/gil/extension/dynamic_image/algorithm.hpp>

#include <boost/core/lightweight_test.hpp>

#include "../test_fixture.hpp"
#include "core/image/test_fixture.hpp"

namespace gil = boost::gil;
namespace fixture = boost::gil::test::fixture;

struct accumulator
{
template <typename Pixel>
void operator()(Pixel const& p)
{
sum += gil::at_c<0>(p);
}

int sum{};
};

struct test_for_each_pixel
{
template <typename Image>
void operator()(Image const&)
{
using image_t = Image;
fixture::dynamic_image image(fixture::create_image<image_t>(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<fixture::image_types>(test_for_each_pixel{});
}
};

int main()
{
test_for_each_pixel::run();

return ::boost::report_errors();
}

0 comments on commit c063d1c

Please sign in to comment.