~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
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
 
19
**     distribution.
 
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.
 
23
**
 
24
**
 
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."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include <QtWidgets>
 
42
 
 
43
#include <math.h>
 
44
#include <stdlib.h>
 
45
 
 
46
#include "extrafiltersplugin.h"
 
47
 
 
48
QStringList ExtraFiltersPlugin::filters() const
 
49
{
 
50
    return QStringList() << tr("Flip Horizontally") << tr("Flip Vertically")
 
51
                         << tr("Smudge...") << tr("Threshold...");
 
52
}
 
53
 
 
54
QImage ExtraFiltersPlugin::filterImage(const QString &filter,
 
55
                                       const QImage &image, QWidget *parent)
 
56
{
 
57
    QImage original = image.convertToFormat(QImage::Format_RGB32);
 
58
    QImage result = original;
 
59
 
 
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);
 
65
            }
 
66
        }
 
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);
 
72
            }
 
73
        }
 
74
    } else if (filter == tr("Smudge...")) {
 
75
        bool ok;
 
76
        int numIters = QInputDialog::getInt(parent, tr("Smudge Filter"),
 
77
                                    tr("Enter number of iterations:"),
 
78
                                    5, 1, 20, 1, &ok);
 
79
        if (ok) {
 
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);
 
88
 
 
89
                        int red = (qRed(p1) + qRed(p2) + qRed(p3) + qRed(p4)
 
90
                                   + qRed(p5)) / 5;
 
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;
 
97
 
 
98
                        result.setPixel(x, y, qRgba(red, green, blue, alpha));
 
99
                    }
 
100
                }
 
101
            }
 
102
        }
 
103
    } else if (filter == tr("Threshold...")) {
 
104
        bool ok;
 
105
        int threshold = QInputDialog::getInt(parent, tr("Threshold Filter"),
 
106
                                                 tr("Enter threshold:"),
 
107
                                                 10, 1, 256, 1, &ok);
 
108
        if (ok) {
 
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,
 
116
                                                qAlpha(pixel)));
 
117
                }
 
118
            }
 
119
        }
 
120
    }
 
121
    return result;
 
122
}