package yi.iu.processor.cannyox;

import yi.iu.processor.*;
import yi.iu.processor.channel.*;
import yi.iu.processor.cannyox.*;
import yi.iu.ui.DisplayFrame;
import java.awt.*;
import java.util.Vector;

public class CannyOxDemo extends yi.iu.processor.ImageProcessor
{
    float sigma= 1;        // Standard deviation of the gaussian kernel.
    float low= 2;       // Fraction of the high threshold in hysteresis.
    float high= 12;
    int minlen = 10;

    public int getArguNum() { return 4;}

    public void setArgument(int i, String st)
    {
        if (st.length()==0) return;
        if (i==0) sigma= Float.valueOf(st).floatValue();
        else if (i==1) low= Float.valueOf(st).floatValue();
        else if (i==2) high= Float.valueOf(st).floatValue();
        else if (i==3) minlen= Integer.valueOf(st).intValue();
    }

    public String getArgument(int i)
    {
        if (i==0) return (new Float(sigma)).toString();
        else if (i==1) return (new Float(low)).toString();
        else if (i==2) return (new Float(high)).toString();
        else if (i==3) return (new Integer(minlen)).toString();
        return null;
    }

    public String getArguName(int i)
    {
        if (i==0) return "Sigma";
        else if (i==1) return "Low";
        else if (i==2) return "High";
        else if (i==3) return "Min_len";
        return null;
    }

    public int[] getProcessed(int source[], Dimension d)
    {
        return null;
    }

    public int[] getProcessedPseudoColor(int source[], Dimension d)
    {
        //float sigma = (float)1.0;          //(argc<3) ? 1.0 : atof(argv[2]);
        float threshold = 1;        //(argc<4) ? 1 : atoi(argv[3]);
        float gauss_tail = (float)0.02;    //(argc<5) ? 0.02 : atof(argv[4]);
        //float low = (float)2.0;            //(argc<6) ? 2.0 : atof(argv[5]);
        //float high = (float)12.0;           //(argc<7) ? 12.0 : atof(argv[6]);
        //int minlen = 10;            //(argc<8) ? 10 : atoi(argv[7]);

        /*
        // Do sanity check on received values
        if (sigma<=0.0) {cerr << "value of sigma must be positive\n"; return 1; }
        if (low<=0.0) {cerr<<"value of low threshold should be positive\n";return 1;}
        if (gauss_tail>1.0) {cerr<<"Gaussian tail should not exceed 1\n"; return 1; }
        if (minlen<0) {cerr<<"minimal edge length should be nonnegative\n";return 1;}
        if (threshold<=0.0) {cerr<<"fitting threshold should be positive\n";return 1;}
        */

        int[] intensity= (new CYMaker()).getProcessed(source,d);
        int[][] image=yi.util.ImageUtil.oneD2twoD(intensity, d);

        // Call EdgeDetector and write to the EdgelChain list edges
        CannyOX filter= new CannyOX(image,sigma,50,gauss_tail,low,high,2,10,minlen,1,0,5,true,true,false);
        Vector edges= filter.Do_cannyOX();

        int[] rst= draw_edges(source,d,edges);
        int[] img= yi.util.ImageUtil.makeNewImageFromLabel(rst);
        (new DisplayFrame("rst", img, d.width, d.height)).show();

        return img;
    }

    int[] draw_edges(int[] source, Dimension d, Vector edges)
    {
        int[] rst= new int[source.length];
        int h=d.height, w=d.width;
        for (int i=0;i<edges.size();i++) {
            Edge e=(Edge)edges.elementAt(i);
            int num= e._curve.length();
            for (int j=0;j<num;j++) {
                int x= (int)Math.round(e._curve._x[j]);
                int y= (int)Math.round(e._curve._y[j]);
                rst[y*w+x]=i+1;
            }
        }
        return rst;
    }

}