7 import ij.plugin.filter.*;
9 import ij.plugin.frame.Recorder;
10 import java.awt.event.*;
12 import java.util.Locale;
14 import java.applet.Applet;
16 import java.lang.reflect.*;
21 public static boolean debugMode =
false;
23 public static final char micronSymbol = (char)181;
24 public static final char angstromSymbol = (char)197;
25 public static final char degreeSymbol = (char)176;
28 private static java.applet.Applet applet;
29 private static ProgressBar progressBar;
30 private static TextPanel textPanel;
31 private static String osname;
33 private static boolean altDown, spaceDown, shiftDown;
35 private static Thread previousThread;
36 private static TextPanel logPanel;
37 private static boolean notVerified =
true;
38 private static PluginClassLoader classLoader;
39 private static boolean memMessageDisplayed;
43 osname = System.getProperty(
"os.name");
44 isWin = osname.startsWith(
"Windows");
45 isMac = !isWin && osname.startsWith(
"Mac");
46 String version = System.getProperty(
"java.version").substring(0,3);
48 isJava2 = version.compareTo(
"1.1")>0 && version.compareTo(
"2.9")<=0;
49 isJava14 = version.compareTo(
"1.3")>0 && version.compareTo(
"2.9")<=0;
52 static void init(
ImageJ imagej, Applet theApplet) {
55 progressBar = ij.getProgressBar();
64 public static Object
runPlugIn(String className, String arg) {
69 static Object
runPlugIn(String commandName, String className, String arg) {
71 IJ.
log(
"runPlugin: "+className+
" "+arg);
72 Object thePlugIn=null;
74 Class c = Class.forName(className);
75 thePlugIn = c.newInstance();
76 if (thePlugIn instanceof PlugIn) {
77 ((PlugIn)thePlugIn).run(arg);
79 runFilterPlugIn(thePlugIn, commandName, arg);
82 catch (ClassNotFoundException e) {
83 if (IJ.getApplet()==null)
84 System.err.println(
"Plugin not found: " + className);
86 catch (InstantiationException e) {
log(
"Unable to load plugin (ins)");}
87 catch (IllegalAccessException e) {
log(
"Unable to load plugin, possibly \nbecause it is not public.");}
91 static void runFilterPlugIn(Object theFilter, String cmd, String arg) {
92 ImagePlus imp = IJ.getInstance().getImagePlus();
93 int capabilities = ((PlugInFilter)theFilter).setup(arg, imp);
94 if ((capabilities&PlugInFilter.DONE)!=0)
96 if ((capabilities&PlugInFilter.NO_IMAGE_REQUIRED)!=0)
97 {((PlugInFilter)theFilter).run(null);
return;}
99 {IJ.noImage();
return;}
100 if ((capabilities&PlugInFilter.ROI_REQUIRED)!=0 && imp.getRoi()==null)
101 {IJ.error(
"Selection required");
return;}
102 int type = imp.getType();
104 case ImagePlus.GRAY8:
105 if ((capabilities&PlugInFilter.DOES_8G)==0)
106 {wrongType(capabilities, cmd);
return;}
108 case ImagePlus.COLOR_256:
109 if ((capabilities&PlugInFilter.DOES_8C)==0)
110 {wrongType(capabilities, cmd);
return;}
112 case ImagePlus.GRAY16:
113 if ((capabilities&PlugInFilter.DOES_16)==0)
114 {wrongType(capabilities, cmd);
return;}
116 case ImagePlus.GRAY32:
117 if ((capabilities&PlugInFilter.DOES_32)==0)
118 {wrongType(capabilities, cmd);
return;}
120 case ImagePlus.COLOR_RGB:
121 if ((capabilities&PlugInFilter.DOES_RGB)==0)
122 {wrongType(capabilities, cmd);
return;}
128 IJ.showStatus(cmd +
"...");
130 ImageProcessor mask = null;
131 float[] cTable = imp.getCalibration().getCTable();
132 ip = imp.getProcessor();
133 mask = imp.getMask();
134 if ((capabilities&PlugInFilter.NO_UNDO)!=0)
137 Undo.setup(Undo.FILTER, imp);
141 ip.setCalibrationTable(cTable);
142 ((PlugInFilter)theFilter).run(ip);
143 if ((capabilities&PlugInFilter.SUPPORTS_MASKING)!=0)
144 ip.reset(ip.getMask());
145 IJ.showTime(imp, imp.getStartTime(), cmd +
": ", 1);
146 if ((capabilities&PlugInFilter.NO_CHANGES)==0) {
153 static Object runUserPlugIn(String commandName, String className, String arg,
boolean createNewLoader) {
156 String pluginsDir = Menus.getPlugInsPath();
157 if (pluginsDir==null)
161 IJ.runPlugIn(
"ij.plugin.ClassChecker",
"");
164 PluginClassLoader loader;
166 loader =
new PluginClassLoader(pluginsDir);
168 if (classLoader==null)
169 classLoader =
new PluginClassLoader(pluginsDir);
170 loader = classLoader;
172 Object thePlugIn = null;
174 thePlugIn = (loader.loadClass(className)).newInstance();
175 if (thePlugIn instanceof PlugIn)
176 ((PlugIn)thePlugIn).run(arg);
177 else if (thePlugIn instanceof PlugInFilter)
178 runFilterPlugIn(thePlugIn, commandName, arg);
180 catch (ClassNotFoundException e) {
181 if (className.indexOf(
'_')!=-1)
182 IJ.error(
"Plugin not found: "+className);
184 catch (InstantiationException e) {IJ.error(
"Unable to load plugin (ins)");}
185 catch (IllegalAccessException e) {IJ.error(
"Unable to load plugin, possibly \nbecause it is not public.");}
189 static void wrongType(
int capabilities, String cmd) {
190 String s =
"\""+cmd+
"\" requires an image of type:\n \n";
191 if ((capabilities&PlugInFilter.DOES_8G)!=0) s +=
" 8-bit grayscale\n";
192 if ((capabilities&PlugInFilter.DOES_8C)!=0) s +=
" 8-bit color\n";
193 if ((capabilities&PlugInFilter.DOES_16)!=0) s +=
" 16-bit grayscale\n";
194 if ((capabilities&PlugInFilter.DOES_32)!=0) s +=
" 32-bit (float) grayscale\n";
195 if ((capabilities&PlugInFilter.DOES_RGB)!=0) s +=
" RGB color\n";
202 ij.doCommand(command);
207 public static void run(String command) {
214 public static void run(String command, String options) {
218 Thread thread = Thread.currentThread();
219 if (previousThread==null || thread!=previousThread) {
220 String name = thread.getName();
221 if (!name.startsWith(
"Run$_"))
222 thread.setName(
"Run$_"+name);
224 if (command.equals(
"Miscellaneous..."))
226 previousThread = thread;
238 private static void testAbort() {
253 if (ij!=null) ij.showStatus(s);
258 public static void write(String s) {
264 System.out.println(s);
267 private static void showResults() {
271 textPanel.addKeyListener(ij);
276 public static synchronized void log(String s) {
277 System.out.println(s);
286 textPanel.setColumnHeadings(headings);
291 return textPanel!=null;
309 showMessage(
"No Image",
"There are no images open.");
316 String tot = Runtime.getRuntime().totalMemory()/1048576L+
"MB";
317 if (!memMessageDisplayed)
318 log(
">>>>>>>>>>>>>>>>>>>>>>>>>>>");
319 log(
"<Out of memory>");
320 if (!memMessageDisplayed) {
321 log(
"<All available memory ("+tot+
") has been>");
322 log(
"<used. Instructions for making more>");
323 log(
"<available can be found in the \"Memory\" >");
324 log(
"<sections of the installation notes at>");
325 log(
"<http://rsb.info.nih.gov/ij/docs/install/>");
326 log(
">>>>>>>>>>>>>>>>>>>>>>>>>>>");
327 memMessageDisplayed =
true;
336 if (progressBar!=null) progressBar.show(progress);
343 if (progressBar!=null) progressBar.show(currentIndex, finalIndex);
350 if (msg.startsWith(
"<html>") &&
isJava2())
351 new HTMLDialog(title, msg);
353 JOptionPane.showMessageDialog(null, msg);
357 System.out.println(msg);
369 public static void error(String msg) {
371 JOptionPane.showMessageDialog(null, msg);
375 System.err.println(msg);
381 GenericDialog gd =
new GenericDialog(title);
384 return !gd.wasCanceled();
387 public static final int CANCELED = Integer.MIN_VALUE;
392 public static double getNumber(String prompt,
double defaultValue) {
393 GenericDialog gd =
new GenericDialog(
"");
394 int decimalPlaces = (int)defaultValue==defaultValue?0:2;
395 gd.addNumericField(prompt, defaultValue, decimalPlaces);
397 if (gd.wasCanceled())
399 double v = gd.getNextNumber();
400 if (gd.invalidNumber())
408 public static String
getString(String prompt, String defaultString) {
409 GenericDialog gd =
new GenericDialog(
"");
410 gd.addStringField(prompt, defaultString, 20);
412 if (gd.wasCanceled())
414 return gd.getNextString();
418 public synchronized static void wait(
int msecs) {
419 try {Thread.sleep(msecs);}
420 catch (InterruptedException e) { }
425 java.awt.Toolkit.getDefaultToolkit().beep();
428 public static String freeMemory() {
430 long freeMem = Runtime.getRuntime().freeMemory();
431 long totMem = Runtime.getRuntime().totalMemory();
432 long inUse = (int)(totMem-freeMem);
433 String inUseStr = inUse<10000*1024?inUse/1024L+
"K":inUse/1048576L+
"MB";
437 double percent = inUse*100/max;
438 maxStr =
" of "+max/1048576L+
"MB ("+(percent<1.0?
"<1":
d2s(percent,0)) +
"%)";
440 return inUseStr + maxStr;
447 Memory mem =
new Memory();
454 public static void showTime(
ImagePlus imp,
long start, String str) {
455 showTime(imp, start, str, 1);
458 static void showTime(ImagePlus imp,
long start, String str,
int nslices) {
459 long elapsedTime = System.currentTimeMillis() - start;
460 double seconds = elapsedTime / 1000.0;
461 long pixels = imp.getWidth() * imp.getHeight();
462 int rate = (int)((
double)pixels*nslices/seconds);
466 else if (rate<1000000)
467 str2 =
", "+rate+
" pixels/second";
469 str2 =
", "+
d2s(rate/1000000.0,1)+
" million pixels/second";
475 public static String
d2s(
double n) {
479 private static DecimalFormat df =
480 new DecimalFormat(
"0.00",
new DecimalFormatSymbols(Locale.US));
481 private static int dfDigits = 2;
486 public static String
d2s(
double n,
int decimalPlaces) {
487 if (n==Float.MAX_VALUE)
489 boolean negative = n<0.0;
492 double whole = Math.round(n * Math.pow(10, decimalPlaces));
493 double rounded = whole/Math.pow(10, decimalPlaces);
496 if (decimalPlaces!=dfDigits)
497 switch (decimalPlaces) {
498 case 0: df.applyPattern(
"0"); dfDigits=0;
break;
499 case 1: df.applyPattern(
"0.0"); dfDigits=1;
break;
500 case 2: df.applyPattern(
"0.00"); dfDigits=2;
break;
501 case 3: df.applyPattern(
"0.000"); dfDigits=3;
break;
502 case 4: df.applyPattern(
"0.0000"); dfDigits=4;
break;
503 case 5: df.applyPattern(
"0.00000"); dfDigits=5;
break;
504 case 6: df.applyPattern(
"0.000000"); dfDigits=6;
break;
505 case 7: df.applyPattern(
"0.0000000"); dfDigits=7;
break;
506 case 8: df.applyPattern(
"0.00000000"); dfDigits=8;
break;
508 String s = df.format(rounded);
514 public static void register(Class c) {
515 if (ij!=null) ij.register(c);
533 public static void setKeyDown(
int key) {
535 case KeyEvent.VK_ALT:
538 case KeyEvent.VK_SHIFT:
544 public static void setKeyUp(
int key) {
546 case KeyEvent.VK_ALT: altDown=
false;
break;
547 case KeyEvent.VK_SHIFT: shiftDown=
false;
break;
551 public static void setInputEvent(InputEvent e) {
552 altDown = e.isAltDown();
553 shiftDown = e.isShiftDown();
584 boolean lessThan =
ImageJ.VERSION.compareTo(version)<0;
586 error(
"This plugin or macro requires ImageJ "+version+
" or later.");
602 if (width<=0 || height<0)
610 public static void makeOval(
int x,
int y,
int width,
int height) {
611 if (width<=0 || height<0)
618 public static void makeLine(
int x1,
int y1,
int x2,
int y2) {
639 public static void setThreshold(
double lowerThreshold,
double upperThresold) {
641 img.
getProcessor().setThreshold(lowerThreshold,upperThresold,ImageProcessor.RED_LUT);
663 setColor(red, green, blue,
true);
668 setColor(red, green, blue,
false);
671 static void setColor(
int red,
int green,
int blue,
boolean foreground) {
672 if (red<0) red=0;
if (green<0) green=0;
if (blue<0) blue=0;
673 if (red>255) red=255;
if (green>255) green=255;
if (blue>255) blue=255;
674 Color c =
new Color(red, green, blue);
676 Toolbar.setForegroundColor(c);
681 Toolbar.setBackgroundColor(c);
687 Toolbar.getInstance().setTool(
id);
695 Wand w =
new Wand(ip);
696 double t1 = ip.getMinThreshold();
697 if (t1==ip.NO_THRESHOLD)
700 w.autoOutline(x, y, (
int)t1, (
int)ip.getMaxThreshold());
702 Roi roi =
new PolygonRoi(w.xpoints, w.ypoints, w.npoints, Roi.TRACED_ROI);
713 mode = mode.toLowerCase(Locale.US);
714 int m = Blitter.COPY;
715 if (mode.startsWith(
"ble") || mode.startsWith(
"ave"))
717 else if (mode.startsWith(
"diff"))
718 m = Blitter.DIFFERENCE;
719 else if (mode.startsWith(
"tran"))
720 m = Blitter.COPY_TRANSPARENT;
721 else if (mode.startsWith(
"and"))
723 else if (mode.startsWith(
"or"))
725 else if (mode.startsWith(
"xor"))
727 else if (mode.startsWith(
"sub"))
728 m = Blitter.SUBTRACT;
729 else if (mode.startsWith(
"add"))
731 else if (mode.startsWith(
"div"))
733 else if (mode.startsWith(
"mul"))
734 m = Blitter.MULTIPLY;
765 static void abort() {
766 throw new RuntimeException(
"Macro canceled");