Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Filler.java
1 package ij.plugin.filter;
2 import ij.*;
3 import ij.gui.*;
4 import ij.process.*;
5 import ij.measure.*;
6 import java.awt.*;
7 
9 public class Filler implements PlugInFilter, Measurements {
10 
11  String arg;
12  Roi roi;
13  ImagePlus imp;
14  int sliceCount;
15  ImageProcessor mask;
16  boolean isTextRoi;
17 
18  public int setup(String arg, ImagePlus imp) {
19  this.arg = arg;
20  this.imp = imp;
21  if (imp!=null)
22  roi = imp.getRoi();
23  isTextRoi = roi!=null && (roi instanceof TextRoi);
24  IJ.register(Filler.class);
25  int baseCapabilities = DOES_ALL+ROI_REQUIRED;
26  if (arg.equals("clear")) {
27  if (isTextRoi || isLineSelection())
28  return baseCapabilities;
29  else
30  return IJ.setupDialog(imp,baseCapabilities+SUPPORTS_MASKING);
31  } else if (arg.equals("draw"))
32  return baseCapabilities;
33  else if (arg.equals("label")) {
34  if (Analyzer.firstParticle<Analyzer.lastParticle)
35  return baseCapabilities-ROI_REQUIRED;
36  else
37  return baseCapabilities;
38  } else if (arg.equals("outside"))
39  return IJ.setupDialog(imp,baseCapabilities);
40  else
41  return IJ.setupDialog(imp,baseCapabilities+SUPPORTS_MASKING);
42  }
43 
44  public void run(ImageProcessor ip) {
45  if (arg.equals("clear"))
46  clear(ip);
47  else if (isTextRoi && (arg.equals("draw") || arg.equals("fill")))
48  draw(ip);
49  else if (arg.equals("fill"))
50  fill(ip);
51  else if (arg.equals("draw"))
52  draw(ip);
53  else if (arg.equals("label"))
54  label(ip);
55  else if (arg.equals("outside"))
56  clearOutside(ip);
57  }
58 
59  boolean isLineSelection() {
60  return roi!=null && roi.getType()>=Roi.LINE && roi.getType()<=Roi.FREELINE;
61  }
62 
63  public void clear(ImageProcessor ip) {
64  ip.setColor(Toolbar.getBackgroundColor());
65  if (isLineSelection())
66  roi.drawPixels();
67  else
68  ip.fill(); // fill with background color
69  ip.setColor(Toolbar.getForegroundColor());
70  }
71 
72  public void fill(ImageProcessor ip) {
73  ip.setColor(Toolbar.getForegroundColor());
74  if (isLineSelection())
75  roi.drawPixels();
76  else
77  ip.fill(); // fill with foreground color
78  }
79 
80  public void draw(ImageProcessor ip) {
81  ip.setColor(Toolbar.getForegroundColor());
82  roi.drawPixels();
83  if (IJ.altKeyDown())
84  drawLabel(ip);
85  }
86 
87  public void label(ImageProcessor ip) {
88  if (Analyzer.getCounter()==0) {
89  IJ.showMessage("Label", "Measurement counter is zero");
90  return;
91  }
92  if (Analyzer.firstParticle<Analyzer.lastParticle)
93  drawParticleLabels(ip);
94  else {
95  ip.setColor(Toolbar.getForegroundColor());
96  roi.drawPixels();
97  drawLabel(ip);
98  }
99  }
100 
101  void drawParticleLabels(ImageProcessor ip) {
102  ResultsTable rt = ResultsTable.getResultsTable();
103  int count = rt.getCounter();
104  int first = Analyzer.firstParticle;
105  int last = Analyzer.lastParticle;
106  if (count==0 || first>=count || last>=count)
107  return;
108  if (!rt.columnExists(ResultsTable.X_CENTROID)) {
109  IJ.showMessage("Label", "\"Centroids\" required to label particles");
110  return;
111  }
112  for (int i=first; i<=last; i++) {
113  int x = (int)rt.getValue(ResultsTable.X_CENTROID, i);
114  int y = (int)rt.getValue(ResultsTable.Y_CENTROID, i);
115  drawLabel(ip, i+1, new Rectangle(x,y,0,0));
116  }
117  }
118 
119  void drawLabel(ImageProcessor ip) {
120  int count = Analyzer.getCounter();
121  if (count>0 && roi!=null)
122  drawLabel(ip, count, roi.getBounds());
123  }
124 
125  void drawLabel(ImageProcessor ip, int count, Rectangle r) {
126  Color foreground = Toolbar.getForegroundColor();
127  Color background = Toolbar.getBackgroundColor();
128  if (foreground.equals(background)) {
129  foreground = Color.black;
130  background = Color.white;
131  }
132  int size = r.width>50&&r.height>50?12:9;
133  ip.setFont(new Font("SansSerif", Font.PLAIN, size));
134  String label = "" + count;
135  int w = ip.getStringWidth(label);
136  int x = r.x + r.width/2 - w/2;
137  int y = r.y + r.height/2 + 6;
138  FontMetrics metrics = ip.getFontMetrics();
139  int h = metrics.getHeight();
140  ip.setColor(background);
141  ip.setRoi(x-1, y-h+2, w+1, h-3);
142  ip.fill();
143  ip.resetRoi();
144  ip.setColor(foreground);
145  ip.drawString(label, x, y);
146  }
147 
148  public synchronized void clearOutside(ImageProcessor ip) {
149  if (isLineSelection()) {
150  IJ.error("\"Clear Outside\" does not work with line selections.");
151  return;
152  }
153  sliceCount++;
154  Rectangle r = ip.getRoi();
155  if (mask==null)
156  makeMask(ip, r);
157  ip.setColor(Toolbar.getBackgroundColor());
158  int stackSize = imp.getStackSize();
159  if (stackSize>1)
160  ip.snapshot();
161  ip.fill();
162  ip.reset(mask);
163  int width = ip.getWidth();
164  int height = ip.getHeight();
165  ip.setRoi(0, 0, r.x, height);
166  ip.fill();
167  ip.setRoi(r.x, 0, r.width, r.y);
168  ip.fill();
169  ip.setRoi(r.x, r.y+r.height, r.width, height-(r.y+r.height));
170  ip.fill();
171  ip.setRoi(r.x+r.width, 0, width-(r.x+r.width), height);
172  ip.fill();
173  ip.resetRoi();
174  if (sliceCount==stackSize) {
175  ip.setColor(Toolbar.getForegroundColor());
176  Roi roi = imp.getRoi();
177  imp.killRoi();
178  imp.updateAndDraw();
179  imp.setRoi(roi);
180  }
181  }
182 
183  public void makeMask(ImageProcessor ip, Rectangle r) {
184  mask = imp.getMask();
185  if (mask==null) {
186  mask = new ByteProcessor(r.width, r.height);
187  mask.invert();
188  } else {
189  // duplicate mask (needed because getMask caches masks)
190  mask = mask.duplicate();
191  }
192  mask.invert();
193  }
194 
195 }