• QImage函数setAlphaChannel


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

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

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

    大概意思:

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

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

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

    QImage::Format_ARGB32_Premultiplied格式。

    测试如下:

    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QImage src1(100,100,QImage::Format_RGB32);
    4. QImage src2(100,100,QImage::Format_RGB16);
    5. QImage src3(100,100,QImage::Format_ARGB32);
    6. QImage alpha(100,100,QImage::Format_Grayscale8);
    7. alpha.fill(Qt::white);
    8. src1.setAlphaChannel(alpha);
    9. src2.setAlphaChannel(alpha);
    10. src3.setAlphaChannel(alpha);
    11. qDebug()<<(src1.format() == QImage::Format_ARGB32_Premultiplied);
    12. qDebug()<<(src2.format() == QImage::Format_ARGB32_Premultiplied);
    13. qDebug()<<(src3.format() == QImage::Format_ARGB32_Premultiplied);
    14. }

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

  • 相关阅读:
    php如何实现文件上传
    初始化IMU
    Lombok工具 : 常用注解介绍 (全)
    Springboot Actuator 环境搭建踩坑
    猿创征文 | 项目整合KafkaStream实现文章热度实时计算
    JOSEF约瑟 10KV高压漏电保护继电器BLD-20 φ100mm 50-500mA 导轨安装
    一文详解最大重复子串
    索引介绍及索引的分类
    GPT引发智能AI时代潮流
    【C#异步】异步多线程的本质,上下文流转和同步
  • 原文地址:https://blog.csdn.net/hulinhulin/article/details/133720487