File: XY_Reader.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (58 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (6)
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
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.measure.*;
import ij.plugin.TextReader;

/** This plugin implements the File/Import/XY Coordinates command. It reads a
	two column text file, such as those created by File/Save As/XY Coordinates,
	as a polygon ROI. The ROI is displayed in the current image or, if the image
	is too small, in a new blank image.
*/
public class XY_Reader implements PlugIn {

	public void run(String arg) {
		TextReader tr = new TextReader();
		ImageProcessor ip = tr.open();
		if (ip==null)
			return;
		int width = ip.getWidth();
		int height = ip.getHeight();
		if (width!=2 || height<3) {
			IJ.showMessage("XY Reader", "Two column text file required");
			return;
		}
		float[] x = new float[height];
		float[] y = new float[height];
		boolean allIntegers = true;
		double length = 0.0;
		for (int i=0; i<height; i++) {
			x[i] = ip.getf(0, i);
			y[i] = ip.getf(1, i);
			if ((int)x[i]!=x[i] || (int)y[i]!=y[i])
				allIntegers = false;
			if (i>0) {
				double dx = x[i] - x[i-1];
				double dy = y[i] - y[i-1];
				length += Math.sqrt(dx*dx+dy*dy);
			}
		}
		Roi roi = null;
		int type = length/x.length>10?Roi.POLYGON:Roi.FREEROI;
		if (allIntegers)
			roi = new PolygonRoi(Roi.toIntR(x), Roi.toIntR(y), height, type);
		else
			roi = new PolygonRoi(x, y, height, type);
		Rectangle r = roi.getBoundingRect();
		ImagePlus imp = WindowManager.getCurrentImage();
		if (imp==null || imp.getWidth()<r.x+r.width || imp.getHeight()<r.y+r.height) {
			new ImagePlus(tr.getName(), new ByteProcessor(Math.abs(r.x)+r.width+10, Math.abs(r.y)+r.height+10)).show();
			imp = WindowManager.getCurrentImage();
		}
		if (imp!=null)
			imp.setRoi(roi);
	}
	
}