Let's assume:
- We have a B&W image with closed loops (lines are 1px thick)
- If we travel on the image, each time we cross a line we change area type (inside or outside)
- The top left pixel (0,0) is defined as outside and it has no line crossing it
- Lines are black the rest is white
I want to fill all the inside areas in the most efficient way (I am working with very large images).
For example Before:
and After:


