Beispiel #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);
 }
Beispiel #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;
   }
 }
 public void run(String arg) {
   if (!LibraryChecker.checkImageJ()) return;
   if (!IJ.macroRunning()) {
     IJ.error("Cannot install extensions from outside a macro.");
     return;
   }
   Functions.registerExtensions(this);
 }
 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);
 }
Beispiel #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("");
 }
Beispiel #6
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);
  }
Beispiel #9
0
 /**
  * Attempts to open the specified file as a tiff, bmp, dicom, fits, pgm, gif or jpeg image.
  * Returns an ImagePlus object if successful. Modified by Gregory Jefferis to call
  * HandleExtraFileTypes plugin if the file type is unrecognised.
  *
  * @see ij.IJ#openImage(String)
  */
 public ImagePlus openImage(String directory, String name) {
   ImagePlus imp;
   FileOpener.setSilentMode(silentMode);
   if (directory.length() > 0 && !(directory.endsWith("/") || directory.endsWith("\\")))
     directory += Prefs.separator;
   String path = directory + name;
   fileType = getFileType(path);
   if (IJ.debugMode) IJ.log("openImage: \"" + types[fileType] + "\", " + path);
   switch (fileType) {
     case TIFF:
       imp = openTiff(directory, name);
       return imp;
     case DICOM:
       imp = (ImagePlus) IJ.runPlugIn("ij.plugin.DICOM", path);
       if (imp.getWidth() != 0) return imp;
       else return null;
     case TIFF_AND_DICOM:
       // "hybrid" files created by GE-Senographe 2000 D */
       imp = openTiff(directory, name);
       ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.DICOM", path);
       if (imp != null && imp2 != null) {
         imp.setProperty("Info", imp2.getProperty("Info"));
         imp.setCalibration(imp2.getCalibration());
       }
       if (imp == null) imp = imp2;
       return imp;
     case FITS:
       imp = (ImagePlus) IJ.runPlugIn("ij.plugin.FITS_Reader", path);
       if (imp.getWidth() != 0) return imp;
       else return null;
     case PGM:
       imp = (ImagePlus) IJ.runPlugIn("ij.plugin.PGM_Reader", path);
       if (imp.getWidth() != 0) {
         if (imp.getStackSize() == 3 && imp.getBitDepth() == 16)
           imp = new CompositeImage(imp, IJ.COMPOSITE);
         return imp;
       } else return null;
     case JPEG:
       imp = openJpegOrGif(directory, name);
       if (imp != null && imp.getWidth() != 0) return imp;
       else return null;
     case GIF:
       imp = (ImagePlus) IJ.runPlugIn("ij.plugin.GIF_Reader", path);
       if (imp != null && imp.getWidth() != 0) return imp;
       else return null;
     case PNG:
       imp = openUsingImageIO(directory + name);
       if (imp != null && imp.getWidth() != 0) return imp;
       else return null;
     case BMP:
       imp = (ImagePlus) IJ.runPlugIn("ij.plugin.BMP_Reader", path);
       if (imp.getWidth() != 0) return imp;
       else return null;
     case ZIP:
       return openZip(path);
     case AVI:
       AVI_Reader reader = new AVI_Reader();
       reader.displayDialog(!IJ.macroRunning());
       reader.run(path);
       return reader.getImagePlus();
     case UNKNOWN:
     case TEXT:
       // Call HandleExtraFileTypes plugin to see if it can handle unknown format
       int[] wrap = new int[] {fileType};
       imp = openWithHandleExtraFileTypes(path, wrap);
       if (imp != null && imp.getNChannels() > 1) imp = new CompositeImage(imp, IJ.COLOR);
       fileType = wrap[0];
       if (imp == null && fileType == UNKNOWN && IJ.getInstance() == null)
         IJ.error("Opener", "Unsupported format or not found");
       return imp;
     default:
       return null;
   }
 }