简体   繁体   English

将BLOB读入图像时出现Magick ++错误

[英]Magick++ Error when reading BLOB into Image

I'm trying to export an image from raw pixel data into a RGBA PNG using the Magick++ library. 我正在尝试使用Magick ++库将图像从原始像素数据导出到RGBA PNG。

However, I'm getting a strange error when I'm attempting to run it: 但是,当我尝试运行它时遇到一个奇怪的错误:

terminate called after throwing an instance of 'Magick::ErrorCorruptImage'
  what():  test: unexpected end-of-file `': No such file or directory @ error/rgb.c/ReadRGBImage/229
Aborted

This is the relevant code part (I omitted filling the pixel vector, but that doesn't change anything): 这是相关的代码部分(我省略了填充像素矢量,但没有任何改变):

#include <iostream>
#include <vector>
#include <ImageMagick/Magick++.h>

using namespace std;

int main(int argc, char *argv[]) {
    Magick::InitializeMagick(*argv);
    int rres, ires;
    cin >> rres >> ires;
    //RGBA
    //rres: horiz. resolution, ires: vert. resolution
    vector<unsigned char> image(rres * ires * 4);
    Magick::Blob blob(&image[0], rres*ires*4);
    Magick::Image img;
    img.size(to_string(rres) + "x" + to_string(ires));
    img.magick("RGBA");
    img.read(blob);
    img.write("out.png");
}

Compilation with: 编译:

g++ --std=c++11 -O0 -g3 -ggdb3 -D_GLIBCXX_DEBUG -Wall test.cpp -o test `Magick++-config --cppflags --cxxflags --ldflags --libs`

Your example works if you are using the Q8 version of ImageMagick. 如果您使用的是ImageMagick的Q8版本,则您的示例有效。 But it seems that you are using the Q16 version of ImageMagick. 但是,似乎您正在使用ImageMagick的Q16版本。 That latter uses 16 bits per pixel channel. 后者每个像素通道使用16位。 You are using a vector<unsigned char> which is only 8 bits. 您正在使用只有8位的vector <unsigned char> I would advise you to switch to a vector<unsigned short> or use an vector<unsigned char> that is twice the current size. 我建议您切换到vector <unsigned short>或使用vector <unsigned char> ,它是当前大小的两倍。 You could also switch to the Q8 version of ImageMagick if you don't need 16 bits per pixel channel. 如果不需要每个像素通道16位,也可以切换到ImageMagick的Q8版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM