PDA

View Full Version : QImage setPixel does not set alpha value



sanjayshelke
19th February 2009, 15:12
Hi,

I am trying to change the pixel value of image by using setPixel() method.
It is changing the RGB values successfully but not changing the Alpha value.
It always shows value 255.

The code is like this

QImage image = pixmap->tpImage();
int width = image.width();
int height = image.height();
for( i = 0 ; i < height; i++)
{
for( j = 0; j < width; j++ )
{
image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
}
}

Thanks in advance
~Sanjay

Lykurg
19th February 2009, 15:21
What's the image format (QImage::Format) of your image? Can it handle alpha values?

Lykurg
19th February 2009, 15:25
image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;

And maybe this should be
image.setPixel(j,i, QColor(0,0,0,0).rgba()) ;

sanjayshelke
19th February 2009, 16:16
hi,

Image format is QImage::Format_ARGB32 and i have tried with QColor(0,0,0,0).rgba() also.

But no effect.

talk2amulya
19th February 2009, 17:19
try using:

QRgb qRgba ( int r, int g, int b, int a ) instead of qcolor.rgba()

sanjayshelke
20th February 2009, 08:05
Hi,

I want to correct my problem statement.

Thing is that whenever i am trying to change pixel value using setPixel(), it actually changes the pixel values.

e.g When i do;
image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
this makes the pixel transparent.
But after making it transparent if i ask to image what is rgba value at that position,
it shows me 0,0,0,255.
Actually it should give 0,0,0,0 rgba values.

I have written the code like this:

QImage image = pixmap->tpImage();
int width = image.width();
int height = image.height();
for( i = 0 ; i < height; i++)
{
for( j = 0; j < width; j++ )
{
image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
QColor clrAtPixel(imgPixmap.pixel(ptPos));
int r,g,b,a;
clrAtPixel.getRgb(&r,&g,&b,&a);
qDebug()<<r<<" "<<g<<" "<<b<<" "<<a;
}
}

I think setPixel is working fine and it is making the pixel transparent. ( I have seen that the image becomes transparent)
But after making it transparent when i ask for rgb values it gives 0,0,0,255.
The black color has same RGBA values as above.
Now my image is transparent but still it gives 0,0,0,255 rgba values.

Can anyone tell me whjat i m doing wrong.


Regards,
~Sanjay

aamer4yu
20th February 2009, 08:09
And maybe this should be
image.setPixel(j,i, QColor(0,0,0,0).rgba()) ;

Had you paid attention to this ? You are setting only rgb data, alpha is omitted hence !

Lykurg
20th February 2009, 11:15
image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
QColor clrAtPixel(imgPixmap.pixel(ptPos));


You change the pixel at image but request the color at imgPixmap! So of course you won't get the correct alpha value. (and what is ptPos???)