Ejemplo n.º 1
0
 /** Sets the cursor based on the current tool and cursor location. */
 public void setCursor(int sx, int sy, int ox, int oy) {
   xMouse = ox;
   yMouse = oy;
   mouseExited = false;
   Roi roi = imp.getRoi();
   ImageWindow win = imp.getWindow();
   if (win == null) return;
   if (IJ.spaceBarDown()) {
     setCursor(handCursor);
     return;
   }
   int id = Toolbar.getToolId();
   switch (Toolbar.getToolId()) {
     case Toolbar.MAGNIFIER:
       setCursor(moveCursor);
       break;
     case Toolbar.HAND:
       setCursor(handCursor);
       break;
     default: // selection tool
       if (id == Toolbar.SPARE1 || id >= Toolbar.SPARE2) {
         if (Prefs.usePointerCursor) setCursor(defaultCursor);
         else setCursor(crosshairCursor);
       } else if (roi != null && roi.getState() != roi.CONSTRUCTING && roi.isHandle(sx, sy) >= 0)
         setCursor(handCursor);
       else if (Prefs.usePointerCursor
           || (roi != null && roi.getState() != roi.CONSTRUCTING && roi.contains(ox, oy)))
         setCursor(defaultCursor);
       else setCursor(crosshairCursor);
   }
 }
Ejemplo n.º 2
0
 protected void handleRoiMouseDown(MouseEvent e) {
   int sx = e.getX();
   int sy = e.getY();
   int ox = offScreenX(sx);
   int oy = offScreenY(sy);
   Roi roi = imp.getRoi();
   int handle = roi != null ? roi.isHandle(sx, sy) : -1;
   boolean multiPointMode =
       roi != null
           && (roi instanceof PointRoi)
           && handle == -1
           && Toolbar.getToolId() == Toolbar.POINT
           && Toolbar.getMultiPointMode();
   if (multiPointMode) {
     imp.setRoi(((PointRoi) roi).addPoint(ox, oy));
     return;
   }
   setRoiModState(e, roi, handle);
   if (roi != null) {
     if (handle >= 0) {
       roi.mouseDownInHandle(handle, sx, sy);
       return;
     }
     Rectangle r = roi.getBounds();
     int type = roi.getType();
     if (type == Roi.RECTANGLE
         && r.width == imp.getWidth()
         && r.height == imp.getHeight()
         && roi.getPasteMode() == Roi.NOT_PASTING
         && !(roi instanceof ImageRoi)) {
       imp.killRoi();
       return;
     }
     if (roi.contains(ox, oy)) {
       if (roi.modState == Roi.NO_MODS) roi.handleMouseDown(sx, sy);
       else {
         imp.killRoi();
         imp.createNewRoi(sx, sy);
       }
       return;
     }
     if ((type == Roi.POLYGON || type == Roi.POLYLINE || type == Roi.ANGLE)
         && roi.getState() == roi.CONSTRUCTING) return;
     int tool = Toolbar.getToolId();
     if ((tool == Toolbar.POLYGON || tool == Toolbar.POLYLINE || tool == Toolbar.ANGLE)
         && !(IJ.shiftKeyDown() || IJ.altKeyDown())) {
       imp.killRoi();
       return;
     }
   }
   imp.createNewRoi(sx, sy);
 }
Ejemplo n.º 3
0
 public void run(ImageProcessor ip) {
   if (enlarge && gd.wasOKed())
     synchronized (this) {
       if (!isEnlarged) {
         enlargeCanvas();
         isEnlarged = true;
       }
     }
   if (isEnlarged) { // enlarging may have made the ImageProcessor invalid, also for the parallel
                     // threads
     int slice = pfr.getSliceNumber();
     if (imp.getStackSize() == 1) ip = imp.getProcessor();
     else ip = imp.getStack().getProcessor(slice);
   }
   ip.setInterpolationMethod(interpolationMethod);
   if (fillWithBackground) {
     Color bgc = Toolbar.getBackgroundColor();
     if (bitDepth == 8) ip.setBackgroundValue(ip.getBestIndex(bgc));
     else if (bitDepth == 24) ip.setBackgroundValue(bgc.getRGB());
   } else ip.setBackgroundValue(0);
   ip.rotate(angle);
   if (!gd.wasOKed()) drawGridLines(gridLines);
   if (isEnlarged && imp.getStackSize() == 1) {
     imp.changes = true;
     imp.updateAndDraw();
     Undo.setup(Undo.COMPOUND_FILTER_DONE, imp);
   }
 }
