4 # @brief Procedures to manage file I/O
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20191112
13 ## @brief Menu for opening and closing image files and quiting
16 proc setupFileMenu {} {
19 .menuBar add cascade -menu .menuBar.file -label "File" -underline 0
20 menu .menuBar.file -tearoff 0
21 .menuBar.file add command -label "Open ..." -command { fileDialog "open" } \
22 -underline 0 -accelerator "Ctrl-o"
23 .menuBar.file add command -label "Save" -command { fileDialog "save" } \
25 .menuBar.file add command -label "Save as ..." -command { fileDialog "saveAs" } \
26 -underline 0 -accelerator "Ctrl-S"
27 .menuBar.file add command -label "Save region ..." -command { extractImage } \
29 .menuBar.file add command -label "File browser" -command { fileBrowserWindow .fb } \
31 .menuBar.file add command -label "Revert" -command { loadImage $filename -1 } \
32 -underline 0 -accelerator "Ctrl-u"
33 .menuBar.file add command -label "Get info" -command { getInfo $filename } \
34 -underline 0 -accelerator "Ctrl-i"
35 .menuBar.file add command -label "Write PostScript" -command { writePostScript } \
37 .menuBar.file add command -label "Quit" -command { Quit } -underline 0 \
41 # Type names Extension(s) Mac File Type(s)
43 #---------------------------------------------------------
47 {"MRC" {.mrc .MRC .stk .STK .st .ST .ali .ALI .rec .REC} }
50 {"IMAGIC" {.img .IMG .hed .HED} }
51 {"SPIDER" {.spi .SPI} }
52 {"SUPRIM" {.sup .SUP .spm .SPM} }
56 {"TIFF" {.tif .tiff .TIF .TIFF} }
58 {"PNM" {.pbm .pgm .ppm .PBM .PGM .PPM} }
59 {"JPEG" {.jpg .jpeg .JPG .JPEG} }
60 {"ASCII" {.txt .asc .ascii .TXT .ASC .ASCII} }
63 ## @brief Selects a file for reading or writing using a standard dialog
65 # @param operation "open", "save" or "saveAs".
67 proc fileDialog { operation } {
69 global filename imgdir
73 if { [string length $currdir] < 2 } { set currdir [pwd] }
74 # puts "Image directory: $imgdir"
75 if { [string compare $operation "open"] == 0 } {
76 set new_filename [tk_getOpenFile -title "Open image file" \
77 -filetypes $filetypes -initialdir $currdir]
78 if { [string length $new_filename] < 1 } { return }
79 set filename [relativePath [pwd] $new_filename]
80 set imgdir [file dirname $filename]
81 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
82 # "imgdir = $imgdir\nfilename = $filename"
83 loadImage $filename -1
85 if { [string compare $operation "saveAs"] == 0 } {
86 set new_filename [tk_getSaveFile -filetypes $filetypes \
87 -initialfile [file tail $filename] -initialdir $currdir \
88 -defaultextension .mrc]
89 if { [string length $new_filename] < 1 } { return }
90 set filename [relativePath [pwd] $new_filename]
92 Bimage save $theimg $filename
95 # puts "Image directory: $imgdir"
96 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
98 if [Bmg exists] { setMgParam }
99 wm title . [list $filename ":" $paramfile]
103 ## @brief Populates a file browser tree node
105 # @param tree File browser tree.
106 # @param node File browser node.
108 proc openNode {tree node} {
111 if {[$tree set $node type] ne "directory"} {
115 set path [$tree set $node fullpath]
116 $tree delete [$tree children $node]
118 foreach f [lsort -dictionary [glob -nocomplain -dir $path *]] {
119 # set r [string match "./$img_filter" $f]
121 if [string match "./$img_filter" $f] {
122 set type [file type $f]
123 set id [$tree insert $node end -text [file tail $f] \
124 -values [list $f $type]]
126 if {$type eq "directory"} {
127 ## Make it so that this node is openable
128 $tree insert $id 0 -text dummy ;# a dummy
129 $tree item $id -text [file tail $f]/
131 } elseif {$type eq "file"} {
132 set size [file size $f]
133 ## Format the file size nicely
134 if {$size >= 1024*1024*1024} {
135 set size [format %.1f\ GB [expr {$size/1024/1024/1024.}]]
136 } elseif {$size >= 1024*1024} {
137 set size [format %.1f\ MB [expr {$size/1024/1024.}]]
138 } elseif {$size >= 1024} {
139 set size [format %.1f\ kB [expr {$size/1024.}]]
143 $tree set $id size $size
148 # Stop this code from rerunning on the current node
149 $tree set $node type processedDirectory
152 ## @brief Points the file browser to the current directory
154 # @param w File browser tree.
156 proc setupFileBrowser { w } {
159 $w.tree set {} fullpath "./"
160 $w.tree set {} type "directory"
162 set img_filter [$w.filter.e get]
163 if { $img_filter eq "" } {
165 $w.filter.e insert 0 $img_filter
171 ## @brief Opens a file indicated by a double-click in the file browser
173 # @param wtree File browser tree.
175 proc openFileFromTree { wtree } {
176 global filename imgdir
177 set type [$wtree set [$wtree focus] type]
178 if { $type eq "file" || $type eq "link" } {
179 set fn [$wtree set [$wtree focus] fullpath]
180 set tbt [exec file $fn]
181 if [string match "*text*" $tbt] {
184 set ft [fileType $fn]
186 set filename [relativePath [pwd] $fn]
187 set imgdir [file dirname $filename]
190 tk_messageBox -icon info -type ok \
191 -title "Error" -message "File type not supported!"
199 ## @brief Sets up the file browser window
201 # @param w Window name.
203 proc fileBrowserWindow { w } {
208 wm title $w "File browser"
209 wm iconname $w "Browser"
210 wm geometry $w 500x800+500+50
213 ttk::frame $w.buttons
214 ttk::button $w.buttons.close -text "Close" \
215 -command [list destroy [winfo toplevel $w]]
216 ttk::button $w.buttons.code -text "Refresh" \
217 -command [list setupFileBrowser $w]
218 pack $w.buttons.code $w.buttons.close -side left
219 pack $w.buttons -side bottom -padx 2 -pady 2
222 labelframe $w.filter -text "File filter" -labelanchor nw
223 ttk::entry $w.filter.e
224 $w.filter.e insert 0 $img_filter
225 pack $w.filter.e -side left -expand yes -fill x
226 pack $w.filter -side bottom -fill x -padx 2 -pady 2
228 ## Create the tree and set it up
229 ttk::treeview $w.tree -columns {fullpath type size} -displaycolumns {size} \
230 -yscroll "$w.vsb set" -xscroll "$w.hsb set"
231 if {[tk windowingsystem] ne "aqua"} {
232 ttk::scrollbar $w.vsb -orient vertical -command "$w.tree yview"
233 ttk::scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
235 scrollbar $w.vsb -orient vertical -command "$w.tree yview"
236 scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
238 $w.tree heading \#0 -text "File"
239 $w.tree heading size -text "Size"
240 $w.tree column size -stretch 0 -width 70
244 ## Arrange the tree and its scrollbars in the toplevel
245 lower [ttk::frame $w.gframe]
246 pack $w.gframe -fill both -expand 1
247 grid $w.tree $w.vsb -sticky nsew -in $w.gframe
248 grid $w.hsb -sticky nsew -in $w.gframe
249 grid columnconfigure $w.gframe 0 -weight 1
250 grid rowconfigure $w.gframe 0 -weight 1
252 bind $w.tree <<TreeviewOpen>> {openNode %W [%W focus]}
253 bind $w.tree <Double-Button-1> {openFileFromTree %W }
254 bind $w <Return> [list setupFileBrowser $w]
257 ## @brief Loads the selected file into a photo image for display.
259 # @param filename Image file name.
260 # @param img_num Sub-image number.
262 proc loadImage { filename img_num } {
265 global paramfile modelfile
267 global window_width window_height window_scale rem_scale
268 global img_load_phase
269 global avg_check kernel
270 if { [string length $filename] < 1 } { return }
271 if { ![winfo exists .] } { return }
272 set imgdir [file dirname $filename]
273 set filename [file tail $filename]
274 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
275 # "$imgdir $filename"
276 if { [Bmg exists] } {
277 wm title . [list $filename ":" $paramfile]
278 } elseif { [Bmodel exists] } {
279 wm title . [list $filename ":" $modelfile]
283 # puts "Opening image $imgdir/$filename ($img_num)"
285 if { [catch { set nimages [Bimage open $theimg $imgdir/$filename $img_num] } err] } {
290 # puts "Image opened with $nimages sub-images"
291 if { $nimages < 1 } { return }
292 set nslices [Bimage get $theimg nslices]
293 # puts "Image opened with $nslices slices"
294 if { $nslices < 1 } { return }
297 # puts "Average filtering kernel: $kernel"
298 Bimage average $theimg $kernel $kernel $kernel
301 set c [getImageCanvas $theimg]
302 set wc [getControlWindow $theimg]
303 # puts "Loading: $window_width $window_height $window_scale"
305 set width [Bimage get $theimg width]
306 set height [Bimage get $theimg height]
307 set origin [Bimage get $theimg origin]
309 if { $img_num < 0 } { set img_num 0 }
310 # puts "Image opened with origin $origin"
311 set oz [lindex $origin 2]
312 if { $oz < 0 } { set oz 0 }
313 if { $oz >= $nslices } { set oz 0 }
314 set scale $window_scale
315 # $wc.scale.scale set $window_scale
316 if { $window_width < 2 || $rem_scale == 0 } {
319 set window_height 512
320 if { $width > $window_width } { set scale [expr 0.1*(($window_width*10)/$width) ] }
322 if { $scale < 0.1 } { set scale 0.1 }
323 set window_scale $scale
324 # puts "Image scale = $scale mode = $mode"
325 set width [expr $width * $scale ]
326 set height [expr $height * $scale ]
327 set size "0 0 $width $height"
328 if { $width < 200 } { set width 200 }
329 if { $height < 200 } { set height 200 }
330 if { $width > $window_width } { set width $window_width }
331 if { $height > $window_height } { set height $window_height }
332 set window_width $width
333 set window_height $height
334 # puts "Width $width height $height scrollregion $size"
335 $c configure -width $width -height $height -scrollregion $size
336 # $c configure -scrollregion $size
337 # puts "Loading2: $window_width $window_height $window_scale"
339 # puts "Range step reconfigured"
341 # puts "Scales reconfigured"
342 if { ![ regexp {^([0-9.]+)$} $oz ] } { set oz 0 }
343 $wc.image.scale set $img_num
344 $wc.slice.scale set $oz
345 $wc.scale.scale set $scale
347 # puts "Pixel size updated"
353 ## @brief Derives the relative path from an absolute one relative to a given directory.
355 # @param cdir Current directory.
356 # @param path Absolute path to convert.
358 proc relativePath { cdir path } {
359 if { $cdir == "/" } { return $path }
360 set dlist [file split $cdir]
361 set plist [file split $path]
362 set len [llength $dlist]
363 # tk_messageBox -icon info -type ok -title "Current directory and path in relativePath" -message \
364 # "Directory = $cdir ($len) Path = $path [lindex $dlist 0]"
365 # puts "Current directory: $cdir Path: $path"
366 if { [lindex $dlist 0] == [lindex $plist 0] } {
367 for { set i 0 } { ( $i < $len ) && ( [lindex $dlist $i] == [lindex $plist $i] ) } { incr i 1 } {
370 for { set j $i } { $j < $len } { incr j 1 } {
373 set len [llength $plist]
374 for { set j $i } { $j < $len } { incr j 1 } {
375 set path [file join $path [lindex $plist $j]]
377 } elseif { [lindex $plist 0] == ".." } {
379 # puts "Relative path: $path"
380 # tk_messageBox -icon info -type ok -title "Modified path in relativePath" -message "$path"
384 ## @brief Loads the information for the current image file into a text window for display.
386 # @param filename Image file name.
388 proc getInfo { filename } {
391 # puts "$Bsoft $imgdir $filename"
392 if ![winfo exists .info] {
395 pack .info.buttons -side bottom -fill x
396 button .info.buttons.dismiss -text Dismiss \
397 -default active -command "destroy .info"
398 pack .info.buttons.dismiss -side left -expand 1 -pady 2
400 pack .info.frame -expand yes -fill both -padx 1 -pady 1
401 text .info.text -height 40 -width 100 -wrap word -background white\
402 -xscrollcommand ".info.xscroll set" \
403 -yscrollcommand ".info.yscroll set" \
404 -setgrid 1 -highlightthickness 0 -pady 2 -padx 3
405 scrollbar .info.xscroll -command ".info.text xview" \
406 -highlightthickness 0 -orient horizontal
407 scrollbar .info.yscroll -command ".info.text yview" \
408 -highlightthickness 0 -orient vertical
410 grid .info.text -in .info.frame -padx 1 -pady 1 \
411 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
412 grid .info.yscroll -in .info.frame -padx 1 -pady 1 \
413 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
414 grid rowconfig .info.frame 0 -weight 1 -minsize 0
415 grid columnconfig .info.frame 0 -weight 1 -minsize 0
420 set filename [file tail $filename]
421 wm title .info $filename
422 wm iconname .info $filename
423 # puts "$Bsoft/bin/bhead -verbose 7 -info $imgdir/$filename"
424 catch { exec $Bsoft/bin/bhead -verbose 7 -info $imgdir/$filename } theinfo
425 .info.text delete 1.0 end
426 .info.text insert 1.0 $theinfo
427 .info.text mark set insert 1.0
430 ## @brief Writes the current canvas to a postscript file.
433 proc writePostScript { } {
438 if { [string length $currdir] < 2 } { set currdir [pwd] }
439 # puts "Image directory: $imgdir"
440 set init_filename [file rootname $filename]
441 append init_filename .ps
442 set ps_filename [tk_getSaveFile -filetypes {{"PostScript" {.ps .PS} }} \
443 -initialfile $init_filename -initialdir $currdir -defaultextension .ps]
444 if { [string length $ps_filename] < 1 } { return }
445 set c [getImageCanvas $theimg]
446 set wc [getControlWindow $theimg]
447 set scale [$wc.scale.scale get]
448 set w [expr [Bimage get $theimg width] * $scale]
449 set h [expr [Bimage get $theimg height] * $scale]
450 $c postscript -file $ps_filename -pageanchor sw -x 0 -y 0 -width $w -height $h
451 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
452 # "$imgdir $filename"