summaryrefslogtreecommitdiffstats
path: root/ds9/library/movie.tcl
blob: b22962d8621f3f8cb81aded5e4f7d06f17fb7bda (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
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
#  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 MovieDef {} {
    global imovie
    global movie

    global tcl_platform

    set imovie(top) .moviestatus
    set imovie(mb) .moviestatusmb

    set movie(type) gif
    set movie(action) slice
    set movie(num) 24
    set movie(az,from) 45
    set movie(az,to) -45
    set movie(el,from) 30
    set movie(el,to) 30
    set movie(sl,from) 1
    set movie(sl,to) 1
    set movie(zm,from) 1
    set movie(zm,to) 1
    set movie(repeat) oscillate
    set movie(repeat,num) 0

    set movie(status) 0
    set movie(abort) 0
    set movie(first) 0

    set movie(error) [msgcat::mc {An error has occurred while creating the image. Please be sure that the ds9 window is in the upper left corner of the default screen and the entire window is visible.}]
}

proc MovieDialog {} {
    global movie
    global mpegfbox
    global giffbox
    global ed
    global current

    set w {.movie}

    set ed(ok) 0
    set ed(action) $movie(action)
    set ed(type) $movie(type)

    DialogCreate $w [msgcat::mc {Create Movie}] ed(ok)

    # Type of Movie
    set f [ttk::labelframe $w.type -text [msgcat::mc {Type}]]

    ttk::label $f.title -text [msgcat::mc {Type}]
    ttk::radiobutton $f.mpeg -text {MPEG} \
	-variable ed(type) -value mpeg
    ttk::radiobutton $f.gif -text {Animated Gif} \
	-variable ed(type) -value gif

    grid $f.mpeg -padx 2 -pady 2 -sticky w
    grid $f.gif -padx 2 -pady 2 -sticky w

    # Param
    set f [ttk::labelframe $w.param -text [msgcat::mc {Format}]]

    ttk::label $f.title -text [msgcat::mc {Format}]
    ttk::radiobutton $f.frame -text {Frames Movie} \
	-variable ed(action) -value frame
    ttk::radiobutton $f.slice -text {Slice Movie} \
	-variable ed(action) -value slice
    ttk::radiobutton $f.3d -text {3D Movie} \
	-variable ed(action) -value 3d

    grid $f.slice -padx 2 -pady 2 -sticky w
    grid $f.frame -padx 2 -pady 2 -sticky w
    grid $f.3d -padx 2 -pady 2 -sticky w

    switch [$current(frame) get type] {
	base -
	rgb {$f.3d configure -state disabled}
	3d {$f.3d configure -state normal}
    }

    # 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
    grid $w.type -sticky news
    grid $w.param -sticky news
    grid $w.buttons -sticky ew
    grid rowconfigure $w 0 -weight 1
    grid rowconfigure $w 1 -weight 1
    grid columnconfigure $w 0 -weight 1

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

    if {$ed(ok)} {
	set movie(action) $ed(action)
	set movie(type) $ed(type)

	switch $movie(type) {
	    mpeg {set fn [SaveFileDialog mpegfbox]}
	    gif {set fn [SaveFileDialog giffbox]}
	}

	if {$fn != {}} {
	    set ok 1
	    switch $movie(action) {
		slice -
		frame {}
		3d {set ok [Movie3dDialog]}
	    }

	    if {$ok} {
		MovieCreate $fn
	    }
	}
    }

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

proc MovieCreate {fn} {
    global ds9
    global movie
    global current
    global saveimage

    if {$fn == {}} {
	return
    }
    set movie(fn) $fn

    # besure we are on top
    raise $ds9(top)

    # for darwin only
    set geom [MacOSPhotoFix $ds9(top) 0 1]

    switch $movie(action) {
	frame {MovieFrame}
	slice {MovieSlice}
	3d {Movie3d}
    }

    # close
    switch $movie(type) {
	mpeg {mpeg close}
	gif {agif close}
    }

    # reset if needed
    DarwinPhotoRestore $ds9(top) $geom
}

proc MovieFrame {} {
    global ds9
    global current
    global movie

    # we need single mode
    if {$ds9(display) != {single}} {
	set modesav $ds9(display)
	set current(display) single
	DisplayMode
    }

    # loop thru all active frames
    set movie(first) 1
    set framesav $current(frame)

    foreach ff $ds9(active) {
	set ds9(next) $ff
	GotoFrame
	if {[MoviePhoto]} {
	    break
	}
    }

    set ds9(next) $framesav
    GotoFrame

    if {[info exists modesav]} {
	set current(display) $modesav
	DisplayMode
    }
}

proc MovieSlice {} {
    global ds9
    global current
    global movie

    # we need single mode
    if {$ds9(display) != {single}} {
	set modesav $ds9(display)
	set current(display) single
	DisplayMode
    }

    set depth [$current(frame) get fits depth]
    set slice [$current(frame) get fits slice]

    set ss [$current(frame) get crop 3d image]
    set from [lindex $ss 0]
    set to [lindex $ss 1]

    # loop thru cube
    set movie(first) 1
    for {set ii $from} {$ii <= $to} {incr ii} {
	$current(frame) update fits slice $ii
	if {[MoviePhoto]} {
	    break
	}
    }

    # reset current slice
    $current(frame) update fits slice $slice

    if {[info exists modesav]} {
	set current(display) $modesav
	DisplayMode
    }
}

proc Movie3d {} {
    global ds9
    global movie
    global current

    # we need single or tile mode
    if {$ds9(display) == {blink}} {
	set modesav $ds9(display)
	set current(display) single
	DisplayMode
    }

    switch $ds9(display) {
	single {
	    set zoom [$current(frame) get zoom]
	    set slice [$current(frame) get fits slice]
	    set vp [$current(frame) get 3d view]
	}
	tile {
	    $current(frame) highlite off
	    foreach ff $ds9(active) {
		set zoom($ff) [$ff get zoom]
		set slice($ff) [$ff get fits slice]
		set vp($ff) [$ff get 3d view]
	    }
	}
	blink {
	    # should not be here
	}
    }

    set azincr [expr 1.*($movie(az,to)-$movie(az,from))/$movie(num)]
    set elincr [expr 1.*($movie(el,to)-$movie(el,from))/$movie(num)]
    set slincr [expr 1.*($movie(sl,to)-$movie(sl,from))/$movie(num)]
    set zmincr [expr 1.*($movie(zm,to)-$movie(zm,from))/$movie(num)]

    # loop over az/el/slice
    set movie(status) 0
    set movie(abort) 0
    set movie(first) 1

    set az $movie(az,from)
    set el $movie(el,from)
    set sl $movie(sl,from)
    set zm $movie(zm,from)

    for {set rr 0} {$rr<=$movie(repeat,num)} {incr rr} {
	for {set nn 0} {$nn<=$movie(num)} {incr nn} {
	    MovieStatusDialog

	    if {$movie(abort)} {
		break
	    }
	    set movie(status) [expr 1.*$nn/$movie(num)*100]
	    update idletasks

	    switch $ds9(display) {
		single {
		    $current(frame) zoom to $zm $zm
		    $current(frame) 3d view $az $el
		    $current(frame) update fits slice [expr int($sl)]
		}
		tile {
		    foreach ff $ds9(active) {
			$ff zoom to $zm $zm
			$ff 3d view $az $el
			$ff update fits slice [expr int($sl)]
		    }
		}
		blink {
		    # should not be here
		}
	    }

	    if {[MoviePhoto]} {
		break
	    }

	    set az [expr $az+$azincr]
	    set el [expr $el+$elincr]
	    set sl [expr $sl+$slincr]
	    set zm [expr $zm+$zmincr]
	}
	switch $movie(repeat) {
	    repeat {
		set az $movie(az,from)
		set el $movie(el,from)
		set sl $movie(sl,from)
		set zm $movie(zm,from)
	    }
	    oscillate {
		set azincr [expr -$azincr]
		set elincr [expr -$elincr]
		set slincr [expr -$slincr]
		set zmincr [expr -$zmincr]
	    }
	}
    }

    MovieStatusDestroyDialog

    # reset
    switch $ds9(display) {
	single {
	    $current(frame) zoom to $zoom
	    $current(frame) 3d view $vp
	    $current(frame) update fits slice $slice
	}
	tile {
	    $current(frame) highlite on
	    foreach ff $ds9(active) {
		$ff zoom to $zoom($ff)
		$ff 3d view $vp($ff)
		$ff update fits slice $slice($ff)
	    }
	}
	blink {
	    # should not be here
	}
    }

    Update3DDialog
    UpdateCubeDialog

    if {[info exists modesav]} {
	set current(display) $modesav
	DisplayMode
    }
}

# Support

proc MoviePhoto {} {
    global movie

    switch $movie(type) {
	mpeg {return [MoviePhotoMPEG]}
	gif {return [MoviePhotoGIF]}
    }
    return 1
}

proc MoviePhotoMPEG {} {
    global ds9
    global movie
    global current

    # yes, we need this
    UpdateDS9
    RealizeDS9 1

    set rr [catch {image create photo -format window -data $ds9(canvas)} ph]
    if {$rr} {
	Error $movie(error)
	return $rr
    }

    if {$movie(first)} {
	set ww [image width $ph]
	set hh [image height $ph]
	# quality must be >=5, or sometimes will generate bad data
	mpeg create "$movie(fn)" $ww $hh 25 1 5
	set movie(first) 0
    }
    mpeg add $ph
    image delete $ph

    return 0
}

proc MoviePhotoGIF {} {
    global ds9
    global movie
    global current
    global colorbar

    # yes, we need this
    UpdateDS9
    RealizeDS9 1

    set rr [catch {image create photo -format window -data $ds9(canvas)} ph]
    if {$rr} {
	Error $movie(error)
	return $rr
    }

    if {$movie(first)} {
	agif create $movie(fn) [image width $ph] [image height $ph]
	set movie(first) 0
    }
    agif add $ph
    image delete $ph

    return 0
}

proc Movie3dDialog {} {
    global movie
    global ed2
    global current

    set w {.movie3d}

    set ed2(ok) 0
    set ed2(num) $movie(num)
    set ed2(az,from) $movie(az,from)
    set ed2(az,to) $movie(az,to)
    set ed2(el,from) $movie(el,from)
    set ed2(el,to) $movie(el,to)
    set ed2(sl,from) [$current(frame) get fits slice]
    set ed2(sl,to) $ed2(sl,from)
    set ed2(zm,from) [lindex [$current(frame) get zoom] 0]
    set ed2(zm,to) $ed2(zm,from)
    set ed2(repeat) $movie(repeat)
    set ed2(repeat,num) $movie(repeat,num)

    DialogCreate $w [msgcat::mc {Save 3D Movie}] ed2(ok)

    # Param
    set f [ttk::frame $w.param]
    ttk::label $f.tnum -text [msgcat::mc {Number}]
    ttk::entry $f.num -textvariable ed2(num) -width 7
    ttk::label $f.tframes -text [msgcat::mc {Frames}]

    ttk::label $f.taz -text [msgcat::mc {Azimuth}]
    ttk::label $f.tazfrom -text [msgcat::mc {From}]
    ttk::entry $f.azfrom -textvariable ed2(az,from) -width 7
    ttk::label $f.tazto -text [msgcat::mc {To}]
    ttk::entry $f.azto -textvariable ed2(az,to) -width 7

    ttk::label $f.tel -text [msgcat::mc {Elevation}]
    ttk::label $f.telfrom -text [msgcat::mc {From}]
    ttk::entry $f.elfrom -textvariable ed2(el,from) -width 7
    ttk::label $f.telto -text [msgcat::mc {To}]
    ttk::entry $f.elto -textvariable ed2(el,to) -width 7

    ttk::label $f.tsl -text [msgcat::mc {Slice}]
    ttk::label $f.tslfrom -text [msgcat::mc {From}]
    ttk::entry $f.slfrom -textvariable ed2(sl,from) -width 7
    ttk::label $f.tslto -text [msgcat::mc {To}]
    ttk::entry $f.slto -textvariable ed2(sl,to) -width 7

    ttk::label $f.tzm -text [msgcat::mc {Zoom}]
    ttk::label $f.tzmfrom -text [msgcat::mc {From}]
    ttk::entry $f.zmfrom -textvariable ed2(zm,from) -width 7
    ttk::label $f.tzmto -text [msgcat::mc {To}]
    ttk::entry $f.zmto -textvariable ed2(zm,to) -width 7

    ttk::radiobutton $f.repeat -text [msgcat::mc {Repeat}] \
	-variable ed2(repeat) -value repeat
    ttk::radiobutton $f.oscillate -text [msgcat::mc {Oscillate}] \
	-variable ed2(repeat) -value oscillate
    ttk::entry $f.repeatnum -textvariable ed2(repeat,num) -width 7
    ttk::label $f.ttimes -text [msgcat::mc {Times}]

    grid $f.tnum x $f.num $f.tframes -padx 2 -pady 2 -sticky w
    grid $f.taz $f.tazfrom $f.azfrom $f.tazto $f.azto -padx 2 -pady 2 -sticky w
    grid $f.tel $f.telfrom $f.elfrom $f.telto $f.elto -padx 2 -pady 2 -sticky w
    grid $f.tsl $f.tslfrom $f.slfrom $f.tslto $f.slto -padx 2 -pady 2 -sticky w
    grid $f.tzm $f.tzmfrom $f.zmfrom $f.tzmto $f.zmto -padx 2 -pady 2 -sticky w
    grid $f.oscillate x $f.repeatnum $f.ttimes -padx 2 -pady 2 -sticky w
    grid $f.repeat -padx 2 -pady 2 -sticky w

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

    bind $w <Return> {set ed2(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 ed2(ok)
    DialogDismiss $w

    if {$ed2(ok)} {
	set movie(num) $ed2(num)
	set movie(az,from) $ed2(az,from)
	set movie(az,to) $ed2(az,to)
	set movie(el,from) $ed2(el,from)
	set movie(el,to) $ed2(el,to)
	set movie(sl,from) $ed2(sl,from)
	set movie(sl,to) $ed2(sl,to)
	set movie(zm,from) $ed2(zm,from)
	set movie(zm,to) $ed2(zm,to)
	set movie(repeat) $ed2(repeat)
	set movie(repeat,num) $ed2(repeat,num)
    }

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

proc MovieStatusDialog {} {
    global imovie
    global movie

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

    # create the 3d window
    set w $imovie(top)
    set mb $imovie(mb)

    Toplevel $w $mb 6 [msgcat::mc {Movie}] MovieStatusDestroyDialog
    raise $imovie(top)

    # Status
    set f [ttk::frame $w.param]
    ttk::label $f.tstatus -text [msgcat::mc {Status}]
    ttk::progressbar $f.status -variable movie(status) -length 350
    grid $f.tstatus $f.status -padx 2 -pady 2 -sticky w

    # Buttons
    set f [ttk::frame $w.buttons]
    ttk::button $f.abort -text [msgcat::mc {Abort}] \
	-command MovieStatusAbortDialog
    pack $f.abort -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 x
}

proc MovieStatusDestroyDialog {} {
    global movie
    global imovie

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

proc MovieStatusAbortDialog {} {
    global movie
    set movie(abort) 1
}

# Process Cmds

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

    # we need to be realized
    # already implemented
    # ProcessRealizeDS9

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