All paint programs, independent of how simple or complex they are, come with a fill tool. This basically replaces the color of a closed region with another color. I know that there are different APIs to do this, but I am interested in the algorithm. What would be an efficient algorithm to implement this tool?
A couple of things I can think of quickly are:
- Convert image into a binary map, where pixels in the color to be replaced are
1and all other colors are0. - Find a closed region around the point you want to change such that all the pixels inside are 1 and all the neighbouring pixels are 0.
Many implementations are done as a recursive conquer and divide algorithm. If you do a quick google for ‘flood fill algorithm’ you will find plenty of resources including the excellent wikipedia page on the topic.