File: Zoom.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (276 lines) | stat: -rw-r--r-- 8,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.*;
import ij.plugin.frame.Recorder;
import java.awt.*;

/** This plugin implements the commands in the Image/Zoom submenu. */
public class Zoom implements PlugIn {

	public static void toSelection(ImagePlus imp) {
		Zoom zoom = new Zoom();
		ImageCanvas ic = imp.getCanvas();
		if (ic!=null)
			zoom.zoomToSelection(imp, ic);
	}

	public static void set(ImagePlus imp, double magnification) {
		Zoom zoom = new Zoom();
		zoom.setZoom(imp, magnification, -1, -1);
	}

	public static void set(ImagePlus imp, double magnification, int x, int y) {
		Zoom zoom = new Zoom();
		zoom.setZoom(imp, magnification, x, y);
	}
	
	public static void in(ImagePlus imp) {
		ImageCanvas ic = imp.getCanvas();
		if (ic==null) return;
		waitUntilActivated(imp);
		int x = ic.screenX(imp.getWidth()/2);
		int y = ic.screenY(imp.getHeight()/2);
		ic.zoomIn(x, y);
		if (ic.getMagnification()<=1.0)
			imp.repaintWindow();
	}

	public static void out(ImagePlus imp) {
		ImageCanvas ic = imp.getCanvas();
		if (ic==null) return;
		waitUntilActivated(imp);
		int x = ic.screenX(imp.getWidth()/2);
		int y = ic.screenY(imp.getHeight()/2);
		ic.zoomOut(x, y);
		if (ic.getMagnification()<=1.0)
			imp.repaintWindow();
	}

	public static void unzoom(ImagePlus imp) {
		ImageCanvas ic = imp.getCanvas();
		if (ic!=null) {
			waitUntilActivated(imp);
			ic.unzoom();
		}
	}
	
	public static void maximize(ImagePlus imp) {
		ImageWindow win = imp.getWindow();
		if (win!=null) {
			waitUntilActivated(imp);
			win.maximize();
			IJ.wait(100);
		}
	}

