File: ImageProcessor.java

package info (click to toggle)
skyview 3.4.2%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,176 kB
  • sloc: java: 29,396; makefile: 18; sh: 1
file content (2717 lines) | stat: -rw-r--r-- 86,257 bytes parent folder | download | duplicates (4)
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
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
package ij.process;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import ij.gui.*;
import ij.util.*;
import ij.plugin.filter.GaussianBlur;
import ij.plugin.Binner;
import ij.process.AutoThresholder.Method;
import ij.gui.Roi;
import ij.gui.ShapeRoi;
import ij.gui.Overlay;
import ij.Prefs;

/**
This abstract class is the superclass for classes that process
the four data types (byte, short, float and RGB) supported by ImageJ.
An ImageProcessor contains the pixel data of a 2D image and
some basic methods to manipulate it.
@see ByteProcessor
@see ShortProcessor
@see FloatProcessor
@see ColorProcessor
@see ij.ImagePlus
@see ij.ImageStack
*/
public abstract class ImageProcessor implements Cloneable {

	/** Value of pixels included in masks. */
	public static final int BLACK = 0xFF000000;
	
	/** Value returned by getMinThreshold() when thresholding is not enabled. */
	public static final double NO_THRESHOLD = -808080.0;
		
	/** Left justify text. */
	public static final int LEFT_JUSTIFY = 0;
	/** Center justify text. */
	public static final int CENTER_JUSTIFY = 1;
	/** Right justify text. */
	public static final int RIGHT_JUSTIFY = 2;
	
	/** Isodata thresholding method */
	public static final int ISODATA = 0;

	/** Modified isodata method used in Image/Adjust/Threshold tool */
	public static final int ISODATA2 = 1;
	
	/** Interpolation methods */
	public static final int NEAREST_NEIGHBOR=0, NONE=0, BILINEAR=1, BICUBIC=2;

	public static final int BLUR_MORE=0, FIND_EDGES=1, MEDIAN_FILTER=2, MIN=3, MAX=4, CONVOLVE=5;
	static public final int RED_LUT=0, BLACK_AND_WHITE_LUT=1, NO_LUT_UPDATE=2, OVER_UNDER_LUT=3;
	static final int INVERT=0, FILL=1, ADD=2, MULT=3, AND=4, OR=5,
		XOR=6, GAMMA=7, LOG=8, MINIMUM=9, MAXIMUM=10, SQR=11, SQRT=12, EXP=13, ABS=14, SET=15;
	static final String WRONG_LENGTH = "width*height!=pixels.length";
	
	int fgColor = 0;
	protected int lineWidth = 1;
	protected int cx, cy; //current drawing coordinates
	protected Font font = ij.ImageJ.SansSerif12;
	protected FontMetrics fontMetrics;
	protected boolean antialiasedText;
	protected boolean boldFont;
	private static String[] interpolationMethods;
	// Over/Under tresholding colors
	private static int overRed, overGreen=255, overBlue;
	private static int underRed, underGreen, underBlue=255;
	private static boolean useBicubic;
	private int sliceNumber;
	private Overlay overlay;
		
    ProgressBar progressBar;
	protected int width, snapshotWidth;
	protected int height, snapshotHeight;
	protected int roiX, roiY, roiWidth, roiHeight;
	protected int xMin, xMax, yMin, yMax;
	boolean snapshotCopyMode;
	ImageProcessor mask;
	protected ColorModel baseCM; // base color model
	protected ColorModel cm;
	protected byte[] rLUT1, gLUT1, bLUT1; // base LUT
	protected byte[] rLUT2, gLUT2, bLUT2; // LUT as modified by setMinAndMax and setThreshold
	protected boolean interpolate;  // replaced by interpolationMethod
	protected int interpolationMethod = NONE;
	protected double minThreshold=NO_THRESHOLD, maxThreshold=NO_THRESHOLD;
	protected int histogramSize = 256;
	protected double histogramMin, histogramMax;
	protected float[] cTable;
	protected boolean lutAnimation;
	protected MemoryImageSource source;
	protected Image img;
	protected boolean newPixels;
        /** Updated by TAM (original change to 138k, updated June 19,2014) */
//	protected Color drawingColor = Color.black;
	protected Color drawingColor = Color.white;
	protected int clipXMin, clipXMax, clipYMin, clipYMax; // clip rect used by drawTo, drawLine, drawDot and drawPixel 
	protected int justification = LEFT_JUSTIFY;
	protected int lutUpdateMode;
	protected WritableRaster raster;
	protected BufferedImage image;
	protected BufferedImage fmImage;
	protected ColorModel cm2;
	protected SampleModel sampleModel;
	protected static IndexColorModel defaultColorModel;
	protected boolean minMaxSet;
        
        /* ------ Code inserted by TAM.  Originally added to 138k
         * ------ Updated to 149b June 19, 2014
         * Adds capabilities to write strings at any angle into an image.
         * Allows us to ensure that a given color is available for writing
         * but only adds it to color table if not already there.
         */

     private class PlotString {
 	
 	String text;
 	double x;
 	double y;
 	double angle;
 	PlotString(String text, double x, double y, double angle) {
 	    this.text = text;
 	    this.x = x;
 	    this.y = y;
 	    this.angle = angle;
 	}
     }
     
     private java.util.ArrayList<PlotString> plotStrings;
     
 		
     public void addPlotString(String str, double x, double y, double angle) {
 	if (plotStrings == null) {
 	    plotStrings = new ArrayList<PlotString>();
 	}
 	plotStrings.add(new PlotString(str, x, y, angle));
     }
     
     public void clearPlotStrings() {
 	if (plotStrings != null) {
 	    plotStrings.clear();
 	}
     }
     
