File: visu_widgets.tcl

package info (click to toggle)
ftools-fv 5.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,072 kB
  • sloc: tcl: 50,774; ansic: 17,858; exp: 2,004; makefile: 172; cpp: 169; sh: 109; csh: 10
file content (2313 lines) | stat: -rw-r--r-- 65,763 bytes parent folder | download | duplicates (6)
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
if { [info exists env(BLT_LIBRARY)] == 0 } {
    set has_blt 0
} else {
    set has_blt 1
}

set _x_ 0
set _y_ 0
set _wid_ 0
set _len_ 0
set _DispMax_ 0
set _DispMin_ 0
set _userDispMax_ 0
set _userDispMin_ 0
set _thres_incr_ 1
set _thres_res_ 1
set _gc_ "or"
set _index_ 0
set _init_index_ 0
set _toggle_gc_ 0
set zoomx 2
set zoomy 2
set fromx1 0
set fromy1 0
set fromx2 0
set fromy2 0
set ONodeX(0) 0
set ONodeY(0) 0
set nb 0
set win_nb 0
set curr_nb_slices 0
set curr_filename {}
set nb_slices 0
set scale_slice 0
set slice_nb 0
set img_nb 0
set palette_button 0
set palette_mapped 0
set cmap gray
set curr_min 0
set curr_max 255
set curr_color "intensity"
foreach k {intensity red green blue} {
    set col_npoints($k) 2
}
unset k
set Private_Colormap 1 
# set def_font "-*-helvetica-*-r-normal-*-14-*-*-*-*-*-*-*"
#set visu_help_file $visu_library/../../man/html/tutorial.html
set dir "."
set file ""
set pattern "*.sdt"
set showdot 0
# option add *font $def_font


proc destroywin win {
    global img_array win_array curr_img title_array color_bar_img


    if { [info exists img_array($win.c)] } {
	unset title_array($win)
	if { $curr_img == $img_array($win.c) } {
	    unset win_array($img_array($win.c))
	    unset img_array($win.c)
	    set new_img [lindex [array names win_array] 0]
	    if { $new_img != {} } {
		set curr_img $new_img
		set_curr_defaults $curr_img
		set_curr_event_loop [winfo toplevel $win_array($curr_img)]
		set_active_title [winfo toplevel $win_array($curr_img)]
	    } else {
		if { [info exists color_bar_img] } {
		    set curr_img $color_bar_img
		}   
	    }
	} else {
	    unset win_array($img_array($win.c))
	    unset img_array($win.c)
	}
	catch {destroy $win}
	update idletasks
    }
}

proc delete_img img {
    upvar $img img1
    global win_array img_array filename_array nb_slices_array 
   
    if { [info exists win_array($img1)] } {
	unset img_array($win_array($img1).c)
	destroy $win_array($img1)
	unset win_array($img1)
    }
    image delete $img1
  
    unset filename_array($img1)
    unset nb_slices_array($img1)
    unset img1

    set new_img [lindex [array names win_array] 0]
    if { $new_img != {} } {
	set curr_img $new_img
	set_curr_defaults $curr_img
	set_curr_event_loop [winfo toplevel $win_array($curr_img)]
	set_active_title [winfo toplevel $win_array($curr_img)]
    } else {
	if { [info exists color_bar_img] } {
	    set curr_img $color_bar_img
	}   
    }
}

proc topConfig { win w h } {
	global _wid_ _len_ sbwidth

    if { [catch {pack slaves $win} msg] > 0} {
	puts "error in topConfig : $msg"
	return
    }
    if { [lsearch [pack slaves $win] "$win.xscroll"] == -1 } {
	set hasX 0
    } else  {
	set hasX 1
    }
    if { [lsearch [pack slaves $win] "$win.yscroll"] == -1 } {
	set hasY 0
    } else  {
	set hasY 1
    }
    if {$w >= $_wid_} {
	if {$hasX} {
	    catch {pack forget $win.xscroll}
	    set hasX 0
	}
    } else {
	if {!$hasX} {
	    catch {pack $win.xscroll -before $win.c -side bottom -expand 1 -fill x}
	    set hasX 1
	}
    }
    if {$h >= $_len_+$hasX*$sbwidth} {
	if {$hasY} {
	    catch {pack forget $win.yscroll}
	    set hasY 0
	}
    } else {
	if {!$hasY} {
	    catch {pack $win.yscroll -before $win.c -side right -expand 1 -fill y}
	    set hasY 1
	}
    }
    #
    # Check again since we now might need the vertical scrollbar
    # after all.
    #
    if {$w >= $_wid_+$hasY*$sbwidth} {
	if {$hasX} {
	    catch {pack forget $win.xscroll}
	    set hasX 0
	}
    } else {
	if {!$hasX} {
	    catch {pack $win.xscroll -before $win.c -side bottom -expand 1 -fill x}
	    set hasX 1
	}
    }
}

proc snap2pict {img1 img2} {
    upvar $img1 in
    upvar $img2 out
    global scale_slice _wid_ _len_ win_array curr_img filename_array curr_filename nb_slices_array nb_slices curr_nb_slices curr_slice_array seq_array sbwidth

    if { ![info exists in] } {
	puts "source Pict image does not exists"
	return
    }
    if { ![info exists out] } {
	set out [image create pict]
    }
    $in snap2pict $out
    set filename_array($out) "snap2pict"
    set curr_filename "snap2pict"
    set nb_slices_array($out) 1
    set seq_array($out) 0
    set curr_slice_array($out) 0
    set curr_nb_slices 1
    set curr_img $out

    if { [ info exists win_array($out)] } { 
	set _wid_ [image width $out]
	set _len_ [image height $out]
	$win_array($out).c configure -width $_wid_ -height $_len_ 
	$win_array($out).c configure -scrollregion "0 0 $_wid_ $_len_" -confine 1
	wm maxsize $win_array($out) $_wid_ $_len_
	wm geometry $win_array($out) [format "%dx%d" $_wid_ $_len_]
	set sbwidth [winfo reqwidth $win_array($out).yscroll]
	topConfig $win_array($out) $_wid_ $_len_
    }
}

proc snap2photo {img1 img2} {
    upvar $img1 in
    upvar $img2 out
   
    if { ![info exists in] } {
	puts "source Pict image does not exists"
	return
    }
    if { ![info exists out] } {
	set out [image create photo -palette 10/10/10]
    }
    set g [image create photo]
    $in snap2photo $out
}

proc disp_photo {img1} {
    upvar $img1 in
    global win_nb

    if { ![info exists in] } {
	puts "Pict image does not exists"
	return
    }
    set win_nb [expr $win_nb+1]
    set win ".win$win_nb"
    catch {toplevel $win  -cursor {crosshair green}}
   
    canvas $win.c -width [image width $in] -height [image height $in]
    $win.c create image 0 0 -image $in -anchor nw
    pack $win.c -expand 1 -fill both -anchor nw
}


proc disp_priv img {
    upvar $img img1
    global Private_Colormap 

    if { [info exists img1] } {
	set Private_Colormap 3
	disp img1
    }
}

proc disp img {
    upvar $img img1
    global win_array win_nb filename_array title_array _wid_ _len_ sbwidth img_array curr_img 
   
    if { ![ info exists win_array($img1)] } {
	set win_nb [expr $win_nb+1]
	set win ".win$win_nb"
	catch {toplevel $win  -cursor {crosshair green}}
	set title_array($win) "<$win_nb>: "
	
	set _wid_ [image width $img1]
	set _len_ [image height $img1]
	
	wm maxsize $win $_wid_ $_len_
	canvas $win.c -width $_wid_ -height $_len_ -borderwidth 0 -highlightthickness 0 -cursor {crosshair green}
	$win.c create image 0 0 -anchor nw -image $img1
	pack $win.c -side top -expand 0 -fill none -in $win

	scrollbar $win.yscroll -orient vertical \
		-command [list $win.c yview] -width 12
	scrollbar $win.xscroll -orient horizontal \
		-command [list $win.c xview] -width 12

	set sbwidth [winfo reqwidth $win.yscroll]

	pack $win.yscroll -side right -fill y -expand 0 -after $win.c
	pack $win.xscroll -side bottom -fill x -expand 0 -after $win.c

	$win.c configure -yscrollcommand "$win.yscroll set" \
		-xscrollcommand "$win.xscroll set" 
	$win.c config -scrollregion "0 0 $_wid_ $_len_" -confine 1
	

	set img_array($win.c) $img1
	set win_array($img1) $win
	
    }
    if {[info exists curr_img] && [info exists win_array($curr_img)]} {
	set_curr_event_loop [winfo toplevel $win_array($curr_img)]
    }
    set_curr_defaults $img1
    set_curr_event_loop $win_array($img1)
    set_active_title $win_array($img1)
    
}



proc set_active_title win {
    global title_array img_array filename_array seq_array curr_slice_array 

    foreach l [array names img_array] {
	set w [winfo toplevel $l]
	if { $seq_array($img_array($w.c)) == 1 } {
	    wm title $w "$title_array($w) [file tail $filename_array($img_array($w.c))]$curr_slice_array($img_array($w.c)).sdt"
	} else {
	    wm title $w "$title_array($w) [file tail $filename_array($img_array($w.c))]:$curr_slice_array($img_array($w.c))"
	}
    }
    if { $seq_array($img_array($win.c)) == 1 } {
	wm title $win "<***> [file tail $filename_array($img_array($win.c))]$curr_slice_array($img_array($win.c)).sdt ACTIVE"
    } else {
	wm title $win "<***> [file tail $filename_array($img_array($win.c))]:$curr_slice_array($img_array($win.c)) ACTIVE"
    }    
}

