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"};
	final static String SCALE_BAR = "|SB|";
	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;

	public void run(String arg) {
		imp = WindowManager.getCurrentImage();
		if (imp!=null) {
			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 && barHeightInPixels1) {
			gd.setInsets(0, 25, 0);
			gd.addCheckbox("Label all slices", labelAll);
		}
		gd.showDialog();
		if (gd.wasCanceled()) {
			imp.getProcessor().reset();
			imp.updateAndDraw();
			Overlay overlay = imp.getOverlay();
			if (showingOverlay && overlay!=null) {
				overlay.remove(SCALE_BAR);
				imp.draw();
			}
			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 = imp.getOverlay();
		if (overlay==null)
			overlay = new Overlay();
		else
			overlay.remove(SCALE_BAR);
		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 (w5)
				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

 

Ads help maintain this website.