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
|
package ij;
import ij.process.*;
import ij.gui.*;
import ij.io.*;
import ij.measure.*;
import ij.plugin.filter.*;
import ij.macro.Interpreter;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Locale;
import java.util.Hashtable;
/** The class contains static methods that perform macro operations. */
public class Macro {
public static final String MACRO_CANCELED = "Macro canceled";
// A table of Thread as keys and String as values, so
// Macro options are local to each calling thread.
static private Hashtable table = new Hashtable();
static boolean abort;
public static boolean open(String path) {
if (path==null || path.equals("")) {
Opener o = new Opener();
return true;
}
Opener o = new Opener();
ImagePlus img = o.openImage(path);
if (img==null)
return false;
img.show();
return true;
}
public static boolean saveAs(String path) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null)
return false;
FileSaver fs = new FileSaver(imp);
if (path==null || path.equals(""))
return fs.saveAsTiff();
if (imp.getStackSize()>1)
return fs.saveAsTiffStack(path);
else
return fs.saveAsTiff(path);
}
public static String getName(String path) {
int i = path.lastIndexOf('/');
if (i==-1)
i = path.lastIndexOf('\\');
if (i>0)
return path.substring(i+1);
else
return path;
}
public static String getDir(String path) {
int i = path.lastIndexOf('/');
if (i==-1)
i = path.lastIndexOf('\\');
if (i>0)
return path.substring(0, i+1);
else
return "";
}
/** Aborts the currently running macro or any plugin using IJ.run(). */
public static void abort() {
abort = true;
//IJ.log("Abort: "+Thread.currentThread().getName());
if (Thread.currentThread().getName().endsWith("Macro$")) {
table.remove(Thread.currentThread());
throw new RuntimeException(MACRO_CANCELED);
}
}
/** If a command started using run(name, options) is running,
and the current thread is the same thread,
returns the options string, otherwise, returns null.
@see ij.gui.GenericDialog
@see ij.io.OpenDialog
*/
public static String getOptions() {
//IJ.log("getOptions: "+Thread.currentThread().hashCode()); //ts
if (Thread.currentThread().getName().startsWith("Run$_")) {
Object options = table.get(Thread.currentThread());
return options==null?null:options+" ";
} else
return null;
}
/** Define a set of Macro options for the current Thread. */
public static void setOptions(String options) {
//IJ.log("setOptions: "+Thread.currentThread().hashCode()+" "+options); //ts
if (options==null || options.equals(""))
table.remove(Thread.currentThread());
else
table.put(Thread.currentThread(), options);
}
/** Define a set of Macro options for a Thread. */
public static void setOptions(Thread thread, String options) {
if (null==thread)
throw new RuntimeException("Need a non-null thread instance");
if (null==options)
table.remove(thread);
else
table.put(thread, options);
}
public static String getValue(String options, String key, String defaultValue) {
key = trimKey(key);
key += '=';
int index=-1;
do { // Require that key not be preceded by a letter
index = options.indexOf(key, ++index);
if (index<0) return defaultValue;
} while (index!=0&&Character.isLetter(options.charAt(index-1)));
options = options.substring(index+key.length(), options.length());
if (options.charAt(0)=='\'') {
index = options.indexOf("'",1);
if (index<0)
return defaultValue;
else
return options.substring(1, index);
} else if (options.charAt(0)=='[') {
index = options.indexOf("]",1);
if (index<0)
return defaultValue;
else
return options.substring(1, index);
} else {
//if (options.indexOf('=')==-1) {
// options = options.trim();
// IJ.log("getValue: "+key+" |"+options+"|");
// if (options.length()>0)
// return options;
// else
// return defaultValue;
//}
index = options.indexOf(" ");
if (index<0)
return defaultValue;
else
return options.substring(0, index);
}
}
public static String trimKey(String key) {
int index = key.indexOf(" ");
if (index>-1)
key = key.substring(0,index);
index = key.indexOf(":");
if (index>-1)
key = key.substring(0,index);
key = key.toLowerCase(Locale.US);
return key;
}
}
|