PDA

View Full Version : QImage and 8 bit unsigner char **



fyanardi
5th June 2006, 17:27
Hi all,

I need to display a tiff image on a QWidget, the image is 8 bit-depth, and I need to represent it using 2 dimensional unsigned char so that it can be processed later. I've successfully read the tiff image into the char, but I can't draw it on the QWidget. What I've tried is:



unsigned char pic[512][512] = TiffIO::tiffReadPic("test.tif", pic, &width, &height);
QImage img(width, height,QImage::Format_Indexed8);

for(int i = 0; i < 255; i++)
{
myColor = qRgb(i, i, i);
img.setColor(i, myColor);
}

for(int i = 0; i<width; i++)
for(int j = 0; j<height; ++j)
img.setPixel(i, j, pic[i][j]);


But it doesn't give anything, the QWidget remains blank and no error message at all :(

Any suggestion?

Thanks...

jacek
5th June 2006, 17:31
How do you draw that image on QWidget? How does this image look like if you save it to a file using QImage::save()?

fyanardi
5th June 2006, 18:10
Thanks for your very fast reply,

I'm sorry, I got error message with that code from QImage that the pixel I try to set is always out of range (QImage::setPixel: index=15 out of range). Then I tried to print img.numColors(), and I found that it always returns 0, even though I've set the format to 8 bit indexed (QImage::Format_Indexed8). However if I set the numColors manually via img.setNumColors(256), that piece of code works, the QWidget and also img.save() produce correct images.

I still don't understand why 8 bit indexed can have 0 numColors() ? From the doc, I read QImage will not return 0 from image that uses color table. Or maybe I miss something?

Thanks btw ;)

wysota
5th June 2006, 19:12
If you choose the format of QImage, you only set a way of storing pixel data in the object. You have to set the number of colours manually using setNumColors() then probably to set the proper size of the CLT.

fyanardi
6th June 2006, 14:13
If you choose the format of QImage, you only set a way of storing pixel data in the object. You have to set the number of colours manually using setNumColors() then probably to set the proper size of the CLT.

Oh, I didn't know about that before, thanks for your explanation :D