/** ROIStats Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.roi;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import com.vistech.util.*;
import com.vistech.jai.imageio.*;
import com.vistech.jai.render.*;

import java.awt.image.RenderedImage;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
import java.awt.image.renderable.*;

/**
  * A utility class that provides a number of static methods for computing ROI-related data.
  * @version 1.0  21 Nov 2000
  * @author Lawrence Rodrigues
  */
public class ROIStats {
   public static double[][] getMinMax(PlanarImage image, ROI roi) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(roi);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("extrema", pb);
      double[][] minmax = (double[][]) op.getProperty("extrema");
      return minmax;
   }

   public static double[][] getMinMax(PlanarImage image) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(null);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("extrema", pb);
      double[][] minmax = (double[][]) op.getProperty("extrema");
      return minmax;
   }

   public static double[] getMean(PlanarImage image) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(null);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("mean", pb);
      double[] mean = (double[]) op.getProperty("mean");
      return mean;
   }

    public static double[] getMean(PlanarImage image, ROI roi) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(roi);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("mean", pb);
      double[] mean = (double[]) op.getProperty("mean");
      return mean;
   }

   public TiledImage createTiledImage(PlanarImage image) {
      SampleModel sampleModel = image.getSampleModel();
      ColorModel colorModel = image.getColorModel();

      TiledImage tlImage = new TiledImage(image.getMinX(), image.getMinY(),
                                    image.getWidth(), image.getHeight(),
                                    image.getTileGridXOffset(), image.getTileGridYOffset(),
                                    sampleModel, colorModel);
      tlImage.setData(image.copyData());

      return tlImage;
   }

   public static PlanarImage loadImage(String filename) {
      return JAI.create("fileload", filename);
   }


   public static int[][] computeHistData(PlanarImage image, int numbins) {
      if (image == null) return null;
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(null);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("extrema", pb);
      double[][] minmax = (double[][]) op.getProperty("extrema");

      int numbands = image.getSampleModel().getNumBands();
      int[] bins =  new int[numbands];
      for(int i=0; i< numbands;i++) {
          bins[i] = 12;
      }
      javax.media.jai.Histogram his = new  javax.media.jai.Histogram(bins, minmax[0], minmax[1]);
      ParameterBlock pb1 = new ParameterBlock();
      pb1.addSource(image);
      pb1.add(his);
      pb1.add(null);
      pb1.add(1);
      pb1.add(1);
      RenderedOp op1 = JAI.create("histogram", pb1);
      javax.media.jai.Histogram hist = (javax.media.jai.Histogram) op1.getProperty("histogram");
      return hist.getBins();
  }

   public static double[][] computeMinMax(PlanarImage image) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(null);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("extrema", pb);
      double[][] minmax = (double[][]) op.getProperty("extrema");
      return minmax;
   }

   public static double[][] computeMinMax(PlanarImage image, ROIShape roi) {
      if(image == null) return null;
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add(roi);
      pb.add(1);
      pb.add(1);
      RenderedOp op = JAI.create("extrema", pb);
      double[][] minmax = (double[][]) op.getProperty("extrema");
      return minmax;
   }

   public static int[][] computeHistData(PlanarImage image,ROIShape roi, int numbins) {
   
      double[][] minmax = computeMinMax(image);
      int numbands = image.getSampleModel().getNumBands();
      int[] bins =  new int[numbands];
      for(int i=0; i< numbands;i++) {
          bins[i] = numbins;
      }
      javax.media.jai.Histogram his = new  javax.media.jai.Histogram(bins, minmax[0], minmax[1]);
      ParameterBlock pb1 = new ParameterBlock();
      pb1.addSource(image);
      pb1.add(his);
      pb1.add(roi);
      pb1.add(1);
      pb1.add(1);
      RenderedOp op1 = JAI.create("histogram", pb1);
      javax.media.jai.Histogram hist = (javax.media.jai.Histogram) op1.getProperty("histogram");
      return hist.getBins();
  }

   public static void  main(String[] args){
      ROIStats rs = new ROIStats();
      PlanarImage image = rs.loadImage(args[0]);
      TiledImage tl = rs.createTiledImage(image);
      rs.getMean(tl);
      rs.getMinMax(tl);
   }
}