Ejemplo n.º 4
0
 void handleMouseMove(int sx, int sy) {
   // Do rubber banding
   int tool = Toolbar.getToolId();
   if (!(tool == Toolbar.POLYGON || tool == Toolbar.POLYLINE || tool == Toolbar.ANGLE)) {
     imp.deleteRoi();
     imp.draw();
     return;
   }
   drawRubberBand(sx, sy);
   degrees = Double.NaN;
   double len = -1;
   if (nPoints > 1) {
     double x1, y1, x2, y2;
     if (xpf != null) {
       x1 = xpf[nPoints - 2];
       y1 = ypf[nPoints - 2];
       x2 = xpf[nPoints - 1];
       y2 = ypf[nPoints - 1];
     } else {
       x1 = xp[nPoints - 2];
       y1 = yp[nPoints - 2];
       x2 = xp[nPoints - 1];
       y2 = yp[nPoints - 1];
     }
     degrees =
         getAngle(
             (int) Math.round(x1),
             (int) Math.round(y1),
             (int) Math.round(x2),
             (int) Math.round(y2));
     if (tool != Toolbar.ANGLE) {
       Calibration cal = imp.getCalibration();
       double pw = cal.pixelWidth, ph = cal.pixelHeight;
       if (IJ.altKeyDown()) {
         pw = 1.0;
         ph = 1.0;
       }
       len = Math.sqrt((x2 - x1) * pw * (x2 - x1) * pw + (y2 - y1) * ph * (y2 - y1) * ph);
     }
   }
   if (tool == Toolbar.ANGLE) {
     if (nPoints == 2) angle1 = degrees;
     else if (nPoints == 3) {
       double angle2 = getAngle(xp[1], yp[1], xp[2], yp[2]);
       degrees = Math.abs(180 - Math.abs(angle1 - angle2));
       if (degrees > 180.0) degrees = 360.0 - degrees;
     }
   }
   String length = len != -1 ? ", length=" + IJ.d2s(len) : "";
   double degrees2 =
       tool == Toolbar.ANGLE && nPoints == 3 && Prefs.reflexAngle ? 360.0 - degrees : degrees;
   String angle = !Double.isNaN(degrees) ? ", angle=" + IJ.d2s(degrees2) : "";
   int ox = ic != null ? ic.offScreenX(sx) : sx;
   int oy = ic != null ? ic.offScreenY(sy) : sy;
   IJ.showStatus(imp.getLocationAsString(ox, oy) + length + angle);
 }
Ejemplo n.º 5
0
 private void init2(int type) {
   if (type == ANGLE && nPoints == 3) getAngleAsString();
   if (type == POINT && Toolbar.getMultiPointMode()) {
     Prefs.pointAutoMeasure = false;
     Prefs.pointAutoNextSlice = false;
     Prefs.pointAddToManager = false;
     userCreated = true;
   }
   if (lineWidth > 1 && isLine()) updateWideLine(lineWidth);
   finishPolygon();
 }
Ejemplo n.º 6
0
 protected void setDrawingColor(int ox, int oy, boolean setBackground) {
   // IJ.log("setDrawingColor: "+setBackground+this);
   int type = imp.getType();
   int[] v = imp.getPixel(ox, oy);
   switch (type) {
     case ImagePlus.GRAY8:
       {
         if (setBackground) setBackgroundColor(getColor(v[0]));
         else setForegroundColor(getColor(v[0]));
         break;
       }
     case ImagePlus.GRAY16:
     case ImagePlus.GRAY32:
       {
         double min = imp.getProcessor().getMin();
         double max = imp.getProcessor().getMax();
         double value = (type == ImagePlus.GRAY32) ? Float.intBitsToFloat(v[0]) : v[0];
         int index = (int) (255.0 * ((value - min) / (max - min)));
         if (index < 0) index = 0;
         if (index > 255) index = 255;
         if (setBackground) setBackgroundColor(getColor(index));
         else setForegroundColor(getColor(index));
         break;
       }
     case ImagePlus.COLOR_RGB:
     case ImagePlus.COLOR_256:
       {
         Color c = new Color(v[0], v[1], v[2]);
         if (setBackground) setBackgroundColor(c);
         else setForegroundColor(c);
         break;
       }
   }
   Color c;
   if (setBackground) c = Toolbar.getBackgroundColor();
   else {
     c = Toolbar.getForegroundColor();
     imp.setColor(c);
   }
   IJ.showStatus("(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + ")");
 }
