Search code examples
c++boostboost-gil

How to convert a gray8_view_t to a rgb8_view_t by using boost::gil and create a rgb8_image_t object from it?


Since boost::gil does not support gray8_view_t writing for the BMP format, I want to convert gray8_view_t to rgb8_view_t. Here is what I've tried so far.

auto rgb_view = boost::gil::planar_rgb_view(width, height, pixels, pixels, pixels, width);

pixels contains the raw pixels from the gray8_view_t object, so I let r=g=b=pixels. But boost::gil::write_view(ofstream, rgb_view, boost::gil::bmp_tag()) gives me an empty image. Any idea?

Update: By using sehe's example code http://coliru.stacked-crooked.com/a/daa0735f774b727f, I was able to get the color conversation to compile with color_converted_view<gil::rgb8_view_t>. But it does not compile when I use boost::gil::write_view to create an image file from the return value of color_converted_view<gil::rgb8_view_t>. My guess is I will have to create an actual rgb8_image_t object from the return value. How can I convert the return value of color_converted_view<gil::rgb8_view_t> to an actual rgb8_image_t object? Thank you!

#include <boost/gil.hpp>
#include <fstream>
namespace gil = boost::gil;

int main()
{
        std::ifstream in("gray8_image_t_sample.jpg", std::ios::binary);
        gil::gray8_image_t img;
        gil::read_image(in, img, gil::jpeg_tag());
        gil::gray8_view_t gv = gil::view(img);
        std::ofstream ofs1("test_image.png", std::ios::out | std::ios_base::binary);
        gil::write_view(ofs1, gv, gil::png_tag()); // This works

        auto rgbv = gil::color_converted_view<gil::rgb8_view_t>(gv);
        std::ofstream ofs2("test_image.bmp", std::ios::out | std::ios_base::binary);
        gil::write_view(ofs2, rgbv, gil::bmp_tag()); // this does not compile
} 

One of the error messages I'm getting

\boost\gil\color_base_algorithm.hpp(170,76): error G1A4676F8: no member named 'layout_t' in 'boost::gil::image<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::mp11::mp_list<std::integral_constant<int, 0>, std::integral_constant<int, 1>, std::integral_constant<int, 2>>>>, false, std::allocator<unsigned char>>' [clang-diagnostic-error]

Here is the gray8_image_t file I'm using


Solution

  • Okay the remaining problem was merely a misspecified template argument, color_converted_view expects a destination pixel type:

    #include <boost/gil.hpp>
    #include <boost/gil/extension/io/bmp.hpp>
    #include <boost/gil/extension/io/jpeg.hpp>
    #include <boost/gil/extension/io/png.hpp>
    #include <fstream>
    namespace gil = boost::gil;
    
    int main()
    {
        std::ifstream in("gray8_image_t_sample.jpg", std::ios::binary);
        gil::gray8_image_t img;
        gil::read_image(in, img, gil::jpeg_tag());
        gil::gray8_view_t gv = gil::view(img);
        gil::write_view("input.png", gv, gil::png_tag());
    
        auto rgbv = gil::color_converted_view<gil::rgb8_pixel_t>(gv);
        gil::write_view("output.png", rgbv, gil::png_tag());
        gil::write_view("output.bmp", rgbv, gil::bmp_tag());
    }
    

    With the resuling files: