开发者

Why does this code make the QImage lose its alpha channel?

开发者 https://www.devze.com 2023-03-26 13:00 出处:网络
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 lo

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 开发者_如何学Gox = 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(...):

Why does this code make the QImage lose its alpha channel?

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

Why does this code make the QImage lose its alpha channel?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消