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

4

我正在尝试使用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个回答

4
Boost 1.68计划于2018年8月8日发布,其中包含了新的Boost.GIL IO(也称为IOv2),该功能已经长时间审核并接受。它已经可以从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;
}

1
如果您不需要read_settings,那么您就不需要将其传递给read_image()。 - chhenning
没错。我只是让它保持与@Hussem Bdr -s答案类似的示例。 - mloskot

4

在此处查看GIL的新版本:稳定版GIL

它运行良好且非常稳定。

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

你的代码看起来是正确的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接