imagecomposition工程分析
imagecomposition工程存放在Qt安裝目錄下的
Examples\Qt-x.xx.xx\widgets\painting\imagecomposition
目錄下。其中
x.xx.xx為Qt的版本號。該工程展示了QPainter::CompositionMode的含義。要弄懂本工程請先參考《QPainter類的CompositionMode各值含義》
,否則本工程對初學者很難弄懂。
預備知識:
把將要畫上去的顏色稱為“源顏色”,把原來的顏色稱為“目標顏色”。初次畫的時候,窗體上什么都沒有,則窗體或窗體的一個區域(當運用裁剪時)為目標,即將要畫上去的圖形為源。
弄懂本工程的關鍵是要搞懂QPainter類的CompositionMode各值含義、源圖像和目標圖像的概念。
loadImage函數分析
該工程的loadImage函數代碼如下:
void ImageComposer::loadImage(const QString &fileName, QImage *image,QToolButton *button) {image->load(fileName);// Scale the image to given size*image = image->scaled(resultSize, Qt::KeepAspectRatio);QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied);QPainter painter(&fixedImage);painter.setCompositionMode(QPainter::CompositionMode_Source);painter.fillRect(fixedImage.rect(), Qt::transparent);painter.setCompositionMode(QPainter::CompositionMode_SourceOver);painter.drawImage(imagePos(*image), *image);painter.end();button->setIcon(QPixmap::fromImage(fixedImage));*image = fixedImage;recalculateResult(); }當代碼執行到第11行但11行后的代碼沒執行時,即將第12、13、19行代碼注釋掉,根據前面源和目標的定義可以知道:此時窗體為目標,fixedImage為源,根據《QPainter類的CompositionMode各值含義》文章對QPainter::CompositionMode_Source的描述,可以知道,此時只顯示源,但因為fixedImage設置為透明的,所以即使顯示的是源,但看到的是源后面的窗體,如下:
如果將第11行改為:
則輸出如下:
因為源是紅色的,不再透明,所以看不到后面的窗體,顯示的是源的紅色。將第11行依然設置為紅色,取消12、13行注釋,則此時11行繪制上去的紅色QImage對象fixedImage為目標,第13行繪制的image對象為源,因為繪圖組合模式是QPainter::CompositionMode_SourceOver,根據《QPainter類的CompositionMode各值含義》文章對QPainter::CompositionMode_SourceOver的描述,可以知道此時目標和源重疊的部分進行混合,且源蓋住(遮擋住)目標,沒有重疊部分各自保留即都繪制出來,結果如下:
將第11行依然設置為Qt::transparent,則如下:
在ImageComposer類的構造函數中,通過本函數分別加載了兩張圖形,一張用于最左側QToolButton按鈕貼圖,一張用于中間QToolButton按鈕貼圖。
recalculateResult函數分析
函數代碼如下:
void ImageComposer::recalculateResult() {QPainter::CompositionMode mode = currentMode();QPainter painter(&resultImage);painter.setCompositionMode(QPainter::CompositionMode_Source);painter.fillRect(resultImage.rect(), Qt::transparent);painter.setCompositionMode(QPainter::CompositionMode_SourceOver);painter.drawImage(0, 0, destinationImage);painter.setCompositionMode(mode);painter.drawImage(0, 0, sourceImage);painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);painter.fillRect(resultImage.rect(), Qt::white);painter.end();resultLabel->setPixmap(QPixmap::fromImage(resultImage)); }代碼執行到第7行時,即代碼如下:
void ImageComposer::recalculateResult() {QPainter::CompositionMode mode = currentMode();QPainter painter(&resultImage);painter.setCompositionMode(QPainter::CompositionMode_Source);painter.fillRect(resultImage.rect(), Qt::transparent);/* painter.setCompositionMode(QPainter::CompositionMode_SourceOver);painter.drawImage(0, 0, destinationImage);painter.setCompositionMode(mode);painter.drawImage(0, 0, sourceImage);painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);painter.fillRect(resultImage.rect(), Qt::white);*/painter.end();resultLabel->setPixmap(QPixmap::fromImage(resultImage)); }目標是QLabel類型的resultLabel子窗體部件,源是透明的QImage類對象resultImage,采用CompositionMode_Source模式繪制,結果就是將resultImage貼在resultLabel子窗體部件上面,因為resultImage是透明的,所以至第7行時,resultImage繪制和沒繪制都一樣,你看到的都是resultLabel子窗體部件。代碼執行到第8、9行時,即代碼如下:
void ImageComposer::recalculateResult() {QPainter::CompositionMode mode = currentMode();QPainter painter(&resultImage);painter.setCompositionMode(QPainter::CompositionMode_Source);painter.fillRect(resultImage.rect(), Qt::transparent);painter.setCompositionMode(QPainter::CompositionMode_SourceOver);painter.drawImage(0, 0, destinationImage);/* painter.setCompositionMode(mode);painter.drawImage(0, 0, sourceImage);painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);painter.fillRect(resultImage.rect(), Qt::white);*/painter.end();resultLabel->setPixmap(QPixmap::fromImage(resultImage)); }設置QPainter::CompositionMode_SourceOver模式后再以resultImage為畫布(繪圖設備)繪制了destinationImage,此時destinationImage為源,resultImage為目標。根據《QPainter類的CompositionMode各值含義》文章對QPainter::CompositionMode_SourceOver的描述,可以知道此時目標和源重疊的部分進行混合,且源蓋住(遮擋住)目標,沒有重疊部分各自保留即都繪制出來,結果如下紅色方框所示:
代碼執行到第10、11行時,即代碼如下:
根據界面的選擇來設置相應的組合模式,并繪制sourceImage,此時第10行之前繪制的結果即destinationImage為目標,即將要繪制的sourceImage為源,如下紅色方框為在界面下拉框選擇組合模式為SourceOver的輸出:
代碼執行到13行,即代碼如下時:
此時resultImage為源,之前繪制的為目標,又因為第12行設置模式為QPainter::CompositionMode_DestinationOver,即源圖像如果和目標圖像有重疊,繪制時,則重疊部分是目標圖像在源圖像上面(目標遮擋住源),當目標的alpha為255時,源被目標完全遮擋。重疊部分的源和重疊部分的目標進行混合,混合之后得出的RGBA值為重疊部分的RGBA值,沒有重疊部分各自保留即都繪制出來。所以最終的輸出結果如下:
可以結合《QPainter類的CompositionMode各值含義》文章對各個組合模式的描述,讀者自行選擇界面下拉框上的不同的模式進行體會、理解。
總結
以上是生活随笔為你收集整理的imagecomposition工程分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不涨就是好消息!今日油价搁浅 下次3月1
- 下一篇: 脑洞大开!科学家想用气球“撬动”太阳 阻