/*
 * @(#) ROIContext.java 1.0 2000/10/3
 * Copyright (c) 2000 Larry Rodrigues
 */
package com.vistech.roi;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
import javax.swing.*;
import java.awt.color.*;
import java.util.*;

/** 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 ROIContext {
   protected BufferedImage originalImage;
   protected BufferedImage displayImage;
   private AffineTransform atx = new AffineTransform();

   /**
     * @param img the image on which ROIs are to be deposited.
     */
   public ROIContext (BufferedImage img) {
      originalImage = img;
      createDisplayImage();
   }

   /** Returns the original image.
     * @return img the original image.
     */
    public BufferedImage getOriginalImage(){
      return originalImage;
   }

   /** Returns the image that is displyed on the screen. This may contain
     * deposited ROIs.
     * @return img the displyed image on which ROIs are deposited.
     */
   public BufferedImage getDisplayImage(){
      return displayImage;
   }

   /** Creates a copy of the original image for depositing the ROIs.
     **/
   protected void createDisplayImage(){
      AffineTransformOp atop =
           new AffineTransformOp(atx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
      displayImage = atop.createCompatibleDestImage(originalImage,
                                                originalImage.getColorModel());
      displayImage = atop.filter(originalImage, displayImage);
   }

   /** Returns the writable raster of the original image.
     * @return the writable raster of the original image.
     */
    public WritableRaster getOriginalRaster() {
      if(originalImage != null) return originalImage.getRaster();
      else return null;
   }

   /** Returns the writable raster of the displayed image.
     * @return the writable raster of the displyed image.
     */
   public WritableRaster getDisplayRaster() {
      if(displayImage!= null) return displayImage.getRaster();
      else return null;
   }

   /** Returns the names of the pixel components as an array of strings.
     * For example, this will return {"red", "green", "blue"} in th case of an
     * RGB image.
     * @return an array of sample names.
     */
   public String[] getComponentNames() {
      if(originalImage == null) return null;
      ColorModel cm = originalImage.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;
   }

   /** Resets the context. Creates a new image for depositing ROIs.
    All the previously drawn ROIs will be lost when you call this method */
   public void reset(){
      displayImage.flush();
      displayImage = null;
      createDisplayImage();
   }
}