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 QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
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 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qquickmaskextruder_p.h" |
41 | #include <QtQml/qqml.h> |
42 | #include <QtQml/qqmlinfo.h> |
43 | #include <QImage> |
44 | #include <QDebug> |
45 | #include <QRandomGenerator> |
46 | QT_BEGIN_NAMESPACE |
47 | /*! |
48 | \qmltype MaskShape |
49 | \instantiates QQuickMaskExtruder |
50 | \inqmlmodule QtQuick.Particles |
51 | \inherits Shape |
52 | \brief For representing an image as a shape to affectors and emitters. |
53 | \ingroup qtquick-particles |
54 | |
55 | */ |
56 | /*! |
57 | \qmlproperty url QtQuick.Particles::MaskShape::source |
58 | |
59 | The image to use as the mask. Areas with non-zero opacity |
60 | will be considered inside the shape. |
61 | */ |
62 | |
63 | |
64 | QQuickMaskExtruder::QQuickMaskExtruder(QObject *parent) : |
65 | QQuickParticleExtruder(parent) |
66 | , m_lastWidth(-1) |
67 | , m_lastHeight(-1) |
68 | { |
69 | } |
70 | |
71 | void QQuickMaskExtruder::setSource(const QUrl &arg) |
72 | { |
73 | if (m_source != arg) { |
74 | m_source = arg; |
75 | |
76 | m_lastHeight = -1;//Trigger reset |
77 | m_lastWidth = -1; |
78 | emit sourceChanged(arg: m_source); |
79 | startMaskLoading(); |
80 | } |
81 | } |
82 | |
83 | void QQuickMaskExtruder::startMaskLoading() |
84 | { |
85 | m_pix.clear(this); |
86 | if (m_source.isEmpty()) |
87 | return; |
88 | m_pix.load(qmlEngine(this), m_source); |
89 | if (m_pix.isLoading()) |
90 | m_pix.connectFinished(this, SLOT(finishMaskLoading())); |
91 | else |
92 | finishMaskLoading(); |
93 | } |
94 | |
95 | void QQuickMaskExtruder::finishMaskLoading() |
96 | { |
97 | if (m_pix.isError()) |
98 | qmlWarning(me: this) << m_pix.error(); |
99 | } |
100 | |
101 | QPointF QQuickMaskExtruder::extrude(const QRectF &r) |
102 | { |
103 | ensureInitialized(r); |
104 | if (!m_mask.count() || m_img.isNull()) |
105 | return r.topLeft(); |
106 | const QPointF p = m_mask[QRandomGenerator::global()->bounded(highest: m_mask.count())]; |
107 | //### Should random sub-pixel positioning be added? |
108 | return p + r.topLeft(); |
109 | } |
110 | |
111 | bool QQuickMaskExtruder::contains(const QRectF &bounds, const QPointF &point) |
112 | { |
113 | ensureInitialized(r: bounds);//###Current usage patterns WILL lead to different bounds/r calls. Separate list? |
114 | if (m_img.isNull()) |
115 | return false; |
116 | |
117 | QPointF pt = point - bounds.topLeft(); |
118 | QPoint p(pt.x() * m_img.width() / bounds.width(), |
119 | pt.y() * m_img.height() / bounds.height()); |
120 | return m_img.rect().contains(p) && (m_img.pixel(pt: p) & 0xff000000); |
121 | } |
122 | |
123 | void QQuickMaskExtruder::ensureInitialized(const QRectF &rf) |
124 | { |
125 | // Convert to integer coords to avoid comparing floats and ints which would |
126 | // often result in rounding errors. |
127 | QRect r = rf.toRect(); |
128 | if (m_lastWidth == r.width() && m_lastHeight == r.height()) |
129 | return;//Same as before |
130 | if (!m_pix.isReady()) |
131 | return; |
132 | m_lastWidth = r.width(); |
133 | m_lastHeight = r.height(); |
134 | |
135 | m_mask.clear(); |
136 | |
137 | m_img = m_pix.image(); |
138 | // Image will in all likelyhood be in this format already, so |
139 | // no extra memory or conversion takes place |
140 | if (m_img.format() != QImage::Format_ARGB32 && m_img.format() != QImage::Format_ARGB32_Premultiplied) |
141 | m_img = m_img.convertToFormat(f: QImage::Format_ARGB32_Premultiplied); |
142 | |
143 | // resample on the fly using 16-bit |
144 | int sx = (m_img.width() << 16) / r.width(); |
145 | int sy = (m_img.height() << 16) / r.height(); |
146 | int w = r.width(); |
147 | int h = r.height(); |
148 | for (int y=0; y<h; ++y) { |
149 | const uint *sl = (const uint *) m_img.constScanLine((y * sy) >> 16); |
150 | for (int x=0; x<w; ++x) { |
151 | if (sl[(x * sx) >> 16] & 0xff000000) |
152 | m_mask << QPointF(x, y); |
153 | } |
154 | } |
155 | } |
156 | QT_END_NAMESPACE |
157 | |
158 | #include "moc_qquickmaskextruder_p.cpp" |
159 | |