1 package ij.plugin.filter;
5 import java.awt.image.*;
6 import java.awt.event.*;
8 import java.awt.datatransfer.*;
11 import ij.measure.Measurements;
12 import ij.plugin.filter.Analyzer;
13 import ij.text.TextWindow;
28 final static int BAR_LENGTH = 128;
29 final static int BAR_THICKNESS = 12;
30 final static int XMARGIN = 10;
31 final static int YMARGIN = 10;
32 final static int WIN_HEIGHT = BAR_LENGTH;
33 final static int BOX_PAD = 0;
34 final static int LINE_WIDTH = 1;
35 static int nBins = 256;
36 static final String[] colors = {
"White",
"Light Gray",
"Dark Gray",
"Black",
"Red",
"Green",
"Blue",
"Yellow",
"None"};
37 static final String[] locations = {
"Upper Right",
"Lower Right",
"Lower Left",
"Upper Left",
"At Selection"};
38 static final int UPPER_RIGHT=0, LOWER_RIGHT=1, LOWER_LEFT=2, UPPER_LEFT=3, AT_SELECTION=4;
40 static String fillColor = colors[0];
41 static String textColor = colors[3];
42 static String location = locations[UPPER_RIGHT];
43 static double zoom = 1;
44 static int numLabels = 5;
45 static int fontSize = 12;
46 static int decimalPlaces = 0;
53 ImageStatistics stats;
58 Button setup, redraw, insert, unInsert;
60 CheckboxGroup locGroup;
67 boolean boldText =
false;
73 String boxOutlineColor = colors[8];
74 String barOutlineColor = colors[3];
78 String[] fieldNames = null;
80 boolean decimalPlacesChanged;
86 if(imp.getRoi()!=null)
87 location = locations[AT_SELECTION];
92 public void run(ImageProcessor ipPassed) {
93 double mag = (imp!=null)?imp.getCanvas().getMagnification():1.0;
95 zoom = (double) 1.0/mag;
96 ip = ipPassed.duplicate().convertToRGB();
100 if(impOriginal.getRoi()!=null)
101 imp.
setRoi(impOriginal.getRoi());
104 insetPad = imp.getWidth()/50;
117 private void updateColorBar() {
119 Roi roi = imp.getRoi();
120 if (roi!=null && location.equals(locations[AT_SELECTION])) {
121 Rectangle r = roi.getBounds();
122 drawColorBar(imp,r.x,r.y);
123 }
else if( location.equals(locations[UPPER_LEFT]))
124 drawColorBar(imp,insetPad,insetPad);
125 else if(location.equals(locations[UPPER_RIGHT])) {
127 drawColorBar(imp,imp.getWidth()-insetPad-win_width,insetPad);
128 }
else if( location.equals(locations[LOWER_LEFT]) )
129 drawColorBar(imp,insetPad,imp.getHeight() - (int)(WIN_HEIGHT*zoom + 2*(
int)(YMARGIN*zoom)) - (int)(insetPad*zoom));
130 else if(location.equals(locations[LOWER_RIGHT])) {
132 drawColorBar(imp,imp.getWidth()-win_width-insetPad,
133 imp.getHeight() - (int)(WIN_HEIGHT*zoom + 2*(
int)(YMARGIN*zoom)) - insetPad);
139 private boolean showDialog() {
140 gd =
new LiveDialog(
"Calibration Bar");
141 gd.addChoice(
"Location:", locations, location);
142 gd.addChoice(
"Fill Color: ", colors, fillColor);
143 gd.addChoice(
"Label Color: ", colors, textColor);
144 gd.addNumericField(
"Number of Labels:", numLabels, 0);
145 gd.addNumericField(
"Decimal Places:", decimalPlaces, 0);
146 gd.addNumericField(
"Font Size:", fontSize, 0);
147 gd.addNumericField(
"Zoom Factor:", zoom, 1);
148 gd.addCheckbox(
"Bold Text", boldText);
150 if (gd.wasCanceled())
152 location = gd.getNextChoice();
153 fillColor = gd.getNextChoice();
154 textColor = gd.getNextChoice();
155 numLabels = (int)gd.getNextNumber();
156 decimalPlaces = (int)gd.getNextNumber();
157 fontSize = (int)gd.getNextNumber();
158 zoom = (double)gd.getNextNumber();
159 boldText = gd.getNextBoolean();
163 public void verticalColorBar(ImageProcessor ip,
int x,
int y,
int thickness,
int length) {
164 int width = thickness;
166 byte[] rLUT,gLUT,bLUT;
168 java.awt.image.ColorModel cm = lut.getColorModel();
169 if (cm instanceof IndexColorModel) {
170 IndexColorModel m = (IndexColorModel)cm;
171 mapSize = m.getMapSize();
172 rLUT =
new byte[mapSize];
173 gLUT =
new byte[mapSize];
174 bLUT =
new byte[mapSize];
180 rLUT =
new byte[mapSize];
181 gLUT =
new byte[mapSize];
182 bLUT =
new byte[mapSize];
183 for (
int i = 0; i < mapSize; i++) {
189 double colors = mapSize;
192 if (ipOrig instanceof ByteProcessor) {
193 int min = (int)ipOrig.getMin();
195 int max = (int)ipOrig.getMax();
196 if (max>255) max = 255;
200 for (
int i = 0; i<(int)(BAR_LENGTH*zoom); i++) {
201 int iMap = start + (int)Math.round((i*colors)/(BAR_LENGTH*zoom));
205 ip.setColor(
new Color(rLUT[iMap]&0xff, gLUT[iMap]&0xff, bLUT[iMap]&0xff));
206 int j = (int)(BAR_LENGTH*zoom) - i - 1;
207 ip.drawLine(x, j+y, thickness+x, j+y);
210 Color c = getColor(barOutlineColor);
214 ip.lineTo(x+width,y);
215 ip.lineTo(x+width,y+height);
216 ip.lineTo(x,y+height);
221 protected void drawColorBar(ImageProcessor ip,
int xOffset,
int yOffset) {
224 ip.setColor(Color.black);
225 if (decimalPlaces == -1)
226 decimalPlaces = Analyzer.getPrecision();
227 x = (int)(XMARGIN*zoom) + xOffset;
228 y = (int)(YMARGIN*zoom) + yOffset;
230 verticalColorBar(ip, x, y, (
int)(BAR_THICKNESS*zoom), (
int)(BAR_LENGTH*zoom) );
231 drawText(ip, x + (
int)(BAR_THICKNESS*zoom), y,
true);
233 Color c = getColor(boxOutlineColor);
234 if (c != null && !fillColor.equals(
"None")) {
236 ip.setLineWidth(LINE_WIDTH);
237 ip.moveTo(xOffset+BOX_PAD,yOffset+BOX_PAD);
238 ip.lineTo(xOffset+win_width-BOX_PAD,yOffset+BOX_PAD);
239 ip.lineTo(xOffset+win_width-BOX_PAD,yOffset+(
int)(WIN_HEIGHT*zoom + 2*(
int)(YMARGIN*zoom))-BOX_PAD);
240 ip.lineTo(xOffset+BOX_PAD,yOffset+(
int)(WIN_HEIGHT*zoom + 2*(
int)(YMARGIN*zoom))-BOX_PAD);
241 ip.lineTo(xOffset+BOX_PAD,yOffset+BOX_PAD);
246 int drawText(ImageProcessor ip,
int x,
int y,
boolean active) {
248 Color c = getColor(textColor);
253 double hmin = cal.
getCValue(stats.histMin);
254 double hmax = cal.
getCValue(stats.histMax);
255 double barStep = (double)(BAR_LENGTH*zoom) ;
257 barStep /= (numLabels - 1);
259 int fontType = boldText?Font.BOLD:Font.PLAIN;
262 font =
new Font(
"SansSerif", fontType, 9);
264 font =
new Font(
"SansSerif", fontType, (
int)( fontSize*zoom));
266 ip.setAntialiasedText(
true);
270 Image img = GUI.createBlankImage(128, 64);
271 Graphics g = img.getGraphics();
272 FontMetrics metrics = g.getFontMetrics(font);
273 fontHeight = metrics.getHeight();
275 for (
int i = 0; i < numLabels; i++) {
276 double yLabelD = (int)(YMARGIN*zoom + BAR_LENGTH*zoom - i*barStep - 1);
277 int yLabel = (int)(Math.round( y + BAR_LENGTH*zoom - i*barStep - 1));
281 double min = ipOrig.getMin();
282 double max = ipOrig.getMax();
283 if (ipOrig instanceof ByteProcessor) {
285 if (max>255) max = 255;
287 if (cal.calibrated()) {
288 min = cal.getCValue((
int)min);
289 max = cal.getCValue((
int)max);
292 if (!decimalPlacesChanged && decimalPlaces==0 && (
int)max!=max)
295 double grayLabel = min + (max-min)/(numLabels-1) * i;
298 ip.drawString(d2s(grayLabel), x + 5, yLabel + fontHeight/2);
300 int iLength = metrics.stringWidth(d2s(grayLabel));
301 if(iLength > maxLength)
308 String d2s(
double d) {
309 return IJ.d2s(d,decimalPlaces);
312 int getFontHeight() {
313 Image img = GUI.createBlankImage(64, 64);
314 Graphics g = img.getGraphics();
315 int fontType = boldText?Font.BOLD:Font.PLAIN;
316 Font font =
new Font(
"SansSerif", fontType, (
int) (fontSize*zoom) );
317 FontMetrics metrics = g.getFontMetrics(font);
318 return metrics.getHeight();
321 Color getColor(String color) {
322 Color c = Color.white;
323 if (color.equals(colors[1]))
325 else if (color.equals(colors[2]))
327 else if (color.equals(colors[3]))
329 else if (color.equals(colors[4]))
331 else if (color.equals(colors[5]))
333 else if (color.equals(colors[6]))
335 else if (color.equals(colors[7]))
337 else if (color.equals(colors[8]))
342 void calculateWidth() {
343 drawColorBar(imp, -1, -1);
346 public void drawColorBar(ImagePlus imp,
int x,
int y) {
347 Roi roi = impOriginal.getRoi();
350 stats = impOriginal.
getStatistics(Measurements.MIN_MAX, nBins);
353 histogram = stats.histogram;
357 int maxTextWidth = drawText(ip, 0, 0,
false);
358 win_width = (int)(XMARGIN*zoom) + 5 + (int)(BAR_THICKNESS*zoom) + maxTextWidth + (int)((XMARGIN/2)*zoom);
362 Color c = getColor(fillColor);
365 ip.setRoi(x, y, win_width, (
int)(WIN_HEIGHT*zoom + 2*(
int)(YMARGIN*zoom)) );
370 drawColorBar(ip,x,y);
373 ip.setColor(Color.black);
378 class LiveDialog
extends GenericDialog {
380 LiveDialog(String title) {
384 public void textValueChanged(TextEvent e) {
386 if(fieldNames == null) {
387 fieldNames =
new String[4];
389 fieldNames[i] = ((TextField)numberField.elementAt(i)).getName();
392 TextField tf = (TextField)e.getSource();
393 String name = tf.getName();
394 String value = tf.getText();
400 boolean needsRefresh =
false;
402 if(name.equals(fieldNames[0])) {
404 i = getValue( value ).intValue() ;
411 }
else if(name.equals(fieldNames[1])) {
412 i = getValue( value ).intValue() ;
418 decimalPlacesChanged =
true;
421 }
else if(name.equals(fieldNames[2])) {
422 i = getValue( value ).intValue() ;
431 }
else if(name.equals(fieldNames[3])) {
433 d = getValue(
"0" + value ).doubleValue() ;
447 public void itemStateChanged(ItemEvent e) {
448 location = ( (Choice)(choice.elementAt(0)) ).getSelectedItem();
449 fillColor = ( (Choice)(choice.elementAt(1)) ).getSelectedItem();
450 textColor = ( (Choice)(choice.elementAt(2)) ).getSelectedItem();
451 boldText = ( (Checkbox)(checkbox.elementAt(0)) ).getState();