File: ScaleBar.java

package info (click to toggle)
imagej 1.46a-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: java: 89,778; sh: 311; xml: 51; makefile: 6
file content (383 lines) | stat: -rw-r--r-- 11,780 bytes parent folder | download | duplicates (2)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.measure.*;
import java.awt.event.*;

/** This plugin implements the Analyze/Tools/Draw Scale Bar command.
	Divakar Ramachandran added options to draw a background 
	and use a serif font on 23 April 2006.
*/
public class ScaleBar implements PlugIn {

	static final String[] locations = {"Upper Right", "Lower Right", "Lower Left", "Upper Left", "At Selection"};
	static final int UPPER_RIGHT=0, LOWER_RIGHT=1, LOWER_LEFT=2, UPPER_LEFT=3, AT_SELECTION=4;
	static final String[] colors = {"White","Black","Light Gray","Gray","Dark Gray","Red","Green","Blue","Yellow"};
	static final String[] bcolors = {"None","Black","White","Dark Gray","Gray","Light Gray","Yellow","Blue","Green","Red"};
	static final String[] checkboxLabels = {"Bold Text", "Hide Text", "Serif Font", "Overlay"};
	static double barWidth;
	static int defaultBarHeight = 4;
	static int barHeightInPixels = defaultBarHeight;
	static String location = locations[LOWER_RIGHT];
	static String color = colors[0];
	static String bcolor = bcolors[0];
	static boolean boldText = true;
	static boolean hideText;
	static boolean createOverlay;
	static int defaultFontSize = 14;
	static int fontSize;
	static boolean labelAll;
	ImagePlus imp;
	double imageWidth;
	double mag;
	int xloc, yloc;
	int barWidthInPixels;
	int roiX=-1, roiY, roiWidth, roiHeight;
	boolean serifFont;
	boolean[] checkboxStates = new boolean[4];
	boolean showingOverlay, drawnScaleBar;
	Overlay baseOverlay;


	public void run(String arg) {
		imp = WindowManager.getCurrentImage();
		if (imp!=null) {
			baseOverlay = imp.getOverlay();
			if (showDialog(imp) && imp.getStackSize()>1 && labelAll)
				labelSlices(imp);
		} else
			IJ.noImage();
	 }

	void labelSlices(ImagePlus imp) {
		if (createOverlay) return;
		ImageStack stack = imp.getStack();
		String units = getUnits(imp);
		for (int i=1; i<=stack.getSize(); i++)
			drawScaleBar(stack.getProcessor(i), units);
		imp.setStack(stack);
	}

	boolean showDialog(ImagePlus imp) {
		Roi roi = imp.getRoi();
		if (roi!=null) {
			Rectangle r = roi.getBounds();
			roiX = r.x;
			roiY = r.y; 
			roiWidth = r.width;
			roiHeight = r.height;
			location = locations[AT_SELECTION];
		} else if (location.equals(locations[AT_SELECTION]))
			location = locations[UPPER_RIGHT];

		Calibration cal = imp.getCalibration();
		ImageWindow win = imp.getWindow();
		mag = (win!=null)?win.getCanvas().getMagnification():1.0;
		if (mag>1.0)
			mag = 1.0;
		if (fontSize<(defaultFontSize/mag))
			fontSize = (int)(defaultFontSize/mag);
		String units = cal.getUnits();
		// Handle Digital Micrograph unit microns
		if (units.equals("micron"))
			units = IJ.micronSymbol+"m";
		double pixelWidth = cal.pixelWidth;
		if (pixelWidth==0.0)
			pixelWidth = 1.0;
		double scale = 1.0/pixelWidth;
		imageWidth = imp.getWidth()*pixelWidth;
		if (roiX>0 && roiWidth>10)
			barWidth = roiWidth*pixelWidth;
		else if (barWidth==0.0 || barWidth>0.67*imageWidth) {
			barWidth = (80.0*pixelWidth)/mag;
			if (barWidth>0.67*imageWidth)
				barWidth = 0.67*imageWidth;
			if (barWidth>5.0)
				barWidth = (int)barWidth;
		}
		int stackSize = imp.getStackSize();
		int digits = (int)barWidth==barWidth?0:1;
		if (barWidth<1.0)
			digits = 2;
		int percent = (int)(barWidth*100.0/imageWidth);
		if (mag<1.0 && barHeightInPixels<defaultBarHeight/mag)
			barHeightInPixels = (int)(defaultBarHeight/mag);
		imp.getProcessor().snapshot();
		if (IJ.macroRunning())
			boldText = hideText = serifFont = createOverlay = false;
		else
			updateScalebar();
		GenericDialog gd = new BarDialog("ScaleBar Plus");
		gd.addNumericField("Width in "+units+": ", barWidth, digits);
		gd.addNumericField("Height in pixels: ", barHeightInPixels, 0);
		gd.addNumericField("Font size: ", fontSize, 0);
		gd.addChoice("Color: ", colors, color);
		gd.addChoice("Background: ", bcolors, bcolor);
		gd.addChoice("Location: ", locations, location);
		checkboxStates[0] = boldText; checkboxStates[1] = hideText;
		checkboxStates[2] = serifFont; checkboxStates[3] = createOverlay;
		gd.setInsets(10, 25, 0);
		gd.addCheckboxGroup(2, 2, checkboxLabels, checkboxStates);
		if (stackSize>1) {
			gd.setInsets(0, 25, 0);
			gd.addCheckbox("Label all slices", labelAll);
		}
		gd.showDialog();
		if (gd.wasCanceled()) {
			imp.getProcessor().reset();
			imp.updateAndDraw();
			if (showingOverlay)
				imp.setOverlay(null);
			return false;
		}
		barWidth = gd.getNextNumber();
		barHeightInPixels = (int)gd.getNextNumber();
		fontSize = (int)gd.getNextNumber();
		color = gd.getNextChoice();
		bcolor = gd.getNextChoice();
		location = gd.getNextChoice();
		boldText = gd.getNextBoolean();
		hideText = gd.getNextBoolean();
		serifFont = gd.getNextBoolean();
		createOverlay = gd.getNextBoolean();
		if (stackSize>1)
			labelAll = gd.getNextBoolean();
		if (IJ.macroRunning()) updateScalebar();
		 return true;
	}

