File: IJ.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 (2570 lines) | stat: -rw-r--r-- 88,196 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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
package ij;
import ij.gui.*;
import ij.process.*;
import ij.text.*;
import ij.io.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import ij.util.Tools;
import ij.plugin.frame.Recorder;
import ij.plugin.frame.ThresholdAdjuster;
import ij.macro.Interpreter;
import ij.macro.MacroRunner;
import ij.measure.Calibration;
import ij.measure.ResultsTable;
import ij.measure.Measurements;
import java.awt.event.*;
import java.text.*;
import java.util.*;	
import java.awt.*;	
import java.applet.Applet;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import javax.net.ssl.*;
import java.security.cert.*;
import java.security.KeyStore;
import java.nio.ByteBuffer;
import java.math.RoundingMode;


/** This class consists of static utility methods. */
public class IJ {

	/** SansSerif, plain, 10-point font */
	public static Font font10 = new Font("SansSerif", Font.PLAIN, 10);
	/** SansSerif, plain, 12-point font */
	public static Font font12 = ImageJ.SansSerif12;
	/** SansSerif, plain, 14-point font */
	public static Font font14 = ImageJ.SansSerif14;
	
	/** Image display modes */
	public static final int COMPOSITE=1, COLOR=2, GRAYSCALE=3;
	
	/** @deprecated */
	public static final String URL = "http://imagej.nih.gov/ij";
	
	/** ImageJ website */
	public static final String URL2 = "http://imagej.net/ij";

	public static final int ALL_KEYS = -1;
	
	/** Use setDebugMode(boolean) to enable/disable debug mode. */
	public static boolean debugMode;
	
	public static boolean hideProcessStackDialog;
	    
    public static final char micronSymbol = '\u00B5';
    public static final char angstromSymbol = '\u00C5';
    public static final char degreeSymbol = '\u00B0';

	private static ImageJ ij;
	private static java.applet.Applet applet;
	private static ProgressBar progressBar;
	private static TextPanel textPanel;
	private static String osname, osarch;
	private static boolean isMac, isWin, isLinux, is64Bit;
	private static int javaVersion;
	private static boolean controlDown, altDown, spaceDown, shiftDown;
	private static boolean macroRunning;
	private static Thread previousThread;
	private static TextPanel logPanel;
	private static ClassLoader classLoader;
	private static boolean memMessageDisplayed;
	private static long maxMemory;
	private static boolean escapePressed;
	private static boolean redirectErrorMessages;
	private static boolean suppressPluginNotFoundError;
	private static Hashtable commandTable;
	private static Vector eventListeners = new Vector();
	private static String lastErrorMessage;
	private static Properties properties;	private static DecimalFormat[] df;
	private static DecimalFormat[] sf;
	private static DecimalFormatSymbols dfs;
	private static boolean trustManagerCreated;
	private static String smoothMacro;
	private static Interpreter macroInterpreter;
	private static boolean protectStatusBar;
	private static Thread statusBarThread;
			
	static {
		osname = System.getProperty("os.name");
		isWin = osname.startsWith("Windows");
		isMac = !isWin && osname.startsWith("Mac");
		isLinux = osname.startsWith("Linux");
		String version = System.getProperty("java.version");
		if (version==null || version.length()<2)
			version = "1.8";
		if (version.startsWith("1.8"))
			javaVersion = 8;
		else if (version.charAt(0)=='1' && Character.isDigit(version.charAt(1)))
			javaVersion = 10 + (version.charAt(1) - '0');
		else if (version.charAt(0)=='2' && Character.isDigit(version.charAt(1)))
			javaVersion = 20 + (version.charAt(1) - '0');
		else if (version.startsWith("1.6"))
			javaVersion = 6;
		else if (version.startsWith("1.9")||version.startsWith("9"))
			javaVersion = 9;
		else if (version.startsWith("1.7"))
			javaVersion = 7;
		else
			javaVersion = 8;
		dfs = new DecimalFormatSymbols(Locale.US);
		df = new DecimalFormat[10];
		df[0] = new DecimalFormat("0", dfs);
		df[1] = new DecimalFormat("0.0", dfs);
		df[2] = new DecimalFormat("0.00", dfs);
		df[3] = new DecimalFormat("0.000", dfs);
		df[4] = new DecimalFormat("0.0000", dfs);
		df[5] = new DecimalFormat("0.00000", dfs);
		df[6] = new DecimalFormat("0.000000", dfs);
		df[7] = new DecimalFormat("0.0000000", dfs);
		df[8] = new DecimalFormat("0.00000000", dfs);
		df[9] = new DecimalFormat("0.000000000", dfs);
		df[0].setRoundingMode(RoundingMode.HALF_UP);
	}
			
	static void init(ImageJ imagej, Applet theApplet) {
		ij = imagej;
		applet = theApplet;
		progressBar = ij.getProgressBar();
	}

	static void cleanup() {
		ij=null; applet=null; progressBar=null; textPanel=null;
	}

	/**Returns a reference to the "ImageJ" frame.*/
	public static ImageJ getInstance() {
		return ij;
	}
	
	/**Enable/disable debug mode.*/
	public static void setDebugMode(boolean b) {
		debugMode = b;
		LogStream.redirectSystem(debugMode);
	}

	/** Runs the macro contained in the string <code>macro</code>
		on the current thread. Returns any string value returned by
		the macro, null if the macro does not return a value, or
		"[aborted]" if the macro was aborted due to an error. The
		equivalent macro function is eval(). */
	public static String runMacro(String macro) {
		return runMacro(macro, "");
	}

	/** Runs the macro contained in the string <code>macro</code>
		on the current thread. The optional string argument can be
		retrieved in the called macro using the getArgument() macro
		function. Returns any string value returned by the macro, null
		if the macro does not return a value, or "[aborted]" if the
		macro was aborted due to an error.  */
	public static String runMacro(String macro, String arg) {
		Macro_Runner mr = new Macro_Runner();
		return mr.runMacro(macro, arg);
	}

	/** Runs the specified macro or script file in the current thread.
		The file is assumed to be in the macros folder
 		unless <code>name</code> is a full path.
		The optional string argument (<code>arg</code>) can be retrieved in the called 
		macro or script using the getArgument() function. 
		Returns any string value returned by the macro, or null. Scripts always return null.
		The equivalent macro function is runMacro(). */
	public static String runMacroFile(String name, String arg) {
		Macro_Runner mr = new Macro_Runner();
		return mr.runMacroFile(name, arg);
	}

	/** Runs the specified macro file. */
	public static String runMacroFile(String name) {
		return runMacroFile(name, null);
	}

	/** Runs the specified plugin using the specified image. */
	public static Object runPlugIn(ImagePlus imp, String className, String arg) {
		if (imp!=null) {
			ImagePlus temp = WindowManager.getTempCurrentImage();
			WindowManager.setTempCurrentImage(imp);
			Object o = runPlugIn("", className, arg);
			WindowManager.setTempCurrentImage(temp);
			return o;
		} else
			return runPlugIn(className, arg);
	}

	/** Runs the specified plugin and returns a reference to it. */
	public static Object runPlugIn(String className, String arg) {
		return runPlugIn("", className, arg);
	}
	
	/** Runs the specified plugin and returns a reference to it. */
	public static Object runPlugIn(String commandName, String className, String arg) {
		if (arg==null) arg = "";
		if (IJ.debugMode)
			IJ.log("runPlugIn: "+className+argument(arg));
		// Load using custom classloader if this is a user 
		// plugin and we are not running as an applet
		if (!className.startsWith("ij.") && applet==null)
			return runUserPlugIn(commandName, className, arg, false);
		Object thePlugIn=null;
		try {
			Class c = Class.forName(className);
 			thePlugIn = c.newInstance();
 			if (thePlugIn instanceof PlugIn)
				((PlugIn)thePlugIn).run(arg);
 			else
				new PlugInFilterRunner(thePlugIn, commandName, arg);
		} catch (ClassNotFoundException e) {
			if (!(className!=null && className.startsWith("ij.plugin.MacAdapter"))) {
				log("Plugin or class not found: \"" + className + "\"\n(" + e+")");
				String path = Prefs.getCustomPropsPath();
				if (path!=null);
					log("Error may be due to custom properties at " + path);
			}
		}
		catch (InstantiationException e) {log("Unable to load plugin (ins)");}
		catch (IllegalAccessException e) {log("Unable to load plugin, possibly \nbecause it is not public.");}
		redirectErrorMessages = false;
		return thePlugIn;
	}
        
	static Object runUserPlugIn(String commandName, String className, String arg, boolean createNewLoader) {
		if (IJ.debugMode)
			IJ.log("runUserPlugIn: "+className+", arg="+argument(arg));
		if (applet!=null) return null;
		if (createNewLoader)
			classLoader = null;
		ClassLoader loader = getClassLoader();
		Object thePlugIn = null;
		try { 
			thePlugIn = (loader.loadClass(className)).newInstance(); 
			if (thePlugIn instanceof PlugIn)
 				((PlugIn)thePlugIn).run(arg);
 			else if (thePlugIn instanceof PlugInFilter)
				new PlugInFilterRunner(thePlugIn, commandName, arg);
		}
		catch (ClassNotFoundException e) {
			if (className.startsWith("macro:"))
				runMacro(className.substring(6));
			else if (className.contains("_")  && !suppressPluginNotFoundError)
				error("Plugin or class not found: \"" + className + "\"\n(" + e+")");
		}
		catch (NoClassDefFoundError e) {
			int dotIndex = className.indexOf('.');
			if (dotIndex>=0 && className.contains("_")) {
				// rerun plugin after removing folder name
				if (debugMode) IJ.log("runUserPlugIn: rerunning "+className);
				return runUserPlugIn(commandName, className.substring(dotIndex+1), arg, createNewLoader);
			}
			if (className.contains("_") && !suppressPluginNotFoundError)
				error("Run User Plugin", "Class not found while attempting to run \"" + className + "\"\n \n   " + e);
		}
		catch (InstantiationException e) {error("Unable to load plugin (ins)");}
		catch (IllegalAccessException e) {error("Unable to load plugin, possibly \nbecause it is not public.");}
		if (thePlugIn!=null && !"HandleExtraFileTypes".equals(className))
 			redirectErrorMessages = false;
		suppressPluginNotFoundError = false;
		return thePlugIn;
	} 
	
	private static String argument(String arg) {
		return arg!=null && !arg.equals("") && !arg.contains("\n")?"(\""+arg+"\")":"";
	}

	static void wrongType(int capabilities, String cmd) {
		String s = "\""+cmd+"\" requires an image of type:\n \n";
		if ((capabilities&PlugInFilter.DOES_8G)!=0) s +=  "    8-bit grayscale\n";
		if ((capabilities&PlugInFilter.DOES_8C)!=0) s +=  "    8-bit color\n";
		if ((capabilities&PlugInFilter.DOES_16)!=0) s +=  "    16-bit grayscale\n";
		if ((capabilities&PlugInFilter.DOES_32)!=0) s +=  "    32-bit (float) grayscale\n";
		if ((capabilities&PlugInFilter.DOES_RGB)!=0) s += "    RGB color\n";
		error(s);
	}
	
    /** Runs a menu command on a separete thread and returns immediately. */
	public static void doCommand(String command) {
		new Executer(command, null);
	}
	
    /** Runs a menu command on a separete thread, using the specified image. */
	public static void doCommand(ImagePlus imp, String command) {
		new Executer(command, imp);
	}
	
    /** Runs an ImageJ command. Does not return until 
    	the command has finished executing. To avoid "image locked",
    	errors, plugins that call this method should implement
    	the PlugIn interface instead of PlugInFilter. */
	public static void run(String command) {
		run(command, null);
	}
	
