File: TextEncoder.java

package info (click to toggle)
imagej 1.46a-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: java: 89,778; sh: 311; xml: 51; makefile: 6
file content (54 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download | duplicates (8)
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
package ij.io;
import java.io.*;
import ij.*;
import ij.process.*;
import ij.measure.*;

/** Saves an image described by an ImageProcessor object as a tab-delimited text file. */
public class TextEncoder {

	private ImageProcessor ip;
	private Calibration cal;
	private int precision;

	/** Constructs a TextEncoder from an ImageProcessor and optional Calibration. */
	public TextEncoder (ImageProcessor ip, Calibration cal, int precision) {
		this.ip = ip;
		this.cal = cal;
		this.precision = precision;
	}

	/** Saves the image as a tab-delimited text file. */
	public void write(DataOutputStream out) throws IOException {
		PrintWriter pw = new PrintWriter(out);
		boolean calibrated = cal!=null && cal.calibrated();
		if (calibrated)
			ip.setCalibrationTable(cal.getCTable());
		else
			ip.setCalibrationTable(null);
		boolean intData = !calibrated && ((ip instanceof ByteProcessor) || (ip instanceof ShortProcessor));
		int width = ip.getWidth();
		int height = ip.getHeight();
		int inc = height/20;
		if (inc<1) inc = 1;
		//IJ.showStatus("Exporting as text...");
		double value;
		for (int y=0; y<height; y++) {
			for (int x=0; x<width; x++) {
				value = ip.getPixelValue(x,y);
				if (intData)
					pw.print((int)value);
				else
					pw.print(IJ.d2s(value, precision));
				if (x!=(width-1))
					pw.print("\t");
			}
			pw.println();
			if (y%inc==0) IJ.showProgress((double)y/height);
		}
		pw.close();
		IJ.showProgress(1.0);
		//IJ.showStatus("");
	}
	
}