qt中常见的图片叠加模式

Qt的QPainter::CompositionMode提供了多种图像叠加的模式。常见的有QPainter::CompositionMode_SourceOver, QPainter::CompositionMode_SourceAtop,

QPainter::CompositionMode_DestinationOver和QPainter::CompositionMode_DestinationAtop。现在逐个描述这四种模式的效果。


从http://www.tuicool.com/articles/NF3qIb下载了两幅尺寸相等图片:

qt中常见的图片叠加模式

qt中常见的图片叠加模式


现在用第一幅图片做掩盖,与第二幅图片合成


1)QPainter::CompositionMode_DestinationOver

[cpp] view plain copy
  1. #include "mainwindow.h"  
  2. #include <QMessageBox>  
  3. #include <QFileDialog>  
  4. #include <QPainter>  
  5. #include <QPaintEvent>  
  6.   
  7.   
  8. MainWindow::MainWindow(QWidget *parent)  
  9.     : QMainWindow(parent)  
  10. {  
  11.     QString filename;  
  12.     filename=QFileDialog::getOpenFileName(this,  
  13.                                               tr("选择图像"),  
  14.                                               "",  
  15.                                               tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));  
  16.         if(filename.isEmpty())  
  17.         {  
  18.              return;  
  19.         }  
  20.         else  
  21.         {  
  22.             if(! ( m_img.load(filename) ) ) //加载图像  
  23.             {  
  24.                 QMessageBox::information(this,  
  25.                                          tr("打开图像失败"),  
  26.                                          tr("打开图像失败!"));  
  27.   
  28.                 return;  
  29.             }  
  30.             m_img.load(filename);  
  31.         }  
  32.   
  33.   
  34.     filename=QFileDialog::getOpenFileName(this,  
  35.                                           tr("选择mask"),  
  36.                                           "",  
  37.                                           tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));  
  38.             if(filename.isEmpty())  
  39.             {  
  40.                  return;  
  41.             }  
  42.             else  
  43.             {  
  44.                 if(! ( m_mask.load(filename) ) ) //加载图像  
  45.                 {  
  46.                     QMessageBox::information(this,  
  47.                                              tr("打开图像失败"),  
  48.                                              tr("打开图像失败!"));  
  49.   
  50.                     return;  
  51.                 }  
  52.                 m_mask.load(filename);  
  53.             }  
  54. }  
  55.   
  56. MainWindow::~MainWindow()  
  57. {  
  58.   
  59. }  
  60.   
  61. void MainWindow::paintEvent(QPaintEvent *e)  
  62. {  
  63.     QImage * newImage = new QImage(m_img);  
  64.       
  65.     QImage * mask = new QImage(m_mask);  
  66.     QPainter painter;  
  67.   
  68.     painter.begin(mask);  
  69.   
  70.     painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);  
  71.     painter.drawImage(0, 0, * newImage);  
  72.   
  73.     painter.end();  
  74.   
  75.     painter.begin(this);  
  76.     painter.drawImage(e->rect(), * mask);  
  77.     painter.end();  
  78.   
  79.     delete mask;  
  80.     delete newImage;  
  81. }  

注: m_mask和m_img都是QImage类型的变量,也是MainWindow类的成员。一个代表掩盖图,一个代表花瓶图。运行程序时,MainWindow的构造函数会先询问哪一个文件是被遮挡的花瓶图。你选定之后,MainWindow会再次询问哪一个是掩盖图。选定之后,程序自动调用paintEvent,把两个图合成。


效果:

qt中常见的图片叠加模式


2)CompositionMode_DestinationAtop

将painter.setCompositionMode(QPainter::CompositionMode_DestinationOver)一句改为painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop)即可。

效果和1)相同。

3)CompositionMode_SourceAtop


将painter.setCompositionMode(QPainter::CompositionMode_DestinationOver)一句改为painter.setCompositionMode(QPainter::CompositionMode_SourceAtop)即可。

效果:

qt中常见的图片叠加模式


4)CompositionMode_SourceOver。遮盖不起作用。

qt中常见的图片叠加模式



enum QPainter::CompositionMode

Defines the modes supported for digital image compositing. Composition modes are used to specify how the pixels in one image, the source, are merged with the pixel in another image, the destination.

Please note that the bitwise raster operation modes, denoted with a RasterOp prefix, are only natively supported in the X11 and raster paint engines. This means that the only way to utilize these modes on the Mac is via a QImage. The RasterOp denoted blend modes are not supported for pens and brushes with alpha components. Also, turning on the QPainter::Antialiasing render hint will effectively disable the RasterOp modes.

qt中常见的图片叠加模式

qt中常见的图片叠加模式

The most common type is SourceOver (often referred to as just alpha blending) where the source pixel is blended on top of the destination pixel in such a way that the alpha component of the source defines the translucency of the pixel.

Several composition modes require an alpha channel in the source or target images to have an effect. For optimal performance the image format Format_ARGB32_Premultiplied is preferred.