    /** Runs an ImageJ command, with options that are passed to the
		GenericDialog and OpenDialog classes. Does not return until
		the command has finished executing. To generate run() calls,
		start the recorder (Plugins/Macro/Record) and run commands
		from the ImageJ menu bar.
	*/
	public static void run(String command, String options) {
		//IJ.log("run1: "+command+" "+Thread.currentThread().hashCode()+" "+options);
		if (ij==null && Menus.getCommands()==null)
			init();
		Macro.abort = false;
		Macro.setOptions(options);
		Thread thread = Thread.currentThread();
		if (previousThread==null || thread!=previousThread) {
			String name = thread.getName();
			if (!name.startsWith("Run$_"))
				thread.setName("Run$_"+name);
		}
		command = convert(command);
		previousThread = thread;
		macroRunning = true;
		Executer e = new Executer(command);
		e.run();
		macroRunning = false;
		Macro.setOptions(null);
		testAbort();
		macroInterpreter = null;
		//IJ.log("run2: "+command+" "+Thread.currentThread().hashCode());
	}
	
	/** The macro interpreter uses this method to run commands. */
	public static void run(Interpreter interpreter, String command, String options) {
		macroInterpreter = interpreter;
		run(command, options);
		macroInterpreter = null;
	}

	/** Converts commands that have been renamed so 
		macros using the old names continue to work. */
	private static String convert(String command) {
		if (commandTable==null) {
			commandTable = new Hashtable(30);
			commandTable.put("New...", "Image...");
			commandTable.put("Threshold", "Make Binary");
			commandTable.put("Display...", "Appearance...");
			commandTable.put("Start Animation", "Start Animation [\\]");
			commandTable.put("Convert Images to Stack", "Images to Stack");
			commandTable.put("Convert Stack to Images", "Stack to Images");
			commandTable.put("Convert Stack to RGB", "Stack to RGB");
			commandTable.put("Convert to Composite", "Make Composite");
			commandTable.put("RGB Split", "Split Channels");
			commandTable.put("RGB Merge...", "Merge Channels...");
			commandTable.put("Channels...", "Channels Tool...");
			commandTable.put("New... ", "Table...");
			commandTable.put("Arbitrarily...", "Rotate... ");
			commandTable.put("Measurements...", "Results... ");
			commandTable.put("List Commands...", "Find Commands...");
			commandTable.put("Capture Screen ", "Capture Screen");
			commandTable.put("Add to Manager ", "Add to Manager");
			commandTable.put("In", "In [+]");
			commandTable.put("Out", "Out [-]");
			commandTable.put("Enhance Contrast", "Enhance Contrast...");
			commandTable.put("XY Coodinates... ", "XY Coordinates... ");
			commandTable.put("Statistics...", "Statistics");
			commandTable.put("Channels Tool... ", "Channels Tool...");
			commandTable.put("Profile Plot Options...", "Plots...");
			commandTable.put("AuPbSn 40 (56K)", "AuPbSn 40");
			commandTable.put("Bat Cochlea Volume (19K)", "Bat Cochlea Volume");
			commandTable.put("Bat Cochlea Renderings (449K)", "Bat Cochlea Renderings");
			commandTable.put("Blobs (25K)", "Blobs");
			commandTable.put("Boats (356K)", "Boats");			
			commandTable.put("Cardio (768K, RGB DICOM)", "Cardio (RGB DICOM)");
			commandTable.put("Cell Colony (31K)", "Cell Colony");
			commandTable.put("Clown (14K)", "Clown");
			commandTable.put("Confocal Series (2.2MB)", "Confocal Series");
			commandTable.put("CT (420K, 16-bit DICOM)", "CT (16-bit DICOM)");			
			commandTable.put("Dot Blot (7K)", "Dot Blot");
			commandTable.put("Embryos (42K)", "Embryos");
			commandTable.put("Fluorescent Cells (400K)", "Fluorescent Cells");
			commandTable.put("Fly Brain (1MB)", "Fly Brain");			
			commandTable.put("Gel (105K)", "Gel");
			commandTable.put("HeLa Cells (1.3M, 48-bit RGB)", "HeLa Cells (48-bit RGB)");
			commandTable.put("Leaf (36K)", "Leaf");
			commandTable.put("Line Graph (21K)", "Line Graph");			
			commandTable.put("Mitosis (26MB, 5D stack)", "Mitosis (5D stack)");
			commandTable.put("MRI Stack (528K)", "MRI Stack");
			commandTable.put("M51 Galaxy (177K, 16-bits)", "M51 Galaxy (16-bits)");
			commandTable.put("Neuron (1.6M, 5 channels)", "Neuron (5 channels)");
			commandTable.put("Nile Bend (1.9M)", "Nile Bend");			
			commandTable.put("Organ of Corti (2.8M, 4D stack)", "Organ of Corti (4D stack)");
			commandTable.put("Particles (75K)", "Particles");
			commandTable.put("T1 Head (2.4M, 16-bits)", "T1 Head (16-bits)");
			commandTable.put("T1 Head Renderings (736K)", "T1 Head Renderings");
			commandTable.put("TEM Filter (112K)", "TEM Filter");
			commandTable.put("Tree Rings (48K)", "Tree Rings");
		}
		String command2 = (String)commandTable.get(command);
		if (command2!=null)
			return command2;
		else
			return command;
	}

	/** Runs an ImageJ command using the specified image and options.
		To generate run() calls, start the recorder (Plugins/Macro/Record)
		and run commands from the ImageJ menu bar.*/
	public static void run(ImagePlus imp, String command, String options) {
		if (ij==null && Menus.getCommands()==null)
			init();
		if (imp!=null) {
			ImagePlus temp = WindowManager.getTempCurrentImage();
			WindowManager.setTempCurrentImage(imp);
			run(command, options);
			WindowManager.setTempCurrentImage(temp);
		} else
			run(command, options);
	}

	static void init() {
		Menus m = new Menus(null, null);
		Prefs.load(m, null);
		m.addMenuBar();
	}

	private static void testAbort() {
		if (Macro.abort)
			abort();
	}

	/** Returns true if the run(), open() or newImage() method is executing. */
	public static boolean macroRunning() {
		return macroRunning;
	}

	/** Returns true if a macro is running, or if the run(), open() 
		or newImage() method is executing. */
	public static boolean isMacro() {
		return macroRunning || Interpreter.getInstance()!=null;
	}

	/**Returns the Applet that created this ImageJ or null if running as an application.*/
	public static java.applet.Applet getApplet() {
		return applet;
	}
	
	/**Displays a message in the ImageJ status bar. If 's' starts 
		with '!', subsequent showStatus() calls in the current
		thread (without "!" in the message) are suppressed. */
	public static void showStatus(String s) {
		if ((Interpreter.getInstance()==null&&statusBarThread==null)
		|| (statusBarThread!=null&&Thread.currentThread()!=statusBarThread))
			protectStatusBar(false);
		boolean doProtect = s.startsWith("!"); // suppress subsequent showStatus() calls
		if (doProtect) {
			protectStatusBar(true);
			statusBarThread = Thread.currentThread();
			s = s.substring(1);
		}
		if (doProtect || !protectStatusBar) {
			if (ij!=null)
				ij.showStatus(s);
			ImagePlus imp = WindowManager.getCurrentImage();
			ImageCanvas ic = imp!=null?imp.getCanvas():null;
			if (ic!=null)
				ic.setShowCursorStatus(s.length()==0?true:false);
		}
	}
	
	/**Displays a message in the status bar and flashes
	 * either the status bar or the active image.<br>
	 * See: http://wsr.imagej.net/macros/FlashingStatusMessages.txt
	*/ 
	public static void showStatus(String message, String options) {
		showStatus(message);
		if (options==null)
			return;
		options = options.replace("flash", "");
		options = options.replace("ms", "");
		Color optionalColor = null;
		int index1 = options.indexOf("#");
		if (index1>=0) {  // hex color?
			int index2 = options.indexOf(" ", index1);
			if (index2==-1) index2 = options.length();
			String hexColor = options.substring(index1, index2);
			optionalColor = Colors.decode(hexColor, null);
			options = options.replace(hexColor, "");
		}
		if (optionalColor==null) {  // "red", "green", etc.
			for (String c : Colors.colors) {
				if (options.contains(c)) {
					optionalColor = Colors.getColor(c, ImageJ.backgroundColor);
					options = options.replace(c, "");
					break;
				}
			}
		}
		boolean flashImage = options.contains("image");
		Color defaultColor = new Color(255,255,245);
		int defaultDelay = 500;
		ImagePlus imp = WindowManager.getCurrentImage();
		if (flashImage) {
			options = options.replace("image", "");
			if (imp!=null && imp.getWindow()!=null) {
				defaultColor = Color.black;
				defaultDelay = 100;
			}
			else
				flashImage = false;
		}
		Color color = optionalColor!=null?optionalColor:defaultColor;
		int delay = (int)Tools.parseDouble(options, defaultDelay);
		if (delay>8000)
			delay = 8000;
		String colorString = null;
		ImageJ ij = IJ.getInstance();
		if (flashImage) {
			Color previousColor = imp.getWindow().getBackground();
			imp.getWindow().setBackground(color);
			if (delay>0) {
				wait(delay);			
				imp.getWindow().setBackground(previousColor);
			}
		} else if (ij!=null) {
			ij.getStatusBar().setBackground(color);
			wait(delay);
			ij.getStatusBar().setBackground(ij.backgroundColor);
		}
	}
	
	/**
	* @deprecated
	* replaced by IJ.log(), ResultsTable.setResult() and TextWindow.append().
	* There are examples at
	*   http://imagej.nih.gov/ij/plugins/sine-cosine.html
	*/
	public static void write(String s) {
		if (textPanel==null && ij!=null)
			showResults();
		if (textPanel!=null)
				textPanel.append(s);
		else
			System.out.println(s);
	}

	private static void showResults() {
		TextWindow resultsWindow = new TextWindow("Results", "", 400, 250);
		textPanel = resultsWindow.getTextPanel();
		textPanel.setResultsTable(Analyzer.getResultsTable());
	}

	public static synchronized void log(String s) {
		if (s==null) return;
		if (logPanel==null && ij!=null) {
			TextWindow logWindow = new TextWindow("Log", "", 400, 250);
			logPanel = logWindow.getTextPanel();
			logPanel.setFont(new Font("SansSerif", Font.PLAIN, 16));
		}
		if (logPanel!=null) {
			if (s.startsWith("\\"))
				handleLogCommand(s);
			else {
				if (s.endsWith("\n")) {
					if (s.equals("\n\n"))
						s= "\n \n ";
					else if (s.endsWith("\n\n"))
						s = s.substring(0, s.length()-2)+"\n \n ";
					else
						s = s+" ";
				}
				logPanel.append(s);
			}
		} else {
			LogStream.redirectSystem(false);
			System.out.println(s);
		}
	}

	static void handleLogCommand(String s) {
		if (s.equals("\\Closed"))
			logPanel = null;
		else if (s.startsWith("\\Update:")) {
			int n = logPanel.getLineCount();
			String s2 = s.substring(8, s.length());
			if (n==0)
				logPanel.append(s2);
			else
				logPanel.setLine(n-1, s2);
		} else if (s.startsWith("\\Update")) {
			int cindex = s.indexOf(":");
			if (cindex==-1)
				{logPanel.append(s); return;}
			String nstr = s.substring(7, cindex);
			int line = (int)Tools.parseDouble(nstr, -1);
			if (line<0 || line>25)
				{logPanel.append(s); return;}
			int count = logPanel.getLineCount();
			while (line>=count) {
				log("");
				count++;
			}
			String s2 = s.substring(cindex+1, s.length());
			logPanel.setLine(line, s2);
		} else if (s.equals("\\Clear")) {
			logPanel.clear();
		} else if (s.startsWith("\\Heading:")) {
			logPanel.updateColumnHeadings(s.substring(10));
		} else if (s.equals("\\Close")) {
			Frame f = WindowManager.getFrame("Log");
			if (f!=null && (f instanceof TextWindow))
				((TextWindow)f).close();
		} else
			logPanel.append(s);
	}
	
