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.
@ QImage alpha("C:/images/alpha.bmp");//8 bit bmp
// This detects and shows 3, which is QImage::Format_Indexed8. Fine so far. qDebug()<<"alpha image format " <<alpha.format(); QColor cola = QColor::fromRgba(alpha.pixel(j,y)); img2.setPixel(j,y,qRgba(red,gren,blue,cola.alpha()));@
Any suggestions on how to retrieve the 8 bit info from the bitmap?
Thanks,
-
[quote author="karthiksrini" date="1294747392"]
@
// This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
qDebug()<<"alpha image format " <<alpha.format();
@
[/quote]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).
-
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.@QImage alpha("C:/images/alpha.bmp");//8 bit bmp
// This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
qDebug()<<"alpha image format " <<alpha.format();QVector<QRgb> vect = alpha.colorTable();
QColor cola = QColor::fromRgba(alpha.pixel(j,y));
img2.setPixel(j,y,qRgba(red,gren,blue,vect.at(alpha.pixel(j,y) & 0xff)));@ -
Only repeating a warning from the API docs on "QImage::pixel() ":http://doc.qt.nokia.com/4.7-snapshot/qimage.html#pixel:
bq. Warning: This function is expensive when used for massive pixel manipulations.
It may slow down your application if you work on big images.
-
[quote author="Volker" date="1294922411"]Only repeating a warning from the API docs on "QImage::pixel() ":http://doc.qt.nokia.com/4.7-snapshot/qimage.html#pixel:
bq. Warning: This function is expensive when used for massive pixel manipulations.
It may slow down your application if you work on big images.[/quote]
So what's a better alternative? QIMage::scanLine? Or reading from bits?
-
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":http://www.graphicsmagick.org/ for the pixel manipulation. It also has a C++ interface.