/* @(#) Zoom.java 1.1 99/11/18
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.imageviewer;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
/** Zooms an image displayed on a canvas.
 * The canvas object must implement the ImageManipulator interface.
 * @version 1.0  18 Nov 1999
 * @author Lawrence Rodrigues
 **/
public class Zoom  implements ZoomController  {
   protected AffineTransform atx = new AffineTransform();
   protected boolean magOn = true;
   protected double magFactor = 1.0;
   protected int magCenterX = 0;
   protected int magCenterY =0;
   protected Point zoomOffset = new Point(0,0);
   protected ImageManipulator imageCanvas;

   public Zoom(){}

   /** @param imagecanvas the component on which the image is drawn.
   */
   public Zoom(ImageManipulator imageCanvas){ this.imageCanvas = imageCanvas;}

   /** @param imagecanvas the component on which the image is drawn.
   */
   public void setImageManipulator(ImageManipulator imageCanvas){
      this.imageCanvas = imageCanvas;
   }

   public  void setMagOn(boolean onOff){ magOn = onOff;}
   public  boolean getMagOn(){ return magOn; }

   public void setMagFactor(double magFactor){
      this.magFactor = magFactor;
      imageCanvas.setMagFactor(magFactor);
   }

   public double getMagFactor(){ return magFactor;}

   public void magnify(int magCenterX, int magCenterY){
       magnify(magCenterX, magCenterY, magFactor);
   }

   public void magnify(int magCenterX, int magCenterY, double magFac){
      setMagFactor(magFac);
      this.magCenterX = magCenterX;
      this.magCenterY = magCenterY;
      Point panOffset = imageCanvas.getPanOffset();
      int x = (int)((magCenterX-panOffset.x)*magFactor)-magCenterX;
      int y = (int)((magCenterY-panOffset.y)*magFactor)-magCenterY;
      atx = imageCanvas.getTransform();
      atx.setToTranslation(-x, -y);
      atx.scale(magFactor, magFactor);
      applyTransform(atx);
   }

   public void paintImage(int magCenterX, int magCenterY, double mag){
      setMagFactor(this.magFactor *mag);
      int dx = this.magCenterX -magCenterX;
      int dy = this.magCenterY-magCenterY;
      this.magCenterX = magCenterX;
      this.magCenterY = magCenterY;
      try {
        Point2D mgp = null;
        atx = imageCanvas.getTransform();
        mgp = atx.inverseTransform((Point2D)(new Point(magCenterX, magCenterY)),(Point2D)mgp);
        double x = (mgp.getX()*mag)-mgp.getX();
        double y = (mgp.getY()*mag)-mgp.getY();
        scale(-x,-y, mag);
      }catch (Exception e) {System.out.println(e); }
   }

   public void scale(double magOffsetX, double magOffsetY, double mag){
      atx.translate(magOffsetX,magOffsetY);
      atx.scale(mag,mag);
      applyTransform(atx);
   }

   public void resetAndScale(double magOffsetX, double magOffsetY, double mag){
      atx.setToTranslation(magOffsetX,magOffsetY);
      atx.scale(mag,mag);
      applyTransform(atx);
  }

   public void applyTransform(AffineTransform atx) {
      imageCanvas.applyTransform(atx);
   }

   public void reset() {
      magCenterX = 0;
      magCenterY =0;
      magFactor = 1.0;
   }
}
