A Stackless Floodfill Automaton
Posted by andsoitis 3 days ago
Comments
Comment by hyperhello 1 day ago
Comment by kragen 1 day ago
If you were to instead use a bounded area of the heap with 4 bits per pixel, as you are suggesting, your bound would be 393216 bytes.
However, it's not totally clear that that's the right way to account for it, because if you have a 256-color image that you have to reduce to 240 colors to have space for this algorithm, there isn't an obvious way to get back the lost colors by storing 9154 bytes somewhere else. You might be able to do something like pick the least popular 16 colors from the image (which occupy a maximum of 49152 pixels and usually less), store a bitmap telling where they were (98304 bytes), store the colors of the marked pixels into a 24576-byte array, replace them with some neutral color, run the flood fill, and then restore the colors. But, while that uses significantly less than 393216 bytes, it still uses a lot more than 9154 bytes, and it's probably pretty slow on the kind of computer where you'd be trying to do something like this.
At the other extreme, if your 256-color image actually only used the 216 "web-safe" colors (#000, #003, #006, ... #030, #033, etc.), you'd already have 16 leftover colors available. And freeing up 16 colors on a 16-bit or 24-bit TrueColor display would damage the image quality only imperceptibly.
Comment by hyperhello 1 day ago
Comment by Jtsummers 1 day ago
https://t3x.org/#wyorc - One example, this is a book on writing a compiler targeting Z80 processors.
Comment by taneq 1 day ago
I don’t think they explain the backtracking part very well, and they should have used an example that fills a non-rectangular area so it actually demonstrates the whole algorithm.
It’s a neat trick if you have spare values to use as markers, though. :)
Comment by zimpenfish 1 day ago
I'll be converting my code to the stackless version toot sweet.
Comment by brcmthrowaway 1 day ago
Comment by zimpenfish 1 day ago
Comment by kragen 1 day ago
Comment by zimpenfish 1 day ago
Comment by kragen 1 day ago
Comment by cyberax 1 day ago
So I needed a way to detect that, and doing a floodfill from the edge of the image turned out to be the easiest way.
Comment by voidUpdate 1 day ago
Comment by Jtsummers 1 day ago
Basically, a pixel will go through states like:
(U,R) ;; came from up, went right
(U,D) ;; came from up, went down
(U,L) ;; came from up, went left
(U,U) ;; came from up, went up
Each of the neighboring pixels will be checked (that last one is superfluous and could be skipped for efficiency). Once all have been checked the pixel will be filled, and the current pixel will be set by using its predecessor direction.Comment by hatthew 1 day ago
If the colors in your original data are important, then you can't really just overwrite them and/or assume that the algorithm can use its own colors. You're going to have to add a stack.
If your original data doesn't have colors, adding a stack is better than integrating metadata into the original data structure.
If for some reason your original data has colors but they aren't considered important, then just remove the colors and add a stack.
If your concern is about dynamic memory, preallocate static memory equal to the size of the original data structure.
Comment by Jtsummers 1 day ago
Constant memory usage. It uses a small amount of additional memory (tracking the current pixel, pixel value, and a couple temp variables used for calculations) over the image itself making it a very low overhead algorithm. You can basically get away with 5 more variables, 20 more bytes if they're all 4-byte values, using this approach compared to the much greater memory needs of a stack-based approach (either a stack data structure or recursive). And actually just 4 variables since the D and P variables don't overlap in lifetime.
EDIT: Light editing of the above for grammar and clarity.
Comment by hatthew 1 day ago
Comment by fatlotus 1 day ago
Comment by kragen 1 day ago
Comment by amelius 1 day ago
Comment by Jtsummers 1 day ago
Comment by NooneAtAll5 1 day ago