QImage函数setAlphaChannel

最近使用QImage的函数setAlphaChannel时遇到了一个坑,花了不少时间才弄清楚:在使用这个函数后,图像格式都会变成QImage::Format_ARGB32_Premultiplied。

先看下setAlphaChannel在帮助文档的说明:

复制代码
void QImage::setAlphaChannel(const QImage &alphaChannel)
Sets the alpha channel of this image to the given alphaChannel.
If alphaChannel is an 8 bit alpha image, the alpha values are 
used directly. Otherwise, alphaChannel is converted to 
8 bit grayscale and the intensity of the pixel values is used.
If the image already has an alpha channel, 
the existing alpha channel is multiplied with the new one. 
If the image doesn't have an alpha channel 
it will be converted to a format that does.
The operation is similar to painting alphaChannel as 
an alpha image over this image using QPainter::CompositionMode_DestinationIn.

大概意思:

setAlphaChannel函数为图像指定透明通道,如果alphaChannel是单通道的8位图片,那么直接使用,如果不是就转换成8位的灰度图片在作为透明通道。

如果图像已经有透明通道,那么两个通道会相乘,如果图像没有透明通道则会将图像转换成有透明通道的格式。

帮助文档只说了如果图像没有透明通道,那么会将图像转化成有透明通道的图像,但在使用过程中会发现,只要使用了setAlphaChannel,图像都会将格式转化成

复制代码
QImage::Format_ARGB32_Premultiplied格式。

测试如下:

复制代码
void MainWindow::on_pushButton_clicked()
{
    QImage src1(100,100,QImage::Format_RGB32);
    QImage src2(100,100,QImage::Format_RGB16);
    QImage src3(100,100,QImage::Format_ARGB32);

    QImage alpha(100,100,QImage::Format_Grayscale8);
    alpha.fill(Qt::white);

    src1.setAlphaChannel(alpha);
    src2.setAlphaChannel(alpha);
    src3.setAlphaChannel(alpha);

    qDebug()<<(src1.format() == QImage::Format_ARGB32_Premultiplied);
    qDebug()<<(src2.format() == QImage::Format_ARGB32_Premultiplied);
    qDebug()<<(src3.format() == QImage::Format_ARGB32_Premultiplied);
}

打印出的结果都是true,也就是图像格式都转换成了QImage::Format_ARGB32_Premultiplied。

相关推荐
Zafir20242 小时前
Qt实现TabWidget通过addTab函数添加的页,页内控件自适应窗口大小
开发语言·c++·qt·ui
王廷胡_白嫖帝7 小时前
Qt文件压缩工具项目开发教程
java·开发语言·qt
牵牛老人14 小时前
Qt 插件开发全解析:从接口定义,插件封装,插件调用到插件间的通信
开发语言·qt
机器视觉知识推荐、就业指导17 小时前
面试问题详解五:Qt 信号与槽的动态管理
开发语言·qt
四维碎片1 天前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio
feiyangqingyun1 天前
纯Qt结合ffmpeg实现本地摄像头采集/桌面采集/应用程序窗口采集/指定采集帧率和分辨率等
qt·ffmpeg·qt桌面采集·qt摄像头采集·qt程序窗口采集
王廷胡_白嫖帝1 天前
Qt个人通讯录项目开发教程 - 从零开始构建联系人管理系统
开发语言·qt
戏言zare1 天前
Qt设置软件使用期限【新版防修改系统时间】
qt
大橘1 天前
【qml-5】qml与c++交互(类型单例)
qt·qml
lxmyzzs2 天前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv