[SOLVED] QImage setPixel alpha issue
Hi,
I'm replacing pixels in a QImage using that code :
Code:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int alpha = qAlpha(image.pixel(j, i));
if (alpha > 0)
{
image.setPixel(j, i,
qRgba(color.red(),
color.green(),
color.blue(),
alpha));
}
}
}
q
->QPixmap
::operator=(QPixmap::fromImage(image
));
My image file is a PNG file.
For some reason, everytime I replace a "transparent" pixel. The pixel goes black instead of the choosen color. Plain pixels are replaced fine.
Any idea ?
Re: QImage setPixel alpha issue
Solved.
QImage dissociates alpha and RGB channel.
Code:
void qkColorPixmapPrivate
::refreshPixmap(const QColor & color
) {
Q_Q(qkColorPixmap);
if (this->color == color) return;
QImage image
= basePixmap.
toImage();
QImage alpha
= image.
alphaChannel();
int width = basePixmap.width();
int height = basePixmap.height();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int a = qAlpha(image.pixel(j, i));
if (a > 0)
{
image.setPixel(j, i,
qRgb(color.red(),
color.green(),
color.blue()));
alpha.setPixel(j, i, a);
}
}
}
image.setAlphaChannel(alpha);
q
->QPixmap
::operator=(QPixmap::fromImage(image
));
this->color = color;
}