Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImageProperties.java
1 package ij.plugin.filter;
2 import ij.*;
3 import ij.process.*;
4 import ij.gui.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.Locale;
8 import ij.measure.Calibration;
9 
10 public class ImageProperties implements PlugInFilter {
11  ImagePlus imp;
12  static final int NANOMETER=0, MICROMETER=1, MILLIMETER=2, CENTIMETER=3,
13  METER=4, KILOMETER=5, INCH=6, FOOT=7, MILE=8, PIXEL=9, OTHER_UNIT=10;
14  int oldUnitIndex;
15  double oldUnitsPerCm;
16  double oldScale;
17 
18 
19  public int setup(String arg, ImagePlus imp) {
20  this.imp = imp;
21  return DOES_ALL+NO_CHANGES;
22  }
23 
24  public void run(ImageProcessor ip) {
25  showDialog(imp);
26  }
27 
28  void showDialog(ImagePlus imp) {
29  Calibration cal = imp.getCalibration();
30  oldUnitIndex = getUnitIndex(cal.getUnit());
31  oldUnitsPerCm = getUnitsPerCm(oldUnitIndex);
32  GenericDialog gd = new ImagePropertiesDialog(imp.getTitle(), this);
33  gd.addStringField("Unit of Length:", cal.getUnit());
34  oldScale = cal.pixelWidth!=0?1.0/cal.pixelWidth:0;
35  gd.addNumericField("Pixels/Unit:", oldScale, (int)oldScale==oldScale?0:3);
36  int stackSize = imp.getStackSize();
37  if (stackSize>1) {
38  gd.addMessage("");
39  gd.addNumericField("Slice Spacing:", cal.pixelDepth, (int)cal.pixelDepth==cal.pixelDepth?0:3);
40  double fps = cal.frameInterval>0.0?1/cal.frameInterval:0.0;
41  gd.addNumericField("Frames per Second:", fps, (int)fps==fps?0:2); }
42  gd.showDialog();
43  if (gd.wasCanceled())
44  return;
45  String unit = gd.getNextString();
46  if (unit.equals("um"))
47  unit = IJ.micronSymbol + "m";
48  else if (unit.equals("u"))
49  unit = "" + IJ.micronSymbol;
50  else if (unit.equals("A"))
51  unit = ""+IJ.angstromSymbol;
52  double resolution = gd.getNextNumber();
53  if (unit.equals("")||unit.equalsIgnoreCase("pixel")
54  ||unit.equalsIgnoreCase("none")||resolution==0.0) {
55  cal.setUnit(null);
56  cal.pixelWidth = 1.0;
57  cal.pixelHeight = 1.0;
58  } else {
59  cal.setUnit(unit);
60  cal.pixelWidth = 1.0/resolution;
61  cal.pixelHeight = 1.0/resolution;
62  }
63  if (stackSize>1) {
64  double spacing = gd.getNextNumber();
65  double fps = gd.getNextNumber();
66  cal.pixelDepth = spacing;
67  if (fps!=0.0)
68  cal.frameInterval = 1.0/fps;
69  else
70  cal.frameInterval = 0.0;
71  }
72  imp.repaintWindow();
73  }
74 
75  double getNewScale(String newUnit) {
76  if (oldUnitsPerCm==0.0)
77  return 0.0;
78  double newScale = 0.0;
79  int newUnitIndex = getUnitIndex(newUnit);
80  if (newUnitIndex!=oldUnitIndex) {
81  double newUnitsPerCm = getUnitsPerCm(newUnitIndex);
82  if (oldUnitsPerCm!=0.0 && newUnitsPerCm!=0.0) {
83  newScale = oldScale * (oldUnitsPerCm/newUnitsPerCm);
84  }
85  }
86  return newScale;
87  }
88 
89  int getUnitIndex(String unit) {
90  unit = unit.toLowerCase(Locale.US);
91  if (unit.equals("cm")||unit.startsWith("cent"))
92  return CENTIMETER;
93  else if (unit.equals("mm")||unit.startsWith("milli"))
94  return MILLIMETER;
95  else if (unit.startsWith("inch"))
96  return INCH;
97  else if (unit.startsWith(""+IJ.micronSymbol)||unit.startsWith("u")||unit.startsWith("micro"))
98  return MICROMETER;
99  else if (unit.equals("nm")||unit.startsWith("nano"))
100  return NANOMETER;
101  else if (unit.startsWith("meter"))
102  return METER;
103  else if (unit.equals("km")||unit.startsWith("kilo"))
104  return KILOMETER;
105  else if (unit.equals("ft")||unit.equals("foot")||unit.equals("feet"))
106  return FOOT;
107  else if (unit.equals("mi")||unit.startsWith("mile"))
108  return MILLIMETER;
109  else
110  return OTHER_UNIT;
111  }
112 
113  double getUnitsPerCm(int unitIndex) {
114  switch (unitIndex) {
115  case NANOMETER: return 10000000.0;
116  case MICROMETER: return 10000.0;
117  case MILLIMETER: return 10.0;
118  case CENTIMETER: return 1.0;
119  case METER: return 0.01;
120  case KILOMETER: return 0.00001;
121  case INCH: return 0.3937;
122  case FOOT: return 0.0328083;
123  case MILE: return 0.000006213;
124  default: return 0.0;
125  }
126  }
127 
128 }
129 
130 class ImagePropertiesDialog extends GenericDialog {
131  ImageProperties iprops;
132 
133  public ImagePropertiesDialog(String title, ImageProperties iprops) {
134  super(title);
135  this.iprops = iprops;
136  }
137 
138  public void textValueChanged(TextEvent e) {
139  TextField unitField = (TextField)stringField.elementAt(0);
140  if (e.getSource()!=unitField)
141  return;
142  String newUnit = unitField.getText();
143  TextField ppuField = (TextField)numberField.elementAt(0);
144  double newScale = iprops.getNewScale(newUnit);
145  if (newScale!=0.0) {
146  ppuField.setText(((int)newScale)==newScale?IJ.d2s(newScale,0):IJ.d2s(newScale,2));
147  iprops.oldUnitIndex = iprops.getUnitIndex(newUnit);
148  iprops.oldUnitsPerCm = iprops.getUnitsPerCm(iprops.oldUnitIndex);
149  iprops.oldScale = newScale;;
150  }
151  }
152 
153 }