private boolean ignoreArrowKeys(ImagePlus imp) { Frame frame = WindowManager.getFrontWindow(); String title = frame.getTitle(); if (title != null && title.equals("ROI Manager")) return true; // Control Panel? if (frame != null && frame instanceof javax.swing.JFrame) return true; ImageWindow win = imp.getWindow(); // LOCI Data Browser window? if (imp.getStackSize() > 1 && win != null && win.getClass().getName().startsWith("loci")) return true; return false; }
void setStackDisplayRange(ImagePlus imp) { ImageStack stack = imp.getStack(); double min = Double.MAX_VALUE; double max = -Double.MAX_VALUE; int n = stack.getSize(); for (int i = 1; i <= n; i++) { if (!silentMode) IJ.showStatus("Calculating stack min and max: " + i + "/" + n); ImageProcessor ip = stack.getProcessor(i); ip.resetMinAndMax(); if (ip.getMin() < min) min = ip.getMin(); if (ip.getMax() > max) max = ip.getMax(); } imp.getProcessor().setMinAndMax(min, max); imp.updateAndDraw(); }
/** Opens a stack of images. */ ImagePlus openStack(ColorModel cm, boolean show) { ImageStack stack = new ImageStack(fi.width, fi.height, cm); long skip = fi.getOffset(); Object pixels; try { ImageReader reader = new ImageReader(fi); InputStream is = createInputStream(fi); if (is == null) return null; IJ.resetEscape(); for (int i = 1; i <= fi.nImages; i++) { if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages); if (IJ.escapePressed()) { IJ.beep(); IJ.showProgress(1.0); silentMode = false; return null; } pixels = reader.readPixels(is, skip); if (pixels == null) break; stack.addSlice(null, pixels); skip = fi.gapBetweenImages; if (!silentMode) IJ.showProgress(i, fi.nImages); } is.close(); } catch (Exception e) { IJ.log("" + e); } catch (OutOfMemoryError e) { IJ.outOfMemory(fi.fileName); stack.trim(); } if (!silentMode) IJ.showProgress(1.0); if (stack.getSize() == 0) return null; if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) { for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1); } ImagePlus imp = new ImagePlus(fi.fileName, stack); if (fi.info != null) imp.setProperty("Info", fi.info); if (show) imp.show(); imp.setFileInfo(fi); setCalibration(imp); ImageProcessor ip = imp.getProcessor(); if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank setStackDisplayRange(imp); if (!silentMode) IJ.showProgress(1.0); silentMode = false; return imp; }
void checkForCalibrationConflict(ImagePlus imp, Calibration cal) { Calibration gcal = imp.getGlobalCalibration(); if (gcal == null || !showConflictMessage || IJ.isMacro()) return; if (cal.pixelWidth == gcal.pixelWidth && cal.getUnit().equals(gcal.getUnit())) return; GenericDialog gd = new GenericDialog(imp.getTitle()); gd.addMessage("The calibration of this image conflicts\nwith the current global calibration."); gd.addCheckbox("Disable_Global Calibration", true); gd.addCheckbox("Disable_these Messages", false); gd.showDialog(); if (gd.wasCanceled()) return; boolean disable = gd.getNextBoolean(); if (disable) { imp.setGlobalCalibration(null); imp.setCalibration(cal); WindowManager.repaintImageWindows(); } boolean dontShow = gd.getNextBoolean(); if (dontShow) showConflictMessage = false; }
private boolean deleteOverlayRoi(ImagePlus imp) { if (imp == null) return false; Overlay overlay = null; ImageCanvas ic = imp.getCanvas(); if (ic != null) overlay = ic.getShowAllList(); if (overlay == null) overlay = imp.getOverlay(); if (overlay == null) return false; Roi roi = imp.getRoi(); for (int i = 0; i < overlay.size(); i++) { Roi roi2 = overlay.get(i); if (roi2 == roi) { overlay.remove(i); imp.deleteRoi(); ic = imp.getCanvas(); if (ic != null) ic.roiManagerSelect(roi, true); return true; } } return false; }
void abortPluginOrMacro(ImagePlus imp) { if (imp != null) { ImageWindow win = imp.getWindow(); if (win != null) { win.running = false; win.running2 = false; } } Macro.abort(); Interpreter.abort(); if (Interpreter.getInstance() != null) IJ.beep(); }
/** Opens the image. Displays it if 'show' is true. Returns an ImagePlus object if successful. */ public ImagePlus open(boolean show) { ImagePlus imp = null; Object pixels; ProgressBar pb = null; ImageProcessor ip; ColorModel cm = createColorModel(fi); if (fi.nImages > 1) { return openStack(cm, show); } switch (fi.fileType) { case FileInfo.GRAY8: case FileInfo.COLOR8: case FileInfo.BITMAP: pixels = readPixels(fi); if (pixels == null) return null; ip = new ByteProcessor(width, height, (byte[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.GRAY16_SIGNED: case FileInfo.GRAY16_UNSIGNED: case FileInfo.GRAY12_UNSIGNED: pixels = readPixels(fi); if (pixels == null) return null; ip = new ShortProcessor(width, height, (short[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.GRAY32_INT: case FileInfo.GRAY32_UNSIGNED: case FileInfo.GRAY32_FLOAT: case FileInfo.GRAY24_UNSIGNED: case FileInfo.GRAY64_FLOAT: pixels = readPixels(fi); if (pixels == null) return null; ip = new FloatProcessor(width, height, (float[]) pixels, cm); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.RGB: case FileInfo.BGR: case FileInfo.ARGB: case FileInfo.ABGR: case FileInfo.BARG: case FileInfo.RGB_PLANAR: pixels = readPixels(fi); if (pixels == null) return null; ip = new ColorProcessor(width, height, (int[]) pixels); imp = new ImagePlus(fi.fileName, ip); break; case FileInfo.RGB48: case FileInfo.RGB48_PLANAR: boolean planar = fi.fileType == FileInfo.RGB48_PLANAR; Object[] pixelArray = (Object[]) readPixels(fi); if (pixelArray == null) return null; ImageStack stack = new ImageStack(width, height); stack.addSlice("Red", pixelArray[0]); stack.addSlice("Green", pixelArray[1]); stack.addSlice("Blue", pixelArray[2]); imp = new ImagePlus(fi.fileName, stack); imp.setDimensions(3, 1, 1); if (planar) imp.getProcessor().resetMinAndMax(); imp.setFileInfo(fi); int mode = CompositeImage.COMPOSITE; if (fi.description != null) { if (fi.description.indexOf("mode=color") != -1) mode = CompositeImage.COLOR; else if (fi.description.indexOf("mode=gray") != -1) mode = CompositeImage.GRAYSCALE; } imp = new CompositeImage(imp, mode); if (!planar && fi.displayRanges == null) { for (int c = 1; c <= 3; c++) { imp.setPosition(c, 1, 1); imp.setDisplayRange(minValue, maxValue); } imp.setPosition(1, 1, 1); } break; } imp.setFileInfo(fi); setCalibration(imp); if (fi.info != null) imp.setProperty("Info", fi.info); if (fi.sliceLabels != null && fi.sliceLabels.length == 1 && fi.sliceLabels[0] != null) imp.setProperty("Label", fi.sliceLabels[0]); if (show) imp.show(); return imp; }
void setCalibration(ImagePlus imp) { if (fi.fileType == FileInfo.GRAY16_SIGNED) { if (IJ.debugMode) IJ.log("16-bit signed"); double[] coeff = new double[2]; coeff[0] = -32768.0; coeff[1] = 1.0; imp.getLocalCalibration().setFunction(Calibration.STRAIGHT_LINE, coeff, "gray value"); } Properties props = decodeDescriptionString(fi); Calibration cal = imp.getCalibration(); boolean calibrated = false; if (fi.pixelWidth > 0.0 && fi.unit != null) { cal.pixelWidth = fi.pixelWidth; cal.pixelHeight = fi.pixelHeight; cal.pixelDepth = fi.pixelDepth; cal.setUnit(fi.unit); calibrated = true; } if (fi.valueUnit != null) { int f = fi.calibrationFunction; if ((f >= Calibration.STRAIGHT_LINE && f <= Calibration.RODBARD2 && fi.coefficients != null) || f == Calibration.UNCALIBRATED_OD) { boolean zeroClip = props != null && props.getProperty("zeroclip", "false").equals("true"); cal.setFunction(f, fi.coefficients, fi.valueUnit, zeroClip); calibrated = true; } } if (calibrated) checkForCalibrationConflict(imp, cal); if (fi.frameInterval != 0.0) cal.frameInterval = fi.frameInterval; if (props == null) return; cal.xOrigin = getDouble(props, "xorigin"); cal.yOrigin = getDouble(props, "yorigin"); cal.zOrigin = getDouble(props, "zorigin"); cal.info = props.getProperty("info"); cal.fps = getDouble(props, "fps"); cal.loop = getBoolean(props, "loop"); cal.frameInterval = getDouble(props, "finterval"); cal.setTimeUnit(props.getProperty("tunit", "sec")); double displayMin = getDouble(props, "min"); double displayMax = getDouble(props, "max"); if (!(displayMin == 0.0 && displayMax == 0.0)) { int type = imp.getType(); ImageProcessor ip = imp.getProcessor(); if (type == ImagePlus.GRAY8 || type == ImagePlus.COLOR_256) ip.setMinAndMax(displayMin, displayMax); else if (type == ImagePlus.GRAY16 || type == ImagePlus.GRAY32) { if (ip.getMin() != displayMin || ip.getMax() != displayMax) ip.setMinAndMax(displayMin, displayMax); } } int stackSize = imp.getStackSize(); if (stackSize > 1) { int channels = (int) getDouble(props, "channels"); int slices = (int) getDouble(props, "slices"); int frames = (int) getDouble(props, "frames"); if (channels == 0) channels = 1; if (slices == 0) slices = 1; if (frames == 0) frames = 1; // IJ.log("setCalibration: "+channels+" "+slices+" "+frames); if (channels * slices * frames == stackSize) { imp.setDimensions(channels, slices, frames); if (getBoolean(props, "hyperstack")) imp.setOpenAsHyperStack(true); } } }
/** Restores original disk or network version of image. */ public void revertToSaved(ImagePlus imp) { Image img; ProgressBar pb = IJ.getInstance().getProgressBar(); ImageProcessor ip; String path = fi.directory + fi.fileName; if (fi.fileFormat == fi.GIF_OR_JPG) { // restore gif or jpg img = Toolkit.getDefaultToolkit().createImage(path); imp.setImage(img); if (imp.getType() == ImagePlus.COLOR_RGB) Opener.convertGrayJpegTo8Bits(imp); return; } if (fi.fileFormat == fi.DICOM) { // restore DICOM ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.DICOM", path); if (imp2 != null) imp.setProcessor(null, imp2.getProcessor()); return; } if (fi.fileFormat == fi.BMP) { // restore BMP ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.BMP_Reader", path); if (imp2 != null) imp.setProcessor(null, imp2.getProcessor()); return; } if (fi.fileFormat == fi.PGM) { // restore PGM ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.PGM_Reader", path); if (imp2 != null) imp.setProcessor(null, imp2.getProcessor()); return; } if (fi.fileFormat == fi.ZIP_ARCHIVE) { // restore ".zip" file ImagePlus imp2 = (new Opener()).openZip(path); if (imp2 != null) imp.setProcessor(null, imp2.getProcessor()); return; } // restore PNG or another image opened using ImageIO if (fi.fileFormat == fi.IMAGEIO) { ImagePlus imp2 = (new Opener()).openUsingImageIO(path); if (imp2 != null) imp.setProcessor(null, imp2.getProcessor()); return; } if (fi.nImages > 1) return; ColorModel cm; if (fi.url == null || fi.url.equals("")) IJ.showStatus("Loading: " + path); else IJ.showStatus("Loading: " + fi.url + fi.fileName); Object pixels = readPixels(fi); if (pixels == null) return; cm = createColorModel(fi); switch (fi.fileType) { case FileInfo.GRAY8: case FileInfo.COLOR8: case FileInfo.BITMAP: ip = new ByteProcessor(width, height, (byte[]) pixels, cm); imp.setProcessor(null, ip); break; case FileInfo.GRAY16_SIGNED: case FileInfo.GRAY16_UNSIGNED: case FileInfo.GRAY12_UNSIGNED: ip = new ShortProcessor(width, height, (short[]) pixels, cm); imp.setProcessor(null, ip); break; case FileInfo.GRAY32_INT: case FileInfo.GRAY32_FLOAT: ip = new FloatProcessor(width, height, (float[]) pixels, cm); imp.setProcessor(null, ip); break; case FileInfo.RGB: case FileInfo.BGR: case FileInfo.ARGB: case FileInfo.ABGR: case FileInfo.RGB_PLANAR: img = Toolkit.getDefaultToolkit() .createImage(new MemoryImageSource(width, height, (int[]) pixels, 0, width)); imp.setImage(img); break; } }
public void keyPressed(KeyEvent e) { // if (e.isConsumed()) return; int keyCode = e.getKeyCode(); IJ.setKeyDown(keyCode); hotkey = false; if (keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_SHIFT) return; char keyChar = e.getKeyChar(); int flags = e.getModifiers(); if (IJ.debugMode) IJ.log( "keyPressed: code=" + keyCode + " (" + KeyEvent.getKeyText(keyCode) + "), char=\"" + keyChar + "\" (" + (int) keyChar + "), flags=" + KeyEvent.getKeyModifiersText(flags)); boolean shift = (flags & KeyEvent.SHIFT_MASK) != 0; boolean control = (flags & KeyEvent.CTRL_MASK) != 0; boolean alt = (flags & KeyEvent.ALT_MASK) != 0; boolean meta = (flags & KeyEvent.META_MASK) != 0; String cmd = null; ImagePlus imp = WindowManager.getCurrentImage(); boolean isStack = (imp != null) && (imp.getStackSize() > 1); if (imp != null && !control && ((keyChar >= 32 && keyChar <= 255) || keyChar == '\b' || keyChar == '\n')) { Roi roi = imp.getRoi(); if (roi instanceof TextRoi) { if ((flags & KeyEvent.META_MASK) != 0 && IJ.isMacOSX()) return; if (alt) { switch (keyChar) { case 'u': case 'm': keyChar = IJ.micronSymbol; break; case 'A': keyChar = IJ.angstromSymbol; break; default: } } ((TextRoi) roi).addChar(keyChar); return; } } // Handle one character macro shortcuts if (!control && !meta) { Hashtable macroShortcuts = Menus.getMacroShortcuts(); if (macroShortcuts.size() > 0) { if (shift) cmd = (String) macroShortcuts.get(new Integer(keyCode + 200)); else cmd = (String) macroShortcuts.get(new Integer(keyCode)); if (cmd != null) { // MacroInstaller.runMacroCommand(cmd); commandName = cmd; MacroInstaller.runMacroShortcut(cmd); return; } } } if ((!Prefs.requireControlKey || control || meta) && keyChar != '+') { Hashtable shortcuts = Menus.getShortcuts(); if (shift) cmd = (String) shortcuts.get(new Integer(keyCode + 200)); else cmd = (String) shortcuts.get(new Integer(keyCode)); } if (cmd == null) { switch (keyChar) { case '<': case ',': if (isStack) cmd = "Previous Slice [<]"; break; case '>': case '.': case ';': if (isStack) cmd = "Next Slice [>]"; break; case '+': case '=': cmd = "In [+]"; break; case '-': cmd = "Out [-]"; break; case '/': cmd = "Reslice [/]..."; break; default: } } if (cmd == null) { switch (keyCode) { case KeyEvent.VK_TAB: WindowManager.putBehind(); return; case KeyEvent.VK_BACK_SPACE: // delete if (deleteOverlayRoi(imp)) return; cmd = "Clear"; hotkey = true; break; // case KeyEvent.VK_BACK_SLASH: cmd=IJ.altKeyDown()?"Animation Options...":"Start // Animation"; break; case KeyEvent.VK_EQUALS: cmd = "In [+]"; break; case KeyEvent.VK_MINUS: cmd = "Out [-]"; break; case KeyEvent.VK_SLASH: case 0xbf: cmd = "Reslice [/]..."; break; case KeyEvent.VK_COMMA: case 0xbc: if (isStack) cmd = "Previous Slice [<]"; break; case KeyEvent.VK_PERIOD: case 0xbe: if (isStack) cmd = "Next Slice [>]"; break; case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: // arrow keys if (imp == null) return; Roi roi = imp.getRoi(); if (IJ.shiftKeyDown() && imp == Orthogonal_Views.getImage()) return; boolean stackKey = imp.getStackSize() > 1 && (roi == null || IJ.shiftKeyDown()); boolean zoomKey = roi == null || IJ.shiftKeyDown() || IJ.controlKeyDown(); if (stackKey && keyCode == KeyEvent.VK_RIGHT) cmd = "Next Slice [>]"; else if (stackKey && keyCode == KeyEvent.VK_LEFT) cmd = "Previous Slice [<]"; else if (zoomKey && keyCode == KeyEvent.VK_DOWN && !ignoreArrowKeys(imp) && Toolbar.getToolId() < Toolbar.SPARE6) cmd = "Out [-]"; else if (zoomKey && keyCode == KeyEvent.VK_UP && !ignoreArrowKeys(imp) && Toolbar.getToolId() < Toolbar.SPARE6) cmd = "In [+]"; else if (roi != null) { if ((flags & KeyEvent.ALT_MASK) != 0) roi.nudgeCorner(keyCode); else roi.nudge(keyCode); return; } break; case KeyEvent.VK_ESCAPE: abortPluginOrMacro(imp); return; case KeyEvent.VK_ENTER: WindowManager.toFront(this); return; default: break; } } if (cmd != null && !cmd.equals("")) { commandName = cmd; if (cmd.equals("Fill") || cmd.equals("Draw")) hotkey = true; if (cmd.charAt(0) == MacroInstaller.commandPrefix) MacroInstaller.runMacroShortcut(cmd); else { doCommand(cmd); keyPressedTime = System.currentTimeMillis(); lastKeyCommand = cmd; } } }