//Source file: D:\\Projects\\src\\yi\\iu\\processor\\texture\\reg\\TexturePatchSeg.java

package yi.iu.processor.texture.reg;

import yi.util.config.*;
import yi.util.io.*;
import java.awt.*;
import yi.iu.processor.resample.*;
import yi.util.label.Label;
import yi.math.*;


public class TexturePatchSeg extends ColorGuidedTextureSeg
{

  public int PATCH_HEIGHT = 16;
  public int PATCH_WIDTH = 16;

  public TexturePatchSeg()
  {
  }

  public int[] getLabels(int[] pixels, int width, int height) {

    DoubleMatrix features = calculateGabor(pixels, width, height);

    int[] labels = Label.labelPatches(width, height, PATCH_WIDTH, PATCH_HEIGHT);

    // assign those patches on the boundary to 0 to remove the boundary effect
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < PATCH_WIDTH; x++)
        labels[y * width + x] = 0;
      for (int x = width - PATCH_WIDTH; x < width; x++)
        labels[y * width + x] = 0;
    }
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < PATCH_HEIGHT; y++)
        labels[y * width + x] = 0;
      for (int y = height - PATCH_HEIGHT; y < height; y++)
        labels[y * width + x] = 0;
    }
    yi.util.label.Label.sequential(labels, null, true);

    return labels;

  }

  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);
    }

    // read in image
    // get the scale
    String scale_str = ReadArgu.getOption('s', args);
    int scale = 1;
    if (scale_str != null) {
      scale = Integer.parseInt(scale_str);
    }

    // read in image
    Image image = yi.util.io.ImageReader.JAIReadImage(img_name);
    Dimension d = new Dimension();
    int[] pixels = yi.util.io.ImageReader.getPixels(image, d);

    // rescale the image if necessary
    double the_scale = 1.0;
    if (scale != 1) {
      ColorGuidedTextureSeg dummy = new ColorGuidedTextureSeg();
      ResampledDescriptor resampled = Resampler.scaleDown(pixels, d,
          dummy.RESAMPLER, scale);
      if (resampled != null) {
        pixels = resampled.m_resampled;
        d = resampled.m_newDimension;
        the_scale = resampled.m_scale;
        img_name = img_name + Resampler.RESIZED_FILE_SUFFIX + scale;
      }
    }

    // process
    try {

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

      TexturePatchSeg processor = new TexturePatchSeg();
      long startTime = System.currentTimeMillis();
      DS_PolygonRegionTextureStat ds = processor.getReusablePolygonRegionTextureStat(pixels, d.width, d.height, the_scale);
      long endTime = System.currentTimeMillis();
      System.out.println("running time: " + (endTime-startTime));

//      int[] demo = ds.getPolygonDemo(null, true);
//      yi.util.io.OutputResult.outputGIFImage(img_name + ".text.reg.gif", demo, d.width,
//                                             d.height, false);

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

    System.exit(0);
  }

}
