package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.*;
import java.awt.*;
/** This plugin implements the commands in the Image/Zoom submenu. */
public class Zoom implements PlugIn{
public void run(String arg) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null)
{IJ.noImage(); return;}
ImageCanvas ic = imp.getCanvas();
if (ic==null) return;
if (ic instanceof PlotCanvas && !((PlotCanvas)ic).isFrozen()) {
((PlotCanvas)ic).zoom(arg);
return;
}
Point loc = ic.getCursorLoc();
if (!ic.cursorOverImage()) {
Rectangle srcRect = ic.getSrcRect();
loc.x = srcRect.x + srcRect.width/2;
loc.y = srcRect.y + srcRect.height/2;
}
int x = ic.screenX(loc.x);
int y = ic.screenY(loc.y);
if (arg.equals("in")) {
ic.zoomIn(x, y);
if (ic.getMagnification()<=1.0) imp.repaintWindow();
} else if (arg.equals("out")) {
ic.zoomOut(x, y);
if (ic.getMagnification()<1.0) imp.repaintWindow();
} else if (arg.equals("orig"))
ic.unzoom();
else if (arg.equals("100%"))
ic.zoom100Percent();
else if (arg.equals("to"))
zoomToSelection(imp, ic);
else if (arg.equals("set"))
setZoom(imp, ic);
else if (arg.equals("max")) {
ImageWindow win = imp.getWindow();
if (win!=null) {
win.maximize();
IJ.wait(100);
}
} else if (arg.equals("scale"))
scaleToFit(imp);
}
void zoomToSelection(ImagePlus imp, ImageCanvas ic) {
Roi roi = imp.getRoi();
ic.unzoom();
if (roi==null) return;
Rectangle w = imp.getWindow().getBounds();
Rectangle r = roi.getBounds();
double mag = ic.getMagnification();
int marginw = (int)((w.width - mag * imp.getWidth()));
int marginh = (int)((w.height - mag * imp.getHeight()));
int x = r.x+r.width/2;
int y = r.y+r.height/2;
mag = ic.getHigherZoomLevel(mag);
while(r.width*mag1.0 && width*mag=width) x=width-1;
if (y>=height) y=height-1;
if (mag<=0.0) mag = 1.0;
ic.setMagnification(mag);
double newWidth = width*mag;
double newHeight = height*mag;
Dimension size = ic.getSize();
if (newWidth>=size.width && newHeight>=size.height) {
srcWidth = (int)Math.round(size.width/mag);
srcHeight = (int)Math.round(size.height/mag);
if ((int)(srcWidth*mag)width)
x = width - srcWidth;
if (y+srcHeight>height)
y = height - srcHeight;
if (x<0) x=0;
if (y<0) y=0;
ic.setSourceRect(new Rectangle(x, y, srcWidth, srcHeight));
} else {
srcWidth = width;
srcHeight = height;
ic.setSize((int)newWidth, (int)newHeight);
ic.setSourceRect(new Rectangle(0, 0, srcWidth, srcHeight));
win.pack();
}
ic.repaint();
}
private void scaleToFit(ImagePlus imp) {
ImageCanvas ic = imp.getCanvas();
if (ic==null)
return;
if (ic.getScaleToFit()) {
ic.setScaleToFit(false);
ic.unzoom();
IJ.showStatus("Exiting scale to fit mode (resize with 'alt' key to scale to fit)");
} else {
ic.setScaleToFit(true);
ic.fitToWindow();
IJ.showStatus("Resize window to scale (use 'alt' key as shortcut)");
}
}
}
Ads help maintain this website.