Create a QImage from a BITMAPINFO and an uchar* data

394 Views Asked by At

I am trying to load an image that is given to me as a BITMAPINFO* and a uchar array. The documentation states that it is a standard Microsoft device independent bitmap (DIB) with 8-bit pixels and a 256-entry color table.

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;

  2. the image is upside down and mirrored. This come from the way the bitmap data is stored in Win32.

Anyone knows how I can load properly this image?

1

There are 1 best solutions below

1
firehelm On

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:

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);