使用Boost.GIL将图像转换成" RAW"字节 [英] Using Boost.GIL to convert an image into "raw" bytes

查看:257
本文介绍了使用Boost.GIL将图像转换成" RAW"字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向升压GIL移动来代替某些类似的功能,我实现了正在达到其维护生命的尽头。

I'm trying to move towards Boost GIL to replace some similar functionality I've implemented that is reaching the end of its maintainable life.

我有现有的code,与24 BPP,8位使用RGB图像 uint8_t有* 的作品。我不能改变,作为相同的接口用于公开来自不同地方的图​​像(例如OpenGL的缓冲区),已经有相当多的code的。

I have existing code that works with 24 BPP, 8bit RGB images using uint8_t*. I can't change that as the same interface is used to expose images from different places (e.g. OpenGL buffers) and there's already quite a lot of code.

所以我想在小步骤,使用GIL,通过读取一个文件,然后按字节复制像素字节到的std ::矢量&lt开始; uint8_t有> 这我可以用它来管理存储,但还是通过获得 uint8_t有* 和矢量[0]

Therefore I'm trying to use GIL in small steps, starting by reading a file and copying the pixels byte by byte into a std::vector<uint8_t> which I can use to manage the storage, but still get a uint8_t* by using &vector[0].

这可以透明地将现有的接口后面,直到它在一个点,重构有意义滴入

This can be dropped in transparently behind the existing interfaces until it's at a point where refactoring makes sense.

我想这应该是使用一个简单的例子 copy_pixels() 有两个构造适当的意见。

I thought this should be a simple case of using copy_pixels() with two appropriately constructed views.

我已经把那说明了什么,我管理的总和通过查看过的文件,并试图东西出来,实现了最小的,完整的例子:

I've put together a minimal, complete example that illustrates the sum of what I've manage to achieve by looking over the documents and trying things out:

#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 char型取值在输出为源图像中的每个像素)与这些转换的问题。转化似乎更针对色彩空间转换(例如RGB-> HSV)或填料的变化。

Which is fairly self-explanatory - an RGB pixel can't be converted to a single unsigned char automatically. I thought I'd try using copy_and_convert_pixels() to fix this, but I can't see a way around the 3:1 (i.e. I have 3 unsigned chars in the output for every pixel in the source image) problem with these conversions. Conversion seems to be more aimed at colourspace conversions (e.g. RGB->HSV) or packing changes.

推荐答案

我只想对的push_back的rgb8_pixel_t每种颜色单独

I would just push_back each color of the rgb8_pixel_t individually:

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));
  }
...
}

...但我不是GIL的专家无论是。

...but I'm not an expert on GIL either.

这篇关于使用Boost.GIL将图像转换成&QUOT; RAW&QUOT;字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