00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "filltool.h"
00015
00016 #include "drawpad.h"
00017 #include "drawpadcanvas.h"
00018 #include "page.h"
00019
00020
00021 const int FILL_THRESHOLD = 65536;
00022
00023 FillTool::FillTool(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas)
00024 : Tool(drawPad, drawPadCanvas)
00025 {
00026 }
00027
00028 FillTool::~FillTool()
00029 {
00030 }
00031
00032 void FillTool::mousePressEvent(QMouseEvent* e)
00033 {
00034 int x = e->x();
00035 int y = e->y();
00036
00037 m_image = m_pDrawPadCanvas->currentPage()->pixmap()->convertToImage();
00038
00039 if (m_image.depth() <= 8) {
00040 m_image = m_image.convertDepth(32);
00041 }
00042
00043 m_fillRgb = m_pDrawPad->brush().color().rgb();
00044 m_oldRgb = m_image.pixel(x, y);
00045
00046 if (m_oldRgb != m_fillRgb) {
00047 m_pDrawPadCanvas->backupPage();
00048
00049 if (m_pDrawPad->antiAliasing()) {
00050 m_mask.create(m_image.width(), m_image.height(), 8, 2);
00051 m_mask.fill(0);
00052
00053 fillMaskLine(x, y);
00054
00055 for (int i = 0; i < m_image.width(); i++) {
00056 for (int j = 0; j < m_image.height(); j++) {
00057 if (m_mask.pixelIndex(i, j) == 1) {
00058 setInterpolatedPixel(i, j);
00059 }
00060 }
00061 }
00062
00063 } else {
00064 fillLine(x, y);
00065 }
00066
00067 m_pDrawPadCanvas->currentPage()->pixmap()->convertFromImage(m_image);
00068 m_pDrawPadCanvas->viewport()->update();
00069 }
00070 }
00071
00072 void FillTool::mouseReleaseEvent(QMouseEvent* e)
00073 {
00074 Q_UNUSED(e)
00075 }
00076
00077 void FillTool::mouseMoveEvent(QMouseEvent* e)
00078 {
00079 Q_UNUSED(e)
00080 }
00081
00082 void FillTool::fillLine(int x, int y)
00083 {
00084 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) {
00085 if (m_image.pixel(x, y) == m_oldRgb) {
00086 int x1, x2;
00087
00088 x1 = x - 1;
00089 x2 = x + 1;
00090
00091 while ((x1 >= 0) && (m_image.pixel(x1, y) == m_oldRgb)) {
00092 x1--;
00093 }
00094
00095 while ((x2 < m_image.width()) && (m_image.pixel(x2, y) == m_oldRgb)) {
00096 x2++;
00097 }
00098
00099 for (int i = x1 + 1; i < x2; i++) {
00100 m_image.setPixel(i, y, m_fillRgb);
00101 }
00102
00103 for (int i = x1 + 1; i < x2; i++) {
00104 fillLine(i, y - 1);
00105 }
00106
00107 for (int i = x1 + 1; i < x2; i++) {
00108 fillLine(i, y + 1);
00109 }
00110 }
00111 }
00112 }
00113
00114 void FillTool::fillMaskLine(int x, int y)
00115 {
00116 if ((x >= 0) && (x < m_image.width()) && (y >= 0) && (y < m_image.height())) {
00117 if (m_mask.pixelIndex(x, y) == 0) {
00118 if (rgbDistance(m_image.pixel(x, y), m_oldRgb) < FILL_THRESHOLD) {
00119 int x1, x2;
00120
00121 x1 = x - 1;
00122 x2 = x + 1;
00123
00124 while ((x1 >= 0) && (rgbDistance(m_image.pixel(x1, y), m_oldRgb) < FILL_THRESHOLD)) {
00125 x1--;
00126 }
00127
00128 while ((x2 < m_image.width()) && (rgbDistance(m_image.pixel(x2, y), m_oldRgb) < FILL_THRESHOLD)) {
00129 x2++;
00130 }
00131
00132 for (int i = x1 + 1; i < x2; i++) {
00133 m_mask.setPixel(i, y, 1);
00134 }
00135
00136 for (int i = x1 + 1; i < x2; i++) {
00137 fillMaskLine(i, y - 1);
00138 }
00139
00140 for (int i = x1 + 1; i < x2; i++) {
00141 fillMaskLine(i, y + 1);
00142 }
00143 }
00144 }
00145 }
00146 }
00147
00148 void FillTool::setInterpolatedPixel(int x, int y)
00149 {
00150 int fillRed = QMIN(QMAX(qRed(m_fillRgb) + qRed(m_image.pixel(x, y)) - qRed(m_oldRgb), 0), 255);
00151 int fillGreen = QMIN(QMAX(qGreen(m_fillRgb) + qGreen(m_image.pixel(x, y)) - qGreen(m_oldRgb), 0), 255);
00152 int fillBlue = QMIN(QMAX(qBlue(m_fillRgb) + qBlue(m_image.pixel(x, y)) - qBlue(m_oldRgb), 0), 255);
00153
00154 m_image.setPixel(x, y, qRgb(fillRed, fillGreen, fillBlue));
00155 }
00156
00157 int FillTool::rgbDistance(QRgb rgb1, QRgb rgb2)
00158 {
00159 int redDistance = qRed(rgb2) - qRed(rgb1);
00160 int greenDistance = qGreen(rgb2) - qGreen(rgb1);
00161 int blueDistance = qBlue(rgb2) - qBlue(rgb1);
00162
00163 return (redDistance * redDistance + greenDistance * greenDistance + blueDistance * blueDistance);
00164 }