使用 Boost.GIL 将图像转换为“原始”字节

Fle*_*exo 4 c++ boost image boost-gil

目标

我正在尝试转向 Boost GIL 以替换我已经实现的一些类似的功能,这些功能即将结束其可维护的生命周期。

我有使用uint8_t*. 我无法改变这一点,因为相同的接口用于从不同的地方(例如 OpenGL 缓冲区)公开图像,并且已经有很多代码。

因此,我试图以小步骤使用 GIL,首先读取文件并将像素逐字节复制到std::vector<uint8_t>可用于管理存储的 a 中,但仍然uint8_t*通过使用&vector[0].

这可以透明地放在现有接口后面,直到重构有意义为止。

我试过的

我认为这应该是使用copy_pixels()两个适当构造的视图的简单案例。

我整理了一个最小的、完整的示例,它说明了我通过查看文档和尝试实现的目标的总和:

#include <boost/gil/rgb.hpp>
#include <boost/gil/extension/io/png_dynamic_io.hpp>
#include <stdint.h>
#include <vector>

int main() {
  std::vector<uint8_t> storage;
  {
    using namespace boost::gil;
    rgb8_image_t img;
    png_read_image("test.png", img);

    // what should replace 3 here to be more "generic"?
    storage.resize(img.width()*img.height()*3);

    // doesn't work, the type of the images aren't compatible.
    copy_pixels(const_view(img), 
                interleaved_view(img.width(), img.height(), &storage[0], 3*img.width()));
  }
}
Run Code Online (Sandbox Code Playgroud)

我被困的地方

这不编译:

error: cannot convert ‘const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >’ to ‘unsigned char’ in assignment
Run Code Online (Sandbox Code Playgroud)

这是不言自明的 - RGB 像素无法自动转换为单个像素unsigned char。我想我会尝试使用copy_and_convert_pixels()来解决这个问题,但是我看不到unsigned char这些转换的 3:1 问题(即,源图像中的每个像素在输出中都有 3秒)问题。转换似乎更多地针对色彩空间转换(例如 RGB->HSV)或包装更改。

Cub*_*bbi 5

我只想分别推回 rgb8_pixel_t 的每种颜色:

struct PixelInserter{
        std::vector<uint8_t>* storage;
        PixelInserter(std::vector<uint8_t>* s) : storage(s) {}
        void operator()(boost::gil::rgb8_pixel_t p) const {
                storage->push_back(boost::gil::at_c<0>(p));
                storage->push_back(boost::gil::at_c<1>(p));
                storage->push_back(boost::gil::at_c<2>(p));
        }
};

int main() {
  std::vector<uint8_t> storage;
  {
    using namespace boost::gil;
    rgb8_image_t img;
    png_read_image("test.png", img);
    storage.reserve(img.width() * img.height() * num_channels<rgb8_image_t>());
    for_each_pixel(const_view(img), PixelInserter(&storage));
  }
...
}
Run Code Online (Sandbox Code Playgroud)

...但我也不是 GIL 方面的专家。