package yi.iu.imgdb.learn;


import java.util.*;
import yi.iu.region.*;
import yi.graph.polygon.*;
import java.awt.*;
import yi.util.io.*;

/**
 * A list of image feature.  Each of the feature is represented by an ellipse
 * region
 */


public class EllipseFeatureRepresentationList
    extends RgnFeatureRepresentationList {

  static final long serialVersionUID =  868492055120356987L;

  /**
   * @param image_width
   * @param image_height
   */
  public EllipseFeatureRepresentationList(int ft_dim, int image_width, int image_height) {
    super(ft_dim, image_width, image_height);
  }

  /**
   * Draw the regions with the color representing the score of corresponding
   * feature.  After having the score from FeatureScoringCapable, the score
   * will be scaled by "scale", and then shifted by "shift".  The resultant
   * number will be put into the given pixel array
   * @param scoring
   * @param shift
   * @param scale
   * @return
   */
  public int[] drawScoreDemo(Object obj, FeatureScoringCapable scoring, double shift,
                            double scale) {
    int[] pixels = new int[imgWidth * imgHeight];
    // loop through each feature
    for (Iterator i=iterator(); i.hasNext(); ) {
      EllipseFeatureRepresentation ft = (EllipseFeatureRepresentation)i.next();
      double[] data = ft.data;
      // get the score for the feature
      double score = scoring.getFeatureScore(data, obj);
      // shift and scale the score
      score = (score * scale) + shift;
      // get the color from the score
      int color = (int)Math.round(score);

      Region region = ft.region;
      region.drawApproximateContour(pixels, imgWidth, imgHeight,
                                       color, 2, 8);
             //drawRegion(pixels, new Dimension(imgWidth, imgHeight), region, color, false, false);
    }
    return pixels;
  }

//  public static EllipseFeatureRepresentationList represent(
//      String img_name, String generator, String ft, int scale, int rst_scale, int mode) throws Exception{
//
//    FeatureRepresentable ft_ds = (FeatureRepresentable) Class.forName(ft).
//        newInstance();
//
//    /**
//     * The scale is combined into the file name already, so just pass scale = 0
//     * Otherwise, the be-called method will try to add "s" again
//     */
//    FeatureRepresentable ds = FeatureUtil.readFromFile(
//       img_name, generator, ft, scale, rst_scale);
//
//    FeatureRepresentationList data = null;
//    if (ds instanceof EllipseFeatureRepresentable) {
//      return ( (EllipseFeatureRepresentable) ds).
//          representEllipseFeatures(generator, mode);
//    }
//    else {
//      System.err.println(ds.getClass().getName() +
//                         " is not EllipseFeatureRepresentable");
//      return null;
//    }
//  }

//  /**
//   * Write the image data center as a java object to file
//   * @param file
//   */
//  public void writeToFile (String file) throws java.io.IOException
//  {
//    yi.util.io.output.DOU_Ser.output(file, this);
//  }

//  public static class Represent {
//    public final static String USAGE = "Usage: -i image_name -g generator -f feature_name -s scale -r rst_scale -m mode -p path -t to_path";
//
//    public static void main(String[] args) {
//      try {
//        // get the image file name
//        String img_name = ReadArgu.getOption('i', args);
//        if (img_name == null) {
//          System.err.println(USAGE);
//          System.exit(1);
//        }
//
//        // generator
//        String generator = ReadArgu.getOption('g', args);
//        if (generator == null) {
//          System.err.println(USAGE);
//          System.exit(1);
//        }
//
//        // feature name
//        String feature = ReadArgu.getOption('f', args);
//        if (feature == null) {
//          System.err.println(USAGE);
//          System.exit(1);
//        }
//
//        // scale
//        String scale_str = ReadArgu.getOption('s', args, "0");
//        int scale = Integer.parseInt(scale_str);
//
//        // result scale
//        String rst_scale_str = ReadArgu.getOption('r', args, "0");
//        int rst_scale = Integer.parseInt(rst_scale_str);
//
//        // mode
//        String mode_str = ReadArgu.getOption('m', args, "0");
//        int mode = Integer.parseInt(mode_str);
//
//        String from_path = ReadArgu.getOption('p', args, "");
//        String to_path = ReadArgu.getOption('t', args, "");
//
//        // represent
//        EllipseFeatureRepresentationList rpst = EllipseFeatureRepresentationList.represent(from_path + img_name, generator, feature, scale, rst_scale, mode);
//        rpst.writeToFile(to_path + img_name + "." + yi.util.io.output.DOU.shortName(generator) +
//                         ".EllipseFeatureRepresentationList" + ".m" + mode + ".ser");
//
//        // draw the ellipse demo
//        int[] pixels = rpst.drawScoreDemo(null, new FeatureScoringCapable() { // a inner class give sequence score
//          int count = 1;
//          public double getFeatureScore(double[] feature, Object obj) {
//            return count++;
//          }
//        }
//        , 0, 1);
//        int[] pixels_img = yi.util.ImageUtil.makeNewImageFromLabel(pixels, true);
//
//        if (scale != 0) {
//          img_name = img_name + yi.iu.processor.resample.Resampler.RESIZED_FILE_SUFFIX + scale;
//        }
//        if (rst_scale != 0) {
//          img_name = img_name + ".r" + rst_scale;
//        }
//
//        yi.util.io.OutputResult.outputGIFImage(to_path + img_name + "." + yi.util.io.output.DOU.shortName(generator) + ".er.gif",
//                                               pixels_img, rpst.imgWidth, rpst.imgHeight);
//
//
//      }catch (Exception e) {
//        e.printStackTrace();
//      }
//    }
//
//  }

}
