summaryrefslogtreecommitdiffstats
path: root/src/imports/particles/maskextruder.cpp
blob: c55e8c8304bfdd1572d1a527e03a40869cd68352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "maskextruder.h"
#include <QImage>
#include <QDebug>
QT_BEGIN_NAMESPACE
MaskExtruder::MaskExtruder(QObject *parent) :
    ParticleExtruder(parent)
  , m_lastWidth(-1)
  , m_lastHeight(-1)
{
}

QPointF MaskExtruder::extrude(const QRectF &r)
{
    ensureInitialized(r);
    if(!m_mask.count())
        return r.topLeft();
    const QPointF p = m_mask[rand() % m_mask.count()];
    //### Should random sub-pixel positioning be added?
    return p + r.topLeft();
}

void MaskExtruder::ensureInitialized(const QRectF &r)
{
    if(m_lastWidth == r.width() && m_lastHeight == r.height())
        return;
    m_lastWidth = r.width();
    m_lastHeight = r.height();

    m_mask.clear();
    if(m_source.isEmpty())
        return;

    QImage img(m_source.toLocalFile());
    img = img.createAlphaMask();
    img = img.convertToFormat(QImage::Format_Mono);//Else LSB, but I think that's easier
    img = img.scaled(r.size().toSize());//TODO: Do they need aspect ratio stuff? Or tiling?
    for(int i=0; i<r.width(); i++){
        for(int j=0; j<r.height(); j++){
            if(img.pixelIndex(i,j))//Direct bit manipulation is presumably more efficient
                m_mask << QPointF(i,j);
        }
    }
}
QT_END_NAMESPACE