PDA

View Full Version : [SOLVED] QImage setPixel alpha issue



bunjee
19th September 2008, 15:59
Hi,

I'm replacing pixels in a QImage using that 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 ?

bunjee
19th September 2008, 16:06
Solved.

QImage dissociates alpha and RGB channel.


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;
}