Search code examples
boost-gil

How do I use boost::gil to load any typical png file


I have configured my environment so that I can load a suitably crafted .png file into an image defined like this:

        boost::gil::rgb8_image_t input;

but how do I load a png file of any typical type (such as is generated by the GIMP, or MS Paint). I think it needs boost::gil::any_image but I don't know the types that I need to configure it with.

I've tried:

        typedef boost::mpl::vector<
            boost::mpl::rgba8_planar_image_t,
            boost::mpl::rgba8_image_t,
            boost::mpl::rgb8_planar_image_t,
            boost::mpl::rgb8_image_t,
            boost::mpl::gray8_image_t
        > my_img_types;
        boost::mpl::any_image<my_img_types> input;
        boost::gil::png_read_image(ipath, input);

but that doesn't load a file created by MS Paint or the GIMP.


Solution

  • Have you tried using the family of functions png_read_and_convert_*

    For example:

    boost::gil::rgb8_image_t input;
    boost::gil::png_read_and_convert_image(ipath, input);
    

    You will lose the original type of the image this way, but if you want a fixed type for your code to manipulate this might be a good way to go.