Text is just single layer fill so I do it in C++ like this:
int x,y,e;
int c0=0x00000000; // space color
int c1=0x00FFFFFF; // edge color
int co=0x00FF0000; // outside tmp color
int ci=0x000000FF; // inside tmp color
// grow/flood fill c0 neigbouring c1 with c2
#define fill(c0,c1,c2)\
for (e=1;e;)\
for (e=0,y=1;y<pic1.ys-1;y++)\
for ( x=1;x<pic1.xs-1;x++)\
if (pic1.p[y][x].dd==c0)\
if ((pic1.p[y-1][x].dd==c1)\
||(pic1.p[y+1][x].dd==c1)\
||(pic1.p[y][x-1].dd==c1)\
||(pic1.p[y][x+1].dd==c1)) { e=1; pic1.p[y][x].dd=c2; }
// copy data pic0 is source pic1 is output
pic1=pic0;
// 0. draw border rectangle for growth fill start with co
for (x=0 ,y=0;y<pic1.ys;y++) pic1.p[y][x].dd=co;
for (x=pic1.xs-1,y=0;y<pic1.ys;y++) pic1.p[y][x].dd=co;
for (x=0,y=0 ;x<pic1.xs;x++) pic1.p[y][x].dd=co;
for (x=0,y=pic1.ys-1;x<pic1.xs;x++) pic1.p[y][x].dd=co;
fill(c0,co,co); // 1. grow outer border
fill(c1,co,co); // 2. grow outer edges
fill(c0,co,ci); // 3. create outer fill edge
fill(c0,ci,ci); // 4. fill the inside
// 5. merge / recolor
for (y=0;y<pic1.ys;y++)
for (x=0;x<pic1.xs;x++)
{
e=c0;
if ((pic0.p[y][x].dd==c1)||(pic1.p[y][x].dd==ci)) e=c1;
pic1.p[y][x].dd=e;
}
#undef fill
Here are how the stages looks like:
Algorithm is like this:
- draw border rectangle for growth fill start with
co
(some unused color)
- grow outer border to nearest text edges with
co
- fill outer edges (they disappear) with
co
- create outer fill edge with
ci
(some unused color)
- fill the inside with
ci
merge / recolor source and result images
copy edges from source image and inside from result image the rest is space
I use my own picture class for images so some members are:
xs,ys
size of image in pixels
p[y][x].dd
is pixel at (x,y) position as 32 bit integer type
clear(color)
- clears entire image
resize(xs,ys)
- resizes image to new resolution
If you have special characters that have more layers
then just add more stages ... layer
is how many layers of space there can be from most inner space/hole to the outer space also you can loop until no more layer found ...