-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloodfilltool.cpp
More file actions
112 lines (89 loc) · 2.81 KB
/
floodfilltool.cpp
File metadata and controls
112 lines (89 loc) · 2.81 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright 2017 SillyLossy.
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "floodfilltool.h"
#include <QStack>
#include <QPainter>
#include <QDebug>
#include <cmath>
#include <vector>
#include "paintarea.h"
const QString FloodFillTool::toolName()
{
static const QString floodFillName = "Flood fill";
return floodFillName;
}
const QString FloodFillTool::iconName()
{
static const QString floodFillIcon = "color";
return floodFillIcon;
}
void FloodFillTool::onMousePress(PaintEvent* event)
{
m_fillColor = event->pen.color();
imageHeight = event->selectedLayer->height();
imageWidth = event->selectedLayer->width();
floodFill(event);
}
void FloodFillTool::onMouseRelease(PaintEvent*)
{
}
void FloodFillTool::onMouseMove(PaintEvent*)
{
}
void FloodFillTool::floodFill(PaintEvent* event)
{
QImage* image = event->selectedLayer;
QPainter painter(image);
painter.setPen(m_fillColor);
QColor oldColor = image->pixelColor(event->currentPoint);
// The stack of pixels to fill
QStack<QPoint> pixels;
// Add the initial point
pixels.append(event->currentPoint);
const int rx[] = { -1, 0, +1, 0 };
const int ry[] = { 0, +1, 0, -1 };
int j = 0;
while (!pixels.empty()) {
QPoint pt = pixels.takeLast();
QPoint next;
for (int i = 0; i < 4; i++) {
next = pt;
next.rx() += rx[(i + j) % 4];
next.ry() += ry[(i + j) % 4];
if (valid(next) && canFill(image, next, oldColor)) {
painter.drawPoint(next);
pixels.append(next);
}
}
j++;
// no infinite loops
if (pixels.size() > imageWidth * imageHeight) {
break;
}
}
}
// Determine if the pixel should be filled
bool FloodFillTool::canFill(const QImage* image, const QPoint& pt, const QColor& oldColor)
{
QColor pixelColor = image->pixelColor(pt);
if (pixelColor == oldColor && pixelColor.alpha() <= m_fillColor.alpha()) {
return true;
}
return false;
}
// Only draw valid points as image may be larger than visible area
bool FloodFillTool::valid(const QPoint& pt)
{
return (pt.x() >= 0 && pt.x() < imageWidth && pt.y() >= 0 && pt.y() < imageHeight);
}