	void drawScaleBar(ImagePlus imp) {
		  if (!updateLocation())
			return;
		Undo.setup(Undo.FILTER, imp);
		drawScaleBar(imp.getProcessor(), getUnits(imp));
		imp.updateAndDraw();
	}
	
	String getUnits(ImagePlus imp) {
		String units = imp.getCalibration().getUnits();
		if (units.equals("microns"))
			units = IJ.micronSymbol+"m";
		return units;
	}

	void createOverlay(ImagePlus imp) {
		Overlay overlay = baseOverlay;
		if (overlay!=null)
			overlay = overlay.duplicate();
		else
			overlay = new Overlay();
		Color color = getColor();
		Color bcolor = getBColor();
		int x = xloc;
		int y = yloc;
		int fontType = boldText?Font.BOLD:Font.PLAIN;
		String face = serifFont?"Serif":"SanSerif";
		Font font = new Font(face, fontType, fontSize);
		String label = getLength(barWidth) + " "+ getUnits(imp);
		ImageProcessor ip = imp.getProcessor();
		ip.setFont(font);
		int swidth = hideText?0:ip.getStringWidth(label);
		int xoffset = (barWidthInPixels - swidth)/2;
		int yoffset =  barHeightInPixels + (hideText?0:fontSize+fontSize/4);
		if (bcolor!=null) {
			int w = barWidthInPixels;
			int h = yoffset;
			if (w<swidth) w = swidth;
			int x2 = x;
			if (x+xoffset<x2) x2 = x + xoffset;
			int margin = w/20;
			if (margin<2) margin = 2;
			x2 -= margin;
			int y2 = y - margin;
			w = w+ margin*2;
			h = h+ margin*2;
			Roi background = new Roi(x2, y2, w, h);
			background.setFillColor(bcolor);
			overlay.add(background);
		}
		Roi bar = new Roi(x, y, barWidthInPixels, barHeightInPixels);
		bar.setFillColor(color);
		overlay.add(bar);
		if (!hideText) {
			TextRoi text = new TextRoi(x+xoffset, y+barHeightInPixels, label, font);
			text.setStrokeColor(color);
			overlay.add(text);
		}
		imp.setOverlay(overlay);
		showingOverlay = true;
	}
	
	void drawScaleBar(ImageProcessor ip, String units) {
		Color color = getColor();
		Color bcolor = getBColor();
		int x = xloc;
		int y = yloc;
		int fontType = boldText?Font.BOLD:Font.PLAIN;
		String font = serifFont?"Serif":"SanSerif";
		ip.setFont(new Font(font, fontType, fontSize));
		ip.setAntialiasedText(true);
		String label = getLength(barWidth) + " "+ units;
		int swidth = hideText?0:ip.getStringWidth(label);
		int xoffset = (barWidthInPixels - swidth)/2;
		int yoffset =  barHeightInPixels + (hideText?0:fontSize+fontSize/(serifFont?8:4));

		// Draw bkgnd box first,  based on bar width and height (and font size if hideText is not checked)
		if (bcolor!=null) {
			int w = barWidthInPixels;
			int h = yoffset;
			if (w<swidth) w = swidth;
			int x2 = x;
			if (x+xoffset<x2) x2 = x + xoffset;
			int margin = w/20;
			if (margin<2) margin = 2;
			x2 -= margin;
			int y2 = y - margin;
			w = w+ margin*2;
			h = h+ margin*2;
			ip.setColor(bcolor);
			ip.setRoi(x2, y2, w, h);
			ip.fill();
		}
		
		ip.resetRoi();
		ip.setColor(color);
		ip.setRoi(x, y, barWidthInPixels, barHeightInPixels);
		ip.fill();
		ip.resetRoi();
		if (!hideText)
			ip.drawString(label, x+xoffset, y+yoffset);
		drawnScaleBar = true;
	}

