private void doParalellpiped() {

    int iNX, iNY;
    int x, y;
    int iMatchingClass = 0;
    int iClass, iGrid;
    final double dMean[][] = new double[m_Classes.size()][m_Window.length];
    final double dStdDev[][] = new double[m_Classes.size()][m_Window.length];
    double dValue;
    ArrayList stats;
    MeanAndStdDev substats;
    Set set;
    Iterator iter;

    iNX = m_Output.getWindowGridExtent().getNX();
    iNY = m_Output.getWindowGridExtent().getNY();

    set = m_Classes.keySet();
    iter = set.iterator();
    iClass = 0;
    while (iter.hasNext()) {
      stats = (ArrayList) m_Classes.get(iter.next());
      for (iGrid = 0; iGrid < m_Window.length; iGrid++) {
        substats = ((MeanAndStdDev) stats.get(iGrid));
        dMean[iClass][iGrid] = substats.mean;
        dStdDev[iClass][iGrid] = substats.stdDev;
      }
      iClass++;
    }

    for (y = 0; y < iNY; y++) {
      for (x = 0; x < iNX; x++) {
        for (iClass = 0; iClass < m_Classes.size(); iClass++) {
          iMatchingClass = iClass;
          for (iGrid = 0; iGrid < m_Window.length; iGrid++) {
            dValue = m_Window[iGrid].getCellValueAsDouble(x, y);
            if (!m_Window[iGrid].isNoDataValue(dValue)) {
              if (Math.abs(m_Window[iGrid].getCellValueAsDouble(x, y) - dMean[iClass][iGrid])
                  > dStdDev[iClass][iGrid]) {
                iMatchingClass = -1;
                break;
              }
            } else {
              break;
            }
          }
          if (iMatchingClass != -1) {
            break;
          }
        }
        if (iMatchingClass != -1) {
          m_Output.setCellValue(x, y, iMatchingClass + 1);
        } else {
          m_Output.setNoData(x, y);
        }
      }
    }
  }
  private void doMinimumDistance() {

    int iNX, iNY;
    int x, y;
    int iClass, iGrid, iMin = 0;
    final double dMean[][] = new double[m_Classes.size()][m_Window.length];
    double dMin, d, e;
    double dValue;
    ArrayList stats;
    Set set;
    Iterator iter;

    iNX = m_Output.getWindowGridExtent().getNX();
    iNY = m_Output.getWindowGridExtent().getNY();

    set = m_Classes.keySet();
    iter = set.iterator();
    iClass = 0;
    while (iter.hasNext()) {
      stats = (ArrayList) m_Classes.get(iter.next());
      for (iGrid = 0; iGrid < m_Window.length; iGrid++) {
        dMean[iClass][iGrid] = ((MeanAndStdDev) stats.get(iGrid)).mean;
      }
      iClass++;
    }

    for (y = 0; y < iNY; y++) {
      for (x = 0; x < iNX; x++) {
        for (iClass = 0, dMin = -1.0; iClass < m_Classes.size(); iClass++) {
          for (iGrid = 0, d = 0.0; iGrid < m_Window.length; iGrid++) {
            dValue = m_Window[iGrid].getCellValueAsDouble(x, y);
            if (!m_Window[iGrid].isNoDataValue(dValue)) {
              e = m_Window[iGrid].getCellValueAsDouble(x, y) - dMean[iClass][iGrid];
              d += e * e;
              if ((dMin < 0.0) || (dMin > d)) {
                dMin = d;
                iMin = iClass;
              }
            } else {
              dMin = -1;
            }
          }
        }

        if (dMin >= 0.0) {
          m_Output.setCellValue(x, y, iMin + 1);
        } else {
          m_Output.setNoData(x, y);
        }
      }
    }
  }
  @Override
  public boolean processAlgorithm() throws GeoAlgorithmExecutionException {

    int i;
    AnalysisExtent ge;

    final int iMethod = m_Parameters.getParameterValueAsInt(METHOD);
    m_Bands = m_Parameters.getParameterValueAsArrayList(INPUT);

    if (m_Bands.size() == 0) {
      return false;
    }

    m_Classes = new HashMap();

    getClassInformation();

    if (m_Task.isCanceled()) {
      return false;
    }

    m_Output =
        getNewRasterLayer(
            CLASSIFICATION,
            Sextante.getText("Classification"),
            IRasterLayer.RASTER_DATA_TYPE_SHORT);
    m_Output.setNoDataValue(-1);
    ge = m_Output.getWindowGridExtent();

    m_Window = new IRasterLayer[m_Bands.size()];
    m_iBands = new int[m_Bands.size()];
    for (i = 0; i < m_Window.length; i++) {
      final RasterLayerAndBand band = (RasterLayerAndBand) m_Bands.get(i);
      m_iBands[i] = band.getBand();
      m_Window[i] = band.getRasterLayer();
      m_Window[i].setWindowExtent(ge);
    }

    switch (iMethod) {
      case 0:
        doParalellpiped();
      case 1:
      default:
        doMinimumDistance();
      case 2:
        doMaximumLikelihood();
    }

    return !m_Task.isCanceled();
  }
  private void doMaximumLikelihood() {

    int iNX, iNY;
    int x, y;
    int iClass, iGrid, iMax = 0;
    final double dMean[][] = new double[m_Classes.size()][m_Window.length];
    final double dStdDev[][] = new double[m_Classes.size()][m_Window.length];
    final double dK[][] = new double[m_Classes.size()][m_Window.length];
    double dMax, d, e;
    double dValue;
    ArrayList stats;
    MeanAndStdDev substats;
    Set set;
    Iterator iter;

    iNX = m_Output.getWindowGridExtent().getNX();
    iNY = m_Output.getWindowGridExtent().getNY();

    set = m_Classes.keySet();
    iter = set.iterator();
    iClass = 0;
    while (iter.hasNext()) {
      stats = (ArrayList) m_Classes.get(iter.next());
      for (iGrid = 0; iGrid < m_Window.length; iGrid++) {
        substats = ((MeanAndStdDev) stats.get(iGrid));
        dMean[iClass][iGrid] = substats.mean;
        dStdDev[iClass][iGrid] = substats.stdDev;
        dK[iClass][iGrid] = 1.0 / (dStdDev[iClass][iGrid] * Math.sqrt(2.0 * Math.PI));
      }
      iClass++;
    }

    for (y = 0; y < iNY; y++) {
      for (x = 0; x < iNX; x++) {
        for (iClass = 0, dMax = 0.0; iClass < m_Classes.size(); iClass++) {
          for (iGrid = 0, d = 0.0; iGrid < m_Window.length; iGrid++) {
            dValue = m_Window[iGrid].getCellValueAsDouble(x, y);
            if (!m_Window[iGrid].isNoDataValue(dValue)) {
              e =
                  (m_Window[iGrid].getCellValueAsDouble(x, y) - dMean[iClass][iGrid])
                      / dStdDev[iClass][iGrid];
              e = dK[iClass][iGrid] * Math.exp(-0.5 * e * e);
              d += e * e;
              if (dMax < d) {
                dMax = d;
                iMax = iClass;
              }
            } else {
              dMax = -1;
            }
          }
        }

        if (dMax > 0.0) {
          m_Output.setCellValue(x, y, iMax + 1);
        } else {
          m_Output.setNoData(x, y);
        }
      }
    }
  }