boostorg / gil

Boost.GIL - Generic Image Library | Requires C++14 since Boost 1.80
https://boostorg.github.io/gil
Boost Software License 1.0
179 stars 163 forks source link

for_each_pixel not work for any_image #579

Closed fsmoke closed 2 years ago

fsmoke commented 3 years ago

I suggest solution with boost::variant2::visit, see code below(code is approximated, not tested and etc, writed right here - it's just idea) you can write specialization for for_each_pixel...i think

template <typename View, typename F,/*may be add sfinae here*/>
F for_each_pixel(View const & view, F fun)
{
    return boost::variant2::visit([&fun](auto && v)
        {
            return for_each_pixel(v, fun);
        }, view);
}

example of usage

using tag_t = gil::jpeg_tag;
using res_image = gil::any_image<
    gil::gray8_image_t,
    gil::rgb8_image_t,
    gil::bgr8_image_t,
    gil::rgba8_image_t,
    gil::abgr8_image_t,
    gil::argb8_image_t>;

res_image img;
gil::read_image("blabla.jpg", img, tag_t());

gil::for_each_pixel(gil::const_view(img), [](auto & pixel))
{
      using TT = std::decay_t<decltype(pixel)>;
      //do here what you want with pixel
});
sdebionne commented 3 years ago

Thanks for the suggestion. No need for SFINAE here, just overload the algorithm:

template <typename ...Types, typename F>
F for_each_pixel(any_image_view<Types...> const& src, F fun)
{
    return apply_operation(src, [&fun](auto && v) {
        return for_each_pixel(v, fun);
    });
}