Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ScaleBar.java
1 package ij.plugin;
2 import ij.*;
3 import ij.process.*;
4 import ij.gui.*;
5 import java.awt.*;
6 import ij.measure.*;
7 import java.awt.event.*;
8 
10 public class ScaleBar implements PlugIn {
11 
12  static final String[] locations = {"Upper Right", "Lower Right", "Lower Left", "Upper Left", "At Selection"};
13  static final int UPPER_RIGHT=0, LOWER_RIGHT=1, LOWER_LEFT=2, UPPER_LEFT=3, AT_SELECTION=4;
14  static final String[] colors = {"White","Black","Light Gray","Gray","Dark Gray","Red","Green","Blue","Yellow"};
15  static final String[] checkboxLabels = {"Bold Text", "Hide Text"};
16  static double barWidth;
17  static int defaultBarHeight = 3;
18  static int barHeightInPixels = defaultBarHeight;
19  static String location = locations[UPPER_RIGHT];
20  static String color = colors[0];
21  static boolean boldText = true;
22  static boolean hideText;
23  static int fontSize;
24  static boolean labelAll;
25  ImagePlus imp;
26  double imageWidth;
27  double mag;
28  int xloc, yloc;
29  int barWidthInPixels;
30  int roiX=-1, roiY, roiWidth, roiHeight;
31  boolean[] checkboxStates = new boolean[2];
32 
33 
34  public void run(String arg) {
35  imp = IJ.getInstance().getImagePlus();
36  if (imp==null) {
37  IJ.noImage();
38  }
39  }
40 
41 
42  boolean showDialog(ImagePlus imp) {
43  Roi roi = imp.getRoi();
44  if (roi!=null) {
45  Rectangle r = roi.getBounds();
46  roiX = r.x;
47  roiY = r.y;
48  roiWidth = r.width;
49  roiHeight = r.height;
50  location = locations[AT_SELECTION];
51  } else if (location.equals(locations[AT_SELECTION]))
52  location = locations[UPPER_RIGHT];
53 
54  Calibration cal = imp.getCalibration();
55  mag = (imp!=null)?imp.getCanvas().getMagnification():1.0;
56  if (mag>1.0)
57  mag = 1.0;
58  if (fontSize<(12/mag))
59  fontSize = (int)(12/mag);
60  String units = cal.getUnits();
61  double pixelWidth = cal.pixelWidth;
62  if (pixelWidth==0.0)
63  pixelWidth = 1.0;
64  double scale = 1.0/pixelWidth;
65  imageWidth = imp.getWidth()*pixelWidth;
66  if (roiX>0 && roiWidth>10)
67  barWidth = roiWidth*pixelWidth;
68  else if (barWidth==0.0 || barWidth>0.67*imageWidth) {
69  barWidth = (80.0*pixelWidth)/mag;
70  if (barWidth>0.67*imageWidth)
71  barWidth = 0.67*imageWidth;
72  if (barWidth>5.0)
73  barWidth = (int)barWidth;
74  }
75  int digits = (int)barWidth==barWidth?0:1;
76  if (barWidth<1.0)
77  digits = 2;
78  int percent = (int)(barWidth*100.0/imageWidth);
79  if (mag<1.0 && barHeightInPixels<defaultBarHeight/mag)
80  barHeightInPixels = (int)(defaultBarHeight/mag);
81  imp.getProcessor().snapshot();
82  updateScalebar();
83  GenericDialog gd = new BarDialog("Scale Bar");
84  gd.addNumericField("Width in "+units+": ", barWidth, digits);
85  gd.addNumericField("Height in pixels: ", barHeightInPixels, 0);
86  gd.addNumericField("Font Size: ", fontSize, 0);
87  gd.addChoice("Color: ", colors, color);
88  gd.addChoice("Location: ", locations, location);
89  checkboxStates[0] = boldText; checkboxStates[1] = hideText;
90  gd.addCheckboxGroup(1, 2, checkboxLabels, checkboxStates);
91  gd.showDialog();
92  if (gd.wasCanceled()) {
93  imp.getProcessor().reset();
94  imp.updateAndDraw();
95  return false;
96  }
97  barWidth = gd.getNextNumber();
98  barHeightInPixels = (int)gd.getNextNumber();
99  fontSize = (int)gd.getNextNumber();
100  color = gd.getNextChoice();
101  location = gd.getNextChoice();
102  boldText = gd.getNextBoolean();
103  hideText = gd.getNextBoolean();
104  return true;
105  }
106 
107  void drawScaleBar(ImagePlus imp) {
108  if (!updateLocation())
109  return;
110  ImageProcessor ip = imp.getProcessor();
111  Undo.setup(Undo.FILTER, imp);
112  Color color = getColor();
113  //if (!(color==Color.white || color==Color.black)) {
114  // ip = ip.convertToRGB();
115  // imp.setProcessor(null, ip);
116  //}
117  ip.setColor(color);
118  int x = xloc;
119  int y = yloc;
120  ip.setRoi(x, y, barWidthInPixels, barHeightInPixels);
121  ip.fill();
122  ip.resetRoi();
123 
124  int fontType = boldText?Font.BOLD:Font.PLAIN;
125  ip.setFont(new Font("SansSerif", fontType, fontSize));
126  ip.setAntialiasedText(true);
127  int digits = (int)barWidth==barWidth?0:1;
128  if (barWidth<1.0)
129  digits = 2;
130  String label = IJ.d2s(barWidth, digits) + " "+ imp.getCalibration().getUnits();
131  x = x +(barWidthInPixels - ip.getStringWidth(label))/2;
132  y = y + barHeightInPixels + fontSize + fontSize/4;
133  if (!hideText)
134  ip.drawString(label, x, y);
135  imp.updateAndDraw();
136  }
137 
138  boolean updateLocation() {
139  Calibration cal = imp.getCalibration();
140  barWidthInPixels = (int)(barWidth/cal.pixelWidth);
141  int width = imp.getWidth();
142  int height = imp.getHeight();
143  int fraction = 20;
144  int x = width - width/fraction - barWidthInPixels;
145  int y = 0;
146  if (location.equals(locations[UPPER_RIGHT]))
147  y = height/fraction;
148  else if (location.equals(locations[LOWER_RIGHT]))
149  y = height - height/fraction - barHeightInPixels - fontSize;
150  else if (location.equals(locations[UPPER_LEFT])) {
151  x = width/fraction;
152  y = height/fraction;
153  } else if (location.equals(locations[LOWER_LEFT])) {
154  x = width/fraction;
155  y = height - height/fraction - barHeightInPixels - fontSize;
156  } else {
157  if (roiX==-1)
158  return false;
159  x = roiX;
160  y = roiY;
161  }
162  xloc = x;
163  yloc = y;
164  return true;
165  }
166 
167  Color getColor() {
168  Color c = Color.white;
169  if (color.equals(colors[1])) c = Color.black;
170  else if (color.equals(colors[2])) c = Color.lightGray;
171  else if (color.equals(colors[3])) c = Color.gray;
172  else if (color.equals(colors[4])) c = Color.darkGray;
173  else if (color.equals(colors[5])) c = Color.red;
174  else if (color.equals(colors[6])) c = Color.green;
175  else if (color.equals(colors[7])) c = Color.blue;
176  else if (color.equals(colors[8])) c = Color.yellow;
177  return c;
178  }
179 
180  void updateScalebar() {
181  updateLocation();
182  imp.getProcessor().reset();
183  drawScaleBar(imp);
184  }
185 
186  class BarDialog extends GenericDialog {
187 
188  BarDialog(String title) {
189  super(title);
190  }
191 
192  public void textValueChanged(TextEvent e) {
193  TextField widthField = ((TextField)numberField.elementAt(0));
194  Double d = getValue(widthField.getText());
195  if (d==null)
196  return;
197  barWidth = d.doubleValue();
198  TextField heightField = ((TextField)numberField.elementAt(1));
199  d = getValue(heightField.getText());
200  if (d==null)
201  return;
202  barHeightInPixels = (int)d.doubleValue();
203  TextField fontSizeField = ((TextField)numberField.elementAt(2));
204  d = getValue(fontSizeField.getText());
205  if (d==null)
206  return;
207  int size = (int)d.doubleValue();
208  if (size>5)
209  fontSize = size;
210  updateScalebar();
211  }
212 
213  public void itemStateChanged(ItemEvent e) {
214  Choice col = (Choice)(choice.elementAt(0));
215  color = col.getSelectedItem();
216  Choice loc = (Choice)(choice.elementAt(1));
217  location = loc.getSelectedItem();
218  boldText = ((Checkbox)(checkbox.elementAt(0))).getState();
219  hideText = ((Checkbox)(checkbox.elementAt(1))).getState();
220  updateScalebar();
221  }
222 
223  } //BarDialog inner class
224 
225 } //ScaleBar class
226 
227