1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
46
#include "extrafiltersplugin.h"
48
QStringList ExtraFiltersPlugin::filters() const
50
return QStringList() << tr("Flip Horizontally") << tr("Flip Vertically")
51
<< tr("Smudge...") << tr("Threshold...");
54
QImage ExtraFiltersPlugin::filterImage(const QString &filter,
55
const QImage &image, QWidget *parent)
57
QImage original = image.convertToFormat(QImage::Format_RGB32);
58
QImage result = original;
60
if (filter == tr("Flip Horizontally")) {
61
for (int y = 0; y < original.height(); ++y) {
62
for (int x = 0; x < original.width(); ++x) {
63
int pixel = original.pixel(original.width() - x - 1, y);
64
result.setPixel(x, y, pixel);
67
} else if (filter == tr("Flip Vertically")) {
68
for (int y = 0; y < original.height(); ++y) {
69
for (int x = 0; x < original.width(); ++x) {
70
int pixel = original.pixel(x, original.height() - y - 1);
71
result.setPixel(x, y, pixel);
74
} else if (filter == tr("Smudge...")) {
76
int numIters = QInputDialog::getInt(parent, tr("Smudge Filter"),
77
tr("Enter number of iterations:"),
80
for (int i = 0; i < numIters; ++i) {
81
for (int y = 1; y < original.height() - 1; ++y) {
82
for (int x = 1; x < original.width() - 1; ++x) {
83
int p1 = original.pixel(x, y);
84
int p2 = original.pixel(x, y + 1);
85
int p3 = original.pixel(x, y - 1);
86
int p4 = original.pixel(x + 1, y);
87
int p5 = original.pixel(x - 1, y);
89
int red = (qRed(p1) + qRed(p2) + qRed(p3) + qRed(p4)
91
int green = (qGreen(p1) + qGreen(p2) + qGreen(p3)
92
+ qGreen(p4) + qGreen(p5)) / 5;
93
int blue = (qBlue(p1) + qBlue(p2) + qBlue(p3)
94
+ qBlue(p4) + qBlue(p5)) / 5;
95
int alpha = (qAlpha(p1) + qAlpha(p2) + qAlpha(p3)
96
+ qAlpha(p4) + qAlpha(p5)) / 5;
98
result.setPixel(x, y, qRgba(red, green, blue, alpha));
103
} else if (filter == tr("Threshold...")) {
105
int threshold = QInputDialog::getInt(parent, tr("Threshold Filter"),
106
tr("Enter threshold:"),
109
int factor = 256 / threshold;
110
for (int y = 0; y < original.height(); ++y) {
111
for (int x = 0; x < original.width(); ++x) {
112
int pixel = original.pixel(x, y);
113
result.setPixel(x, y, qRgba(qRed(pixel) / factor * factor,
114
qGreen(pixel) / factor * factor,
115
qBlue(pixel) / factor * factor,