PDA

View Full Version : image alpha



murko81
26th October 2006, 11:41
hi, i'm a new qt user and i want to modify the alpha of an entire image with a slider or even a stupid button just for see if it is working, but i don't know how to manipulate this alpha channel of the image..
I have an QImage object and i have putted in it a jpg image that has an alpha channel (image.hasAlphaChannel gives me true). but how to modify the alpha values?
i tried to work onto the "image.colorTable" but it gives me nothing back..

ps: i'm using the latest qt version and windows

thank you in advance!
waitin' for your replies!
Marco

jpn
26th October 2006, 15:48
QImage::setAlphaChannel(const QImage & alphaChannel) ?


Sets the alpha channel of this image to the given alphaChannel.

If alphaChannel is an 8 bit grayscale image, the intensity values are written into this buffer directly. Otherwise, alphaChannel is converted to 32 bit and the intensity of the RGB pixel values is used.

Note that the image will be converted to the Format_ARGB32_Premultiplied format if the function succeeds.

wysota
26th October 2006, 18:29
I have an QImage object and i have putted in it a jpg image that has an alpha channel

Hmm... jpeg images have alpha channels? Are you sure? Or did you add the channel after loading the image?

murko81
27th October 2006, 17:46
Hmm... jpeg images have alpha channels? Are you sure? Or did you add the channel after loading the image?

yes you're right it was a png file, and i did this code:


QImage image = imageLabel->pixmap()->toImage();
for(int i=0;i<image.height();i++){
for(int j=0;j<image.width();j++){
QColor color;
color=image.pixel(i,j);
color.setAlpha(alpha);
image.setPixel(i,j,color.rgba());
}
}

but it's not working well cause the resulting images have different colours and not different alpha values.. ?!
can anyone tell me why?

wysota
27th October 2006, 17:54
How about:

QImage yourimage;
QImage alphamask = yourimage;
alphamask.fill(alpha);
yourimage.setAlphaChannel(alphamask);

murko81
27th October 2006, 18:59
if i set an alpha value from zero to 255 and i fill the mask.. i'm no more able to see the image.. 4example if i set .fill(255) the mask is white and the image is totally covered by the white mask..

wysota
27th October 2006, 19:12
Ok, fill() corrupts the image. Use this instead:

#include <QApplication>
#include <QImage>
#include <QLabel>
#include <QPalette>
#include <QVBoxLayout>
#include <QPixmap>
#include <QPainter>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget wgt;
QPalette pal = wgt.palette();
pal.setColor(QPalette::Window, Qt::red);
QLabel *lab = new QLabel;
QVBoxLayout *l = new QVBoxLayout;
l->addWidget(lab);
wgt.setLayout(l);
QImage image("test.jpg");
QImage alpha = image;
QPainter painter(&alpha);
painter.fillRect(alpha.rect(), QColor(127,127,127));
painter.end();
image.setAlphaChannel(alpha);
QPixmap pix = QPixmap::fromImage(image);
lab->setPixmap(pix);
wgt.show();
return app.exec();
}

murko81
27th October 2006, 20:01
thank you allot!!
this is working really good!
another question:


if i create a png (4ex in fireworks) with an alpha (4ex 60) and then i close and re-open the image.. the program is able to recognize the fact that the image has an alpha value = 60;
is it possible to do the same thing with qt ? 'cause now when i re-open the image with the viewer i made it reads the initial alpha value as 255

jpn
27th October 2006, 20:10
if i create a png (4ex in fireworks) with an alpha (4ex 60) and then i close and re-open the image.. the program is able to recognize the fact that the image has an alpha value = 60;
is it possible to do the same thing with qt ? 'cause now when i re-open the image with the viewer i made it reads the initial alpha value as 255
The image you are showing is created dynamically at run time. You would have to save it back to a file to keep the changes, see QImage::save().

wysota
27th October 2006, 23:11
if i create a png (4ex in fireworks) with an alpha (4ex 60) and then i close and re-open the image.. the program is able to recognize the fact that the image has an alpha value = 60;
is it possible to do the same thing with qt ? 'cause now when i re-open the image with the viewer i made it reads the initial alpha value as 255

Qt handles alpha just fine. If you feed it with an image which already has an alpha channel, it will display it correctly without the 'hack' we did here.

murko81
28th October 2006, 12:52
of course i save the file before re-opening and of course qt handles fine the alpha channel, maybe i didn't wrote it good (i apologize4my english ).
The fact is:
1- I create an image with a commercial editor and i give to that image an alpha value, and i save it: myfolder/myimage.png , and then i close everything.
2- if i reopen that image with the same commercial editor, it is able to recognize the right alpha value (not only displaying the image in the proper way but also writing it correctly in between the properties of the image)
3-instead if i reopen the image with my self made editor, i am able to display the image correctly but into the properties the alpha value is recognized as the maximum (255)

so the question was: how can i read the rigth alpha value? 'cause if a commercial editor is able to do that maybe i can also:-)

thank you in advance!

wysota
28th October 2006, 14:15
3-instead if i reopen the image with my self made editor, i am able to display the image correctly but into the properties the alpha value is recognized as the maximum (255)

so the question was: how can i read the rigth alpha value?

And how do you read the alpha value now? This question is really tricky, as alpha is not a single value but a bitmap.

murko81
28th October 2006, 19:06
:-) yes it's not a single value but starting from the fact that in fireworks it shows me it like an unique value i thought that fireworks changes all the alpha values to the same value.. so reading one of them from one single pixel it was enough.. maybe it's not the right way to get it but this is how i did it and this is how i get back always a value = 255;

wysota
28th October 2006, 20:31
Attached code shows how to do it properly and attached image shows an example result. Image on the left is the actual image whether image on the right is a bitmap of the alpha channel.