c++boostgilcolor-conversion

BOOST::GIL color_converted_view doesn't compile


Here is the troublesome code

#include <boost/gil.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/gil/extension/io/png.hpp>

namespace gil = boost::gil;

int main() {
      gil::rgb8_image_t input_image;
      gil::read_image("box.jpg",input_image,gil::jpeg_tag{});
      gil::color_converted_view<gil::rgb8_view_t,gil::gray8_pixel_t>::type input_image_view=
                                   gil::color_converted_view<gil::gray8_pixel_t>(gil::view(input_image));
}

So, I am trying to convert an rgb image to grayscale. However this code gives the following error

c++ -I /data/home/sayan/Boost_Library/boost_1_75_0 Fast_testing.cpp -L./ -ljpeg -lpng -ltiff                                                                           
In file included from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/planar_pixel_iterator.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/rgb.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/typedefs.hpp:16,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/channel_algorithm.hpp:13,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/color_convert.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/image_view_factory.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/algorithm.hpp:15,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil.hpp:12,
                 from Fast_testing.cpp:9:
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp: In instantiation of ‘boost::gil::memory_based_step_iterator<I>::memory_based_step_iterator(const boost::gil::memory_based_step_iterator<I2>&) [with I2 = boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> >; Iterator = boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*]’:
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/locator.hpp:262:99:   required from ‘boost::gil::memory_based_2d_locator<I>::memory_based_2d_locator(const boost::gil::memory_based_2d_locator<SI>&) [with X = boost::gil::memory_based_step_iterator<boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> > >; StepIterator = boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>]’
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/image_view.hpp:100:89:   required from ‘boost::gil::image_view<L>::image_view(const View&) [with View = boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> > > > >; Loc = boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >]’
Fast_testing.cpp:137:113:   required from here
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:155:67: error: no matching function for call to ‘boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >::step_iterator_adaptor(const x_iterator&, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>)’
         : parent_t(it.base(), memunit_step_fn<Iterator>(it.step())) {}
                                                                   ^
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:50:5: note: candidate: ‘boost::gil::detail::step_iterator_adaptor<Derived, Iterator, SFn>::step_iterator_adaptor(const Iterator&, SFn) [with Derived = boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>; Iterator = boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*; SFn = boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>]’
     step_iterator_adaptor(const Iterator& it, SFn step_fn=SFn()) : parent_t(it), _step_fn(step_fn) {}
     ^~~~~~~~~~~~~~~~~~~~~
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:50:5: note:   no known conversion for argument 1 from ‘const x_iterator’ {aka ‘const boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> >’} to ‘boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >* const&’
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:49:5: note: candidate: ‘boost::gil::detail::step_iterator_adaptor<Derived, Iterator, SFn>::step_iterator_adaptor() [with Derived = boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>; Iterator = boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*; SFn = boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>]’
     step_iterator_adaptor() {}
     ^~~~~~~~~~~~~~~~~~~~~
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:49:5: note:   candidate expects 0 arguments, 2 provided
In file included from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/planar_pixel_iterator.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/rgb.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/typedefs.hpp:16,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/channel_algorithm.hpp:13,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/color_convert.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/image_view_factory.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/algorithm.hpp:15,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil.hpp:12,
                 from Fast_testing.cpp:9:
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note: candidate: ‘constexpr boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >::step_iterator_adaptor(const boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >&)’
 class step_iterator_adaptor : public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>
       ^~~~~~~~~~~~~~~~~~~~~
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note:   candidate expects 1 argument, 2 provided
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note: candidate: ‘constexpr boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >::step_iterator_adaptor(boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >&&)’
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note:   candidate expects 1 argument, 2 provided

But when I am specifying the type of input_image_view as auto, no such error crops up and the program is properly executed. Also, in such a case, I obtain the correct output. So, why is this error cropping up? I googled the error and came across this link https://sourceforge.net/p/adobe-source/discussion/648138/thread/b4979028/. I attempted the solution out there (not the copy and convert pixel one) but still the same error keep coming.


Solution

  • My compiler (GCC10) straight up informs me that your type id is wrong:

    using V = gil::color_converted_view<gil::rgb8_view_t, gil::gray8_pixel_t>::type;
    

    Gives

    test.cpp|15 col 20| error: function template-id ‘color_converted_view<boost::gil::rgb8_view_t, boost::gil::gray8_pixel_t>’ in nested-name-specifier
    ||    15 |     using V = gil::color_converted_view<gil::rgb8_view_t, gil::gray8_pixel_t>::type;
    ||       |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    So, there's your problem. You could spell out the effective result type:

    using boost::mp11::mp_list;
    using std::integral_constant;
    boost::gil::image_view<gil::memory_based_2d_locator<
        gil::memory_based_step_iterator<gil::dereference_iterator_adaptor<
            gil::pixel<
                unsigned char,
                gil::layout<mp_list<gil::red_t, gil::green_t, gil::blue_t>,
                            mp_list<integral_constant<int, 0>,
                                    integral_constant<int, 1>,
                                    integral_constant<int, 2>>>>*,
            gil::color_convert_deref_fn<
                const gil::pixel<
                    unsigned char,
                    gil::layout<mp_list<gil::red_t, gil::green_t, gil::blue_t>,
                                mp_list<integral_constant<int, 0>,
                                        integral_constant<int, 1>,
                                        integral_constant<int, 2>>>>&,
                gil::pixel<unsigned char,
                           gil::layout<mp_list<gil::gray_color_t>,
                                       mp_list<integral_constant<int, 0>>>>,
                gil::default_color_converter>>>>>
    

    WHich does work, but is unwieldy for obvious reasons. So, checking the function docs/implementation tells me that you wanted to say:

    gil::color_converted_view_type<
        std::remove_reference_t<decltype(gil::view(input_image))>,
        gil::gray8_pixel_t>::type input_image_view =
        gil::color_converted_view<gil::gray8_pixel_t>(gil::view(input_image));
    

    Or, if indeed you know the view to be rgb8_view_t, just that:

    gil::color_converted_view_type<gil::rgb8_view_t, gil::gray8_pixel_t>::type
        input_image_view = gil::color_converted_view<gil::gray8_pixel_t>(
            gil::view(input_image));
    

    TL;DR

    You typoed color_converted_view instead of color_converted_view_type.