package yi.or.db;

import yi.or.*;
import yi.or.db.FindAllImages;
import yi.iu.processor.*;
import yi.iu.processor.channel.CYMaker;
import yi.iu.processor.texture.LawsMaker;

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

public class ORonDB
{
    boolean VERBOSE= true;
    static String DEFAULTDIR= "/gts/test";
    static String RESULTFILE= "objects";

    void objectRecognition(String dirName)
    {
        //find all the images to deal with
        String[] allImages = FindAllImages.everySingleName(dirName);
        //find all the available object recognition processor
        String[] allORNames = ObjRcg.getAllORs();
        ObjRcg[] allORs = new ObjRcg[allORNames.length];
        for (int i=0;i<allORs.length;i++) allORs[i]= getORbyName(allORNames[i]);
        //String[] allObjNames= getAllObjNames(allORs);

        //find all the objects in the images one by one
        for (int i=0;i<allImages.length;i++) {
            String[] rst= findObjs(allORs, allImages[i]);
            outputResult(allImages[i], rst);
        }

        if (VERBOSE) System.err.println("DONE!");

    }
        //return an instance of the object recognition class given the name of it
        ObjRcg getORbyName(String orName)
        {
                ObjRcg or=null;
            try {
                Class orc= Class.forName(orName);
                or= (ObjRcg)orc.newInstance();
            } catch (Exception e) {
                System.err.println("Can't found the object recognition processor");
            }
            return or;
        }

        //get all the names of all the objects under examination
        String[] getAllObjNames(ObjRcg[] ors) {
            String[] orn= new String[ors.length];
            for (int i=0;i<orn.length;i++) {
                orn[i]= ors[i].objName();
            }
            return orn;
        }


    //use all given obj-rcg object to do the recognition in the given image, return all the name
    String[] findObjs(ObjRcg[] ors, String img) {
        if (VERBOSE) System.err.println("dealing with "+img+"...");

        if (VERBOSE) System.err.println("loading ...");
        Dimension d= new Dimension();
        int[] pixel= yi.util.ImageUtil.getPixelFromFile(img, d);

        if (VERBOSE) System.err.println("calculate hsi...");
        int[][] hsi= (new CYMaker()).getProcessed_ex(pixel, d);

        if (VERBOSE) System.err.println("calculate laws...");
        int[][] laws= (new LawsMaker()).getProcessed_ex(pixel, d, ObjRcg.LAWS_ENER_PAD_LENGTH);

        String[] obj_names= new String[ors.length];
        for (int i=0;i<ors.length;i++) {
            if (VERBOSE) System.err.println(" recognizing "+ors[i].objName());
            Region rst= ors[i].recognition(pixel, hsi, laws, null, d);
            if (rst.getSize()>0) obj_names[i]= ors[i].objName();
            else obj_names[i]= null;
        }
        hsi=null;laws=null;pixel=null;System.gc();
        return obj_names;

    }

    void outputResult(String img, String[] objs)
    {
        //get the directory name
        int index= img.lastIndexOf('/');
        String dirName, fileName;
        if (index>=0) {
            dirName=img.substring(0, index+1);
            fileName= img.substring(index+1, img.length());
        }
        else {
            fileName=img;
            dirName="";
        }

        try {
            //write to the objects file
            BufferedWriter wf= new BufferedWriter(new FileWriter(dirName+RESULTFILE,true));
            wf.write(fileName+": ");
            for (int i=0;i<objs.length;i++) {
                if (objs[i]==null?true:(objs[i].length()<1)) continue;
                wf.write(objs[i]+", ");
            }
            wf.write("\r\n");
            wf.flush();
            wf.close();
        }
        catch (IOException e) {
            System.err.println("err happened when writing the results: "+e);
        }
    }


    public static void main(String args[])
    {
        ORonDB ordb= new ORonDB();
        if (args.length>0)
            ordb.objectRecognition(args[0]);
        else ordb.objectRecognition(DEFAULTDIR);
    }
}