Ejemplo n.º 7
0
 /** Starts the process of creating a new user-generated polygon or polyline ROI. */
 public PolygonRoi(int sx, int sy, ImagePlus imp) {
   super(sx, sy, imp);
   int tool = Toolbar.getToolId();
   switch (tool) {
     case Toolbar.POLYGON:
       type = POLYGON;
       break;
     case Toolbar.FREEROI:
       type = FREEROI;
       break;
     case Toolbar.FREELINE:
       type = FREELINE;
       if (Prefs.subPixelResolution) subPixel = true;
       break;
     case Toolbar.ANGLE:
       type = ANGLE;
       break;
     default:
       type = POLYLINE;
       if (Prefs.subPixelResolution) subPixel = true;
       break;
   }
   if (this instanceof EllipseRoi) subPixel = true;
   x = ic.offScreenX(sx);
   y = ic.offScreenY(sy);
   startXD = subPixelResolution() ? ic.offScreenXD(sx) : x;
   startYD = subPixelResolution() ? ic.offScreenYD(sy) : y;
   if (subPixelResolution()) {
     xpf = new float[maxPoints];
     ypf = new float[maxPoints];
     xpf[0] = (float) (startXD - x);
     ypf[0] = (float) (startYD - y);
     xpf[1] = xpf[0];
     ypf[1] = ypf[0];
   } else {
     xp = new int[maxPoints];
     yp = new int[maxPoints];
   }
   xp2 = new int[maxPoints];
   yp2 = new int[maxPoints];
   nPoints = 2;
   width = 1;
   height = 1;
   clipX = x;
   clipY = y;
   clipWidth = 1;
   clipHeight = 1;
   state = CONSTRUCTING;
   userCreated = true;
   if (lineWidth > 1 && isLine()) updateWideLine(lineWidth);
   drawOffset = subPixelResolution();
 }
Ejemplo n.º 8
0
 void setRoiModState(MouseEvent e, Roi roi, int handle) {
   if (roi == null || (handle >= 0 && roi.modState == Roi.NO_MODS)) return;
   if (roi.state == Roi.CONSTRUCTING) return;
   int tool = Toolbar.getToolId();
   if (tool > Toolbar.FREEROI && tool != Toolbar.WAND && tool != Toolbar.POINT) {
     roi.modState = Roi.NO_MODS;
     return;
   }
   if (e.isShiftDown()) roi.modState = Roi.ADD_TO_ROI;
   else if (e.isAltDown()) roi.modState = Roi.SUBTRACT_FROM_ROI;
   else roi.modState = Roi.NO_MODS;
   // IJ.log("setRoiModState: "+roi.modState+" "+ roi.state);
 }
Ejemplo n.º 9
0
 public void mouseDragged(MouseEvent e) {
   int x = e.getX();
   int y = e.getY();
   xMouse = offScreenX(x);
   yMouse = offScreenY(y);
   flags = e.getModifiers();
   // IJ.log("mouseDragged: "+flags);
   if (flags == 0) // workaround for Mac OS 9 bug
   flags = InputEvent.BUTTON1_MASK;
   if (Toolbar.getToolId() == Toolbar.HAND || IJ.spaceBarDown()) scroll(x, y);
   else {
     IJ.setInputEvent(e);
     Roi roi = imp.getRoi();
     if (roi != null) roi.handleMouseDrag(x, y, flags);
   }
 }
