FloodFill - best way?
BlitzPlus Forums/BlitzPlus Programming/FloodFill - best way?
| ||
Hy, I'm writing a Paint Program and I've to solve a very simple thing: the flood fill. But this isn't as easy as I thaught... I tried a recoursive function: 1. Get Pixelcolor. 2. if Color correct fill the pixel 3. if Color correct goto step 1 but with the pixel -left ,-above, -right, -below 4. end function It's a big time until the program reaches the Final End. There's the code: Global FillOnR,FillOnG,FillOnB,image=LoadImage("compiling.bmp") DrawImage image,0,0 : Text 100,4,"please wait" : Delay 100 Fill(30,30 ,255,140,140 ,0) DrawImage image,0,0 Repeat Until GetKey() End ;************************************************************************* Function Fill(X,Y,R,G,B,level) ;If Rand(0,1000)=1 Then DrawImage image,0,0:Delay 10 ;activate to see process If level>10000 Then Return ;safety If X<0 Or Y<0 Or X>ImageWidth(image)-1 Or Y>ImageHeight(image)-1 Then Return;outside of image col=ReadPixel(X,Y,ImageBuffer(image)) TheB=(col And 255) : TheG=(col Shr 8) And 255 : TheR=(col Shr 16) And 255 ;get the RGBcolors of position XY If level=0 Then FillOnR=TheR : FillOnG=TheG : FillOnB=TheB ;get the color on which we are painting If TheR=R And TheG=G And TheB=B Then Return ;don't fill red on red etc If TheR=FillOnR And TheG=FillOnG And TheB=FillOnB Then WritePixel(X,Y,R*256*256+G*256+B,ImageBuffer(image)) :Else Return ; actually fills the point in the image! Fill(X+1,Y,R,G,B,level+1) ; try to fill right one Fill(X-1,Y,R,G,B,level+1) ; try to fill left one Fill(X,Y+1,R,G,B,level+1) ; try to fill below Fill(X,Y-1,R,G,B,level+1) ; try to fill above End Function ;************************************************************************* The problems: -it is slow -sometimes it doesn't fill the whole thing because recoursive levels above 12000 cause Blitz to crash... I wonder how Microsoft Paint does this thing. I click and next moment a huge and complex floodfill is complete! So it looks like (with my little proggy): |
| ||
There is one in the Code Archives: Fast Flood Fill (ProPixel Code) by Snarty |
| ||
Thank you very much, Fredborg!:-) I'll try this out. |