1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the Declarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickmaskextruder_p.h"
47 \qmlclass MaskShape QQuickMaskExtruder
48 \inqmlmodule QtQuick.Particles 2
50 \brief The MaskShape element allows you to represent an image as a shape to affectors and emitters.
54 \qmlproperty url QtQuick.Particles2::MaskShape::source
56 The image to use as the mask. Areas with non-zero opacity
57 will be considered inside the shape.
61 QQuickMaskExtruder::QQuickMaskExtruder(QObject *parent) :
62 QQuickParticleExtruder(parent)
68 QPointF QQuickMaskExtruder::extrude(const QRectF &r)
71 if (!m_mask.count() || m_img.isNull())
73 const QPointF p = m_mask[rand() % m_mask.count()];
74 //### Should random sub-pixel positioning be added?
75 return p + r.topLeft();
78 bool QQuickMaskExtruder::contains(const QRectF &bounds, const QPointF &point)
80 ensureInitialized(bounds);//###Current usage patterns WILL lead to different bounds/r calls. Separate list?
83 QPoint p = point.toPoint() - bounds.topLeft().toPoint();
84 return m_img.rect().contains(p) && (bool)m_img.pixelIndex(p);
87 void QQuickMaskExtruder::ensureInitialized(const QRectF &r)
89 if (m_lastWidth == r.width() && m_lastHeight == r.height())
90 return;//Same as before
91 m_lastWidth = r.width();
92 m_lastHeight = r.height();
96 if (m_source.isEmpty())
98 m_img = QImage(m_source.toLocalFile());
100 qWarning() << "MaskShape: Cannot load" << qPrintable(m_source.toLocalFile());
103 m_img = m_img.createAlphaMask();
104 m_img = m_img.convertToFormat(QImage::Format_Mono);//Else LSB, but I think that's easier
105 m_img = m_img.scaled(r.size().toSize());//TODO: Do they need aspect ratio stuff? Or tiling?
106 for (int i=0; i<r.width(); i++){
107 for (int j=0; j<r.height(); j++){
108 if (m_img.pixelIndex(i,j))//Direct bit manipulation is presumably more efficient
109 m_mask << QPointF(i,j);