37 lines
1.1 KiB
Plaintext
37 lines
1.1 KiB
Plaintext
>file="test.png";
|
|
>A=loadrgb(file); ...
|
|
>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);
|
|
>B=floodfill(B,10,10,rgb(0.5,0,0),0.5);
|
|
>B=floodfill(B,150,60,rgb(0,0.5,0),0.5);
|
|
>B=floodfill(B,200,200,rgb(0,0,0.5),0.5);
|
|
>insrgb(B);
|