/** Redoes the text. */ final void redo() { if (undo == null) return; text = new BaseXTextTokens(undo.next()); rend.setText(text); text.pos(undo.cursor()); text.setCaret(); }
void scale(ImageProcessor ip) { if (newWindow) { Rectangle r = ip.getRoi(); ImagePlus imp2 = imp.createImagePlus(); imp2.setProcessor(title, ip.resize(newWidth, newHeight)); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } imp2.show(); imp.trimProcessor(); imp2.trimProcessor(); imp2.changes = true; } else { if (processStack && imp.getStackSize() > 1) { Undo.reset(); StackProcessor sp = new StackProcessor(imp.getStack(), ip); sp.scale(xscale, yscale, bgValue); } else { ip.snapshot(); Undo.setup(Undo.FILTER, imp); ip.setSnapshotCopyMode(true); ip.scale(xscale, yscale); ip.setSnapshotCopyMode(false); } imp.killRoi(); imp.updateAndDraw(); imp.changes = true; } }
/** Deletes the selected text. */ final void delete() { if (undo == null) return; text.pos(text.cursor()); undo.cursor(text.cursor()); text.delete(); undo.store(text.text(), text.cursor()); text.setCaret(); }
/** * Pastes the specified string. * * @param txt string to be pasted * @return success flag */ final boolean paste(final String txt) { if (txt == null || undo == null) return false; text.pos(text.cursor()); undo.cursor(text.cursor()); if (text.marked()) text.delete(); text.add(txt); undo.store(text.text(), text.cursor()); return true; }
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); } }
void createEllipse(ImagePlus imp) { IJ.showStatus("Fitting ellipse"); Roi roi = imp.getRoi(); if (roi == null) { noRoi("Fit Ellipse"); return; } if (roi.isLine()) { IJ.error("Fit Ellipse", "\"Fit Ellipse\" does not work with line selections"); return; } ImageProcessor ip = imp.getProcessor(); ip.setRoi(roi); int options = Measurements.CENTROID + Measurements.ELLIPSE; ImageStatistics stats = ImageStatistics.getStatistics(ip, options, null); double dx = stats.major * Math.cos(stats.angle / 180.0 * Math.PI) / 2.0; double dy = -stats.major * Math.sin(stats.angle / 180.0 * Math.PI) / 2.0; double x1 = stats.xCentroid - dx; double x2 = stats.xCentroid + dx; double y1 = stats.yCentroid - dy; double y2 = stats.yCentroid + dy; double aspectRatio = stats.minor / stats.major; Undo.setup(Undo.ROI, imp); imp.deleteRoi(); Roi roi2 = new EllipseRoi(x1, y1, x2, y2, aspectRatio); transferProperties(roi, roi2); imp.setRoi(roi2); }
void interpolate() { Roi roi = imp.getRoi(); if (roi == null) { noRoi("Interpolate"); return; } if (roi.getType() == Roi.POINT) return; if (IJ.isMacro() && Macro.getOptions() == null) Macro.setOptions("interval=1"); GenericDialog gd = new GenericDialog("Interpolate"); gd.addNumericField("Interval:", 1.0, 1, 4, "pixel"); gd.addCheckbox("Smooth", IJ.isMacro() ? false : smooth); gd.showDialog(); if (gd.wasCanceled()) return; double interval = gd.getNextNumber(); smooth = gd.getNextBoolean(); Undo.setup(Undo.ROI, imp); FloatPolygon poly = roi.getInterpolatedPolygon(interval, smooth); int t = roi.getType(); int type = roi.isLine() ? Roi.FREELINE : Roi.FREEROI; if (t == Roi.POLYGON && interval > 1.0) type = Roi.POLYGON; if ((t == Roi.RECTANGLE || t == Roi.OVAL || t == Roi.FREEROI) && interval >= 5.0) type = Roi.POLYGON; if ((t == Roi.LINE || t == Roi.FREELINE) && interval >= 5.0) type = Roi.POLYLINE; if (t == Roi.POLYLINE && interval >= 1.0) type = Roi.POLYLINE; ImageCanvas ic = imp.getCanvas(); if (poly.npoints <= 150 && ic != null && ic.getMagnification() >= 12.0) type = roi.isLine() ? Roi.POLYLINE : Roi.POLYGON; Roi p = new PolygonRoi(poly, type); if (roi.getStroke() != null) p.setStrokeWidth(roi.getStrokeWidth()); p.setStrokeColor(roi.getStrokeColor()); p.setName(roi.getName()); transferProperties(roi, p); imp.setRoi(p); }
/** * Sets the output text. * * @param t output text * @param s text size */ public final void setText(final byte[] t, final int s) { // remove invalid characters and compare old with new string int ns = 0; final int ts = text.size(); final byte[] tt = text.text(); boolean eq = true; for (int r = 0; r < s; ++r) { final byte b = t[r]; // support characters, highlighting codes, tabs and newlines if (b >= ' ' || b <= TokenBuilder.MARK || b == 0x09 || b == 0x0A) { t[ns++] = t[r]; } eq &= ns < ts && ns < s && t[ns] == tt[ns]; } eq &= ns == ts; // new text is different... if (!eq) { text = new BaseXTextTokens(Arrays.copyOf(t, ns)); rend.setText(text); scroll.pos(0); } if (undo != null) undo.store(t.length != ns ? Arrays.copyOf(t, ns) : t, 0); SwingUtilities.invokeLater(calc); }
void showResults() { int count = rt.getCounter(); // if (count==0) return; boolean lastSlice = !processStack || slice == imp.getStackSize(); if ((showChoice == OVERLAY_OUTLINES || showChoice == OVERLAY_MASKS) && slice == 1 && count > 0) imp.setOverlay(overlay); else if (outlines != null && lastSlice) { String title = imp != null ? imp.getTitle() : "Outlines"; String prefix; if (showChoice == MASKS) prefix = "Mask of "; else if (showChoice == ROI_MASKS) prefix = "Count Masks of "; else prefix = "Drawing of "; outlines.update(drawIP); outputImage = new ImagePlus(prefix + title, outlines); if (inSituShow) { if (imp.getStackSize() == 1) Undo.setup(Undo.TRANSFORM, imp); imp.setStack(null, outputImage.getStack()); } else if (!hideOutputImage) outputImage.show(); } if (showResults && !processStack) { TextPanel tp = IJ.getTextPanel(); if (beginningCount > 0 && tp != null && tp.getLineCount() != count) rt.show("Results"); Analyzer.firstParticle = beginningCount; Analyzer.lastParticle = Analyzer.getCounter() - 1; } else Analyzer.firstParticle = Analyzer.lastParticle = 0; }
void fitSpline() { Roi roi = imp.getRoi(); if (roi == null) { noRoi("Spline"); return; } int type = roi.getType(); boolean segmentedSelection = type == Roi.POLYGON || type == Roi.POLYLINE; if (!(segmentedSelection || type == Roi.FREEROI || type == Roi.TRACED_ROI || type == Roi.FREELINE)) { IJ.error("Spline", "Polygon or polyline selection required"); return; } if (roi instanceof EllipseRoi) return; PolygonRoi p = (PolygonRoi) roi; Undo.setup(Undo.ROI, imp); if (!segmentedSelection && p.getNCoordinates() > 3) { if (p.subPixelResolution()) p = trimFloatPolygon(p, p.getUncalibratedLength()); else p = trimPolygon(p, p.getUncalibratedLength()); } String options = Macro.getOptions(); if (options != null && options.indexOf("straighten") != -1) p.fitSplineForStraightening(); else if (options != null && options.indexOf("remove") != -1) p.removeSplineFit(); else p.fitSpline(); imp.draw(); LineWidthAdjuster.update(); }
void lineToArea(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isLine()) { IJ.error("Line to Area", "Line selection required"); return; } Undo.setup(Undo.ROI, imp); Roi roi2 = null; if (roi.getType() == Roi.LINE) { double width = roi.getStrokeWidth(); if (width <= 1.0) roi.setStrokeWidth(1.0000001); FloatPolygon p = roi.getFloatPolygon(); roi.setStrokeWidth(width); roi2 = new PolygonRoi(p, Roi.POLYGON); roi2.setDrawOffset(roi.getDrawOffset()); } else { ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight()); ip2.setColor(255); roi.drawPixels(ip2); // new ImagePlus("ip2", ip2.duplicate()).show(); ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE); ThresholdToSelection tts = new ThresholdToSelection(); roi2 = tts.convert(ip2); } transferProperties(roi, roi2); roi2.setStrokeWidth(0); Color c = roi2.getStrokeColor(); if (c != null) // remove any transparency roi2.setStrokeColor(new Color(c.getRed(), c.getGreen(), c.getBlue())); imp.setRoi(roi2); Roi.previousRoi = (Roi) roi.clone(); }
private void enlarge(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi != null) { Undo.setup(Undo.ROI, imp); roi = (Roi) roi.clone(); (new RoiEnlarger()).run(""); } else noRoi("Enlarge"); }
public VRedoButton(SessionShare sshare, ButtonIF vif, String typ) { super(sshare, vif, typ); addFocusListener( new FocusAdapter() { public void focusGained(FocusEvent evt) { Undo.restoreLastUndoMgr(); } }); Undo.addUndoListener(this); }
public void undoableEditHappened(UndoableEditEvent e) { UndoManager mgr = Undo.getCurrentUndoMgr(); if (mgr != null && mgr.canRedo()) { setToolTipText(mgr.getRedoPresentationName()); setEnabled(true); } else { setToolTipText(tipStr); setEnabled(false); } }
/** @param undo */ public final void storeUndo(final Undo undo) { if (Sniper.undoCacheSize <= 0) { return; } if (undo != null && undo.getSize() > 0) { while (this.undoList.size() > Sniper.undoCacheSize) { this.undoList.pop(); } this.undoList.add(undo); } }
void invert(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isArea()) { IJ.error("Inverse", "Area selection required"); return; } ShapeRoi s1, s2; if (roi instanceof ShapeRoi) s1 = (ShapeRoi) roi; else s1 = new ShapeRoi(roi); s2 = new ShapeRoi(new Roi(0, 0, imp.getWidth(), imp.getHeight())); Undo.setup(Undo.ROI, imp); imp.setRoi(s1.xor(s2)); }
void toBoundingBox(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null) { noRoi("To Bounding Box"); return; } Undo.setup(Undo.ROI, imp); Rectangle r = roi.getBounds(); imp.deleteRoi(); Roi roi2 = new Roi(r.x, r.y, r.width, r.height); transferProperties(roi, roi2); imp.setRoi(roi2); }
/** @param num */ public final void doUndo(final int num) { int _sum = 0; if (this.undoList.isEmpty()) { this.player.sendMessage(ChatColor.GREEN + "Nothing to undo"); } else { for (int _x = 0; _x < num; _x++) { final Undo _undo = this.undoList.pollLast(); if (_undo != null) { _undo.undo(); _sum += _undo.getSize(); } else { break; } } this.player.sendMessage( ChatColor.GREEN + "Undo succesfull! " + ChatColor.RED + _sum + ChatColor.GREEN + " Blocks have been replaced."); } }
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); }
/** * Execute the plugin functionality: duplicate and scale the given image. * * @return an Object[] array with the name and the scaled ImagePlus. Does NOT show the new, image; * just returns it. */ public Object[] exec( ImagePlus imp, String myMethod, int radius, double par1, double par2, boolean doIwhite) { // 0 - Check validity of parameters if (null == imp) return null; ImageProcessor ip = imp.getProcessor(); int xe = ip.getWidth(); int ye = ip.getHeight(); // int [] data = (ip.getHistogram()); IJ.showStatus("Thresholding..."); long startTime = System.currentTimeMillis(); // 1 Do it if (imp.getStackSize() == 1) { ip.snapshot(); Undo.setup(Undo.FILTER, imp); } // Apply the selected algorithm if (myMethod.equals("Bernsen")) { Bernsen(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Contrast")) { Contrast(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Mean")) { Mean(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Median")) { Median(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("MidGrey")) { MidGrey(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Niblack")) { Niblack(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Otsu")) { Otsu(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Phansalkar")) { Phansalkar(imp, radius, par1, par2, doIwhite); } else if (myMethod.equals("Sauvola")) { Sauvola(imp, radius, par1, par2, doIwhite); } // IJ.showProgress((double)(255-i)/255); imp.updateAndDraw(); imp.getProcessor().setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE); // 2 - Return the threshold and the image IJ.showStatus("\nDone " + (System.currentTimeMillis() - startTime) / 1000.0); return new Object[] {imp}; }
void enlargeCanvas() { imp.unlock(); if (imp.getStackSize() == 1) Undo.setup(Undo.COMPOUND_FILTER, imp); IJ.run("Select All"); IJ.run("Rotate...", "angle=" + angle); Roi roi = imp.getRoi(); Rectangle r = roi.getBounds(); if (r.width < imp.getWidth()) r.width = imp.getWidth(); if (r.height < imp.getHeight()) r.height = imp.getHeight(); IJ.showStatus("Rotate: Enlarging..."); IJ.run( "Canvas Size...", "width=" + r.width + " height=" + r.height + " position=Center " + (fillWithBackground ? "" : "zero")); IJ.showStatus("Rotating..."); }
void areaToLine(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null || !roi.isArea()) { IJ.error("Area to Line", "Area selection required"); return; } Undo.setup(Undo.ROI, imp); Polygon p = roi.getPolygon(); if (p == null) return; int type1 = roi.getType(); if (type1 == Roi.COMPOSITE) { IJ.error("Area to Line", "Composite selections cannot be converted to lines."); return; } int type2 = Roi.POLYLINE; if (type1 == Roi.OVAL || type1 == Roi.FREEROI || type1 == Roi.TRACED_ROI || ((roi instanceof PolygonRoi) && ((PolygonRoi) roi).isSplineFit())) type2 = Roi.FREELINE; Roi roi2 = new PolygonRoi(p.xpoints, p.ypoints, p.npoints, type2); transferProperties(roi, roi2); imp.setRoi(roi2); }
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(""); }
void convexHull(ImagePlus imp) { Roi roi = imp.getRoi(); int type = roi != null ? roi.getType() : -1; if (!(type == Roi.FREEROI || type == Roi.TRACED_ROI || type == Roi.POLYGON || type == Roi.POINT)) { IJ.error("Convex Hull", "Polygonal or point selection required"); return; } if (roi instanceof EllipseRoi) return; // if (roi.subPixelResolution() && roi instanceof PolygonRoi) { // FloatPolygon p = ((PolygonRoi)roi).getFloatConvexHull(); // if (p!=null) // imp.setRoi(new PolygonRoi(p.xpoints, p.ypoints, p.npoints, roi.POLYGON)); // } else { Polygon p = roi.getConvexHull(); if (p != null) { Undo.setup(Undo.ROI, imp); Roi roi2 = new PolygonRoi(p.xpoints, p.ypoints, p.npoints, roi.POLYGON); transferProperties(roi, roi2); imp.setRoi(roi2); } }
void addImage() { ImagePlus imp = IJ.getImage(); int[] wList = WindowManager.getIDList(); if (wList == null || wList.length < 2) { IJ.error("Add Image...", "The command requires at least two open images."); return; } String[] titles = new String[wList.length]; for (int i = 0; i < wList.length; i++) { ImagePlus imp2 = WindowManager.getImage(wList[i]); titles[i] = imp2 != null ? imp2.getTitle() : ""; } int x = 0, y = 0; Roi roi = imp.getRoi(); if (roi != null && roi.isArea()) { Rectangle r = roi.getBounds(); x = r.x; y = r.y; } int index = 0; if (wList.length == 2) { ImagePlus i1 = WindowManager.getImage(wList[0]); ImagePlus i2 = WindowManager.getImage(wList[1]); if (i2.getWidth() < i1.getWidth() && i2.getHeight() < i1.getHeight()) index = 1; } else if (imp.getID() == wList[0]) index = 1; GenericDialog gd = new GenericDialog("Add Image..."); gd.addChoice("Image to add:", titles, titles[index]); gd.addNumericField("X location:", x, 0); gd.addNumericField("Y location:", y, 0); gd.addNumericField("Opacity (0-100%):", 100, 0); gd.addCheckbox("Create image selection", createImageRoi); gd.showDialog(); if (gd.wasCanceled()) return; index = gd.getNextChoiceIndex(); x = (int) gd.getNextNumber(); y = (int) gd.getNextNumber(); double opacity = gd.getNextNumber() / 100.0; createImageRoi = gd.getNextBoolean(); ImagePlus overlay = WindowManager.getImage(wList[index]); if (wList.length == 2) { ImagePlus i1 = WindowManager.getImage(wList[0]); ImagePlus i2 = WindowManager.getImage(wList[1]); if (i2.getWidth() < i1.getWidth() && i2.getHeight() < i1.getHeight()) { imp = i1; overlay = i2; } } if (overlay == imp) { IJ.error( "Add Image...", "Image to be added cannot be the same as\n\"" + imp.getTitle() + "\"."); return; } if (overlay.getWidth() > imp.getWidth() && overlay.getHeight() > imp.getHeight()) { IJ.error( "Add Image...", "Image to be added cannnot be larger than\n\"" + imp.getTitle() + "\"."); return; } if (createImageRoi && x == 0 && y == 0) { x = imp.getWidth() / 2 - overlay.getWidth() / 2; y = imp.getHeight() / 2 - overlay.getHeight() / 2; } roi = new ImageRoi(x, y, overlay.getProcessor()); roi.setName(overlay.getShortTitle()); if (opacity != 1.0) ((ImageRoi) roi).setOpacity(opacity); if (createImageRoi) imp.setRoi(roi); else { Overlay overlayList = imp.getOverlay(); if (overlayList == null) overlayList = new Overlay(); overlayList.add(roi); imp.setOverlay(overlayList); overlay2 = overlayList; Undo.setup(Undo.OVERLAY_ADDITION, imp); } }
@Override public void keyPressed(final KeyEvent e) { if (modifier(e)) return; // operations that change the focus are put first.. if (PREVTAB.is(e)) { transferFocusBackward(); return; } if (NEXTTAB.is(e)) { transferFocus(); return; } if (FIND.is(e)) { if (find != null) find.requestFocusInWindow(); return; } // re-animate cursor cursor(true); // operations without cursor movement... final int fh = rend.fontH(); if (SCROLLDOWN.is(e)) { scroll.pos(scroll.pos() + fh); return; } if (SCROLLUP.is(e)) { scroll.pos(scroll.pos() - fh); return; } if (COPY1.is(e) || COPY2.is(e)) { copy(); return; } // set cursor position and reset last column text.pos(text.cursor()); if (!PREVLINE.is(e) && !NEXTLINE.is(e)) lastCol = -1; if (FINDNEXT.is(e) || FINDNEXT2.is(e)) { scroll(rend.find(true, true)); return; } if (FINDPREV.is(e) || FINDPREV2.is(e)) { scroll(rend.find(false, true)); return; } if (SELECTALL.is(e)) { selectAll(); return; } // necessary on Macs as the shift button is pressed for REDO final boolean marking = e.isShiftDown() && !DELNEXT.is(e) && !DELPREV.is(e) && !PASTE2.is(e) && !COMMENT.is(e) && !DELLINE.is(e) && !REDOSTEP.is(e); final boolean nomark = !text.marked(); if (marking && nomark) text.startMark(); boolean down = true; boolean consumed = true; // operations that consider the last text mark.. final byte[] txt = text.text(); if (NEXTWORD.is(e)) { text.nextToken(marking); } else if (PREVWORD.is(e)) { text.prevToken(marking); down = false; } else if (TEXTSTART.is(e)) { if (!marking) text.noMark(); text.pos(0); down = false; } else if (TEXTEND.is(e)) { if (!marking) text.noMark(); text.pos(text.size()); } else if (LINESTART.is(e)) { text.bol(marking); down = false; } else if (LINEEND.is(e)) { text.eol(marking); } else if (NEXTPAGE.is(e)) { down(getHeight() / fh, marking); } else if (PREVPAGE.is(e)) { up(getHeight() / fh, marking); down = false; } else if (NEXT.is(e)) { text.next(marking); } else if (PREV.is(e)) { text.prev(marking); down = false; } else if (PREVLINE.is(e)) { up(1, marking); down = false; } else if (NEXTLINE.is(e)) { down(1, marking); } else if (FINDERROR.is(e)) { final int p = text.error(); if (p != -1) setCaret(p); } else { consumed = false; } if (marking) { // refresh scroll position text.endMark(); text.checkMark(); } else if (undo != null) { // edit operations... if (CUT1.is(e) || CUT2.is(e)) { cut(); } else if (PASTE1.is(e) || PASTE2.is(e)) { paste(); } else if (UNDOSTEP.is(e)) { undo(); } else if (REDOSTEP.is(e)) { redo(); } else if (COMMENT.is(e)) { text.comment(rend.getSyntax()); } else if (DELLINE.is(e)) { text.deleteLine(); } else if (DELLINEEND.is(e) || DELNEXTWORD.is(e) || DELNEXT.is(e)) { if (nomark) { if (text.pos() == text.size()) return; text.startMark(); if (DELNEXTWORD.is(e)) { text.nextToken(true); } else if (DELLINEEND.is(e)) { text.eol(true); } else { text.next(true); } text.endMark(); } undo.cursor(text.cursor()); text.delete(); } else if (DELLINESTART.is(e) || DELPREVWORD.is(e) || DELPREV.is(e)) { if (nomark) { if (text.pos() == 0) return; text.startMark(); if (DELPREVWORD.is(e)) { text.prevToken(true); } else if (DELLINESTART.is(e)) { text.bol(true); } else { text.prev(); } text.endMark(); } undo.cursor(text.cursor()); text.delete(); down = false; } else { consumed = false; } } if (consumed) e.consume(); text.setCaret(); if (txt != text.text()) rend.calc(); showCursor(down ? 2 : 0); }
@Override public void keyReleased(final KeyEvent e) { if (undo != null) undo.store(text.text(), text.cursor()); }
public void mousePressed(MouseEvent e) { Undo.reset(); if (!Prefs.noClickToGC) System.gc(); IJ.showStatus(version() + IJ.freeMemory()); if (IJ.debugMode) IJ.log("Windows: " + WindowManager.getWindowCount()); }
/* if selection is closed shape, create a circle with the same area and centroid, otherwise use<br> the Pratt method to fit a circle to the points that define the line or multi-point selection.<br> Reference: Pratt V., Direct least-squares fitting of algebraic surfaces", Computer Graphics, Vol. 21, pages 145-152 (1987).<br> Original code: Nikolai Chernov's MATLAB script for Newton-based Pratt fit.<br> (http://www.math.uab.edu/~chernov/cl/MATLABcircle.html)<br> Java version: https://github.com/mdoube/BoneJ/blob/master/src/org/doube/geometry/FitCircle.java<br> Authors: Nikolai Chernov, Michael Doube, Ved Sharma */ void fitCircle(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null) { noRoi("Fit Circle"); return; } if (roi.isArea()) { // create circle with the same area and centroid Undo.setup(Undo.ROI, imp); ImageProcessor ip = imp.getProcessor(); ip.setRoi(roi); ImageStatistics stats = ImageStatistics.getStatistics(ip, Measurements.AREA + Measurements.CENTROID, null); double r = Math.sqrt(stats.pixelCount / Math.PI); imp.deleteRoi(); int d = (int) Math.round(2.0 * r); Roi roi2 = new OvalRoi( (int) Math.round(stats.xCentroid - r), (int) Math.round(stats.yCentroid - r), d, d); transferProperties(roi, roi2); imp.setRoi(roi2); return; } Polygon poly = roi.getPolygon(); int n = poly.npoints; int[] x = poly.xpoints; int[] y = poly.ypoints; if (n < 3) { IJ.error("Fit Circle", "At least 3 points are required to fit a circle."); return; } // calculate point centroid double sumx = 0, sumy = 0; for (int i = 0; i < n; i++) { sumx = sumx + poly.xpoints[i]; sumy = sumy + poly.ypoints[i]; } double meanx = sumx / n; double meany = sumy / n; // calculate moments double[] X = new double[n], Y = new double[n]; double Mxx = 0, Myy = 0, Mxy = 0, Mxz = 0, Myz = 0, Mzz = 0; for (int i = 0; i < n; i++) { X[i] = x[i] - meanx; Y[i] = y[i] - meany; double Zi = X[i] * X[i] + Y[i] * Y[i]; Mxy = Mxy + X[i] * Y[i]; Mxx = Mxx + X[i] * X[i]; Myy = Myy + Y[i] * Y[i]; Mxz = Mxz + X[i] * Zi; Myz = Myz + Y[i] * Zi; Mzz = Mzz + Zi * Zi; } Mxx = Mxx / n; Myy = Myy / n; Mxy = Mxy / n; Mxz = Mxz / n; Myz = Myz / n; Mzz = Mzz / n; // calculate the coefficients of the characteristic polynomial double Mz = Mxx + Myy; double Cov_xy = Mxx * Myy - Mxy * Mxy; double Mxz2 = Mxz * Mxz; double Myz2 = Myz * Myz; double A2 = 4 * Cov_xy - 3 * Mz * Mz - Mzz; double A1 = Mzz * Mz + 4 * Cov_xy * Mz - Mxz2 - Myz2 - Mz * Mz * Mz; double A0 = Mxz2 * Myy + Myz2 * Mxx - Mzz * Cov_xy - 2 * Mxz * Myz * Mxy + Mz * Mz * Cov_xy; double A22 = A2 + A2; double epsilon = 1e-12; double ynew = 1e+20; int IterMax = 20; double xnew = 0; int iterations = 0; // Newton's method starting at x=0 for (int iter = 1; iter <= IterMax; iter++) { iterations = iter; double yold = ynew; ynew = A0 + xnew * (A1 + xnew * (A2 + 4. * xnew * xnew)); if (Math.abs(ynew) > Math.abs(yold)) { if (IJ.debugMode) IJ.log("Fit Circle: wrong direction: |ynew| > |yold|"); xnew = 0; break; } double Dy = A1 + xnew * (A22 + 16 * xnew * xnew); double xold = xnew; xnew = xold - ynew / Dy; if (Math.abs((xnew - xold) / xnew) < epsilon) break; if (iter >= IterMax) { if (IJ.debugMode) IJ.log("Fit Circle: will not converge"); xnew = 0; } if (xnew < 0) { if (IJ.debugMode) IJ.log("Fit Circle: negative root: x = " + xnew); xnew = 0; } } if (IJ.debugMode) IJ.log("Fit Circle: n=" + n + ", xnew=" + IJ.d2s(xnew, 2) + ", iterations=" + iterations); // calculate the circle parameters double DET = xnew * xnew - xnew * Mz + Cov_xy; double CenterX = (Mxz * (Myy - xnew) - Myz * Mxy) / (2 * DET); double CenterY = (Myz * (Mxx - xnew) - Mxz * Mxy) / (2 * DET); double radius = Math.sqrt(CenterX * CenterX + CenterY * CenterY + Mz + 2 * xnew); if (Double.isNaN(radius)) { IJ.error("Fit Circle", "Points are collinear."); return; } CenterX = CenterX + meanx; CenterY = CenterY + meany; Undo.setup(Undo.ROI, imp); imp.deleteRoi(); IJ.makeOval( (int) Math.round(CenterX - radius), (int) Math.round(CenterY - radius), (int) Math.round(2 * radius), (int) Math.round(2 * radius)); }
private void makeBand(ImagePlus imp) { Roi roi = imp.getRoi(); if (roi == null) { noRoi("Make Band"); return; } Roi roiOrig = roi; if (!roi.isArea()) { IJ.error("Make Band", "Area selection required"); return; } Calibration cal = imp.getCalibration(); double pixels = bandSize; double size = pixels * cal.pixelWidth; int decimalPlaces = 0; if ((int) size != size) decimalPlaces = 2; GenericDialog gd = new GenericDialog("Make Band"); gd.addNumericField("Band Size:", size, decimalPlaces, 4, cal.getUnits()); gd.showDialog(); if (gd.wasCanceled()) return; size = gd.getNextNumber(); if (Double.isNaN(size)) { IJ.error("Make Band", "invalid number"); return; } int n = (int) Math.round(size / cal.pixelWidth); if (n > 255) { IJ.error("Make Band", "Cannot make bands wider that 255 pixels"); return; } int width = imp.getWidth(); int height = imp.getHeight(); Rectangle r = roi.getBounds(); ImageProcessor ip = roi.getMask(); if (ip == null) { ip = new ByteProcessor(r.width, r.height); ip.invert(); } ImageProcessor mask = new ByteProcessor(width, height); mask.insert(ip, r.x, r.y); ImagePlus edm = new ImagePlus("mask", mask); boolean saveBlackBackground = Prefs.blackBackground; Prefs.blackBackground = false; int saveType = EDM.getOutputType(); EDM.setOutputType(EDM.BYTE_OVERWRITE); IJ.run(edm, "Distance Map", ""); EDM.setOutputType(saveType); Prefs.blackBackground = saveBlackBackground; ip = edm.getProcessor(); ip.setThreshold(0, n, ImageProcessor.NO_LUT_UPDATE); int xx = -1, yy = -1; for (int x = r.x; x < r.x + r.width; x++) { for (int y = r.y; y < r.y + r.height; y++) { if (ip.getPixel(x, y) < n) { xx = x; yy = y; break; } } if (xx >= 0 || yy >= 0) break; } int count = IJ.doWand(edm, xx, yy, 0, null); if (count <= 0) { IJ.error("Make Band", "Unable to make band"); return; } ShapeRoi roi2 = new ShapeRoi(edm.getRoi()); if (!(roi instanceof ShapeRoi)) roi = new ShapeRoi(roi); ShapeRoi roi1 = (ShapeRoi) roi; roi2 = roi2.not(roi1); Undo.setup(Undo.ROI, imp); transferProperties(roiOrig, roi2); imp.setRoi(roi2); bandSize = n; }