www.pudn.com > src.rar > inpaint.m, change:2010-07-20,size:4126b


function result=inpaint(texfill,dmfill,crackfill) 
 
result=texfill; 
 
for y=1:size(texfill,1)  
    for x=1:size(texfill,2) 
        if(crackfill(y,x)) 
            color=zeros(8,3); 
            dm=[-1;-1;-1;-1;-1;-1;-1;-1]; 
            searchrange=zeros(8,1); 
 
            %search right 
            i=1; 
            while(i<=size(texfill,2)-x) 
                if(crackfill(y,x+i)) 
                    i=i+1; 
                else 
                   color(1,:)=texfill(y,x+i,:); 
                   dm(1)=dmfill(y,x+i,:); 
                   break; 
                end 
            end             
            searchrange(1)=i; 
             
            %right-up 
            i=1; 
            while(i<=size(texfill,2)-x && i<=y-1) 
                if(crackfill(y-i,x+i)) 
                    i=i+1; 
                else 
                    color(2,:)=texfill(y-i,x+i,:); 
                    dm(2)=dmfill(y-i,x+i,:); 
                    break; 
                end 
            end 
            searchrange(2)=i; 
             
            %up 
            i=1; 
            while(i<=y-1) 
                if(crackfill(y-i,x)) 
                    i=i+1; 
                else 
                   color(3,:)=texfill(y-i,x,:); 
                   dm(3)=dmfill(y-i,x,:); 
                   break; 
                end 
            end 
            searchrange(3)=i; 
             
            %leftup 
            i=1; 
            while(i<=x-1 && i<=y-1) 
                if(crackfill(y-i,x-i)) 
                    i=i+1; 
                else 
                    color(4,:)=texfill(y-i,x-i,:); 
                    dm(4)=dmfill(y-i,x-i,:); 
                    break; 
                end 
            end 
            searchrange(4)=i; 
             
            %left 
            i=1; 
            while(i<=x-1) 
                if(crackfill(y,x-i)) 
                    i=i+1; 
                else 
                   color(5,:)=texfill(y,x-i,:); 
                   dm(5)=dmfill(y,x-i,:); 
                   break; 
                end 
            end 
            searchrange(5)=i; 
             
            %leftdown 
            i=1; 
            while(i<=x-1 && i<=size(texfill,1)-y) 
                if(crackfill(y+i,x-i)) 
                    i=i+1; 
                else 
                    color(6,:)=texfill(y+i,x-i,:); 
                    dm(6)=dmfill(y+i,x-i,:); 
                    break; 
                end 
            end 
            searchrange(6)=i; 
             
            %down 
            i=1; 
            while(i<=size(texfill,1)-y) 
                if(crackfill(y+i,x)) 
                    i=i+1; 
                else 
                   color(7,:)=texfill(y+i,x,:); 
                   dm(7)=dmfill(y+i,x,:); 
                   break; 
                end 
            end 
            searchrange(7)=i; 
             
            %rightdown 
            i=1; 
            while(i<=size(texfill,2)-x && i<=size(texfill,1)-y) 
                if(crackfill(y+i,x+i)) 
                    i=i+1; 
                else 
                    color(8,:)=texfill(y+i,x+i,:); 
                    dm(8)=dmfill(y+i,x+i,:); 
                    break; 
                end 
            end 
            searchrange(8)=i; 
             
            %---------------------------------------------     
 
            if(dm(5)==-1) 
                result(y,x,:)=color(1,:); 
            elseif(dm(1)==-1) 
                result(y,x,:)=color(5,:); 
            else 
                sum=zeros(1,3); 
                rsum=0; 
                 
                if(dm(1)>dm(5)) 
                    threshold=dm(1); 
                else 
                    threshold=dm(5); 
                end 
                 
                for i=1:8 
                    if(dm(i)>=0 && dm(i)<threshold) 
                        sum=sum+ color(i,:)/(searchrange(i)*searchrange(i)); 
                        rsum=rsum+1/(searchrange(i)*searchrange(i)); 
                    end 
                end 
                result(y,x,:)=sum/rsum; 
            end 
        end 
    end 
end