使用boost :: gil从内存中读取JPEG图像

时间:2013-04-28 04:38:00

标签: c++ boost jpeg boost-gil

我试图通过使用boost 1.53中的boost::gil来从内存中读取图像。我从互联网上的一个例子中采取了以下几行:

#include <boost/gil/gil_all.hpp>
boost::gil::rgb8_image_t img;
boost::gil::image_read_settings<jpeg_tag> readSettings;
boost::gil::read_image(mystream, img, readSettings);

除第一行外,其余行中的类型和函数在boost::gil命名空间中找不到,因此我无法测试上述行是否符合我的要求。你知道从哪里获得所需的类型和功能吗?

2 个答案:

答案 0 :(得分:3)

在此处查看新版本的gil:gil stable version

效果很好而且很稳定。

using namespace boost::gil;
image_read_settings<jpeg_tag> readSettings;
rgb8_image_t newImage;
read_image(stream, newImage, readSettings);

你的代码似乎是正确的。

答案 1 :(得分:2)

Boost 1.68(即planned for release on 8th of August, 2018)将最终交付新的Boost.GIL IO(又名IOv2)reviewed and accepted long time ago。 它可以从Boost超级项目的当前master分支中获得(有关如何使用超级项目的准则,请检查Boost.GIL CONTRIBUTING.md)。

现在,您可以使用Boost 1.68或更高版本中的GIL,以下示例显示了如何从输入流中读取图像。它不一定是基于文件的流,但是任何std::istream兼容的流都可以使用。

#include <boost/gil.hpp>
#include <boost/gil/io/io.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <fstream>
#include <iostream>

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "input jpeg file missing\n";
        return EXIT_FAILURE;
    }

    try
    {
        std::ifstream stream(argv[1], std::ios::binary);

        namespace bg = boost::gil;
        bg::image_read_settings<bg::jpeg_tag> read_settings;
        bg::rgb8_image_t image;
        bg::read_image(stream, image, read_settings);

        return EXIT_SUCCESS;
    }
    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return EXIT_FAILURE;
}