简体   繁体   English

从 BITMAPINFO 和 uchar* 数据创建 QImage

[英]Create a QImage from a BITMAPINFO and an uchar* data

I am trying to load an image that is given to me as a BITMAPINFO* and a uchar array.我正在尝试加载作为BITMAPINFO*uchar数组提供给我的图像。 The documentation states that it is a standard Microsoft device independent bitmap (DIB) with 8-bit pixels and a 256-entry color table .文档声明它是标准的 Microsoft 设备独立 bitmap (DIB),具有 8 位像素和 256 项颜色表

I am curently able to open this image through:我目前可以通过以下方式打开此图像:

BITMAPINFO* bmih = givenBITMAPINFO;
uchar* data = givenData;

QImage img = QImage(data, bmih->biWidth, bmih->biHeight, QImage::Format_Grayscale8);

But I have two problems with that:但我有两个问题:

  1. the image is in QImage::Format_Grayscale8 when the documentation states an 8-bit pixels and a 256-entry color table;当文档说明 8 位像素和 256 项颜色表时,图像位于QImage::Format_Grayscale8中;

  2. the image is upside down and mirrored.图像颠倒并镜像。 This come from the way the bitmap data is stored in Win32.这来自 bitmap 数据在 Win32 中的存储方式。

Anyone knows how I can load properly this image?任何人都知道如何正确加载此图像?

By casting the provided header to a BITMAPINFO instead of a BITMAPINFOHEADER I have access to the color table and then apply a trasformation to get a streight image:通过将提供的 header 转换为BITMAPINFO而不是BITMAPINFOHEADER我可以访问颜色表,然后应用转换以获得立体图像:

BITMAPINFO* bmi = givenHeader;
uchar* data = givenData;

QImage img = QImage(data, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, QImage::Format_Indexed8);

img.setColorCount(256);
for (int i=0; i<256; ++i){
    RGBQUAD* rgbBmi = bmi->bmiColors;
    img.setColor(i, qRgb(rgbBmi[i].rgbRed, rgbBmi[i].rgbGreen, rgbBmi[i].rgbBlue))
}

img = img.mirrored(false, true);

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

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