package yi.iu.processor.clc;

import yi.iu.processor.clc.*;
import yi.iu.processor.ort.*;
import yi.util.ImageUtil;
import yi.util.io.OutputResult;
import yi.iu.processor.clc.*;
import Acme.JPM.Decoders.*;

import java.awt.*;
import java.io.*;
import java.util.*;

public final class ArgumentExperiment_ol
{
    public static int maxSize=0;
    public static int maxSizeAfterSpatial=0;
    public static int maxSizeAfterOverlap=0;
    public static double maxSizeW=0;
    public static double maxSizeAfterSpatialW=0;
    public static double maxSizeAfterOverlapW=0;

    static String directory="/images/test";
    static String outFile = null;
    static String imageName=null;
    static Dimension d= new Dimension();
    static int[] source;
    static Vector clusters= new Vector();
    static boolean OUTPUTEDGEIMAGE=false;
    static boolean OUTPUTRSTIMG=false;

    public static void aCluster(LineCluster lc0, LineCluster lc1)
    {
        if (lc1.size()<8) return;
        int[] sizes= {lc0.size(), lc1.size()};
        clusters.addElement(sizes);
        //System.err.println(" "+lc0.size()+"->"+lc1.size());
        if (OUTPUTEDGEIMAGE) {
            int [] edgeimg= new int[source.length];
            for (int j=0;j<edgeimg.length;j++) edgeimg[j]=0xffffffff;
            for (int j=0;j<lc0.size();j++) {
                ORTLine l= (ORTLine)lc0.getLineAt(j);
                DrawUtil.ort2image(edgeimg, d, l, 0xffff0000, 1);
            }
            for (int j=0;j<lc1.size();j++) {
                ORTLine l= (ORTLine)lc1.getLineAt(j);
                DrawUtil.ort2image(edgeimg, d, l, 0xff0000ff, 1);
            }

            String newName;
            int index=imageName.lastIndexOf(".jpg");
            if (index<0) index=imageName.lastIndexOf(".JPG");
            if (index<0) index=imageName.lastIndexOf(".ppm");
            if (index<0) index=imageName.lastIndexOf(".PPM");
            if (index>=0) newName= imageName.substring(0,index)+"_edge"+clusters.size()+".gif";
            else newName= imageName+"_edge"+clusters.size()+".gif";
            System.err.println("output: "+newName);
            yi.util.io.OutputResult.outputGIFImage(newName, edgeimg, d.width, d.height);

            edgeimg=null;
            System.gc();
        }

    }

    public static void outputClusters(int index)
    {
        try {
            FileWriter out= new FileWriter(outFile, true);
            out.write(imageName+"\t\n");
            for (int i=0;i<clusters.size();i++) {
                int[] sizes= (int[]) clusters.elementAt(i);
                out.write(imageName+"\t");
                out.write((index+1)+"\t");
                out.write((i+1)+"\t");
                out.write(sizes[0]+"\t");
                out.write(sizes[1]+"\t");
                out.write((double)sizes[1]/sizes[0]+"\n");
            }
            out.close();
        }
        catch (IOException e) {
            System.err.println("io error: "+e);
        }

    }


