Exemple #1
0
 void addToRoiManager(ImagePlus imp) {
   if (IJ.macroRunning() && Interpreter.isBatchModeRoiManager())
     IJ.error("run(\"Add to Manager\") may not work in batch mode macros");
   Frame frame = WindowManager.getFrame("ROI Manager");
   if (frame == null) IJ.run("ROI Manager...");
   if (imp == null) return;
   Roi roi = imp.getRoi();
   if (roi == null) return;
   frame = WindowManager.getFrame("ROI Manager");
   if (frame == null || !(frame instanceof RoiManager)) IJ.error("ROI Manager not found");
   RoiManager rm = (RoiManager) frame;
   boolean altDown = IJ.altKeyDown();
   IJ.setKeyUp(IJ.ALL_KEYS);
   if (altDown && !IJ.macroRunning()) IJ.setKeyDown(KeyEvent.VK_SHIFT);
   if (roi.getState() == Roi.CONSTRUCTING) { // wait (up to 2 sec.) until ROI finished
     long start = System.currentTimeMillis();
     while (true) {
       IJ.wait(10);
       if (roi.getState() != Roi.CONSTRUCTING) break;
       if ((System.currentTimeMillis() - start) > 2000) {
         IJ.beep();
         IJ.error("Add to Manager", "Selection is not complete");
         return;
       }
     }
   }
   rm.runCommand("add");
   IJ.setKeyUp(IJ.ALL_KEYS);
 }
Exemple #2
0
 void runMacro(String arg) {
   Roi roi = imp.getRoi();
   if (IJ.macroRunning()) {
     String options = Macro.getOptions();
     if (options != null
         && (options.indexOf("grid=") != -1 || options.indexOf("interpolat") != -1)) {
       IJ.run("Rotate... ", options); // run Image>Transform>Rotate
       return;
     }
   }
   if (roi == null) {
     noRoi("Rotate>Selection");
     return;
   }
   roi = (Roi) roi.clone();
   if (arg.equals("rotate")) {
     double d = Tools.parseDouble(angle);
     if (Double.isNaN(d)) angle = "15";
     String value = IJ.runMacroFile("ij.jar:RotateSelection", angle);
     if (value != null) angle = value;
   } else if (arg.equals("enlarge")) {
     String value = IJ.runMacroFile("ij.jar:EnlargeSelection", enlarge);
     if (value != null) enlarge = value;
     Roi.previousRoi = roi;
   }
 }
Exemple #3
0
 /** Called by ImageJ when the user selects Quit. */
 public void quit() {
   quitMacro = IJ.macroRunning();
   Thread thread = new Thread(this, "Quit");
   thread.setPriority(Thread.NORM_PRIORITY);
   thread.start();
   IJ.wait(10);
 }
 void addToRoiManager(ImagePlus imp) {
   if (IJ.macroRunning() && Interpreter.isBatchModeRoiManager())
     IJ.error("run(\"Add to Manager\") may not work in batch mode macros");
   Frame frame = WindowManager.getFrame("ROI Manager");
   if (frame == null) IJ.run("ROI Manager...");
   if (imp == null) return;
   Roi roi = imp.getRoi();
   if (roi == null) return;
   frame = WindowManager.getFrame("ROI Manager");
   if (frame == null || !(frame instanceof RoiManager)) IJ.error("ROI Manager not found");
   RoiManager rm = (RoiManager) frame;
   boolean altDown = IJ.altKeyDown();
   IJ.setKeyUp(IJ.ALL_KEYS);
   if (altDown && !IJ.macroRunning()) IJ.setKeyDown(KeyEvent.VK_SHIFT);
   rm.runCommand("add");
   IJ.setKeyUp(IJ.ALL_KEYS);
 }
Exemple #5
0
 private void rotate(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (IJ.macroRunning()) {
     String options = Macro.getOptions();
     if (options != null
         && (options.indexOf("grid=") != -1 || options.indexOf("interpolat") != -1)) {
       IJ.run("Rotate... ", options); // run Image>Transform>Rotate
       return;
     }
   }
   (new RoiRotator()).run("");
 }