     public void plotStrings() {
 	
 	if (plotStrings != null  && plotStrings.size() > 0) {
 	    Image img;
 	    if (getColorModel() instanceof IndexColorModel) {
 	        img              = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, (IndexColorModel) getColorModel());
 	    } else {
 	        img              = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
 	    }
 	    Graphics2D g           = (Graphics2D) img.getGraphics();
 	    g.drawImage(this.createImage(), 0, 0, null);
 	    g.setColor(drawingColor);
 	    if (font==null)
 		font = new Font("SansSerif", Font.PLAIN, 12);
 	    g.setFont(font);
             FontMetrics metrics    = g.getFontMetrics(font);
 	    int fontHeight         = metrics.getHeight();
 	    int descent            = metrics.getDescent();
 	    
 	    for (PlotString ps: plotStrings) {
 		
 		String text = ps.text;
 		
 		int x = (int)(ps.x+0.5);
 		int y = (int)(ps.y+0.5);
 		
 		double angle = ps.angle;
 		
 	        if (ps.angle != 0) {
 	            g.rotate(angle, x, y);
 	        }
 	    
 	        int sWidth = metrics.stringWidth(text);
 	        g.drawString(text, x-sWidth/2, y);
 		
 	        if (angle != 0) {
 	            g.rotate(-angle, x, y);
 	        }
 	    }
 	    
 	    g.dispose();
 	    ImageProcessor ip = new ByteProcessor(img);
 	    if (this instanceof ByteProcessor) {
 	        ip = ip.convertToByte(false);
 //	        if (isInvertedLut())
 //	 	    ip.invert();
 	    }
 	    insert(ip, 0, 0);
 	}
 	clearPlotStrings();
     }


     /** Adds the color to the end of the color model
      *  if not already there.
      */
     public boolean addColor(Color color) {
 	return addColor(color, -1);
     }
     
     /** Adds the color to the color model at the specified
      *  index.
      */
     public boolean addColor(Color color, int index) {	
    	if (color == null) {
 	    return false;
 	}
 	
 	int red   = color.getRed();
 	int green = color.getGreen();
 	int blue  = color.getBlue();
 	
 	int best = getBestIndex(color);
 	
         IndexColorModel icm;
 	if (cm==null)
 	    makeDefaultColorModel();
 	
 	if (minThreshold != NO_THRESHOLD) {
 	    
 	    double saveMin = getMinThreshold(); 
 	    double saveMax = getMaxThreshold();
 	    resetThreshold();
 	    icm = (IndexColorModel)cm;
 	    setThreshold(saveMin, saveMax, lutUpdateMode);
 	    
 	} else
     	    icm = (IndexColorModel)cm;
 	
 	int mapSize = icm.getMapSize();
 	byte[] rLUT = new byte[mapSize];
     	byte[] gLUT = new byte[mapSize];
 	byte[] bLUT = new byte[mapSize];
 	
     	icm.getReds(rLUT); 
     	icm.getGreens(gLUT); 
     	icm.getBlues(bLUT); 
 	
 	for (int i=0; i<mapSize; i += 1) {
 	    if (red ==  rLUT[i] && green == gLUT[i] && blue == bLUT[i]) {
 		return false;
 	    }
 	}
 	if (index < 0 || index >= mapSize) {
 	    index = mapSize-1;
 	}
 	rLUT[index] = (byte) red;
 	gLUT[index] = (byte) green;
 	bLUT[index] = (byte) blue;
 	
 	setColorModel(new IndexColorModel(8, mapSize, rLUT, gLUT, bLUT));
 	return true;
     }

		
	protected void showProgress(double percentDone) {
		if (progressBar!=null)
        	progressBar.show(percentDone);
	}

	/** @deprecated */
	protected void hideProgress() {
		showProgress(1.0);
	}
		
	/** Returns the width of this image in pixels. */
	public int getWidth() {
		return width;
	}
	
	/** Returns the height of this image in pixels. */
	public int getHeight() {
		return height;
	}
	
    /** Returns the bit depth, 8, 16, 24 (RGB) or 32. RGB images actually use 32 bits per pixel. */
    public int getBitDepth() {
    	Object pixels = getPixels();
    	if (pixels==null)
    		return 0;
    	else if (pixels instanceof byte[])
    		return 8;
    	else if (pixels instanceof short[])
    		return 16;
    	else if (pixels instanceof int[])
    		return 24;
    	else if (pixels instanceof float[])
    		return 32;
    	else
    		return 0;
    }
    
    /** Returns this processor's color model. For non-RGB processors,
 		this is the base lookup table (LUT), not the one that may have
		been modified by setMinAndMax() or setThreshold(). */
	public ColorModel getColorModel() {
		if (cm==null)
			makeDefaultColorModel();
		if (baseCM!=null)
			return baseCM;
		else
			return cm;
	}
	
	/** Returns the current color model, which may have
		been modified by setMinAndMax() or setThreshold(). */
	public ColorModel getCurrentColorModel() {
		if (cm==null) makeDefaultColorModel();
		return cm;
	}

	/** Sets the color model. Must be an IndexColorModel (aka LUT)
		for all processors except the ColorProcessor. */
	public void setColorModel(ColorModel cm) {
		if (cm!=null && !(cm instanceof IndexColorModel))
			throw new IllegalArgumentException("IndexColorModel required");
		this.cm = cm;
		baseCM = null;
		rLUT1 = rLUT2 = null;
		newPixels = true;
		inversionTested = false;
		minThreshold = NO_THRESHOLD;
		source = null;
	}

	public LUT getLut() {
		ColorModel cm2 = getColorModel();
		if (cm2!=null && (cm2 instanceof IndexColorModel))
			return new LUT((IndexColorModel)cm2, getMin(), getMax());
		else
			return null;
	}
	
	public void setLut(LUT lut) {
		setColorModel(lut);
		if (lut!=null && (lut.min!=0.0||lut.max!=0.0))
			setMinAndMax(lut.min, lut.max);
	}


	protected void makeDefaultColorModel() {
		cm = getDefaultColorModel();
	}

	/** Inverts the values in this image's LUT (indexed color model).
		Does nothing if this is a ColorProcessor. */
	public void invertLut() {
		IndexColorModel icm = (IndexColorModel)getColorModel();
		int mapSize = icm.getMapSize();
		byte[] reds = new byte[mapSize];
		byte[] greens = new byte[mapSize];
		byte[] blues = new byte[mapSize];	
		byte[] reds2 = new byte[mapSize];
		byte[] greens2 = new byte[mapSize];
		byte[] blues2 = new byte[mapSize];	
		icm.getReds(reds); 
		icm.getGreens(greens); 
		icm.getBlues(blues);
		for (int i=0; i<mapSize; i++) {
			reds2[i] = (byte)(reds[mapSize-i-1]&255);
			greens2[i] = (byte)(greens[mapSize-i-1]&255);
			blues2[i] = (byte)(blues[mapSize-i-1]&255);
		}
		ColorModel cm = new IndexColorModel(8, mapSize, reds2, greens2, blues2);
		double min=getMin(), max=getMax();
		setColorModel(cm);
		setMinAndMax(min, max);
	}

	/** Returns the LUT index that's the best match for this color. */
	public int getBestIndex(Color c) {
    	IndexColorModel icm;
		if (cm==null)
			makeDefaultColorModel();
		if (minThreshold!=NO_THRESHOLD) {
			double saveMin = getMinThreshold(); 
			double saveMax = getMaxThreshold();
			resetThreshold();
			icm = (IndexColorModel)cm;
			setThreshold(saveMin, saveMax, lutUpdateMode);
		} else
    		icm = (IndexColorModel)cm;
		int mapSize = icm.getMapSize();
		byte[] rLUT = new byte[mapSize];
    	byte[] gLUT = new byte[mapSize];
		byte[] bLUT = new byte[mapSize];
    	icm.getReds(rLUT); 
    	icm.getGreens(gLUT); 
    	icm.getBlues(bLUT); 
		int minDistance = Integer.MAX_VALUE;
		int distance;
		int minIndex = 0;
		int r1=c.getRed();
		int g1=c.getGreen();
		int b1=c.getBlue();
		int r2,b2,g2;
    	for (int i=0; i<mapSize; i++) {
			r2 = rLUT[i]&0xff; g2 = gLUT[i]&0xff; b2 = bLUT[i]&0xff;
    		distance = (r2-r1)*(r2-r1)+(g2-g1)*(g2-g1)+(b2-b1)*(b2-b1);
			//ij.IJ.write(i+" "+minIndex+" "+distance+" "+(rLUT[i]&255));
    		if (distance<minDistance) {
    			minDistance = distance;
    			minIndex = i;
    		}
    		if (minDistance==0.0)
    			break;
    	}
    	return minIndex;
	}

	protected boolean inversionTested = false;
	protected boolean invertedLut;
	
	/** Returns true if this image uses an inverting LUT
		that displays zero as white and 255 as black. */
	public boolean isInvertedLut() {
		if (inversionTested)
			return invertedLut;
		if (cm==null || !(cm instanceof IndexColorModel)) {
			invertedLut = false;
			inversionTested = true;
			return invertedLut;
		}
		IndexColorModel icm = (IndexColorModel)cm;
		boolean hasAscendingStep = false;
		int v1, v2;
		for (int i=1; i<255; i++) {
			v1 = icm.getRed(i-1)+icm.getGreen(i-1)+icm.getBlue(i-1);
			v2 = icm.getRed(i)+icm.getGreen(i)+icm.getBlue(i);
			if (v1<v2) {
				hasAscendingStep = true;
				break;
			}
		}
		invertedLut = !hasAscendingStep;
		inversionTested = true;
		return invertedLut;
	}
	
	/** Returns 'true' if this is an image with a grayscale LUT or an
	 * RGB image with identical red, green and blue channels.
	*/
	public boolean isGrayscale() {
		return !isColorLut();
	}

	/** Returns true if this image uses a color LUT. */
	public boolean isColorLut() {
		if (cm==null || !(cm instanceof IndexColorModel))
			return false;
    	IndexColorModel icm = (IndexColorModel)cm;
		int mapSize = icm.getMapSize();
		byte[] reds = new byte[mapSize];
		byte[] greens = new byte[mapSize];
		byte[] blues = new byte[mapSize];	
		icm.getReds(reds); 
		icm.getGreens(greens); 
		icm.getBlues(blues);
		boolean isColor = false;
		for (int i=0; i<mapSize; i++) {
			if ((reds[i] != greens[i]) || (greens[i] != blues[i])) {
				isColor = true;
				break;
			}
		}
		return isColor;
	}

	/** Returns true if this image uses a pseudocolor or grayscale LUT, 
		in other words, is this an image that can be filtered. */
    public boolean isPseudoColorLut() {
		if (cm==null || !(cm instanceof IndexColorModel))
			return false;
		if (getMinThreshold()!=NO_THRESHOLD)
			return true;
    	IndexColorModel icm = (IndexColorModel)cm;
		int mapSize = icm.getMapSize();
		if (mapSize!=256)
			return false;
		byte[] reds = new byte[mapSize];
		byte[] greens = new byte[mapSize];
		byte[] blues = new byte[mapSize];	
		icm.getReds(reds); 
		icm.getGreens(greens); 
		icm.getBlues(blues);
		int r, g, b, d;
		int r2=reds[0]&255, g2=greens[0]&255, b2=blues[0]&255;
		double sum=0.0, sum2=0.0;
		for (int i=0; i<mapSize; i++) {
			r=reds[i]&255; g=greens[i]&255; b=blues[i]&255;
			d=r-r2; sum+=d; sum2+=d*d;
			d=g-g2; sum+=d; sum2+=d*d;
			d=b-b2; sum+=d; sum2+=d*d;
			r2=r; g2=g; b2=b;
		}
		double stdDev = (768*sum2-sum*sum)/768.0;
		if (stdDev>0.0)
			stdDev = Math.sqrt(stdDev/(767.0));
		else
			stdDev = 0.0;
		boolean isPseudoColor = stdDev<20.0;
		if ((int)stdDev==67) isPseudoColor = true; // "3-3-2 RGB" LUT
		if (ij.IJ.debugMode)
			ij.IJ.log("isPseudoColorLut: "+(isPseudoColor) + " " + stdDev);
		return isPseudoColor;
	}
	
	/** Returns true if the image is using the default grayscale LUT. */
	public boolean isDefaultLut() {
		if (cm==null)
			makeDefaultColorModel();
		if (!(cm instanceof IndexColorModel))
			return false;
    	IndexColorModel icm = (IndexColorModel)cm;
		int mapSize = icm.getMapSize();
		if (mapSize!=256)
			return false;
		byte[] reds = new byte[mapSize];
		byte[] greens = new byte[mapSize];
		byte[] blues = new byte[mapSize];	
		icm.getReds(reds); 
		icm.getGreens(greens); 
		icm.getBlues(blues);
		boolean isDefault = true;
		for (int i=0; i<mapSize; i++) {
			if ((reds[i]&255)!=i || (greens[i]&255)!=i || (blues[i]&255)!=i) {
				isDefault = false;
				break;
			}
		}
		return isDefault;
	}

	/** Sets the default fill/draw value to the pixel
		value closest to the specified color. */
	public abstract void setColor(Color color);

	/** Sets the default fill/draw value. */
	public void setColor(int value) {
		setValue(value);
	}

	/** Sets the default fill/draw value. */
	public void setColor(double value) {
		setValue(value);
	}

	/** Sets the default fill/draw value. */
	public abstract void setValue(double value);

	/** Sets the background fill value used by the rotate() and scale() methods. */
	public abstract void setBackgroundValue(double value);

	/** Returns the background fill value. */
	public abstract double getBackgroundValue();

	/** Returns the smallest displayed pixel value. */
	public abstract double getMin();

	/** Returns the largest displayed pixel value. */
	public abstract double getMax();

	/** This image will be displayed by mapping pixel values in the
		range min-max to screen values in the range 0-255. For
		byte images, this mapping is done by updating the LUT. For
		short and float images, it's done by generating 8-bit AWT
		images. For RGB images, it's done by changing the pixel values. */
	public abstract void setMinAndMax(double min, double max);

	/** For short and float images, recalculates the min and max
		image values needed to correctly display the image. For
		ByteProcessors, resets the LUT. */
	public void resetMinAndMax() {}

	/** Sets the lower and upper threshold levels. The 'lutUpdate' argument
		can be RED_LUT, BLACK_AND_WHITE_LUT, OVER_UNDER_LUT or NO_LUT_UPDATE.
		Thresholding of RGB images is not supported. */
	public void setThreshold(double minThreshold, double maxThreshold, int lutUpdate) {
		//ij.IJ.log("setThreshold: "+" "+minThreshold+" "+maxThreshold+" "+lutUpdate);
		if (this instanceof ColorProcessor)
			return;
		this.minThreshold = minThreshold;
		this.maxThreshold = maxThreshold;
		lutUpdateMode = lutUpdate;

		if (minThreshold==NO_THRESHOLD) {
			resetThreshold();
			return;
		}

		if (lutUpdate==NO_LUT_UPDATE)
			return;
		if (rLUT1==null) {
			if (cm==null)
				makeDefaultColorModel();
			baseCM = cm;
			IndexColorModel m = (IndexColorModel)cm;
			rLUT1 = new byte[256]; gLUT1 = new byte[256]; bLUT1 = new byte[256];
			m.getReds(rLUT1); m.getGreens(gLUT1); m.getBlues(bLUT1);
			rLUT2 = new byte[256]; gLUT2 = new byte[256]; bLUT2 = new byte[256];
		}
		int t1 = (int)minThreshold;
		int t2 = (int)maxThreshold;
		int index;
		if (lutUpdate==RED_LUT)
			for (int i=0; i<256; i++) {
				if (i>=t1 && i<=t2) {
					rLUT2[i] = (byte)255;
					gLUT2[i] = (byte)0;
					bLUT2[i] = (byte)0;
				} else {
					rLUT2[i] = rLUT1[i];
					gLUT2[i] = gLUT1[i];
					bLUT2[i] = bLUT1[i];
				}
			}

		else if (lutUpdate==BLACK_AND_WHITE_LUT) {
			// updated in v1.43i by Gabriel Lindini to use blackBackground setting

			byte  foreground = Prefs.blackBackground?(byte)255:(byte)0;

			byte background = (byte)(255 - foreground);

			for (int i=0; i<256; i++) {

				if (i>=t1 && i<=t2) {

					rLUT2[i] = foreground;

					gLUT2[i] = foreground;

					bLUT2[i] = foreground;

				} else {

					rLUT2[i] = background;

					gLUT2[i] =background;

					bLUT2[i] =background;

				}

			}
		} else {
			for (int i=0; i<256; i++) {
				if (i>=t1 && i<=t2) {
					rLUT2[i] = rLUT1[i];
					gLUT2[i] = gLUT1[i];
					bLUT2[i] = bLUT1[i];

				} else if (i>t2) {
					rLUT2[i] = (byte)overRed;
					gLUT2[i] = (byte)overGreen;
					bLUT2[i] = (byte)overBlue;
				} else { 
					rLUT2[i] = (byte)underRed;
					gLUT2[i] = (byte)underGreen; 
					bLUT2[i] = (byte)underBlue;
				}
			}
		}

		cm = new IndexColorModel(8, 256, rLUT2, gLUT2, bLUT2);
		newPixels = true;
		source = null;
	}
	
	public void setAutoThreshold(String mString) {
		if (mString==null)
			throw new IllegalArgumentException("Null method");
		boolean darkBackground = mString.indexOf("dark")!=-1;
		int index = mString.indexOf(" ");
		if (index!=-1)
			mString = mString.substring(0, index);
		setAutoThreshold(mString, darkBackground, RED_LUT);
	}
	
	public void setAutoThreshold(String mString, boolean darkBackground, int lutUpdate) {
		Method m = null;
		try {
			m = Method.valueOf(Method.class, mString);
		} catch(Exception e) {
			m = null;
		}
		if (m==null)
			throw new IllegalArgumentException("Invalid method (\""+mString+"\")");
		setAutoThreshold(m, darkBackground, lutUpdate);
	}

	public void setAutoThreshold(Method method, boolean darkBackground) {
		setAutoThreshold(method, darkBackground, RED_LUT);
	}

	public void setAutoThreshold(Method method, boolean darkBackground, int lutUpdate) {
		if (method==null || (this instanceof ColorProcessor))
			return;
		double min=0.0, max=0.0;
		boolean notByteData = !(this instanceof ByteProcessor);
		ImageProcessor ip2 = this;
		if (notByteData) {
			ImageProcessor mask = ip2.getMask();
			Rectangle rect = ip2.getRoi();
			resetMinAndMax();
			min = getMin(); max = getMax();
			ip2 = convertToByte(true);
			ip2.setMask(mask);
			ip2.setRoi(rect);	
		}
		ImageStatistics stats = ip2.getStatistics();
		AutoThresholder thresholder = new AutoThresholder();
		int threshold = thresholder.getThreshold(method, stats.histogram);
		double lower, upper;
		if (darkBackground) {
			if (isInvertedLut())
				{lower=0.0; upper=threshold;}
			else
				{lower=threshold+1; upper=255.0;}
		} else {
			if (isInvertedLut())
				{lower=threshold+1; upper=255.0;}
			else
				{lower=0.0; upper=threshold;}
		}
		if (lower>255) lower = 255;
		if (notByteData) {
			if (max>min) {
				lower = min + (lower/255.0)*(max-min);
				upper = min + (upper/255.0)*(max-min);
			} else
				lower = upper = min;
		}
		setThreshold(lower, upper, lutUpdate);
	}

	/** Automatically sets the lower and upper threshold levels, where 'method'
		 must be ISODATA or ISODATA2 and 'lutUpdate' must be RED_LUT,
		 BLACK_AND_WHITE_LUT, OVER_UNDER_LUT or NO_LUT_UPDATE.
	*/
	public void setAutoThreshold(int method, int lutUpdate) {
		if (method<0 || method>ISODATA2)
			throw new IllegalArgumentException("Invalid thresholding method");
		if (this instanceof ColorProcessor)
			return;
		double min=0.0, max=0.0;
		boolean notByteData = !(this instanceof ByteProcessor);
		ImageProcessor ip2 = this;
		if (notByteData) {
			ImageProcessor mask = ip2.getMask();
			Rectangle rect = ip2.getRoi();
			resetMinAndMax();
			min = getMin(); max = getMax();
			ip2 = convertToByte(true);
			ip2.setMask(mask);
			ip2.setRoi(rect);	
		}
		ImageStatistics stats = ip2.getStatistics();
		int[] histogram = stats.histogram;
		int originalModeCount = histogram[stats.mode];
		if (method==ISODATA2) {
			int maxCount2 = 0;
			for (int i = 0; i<stats.nBins; i++) {
				if ((histogram[i] > maxCount2) && (i!=stats.mode))
					maxCount2 = histogram[i];
			}
			int hmax = stats.maxCount;
			if ((hmax>(maxCount2 * 2)) && (maxCount2 != 0)) {
				hmax = (int)(maxCount2 * 1.5);
				histogram[stats.mode] = hmax;
        		}
		}
		int threshold = ip2.getAutoThreshold(stats.histogram);
		histogram[stats.mode] = originalModeCount;
		float[] hist = new float[256];
		for (int i=0; i<256; i++)
			hist[i] = stats.histogram[i];
		FloatProcessor fp = new FloatProcessor(256, 1, hist, null);
		GaussianBlur gb = new GaussianBlur();
		gb.blur1Direction(fp, 2.0, 0.01, true, 0);
		float maxCount=0f, sum=0f, mean, count;
		int mode = 0;
		for (int i=0; i<256; i++) {
			count = hist[i];
			sum += count;
			if (count>maxCount) {
				maxCount = count;
				mode = i;
			}
		}
		double avg = sum/256.0;
		double lower, upper;
		if (maxCount/avg>1.5) {
			if ((stats.max-mode)>(mode-stats.min))
				{lower=threshold; upper=255.0;}
			else
				{lower=0.0; upper=threshold;}
		} else {
			if (isInvertedLut())
				{lower=threshold; upper=255.0;}
			else
				{lower=0.0; upper=threshold;}
		}
		if (notByteData) {
			if (max>min) {
				lower = min + (lower/255.0)*(max-min);
				upper = min + (upper/255.0)*(max-min);
			} else
				lower = upper = min;
		}
		setThreshold(lower, upper, lutUpdate);
		//if (notByteData && lutUpdate!=NO_LUT_UPDATE)
		//	setLutAnimation(true);
	}

	/** Disables thresholding. */
	public void resetThreshold() {
		minThreshold = NO_THRESHOLD;
		if (baseCM!=null) {
			cm = baseCM;
			baseCM = null;
		}
		rLUT1 = rLUT2 = null;
		inversionTested = false;
		newPixels = true;
		source = null;
	}

	/** Returns the lower threshold level. Returns NO_THRESHOLD
		if thresholding is not enabled. */
	public double getMinThreshold() {
		return minThreshold;
	}

	/** Returns the upper threshold level. */
	public double getMaxThreshold() {
		return maxThreshold;
	}
	
	/** Returns the LUT update mode, which can be RED_LUT, BLACK_AND_WHITE_LUT, 
		OVER_UNDER_LUT or NO_LUT_UPDATE. */
	public int getLutUpdateMode() {
		return lutUpdateMode;
	}

	/* Sets the threshold levels (non-visible) of an 8-bit mask based on
		the state of Prefs.blackBackground and isInvertedLut().
		@see ImageProcessor#resetBinaryThreshold	
	*/
	public void setBinaryThreshold() {
		//ij.IJ.log("setMaskThreshold1");
		if (!(this instanceof ByteProcessor)) return;
		double t1=255.0, t2=255.0;
		boolean invertedLut = isInvertedLut();
		if ((invertedLut&&ij.Prefs.blackBackground) || (!invertedLut&&!ij.Prefs.blackBackground)) {
			t1 = 0.0;
			t2 = 0.0;
		}
		//ij.IJ.log("setMaskThreshold2 "+t1+" "+t2);
		setThreshold(t1, t2, ImageProcessor.NO_LUT_UPDATE);
	}

	/** Resets the threshold if minThreshold=maxThreshold and lutUpdateMode=NO_LUT_UPDATE. 
		This removes the invisible threshold set by the MakeBinary and Convert to Mask commands.
		@see ImageProcessor#setBinaryThreshold	
	*/
	public void resetBinaryThreshold() {
		if (minThreshold==maxThreshold && lutUpdateMode==NO_LUT_UPDATE)
			resetThreshold();
	}

	/** Defines a rectangular region of interest and sets the mask 
		to null if this ROI is not the same size as the previous one. 
		@see ImageProcessor#resetRoi		
	*/
	public void setRoi(Rectangle roi) {
		if (roi==null)
			resetRoi();
		else
			setRoi(roi.x, roi.y, roi.width, roi.height);
	}

	/** Defines a rectangular region of interest and sets the mask to 
		null if this ROI is not the same size as the previous one. 
		@see ImageProcessor#resetRoi		
	*/
	public void setRoi(int x, int y, int rwidth, int rheight) {
		if (x<0 || y<0 || x+rwidth>width || y+rheight>height) {
			//find intersection of roi and this image
			Rectangle r1 = new Rectangle(x, y, rwidth, rheight);
			Rectangle r2 = r1.intersection(new Rectangle(0, 0, width, height));
			if (r2.width<=0 || r2.height<=0) {
				roiX=0; roiY=0; roiWidth=0; roiHeight=0;
				xMin=0; xMax=0; yMin=0; yMax=0;
				mask=null;
				return;
			}
			if (mask!=null && mask.getWidth()==rwidth && mask.getHeight()==rheight) {
				Rectangle r3 = new Rectangle(0, 0, r2.width, r2.height);
				if (x<0) r3.x = -x;
				if (y<0) r3.y = -y;
				mask.setRoi(r3);
				mask = mask.crop();
			}
			roiX=r2.x; roiY=r2.y; roiWidth=r2.width; roiHeight=r2.height;
		} else {
			roiX=x; roiY=y; roiWidth=rwidth; roiHeight=rheight;
		}
		if (mask!=null && (mask.getWidth()!=roiWidth||mask.getHeight()!=roiHeight))
			mask = null;
		//setup limits for 3x3 filters
		xMin = Math.max(roiX, 1);
		xMax = Math.min(roiX + roiWidth - 1, width - 2);
		yMin = Math.max(roiY, 1);
		yMax = Math.min(roiY + roiHeight - 1, height - 2);
	}
	
	/** Defines a non-rectangular region of interest that will consist of a
		rectangular ROI and a mask. After processing, call <code>reset(mask)</code>
		to restore non-masked pixels. Here is an example:
		<pre>
		ip.setRoi(new OvalRoi(50, 50, 100, 50));
		ip.fill();
		ip.reset(ip.getMask());
		</pre>
		The example assumes <code>snapshot()</code> has been called, which is the case
		for code executed in the <code>run()</code> method of plugins that implement the 
		<code>PlugInFilter</code> interface.
		@see ij.ImagePlus#getRoi
	*/
	public void setRoi(Roi roi) {
		if (roi==null)
			resetRoi();
		else {
			if (roi instanceof PointRoi && ((PointRoi)roi).getNCoordinates()==1) {
				setMask(null);
				FloatPolygon p = roi.getFloatPolygon();
				setRoi((int)p.xpoints[0], (int)p.ypoints[0], 1, 1);
			} else {
				setMask(roi.getMask());
				setRoi(roi.getBounds());
			}
		}
	}

	/** Defines a polygonal region of interest that will consist of a
		rectangular ROI and a mask. After processing, call <code>reset(mask)</code>
		to restore non-masked pixels. Here is an example:
		<pre>
		Polygon p = new Polygon();
		p.addPoint(50, 0); p.addPoint(100, 100); p.addPoint(0, 100);
		ip.setRoi(triangle);
		ip.invert();
		ip.reset(ip.getMask());
		</pre>
		The example assumes <code>snapshot()</code> has been called, which is the case
		for code executed in the <code>run()</code> method of plugins that implement the 
		<code>PlugInFilter</code> interface.
		@see ij.gui.Roi#getPolygon
		@see ImageProcessor#drawPolygon
		@see ImageProcessor#fillPolygon
	*/
	public void setRoi(Polygon roi) {
		if (roi==null)
			{resetRoi(); return;}
		Rectangle bounds = roi.getBounds();
		for (int i=0; i<roi.npoints; i++) {
			roi.xpoints[i] -= bounds.x;
			roi.ypoints[i] -= bounds.y;
		}
		PolygonFiller pf = new PolygonFiller();
		pf.setPolygon(roi.xpoints, roi.ypoints, roi.npoints);
		ImageProcessor mask = pf.getMask(bounds.width, bounds.height);
		setMask(mask);
		setRoi(bounds);
		for (int i=0; i<roi.npoints; i++) {
			roi.xpoints[i] += bounds.x;
			roi.ypoints[i] += bounds.y;
		}
	}

	/** Sets the ROI (Region of Interest) and clipping rectangle to the entire image. */
	public void resetRoi() {
		roiX=0; roiY=0; roiWidth=width; roiHeight=height;
		xMin=1; xMax=width-2; yMin=1; yMax=height-2;
		mask=null;
		clipXMin=0; clipXMax=width-1; clipYMin=0; clipYMax=height-1; 
	}

	/** Returns a Rectangle that represents the current
		region of interest. */
	public Rectangle getRoi() {
		return new Rectangle(roiX, roiY, roiWidth, roiHeight);
	}

	/** Defines a byte mask that limits processing to an
		irregular ROI. Background pixels in the mask have
		a value of zero. */
	public void setMask(ImageProcessor mask) {
		this.mask = mask;
	}

	/** For images with irregular ROIs, returns a mask, otherwise, 
		returns null. Pixels outside the mask have a value of zero. */
	public ImageProcessor getMask() {
		return mask;
	}

	/** Returns a reference to the mask pixel array, or null if there is no mask. */
	public byte[] getMaskArray() {
		return mask!=null?(byte[])mask.getPixels():null;
	}

	/** Assigns a progress bar to this processor. Set 'pb' to
		null to disable the progress bar. */
	public void setProgressBar(ProgressBar pb) {
		progressBar = pb;
	}

	/** This method has been replaced by setInterpolationMethod(). */
	public void setInterpolate(boolean interpolate) {
		this.interpolate = interpolate;
		if (interpolate)
			interpolationMethod = useBicubic?BICUBIC:BILINEAR;
		else
			interpolationMethod = NONE;
	}
	
	/** Use this method to set the interpolation method (NONE, 
		 BILINEAR or BICUBIC) used by scale(), resize() and rotate(). */
	public void setInterpolationMethod(int method) {
		if (method<NONE || method>BICUBIC)
			throw new IllegalArgumentException("Invalid interpolation method");
		interpolationMethod = method;
		interpolate = method!=NONE?true:false;
	}
	
	/** Returns the current interpolation method (NONE, BILINEAR or BICUBIC). */
	public int getInterpolationMethod() {
		return interpolationMethod;
	}
	
	public static String[] getInterpolationMethods() {
		if (interpolationMethods==null)
			interpolationMethods = new String[] {"None", "Bilinear", "Bicubic"};
		return interpolationMethods;
	}

	/** Returns the value of the interpolate field. */
	public boolean getInterpolate() {
		return interpolate;
	}

	/** @deprecated */
	public boolean isKillable() {
		return false;
	}

	private void process(int op, double value) {
		double SCALE = 255.0/Math.log(255.0);
		int v;
		
		int[] lut = new int[256];
		for (int i=0; i<256; i++) {
			switch(op) {
				case INVERT:
					v = 255 - i;
					break;
				case FILL:
					v = fgColor;
					break;
				case SET:
					v = (int)value;
					break;
				case ADD:
					v = i + (int)value;
					break;
				case MULT:
					v = (int)Math.round(i * value);
					break;
				case AND:
					v = i & (int)value;
					break;
				case OR:
					v = i | (int)value;
					break;
				case XOR:
					v = i ^ (int)value;
					break;
				case GAMMA:
					v = (int)(Math.exp(Math.log(i/255.0)*value)*255.0);
					break;
				case LOG:
					if (i==0)
						v = 0;
					else
						v = (int)(Math.log(i) * SCALE);
					break;
				case EXP:
					v = (int)(Math.exp(i/SCALE));
					break;
				case SQR:
						v = i*i;
					break;
				case SQRT:
						v = (int)Math.sqrt(i);
					break;
				case MINIMUM:
					if (i<value)
						v = (int)value;
					else
						v = i;
					break;
				case MAXIMUM:
					if (i>value)
						v = (int)value;
					else
						v = i;
					break;
				 default:
				 	v = i;
			}
			if (v < 0)
				v = 0;
			if (v > 255)
				v = 255;
			lut[i] = v;
		}
		applyTable(lut);
    }

	/**
		Returns an array containing the pixel values along the
		line starting at (x1,y1) and ending at (x2,y2). For byte
		and short images, returns calibrated values if a calibration
		table has been set using setCalibrationTable().
		@see ImageProcessor#setInterpolate
	*/
	public double[] getLine(double x1, double y1, double x2, double y2) {
		double dx = x2-x1;
		double dy = y2-y1;
		int n = (int)Math.round(Math.sqrt(dx*dx + dy*dy));
		double xinc = dx/n;
		double yinc = dy/n;
		if (!((xinc==0&&n==height) || (yinc==0&&n==width)))
			n++;
		double[] data = new double[n];
		double rx = x1;
		double ry = y1;
		if (interpolate) {
			for (int i=0; i<n; i++) {
				data[i] = getInterpolatedValue(rx, ry);
				rx += xinc;
				ry += yinc;
			}
		} else {
			for (int i=0; i<n; i++) {
				data[i] = getPixelValue((int)(rx+0.5), (int)(ry+0.5));
				rx += xinc;
				ry += yinc;
			}
		}
		return data;
	}
	
	/** Returns the pixel values along the horizontal line starting at (x,y). */
	public void getRow(int x, int y, int[] data, int length) {
		for (int i=0; i<length; i++)
			data[i] = getPixel(x++, y);
	}

	/** Returns the pixel values along the horizontal line starting at (x,y). */
	public float[] getRow(int x, int y, float[] data, int length) {
		if (data==null)
			data = new float[length];
		for (int i=0; i<length; i++)
			data[i] = getf(x++, y);
		return data;
	}

	/** Returns the pixel values down the column starting at (x,y). */
	public void getColumn(int x, int y, int[] data, int length) {
		for (int i=0; i<length; i++)
			data[i] = getPixel(x, y++);
	}

	/** Inserts the pixels contained in 'data' into a 
		horizontal line starting at (x,y). */
	public void putRow(int x, int y, int[] data, int length) {
		for (int i=0; i<length; i++)
			putPixel(x++, y, data[i]);
	}
	
	/** Inserts the pixels contained in 'data' into a 
		horizontal line starting at (x,y). */
	public void putRow(int x, int y, float[] data, int length) {
		for (int i=0; i<length; i++)
			setf(x++, y, data[i]);
	}

	/** Inserts the pixels contained in 'data' into a 
		column starting at (x,y). */
	public void putColumn(int x, int y, int[] data, int length) {
		//if (x>=0 && x<width && y>=0 && (y+length)<=height)
		//	((ShortProcessor)this).putColumn2(x, y, data, length);
		//else 
			for (int i=0; i<length; i++)
				putPixel(x, y++, data[i]);
	}

	/**
	Sets the current drawing location.
	@see ImageProcessor#lineTo
	@see ImageProcessor#drawString
	*/
	public void moveTo(int x, int y) {
		cx = x;
		cy = y;
	}
	
	/** Sets the line width used by lineTo() and drawDot(). */
	public void setLineWidth(int width) {
		lineWidth = width;
		if (lineWidth<1) lineWidth = 1;
	}
				
	/** Returns the current line width. */
	public int getLineWidth() {
		return lineWidth;
	}

	/** Draws a line from the current drawing location to (x2,y2). */
	public void lineTo(int x2, int y2) {
		int dx = x2-cx;
		int dy = y2-cy;
		int absdx = dx>=0?dx:-dx;
		int absdy = dy>=0?dy:-dy;
		int n = absdy>absdx?absdy:absdx;
		double xinc = (double)dx/n;
		double yinc = (double)dy/n;
		double x = cx;
		double y = cy;
		n++;
		cx = x2; cy = y2;
		if (n>1000000) return;
		do {
			if (lineWidth==1)
				drawPixel((int)Math.round(x), (int)Math.round(y));
			else if (lineWidth==2)
				drawDot2((int)Math.round(x), (int)Math.round(y));
			else
				drawDot((int)x, (int)y);
			x += xinc;
			y += yinc;
		} while (--n>0);
		//if (lineWidth>2) resetRoi();
	}
		
	/** Draws a line from (x1,y1) to (x2,y2). */
	public void drawLine(int x1, int y1, int x2, int y2) {
		moveTo(x1, y1);
		lineTo(x2, y2);
	}

	/** Draws a rectangle. */
	public void drawRect(int x, int y, int width, int height) {
		if (width<1 || height<1)
			return;
		if (lineWidth==1) {
			moveTo(x, y);
			lineTo(x+width-1, y);
			lineTo(x+width-1, y+height-1);
			lineTo(x, y+height-1);
			lineTo(x, y);
		} else {
			moveTo(x, y);
			lineTo(x+width, y);
			lineTo(x+width, y+height);
			lineTo(x, y+height);
			lineTo(x, y);
		}
	}

	/** Draws an elliptical shape. */
	public void drawOval(int x, int y, int width, int height) {
		if ((long)width*height>4*this.width*this.height) return;
		OvalRoi oval = new OvalRoi(x, y, width, height);
		drawPolygon(oval.getPolygon());
	}

	/** Fills an elliptical shape. */
	public void fillOval(int x, int y, int width, int height) {
		if ((long)width*height>4*this.width*this.height) return;
		OvalRoi oval = new OvalRoi(x, y, width, height);
		fillPolygon(oval.getPolygon());
	}

	/** Draws a polygon. */
	public void drawPolygon(Polygon p) {
		moveTo(p.xpoints[0], p.ypoints[0]);
		for (int i=0; i<p.npoints; i++)
			lineTo(p.xpoints[i], p.ypoints[i]);
		lineTo(p.xpoints[0], p.ypoints[0]);
	}

	/** Fills a polygon. */
	public void fillPolygon(Polygon p) {
		setRoi(p);
		fill(getMask());
		resetRoi();
	}

	/** @deprecated */
	public void drawDot2(int x, int y) {
		drawPixel(x, y);
		drawPixel(x-1, y);
		drawPixel(x, y-1);
		drawPixel(x-1, y-1);
	}
		
    /** Draws a dot using the current line width and fill/draw value. */
	public void drawDot(int xcenter, int ycenter) {
		double r = lineWidth/2.0;
		int xmin=(int)(xcenter-r+0.5), ymin=(int)(ycenter-r+0.5);
		int xmax=xmin+lineWidth, ymax=ymin+lineWidth;
		if (xmin<clipXMin || ymin<clipYMin || xmax>clipXMax || ymax>clipYMax ) {
			// draw edge dot
			double r2 = r*r;
			r -= 0.5;
			double xoffset=xmin+r, yoffset=ymin+r;
			double xx, yy;
			for (int y=ymin; y<ymax; y++) {
				for (int x=xmin; x<xmax; x++) {
					xx = x-xoffset; yy = y-yoffset;
					if (xx*xx+yy*yy<=r2)
						drawPixel(x, y);
				}
			}
		} else {
			if (dotMask==null || lineWidth!=dotMask.getWidth()) {
				OvalRoi oval = new OvalRoi(0, 0, lineWidth, lineWidth);
				dotMask = oval.getMask();
			}
			setRoi(xmin, ymin, lineWidth, lineWidth);
			fill(dotMask);
		}
	}
	
    private ImageProcessor dotMask;

	private void setupFontMetrics() {
		if (fmImage==null)
			fmImage=new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
		if (fontMetrics==null) {
			Graphics g = fmImage.getGraphics();
			fontMetrics = g.getFontMetrics(font);
		}
	}

	/** Draws a string at the current location using the current fill/draw value.
        Draws multiple lines if the string contains newline characters. */
	public void drawString(String s) {
		if (s==null || s.equals("")) return;
		setupFontMetrics();
		if (ij.IJ.isMacOSX()) s += " ";
		if (s.indexOf("\n")==-1)
			drawString2(s);
		else {
			String[] s2 = Tools.split(s, "\n");
			for (int i=0; i<s2.length; i++)
				drawString2(s2[i]);
		}
	}

	private void drawString2(String s) {
		int w =  getStringWidth(s);
		int cxx = cx;
		if (justification==CENTER_JUSTIFY)
			cxx -= w/2;
		else if (justification==RIGHT_JUSTIFY)
			cxx -= w;
		int h =  fontMetrics.getHeight();
		if (w<=0 || h<=0) return;
		Image bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.getGraphics();
		FontMetrics metrics = g.getFontMetrics(font);
		int fontHeight = metrics.getHeight();
		int descent = metrics.getDescent();
		g.setFont(font);

		if (antialiasedText && cxx>=00 && cy-h>=0) {
			Java2.setAntialiasedText(g, true);
			setRoi(cxx, cy-h, w, h);
			ImageProcessor ip = crop();
			resetRoi();
			if (ip.getWidth()==0||ip.getHeight()==0)
				return;
			g.drawImage(ip.createImage(), 0, 0, null);
			g.setColor(drawingColor);
			g.drawString(s, 0, h-descent);
			g.dispose();
			ip = new ColorProcessor(bi);
			if (this instanceof ByteProcessor) {
				ip = ip.convertToByte(false);
				if (isInvertedLut()) ip.invert();
			}
			//new ij.ImagePlus("ip",ip).show();
			insert(ip, cxx, cy-h);
			cy += h;
			return;
		}
		
		Java2.setAntialiasedText(g, false);
		g.setColor(Color.white);
		g.fillRect(0, 0, w, h);
		g.setColor(Color.black);
		g.drawString(s, 0, h-descent);
		g.dispose();
		ImageProcessor ip = new ColorProcessor(bi);
		ImageProcessor textMask = ip.convertToByte(false);
		byte[] mpixels = (byte[])textMask.getPixels();
		//new ij.ImagePlus("textmask",textMask).show();
		textMask.invert();
		if (cxx<width && cy-h<height) {
			setMask(textMask);
			setRoi(cxx,cy-h,w,h);
			fill(getMask());
		}
		resetRoi();
		cy += h;
	}

	/** Draws a string at the specified location using the current fill/draw value. */
	public void drawString(String s, int x, int y) {
		moveTo(x, y);
		drawString(s);
	}

	/** Draws a string at the specified location with a filled background.
		A JavaScript example is available at
			http://imagej.nih.gov/ij/macros/js/DrawTextWithBackground.js
	*/
	public void drawString(String s, int x, int y, Color background) {
		Color foreground = drawingColor;
		FontMetrics metrics = getFontMetrics();
		int w = 0;
		int h = metrics.getAscent() + metrics.getDescent();
		int y2 = y;
		if (s.indexOf("\n")!=-1) {
			String[] s2 = Tools.split(s, "\n");
			for (int i=0; i<s2.length; i++) {
				int w2 = getStringWidth(s2[i]);
				if (w2>w) w = w2;
			}
			int h2 = metrics.getHeight();
			y2 += h2*(s2.length-1);
			h += h2*(s2.length-1);
		} else
			w = getStringWidth(s);
		int x2 = x;
		if (justification==CENTER_JUSTIFY)
			x2 -= w/2;
		else if (justification==RIGHT_JUSTIFY)
			x2 -= w;
		setColor(background);
		setRoi(x2, y2-h, w, h);
		fill();
		resetRoi();
		setColor(foreground);
		drawString(s, x, y);
	}

	/** Sets the justification used by drawString(), where <code>justification</code>
		is CENTER_JUSTIFY, RIGHT_JUSTIFY or LEFT_JUSTIFY. The default is LEFT_JUSTIFY. */
	public void setJustification(int justification) {
		this.justification = justification;
	}

	/** Sets the font used by drawString(). */
	public void setFont(Font font) {
		this.font = font;
		fontMetrics	= null;
		boldFont = font.isBold();
	}
	
	/** Specifies whether or not text is drawn using antialiasing. Antialiased
		test requires an 8 bit or RGB image. Antialiasing does not
		work with 8-bit images that are not using 0-255 display range. */
	public void setAntialiasedText(boolean antialiasedText) {
		if (antialiasedText && (((this instanceof ByteProcessor)&&getMin()==0.0&&getMax()==255.0) || (this instanceof ColorProcessor)))
			this.antialiasedText = true;
		else
			this.antialiasedText = false;
	}
	
	/** Returns the width in pixels of the specified string. */
	public int getStringWidth(String s) {
		setupFontMetrics();
		int w;
		if (antialiasedText) {
			Graphics g = fmImage.getGraphics();
			if (g==null) {
				fmImage = null;
				setupFontMetrics();
				g = fmImage.getGraphics();
			}
			Java2.setAntialiasedText(g, true);
			w = Java2.getStringWidth(s, fontMetrics, g);
			g.dispose();
		} else
			w =  fontMetrics.stringWidth(s);
		return w;
	}
	
	/** Returns the current font. */
	public Font getFont() {
		setupFontMetrics();
		return font;
	}

	/** Returns the current FontMetrics. */
	public FontMetrics getFontMetrics() {
		setupFontMetrics();
		return fontMetrics;
	}

	/** Replaces each pixel with the 3x3 neighborhood mean. */
	public void smooth() {
		if (width>1)
			filter(BLUR_MORE);
	}

	/** Sharpens the image or ROI using a 3x3 convolution kernel. */
	public void sharpen() {
		if (width>1) {
			int[] kernel = {-1, -1, -1,
		                -1, 12, -1,
		                -1, -1, -1};
			convolve3x3(kernel);
		}
	}
	
	/** Finds edges in the image or ROI using a Sobel operator. */
	public void findEdges() {
		if (width>1)
			filter(FIND_EDGES);
	}

	/** Flips the image or ROI vertically. */
	public abstract void flipVertical();

	/** Flips the image or ROI horizontally. */
	public void flipHorizontal() {
		int[] col1 = new int[roiHeight];
		int[] col2 = new int[roiHeight];
		for (int x=0; x<roiWidth/2; x++) {
			getColumn(roiX+x, roiY, col1, roiHeight);
			getColumn(roiX+roiWidth-x-1, roiY, col2, roiHeight);
			putColumn(roiX+x, roiY, col2, roiHeight);
			putColumn(roiX+roiWidth-x-1, roiY, col1, roiHeight);
		}
	}

	/** Rotates the entire image 90 degrees clockwise. Returns
		a new ImageProcessor that represents the rotated image. */
	public ImageProcessor rotateRight() {
		int width2 = height;
		int height2 = width;
        ImageProcessor ip2 = createProcessor(width2, height2);
		int[] arow = new int[width];
		for (int row=0; row<height; row++) {
			getRow(0, row, arow, width);
			ip2.putColumn(width2-row-1, 0, arow, height2);
		}
        return ip2;
	}
	
	/** Rotates the entire image 90 degrees counter-clockwise. Returns
		a new ImageProcessor that represents the rotated image. */
	public ImageProcessor rotateLeft() {
		int width2 = height;
		int height2 = width;
        ImageProcessor ip2 = createProcessor(width2, height2);
		int[] arow = new int[width];
		int[] arow2 = new int[width];
		for (int row=0; row<height; row++) {
			getRow(0, row, arow, width);
			for (int i=0; i<width; i++) {
				arow2[i] = arow[width-i-1];
			}
			ip2.putColumn(row, 0, arow2, height2);
		}
        return ip2;
	}

	/** Inserts the image contained in 'ip' at (xloc, yloc). */
	public void insert(ImageProcessor ip, int xloc, int yloc) {
		copyBits(ip, xloc, yloc, Blitter.COPY);
	}
		
	/** Returns a string containing information about this ImageProcessor. */
	public String toString() {
		return ("ip[width="+width+", height="+height+", bits="+getBitDepth()+", min="+getMin()+", max="+getMax()+"]");
	}

	/** Fills the image or ROI bounding rectangle with the current fill/draw value. Use
	*	fill(Roi) or fill(ip.getMask()) to fill non-rectangular selections.
	*	@see #setColor(Color)
	*	@see #setValue(double)
	*	@see #fill(Roi)
	*/
	public void fill() {
		process(FILL, 0.0);
	}

	/** Fills pixels that are within the ROI bounding rectangle and part of 
	*	the mask (i.e. pixels that have a value=BLACK in the mask array).
	*	Use ip.getMask() to acquire the mask. 
	*	Throws and IllegalArgumentException if the mask is null or
	*	the size of the mask is not the same as the size of the ROI.
	*	@see #setColor(Color)
	*	@see #setValue(double)
	*	@see #getMask
	*	@see #fill(Roi)
	*/
	public abstract void fill(ImageProcessor mask);

	/**	 Fills the ROI with the current fill/draw value. 
	*	@see #setColor(Color)
	*	@see #setValue(double)
	*	@see #fill(Roi)
	*/
	public void fill(Roi roi) {
		ImageProcessor m = getMask();
		Rectangle r = getRoi();
		setRoi(roi);
		fill(getMask());
		setMask(m);
		setRoi(r);
	}

	/** Fills outside an Roi. */
	public void fillOutside(Roi roi) {
		if (roi==null || !roi.isArea()) return;
		ImageProcessor m = getMask();
		Rectangle r = getRoi();
		ShapeRoi s1, s2;
		if (roi instanceof ShapeRoi)
			s1 = (ShapeRoi)roi;
		else
			s1 = new ShapeRoi(roi);
		s2 = new ShapeRoi(new Roi(0,0, width, height));
		setRoi(s1.xor(s2));
		fill(getMask());
		setMask(m);
		setRoi(r);
	}

	/** Draws the specified ROI on this image using the line
		width and color defined by ip.setLineWidth() and ip.setColor().
		@see ImageProcessor#drawRoi
	*/
	public void draw(Roi roi) {
		roi.drawPixels(this);
	}

	/** Draws the specified ROI on this image using the stroke
		width, stroke color and fill color defined by roi.setStrokeWidth,
		roi.setStrokeColor() and roi.setFillColor(). Works best with RGB
		images. Does not work with 16-bit and float images.
		Requires Java 1.6.
		@see ImageProcessor#draw
		@see ImageProcessor#drawOverlay
	*/
	public void drawRoi(Roi roi) {
		Image img = createImage();
		Graphics g = img.getGraphics();
		ij.ImagePlus imp = roi.getImage();
		if (imp!=null) {
			roi.setImage(null);
			roi.drawOverlay(g);
			roi.setImage(imp);
		} else
			roi.drawOverlay(g);
	}

	/** Draws the specified Overlay on this image. Works best
		with RGB images. Does not work with 16-bit and float 
		images. Requires Java 1.6.
		@see ImageProcessor#drawRoi
	*/
	public void drawOverlay(Overlay overlay) {
		 Roi[] rois = overlay.toArray();
		 for (int i=0; i<rois.length; i++)
		 	drawRoi(rois[i]);
	}

	/** Set a lookup table used by getPixelValue(), getLine() and
		convertToFloat() to calibrate pixel values. The length of
		the table must be 256 for byte images and 65536 for short
		images. RGB and float processors do not do calibration.
		@see ij.measure.Calibration#setCTable
	*/
	public void setCalibrationTable(float[] cTable) {
		this.cTable = cTable;
	}

	/** Returns the calibration table or null. */
	public float[] getCalibrationTable() {
		return cTable;
	}

	/** Set the number of bins to be used for histograms of float images. */
	public void setHistogramSize(int size) {
		histogramSize = size;
		if (histogramSize<1) histogramSize = 1;
	}

	/** Returns the number of float image histogram bins. The bin
		count is fixed at 256 for the other three data types. */
	public int getHistogramSize() {
		return histogramSize;
	}

	/** Set the range used for histograms of float images. The image range is
		used if both <code>histMin</code> and <code>histMax</code> are zero. */
	public void setHistogramRange(double histMin, double histMax) {
		if (histMin>histMax) {
			histMin = 0.0;
			histMax = 0.0;
		}
		histogramMin = histMin;
		histogramMax = histMax;
	}

	/**	Returns the minimum histogram value used for histograms of float images. */
	public double getHistogramMin() {
		return histogramMin;
	}

	/**	Returns the maximum histogram value used for histograms of float images. */
	public double getHistogramMax() {
		return histogramMax;
	}

	/** Returns a reference to this image's pixel array. The
		array type (byte[], short[], float[] or int[]) varies
		depending on the image type. */
	public abstract Object getPixels();
	
	/** Returns a copy of the pixel data. Or returns a reference to the
		snapshot buffer if it is not null and 'snapshotCopyMode' is true.
		@see ImageProcessor#snapshot
		@see ImageProcessor#setSnapshotCopyMode
	*/
	public abstract Object getPixelsCopy();

	/** Returns the value of the pixel at (x,y). For RGB images, the
		argb values are packed in an int. For float images, the
		the value must be converted using Float.intBitsToFloat().
		Returns zero if either the x or y coodinate is out of range. */
	public abstract int getPixel(int x, int y);
	
	public int getPixelCount() {
		return width*height;
	}

	/** This is a faster version of getPixel() that does not do bounds checking. */
	public abstract int get(int x, int y);
	
	public abstract int get(int index);

	/** This is a faster version of putPixel() that does not clip  
		out of range values and does not do bounds checking. */
	public abstract void set(int x, int y, int value);

	public abstract void set(int index, int value);

	/** Returns the value of the pixel at (x,y) as a float. Faster than
	    getPixel() because no bounds checking is done. */
	public abstract float getf(int x, int y);
	
	public abstract float getf(int index);

	/** Sets the value of the pixel at (x,y) to 'value'. Does no bounds
	    checking or clamping, making it faster than putPixel(). Due to the lack
	    of bounds checking, (x,y) coordinates outside the image may cause
	    an exception. Due to the lack of clamping, values outside the 0-255
	    range (for byte) or 0-65535 range (for short) are not handled correctly.
	*/
	public abstract void setf(int x, int y, float value);

	public abstract void setf(int index, float value);

	/** Returns a copy of the pixel data as a 2D int array with
		dimensions [x=0..width-1][y=0..height-1]. With RGB
		images, the returned values are in packed ARGB format.
		With float images, the returned values must be converted
		to float using Float.intBitsToFloat(). */
	public int[][] getIntArray() {
		int[][] a = new int [width][height];
		for(int y=0; y<height; y++) {
			for(int x=0; x<width; x++)
				a[x][y]=get(x,y);
		}
		return a; 
	}

	/** Replaces the pixel data with contents of the specified 2D int array. */
	public void setIntArray(int[][] a) {
		for(int y=0; y<height; y++) {
			for(int x=0; x<width; x++)
				set(x, y, a[x][y]);
		}
	}

	/** Returns a copy of the pixel data as a 2D float 
		array with dimensions [x=0..width-1][y=0..height-1]. */
	public float[][] getFloatArray() {
		float[][] a = new float[width][height];
		for(int y=0; y<height; y++) {
			for(int x=0; x<width; x++)
				a[x][y]=getf(x,y);
		}
		return a; 
	}

	/** Replaces the pixel data with contents of the specified 2D float array. */
	public void setFloatArray(float[][] a) {
		for(int y=0; y<height; y++) {
			for(int x=0; x<width; x++)
				setf(x, y, a[x][y]);
		}
	}
	
	/** Experimental */
	public void getNeighborhood(int x, int y, double[][] arr) {
		int nx=arr.length;
		int ny=arr[0].length;
		int nx2 = (nx-1)/2;
		int ny2 = (ny-1)/2;
	 	if (x>=nx2 && y>=ny2 && x<width-nx2-1 && y<height-ny2-1) { 
			int index = (y-ny2)*width + (x-nx2);
			for (int y2=0; y2<ny; y2++) {
	 			for (int x2=0; x2<nx; x2++)
					arr[x2][y2] = getf(index++);			
				index += (width - nx);
			}	
		} else {
			for (int y2=0; y2<ny; y2++) {
	 			for (int x2=0; x2<nx; x2++)
					arr[x2][y2] = getPixelValue(x2, y2);			
			}	
		}
	}

    /** Returns the samples for the pixel at (x,y) in an int array.
    	RGB pixels have three samples, all others have one.
		Returns zeros if the the coordinates are not in bounds.
		iArray is an optional preallocated array. */
	public int[] getPixel(int x, int y, int[] iArray) {
		if (iArray==null) iArray = new int[1];
		iArray[0] = getPixel(x, y);
		return iArray;
	}

	/** Sets a pixel in the image using an int array of samples.
		RGB pixels have three samples, all others have one. */
	public void putPixel(int x, int y, int[] iArray) {
		putPixel(x, y, iArray[0]);
	}

	/** Uses the current interpolation method (bilinear or bicubic)
		to find the pixel value at real coordinates (x,y). */
	public abstract double getInterpolatedPixel(double x, double y);

	/** Uses the current interpolation method to find the pixel value at real coordinates (x,y).
		For RGB images, the argb values are packed in an int. For float images,
		the value must be converted using Float.intBitsToFloat().  Returns zero
		if the (x, y) is not inside the image. */
	public abstract int getPixelInterpolated(double x, double y);
	
	/** Uses bilinear interpolation to find the pixel value at real coordinates (x,y). 
		Returns zero if the (x, y) is not inside the image. */
	public final double getInterpolatedValue(double x, double y) {
		if (useBicubic)
			return getBicubicInterpolatedPixel(x, y, this);
		if (x<0.0 || x>=width-1.0 || y<0.0 || y>=height-1.0) {
			if (x<-1.0 || x>=width || y<-1.0 || y>=height)
				return 0.0;
			else
				return getInterpolatedEdgeValue(x, y);
		}
		int xbase = (int)x;
		int ybase = (int)y;
		double xFraction = x - xbase;
		double yFraction = y - ybase;
		if (xFraction<0.0) xFraction = 0.0;
		if (yFraction<0.0) yFraction = 0.0;
		double lowerLeft = getPixelValue(xbase, ybase);
		double lowerRight = getPixelValue(xbase+1, ybase);
		double upperRight = getPixelValue(xbase+1, ybase+1);
		double upperLeft = getPixelValue(xbase, ybase+1);
		double upperAverage = upperLeft + xFraction * (upperRight - upperLeft);
		double lowerAverage = lowerLeft + xFraction * (lowerRight - lowerLeft);
		return lowerAverage + yFraction * (upperAverage - lowerAverage);
	}

	/** This method is from Chapter 16 of "Digital Image Processing:
		An Algorithmic Introduction Using Java" by Burger and Burge
		(http://www.imagingbook.com/). */
	public double getBicubicInterpolatedPixel(double x0, double y0, ImageProcessor ip2) {
		int u0 = (int) Math.floor(x0);	//use floor to handle negative coordinates too
		int v0 = (int) Math.floor(y0);
		if (u0<=0 || u0>=width-2 || v0<=0 || v0>=height-2)
			return ip2.getBilinearInterpolatedPixel(x0, y0);
		double q = 0;
		for (int j = 0; j <= 3; j++) {
			int v = v0 - 1 + j;
			double p = 0;
			for (int i = 0; i <= 3; i++) {
				int u = u0 - 1 + i;
				p = p + ip2.get(u,v) * cubic(x0 - u);
			}
			q = q + p * cubic(y0 - v);
		}
		return q;
	}
	
	final double getBilinearInterpolatedPixel(double x, double y) {
		if (x>=-1 && x<width && y>=-1 && y<height) {
			int method = interpolationMethod;
			interpolationMethod = BILINEAR;
			double value = getInterpolatedPixel(x, y);
			interpolationMethod = method;
			return value;
		} else
			return getBackgroundValue();
	}
	
	static final double a = 0.5; // Catmull-Rom interpolation
	public static final double cubic(double x) {
		if (x < 0.0) x = -x;
		double z = 0.0;
		if (x < 1.0) 
			z = x*x*(x*(-a+2.0) + (a-3.0)) + 1.0;
		else if (x < 2.0) 
			z = -a*x*x*x + 5.0*a*x*x - 8.0*a*x + 4.0*a;
		return z;
	}	

	/*
		// a = 0.5
	double cubic2(double x) {
		if (x < 0) x = -x;
		double z = 0;
		if (x < 1)
			z = 1.5*x*x*x + -2.5*x*x + 1.0;
		else if (x < 2)
			z = -0.5*x*x*x + 2.5*x*x - 4.0*x + 2.0;
		return z;
	}
	*/	

	private final double getInterpolatedEdgeValue(double x, double y) {
		int xbase = (int)x;
		int ybase = (int)y;
		double xFraction = x - xbase;
		double yFraction = y - ybase;
		if (xFraction<0.0) xFraction = 0.0;
		if (yFraction<0.0) yFraction = 0.0;
		double lowerLeft = getEdgeValue(xbase, ybase);
		double lowerRight = getEdgeValue(xbase+1, ybase);
		double upperRight = getEdgeValue(xbase+1, ybase+1);
		double upperLeft = getEdgeValue(xbase, ybase+1);
		double upperAverage = upperLeft + xFraction * (upperRight - upperLeft);
		double lowerAverage = lowerLeft + xFraction * (lowerRight - lowerLeft);
		return lowerAverage + yFraction * (upperAverage - lowerAverage);
	}

	private float getEdgeValue(int x, int y) {
		if (x<=0) x = 0;
		if (x>=width) x = width-1;
		if (y<=0) y = 0;
		if (y>=height) y = height-1;
		return getPixelValue(x, y);
	}

	/** Stores the specified value at (x,y). Does
		nothing if (x,y) is outside the image boundary.
		For 8-bit and 16-bit images, out of range values
		are clamped. For RGB images, the
		argb values are packed in 'value'. For float images,
		'value' is expected to be a float converted to an int
		using Float.floatToIntBits(). */
	public abstract void putPixel(int x, int y, int value);
	
	/** Returns the value of the pixel at (x,y). For byte and short
		images, returns a calibrated value if a calibration table
		has been  set using setCalibraionTable(). For RGB images,
		returns the luminance value. */
	public abstract float getPixelValue(int x, int y);
		
	/** Stores the specified value at (x,y). */
	public abstract void putPixelValue(int x, int y, double value);

	/** Sets the pixel at (x,y) to the current fill/draw value. */
	public abstract void drawPixel(int x, int y);
	
	/** Sets a new pixel array for the image. The length of the array must be equal to width*height.
		Use setSnapshotPixels(null) to clear the snapshot buffer. */
	public abstract void setPixels(Object pixels);
	
	/** Copies the image contained in 'ip' to (xloc, yloc) using one of
		the transfer modes defined in the Blitter interface. */
	public abstract void copyBits(ImageProcessor ip, int xloc, int yloc, int mode);

	/** Transforms the image or ROI using a lookup table. The
		length of the table must be 256 for byte images and 
		65536 for short images. RGB and float images are not
		supported. */
	public abstract void applyTable(int[] lut);

	/** Inverts the image or ROI. */
	public void invert() {process(INVERT, 0.0);}
	
	/** Adds 'value' to each pixel in the image or ROI. */
	public void add(int value) {process(ADD, value);}
	
	/** Adds 'value' to each pixel in the image or ROI. */
	public void add(double value) {process(ADD, value);}
	
	/** Subtracts 'value' from each pixel in the image or ROI. */
	public void subtract(double value) {
		add(-value);
	}
	
	/** Multiplies each pixel in the image or ROI by 'value'. */
	public void multiply(double value) {process(MULT, value);}
	
	/** Assigns 'value' to each pixel in the image or ROI. */
	public void set(double value) {process(SET, value);}

	/** Binary AND of each pixel in the image or ROI with 'value'. */
	public void and(int value) {process(AND, value);}

	/** Binary OR of each pixel in the image or ROI with 'value'. */
	public void or(int value) {process(OR, value);}
	
	/** Binary exclusive OR of each pixel in the image or ROI with 'value'. */
	public void xor(int value) {process(XOR, value);}
	
	/** Performs gamma correction of the image or ROI. */
	public void gamma(double value) {process(GAMMA, value);}
	
	/** Does a natural logarithmic (base e) transform of the image or ROI. */
	public void log() {process(LOG, 0.0);}

	/** Does a natural logarithmic (base e) transform of the image or ROI. */
	public void ln() {log();}

	/** Performs a exponential transform on the image or ROI. */
	public void exp() {process(EXP, 0.0);}

	/** Performs a square transform on the image or ROI. */
	public void sqr() {process(SQR, 0.0);}

	/** Performs a square root transform on the image or ROI. */
	public void sqrt() {process(SQRT, 0.0);}

	/** If this is a 32-bit or signed 16-bit image, performs an 
		absolute value transform, otherwise does nothing. */
	public void abs() {}

	/** Pixels less than 'value' are set to 'value'. */
	public void min(double value) {process(MINIMUM, value);}

	/** Pixels greater than 'value' are set to 'value'. */
	public void max(double value) {process(MAXIMUM, value);}

	/** Returns a copy of this image is the form of an AWT Image. */
	public abstract Image createImage();
	
	/** Returns this image as a BufferedImage. */
	public BufferedImage getBufferedImage() {
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D)bi.getGraphics();
		g.drawImage(createImage(), 0, 0, null);
		return bi;
	}

	/** Returns a new, blank processor with the specified width and height. */
	public abstract ImageProcessor createProcessor(int width, int height);
	
	/** Makes a copy of this image's pixel data that can be 
		later restored using reset() or reset(mask).
		@see ImageProcessor#reset		
		@see ImageProcessor#reset(ImageProcessor)		
	*/
	public abstract void snapshot();
	
	/** Restores the pixel data from the snapshot (undo) buffer. */
	public abstract void reset();
	
	/** Swaps the pixel and snapshot (undo) buffers. */
	public abstract void swapPixelArrays();

	/** Restores pixels from the snapshot buffer that are 
		within the rectangular roi but not part of the mask. */
	public abstract void reset(ImageProcessor mask);
	
	/** Sets a new pixel array for the snapshot (undo) buffer. */
	public abstract void setSnapshotPixels(Object pixels);

	/** Returns a reference to the snapshot (undo) buffer, or null. */
	public abstract Object getSnapshotPixels();

	/** Convolves the image or ROI with the specified
		3x3 integer convolution kernel. */
	public abstract void convolve3x3(int[] kernel);
	
	/** A 3x3 filter operation, where the argument (BLUR_MORE,  FIND_EDGES, 
	     MEDIAN_FILTER, MIN or MAX) determines the filter type. */
	public abstract void filter(int type);
	
	/** A 3x3 median filter. Requires 8-bit or RGB image. */
	public abstract void medianFilter();
	
    /** Adds pseudorandom, Gaussian ("normally") distributed values, with
    	mean 0.0 and the specified standard deviation, to this image or ROI. */
    public abstract void noise(double standardDeviation);
    
	/** Creates a new processor containing an image
		that corresponds to the current ROI. */
	public abstract ImageProcessor crop();
	
	/** Sets pixels less than or equal to level to 0 and all other 
		pixels to 255. Only works with 8-bit and 16-bit images. */
	public abstract void threshold(int level);

	/** Returns a duplicate of this image. */
	public abstract ImageProcessor duplicate();

	/** Scales the image by the specified factors. Does not
		change the image size.
		@see ImageProcessor#setInterpolate
		@see ImageProcessor#resize
	*/
	public abstract void scale(double xScale, double yScale);
	
	/** Creates a new ImageProcessor containing a scaled copy of this image or ROI.
		@see ij.process.ImageProcessor#setInterpolate
	*/
	public abstract ImageProcessor resize(int dstWidth, int dstHeight);
	
	/** Creates a new ImageProcessor containing a scaled copy 
		of this image or ROI, with the aspect ratio maintained. */
	public ImageProcessor resize(int dstWidth) {
		return resize(dstWidth, (int)(dstWidth*((double)roiHeight/roiWidth)));
	}

	/** Creates a new ImageProcessor containing a scaled copy of this image or ROI.
		@param dstWidth   Image width of the resulting ImageProcessor
		@param dstHeight  Image height of the resulting ImageProcessor
		@param useAverging  True means that the averaging occurs to avoid
			aliasing artifacts; the kernel shape for averaging is determined by 
			the interpolationMethod. False if subsampling without any averaging  
			should be used on downsizing.  Has no effect on upsizing.
		@see ImageProcessor#setInterpolationMethod
		Author: Michael Schmid
	*/
	public ImageProcessor resize(int dstWidth, int dstHeight, boolean useAverging) {
		Rectangle r = getRoi();
		int rWidth = r.width;
		int rHeight = r.height;
		if ((dstWidth>=rWidth && dstHeight>=rHeight) || !useAverging)
			return resize(dstWidth, dstHeight);  //upsizing or downsizing without averaging
		else {  //downsizing with averaging in at least one direction: convert to float
			ImageProcessor ip2 = createProcessor(dstWidth, dstHeight);
			FloatProcessor fp = null;
			for (int channelNumber=0; channelNumber<getNChannels(); channelNumber++) {
				fp = toFloat(channelNumber, fp);
				fp.setInterpolationMethod(interpolationMethod);
				fp.setRoi(getRoi());
				FloatProcessor fp2 = fp.downsize(dstWidth, dstHeight);
				ip2.setPixels(channelNumber, fp2);
			}
			return ip2;
		}
	}
	
	/** Use linear interpolation to resize images that have a width or height of one. */
	protected ImageProcessor resizeLinearly(int width2, int height2) {
		int bitDepth = getBitDepth();
		ImageProcessor ip1 = this;
		boolean rotate = width==1;
		if (rotate) {
			ip1=ip1.rotateLeft();
			int w2 = width2;
			width2 = height2;
			height2 = w2;
		}
		ip1 = ip1.convertToFloat();
		int width1 = ip1.getWidth();
		int height1 = ip1.getHeight();
		ImageProcessor ip2 = ip1.createProcessor(width2, height2);
        double scale = (double)(width1-1)/(width2-1);
		float[] data1 = new float[width1];
		float[] data2 = new float[width2];
        ip1.getRow(0, 0, data1, width1);
		double fraction;
		for (int x=0; x<width2; x++) {
			int x1 = (int)(x*scale);
			int x2 = x1+1;
			if (x2==width1) x2=width1-1;
			fraction = x*scale - x1;
			//ij.IJ.log(x+" "+x1+" "+x2+" "+fraction+" "+width1+" "+width2);
			data2[x] = (float)((1.0-fraction)*data1[x1] + fraction*data1[x2]);
		}
		for (int y=0; y<height2; y++)
        	ip2.putRow(0, y, data2, width2);
        if (bitDepth==8)
			ip2 = ip2.convertToByte(false);
		else if (bitDepth==16)
			ip2 = ip2.convertToShort(false);
		if (rotate)
			ip2=ip2.rotateRight();
        return ip2;
	}

	/** Returns a copy of this image that has been reduced in size using binning. */
	public ImageProcessor bin(int shrinkFactor) {
		return new Binner().shrink(this, shrinkFactor, shrinkFactor, 0);
	}

	/** Rotates the image or selection 'angle' degrees clockwise.
		@see ImageProcessor#setInterpolationMethod
		@see ImageProcessor#setBackgroundValue
	*/
  	public abstract void rotate(double angle);
  		
	/**  Moves the image or selection vertically or horizontally by a specified 
	      number of pixels. Positive x values move the image or selection to the 
	      right, negative values move it to the left. Positive y values move the 
	      image or selection down, negative values move it up.
	*/
  	public void translate(double xOffset, double yOffset) {
  		ImageProcessor ip2 = this.duplicate();
  		ip2.setBackgroundValue(0.0);
		boolean integerOffsets = xOffset==(int)xOffset && yOffset==(int)yOffset;
  		if (integerOffsets || interpolationMethod==NONE) {
			for (int y=roiY; y<(roiY + roiHeight); y++) {
				for (int x=roiX; x<(roiX + roiWidth); x++)
					putPixel(x, y, ip2.getPixel(x-(int)xOffset, y-(int)yOffset));
			}
		} else {
			if (interpolationMethod==BICUBIC && (this instanceof ColorProcessor))
				((ColorProcessor)this).filterRGB(ColorProcessor.RGB_TRANSLATE, xOffset, yOffset);
			else {
				for (int y=roiY; y<(roiY + roiHeight); y++) {
					for (int x=roiX; x<(roiX + roiWidth); x++)
						putPixel(x, y, ip2.getPixelInterpolated(x-xOffset, y-yOffset));
				}
			}
		} 
  	}
  	
	/**
	* @deprecated
	* replaced by translate(x,y)
	*/
  	public void translate(int xOffset, int yOffset, boolean eraseBackground) {
		translate(xOffset, yOffset);
  	}

	/** Returns the histogram of the image or ROI. Returns
		a luminosity histogram for RGB images and null
		for float images.
		<p>
		For 8-bit and 16-bit images, returns an array with one entry for each possible
		value that a pixel can have, from 0 to 255 (8-bit image) or 0-65535 (16-bit image).
		Thus, the array size is 256 or 65536, and the bin width in uncalibrated units is 1.
		<p>
		For RGB images, the brightness is evaluated using the color weights (which would result in a
		float value) and rounded to an int. This gives 256 bins. FloatProcessor.getHistogram is not
		implemented (returns null).
	*/
	public abstract int[] getHistogram();
	
	/** Erodes the image or ROI using a 3x3 maximum filter. Requires 8-bit or RGB image. */
	public abstract void erode();
	
	/** Dilates the image or ROI using a 3x3 minimum filter. Requires 8-bit or RGB image. */
	public abstract void dilate();
	
	/** For 16 and 32 bit processors, set 'lutAnimation' true
		to have createImage() use the cached 8-bit version
		of the image. */
	public void setLutAnimation(boolean lutAnimation) {
		this.lutAnimation = lutAnimation;
		newPixels = true;
		source = null;
	}
	
	void resetPixels(Object pixels) {
		if (pixels==null) {
			if (img!=null) {
				img.flush();
				img = null;
			}
			source = null;
		}
		newPixels = true;
		source = null;
	}

	/** Returns an 8-bit version of this image as a ByteProcessor. */
	public ImageProcessor convertToByte(boolean doScaling) {
		TypeConverter tc = new TypeConverter(this, doScaling);
		return tc.convertToByte();
	}

	/** Returns a 16-bit version of this image as a ShortProcessor. */
	public ImageProcessor convertToShort(boolean doScaling) {
		TypeConverter tc = new TypeConverter(this, doScaling);
		return tc.convertToShort();
	}

	/** Returns a 32-bit float version of this image as a FloatProcessor. 
		For byte and short images, converts using a calibration function 
		if a calibration table has been set using setCalibrationTable(). */
	public ImageProcessor convertToFloat() {
		TypeConverter tc = new TypeConverter(this, false);
		return tc.convertToFloat(cTable);
	}
	
	/** Returns an RGB version of this image as a ColorProcessor. */
	public ImageProcessor convertToRGB() {
		TypeConverter tc = new TypeConverter(this, true);
		return tc.convertToRGB();
	}
	
	/** Returns an 8-bit version of this image as a ByteProcessor. 16-bit and 32-bit
	 * pixel data are scaled from min-max to 0-255.
	*/
	public ByteProcessor convertToByteProcessor() {
		return convertToByteProcessor(true);
	}

	/** Returns an 8-bit version of this image as a ByteProcessor. 16-bit and 32-bit
	 * pixel data are scaled from min-max to 0-255 if 'scale' is true.
	*/
	public ByteProcessor convertToByteProcessor(boolean scale) {
		ByteProcessor bp;
		if (this instanceof ByteProcessor)
			bp = (ByteProcessor)this.duplicate();
		else
			bp = (ByteProcessor)this.convertToByte(scale);
		return bp;
	}

	/** Returns a 16-bit version of this image as a ShortProcessor. 32-bit
	 * pixel data are scaled from min-max to 0-255.
	*/
	public ShortProcessor convertToShortProcessor() {
		return convertToShortProcessor(true);
	}

	/** Returns a 16-bit version of this image as a ShortProcessor. 32-bit
	 * pixel data are scaled from min-max to 0-255 if 'scale' is true.
	*/
	public ShortProcessor convertToShortProcessor(boolean scale) {
		ShortProcessor sp;
		if (this instanceof ShortProcessor)
			sp = (ShortProcessor)this.duplicate();
		else
			sp = (ShortProcessor)this.convertToShort(scale);
		return sp;
	}

	/** Returns a 32-bit float version of this image as a FloatProcessor. 
		For byte and short images, converts using a calibration function 
		if a calibration table has been set using setCalibrationTable(). */
	public FloatProcessor convertToFloatProcessor() {
		FloatProcessor fp;
		if (this instanceof FloatProcessor)
			fp = (FloatProcessor)this.duplicate();
		else
			fp = (FloatProcessor)this.convertToFloat();
		return fp;
	}
	
	/** Returns an RGB version of this image as a ColorProcessor. */
	public ColorProcessor convertToColorProcessor() {
		ColorProcessor cp;
		if (this instanceof ColorProcessor)
			cp = (ColorProcessor)this.duplicate();
		else
			cp = (ColorProcessor)this.convertToRGB();
		return cp;
	}
	
	/** Performs a convolution operation using the specified kernel. 
	KernelWidth and kernelHeight must be odd. */
	public abstract void convolve(float[] kernel, int kernelWidth, int kernelHeight);
	
	/** Converts the image to binary using an automatically determined threshold.
		For byte and short images, converts to binary using an automatically determined
		threshold. For RGB images, converts each channel to binary. For
		float images, does nothing.
	*/
	public void autoThreshold() {
		threshold(getAutoThreshold());
	}

	/**	Returns a pixel value (threshold) that can be used to divide the image into objects 
		and background. It does this by taking a test threshold and computing the average 
		of the pixels at or below the threshold and pixels above. It then computes the average
		of those two, increments the threshold, and repeats the process. Incrementing stops 
		when the threshold is larger than the composite average. That is, threshold = (average 
		background + average objects)/2. This description was posted to the ImageJ mailing 
		list by Jordan Bevic. */
	public int getAutoThreshold() {
		return getAutoThreshold(getHistogram());
	}

	/**	This is a version of getAutoThreshold() that uses a histogram passed as an argument. */
	public int getAutoThreshold(int[] histogram) {
		int level;
		int maxValue = histogram.length - 1;
		double result, sum1, sum2, sum3, sum4;
		
		int count0 = histogram[0];
		histogram[0] = 0; //set to zero so erased areas aren't included
		int countMax = histogram[maxValue];
		histogram[maxValue] = 0;
		int min = 0;
		while ((histogram[min]==0) && (min<maxValue))
			min++;
		int max = maxValue;
		while ((histogram[max]==0) && (max>0))
			max--;
		if (min>=max) {
			histogram[0]= count0; histogram[maxValue]=countMax;
			level = histogram.length/2;
			return level;
		}
		
		int movingIndex = min;
		int inc = Math.max(max/40, 1);
		do {
			sum1=sum2=sum3=sum4=0.0;
			for (int i=min; i<=movingIndex; i++) {
				sum1 += (double)i*histogram[i];
				sum2 += histogram[i];
			}
			for (int i=(movingIndex+1); i<=max; i++) {
				sum3 += (double)i*histogram[i];
				sum4 += histogram[i];
			}			
			result = (sum1/sum2 + sum3/sum4)/2.0;
			movingIndex++;
		} while ((movingIndex+1)<=result && movingIndex<max-1);
		
		histogram[0]= count0; histogram[maxValue]=countMax;
		level = (int)Math.round(result);
		return level;
	}
	
	/** Updates the clipping rectangle used by lineTo(), drawLine(), drawDot() and drawPixel().
		The clipping rectangle is reset by passing a null argument or by calling resetRoi(). */
	public void setClipRect(Rectangle clipRect) {
		if (clipRect==null) {
			clipXMin=0; 
			clipXMax=width-1; 
			clipYMin=0; 
			clipYMax=height-1; 
		} else {
			clipXMin = clipRect.x; 
			clipXMax = clipRect.x + clipRect.width - 1; 
			clipYMin = clipRect.y; 
			clipYMax = clipRect.y + clipRect.height - 1; 
			if (clipXMin<0) clipXMin = 0;
			if (clipXMax>=width) clipXMax = width-1;
			if (clipYMin<0) clipYMin = 0;
			if (clipYMax>=height) clipYMax = height-1;
		}
	}
	
	protected String maskSizeError(ImageProcessor mask) {
		return "Mask size ("+mask.getWidth()+"x"+mask.getHeight()+") != ROI size ("+
			roiWidth+"x"+roiHeight+")";
	}
	
	protected SampleModel getIndexSampleModel() {
		if (sampleModel==null) {
			IndexColorModel icm = getDefaultColorModel();
			WritableRaster wr = icm.createCompatibleWritableRaster(1, 1);
			sampleModel = wr.getSampleModel();
			sampleModel = sampleModel.createCompatibleSampleModel(width, height);
		}
		return sampleModel;
	}

	/** Returns the default grayscale IndexColorModel. */
	public IndexColorModel getDefaultColorModel() {
		if (defaultColorModel==null) {
			byte[] r = new byte[256];
			byte[] g = new byte[256];
			byte[] b = new byte[256];
			for(int i=0; i<256; i++) {
				r[i]=(byte)i;
				g[i]=(byte)i;
				b[i]=(byte)i;
			}
			defaultColorModel = new IndexColorModel(8, 256, r, g, b);
		}
		return defaultColorModel;
	}
	
	/**	The getPixelsCopy() method returns a reference to the
		snapshot buffer if it is not null and 'snapshotCopyMode' is true.
		@see ImageProcessor#getPixelsCopy		
		@see ImageProcessor#snapshot		
	*/
	public void setSnapshotCopyMode(boolean b) {
		snapshotCopyMode = b;
	}
	
	/** Returns the number of color channels in the image. The color channels can be
	*  accessed by toFloat(channelNumber, fp) and written by setPixels(channelNumber, fp).
	* @return 1 for grayscale images, 3 for RGB images
	*/
	public int getNChannels() {
		return 1;   /* superseded by ColorProcessor */
	}
	
	/** Returns a FloatProcessor with the image or one color channel thereof.
	*  The roi and mask are also set for the FloatProcessor.
	*  @param channelNumber   Determines the color channel, 0=red, 1=green, 2=blue. Ignored for
	*                         grayscale images.
	*  @param fp     Here a FloatProcessor can be supplied, or null. The FloatProcessor
	*                         is overwritten when converting data (re-using its pixels array 
	*                         improves performance).
	*  @return A FloatProcessor with the converted image data of the color channel selected
	*/
	public abstract FloatProcessor toFloat(int channelNumber, FloatProcessor fp);
	
	/** Sets the pixels (of one color channel for RGB images) from a FloatProcessor.
	*  @param channelNumber   Determines the color channel, 0=red, 1=green, 2=blue.Ignored for
	*                         grayscale images.
	*  @param fp              The FloatProcessor where the image data are read from.
	*/
	public abstract void setPixels(int channelNumber, FloatProcessor fp);
	
	/** Returns the minimum possible pixel value. */
	public double minValue() {
		return 0.0;
	}

	/** Returns the maximum possible pixel value. */
	public double maxValue() {
		return 255.0;
	}
	
	/** CompositeImage calls this method to generate an updated color image. */
	public void updateComposite(int[] rgbPixels, int channel) {
		int redValue, greenValue, blueValue;
		int size = width*height;
		if (bytes==null || !lutAnimation)
			bytes = create8BitImage();
		if (cm==null)
			makeDefaultColorModel();
		if (reds==null || cm!=cm2)
			updateLutBytes();
		switch (channel) {
			case 1: // update red channel
				for (int i=0; i<size; i++)
					rgbPixels[i] = (rgbPixels[i]&0xff00ffff) | reds[bytes[i]&0xff];
				break;
			case 2: // update green channel
				for (int i=0; i<size; i++)
					rgbPixels[i] = (rgbPixels[i]&0xffff00ff) | greens[bytes[i]&0xff];
				break;
			case 3: // update blue channel
				for (int i=0; i<size; i++)
					rgbPixels[i] = (rgbPixels[i]&0xffffff00) | blues[bytes[i]&0xff];
				break;
			case 4: // get first channel
				for (int i=0; i<size; i++) {
					redValue = reds[bytes[i]&0xff];
					greenValue = greens[bytes[i]&0xff];
					blueValue = blues[bytes[i]&0xff];
					rgbPixels[i] = redValue | greenValue | blueValue;	
				}
				break;
			case 5: // merge next channel
				int pixel;
				for (int i=0; i<size; i++) {
					pixel = rgbPixels[i];
					redValue = (pixel&0x00ff0000) + reds[bytes[i]&0xff];
					greenValue = (pixel&0x0000ff00) + greens[bytes[i]&0xff];
					blueValue = (pixel&0x000000ff) + blues[bytes[i]&0xff];
					if (redValue>16711680) redValue = 16711680;
					if (greenValue>65280) greenValue = 65280;
					if (blueValue>255) blueValue = 255;
					rgbPixels[i] = redValue | greenValue | blueValue;	
				}
				break;
		}
		lutAnimation = false;
	}
	
	// method and variables used by updateComposite()
	byte[]  create8BitImage() {return null;}
	private byte[] bytes;
	private int[] reds, greens, blues;

	void updateLutBytes() {
		IndexColorModel icm = (IndexColorModel)cm;
		int mapSize = icm.getMapSize();
		if (reds==null || reds.length!=mapSize) {
			reds = new int[mapSize];
			greens = new int[mapSize];
			blues = new int[mapSize];
		}
		byte[] tmp = new byte[mapSize];
		icm.getReds(tmp);
		for (int i=0; i<mapSize; i++) reds[i] = (tmp[i]&0xff)<<16;
		icm.getGreens(tmp); 
		for (int i=0; i<mapSize; i++) greens[i] = (tmp[i]&0xff)<<8;
		icm.getBlues(tmp);
		for (int i=0; i<mapSize; i++) blues[i] = tmp[i]&0xff;
		cm2 = cm;
	}
	
	/** Sets the upper Over/Under threshold color. Can be called from a macro,
		e.g., call("ij.process.ImageProcessor.setOverColor", 0,255,255). */
	public static void setOverColor(int red, int green, int blue) {
		overRed=red; overGreen=green; overBlue=blue;
	}

	/** Set the lower Over/Under thresholding color. */
	public static void setUnderColor(int red, int green, int blue) {
		underRed=red; underGreen=green; underBlue=blue;
	}
	
	/** Returns 'true' if this is a binary image (8-bit-image with only 0 and 255). */
	public boolean isBinary() {
		return false;
	}

	/* This method is experimental and may be removed. */
	public static void setUseBicubic(boolean b) {
		useBicubic = b;
	}
	
	/* Calculates and returns statistics (area, mean, std-dev, mode, min, max,
		centroid, center of mass, 256 bin histogram) for this image or ROI. */
	public ImageStatistics getStatistics() {
		// 127 = AREA+MEAN+STD_DEV+MODE+MIN_MAX+CENTROID+CENTER_OF_MASS
		return ImageStatistics.getStatistics(this, 127, null);
	}
	
	/** Blurs the image by convolving with a Gaussian function. */
	public void blurGaussian(double sigma) {
		double accuracy = getBitDepth()==8||getBitDepth()==24?0.002:0.0002;
		resetRoi();
		GaussianBlur gb = new GaussianBlur();
		gb.showProgress(false);
        gb.blurGaussian(this, sigma, sigma, accuracy);
	}

	/** Uses the Process/Math/Macro command to apply macro code to this image. */
	public void applyMacro(String macro) {
		ij.plugin.filter.ImageMath.applyMacro(this, macro, false);
	}

	/* Returns the PlugInFilter slice number. */
	public int getSliceNumber() {
		if (sliceNumber<1)
			return 1;
		else
			return sliceNumber;
	}

	/** PlugInFilterRunner uses this method to set the slice number. */
	public void setSliceNumber(int slice) {
		sliceNumber = slice;
	}
	
	/** Returns a shallow copy of this ImageProcessor, where this 
	* image and the copy share pixel data. Use the duplicate() method 
	* to create a copy that does not share the pixel data.
	* @see ImageProcessor#duplicate	
	*/
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}
	
	/** This method is used to display virtual stack overlays. */
	public void setOverlay(Overlay overlay) {
		this.overlay = overlay;
	}
	
	public Overlay getOverlay() {
		return overlay;
	}

}