2
votes

I'm trying to understand why the code below changes the QImage in Qt. It's not meant to do anything (yet), it's just for testing. When I run the code on an image with alpha, the alpha channel is lost and replaced by a black background.

QImage image;
image.load("image.png");

for (int y = 0; y < image.height(); y++) {
    for (int x = 0; x < image.height(); x++) {
        QColor c = QColor::fromRgba(image.pixel(x, y));
        c.setHsv(c.hue(), c.saturation(), c.value());
        image.setPixel(x, y, c.rgba());
    }
}

Here is the result when I comment out the line image.setPixel(...):

enter image description here

And here is the result with the image.setPixel(...) line:

enter image description here

I would expect my code to do no change on the image. Any idea why it's doing this?

1

1 Answers

3
votes

If you look at the documentation of setHsv(), you will see that alpha is set by default to 255 (or 1.0 for the float version) if you don't explicitly specify it.

Perhaps using the line c.setHsv(c.hue(), c.saturation(), c.value(), c.alpha()); will resolve your problem.