Exemple #6
0
 /** Quit using a separate thread, hopefully avoiding thread deadlocks. */
 public void run() {
   quitting = true;
   boolean changes = false;
   int[] wList = WindowManager.getIDList();
   if (wList != null) {
     for (int i = 0; i < wList.length; i++) {
       ImagePlus imp = WindowManager.getImage(wList[i]);
       if (imp != null && imp.changes == true) {
         changes = true;
         break;
       }
     }
   }
   Frame[] frames = WindowManager.getNonImageWindows();
   if (frames != null) {
     for (int i = 0; i < frames.length; i++) {
       if (frames[i] != null && (frames[i] instanceof Editor)) {
         if (((Editor) frames[i]).fileChanged()) {
           changes = true;
           break;
         }
       }
     }
   }
   if (windowClosed
       && !changes
       && Menus.window.getItemCount() > Menus.WINDOW_MENU_ITEMS
       && !(IJ.macroRunning() && WindowManager.getImageCount() == 0)) {
     GenericDialog gd = new GenericDialog("ImageJ", this);
     gd.addMessage("Are you sure you want to quit ImageJ?");
     gd.showDialog();
     quitting = !gd.wasCanceled();
     windowClosed = false;
   }
   if (!quitting) return;
   if (!WindowManager.closeAllWindows()) {
     quitting = false;
     return;
   }
   // IJ.log("savePreferences");
   if (applet == null) {
     saveWindowLocations();
     Prefs.savePreferences();
   }
   IJ.cleanup();
   // setVisible(false);
   // IJ.log("dispose");
   dispose();
   if (exitWhenQuitting) System.exit(0);
 }
 void addSelection() {
   ImagePlus imp = IJ.getImage();
   String macroOptions = Macro.getOptions();
   if (macroOptions != null && IJ.macroRunning() && macroOptions.indexOf("remove") != -1) {
     imp.setOverlay(null);
     return;
   }
   Roi roi = imp.getRoi();
   if (roi == null && imp.getOverlay() != null) {
     GenericDialog gd = new GenericDialog("No Selection");
     gd.addMessage("\"Overlay>Add\" requires a selection.");
     gd.setInsets(15, 40, 0);
     gd.addCheckbox("Remove existing overlay", false);
     gd.showDialog();
     if (gd.wasCanceled()) return;
     if (gd.getNextBoolean()) imp.setOverlay(null);
     return;
   }
   if (roi == null) {
     IJ.error("This command requires a selection.");
     return;
   }
   roi = (Roi) roi.clone();
   if (roi.getStrokeColor() == null) roi.setStrokeColor(Toolbar.getForegroundColor());
   int width = Line.getWidth();
   Rectangle bounds = roi.getBounds();
   boolean tooWide = width > Math.max(bounds.width, bounds.height) / 3.0;
   if (roi.getStroke() == null && width > 1 && !tooWide) roi.setStrokeWidth(Line.getWidth());
   Overlay overlay = imp.getOverlay();
   if (overlay != null && overlay.size() > 0 && !roi.isDrawingTool()) {
     Roi roi2 = overlay.get(overlay.size() - 1);
     if (roi.getStroke() == null) roi.setStrokeWidth(roi2.getStrokeWidth());
     if (roi.getFillColor() == null) roi.setFillColor(roi2.getFillColor());
   }
   boolean points = roi instanceof PointRoi && ((PolygonRoi) roi).getNCoordinates() > 1;
   if (points) roi.setStrokeColor(Color.red);
   if (!IJ.altKeyDown() && !(roi instanceof Arrow)) {
     RoiProperties rp = new RoiProperties("Add to Overlay", roi);
     if (!rp.showDialog()) return;
   }
   String name = roi.getName();
   boolean newOverlay = name != null && name.equals("new-overlay");
   if (overlay == null || newOverlay) overlay = new Overlay();
   overlay.add(roi);
   imp.setOverlay(overlay);
   overlay2 = overlay;
   if (points || (roi instanceof ImageRoi) || (roi instanceof Arrow)) imp.killRoi();
   Undo.setup(Undo.OVERLAY_ADDITION, imp);
 }
 void runMacro(String arg, ImagePlus imp) {
   boolean rotate = arg.equals("rotate");
   Roi roi = imp.getRoi();
   if (rotate && IJ.macroRunning()) {
     String options = Macro.getOptions();
     if (options != null
         && (options.indexOf("grid=") != -1 || options.indexOf("interpolat") != -1)) {
       IJ.run("Rotate... ", options); // run Image>Transform>Rotate
       return;
     }
   }
   if (roi == null) {
     noRoi(rotate ? "Rotate" : "Enlarge");
     return;
   }
   double dangle = Tools.parseDouble(angle);
   if (Double.isNaN(dangle)) {
     dangle = 15;
     angle = "" + dangle;
   }
   if (rotate && (roi instanceof ImageRoi)) {
     dangle = IJ.getNumber("Angle (degrees):", dangle);
     ((ImageRoi) roi).rotate(dangle);
     imp.draw();
     angle = "" + dangle;
     return;
   }
   Undo.setup(Undo.ROI, imp);
   roi = (Roi) roi.clone();
   if (rotate) {
     String value = IJ.runMacroFile("ij.jar:RotateSelection", angle);
     Roi roi2 = imp.getRoi();
     transferProperties(roi, roi2);
     imp.setRoi(roi2);
     if (value != null) angle = value;
   } else if (arg.equals("enlarge")) (new RoiEnlarger()).run("");
 }
  public void run(String arg) {
    int wOld, hOld, wNew, hNew;
    boolean fIsStack = false;

    ImagePlus imp = IJ.getImage();
    wOld = imp.getWidth();
    hOld = imp.getHeight();

    ImageStack stackOld = imp.getStack();
    if ((stackOld != null) && (stackOld.getSize() > 1)) fIsStack = true;

    String[] sPositions = {
      "Top-Left", "Top-Center", "Top-Right",
      "Center-Left", "Center", "Center-Right",
      "Bottom-Left", "Bottom-Center", "Bottom-Right"
    };

    String strTitle = fIsStack ? "Resize Stack Canvas" : "Resize Image Canvas";
    GenericDialog gd = new GenericDialog(strTitle);
    gd.addNumericField("Width:", wOld, 0, 5, "pixels");
    gd.addNumericField("Height:", hOld, 0, 5, "pixels");
    gd.addChoice("Position:", sPositions, sPositions[4]);
    gd.addCheckbox("Zero Fill", zeroFill);
    gd.showDialog();
    if (gd.wasCanceled()) return;

    wNew = (int) gd.getNextNumber();
    hNew = (int) gd.getNextNumber();
    int iPos = gd.getNextChoiceIndex();
    zeroFill = gd.getNextBoolean();
    Prefs.set("resizer.zero", zeroFill);

    int xOff, yOff;
    int xC = (wNew - wOld) / 2; // offset for centered
    int xR = (wNew - wOld); // offset for right
    int yC = (hNew - hOld) / 2; // offset for centered
    int yB = (hNew - hOld); // offset for bottom

    switch (iPos) {
      case 0: // TL
        xOff = 0;
        yOff = 0;
        break;
      case 1: // TC
        xOff = xC;
        yOff = 0;
        break;
      case 2: // TR
        xOff = xR;
        yOff = 0;
        break;
      case 3: // CL
        xOff = 0;
        yOff = yC;
        break;
      case 4: // C
        xOff = xC;
        yOff = yC;
        break;
      case 5: // CR
        xOff = xR;
        yOff = yC;
        break;
      case 6: // BL
        xOff = 0;
        yOff = yB;
        break;
      case 7: // BC
        xOff = xC;
        yOff = yB;
        break;
      case 8: // BR
        xOff = xR;
        yOff = yB;
        break;
      default: // center
        xOff = xC;
        yOff = yC;
        break;
    }

    if (fIsStack) {
      ImageStack stackNew = expandStack(stackOld, wNew, hNew, xOff, yOff);
      imp.setStack(null, stackNew);
    } else {
      if (!IJ.macroRunning()) Undo.setup(Undo.COMPOUND_FILTER, imp);
      ImageWindow win = imp.getWindow();
      if (win != null && (win instanceof PlotWindow)) ((PlotWindow) win).getPlot().setFrozen(true);
      ImageProcessor newIP = expandImage(imp.getProcessor(), wNew, hNew, xOff, yOff);
      imp.setProcessor(null, newIP);
      if (!IJ.macroRunning()) Undo.setup(Undo.COMPOUND_FILTER_DONE, imp);
    }
    Overlay overlay = imp.getOverlay();
    if (overlay != null) overlay.translate(xOff, yOff);
  }