	String getLength(double barWidth) {
		int digits = (int)barWidth==barWidth?0:1;
		if (barWidth<1.0) digits=1;
		if (digits==1) {
			String s = IJ.d2s(barWidth/0.1, 2);
			if (!s.endsWith(".00")) digits = 2;
		}
		return IJ.d2s(barWidth, digits);
	}

	boolean updateLocation() {
		Calibration cal = imp.getCalibration();
		barWidthInPixels = (int)(barWidth/cal.pixelWidth);
		int width = imp.getWidth();
		int height = imp.getHeight();
		int fraction = 20;
		int x = width - width/fraction - barWidthInPixels;
		int y = 0;
		if (location.equals(locations[UPPER_RIGHT]))
			 y = height/fraction;
		else if (location.equals(locations[LOWER_RIGHT]))
			y = height - height/fraction - barHeightInPixels - fontSize;
		else if (location.equals(locations[UPPER_LEFT])) {
			x = width/fraction;
			y = height/fraction;
		} else if (location.equals(locations[LOWER_LEFT])) {
			x = width/fraction;
			y = height - height/fraction - barHeightInPixels - fontSize;
		} else {
			if (roiX==-1)
				 return false;
			x = roiX;
			y = roiY;
		}
		xloc = x;
		yloc = y;
		return true;
	}

	Color getColor() {
		Color c = Color.black;
		if (color.equals(colors[0])) c = Color.white;
		else if (color.equals(colors[2])) c = Color.lightGray;
		else if (color.equals(colors[3])) c = Color.gray;
		else if (color.equals(colors[4])) c = Color.darkGray;
		else if (color.equals(colors[5])) c = Color.red;
		else if (color.equals(colors[6])) c = Color.green;
		else if (color.equals(colors[7])) c = Color.blue;
		else if (color.equals(colors[8])) c = Color.yellow;
	   return c;
	}

	// Div., mimic getColor to write getBColor for bkgnd	
	Color getBColor() {
		if (bcolor==null || bcolor.equals(bcolors[0])) return null;
		Color bc = Color.white;
		if (bcolor.equals(bcolors[1])) bc = Color.black;
		else if (bcolor.equals(bcolors[3])) bc = Color.darkGray;
		else if (bcolor.equals(bcolors[4])) bc = Color.gray;
		else if (bcolor.equals(bcolors[5])) bc = Color.lightGray;
		else if (bcolor.equals(bcolors[6])) bc = Color.yellow;
		else if (bcolor.equals(bcolors[7])) bc = Color.blue;
		else if (bcolor.equals(bcolors[8])) bc = Color.green;
		else if (bcolor.equals(bcolors[9])) bc = Color.red;
		return bc;
	}

	void updateScalebar() {
		updateLocation();
		imp.getProcessor().reset();
		if (createOverlay) {
			if (drawnScaleBar) {
				imp.updateAndDraw();
				drawnScaleBar = false;
			}
			createOverlay(imp);
		} else {
			if (showingOverlay) {
				imp.setOverlay(null);
				showingOverlay = false;
			}
			drawScaleBar(imp);
		}
	}

   class BarDialog extends GenericDialog {

		BarDialog(String title) {
			super(title);
		}

		public void textValueChanged(TextEvent e) {
			TextField widthField = ((TextField)numberField.elementAt(0));
			Double d = getValue(widthField.getText());
			if (d==null)
				return;
			barWidth = d.doubleValue();
			TextField heightField = ((TextField)numberField.elementAt(1));
			d = getValue(heightField.getText());
			if (d==null)
				return;
			barHeightInPixels = (int)d.doubleValue();
			TextField fontSizeField = ((TextField)numberField.elementAt(2));
			d = getValue(fontSizeField.getText());
			if (d==null)
				return;
			int size = (int)d.doubleValue();
			if (size>5)
				fontSize = size;
			updateScalebar();
		}

		public void itemStateChanged(ItemEvent e) {
			Choice col = (Choice)(choice.elementAt(0));
			color = col.getSelectedItem();
			Choice bcol = (Choice)(choice.elementAt(1));
			bcolor = bcol.getSelectedItem();
			Choice loc = (Choice)(choice.elementAt(2));
			location = loc.getSelectedItem();
			boldText = ((Checkbox)(checkbox.elementAt(0))).getState();
			hideText = ((Checkbox)(checkbox.elementAt(1))).getState();
			serifFont = ((Checkbox)(checkbox.elementAt(2))).getState();
			createOverlay = ((Checkbox)(checkbox.elementAt(3))).getState();
			updateScalebar();
		}

   } //BarDialog inner class

} //ScaleBar class