1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "imagecomposer.h" |
52 | |
53 | #include <QtWidgets> |
54 | |
55 | //! [0] |
56 | static const QSize resultSize(200, 200); |
57 | //! [0] |
58 | |
59 | //! [1] |
60 | ImageComposer::ImageComposer() |
61 | { |
62 | sourceButton = new QToolButton; |
63 | sourceButton->setIconSize(resultSize); |
64 | |
65 | operatorComboBox = new QComboBox; |
66 | addOp(mode: QPainter::CompositionMode_SourceOver, name: tr(s: "SourceOver" )); |
67 | addOp(mode: QPainter::CompositionMode_DestinationOver, name: tr(s: "DestinationOver" )); |
68 | addOp(mode: QPainter::CompositionMode_Clear, name: tr(s: "Clear" )); |
69 | addOp(mode: QPainter::CompositionMode_Source, name: tr(s: "Source" )); |
70 | addOp(mode: QPainter::CompositionMode_Destination, name: tr(s: "Destination" )); |
71 | addOp(mode: QPainter::CompositionMode_SourceIn, name: tr(s: "SourceIn" )); |
72 | addOp(mode: QPainter::CompositionMode_DestinationIn, name: tr(s: "DestinationIn" )); |
73 | addOp(mode: QPainter::CompositionMode_SourceOut, name: tr(s: "SourceOut" )); |
74 | addOp(mode: QPainter::CompositionMode_DestinationOut, name: tr(s: "DestinationOut" )); |
75 | addOp(mode: QPainter::CompositionMode_SourceAtop, name: tr(s: "SourceAtop" )); |
76 | addOp(mode: QPainter::CompositionMode_DestinationAtop, name: tr(s: "DestinationAtop" )); |
77 | addOp(mode: QPainter::CompositionMode_Xor, name: tr(s: "Xor" )); |
78 | addOp(mode: QPainter::CompositionMode_Plus, name: tr(s: "Plus" )); |
79 | addOp(mode: QPainter::CompositionMode_Multiply, name: tr(s: "Multiply" )); |
80 | addOp(mode: QPainter::CompositionMode_Screen, name: tr(s: "Screen" )); |
81 | addOp(mode: QPainter::CompositionMode_Overlay, name: tr(s: "Overlay" )); |
82 | addOp(mode: QPainter::CompositionMode_Darken, name: tr(s: "Darken" )); |
83 | addOp(mode: QPainter::CompositionMode_Lighten, name: tr(s: "Lighten" )); |
84 | addOp(mode: QPainter::CompositionMode_ColorDodge, name: tr(s: "ColorDodge" )); |
85 | addOp(mode: QPainter::CompositionMode_ColorBurn, name: tr(s: "ColorBurn" )); |
86 | addOp(mode: QPainter::CompositionMode_HardLight, name: tr(s: "HardLight" )); |
87 | addOp(mode: QPainter::CompositionMode_SoftLight, name: tr(s: "SoftLight" )); |
88 | addOp(mode: QPainter::CompositionMode_Difference, name: tr(s: "Difference" )); |
89 | addOp(mode: QPainter::CompositionMode_Exclusion, name: tr(s: "Exclusion" )); |
90 | //! [1] |
91 | |
92 | //! [2] |
93 | destinationButton = new QToolButton; |
94 | destinationButton->setIconSize(resultSize); |
95 | |
96 | equalLabel = new QLabel(tr(s: "=" )); |
97 | |
98 | resultLabel = new QLabel; |
99 | resultLabel->setMinimumWidth(resultSize.width()); |
100 | //! [2] |
101 | |
102 | //! [3] |
103 | connect(sender: sourceButton, signal: &QAbstractButton::clicked, |
104 | receiver: this, slot: &ImageComposer::chooseSource); |
105 | connect(sender: operatorComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
106 | receiver: this, slot: &ImageComposer::recalculateResult); |
107 | connect(sender: destinationButton, signal: &QAbstractButton::clicked, |
108 | receiver: this, slot: &ImageComposer::chooseDestination); |
109 | //! [3] |
110 | |
111 | //! [4] |
112 | QGridLayout *mainLayout = new QGridLayout; |
113 | mainLayout->addWidget(sourceButton, row: 0, column: 0, rowSpan: 3, columnSpan: 1); |
114 | mainLayout->addWidget(operatorComboBox, row: 1, column: 1); |
115 | mainLayout->addWidget(destinationButton, row: 0, column: 2, rowSpan: 3, columnSpan: 1); |
116 | mainLayout->addWidget(equalLabel, row: 1, column: 3); |
117 | mainLayout->addWidget(resultLabel, row: 0, column: 4, rowSpan: 3, columnSpan: 1); |
118 | mainLayout->setSizeConstraint(QLayout::SetFixedSize); |
119 | setLayout(mainLayout); |
120 | //! [4] |
121 | |
122 | //! [5] |
123 | resultImage = QImage(resultSize, QImage::Format_ARGB32_Premultiplied); |
124 | |
125 | loadImage(fileName: ":/images/butterfly.png" , image: &sourceImage, button: sourceButton); |
126 | loadImage(fileName: ":/images/checker.png" , image: &destinationImage, button: destinationButton); |
127 | |
128 | setWindowTitle(tr(s: "Image Composition" )); |
129 | } |
130 | //! [5] |
131 | |
132 | //! [6] |
133 | void ImageComposer::chooseSource() |
134 | { |
135 | chooseImage(title: tr(s: "Choose Source Image" ), image: &sourceImage, button: sourceButton); |
136 | } |
137 | //! [6] |
138 | |
139 | //! [7] |
140 | void ImageComposer::chooseDestination() |
141 | { |
142 | chooseImage(title: tr(s: "Choose Destination Image" ), image: &destinationImage, button: destinationButton); |
143 | } |
144 | //! [7] |
145 | |
146 | //! [8] |
147 | void ImageComposer::recalculateResult() |
148 | { |
149 | QPainter::CompositionMode mode = currentMode(); |
150 | |
151 | QPainter painter(&resultImage); |
152 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
153 | painter.fillRect(r: resultImage.rect(), c: Qt::transparent); |
154 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
155 | painter.drawImage(x: 0, y: 0, image: destinationImage); |
156 | painter.setCompositionMode(mode); |
157 | painter.drawImage(x: 0, y: 0, image: sourceImage); |
158 | painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); |
159 | painter.fillRect(r: resultImage.rect(), c: Qt::white); |
160 | painter.end(); |
161 | |
162 | resultLabel->setPixmap(QPixmap::fromImage(image: resultImage)); |
163 | } |
164 | //! [8] |
165 | |
166 | //! [9] |
167 | void ImageComposer::addOp(QPainter::CompositionMode mode, const QString &name) |
168 | { |
169 | operatorComboBox->addItem(atext: name, auserData: mode); |
170 | } |
171 | //! [9] |
172 | |
173 | //! [10] |
174 | void ImageComposer::chooseImage(const QString &title, QImage *image, |
175 | QToolButton *button) |
176 | { |
177 | QString fileName = QFileDialog::getOpenFileName(parent: this, caption: title); |
178 | if (!fileName.isEmpty()) |
179 | loadImage(fileName, image, button); |
180 | } |
181 | //! [10] |
182 | |
183 | //! [11] |
184 | void ImageComposer::loadImage(const QString &fileName, QImage *image, |
185 | QToolButton *button) |
186 | { |
187 | image->load(fileName); |
188 | |
189 | // Scale the image to given size |
190 | *image = image->scaled(s: resultSize, aspectMode: Qt::KeepAspectRatio); |
191 | |
192 | QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied); |
193 | QPainter painter(&fixedImage); |
194 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
195 | painter.fillRect(r: fixedImage.rect(), c: Qt::transparent); |
196 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
197 | painter.drawImage(p: imagePos(image: *image), image: *image); |
198 | painter.end(); |
199 | button->setIcon(QPixmap::fromImage(image: fixedImage)); |
200 | |
201 | *image = fixedImage; |
202 | |
203 | recalculateResult(); |
204 | } |
205 | //! [11] |
206 | |
207 | //! [12] |
208 | QPainter::CompositionMode ImageComposer::currentMode() const |
209 | { |
210 | return (QPainter::CompositionMode) |
211 | operatorComboBox->itemData(index: operatorComboBox->currentIndex()).toInt(); |
212 | } |
213 | //! [12] |
214 | |
215 | //! [13] |
216 | QPoint ImageComposer::imagePos(const QImage &image) const |
217 | { |
218 | return QPoint((resultSize.width() - image.width()) / 2, |
219 | (resultSize.height() - image.height()) / 2); |
220 | } |
221 | //! [13] |
222 | |