proc update_active_title win {
    global title_array img_array filename_array seq_array curr_slice_array 

    if { $seq_array($img_array($win.c)) == 1 } {
	wm title $win "<***> [file tail $filename_array($img_array($win.c))]$curr_slice_array($img_array($win.c)).sdt ACTIVE"
    } else {
	wm title $win "<***> [file tail $filename_array($img_array($win.c))]:$curr_slice_array($img_array($win.c)) ACTIVE"
    }    
}


proc set_curr_defaults img {
    global curr_img img_array curr_filename filename_array curr_nb_slices nb_slices_array scale_slice curr_slice_array _wid_ _len_ _DispMin_ _DispMax_

    set curr_img $img
    set curr_filename $filename_array($curr_img)
    set curr_nb_slices $nb_slices_array($curr_img)
    set scale_slice $curr_slice_array($curr_img) 
    if { $scale_slice > $curr_nb_slices } {
	set scale_slice $curr_nb_slices
    }
    set _len_ [image height $curr_img]
    set _wid_ [image width $curr_img]
    set code [catch {.slice configure -to [expr $nb_slices_array($curr_img)-1]} string]
    if {$code>0} {
	return
    }
    set _DispMin_ [$curr_img getmin]
    set _DispMax_ [$curr_img getmax]
    update_scale
}

proc set_curr_event_loop win {
    global curr_img _wid_ _len_ _x_ _y_ _pix_ img_array

    bind $win <Destroy> {
	set win [winfo toplevel %W]
	bind $win <Destroy> {} 
	bind $win.c <Destroy> {}
	destroywin $win 
    }
    bind $win.c <Destroy> {
	set win [winfo toplevel %W]
	bind $win <Destroy> {}	
	bind $win.c <Destroy> {}
	destroywin $win 
    }

    bind $win <Configure> "topConfig $win %w %h"
    
    bind $win <Enter> {
	set win [winfo toplevel %W]
	if { ![info exists curr_img] } {
	    set_curr_defaults $img_array($win.c)
	    set_active_title $win
	}
    }

    bind $win.c <Enter> {
	set win [winfo toplevel %W]
	if { ![info exists curr_img] } {
	    set_curr_defaults $img_array($win.c)
	    set_active_title $win
	}
    }
    bind $win.c <Button-1> {
	# reset the events in curr_img if we are in overlay paint mode
	if { [info exists curr_img] && [info exists  win_array($curr_img)] } {
	    set_curr_event_loop  [winfo toplevel $win_array($curr_img)]
	}
	set can %W
	set win [winfo toplevel %W]
	set_curr_defaults $img_array($win.c)
	set_active_title [winfo toplevel %W]
	set _x_ [expr int([$can canvasx %x])]
	set _y_ [expr int([$can canvasy %y])]
	if {  ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
	    set _pix_ [$curr_img get $_x_ $_y_]
	}
    }
    bind $win.c <B1-Motion> {
	set can %W
	set _x_ [expr int([$can canvasx %x])]
        set _y_ [expr int([$can canvasy %y])]
        if { ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
           set _pix_ [$curr_img get $_x_ $_y_] 
        }
    }      
    bind $win.c <Motion> {
        set _x_ [expr int([%W canvasx %x])] 
	set _y_ [expr int([%W canvasy %y])]
        update idletasks
        if { ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
           set _pix_ [$curr_img get $_x_ $_y_] 
        }
    } 
    bind $win.c <ButtonRelease-1> {}
    bind $win.c <Button-2> {}
    bind $win.c <B2-Motion> {} 
    bind $win.c <ButtonRelease-2> {}
    bind $win.c <Button-3> {}
}


proc rdfile { file img {args {}} } {
    upvar $img img1
   
    global scale_slice _wid_ _len_ win_array filename_array slice_nb \
	    nb_slices_array nb_slices curr_nb_slices curr_slice_array seq_array sbwidth
  
    if { $file == {} } {
	return
    }
    if { ![info exists img1] } {
	set new 1
    } else { set new 0 }
    
    set seqbase [string range $file 0 [string last _ $file]]
    set seqend [string range $file [string last _ $file] end]
    
    if {$new == 1} { 
	set img1 [image create pict]
    }
	
    if { $args != {} } {
	eval { $img1 read $file -shrink } $args
    } else {
	$img1 read $file -shrink
    }
    if { [scan $seqend "_%d.sdt" seqnum]==1 } {
	set slice_nb 0
	set scale_slice $seqnum
	set filename_array($img1) $seqbase
	set nb_slices_array($img1) [llength [glob [format "%s*.sdt" $seqbase]]]
	set seq_array($img1) 1
	set curr_slice_array($img1) $seqnum
    } else {
	set slice_nb 0
	set scale_slice 0
	set filename_array($img1) $file
	set nb_slices_array($img1) $nb_slices
	set seq_array($img1) 0
	set curr_slice_array($img1) 0
    }

    if { [ info exists win_array($img1)] } {
	set _wid_ [image width $img1]
	set _len_ [image height $img1]
	$win_array($img1).c configure -width $_wid_ -height $_len_ 
	$win_array($img1).c configure -scrollregion "0 0 $_wid_ $_len_" -confine 1
	wm maxsize $win_array($img1) $_wid_ $_len_
	wm geometry $win_array($img1) [format "%dx%d" $_wid_ $_len_]
	set sbwidth [winfo reqwidth $win_array($img1).yscroll]
	topConfig $win_array($img1) $_wid_ $_len_
	# reset the events in curr_img if we are in overlay paint mode
	if { [info exists curr_img] && [info exists win_array($curr_img)] } {
	    set_curr_event_loop  [winfo toplevel $win_array($curr_img)]
	}
	set_curr_defaults $img1
    }
}

proc rdbinary {img access file  {args {}} } {
    global scale_slice _wid_ _len_ win_array filename_array slice_nb nb_slices_array nb_slices curr_nb_slices curr_slice_array seq_array sbwidth
    
    upvar $img img1
    if { $file == {} || $access == {} } {
	puts "Usage rdbinary img -file|channel name \[-width -height -skip\]"
	return
    }
    if { ![info exists img1] } {
	set new 1
    } else { 
	set new 0 
    }
    
    if { [string compare "-channel" $access] } {
	set has_file 1
    } elseif  { [string compare "-file" $access] } {
	set has_file 0
    } else {
	puts  "Usage rdbinary img -file|channel name \[-width -height -skip\]"
	return
    }

    if {$has_file} {
	set seqbase [string range $file 0 [string last _ $file]]
	set seqend [string range $file [string last _ $file] end]
    }

    if {$new == 1} { 
	set img1 [image create pict]
    }

    if { $args != {} } {
	eval { $img1 rdbinary $access $file } $args
    } else {
	$img1 rdbinary $access $file
    }

    if {$has_file} {
	if { [scan $seqend "_%d.sdt" seqnum]==1 } {
	    set slice_nb 0
	    set scale_slice $seqnum
	    set filename_array($img1) $seqbase
	    set nb_slices_array($img1) [llength [glob [format "%s*.sdt" $seqbase]]]
	    set seq_array($img1) 1
	    set curr_slice_array($img1) $seqnum
	} else {
	    set slice_nb 0
	    set scale_slice 0
	    set filename_array($img1) $file
	    set nb_slices_array($img1) $nb_slices
	    set seq_array($img1) 0
	    set curr_slice_array($img1) 0
	}
    } else {
	set slice_nb 0
	set scale_slice 0
	set filename_array($img1)  $file
	set nb_slices_array($img1) $nb_slices
	set seq_array($img1) 0
	set curr_slice_array($img1) 0
    }
   
    if { [ info exists win_array($img1)] } {
	set _wid_ [image width $img1]
	set _len_ [image height $img1]
	$win_array($img1).c configure -width $_wid_ -height $_len_ 
	$win_array($img1).c configure -scrollregion "0 0 $_wid_ $_len_" -confine 1
	wm maxsize $win_array($img1) $_wid_ $_len_
	wm geometry $win_array($img1) [format "%dx%d" $_wid_ $_len_]
	set sbwidth [winfo reqwidth $win_array($img1).yscroll]
	topConfig $win_array($img1) $_wid_ $_len_ 
	# reset the events in curr_img if we are in overlay paint mode
	if { [info exists  curr_img] && [info exists  win_array($curr_img)] } {
	    set_curr_event_loop  [winfo toplevel $win_array($curr_img)]
	}
	set_curr_defaults $img1
    }
}

proc upslice {} {
    global curr_img scale_slice

    if { [info exists curr_img] } {
	set nb [expr $scale_slice+1]
	rdslice curr_img $nb
    }
}

proc downslice {} {
    global curr_img scale_slice

    if { [info exists curr_img] } {
	set nb [expr $scale_slice-1]
	rdslice curr_img $nb
    }
}

proc rdslice { img nb } {
    upvar $img img1

    global scale_slice slice_nb nb_slices_array _wid_ _len_ filename_array curr_img seq_array curr_slice_array win_array
    
    if { ![info exists img1] } { 
        #Create variable first
	return
    }   
    if { $nb<0 } {
	return
    }
    if { $nb>=$nb_slices_array($img1)} {
	return
    }
    set scale_slice $nb
    set curr_slice_array($img1) $nb
    if { $seq_array($img1)== 0 } {
	# now just read image slice $scale_slice
	set slice_nb $scale_slice
	$img1 read $filename_array($img1)
	# using rdbinary is faster, but can cause problems if the
	# images have been converted to a different type
	# comment it out if you want it
	# $img1 rdbinary -file $filename_array($img1)
	
    } else {
	set slice_nb 0
	set filenm [format "%s%d.sdt" $filename_array($img1) $nb]
	$img1 read $filenm
	# using rdbinary is faster, but can cause problems if the
	# images have been converted to a different type
	# comment it out if you want it
	# $img1 rdbinary -file $filenm
    }
    # reset the events in curr_img if we are in overlay paint mode
    if { [info exists  curr_img] && [info exists  win_array($curr_img)] } {
	set_curr_event_loop  [winfo toplevel $win_array($curr_img)]
    }
    if { [info exists win_array($img1)] } { 
	set_curr_defaults $img1
	update_active_title $win_array($img1)
    }
}

