summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/graphics/drawpad/filltool.cpp5
1 files changed, 5 insertions, 0 deletions
diff --git a/noncore/graphics/drawpad/filltool.cpp b/noncore/graphics/drawpad/filltool.cpp
index 2a39d04..b47aa60 100644
--- a/noncore/graphics/drawpad/filltool.cpp
+++ b/noncore/graphics/drawpad/filltool.cpp
@@ -1,160 +1,165 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * DrawPad - a drawing program for Opie Environment * 3 * DrawPad - a drawing program for Opie Environment *
4 * * 4 * *
5 * (C) 2002 by S. Prud'homme <prudhomme@laposte.net> * 5 * (C) 2002 by S. Prud'homme <prudhomme@laposte.net> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or * 9 * the Free Software Foundation; either version 2 of the License, or *
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; 22const int FILL_THRESHOLD = 65536;
23 23
24FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas) 24FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas)
25 : Tool(drawPad, drawPadCanvas) 25 : Tool(drawPad, drawPadCanvas)
26{ 26{
27} 27}
28 28
29FillTool::~FillTool() 29FillTool::~FillTool()
30{ 30{
31} 31}
32 32
33void FillTool::mousePressEvent(QMouseEvent* e) 33void FillTool::mousePressEvent(QMouseEvent* e)
34{ 34{
35 int x = e->x(); 35 int x = e->x();
36 int y = e->y(); 36 int y = e->y();
37 37
38 m_image = m_pDrawPadCanvas->currentPage()->pixmap()->convertToImage(); 38 m_image = m_pDrawPadCanvas->currentPage()->pixmap()->convertToImage();
39
40 if (m_image.depth() <= 8) {
41 m_image = m_image.convertDepth(32);
42 }
43
39 m_fillRgb = m_pDrawPad->brush().color().rgb(); 44 m_fillRgb = m_pDrawPad->brush().color().rgb();
40 m_oldRgb = m_image.pixel(x, y); 45 m_oldRgb = m_image.pixel(x, y);
41 46
42 if (m_oldRgb != m_fillRgb) { 47 if (m_oldRgb != m_fillRgb) {
43 m_pDrawPadCanvas->backupPage(); 48 m_pDrawPadCanvas->backupPage();
44 49
45 if (m_pDrawPad->antiAliasing()) { 50 if (m_pDrawPad->antiAliasing()) {
46 m_mask.create(m_image.width(), m_image.height(), 8, 2); 51 m_mask.create(m_image.width(), m_image.height(), 8, 2);
47 m_mask.fill(0); 52 m_mask.fill(0);
48 53
49 fillMaskLine(x, y); 54 fillMaskLine(x, y);
50 55
51 for (int i = 0; i < m_image.width(); i++) { 56 for (int i = 0; i < m_image.width(); i++) {
52 for (int j = 0; j < m_image.height(); j++) { 57 for (int j = 0; j < m_image.height(); j++) {
53 if (m_mask.pixelIndex(i, j) == 1) { 58 if (m_mask.pixelIndex(i, j) == 1) {
54 setInterpolatedPixel(i, j); 59 setInterpolatedPixel(i, j);
55 } 60 }
56 } 61 }
57 } 62 }
58 63
59 } else { 64 } else {
60 fillLine(x, y); 65 fillLine(x, y);
61 } 66 }
62 67
63 m_pDrawPadCanvas->currentPage()->pixmap()->convertFromImage(m_image); 68 m_pDrawPadCanvas->currentPage()->pixmap()->convertFromImage(m_image);
64 m_pDrawPadCanvas->viewport()->update(); 69 m_pDrawPadCanvas->viewport()->update();
65 } 70 }
66} 71}
67 72
68void FillTool::mouseReleaseEvent(QMouseEvent* e) 73void FillTool::mouseReleaseEvent(QMouseEvent* e)
69{ 74{
70 Q_UNUSED(e) 75 Q_UNUSED(e)
71} 76}
72 77
73void FillTool::mouseMoveEvent(QMouseEvent* e) 78void FillTool::mouseMoveEvent(QMouseEvent* e)
74{ 79{
75 Q_UNUSED(e) 80 Q_UNUSED(e)
76} 81}
77 82
78void FillTool::fillLine(int x, int y) 83void FillTool::fillLine(int x, int y)
79{ 84{
80 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) { 85 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) {
81 if (m_image.pixel(x, y) == m_oldRgb) { 86 if (m_image.pixel(x, y) == m_oldRgb) {
82 int x1, x2; 87 int x1, x2;
83 88
84 x1 = x - 1; 89 x1 = x - 1;
85 x2 = x + 1; 90 x2 = x + 1;
86 91
87 while ((x1 >= 0) && (m_image.pixel(x1, y) == m_oldRgb)) { 92 while ((x1 >= 0) && (m_image.pixel(x1, y) == m_oldRgb)) {
88 x1--; 93 x1--;
89 } 94 }
90 95
91 while ((x2 < m_image.width()) && (m_image.pixel(x2, y) == m_oldRgb)) { 96 while ((x2 < m_image.width()) && (m_image.pixel(x2, y) == m_oldRgb)) {
92 x2++; 97 x2++;
93 } 98 }
94 99
95 for (int i = x1 + 1; i < x2; i++) { 100 for (int i = x1 + 1; i < x2; i++) {
96 m_image.setPixel(i, y, m_fillRgb); 101 m_image.setPixel(i, y, m_fillRgb);
97 } 102 }
98 103
99 for (int i = x1 + 1; i < x2; i++) { 104 for (int i = x1 + 1; i < x2; i++) {
100 fillLine(i, y - 1); 105 fillLine(i, y - 1);
101 } 106 }
102 107
103 for (int i = x1 + 1; i < x2; i++) { 108 for (int i = x1 + 1; i < x2; i++) {
104 fillLine(i, y + 1); 109 fillLine(i, y + 1);
105 } 110 }
106 } 111 }
107 } 112 }
108} 113}
109 114
110void FillTool::fillMaskLine(int x, int y) 115void FillTool::fillMaskLine(int x, int y)
111{ 116{
112 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) { 117 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) {
113 if (m_mask.pixelIndex(x, y) == 0) { 118 if (m_mask.pixelIndex(x, y) == 0) {
114 if (rgbDistance(m_image.pixel(x, y), m_oldRgb) < FILL_THRESHOLD) { 119 if (rgbDistance(m_image.pixel(x, y), m_oldRgb) < FILL_THRESHOLD) {
115 int x1, x2; 120 int x1, x2;
116 121
117 x1 = x - 1; 122 x1 = x - 1;
118 x2 = x + 1; 123 x2 = x + 1;
119 124
120 while ((x1 >= 0) && (rgbDistance(m_image.pixel(x1, y), m_oldRgb) < FILL_THRESHOLD)) { 125 while ((x1 >= 0) && (rgbDistance(m_image.pixel(x1, y), m_oldRgb) < FILL_THRESHOLD)) {
121 x1--; 126 x1--;
122 } 127 }
123 128
124 while ((x2 < m_image.width()) && (rgbDistance(m_image.pixel(x2, y), m_oldRgb) < FILL_THRESHOLD)) { 129 while ((x2 < m_image.width()) && (rgbDistance(m_image.pixel(x2, y), m_oldRgb) < FILL_THRESHOLD)) {
125 x2++; 130 x2++;
126 } 131 }
127 132
128 for (int i = x1 + 1; i < x2; i++) { 133 for (int i = x1 + 1; i < x2; i++) {
129 m_mask.setPixel(i, y, 1); 134 m_mask.setPixel(i, y, 1);
130 } 135 }
131 136
132 for (int i = x1 + 1; i < x2; i++) { 137 for (int i = x1 + 1; i < x2; i++) {
133 fillMaskLine(i, y - 1); 138 fillMaskLine(i, y - 1);
134 } 139 }
135 140
136 for (int i = x1 + 1; i < x2; i++) { 141 for (int i = x1 + 1; i < x2; i++) {
137 fillMaskLine(i, y + 1); 142 fillMaskLine(i, y + 1);
138 } 143 }
139 } 144 }
140 } 145 }
141 } 146 }
142} 147}
143 148
144void FillTool::setInterpolatedPixel(int x, int y) 149void FillTool::setInterpolatedPixel(int x, int y)
145{ 150{
146 int fillRed = QMIN(QMAX(qRed(m_fillRgb) + qRed(m_image.pixel(x, y)) - qRed(m_oldRgb), 0), 255); 151 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); 152 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); 153 int fillBlue = QMIN(QMAX(qBlue(m_fillRgb) + qBlue(m_image.pixel(x, y)) - qBlue(m_oldRgb), 0), 255);
149 154
150 m_image.setPixel(x, y, qRgb(fillRed, fillGreen, fillBlue)); 155 m_image.setPixel(x, y, qRgb(fillRed, fillGreen, fillBlue));
151} 156}
152 157
153int FillTool::rgbDistance(QRgb rgb1, QRgb rgb2) 158int FillTool::rgbDistance(QRgb rgb1, QRgb rgb2)
154{ 159{
155 int redDistance = qRed(rgb2) - qRed(rgb1); 160 int redDistance = qRed(rgb2) - qRed(rgb1);
156 int greenDistance = qGreen(rgb2) - qGreen(rgb1); 161 int greenDistance = qGreen(rgb2) - qGreen(rgb1);
157 int blueDistance = qBlue(rgb2) - qBlue(rgb1); 162 int blueDistance = qBlue(rgb2) - qBlue(rgb1);
158 163
159 return (redDistance * redDistance + greenDistance * greenDistance + blueDistance * blueDistance); 164 return (redDistance * redDistance + greenDistance * greenDistance + blueDistance * blueDistance);
160} 165}