Updated 2010-06-06 17:12:00 by paul

Paul Obermeier 2002/11/27

Simple image viewer using Img, showing the time needed for reading and displaying the image.
 package require Img

 proc CreateScrolledWidget { wType w titleStr args } {
     frame $w.par
     if { [string compare $titleStr ""] != 0 } {
         label $w.par.label -text "$titleStr"
     }
     eval { $wType $w.par.widget \
             -xscrollcommand "$w.par.xscroll set" \
             -yscrollcommand "$w.par.yscroll set" } $args
     scrollbar $w.par.xscroll -command "$w.par.widget xview" -orient horizontal
     scrollbar $w.par.yscroll -command "$w.par.widget yview" -orient vertical
     set rowNo 0
     if { [string compare $titleStr ""] != 0 } {
         set rowNo 1
         grid $w.par.label -sticky ew -columnspan 2
     }
     grid $w.par.widget $w.par.yscroll -sticky news
     grid $w.par.xscroll               -sticky ew
     grid rowconfigure    $w.par $rowNo -weight 1
     grid columnconfigure $w.par 0      -weight 1
     pack $w.par -side top -fill both -expand 1
     return $w.par.widget
 }

 proc CreateScrolledText { w titleStr args } {
     return [eval {CreateScrolledWidget text $w $titleStr} $args ]
 }

 proc CreateScrolledCanvas { w titleStr args } {
     return [eval {CreateScrolledWidget canvas $w $titleStr} $args ]
 }

 proc OpenFile {} {
     set imgName [tk_getOpenFile -initialdir $::lastDir -initialfile $::lastFile]
     if { $imgName != "" } {
         ReadPhoto $imgName
         set ::lastDir  [file dirname $imgName]
         set ::lastFile [file tail    $imgName]
     }
 }

 proc ReopenFile {} {
     ReadPhoto [file join $::lastDir $::lastFile]
 }

 proc ReadPhoto { name } {
     set sTime [clock clicks -milliseconds]
     set retVal [catch {image create photo -file $name} ph]
     if { $retVal != 0 } {
         $::timeMsg insert end "Warning: Cannot detect image file format.\n"
         return
     }
     set rTime [clock clicks -milliseconds]
     set iw [image width  $ph]
     set ih [image height $ph]

     $::imgCanv itemconfigure MyImage -image $ph
     set eTime [clock clicks -milliseconds]
     $::imgCanv configure -scrollregion "0 0 $iw $ih"

     $::timeMsg insert end "[file tail $name] ($iw x $ih): "
     $::timeMsg insert end \
         "[format "Read: %.2f sec "   [expr ($rTime - $sTime) / 1.0E3]]"
     $::timeMsg insert end \
         "[format "Display: %.2f sec" [expr ($eTime - $rTime) / 1.0E3]]"
     $::timeMsg insert end "\n"
     $::timeMsg see end
 }

 proc DeletePhotos {} {
     foreach ph [image names] {
         image delete $ph
     }
 }

 wm title . "Timed image viewer"
 set lastDir [pwd]
 set lastFile ""

 frame .cmd
 frame .view -borderwidth 2 -relief sunken
 frame .msg
 grid  .cmd  -row 0 -sticky new
 grid  .msg  -row 1 -sticky new
 grid  .view -row 2 -sticky news
 grid rowconfigure    . 2 -weight 1
 grid columnconfigure . 0 -weight 1

 button .cmd.open   -text "Open image ..." -command OpenFile
 button .cmd.reopen -text "Reopen image"   -command ReopenFile
 button .cmd.clr    -text "Clear images"   -command DeletePhotos
 pack .cmd.open .cmd.reopen .cmd.clr -side left -expand 1 -fill x

 set imgCanv [CreateScrolledCanvas .view ""]
 $imgCanv create image 0 0 -anchor nw -tag "MyImage"

 set timeMsg [CreateScrolledText   .msg  "Timings" -height 5]