Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
TextEncoder.java
1 package ij.io;
2 import java.io.*;
3 import ij.*;
4 import ij.process.*;
5 import ij.measure.*;
6 
8 public class TextEncoder {
9 
10  private ImageProcessor ip;
11  private Calibration cal;
12  private int precision;
13 
15  public TextEncoder (ImageProcessor ip, Calibration cal, int precision) {
16  this.ip = ip;
17  this.cal = cal;
18  this.precision = precision;
19  }
20 
22  public void write(DataOutputStream out) throws IOException {
23  PrintWriter pw = new PrintWriter(out);
24  boolean calibrated = cal!=null && cal.calibrated();
25  if (calibrated)
26  ip.setCalibrationTable(cal.getCTable());
27  else
28  ip.setCalibrationTable(null);
29  boolean intData = !calibrated && ((ip instanceof ByteProcessor) || (ip instanceof ShortProcessor));
30  int width = ip.getWidth();
31  int height = ip.getHeight();
32  int inc = height/20;
33  if (inc<1) inc = 1;
34  //IJ.showStatus("Exporting as text...");
35  double value;
36  for (int y=0; y<height; y++) {
37  for (int x=0; x<width; x++) {
38  value = ip.getPixelValue(x,y);
39  if (intData)
40  pw.print((int)value);
41  else
42  pw.print(IJ.d2s(value, precision));
43  if (x!=(width-1))
44  pw.print("\t");
45  }
46  pw.println();
47  if (y%inc==0) IJ.showProgress((double)y/height);
48  }
49  pw.close();
50  IJ.showProgress(1.0);
51  //IJ.showStatus("");
52  }
53 
54 }