//Source file: D:\\Projects\\src\\yi\\iu\\processor\\color\\reg\\ColorLabeledRgnFts.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.seg.mb.MeanShiftBasedImageSegmenter;
import java.awt.Dimension;

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

  public ColorLabeledRgnFts() {};

  /**
   * 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 ColorLabeledRgnFts(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];
    for (int i = 0; i < pixels.length; i++) {
      // convert rgb values of each pixel to hsb
      int r = (pixels[i] >> 16) & 0xff;
      int g = (pixels[i] >> 8)  & 0xff;
      int b =  pixels[i]        & 0xff;
      float[] hsbvals = new float[3];
      java.awt.Color.RGBtoHSB(r, g, b, hsbvals);
      data[i][0] = hsbvals[0];
      data[i][1] = hsbvals[1];
      data[i][2] = hsbvals[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 ColorLabeledRgnFts(double[][] means, int[] labels, int w, int h,
                            int old_w, int old_h, boolean zeroAsBg) {
    super(means, labels, w, h, old_w, old_h, zeroAsBg, 3, true);
  }

  public int[] colorDemo() {
    int[] demo = (int[])m_labels.clone();
    int[] colors = new int[featureMeans.length];
    for (int i=0; i<featureMeans.length; i++) {
      colors[i] = featureMeans[i]==null?
          0xff000000:
          java.awt.Color.HSBtoRGB((float)featureMeans[i][0], (float)featureMeans[i][1], (float)featureMeans[i][2]);
    }
    for (int i=0; i<demo.length; i++) demo[i] = colors[m_labels[i]];
    return demo;
  }

//  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);
//
//    if (processor_name.indexOf("KMeansHSISeg") > 0) {
//      KMeansHSISeg dp = (KMeansHSISeg) Class.forName(processor_name).newInstance();
//
//      ColorLabeledRgnFts ds = (ColorLabeledRgnFts) dp.reuseColorLabeledRgnFts(
//          DIU.
//          INPUT_SER);
//      return ds;
//    } else if (processor_name.indexOf("MeanShiftBasedImageSegmenter") > 0) {
//      MeanShiftBasedImageSegmenter dp = (MeanShiftBasedImageSegmenter) Class.
//          forName(processor_name).newInstance();
//
//      ColorLabeledRgnFts ds = (ColorLabeledRgnFts) dp.reuseColorLabeledRgnFts(
//          DIU.
//          INPUT_SER);
//      return ds;
//    }
//
//
//    return null;
//  }

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

}
