-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskbased.cpp
76 lines (65 loc) · 1.74 KB
/
maskbased.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "maskbased.h"
#include <QPainter>
#include <QDebug>
MaskBased::MaskBased() :
BasicBrush()
{
typedef BrushFeature BF;
BF::FeatureBits bits;
bits.set(BF::WIDTH);
bits.set(BF::COLOR);
bits.set(BF::THICKNESS);
features_ = bits;
name_ = QObject::tr("Crayon");
displayName_ = name_;
shortcut_ = Qt::Key_5;
}
void MaskBased::makeStencil(QColor color)
{
BasicBrush::makeStencil(color);
if(mask_.isNull()){
return;
}
}
void MaskBased::drawPointInternal(const QPoint &p, const QImage &stencil, QPainter *painter)
{
QImage copied_stencil = stencil;
bool need_delete = false;
if(!painter) {
painter = new QPainter;
need_delete = true;
painter->setRenderHint(QPainter::Antialiasing);
painter->begin(surface_.data());
}
int lineLength = copied_stencil.width();
QRgb * data = (QRgb *)copied_stencil.bits();
int mask_start_x = (p.x()+mask_.width()) % mask_.width();
int mask_start_y = (p.y()+mask_.height()) % mask_.height();
for(int y = 0; y<copied_stencil.height();++y){
for(int x = 0; x<lineLength;++x) {
int mask_x = (mask_start_x + x) % mask_.width();
int mask_y = (mask_start_y + y) % mask_.height();
if(!mask_.pixelIndex(mask_x, mask_y)) {
data[y*lineLength+x] = qRgba(0, 0, 0, 0);
}
}
}
painter->drawImage(p.x(), p.y(), copied_stencil);
if(need_delete) {
painter->end();
delete painter;
}
}
QImage MaskBased::mask() const
{
return mask_;
}
void MaskBased::setMask(const QImage &mask)
{
if(mask.isNull()){
qDebug()<<"null mask";
return;
}
mask_ = mask.createAlphaMask();
makeStencil(color_);
}