package yi.iu.processor.symmetry;

import java.awt.*;
import java.util.Vector;
import yi.util.io.OutputResult;
import yi.iu.processor.contour.*;

public class HorizontalLineSymCal extends ContourBasedSymProcessor
{
    //static int skewAllowed=100;     // lenth of horizontal line/skew allowed vertical 
    static int ALLOWONESKEW=20;
    
    public HorizontalLineSymCal() {super();}
    public HorizontalLineSymCal(String edgeDectorName) {super(edgeDectorName); }
    
    public int[] getProcessed(int source[], Dimension d, int maxwidth)
    {
        yi.iu.processor.ImageProcessor ip= null;
        
        try {
	        Class ipc= Class.forName(edgeDector);
	        ip= (yi.iu.processor.ImageProcessor)ipc.newInstance();
	    } catch (Exception e) {
	        System.err.println("Can't found that edge dector!");
	        System.exit(0);
	    }

        int[] contour= ip.getProcessed(source, d);
        
        //mdContourExtractor mdce= new mdContourExtractor();
        //int[] contour= mdce.getProcessed(source, d);
        int width=d.width, height=d.height;
        int[] rst= new int[width];
        for (int i=0;i<height;i++)
            for (int j=0;j<width;j++) {
                int nowl= i, nowr=i;;     // the current line height
                for (int k=1;k<width;k++) {
                    if (k*2+1>maxwidth) break;
                    int l=j-k; if (l<0) {k=width;continue;}
                    int r=j+k; if (r>=width) {k=width;continue;}
/*                    
                    if (contour[nowl*width+l]!=0){;}
                    else if (nowl>0&&contour[(nowl-1)*width+l]!=0){nowl--;}
                    else if (nowl<height-1&&contour[(nowl+1)*width+l]!=0){nowl++;}
                    else {k=width; continue;}
                    if (contour[nowr*width+r]!=0){;}
                    else if (nowr>0&&contour[(nowr-1)*width+r]!=0){nowr--;}
                    else if (nowr<height-1&&contour[(nowr+1)*width+r]!=0){nowr++;}
                    else {k=width; continue;}
                    if (nowl<i-2||nowl>i+2||nowr<i-2||nowr>i+2)
                        {k=width; continue;}
                    if (k<ALLOWONESKEW&&(nowl<i-1||nowl>i+1||nowr<i-1||nowr>i+1))
                    	{k=width; continue;}
                    if (k>rst[j]) rst[j]=k;
*/                    
                    if (contour[i*width+l]==0){k=width; continue;}
                    if (contour[i*width+r]!=0){;}
                    else if (i>0&&contour[(i-1)*width+r]!=0){;}
                    else if (i<height-1&&contour[(i+1)*width+r]!=0){;}
                    else {k=width; continue;}
                    if (k>rst[j]) rst[j]=k;
                }
            }
        d.width= rst.length; d.height=1;
        //OutputResult.outputResult(rst,rst.length,1);
        return rst;
    }

    Vector[] getInfo(int source[], Dimension d)
    {
        
        mdContourExtractor mdce= new mdContourExtractor();
        int[] contour= mdce.getProcessed(source, d);
        int width=d.width, height=d.height;
        
        Vector[] info= new Vector[width];
        for (int i=0; i<width; i++) info[i]= new Vector();

        for (int i=0;i<height;i++)
            for (int j=0;j<width;j++) {
                int max=0;
                for (int k=1;k<width;k++) {
                    int l=j-k; if (l<0) continue;
                    int r=j+k; if (r>=width) continue;
                    if (contour[i*width+l]==0||contour[i*width+r]==0) {
                        k=width;
                        continue;
                    }
                    if (k>max) max=k;
                }
                info[j].addElement(new Point(j-max,i));
                info[j].addElement(new Point(j+max,i));
            }
        d.width= info.length; d.height=1;       
        return info;
        
    }
}