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 "extrafiltersplugin.h" |
52 | |
53 | #include <QInputDialog> |
54 | |
55 | QStringList ExtraFiltersPlugin::() const |
56 | { |
57 | return {tr(s: "Flip Horizontally" ), tr(s: "Flip Vertically" ), |
58 | tr(s: "Smudge..." ), tr(s: "Threshold..." )}; |
59 | } |
60 | |
61 | QImage ExtraFiltersPlugin::(const QString &filter, |
62 | const QImage &image, QWidget *parent) |
63 | { |
64 | QImage original = image.convertToFormat(f: QImage::Format_RGB32); |
65 | QImage result = original; |
66 | |
67 | if (filter == tr(s: "Flip Horizontally" )) { |
68 | for (int y = 0; y < original.height(); ++y) { |
69 | for (int x = 0; x < original.width(); ++x) { |
70 | QRgb pixel = original.pixel(x: original.width() - x - 1, y); |
71 | result.setPixel(x, y, index_or_rgb: pixel); |
72 | } |
73 | } |
74 | } else if (filter == tr(s: "Flip Vertically" )) { |
75 | for (int y = 0; y < original.height(); ++y) { |
76 | for (int x = 0; x < original.width(); ++x) { |
77 | QRgb pixel = original.pixel(x, y: original.height() - y - 1); |
78 | result.setPixel(x, y, index_or_rgb: pixel); |
79 | } |
80 | } |
81 | } else if (filter == tr(s: "Smudge..." )) { |
82 | bool ok; |
83 | int numIters = QInputDialog::getInt(parent, title: tr(s: "Smudge Filter" ), |
84 | label: tr(s: "Enter number of iterations:" ), |
85 | value: 5, minValue: 1, maxValue: 20, step: 1, ok: &ok); |
86 | if (ok) { |
87 | for (int i = 0; i < numIters; ++i) { |
88 | for (int y = 1; y < original.height() - 1; ++y) { |
89 | for (int x = 1; x < original.width() - 1; ++x) { |
90 | QRgb p1 = original.pixel(x, y); |
91 | QRgb p2 = original.pixel(x, y: y + 1); |
92 | QRgb p3 = original.pixel(x, y: y - 1); |
93 | QRgb p4 = original.pixel(x: x + 1, y); |
94 | QRgb p5 = original.pixel(x: x - 1, y); |
95 | |
96 | int red = (qRed(rgb: p1) + qRed(rgb: p2) + qRed(rgb: p3) + qRed(rgb: p4) |
97 | + qRed(rgb: p5)) / 5; |
98 | int green = (qGreen(rgb: p1) + qGreen(rgb: p2) + qGreen(rgb: p3) |
99 | + qGreen(rgb: p4) + qGreen(rgb: p5)) / 5; |
100 | int blue = (qBlue(rgb: p1) + qBlue(rgb: p2) + qBlue(rgb: p3) |
101 | + qBlue(rgb: p4) + qBlue(rgb: p5)) / 5; |
102 | int alpha = (qAlpha(rgb: p1) + qAlpha(rgb: p2) + qAlpha(rgb: p3) |
103 | + qAlpha(rgb: p4) + qAlpha(rgb: p5)) / 5; |
104 | |
105 | result.setPixel(x, y, index_or_rgb: qRgba(r: red, g: green, b: blue, a: alpha)); |
106 | } |
107 | } |
108 | } |
109 | } |
110 | } else if (filter == tr(s: "Threshold..." )) { |
111 | bool ok; |
112 | int threshold = QInputDialog::getInt(parent, title: tr(s: "Threshold Filter" ), |
113 | label: tr(s: "Enter threshold:" ), |
114 | value: 10, minValue: 1, maxValue: 256, step: 1, ok: &ok); |
115 | if (ok) { |
116 | int factor = 256 / threshold; |
117 | for (int y = 0; y < original.height(); ++y) { |
118 | for (int x = 0; x < original.width(); ++x) { |
119 | QRgb pixel = original.pixel(x, y); |
120 | result.setPixel(x, y, index_or_rgb: qRgba(r: qRed(rgb: pixel) / factor * factor, |
121 | g: qGreen(rgb: pixel) / factor * factor, |
122 | b: qBlue(rgb: pixel) / factor * factor, |
123 | a: qAlpha(rgb: pixel))); |
124 | } |
125 | } |
126 | } |
127 | } |
128 | return result; |
129 | } |
130 | |