proc make_img {img} {
    upvar $img img1
    
    global filename_array nb_slices_array curr_slice_array seq_array 
    
    if { ![info exists img1] } {
	set img1 [image create pict]
	set nb_slices_array($img1) 1
	set seq_array($img1) 0
	set curr_slice_array($img1)  0
	set filename_array($img1) "make_img"
    }
}

proc savefile {} {
    global curr_img

    if { [info exists curr_img] } {
	wrfile curr_img
    }
}

proc wrfile {{img1 {}} {name {}} {args {}} } {
     global filename_array nb_slices_array curr_slice_array seq_array dir file pattern showdot 
    upvar $img1 img

    if { [info exists img] } { 
	if { $name=={} } {
	    if { $seq_array($img) == 1 } {
		set name "$filename_array($img)$curr_slice_array($img)"
	    } else {
		set name $filename_array($img)
	    }
	}
	set text [format "Do you really want \n to save file \n %s ?" $name]
	set sure [tk_dialog .d {Save to disk} $text warning 0 {cancel} {save file as} {save file} ]
	if { $sure==0 } {
	    return
	}
	if { $sure==1 } {
	    set name [fileselect .fsel "Select A File" dir file pattern showdot]
	    if { $name == {} } {
		return
	    }
	}
	if { [file exists $name] } {
	    set text [format "Overwrite file \n %s ?" $name]
	    set sure [tk_dialog .d {Save to disk} $text warning 0 {cancel} {overwrite} ]
	    if { $sure==0 } {
		return
	    }
	}
	if { $args=={} } {
	    $img write $name
	} else {
	    eval {$img write $name} $args
	}
    }
}  

proc zoom { img {zx 1} {zy 1} {subx 1} {suby 1} } {
    upvar $img img1
    global _wid_ _len_ nb_slices_array filename_array curr_img seq_array curr_slice_array
    
    if { ![info exists img1] } { 
        #Create variable first
       return
    }   
    
    set g [image create pict]
    $g copy $img1 -zoom $zx $zy -subsample $subx $subx
    set nb_slices_array($g) $nb_slices_array($img1)
    set filename_array($g) $filename_array($img1)
    set seq_array($g) $seq_array($img1)
    set curr_slice_array($g) $curr_slice_array($img1)
    unset nb_slices_array($img1)
    unset filename_array($img1)
    unset seq_array($img1)
    unset curr_slice_array($img1)
    unset img1
    set img1 $g
    unset g
    if { [ info exists win_array($img1)] } {
	set _wid_ [image width $img1]
	set _len_ [image height $img1]
	$win_array($img1).c configure -width $_wid_ -height $_len_ 
	$win_array($img1).c configure -scrollregion "0 0 $_wid_ $_len_" -confine 1
	wm maxsize $win_array($img1) $_wid_ $_len_
	wm geometry $win_array($img1) [format "%dx%d" $_wid_ $_len_]
	set sbwidth [winfo reqwidth $win_array($img1).yscroll]
	topConfig $win_array($img1) $_wid_ $_len_ 
	set_curr_defaults $img1
    } 
   
}

proc colorbar {} {
    global color_bar_img curr_img win_color_bar

    if { ![info exists color_bar_img] } {
	if { [info exists curr_img] } {
	    set _wid_ [image width $curr_img]
	    set _len_ 30
	} else {
	    set _wid_ 400
	    set _len_ 30
	}
	set color_bar_img [image create pict -width $_wid_ -height $_len_]
	$color_bar_img colorbar
    } else {
	if { [info exists curr_img] && $color_bar_img != $curr_img} {
	    if { [image width $color_bar_img] != [image width $curr_img] } {
		if { [info exists win_color_bar] } {
		    destroy .win_color_bar
		} else {
		    delete_colorbar
		}
		set _wid_ [image width $curr_img]
		set _len_ 30
		set color_bar_img [image create pict -width $_wid_ -height $_len_]
		$color_bar_img colorbar
	    }
	}
    }
    disp_colorbar 
    colormap
}



proc delete_colorbar {} {
    global color_bar_img win_color_bar curr_img

    if { $color_bar_img == $curr_img } {
	image delete $color_bar_img
	unset color_bar_img
	unset win_color_bar
	unset curr_img
    } else {
	image delete $color_bar_img
	unset color_bar_img
	unset win_color_bar
    }
}

proc disp_colorbar { } {
    global color_bar_img win_color_bar curr_img Private_Colormap
   
    if { ![ info exists win_color_bar] } {
	set win_color_bar 1
	set win ".win_color_bar"
	catch {toplevel $win}
	
	set _wid_ [image width $color_bar_img]
	set _len_ [image height $color_bar_img]
	set Private_Colormap 0
	wm maxsize $win $_wid_ [expr $_len_+30]
	wm title $win "Colorbar"
	canvas $win.c -width $_wid_ -height [expr $_len_+30] -borderwidth 0 -highlightthickness 0
	$win.c create image 0 0 -anchor nw -image $color_bar_img
	pack $win.c -side top -expand 0 -fill none -in $win
	if { ![info exists curr_img] } {
	    set curr_img $color_bar_img
	}
	$win.c create line 0 $_len_ $_wid_ $_len_
	for {set i 0} {$i<10} {incr i} {
	    set _x_ [expr $_wid_/10.0*$i]
	    set _y_ $_len_
	    $win.c create line $_x_ $_y_ $_x_ [expr $_y_+5]
	} 
	set min [$curr_img getmin]
	set max [$curr_img getmax]
	
	$win.c create line 0 $_len_ 0 [expr $_len_+10]
	$win.c create text 5 [expr $_len_+30] -text $min -anchor sw
	set _x_ [expr $_wid_/2]
	$win.c create line $_x_ $_len_ $_x_ [expr $_len_+10]
	$win.c create text $_x_ [expr $_len_+30] -text [expr ($max-$min)/2] -anchor s

	$win.c create line $_wid_ $_len_ $_wid_ [expr $_len_+10]
	$win.c create text [expr $_wid_-5] [expr $_len_+30] -text $max -anchor se
	bind $win.c <Destroy> {
	    if { $win_color_bar == 1 } {
		delete_colorbar
	    }
	}
    }
}

proc set_cmap_level {} {
    global curr_img color_bar_img Private_Colormap

    if { [info exists curr_img] } {
	if { [info exists color_bar_img] } {
	    if {$color_bar_img != $curr_img } {
		$curr_img cmap_level $Private_Colormap
	    } else {
		set Private_Colormap [$curr_img cmap_level]
	    }
	} else {
	    $curr_img cmap_level $Private_Colormap
	}
    }
}

proc movie { img {first 0} {nb 0} {offset 1}} {
    upvar $img img1
    global nb_slices_array slice_nb scale_slice filename_array win_array title_array curr_slice_array
    
    if { ![info exists img1] } { 
	#puts "Create variable first"
	return
    }   
    if {  $nb_slices_array($img1) == 0 } {
	#puts "just one image "
	return
    }
    
    if { $first > $nb_slices_array($img1) } {
	return
    }
    
    for {set i $first} {$i<$nb} {set i [expr $i+$offset]} {
	if { $i > $nb_slices_array($img1) } {
	    set scale_slice $nb_slices_array($img1)
	    set slice_nb $nb_slices_array($img1)
	} else { 
	    set scale_slice $i
	    set slice_nb $i
	}
	#read image slice $scale_slice
	$img1 rdbinary -file $filename_array($img1)
	set curr_slice_array($img1) $slice_nb
	disp img1
	update idletasks
    }
}



