package yi.iu.processor.color.reg;

import java.awt.*;

import yi.iu.processor.color.*;
import yi.iu.region.*;

public class CIELabLabRgnFtsPxlVal extends LabeledRgnFtsWithPxlValue
{

  static final long serialVersionUID = 6927093046491720705L; // for color with post merge

  public CIELabLabRgnFtsPxlVal() {

  }

  /**
   * Given the RGB values of each pixel and the labels, contruct the
   * color features with labeled region.
   * @param pixels
   * @param labels
   * @param w
   * @param h
   */
  public CIELabLabRgnFtsPxlVal(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]; // to save the lab colors
    CIELab.rgb2lab(pixels, data);

    // 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);
    m_pixelValues = data;
  }

//  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 = scaled_img_name +
//          ".r" + rst_scale;
//
//    Configuration.setParameter("SOURCE_NAME", scaled_img_name);
//
//    KMeansHSISeg dp = (KMeansHSISeg) Class.forName(processor_name).newInstance();
//
//    ColorLabRgnFtsPxlVal ds = (ColorLabRgnFtsPxlVal) dp.reuseColorLabRgnFtsPxlVal(DIU.
//        INPUT_SER);
//
//    return ds;
//  }

  /**
   * 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]);
    }
    return colors;
  }

  public int[] colorDemo() {
    int[] demo = (int[])m_labels.clone();
    int[] colors = getRGBColors(0xffffffff);
    for (int i=0; i<demo.length; i++) demo[i] = colors[m_labels[i]];
    return demo;
  }

  public int[] colorDemo(Dimension d) {
    d.width = width; d.height = height;
    return colorDemo();
  }



}
