PDA

View Full Version : Construct QPixmap or QImage from raw data.



The Storm
28th November 2008, 14:50
Hi all,

I am writing a plugin for an application. This application have a callback interface that I inherit and when an image arrive the following method is called:



void IListener::onImage(int height, int width, int *data);


In that method I have to create a valid QPixmap or QImage from those height, weight and integer data pointer. If someone know how to do that, please share with me. :)

Regards,
The Storm

drhex
28th November 2008, 15:32
Is that "data" raw pixels with no header?

QImage has a constructor for width and height where you can supply a format argument indicating how many bits per pixel you have and how they are arranged.
Then you can copy the data into the QImage line by line using the pointer provided by
QImage::scanLine (which needs to be properly casted first).

The Storm
28th November 2008, 16:24
So basicly you mean that I have to do something like this:



QImage image( width, height, format );


But then how to copy the data ? Could you please be more specific. :) Is QImage::Format_RGB32 good format, the image have transparent parts btw.

The Storm
30th November 2008, 20:05
Anyone, please. I forgot to mentoin that the data are raw pixels.

pgorszkowski
1st December 2008, 08:57
Tha main problem is what format of data is inside in this integer data pointer. Is it rgba (0xAARRGGBB) or rgb( (0xffRRGGBB) or what. In case of rgba you can do something like this:



void onImage(int height, int width, int* data)
{
QImage image(width, height, QImage::Format_ARGB32);
for(int y(0);y < height;++y)
for(int x(0);x < width;++x)
{
int index(y*width + x);
image.setPixel(x, y, data[index]);
}
}


Here you have the link to available format of image:
http://doc.trolltech.com/4.4/qimage.html#Format-enum

The Storm
1st December 2008, 09:21
Thanks a lot. The image is PNG and the format QImage::Format_ARGB32 have done the work. My end realisation actually is a bit differend and I hope more fast:



void onImage(int height, int width, int* data)
{
QImage image = QImage( width, height, QImage::Format_ARGB32 );

for (int i = 0; i < height; ++i)
memcpy(image.scanLine(i), data + i * width, width * 4);
}


I hope that this will be useful for others that have my issue. :)