ij.plugin.Zoom Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of imagej Show documentation
Show all versions of imagej Show documentation
Image processing and analysis framework. Allows the user to
record/replay macros and can be extended using plug-ins.
The newest version!
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{
/** 'arg' must be "in", "out", "100%" or "orig". */
public void run(String arg) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null)
{IJ.noImage(); return;}
ImageCanvas ic = imp.getCanvas();
if (ic==null) 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();
win.setBounds(win.getMaximumBounds());
win.maximize();
}
}
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*mag=imp.getWidth()) x=imp.getWidth()-1;
if (y>=imp.getHeight()) y=imp.getHeight()-1;
if (mag<=0.0) mag = 1.0;
win.getCanvas().setMagnification(mag);
double w = imp.getWidth()*mag;
double h = imp.getHeight()*mag;
Dimension screen = IJ.getScreenSize();
if (w>screen.width-20) w = screen.width - 20; // does it fit?
if (h>screen.height-50) h = screen.height - 50;
int width = (int)(w/mag);
int height = (int)(h/mag);
x -= width/2;
y -= height/2;
if (x<0) x=0;
if (y<0) y=0;
ic.setSourceRect(new Rectangle(x, y, width, height));
ic.setDrawingSize((int)w, (int)h);
win.pack();
ic.repaint();
}
}