/** TiledRenderableImageCanvas Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.render;
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 java.awt.image.renderable.*;
import javax.media.jai.*;

/** A component class for drawing large renderable images. 
  * @version 1.0 18 Apr 2000
  * @author Lawrence Rodrigues
  **/
public class TiledRenderableImageCanvas extends RenderedImageCanvas  {
   public final static int TO_FIT =0;
   public final static int SCALED = 1;
   public final static int ORIG_SIZE = 4;

   protected RenderableImage sourceImage;
   protected  int interpolationMode = Interpolation.INTERP_BILINEAR;

   protected int displayMode = ORIG_SIZE;
   protected float sourceImageHeight = 1.0f;
   protected float sourceImageWidth = 1.0f;
   protected int maxHeight =1024, maxWidth = 1024;

   public TiledRenderableImageCanvas() {}

   public TiledRenderableImageCanvas(RenderableImage img){
      atx = new AffineTransform();
      setImage(img);
   }

   public void setImage(RenderableImage pixelLessImg){
      PlanarImage img = createPlanarImage(pixelLessImg);
      displayImage =  makeTiledImage(img);
      sampleModel = displayImage.getSampleModel();
      colorModel = displayImage.getColorModel();
      getTileInfo(displayImage);
      imageDrawn = false;
      repaint();
   }

   public void setDisplayMode(int dispMode) {
      displayMode = dispMode;
      setImage(sourceImage);
   }
   public int getDisplayMode(){ return displayMode;}

   public void setOrigImageSize(Dimension size){
      maxWidth = size.width;
      maxHeight = size.height;
   }
   public void setInterpolationMode(int interpMode) { interpolationMode = interpMode;}
   public int getInterpolationMode(){ return interpolationMode;}
   protected PlanarImage createPlanarImage(RenderableImage pixelLessImg){
      panX =0; panY =0;
      sourceImage = pixelLessImg;
      sourceImageHeight = sourceImage.getHeight();
      sourceImageWidth = sourceImage.getWidth();
      return createPixelImage(pixelLessImg);
   }

   protected PlanarImage createPixelImage(RenderableImage  pixelLessImg){
      RenderingHints hints = new RenderingHints(JAI.KEY_INTERPOLATION,
                    Interpolation.getInstance(interpolationMode));
      int wid = viewerWidth, ht = viewerHeight;
      double aspectRatio = sourceImageWidth/sourceImageHeight;
      switch (displayMode) {
          case SCALED:
           if(aspectRatio > 1.00)  ht =  (int)(viewerHeight/aspectRatio);
           else  wid = (int)(viewerWidth*aspectRatio);
           break;
         case ORIG_SIZE:
           ht = maxHeight;  wid = maxWidth;
           break;
         case TO_FIT:
      }
      if(sourceImage == null) return null;
      return (PlanarImage) sourceImage.createScaledRendering(wid, ht, hints);
   }

   public void setTileWidth(int tw){
      tileWidth = tw;
      setImage(sourceImage);
      repaint();
   }
   public void setTileHeight(int th){
      tileHeight = th;
      setImage(sourceImage);
      repaint();
   }
}