	/** Returns the contents of the Log window or null if the Log window is not open. */
	public static synchronized String getLog() { 
		if (logPanel==null || ij==null)
			return null;
		else
			return logPanel.getText(); 
	} 

/** Clears the "Results" window and sets the column headings to
		those in the tab-delimited 'headings' String. Writes to
		System.out.println if the "ImageJ" frame is not present.*/
	public static void setColumnHeadings(String headings) {
		if (textPanel==null && ij!=null)
			showResults();
		if (textPanel!=null)
			textPanel.setColumnHeadings(headings);
		else
			System.out.println(headings);
	}

	/** Returns true if the "Results" window is open. */
	public static boolean isResultsWindow() {
		return textPanel!=null;
	}
	
	/** Renames a results window. */
	public static void renameResults(String title) {
		Frame frame = WindowManager.getFrontWindow();
		if (frame!=null && (frame instanceof TextWindow)) {
			TextWindow tw = (TextWindow)frame;
			if (tw.getResultsTable()==null) {
				IJ.error("Rename", "\""+tw.getTitle()+"\" is not a results table");
				return;
			}
			tw.rename(title);
		} else if (isResultsWindow()) {
			TextPanel tp = getTextPanel();
			TextWindow tw = (TextWindow)tp.getParent();
			tw.rename(title);
		}
	}

	/** Changes the name of a table window from 'oldTitle' to 'newTitle'. */
	public static void renameResults(String oldTitle, String newTitle) {
		Frame frame = WindowManager.getFrame(oldTitle);
		if (frame==null) {
			error("Rename", "\""+oldTitle+"\" not found");
			return;
		} else if (frame instanceof TextWindow) {
			TextWindow tw = (TextWindow)frame;
			if (tw.getResultsTable()==null) {
				error("Rename", "\""+oldTitle+"\" is not a table");
				return;
			}
			tw.rename(newTitle);
		} else
			error("Rename", "\""+oldTitle+"\" is not a table");
	}

	/** Deletes 'row1' through 'row2' of the "Results" window, where
		'row1' and 'row2' must be in the range 0-Analyzer.getCounter()-1. */
	public static void deleteRows(int row1, int row2) {
		ResultsTable rt = Analyzer.getResultsTable();
		int tableSize = rt.size();
		rt.deleteRows(row1, row2);
		ImagePlus imp = WindowManager.getCurrentImage();
		if (imp!=null)
			Overlay.updateTableOverlay(imp, row1, row2, tableSize);
		rt.show("Results");
	}
	
	/** Returns a measurement result, where 'measurement' is "Area", 
	 * "Mean", "StdDev", "Mode", "Min", "Max", "X", "Y", "XM", "YM",
	 * "Perim.", "BX", "BY", "Width", "Height", "Major", "Minor", "Angle",
	 * "Circ.", "Feret", "IntDen", "Median", "Skew", "Kurt", "%Area",
	 * "RawIntDen", "Ch", "Slice", "Frame", "FeretX", "FeretY",
	 * "FeretAngle", "MinFeret", "AR", "Round", "Solidity", "MinThr"
	 * or "MaxThr". Add " raw" to the argument to disable calibration,
	 * for example IJ.getValue("Mean raw"). Add " limit" to enable
	 * the "limit to threshold" option.
	*/
	public static double getValue(ImagePlus imp, String measurement) {
		String options = "";
		int index = measurement.indexOf(" ");
		if (index>0) {
			if (index<measurement.length()-1)
				options = measurement.substring(index+1, measurement.length());
			measurement = measurement.substring(0, index);
		}
		int measurements = Measurements.ALL_STATS + Measurements.SLICE;
		if (options.contains("limit"))
			measurements += Measurements.LIMIT;
		Calibration cal = null;
		if (options.contains("raw")) {
			cal = imp.getCalibration();
			imp.setCalibration(null);
		}
		ResultsTable rt = new ResultsTable();
		Analyzer analyzer = new Analyzer(imp, measurements, rt);
		analyzer.measure();
		double value = Double.NaN;
		try {
			value = rt.getValue(measurement, 0);
		} catch (Exception e) {};
		if (cal!=null)
			imp.setCalibration(cal);
		return value;
	}

	/** Returns a reference to the "Results" window TextPanel.
		Opens the "Results" window if it is currently not open.
		Returns null if the "ImageJ" window is not open. */
	public static TextPanel getTextPanel() {
		if (textPanel==null && ij!=null)
			showResults();
		return textPanel;
	}
	
	/** TextWindow calls this method with a null argument when the "Results" window is closed. */
	public static void setTextPanel(TextPanel tp) {
		textPanel = tp;
	}
    
    /**Displays a "no images are open" dialog box.*/
	public static void noImage() {
		String msg = "There are no images open";
		if (macroInterpreter!=null) {
			macroInterpreter.abort(msg);
			macroInterpreter = null;
		} else
			error("No Image", msg);
	}

	/** Displays an "out of memory" message to the "Log" window. */
	public static void outOfMemory(String name) {
		Undo.reset();
		System.gc();
		lastErrorMessage = "out of memory";
		String tot = Runtime.getRuntime().maxMemory()/1048576L+"MB";
		if (!memMessageDisplayed)
			log(">>>>>>>>>>>>>>>>>>>>>>>>>>>");
		log("<Out of memory>");
		if (!memMessageDisplayed) {
			log("<All available memory ("+tot+") has been>");
			log("<used. To make more available, use the>");
			log("<Edit>Options>Memory & Threads command.>");
			log(">>>>>>>>>>>>>>>>>>>>>>>>>>>");
			memMessageDisplayed = true;
		}
		Macro.abort();
	}

	/**	Updates the progress bar, where 0<=progress<=1.0. The progress bar is 
	not shown in BatchMode and erased if progress>=1.0. The progress bar is
    updated only if more than 90 ms have passes since the last call. Does nothing 
	if the ImageJ window is not present. */
	public static void showProgress(double progress) {
		if (progressBar!=null) progressBar.show(progress, false);
	}

	/**	Updates the progress bar, where the length of the bar is set to
     * (<code>currentValue+1)/finalValue</code> of the maximum bar length.
     * The bar is erased if <code>currentValue&gt;=finalValue</code>. 
     * The bar is updated only if more than 90 ms have passed since the
     * last call. Displays subordinate progress bars as dots if
     * 'currentIndex' is negative (example: Plugins/Utilities/Benchmark).
    */
    public static void showProgress(int currentIndex, int finalIndex) {
		if (progressBar!=null) {
			progressBar.show(currentIndex, finalIndex);
			if (currentIndex==finalIndex)
				progressBar.setBatchMode(false);
		}
	}

	/** Displays a message in a dialog box titled "Message".
		Writes the Java console if ImageJ is not present. */
	public static void showMessage(String msg) {
		showMessage("Message", msg);
	}

	/** Displays a message in a dialog box with the specified title.
		Displays HTML formatted text if 'msg' starts with "<html>".
		There are examples at
		"http://imagej.nih.gov/ij/macros/HtmlDialogDemo.txt".
		Writes to the Java console if ImageJ is not present. */
	public static void showMessage(String title, String msg) {
		if (ij!=null) {
			if (msg!=null && (msg.startsWith("<html>")||msg.startsWith("<HTML>"))) {
				HTMLDialog hd = new HTMLDialog(title, msg);
				if (isMacro() && hd.escapePressed())
					throw new RuntimeException(Macro.MACRO_CANCELED);
			} else {
				MessageDialog md = new MessageDialog(ij, title, msg); 
				if (isMacro() && md.escapePressed())
					throw new RuntimeException(Macro.MACRO_CANCELED);
			}
		} else
			System.out.println(msg);
	}

	/** Displays a message in a dialog box titled "ImageJ". If a 
		macro or JavaScript is running, it is aborted. Writes to the
		Java console if the ImageJ window is not present.*/
	public static void error(String msg) {
		error(null, msg);
		if (Thread.currentThread().getName().endsWith("JavaScript"))
			throw new RuntimeException(Macro.MACRO_CANCELED);
		else
			Macro.abort();
	}
	
	/** Displays a message in a dialog box with the specified title. If a 
		macro or JavaScript is running, it is aborted. Writes to the
		Java console if the ImageJ window is not present. */
	public static void error(String title, String msg) {
		if (macroInterpreter!=null) {
			macroInterpreter.abort(msg);
			macroInterpreter = null;
			return;
		}
		if (msg!=null && msg.endsWith(Macro.MACRO_CANCELED))
			return;
		String title2 = title!=null?title:"ImageJ";
		boolean abortMacro = title!=null;
		lastErrorMessage = msg;
		if (redirectErrorMessages) {
			IJ.log(title2 + ": " + msg);
			if (abortMacro && (title.contains("Open")||title.contains("Reader")))
				abortMacro = false;
		} else
			showMessage(title2, msg);
		redirectErrorMessages = false;
		if (abortMacro)
			Macro.abort();
	}

	/** Aborts any currently running JavaScript, or use IJ.error(string)
		to abort a JavaScript with a message. */
	public static void exit() {
		if (Thread.currentThread().getName().endsWith("JavaScript"))
			throw new RuntimeException(Macro.MACRO_CANCELED);
	}

	/** 
	 * Returns the last error message written by IJ.error() or null if there
	 * was no error since the last time this method was called.
	 * @see #error(String)
	 */
	public static String getErrorMessage() {
		String msg = lastErrorMessage;
		lastErrorMessage = null;
		return msg;
	}

	/** Displays a message in a dialog box with the specified title.
		Returns false if the user pressed "Cancel". */
	public static boolean showMessageWithCancel(String title, String msg) {
		GenericDialog gd = new GenericDialog(title);
		gd.addMessage(msg);
		gd.showDialog();
		return !gd.wasCanceled();
	}

	public static final int CANCELED = Integer.MIN_VALUE;

	/** Allows the user to enter a number in a dialog box. Returns the	
	    value IJ.CANCELED (-2,147,483,648) if the user cancels the dialog box. 
	    Returns 'defaultValue' if the user enters an invalid number. */
	public static double getNumber(String prompt, double defaultValue) {
		GenericDialog gd = new GenericDialog("");
		int decimalPlaces = (int)defaultValue==defaultValue?0:2;
		gd.addNumericField(prompt, defaultValue, decimalPlaces);
		gd.showDialog();
		if (gd.wasCanceled())
			return CANCELED;
		double v = gd.getNextNumber();
		if (gd.invalidNumber())
			return defaultValue;
		else
			return v;
	}

	/** Allows the user to enter a string in a dialog box. Returns
	    "" if the user cancels the dialog box. */
	public static String getString(String prompt, String defaultString) {
		GenericDialog gd = new GenericDialog("");
		gd.addStringField(prompt, defaultString, 20);
		gd.showDialog();
		if (gd.wasCanceled())
			return "";
		return gd.getNextString();
	}

	/**Delays 'msecs' milliseconds.*/
	public static void wait(int msecs) {
		try {Thread.sleep(msecs);}
		catch (InterruptedException e) { }
	}
	
	/** Emits an audio beep. */
	public static void beep() {
		java.awt.Toolkit.getDefaultToolkit().beep();
	}
	
	/**	Returns a string something like "64K of 256MB (25%)"
	 * that shows how much of  the  available memory is in use.
	 * This is the string displayed when the user clicks in the
	 * status bar.
	*/
	public static String freeMemory() {
		long inUse = currentMemory();
		String inUseStr = inUse<10000*1024?inUse/1024L+"K":inUse/1048576L+"MB";
		String maxStr="";
		long max = maxMemory();
		if (max>0L) {
			double percent = inUse*100/max;
			maxStr = " of "+max/1048576L+"MB ("+(percent<1.0?"<1":d2s(percent,0)) + "%)";
		}
		return  inUseStr + maxStr;
	}
	