    public static void output(LineCluster[] lcs)
    {
        if (true) return;
        try {
            FileWriter out= new FileWriter(directory+"/statics_ol.rst", true);
            if (lcs.length==0) {
                out.write(imageName+"\t");
                out.write(maxSize+"\t");
                out.write(maxSizeW+"\n");
                //out.write(maxSizeAfterSpatial+"\t");
                //out.write(maxSizeAfterOverlap+"\t");
                //weighted
                //out.write(maxSizeAfterSpatialW+"\t");
                //out.write(maxSizeAfterOverlapW+"\t");
            }

            for (int i=0;i<lcs.length;i++) {
                LineCluster lc= lcs[i];
                int size= lc.size();
                double sizew= lc.weightedSize();
                double ratio= sizew/size;
                double mean= lc.getAverageLength();
                double variance= lc.getLineLenVariance();
                double deviation= lc.getLineMeanDeviation();
                out.write(imageName+"\t");
                out.write(maxSize+"\t");
                out.write(maxSizeW+"\t");
                out.write(i+"\t");
                out.write(size+"\t");
                out.write(sizew+"\t");
                out.write(ratio+"\t");
                out.write(mean+"\t");
                out.write(variance+"\t");
                out.write(deviation+"\n");
            }
            maxSize=maxSizeAfterSpatial=maxSizeAfterOverlap=0;
            maxSizeW=maxSizeAfterSpatialW=maxSizeAfterOverlapW=0;


            out.close();

            int [] edgeimg= new int[source.length];
            for (int i=0;i<lcs.length;i++) {
                for (int j=0;j<edgeimg.length;j++) edgeimg[j]=0xffffffff;
                LineCluster lc= lcs[i];
                for (int j=0;j<lc.size();j++) {
                    ORTLine l= (ORTLine)lc.getLineAt(j);
                    DrawUtil.ort2image(edgeimg, d, l, 0xffff0000, 1);
                }
                String newName;
                int index=imageName.lastIndexOf(".jpg");
                if (index<0) index=imageName.lastIndexOf(".JPG");
                if (index<0) index=imageName.lastIndexOf(".ppm");
                if (index<0) index=imageName.lastIndexOf(".PPM");
                if (index>=0) newName= imageName.substring(0,index)+"_edge"+i+".gif";
                else newName= imageName+"_edge"+i+".gif";
                System.err.println("output: "+newName);
                yi.util.io.OutputResult.outputGIFImage(newName, edgeimg, d.width, d.height);

            }
            edgeimg=null;
            System.gc();

        }
        catch (IOException e) {
            System.err.println("io error: "+e);
        }

    }

    public static void main(String[] args)
    {

        if (args.length>0) {
            directory=args[0];
        }
        System.err.println("directory name = "+directory);

        if (args.length>1) {
            outFile= args[1];
        }
        else {
            outFile= "statics_intra_clusters.rst";
        }
        System.err.println("output file name = " + outFile);

        //String images[]= yi.util.io.File.getFileNames(directory);
        String images[]= yi.util.io.File.getImagesNameRecursively(directory);
        System.err.println("Image#="+images.length);

        CSOSSM br= new CSOSSM();
        CSOSSM.TESTOL= true;
        br.Thuge=15;                   //the size of overlaped line cluster
        br.Tlenforollines= 0.5;    //the width of considered overlaped lines must > 0.75

        java.util.Date date= new Date();

        for (int i=0;i<images.length;i++) {
            imageName= images[i];
            System.err.println();
            System.err.println("==== "+imageName+" ==== "+(i+1)+"/"+images.length);
            source= yi.util.ImageUtil.readImage(imageName, d);
            if (source==null) continue;

            clusters.removeAllElements();

            int[] result= br.getProcessedPseudoColor(source, d);

            outputClusters(i);
            if (OUTPUTRSTIMG&&result!=null) {
                String newName;
                int index=imageName.lastIndexOf(".jpg");
                if (index<0) index=imageName.lastIndexOf(".JPG");
                if (index<0) index=imageName.lastIndexOf(".ppm");
                if (index<0) index=imageName.lastIndexOf(".PPM");
                if (index>=0) newName= imageName.substring(0,index)+"_rst2.gif";
                else newName= imageName+"_rst2.gif";
                System.err.println("output: "+newName);
                yi.util.io.OutputResult.outputGIFImage(newName, result, d.width, d.height);
            }
            result=null;
            source=null;
            System.gc();
            java.util.Date date2= new Date();
            System.err.println("time elapse: "+(date2.getTime()-date.getTime())/1000.0+"/"
                +(date2.getTime()-date.getTime())/1000.0/(i+1));
        }


        System.err.println();
        System.err.println("Done!");
        System.exit(0);
    }
}