package yi.or;

import java.awt.*;

import yi.iu.CY_HSI;
import yi.iu.processor.*;
import yi.iu.processor.morph.*;
import yi.iu.ui.DisplayFrame;
import yi.util.io.*;
import yi.iu.processor.resample.*;
import yi.util.config.Configuration;

public class OR_sea extends OR_simple
{

    public String objName() {return "sea";}

    public Region recognition(int source[], int[][] hsi, int[][] laws, int[][] diff, Dimension d)
    {
        VISUALIZE=false;

        Region rgns= super.recognition(source, hsi, laws, diff, d);

        if (VERBOSE) System.err.println("dilation...");
        int dilation_r=9;       //extend 4 pixels
        //dilation
        rgns.dilation(Morph.DISK, dilation_r);
        //get the statistics information about all the connected component
        rgns.connectComponent();
        if (VISUALIZE) rgns.display("dilation",source);

        if (VERBOSE) System.err.println("region filtering...");
        //first eliminate the small regions
        double[][] rgns_stat= rgns.getStatistics();
        double[][] stat_filter= {
            {0.1,1.1},      //area [0.3,1]
            null,null,null,null,null,null,null,null,null,null,null,
        };
        rgns.rangeFilterOnRegion(rgns_stat, stat_filter);

        //second get some
        rgns_stat= rgns.getStatistics();
        double[][] stat_filter_2= {
            {0.3,1.1},      //area [0.3,1]
            {0,3,0.7},      //center.x [0.3,0.7] in the middle
            {0.2,1.0},      //center.y in the bottome
            {0.0,0.3},      //x1
            null,
            {0.5,1.1},      //width
            null,
            null,
            {0,0.4},        //slant
            {0.5,1.1},      //density
            null,
            null,
        };
        rgns.rangeFilterOnRegion(rgns_stat, stat_filter_2);
        if (VISUALIZE) rgns.display("region_filtered", source);

        return rgns;
    }

    public int[][] getHSIFilter()
    {
        int[][] filter= {
            {216, 232},
            {9,	35},
            {42, 134},
        };
        return filter;
    }

    public int[][] getLawsFilter()
    {
        int[][] filter= {
            {225, 1317, },
            {137, 882, },
            {142, 1001 },
            {255, 2654, },
            {36, 199, },
            {35, 218, },
            {62, 547, },
            {18, 138, },
            {32, 333, },
            {32, 350, },
            {59, 319, },
            {19, 135, },
            {17, 159, },
            {55, 747, },
        };
        return filter;
    }

  public static void main(String[] args) {
    // init configuration
    Configuration.init(args);

    // get the image name
    String img_name = ReadArgu.getOption('i', args);
    if (img_name == null) {
      System.err.println("Usage: -i image_name");
      System.exit(1);
    }

    // whether scale the image first
    String scale_str = ReadArgu.getOption('s', args);

    // read in image
    Image image = null;
    Dimension d = new Dimension();
    int[] pixels = null;
    try {
      image = yi.util.io.ImageReader.readImageFile(img_name);
      pixels = yi.util.io.ImageReader.getPixels(image, d);
    }
    catch (java.io.IOException e) {
      System.err.println("Can't read image " + img_name);
      e.printStackTrace();
      System.exit(2);
    }

    // process
    try {

      if (scale_str != null) {
        int new_size = Integer.parseInt(scale_str);
        ResampledDescriptor resampled = Resampler.scaleDown(pixels, d, Resampler.NEAREST_RESAMPLER, new_size);
        if (resampled != null) {
          pixels = resampled.m_resampled;
          d = resampled.m_newDimension;
        }
        img_name = img_name + Resampler.RESIZED_FILE_SUFFIX + new_size;
      }

      Configuration.setParameter("SOURCE_NAME", img_name);
      yi.util.ImageUtil.setPixels(pixels);
      yi.util.ImageUtil.setDimension(d);

      long startTime = System.currentTimeMillis();

      OR_sea sea = new OR_sea();
      Region r = sea.recognition(pixels, d);
      long endTime = System.currentTimeMillis();
      System.out.println("running time: " + (endTime-startTime));
      r.display();

    } catch (Exception e) {
      System.err.println("broken when processing " + img_name);
      e.printStackTrace();
    }

//    System.exit(0);
  }
}