	/** Returns the amount of memory currently being used by ImageJ. */
	public static long currentMemory() {
		long freeMem = Runtime.getRuntime().freeMemory();
		long totMem = Runtime.getRuntime().totalMemory();
		return totMem-freeMem;
	}

	/** Returns the maximum amount of memory available to ImageJ or
		zero if ImageJ is unable to determine this limit. */
	public static long maxMemory() {
		if (maxMemory==0L) {
			Memory mem = new Memory();
			maxMemory = mem.getMemorySetting();
			if (maxMemory==0L) maxMemory = mem.maxMemory();
		}
		return maxMemory;
	}
	
	public static void showTime(ImagePlus imp, long start, String str) {
		showTime(imp, start, str, 1);
	}
	
	public static void showTime(ImagePlus imp, long start, String str, int nslices) {
		if (Interpreter.isBatchMode())
			return;
		double seconds = (System.currentTimeMillis()-start)/1000.0;
		if (seconds<=0.5 && macroRunning())
			return;
		double pixels = (double)imp.getWidth() * imp.getHeight();
		double rate = pixels*nslices/seconds;
		String str2;
		if (rate>1000000000.0)
			str2 = "";
		else if (rate<1000000.0)
			str2 = ", "+d2s(rate,0)+" pixels/second";
		else
			str2 = ", "+d2s(rate/1000000.0,1)+" million pixels/second";
		showStatus(str+seconds+" seconds"+str2);
	}
	
	/** Experimental */
	public static  String time(ImagePlus imp, long startNanoTime) {
		double planes = imp.getStackSize();
		double seconds = (System.nanoTime()-startNanoTime)/1000000000.0;
		double mpixels = imp.getWidth()*imp.getHeight()*planes/1000000.0;
		String time = seconds<1.0?d2s(seconds*1000.0,0)+" ms":d2s(seconds,1)+" seconds";
		return time+", "+d2s(mpixels/seconds,1)+" million pixels/second";
	}
		
	/** Converts a number to a formatted string using
		2 digits to the right of the decimal point. */
	public static String d2s(double n) {
		return d2s(n, 2);
	}
	
	/** Converts a number to a rounded formatted string.
		The 'decimalPlaces' argument specifies the number of
		digits to the right of the decimal point (0-9). Uses
		scientific notation if 'decimalPlaces is negative. */
	public static String d2s(double n, int decimalPlaces) {
		if (Double.isNaN(n)||Double.isInfinite(n))
			return ""+n;
		if (n==Float.MAX_VALUE) // divide by 0 in FloatProcessor
			return "3.4e38";
		double np = n;
		if (n<0.0) np = -n;
		if (decimalPlaces<0) synchronized(IJ.class) {
			decimalPlaces = -decimalPlaces;
			if (decimalPlaces>9) decimalPlaces=9;
			if (sf==null) {
				if (dfs==null)
					dfs = new DecimalFormatSymbols(Locale.US);
				sf = new DecimalFormat[10];
				sf[1] = new DecimalFormat("0.0E0",dfs);
				sf[2] = new DecimalFormat("0.00E0",dfs);
				sf[3] = new DecimalFormat("0.000E0",dfs);
				sf[4] = new DecimalFormat("0.0000E0",dfs);
				sf[5] = new DecimalFormat("0.00000E0",dfs);
				sf[6] = new DecimalFormat("0.000000E0",dfs);
				sf[7] = new DecimalFormat("0.0000000E0",dfs);
				sf[8] = new DecimalFormat("0.00000000E0",dfs);
				sf[9] = new DecimalFormat("0.000000000E0",dfs);
			}
			return sf[decimalPlaces].format(n); // use scientific notation
		}
		if (decimalPlaces<0) decimalPlaces = 0;
		if (decimalPlaces>9) decimalPlaces = 9;
		return df[decimalPlaces].format(n);
	}

    /** Converts a number to a rounded formatted string.
    * The 'significantDigits' argument specifies the minimum number
    * of significant digits, which is also the preferred number of
    * digits behind the decimal. Fewer decimals are shown if the 
    * number would have more than 'maxDigits'.
    * Exponential notation is used if more than 'maxDigits' would be needed.
    */
    public static String d2s(double x, int significantDigits, int maxDigits) {
        double log10 = Math.log10(Math.abs(x));
        double roundErrorAtMax = 0.223*Math.pow(10, -maxDigits);
        int magnitude = (int)Math.ceil(log10+roundErrorAtMax);
        int decimals = x==0 ? 0 : maxDigits - magnitude;
        if (decimals<0 || magnitude<significantDigits+1-maxDigits)
            return IJ.d2s(x, -significantDigits); // exp notation for large and small numbers
        else {
            if (decimals>significantDigits)
                decimals = Math.max(significantDigits, decimals-maxDigits+significantDigits);
            return IJ.d2s(x, decimals);
        }
    }

	/** Pad 'n' with leading zeros to the specified number of digits. */
	public static String pad(int n, int digits) {
		String str = ""+n;
		while (str.length()<digits)
			str = "0"+str;
		return str;
	}

	/** Pad 's' with leading zeros to the specified number of digits. */
	public static String pad(String s, int digits) {
		String str = ""+s;
		while (str.length()<digits)
			str = "0"+str;
		return str;
	}

	/** Obsolete */
	public static void register(Class c) {
		if (ij!=null) ij.register(c);
	}
	
	/** Returns true if the space bar is down. */
	public static boolean spaceBarDown() {
		return spaceDown;
	}

	/** Returns true if the control key is down. */
	public static boolean controlKeyDown() {
		return controlDown;
	}

	/** Returns true if the alt key is down. */
	public static boolean altKeyDown() {
		return altDown;
	}

	/** Returns true if the shift key is down. */
	public static boolean shiftKeyDown() {
		return shiftDown;
	}
	
	/* Sets the specified key "down", where 'key'
	 * is KeyEvent.VK_CONTROL, KeyEvent.VK_META, 
	 * KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,
	 * KeyEvent.VK_SPACE or KeyEvent.VK_ESCAPE.
	*/
	public static void setKeyDown(int key) {
		switch (key) {
			case KeyEvent.VK_CONTROL:
				controlDown=true;
				break;
			case KeyEvent.VK_META:
				if (isMacintosh()) controlDown=true;
				break;
			case KeyEvent.VK_ALT:
				altDown=true;
				updateStatus();
				break;
			case KeyEvent.VK_SHIFT:
				shiftDown=true;
				if (debugMode) beep();
				break;
			case KeyEvent.VK_SPACE: {
				spaceDown=true;
				ImageWindow win = WindowManager.getCurrentWindow();
				//if (win!=null) win.getCanvas().setCursor(-1,-1,-1, -1);
				break;
			}
			case KeyEvent.VK_ESCAPE: {
				escapePressed = true;
				break;
			}
		}
	}

	/* Sets the specified key "up", where 'key'
	 * is KeyEvent.VK_CONTROL, KeyEvent.VK_META, 
	 * KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,
	 * KeyEvent.VK_SPACE or IJ.ALL_KEYS (-1).
	*/
	public static void setKeyUp(int key) {
		switch (key) {
			case KeyEvent.VK_CONTROL: controlDown=false; break;
			case KeyEvent.VK_META: if (isMacintosh()) controlDown=false; break;
			case KeyEvent.VK_ALT: altDown=false; updateStatus(); break;
			case KeyEvent.VK_SHIFT: shiftDown=false; if (debugMode) beep(); break;
			case KeyEvent.VK_SPACE:
				spaceDown=false;
				ImageWindow win = WindowManager.getCurrentWindow();
				//if (win!=null) win.getCanvas().setCursor(-1,-1,-1,-1);
				break;
			case ALL_KEYS:
				shiftDown=controlDown=altDown=spaceDown=false;
				break;
		}
	}
	
	private static void updateStatus() {
		ImagePlus imp = WindowManager.getCurrentImage();
		if (imp!=null) {
			Roi roi = imp.getRoi();
			if (roi!=null && imp.getCalibration().scaled()) {
				roi.showStatus();
			}
		}
	}

	public static void setInputEvent(InputEvent e) {
		altDown = e.isAltDown();
		shiftDown = e.isShiftDown();
	}

	/** Returns true if this machine is a Macintosh. */
	public static boolean isMacintosh() {
		return isMac;
	}
	
	/** Returns true if this machine is a Macintosh running OS X. */
	public static boolean isMacOSX() {
		return isMacintosh();
	}

	/** Returns true if this machine is running Windows. */
	public static boolean isWindows() {
		return isWin;
	}
	
	/** Returns the Java version (6, 7, 8, 9, 10, etc.). */
	public static int javaVersion() {
		return javaVersion;
	}
	
	/** Always returns true. */
	public static boolean isJava2() {
		return true;
	}
	
	/** Always returns true. */
	public static boolean isJava14() {
		return true;
	}

	/** Always returns true. */
	public static boolean isJava15() {
		return true;
	}

	/** Returns true if ImageJ is running on a Java 1.6 or greater JVM. */
	public static boolean isJava16() {
		return javaVersion >= 6;
	}

	/** Returns true if ImageJ is running on a Java 1.7 or greater JVM. */
	public static boolean isJava17() {
		return javaVersion >= 7;
	}

	/** Returns true if ImageJ is running on a Java 1.8 or greater JVM. */
	public static boolean isJava18() {
		return javaVersion >= 8;
	}

	/** Returns true if ImageJ is running on a Java 1.9 or greater JVM. */
	public static boolean isJava19() {
		return javaVersion >= 9;
	}

	/** Returns true if ImageJ is running on Linux. */
	public static boolean isLinux() {
		return isLinux;
	}

	/** Obsolete; always returns false. */
	public static boolean isVista() {
		return false;
	}
	
	/** Returns true if ImageJ is running a 64-bit version of Java. */
	public static boolean is64Bit() {
		if (osarch==null)
			osarch = System.getProperty("os.arch");
		return osarch!=null && osarch.indexOf("64")!=-1;
	}

	/** Displays an error message and returns true if the
		ImageJ version is less than the one specified. */
	public static boolean versionLessThan(String version) {
		boolean lessThan = ImageJ.VERSION.compareTo(version)<0;
		if (lessThan)
			error("This plugin or macro requires ImageJ "+version+" or later. Use\nHelp>Update ImageJ to upgrade to the latest version.");
		return lessThan;
	}
	
	/** Displays a "Process all images?" dialog. Returns
		'flags'+PlugInFilter.DOES_STACKS if the user selects "Yes",
		'flags' if the user selects "No" and PlugInFilter.DONE
		if the user selects "Cancel".
	*/
	public static int setupDialog(ImagePlus imp, int flags) {
		if (imp==null || (ij!=null&&ij.hotkey)) {
			if (ij!=null) ij.hotkey=false;
			return flags;
		}
		int stackSize = imp.getStackSize();
		if (stackSize>1) {
			String macroOptions = Macro.getOptions();
			if (imp.isComposite() && ((CompositeImage)imp).getMode()==IJ.COMPOSITE) {
				if (macroOptions==null || !macroOptions.contains("slice"))
					return flags | PlugInFilter.DOES_STACKS;
			}
			if (macroOptions!=null) {
				if (macroOptions.indexOf("stack ")>=0)
					return flags | PlugInFilter.DOES_STACKS;
				else
					return flags;
			}
			if (hideProcessStackDialog)
				return flags;
			String note = ((flags&PlugInFilter.NO_CHANGES)==0)?" There is\nno Undo if you select \"Yes\".":"";
 			YesNoCancelDialog d = new YesNoCancelDialog(getInstance(),
				"Process Stack?", "Process all "+stackSize+" images?"+note);
			if (d.cancelPressed())
				return PlugInFilter.DONE;
			else if (d.yesPressed()) {
		    	if (imp.getStack().isVirtual() && ((flags&PlugInFilter.NO_CHANGES)==0)) {
		    		int size = (stackSize*imp.getWidth()*imp.getHeight()*imp.getBytesPerPixel()+524288)/1048576;
		    		String msg =
						"Use the Process>Batch>Virtual Stack command\n"+
						"to process a virtual stack or convert it into a\n"+
						"normal stack using Image>Duplicate, which\n"+
						"will require "+size+"MB of additional memory.";
		    		error(msg);
					return PlugInFilter.DONE;
		    	}
				if (Recorder.record)
					Recorder.recordOption("stack");
				return flags | PlugInFilter.DOES_STACKS;
			}
			if (Recorder.record)
				Recorder.recordOption("slice");
		}
		return flags;
	}
		
