【发布时间】:2015-01-26 09:54:02
【问题描述】:
目标
我正在尝试转向 Boost GIL,以替换我已实现的一些类似功能,这些功能已达到其可维护生命周期的终点。
我现有的代码可以使用uint8_t* 处理 24 BPP、8 位 RGB 图像。我无法改变这一点,因为相同的接口用于从不同的地方(例如 OpenGL 缓冲区)公开图像,并且已经有相当多的代码。
因此,我尝试以小步骤使用 GIL,首先读取文件并将像素逐字节复制到 std::vector<uint8_t> 中,我可以用它来管理存储,但仍然通过使用获得 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()));
}
}
我被困在哪里
这不会编译:
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
这是不言自明的 - RGB 像素无法自动转换为单个 unsigned char。我想我会尝试使用copy_and_convert_pixels() 来解决这个问题,但是我看不到 3:1 的解决方法(即,对于源图像中的每个像素,我在输出中有 3 个unsigned chars)问题转换。转换似乎更针对色彩空间转换(例如 RGB->HSV)或包装更改。
【问题讨论】:
-
只是想知道,您最终是否设法让这段代码正常工作?我也刚刚开始尝试使用 GIL,通过 GIL 从文件中获取数据到 OpenGL 是一个挑战。你有我可以看看的工作示例吗?
-
@thecoshman - 查看我正在处理的代码,接受的答案与我使用的非常接近。我从我的代码库中提取了一个 MWE,并用它回答了这个问题。