proc make_palette { {pal 0} } {
    global curr_img curr_color loval loval_ent hival hival_ent midthresval diffthresval coledit _cwid_ _clen_ cx cy plist pedge col_npoints palette_button curr_min curr_max _thres_incr_ _thres_res_ palette_mapped has_plb_segment keypress
	   
    if { $pal != 0 } {
	if {[catch {set pal [toplevel .pal]}] > 0} {
	    catch {wm deiconify .pal}
	    if { [catch {raise .pal}] > 0} {
		puts "window already exists and needs to be raised"
	    }   
	    return
	}
    } else {
	set pal .
    }
    wm geometry $pal +580+30
    wm title $pal "Palette operations"
    set palette_mapped 1

    set fcolor [frame $pal.f0 -relief sunken -borderwidth 1]
    set cmap "gray"
    set color [frame $fcolor.f0]
    
    set color1 [frame $color.f1]    
    radiobutton $color1.gray -text gray -variable cmap -value gray -command colormap 
    radiobutton $color1.ct -text ct -variable cmap -value ct -command colormap 
    radiobutton $color1.hot -text hot -variable cmap -value hot -command colormap 
    radiobutton $color1.cold -text cold -variable cmap -value cold -command colormap 
    pack $color1.gray $color1.ct $color1.hot $color1.cold -side top  -anchor w -in $color1

    set color2 [frame $color.f2]
    radiobutton $color2.hls -text hls -variable cmap -value hls -command colormap 
    radiobutton $color2.spectrum -text spectrum -variable cmap -value spectrum -command colormap 
    radiobutton $color2.invert -text invert -variable cmap -value invert -command colormap 
    radiobutton $color2.rand -text random -variable cmap -value random -command colormap 
    pack $color2.hls $color2.spectrum $color2.invert $color2.rand  -side top  -anchor w -in $color2
    pack $color1 $color2 -side left -expand 1 -fill both -in $color
    
    set lut [frame $fcolor.f1]
    set lut1 [frame $lut.f1]
    set rread [button $lut1.read  -text "Read" -command "select_input_lut_file" ]
    set n_add [button $lut1.nadd -text "Add" -command add_color_node]
    set rset [button $lut1.rsetpal  -text "Reset" -command "reset_lut" ]
    pack $rread $n_add $rset -side top -expand 1 -fill x

    set lut2 [frame $lut.f2]
    set save [button $lut2.save -text "Save" -command "select_output_lut_file" ]
    set n_remove [button $lut2.remove -text "Remove" -command remove_color_node]
    set rapply [button $lut2.rapplypal  -text "Apply" -command "colormap" ]
    pack $save $n_remove $rapply -expand 1 -fill x 
    pack $lut1 $lut2 -side left -fill both -expand 1
    pack $color $lut -side top -fill both -expand 1 

    set level [frame $fcolor.f2]
    set cbar [button $level.colorbar -text Colorbar -command "colorbar" ]
    if {$palette_button==1} {
	set cmap_level [menubutton $level.cmap  -text "Colormap Level" -menu $level.cmap.menu -relief raised]
	pack $cmap_level -side top -fill both -expand 1 
	menu $level.cmap.menu -postcommand [list fillCmapMenu $level.cmap.menu "set_cmap_level" ]
	if {$has_plb_segment==1} {
	    set bclip [button $level.bclip -text "Clip data" -command "clip"]
	    set stretch [button $level.str -text "Stretch" -command "stretch_colormap"]
	    pack $bclip $stretch -side top -fill both -expand 1
	}
    }
    pack $cbar -side top -fill both -expand 1
    
    pack $level -side bottom -fill both -expand 1
    pack $fcolor -side right -anchor ne 
   
   
    set thresframe [frame $pal.f1]
    set loval $curr_min
    set lothres [frame $thresframe.f1]
    label $lothres.lothrestext -text "lo thresh"  
    entry $lothres.ent -width 7 -textvariable loval
    scale $lothres.lothres -from $curr_min -to $curr_max -orient horizontal \
        -variable loval -length 6c -resolution $_thres_res_ 
    pack $lothres.lothrestext $lothres.ent $lothres.lothres -side left -expand 1 -fill x -in $lothres
    pack $lothres -side top

    set hival $curr_max
    set hithres [frame $thresframe.f2]
    label $hithres.hithrestext -text "hi thresh" 
    entry $hithres.ent -width 7 -textvariable hival
    scale $hithres.hithres -from $curr_min -to $curr_max -orient horizontal \
        -variable hival -length 6c -resolution  $_thres_res_ 
    pack $hithres.hithrestext $hithres.ent $hithres.hithres -side left -expand 1 -fill x -in $hithres
    pack $hithres -side top
  
    set diffthresval [expr $curr_max-$curr_min]
    set midthresval [expr $curr_min+($diffthresval/2)]

    set midthres [frame $thresframe.f3]
    label $midthres.midthrestext -text "mean threshold  "  
    scale $midthres.midthres -from $curr_min -to $curr_max -orient horizontal \
        -variable midthresval -length 6c -resolution $_thres_res_ 
    pack $midthres.midthrestext $midthres.midthres -side left -expand 1 -fill x -in $midthres
    pack $midthres -side top

    set diffthres [frame $thresframe.f4]
    label $diffthres.diffthrestext -text "threshold range  " 
    scale $diffthres.diffthres -from 0.0 -to [expr $curr_max-$curr_min] -orient horizontal \
        -variable diffthresval -length 6c -resolution $_thres_res_ 
    pack $diffthres.diffthrestext $diffthres.diffthres -side left -expand 1 -fill x -in $diffthres
    pack $diffthres -side top
    
    set bframe [frame $pal.f2]
    set _cwid_ 100
    set _clen_ 100
    set intframe [frame $pal.in -width $_cwid_ -height $_clen_ -relief sunken -borderwidth 1]
    set title [frame $intframe.ti -width $_cwid_ -height 10  -relief raised -borderwidth 1]
    label $title.lab -text Intensity -fg black
    pack $title.lab
    set coledit(intensity) [canvas $intframe.c -width $_cwid_ -height $_clen_ -background gray]
    pack $title $coledit(intensity) -fill both -expand 1
    set redframe [frame $pal.red -width $_cwid_ -height $_clen_ -relief sunken -borderwidth 1]
    set title [frame $redframe.ti -width $_cwid_ -height 10  -relief raised -borderwidth 1]
    label $title.lab -text Red -fg red
    pack $title.lab
    set coledit(red) [canvas $redframe.c -width $_cwid_ -height $_clen_ -background white]
    pack $title $coledit(red) -fill both -expand 1
    set greenframe [frame $pal.green -width $_cwid_ -height $_clen_ -relief sunken -borderwidth 1] 
    set title [frame $greenframe.ti -width $_cwid_ -height 10  -relief raised -borderwidth 1]
    label $title.lab -text Green -fg green
    pack $title.lab
    set coledit(green) [canvas $greenframe.c -width $_cwid_ -height $_clen_ -background white]
    pack $title $coledit(green) -fill both -expand 1
    set blueframe [frame $pal.blue -width $_cwid_ -height $_clen_ -relief sunken -borderwidth 1]
    set title [frame $blueframe.ti -width $_cwid_ -height 10  -relief raised -borderwidth 1]
    label $title.lab -text Blue -fg blue
    pack $title.lab
    set coledit(blue) [canvas $blueframe.c -width $_cwid_ -height $_clen_ -background white]
    pack $title $coledit(blue) -fill both -expand 1
    pack $intframe $redframe $greenframe $blueframe  -side left -expand 1 -in $bframe
    pack $thresframe $bframe -side top -fill x -expand 1 -anchor nw

    set_color_binds 
   
    bind $diffthres.diffthres <B1-Motion> {
	if { ![info exists curr_img] } {
	    return
	}
	set loval [expr $midthresval-$diffthresval/2]
	set hival [expr $midthresval+$diffthresval/2] 
	$curr_img cmap_threshold $loval $hival
    }

    bind $midthres.midthres <B1-Motion> {
	if { ![info exists curr_img] } {
	    return
	}
	set loval [expr $midthresval-$diffthresval/2]
	set hival [expr $midthresval+$diffthresval/2]
	$curr_img cmap_threshold $loval $hival
    }

    bind $lothres.lothres <B1-Motion> {
	if { ![info exists curr_img] } {
	    return
	}
	if { $loval >= $hival } {
	    set loval [expr $hival -$_thres_incr_] 
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
    bind $hithres.hithres <B1-Motion> {
	if { ![info exists curr_img] } {
	    return
	}
	if { $loval >= $hival } {
	    set hival [expr $loval+$_thres_incr_]
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
	
    bind $lothres.lothres <ButtonRelease-1> {
	if { ![info exists curr_img] } {
	    return
	}
	if { $loval >= $hival } {
	    set loval [expr $hival -$_thres_incr_]   
	} 
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }

    
    bind $hithres.hithres <ButtonRelease-1> {
	if { ![info exists curr_img] } {
	    return
	}
	if { $loval >= $hival } {
	    set hival [expr $loval+$_thres_incr_]
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
    bind $midthres.midthres <ButtonRelease-1> {
	if { ![info exists curr_img] } {
	    return
	}
	if { $loval == $curr_min } {
	    set midthresval [expr $curr_min+$diffthresval/2]
	    set hival [expr $curr_min+$diffthresval]
	} elseif { $hival == $curr_max } {
	    set midthresval [expr $curr_max-$diffthresval/2]
	    set loval [expr $curr_max-$diffthresval]   
	}
	$curr_img cmap_threshold $loval $hival
    }
    bind $diffthres.diffthres <ButtonRelease-1> {
	if { ![info exists curr_img] } {
	    return
	}
	if { $loval == $curr_min } {
	    set diffthresval [expr ($midthresval-$curr_min)*2]
	    set hival [expr $curr_min+$diffthresval]
	} elseif { $hival == $curr_max } {
	    set diffthresval [expr ($curr_max-$midthresval)*2]
	    set loval [expr $curr_max-$diffthresval]
	}
	$curr_img cmap_threshold $loval $hival
    }

    bind $lothres.ent <Leave> {
	if { ![info exists curr_img] } {
	    return
	}
	%W configure -textvariable loval
	if {$keypress==1} {
	    set keypress 0
	} else {
	    return
	}
	if { [string compare $loval_ent ""] == 0 } {
	    set loval_ent [expr $curr_max-$diffthresval]
	}
	set loval $loval_ent
	
	if { $loval >= $hival } {
	    set loval [expr $hival -$_thres_incr_]
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
    bind $hithres.ent <Leave> {
	if { ![info exists curr_img] } {
	    return
	}
	%W configure -textvariable hival
	if {$keypress==1} {
	    set keypress 0
	} else {
	    return
	}
	if { [string compare $hival_ent ""] == 0 } {
	    set hival_ent [expr $curr_min+$diffthresval]
	}
	set hival $hival_ent
	
	if { $loval >= $hival } {
	    set hival [expr $loval+$_thres_incr_]
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
    bind $lothres.ent <Return> {
	if { ![info exists curr_img] } {
	    return
	}
	if {$keypress==1} {
	    set keypress 0
	}
	if { [string compare $loval_ent ""] == 0 } {
	    set loval_ent [expr $curr_max-$diffthresval]
	}
	set loval $loval_ent
	%W configure -textvariable loval
	if { $loval >= $hival } {
	    set loval [expr $hival -$_thres_incr_]
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
    bind $hithres.ent <Return> {
	if { ![info exists curr_img] } {
	    return
	}
	if {$keypress==1} {
	    set keypress 0
	} 
	if { [string compare $hival_ent ""] == 0 } {
	    set hival_ent [expr $curr_min+$diffthresval]
	}
	set hival $hival_ent
	%W configure -textvariable hival
	if { $loval >= $hival } {
	    set hival [expr $loval+$_thres_incr_]
	}
	set midthresval [expr ($loval+$hival)/2]
	set diffthresval [expr $hival-$loval]
	$curr_img cmap_threshold $loval $hival
    }
    foreach evnt {<Enter>} {
	bind $lothres.ent $evnt {
	    if { ![info exists curr_img] } {
		return
	    }
	    set keypress 0
	    set loval_ent $loval
	    %W configure -textvariable loval_ent
	}
	bind $hithres.ent $evnt {
	    if { ![info exists curr_img] } {
		return
	    }
	    set keypress 0
	    set hival_ent $hival
	    %W configure -textvariable hival_ent
	}
    }
    foreach evnt {<KeyPress>} {
	bind $lothres.ent $evnt {
	    if { ![info exists curr_img] } {
		return
	    }
	    if {$keypress==0} {
		set keypress 1 
		set loval_ent $loval
		%W configure -textvariable loval_ent
	    }
	}
	bind $hithres.ent $evnt {
	    if { ![info exists curr_img] } {
		return
	    } 
	    if {$keypress==0} {
		set keypress 1 
		set hival_ent $hival
		%W configure -textvariable hival_ent
	    }
	}
    }

    bind $pal <Destroy> {
	set palette_mapped 0
	if { $palette_button == 1 } {
	    bind .pal <Destroy> {}
	}
    }
}

proc normalize {{a {0}} {b {0}}} {
    global curr_img _DispMax_ _DispMin_ _userDispMin_ _userDispMax_

    if { [info exists curr_img] } {
	set _userDispMin_ $a
	set _userDispMax_ $b
	$curr_img range $a $b
	set _DispMin_ [$curr_img getmin]
	set _DispMax_ [$curr_img getmax]
    }
}

proc stretch_colormap {} {
    global curr_img curr_color plist cx cy _cwid_ _clen_ cmap col_npoints

    if { ![info exists curr_img] } {
	return
    }
    $curr_img cmap_stretch
}

proc colormap {} {
    global curr_img curr_color plist cx cy _cwid_ _clen_ cmap col_npoints
    global currgn currimg

    if { ![info exists curr_img] } {
	return
    }
    set old_color $curr_color
    set clist {intensity red green blue}
    foreach k $clist {
	set curr_color $k
	set l {}
	for {set i 0} {$i<$col_npoints($curr_color)} {incr i} {
	    lappend l [expr int(floor($cx($plist($i,$curr_color),$curr_color)))]
	    lappend l [expr int(floor($cy($plist($i,$curr_color),$curr_color)))]
	}
#	eval {$curr_img cmap_stretch $curr_color $_cwid_ $_clen_} $l
    }
#    $curr_img colormap $cmap
    catch {set curr_color $old_color}
#    set powCurrentLut($curr_img) $l
    powCmapStretchIntensity $_cwid_ $_clen_ $l
    powReditherImages $currgn $currimg
}

proc reset_lut {} {
    global coledit curr_img curr_color plist pedge cx cy _cwid_ _clen_ col_npoints 

    set npts [expr $col_npoints($curr_color) -1]
    for {set i 0} {$i<$col_npoints($curr_color)} {incr i} {
	set _x_ [expr $_cwid_.0/$npts.0*$i]
	set _y_ [expr $_clen_*($npts-$i)/$npts.0]
	set p $plist($i,$curr_color)
	$coledit($curr_color) coords $p [expr $_x_-3] [expr $_y_-3] [expr $_x_+3] [expr $_y_+3]
	set cx($p,$curr_color) $_x_
	set cy($p,$curr_color) $_y_
    }
    for {set i 1} {$i<$col_npoints($curr_color)} {incr i} {
	set old_i [expr $i-1]
	$coledit($curr_color) coords $pedge($i,$curr_color) \
		$cx($plist($old_i,$curr_color),$curr_color) \
		$cy($plist($old_i,$curr_color),$curr_color) \
		$cx($plist($i,$curr_color),$curr_color)	\
		$cy($plist($i,$curr_color),$curr_color)	
	$coledit($curr_color) lower $pedge($i,$curr_color)
    }
    colormap 
}

proc image_open value {
    global img_nb curr_img color_bar_img dir file pattern showdot 
    
    if { $value == 1 } {
	incr img_nb
	set file [fileselect .fsel "Select A File" dir file pattern  showdot]
	if { $file != {} } {
	    rdfile $file img$img_nb
	    disp img$img_nb
	}
    } else {
	if { ![ info exists curr_img] } {
	    return
	}
	if { [info exists color_bar_img] && $curr_img==$color_bar_img} {
	    return
	}
	set file [fileselect .fsel "Select A File" dir file pattern showdot]
	if { $file != {} } {
	    rdfile $file curr_img
	    disp curr_img
	}
    }
}

proc copy { src dest args } {
    upvar $src src1
    upvar $dest dest1
    global scale_slice _wid_ _len_ win_array curr_img filename_array curr_filename nb_slices_array \
nb_slices curr_nb_slices curr_slice_array seq_array sbwidth

    if { [info exists src1] } {
	if { ![info exists dest1] } {
	    set dest1 [image create pict]
	}
	eval {$dest1 copy $src1} $args
	set slice_nb 0
	set scale_slice 
	set filename_array($dest1) "copy"
	set curr_filename "copy"
	set nb_slices_array($dest1) $nb_slices
	set seq_array($dest1) 0
	set curr_slice_array($dest1) 0
	set curr_nb_slices $nb_slices
	set curr_img $dest1
	if { [ info exists win_array($dest1)] } {
	    set _wid_ [image width $dest1]
	    set _len_ [image height $dest1]
	    $win_array($dest1).c configure -width $_wid_ -height $_len_ 
	    $win_array($dest1).c configure -scrollregion "0 0 $_wid_ $_len_" -confine 1
	    wm maxsize $win_array($dest1) $_wid_ $_len_
	    wm geometry $win_array($dest1) [format "%dx%d" $_wid_ $_len_]
	    set sbwidth [winfo reqwidth $win_array($dest1).yscroll]
	    topConfig $win_array($dest1) $_wid_ $_len_
	}
    } else {
	puts "source image $src1 does not exist"
    }
}

proc get_min_max {} {
    global curr_img curr_min curr_max _thres_res_ _thres_incr_
    
    if { [info exists curr_img] } {
	set max [$curr_img getmax]
	set min [$curr_img getmin]
	if {$max==$min} {
	    return
	}
	set t [format "%.1g" [expr ($max-$min)/255.0]] 
	set div [expr $max/$t]
	set h [expr ceil($div)]
	set curr_max [expr $t*$h]
	set div [expr $min/$t]
	set h [expr floor($div)]
	set curr_min [expr $t*$h]
	set _thres_res_ $t
	set _thres_incr_ $t
    }
}

proc update_scale {} {
    global curr_min curr_max _thres_res_ pal_window palette_button palette_mapped
  
    if { ($palette_button == 1) && ($palette_mapped == 1) } {
	set old_min $curr_min
	set old_max $curr_max
	
	set x [.pal.f1.f1.lothres get]
	set x1 [lindex [.pal.f1.f1.lothres coords $curr_min] 0]
	set x2 [lindex [.pal.f1.f1.lothres coords $curr_max] 0]
	
	set y [.pal.f1.f2.hithres get]
	set z [.pal.f1.f3.midthres get]
	set t [.pal.f1.f4.diffthres get]
	
	get_min_max
	
	set fac [expr ($curr_max-$curr_min)/($old_max-$old_min)]
	.pal.f1.f1.lothres config -from $curr_min -to $curr_max -resolution $_thres_res_
	.pal.f1.f1.lothres set [expr ($x-$old_min)*$fac+$curr_min]
	
	.pal.f1.f2.hithres config -from $curr_min -to $curr_max -resolution $_thres_res_
	.pal.f1.f2.hithres set [expr ($y-$old_min)*$fac+$curr_min]
	
	.pal.f1.f3.midthres config -from $curr_min -to $curr_max -resolution $_thres_res_
	.pal.f1.f3.midthres set [expr ($z-$old_min)*$fac+$curr_min]
	
	.pal.f1.f4.diffthres config -from 0.0 -to [expr $curr_max-$curr_min] -resolution $_thres_res_
	.pal.f1.f4.diffthres  set [expr $t*$fac]
    } else {
	get_min_max
    }
}


proc fillCmapMenu {topmenu cmd} {
    global Private_Colormap curr_img win_array color_bar_img

    if { [string compare "image_open 1" $cmd] != 0 } {
	if { [info exists curr_img] && [info exist win_array($curr_img)] } {
	    set Private_Colormap [$curr_img cmap_level]
	}
    }
    catch {$topmenu delete 0 last}
    $topmenu add radiobutton -label "Shared Colormap" -variable Private_Colormap -value 0   -command $cmd
    $topmenu add radiobutton -label "Default Colormap" -variable Private_Colormap -value 1  -command $cmd
    $topmenu add radiobutton -label "Shared Private Cmap" -variable Private_Colormap -value 2  -command $cmd
    $topmenu add radiobutton -label "Private Colormap" -variable Private_Colormap -value 3  -command  $cmd

}

proc clip {{lo {}} {hi {}} } {
    global curr_img color_bar_img loval hival

    if { [info exists curr_img] } {
	if { [info exists color_bar_img] } {
	    if {$color_bar_img != $curr_img } {
		if {$hi=={}} {
		    set hi $hival 
		}
		if {$lo=={}} {
		    set lo $loval
		}
		$curr_img clip $lo $hi
	    }
	} else {
	    if {$hi=={}} {
		set hi $hival 
	    }
	    if {$lo=={}} {
		set lo $loval
	    }
	    $curr_img clip $lo $hi
	}
	get_min_max
	update_scale
    }
}

proc quit {} { 
    exit 
}


proc control_panel {} {
    global  curr_img  scale_slice nb_slices_array curr_slice_array palette_button Private_Colormap visu_help_file has_plb_segment has_blt _DispMin_ _DispMax_ _userDispMin_ _userDispMax__x_ _y_ _pix_ _wid_ _len_
	  

    set pixtop .
    wm geometry . 530x140+10+30
    wm title $pixtop "[wm title $pixtop]: Control Panel"
    set f0 [frame $pixtop.f0]
  
    menubutton .files -text Files -menu .files.menu -relief raised 
    menu .files.menu
    .files.menu add cascade -label "New" -menu .files.menu.new  
    .files.menu add cascade -label "Open" -menu .files.menu.open
    .files.menu add command -label "Save" -command "savefile" 
    menu .files.menu.new -postcommand [list fillCmapMenu .files.menu.new {image_open 1}]
    menu .files.menu.open -postcommand [list fillCmapMenu .files.menu.open {image_open 0;set_cmap_level}]
    
    button .palette -text Palette -command "make_palette 1" 
    button .broi -text ROI -command "make_roi" 
    if { $has_blt == 1} {
	menubutton .prof -text " 1D " -menu .prof.m  -relief raised
	menu .prof.m -postcommand fillGraphMenu
    }
    if { $has_plb_segment == 1} {
	button .filter -text Filter -command "make_filter" 
    }
    button .overlay -text Overlays -command "make_overlay" 
    button .help -text Help -command "exec netscape $visu_help_file &"
    button .quit -text Quit -command exit 
    pack .files .palette .overlay .broi  -side left -fill x -expand 1 -in $f0
    if {$has_blt == 1} {
	 pack .prof  -side left -fill x -expand 1 -in $f0
    }
    if {$has_plb_segment == 1} {
	 pack .filter  -side left -fill x -expand 1 -in $f0
    }
    pack .help .quit -side left -fill x -expand 1 -in $f0
    pack $f0 -side top -fill both -expand 1 
    set palette_button 1
    set f1 [frame $pixtop.f1]

    set f2 [frame $pixtop.f2 -width 4c]
    entry $f2.x -width 4 -textvariable _x_
    label $f2.label1 -text "x" 
    entry $f2.y -width 4 -textvariable _y_
    label $f2.label2 -text " = " 
    label $f2.pix -textvariable _pix_ 
    pack $f2.x $f2.label1 $f2.y $f2.label2 $f2.pix -side left -in $f2
    pack $f2 -side left -fill y -expand 0 -in $f1
   
    set f3 [frame $pixtop.f3]
    label $f3.text -text "rows" 
    label $f3.value -textvariable _len_ 
    pack $f3.text $f3.value -side left -expand 0 -fill none -in $f3
   
    
    set f4 [frame $pixtop.f4]
    label $f4.text -text "cols" 
    label $f4.value -textvariable _wid_ 
    pack $f4.text $f4.value -side left -expand 0 -fill none -in $f4

    set f5 [frame $pixtop.f5]
    label $f5.text -text "nb_slices" 
    label $f5.value -textvariable curr_nb_slices 
    pack $f5.text $f5.value -side left -expand 0 -fill none -in $f5

    pack $f5 -side right -expand 0 -fill none -anchor e -in $f1
    pack $f4 -side right -expand 0 -fill none -anchor e -in $f1
    pack $f3 -side right -expand 0 -fill none -anchor e -in $f1

    pack $f1 -side top -fill both -expand 1
    
    set f6 [frame $pixtop.f6]
    label $f6.labelumin -text "user min " 
    entry $f6.umin -width 7 -textvariable _userDispMin_ 
    label $f6.labelumax -text " user max " 
    entry $f6.umax -width 7 -textvariable _userDispMax_ 
    label $f6.labelmin -text " min " 
    label $f6.min -textvariable _DispMin_ 
    label $f6.labelmax -text " max " 
    label $f6.amax -textvariable _DispMax_ 
    pack $f6.labelumin $f6.umin $f6.labelumax $f6.umax $f6.labelmin $f6.min $f6.labelmax $f6.amax -side left -expand 0 -fill x -in $f6
    pack $f6 -side top -fill both -expand 1

    set slice [frame $pixtop.f7]
    label .slicetext -text "slice number" 
    button .plus -text ">>" -command "upslice" 
    button .minus -text "<<" -command "downslice" 
    scale .slice -from 0 -to 9 -orient horizontal \
        -variable scale_slice -length 8c
    pack .slicetext .plus .minus .slice -side left -expand 1 -fill x -in $slice
    pack $slice -side top
    
    bind .slice <ButtonRelease> {
	if { ![info exists curr_img] } {
	    return
	}
	if {  $nb_slices_array($curr_img) == 1 } {
	    #just one image 
	    set scale_slice 0
	}
	if { $scale_slice > $nb_slices_array($curr_img) } {
	    set scale_slice $nb_slices_array($curr_img)
	} 
	set curr_slice_array($curr_img) $scale_slice
        rdslice curr_img $scale_slice
    }


    bind $f2.x <Leave> {
	if { ![info exists curr_img] } {
	    return
	}
	if { ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
	    set _pix_ [$curr_img get $_x_ $_y_]	
	}
    }	
    bind $f2.x <Return> {
	if { ![info exists curr_img] } {
	    return
	}
	if { ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
	    set _pix_ [$curr_img get $_x_ $_y_]	
	}
    }	 
    bind $f2.y <Leave> {
	if { ![info exists curr_img] } {
	    return
	}
	if { ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
	    set _pix_ [$curr_img get $_x_ $_y_] 
	}
    }	
    bind $f2.y <Return> {
	if { ![info exists curr_img] } {
	    return
	}
	if { ($_x_>0) && ($_x_<$_wid_) && ($_y_>0) && ($_y_<$_len_) } {
	    set _pix_ [$curr_img get $_x_ $_y_] 
	}
    }	
    
    bind $f6.umin <Return> {
	normalize $_userDispMin_ $_userDispMax_
    }
   
    bind $f6.umax <Return> {
	normalize $_userDispMin_ $_userDispMax_
    }

}



proc mkedges {} {
    global coledit cx cy plist pedge _cwid_ _clen_ curr_color col_npoints

    for {set i 1} {$i<$col_npoints($curr_color)} {incr i} {
	set old_i [expr $i-1]
	set pedge($i,$curr_color) [$coledit($curr_color) create line \
		$cx($plist($old_i,$curr_color),$curr_color) \
		$cy($plist($old_i,$curr_color),$curr_color) \
		$cx($plist($i,$curr_color),$curr_color) \
		$cy($plist($i,$curr_color),$curr_color) -tag edge]
	$coledit($curr_color) lower $pedge($i,$curr_color)
    }
}


proc mknodes {} {
    global coledit cx cy plist _cwid_ _clen_ curr_color col_npoints

    set npts [expr $col_npoints($curr_color)-1]
    for {set i 0} {$i<$col_npoints($curr_color)} {incr i} {
	set x [expr $_cwid_.0/$npts.0*$i]
	set y [expr $_clen_*($npts-$i)/$npts.0]
	set plist($i,$curr_color) [$coledit($curr_color) create oval [expr $x-3] [expr $y-3] [expr $x+3] [expr $y+3] -outline black -fill white -tags node]
	set cx($plist($i,$curr_color),$curr_color) $x
	set cy($plist($i,$curr_color),$curr_color) $y
    }
}

proc mvnode {node x y} {
    global coledit curr_img curr_color plist pedge cx cy _cwid_ _clen_ col_npoints

    if { $x < 0 } {
	set x 0
    } elseif { $x > $_cwid_ } {
	set x $_cwid_ 
    }
    if {$y < 0} {
	set y 0
    } elseif {$y > $_clen_} {
	set y $_clen_
    }
	
    if { $node == $plist(0,$curr_color) } {	
	set p $plist(0,$curr_color)
	set p1 $plist(1,$curr_color)
	if { $x>$cx($p1,$curr_color) } {
	    set x $cx($p1,$curr_color)
	}
	if { $x<($_clen_-$y)}  {
	    set x 0
	} else {
	    set y $_clen_ 
	}   
	set cx($p,$curr_color) $x
	set cy($p,$curr_color) $y
	$coledit($curr_color) coords $p [expr $x-3] [expr $y-3] [expr $x+3] [expr $y+3]
	$coledit($curr_color) coords $pedge(1,$curr_color) $cx($p,$curr_color) $cy($p,$curr_color) $cx($p1,$curr_color) $cy($p1,$curr_color)

    } elseif { $node == $plist([expr $col_npoints($curr_color)-1],$curr_color) } {
	set p  $plist([expr $col_npoints($curr_color)-2],$curr_color)
	set p1 $plist([expr $col_npoints($curr_color)-1],$curr_color)
	
	if { $x<$cx($p,$curr_color) } {
	   set x $cx($p,$curr_color)
	}
	if { $x<($_clen_-$y)}  {
	    set y 0
	    
	} else {
	    set x $_cwid_
	}
	set cx($p1,$curr_color) $x 
	set cy($p1,$curr_color) $y
	 
	$coledit($curr_color) coords $p1 [expr $x-3] [expr $y-3] [expr $x+3] [expr $y+3]
	$coledit($curr_color) coords $pedge([expr $col_npoints($curr_color)-1],$curr_color) $cx($p,$curr_color) $cy($p,$curr_color) $cx($p1,$curr_color) $cy($p1,$curr_color)
    } else {
	set i 1
	while {$node != $plist($i,$curr_color) } {
	    incr i
	}
	set p $plist($i,$curr_color)
	set pm $plist([expr $i-1],$curr_color)
	set pp $plist([expr $i+1],$curr_color)

	if { $x<$cx($pm,$curr_color) } {
	    set x $cx($pm,$curr_color)
	} elseif {$x>$cx($pp,$curr_color) } {
	    set x $cx($pp,$curr_color)
	}
	$coledit($curr_color) move $node [expr $x-$cx($p,$curr_color)] [expr $y-$cy($p,$curr_color)]
	set cx($p,$curr_color) $x
	set cy($p,$curr_color) $y
	$coledit($curr_color) coords $pedge($i,$curr_color) $cx($pm,$curr_color) $cy($pm,$curr_color) $cx($p,$curr_color) $cy($p,$curr_color)
	$coledit($curr_color) coords $pedge([expr $i+1],$curr_color) $cx($p,$curr_color) $cy($p,$curr_color) $cx($pp,$curr_color) $cy($pp,$curr_color)
    
    }
  
    if { [info exists curr_img] } {
	set l {}
	for {set i 0} {$i<$col_npoints($curr_color)} {incr i} {
	    lappend l [expr int(floor($cx($plist($i,$curr_color),$curr_color)))]
	    lappend l [expr int(floor($cy($plist($i,$curr_color),$curr_color)))]
	}
	eval {$curr_img cmap_stretch $curr_color $_cwid_ $_clen_} $l
    }
}

proc set_curr_color col {
    global coledit curr_color

    set bgcol [$coledit($col) cget -bg]
    $coledit($curr_color) configure -bg $bgcol
    set curr_color $col
    $coledit($curr_color) configure -bg gray
}

proc set_node_number {numnodes} {
    global coledit curr_img curr_color plist pedge cx cy _cwid_ _clen_ col_npoints

    #delete existing nodes
    for {set i 0} {$i<$col_npoints($curr_color)} {incr i} {
	$coledit($curr_color) delete $plist($i,$curr_color)
    }
    for {set i 1} {$i<$col_npoints($curr_color)} {incr i} {
	$coledit($curr_color) delete $pedge($i,$curr_color)
    }

    set col_npoints($curr_color) $numnodes

    mknodes
    mkedges
}

proc add_color_node {} {
    global col_npoints curr_color

    set num $col_npoints($curr_color)
    set_node_number [incr num]
    colormap
}

proc remove_color_node {} {
    global col_npoints curr_color
    
    set num $col_npoints($curr_color)
    if {$num==2} {
	return
    } else {
	set num [expr $num-1]
	set_node_number $num
    } 
    colormap
}

proc save_color_nodes {filename} {
    global curr_color col_npoints cx cy plist _cwid_ _clen_

    set f [open $filename w]
    set l {red green blue intensity}
    foreach k  $l {
	puts $f "$col_npoints($k) \#$k"
	for {set i 0} {$i<$col_npoints($k)} {incr i} {
	    set x [expr $cx($plist($i,$k),$k)/double($_cwid_)]
	    set y [expr (double($_clen_)-$cy($plist($i,$k),$k))/double($_clen_)]
	    puts $f "$x $y"
	}
	puts $f ""
    }
    close $f
}
	

proc read_color_nodes {filename} {
    global coledit curr_color col_npoints cx cy plist pedge _cwid_ _clen_

    set f [open $filename r]
    set l {red green blue intensity}
    foreach k  $l {
	set_curr_color $k
	for {set i 0} {$i<$col_npoints($k)} {incr i} {
	    $coledit($k) delete $plist($i,$k)
	} 
	for {set i 1} {$i<$col_npoints($k)} {incr i} { 
	    $coledit($k) delete $pedge($i,$k)
	}
	gets $f line 
	if { [eof $f] } {
	    close $f
	    return
	}
	scan $line "%d" col_npoints($k) 
	mknodes
	mkedges 
	for {set i 0} {$i<$col_npoints($k)} {incr i} {
	    gets $f line 
	    if { [eof $f] } {
		close $f
		return
	    }
	    scan $line "%g %g" x y 
	    mvnode $plist($i,$k) [expr $x*$_cwid_] [expr $_clen_*(1-$y)]
	} 
	gets $f line 
	if { [eof $f] } {
	    close $f
	}
    }
    colormap
}
	    

proc select_input_lut_file {} {
    global dir file pattern showdot 

    set oldpattern $pattern
    set pattern "*.tr.dat"
    set name [fileselect .fsel "Select A File" dir file pattern showdot]
    if { $name == {} } {
	set pattern $oldpattern
	return
    }
    read_color_nodes $name
    set pattern $oldpattern
}

proc select_output_lut_file {} {
    global dir file pattern showdot 

    set oldpattern $pattern
    set pattern "*.tr.dat"
    set name [fileselect .fsel "Select A File" dir file pattern showdot]
    if { $name == {} } {
	set pattern $oldpattern
	return
    }
    if { [file exists $name] } {
	set text [format "Overwrite file \n %s ?" $name]
	set sure [tk_dialog .d {Save to disk} $text warning 0 {cancel} {overwrite} ]
	if { $sure==0 } {
	    set pattern $oldpattern
	    return
	}
    }
    save_color_nodes $name
    set pattern $oldpattern
}

proc set_color_binds {} {
    global coledit curr_color plist pedge col_npoints oldx oldy _slope_
 
    set old_color $curr_color
    set l {intensity red green blue}
    foreach k  $l {
	set_curr_color $k
	mknodes
	mkedges 
	
	bind $coledit($curr_color) <Button-1> {
	    set can %W
	    set l {intensity red green blue}
	    foreach k  $l {
		if {$coledit($k) == $can } {
		    set_curr_color $k
		}
	    }
	}

	$coledit($curr_color) bind node <B1-Motion> {
	    set p [$coledit($curr_color) find withtag current]
	    mvnode $p %x %y 
	}
   
	$coledit($curr_color) bind edge <Button-2> { 
	    set can %W
	    set l {intensity red green blue}
	    foreach k  $l {
		if {$coledit($k) == $can } {
		    set_curr_color $k
		}
	    }
	    set e  [$coledit($curr_color) find withtag current]
	    for {set i 1} {$i<$col_npoints($curr_color)} {incr i} {
		if {$e==$pedge($i,$curr_color)} {
		    set oldx %x
		    set oldy %y
		    set p $plist([expr $i-1],$curr_color)
		    set p1 $plist($i,$curr_color)
		    set x1 $cx($p,$curr_color)
		    set x2 $cx($p1,$curr_color)
		    set y1 $cy($p,$curr_color)
		    set y2 $cy($p1,$curr_color) 
		    if {abs(double($x1-$x2))>0.01} {
			set _slope_ [expr double($y2-$y1)/double($x2-$x1)]
		    } else {
			set _slope_ "NaN"
		    }
		}
	    }
	}

	$coledit($curr_color) bind edge <B2-Motion> {
	    set e  [$coledit($curr_color) find withtag current]
	    if {$col_npoints($curr_color)==2} {
		mvramp [expr $oldx-%x] [expr $oldy-%y]
		set oldx %x
		set oldy %y
	    } else {
		
		for {set i 1} {$i<$col_npoints($curr_color)} {incr i} {
		    if {$e==$pedge($i,$curr_color)} {
			set p $plist([expr $i-1],$curr_color)
			set x [expr $cx($p,$curr_color)-$oldx+%x]
			set y [expr $cy($p,$curr_color)-$oldy+%y]
			mvnode $p $x $y
			set p $plist($i,$curr_color)
			set x [expr $cx($p,$curr_color)-$oldx+%x]
			set y [expr $cy($p,$curr_color)-$oldy+%y]
			mvnode $p $x $y
			set oldx %x
			set oldy %y
		    }
		}
	    }
	}
    }
    set_curr_color $old_color
}

proc mvramp {dx dy} {
    global coledit curr_img curr_color plist pedge cx cy _cwid_ _clen_ col_npoints _slope_ 

    set p $plist(0,$curr_color)
    set p1 $plist(1,$curr_color)
    
    set x1 $cx($p,$curr_color)
    set x2 $cx($p1,$curr_color) 
    set y1 $cy($p,$curr_color)
    set y2 $cy($p1,$curr_color)
   
    set x [expr $x1-$dx]
    set y [expr $y1-$dy]

    if { $x<($_clen_-$y)}  {
	set x 0
    } else {
	set y $_clen_ 
    }   
     
    if { [string compare $_slope_ "NaN"] != 0} {
	set a $_slope_
	set b [expr ($y-$a*$x)]
	
	set newy  [expr $a*$_cwid_+$b]
	if { $newy < 0 } {
	    if {$a!=0} {
		set xp [expr -$b/$a]
	    } else {
		set xp $x
	    }
	    set yp 0
	} else {
	    set xp $_cwid_
	    set yp $newy
	}
	mvnode $p $x $y
	mvnode $p1 $xp $yp
    } else {
	set x2 $x
	set y2 [expr $y2-$dy]
	#update point in most logical order
	if {$dx<0} {
	    mvnode $p1 $x2 $y2
	    mvnode $p $x $y
	} else {
	    mvnode $p $x $y
	    mvnode $p1 $x2 $y2
	}
    }
    update idletasks
}


























# set global variables 
# --------------------
if { ![info exists dir] }  {set dir "."}
if { ![info exists file] } {set file ""}
if { ![info exists pattern] } {set pattern "*.sdt"}
if { ![info exists showdot] } {set showdot 0}


# file selector box
# -----------------

proc fileselect {w title dir1 file1 pattern1 showdot1} {
    upvar $dir1 dir
    upvar $file1 file
    upvar $pattern1 pattern
    upvar $showdot1 showdot
    global fileselect


    # local functions
    # ---------------

    proc settabstops {toplevel list} {
	for {set i 0} {$i < [llength $list]} {incr i} {
	    set tab1 [lindex $list $i]
	    set tab2 [lindex $list [expr ( $i + 1 ) % [llength $list]]]
	    bindtags $tab1 [list $toplevel [winfo class $tab1] $tab1]
	    bind $tab1 <Tab> "focus $tab2"
	    bind $tab2 <Shift-Tab> "focus $tab1"
	}
    }

    proc dirnormalize {dir} {
	if {[string compare $dir ""] == 0} {set dir "."}
	set list [split $dir "/"]
	switch [lindex $list 0] {
	    ""      { set d [list ""] }
	    default { set d [split [pwd] "/"] }
	}
	foreach i $list {
	    switch $i {
		""      -
		"."     { }
		".."	{ if {[string compare [lindex $d end] ""] != 0} {
		             set d [lrange $d 0 [expr [llength $d] - 2]]
		          }
		        }
		default { lappend d $i }
	    }
	}
	set s ""
	foreach i $d { append s $i/ }
	return $s
    }

    proc readdir {w dir file pattern} {
	global fileselect

	set dir [dirnormalize $dir]
	if {![file readable $dir]} {
	    bell
	    return
	}
	set file [file tail $file]

	if {[string compare $pattern ""] == 0} { set pattern "*" }

	set fileselect(path) $dir
	set fileselect(file) $file
	set fileselect(pattern) $pattern
	set fileselect(selection) $dir$file
	$w.filter.entry delete 0 end
	$w.filter.entry insert 0 "$dir$pattern"

	
	set file_list [lsort [glob -nocomplain "$dir{.,}*"]]
	$w.lists.frame1.list delete 0 end
	if {$fileselect(showdot)} {
	    foreach i $file_list {
		if {[file isdirectory $i]} { $w.lists.frame1.list insert end $i }
	    }
	} else {
	    $w.lists.frame1.list insert end "$dir."
	    $w.lists.frame1.list insert end "$dir.."  
	    foreach i $file_list {
		if {[file isdirectory $i]} {
		    if { ![string match "$dir\.*" $i] } {
			$w.lists.frame1.list insert end $i
		    }
		}
	    }
	}
	
	if {[$w.lists.frame1.list size] > 0} {
	    $w.lists.frame1.list xview \
		    [string last "/" [$w.lists.frame1.list get 0]]
	}

	if {$fileselect(showdot)} {
	    set file_list [lsort [glob -nocomplain "$dir{.,}$pattern"]]
	} else {
	    set file_list [lsort [glob -nocomplain "$dir$pattern"]]
	}
	$w.lists.frame2.list delete 0 end
	foreach i $file_list {
	    if {![file isdirectory $i]} {
		$w.lists.frame2.list insert end [file tail $i]
	    }
	}
	if {[$w.lists.frame2.list size] > 0} {
	    $w.lists.frame2.list xview \
		    [string last "/" [$w.lists.frame2.list get 0]]
	}
    }


    # event actions
    # -------------

    proc cmd_showdot {w} {
	global fileselect

	readdir $w $fileselect(path) $fileselect(file) $fileselect(pattern)
    }

    proc cmd_filesel_filter {w} {
	set dir [file dirname [$w.filter.entry get]]
	set pattern [file tail [$w.filter.entry get]]
	readdir $w $dir "" $pattern
    }

    proc cmd_filesel_left_dblb1 {w list} {
	global fileselect

	if {[$list size] > 0 } {
	    readdir $w "[$list get [$list curselection]]/" "" \
		    $fileselect(pattern)
	}
    }

    proc cmd_filesel_right_b1 {w list} {
	global fileselect

	if {[$list size] > 0} {
	    set fileselect(file) [$list get [$list curselection]]
	    set fileselect(selection) "$fileselect(path)$fileselect(file)"
	}
    }

    proc cmd_filesel_right_dblb1 {w list} {
	if {[$list size] > 0} {
	    $w.buttons.ok flash
	    $w.buttons.ok invoke
	}
    }


    # GUI
    # ---

    catch {destroy $w}
    toplevel $w
    wm protocol $w WM_DELETE_WINDOW { }
    wm transient $w [winfo toplevel [winfo parent $w]]
    wm title $w $title

    frame $w.title -relief raised -borderwidth 1
    frame $w.top -relief raised -borderwidth 1
    frame $w.bottom -relief raised -borderwidth 1
    pack $w.title $w.top $w.bottom -fill both -expand 1

    label $w.title.label -text $title
    pack $w.title.label -in $w.title -expand 1 -pady 5

    frame $w.filter
    label $w.filter.label -text "Filter" -anchor w
    checkbutton $w.filter.showdot -text "Show Dot Files" \
	    -command "cmd_showdot $w" -variable fileselect(showdot)
    entry $w.filter.entry
    pack $w.filter.entry -side bottom -fill x
    pack $w.filter.label -side left
    pack $w.filter.showdot -side right

    frame $w.selection
    label $w.selection.label -text "Selection" -anchor w
    entry $w.selection.entry -textvariable fileselect(selection)
    pack $w.selection.label $w.selection.entry -fill x

    frame $w.lists

    frame $w.lists.frame1
    label $w.lists.frame1.label -text "directories" -anchor w
    scrollbar $w.lists.frame1.yscroll -relief sunken \
	    -command "$w.lists.frame1.list yview"
    scrollbar $w.lists.frame1.xscroll -relief sunken -orient horizontal \
	    -command "$w.lists.frame1.list xview"
    listbox $w.lists.frame1.list -width 30 -height 10 \
	    -yscroll "$w.lists.frame1.yscroll set" \
	    -xscroll "$w.lists.frame1.xscroll set" \
	    -relief sunken -setgrid 1 -selectmode single
    pack $w.lists.frame1.label -side top -fill x
    pack $w.lists.frame1.yscroll -side right -fill y
    pack $w.lists.frame1.xscroll -side bottom -fill x
    pack $w.lists.frame1.list -expand yes -fill y

    frame $w.lists.frame2
    label $w.lists.frame2.label -text "Files" -anchor w
    scrollbar $w.lists.frame2.yscroll -relief sunken \
	    -command "$w.lists.frame2.list yview"
    scrollbar $w.lists.frame2.xscroll -relief sunken -orient horizontal \
	    -command "$w.lists.frame2.list xview"
    listbox $w.lists.frame2.list -width 30 -height 10 \
	    -yscroll "$w.lists.frame2.yscroll set" \
	    -xscroll "$w.lists.frame2.xscroll set" \
	    -relief sunken -setgrid 1 -selectmode single
    pack $w.lists.frame2.label -side top -fill x
    pack $w.lists.frame2.yscroll -side right -fill y
    pack $w.lists.frame2.xscroll -side bottom -fill x
    pack $w.lists.frame2.list -expand yes -fill y

    frame $w.lists.fill
    pack $w.lists.frame1 -side left
    pack $w.lists.frame2 -side right
    pack $w.lists.fill -padx 10

    frame $w.buttons
    button $w.buttons.ok -text "Ok" -width 10
    button $w.buttons.filter -text "Filter" -width 10
    button $w.buttons.cancel -text "Cancel" -width 10
    pack $w.buttons.ok $w.buttons.filter $w.buttons.cancel -side left -expand 1

    pack $w.filter $w.lists $w.selection -side top -padx 20 -fill x -pady 5 \
	    -in $w.top
    pack $w.buttons -expand 1 -fill both -pady 10 -in $w.bottom


    # event bindings
    # --------------

    $w.buttons.ok config -command "set fileselect(button) 0"
    $w.buttons.cancel config -command "set fileselect(button) 2"
    $w.buttons.filter config -command "cmd_filesel_filter $w"

    bind $w.lists.frame1.list <Double-Button-1> "cmd_filesel_left_dblb1 $w %W"
    bind $w.lists.frame1.list <Double-space> "cmd_filesel_left_dblb1 $w %W"
    bind $w.lists.frame2.list <Button-1> "cmd_filesel_right_b1 $w %W"
    bind $w.lists.frame2.list <space> "cmd_filesel_right_b1 $w %W"
    bind $w.lists.frame2.list <Double-Button-1> "cmd_filesel_right_dblb1 $w %W"
    bind $w.lists.frame2.list <Double-space> "cmd_filesel_right_dblb1 $w %W"

    bind $w.filter.entry <Return> \
	    "$w.buttons.filter flash; $w.buttons.filter invoke"
    bind $w.selection.entry <Return> \
	    "$w.buttons.ok flash; $w.buttons.ok invoke"
    bind $w <Escape> "$w.buttons.cancel flash; $w.buttons.cancel invoke"

    settabstops $w [list $w.filter.showdot $w.filter.entry \
	    $w.lists.frame1.list $w.lists.frame2.list $w.selection.entry \
	    $w.buttons.ok $w.buttons.filter $w.buttons.cancel]


    # initialization
    # --------------

    set fileselect(path) $dir
    set fileselect(file) ""
    set fileselect(pattern) $pattern
    set fileselect(showdot) $showdot
    set fileselect(button) 0
    set fileselect(selection) ""

    readdir $w $dir $file $pattern
    wm withdraw $w
    update idletasks
    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
            - [winfo vrootx [winfo parent $w]]]
    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
            - [winfo vrooty [winfo parent $w]]]
    wm geom $w +$x+$y
    wm deiconify $w
    set old_focus [focus]
    grab $w
    focus $w.selection.entry
    tkwait variable fileselect(button)
    grab release $w
    catch {focus $old_focus}
    destroy $w
    if {$fileselect(button) == 0} {
	set dir $fileselect(path)
	set file $fileselect(file)
	set pattern $fileselect(pattern)
	set showdot $fileselect(showdot)
	return $fileselect(selection)
    } else {
	return {}
    }
}