Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could try converting it from a recursive function to an iterative function that uses an array instead of the program stack.</p> <p>Here's an iterative implementation of FloodFill as described in <a href="http://www.cs.nps.navy.mil/people/faculty/capps/iap/class1/fill/fill.html" rel="nofollow noreferrer">the article Allan mentioned</a>:</p> <pre><code>function floodFill(bmpData:BitmapData, startX:int, startY:int, fill:uint, old:uint):void { var queue:Array = new Array(); queue.push(new Point(startX, startY)); var iterations:uint = 0; var bmpWidth:int = bmpData.width; var bmpHeight:int = bmpData.height; var searchBmp:BitmapData = new BitmapData(bmpData.width, bmpData.height); var currPoint:Point, newPoint:Point; while (queue.length &gt; 0) { currPoint = queue.shift(); ++iterations; if (currPoint.x &lt; 0 || currPoint.x &gt;= bmpWidth) continue; if (currPoint.y &lt; 0 || currPoint.y &gt;= bmpHeight) continue; searchBmp.setPixel(currPoint.x, currPoint.y, 0x00); if (bmpData.getPixel(currPoint.x, currPoint.y) == old) { bmpData.setPixel(currPoint.x, currPoint.y, fill); if (searchBmp.getPixel(currPoint.x + 1, currPoint.y) == 0xFFFFFF) { queue.push(new Point(currPoint.x + 1, currPoint.y)); } if (searchBmp.getPixel(currPoint.x, currPoint.y + 1) == 0xFFFFFF) { queue.push(new Point(currPoint.x, currPoint.y + 1)); } if (searchBmp.getPixel(currPoint.x - 1, currPoint.y) == 0xFFFFFF) { queue.push(new Point(currPoint.x - 1, currPoint.y)); } if (searchBmp.getPixel(currPoint.x, currPoint.y - 1) == 0xFFFFFF) { queue.push(new Point(currPoint.x, currPoint.y - 1)); } } } trace("iterations: " + iterations); } </code></pre> <p><strong>Edit:</strong> After trying a couple of ways to keep track of which pixels have already been searched, I realized that using a second BitmapData instance works really well. I've updated the code accordingly.</p> <p>This code fills a 2000x1400 bitmap in ~14 seconds on my computer. For a better user experience, you should break up the fill processing over multiple frames, so the user doesn't have to stare at a waiting icon.</p> <p>Oh, and this still only affects one color, but it should be easy to modify it to take multiple colors, or a color threshold, or similar.</p> <p>Edit to add: The <a href="http://en.wikipedia.org/wiki/Flood_fill" rel="nofollow noreferrer">Flood fill</a> article on Wikipedia has several algorithm suggestions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload