File: ScaleBar.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (862 lines) | stat: -rw-r--r-- 31,655 bytes parent folder | download
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.measure.*;
import ij.util.Tools;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/** This plugin implements the Analyze/Tools/Scale Bar command.
 * Divakar Ramachandran added options to draw a background 
 * and use a serif font on 23 April 2006.
 * Remi Berthoz added an option to draw vertical scale
 * bars on 17 September 2021.
*/
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 = {"Horizontal", "Vertical", "Bold Text", "Hide Text", "Serif Font", "Overlay"};
	final static String SCALE_BAR = "|SB|";
	
	private static final ScaleBarConfiguration sConfig = new ScaleBarConfiguration();
	private ScaleBarConfiguration config = new ScaleBarConfiguration(sConfig);

	ImagePlus imp;
	ArrayList<Roi> previousScaleBar;
	int hBarWidthInPixels;
	int vBarHeightInPixels;
	int roiX, roiY, roiWidth, roiHeight;
	boolean userRoiExists;
	boolean[] checkboxStates = new boolean[6];

	Rectangle hBackground = new Rectangle();
	Rectangle hBar = new Rectangle();
	Rectangle hText = new Rectangle();
	Rectangle vBackground = new Rectangle();
	Rectangle vBar = new Rectangle();
	Rectangle vText = new Rectangle();

	/**
	 * This method is called when the plugin is loaded. 'arg', which
	 * may be blank, is the argument specified for this plugin in
	 * IJ_Props.txt.
	 */
	public void run(String arg) {
		imp = WindowManager.getCurrentImage();
		if (imp == null) {
			IJ.noImage();
			return;
		}
		// Snapshot before anything, so we can revert if the user cancels the action.
		imp.getProcessor().snapshot();
		saveAndDeleteOverlayScaleBar();

		userRoiExists = parseCurrentROI();
		GenericDialog dialog = askUserConfiguration(userRoiExists);

		if (dialog.wasCanceled()) {
			removeScalebar();
			restoreOverlayScaleBar();
			return;
		} else if (dialog.wasOKed()) {
			if (!IJ.isMacro())
				persistConfiguration();
			updateScalebar(!config.labelAll);
		} else				//user has pressed 'remove scale bar'
			removeScalebar();
	 }

	/**
	 * Remove the scalebar drawn by this plugin.
	 * 
	 * If the scalebar was drawn without the overlay by another
	 * instance of the plugin (it is drawn into the image), then
	 * we cannot remove it.
	 * 
	 * If the scalebar was drawn using the overlay by another
	 * instance of the plugin, then we can remove it.
	 * 
	 * With or without the overlay, we can remove a scalebar
	 * drawn by this instance of the plugin.
	 */
	void removeScalebar() {
		// Revert with Undo, in case "Use Overlay" is not ticked
		imp.getProcessor().reset();
		imp.updateAndDraw();
		// Remove overlay drawn by this plugin, in case "Use Overlay" is ticked
		Overlay overlay = imp.getOverlay();
		if (overlay!=null) {
			overlay.remove(SCALE_BAR);
			imp.draw();
		}
	}

	/** Saves the current scale bar previously drawn as an overlay by this plugin,
	 *  so it can be restored later. The scale bar overlay is deleted. */
	void saveAndDeleteOverlayScaleBar() {
		Overlay overlay = imp.getOverlay();
		if (overlay == null) return;
		while (true) {
			int index = overlay.getIndex(SCALE_BAR);
			if (index < 0) break;
			if (previousScaleBar == null)
				previousScaleBar = new ArrayList<Roi>();
			previousScaleBar.add(overlay.get(index));
			overlay.remove(index);
		}
	}

	/** Restores the overlay scale bar previously saved */
	void restoreOverlayScaleBar() {
		if (previousScaleBar == null) return;
		Overlay overlay = imp.getOverlay();
		if (overlay == null) {
			overlay = new Overlay();
			imp.setOverlay(overlay);
		}
		for (Roi roi : previousScaleBar)
			overlay.add(roi, SCALE_BAR);
		imp.draw();
	}

	/**
	 * If there is a user selected ROI, set the class variables {roiX}
	 * and {roiY}, {roiWidth}, {roiHeight} to the corresponding
	 * features of the ROI, and return true. Otherwise, return false.
	 */
    boolean parseCurrentROI() {
        Roi roi = imp.getRoi();
        if (roi == null) return false;

        Rectangle r = roi.getBounds();
        roiX = r.x;
        roiY = r.y;
		roiWidth = r.width;
		roiHeight = r.height;
        return true;
    }

	/**
	 * There is no hard coded value for the width of the scalebar,
	 * when the plugin is called for the first time in an ImageJ
	 * instance, a default value for the width will be computed by
	 * this method.
	 */
	void computeDefaultBarWidth(boolean currentROIExists) {
		Calibration cal = imp.getCalibration();
		ImageWindow win = imp.getWindow();
		double mag = (win!=null)?win.getCanvas().getMagnification():1.0;
		if (mag>1.0) mag=1.0;
		double pixelWidth = cal.pixelWidth;
		if (pixelWidth==0.0)
			pixelWidth = 1.0;
		double pixelHeight = cal.pixelHeight;
		if (pixelHeight==0.0)
			pixelHeight = 1.0;
		double imageWidth = imp.getWidth()*pixelWidth;
		double imageHeight = imp.getHeight()*pixelHeight;

		boolean hBarWidthChanged = false;
		String options = Macro.getOptions();
		if (options!=null && options.contains("width=")) {
			// macro sets the bar width
			String hBarWidthStr = Macro.getValue(options,"width","-1");
			config.hBarWidth = Tools.parseDouble(hBarWidthStr,-1);
			if (config.hBarWidth>0)
				config.hDigits = Utils.getDigits(hBarWidthStr);
		} else if (currentROIExists && roiX>=0 && roiWidth>10) {
			// If the user has a ROI, set the bar width according to ROI width.
			config.hBarWidth = roiWidth*pixelWidth;
			hBarWidthChanged = true;
		} else if (config.hBarWidth<=0.0 || config.hBarWidth<0.01*imageWidth || config.hBarWidth>0.67*imageWidth) {
			// If the bar is of negative width or too wide for the image,
			// set the bar width to 80 pixels.
			config.hBarWidth = (80.0*pixelWidth)/mag;
			config.hBarWidth = Utils.round(config.hBarWidth);
			// If 80 pixels is too much, do 2/3 of the image. If too small, 4% of image (rounded down)
			if (config.hBarWidth>0.67*imageWidth)
				config.hBarWidth = Utils.round(0.67*imageWidth);
			else if (config.hBarWidth<0.04*imageWidth)
				config.hBarWidth = Utils.round(0.04*imageWidth);
			hBarWidthChanged = true;
		}

		boolean vBarHeightChanged = false;
		if (options!=null && options.contains("height=")) {
			// macro sets the bar height
			String vBarHeightStr = Macro.getValue(options,"height","-1");
			config.vBarHeight = Tools.parseDouble(vBarHeightStr,-1);
			if (config.vBarHeight>0)
				config.vDigits = Utils.getDigits(vBarHeightStr);
		} else if (currentROIExists && roiY>=0 && roiHeight>10) {
			config.vBarHeight = roiHeight*pixelHeight;
			vBarHeightChanged = true;
		} else if (config.vBarHeight<=0.0 || config.vBarHeight<0.01*imageHeight || config.vBarHeight>0.67*imageHeight) {
			config.vBarHeight = (80.0*pixelHeight)/mag;
			config.vBarHeight = Utils.round(config.vBarHeight);
			// If 80 pixels is too much, do 2/3 of the image. If too small, 4% of image (rounded down)
			if (config.vBarHeight>0.67*imageHeight)
				config.vBarHeight = Utils.round(0.67*imageHeight);
			else if (config.vBarHeight<0.04*imageHeight)
				config.vBarHeight = Utils.round(0.04*imageHeight);
			vBarHeightChanged = true;
		}
		if (hBarWidthChanged)
			config.hDigits = Utils.getDigits(config.hBarWidth);
		if (vBarHeightChanged)
			config.vDigits = Utils.getDigits(config.vBarHeight);
		//IJ.log("computeDefaultBarWidth2: "+config.hBarWidth+" "+currentROIExists+" "+config.vDigits);
	} 

	/**
	 * Generates & draws the configuration dialog.
	 * 
	 * Returns a reference to the dialog, to check for
	 * dialog.wasOKed() (draw the dialog),
	 * dialog.wasCanceled (cancel, revert to previous) or
	 * none of these (remove previous overlay scale bar)
	 */
	GenericDialog askUserConfiguration(boolean currentROIExists) {
		// Update the user configuration if there is an ROI, or if
		// the defined bar width is negative (it is if it has never
		// been set in this ImageJ instance).
		if (currentROIExists)
			config.location = locations[AT_SELECTION];
		if (IJ.isMacro()) {
			config = new ScaleBarConfiguration();
			config.boldText = false;
			config.useOverlay = false;
		}
		Calibration cal = imp.getCalibration();
		if (config.showHorizontal && (config.hBarWidth <= 5*cal.pixelWidth || config.hBarWidth > cal.pixelWidth*imp.getWidth()) ||
				config.showVertical && (config.vBarHeight <= 5*cal.pixelHeight || config.vBarHeight > cal.pixelHeight*imp.getHeight()) ||
				currentROIExists) {
			computeDefaultBarWidth(currentROIExists);
		}

		// Draw a first preview scalebar, with the default or presisted
		// configuration.
		updateScalebar(true);
		
		// Create & show the dialog, then return.
		boolean multipleSlices = imp.getStackSize() > 1;
		GenericDialog dialog = new BarDialog(getHUnit(), getVUnit(), multipleSlices);
		DialogListener dialogListener = new BarDialogListener(multipleSlices);
		dialog.addDialogListener(dialogListener);
		dialog.showDialog();
		return dialog;
	}

	/**
	 * Stores the active configuration into the static variable that
	 * is persisted across calls of the plugin.
	 * 
	 * The "active" configuration is normally the one reflected by
	 * the dialog.
	 */
	void persistConfiguration() {
		sConfig.updateFrom(config);
	}
	
	/**
	 * Return the X unit strings defined in the image calibration.
	 */
	String getHUnit() {
		String hUnits = imp.getCalibration().getXUnits();
		if (hUnits.equals("microns"))
			hUnits = IJ.micronSymbol+"m";
		return hUnits;
	}

	/**
	 * Return the Y unit strings defined in the image calibration.
	 */
	String getVUnit() {
		String vUnits = imp.getCalibration().getYUnits();
		if (vUnits.equals("microns"))
			vUnits = IJ.micronSymbol+"m";
		return vUnits;
	}

	/**
	 * Create & draw the scalebar using an Overlay.
	 */
	Overlay createScaleBarOverlay() throws MissingRoiException {
		Overlay overlay = new Overlay();

		Color color = getColor();
		Color bcolor = getBColor();
		
		int fontType = config.boldText?Font.BOLD:Font.PLAIN;
		String face = config.serifFont?"Serif":"SanSerif";
		Font font = new Font(face, fontType, config.fontSize);
		ImageProcessor ip = imp.getProcessor();
		ip.setFont(font);

		setElementsPositions(ip);

		if (bcolor != null) {
			if (config.showHorizontal) {
				Roi hBackgroundRoi = new Roi(hBackground.x, hBackground.y, hBackground.width, hBackground.height);
				hBackgroundRoi.setFillColor(bcolor);
				overlay.add(hBackgroundRoi, SCALE_BAR);
			}
			if (config.showVertical) {
				Roi vBackgroundRoi = new Roi(vBackground.x, vBackground.y, vBackground.width, vBackground.height);
				vBackgroundRoi.setFillColor(bcolor);
				overlay.add(vBackgroundRoi, SCALE_BAR);
			}
		}

		if (config.showHorizontal) {
			Roi hBarRoi = new Roi(hBar.x, hBar.y, hBar.width, hBar.height);
			hBarRoi.setFillColor(color);
			overlay.add(hBarRoi, SCALE_BAR);
		}
		if (config.showVertical) {
			Roi vBarRoi = new Roi(vBar.x, vBar.y, vBar.width, vBar.height);
			vBarRoi.setFillColor(color);
			overlay.add(vBarRoi, SCALE_BAR);
		}

		if (!config.hideText) {
			if (config.showHorizontal) {
				TextRoi hTextRoi = new TextRoi(hText.x, hText.y, getHLabel(), font);
				hTextRoi.setStrokeColor(color);
				overlay.add(hTextRoi, SCALE_BAR);
			}
			if (config.showVertical) {
				TextRoi vTextRoi = new TextRoi(vText.x, vText.y + vText.height, getVLabel(), font);
				vTextRoi.setStrokeColor(color);
				vTextRoi.setAngle(90.0);
				overlay.add(vTextRoi, SCALE_BAR);
			}
		}

		return overlay;
	}

	/**
	 * Returns the text to draw near the scalebar (<width> <unit>).
	 */
	String getHLabel() {
		return IJ.d2s(config.hBarWidth, config.hDigits) + " " + getHUnit();
	}

	/**
	 * Returns the text to draw near the scalebar (<height> <unit>).
	 */
	String getVLabel() {
		return IJ.d2s(config.vBarHeight, config.vDigits) + " " + getVUnit();
	}

	/**
	 * Returns the width of the box that contains the horizontal scalebar and
	 * its label.
	 */
	int getHBoxWidthInPixels() {
		updateFont();
		ImageProcessor ip = imp.getProcessor();
		int hLabelWidth = config.hideText ? 0 : ip.getStringWidth(getHLabel());
		int hBoxWidth = Math.max(hBarWidthInPixels, hLabelWidth);
		return (config.showHorizontal ? hBoxWidth : 0);
	}

	/**
	 * Returns the height of the box that contains the horizontal scalebar and
	 * its label.
	 */
	int getHBoxHeightInPixels() {
		int hLabelHeight = config.hideText ? 0 : config.fontSize;
		int hBoxHeight = config.barThicknessInPixels + (int) (hLabelHeight * 1.25);
		return (config.showHorizontal ? hBoxHeight : 0);
	}

	/**
	 * Returns the height of the box that contains the vertical scalebar and
	 * its label.
	 */
	int getVBoxHeightInPixels() {
		updateFont();
		ImageProcessor ip = imp.getProcessor();
		int vLabelHeight = config.hideText ? 0 : ip.getStringWidth(getVLabel());
		int vBoxHeight = Math.max(vBarHeightInPixels, vLabelHeight);
		return (config.showVertical ? vBoxHeight : 0);
	}

	/**
	 * Returns the width of the box that contains the vertical scalebar and
	 * its label.
	 */
	int getVBoxWidthInPixels() {
		int vLabelWidth = config.hideText ? 0 : config.fontSize;
		int vBoxWidth = config.barThicknessInPixels + (int) (vLabelWidth * 1.25);
		return (config.showVertical ? vBoxWidth : 0);
	}

	/**
	 * Returns the size of margins that should be displayed between the scalebar
	 * elements and the image edge.
	 */
	int getOuterMarginSizeInPixels() {
		int imageWidth = imp.getWidth();
		int imageHeight = imp.getHeight();
		return (imageWidth + imageHeight) / 100;
	}

	/**
	 * Retruns the size of margins that should be displayed between the scalebar
	 * elements and the edge of the element's backround.
	 */
	int getInnerMarginSizeInPixels() {
		int maxWidth = Math.max(getHBoxWidthInPixels(), getVBoxHeightInPixels());
		int margin = Math.max(maxWidth/20, 2);
		return config.bcolor.equals("None") ? 0 : margin;
	}

	void updateFont() {
		int fontType = config.boldText?Font.BOLD:Font.PLAIN;
		String font = config.serifFont?"Serif":"SanSerif";
		ImageProcessor ip = imp.getProcessor();
		ip.setFont(new Font(font, fontType, config.fontSize));
		ip.setAntialiasedText(true);
	}

	/**
	 * Sets the positions x y of hBackground and vBackground based on
	 * the current configuration.
	 */
	void setBackgroundBoxesPositions(ImageProcessor ip) throws MissingRoiException {
		Calibration cal = imp.getCalibration();
		hBarWidthInPixels = (int)Math.round(config.hBarWidth/cal.pixelWidth);
		vBarHeightInPixels = (int)Math.round(config.vBarHeight/cal.pixelHeight);

		boolean hTextTop = config.showVertical && (config.location.equals(locations[UPPER_LEFT]) || config.location.equals(locations[UPPER_RIGHT]));
		
		int imageWidth = imp.getWidth();
		int imageHeight = imp.getHeight();
		int hBoxWidth = getHBoxWidthInPixels();
		int hBoxHeight = getHBoxHeightInPixels();
		int vBoxWidth = getVBoxWidthInPixels();
		int vBoxHeight = getVBoxHeightInPixels();
		int outerMargin = getOuterMarginSizeInPixels();
		int innerMargin = getInnerMarginSizeInPixels();
		
		hBackground.width = innerMargin + hBoxWidth + innerMargin;
		hBackground.height = innerMargin + hBoxHeight + innerMargin;
		vBackground.width = innerMargin + vBoxWidth + innerMargin;
		vBackground.height = innerMargin + vBoxHeight + innerMargin;

		if (config.location.equals(locations[UPPER_RIGHT])) {
			hBackground.x = imageWidth - outerMargin - innerMargin - vBoxWidth + (config.showVertical ? config.barThicknessInPixels : 0) - hBoxWidth - innerMargin;
			hBackground.y = outerMargin;
			vBackground.x = imageWidth - outerMargin - innerMargin - vBoxWidth - innerMargin;
			vBackground.y = outerMargin + (hTextTop ? hBoxHeight - config.barThicknessInPixels : 0);
			hBackground.width += (config.showVertical ? vBoxWidth - config.barThicknessInPixels : 0);

		} else if (config.location.equals(locations[LOWER_RIGHT])) {
			hBackground.x = imageWidth - outerMargin - innerMargin - vBoxWidth - hBoxWidth + (config.showVertical ? config.barThicknessInPixels : 0) - innerMargin;
			hBackground.y = imageHeight - outerMargin - innerMargin - hBoxHeight - innerMargin;
			vBackground.x = imageWidth - outerMargin - innerMargin - vBoxWidth - innerMargin;
			vBackground.y = imageHeight - outerMargin - innerMargin - hBoxHeight + (config.showHorizontal ? config.barThicknessInPixels : 0) - vBoxHeight - innerMargin;
			vBackground.height += (config.showHorizontal ? hBoxHeight - config.barThicknessInPixels : 0);

		} else if (config.location.equals(locations[UPPER_LEFT])) {
			hBackground.x = outerMargin + (config.showVertical ? vBackground.width - 2*innerMargin - config.barThicknessInPixels : 0);
			hBackground.y = outerMargin;
			vBackground.x = outerMargin;
			vBackground.y = outerMargin + (hTextTop ? hBoxHeight - config.barThicknessInPixels : 0);
			hBackground.width += (config.showVertical ? vBoxWidth - config.barThicknessInPixels : 0);
			hBackground.x -= (config.showVertical ? vBoxWidth - config.barThicknessInPixels : 0);

		} else if (config.location.equals(locations[LOWER_LEFT])) {
			hBackground.x = outerMargin + (config.showVertical ? vBackground.width - 2*innerMargin - config.barThicknessInPixels : 0);
			hBackground.y = imageHeight - outerMargin - innerMargin - hBoxHeight - innerMargin;
			vBackground.x = outerMargin;
			vBackground.y = imageHeight - outerMargin - innerMargin - hBoxHeight + (config.showHorizontal ? config.barThicknessInPixels : 0) - vBoxHeight - innerMargin;
			vBackground.height += (config.showHorizontal ? hBoxHeight - config.barThicknessInPixels : 0);

		} else {
			if (!userRoiExists)
				throw new MissingRoiException();

			hBackground.x = roiX;
			hBackground.y = roiY;
			vBackground.x = roiX;
			vBackground.y = roiY;
		}
	}

	/**
	 * Sets the rectangles x y positions for scalebar elements (hBar, hText, vBar, vText),
	 * based on the current configuration. Also sets the width and height of the rectangles.
	 * 
	 * The position of each rectangle is relative to hBackground and vBackground,
	 * so setBackgroundBoxesPositions() must run before this method computes positions.
	 * This method calls setBackgroundBoxesPositions().
	 */
	void setElementsPositions(ImageProcessor ip) throws MissingRoiException {

		setBackgroundBoxesPositions(ip);

		int hBoxWidth = getHBoxWidthInPixels();
		int hBoxHeight = getHBoxHeightInPixels();

		int vBoxWidth = getVBoxWidthInPixels();
		int vBoxHeight = getVBoxHeightInPixels();

		int innerMargin = getInnerMarginSizeInPixels();

		boolean right = config.location.equals(locations[LOWER_RIGHT]) || config.location.equals(locations[UPPER_RIGHT]);
		boolean upper = config.location.equals(locations[UPPER_RIGHT]) || config.location.equals(locations[UPPER_LEFT]);
		boolean hTextTop = config.showVertical && upper;
		
		hBar.x = hBackground.x + innerMargin + (hBoxWidth - hBarWidthInPixels)/2 + (config.showVertical && !right && upper ? vBoxWidth - config.barThicknessInPixels : 0);
		hBar.y = hBackground.y + innerMargin + (hTextTop ? hBoxHeight - config.barThicknessInPixels : 0);
		hBar.width = hBarWidthInPixels;
		hBar.height = config.barThicknessInPixels;

		hText.height = config.hideText ? 0 : config.fontSize;
		hText.width = config.hideText ? 0 : ip.getStringWidth(getHLabel());
		hText.x = hBackground.x + innerMargin + (hBoxWidth - hText.width)/2 + (config.showVertical && !right && upper ? vBoxWidth - config.barThicknessInPixels : 0);
		hText.y = hTextTop ? (hBackground.y + innerMargin - (int)Math.round(config.fontSize*0.25)) : (hBar.y + hBar.height);

		vBar.width = config.barThicknessInPixels;
		vBar.height = vBarHeightInPixels;
		vBar.x = vBackground.x + (right ? innerMargin : vBackground.width - config.barThicknessInPixels - innerMargin);
		vBar.y = vBackground.y + innerMargin + (vBoxHeight - vBar.height)/2;

		vText.height = config.hideText ? 0 : ip.getStringWidth(getVLabel());
		vText.width = config.hideText ? 0 : config.fontSize;
		vText.x = right ? (vBar.x + vBar.width) : (vBar.x - vBoxWidth + config.barThicknessInPixels - (int)Math.round(config.fontSize*0.25));
		vText.y = vBackground.y + innerMargin + (vBoxHeight - vText.height)/2;
	}

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

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

	/**
	 * Draw the scale bar, based on the current configuration.
	 * 
	 * If {previewOnly} is true, only the active slice will be
	 * labeled with a scalebar. If it is false, all slices of
	 * the stack will be labeled.
	 * 
	 * This method chooses whether to use an overlay or the
	 * drawing tool to create the scalebar.
	 */
	void updateScalebar(boolean previewOnly) {
		removeScalebar();

		Overlay scaleBarOverlay;
		try {
			scaleBarOverlay = createScaleBarOverlay();
		} catch (MissingRoiException e) {
			return; // Simply don't draw the scalebar.
		}

		Overlay impOverlay = imp.getOverlay();
		if (impOverlay==null) {
			impOverlay = new Overlay();
		}

		if (config.useOverlay) {
			for (Roi roi : scaleBarOverlay)
				impOverlay.add(roi);
			imp.setOverlay(impOverlay);
		} else {
			if (previewOnly) {
				ImageProcessor ip = imp.getProcessor();
				drawOverlayOnProcessor(scaleBarOverlay, ip);
				imp.updateAndDraw();
			} else {
				ImageStack stack = imp.getStack();
				for (int i=1; i<=stack.size(); i++) {
					ImageProcessor ip = stack.getProcessor(i);
					drawOverlayOnProcessor(scaleBarOverlay, ip);
					imp.updateAndDraw();
				}
				imp.setStack(stack);
			}
		}
	}

	void drawOverlayOnProcessor(Overlay overlay, ImageProcessor processor) {
		if (processor.getBitDepth() == 8 || processor.getBitDepth() == 24) {
			// drawOverlay() only works for 8-bits and RGB
			processor.drawOverlay(overlay);
			return;
		}
		// Generate a buffer ImageProcessor, completely black.
		// We will draw the overlay on it, then loop over each pixel,
		// to replace drawn pixels in the original processor.
		ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight());
		ip.setCalibrationTable(processor.getCalibrationTable());
		LUT lut = ip.getLut();
		for (Roi roi : overlay) {
			Color fillColor = roi.getFillColor();
			if (fillColor != null) {
				int i = processor.getBestIndex(fillColor);
				roi.setFillColor(new Color(lut.getRed(i), lut.getGreen(i), lut.getBlue(i)));
				// Below, when looping on each pixel of the buffer, we detect whether it was
				// drawn by checking if the pixel value is greater than zero. Hence, we cannot
				// put zero-valued pixels when the Roi is black, or these pixels will not
				// be part of the drawing.
				if (roi.getFillColor().equals(Color.BLACK)) roi.setFillColor(new Color(1, 1, 1));
			}
			Color strokeColor = roi.getStrokeColor();
			if (strokeColor != null) {
				int i = processor.getBestIndex(strokeColor);
				roi.setStrokeColor(new Color(lut.getRed(i), lut.getGreen(i), lut.getBlue(i)));
				if (roi.getStrokeColor().equals(Color.BLACK)) roi.setStrokeColor(new Color(1, 1, 1));
			}
		}
		ip.drawOverlay(overlay);
		for (int y = 0; y < ip.getHeight(); y++)
			for (int x = 0; x < ip.getWidth(); x++) {
				int p = ip.get(x, y);
				if (p > 0) {
					p = (int)Math.round(p * ((processor.getMax() - processor.getMin()) / 255d) + (float)processor.getMin());
					if (processor.getBitDepth() == 32)
						p = Float.floatToIntBits(p);
					processor.putPixel(x, y, p);
				}
			}
	}

   class BarDialog extends GenericDialog {

		BarDialog(String hUnits, String vUnits, boolean multipleSlices) {
			super("Scale Bar");
			if (config.hDigits>7) config.hDigits=4;
			if (config.vDigits>7) config.vDigits=4;
			addNumericField("Width in "+hUnits+": ", config.hBarWidth, config.hDigits);
			addNumericField("Height in "+vUnits+": ", config.vBarHeight, config.vDigits);
			addNumericField("Thickness in pixels: ", config.barThicknessInPixels, 0);
			addNumericField("Font size: ", config.fontSize, 0);
			addChoice("Color: ", colors, config.color);
			addChoice("Background: ", bcolors, config.bcolor);
			addChoice("Location: ", locations, config.location);
			checkboxStates[0] = config.showHorizontal; checkboxStates[1] = config.showVertical;
			checkboxStates[2] = config.boldText; checkboxStates[3] = config.hideText;
			checkboxStates[4] = config.serifFont; checkboxStates[5] = config.useOverlay;
			setInsets(10, 25, 0);
			addCheckboxGroup(3, 2, checkboxLabels, checkboxStates);

			// For simplicity of the itemStateChanged() method below,
			// is is best to keep the "Label all slices" checkbox in
			// the last position.
			if (multipleSlices) {
				setInsets(0, 25, 0);
				addCheckbox("Label all slices", config.labelAll);
			}
			if (previousScaleBar != null)
				enableYesNoCancel("OK", "Remove Scale Bar");
		}
   } //BarDialog inner class

	class BarDialogListener implements DialogListener {

		boolean multipleSlices;

		public BarDialogListener(boolean multipleSlices) {
			super();
			this.multipleSlices = multipleSlices;
		}

		@Override
		public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
			config.hBarWidth = gd.getNextNumber();
			config.vBarHeight = gd.getNextNumber();
			config.barThicknessInPixels = (int)gd.getNextNumber();
			config.fontSize = (int)gd.getNextNumber();
			config.color = gd.getNextChoice();
			config.bcolor = bcolors[gd.getNextChoiceIndex()];
			if (imp!=null && imp.getRoi()!=null)
				config.location = gd.getNextChoice(); //disables smart recording
			else
				config.location = locations[gd.getNextChoiceIndex()];
			config.showHorizontal = gd.getNextBoolean();
			config.showVertical = gd.getNextBoolean();
			config.boldText = gd.getNextBoolean();
			config.hideText = gd.getNextBoolean();
			config.serifFont = gd.getNextBoolean();
			config.useOverlay = gd.getNextBoolean();
			if (imp.isComposite())
				config.useOverlay = true;
			if (multipleSlices)
				config.labelAll = gd.getNextBoolean();
			if (!config.showHorizontal && !config.showVertical && IJ.isMacro()) {
				// Previous versions of this plugin did not handle vertical scale bars:
				// the macro syntax was different in that "height" meant "thickness" of
				// the horizontal scalebar.
				// If the conditional above is true, then the macro syntax is the old
				// one, so we swap a few config variables.
				config.showHorizontal = true;
				String options = Macro.getOptions();
				if ((int)config.vBarHeight!=50 && options!=null&&options.contains("height=")&&!options.contains("thickness="))
					config.barThicknessInPixels = (int)config.vBarHeight;
				config.vBarHeight = 0.0;
			}
			Vector numericFields = gd.getNumericFields();
			if (numericFields!=null) {
				String widthString = ((TextField) numericFields.elementAt(0)).getText();
				config.hDigits = Utils.getDigits(widthString);
				String heightString = ((TextField) numericFields.elementAt(1)).getText();
				config.vDigits = Utils.getDigits(heightString);
			}
			updateScalebar(true);
			return true;
		}
	}

   class MissingRoiException extends Exception {
		MissingRoiException() {
			super("Scalebar location is set to AT_SELECTION but there is no selection on the image.");
		}
   } //MissingRoiException inner class

	static class ScaleBarConfiguration {
	
		private static int defaultBarHeight = 4;

		boolean showHorizontal;
		boolean showVertical;
		double hBarWidth;
		double vBarHeight;
		int hDigits;  // The number of digits after the decimal point that the user input in the dialog for vBarWidth.
		int vDigits;
		int barThicknessInPixels;
		String location;
		String color;
		String bcolor;
		boolean boldText;
		boolean hideText;
		boolean serifFont;
		boolean useOverlay;
		int fontSize;
		boolean labelAll;

		/**
		 * Create ScaleBarConfiguration with default values.
		 */
		ScaleBarConfiguration() {
			this.showHorizontal = true;
			this.showVertical = false;
			this.hBarWidth = -1;
			this.vBarHeight = -1;
			this.barThicknessInPixels = defaultBarHeight;
			this.location = locations[LOWER_RIGHT];
			this.color = colors[0];
			this.bcolor = bcolors[0];
			this.boldText = true;
			this.hideText = false;
			this.serifFont = false;
			this.useOverlay = true;
			this.fontSize = 14;
			this.labelAll = false;
		}

		/**
		 * Copy constructor.
		 */
		ScaleBarConfiguration(ScaleBarConfiguration model) {
			this.updateFrom(model);
		}
		
		void updateFrom(ScaleBarConfiguration model) {
			this.showHorizontal = model.showHorizontal;
			this.showVertical = model.showVertical;
			this.hBarWidth = model.hBarWidth;
			this.vBarHeight = model.vBarHeight;
			this.hDigits = model.hDigits;
			this.vDigits = model.vDigits;
			this.barThicknessInPixels = model.barThicknessInPixels;
			this.location = locations[LOWER_RIGHT];
			this.color = model.color;
			this.bcolor = model.bcolor;
			this.boldText = model.boldText;
			this.serifFont = model.serifFont;
			this.hideText = model.hideText;
			this.useOverlay = model.useOverlay;
			this.fontSize = model.fontSize;
			this.labelAll = model.labelAll;
		}
	} //ScaleBarConfiguration inner class

	// Utils inner class (for methods called from main and inner classes)
	static class Utils {
		/** Decimal digits for displaying a number encoded in a String */
		static int getDigits(double x) {
			if (Math.abs(x) > 1000000 || Math.abs(x) < 1e-4)
				return getDigits(IJ.d2s(x, -9).replaceAll("0+E", "E")); //('0' before exponent deleted)
			else
				return getDigits(IJ.d2s(x, 9).replaceAll("0+\\z", "")); //(trailing '0' deleted)
		}

		/** Decimal digits for displaying a number encoded in a String */
		static int getDigits(String str) {
			boolean hasDecimalPoint = false;
			boolean scientificFormat = false;
			int totalDigits = 0;
			int decimalDigits = 0;
			for (int i = 0; i < str.length(); i++) {
				char c = str.charAt(i);
				if (c == '.') {
					hasDecimalPoint = true;
				} else if (c == 'e' || c == 'E') {
					scientificFormat = true;
					break;
				} else if (c >= '0' && c <= '9') {
					totalDigits++;
					if (hasDecimalPoint)
						decimalDigits++;
				}
			}
			if (scientificFormat) {
				int digits = -(totalDigits-1);
				if (digits == 0) digits = -1;
				return digits;
			} else {
				return decimalDigits;
			}
		}
		/** Rounds down a positive value such that the first digit is 1, 2, or 5 */
		static double round(double x) {
			double base = Math.pow(10, Math.floor(Math.log10(x)+1e-8));
			if (x > 4.999999*base) return 5*base;
			else if (x > 1.999999*base) return 2*base;
			else return base;
		}
	} // Utils inner class 
} //ScaleBar class