Bitmap/Flood fill: Difference between revisions

Line 722:
>insrgb(A);
>function floodfill (A0,i,j,color,dist) ...
$ A=A0;
$ R=getred(A); G=getgreen(A); B=getblue(A);
$ d=sqrt((R-R[i,j])^2+(G-G[i,j])^2+(B-B[i,j])^2);
$ n=rows(A); m=cols(A);
$ V=zeros(n,m);
$ S=zeros(n*m,2); sn=0;
$ A[i,j]=color; V[i,j]=1;
$ repeat;
$ if fill(A,i+1,j,n,m,d,dist,V,S,sn,color) then i=i+1; continue;
$ elseif fill(A,i,j+1,n,m,d,dist,V,S,sn,color) then j=j+1; continue;
$ elseif fill(A,i-1,j,n,m,d,dist,V,S,sn,color) then i=i-1; continue;
$ elseif fill(A,i,j-1,n,m,d,dist,V,S,sn,color) then j=j-1; continue;
$ endif;
$ sn=sn-1; if sn==0 then break; endif;
$ i=S[sn,1]; j=S[sn,2];
$ end;
$ return A;
$endfunction
>function fill (A,i,j,n,m,d,dist,V,S,%sn,color) ...
$ if i<1 or i>n or j<1 or j>n then return 0; endif;
$ if V[i,j] || d[i,j]>dist then A[i,j]=color; return 0; endif;
$ V[i,j]=1;
$ A[i,j]=color;
$ %sn=%sn+1;
$ S[%sn]=[i,j];
$ return 1;
$endfunction
>B=floodfill(A,10,240,rgb(0.5,0,0),0.5);
Anonymous user