View Full Version : Creating a QImage from uchar* data

29th January 2007, 17:07
I have a pointer to the start of a Format_Indexed8 bmp image and need to store it into a QImage. I have not been able to successfully store the image. I was hoping there would be a way to pass the start of the data and the Format_Indexed8, so that the function would locate where the Color Table is. Is this possible or do I need to pass the exact location of where the image pixel data is stored and where the color table is located?

Any help would be appreciated.

I was hoping there would be something like this:
unsigned int size = /* Size of header + Size of Color Table + Size of Pixel Data */
QImage tmp(dataPointer->GetNumCols(), dataPointer->GetNumRows(), QImage::Format_Indexed8);
tmp.loadFromData((const uchar*)dataPointer->GetImageData(), size);

Any help is appreciated...


29th January 2007, 17:41

Are you loading an image from disk? If you are loading an image from a file you can load the image with the operator
"bool load ( const QString & fileName, const char * format = 0 )".

If you have the image into memory and want to put it into a QImage you have to create a QImage, create the palette for this image and finally load the data using the memory pointer with the operator
"bool loadFromData ( const uchar * data, int len, const char * format = 0 )".

I think that you can't let it know where the palette is. If the image is a Black and White image you have to create a palette with "Red = Green = Blue" and 256 colors.

29th January 2007, 18:50
I am loading the image from memory...and what is 'data' supposed to point to? Should it point to the beginning of the files data or to where the actual pixel data is? also what is 'len'? and format supposed to be sent to?

"bool loadFromData ( const uchar * data, int len, const char * format = 0 )".


29th January 2007, 19:14
"*data" is a pointer to the image in memory. It has to be the pointer to the first pixel.
"len" is the lenght of the image in bytes, so it will be width*height if it is a Black/White image or widht*height*3 if it is a RGB image.

Have you read the documentation? It is explained there.

The image is in memory, so you will have an image structure or an image class? Or you have the data of the image into an array? Could you post wich image structure are you using please.

29th January 2007, 19:44
Below is the structures which defines the image. I also have an image class which returns the pixel data of the image. The issue is that I need to somehow set the ColorMap, which "bool loadFromData ( const uchar * data, int len, const char * format = 0 )" will not do if I'm only passing the pixel data. How do I create the QVector of QRgb values to pass to SetColorMap and when am I supposed to call that function? Am I supposed to call SetColorMap after setting the width and height of the image, but before setting the pixel data...or does the order not matter?

Another note: Because this bmp uses a color map it uses one byte per pixel.

// BMP File header
typedef struct
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;

// BMP file info
typedef struct
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;

// Colormap entry
typedef struct
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;

// Bitmap Info
typedef struct
RGBQUAD bmiColors[256];

29th January 2007, 21:31
OK, so "bmiColors" is an array that define the palette. You can do something like this:

for (int i=0; i<255; i++)
image.setColor(i, qRgb(bmiColors.rgbRed,bmiColors.rgbGreen,bmiColors .rgbBlue));
QVector<QRgb> m_qPalette; //If you want to save the palette and use it another time
image.loadFromData((const uchar*)dataPointer->GetImageData());

It will take the color "i" from the "bmiColors" array and put them into the color table of the image, then load the image into the QImage.

You can set the color table before or after of setting the pixel data. It will be used when the image will be used.

8th February 2007, 16:21
Thanks, I got something similar to what you explained working.