/*
 * @(#) ROIContextJAI.java 1.0 2000/10/3
 * Copyright (c) 2000 Larry Rodrigues
 */
package com.vistech.jai.roi;
import java.awt.*;
import java.awt.color.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import javax.media.jai.*;
/** A convenient class that represents the context of an ROI. It contains the image
  * in which the ROI is deposited. All the ROIs deposited on an image have the same ROI context.
  * @version 1.0  3 Oct 2000
  * @author Lawrence Rodrigues
  */
public class ROIContextJAI {
   protected PlanarImage origImage;
   protected TiledImage displayImage;
   protected Vector roiCollection = new Vector();
   protected ROI currentROI;

   private AffineTransform atx = new AffineTransform();
   public ROIContextJAI (PlanarImage img) {
      origImage = img;
      roiCollection = new Vector();
      reset();
   }

   public PlanarImage getOriginalImage(){ return origImage;}

   public TiledImage getDisplayImage(){return displayImage;}

   protected void createDisplayImage(){
      SampleModel sampleModel = origImage.getSampleModel();
      ColorModel colorModel = origImage.getColorModel();

      displayImage = new TiledImage(origImage.getMinX(), origImage.getMinY(),
                                    origImage.getWidth(), origImage.getHeight(),
                                    origImage.getTileGridXOffset(),
                                    origImage.getTileGridYOffset(),
                                    sampleModel, colorModel);
      displayImage.setData(origImage.copyData());
   }

   public WritableRaster getOriginalRaster() {
      if(origImage != null) return origImage.copyData();
      return null;
   }

   public WritableRaster getDisplayRaster() {
      if(displayImage!= null) return displayImage.copyData();
      return null;
   }

   public void setCurrentROI(ROI roi){ currentROI = roi;}
   public ROI getCurrentROI() { return currentROI;}

   public void addROI(ROI roi){roiCollection.add(roi);}
   public void removeROI(ROI roi) {roiCollection.remove(roi);}

   public void depositROI(ROIShape roi) {
      displayImage.setProperty("ROI", roiCollection);
   }

   public String[] getComponentNames() {
      if(origImage == null) return null;
      ColorModel cm = origImage.getColorModel();
      ColorSpace cs = cm.getColorSpace();
      int numComponents = cs.getNumComponents();
      String[] componentName = new String[numComponents];
      for(int i=0;i<numComponents;i++){
           componentName[i] = cs.getName(i);
      }
      return componentName;
   }
   public void reset(){
      displayImage = null;
      createDisplayImage();
   }
}