日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt QPainter CompositionMode解读及图片透明度设置

發布時間:2023/12/20 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt QPainter CompositionMode解读及图片透明度设置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 前言
    • 研究相關
  • QPainter::CompositionMode
    • QPainter::CompositionMode_SourceOver
    • QPainter::CompositionMode_Source
    • QPainter::CompositionMode_DestinationIn
    • QPainter::CompositionMode_DestinationOut
  • 調節圖片透明度
  • 畫板橡皮擦
  • 寫在最后

前言

Qt的QPainter十分強大,設置不同的組合模式,可以得到迥然不同的結果,比如調節圖片的透明度等,因此是否有必要弄清除QPainter的組合模式代表的意義。

研究相關

  • Source指的是繪制的輸入,Destination指的是目的繪制區域本來存在的像素。
  • 本文使用"Compositon Modes"程序對QPainter的組合模式進行解讀,其中花是繪制區域原來存在的像素,橢圓是繪制的輸入,Circle color是橢圓的的RGB通道數值,circle alpha是橢圓的透明通道數值。

QPainter::CompositionMode

QPainter::CompositionMode_SourceOver

  • Qt幫組文檔描述:This is the default mode. The alpha of the source is used to blend the pixel on top of the destination.
  • SourceOver是默認的繪制模式,繪制結果 = 繪制輸入*ratio_source + 目的區域原來像素 * ratio_destination。ratio_source和ratio_destination取決于繪制輸入的alpha通道值與繪制區域的alpha通道數值的比值(大部分取決于繪制區域的alpha數值,關系不是線性)。
    • 測試數據
目的區域像素輸入像素結果
(255, 255, 255, 10)(0, 0, 0, 100)(14, 14, 14, 102)
(255, 255, 255, 10)(0, 0, 0, 200)(3,3,3,202)
(255, 255, 255, 10)(0, 0, 0, 255)(0, 0, 0, 255)
(255, 255, 255, 255)(100, 0, 0, 200)(133, 55, 55, 255)
(255, 255, 255, 0)(0, 0, 0, 200)(0, 0, 0, 200)

QPainter::CompositionMode_Source

  • Qt幫組文檔描述:The output is the source pixel. (This means a basic copy operation and is identical to SourceOver when the source pixel is opaque).
  • Source模式:繪制結果 = 繪制輸入,目的繪制區域原來的像素被完全覆蓋。
  • 當繪制輸入的Alpha通道為255(不透明時),Source和SourceOver模式的表現相同,因為SourceOver模式中ratio_source = 1,ratio_destination = 0;

QPainter::CompositionMode_DestinationIn

  • Qt幫組文檔描述:The output is the destination, where the alpha is reduced by that of the source. This mode is the inverse of CompositionMode_SourceIn.
  • DestinationIn模式,最后的繪制結果為原繪制區域圖像,但是其alpha通道數值會受到輸入的影響。
  • 繪制結果 = 區域原像素(除Alpha通道外) + 區域原像素(Alpha通道)* 繪制輸入(Alpha通道)/ 255,繪制輸入(除Alpha通道)的值被丟棄,因此該模式一般僅用于設置繪制區域的透明度。
    • 繪制結果通過代碼測試出來
      • x = 200; 輸出為1325400063(4EFFFFFF),Alpha通道值為78(0x4E),而100 * (200/255) = 78.4313;
      • x = 100;輸出為671088639(27FFFFFF),Alpha通道為39(0x27),而100 * (100/255) = 39.6825
    • 測試代碼QImage temp(QSize(100, 100), QImage::Format_ARGB32); temp.fill(QColor(255, 255, 255, 100)); QPainter painter(&temp); painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); painter.fillRect(temp.rect(), QColor(0, 0, 0, x)); painter.end(); qDebug() << temp.pixel(50, 50);

QPainter::CompositionMode_DestinationOut

  • Qt幫組文檔描述:The output is the destination, where the alpha is reduced by the inverse of the source. This mode is the inverse of CompositionMode_SourceOut.
  • DestinationOut模式,最后的繪制結果為原繪制區域圖像,但是其alpha透明通道數值會受到輸入alpha透明通道取反值的影響。
  • 繪制結果 = 區域原像素(除Alpha通道外) + 區域原像素(Alpha通道)* (255 - 繪制輸入(Alpha通道))/ 255,繪制輸入(除Alpha通道)的值被丟棄,因此該模式一般僅用于設置繪制區域的透明度。
目的區域像素輸入像素結果
(255, 255, 255, 200)(255, 255, 255, 127)(255, 255, 255, 100)
(255, 255, 255, 100)(255, 255, 255, 127)(255, 255, 255, 50)
(255, 255, 255, 50)(255, 255, 255, 127)(255, 255, 255, 25)
(255, 255, 255, 25)(255, 255, 255, 127)(255, 255, 255, 13)
(255, 255, 255, 13)(255, 255, 255, 127)(255, 255, 255, 7)
(255, 255, 255, 7)(255, 255, 255, 127)(255, 255, 255, 4)
(255, 255, 255, 4)(255, 255, 255, 127)(255, 255, 255, 2)
(255, 255, 255, 2)(255, 255, 255, 127)(255, 255, 255, 1)
(255, 255, 255, 1)(255, 255, 255, 127)(255, 255, 255, 1)
  • 從測試數據中不難看出,該模式可以用作畫板的橡皮檫,多次擦除后,最后殘留的像素必為(x, x, x, 1),顯示出來的結果近似與空白,而又不會破壞畫板。
    • 需要注意的是,橡皮檫的透明通道最大為127,否則將穿透畫板(最后的像素透明通道為0),因為大于127,則有(255 - 127)/255 < 0.5,多次擦除后透明通道將為0。

調節圖片透明度

/* 最后生成的img_res是img圖片的透明復制 */QPixmap temp(this->img.size()); temp.fill(Qt::transparent);QPainter p1(&temp); // CompositionMode_Source將圖片繪制進去 p1.setCompositionMode(QPainter::CompositionMode_Source); p1.drawPixmap(0, 0, this->img); // CompositionMode_DestinationIn設置圖片的透明度 p1.setCompositionMode(QPainter::CompositionMode_DestinationIn); // 根據QColor中第四個參數設置透明度,此處position的取值范圍是0~255 p1.fillRect(temp.rect(), QColor(0, 0, 0, this->transparency * 255/100)); p1.end();this->img_res = temp.copy();

畫板橡皮擦

QPainter p1(&temp); p1.setPen(QPen(QColor(255, 255, 255, 127),pen_thickness + 10,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin)); p1.setCompositionMode(QPainter::CompositionMode_DestinationOut); p1.drawLine(start_pos, end_pos);

寫在最后

  • 目前只列舉了常見的三種CompositionMode,這三種模式可以用于調節圖片的透明度。后續將繼續補充。
  • 20200328更新:加入CompositionMode_DestinationOut模式的解讀。

總結

以上是生活随笔為你收集整理的Qt QPainter CompositionMode解读及图片透明度设置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。