简体   繁体   English

从RGBQUAD指针创建Magick ++图像

[英]Create a Magick++ Image from an RGBQUAD pointer

I need to convert an RGBQUAD pointer to a Magic::Image . 我需要将RGBQUAD指针转换为Magic::Image I tried with the code below. 我尝试了下面的代码。 However, it crashes with Magick::ErrorCorruptImage 但是,它与Magick::ErrorCorruptImage崩溃

Blob blob = Blob(pBuffer, width*height*sizeof(RGBQUAD));
Image img;
img.size(Geometry(width, height));
img.magick("RGBA");
img.read(blob);

So how can I convert the RGBQUAD array to a Magick::Image? 那么如何将RGBQUAD数组转换为Magick :: Image?

I believe that the error is caused by "RGBA" format defaulting to Quantum size. 我认为该错误是由默认为Quantum大小的“ RGBA”格式引起的。 Usually 16Q . 通常是16Q

Just set the Magick::Image.depth to 8Q as RGBQUAD should be BYTE data types. 只需将Magick::Image.depth设置为8Q因为RGBQUAD应该是BYTE数据类型。

Blob blob = Blob(pBuffer, width*height*sizeof(RGBQUAD));
Image img;
img.size(Geometry(width, height));
img.depth(8);
img.magick("RGBA");
img.read(blob);

... But ... In RGBQUAD the last byte is reserved, and not an alpha channel. ... 但是 ...在RGBQUAD ,最后一个字节被保留,而不是alpha通道。 Quote from docs... 引用文档...

rgbReserved 保留

This member is reserved and must be zero. 该成员已保留,并且必须为零。

This would make any blob you pass to ImageMagick transparent. 这会使传递给ImageMagick的任何斑点透明。

I would suggest coping the data to a new buffer, and ensuring that the last byte is fully opaque. 我建议将数据处理到新缓冲区,并确保最后一个字节完全不透明。

For example... 例如...

// Note: I'm using data type `uint8_t' over `BYTE' for no particular reason.
size_t rgbquad_size = sizeof(RGBQUAD);
size_t total_bytes = width * height * rgbquad_size;
uint8_t * pCopyBuffer = new uint8_t[total_bytes];
for (size_t cursor = 0; cursor < total_bytes; ++cursor) {
    if (cursor % rgbquad_size < rgbquad_size - 1) {
        pCopyBuffer[cursor] = pBuffer[cursor];
    } else {
        pCopyBuffer[cursor] = 0xFF;
    }
}
Blob blob = Blob(pCopyBuffer, total_bytes);

Edit from comments 根据评论编辑

If pBuffer is defined as RGBQUAD * , than it would be easier to iterate over them, and assign the alpha channel value. 如果将pBuffer定义为RGBQUAD * ,则对它们进行迭代并分配alpha通道值会更容易。

for (size_t cursor = 0; cursor < width*height; ++cursor)
    pBuffer[cursor].rgbReserved = 0xFF; // Set Alpha

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

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