summaryrefslogtreecommitdiffstats
path: root/ds9/library/mask.tcl
blob: 6d302155c2545ae2d4010ac942cde23ceb187589 (plain)
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
#  Copyright (C) 1999-2018
#  Smithsonian Astrophysical Observatory, Cambridge, MA, USA
#  For conditions of distribution and use, see copyright notice in "copyright"

package provide DS9 1.0

proc MaskDef {} {
    global mask
    global imask
    global pmask

    set imask(top) .msk
    set imask(mb) .mskmb

    set mask(transparency) 0
    set mask(system) physical

    set mask(color) red
    set mask(mark) nonzero
    set mask(low) 0
    set mask(high) 0

    array set pmask [array get mask]
}

proc MaskMark {} {
    global mask
    global current

    if {$current(frame) != {}} {
	$current(frame) mask mark $mask(mark)
	# for backward compatibility
	set mask(mark) [$current(frame) get mask mark]
    }
}

proc MaskRange {} {
    global mask
    global current

    if {$current(frame) != {}} {
	$current(frame) mask range $mask(low) $mask(high)
    }
}

proc MaskColor {} {
    global mask
    global current

    if {$current(frame) != {}} {
	$current(frame) mask color $mask(color)
    }
}

proc MaskTransparency {} {
    global mask
    global current

    if {$current(frame) != {}} {
	$current(frame) mask transparency $mask(transparency)
    }
}

proc MaskSystem {} {
    global mask
    global current

    if {$current(frame) != {}} {
	$current(frame) mask system $mask(system)
    }
}

proc MaskClear {} {
    global current

    if {$current(frame) != {}} {
	$current(frame) mask clear
    }
}

# used in Backup
proc MaskDialog {} {
    global mask
    global imask
    global current
    global ds9

    # see if we already have a ctr window visible
    if {[winfo exists $imask(top)]} {
	raise $imask(top)
	return
    }

    # create the mask window
    set w $imask(top)
    set mb $imask(mb)

    Toplevel $w $mb 6 [msgcat::mc {Mask Parameters}] MaskDestroyDialog

    $mb add cascade -label [msgcat::mc {File}] -menu $mb.file
    $mb add cascade -label [msgcat::mc {Edit}] -menu $mb.edit
    $mb add cascade -label [msgcat::mc {Align}] -menu $mb.align

    menu $mb.file
    $mb.file add command -label "[msgcat::mc {Load}]..." \
	-command [list OpenDialog fits mask]
    $mb.file add cascade -label [msgcat::mc {Load as}] \
	-menu $mb.file.open
    $mb.file add separator
    $mb.file add cascade -label [msgcat::mc {Import}] \
	-menu $mb.file.import
    $mb.file add separator
    $mb.file add command -label [msgcat::mc {Apply}] -command MaskApplyDialog
    $mb.file add command -label [msgcat::mc {Clear}] -command MaskClear
    $mb.file add separator
    $mb.file add command -label [msgcat::mc {Close}] -command MaskDestroyDialog

    menu $mb.file.open
    $mb.file.open add command -label "[msgcat::mc {Mosaic WCS}]..." \
	-command [list OpenDialog mosaicimagewcs mask]
    $mb.file.open add command -label "[msgcat::mc {Mosaic WCS Segment}]..." \
	-command [list OpenDialog mosaicwcs mask]
    $mb.file.open add command -label "[msgcat::mc {Mosaic IRAF}]..." \
	-command [list OpenDialog mosaicimageiraf mask]
    $mb.file.open add command -label "[msgcat::mc {Mosaic IRAF Segment}]..." \
	-command [list OpenDialog mosaiciraf mask]
    $mb.file.open add command -label "[msgcat::mc {Mosaic WFPC2}]..." \
	-command [list OpenDialog mosaicimagewfpc2 mask]

    menu $mb.file.import
    $mb.file.import add command -label "[msgcat::mc {Array}]..." \
	-command [list ImportDialog array mask]
    $mb.file.import add command -label {NRRD} \
	-command [list ImportDialog nrrd mask]

    EditMenu $mb imask

    CoordMenu $mb.align mask system 1 {} {} MaskSystem

    # Param
    set f [ttk::frame $w.param]

    slider $f.slider 0 100 [msgcat::mc {Transparency}] \
	mask(transparency) [list MaskTransparency]

    grid $f.slider -padx 2 -pady 2 -sticky ew
    grid columnconfigure $f 0 -weight 1

    # Buttons
    set f [ttk::frame $w.buttons]
    ttk::button $f.apply -text [msgcat::mc {Apply}] -command MaskApplyDialog
    ttk::button $f.clear -text [msgcat::mc {Clear}] -command MaskClear
    ttk::button $f.close -text [msgcat::mc {Close}] -command MaskDestroyDialog
    pack $f.apply $f.clear $f.close -side left -expand true -padx 2 -pady 4

    # Fini
    ttk::separator $w.sep -orient horizontal
    pack $w.buttons $w.sep -side bottom -fill x
    pack $w.param -side top -fill both -expand true
}

proc MaskApplyDialog {} {
    global mask

    MaskTransparency
}

