For example: we could pop items off the stack based on their line number, which reduces the number of items on the stack. Note : Since most programmers now work with bitmaps, instead of reading and writing directly to video, the first optimization above may be all that is required to make this a truly fast solid flood fill algorithm. Finally, we get to the QuickFill method of flood filling, which is a type of 4 way method, but is more efficient than the simple non-recursive method believe it or not.
Note : For the purposes of this article, I will be describing mostly the original QuickFill algorithm, since the included code does not use optimized scan, search, and line drawing functions remember, the original code directly accessed the video. As you can see from the code below, this is the most complicated method of flood filling. It was derived, indirectly, from the ideas presented in the simple non-recursive method. Note : While testing the port of the code to Windows, I discovered a gapping hole in the single visit code, patterns and masks could not be empty or the code would get stuck looping for ever hence the following.
In order to make this function more useful to Windows programmers, I added code to support flooding of image with a bitmap pattern. Since PushOpposite only reduces the number of lines revisited and does not prevent revisits, I had to add another linked list to keep track of which lines had already been visited. The PushOpposite function is still required as it still reduces the number of line revisits and, as a bonus, it reduces the number of items placed in the visited list.
To optimize the visited list, I decided to use visited blocks rectangles instead of visited lines, this serves to reduce the number of items needed in the list, which means less memory allocations. Note : In the following diagram, "Scanned" implies next line to be scanned, when line being pushed is popped off the stack. Note : In the following diagram, the blue arrowed lines represent the lines being pushed.
At the time that the QuickFill algorithm was created, I had seen only two types of flood fills used and wanted an algorithm that could handle both types. The first type used a border-color approach, which meant that the area that needed to be filled first had to be outlined with a border of a given color; this approach is the least desirable of the flood fills.
The second type used the color at the seed-point, which meant that only pixels of that color were filled; this approach is much more versatile, since it allows for multiple image objects of varying color to be contained in the area being filled.
Both of these flood fill types used a horizontal scan-line approach to solve the problem of flood filling an image. When trying to find information on how to implement a fast flood fill algorithm, I discovered that there was almost no information on the subject.
The only algorithms that I found were:. The first two methods had the advantage of being very compact, and very slow. The third method on the other hand, had the potential of being very fast and complicated. After placing the idea on the back burners for a while, since it was for my own personal graphics library and was not a priority, I had an epiphany.
I was having a cup of coffee at the local coffee shop and thinking about how I could solve the problem, when it all came together in my head. I started with the ideas used in the simple scan-line algorithm ref. Over the next nine days, I made incremental changes to the algorithm, each designed to increase the over all speed.
When I was finished, I had an algorithm that was not only fast, but faster than every implementation that I could find, except for the one implemented in the Fastgraph library by Ted Gruber written in assembly. Five months after the completion of the code, I finally went back and added the code necessary for single line visits so that the QuickFill function could also handle pattern fills. If you have not installed the Windows SDK from Microsoft, you will get some compilation errors when compiling in debug mode.
The code included with this article relies on the CDibData class to provide direct access to bitmap pixels. Besides, I have a personal paint program written using GDI and would like to add this code to it.
Note : The above is how I tested the recursive scan line code. Creating queue for bfs. Until queue is empty. Extracting front pair.
Popping front pair of queue. For Upside Pixel or Cell. For Downside Pixel or Cell. For Right side Pixel or Cell.
For Left side Pixel or Cell. This code is contributed by decode RemoveAt 0 ;. Next Count all possible paths from top left to bottom right of a mXn matrix. Recommended Articles. Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted.
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Graph. Most visited in Matrix. Program to find largest element in an array Matrix Chain Multiplication DP-8 Print a given matrix in spiral form Sudoku Backtracking-7 Python program to multiply two matrices.
We use cookies to ensure you have the best browsing experience on our website. Start Your Coding Journey Now!
0コメント