When a composition mode is set it applies to all painting operator, pens, brushes, gradients and pixmap/image drawing.


Constant

Value

Description

QPainter::CompositionMode_SourceOver

0

This is the default mode. The alpha of the source is used to blend the pixel on top of the destination.

QPainter::CompositionMode_DestinationOver

1

The alpha of the destination is used to blend it on top of the source pixels. This mode is the inverse of CompositionMode_SourceOver.

QPainter::CompositionMode_Clear

2

The pixels in the destination are cleared (set to fully transparent) independent of the source.

QPainter::CompositionMode_Source

3

The output is the source pixel. (This means a basic copy operation and is identical to SourceOver when the source pixel is opaque).

QPainter::CompositionMode_Destination

4

The output is the destination pixel. This means that the blending has no effect. This mode is the inverse of CompositionMode_Source.

QPainter::CompositionMode_SourceIn

5

The output is the source, where the alpha is reduced by that of the destination.

QPainter::CompositionMode_DestinationIn

6

The output is the destination, where the alpha is reduced by that of the source. This mode is the inverse of CompositionMode_SourceIn.

QPainter::CompositionMode_SourceOut

7

The output is the source, where the alpha is reduced by the inverse of destination.

QPainter::CompositionMode_DestinationOut

8

The output is the destination, where the alpha is reduced by the inverse of the source. This mode is the inverse of CompositionMode_SourceOut.

QPainter::CompositionMode_SourceAtop

9

The source pixel is blended on top of the destination, with the alpha of the source pixel reduced by the alpha of the destination pixel.

QPainter::CompositionMode_DestinationAtop

10

The destination pixel is blended on top of the source, with the alpha of the destination pixel is reduced by the alpha of the destination pixel. This mode is the inverse of CompositionMode_SourceAtop.

QPainter::CompositionMode_Xor

11

The source, whose alpha is reduced with the inverse of the destination alpha, is merged with the destination, whose alpha is reduced by the inverse of the source alpha. CompositionMode_Xor is not the same as the bitwise Xor.

QPainter::CompositionMode_Plus

12

Both the alpha and color of the source and destination pixels are added together.

QPainter::CompositionMode_Multiply

13

The output is the source color multiplied by the destination. Multiplying a color with white leaves the color unchanged, while multiplying a color with black produces black.

QPainter::CompositionMode_Screen

14

The source and destination colors are inverted and then multiplied. Screening a color with white produces white, whereas screening a color with black leaves the color unchanged.

QPainter::CompositionMode_Overlay

15

Multiplies or screens the colors depending on the destination color. The destination color is mixed with the source color to reflect the lightness or darkness of the destination.

QPainter::CompositionMode_Darken

16

The darker of the source and destination colors is selected.

QPainter::CompositionMode_Lighten

17

The lighter of the source and destination colors is selected.

QPainter::CompositionMode_ColorDodge

18

The destination color is brightened to reflect the source color. A black source color leaves the destination color unchanged.

QPainter::CompositionMode_ColorBurn

19

The destination color is darkened to reflect the source color. A white source color leaves the destination color unchanged.

QPainter::CompositionMode_HardLight

20

Multiplies or screens the colors depending on the source color. A light source color will lighten the destination color, whereas a dark source color will darken the destination color.

QPainter::CompositionMode_SoftLight

21

Darkens or lightens the colors depending on the source color. Similar to CompositionMode_HardLight.

QPainter::CompositionMode_Difference

22

Subtracts the darker of the colors from the lighter. Painting with white inverts the destination color, whereas painting with black leaves the destination color unchanged.

QPainter::CompositionMode_Exclusion

23

Similar to CompositionMode_Difference, but with a lower contrast. Painting with white inverts the destination color, whereas painting with black leaves the destination color unchanged.

QPainter::RasterOp_SourceOrDestination

24

Does a bitwise OR operation on the source and destination pixels (src OR dst).

QPainter::RasterOp_SourceAndDestination

25

Does a bitwise AND operation on the source and destination pixels (src AND dst).

QPainter::RasterOp_SourceXorDestination

26

Does a bitwise XOR operation on the source and destination pixels (src XOR dst).

QPainter::RasterOp_NotSourceAndNotDestination

27

Does a bitwise NOR operation on the source and destination pixels ((NOT src) AND (NOT dst)).

QPainter::RasterOp_NotSourceOrNotDestination

28

Does a bitwise NAND operation on the source and destination pixels ((NOT src) OR (NOT dst)).

QPainter::RasterOp_NotSourceXorDestination

29

Does a bitwise operation where the source pixels are inverted and then XOR'ed with the destination ((NOT src) XOR dst).

QPainter::RasterOp_NotSource

30

Does a bitwise operation where the source pixels are inverted (NOT src).

QPainter::RasterOp_NotSourceAndDestination

31

Does a bitwise operation where the source is inverted and then AND'ed with the destination ((NOT src) AND dst).

QPainter::RasterOp_SourceAndNotDestination

32

Does a bitwise operation where the source is AND'ed with the inverted destination pixels (src AND (NOT dst)).

QPainter::RasterOp_NotSourceOrDestination

33

Does a bitwise operation where the source is inverted and then OR'ed with the destination ((NOT src) OR dst).

QPainter::RasterOp_ClearDestination

35

The pixels in the destination are cleared (set to 0) independent of the source.

QPainter::RasterOp_SetDestination

36

The pixels in the destination are set (set to 1) independent of the source.

QPainter::RasterOp_NotDestination

37

Does a bitwise operation where the destination pixels are inverted (NOT dst).

QPainter::RasterOp_SourceOrNotDestination

34

Does a bitwise operation where the source is OR'ed with the inverted destination pixels (src OR (NOT dst)).

Qt的QPainter::CompositionMode提供了多种图像叠加的模式。常见的有QPainter::CompositionMode_SourceOver, QPainter::CompositionMode_SourceAtop,

QPainter::CompositionMode_DestinationOver和QPainter::CompositionMode_DestinationAtop。现在逐个描述这四种模式的效果。


从http://www.tuicool.com/articles/NF3qIb下载了两幅尺寸相等图片:

qt中常见的图片叠加模式

qt中常见的图片叠加模式


现在用第一幅图片做掩盖,与第二幅图片合成


1)QPainter::CompositionMode_DestinationOver

[cpp] view plain copy
  1. #include "mainwindow.h"  
  2. #include <QMessageBox>  
  3. #include <QFileDialog>  
  4. #include <QPainter>  
  5. #include <QPaintEvent>  
  6.   
  7.   
  8. MainWindow::MainWindow(QWidget *parent)  
  9.     : QMainWindow(parent)  
  10. {  
  11.     QString filename;  
  12.     filename=QFileDialog::getOpenFileName(this,  
  13.                                               tr("选择图像"),  
  14.                                               "",  
  15.                                               tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));  
  16.         if(filename.isEmpty())  
  17.         {  
  18.              return;  
  19.         }  
  20.         else  
  21.         {  
  22.             if(! ( m_img.load(filename) ) ) //加载图像  
  23.             {  
  24.                 QMessageBox::information(this,  
  25.                                          tr("打开图像失败"),  
  26.                                          tr("打开图像失败!"));  
  27.   
  28.                 return;  
  29.             }  
  30.             m_img.load(filename);  
  31.         }  
  32.   
  33.   
  34.     filename=QFileDialog::getOpenFileName(this,  
  35.                                           tr("选择mask"),  
  36.                                           "",  
  37.                                           tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));  
  38.             if(filename.isEmpty())  
  39.             {  
  40.                  return;  
  41.             }  
  42.             else  
  43.             {  
  44.                 if(! ( m_mask.load(filename) ) ) //加载图像  
  45.                 {  
  46.                     QMessageBox::information(this,  
  47.                                              tr("打开图像失败"),  
  48.                                              tr("打开图像失败!"));  
  49.   
  50.                     return;  
  51.                 }  
  52.                 m_mask.load(filename);  
  53.             }  
  54. }  
  55.   
  56. MainWindow::~MainWindow()  
  57. {  
  58.   
  59. }  
  60.   
  61. void MainWindow::paintEvent(QPaintEvent *e)  
  62. {  
  63.     QImage * newImage = new QImage(m_img);  
  64.       
  65.     QImage * mask = new QImage(m_mask);  
  66.     QPainter painter;  
  67.   
  68.     painter.begin(mask);  
  69.   
  70.     painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);  
  71.     painter.drawImage(0, 0, * newImage);  
  72.   
  73.     painter.end();  
  74.   
  75.     painter.begin(this);  
  76.     painter.drawImage(e->rect(), * mask);  
  77.     painter.end();  
  78.   
  79.     delete mask;  
  80.     delete newImage;  
  81. }  

注: m_mask和m_img都是QImage类型的变量,也是MainWindow类的成员。一个代表掩盖图,一个代表花瓶图。运行程序时,MainWindow的构造函数会先询问哪一个文件是被遮挡的花瓶图。你选定之后,MainWindow会再次询问哪一个是掩盖图。选定之后,程序自动调用paintEvent,把两个图合成。


效果:

qt中常见的图片叠加模式


2)CompositionMode_DestinationAtop

将painter.setCompositionMode(QPainter::CompositionMode_DestinationOver)一句改为painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop)即可。

效果和1)相同。

3)CompositionMode_SourceAtop


将painter.setCompositionMode(QPainter::CompositionMode_DestinationOver)一句改为painter.setCompositionMode(QPainter::CompositionMode_SourceAtop)即可。

效果:

qt中常见的图片叠加模式


4)CompositionMode_SourceOver。遮盖不起作用。

qt中常见的图片叠加模式