###########################################################################
#
# This script is written to read Novo Images that have a ".fin" extension. 
#
# On execution  a widget will pop up asking for various parameters for the 
# images and also to specify a directory wherein these images are available.
#
# It then proceeds to open all these images as Raw Generic
# 
# All the images in this directory need to be similar i.e they need to be of
# the same dimensions, encoding etc.
#
# Written by Raj Jagannathan
# Date : May 26, 1999
#
#########################################################################

##########################################################################
#
#Setting path for widget
#
###########################################################################

set PXHOME $env(PXHOME)
set auto_path [linsert $auto_path 0 $PXHOME/ui/tix $PXHOME/tcl]

##########################################################################
#
# Procedure:   ReadAsRaw
#
# Usage:       Read As Raw Widget 
#
# Summary:     Creates a dialog box for setting the width, height, number of 
#              slices, pixel encoding and bits per pixel
#
##########################################################################


proc ImageInfo {w } {
    global ImageInfo 
 
    if [winfo exists $w] {
        wm deiconify $w
        raise $w
        return
    }
 puts "starting script"

 toplevel $w
    wm title $w " Image Information"
    wm iconname $w " Image Information"
 
 
    frame $w.f3


####################################
#
# Selecting a directory
#
#####################################

  tixFileEntry $w.directory -label "Directory Name:"\
      -variable ImageInfo($w,directory)\
         -options {
              entry.width 25
         }


   bind [$w.directory subwidget entry] <Return> {
         [winfo toplevel %W].apply invoke}

   pack  $w.directory -in $w.f3 -side top -expand yes -fill both \
       -padx 100 -pady 10


###########################################################################
#
#Setting the width, height, number of slices, pixel encoding, bits per pixel
# 
#
#########################################################################

 frame $w.f1
 
set ImageInfo($w,x) 64
    tixControl $w.x -label "Width(No. of columns)" \
        -integer true -variable ImageInfo($w,x) -min 1 -step 1 \
        -selectmode immediate \
        -options {
            entry.width 4
            label.width 22
            label.anchor w
        }


    set ImageInfo($w,y) 64
    tixControl $w.y -label "Height(No. of rows)" \
        -integer true  -variable ImageInfo($w,y) -min 1 -step 1 \
        -selectmode immediate \
        -options {
            entry.width 4
            label.width 22
            label.anchor w
        }


  set ImageInfo($w,z) 14
    tixControl $w.z -label "No. of slices" \
        -integer true  -variable ImageInfo($w,z) -min 1 -step 1 \
        -selectmode immediate \
        -options {
            entry.width 4
            label.width 22
            label.anchor w
        }
    
    
    set ImageInfo($w,bits) 16
    tixControl $w.bits -label "Bits per pixel" \
        -integer true  -variable ImageInfo($w,bits) -min 1 -step 1 \
        -selectmode immediate \
        -options {
            entry.width 4
            label.width 22
            label.anchor w
        }


pack $w.x $w.y $w.z $w.bits -in $w.f1 -side top -anchor w \
        -padx 3 -pady 10

#########################################################################
#
# Setting the x,y,z dimensions and the pixel encoding for the images
#
#########################################################################

frame $w.f2

 set ImageInfo($w,xdim) 1
    tixControl $w.xdim -label "  Voxel Dimension along X (mm)" \
         -variable ImageInfo($w,xdim) -min 0 -step 0.5 \
        -selectmode immediate \
        -options {
            entry.width 8
            label.width 30
            label.anchor w
        }
 
 
    set ImageInfo($w,ydim) 1
    tixControl $w.ydim -label "  Voxel Dimension along Y (mm)" \
         -variable ImageInfo($w,ydim) -min 0 -step 0.5 \
        -selectmode immediate \
        -options {
            entry.width 8
            label.width 30
            label.anchor w
        }
 
 
  set ImageInfo($w,zdim) 1
    tixControl $w.zdim -label "  Voxel Dimension along Z (mm)" \
         -variable ImageInfo($w,zdim) -min 0 -step 0.5 \
        -selectmode immediate \
        -options {
            entry.width 8
            label.width 30
            label.anchor w
        }
  
    set ImageInfo($w,enc) 2
    tixControl $w.enc -label "  Encoding (1-Unsigned, 2-Signed, 3-Real)" \
        -integer true  -variable ImageInfo($w,enc) -min 1 -max 3 -step 1 \
        -selectmode immediate \
        -options {
            entry.width 4
            label.width 40
            label.anchor w
        }

 
pack $w.enc $w.xdim $w.ydim $w.zdim -in $w.f2 -side top -anchor w \
        -padx 3 -pady 10

  frame $w.btns
   frame $w.btns.b -relief raised -borderwidth 1

   button $w.ok -text "OK" -width 5  \
   -command "ImageInfo:apply $w destroy "
  
     bind $w.ok <Return> {
	 [winfo toplevel %W].ok invoke
       }

   button $w.apply     -command "ImageInfo:apply $w keep" \
        -text "Apply" -width 5
    bind $w.apply <Return> {
        [winfo toplevel %W].apply invoke}
 
      
    button $w.cancel    -command "ImageInfo:destroy $w" \
        -text "Cancel" -width 5
    bind $w.cancel <Return> {
        [winfo toplevel %W].cancel invoke}
   
    pack $w.btns.b -side bottom -fill x -padx 3 -pady 5
    pack $w.ok $w.apply $w.cancel -in $w.btns.b \
        -side left -expand yes -padx 3 -pady 10 -fill y

    pack $w.f3 -side top -expand yes -fill both
     pack $w.btns -side bottom  -expand yes -fill both
    pack $w.f1  -side left  -expand yes -fill both 
    pack $w.f2 -side right   -expand yes -fill both

   
  }