	/** Creates a rectangular selection. Removes any existing 
		selection if width or height are less than 1. */
	public static void makeRectangle(int x, int y, int width, int height) {
		if (width<=0 || height<0)
			getImage().deleteRoi();
		else {
			ImagePlus img = getImage();
			if (Interpreter.isBatchMode())
				img.setRoi(new Roi(x,y,width,height), false);
			else
				img.setRoi(x, y, width, height);
		}
	}
	
	/** Creates a subpixel resolution rectangular selection. */
	public static void makeRectangle(double x, double y, double width, double height) {
		if (width<=0 || height<0)
			getImage().deleteRoi();
		else
			getImage().setRoi(new Roi(x,y,width,height), !Interpreter.isBatchMode());
	}

	/** Creates an oval selection. Removes any existing 
		selection if width or height are less than 1. */
	public static void makeOval(int x, int y, int width, int height) {
		if (width<=0 || height<0)
			getImage().deleteRoi();
		else {
			ImagePlus img = getImage();
			img.setRoi(new OvalRoi(x, y, width, height));
		}
	}
	
	/** Creates an subpixel resolution oval selection. */
	public static void makeOval(double x, double y, double width, double height) {
		if (width<=0 || height<0)
			getImage().deleteRoi();
		else
			getImage().setRoi(new OvalRoi(x, y, width, height));
	}

	/** Creates a straight line selection. */
	public static void makeLine(int x1, int y1, int x2, int y2) {
		getImage().setRoi(new Line(x1, y1, x2, y2));
	}
	
	/** Creates a straight line selection using floating point coordinates. */
	public static void makeLine(double x1, double y1, double x2, double y2) {
		getImage().setRoi(new Line(x1, y1, x2, y2));
	}

	/** Creates a point selection using integer coordinates.. */
	public static void makePoint(int x, int y) {
		ImagePlus img = getImage();
		Roi roi = img.getRoi();
		if (shiftKeyDown() && roi!=null && roi.getType()==Roi.POINT) {
			Polygon p = roi.getPolygon();
			p.addPoint(x, y);
			img.setRoi(new PointRoi(p.xpoints, p.ypoints, p.npoints));
			IJ.setKeyUp(KeyEvent.VK_SHIFT);
		} else if (altKeyDown() && roi!=null && roi.getType()==Roi.POINT) {
			((PolygonRoi)roi).deleteHandle(x, y);
			IJ.setKeyUp(KeyEvent.VK_ALT);
		} else
			img.setRoi(new PointRoi(x, y));
	}
	
	/** Creates a point selection using floating point coordinates. */
	public static void makePoint(double x, double y) {
		ImagePlus img = getImage();
		Roi roi = img.getRoi();
		if (shiftKeyDown() && roi!=null && roi.getType()==Roi.POINT) {
			Polygon p = roi.getPolygon();
			p.addPoint((int)Math.round(x), (int)Math.round(y));
			img.setRoi(new PointRoi(p.xpoints, p.ypoints, p.npoints));
			IJ.setKeyUp(KeyEvent.VK_SHIFT);
		} else if (altKeyDown() && roi!=null && roi.getType()==Roi.POINT) {
			((PolygonRoi)roi).deleteHandle(x, y);
			IJ.setKeyUp(KeyEvent.VK_ALT);
		} else
			img.setRoi(new PointRoi(x, y));
	}

	/** Creates an Roi. */
	public static Roi Roi(double x, double y, double width, double height) {
		return new Roi(x, y, width, height);
	}

	/** Creates an OvalRoi. */
	public static OvalRoi OvalRoi(double x, double y, double width, double height) {
		return new OvalRoi(x, y, width, height);
	}

	/** Sets the display range (minimum and maximum displayed pixel values) of the current image. */
	public static void setMinAndMax(double min, double max) {
		setMinAndMax(getImage(), min, max, 7);
	}

	/** Sets the display range (minimum and maximum displayed pixel values) of the specified image. */
	public static void setMinAndMax(ImagePlus img, double min, double max) {
		setMinAndMax(img, min, max, 7);
	}

	/** Sets the minimum and maximum displayed pixel values on the specified RGB
	channels, where 4=red, 2=green and 1=blue. */
	public static void setMinAndMax(double min, double max, int channels) {
		setMinAndMax(getImage(), min, max, channels);
	}

	private static void setMinAndMax(ImagePlus img, double min, double max, int channels) {
		Calibration cal = img.getCalibration();
		min = cal.getRawValue(min); 
		max = cal.getRawValue(max);
		if (channels==7)
			img.setDisplayRange(min, max);
		else
			img.setDisplayRange(min, max, channels);
		img.updateAndDraw();
	}

	/** Resets the minimum and maximum displayed pixel values of the
		current image to be the same as the min and max pixel values. */
	public static void resetMinAndMax() {
		resetMinAndMax(getImage());
	}

	/** Resets the minimum and maximum displayed pixel values of the
		specified image to be the same as the min and max pixel values. */
	public static void resetMinAndMax(ImagePlus img) {
		img.resetDisplayRange();
		img.updateAndDraw();
	}

	/** Sets the lower and upper threshold levels and displays the image 
		using red to highlight thresholded pixels. May not work correctly on
		16 and 32 bit images unless the display range has been reset using IJ.resetMinAndMax().
	*/
	public static void setThreshold(double lowerThreshold, double upperThresold) {
		setThreshold(lowerThreshold, upperThresold, null);
	}
	
	/** Sets the lower and upper threshold levels and displays the image using
		the specified <code>displayMode</code> ("Red", "Black & White", "Over/Under" or "No Update"). */
	public static void setThreshold(double lowerThreshold, double upperThreshold, String displayMode) {
		setThreshold(getImage(), lowerThreshold, upperThreshold, displayMode);
	}

	/** Sets the lower and upper threshold levels of the specified image. */
	public static void setThreshold(ImagePlus img, double lowerThreshold, double upperThreshold) {
		setThreshold(img, lowerThreshold, upperThreshold, "Red");
	}

	/** Sets the lower and upper threshold levels of the specified image and updates the display using
		the specified <code>displayMode</code> ("Red", "Black & White", "Over/Under" or "No Update").
		With calibrated images, 'lowerThreshold' and 'upperThreshold' must be density calibrated values.
		Use setRawThreshold() to set the threshold using raw (uncalibrated) values. */
	public static void setThreshold(ImagePlus img, double lowerThreshold, double upperThreshold, String displayMode) {
		Calibration cal = img.getCalibration();
		if (displayMode==null || !displayMode.contains("raw")) {
			lowerThreshold = cal.getRawValue(lowerThreshold); 
			upperThreshold = cal.getRawValue(upperThreshold);
		}
		setRawThreshold(img, lowerThreshold, upperThreshold, displayMode);
	}

	/** This is a version of setThreshold() that uses raw (uncalibrated)
	 * values in the range 0-255 for 8-bit images and 0-65535 for 16-bit
	 * images and the "Red" LUT display mode.
	*/
	public static void setRawThreshold(ImagePlus img, double lowerThreshold, double upperThreshold) {
		setRawThreshold(img, lowerThreshold, upperThreshold, null);
	}

	/** This is a version of setThreshold() that always uses raw (uncalibrated) values
	 * in the range 0-255 for 8-bit images and 0-65535 for 16-bit images.
	*/
	public static void setRawThreshold(ImagePlus img, double lowerThreshold, double upperThreshold, String displayMode) {
		int mode = ImageProcessor.RED_LUT;
		if (displayMode!=null) {
			displayMode = displayMode.toLowerCase(Locale.US);
			if (displayMode.contains("black"))
				mode = ImageProcessor.BLACK_AND_WHITE_LUT;
			else if (displayMode.contains("over"))
				mode = ImageProcessor.OVER_UNDER_LUT;
			else if (displayMode.contains("no"))
				mode = ImageProcessor.NO_LUT_UPDATE;
		}
		img.getProcessor().setThreshold(lowerThreshold, upperThreshold, mode);
		if (mode!=ImageProcessor.NO_LUT_UPDATE && img.getWindow()!=null) {
			img.getProcessor().setLutAnimation(true);
			img.updateAndDraw();
			ThresholdAdjuster.update();
		}
	}

	/** Replaced by ImagePlus.setAutoThreshold(). */
	public static void setAutoThreshold(ImagePlus imp, String method) {
		ImageProcessor ip = imp.getProcessor();
		if (ip instanceof ColorProcessor)
			throw new IllegalArgumentException("Non-RGB image required");
		ip.setRoi(imp.getRoi());
		if (method!=null) {
			try {
				if (method.indexOf("stack")!=-1)
					setStackThreshold(imp, ip, method);
				else
					ip.setAutoThreshold(method);
			} catch (Exception e) {
				log(e.getMessage());
			}
		} else
			ip.setAutoThreshold(ImageProcessor.ISODATA2, ImageProcessor.RED_LUT);
		imp.updateAndDraw();
	}
	
	private static void setStackThreshold(ImagePlus imp, ImageProcessor ip, String method) {
		boolean darkBackground = method.contains("dark");
		boolean histo16 = imp.getBitDepth()==16 && method.contains("16");
		//IJ.log("setStackThreshold: "+histo16+" "+method);
		boolean addOne = !method.contains("16");
		int measurements = Analyzer.getMeasurements();
		Analyzer.setMeasurements(Measurements.AREA+Measurements.MIN_MAX);
		ImageStatistics stats = new StackStatistics(imp);
		Analyzer.setMeasurements(measurements);
		AutoThresholder thresholder = new AutoThresholder();
		double min=0.0, max=255.0;
		if (imp.getBitDepth()!=8) {
			min = stats.min;
			max = stats.max;
		}
		int[] histogram = stats.histogram;
		if (histo16)
			histogram = stats.histogram16;
		int threshold = thresholder.getThreshold(method, histogram);
		double lower, upper;
		double tmax = 255.0;
		if (histogram.length>256)
			tmax = 65535.0;
		if (darkBackground) {
			if (ip.isInvertedLut())
				{lower=0.0; upper=threshold;}
			else
				{lower=threshold+(addOne?1:0); upper=tmax;}
		} else {
			if (ip.isInvertedLut())
				{lower=threshold+(addOne?1:0); upper=tmax;}
			else
				{lower=0.0; upper=threshold;}
		}
		if (!histo16) {
			if (lower>255) lower = 255;
			if (max>min) {
				lower = min + (lower/255.0)*(max-min);
				upper = min + (upper/255.0)*(max-min);
			} else
				lower = upper = min;
			ip.setMinAndMax(min, max);
		}
		ip.setThreshold(lower, upper, ImageProcessor.RED_LUT);
		imp.updateAndDraw();
	}

	/** Disables thresholding on the current image. */
	public static void resetThreshold() {
		resetThreshold(getImage());
	}
	
	/** Disables thresholding on the specified image. */
	public static void resetThreshold(ImagePlus img) {
		ImageProcessor ip = img.getProcessor();
		ip.resetThreshold();
		ip.setLutAnimation(true);
		img.updateAndDraw();
		ThresholdAdjuster.update();
	}
	
