package yi.iu.img;

import java.awt.*;
import yi.iu.processor.color.*;
import yi.iu.processor.channel.*;

/**
 * Calculate the differential image in CIELab color space
 */

public class HSIDifferentialImage implements DifferentialImage{

  double[][] m_horizontalDifferentials;
  double[][] m_verticalDifferentials;

  public HSIDifferentialImage(int[] pixels, Dimension d) {
    CYMaker cymaker = new CYMaker();
    int[][] hsi = cymaker.getHSI(pixels, d);

    // calculate horizontal differentials
    m_horizontalDifferentials = new double[d.height][d.width - 1];
    for (int y = 0; y < d.height; y++) {
      for (int x = 0, ptr = y*d.width; x < d.width - 1; x++, ptr++) {
        int[] lab1 = hsi[ptr], lab2 = hsi[ptr+1];
        m_horizontalDifferentials[y][x] = yi.math.VectorUtil.EuclideanDistance(
            lab1, lab2);
      }
    }

    // calculate vertial differentials
    m_verticalDifferentials = new double[d.height - 1][d.width];
    for (int y = 0, ptr=0, ptr2=d.width; y < d.height - 1; y++) {
      for (int x = 0; x < d.width; x++, ptr++, ptr2++) {
        int[] lab1 = hsi[ptr], lab2 = hsi[ptr2];
        m_verticalDifferentials[y][x] = yi.math.VectorUtil.EuclideanDistance(
            lab1, lab2);
      }
    }
  }

  /**
   * Return the distance between (x,y) and (x+1, y)
   * @param x
   * @param y
   * @return
   */
  public double getHorizontalDistance(int x, int y) {
    return m_horizontalDifferentials[y][x];
  }

  /**
   * Return the distance between (x,y) and (x, y+1)
   * @param x
   * @param y
   * @return
   */
  public double getVerticalDistance(int x, int y) {
    return m_verticalDifferentials[y][x];
  }

}