#######################################################################
#
# Procedure     ImageInfo:apply
#
# Usage:        ImageInfo:apply w 
#
# Summary:      Applies Image Info supplied to open images as Raw Generic 
#                      
#
############################################################################

proc ImageInfo:apply { w dialog } {

 global ImageInfo 

 set enc $ImageInfo($w,enc) 

 #
 # cd to the directory where the files are
 #
    $w.directory update
    if {![file isdirectory $ImageInfo($w,directory)]} {
        LocalErrorBox "No such directory: $ImageInfo($w,directory)"
        return
    }
    cd $ImageInfo($w,directory)
 
 #
 # Check if there are ".fin" files in the given directory
 #
    set any [catch {glob *.fin } files]
 

   if {$any == 1} {
   MxPause "There are no .fin files in this directory ...."
   return 
    }

#
# Get the current folder - open a new folder if none exists
#
    set result [MxGetCurrentFolder folder]
    if $result {
        MxNewFolder folder Window
  }

#
# Setting the encoding based on specified number in widget
#

   if {$enc == 1} {
     
         set e "UI"
   
   } 
     if {$enc == 2} { 

         set e "SI"
   
   } else {
   
         set e "R"
   
    }

#
#Setting values for the Format list
#

keylset formatlist Width $ImageInfo($w,x)
keylset formatlist Height $ImageInfo($w,y)
keylset formatlist Slices $ImageInfo($w,z)
keylset formatlist Bits $ImageInfo($w,bits)
keylset formatlist Encoding $e
keylset formatlist Type 3D
keylset formatlist Orientation U
keylset formatlist Interleave B
keylset formatlist ByteOrder N


#
#Setting values for the property list
#

keylset propertylist XScale $ImageInfo($w,xdim)
keylset propertylist YScale $ImageInfo($w,ydim)
keylset propertylist ZScale $ImageInfo($w,zdim)
keylset propertylist Op 3D

#
# Looping over all the ".fin" files in the specified directory
#

foreach file $files {

puts "got into loop..............."
set check1 [MxOpenRawImage $folder propertylist formatlist $ImageInfo($w,directory)/$file  image]
puts "ok got here..and check1 is $check1...file is.....$file"
puts "list values are [set formatlist]"
 
  }

#
# Getting rid of dialog box if OK button is used
#

 if {$dialog == "destroy"} {
 
    ImageInfo:destroy $w
 
   }

 
 }


# Procedure:    Image Info:destroy
#
# Usage:        Image Info :destroy w
#
# Summary:      Destroys Image Info  dialog box
#


  proc ImageInfo:destroy { w } {
 
        destroy $w
  }

ImageInfo .file
tkwait window .file


