package yi.iu.processor.texture;

import java.util.*;
import java.awt.*;
import yi.util.io.*;
import yi.util.config.*;

/**
 * Read texture image segments and make up 2*2 texture images with 4 different
 * textures
 */
public class MakeUpImage {
  public final static String USAGE = "Usage: -p texture_seg_image_path -n generate_number";

  /**
   * Texture segment number on vertical direction
   */
  public static int Y_SEG_NUM = 2;

  /**
   * Texture segment number on horizontal direction
   */
  public static int X_SEG_NUM = 2;

  /**
   * Image width
   */
  public static int IMAGE_WIDTH = 256;

  /**
   * Image height
   */
  public static int IMAGE_HEIGHT = 256;

  /**
   * Read an image, and return a segment of it
   * @param img_name image name
   * @param left left of the segment
   * @param width width of the segment
   * @param top top of the segment
   * @param height height of the segment
   * @return the segment
   */
  static int[] readImageSeg(String img_name, int left, int top, int width, int height) {
    Image image = null;
    Dimension d = new Dimension();
    int[] pixels = null;
    try {
      image = yi.util.io.ImageReader.readImageFile(img_name);
      pixels = yi.util.io.ImageReader.getPixels(image, d);
    }
    catch (java.io.IOException e) {
      System.err.println("Can't read image " + img_name);
      e.printStackTrace();
      System.exit(2);
    }
    int[] seg = new int[width * height];
    for (int h=0; h<height; h++) {
      for (int w=0; w<width; w++) {
        int x = left + w, y = top + h;
        seg[h * width + w] = (x < d.width && y < d.height) ?
                              pixels[y * width + x] :
                              pixels[d.width * d.height + 1];
      }
    }
    return seg;
  }

  /**
   * Copy a segment of dest image to the src image
   */
  static void segCopy(int[] src_img, int src_img_width, int src_img_height,
                      int left, int top, int width, int height,
                      int[] dest_img, int dest_img_width, int deset_img_height) {
    for (int h=0; h<height; h++) {
      for (int w=0; w<width; w++) {
        src_img[(top+h)*src_img_width+(left+w)] = dest_img[h*dest_img_width + w];
      }
    }
  }

  public static void main(String[] args) {

    // init configuration
    Configuration.init(args);

    // get the image name
    String seg_path = ReadArgu.getOption('p', args);
    String generate_num_str = ReadArgu.getOption('n', args);
    if (seg_path == null || generate_num_str == null) {
      System.err.println(USAGE);
      System.exit(1);
    }
    int generate_num = Integer.parseInt(generate_num_str);

    // read in the image names in the texture image path
    String[] seg_names = yi.util.io.File.getFileNames(seg_path);
    int seg_num = seg_names.length;

    // generate the 2*2 texture images
    Random r = new Random();
    for (int i=0; i<generate_num; i++) {
      int[] new_image = new int[IMAGE_WIDTH * IMAGE_HEIGHT];
      for (int y=0; y<Y_SEG_NUM; y++) {
        for (int x=0; x<X_SEG_NUM; x++) {
          int seg_index = r.nextInt(seg_num);
          int seg_left = IMAGE_WIDTH * x / X_SEG_NUM;
          int seg_width = IMAGE_WIDTH * (x+1) / X_SEG_NUM - seg_left;
          int seg_top = IMAGE_HEIGHT * y / Y_SEG_NUM;
          int seg_height = IMAGE_HEIGHT * (y+1) / Y_SEG_NUM - seg_top;
          int[] seg = readImageSeg(seg_names[seg_index],
                                   0, 0, seg_width, seg_height);
          // copy the seg to image
          segCopy(new_image, IMAGE_WIDTH, IMAGE_HEIGHT,
                  seg_left, seg_top, seg_width, seg_height,
                  seg, seg_width, seg_height);
        }
      }
      yi.util.io.OutputResult.outputColorImage(new_image, IMAGE_WIDTH, IMAGE_HEIGHT);
    }
    System.exit(0);
  }
}