	/** For IDs less than zero, activates the image with the specified ID.
		For IDs greater than zero, activates the Nth image. */
	public static void selectWindow(int id) {
		if (id>0)
			id = WindowManager.getNthImageID(id);
		ImagePlus imp = WindowManager.getImage(id);
		if (imp==null)
			error("Macro Error", "Image "+id+" not found or no images are open.");
		if (Interpreter.isBatchMode()) {
			ImagePlus impT = WindowManager.getTempCurrentImage();
			ImagePlus impC = WindowManager.getCurrentImage();
			if (impC!=null && impC!=imp && impT!=null)
				impC.saveRoi();
            WindowManager.setTempCurrentImage(imp);
            Interpreter.activateImage(imp);
            WindowManager.setWindow(null);
		} else {
			if (imp==null)
				return;
			ImageWindow win = imp.getWindow();
			if (win!=null) {
				win.toFront();
				win.setState(Frame.NORMAL);
				WindowManager.setWindow(win);
			}
			long start = System.currentTimeMillis();
			// timeout after 1 second unless current thread is event dispatch thread
			String thread = Thread.currentThread().getName();
			int timeout = thread!=null&&thread.indexOf("EventQueue")!=-1?0:1000;
			if (IJ.isMacOSX() && IJ.isJava18() && timeout>0)
				timeout = 250;  //work around OS X/Java 8 window activation bug
			while (true) {
				wait(10);
				imp = WindowManager.getCurrentImage();
				if (imp!=null && imp.getID()==id)
					return; // specified image is now active
				if ((System.currentTimeMillis()-start)>timeout && win!=null) {
					WindowManager.setCurrentWindow(win);
					return;
				}
			}
		}
	}

	/** Activates the window with the specified title. */
	public static void selectWindow(String title) {
		if (title.equals("ImageJ")&&ij!=null) {
			ij.toFront();
			return;
		}
		long start = System.currentTimeMillis();
		while (System.currentTimeMillis()-start<3000) { // 3 sec timeout
			Window win = WindowManager.getWindow(title);
			if (win!=null && !(win instanceof ImageWindow)) {
				selectWindow(win);
				return;
			}
			int[] wList = WindowManager.getIDList();
			int len = wList!=null?wList.length:0;
			for (int i=0; i<len; i++) {
				ImagePlus imp = WindowManager.getImage(wList[i]);
				if (imp!=null) {
					if (imp.getTitle().equals(title)) {
						selectWindow(imp.getID());
						return;
					}
				}
			}
			wait(10);
		}
		error("Macro Error", "No window with the title \""+title+"\" found.");
	}
	
	static void selectWindow(Window win) {
		if (win instanceof Frame) {
			((Frame)win).toFront();
			((Frame)win).setState(Frame.NORMAL);
		} else
			((Dialog)win).toFront();
		long start = System.currentTimeMillis();
		while (true) {
			wait(10);
			if (WindowManager.getActiveWindow()==win)
				return; // specified window is now in front
			if ((System.currentTimeMillis()-start)>1000) {
				WindowManager.setWindow(win);
				return;   // 1 second timeout
			}
		}
	}
	
	/** Sets the foreground color. */
	public static void setForegroundColor(int red, int green, int blue) {
		setColor(red, green, blue, true);
	}

	/** Sets the background color. */
	public static void setBackgroundColor(int red, int green, int blue) {
		setColor(red, green, blue, false);
	}
	
	static void setColor(int red, int green, int blue, boolean foreground) {
		Color c = Colors.toColor(red, green, blue);
		if (foreground) {
			Toolbar.setForegroundColor(c);
			ImagePlus img = WindowManager.getCurrentImage();
			if (img!=null)
				img.getProcessor().setColor(c);
		} else
			Toolbar.setBackgroundColor(c);
	}

	/** Switches to the specified tool, where id = Toolbar.RECTANGLE (0),
		Toolbar.OVAL (1), etc. */
	public static void setTool(int id) {
		Toolbar.getInstance().setTool(id);
	}

	/** Switches to the specified tool, where 'name' is "rect", "elliptical", 
		"brush", etc. Returns 'false' if the name is not recognized. */
	public static boolean setTool(String name) {
		return Toolbar.getInstance().setTool(name);
	}

	/** Returns the name of the current tool. */
	public static String getToolName() {
		return Toolbar.getToolName();
	}

	/** Equivalent to clicking on the current image at (x,y) with the
		wand tool. Returns the number of points in the resulting ROI. */
	public static int doWand(int x, int y) {
		return doWand(getImage(), x, y, 0, null);
	}

	/** Traces the boundary of the area with pixel values within
	* 'tolerance' of the value of the pixel at the starting location.
	* 'tolerance' is in uncalibrated units.
	* 'mode' can be "4-connected", "8-connected" or "Legacy".
	* "Legacy" is for compatibility with previous versions of ImageJ;
	* it is ignored if 'tolerance' > 0.
	*/
	public static int doWand(int x, int y, double tolerance, String mode) {
		return doWand(getImage(), x, y, tolerance, mode);
	}
	
	/** This version of doWand adds an ImagePlus argument. */
	public static int doWand(ImagePlus img, int x, int y, double tolerance, String mode) {
		ImageProcessor ip = img.getProcessor();
		if ((img.getType()==ImagePlus.GRAY32) && Double.isNaN(ip.getPixelValue(x,y)))
			return 0;
		int imode = Wand.LEGACY_MODE;
		boolean smooth = false;
		if (mode!=null) {
			if (mode.startsWith("4"))
				imode = Wand.FOUR_CONNECTED;
			else if (mode.startsWith("8"))
				imode = Wand.EIGHT_CONNECTED;
			smooth = mode.contains("smooth");
				
		}
		Wand w = new Wand(ip);
		if (!ip.isThreshold() || (ip.getLutUpdateMode()==ImageProcessor.NO_LUT_UPDATE&& tolerance>0.0)) {
			w.autoOutline(x, y, tolerance, imode);
			smooth = false;
		} else {
			w.autoOutline(x, y, ip.getMinThreshold(), ip.getMaxThreshold(), imode);
		}
		if (w.npoints>0) {
			Roi previousRoi = img.getRoi();
			Roi roi = new PolygonRoi(w.xpoints, w.ypoints, w.npoints, Roi.TRACED_ROI);
			img.deleteRoi();
			img.setRoi(roi);			
			if (previousRoi!=null)
				roi.update(shiftKeyDown(), altKeyDown());  // add/subtract ROI to previous one if shift/alt key down
			Roi roi2 = img.getRoi();
			if (smooth && roi2!=null && roi2.getType()==Roi.TRACED_ROI) {
				Rectangle bounds = roi2.getBounds();
				if (bounds.width>1 && bounds.height>1) {
					if (smoothMacro==null)
						smoothMacro = BatchProcessor.openMacroFromJar("SmoothWandTool.txt");
					if (EventQueue.isDispatchThread())
						new MacroRunner(smoothMacro); // run on separate thread
					else
						Macro.eval(smoothMacro);
				}
			}
		}
		return w.npoints;
	}
	
	/** Sets the transfer mode used by the <i>Edit/Paste</i> command, where mode is "Copy", "Blend", "Average", "Difference", 
		"Transparent", "Transparent2", "AND", "OR", "XOR", "Add", "Subtract", "Multiply", or "Divide". */
	public static void setPasteMode(String mode) {
		Roi.setPasteMode(stringToPasteMode(mode));
	}

	public static int stringToPasteMode(String mode) {
		if (mode==null)
			return Blitter.COPY;
		mode = mode.toLowerCase(Locale.US);
		int m = Blitter.COPY;
		if (mode.startsWith("ble") || mode.startsWith("ave"))
			m = Blitter.AVERAGE;
		else if (mode.startsWith("diff"))
			m = Blitter.DIFFERENCE;
		else if (mode.indexOf("zero")!=-1)
			m = Blitter.COPY_ZERO_TRANSPARENT;
		else if (mode.startsWith("tran"))
			m = Blitter.COPY_TRANSPARENT;
		else if (mode.startsWith("and"))
			m = Blitter.AND;
		else if (mode.startsWith("or"))
			m = Blitter.OR;
		else if (mode.startsWith("xor"))
			m = Blitter.XOR;
		else if (mode.startsWith("sub"))
			m = Blitter.SUBTRACT;
		else if (mode.startsWith("add"))
			m = Blitter.ADD;
		else if (mode.startsWith("div"))
			m = Blitter.DIVIDE;
		else if (mode.startsWith("mul"))
			m = Blitter.MULTIPLY;
		else if (mode.startsWith("min"))
			m = Blitter.MIN;
		else if (mode.startsWith("max"))
			m = Blitter.MAX;
		return m;
	}

	/** Returns a reference to the active image, or displays an error
		message and aborts the plugin or macro if no images are open. */
	public static ImagePlus getImage() {
		ImagePlus img = WindowManager.getCurrentImage();
		if (img==null) {
			IJ.noImage();
			if (ij==null)
				System.exit(0);
			else
				abort();
		}
		return img;
	}
	
	/**The macro interpreter uses this method to call getImage().*/
	public static ImagePlus getImage(Interpreter interpreter) {
		macroInterpreter = interpreter;
		ImagePlus imp =  getImage();
		macroInterpreter = null;
		return imp;
	}
	
	/** Returns the active image or stack slice as an ImageProcessor, or displays
		an error message and aborts the plugin or macro if no images are open. */
	public static ImageProcessor getProcessor() {
		ImagePlus imp = IJ.getImage();
		return imp.getProcessor();
	}

	/** Switches to the specified stack slice, where 1<='slice'<=stack-size. */
	public static void setSlice(int slice) {
		getImage().setSlice(slice);
	}

	/** Returns the ImageJ version number as a string. */
	public static String getVersion() {
		return ImageJ.VERSION;
	}
	
	/** Returns the ImageJ version and build number as a String, for 
		example "1.46n05", or 1.46n99 if there is no build number. */
	public static String getFullVersion() {
		String build = ImageJ.BUILD;
		if (build.length()==0)
			build = "99";
		else if (build.length()==1)
			build = "0" + build;
		return ImageJ.VERSION+build;
	}
		
	/** Returns the path to the specified directory if <code>title</code> is
		"home" ("user.home"), "downloads", "startup",  "imagej" (ImageJ directory),
		"plugins", "macros", "luts", "temp", "current", "default",
		"image" (directory active image was loaded from), "file" 
		(directory most recently used to open or save a file), "cwd"
		(current working directory) or "preferences" (location of
		"IJ_Prefs.txt" file), otherwise displays a dialog and
		returns the path to the directory selected by the user. Returns
		null if the specified directory is not found or the user cancels the
		dialog box. Also aborts the macro if the user cancels the
		dialog box.*/
	public static String getDirectory(String title) {
		String dir = null;
		String title2 = title.toLowerCase(Locale.US);
		if (title2.equals("plugins"))
			dir = Menus.getPlugInsPath();
		else if (title2.equals("macros"))
			dir = Menus.getMacrosPath();
		else if (title2.equals("luts")) {
			String ijdir = Prefs.getImageJDir();
			if (ijdir!=null)
				dir = ijdir + "luts" + File.separator;
			else
				dir = null;
		} else if (title2.equals("home"))
			dir = System.getProperty("user.home");
		else if (title2.equals("downloads"))
			dir = System.getProperty("user.home")+File.separator+"Downloads";
		else if (title2.equals("startup"))
			dir = Prefs.getImageJDir();
		else if (title2.equals("imagej"))
			dir = Prefs.getImageJDir();
		else if (title2.equals("current") || title2.equals("default"))
			dir = OpenDialog.getDefaultDirectory();
		else if (title2.equals("preferences"))
			dir = Prefs.getPrefsDir();
		else if (title2.equals("temp")) {
			dir = System.getProperty("java.io.tmpdir");
			if (isMacintosh()) dir = "/tmp/";
		} else if (title2.equals("image")) {
			ImagePlus imp = WindowManager.getCurrentImage();
			FileInfo fi = imp!=null?imp.getOriginalFileInfo():null;
			if (fi!=null && fi.directory!=null) {
				dir = fi.directory;
			} else
				dir = null;
		} else if (title2.equals("file"))
			dir = OpenDialog.getLastDirectory();
		else if (title2.equals("cwd"))
			dir = System.getProperty("user.dir");
		else {
			String defaultDir = OpenDialog.getDefaultDirectory();
			DirectoryChooser dc = new DirectoryChooser(title);
			dir = dc.getDirectory();
			OpenDialog.setDefaultDirectory(defaultDir);
			if (dir==null) Macro.abort();
		}
		dir = addSeparator(dir);
		return dir;
	}
	
