package ij.plugin.filter; import ij.*; import ij.gui.*; import ij.process.*; import ij.measure.*; import ij.util.*; import ij.io.*; import ij.plugin.TextReader; import java.awt.*; import java.util.*; import java.awt.event.*; import java.io.*; /** Implements the Analyze/Calibrate command. */ public class Calibrator implements PlugInFilter, Measurements, ActionListener { private static final String NONE = "None"; private static final String INVERTER = "Pixel Inverter"; private static final String UNCALIBRATED_OD = "Uncalibrated OD"; private static final String CUSTOM = "Custom"; private static boolean showSettings; private boolean global1, global2; private ImagePlus imp; private int choiceIndex; private String[] functions; private int nFits = CurveFitter.fitList.length; private int spacerIndex = nFits+1; private int inverterIndex = nFits+2; private int odIndex = nFits+3; private int customIndex = nFits+4; private static String xText = ""; private static String yText = ""; private static boolean importedValues; private String unit; private double lx=0.02, ly=0.1; private int oldFunction; private String sumResiduals, fitGoodness; private Button open, save; private GenericDialog gd; public int setup(String arg, ImagePlus imp) { this.imp = imp; IJ.register(Calibrator.class); return DOES_ALL-DOES_RGB+NO_CHANGES; } public void run(ImageProcessor ip) { global1 = imp.getGlobalCalibration()!=null; if (!showDialog(imp)) return; if (choiceIndex==customIndex) { showPlot(null, null, imp.getCalibration(), null); return; } else if (imp.getType()==ImagePlus.GRAY32) { if (choiceIndex==0) imp.getCalibration().setValueUnit(unit); else IJ.error("Calibrate", "Function must be \"None\" for 32-bit images,\nbut you can change the Unit."); } else calibrate(imp); } public boolean showDialog(ImagePlus imp) { String defaultChoice; Calibration cal = imp.getCalibration(); functions = getFunctionList(cal.getFunction()==Calibration.CUSTOM); int function = cal.getFunction(); oldFunction = function; double[] p = cal.getCoefficients(); unit = cal.getValueUnit(); if (function==Calibration.NONE) defaultChoice=NONE; else if (function0 && is16Bits) { // IJ.error("Calibrate", "Calibration of 16-bit images, except with straight\nline functions, is currently not supported."); // return; //} x = getData(xText); y = getData(yText); if (!cal.calibrated() || y.length!=0 || function!=oldFunction) { parameters = doCurveFitting(x, y, function); if (parameters==null) return; } zeroClip = true; for (int i=0; i0&&y.length>0) pw.addPoints(x, y, PlotWindow.CIRCLE); double[] p = cal.getCoefficients(); if (fit<=Calibration.LOG2) { drawLabel(pw, CurveFitter.fList[fit]); ly += 0.04; } if (p!=null) { int np = p.length; drawLabel(pw, "a="+IJ.d2s(p[0],6)); drawLabel(pw, "b="+IJ.d2s(p[1],6)); if (np>=3) drawLabel(pw, "c="+IJ.d2s(p[2],6)); if (np>=4) drawLabel(pw, "d="+IJ.d2s(p[3],6)); if (np>=5) drawLabel(pw, "e="+IJ.d2s(p[4],6)); ly += 0.04; } if (rSquared!=null) {drawLabel(pw, "R^2="+rSquared); rSquared=null;} pw.draw(); } void drawLabel(PlotWindow pw, String label) { pw.addLabel(lx, ly, label); ly += 0.08; } double sqr(double x) {return x*x;} String[] getFunctionList(boolean custom) { int n = nFits+4; if (custom) n++; String[] list = new String[n]; list[0] = NONE; for (int i=0; iMAX_STANDARDS) count = MAX_STANDARDS; String s = ""; for (int i=0; i1)) { IJ.error("Calibrate", "This appears to not be a one or two column text file"); return; } StringBuffer sb = new StringBuffer(); for (int i=0; i