开发者

Checking if a QImage has an alpha channel

开发者 https://www.devze.com 2023-03-09 01:38 出处:网络
I want to know if a QImage I loaded 开发者_StackOverflowcontains an alpha channel. I already know that QImage::hasAlphaChannel() can tell me if the image format I\'m using supports alpha channels, but

I want to know if a QImage I loaded 开发者_StackOverflowcontains an alpha channel. I already know that QImage::hasAlphaChannel() can tell me if the image format I'm using supports alpha channels, but is there a way to know if it's actually being used in the loaded image?


Here you have my snippet for checking if alpha is really used. It's useful when image is in ARGB32.

bool useAlpha = false;
const uchar* pixelData = image.bits();
int bytes = image.byteCount();

for (const QRgb* pixel = reinterpret_cast<const QRgb*>(pixelData); bytes > 0; pixel++, bytes -= sizeof(QRgb)) {
    if (qAlpha(*pixel) != UCHAR_MAX) {
        useAlpha = true;
        break;
    }
}

Remember also that there is format() method.


If the format you load the QImage as has an alpha channel, your QImage has an alpha channel.

If you're checking to see if any pixel in an image with an alpha channel actually sets any pixel to something other than opaque, you could try something like generating an alpha mask using QImage::createAlphaMask() and inspecting its pixel values.

0

精彩评论

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