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