PDA

View Full Version : Loading a raw image into QImage



Luc4
13th May 2010, 15:06
Hi! I'm trying to read data pointed by unsigned char* in RG565 in QImage. I found something on the forums, and I wrote this... but the result is not the image...


QImage* rawImage = new QImage(QSize(_width, _height), QImage::Format_RGB16);
for (int i = 0; i < 256; i++) rawImage->setColor(i, qRgb(i, i, i));
for (int i = 0; i < _height; i++) {
unsigned char* image = rawImage->scanLine(i);
for (int j = 0; j < _width*2; j++) image[j] = data[i*_width*2 + j];
}
rawImage->save("path");

Any idea what could be wrong?
Thanks!

borisbn
13th May 2010, 18:26
Did you try QImage::fromData ?

Luc4
13th May 2010, 19:51
No, because fromData requires a format for the image. Raw is not a supported format as far as I know.

borisbn
13th May 2010, 20:04
Sorry for fromData. It is not, what you need. Did you try constructor QImage::QImage ( const uchar * data, int width, int height, Format format ) (http://doc.qt.nokia.com/4.6/qimage.html#QImage-5) like this:


QImage rawImage( data, _width, _height, QImage::Format_RGB16 );

if it wouldn't work, try to change your code like this:


unsigned short* usData = (unsigned short*)data;
for (int i = 0; i < _height; i++)
{
unsigned short* image = (unsigned short*)rawImage->scanLine(i);
for (int j = 0; j < _width; j++)
image[j] = usData[i*_width + j];
}

Luc4
14th May 2010, 08:49
I get always the same result... I'm beginning to suspect the data has something wrong...
Thanks for your help!

wysota
14th May 2010, 17:14
Do you get complete garbage or do you see any shapes in the image or how does it look like? I don't know anything about RGB565 - does it store colours in a lookup table and "pixel data" represents indexes in the lookup table or do the "pixel data" contain actual colours? Because your first piece of code tries to setup the lookup table but then I don't see what the 5-6-5 combination would represent. I would rather expect it to contain actual colour data.

Luc4
16th May 2010, 15:20
I found some information about how to do that in some forums and so I tried many ways to make it work. In some they created that table, but I couldn't understand why. The result is a uniformly green image, even with this instruction suggested :):


QImage rawImage( data, _width, _height, QImage::Format_RGB16 );

As far as I know RGB565 stores 16 bits pixels one after the other with 5 bits for red, 6 bits for green and 5 bits for blue. I thought I could just copy all the data byte by byte in the QImage, but it seems the result is always the same...