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

feat: Added for_each_pixel overload for any_image #648

Merged
merged 3 commits into from
Apr 30, 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
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();
}