PDA

View Full Version : Display an image from a unsigned char array



Percy
1st December 2009, 19:30
Hi there!

I am trying to display an image from an unsigne char array. Tried many things like the QPixmap.loadfromdata method and the label->pixmap property. Nothing worked so far.
Does anyone have an example or something that could "inspire" me to get that?
Thanks in advance,

Percy.

scascio
1st December 2009, 21:16
I had same needs and never succeed with QImage::loadFromData since you need to write the header I presume.


Just create a QImage with the right pixel size, with the format QImage::Format_Indexed8

Then get acces buffer data in read/write with scanline, and use memset to copy from your buffer into the image buffer in a loop.

You can set color values in your color table for each different pixel with QImage::setColor . Each pixel value of your source buffer is the index and you need to convert your colors into QRgb. Empty entries will be not used.

There is another way to copy the whole buffer in one instruction with QImage::bits but since buffer inside the image is 32bits aligned, you will get an offset and unexpected result.