January 11, 2011

karthiksrini karthiksrini
Lab Rat
16 posts

QImage - Retrieve data from an 8 bit bitmap image

 

Hi,

I have an 8 bit bitmap image that I need to read, retrieve the data and use that information as the alpha channel for another image. How do I go about doing that? I have tried the below code, but doesn’t seem to work.

  1.     QImage alpha("C:/images/alpha.bmp");//8 bit bmp
  2.  
  3.     // This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
  4.     qDebug()<<"alpha image format " <<alpha.format();
  5.  
  6.     QColor cola = QColor::fromRgba(alpha.pixel(j,y));
  7.     img2.setPixel(j,y,qRgba(red,gren,blue,cola.alpha()));

Any suggestions on how to retrieve the 8 bit info from the bitmap?

Thanks,

7 replies

January 11, 2011

Andre Andre
Robot Herder
6393 posts

Doesn’t QImage::constBits() or QImage::constScanLine() help you? Sound like you just need raw data, right?

January 11, 2011

Volker Volker
Ant Farmer
5428 posts

The alpha QImage is a regular image, the alpha value of any pixel is always 0xff. You have to reconstruct the value from the RGB values.

January 11, 2011

peppe peppe
Ant Farmer
1028 posts
karthiksrini wrote:
  1.     // This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
  2.     qDebug()<<"alpha image format " <<alpha.format();

Watch out, you don’t have a 8bpp image, you have a 8bpp INDEXED image, so the color of each pixel is stored in the colortable (which is indexed by the value of each pixel, and stores 32bit colors).

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

January 13, 2011

karthiksrini karthiksrini
Lab Rat
16 posts

Thanks for your replies!
I have tried the below code based on Volker’s and peppe’s replies (and based on what the documentaion says – In case of a 8-bit and monchrome images, the pixel value is only an index from the image’s color table) I seem to get what I want. If you have any comments on what I am doing, i.e if I am doing the right/wrong thing, let me know.

  1. QImage alpha("C:/images/alpha.bmp");//8 bit bmp
  2.  
  3. // This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
  4. qDebug()<<"alpha image format " <<alpha.format();
  5.  
  6. QVector<QRgb> vect = alpha.colorTable();
  7. QColor cola = QColor::fromRgba(alpha.pixel(j,y));
  8. img2.setPixel(j,y,qRgba(red,gren,blue,vect.at(alpha.pixel(j,y) & 0xff)));

January 13, 2011

Volker Volker
Ant Farmer
5428 posts

Only repeating a warning from the API docs on QImage::pixel() [doc.qt.nokia.com]:

Warning: This function is expensive when used for massive pixel manipulations.

It may slow down your application if you work on big images.

January 14, 2011

karthiksrini karthiksrini
Lab Rat
16 posts

Volker wrote:
Only repeating a warning from the API docs on QImage::pixel() [doc.qt.nokia.com]:

Warning: This function is expensive when used for massive pixel manipulations.

It may slow down your application if you work on big images.

So what’s a better alternative? QIMage::scanLine? Or reading from bits?

January 14, 2011

Volker Volker
Ant Farmer
5428 posts

scanLine() can work, but be aware that you must handle the alignment of the data and all that manually.

If you have big images/and or do this often, you might consider including an external image library like GraphicsMagick [graphicsmagick.org] for the pixel manipulation. It also has a C++ interface.

 
  ‹‹ [Solved] Qt layout direction and window title bar      How to change the colume info in QDirModel? ››

You must log in to post a reply. Not a member yet? Register here!