Ejemplo n.º 10
0
  public void mousePressed(MouseEvent e) {
    // if (ij==null) return;
    showCursorStatus = true;
    int toolID = Toolbar.getToolId();
    ImageWindow win = imp.getWindow();
    if (win != null && win.running2 && toolID != Toolbar.MAGNIFIER) {
      if (win instanceof StackWindow) ((StackWindow) win).setAnimate(false);
      else win.running2 = false;
      return;
    }

    int x = e.getX();
    int y = e.getY();
    flags = e.getModifiers();
    // IJ.log("Mouse pressed: " + e.isPopupTrigger() + "  " + ij.modifiers(flags));
    // if (toolID!=Toolbar.MAGNIFIER && e.isPopupTrigger()) {
    if (toolID != Toolbar.MAGNIFIER
        && (e.isPopupTrigger() || (!IJ.isMacintosh() && (flags & Event.META_MASK) != 0))) {
      handlePopupMenu(e);
      return;
    }

    int ox = offScreenX(x);
    int oy = offScreenY(y);
    xMouse = ox;
    yMouse = oy;
    if (IJ.spaceBarDown()) {
      // temporarily switch to "hand" tool of space bar down
      setupScroll(ox, oy);
      return;
    }
    if (showAllROIs) {
      Roi roi = imp.getRoi();
      if (!(roi != null && (roi.contains(ox, oy) || roi.isHandle(x, y) >= 0))
          && roiManagerSelect(x, y)) return;
    }
    if (customRoi && overlay != null) return;

    switch (toolID) {
      case Toolbar.MAGNIFIER:
        if (IJ.shiftKeyDown()) zoomToSelection(ox, oy);
        else if ((flags & (Event.ALT_MASK | Event.META_MASK | Event.CTRL_MASK)) != 0) {
          // IJ.run("Out");
          zoomOut(x, y);
          if (getMagnification() < 1.0) imp.repaintWindow();
        } else {
          // IJ.run("In");
          zoomIn(x, y);
          if (getMagnification() <= 1.0) imp.repaintWindow();
        }
        break;
      case Toolbar.HAND:
        setupScroll(ox, oy);
        break;
      case Toolbar.DROPPER:
        setDrawingColor(ox, oy, IJ.altKeyDown());
        break;
      case Toolbar.WAND:
        Roi roi = imp.getRoi();
        if (roi != null && roi.contains(ox, oy)) {
          Rectangle r = roi.getBounds();
          if (r.width == imageWidth && r.height == imageHeight) imp.killRoi();
          else if (!e.isAltDown()) {
            handleRoiMouseDown(e);
            return;
          }
        }
        if (roi != null) {
          int handle = roi.isHandle(x, y);
          if (handle >= 0) {
            roi.mouseDownInHandle(handle, x, y);
            return;
          }
        }
        setRoiModState(e, roi, -1);
        String mode = WandToolOptions.getMode();
        double tolerance = WandToolOptions.getTolerance();
        int npoints = IJ.doWand(ox, oy, tolerance, mode);
        if (Recorder.record && npoints > 0) {
          if (tolerance == 0.0 && mode.equals("Legacy")) Recorder.record("doWand", ox, oy);
          else
            Recorder.recordString(
                "doWand(" + ox + ", " + oy + ", " + tolerance + ", \"" + mode + "\");\n");
        }
        break;
      case Toolbar.OVAL:
        if (Toolbar.getBrushSize() > 0) new RoiBrush();
        else handleRoiMouseDown(e);
        break;
      case Toolbar.SPARE1:
      case Toolbar.SPARE2:
      case Toolbar.SPARE3:
      case Toolbar.SPARE4:
      case Toolbar.SPARE5:
      case Toolbar.SPARE6:
      case Toolbar.SPARE7:
      case Toolbar.SPARE8:
      case Toolbar.SPARE9:
        Toolbar.getInstance().runMacroTool(toolID);
        break;
      default: // selection tool
        handleRoiMouseDown(e);
    }
  }
Ejemplo n.º 11
0
 private void setBackgroundColor(Color c) {
   Toolbar.setBackgroundColor(c);
   if (Recorder.record)
     Recorder.record("setBackgroundColor", c.getRed(), c.getGreen(), c.getBlue());
 }