//Source file: D:\\projects\\src\\yi\\iu\\processor\\color\\reg\\CIELabLabeledRgnFts.java

package yi.iu.processor.color.reg;

import yi.iu.region.LabeledRegionFeatures;
import yi.iu.imgdb.learn.*;
import yi.util.config.*;
import yi.iu.processor.color.reg.merge.*;
import yi.util.io.input.*;
import yi.iu.processor.color.*;
import yi.iu.processor.color.seg.mb.*;
import java.awt.*;
import org.jmat.data.Matrix;
import org.jmat.data.AbstractMatrix;
import org.jmat.gui.Plot3DPanel;
import yi.util.io.*;

public class CIELabLabeledRgnFts extends LabeledRegionFeatures
{
  static final long serialVersionUID = -5606901577636849483L;

  public CIELabLabeledRgnFts() {};

  /**
   * Given the RGB values of each pixel and the labels, contruct the
   * color features with labeled region.
   * @param labs
   * @param labels
   * @param w
   * @param h
   * @param old_w
   * @param old_h
   * @param zeroAsBg
   */
  public CIELabLabeledRgnFts(double[][] labs, int[] labels, int w, int h, int old_w, int old_h, boolean zeroAsBg)
  {
    super(labels, w, h, old_w, old_h, zeroAsBg, 3);

    // if zeor is not background, then make mean[0] to null.
    if (zeroAsBg) {
      featureMeans = labs;
    } else {
      featureMeans = new double[labs.length+1][];
      featureMeans[0] = null;
      for (int i=0; i<labs.length; i++) {
        featureMeans[i+1] = labs[i];
      }
    }
  }

  public CIELabLabeledRgnFts(int[] pixels, int[] labels, int w, int h, int old_w, int old_h, boolean zeroAsBg)
  {
    super(labels, w, h, old_w, old_h, zeroAsBg, 3);

    double[][] data = new double[pixels.length][3];
    double[] lab = new double[3];
    for (int i = 0; i < pixels.length; i++) {
      yi.iu.processor.color.CIELab.rgb2lab(pixels[i], lab);
      data[i][0] = lab[0];
      data[i][1] = lab[1];
      data[i][2] = lab[2];
    }

    // have to use m_labels instead of labels since m_labels may be different
    // if the passed in labels don't have 0 as background
    featureMeans = super.getLabeledRgnMeans(data, m_labels, width, height);
  }

//  public LabeledRegionFeatures createFromFile(String processor_name, int scale, int rst_scale, String img) throws Exception {
//    String scaled_img_name = img;
//    if (scale != 0)
//      scaled_img_name = img +
//          yi.iu.processor.resample.Resampler.RESIZED_FILE_SUFFIX + scale;
//    if (rst_scale != 0)
//      scaled_img_name = img +
//          ".r" + rst_scale;
//    Configuration.setParameter("SOURCE_NAME", scaled_img_name);
//
//    if (processor_name.indexOf("DPU_GongProminentColor") >= 0) {
//      DPU_GongProminentColor dp = (DPU_GongProminentColor) Class.forName(
//          processor_name).newInstance();
//
//      CIELabLabeledRgnFts ds = (CIELabLabeledRgnFts) dp.
//          reuseCIELabLabeledRgnFts(DIU.
//                                   INPUT_SER);
//
//      return ds;
//    } else if (processor_name.indexOf("MeanShiftBasedImageSegmenter") >= 0) {
//      MeanShiftBasedImageSegmenter dp = (MeanShiftBasedImageSegmenter) Class.forName(
//          processor_name).newInstance();
//
//      CIELabLabeledRgnFts ds = (CIELabLabeledRgnFts) dp.
//          reuseCIELabLabeledRgnFts(DIU.
//                                   INPUT_SER);
//
//      return ds;
//    } else {
//      System.err.println("LabeledRegionFeatures can't be generated by " + processor_name);
//      return null;
//    }
//
//  }
//  public FeatureRepresentable createFeatureFromFile(String processor_name, int scale,
//      String img) throws Exception {
//    return createFromFile(processor_name, scale, 0, img);
//  }
//
//  public FeatureRepresentable createFeatureFromFile(String processor_name, int scale, int rst_scale,
//      String img) throws Exception {
//    return createFromFile(processor_name, scale, rst_scale, img);
//  }

  /**
   * Return the feature colors in rgb space.  (the null features will be returned
   * as "rgb4null")
   * @param rgb4null
   * @return
   */
  public int[] getRGBColors(int rgb4null) {
    int[] colors = new int[featureMeans.length];
    for (int i=0; i<featureMeans.length; i++) {
      colors[i] = featureMeans[i]==null?
          rgb4null:
          CIELab.lab2rgb(featureMeans[i][0], featureMeans[i][1], featureMeans[i][2]);
      if (featureMeans[i]!=null) {
        System.out.println("#" + i + "\t" + featureMeans[i][0] + "\t" +
                           featureMeans[i][1] + "\t" + featureMeans[i][2] +
                           "\t" + "rgb" + "\t" + ( (colors[i] >> 16) & 0xff) +
                           "\t" + ( (colors[i] >> 8) & 0xff) +
                           "\t" + ( (colors[i]) & 0xff));
      }
    }
    return colors;
  }


  public int[] colorDemo(Dimension d) {
    d.width = width; d.height = height;
    int[] demo = (int[])m_labels.clone();
    int[] colors = getRGBColors(0xffffffff);
    for (int i=0; i<demo.length; i++) demo[i] = colors[m_labels[i]];

//    Plot3DPanel pp = ColorUtil.drawPointsInRGBSpace(colors);
//    pp.toGraphicFile(new java.io.File("points" + ".png"));

    return demo;
  }

  public static CIELabLabeledRgnFts createFromFile(String file) throws Exception {
    return (CIELabLabeledRgnFts) yi.util.io.input.DIU_Ser.input(file);
  }


  /**
   * Use mean color to demonstrate the color clusters
   */
  public static class DemoFeatureColors {

    public final static String USAGE = "Usage: -i input_obj";

    public static void main(String[] args) {

      // init configuration
      Configuration.init(args);

      try {

        // read in an ObjGMMAdapter object
        String obj_file = ReadArgu.getOption('i', args, null);
        if (obj_file == null) {
          System.err.println(USAGE);
          System.exit(1);
        }

        CIELabLabeledRgnFts obj = CIELabLabeledRgnFts.createFromFile(obj_file);
        int[] colors = obj.getRGBColors(0xffffffff);

        int width = 64, height = 64;
        int[] demo = yi.iu.processor.color.ColorUtil.createSpectrum(colors, width, height);
        String out_name = yi.util.data.StringUtil.getFileSuffixNameRemoved(obj_file) +
                          ".rgb.png";

        ImageIOUtil.write(out_name,
                          "png", demo, width * colors.length, height);
        System.out.print("rgb colors output to " + out_name);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

}
