summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--noncore/graphics/drawpad/filltool.cpp70
-rw-r--r--noncore/graphics/drawpad/filltool.h4
2 files changed, 74 insertions, 0 deletions
diff --git a/noncore/graphics/drawpad/filltool.cpp b/noncore/graphics/drawpad/filltool.cpp
index d323cc2..0177e1c 100644
--- a/noncore/graphics/drawpad/filltool.cpp
+++ b/noncore/graphics/drawpad/filltool.cpp
@@ -19,6 +19,8 @@
19 19
20#include <qimage.h> 20#include <qimage.h>
21 21
22const int FILL_THRESHOLD = 65536;
23
22FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas) 24FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas)
23 : Tool(drawPad, drawPadCanvas) 25 : Tool(drawPad, drawPadCanvas)
24{ 26{
@@ -38,7 +40,23 @@ void FillTool::mousePressEvent(QMouseEvent* e)
38 m_oldRgb = m_image.pixel(x, y); 40 m_oldRgb = m_image.pixel(x, y);
39 41
40 if (m_oldRgb != m_fillRgb) { 42 if (m_oldRgb != m_fillRgb) {
43 if (m_pDrawPad->antiAliasing()) {
44 m_mask.create(m_image.width(), m_image.height(), 8, 2);
45 m_mask.fill(0);
46
47 fillMaskLine(x, y);
48
49 for (int i = 0; i < m_image.width(); i++) {
50 for (int j = 0; j < m_image.height(); j++) {
51 if (m_mask.pixelIndex(i, j) == 1) {
52 setInterpolatedPixel(i, j);
53 }
54 }
55 }
56
57 } else {
41 fillLine(x, y); 58 fillLine(x, y);
59 }
42 60
43 m_pDrawPadCanvas->currentPage()->convertFromImage(m_image); 61 m_pDrawPadCanvas->currentPage()->convertFromImage(m_image);
44 m_pDrawPadCanvas->viewport()->update(); 62 m_pDrawPadCanvas->viewport()->update();
@@ -88,3 +106,55 @@ void FillTool::fillLine(int x, int y)
88 } 106 }
89 } 107 }
90} 108}
109
110void FillTool::fillMaskLine(int x, int y)
111{
112 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) {
113 if (m_mask.pixelIndex(x, y) == 0) {
114 if (rgbDistance(m_image.pixel(x, y), m_oldRgb) < FILL_THRESHOLD) {
115 int x1, x2;
116
117 x1 = x - 1;
118 x2 = x + 1;
119
120 while ((x1 >= 0) && (rgbDistance(m_image.pixel(x1, y), m_oldRgb) < FILL_THRESHOLD)) {
121 x1--;
122 }
123
124 while ((x2 < m_image.width()) && (rgbDistance(m_image.pixel(x2, y), m_oldRgb) < FILL_THRESHOLD)) {
125 x2++;
126 }
127
128 for (int i = x1 + 1; i < x2; i++) {
129 m_mask.setPixel(i, y, 1);
130 }
131
132 for (int i = x1 + 1; i < x2; i++) {
133 fillMaskLine(i, y - 1);
134 }
135
136 for (int i = x1 + 1; i < x2; i++) {
137 fillMaskLine(i, y + 1);
138 }
139 }
140 }
141 }
142}
143
144void FillTool::setInterpolatedPixel(int x, int y)
145{
146 int fillRed = QMIN(QMAX(qRed(m_fillRgb) + qRed(m_image.pixel(x, y)) - qRed(m_oldRgb), 0), 255);
147 int fillGreen = QMIN(QMAX(qGreen(m_fillRgb) + qGreen(m_image.pixel(x, y)) - qGreen(m_oldRgb), 0), 255);
148 int fillBlue = QMIN(QMAX(qBlue(m_fillRgb) + qBlue(m_image.pixel(x, y)) - qBlue(m_oldRgb), 0), 255);
149
150 m_image.setPixel(x, y, qRgb(fillRed, fillGreen, fillBlue));
151}
152
153int FillTool::rgbDistance(QRgb rgb1, QRgb rgb2)
154{
155 int redDistance = qRed(rgb2) - qRed(rgb1);
156 int greenDistance = qGreen(rgb2) - qGreen(rgb1);
157 int blueDistance = qBlue(rgb2) - qBlue(rgb1);
158
159 return (redDistance * redDistance + greenDistance * greenDistance + blueDistance * blueDistance);
160}
diff --git a/noncore/graphics/drawpad/filltool.h b/noncore/graphics/drawpad/filltool.h
index eaf9a27..03ee489 100644
--- a/noncore/graphics/drawpad/filltool.h
+++ b/noncore/graphics/drawpad/filltool.h
@@ -30,8 +30,12 @@ public:
30 30
31private: 31private:
32 void fillLine(int x, int y); 32 void fillLine(int x, int y);
33 void fillMaskLine(int x, int y);
34 void setInterpolatedPixel(int x, int y);
35 int rgbDistance(QRgb rgb1, QRgb rgb2);
33 36
34 QImage m_image; 37 QImage m_image;
38 QImage m_mask;
35 QRgb m_fillRgb; 39 QRgb m_fillRgb;
36 QRgb m_oldRgb; 40 QRgb m_oldRgb;
37}; 41};