package yi.iu.processor.symmetry;

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

public class GrayLevelSymmetryCal extends ContourBasedSymProcessor
{
    int maxGrayDiff=2;

    public GrayLevelSymmetryCal() {super();}
    public GrayLevelSymmetryCal(String edgeDectorName) {super(edgeDectorName); }
    
    
    public int[] getProcessed(int source[], Dimension d, int maxwidth)
    {
        int oldw=d.width,oldh=d.height;
        IntensityMaker im= new IntensityMaker();
        int[] in= im.getProcessed(source, d);
        
        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 regionSize= oldw/width;
        int neww=regionSize*width, newh=regionSize*height;
        
        int[] rst= new int[width];
        for (int i=0;i<height;i++)
            for (int j=0;j<width;j++) {
                for (int k=1;k<width;k++) {
                    if (k*2+1>maxwidth) break;
                    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) continue;
                    for (int m=i*regionSize;m<(i+1)*regionSize;m++)
                        for (int n=l*regionSize;n<(l+1)*regionSize;n++)
                            for (int s=i*regionSize;s<(i+1)*regionSize;s++)
                                for (int t=r*regionSize;t<(r+1)*regionSize;t++) {
                                    int diff=in[m*oldw+n]-in[s*oldw+t];
                                    if (diff<0) diff=-diff;
                                    if (diff<=maxGrayDiff) {
                                        rst[j]++;
                                        // exit from the inner two loops
                                        s=(i+1)*regionSize;t=(r+1)*regionSize;
                                    }
                                }
                            
                }
            }
        d.width= rst.length; d.height=1;
        return rst;
    }
    
    Vector[] getInfo(int source[], Dimension d)
    {
        int oldw=d.width,oldh=d.height;
        IntensityMaker im= new IntensityMaker();
        int[] in= im.getProcessed(source, d);
        mdContourExtractor mdce= new mdContourExtractor();
        int[] contour= mdce.getProcessed(source, d);
        int width=d.width, height=d.height;
        int regionSize= oldw/width;
        int neww=regionSize*width, newh=regionSize*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++) {
                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) continue;
                    for (int m=i*regionSize;m<(i+1)*regionSize;m++)
                        for (int n=l*regionSize;n<(l+1)*regionSize;n++)
                            for (int s=i*regionSize;s<(i+1)*regionSize;s++)
                                for (int t=r*regionSize;t<(r+1)*regionSize;t++) {
                                    int diff=in[m*oldw+n]-in[s*oldw+t];
                                    if (diff<0) diff=-diff;
                                    if (diff<=maxGrayDiff) {
                                        info[j].addElement(new Point(l,i));
                                        info[j].addElement(new Point(r,i));
                                        // exit from the inner two loops
                                        s=(i+1)*regionSize;t=(r+1)*regionSize;
                                            
                                    }
                                }
                            
                }
            }
        d.width= info.length; d.height=1;       
        return info;
    }

}