private String displayRanges(ImagePlus imp) { LUT[] luts = imp.getLuts(); if (luts == null) return ""; String s = "Display ranges\n"; int n = luts.length; if (n > 7) n = 7; for (int i = 0; i < n; i++) { double min = luts[i].min; double max = luts[i].max; int digits = (int) min == min && (int) max == max ? 0 : 2; s += " " + (i + 1) + ": " + IJ.d2s(min, digits) + "-" + IJ.d2s(max, digits) + "\n"; } return s; }
byte[] getJar(String address) { // System.out.println("getJar: "+address); byte[] data; try { URL url = new URL(address); IJ.showStatus("Connecting to " + IJ.URL); URLConnection uc = url.openConnection(); int len = uc.getContentLength(); if (IJ.debugMode) IJ.log("Updater (url): " + address + " " + len); if (len <= 0) return null; String name = address.contains("wsr") ? "daily build (" : "ij.jar ("; IJ.showStatus("Downloading " + name + IJ.d2s((double) len / 1048576, 1) + "MB)"); InputStream in = uc.getInputStream(); data = new byte[len]; int n = 0; while (n < len) { int count = in.read(data, n, len - n); if (count < 0) throw new EOFException(); n += count; IJ.showProgress(n, len); } in.close(); } catch (IOException e) { if (IJ.debugMode) IJ.log("" + e); return null; } if (IJ.debugMode) IJ.wait(6000); return data; }
public synchronized void adjustmentValueChanged(AdjustmentEvent e) { Object source = e.getSource(); for (int i = 0; i < slider.size(); i++) { if (source == slider.elementAt(i)) { Scrollbar sb = (Scrollbar) source; TextField tf = (TextField) numberField.elementAt(sliderIndexes[i]); int digits = sliderScales[i] == 1.0 ? 0 : 2; tf.setText("" + IJ.d2s(sb.getValue() / sliderScales[i], digits)); } } }
/** * Adds a numeric field. The first word of the label must be unique or command recording will not * work. * * @param label the label * @param defaultValue value to be initially displayed * @param digits number of digits to right of decimal point * @param columns width of field in characters * @param units a string displayed to the right of the field */ public void addNumericField( String label, double defaultValue, int digits, int columns, String units) { String label2 = label; if (label2.indexOf('_') != -1) label2 = label2.replace('_', ' '); Label theLabel = makeLabel(label2); c.gridx = 0; c.gridy = y; c.anchor = GridBagConstraints.EAST; c.gridwidth = 1; if (firstNumericField) c.insets = getInsets(5, 0, 3, 0); else c.insets = getInsets(0, 0, 3, 0); grid.setConstraints(theLabel, c); add(theLabel); if (numberField == null) { numberField = new Vector(5); defaultValues = new Vector(5); defaultText = new Vector(5); } if (IJ.isWindows()) columns -= 2; if (columns < 1) columns = 1; String defaultString = IJ.d2s(defaultValue, digits); if (Double.isNaN(defaultValue)) defaultString = ""; TextField tf = new TextField(defaultString, columns); if (IJ.isLinux()) tf.setBackground(Color.white); tf.addActionListener(this); tf.addTextListener(this); tf.addFocusListener(this); tf.addKeyListener(this); numberField.addElement(tf); defaultValues.addElement(new Double(defaultValue)); defaultText.addElement(tf.getText()); c.gridx = 1; c.gridy = y; c.anchor = GridBagConstraints.WEST; tf.setEditable(true); // if (firstNumericField) tf.selectAll(); firstNumericField = false; if (units == null || units.equals("")) { grid.setConstraints(tf, c); add(tf); } else { Panel panel = new Panel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); panel.add(tf); panel.add(new Label(" " + units)); grid.setConstraints(panel, c); add(panel); } if (Recorder.record || macro) saveLabel(tf, label); y++; }
byte[] getJar(String address) { byte[] data; boolean gte133 = version().compareTo("1.33u") >= 0; try { URL url = new URL(address); URLConnection uc = url.openConnection(); int len = uc.getContentLength(); String name = address.endsWith("ij/ij.jar") ? "daily build" : "ij.jar"; IJ.showStatus("Downloading ij.jar (" + IJ.d2s((double) len / 1048576, 1) + "MB)"); InputStream in = uc.getInputStream(); data = new byte[len]; int n = 0; while (n < len) { int count = in.read(data, n, len - n); if (count < 0) throw new EOFException(); n += count; if (gte133) IJ.showProgress(n, len); } in.close(); } catch (IOException e) { return null; } return data; }
String getInfo(ImagePlus imp, ImageProcessor ip) { String s = new String("\n"); s += "Title: " + imp.getTitle() + "\n"; Calibration cal = imp.getCalibration(); int stackSize = imp.getStackSize(); int channels = imp.getNChannels(); int slices = imp.getNSlices(); int frames = imp.getNFrames(); int digits = imp.getBitDepth() == 32 ? 4 : 0; if (cal.scaled()) { String unit = cal.getUnit(); String units = cal.getUnits(); s += "Width: " + IJ.d2s(imp.getWidth() * cal.pixelWidth, 2) + " " + units + " (" + imp.getWidth() + ")\n"; s += "Height: " + IJ.d2s(imp.getHeight() * cal.pixelHeight, 2) + " " + units + " (" + imp.getHeight() + ")\n"; if (slices > 1) s += "Depth: " + IJ.d2s(slices * cal.pixelDepth, 2) + " " + units + " (" + slices + ")\n"; double xResolution = 1.0 / cal.pixelWidth; double yResolution = 1.0 / cal.pixelHeight; int places = Tools.getDecimalPlaces(xResolution, yResolution); if (xResolution == yResolution) s += "Resolution: " + IJ.d2s(xResolution, places) + " pixels per " + unit + "\n"; else { s += "X Resolution: " + IJ.d2s(xResolution, places) + " pixels per " + unit + "\n"; s += "Y Resolution: " + IJ.d2s(yResolution, places) + " pixels per " + unit + "\n"; } } else { s += "Width: " + imp.getWidth() + " pixels\n"; s += "Height: " + imp.getHeight() + " pixels\n"; if (stackSize > 1) s += "Depth: " + slices + " pixels\n"; } if (stackSize > 1) s += "Voxel size: " + d2s(cal.pixelWidth) + "x" + d2s(cal.pixelHeight) + "x" + d2s(cal.pixelDepth) + " " + cal.getUnit() + "\n"; else s += "Pixel size: " + d2s(cal.pixelWidth) + "x" + d2s(cal.pixelHeight) + " " + cal.getUnit() + "\n"; s += "ID: " + imp.getID() + "\n"; String zOrigin = stackSize > 1 || cal.zOrigin != 0.0 ? "," + d2s(cal.zOrigin) : ""; s += "Coordinate origin: " + d2s(cal.xOrigin) + "," + d2s(cal.yOrigin) + zOrigin + "\n"; int type = imp.getType(); switch (type) { case ImagePlus.GRAY8: s += "Bits per pixel: 8 "; String lut = "LUT"; if (imp.getProcessor().isColorLut()) lut = "color " + lut; else lut = "grayscale " + lut; if (imp.isInvertedLut()) lut = "inverting " + lut; s += "(" + lut + ")\n"; if (imp.getNChannels() > 1) s += displayRanges(imp); else s += "Display range: " + (int) ip.getMin() + "-" + (int) ip.getMax() + "\n"; break; case ImagePlus.GRAY16: case ImagePlus.GRAY32: if (type == ImagePlus.GRAY16) { String sign = cal.isSigned16Bit() ? "signed" : "unsigned"; s += "Bits per pixel: 16 (" + sign + ")\n"; } else s += "Bits per pixel: 32 (float)\n"; if (imp.getNChannels() > 1) s += displayRanges(imp); else { s += "Display range: "; double min = ip.getMin(); double max = ip.getMax(); if (cal.calibrated()) { min = cal.getCValue((int) min); max = cal.getCValue((int) max); } s += IJ.d2s(min, digits) + " - " + IJ.d2s(max, digits) + "\n"; } break; case ImagePlus.COLOR_256: s += "Bits per pixel: 8 (color LUT)\n"; break; case ImagePlus.COLOR_RGB: s += "Bits per pixel: 32 (RGB)\n"; break; } double interval = cal.frameInterval; double fps = cal.fps; if (stackSize > 1) { ImageStack stack = imp.getStack(); int slice = imp.getCurrentSlice(); String number = slice + "/" + stackSize; String label = stack.getShortSliceLabel(slice); if (label != null && label.length() > 0) label = " (" + label + ")"; else label = ""; if (interval > 0.0 || fps != 0.0) { s += "Frame: " + number + label + "\n"; if (fps != 0.0) { String sRate = Math.abs(fps - Math.round(fps)) < 0.00001 ? IJ.d2s(fps, 0) : IJ.d2s(fps, 5); s += "Frame rate: " + sRate + " fps\n"; } if (interval != 0.0) s += "Frame interval: " + ((int) interval == interval ? IJ.d2s(interval, 0) : IJ.d2s(interval, 5)) + " " + cal.getTimeUnit() + "\n"; } else s += "Image: " + number + label + "\n"; if (imp.isHyperStack()) { if (channels > 1) s += " Channel: " + imp.getChannel() + "/" + channels + "\n"; if (slices > 1) s += " Slice: " + imp.getSlice() + "/" + slices + "\n"; if (frames > 1) s += " Frame: " + imp.getFrame() + "/" + frames + "\n"; } if (imp.isComposite()) { if (!imp.isHyperStack() && channels > 1) s += " Channels: " + channels + "\n"; String mode = ((CompositeImage) imp).getModeAsString(); s += " Composite mode: \"" + mode + "\"\n"; } } if (ip.getMinThreshold() == ImageProcessor.NO_THRESHOLD) s += "No Threshold\n"; else { double lower = ip.getMinThreshold(); double upper = ip.getMaxThreshold(); int dp = digits; if (cal.calibrated()) { lower = cal.getCValue((int) lower); upper = cal.getCValue((int) upper); dp = cal.isSigned16Bit() ? 0 : 4; } s += "Threshold: " + IJ.d2s(lower, dp) + "-" + IJ.d2s(upper, dp) + "\n"; } ImageCanvas ic = imp.getCanvas(); double mag = ic != null ? ic.getMagnification() : 1.0; if (mag != 1.0) s += "Magnification: " + IJ.d2s(mag, 2) + "\n"; if (cal.calibrated()) { s += " \n"; int curveFit = cal.getFunction(); s += "Calibration Function: "; if (curveFit == Calibration.UNCALIBRATED_OD) s += "Uncalibrated OD\n"; else if (curveFit == Calibration.CUSTOM) s += "Custom lookup table\n"; else s += CurveFitter.fList[curveFit] + "\n"; double[] c = cal.getCoefficients(); if (c != null) { s += " a: " + IJ.d2s(c[0], 6) + "\n"; s += " b: " + IJ.d2s(c[1], 6) + "\n"; if (c.length >= 3) s += " c: " + IJ.d2s(c[2], 6) + "\n"; if (c.length >= 4) s += " c: " + IJ.d2s(c[3], 6) + "\n"; if (c.length >= 5) s += " c: " + IJ.d2s(c[4], 6) + "\n"; } s += " Unit: \"" + cal.getValueUnit() + "\"\n"; } else s += "Uncalibrated\n"; FileInfo fi = imp.getOriginalFileInfo(); if (fi != null) { if (fi.url != null && !fi.url.equals("")) s += "URL: " + fi.url + "\n"; else if (fi.directory != null && fi.fileName != null) s += "Path: " + fi.directory + fi.fileName + "\n"; } ImageWindow win = imp.getWindow(); if (win != null) { Point loc = win.getLocation(); Dimension screen = IJ.getScreenSize(); s += "Screen location: " + loc.x + "," + loc.y + " (" + screen.width + "x" + screen.height + ")\n"; } Overlay overlay = imp.getOverlay(); if (overlay != null) { String hidden = imp.getHideOverlay() ? " (hidden)" : " "; int n = overlay.size(); String elements = n == 1 ? " element" : " elements"; s += "Overlay: " + n + elements + (imp.getHideOverlay() ? " (hidden)" : "") + "\n"; } else s += "No Overlay\n"; Roi roi = imp.getRoi(); if (roi == null) { if (cal.calibrated()) s += " \n"; s += "No Selection\n"; } else if (roi instanceof EllipseRoi) { s += "\nElliptical Selection\n"; double[] p = ((EllipseRoi) roi).getParams(); double dx = p[2] - p[0]; double dy = p[3] - p[1]; double major = Math.sqrt(dx * dx + dy * dy); s += " Major: " + IJ.d2s(major, 2) + "\n"; s += " Minor: " + IJ.d2s(major * p[4], 2) + "\n"; s += " X1: " + IJ.d2s(p[0], 2) + "\n"; s += " Y1: " + IJ.d2s(p[1], 2) + "\n"; s += " X2: " + IJ.d2s(p[2], 2) + "\n"; s += " Y2: " + IJ.d2s(p[3], 2) + "\n"; s += " Aspect ratio: " + IJ.d2s(p[4], 2) + "\n"; } else { s += " \n"; s += roi.getTypeAsString() + " Selection"; String points = null; if (roi instanceof PointRoi) { int npoints = ((PolygonRoi) roi).getNCoordinates(); String suffix = npoints > 1 ? "s)" : ")"; points = " (" + npoints + " point" + suffix; } String name = roi.getName(); if (name != null) { s += " (\"" + name + "\")"; if (points != null) s += "\n " + points; } else if (points != null) s += points; s += "\n"; Rectangle r = roi.getBounds(); if (roi instanceof Line) { Line line = (Line) roi; s += " X1: " + IJ.d2s(line.x1d * cal.pixelWidth) + "\n"; s += " Y1: " + IJ.d2s(yy(line.y1d, imp) * cal.pixelHeight) + "\n"; s += " X2: " + IJ.d2s(line.x2d * cal.pixelWidth) + "\n"; s += " Y2: " + IJ.d2s(yy(line.y2d, imp) * cal.pixelHeight) + "\n"; } else if (cal.scaled()) { s += " X: " + IJ.d2s(cal.getX(r.x)) + " (" + r.x + ")\n"; s += " Y: " + IJ.d2s(cal.getY(r.y, imp.getHeight())) + " (" + r.y + ")\n"; s += " Width: " + IJ.d2s(r.width * cal.pixelWidth) + " (" + r.width + ")\n"; s += " Height: " + IJ.d2s(r.height * cal.pixelHeight) + " (" + r.height + ")\n"; } else { s += " X: " + r.x + "\n"; s += " Y: " + yy(r.y, imp) + "\n"; s += " Width: " + r.width + "\n"; s += " Height: " + r.height + "\n"; } } return s; }
String d2s(double n) { return n == (int) n ? Integer.toString((int) n) : IJ.d2s(n); }
/** * Adds a slider (scroll bar) to the dialog box. Floating point values will be used if * (maxValue-minValue)<=5.0 and either minValue or maxValue are non-integer. * * @param label the label * @param minValue the minimum value of the slider * @param maxValue the maximum value of the slider * @param defaultValue the initial value of the slider */ public void addSlider(String label, double minValue, double maxValue, double defaultValue) { if (defaultValue < minValue) defaultValue = minValue; if (defaultValue > maxValue) defaultValue = maxValue; int columns = 4; int digits = 0; double scale = 1.0; if ((maxValue - minValue) <= 5.0 && (minValue != (int) minValue || maxValue != (int) maxValue || defaultValue != (int) defaultValue)) { scale = 20.0; minValue *= scale; maxValue *= scale; defaultValue *= scale; digits = 2; } String label2 = label; if (label2.indexOf('_') != -1) label2 = label2.replace('_', ' '); Label theLabel = makeLabel(label2); c.gridx = 0; c.gridy = y; c.anchor = GridBagConstraints.EAST; c.gridwidth = 1; c.insets = new Insets(0, 0, 3, 0); grid.setConstraints(theLabel, c); add(theLabel); if (slider == null) { slider = new Vector(5); sliderIndexes = new int[MAX_SLIDERS]; sliderScales = new double[MAX_SLIDERS]; } Scrollbar s = new Scrollbar( Scrollbar.HORIZONTAL, (int) defaultValue, 1, (int) minValue, (int) maxValue + 1); GUI.fix(s); slider.addElement(s); s.addAdjustmentListener(this); s.setUnitIncrement(1); if (numberField == null) { numberField = new Vector(5); defaultValues = new Vector(5); defaultText = new Vector(5); } if (IJ.isWindows()) columns -= 2; if (columns < 1) columns = 1; TextField tf = new TextField(IJ.d2s(defaultValue / scale, digits), columns); if (IJ.isLinux()) tf.setBackground(Color.white); tf.addActionListener(this); tf.addTextListener(this); tf.addFocusListener(this); tf.addKeyListener(this); numberField.addElement(tf); sliderIndexes[slider.size() - 1] = numberField.size() - 1; sliderScales[slider.size() - 1] = scale; defaultValues.addElement(new Double(defaultValue / scale)); defaultText.addElement(tf.getText()); tf.setEditable(true); firstSlider = false; Panel panel = new Panel(); GridBagLayout pgrid = new GridBagLayout(); GridBagConstraints pc = new GridBagConstraints(); panel.setLayout(pgrid); pc.gridx = 0; pc.gridy = 0; pc.gridwidth = 1; pc.ipadx = 85; pc.anchor = GridBagConstraints.WEST; pgrid.setConstraints(s, pc); panel.add(s); pc.ipadx = 0; // reset // text field pc.gridx = 1; pc.insets = new Insets(5, 5, 0, 0); pc.anchor = GridBagConstraints.EAST; pgrid.setConstraints(tf, pc); panel.add(tf); grid.setConstraints(panel, c); c.gridx = 1; c.gridy = y; c.gridwidth = 1; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(0, 0, 0, 0); grid.setConstraints(panel, c); add(panel); y++; if (Recorder.record || macro) saveLabel(tf, label); }
private String d2s(double n) { return IJ.d2s(n, Tools.getDecimalPlaces(n)); }
public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) { if (IJ.isMacOSX()) IJ.wait(50); Calibration cal = imp.getCalibration(); width = gd.getNextNumber(); height = gd.getNextNumber(); xRoi = gd.getNextNumber(); yRoi = gd.getNextNumber(); if (stackSize > 1) iSlice = (int) gd.getNextNumber(); oval = gd.getNextBoolean(); square = gd.getNextBoolean(); centered = gd.getNextBoolean(); if (cal.scaled()) scaledUnits = gd.getNextBoolean(); if (gd.invalidNumber() || width <= 0 || height <= 0) return false; // Vector numFields = gd.getNumericFields(); Vector checkboxes = gd.getCheckboxes(); boolean newWidth = false, newHeight = false, newXY = false; if (e != null && e.getSource() == checkboxes.get(SQUARE) && square) { width = 0.5 * (width + height); // make square: same width&height height = width; newWidth = true; newHeight = true; } if (e != null && e.getSource() == checkboxes.get(CENTERED)) { double shiftBy = centered ? 0.5 : -0.5; // 'centered' changed: xRoi += shiftBy * width; // shift x, y to keep roi the same yRoi += shiftBy * height; newXY = true; } if (square && width != height && e != null) { // in 'square' mode, synchronize width&height if (e.getSource() == numFields.get(WIDTH)) { height = width; newHeight = true; } else if (e.getSource() == numFields.get(HEIGHT)) { width = height; newWidth = true; } } if (e != null && cal.scaled() && e.getSource() == checkboxes.get(SCALED_UNITS)) { double xFactor = scaledUnits ? cal.pixelWidth : 1. / cal.pixelWidth; double yFactor = scaledUnits ? cal.pixelHeight : 1. / cal.pixelHeight; width *= xFactor; // transform everything to keep roi the same height *= yFactor; xRoi *= xFactor; yRoi *= yFactor; newWidth = true; newHeight = true; newXY = true; } int digits = (scaledUnits || (int) width != width) ? 2 : 0; if (newWidth) ((TextField) (numFields.get(WIDTH))).setText(IJ.d2s(width, digits)); if (newHeight) ((TextField) (numFields.get(HEIGHT))).setText(IJ.d2s(height, digits)); digits = (scaledUnits || (int) xRoi != xRoi || (int) yRoi != yRoi) ? 2 : 0; if (newXY) { ((TextField) (numFields.get(X_ROI))).setText(IJ.d2s(xRoi, digits)); ((TextField) (numFields.get(Y_ROI))).setText(IJ.d2s(yRoi, digits)); } if (stackSize > 1 && iSlice > 0 && iSlice <= stackSize) imp.setSlice(iSlice); if (!newWidth && !newHeight && !newXY) // don't draw if an update will come immediately drawRoi(); return true; }