View Full Version : reading in image from 24bit char*

19th February 2006, 19:41
I'm trying to read in an image from a character array, and I'm having some difficulties getting it into QImage and displaying it. I can write the raw data out to a file with a pgm header, and everything looks fine. So I know the data is ok.

When I write the data out to a pgm file I see that R and B are swapped, so I'd like to make use of the RGB swapping method provided by QImage.
The problem is that I can't read the data into a QImage. I've tried loadFromData, and I get a segfault. It seems to me that it wants to read my 24bit image stream as 32bits (I can't seem to create a 24bit image), and its running out of data at the end, of course.
Surely there is a way to read in 24bit RGB without me having to pad each triplet with a byte for the alpha channel "manually"?
Thanks for any advice.

void MainForm::showImage( int fd, char *imgbuf ){
QImage image;
image.create(320,240,32); // it won't let me put 24bit depth
image.loadFromData((uchar*)imgbuf, 320*240*3, 0);

Update: I've solved it for now by writing a small for loop to copy over each RGB value. Of course a built-in method would still be preferred, but it looks like it doesn't exist.

19th February 2006, 23:42
Just so you are aware, QImage supports only 1, 8, and 32 bit formats. So AFAIK, your only option is to do it manually.


20th February 2006, 01:09
Yes, I'm aware QImage only supports those types. I just thought that since there is a method to read in 24bit images from files, there might be a similar one for reading directly from data arrays. In any case, the manual method seems to be working well.

I need to display multiple image streams (that may be started or stopped at any time). Would it be a bad idea to write these pixmaps onto icons in an iconview?