author | leseb <leseb> | 2002-06-27 08:42:50 (UTC) |
---|---|---|
committer | leseb <leseb> | 2002-06-27 08:42:50 (UTC) |
commit | 596749dc52cc89b5325f9089b79d0b9cc8240682 (patch) (unidiff) | |
tree | 7d400323444c6ea5c1bfb5d9c2f8dfb3a2a01435 | |
parent | 4080b87bd8f9a76217ba8ae06f2a1b2ca9bcc285 (diff) | |
download | opie-596749dc52cc89b5325f9089b79d0b9cc8240682.zip opie-596749dc52cc89b5325f9089b79d0b9cc8240682.tar.gz opie-596749dc52cc89b5325f9089b79d0b9cc8240682.tar.bz2 |
Try to take AA into account when filling
-rw-r--r-- | noncore/graphics/drawpad/filltool.cpp | 72 | ||||
-rw-r--r-- | noncore/graphics/drawpad/filltool.h | 4 |
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 | |||
@@ -16,12 +16,14 @@ | |||
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 | ||
22 | const int FILL_THRESHOLD = 65536; | ||
23 | |||
22 | FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas) | 24 | FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas) |
23 | : Tool(drawPad, drawPadCanvas) | 25 | : Tool(drawPad, drawPadCanvas) |
24 | { | 26 | { |
25 | } | 27 | } |
26 | 28 | ||
27 | FillTool::~FillTool() | 29 | FillTool::~FillTool() |
@@ -35,13 +37,29 @@ void FillTool::mousePressEvent(QMouseEvent* e) | |||
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 | } |
@@ -85,6 +103,58 @@ void FillTool::fillLine(int x, int y) | |||
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 | |||
110 | void 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 | |||
144 | void 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 | |||
153 | int 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 | |||
@@ -27,13 +27,17 @@ public: | |||
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 | ||
31 | private: | 31 | private: |
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 |