From c9a88909adcd2f6a6304fb48acd3ed402188b479 Mon Sep 17 00:00:00 2001 From: Mienek Date: Wed, 18 Dec 2019 19:41:11 +0100 Subject: [PATCH 1/2] FloodFill ist fertig --- src/Layer/PaintingArea.cpp | 3 +- src/Tool/IntelliToolFloodFill.cpp | 74 +++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/Layer/PaintingArea.cpp b/src/Layer/PaintingArea.cpp index af9be56..0275c00 100644 --- a/src/Layer/PaintingArea.cpp +++ b/src/Layer/PaintingArea.cpp @@ -15,11 +15,12 @@ #include "Tool/IntelliToolLine.h" #include "Tool/IntelliToolCircle.h" #include "Tool/IntelliToolRectangle.h" +#include "Tool/IntelliToolFloodFill.h" PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent) :QWidget(parent){ //test yout tool here and reset after accomplished test - this->Tool = new IntelliToolRectangle(this, &colorPicker); + this->Tool = new IntelliToolFloodFill(this, &colorPicker); this->setUp(maxWidth, maxHeight); //tetsing this->addLayer(200,200,0,0,ImageType::Shaped_Image); diff --git a/src/Tool/IntelliToolFloodFill.cpp b/src/Tool/IntelliToolFloodFill.cpp index 7712f90..e3b2c3c 100644 --- a/src/Tool/IntelliToolFloodFill.cpp +++ b/src/Tool/IntelliToolFloodFill.cpp @@ -2,6 +2,8 @@ #include "Layer/PaintingArea.h" #include "QColorDialog" #include "QInputDialog" +#include +#include IntelliToolFloodFill::IntelliToolFloodFill(PaintingArea* Area, IntelliColorPicker* colorPicker) :IntelliTool(Area, colorPicker){ @@ -22,11 +24,55 @@ void IntelliToolFloodFill::onMouseRightReleased(int x, int y){ void IntelliToolFloodFill::onMouseLeftPressed(int x, int y){ IntelliTool::onMouseLeftPressed(x,y); - this->Canvas->image->get - auto depthsearch = [](int x, int y, LayerObject* Canvas){ + QColor oldColor = this->Active->image->getPixelColor(x,y); + std::queue Q; + Q.push(QPoint(x,y)); + QColor newColor = this->colorPicker->getFirstColor(); + Canvas->image->drawPixel(QPoint(x,y),newColor); + while(!Q.empty()){ + QPoint Front = Q.front(); + Q.pop(); + if((Front.x()+1 < Canvas->width)&& + (Active->image->getPixelColor(Front.x()+1,Front.y()) == oldColor)&& + (Canvas->image->getPixelColor(Front.x()+1,Front.y()) != newColor)){ + Canvas->image->drawPixel(QPoint(Front.x()+1, Front.y()),newColor); + Q.push(QPoint(Front.x()+1,Front.y())); + } + if((Front.x()-1 >= 0)&& + (Active->image->getPixelColor(Front.x()-1,Front.y()) == oldColor)&& + (Canvas->image->getPixelColor(Front.x()-1,Front.y()) != newColor)){ + Canvas->image->drawPixel(QPoint(Front.x()-1, Front.y()),newColor); + Q.push(QPoint(Front.x()-1,Front.y())); + } + if((Front.y()+1 < Canvas->hight)&& + (Active->image->getPixelColor(Front.x(),Front.y()+1) == oldColor)&& + (Canvas->image->getPixelColor(Front.x(),Front.y()+1) != newColor)){ + Canvas->image->drawPixel(QPoint(Front.x(), Front.y()+1),newColor); + Q.push(QPoint(Front.x(),Front.y()+1)); + } + if((Front.y()-1 >= 0)&& + (Active->image->getPixelColor(Front.x(),Front.y()-1) == oldColor)&& + (Canvas->image->getPixelColor(Front.x(),Front.y()-1) != newColor)){ + Canvas->image->drawPixel(QPoint(Front.x(), Front.y()-1),newColor); + Q.push(QPoint(Front.x(),Front.y()-1)); + } + } - }; + /* std::function depthsearch = + [&depthsearch](int x, int y,LayerObject* Active, LayerObject* Canvas, QColor oldColor, QColor newColor){ + if((x >= 0) && (y >= 0) && (x < Canvas->width) && (y < Canvas->hight)){ + if(Active->image->getPixelColor(x,y) == oldColor){ + Canvas->image->drawPoint(QPoint(x,y),newColor,1); + depthsearch(x-1,y,Active,Canvas,oldColor,newColor); + depthsearch(x,y+1,Active,Canvas,oldColor,newColor); + depthsearch(x+1,y,Active,Canvas,oldColor,newColor); + depthsearch(x,y-1,Active,Canvas,oldColor,newColor); + } + } + }; + depthsearch(x,y,Active,Canvas,oldColor,this->colorPicker->getFirstColor()); + */ Canvas->image->calculateVisiblity(); @@ -38,29 +84,9 @@ void IntelliToolFloodFill::onMouseLeftReleased(int x, int y){ void IntelliToolFloodFill::onWheelScrolled(int value){ IntelliTool::onWheelScrolled(value); - this->lineWidth+=value; - if(this->lineWidth<=0){ - this->lineWidth=1; - } + } void IntelliToolFloodFill::onMouseMoved(int x, int y){ IntelliTool::onMouseMoved(x,y); - if(this->drawing){ - this->Canvas->image->drawPlain(Qt::transparent); - QPoint next(x,y); - switch(lineStyle){ - case LineStyle::SOLID_LINE: - this->Canvas->image->drawLine(start,next,colorPicker->getFirstColor(),lineWidth); - break; - case LineStyle::DOTTED_LINE: - QPoint p1 =start.x() <= next.x() ? start : next; - QPoint p2 =start.x() < next.x() ? next : start; - int m = (float)(p2.y()-p1.y())/(float)(p2.x()-p1.x())+0.5f; - int c = start.y()-start.x()*m; - - break; - } - } - IntelliTool::onMouseMoved(x,y); } From 2ceacff4ef3bf6cc2411c209f44fa556242896b4 Mon Sep 17 00:00:00 2001 From: Sonaion Date: Wed, 18 Dec 2019 20:37:38 +0100 Subject: [PATCH 2/2] Performance Update --- src/Image/IntelliImage.cpp | 4 +- src/Image/IntelliImage.h | 2 +- src/Tool/IntelliToolFloodFill.cpp | 67 ++++++++++++------------------- 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/Image/IntelliImage.cpp b/src/Image/IntelliImage.cpp index 36fce1f..fe1028b 100644 --- a/src/Image/IntelliImage.cpp +++ b/src/Image/IntelliImage.cpp @@ -78,6 +78,6 @@ void IntelliImage::drawPlain(const QColor& color){ imageData.fill(color); } -QColor IntelliImage::getPixelColor(int x, int y){ - return imageData.pixelColor(x,y); +QColor IntelliImage::getPixelColor(QPoint& point){ + return imageData.pixelColor(point); } diff --git a/src/Image/IntelliImage.h b/src/Image/IntelliImage.h index 9d37a31..d90f690 100644 --- a/src/Image/IntelliImage.h +++ b/src/Image/IntelliImage.h @@ -51,7 +51,7 @@ public: //loads an image to the ColorBuffer virtual bool loadImage(const QString &fileName); - virtual QColor getPixelColor(int x, int y); + virtual QColor getPixelColor(QPoint& point); }; #endif diff --git a/src/Tool/IntelliToolFloodFill.cpp b/src/Tool/IntelliToolFloodFill.cpp index e3b2c3c..6d655ea 100644 --- a/src/Tool/IntelliToolFloodFill.cpp +++ b/src/Tool/IntelliToolFloodFill.cpp @@ -24,58 +24,43 @@ void IntelliToolFloodFill::onMouseRightReleased(int x, int y){ void IntelliToolFloodFill::onMouseLeftPressed(int x, int y){ IntelliTool::onMouseLeftPressed(x,y); - QColor oldColor = this->Active->image->getPixelColor(x,y); + + QPoint start(x,y); std::queue Q; - Q.push(QPoint(x,y)); + Q.push(start); + + QColor oldColor = this->Active->image->getPixelColor(start); QColor newColor = this->colorPicker->getFirstColor(); - Canvas->image->drawPixel(QPoint(x,y),newColor); + Canvas->image->drawPixel(start,newColor); + + QPoint left, right, top, down; while(!Q.empty()){ - QPoint Front = Q.front(); + QPoint Current = Q.front(); Q.pop(); - if((Front.x()+1 < Canvas->width)&& - (Active->image->getPixelColor(Front.x()+1,Front.y()) == oldColor)&& - (Canvas->image->getPixelColor(Front.x()+1,Front.y()) != newColor)){ - Canvas->image->drawPixel(QPoint(Front.x()+1, Front.y()),newColor); - Q.push(QPoint(Front.x()+1,Front.y())); + + left = QPoint(Current.x()-1,Current.y() ); + right = QPoint(Current.x()+1,Current.y() ); + top = QPoint(Current.x() ,Current.y()-1); + down = QPoint(Current.x() ,Current.y()+1); + if((right.x() < Canvas->width) && (Canvas->image->getPixelColor(right) != newColor) && (Active->image->getPixelColor(right) == oldColor)){ + Canvas->image->drawPixel(right,newColor); + Q.push(right); } - if((Front.x()-1 >= 0)&& - (Active->image->getPixelColor(Front.x()-1,Front.y()) == oldColor)&& - (Canvas->image->getPixelColor(Front.x()-1,Front.y()) != newColor)){ - Canvas->image->drawPixel(QPoint(Front.x()-1, Front.y()),newColor); - Q.push(QPoint(Front.x()-1,Front.y())); + if((left.x() >= 0) && (Canvas->image->getPixelColor(left) != newColor) && (Active->image->getPixelColor(left) == oldColor)){ + Canvas->image->drawPixel(left,newColor); + Q.push(left); } - if((Front.y()+1 < Canvas->hight)&& - (Active->image->getPixelColor(Front.x(),Front.y()+1) == oldColor)&& - (Canvas->image->getPixelColor(Front.x(),Front.y()+1) != newColor)){ - Canvas->image->drawPixel(QPoint(Front.x(), Front.y()+1),newColor); - Q.push(QPoint(Front.x(),Front.y()+1)); + if((top.y() < Canvas->hight) && (Canvas->image->getPixelColor(top) != newColor) && (Active->image->getPixelColor(top) == oldColor)){ + Canvas->image->drawPixel(top,newColor); + Q.push(top); } - if((Front.y()-1 >= 0)&& - (Active->image->getPixelColor(Front.x(),Front.y()-1) == oldColor)&& - (Canvas->image->getPixelColor(Front.x(),Front.y()-1) != newColor)){ - Canvas->image->drawPixel(QPoint(Front.x(), Front.y()-1),newColor); - Q.push(QPoint(Front.x(),Front.y()-1)); + if((down.y() >= 0) && (Canvas->image->getPixelColor(down) != newColor) && (Active->image->getPixelColor(down) == oldColor)){ + Canvas->image->drawPixel(down,newColor); + Q.push(down); } } - - /* std::function depthsearch = - [&depthsearch](int x, int y,LayerObject* Active, LayerObject* Canvas, QColor oldColor, QColor newColor){ - if((x >= 0) && (y >= 0) && (x < Canvas->width) && (y < Canvas->hight)){ - if(Active->image->getPixelColor(x,y) == oldColor){ - Canvas->image->drawPoint(QPoint(x,y),newColor,1); - depthsearch(x-1,y,Active,Canvas,oldColor,newColor); - depthsearch(x,y+1,Active,Canvas,oldColor,newColor); - depthsearch(x+1,y,Active,Canvas,oldColor,newColor); - depthsearch(x,y-1,Active,Canvas,oldColor,newColor); - } - } - }; - depthsearch(x,y,Active,Canvas,oldColor,this->colorPicker->getFirstColor()); - */ Canvas->image->calculateVisiblity(); - - } void IntelliToolFloodFill::onMouseLeftReleased(int x, int y){