6#include <QtQml/qqmlinfo.h>
7#include <QtQml/qqmlcontext.h>
11#include <QRandomGenerator>
15
16
17
18
19
20
21
22
24
25
26
27
28
31QQuickMaskExtruder::QQuickMaskExtruder(QObject *parent) :
32 QQuickParticleExtruder(parent)
38void QQuickMaskExtruder::setSource(
const QUrl &arg)
40 if (m_source != arg) {
45 emit sourceChanged(m_source);
50void QQuickMaskExtruder::startMaskLoading()
53 if (m_source.isEmpty())
55 const QQmlContext *context = qmlContext(
this);
56 m_pix.load(context->engine(), context->resolvedUrl(m_source));
57 if (m_pix.isLoading())
58 m_pix.connectFinished(
this, SLOT(finishMaskLoading()));
63void QQuickMaskExtruder::finishMaskLoading()
66 qmlWarning(
this) << m_pix.error();
69QPointF QQuickMaskExtruder::extrude(
const QRectF &r)
72 if (!m_mask.size() || m_img.isNull())
74 const QPointF p = m_mask[QRandomGenerator::global()->bounded(m_mask.size())];
76 return p + r.topLeft();
79bool QQuickMaskExtruder::contains(
const QRectF &bounds,
const QPointF &point)
81 ensureInitialized(bounds);
85 QPointF pt = point - bounds.topLeft();
86 QPoint p(pt.x() * m_img.width() / bounds.width(),
87 pt.y() * m_img.height() / bounds.height());
88 return m_img.rect().contains(p) && (m_img.pixel(p) & 0xff000000);
91void QQuickMaskExtruder::ensureInitialized(
const QRectF &rf)
95 QRect r = rf.toRect();
96 if (m_lastWidth == r.width() && m_lastHeight == r.height())
100 m_lastWidth = r.width();
101 m_lastHeight = r.height();
105 m_img = m_pix.image();
108 if (m_img.format() != QImage::Format_ARGB32 && m_img.format() != QImage::Format_ARGB32_Premultiplied)
109 m_img = std::move(m_img).convertToFormat(QImage::Format_ARGB32_Premultiplied);
112 int sx = (m_img.width() << 16) / r.width();
113 int sy = (m_img.height() << 16) / r.height();
116 for (
int y=0; y<h; ++y) {
117 const uint *sl = (
const uint *) m_img.constScanLine((y * sy) >> 16);
118 for (
int x=0; x<w; ++x) {
119 if (sl[(x * sx) >> 16] & 0xff000000)
120 m_mask << QPointF(x, y);
126#include "moc_qquickmaskextruder_p.cpp"