1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.measure.Calibration;
import ij.plugin.filter.EDM;
import ij.plugin.filter.ThresholdToSelection;
import ij.plugin.frame.Recorder;
import java.awt.*;
import java.util.Vector;
/** This plugin, which enlarges or shrinks selections, implements the Edit/Selection/Enlarge command. */
public class RoiEnlarger implements PlugIn, DialogListener {
private static final String DISTANCE_KEY = "enlarger.distance";
private static final String USE_PIXELS_KEY = "enlarger.pixels";
private double defaultDistance = Prefs.get(DISTANCE_KEY, 15); // pixels
private boolean defaultUsePixels = Prefs.get(USE_PIXELS_KEY, false);
private Calibration cal;
private Label unitsLabel;
public void run(String arg) {
ImagePlus imp = IJ.getImage();
Roi roi = imp.getRoi();
if (roi==null || roi.isLine()) {
IJ.error("Enlarge", "This command requires an area selection");
return;
}
if (!imp.okToDeleteRoi())
return;
double n = showDialog(imp, defaultDistance);
if (Double.isNaN(n))
return;
Prefs.set(DISTANCE_KEY, defaultDistance);
Prefs.set(USE_PIXELS_KEY, defaultUsePixels);
Roi roi2 = Math.abs(n)<256?enlarge255(roi,n):enlarge(roi,n);
if (roi2!=null) {
imp.setRoi(roi2);
Roi.setPreviousRoi(roi);
defaultDistance = n;
}
int pixels = (int)Math.round(n);
Recorder.recordCall("RoiEnlarger.enlarge(imp, "+pixels+");");
}
public static void enlarge(ImagePlus imp, int pixels) {
Roi roi = imp.getRoi();
if (roi==null || roi.isLine() || (roi instanceof PointRoi))
return;
Roi roi2 = Math.abs(pixels)<256?enlarge255(roi,pixels):enlarge(roi,pixels);
if (roi2!=null)
imp.setRoi(roi2);
}
public double showDialog(ImagePlus imp, double pixels) {
cal = imp.getCalibration();
boolean scaled = cal.scaled();
double pixelWidth = cal.pixelWidth;
boolean xyScaleDifferent = scaled && cal.pixelWidth != cal.pixelHeight;
boolean usePixels = defaultUsePixels;
double n = pixels;
int decimalPlaces = 0;
if (scaled && !usePixels) {
n *= pixelWidth;
decimalPlaces = getDecimalPlaces(pixelWidth, n);
}
GenericDialog gd = new GenericDialog("Enlarge Selection");
gd.addNumericField("Enlarge by", n, decimalPlaces);
String units = scaled && !usePixels ? cal.getUnits()+" " : "pixels ";
gd.addToSameRow();
gd.addMessage(units.replace('\n', ' ')); //just in case of a newline character, which would make it a MultiLineLabel
unitsLabel = (Label)gd.getMessage();
if (scaled) {
gd.setInsets(0, 20, 0); //top left bottom
gd.addCheckbox("Pixel units", usePixels);
}
gd.setInsets(10, 0, 0);
gd.addMessage("Enter negative number to shrink", null, Color.darkGray);
if (xyScaleDifferent) {
gd.setInsets(5, 0, 0);
gd.addMessage(" \n ", null, Color.RED);
}
gd.addDialogListener(this);
if (xyScaleDifferent && Macro.getOptions()==null)
updateWarning(gd); //in interactive mode only
gd.showDialog();
if (gd.wasCanceled())
return Double.NaN;
n = gd.getNextNumber();
if (scaled)
usePixels = gd.getNextBoolean();
pixels = usePixels ? n : n/pixelWidth;
defaultDistance = pixels;
defaultUsePixels = usePixels;
return pixels;
}
public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
Vector checkboxes = gd.getCheckboxes();
Checkbox usePixelsCbx = checkboxes == null ? null : (Checkbox)checkboxes.get(0);
double n = gd.getNextNumber();
boolean usePixels = cal.scaled() ? gd.getNextBoolean() : true; //getNextBoolean also needed for macro recorded
if (e!=null && e.getSource() == usePixelsCbx) {
double pixelWidth = cal.pixelWidth;
int decimalPlaces = 0;
if (usePixels) {
n /= pixelWidth; //scaled to pixels
} else {
n *= pixelWidth; //pixels to scaled
decimalPlaces = getDecimalPlaces(pixelWidth, n);
}
TextField numberField = (TextField)gd.getNumericFields().get(0);
numberField.setText(IJ.d2s(n, decimalPlaces));
if (unitsLabel != null) unitsLabel.setText(usePixels ? "pixels" : cal.getUnits());
boolean xyScaleDifferent = cal.scaled() && cal.pixelWidth != cal.pixelHeight;
if (xyScaleDifferent && usePixelsCbx != null) updateWarning(gd);
}
return !gd.invalidNumber();
}
private void updateWarning(GenericDialog gd) {
Checkbox usePixelsCbx = (Checkbox)gd.getCheckboxes().get(0);
MultiLineLabel warningLabel = (MultiLineLabel)gd.getMessage();
boolean showWarning = !usePixelsCbx.getState(); //warn if not pixels units
warningLabel.setText(showWarning ? "WARNING: x & y scales differ\nConversion to pixels uses x scale" : " \n ");
}
//decimal places for displaying the scaled enlarge/shrink value
private static int getDecimalPlaces(double pixelWidth, double number) {
if (number == (int)number || pixelWidth == 1) return 0;
int decimalPlaces = (int)(-Math.log10(pixelWidth)+1.9);
if (decimalPlaces < 0) decimalPlaces = 0;
if (decimalPlaces >9) decimalPlaces = 9;
return decimalPlaces;
}
public static Roi enlarge(Roi roi, double pixels) {
if (pixels==0)
return roi;
int type = roi.getType();
int n = (int)Math.round(pixels);
if (type==Roi.RECTANGLE || type==Roi.OVAL)
return enlargeRectOrOval(roi, n);
if (n<0)
return shrink(roi, -n);
Rectangle bounds = roi.getBounds();
int width = bounds.width;
int height = bounds.height;
width += 2*n + 2;
height += 2*n + 2;
ImageProcessor ip = new ByteProcessor(width, height);
ip.invert();
roi.setLocation(n+1, n+1);
ip.setColor(0);
ip.fill(roi);
ip.setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE);
Roi roi2 = (new ThresholdToSelection()).convert(ip);
Rectangle bounds2 = roi2.getBounds();
int xoffset = bounds2.x - (n+1);
int yoffset = bounds2.y - (n+1);
roi.setLocation(bounds.x, bounds.y);
FloatProcessor edm = new EDM().makeFloatEDM (ip, 0, false);
edm.setThreshold(0, n, ImageProcessor.NO_LUT_UPDATE);
roi2 = (new ThresholdToSelection()).convert(edm);
if (roi2==null)
return roi;
roi2.copyAttributes(roi);
roi2.setLocation(bounds.x-n+xoffset, bounds.y-n+yoffset);
if (roi.getStroke()!=null)
roi2.setStroke(roi.getStroke());
return roi2;
}
private static Roi enlargeRectOrOval(Roi roi, int n) {
Rectangle bounds = roi.getBounds();
bounds.x -= n;
bounds.y -= n;
bounds.width += 2*n;
bounds.height += 2*n;
if (bounds.width<=0 || bounds.height<=0)
return roi;
Roi roi2 = null;
if (roi.getType()==Roi.RECTANGLE)
roi2 = new Roi(bounds.x, bounds.y, bounds.width, bounds.height);
else
roi2 = new OvalRoi(bounds.x, bounds.y, bounds.width, bounds.height);
roi2.copyAttributes(roi);
return roi2;
}
private static Roi shrink(Roi roi, int n) {
Rectangle bounds = roi.getBounds();
int width = bounds.width + 2;
int height = bounds.height + 2;
ImageProcessor ip = new ByteProcessor(width, height);
roi.setLocation(1, 1);
ip.setColor(255);
ip.fill(roi);
roi.setLocation(bounds.x, bounds.y);
FloatProcessor edm = new EDM().makeFloatEDM (ip, 0, false);
edm.setThreshold(n+1, Float.MAX_VALUE, ImageProcessor.NO_LUT_UPDATE);
Roi roi2 = (new ThresholdToSelection()).convert(edm);
if (roi2==null)
return roi;
Rectangle bounds2 = roi2.getBounds();
if (bounds2.width<=0 && bounds2.height<=0)
return roi;
roi2.copyAttributes(roi);
roi2.setLocation(bounds.x+bounds2.x-1, bounds.y+bounds2.y-1);
return roi2;
}
public static Roi enlarge255(Roi roi, double pixels) {
if (pixels==0)
return roi;
int type = roi.getType();
int n = (int)Math.round(pixels);
if (type==Roi.RECTANGLE || type==Roi.OVAL)
return enlargeRectOrOval(roi, n);
if (n<0)
return shrink255(roi, -n);
Rectangle bounds = roi.getBounds();
int width = bounds.width;
int height = bounds.height;
width += 2*n + 2;
height += 2*n + 2;
ImageProcessor ip = new ByteProcessor(width, height);
ip.invert();
roi.setLocation(n+1, n+1);
ip.setColor(0);
ip.fill(roi);
ip.setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE);
Roi roi2 = (new ThresholdToSelection()).convert(ip);
Rectangle bounds2 = roi2.getBounds();
int xoffset = bounds2.x - (n+1);
int yoffset = bounds2.y - (n+1);
roi.setLocation(bounds.x, bounds.y);
boolean bb = Prefs.blackBackground;
Prefs.blackBackground = true;
new EDM().toEDM(ip);
Prefs.blackBackground = bb;
ip.setThreshold(0, n, ImageProcessor.NO_LUT_UPDATE);
roi2 = (new ThresholdToSelection()).convert(ip);
if (roi2==null)
return roi;
roi2.copyAttributes(roi);
roi2.setLocation(bounds.x-n+xoffset, bounds.y-n+yoffset);
if (roi.getStroke()!=null)
roi2.setStroke(roi.getStroke());
return roi2;
}
private static Roi shrink255(Roi roi, int n) {
Rectangle bounds = roi.getBounds();
int width = bounds.width + 2;
int height = bounds.height + 2;
ImageProcessor ip = new ByteProcessor(width, height);
roi.setLocation(1, 1);
ip.setColor(255);
ip.fill(roi);
roi.setLocation(bounds.x, bounds.y);
boolean bb = Prefs.blackBackground;
Prefs.blackBackground = true;
new EDM().toEDM(ip);
Prefs.blackBackground = bb;
ip.setThreshold(n+1, 255, ImageProcessor.NO_LUT_UPDATE);
Roi roi2 = (new ThresholdToSelection()).convert(ip);
if (roi2==null)
return roi;
Rectangle bounds2 = roi2.getBounds();
if (bounds2.width<=0 && bounds2.height<=0)
return roi;
roi2.copyAttributes(roi);
roi2.setLocation(bounds.x+bounds2.x-1, bounds.y+bounds2.y-1);
return roi2;
}
}
|