	private static void waitUntilActivated(ImagePlus imp) {
		int count = 0;
		boolean isCanvas = imp.getCanvas()!=null;
		if (isCanvas) {
			long t0 = System.currentTimeMillis();
			while (!imp.windowActivated() && (System.currentTimeMillis()-t0)<=1000) {
				IJ.wait(10);
				count++;
			}
		}
		if (IJ.debugMode)
			IJ.log("Zoom: "+ count+" "+imp.windowActivated()+" "+isCanvas+" "+imp);
    }

	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")) {
    		waitUntilActivated(imp);
			ic.zoomIn(x, y);
			if (ic.getMagnification()<=1.0)
				imp.repaintWindow();
			Recorder.recordCall("Zoom.in(imp);");
    	} else if (arg.equals("out")) {
    		waitUntilActivated(imp);
			ic.zoomOut(x, y);
			if (ic.getMagnification()<1.0)
				imp.repaintWindow();
			Recorder.recordCall("Zoom.out(imp);");
    	} else if (arg.equals("orig")) {
			unzoom(imp);
			Recorder.recordCall("Zoom.unzoom(imp);");
    	} else if (arg.equals("100%")) {
    	    waitUntilActivated(imp);
    		ic.zoom100Percent();
		} else if (arg.equals("to")) {
			zoomToSelection(imp, ic);
			Recorder.recordCall("Zoom.toSelection(imp);");
		} else if (arg.equals("set")) {
			setZoom(imp, -1, -1, -1);
		} else if (arg.equals("max")) {
			maximize(imp);
			Recorder.recordCall("Zoom.maximize(imp);");
		} else if (arg.equals("scale"))
			scaleToFit(imp);
	}
		
	void zoomToSelection(ImagePlus imp, ImageCanvas ic) {
		waitUntilActivated(imp);
		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<w.width-marginw && r.height*mag<w.height-marginh) {
			ic.zoomIn(ic.screenX(x), ic.screenY(y));
			double cmag = ic.getMagnification();
			if (cmag==32.0) break;
			mag = ic.getHigherZoomLevel(cmag);
			w = imp.getWindow().getBounds();
		}
	}
	
	/** Based on Albert Cardona's ZoomExact plugin:
		http://albert.rierol.net/software.html */
	void setZoom(ImagePlus imp, double mag, int x, int y) {
		waitUntilActivated(imp);
		ImageCanvas ic = imp.getCanvas();
		if (ic==null)
			return;
		int width = imp.getWidth();
		int height = imp.getHeight();
		if (x==-1) {
			x = width/2;
			y = height/2;
		}
		Rectangle srcRect = ic.getSrcRect();
		Roi roi = imp.getRoi();
		boolean areaSelection = false;
		if (roi!=null) {
			Rectangle bounds = roi.getBounds();
			x = bounds.x + bounds.width/2;
			y = bounds.y + bounds.height/2;
			areaSelection = roi.isArea();
			if (areaSelection)
				srcRect = bounds;
		}
		ImageWindow win = imp.getWindow();
		int srcWidth = srcRect.width;
		int srcHeight = srcRect.height;
		if (mag==-1) {
			GenericDialog gd = new GenericDialog("Set Zoom");
			gd.addNumericField("Zoom:", ic.getMagnification() * 200, 0, 4, "%");
			gd.addNumericField("X center:", x, 0, 5, "");
			gd.addNumericField("Y center:", y, 0, 5, "");
			if (areaSelection) {
				gd.addNumericField("Width:", srcRect.width, 0, 5, "");
				gd.addNumericField("Height:", srcRect.height, 0, 5, "");
			}
			gd.showDialog();
			if (gd.wasCanceled()) return;
			mag = gd.getNextNumber()/100.0;
			int x2 = (int)gd.getNextNumber();
			int y2 = (int)gd.getNextNumber();
			boolean defaultLocation = x==x2 && y==y2;
			x = x2;
			y = y2;
			if (areaSelection) {
				srcWidth = (int)gd.getNextNumber();
				srcHeight = (int)gd.getNextNumber();
			}
			if (defaultLocation)
				Recorder.recordCall("Zoom.set(imp, "+mag+");");
			else
				Recorder.recordCall("Zoom.set(imp, "+mag+", "+x+", "+y+");");
		}
		if (x<0) x=0;
		if (y<0) y=0;
		String options = IJ.macroRunning()?Macro.getOptions():null;
		boolean legacyMacro = areaSelection && options!=null && options.contains("x=") && !options.contains("width=");
		Rectangle bounds = GUI.getMaxWindowBounds(win);
		boolean smallImage = mag>1.0 && width*mag<bounds.width && height*mag<bounds.height;
		if ((areaSelection||smallImage||srcWidth!=srcRect.width||srcHeight!=srcRect.height) && !legacyMacro) {
			if (areaSelection && roi.getType()==Roi.RECTANGLE)
				imp.deleteRoi();
			Insets insets = win.getInsets();
			int canvasWidth = (int)(srcWidth*mag+insets.right+insets.left+ImageWindow.HGAP*2);
			int canvasHeight = (int)(srcHeight*mag+insets.top+insets.bottom+ImageWindow.VGAP*2+win.getSliderHeight());
			ic.setSourceRect(new Rectangle(x-srcWidth/2,y-srcHeight/2,srcWidth,srcHeight));
			ic.setMagnification(mag);
			ic.setSize(new Dimension((int)(srcWidth*mag), (int)(srcHeight*mag)));
			win.setSize(canvasWidth, canvasHeight);
			return;
		}
		if (x>=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)<size.width)
				srcWidth++;
			if ((int)(srcHeight*mag)<size.height)
				srcHeight++;
			x = x-srcWidth/2;
			y = y-srcHeight/2;
			if (x+srcWidth>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();
		IJ.wait(100);
	}
	
	private void scaleToFit(ImagePlus imp) {
		waitUntilActivated(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)");
		}
	}
	
}