proc MaskDestroyDialog {} {
    global imask

    if {[winfo exists $imask(top)]} {
	destroy $imask(top)
	destroy $imask(mb)
    }
}

proc UpdateMaskMenu {} {
    global mask
    global current
    global ds9

    global debug
    if {$debug(tcl,update)} {
	puts stderr "UpdateMaskMenu"
    }

    if {$current(frame) == {}} {
	return
    }

    set mask(transparency) [$current(frame) get mask transparency]
    set mask(system) [$current(frame) get mask system]

    set mask(color) [$current(frame) get mask color]
    set mask(mark) [$current(frame) get mask mark]
    set range [$current(frame) get mask range]
    set mask(low) [lindex $range 0]
    set mask(high) [lindex $range 1]

    switch -- [$current(frame) get type] {
	base {
	    $ds9(mb).analysis entryconfig \
		"[msgcat::mc {Mask Parameters}]..." -state normal
	}
	3d -
	rgb {
	    $ds9(mb).analysis entryconfig \
		"[msgcat::mc {Mask Parameters}]..." -state disabled
	}
    }
}

proc MaskLoad {} {
    global current
    global mask

    set rr [MaskParamsDialog]
    if {$current(frame) != {}} {
	if {$rr} {
	    $current(frame) mask color $mask(color)
	    $current(frame) mask mark $mask(mark)
	    $current(frame) mask range $mask(low) $mask(high)
	}
    }
    return $rr
}

proc MaskParamsDialog {} {
    global mask
    global ed

    set w {.mskd}

    set ed(ok) 0
    set ed(color) $mask(color)
    set ed(mark) $mask(mark)
    set ed(low) $mask(low)
    set ed(high) $mask(high)

    DialogCreate $w [msgcat::mc {Mask Parameters}] ed(ok)

    # Param
    set f [ttk::frame $w.param]

    ttk::label $f.colortitle -text [msgcat::mc {Color}]
    ColorMenuButton $f.colorbutton ed color {}

    ttk::label $f.marktitle -text [msgcat::mc {Block Pixel}]
    set mb $f.markbutton.menu
    ttk::menubutton $f.markbutton -textvariable ed(mark) -menu $mb
    menu $mb
    $mb add radiobutton -label [msgcat::mc {Zero}] \
	-variable ed(mark) -value zero
    $mb add radiobutton -label [msgcat::mc {Non-Zero}] \
	-variable ed(mark) -value nonzero
    $mb add radiobutton -label [msgcat::mc {NAN}] \
	-variable ed(mark) -value nan
    $mb add radiobutton -label [msgcat::mc {Non-NAN}] \
	-variable ed(mark) -value nonnan
    $mb add radiobutton -label [msgcat::mc {Range}] \
	-variable ed(mark) -value range

    ttk::label $f.rangetitle -text [msgcat::mc {Range}]
    ttk::entry $f.low -textvariable ed(low) -width 13
    ttk::entry $f.high -textvariable ed(high) -width 13

    grid $f.colortitle $f.colorbutton -padx 2 -pady 2 -sticky w
    grid $f.marktitle $f.markbutton -padx 2 -pady 2 -sticky w
    grid $f.rangetitle $f.low $f.high -padx 2 -pady 2 -sticky w

    # Buttons
    set f [ttk::frame $w.buttons]
    ttk::button $f.ok -text [msgcat::mc {OK}] -command {set ed(ok) 1} \
	-default active 
    ttk::button $f.cancel -text [msgcat::mc {Cancel}] -command {set ed(ok) 0}
    pack $f.ok $f.cancel -side left -expand true -padx 2 -pady 4

    bind $w <Return> {set ed(ok) 1}

    # Fini
    ttk::separator $w.sep -orient horizontal
    pack $w.buttons $w.sep -side bottom -fill x
    pack $w.param -side top -fill both -expand true

    DialogCenter $w 
    DialogWait $w ed(ok)
    DialogDismiss $w

    if {$ed(ok)} {
	set mask(color) [string tolower $ed(color)]
	set mask(mark) $ed(mark)
	set mask(low) $ed(low)
	set mask(high) $ed(high)
    }

    set rr $ed(ok)
    unset ed
    return $rr
}

proc MaskBackup {ch which} {
    puts $ch "$which mask transparency [$which get mask transparency]"
    puts $ch "$which mask system [$which get mask system]"

    puts $ch "$which mask color [$which get mask color]"
    puts $ch "$which mask mark [$which get mask mark]"
    puts $ch "$which mask range [$which get mask range]"
}

proc ProcessMaskCmd {varname iname} {
    upvar $varname var
    upvar $iname i

    global mask
    global parse
    set parse(result) {}

    MaskDialog

    mask::YY_FLUSH_BUFFER
    mask::yy_scan_string [lrange $var $i end]
    mask::yyparse
    incr i [expr $mask::yycnt-1]

    return $parse(result)
}

proc ProcessSendMaskCmd {proc id param {sock {}} {fn {}}} {
    global parse
    set parse(proc) $proc
    set parse(id) $id

    masksend::YY_FLUSH_BUFFER
    masksend::yy_scan_string $param
    masksend::yyparse
}