| 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(QPainter::CompositionMode_SourceOver, tr("SourceOver")); |
| 67 |
addOp(QPainter::CompositionMode_DestinationOver, tr("DestinationOver")); |
| 68 |
addOp(QPainter::CompositionMode_Clear, tr("Clear")); |
| 69 |
addOp(QPainter::CompositionMode_Source, tr("Source")); |
| 70 |
addOp(QPainter::CompositionMode_Destination, tr("Destination")); |
| 71 |
addOp(QPainter::CompositionMode_SourceIn, tr("SourceIn")); |
| 72 |
addOp(QPainter::CompositionMode_DestinationIn, tr("DestinationIn")); |
| 73 |
addOp(QPainter::CompositionMode_SourceOut, tr("SourceOut")); |
| 74 |
addOp(QPainter::CompositionMode_DestinationOut, tr("DestinationOut")); |
| 75 |
addOp(QPainter::CompositionMode_SourceAtop, tr("SourceAtop")); |
| 76 |
addOp(QPainter::CompositionMode_DestinationAtop, tr("DestinationAtop")); |
| 77 |
addOp(QPainter::CompositionMode_Xor, tr("Xor")); |
| 78 |
addOp(QPainter::CompositionMode_Plus, tr("Plus")); |
| 79 |
addOp(QPainter::CompositionMode_Multiply, tr("Multiply")); |
| 80 |
addOp(QPainter::CompositionMode_Screen, tr("Screen")); |
| 81 |
addOp(QPainter::CompositionMode_Overlay, tr("Overlay")); |
| 82 |
addOp(QPainter::CompositionMode_Darken, tr("Darken")); |
| 83 |
addOp(QPainter::CompositionMode_Lighten, tr("Lighten")); |
| 84 |
addOp(QPainter::CompositionMode_ColorDodge, tr("ColorDodge")); |
| 85 |
addOp(QPainter::CompositionMode_ColorBurn, tr("ColorBurn")); |
| 86 |
addOp(QPainter::CompositionMode_HardLight, tr("HardLight")); |
| 87 |
addOp(QPainter::CompositionMode_SoftLight, tr("SoftLight")); |
| 88 |
addOp(QPainter::CompositionMode_Difference, tr("Difference")); |
| 89 |
addOp(QPainter::CompositionMode_Exclusion, tr("Exclusion")); |
| 90 |
//! [1] |
| 91 |
|
| 92 |
//! [2] |
| 93 |
destinationButton = new QToolButton; |
| 94 |
destinationButton->setIconSize(resultSize); |
| 95 |
|
| 96 |
equalLabel = new QLabel(tr("=")); |
| 97 |
|
| 98 |
resultLabel = new QLabel; |
| 99 |
resultLabel->setMinimumWidth(resultSize.width()); |
| 100 |
//! [2] |
| 101 |
|
| 102 |
//! [3] |
| 103 |
connect(sourceButton, &QAbstractButton::clicked, |
| 104 |
this, &ImageComposer::chooseSource); |
| 105 |
connect(operatorComboBox, QOverload<int>::of(&QComboBox::activated), |
| 106 |
this, &ImageComposer::recalculateResult); |
| 107 |
connect(destinationButton, &QAbstractButton::clicked, |
| 108 |
this, &ImageComposer::chooseDestination); |
| 109 |
//! [3] |
| 110 |
|
| 111 |
//! [4] |
| 112 |
QGridLayout *mainLayout = new QGridLayout; |
| 113 |
mainLayout->addWidget(sourceButton, 0, 0, 3, 1); |
| 114 |
mainLayout->addWidget(operatorComboBox, 1, 1); |
| 115 |
mainLayout->addWidget(destinationButton, 0, 2, 3, 1); |
| 116 |
mainLayout->addWidget(equalLabel, 1, 3); |
| 117 |
mainLayout->addWidget(resultLabel, 0, 4, 3, 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(":/images/butterfly.png", &sourceImage, sourceButton); |
| 126 |
loadImage(":/images/checker.png", &destinationImage, destinationButton); |
| 127 |
|
| 128 |
setWindowTitle(tr("Image Composition")); |
| 129 |
} |
| 130 |
//! [5] |
| 131 |
|
| 132 |
//! [6] |
| 133 |
void ImageComposer::chooseSource() |
| 134 |
{ |
| 135 |
chooseImage(tr("Choose Source Image"), &sourceImage, sourceButton); |
| 136 |
} |
| 137 |
//! [6] |
| 138 |
|
| 139 |
//! [7] |
| 140 |
void ImageComposer::chooseDestination() |
| 141 |
{ |
| 142 |
chooseImage(tr("Choose Destination Image"), &destinationImage, 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(resultImage.rect(), Qt::transparent); |
| 154 |
painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 155 |
painter.drawImage(0, 0, destinationImage); |
| 156 |
painter.setCompositionMode(mode); |
| 157 |
painter.drawImage(0, 0, sourceImage); |
| 158 |
painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); |
| 159 |
painter.fillRect(resultImage.rect(), Qt::white); |
| 160 |
painter.end(); |
| 161 |
|
| 162 |
resultLabel->setPixmap(QPixmap::fromImage(resultImage)); |
| 163 |
} |
| 164 |
//! [8] |
| 165 |
|
| 166 |
//! [9] |
| 167 |
void ImageComposer::addOp(QPainter::CompositionMode mode, const QString &name) |
| 168 |
{ |
| 169 |
operatorComboBox->addItem(name, 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(this, 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(resultSize, Qt::KeepAspectRatio); |
| 191 |
|
| 192 |
QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied); |
| 193 |
QPainter painter(&fixedImage); |
| 194 |
painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 195 |
painter.fillRect(fixedImage.rect(), Qt::transparent); |
| 196 |
painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
| 197 |
painter.drawImage(imagePos(*image), *image); |
| 198 |
painter.end(); |
| 199 |
button->setIcon(QPixmap::fromImage(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(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] |