summaryrefslogtreecommitdiff
authorleseb <leseb>2002-06-27 08:42:50 (UTC)
committer leseb <leseb>2002-06-27 08:42:50 (UTC)
commit596749dc52cc89b5325f9089b79d0b9cc8240682 (patch) (unidiff)
tree7d400323444c6ea5c1bfb5d9c2f8dfb3a2a01435
parent4080b87bd8f9a76217ba8ae06f2a1b2ca9bcc285 (diff)
downloadopie-596749dc52cc89b5325f9089b79d0b9cc8240682.zip
opie-596749dc52cc89b5325f9089b79d0b9cc8240682.tar.gz
opie-596749dc52cc89b5325f9089b79d0b9cc8240682.tar.bz2
Try to take AA into account when filling
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/graphics/drawpad/filltool.cpp72
-rw-r--r--noncore/graphics/drawpad/filltool.h4
2 files changed, 75 insertions, 1 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
@@ -10,44 +10,62 @@
10 * (at your option) any later version. * 10 * (at your option) any later version. *
11 * * 11 * *
12 ***************************************************************************/ 12 ***************************************************************************/
13 13
14#include "filltool.h" 14#include "filltool.h"
15 15
16#include "drawpad.h" 16#include "drawpad.h"
17#include "drawpadcanvas.h" 17#include "drawpadcanvas.h"
18#include "page.h" 18#include "page.h"
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{
25} 27}
26 28
27FillTool::~FillTool() 29FillTool::~FillTool()
28{ 30{
29} 31}
30 32
31void FillTool::mousePressEvent(QMouseEvent* e) 33void FillTool::mousePressEvent(QMouseEvent* e)
32{ 34{
33 int x = e->x(); 35 int x = e->x();
34 int y = e->y(); 36 int y = e->y();
35 37
36 m_image = m_pDrawPadCanvas->currentPage()->convertToImage(); 38 m_image = m_pDrawPadCanvas->currentPage()->convertToImage();
37 m_fillRgb = m_pDrawPad->brush().color().rgb(); 39 m_fillRgb = m_pDrawPad->brush().color().rgb();
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) {
41 fillLine(x, y); 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 {
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();
45 63
46 m_pDrawPadCanvas->backupPage(); 64 m_pDrawPadCanvas->backupPage();
47 } 65 }
48} 66}
49 67
50void FillTool::mouseReleaseEvent(QMouseEvent* e) 68void FillTool::mouseReleaseEvent(QMouseEvent* e)
51{ 69{
52 Q_UNUSED(e) 70 Q_UNUSED(e)
53} 71}
@@ -79,12 +97,64 @@ void FillTool::fillLine(int x, int y)
79 } 97 }
80 98
81 for (int i = x1 + 1; i < x2; i++) { 99 for (int i = x1 + 1; i < x2; i++) {
82 fillLine(i, y - 1); 100 fillLine(i, y - 1);
83 } 101 }
84 102
85 for (int i = x1 + 1; i < x2; i++) { 103 for (int i = x1 + 1; i < x2; i++) {
86 fillLine(i, y + 1); 104 fillLine(i, y + 1);
87 } 105 }
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
@@ -21,19 +21,23 @@
21class FillTool : public Tool 21class FillTool : public Tool
22{ 22{
23public: 23public:
24 FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas); 24 FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas);
25 ~FillTool(); 25 ~FillTool();
26 26
27 void mousePressEvent(QMouseEvent* e); 27 void mousePressEvent(QMouseEvent* e);
28 void mouseReleaseEvent(QMouseEvent* e); 28 void mouseReleaseEvent(QMouseEvent* e);
29 void mouseMoveEvent(QMouseEvent* e); 29 void mouseMoveEvent(QMouseEvent* e);
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};
38 42
39#endif // FILLTOOL_H 43#endif // FILLTOOL_H