	public static String addSeparator(String path) {
		if (path==null)
			return null;
		if (path.length()>0 && !(path.endsWith(File.separator)||path.endsWith("/"))) {
			if (IJ.isWindows()&&path.contains(File.separator))
				path += File.separator;
			else
				path += "/";
		}
		return path;
	}
		
	/** Alias for getDirectory(). */
	public static String getDir(String title) {
		return getDirectory(title);
	}

	
	/** Displays an open file dialog and returns the path to the
		choosen file, or returns null if the dialog is canceled. */
	public static String getFilePath(String dialogTitle) {
		OpenDialog od = new OpenDialog(dialogTitle);
		return od.getPath();
	}
		
	/** Displays a file open dialog box and then opens the tiff, dicom, 
		fits, pgm, jpeg, bmp, gif, lut, roi, or text file selected by 
		the user. Displays an error message if the selected file is not
		in one of the supported formats, or if it is not found. */
	public static void open() {
		open(null);
	}

	/** Opens and displays a tiff, dicom, fits, pgm, jpeg, bmp, gif, lut, 
		roi, or text file. Displays an error message if the specified file
		is not in one of the supported formats, or if it is not found.
		With 1.41k or later, opens images specified by a URL.
		*/
	public static void open(String path) {
		if (ij==null && Menus.getCommands()==null)
			init();
		Opener o = new Opener();
		macroRunning = true;
		if (path==null || path.equals(""))		
			o.open();
		else
			o.open(path);
		macroRunning = false;
	}
		
	/** Opens and displays the nth image in the specified tiff stack. */
	public static void open(String path, int n) {
		if (ij==null && Menus.getCommands()==null)
			init();
		ImagePlus imp = openImage(path, n);
		if (imp!=null) imp.show();
	}

	/** Opens the specified file as a tiff, bmp, dicom, fits, pgm, gif, jpeg 
	 * or text image and returns an ImagePlus object if successful.
	 * Calls HandleExtraFileTypes plugin if the file type is not recognised.
	 * Displays a file open dialog if 'path' is null or an empty string.
	 * Note that 'path' can also be a URL. Some reader plugins, including
	 * the Bio-Formats plugin, display the image and return null.
	 * Use IJ.open() to display a file open dialog box.
	 * @see ij.io.Opener#openUsingBioFormats(String)
	*/
	public static ImagePlus openImage(String path) {
		macroRunning = true;
		ImagePlus imp = (new Opener()).openImage(path);
		macroRunning = false;
		return imp;
	}

	/** Opens the nth image of the specified tiff stack. */
	public static ImagePlus openImage(String path, int n) {
		return (new Opener()).openImage(path, n);
	}

	/** Opens the specified tiff file as a virtual stack. */
	public static ImagePlus openVirtual(String path) {
		return FileInfoVirtualStack.openVirtual(path);
	}

	/** Opens an image using a file open dialog and returns it as an ImagePlus object. */
	public static ImagePlus openImage() {
		return openImage(null);
	}

	/** Opens a URL and returns the contents as a string.
		Returns "<Error: message>" if there an error, including
		host or file not found. */
	public static String openUrlAsString(String url) {
		//if (!trustManagerCreated && url.contains("nih.gov")) trustAllCerts();
		url = Opener.updateUrl(url);
		if (debugMode) log("OpenUrlAsString: "+url);
		StringBuffer sb = null;
		url = url.replaceAll(" ", "%20");
		try {
			//if (url.contains("nih.gov")) addRootCA();
			URL u = new URL(url);
			URLConnection uc = u.openConnection();
			long len = uc.getContentLength();
			if (len>5242880L)
				return "<Error: file is larger than 5MB>";
			InputStream in = u.openStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
			sb = new StringBuffer() ;
			String line;
			while ((line=br.readLine()) != null)
				sb.append (line + "\n");
			in.close ();
		} catch (Exception e) {
			return("<Error: "+e+">");
		}
		if (sb!=null)
			return new String(sb);
		else
			return "";
	}	

	/** Saves the current image, lookup table, selection or text window to the specified file path. 
		The path must end in ".tif", ".jpg", ".gif", ".zip", ".raw", ".avi", ".bmp", ".fits", ".pgm", ".png", ".lut", ".roi" or ".txt".  */
	public static void save(String path) {
		save(null, path);
	}

	/** Saves the specified image, lookup table or selection to the specified file path. 
		The file path should end with ".tif", ".jpg", ".gif", ".zip", ".raw", ".avi", ".bmp", 
		".fits", ".pgm", ".png", ".lut", ".roi" or ".txt". The specified image is saved in 
		TIFF format if there is no extension. */
	public static void save(ImagePlus imp, String path) {
		ImagePlus imp2 = imp;
		if (imp2==null)
			imp2 = WindowManager.getCurrentImage();
		int dotLoc = path.lastIndexOf('.');
		if (dotLoc==-1 && imp2!=null) {
			path = path + ".tif"; // save as TIFF if file name does not have an extension
			dotLoc = path.lastIndexOf('.');
		}
		if (dotLoc!=-1) {
			String title = imp2!=null?imp2.getTitle():null;
			saveAs(imp, path.substring(dotLoc+1), path);
			if (title!=null)
				imp2.setTitle(title);
		} else
			error("The file path passed to IJ.save() method or save()\nmacro function is missing the required extension.\n \n\""+path+"\"");
	}

	/* Saves the active image, lookup table, selection, measurement results, selection XY 
		coordinates or text window to the specified file path. The format argument must be "tiff", 
		"jpeg", "gif", "zip", "raw", "avi", "bmp", "fits", "pgm", "png", "text image", "lut", "selection", "measurements", 
		"xy Coordinates" or "text".  If <code>path</code> is null or an emply string, a file
		save dialog is displayed. */
 	public static void saveAs(String format, String path) {
 		saveAs(null, format, path);
 	}

	/* Saves the specified image. The format argument must be "tiff",  
		"jpeg", "gif", "zip", "raw", "avi", "bmp", "fits", "pgm", "png", 
		"text image", "lut", "selection" or "xy Coordinates". */
 	public static void saveAs(ImagePlus imp, String format, String path) {
		if (format==null)
			return;
		if (path!=null && path.length()==0)
			path = null;
		format = format.toLowerCase(Locale.US);
		Roi roi2 = imp!=null?imp.getRoi():null;
		if (roi2!=null)
			roi2.endPaste();
		if (format.indexOf("tif")!=-1) {
			saveAsTiff(imp, path);
			return;
		} else if (format.indexOf("jpeg")!=-1 || format.indexOf("jpg")!=-1) {
			path = updateExtension(path, ".jpg");
			JpegWriter.save(imp, path, FileSaver.getJpegQuality());
			return;
		} else if (format.indexOf("gif")!=-1) {
			path = updateExtension(path, ".gif");
			GifWriter.save(imp, path);
			return;
		} else if (format.indexOf("text image")!=-1) {
			path = updateExtension(path, ".txt");
			format = "Text Image...";
		} else if (format.indexOf("text")!=-1 || format.indexOf("txt")!=-1) {
			if (path!=null && !path.endsWith(".xls") && !path.endsWith(".csv") && !path.endsWith(".tsv"))
				path = updateExtension(path, ".txt");
			format = "Text...";
		} else if (format.indexOf("zip")!=-1) {
			path = updateExtension(path, ".zip");
			format = "ZIP...";
		} else if (format.indexOf("raw")!=-1) {
			//path = updateExtension(path, ".raw");
			format = "Raw Data...";
		} else if (format.indexOf("avi")!=-1) {
			path = updateExtension(path, ".avi");
			format = "AVI... ";
		} else if (format.indexOf("bmp")!=-1) {
			path = updateExtension(path, ".bmp");
			format = "BMP...";
		} else if (format.indexOf("fits")!=-1) {
			path = updateExtension(path, ".fits");
			format = "FITS...";
		} else if (format.indexOf("png")!=-1) {
			path = updateExtension(path, ".png");
			format = "PNG...";
		} else if (format.indexOf("pgm")!=-1) {
			path = updateExtension(path, ".pgm");
			format = "PGM...";
		} else if (format.indexOf("lut")!=-1) {
			path = updateExtension(path, ".lut");
			format = "LUT...";
		} else if (format.contains("results") || format.contains("measurements") || format.contains("table")) {
			format = "Results...";
		} else if (format.contains("selection") || format.contains("roi")) {
			path = updateExtension(path, ".roi");
			format = "Selection...";
		} else if (format.indexOf("xy")!=-1 || format.indexOf("coordinates")!=-1) {
			path = updateExtension(path, ".txt");
			format = "XY Coordinates...";
		} else
			error("Unsupported save() or saveAs() file format: \""+format+"\"\n \n\""+path+"\"");
		if (path==null)
			run(format);
		else {
			if (path.contains(" "))
				run(imp, format, "save=["+path+"]");
			else
				run(imp, format, "save="+path);
		}
	}
	
	/** Saves the specified image in TIFF format. Displays a file save dialog
		if 'path' is null or an empty string. Returns 'false' if there is an
		error or if the user selects "Cancel" in the file save dialog. */
	public static boolean saveAsTiff(ImagePlus imp, String path) {
		if (imp==null)
			imp = getImage();
		if (path==null || path.equals(""))
			return (new FileSaver(imp)).saveAsTiff();
		if (!path.endsWith(".tiff"))
			path = updateExtension(path, ".tif");
		FileSaver fs = new FileSaver(imp);
		boolean ok;
		if (imp.getStackSize()>1)
			ok = fs.saveAsTiffStack(path);
		else
			ok = fs.saveAsTiff(path);
		if (ok)
			fs.updateImagePlus(path, FileInfo.TIFF);
		return ok;
	}
	
	static String updateExtension(String path, String extension) {
		if (path==null) return null;
		int dotIndex = path.lastIndexOf(".");
		int separatorIndex = path.lastIndexOf(File.separator);
		if (dotIndex>=0 && dotIndex>separatorIndex && (path.length()-dotIndex)<=5) {
			if (dotIndex+1<path.length() && Character.isDigit(path.charAt(dotIndex+1)))
				path += extension;
			else
				path = path.substring(0, dotIndex) + extension;
		} else
			path += extension;
		return path;
	}
	
	/** Saves a string as a file. Displays a file save dialog if
		'path' is null or blank. Returns an error message 
		if there is an exception, otherwise returns null. */
	public static String saveString(String string, String path) {
		return write(string, path, false);
	}

	/** Appends a string to the end of a file. A newline character ("\n") 
		is added to the end of the string before it is written. Returns an  
		error message if there is an exception, otherwise returns null. */
	public static String append(String string, String path) {
		return write(string+"\n", path, true);
	}

