Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Calibration.java
1 package ij.measure;
2 import ij.*;
3 
6 public class Calibration {
7 
8  public static final int STRAIGHT_LINE=0,POLY2=1,POLY3=2,POLY4=3,
9  EXPONENTIAL=4,POWER=5,LOG=6,RODBARD=7,GAMMA_VARIATE=8, LOG2=9;
10  public static final int NONE=20, UNCALIBRATED_OD=21;
11 
13  public double pixelWidth = 1.0;
14 
16  public double pixelHeight = 1.0;
17 
19  public double pixelDepth = 1.0;
20 
22  public double frameInterval;
23 
25  public double xOrigin;
26 
28  public double yOrigin;
29 
31  public double zOrigin;
32 
36  public String info;
37 
39  private double[] coefficients;
40 
41  /* Distance unit (e.g. 'cm', 'inch') */
42  private String unit = "pixel";
43 
44  /* Distance units (e.g. 'microns', 'inches') */
45  private String units;
46 
47  /* Pixel value unit (e.g. 'gray level', 'OD') */
48  private String valueUnit = "Gray Value";
49 
50  /* Calibration function ID */
51  private int function = NONE;
52 
53  /* Calibration table */
54  private float[] cTable;
55 
56  private boolean invertedLut;
57  private int bitDepth = 8;
58  private boolean zeroClip;
59 
61  public Calibration(ImagePlus imp) {
62  if (imp!=null) {
63  bitDepth = imp.getBitDepth();
64  invertedLut = imp.isInvertedLut();
65  }
66  }
67 
70  public Calibration() {
71  }
72 
74  public boolean scaled() {
75  return pixelWidth!=1.0 || pixelHeight!=1.0 || pixelDepth!=1.0;
76  }
77 
79  public void setUnit(String unit) {
80  if (unit==null || unit.equals(""))
81  this.unit = "pixel";
82  else
83  this.unit = unit;
84  units = null;
85  }
86 
88  public String getUnit() {
89  return unit;
90  }
91 
93  public String getUnits() {
94  if (units==null) {
95  if (unit.equals("pixel"))
96  units = "pixels";
97  else if (unit.equals("micron"))
98  units = "microns";
99  else if (unit.equals("inch"))
100  units = "inches";
101  else
102  units = unit;
103  }
104  return units;
105  }
106 
108  public double getX(int x) {
109  return (x-xOrigin)*pixelWidth;
110  }
111 
113  public double getY(int y) {
114  return (y-yOrigin)*pixelHeight;
115  }
116 
118  public double getZ(int z) {
119  return (z-zOrigin)*pixelDepth;
120  }
121 
123  public void setFunction(int function, double[] coefficients, String unit) {
124  setFunction(function, coefficients, unit, false);
125  }
126 
127  public void setFunction(int function, double[] coefficients, String unit, boolean zeroClip) {
128  if (function==NONE)
129  {disableDensityCalibration(); return;}
130  if (coefficients==null && function>=STRAIGHT_LINE && function<=LOG2)
131  return;
132  this.function = function;
133  this.coefficients = coefficients;
134  this.zeroClip = zeroClip;
135  if (unit!=null)
136  valueUnit = unit;
137  cTable = null;
138  }
139 
141  public void setImage(ImagePlus imp) {
142  if (imp==null)
143  return;
144  int type = imp.getType();
145  int newBitDepth = imp.getBitDepth();
146  if (newBitDepth!=bitDepth || type==ImagePlus.GRAY32 || type==ImagePlus.COLOR_RGB)
147  disableDensityCalibration();
148  bitDepth = newBitDepth;
149  }
150 
151  public void disableDensityCalibration() {
152  function = NONE;
153  coefficients = null;
154  cTable = null;
155  valueUnit = "Gray Value";
156  }
157 
159  public String getValueUnit() {
160  return valueUnit;
161  }
162 
164  public void setValueUnit(String unit) {
165  if (unit!=null)
166  valueUnit = unit;
167  }
168 
170  public double[] getCoefficients() {
171  return coefficients;
172  }
173 
175  public boolean calibrated() {
176  return function!=NONE;
177  }
178 
180  public int getFunction() {
181  return function;
182  }
183 
187  public float[] getCTable() {
188  if (cTable==null)
189  makeCTable();
190  return cTable;
191  }
192 
193  void makeCTable() {
194  if (bitDepth==16)
195  {make16BitCTable(); return;}
196  if (bitDepth!=8)
197  return;
198  if (function==UNCALIBRATED_OD) {
199  cTable = new float[256];
200  for (int i=0; i<256; i++)
201  cTable[i] = (float)od(i);
202  } else if (function>=STRAIGHT_LINE && function<=LOG2 && coefficients!=null) {
203  cTable = new float[256];
204  double value;
205  for (int i=0; i<256; i++) {
206  value = CurveFitter.f(function, coefficients, i);
207  if (zeroClip && value<0.0)
208  cTable[i] = 0f;
209  else
210  cTable[i] = (float)value;
211  }
212  } else
213  cTable = null;
214  }
215 
216  void make16BitCTable() {
217  if (function>=STRAIGHT_LINE && function<=LOG2 && coefficients!=null) {
218  cTable = new float[65536];
219  for (int i=0; i<65536; i++)
220  cTable[i] = (float)CurveFitter.f(function, coefficients, i);
221  } else
222  cTable = null;
223  }
224 
225  double od(double v) {
226  if (invertedLut) {
227  if (v==255.0) v = 254.5;
228  return 0.434294481*Math.log(255.0/(255.0-v));
229  } else {
230  if (v==0.0) v = 0.5;
231  return 0.434294481*Math.log(255.0/v);
232  }
233  }
234 
236  public double getCValue(int value) {
237  if (function==NONE)
238  return value;
239  if (function>=STRAIGHT_LINE && function<=LOG2 && coefficients!=null) {
240  double v = CurveFitter.f(function, coefficients, value);
241  if (zeroClip && v<0.0)
242  return 0.0;
243  else
244  return v;
245  } if (cTable==null)
246  makeCTable();
247  if (cTable!=null && value>=0 && value<cTable.length)
248  return cTable[value];
249  else
250  return value;
251  }
252 
254  public double getCValue(double value) {
255  if (function==NONE)
256  return value;
257  else {
258  if (function>=STRAIGHT_LINE && function<=LOG2 && coefficients!=null) {
259  double v = CurveFitter.f(function, coefficients, value);
260  if (zeroClip && v<0.0)
261  return 0.0;
262  else
263  return v;
264  } else
265  return getCValue((int)value);
266  }
267  }
268 
270  public double getRawValue(double value) {
271  if (function==NONE)
272  return value;
273  if (cTable==null)
274  makeCTable();
275  float fvalue = (float)value;
276  float smallestDiff = Float.MAX_VALUE;
277  float diff;
278  int index = 0;
279  for (int i=0; i<cTable.length; i++) {
280  diff = fvalue - cTable[i];
281  if (diff<0f) diff = -diff;
282  if (diff<smallestDiff) {
283  smallestDiff = diff;
284  index = i;
285  }
286  }
287  return index;
288  }
289 
291  public Calibration copy() {
292  Calibration copy = new Calibration();
293  copy.pixelWidth = pixelWidth;
294  copy.pixelHeight = pixelHeight;
295  copy.pixelDepth = pixelDepth;
297  copy.xOrigin = xOrigin;
298  copy.yOrigin = yOrigin;
299  copy.zOrigin = zOrigin;
300  copy.info = info;
301  copy.unit = unit;
302  copy.units = units;
303  copy.valueUnit = valueUnit;
304  copy.function = function;
305  copy.coefficients = coefficients;
306  copy.cTable = cTable;
307  copy.invertedLut = invertedLut;
308  copy.bitDepth = bitDepth;
309  copy.zeroClip = zeroClip;
310  return copy;
311  }
312 
314  public boolean equals(Calibration cal) {
315  if (cal==null)
316  return false;
317  boolean equal = true;
319  equal = false;
320  if (!cal.unit.equals(unit))
321  equal = false;
322  if (!cal.valueUnit.equals(valueUnit) || cal.function!=function)
323  equal = false;
324  return equal;
325  }
326 
328  public boolean isSigned16Bit() {
329  return bitDepth==16 && cTable!=null && cTable[0]==-32768;
330  }
331 
332  public String toString() {
333  return
334  "w=" + pixelWidth
335  + ", h=" + pixelHeight
336  + ", d=" + pixelDepth
337  + ", unit=" + unit
338  + ", f=" + function
339  + ", nc=" + (coefficients!=null?""+coefficients.length:"null")
340  + ", table=" + (cTable!=null?""+cTable.length:"null")
341  + ", vunit=" + valueUnit;
342  }
343 }
344