	private static String write(String string, String path, boolean append) {
		if (path==null || path.equals("")) {
			String msg = append?"Append String...":"Save String...";
			SaveDialog sd = new SaveDialog(msg, "Untitled", ".txt");
			String name = sd.getFileName();
			if (name==null) return null;
			path = sd.getDirectory() + name;
		}
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(path, append));
			out.write(string);
			out.close();
		} catch (IOException e) {
			return ""+e;
		}
		return null;
	}

	/** Opens a text file as a string. Displays a file open dialog
		if path is null or blank. Returns null if the user cancels
		the file open dialog. If there is an error, returns a 
		 message in the form "Error: message". */
	public static String openAsString(String path) {
		if (path==null || path.equals("")) {
			OpenDialog od = new OpenDialog("Open Text File", "");
			String directory = od.getDirectory();
			String name = od.getFileName();
			if (name==null) return null;
			path = directory + name;
		}
		String str = "";
		File file = new File(path);
		if (!file.exists())
			return "Error: file not found";
		try {
			StringBuffer sb = new StringBuffer(5000);
			BufferedReader r = new BufferedReader(new FileReader(file));
			while (true) {
				String s=r.readLine();
				if (s==null)
					break;
				else
					sb.append(s+"\n");
			}
			r.close();
			str = new String(sb);
		}
		catch (Exception e) {
			str = "Error: "+e.getMessage();
		}
		return str;
	}
	
	public static ByteBuffer openAsByteBuffer(String path) {
		if (path==null || path.equals("")) {
			OpenDialog od = new OpenDialog("Open as ByteBuffer", "");
			String directory = od.getDirectory();
			String name = od.getFileName();
			if (name==null) return null;
			path = directory + name;
		}
		File file = new File(path);
		if (!file.exists()) {
			error("OpenAsByteBuffer", "File not found");
			return null;
		}
		int len = (int)file.length();
		byte[] buffer = new byte[len];
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(path));
			DataInputStream dis = new DataInputStream(in);
			dis.readFully(buffer);
			dis.close();
		}
		catch (Exception e) {
			error("OpenAsByteBuffer", e.getMessage());
			return null;
		}
		return ByteBuffer.wrap(buffer);
	}

	/** Creates a new image.
	*  @param title   image name
	*  @param width  image width in pixels
	*  @param height image height in pixels
	*  @param depth number of stack images
	*  @param bitdepth  8, 16, 32 (float) or 24 (RGB)
	*/
	 public static ImagePlus createImage(String title, int width, int height, int depth, int bitdepth) {
		return NewImage.createImage(title, width, height, depth, bitdepth, NewImage.FILL_BLACK);
	 }

	 /** Creates a new imagePlus. <code>Type</code> should contain "8-bit", "16-bit", "32-bit" or "RGB". 
		 In addition, it can contain "white", "black" or "ramp". <code>Width</code> 
	 	and <code>height</code> specify the width and height of the image in pixels.  
	 	<code>Depth</code> specifies the number of stack slices. */
	 public static ImagePlus createImage(String title, String type, int width, int height, int depth) {
		type = type.toLowerCase(Locale.US);
		int bitDepth = 8;
		if (type.contains("16"))
			bitDepth = 16;
		boolean signedInt = type.contains("32-bit int");
		if (type.contains("32"))
			bitDepth = 32;
		if (type.contains("24") || type.contains("rgb") || signedInt)
			bitDepth = 24;
		int options = NewImage.FILL_WHITE;
		if (bitDepth==16 || bitDepth==32)
			options = NewImage.FILL_BLACK;
		if (type.contains("white"))
			options = NewImage.FILL_WHITE;
		else if (type.contains("black"))
			options = NewImage.FILL_BLACK;
		else if (type.contains("ramp"))
			options = NewImage.FILL_RAMP;
		else if (type.contains("noise") || type.contains("random"))
			options = NewImage.FILL_NOISE;
		options += NewImage.CHECK_AVAILABLE_MEMORY;
		if (signedInt)
			options += NewImage.SIGNED_INT;
		return NewImage.createImage(title, width, height, depth, bitDepth, options);
	}

	/** Creates a new hyperstack.
	* @param title   image name
	* @param type  "8-bit", "16-bit", "32-bit" or "RGB".  May also
	* contain "white" , "black" (the default), "ramp", "composite-mode",
	* "color-mode", "grayscale-mode or "label".
	* @param width  image width in pixels
	* @param height image height in pixels
	* @param channels number of channels
	* @param slices number of slices
	* @param frames number of frames
	*/
	 public static ImagePlus createImage(String title, String type, int width, int height, int channels, int slices, int frames) {
		if (type.contains("label"))
	 		type += "ramp";
		if (!(type.contains("white")||type.contains("ramp")))
	 		type += "black";
		ImagePlus imp = IJ.createImage(title, type, width, height, channels*slices*frames);
		imp.setDimensions(channels, slices, frames);
		int mode = IJ.COLOR;
		if (type.contains("composite"))
			mode = IJ.COMPOSITE;
		if (type.contains("grayscale"))
			mode = IJ.GRAYSCALE;
		if (channels>1 && imp.getBitDepth()!=24)
			imp = new CompositeImage(imp, mode);
		imp.setOpenAsHyperStack(true);
		if (type.contains("label"))
			HyperStackMaker.labelHyperstack(imp);
		return imp;
	 }

	/** Creates a new hyperstack.
	*  @param title   image name
	*  @param width  image width in pixels
	*  @param height image height in pixels
	*  @param channels number of channels
	*  @param slices number of slices
	*  @param frames number of frames
	*  @param bitdepth  8, 16, 32 (float) or 24 (RGB)
	*/
	 public static ImagePlus createHyperStack(String title, int width, int height, int channels, int slices, int frames, int bitdepth) {
		ImagePlus imp = createImage(title, width, height, channels*slices*frames, bitdepth);
		imp.setDimensions(channels, slices, frames);
		if (channels>1 && bitdepth!=24)
			imp = new CompositeImage(imp, IJ.COMPOSITE);
		imp.setOpenAsHyperStack(true);
		return imp;
	 }
	 
	 /** Opens a new image. <code>Type</code> should contain "8-bit", "16-bit", "32-bit" or "RGB". 
		In addition, it can contain "white", "black" or "ramp". <code>Width</code> 
		and <code>height</code> specify the width and height of the image in pixels.  
		<code>Depth</code> specifies the number of stack slices. */
	public static void newImage(String title, String type, int width, int height, int depth) {
		ImagePlus imp = createImage(title, type, width, height, depth);
		if (imp!=null) {
			macroRunning = true;
			imp.show();
			macroRunning = false;
		}
	}

	/** Returns true if the <code>Esc</code> key was pressed since the
		last ImageJ command started to execute or since resetEscape() was called. */
	public static boolean escapePressed() {
		return escapePressed;
	}

	/** This method sets the <code>Esc</code> key to the "up" position.
		The Executer class calls this method when it runs 
		an ImageJ command in a separate thread. */
	public static void resetEscape() {
		escapePressed = false;
	}
	
	/** Causes IJ.error() output to be temporarily redirected to the "Log" window. */
	public static void redirectErrorMessages() {
		redirectErrorMessages = true;
		lastErrorMessage = null;
	}
	
	/** Set 'true' and IJ.error() output will be temporarily redirected to the "Log" window. */
	public static void redirectErrorMessages(boolean redirect) {
		redirectErrorMessages = redirect;
		lastErrorMessage = null;
	}

	/** Returns the state of the  'redirectErrorMessages' flag, which is set by File/Import/Image Sequence. */
	public static boolean redirectingErrorMessages() {
		return redirectErrorMessages;
	}

	/** Temporarily suppress "plugin not found" errors. */
	public static void suppressPluginNotFoundError() {
		suppressPluginNotFoundError = true;
	}

	/** Returns the class loader ImageJ uses to run plugins or the
		system class loader if Menus.getPlugInsPath() returns null. */
	public static ClassLoader getClassLoader() {
		if (classLoader==null) {
			String pluginsDir = Menus.getPlugInsPath();
			if (pluginsDir==null) {
				String home = System.getProperty("plugins.dir");
				if (home!=null) {
					if (!home.endsWith(Prefs.separator)) home+=Prefs.separator;
					pluginsDir = home+"plugins"+Prefs.separator;
					if (!(new File(pluginsDir)).isDirectory())
						pluginsDir = home;
				}
			}
			if (pluginsDir==null)
				return IJ.class.getClassLoader();
			else {
				if (Menus.jnlp)
					classLoader = new PluginClassLoader(pluginsDir, true);
				else
					classLoader = new PluginClassLoader(pluginsDir);
			}
		}
		return classLoader;
	}
	
	/** Returns the size, in pixels, of the primary display. */
	public static Dimension getScreenSize() {
		Rectangle bounds = GUI.getScreenBounds();
		return new Dimension(bounds.width, bounds.height);
	}
	
	/** Returns, as an array of strings, a list of the LUTs in the 
	 * Image/Lookup Tables menu.
	 * @see ij.plugin.LutLoader#getLut
	 * See also: Help>Examples>JavaScript/Show all LUTs
	 * and Image/Color/Display LUTs
	*/
	public static String[] getLuts() {
		ArrayList list = new ArrayList();
		Hashtable commands = Menus.getCommands();
		Menu lutsMenu = Menus.getImageJMenu("Image>Lookup Tables");
		if (commands==null || lutsMenu==null)
			return new String[0];
		for (int i=0; i<lutsMenu.getItemCount(); i++) {
			MenuItem menuItem = lutsMenu.getItem(i);
			String label = menuItem.getLabel();
			if (label.equals("-") || label.equals("Invert LUT") || label.equals("Apply LUT"))
				continue;
			String command = (String)commands.get(label);
			if (command==null || command.startsWith("ij.plugin.LutLoader"))
				list.add(label);
		}
		return (String[])list.toArray(new String[list.size()]);
	}
	
	static void abort() {
		if ((ij!=null || Interpreter.isBatchMode()) && macroInterpreter==null)
			throw new RuntimeException(Macro.MACRO_CANCELED);
	}
	
	static void setClassLoader(ClassLoader loader) {
		classLoader = loader;
	}

	public static void resetClassLoader() {
		setClassLoader(null);
	}

	/** Displays a stack trace. Use the setExceptionHandler 
		method() to override with a custom exception handler. */
	public static void handleException(Throwable e) {
		if (exceptionHandler!=null) {
			exceptionHandler.handle(e);
			return;
		}
		if (Macro.MACRO_CANCELED.equals(e.getMessage()))
			return;
		CharArrayWriter caw = new CharArrayWriter();
		PrintWriter pw = new PrintWriter(caw);
		e.printStackTrace(pw);
		String s = caw.toString();
		String lineNumber = "";
		if (s!=null && s.contains("ThreadDeath"))
			return;
		Interpreter interpreter = Thread.currentThread().getName().endsWith("Macro$") ? Interpreter.getInstance() : null;
		if (interpreter!=null)
			lineNumber = "\nMacro line number: " + interpreter.getLineNumber();
		if (getInstance()!=null) {
			s = IJ.getInstance().getInfo()+lineNumber+"\n \n"+s;
			new TextWindow("Exception", s, 500, 340);
		} else
			log(s);
	}

	/** Installs a custom exception handler that 
		overrides the handleException() method. */
	public static void setExceptionHandler(ExceptionHandler handler) {
		exceptionHandler = handler;
	}

	public interface ExceptionHandler {
		public void handle(Throwable e);
	}

	static ExceptionHandler exceptionHandler;

	public static void addEventListener(IJEventListener listener) {
		eventListeners.addElement(listener);
	}
	
	public static void removeEventListener(IJEventListener listener) {
		eventListeners.removeElement(listener);
	}
	
	public static void notifyEventListeners(int eventID) {
		synchronized (eventListeners) {
			for (int i=0; i<eventListeners.size(); i++) {
				IJEventListener listener = (IJEventListener)eventListeners.elementAt(i);
				listener.eventOccurred(eventID);
			}
		}
	}

	/** Adds a key-value pair to IJ.properties. The key
	* and value are removed if 'value' is null.
	*/
	public static void setProperty(String key, Object value) {
		if (properties==null)
			properties = new Properties();
		if (value==null)
			properties.remove(key);
		else
			properties.put(key, value);
	}

	/** Returns the object in IJ.properties associated
	*  with 'key', or null if 'key' is not found.
	*/
	public static Object getProperty(String key) {
		if (properties==null)
			return null;
		else
			return properties.get(key);
	}
	
	public static boolean statusBarProtected() {
		return protectStatusBar;
	}
	
	public static void protectStatusBar(boolean protect) {
		protectStatusBar = protect;
		if (!protectStatusBar)
			statusBarThread = null;
	}

}