#!/bin/sh
# the next line restarts using wish\
exec wish "$0" "$@" 

if {![info exists imScilab(sourcing)]} {
    switch $tcl_platform(platform) {
	windows {
	}
	default {
	    option add *Scrollbar.width 10
	}
    }
    
}
#############################################################################
# Visual Tcl v1.51 Project
#

#################################
# imScilab LIBRARY PROCEDURES
#

if {![info exists imScilab(sourcing)]} {
proc Window {args} {
    global imScilab
    set cmd     [lindex $args 0]
    set name    [lindex $args 1]
    set newname [lindex $args 2]
    set rest    [lrange $args 3 end]
    if {$name == "" || $cmd == ""} { return }
    if {$newname == ""} { set newname $name }
    if {$name == "."} { wm withdraw $name; return }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists} {
                wm deiconify $newname
            } elseif {[info procs imScilabWindow$name] != ""} {
                eval "imScilabWindow$name $newname $rest"
            }
            if {[wm state $newname] == "normal"} {
                imScilab:FireEvent $newname <<Show>>
            }
        }
        hide    {
            if {$exists} {
                wm withdraw $newname
                imScilab:FireEvent $newname <<Hide>>
                return}
        }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

proc imScilab:WindowsCleanup {} {
    global imScilab
    if {[info exists imScilab(sourcing)]} { return }
    foreach w [winfo children .] {
    	wm protocol $w WM_DELETE_WINDOW { exit }
    }
}
}

if {![info exists imScilab(sourcing)]} {
proc {imScilab:DefineAlias} {target alias widgetProc top_or_alias cmdalias} {
    global widget

    set widget($alias) $target
    set widget(rev,$target) $alias

    if {$cmdalias} {
        interp alias {} $alias {} $widgetProc $target
    }

    if {$top_or_alias != ""} {
        set widget($top_or_alias,$alias) $target

        if {$cmdalias} {
            interp alias {} $top_or_alias.$alias {} $widgetProc $target
        }
    }
}

proc {imScilab:DoCmdOption} {target cmd} {
    ## menus are considered toplevel windows
    set parent $target
    while {[winfo class $parent] == "Menu"} {
        set parent [winfo parent $parent]
    }

    regsub -all {\%widget} $cmd $target cmd
    regsub -all {\%top} $cmd [winfo toplevel $parent] cmd

    uplevel #0 [list eval $cmd]
}

proc {imScilab:FireEvent} {target event} {
    foreach bindtag [bindtags $target] {
        set tag_events [bind $bindtag]
        set stop_processing 0
        foreach tag_event $tag_events {
            if {$tag_event == $event} {
                set bind_code [bind $bindtag $tag_event]
                regsub -all %W $bind_code $target bind_code
                set result [catch {uplevel #0 $bind_code} errortext]
                if {$result == 3} {
                    # break exception, stop processing
                    set stop_processing 1
                } elseif {$result != 0} {
                    bgerror $errortext
                }
                break
            }
        }
        if {$stop_processing} {break}
    }
}

proc {imScilab:Toplevel:WidgetProc} {w args} {
    if {[llength $args] == 0} {
        return -code error "wrong # args: should be \"$w option ?arg arg ...?\""
    }

    ## The first argument is a switch, they must be doing a configure.
    if {[string index $args 0] == "-"} {
        set command configure

        ## There's only one argument, must be a cget.
        if {[llength $args] == 1} {
            set command cget
        }
    } else {
        set command [lindex $args 0]
        set args [lrange $args 1 end]
    }

    switch -- $command {
        "hide" -
        "Hide" {
            Window hide $w
        }

        "show" -
        "Show" {
            Window show $w
        }

        "ShowModal" {
            Window show $w
            raise $w
            grab $w
            tkwait window $w
            grab release $w
        }

        default {
            eval $w $command $args
        }
    }
}

proc {imScilab:WidgetProc} {w args} {
    if {[llength $args] == 0} {
        return -code error "wrong # args: should be \"$w option ?arg arg ...?\""
    }

    ## The first argument is a switch, they must be doing a configure.
    if {[string index $args 0] == "-"} {
        set command configure

        ## There's only one argument, must be a cget.
        if {[llength $args] == 1} {
            set command cget
        }
    } else {
        set command [lindex $args 0]
        set args [lrange $args 1 end]
    }

    eval $w $command $args
}

proc {imScilab:toplevel} {args} {
    uplevel #0 eval toplevel $args
    set target [lindex $args 0]
    namespace eval ::$target {}
}
}

if {[info exists imScilab(sourcing)]} {
proc imScilab:project:info {} {
    namespace eval ::widgets::.avi {
        array set save {-relief 1}
    }
    namespace eval ::widgets::.avi.fra38 {
        array set save {-borderwidth 1 -height 1 -relief 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra38.sca62 {
        array set save {-bigincrement 1 -cursor 1 -from 1 -orient 1 -resolution 1 -tickinterval 1 -to 1 -variable 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra38.ent66 {
        array set save {-background 1 -textvariable 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra38.lab67 {
        array set save {-height 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra46 {
        array set save {-borderwidth 1 -height 1 -relief 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra46.lab47 {
        array set save {-background 1 -height 1 -relief 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.mes65 {
        array set save {-relief 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra69 {
        array set save {-borderwidth 1 -height 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra69.but70 {
        array set save {-height 1 -pady 1 -relief 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra69.but74 {
        array set save {-height 1 -pady 1 -relief 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra69.but75 {
        array set save {-height 1 -pady 1 -relief 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra69.but76 {
        array set save {-height 1 -pady 1 -relief 1 -text 1 -width 1}
    }
    namespace eval ::widgets::.avi.fra77 {
        array set save {-borderwidth 1 -height 1 -width 1}
    }
    namespace eval ::widgets_bindings {
        set tagslist {}
    }
}
}
############################
# code to load user images
global imScilab
imScilab:image:create_new_image "aviopen.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "avisave.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "aviplay.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "aviclose.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "tdiff.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "bsub.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "avidemo.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "confirm.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "framechoose.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "choosebg.gif" \
    "user image" \
    "user"
#################################
# GLOBAL VARIABLES
#
global imScilab
	set imScilab(avimsg) "AVI Process by V.I.P"
	set imScilab(avimainmsg) "AVI Process by V.I.P"
	set imScilab(aviprocess) ""
global widget
	set widget(avimsg) {.avi.mes65}
global tcl_platform

#################################
# USER DEFINED PROCEDURES
#
#######################		Avi File Ctrl		##############
proc {avi_save} {} {
global widget
global imScilab
	if {$imScilab(aviprocess)==""} {
	tk_messageBox -message "You do not have any avifile process!"
	return
	}
	set imScilab(avisave,to) [expr $imScilab(aviframeSum)-1]
	source [file join $imScilab(mainfuncdir) avisave_sub.tcl]
	vwait imScilab(avisave,ready)
	if {$imScilab(avisave,ready) == 0} {
	return
	}
	
	set file_types {
	        {"AVI Files" { .avi } }
	        {"All Files" * }
	}
	set imScilab(openedavifile) [tk_getSaveFile -filetypes $file_types -initialdir pwd -initialfile $imScilab(openedavifile) -defaultextension .avi -parent .avi]
	
	if {$imScilab(openedavifile) != "" } {
	set imScilab(avifileextension) [file extension $imScilab(openedavifile)]
   	if {$imScilab(avifileextension) != ".avi" && $imScilab(avifileextension) != ".AVI"} {
   	tk_messageBox -message "Wrong Open File Type!"
   	set imScilab(avi,wrongfiletype) 1
   	}
   	if {$imScilab(avi,wrongfiletype) == 0} {
	set imScilab(avi,exposeclose) 1
	source [file join $imScilab(mainfuncdir) avisub.tcl]
	set imScilab(avifilepath) $imScilab(openedavifile)
	ScilabEval "                                                                                      
	avisave('$imScilab(avifilepath)',$imScilab(avisave,scale),$imScilab(avisave,rate),$imScilab(avisave,from),$imScilab(avisave,to));"
	set imScilab(status_hint) $imScilab(openedavifile)
	imScilab:avi_reset_change
	}
	set imScilab(avi,wrongfiletype) 0
	}
}

proc {avi_get_file} {} {
global widget
global imScilab
if {$imScilab(avi_new) == 0} {
 uplevel #0 {set imScilab(avi_new) 1}
 } else {return}
if {$imScilab(avi_change) > 0} {
   set ans [tk_messageBox -message "File Unsaved! Save Now?" -type yesnocancel -default no -title "Warning" ]
       if {$ans == "yes" } {avi_save}
       if {$ans == "cancel"} {set imScilab(avi_new) 0;return}
}
########begin open file ###

	set file_types {
	        {"AVI Files" { .avi } }
		{"All Files" * }
	}
	set imScilab(openedavifile) [tk_getOpenFile -filetypes $file_types -initialdir pwd -defaultextension .avi -parent .avi]
	set imScilab(status_hint) $imScilab(openedavifile)
	wm title .avi "VIP_Video - $imScilab(status_hint)"

   	if {$imScilab(openedavifile) != "" } {
   	set imScilab(avifileextension) [file extension $imScilab(openedavifile)]
   	if {$imScilab(avifileextension) != ".avi" && $imScilab(avifileextension) != ".AVI"} {
   	tk_messageBox -message "Wrong Open File Type!"
   	set imScilab(avi,wrongfiletype) 1
   	} 
###	
	if {$imScilab(avi,wrongfiletype) == 0} {
	set imScilab(aviState) normal
	.avi.fra38.btnframechoose configure -state normal
	.avi.fra21.btnchoosebg configure -state normal
	ScilabEval "                                                                                     
	aviopen('$imScilab(openedavifile)');aviframe;TK_SetVar('imScilab(aviprev)',string(1));"
#
	set imScilab(avifilepath) $imScilab(openedavifile)
	imScilab:image_change_savestate normal
	set imScilab(currimage) 4
	set imScilab(imagepath4) ""
	imScilab:aviinfobtn
	ScilabEval "                                                                                    
	TK_SetVar('imScilab(main,HelpMsg)','$imScilab(avifilepath)'+' '+string(ppavi(1))+'K'+' '+string(ppavi(2))+'FRAMES');"
#	
      	if {$imScilab(switch) == 1 } {
	ScilabEval "                                                                                             
	impreview(avilabid_sub,aviframe_sub_tdiff,320,240);"
	} 
	if {$imScilab(switch) == 2 } {
	ScilabEval "                                                                                             
	impreview(avilabid_sub,aviframe_sub_bsub,320,240);"
	}
	set imScilab(imageChoose) AviFrame

	set imScilab(aviframe,storagetype) "truecolor"

	set imScilab(image4,storagetype) "truecolor"
	imScilab:avi_reset_change
	}
	set imScilab(avi,wrongfiletype) 0
	}
 uplevel #0 {set imScilab(avi_new) 0}
}


proc {avi_play} {} {
global widget imScilab tcl_platform
if {$tcl_platform(platform) == "windows"} {
	set tmp [file join C:\\Program\ Files\\Windows\ Media\ Player\\wmplayer.exe]
	exec $tmp /play [file join $imScilab(avifilepath)] &
} else {
	set imScilab(avimovie) {file:}
	append imScilab(avimovie) $imScilab(avifilepath)
	exec mplayer $imScilab(avimovie) &
}
}

proc {avi_close} {} {
global imScilab
	if {$imScilab(aviprev) == 1} {
	ScilabEval "                                                                                       
	AviReadEnd(ppavi);aviframe=\[\];aviframe_temp=\[\];aviframe_sub_tdiff=\[\];aviframe_sub_bsub=\[\];"
	.avi.fra38.btnframechoose configure -state disable
	set imScilab(aviState) disable
	set imScilab(imageChoose) all
	set imScilab(openedavifile) ""
	set imScilab(aviprev) 0
	pack forget .main.fra17.fra24
	imScilab:avi_reset_change
	}
	wm title .main "VIP for Scilab2.7(win)"
	destroy .avi
}

##################################		aviframe_sub function
###
# aviframe_tdiff
proc {avi_tdiff_sub} {} {
global imScilab
set base .avi
    place $base.fra31 \
    	-in $base -x 511 -y 3 -width 320 -height 21 -anchor nw \
        -bordermode ignore
    place $base.fra31.btnaviconfirm \
    	-in $base.fra31 -x 2 -y 2 -width 80 -height 17 -anchor nw \
        -bordermode ignore
    
    place $base.fra41 \
    	-in $base -x 511 -y 270 -width 320 -height 25 -anchor nw \
        -bordermode ignore
    place $base.fra41.lab42 \
    	-in $base.fra41 -x 1 -y 1 -width 110 -height 21 \
    	-bordermode ignore
    pack $base.fra41.btnInterval \
    	-in $base.fra41 -anchor ne -expand 0 -side right -fill none -pady 1
    place $base.fra51 \
    	-in $base -x 511 -y 300 -width 320 -height 25 -anchor nw \
        -bordermode ignore
    place $base.fra51.lab52 \
    	-in $base.fra51 -x 1 -y 1 -width 80 -height 21 \
    	-bordermode ignore
    pack $base.fra51.ent53 \
    	-in $base.fra51 -anchor ne -expand 0 -side right -fill none -pady 2
}

proc {avi_tdiff_forget} {} {
global imScilab
set base .avi
	place forget $base.fra41
	place forget $base.fra51
}


proc {avi_tdiff} {} {
global imScilab

if { $imScilab(switch) == 2} {
	wm geometry .avi 840x326+[winfo x .avi]+[winfo y .avi]
	avi_busb_forget
	avi_tdiff_sub
    	set imScilab(avi,tdiff) 1
	.avi.fra77.btntdiff configure -relief sunken
	.avi.fra77.btnbsub configure -relief flat
	set imScilab(switch) 1
	return
}

if { $imScilab(avi,tdiff) == 0 } {
    wm geometry .avi 840x326+[winfo x .avi]+[winfo y .avi]
    avi_busb_forget
    avi_tdiff_sub
    set imScilab(avi,tdiff) 1
    .avi.fra77.btntdiff configure -relief sunken
    .avi.fra77.btnbsub configure -relief flat
    set imScilab(switch) 1
} else {
    wm geometry .avi 511x326+[winfo x .avi]+[winfo y .avi]
    set imScilab(avi,tdiff) 0
    .avi.fra77.btntdiff configure -relief flat
}	
}

# aviframe bsub
proc {avi_bsub_sub} {} {
global imScilab
set base .avi
    place $base.fra31 \
    	-in $base -x 511 -y 3 -width 320 -height 21 -anchor nw \
        -bordermode ignore
    place $base.fra31.btnaviconfirm \
    	-in $base.fra31 -x 2 -y 2 -width 80 -height 17 -anchor nw \
        -bordermode ignore
    place $base.fra31.lab12 \
    	-in $base.fra31 -x 160 -y 2 -width 80 -height 17 -anchor nw \
    	-bordermode ignore
    pack $base.fra31.ent13 \
    	-in $base.fra31 -anchor ne -expand 0 -side right -fill none -pady 2
    place $base.fra21 \
        -in $base -x 511 -y 270 -width 320 -height 55 -anchor nw \
        -bordermode ignore 
    place $base.fra21.sca22 \
        -in $base.fra21 -x 5 -y 19 -width 310 -height 32 -anchor nw \
        -bordermode ignore 
    place $base.fra21.btnchoosebg \
        -in $base.fra21 -x 2 -y 3 -width 53 -height 20 -anchor nw \
        -bordermode ignore
    pack $base.fra21.ent23 \
    	-in $base.fra21 -anchor ne -expand 0 -side right -fill none -pady 2
}

proc {avi_busb_forget} {} {
global imScilab
set base .avi
	place forget $base.fra21
	place forget $base.fra31.lab12
	pack forget $base.fra31.ent13
}

proc {avi_bsub} {} {
global imScilab

if { $imScilab(switch) == 1 } {
	wm geometry .avi 840x326+[winfo x .avi]+[winfo y .avi]
	avi_tdiff_forget
	avi_bsub_sub
	set imScilab(avi,bsub) 1
	.avi.fra77.btnbsub configure -relief sunken
	.avi.fra77.btntdiff configure -relief flat
	.avi.fra21.sca22 configure -to $imScilab(aviframeSum)
	set imScilab(switch) 2
	return
}

if { $imScilab(avi,bsub) == 0 } {
    wm geometry .avi 840x326+[winfo x .avi]+[winfo y .avi]
    avi_tdiff_forget
    avi_bsub_sub
    set imScilab(avi,bsub) 1
    .avi.fra77.btnbsub configure -relief sunken
    .avi.fra77.btntdiff configure -relief flat
    .avi.fra21.sca22 configure -to $imScilab(aviframeSum)
    set imScilab(switch) 2
} else {
    wm geometry .avi 511x326+[winfo x .avi]+[winfo y .avi]
    set imScilab(avi,bsub) 0
    .avi.fra77.btnbsub configure -relief flat
}	
}

# avi shipdemo
proc {avi_moddemo_sub} {} {
global imScilab
	
	set imScilab(avisave,to) [expr $imScilab(aviframeSum)-1]
	source [file join $imScilab(mainfuncdir) avisave_sub.tcl]
	vwait imScilab(avisave,ready)
	if {$imScilab(avisave,ready) == 0} {
	return
	}
	
	set file_types {
	        {"AVI Files" { .avi } }
	        {"All Files" * }
	}
	set imScilab(openedavifile) [tk_getSaveFile -filetypes $file_types -initialdir pwd -initialfile $imScilab(openedavifile) -defaultextension .avi -parent .avi]
	if {$imScilab(openedavifile) == "" } {
	return
	}
	if {$imScilab(openedavifile) != "" } {
	set imScilab(avi,exposeclose) 1
	source [file join $imScilab(mainfuncdir) avisub.tcl]
	ScilabEval "                                                                                      
	shipdemo(ppavi,'$imScilab(openedavifile)',$imScilab(avisave,from),$imScilab(avisave,to));"
	}
}


proc {avi_moddemo} {} {
global imScilab
	if {$imScilab(openedavifile) == "" } {
	tk_messageBox -message "Please open an AVI file first!" -title "imScilab_avi_error"
	}
	if {$imScilab(openedavifile) != "" } {
	avi_moddemo_sub
        }
}

proc {avi_btnInterval} {interval} {
global imScilab
	set imScilab(avi,tdiff,interval) $interval
	if {[expr $imScilab(aviframeID)+$interval]<1520} {
	set imScilab(aviframeID_tdiff) [expr $imScilab(aviframeID)+$interval]
	} else {
	set imScilab(aviframeID_tdiff) [expr $imScilab(aviframeID)-$interval]
	}
	ScilabEval "                                                                                     
	aviframe_sub_tdiff=AviReadFrame(ppavi,$imScilab(aviframeID_tdiff));$imScilab(avitdiffprocess)impreview(avilabid_sub,aviframe_sub_tdiff,320,240);"
}

### avi special process confirm
proc {avi_confirm} {} {
global imScilab
	if {$imScilab(openedavifile) != ""} {
	if {$imScilab(switch)==1} {
	ScilabEval "                                                                                     
	aviframe_sub_tdiff;aviframe=OnSubtract(aviframe,aviframe_sub_tdiff,$imScilab(avi,tdiff,threshold));impreview(avilabid,aviframe,320,240);impreview(avilabid_sub,aviframe_sub_tdiff,320,240);aviframe_temp=aviframe;"
	append imScilab(aviprocess) aviframe=framesub\(aviframe\,$imScilab(avi,tdiff,interval)\,$imScilab(avi,tdiff,threshold)\)\;
	set imScilab(aviframe,storagetype) "binary"
	set imScilab(image4,storagetype) "binary"
	} else {
	ScilabEval "                                                                                     
	aviframe_sub_bsub;aviframe=OnSubtract(aviframe,aviframe_sub_bsub,$imScilab(avi,bsub,threshold));impreview(avilabid,aviframe,320,240);impreview(avilabid_sub,aviframe_sub_bsub,320,240);aviframe_temp=aviframe;"
	append imScilab(aviprocess) aviframe=OnSubtract\(aviframe\,aviframe_sub_bsub\,$imScilab(avi,bsub,threshold)\)\;
	set imScilab(aviframe,storagetype) "binary"
	set imScilab(image4,storagetype) "binary"
	}
	}
}

##########################	aviframe sub end here

### avi expose
proc {avi_expose} {} {
global imScilab 
set imScilab(avilabid) [winfo id .avi.fra46.lab47]
set imScilab(avilabid_sub) [winfo id .avi.fra88.lab89]
if {$imScilab(avi,exposeclose) == 1 } {
return
}
if {$imScilab(aviprev) == 1 } {
ScilabEval "                                                                                             
      impreview(avilabid,aviframe_temp,320,240);" }
}

proc {avi_expose_sub} {} {
global imScilab 
if {$imScilab(avi,exposeclose) == 1 } {
return
}
if {$imScilab(aviprev) == 1 } {
	set imScilab(avi,exposeflag) [expr $imScilab(avi,bsub)+$imScilab(avi,tdiff)]
	if {$imScilab(avi,exposeflag) != 0} {
	if {$imScilab(switch) == 1 } {
	ScilabEval "                                                                                             
	impreview(avilabid_sub,aviframe_sub_tdiff,320,240);"
	} else {
	ScilabEval "                                                                                             
	impreview(avilabid_sub,aviframe_sub_bsub,320,240);"
	}
	}
}
}
###

#######################		Avi File Ctrl End Here	#############




###########################################################
## Procedure:  init
proc {imScilab:avi_init} {} {
global imScilab
	set imScilab(aviframeSum) 0
	set imScilab(aviframeCurr_demo) 0
	set imScilab(aviframeSum_demo) 0
	set imScilab(aviframeID_bsub) 0
	set imScilab(aivframeID_tdiff) 0
	set imScilab(avi_new) 0
	set imScilab(avi_change) 0
	set imScilab(aviprev) 0
	
	set imScilab(avi,tdiff) 0
	set imScilab(avi,bsub) 0
	set imScilab(switch) 0
	set imScilab(avi,tdiff,interval) 3
	set imScilab(avi,tdiff,threshold) 15
	set imScilab(avi,bsub,threshold) 15
	
	set imScilab(avi,exposeclose) 0
	set imScilab(avi,exposeflag) 0
	
	set imScilab(avi,wrongfiletype) 0
	set imScilab(openedavifile) ""
	set imScilab(aviprocess) ""
}
imScilab:avi_init

###########################################################
## Procedure:  main
proc {imScilab:avi_main} {} {
::::vBalloon::imScilab:set_balloon .avi.fra69.btnaviopen "Open AviFile"
::::vBalloon::imScilab:set_balloon .avi.fra69.btnavisave "Save AviFile"
::::vBalloon::imScilab:set_balloon .avi.fra69.btnaviplay "Play AviFile"
::::vBalloon::imScilab:set_balloon .avi.fra69.btnaviclose "Close"
::::vBalloon::imScilab:set_balloon .avi.fra38.btnframechoose "Choose Frame"
::::vBalloon::imScilab:set_balloon .avi.fra77.btntdiff "Temporal differencing"
::::vBalloon::imScilab:set_balloon .avi.fra77.btnbsub "Background substrations"
::::vBalloon::imScilab:set_balloon .avi.fra77.btnavidemo "Demo"
::::vBalloon::imScilab:set_balloon .avi.fra31.btnaviconfirm "Confirm process"
global imScilab

}


#################################
# imScilab GENERATED GUI PROCEDURES
#

proc imScilabWindow. {base {container 0}} {
    if {$base == ""} {
        set base .
    }
    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    wm focusmodel $base passive
    wm geometry $base 200x200+88+116; update
    wm maxsize $base 1028 746
    wm minsize $base 115 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm withdraw $base
    wm title $base "imScilab_avi"
    bindtags $base "$base imScilab all"
    imScilab:FireEvent $base <<Create>>
    }
    ###################
    # SETTING GEOMETRY
    ###################

    imScilab:FireEvent $base <<Ready>>
}

proc imScilabWindow.avi {base {container 0}} {
    if {$base == ""} {
        set base .avi
    }
    if {[winfo exists $base] && (!$container)} {
        wm deiconify $base; return
    }
    global imScilab
    global widget

    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    imScilab:toplevel $base -class Toplevel \
        -relief groove 
    wm transient $base .main
    wm focusmodel $base passive
    wm geometry $base 511x326+260+182; update
    wm maxsize $base 1028 746
    wm minsize $base 115 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm deiconify $base
    wm title $base "VIP_Video"
    imScilab:FireEvent $base <<Create>>
    }
    
    bind .avi <Destroy> {
    avi_close
}
    frame $base.fra38 \
        -borderwidth 2 -relief groove -height 55 -width 280
    scale $base.fra38.sca62 \
        -bigincrement 0.0 -cursor fleur -from 0 -orient horizontal \
        -resolution 1 -tickinterval 0.0 -to 0 \
        -width 273
    
    entry $base.fra38.ent66 \
        -background white -textvariable imScilab(aviframeID) -width 10
    
    button $base.fra38.btnframechoose \
        -height 10 -text aviframe -width 80 -relief groove -pady 0 \
        -command {ScilabEval "aviframe=AviReadFrame(ppavi, $imScilab(aviframeID));aviframe_temp=aviframe;impreview(avilabid,aviframe_temp,320,240);"
        set imScilab(aviframe,storagetype) "truecolor"
	set imScilab(image4,storagetype) "truecolor"
	set imScilab(aviprocess) "" } \
        -state disable
    bind $base.fra38.btnframechoose <Enter> {
        .avi.fra38.btnframechoose configure -relief raise
        set imScilab(avimsg) "Choose AVIFile Frame"
    }
    bind $base.fra38.btnframechoose <Leave> {
        .avi.fra38.btnframechoose configure -relief groove
        set imScilab(avimsg) $imScilab(avimainmsg)
    }
    
    
    frame $base.fra46 \
        -borderwidth 2 -relief groove -height 240 -width 320  
    label $base.fra46.lab47 \
        -background #ffffff -height 0 -relief sunken -text {aviframe preview} \
        -width 278
    bind $base.fra46 <Expose> {
    avi_expose }
    
    message $base.mes65 \
        -relief groove -text message -width 320 \
        -textvariable imScilab(avimsg)
    
    frame $base.fra69 \
        -borderwidth 2 -height 245 -width 90 
    button $base.fra69.btnaviopen \
        -height 50 -pady 0 -relief flat -text Open -width 50 \
        -image [imScilab:image:get_image "aviopen.gif"] \
        -command avi_get_file
    bind $base.fra69.btnaviopen <Enter> {
        .avi.fra69.btnaviopen configure -relief raise
        set imScilab(avimsg) "Open AVIFile"
    }
    bind $base.fra69.btnaviopen <Leave> {
        .avi.fra69.btnaviopen configure -relief flat
        set imScilab(avimsg) $imScilab(avimainmsg)
    }
      
    button $base.fra69.btnavisave \
        -height 50 -pady 0 -relief flat -text Save -width 50 \
        -image [imScilab:image:get_image "avisave.gif"] \
        -command avi_save
    bind $base.fra69.btnavisave <Enter> {
        .avi.fra69.btnavisave configure -relief raise
        set imScilab(avimsg) "Save AVIFile"
    }
    bind $base.fra69.btnavisave <Leave> {
        .avi.fra69.btnavisave configure -relief flat
        set imScilab(avimsg) $imScilab(avimainmsg)
    }
    
    button $base.fra69.btnaviplay \
        -height 50 -pady 0 -relief flat -text Play -width 50 \
        -image [imScilab:image:get_image "aviplay.gif"] \
        -command avi_play
    bind $base.fra69.btnaviplay <Enter> {
        .avi.fra69.btnaviplay configure -relief raise
        set imScilab(avimsg) "play AVIFile"
    }
    bind $base.fra69.btnaviplay <Leave> {
        .avi.fra69.btnaviplay configure -relief flat
        set imScilab(avimsg) $imScilab(avimainmsg)
    }
    button $base.fra69.btnaviclose \
        -height 50 -pady 0 -relief flat -text Close -width 50 \
        -image [imScilab:image:get_image "aviclose.gif"] \
        -command avi_close
    bind $base.fra69.btnaviclose <Enter> {
        .avi.fra69.btnaviclose configure -relief raise
        set imScilab(avimsg) "Exit AVIProcess"
    }
    bind $base.fra69.btnaviclose <Leave> {
        .avi.fra69.btnaviclose configure -relief flat
        set imScilab(avimsg) $imScilab(avimainmsg)
    }
    
    
    frame $base.fra77 \
        -borderwidth 2 -height 240 -width 80 -relief sunken
    button $base.fra77.btntdiff \
        -height 50 -pady 0 -relief flat -text tdiff -width 50 \
        -image [imScilab:image:get_image "tdiff.gif"] \
        -command {avi_tdiff}
    bind $base.fra77.btntdiff <Enter> {
        set imScilab(avimsg) "Temporal Differencing"
        .avi.fra77.btntdiff configure -relief raise
    }
    bind $base.fra77.btntdiff <Leave> {
        set imScilab(avimsg) $imScilab(avimainmsg)
        .avi.fra77.btntdiff configure -relief flat
    }
    button $base.fra77.btnbsub \
        -height 50 -pady 0 -relief flat -text bsub -width 50 \
        -image [imScilab:image:get_image "bsub.gif"] \
        -command {avi_bsub}
    bind $base.fra77.btnbsub <Enter> {
        set imScilab(avimsg) "Background Subtraction"
        .avi.fra77.btnbsub configure -relief raise
    }
    bind $base.fra77.btnbsub <Leave> {
        set imScilab(avimsg) $imScilab(avimainmsg)
        .avi.fra77.btnbsub configure -relief flat
    }
    button $base.fra77.btnavidemo \
        -height 50 -pady 0 -relief flat -text avidemo -width 50 \
        -image [imScilab:image:get_image "avidemo.gif"] \
        -command {avi_moddemo}
    bind $base.fra77.btnavidemo <Enter> {
        set imScilab(avimsg) "Demo-Moving maritime Objects detection"
        .avi.fra77.btnavidemo configure -relief raise
    }
    bind $base.fra77.btnavidemo <Leave> {
        set imScilab(avimsg) $imScilab(avimainmsg)
        .avi.fra77.btnavidemo configure -relief flat
    }


### aviframe_sub    
    frame $base.fra88 \
        -borderwidth 2 -relief groove -height 240 -width 320  
    label $base.fra88.lab89 \
        -background #ffffff -height 0 -relief sunken -text {aviframe_sub preview} \
        -width 278
    bind $base.fra88 <Expose> {
    avi_expose_sub }
    
    frame $base.fra31 \
    	-borderwidth 2 -width 320 -relief groove
    button $base.fra31.btnaviconfirm \
    	-height 20 -text confirm -width 80 -relief flat -pady 0 \
        -command {avi_confirm} -image [imScilab:image:get_image "confirm.gif"]
    bind $base.fra31.btnaviconfirm <Enter> {
        .avi.fra31.btnaviconfirm configure -relief raise
        set imScilab(avimsg) "Confirm special avi process"
    }
    bind $base.fra31.btnaviconfirm <Leave> {
        .avi.fra31.btnaviconfirm configure -relief flat
        set imScilab(avimsg) $imScilab(avimainmsg)
    }

# aviframe_bsub
    label $base.fra31.lab12 \
    	-relief flat -text {input threshold} \
    	-width 50
    entry $base.fra31.ent13 \
    	-background white -textvariable "imScilab(avi,bsub,threshold)" -width 10
    
    
    frame $base.fra21 \
        -borderwidth 2 -relief groove -height 55 -width 280
    scale $base.fra21.sca22 \
        -bigincrement 0.0 -cursor fleur -from 0 -orient horizontal \
        -resolution 1 -tickinterval 0.0 -width 273
    entry $base.fra21.ent23 \
        -background white -textvariable imScilab(aviframeID_bsub) -width 10
    
    button $base.fra21.btnchoosebg \
        -height 10 -text choosebg -width 53 -relief groove -pady 0 \
        -command {ScilabEval "aviframe_sub_bsub=AviReadFrame(ppavi, $imScilab(aviframeID_bsub));$imScilab(avibsubprocess)impreview(avilabid_sub,aviframe_sub_bsub,320,240);"} \
        -state disable
    bind $base.fra21.btnchoosebg <Enter> {
        .avi.fra21.btnchoosebg configure -relief raise
        set imScilab(avimsg) "Choose background for subtrcion"
    }
    bind $base.fra21.btnchoosebg <Leave> {
        .avi.fra21.btnchoosebg configure -relief groove
        set imScilab(avimsg) $imScilab(avimainmsg)
    }

# aviframe_tdiff
    frame $base.fra41 \
    	-borderwidth 2 -relief groove -height 25 -width 320
    label $base.fra41.lab42 \
    	-relief flat -text {input interval(frame(s))} -width 110
    menubutton $base.fra41.btnInterval \
        -borderwidth 2 -menu "$base.fra41.btnInterval.m" \
        -relief groove -height 20 -width 9 -textvariable imScilab(avi,tdiff,interval)
    bind $base.fra41.btnInterval <Enter> {
        .avi.fra41.btnInterval configure -relief raise
         set imScilab(avimsg) "Choose Temporal Differencing interval"
    }
    bind $base.fra41.btnInterval <Leave> {
        .avi.fra41.btnInterval configure -relief groove
    	set imScilab(avimsg) $imScilab(avimainmsg)
    }
    menu $base.fra41.btnInterval.m \
        -activeborderwidth 1 -borderwidth 2 -font {times 10 bold} -tearoff 0 
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 1}  -label 1
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 2} -label 2   
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 3} -label 3
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 4} -label 4
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 5} -label 5
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 6} -label 6
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 7} -label 7
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 8} -label 8
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 9} -label 9
    $base.fra41.btnInterval.m add command \
        -command {avi_btnInterval 10} -label 10
        
        
    frame $base.fra51 \
    	-borderwidth 2 -relief groove -height 25 -width 320
    label $base.fra51.lab52 \
    	-relief flat -text {input threshold} -width 50
    entry $base.fra51.ent53 \
    	-background white -textvariable "imScilab(avi,tdiff,threshold)" -width 10 -justify center
    	
    ###################
    # SETTING GEOMETRY
    ###################
     place $base.fra38 \
        -in $base -x 100 -y 270 -width 320 -height 55 -anchor nw \
        -bordermode ignore 
    place $base.fra38.sca62 \
        -in $base.fra38 -x 5 -y 19 -width 310 -height 32 -anchor nw \
        -bordermode ignore 
    place $base.fra38.btnframechoose \
        -in $base.fra38 -x 2 -y 3 -width 53 -height 20 -anchor nw \
        -bordermode ignore
    pack $base.fra38.ent66 \
    	-in $base.fra38 -anchor ne -expand 0 -side right -fill none -pady 2

    place $base.fra46 \
        -in $base -x 100 -y 25 -width 320 -height 240 -anchor nw \
        -bordermode ignore 
    place $base.fra46.lab47 \
        -in $base.fra46 -x 0 -y 0 -width 320 -height 240 -anchor nw \
        -bordermode ignore
    
        
    place $base.mes65 \
        -in $base -x 101 -y 3 -width 320 -height 21 -anchor nw \
        -bordermode ignore 
    
    place $base.fra69 \
        -in $base -x 5 -y 25 -width 90 -height 245 -anchor nw \
        -bordermode ignore 
    place $base.fra69.btnaviopen \
        -in $base.fra69 -x 20 -y 5 -width 50 -height 50 -anchor nw \
        -bordermode ignore 
    place $base.fra69.btnavisave \
        -in $base.fra69 -x 20 -y 65 -width 50 -height 50 -anchor nw \
        -bordermode ignore 
    place $base.fra69.btnaviplay \
        -in $base.fra69 -x 20 -y 127 -width 50 -height 50 -anchor nw \
        -bordermode ignore 
    place $base.fra69.btnaviclose \
        -in $base.fra69 -x 20 -y 190 -width 50 -height 50 -anchor nw \
        -bordermode ignore 
    
    place $base.fra77 \
        -in $base -x 425 -y 25 -width 80 -height 240 -anchor nw \
        -bordermode ignore 
    place $base.fra77.btntdiff \
        -in $base.fra77 -x 15 -y 23 -width 50 -height 50 -anchor nw \
        -bordermode ignore
    place $base.fra77.btnbsub \
        -in $base.fra77 -x 15 -y 97 -width 50 -height 50 -anchor nw \
        -bordermode ignore
    place $base.fra77.btnavidemo \
        -in $base.fra77 -x 15 -y 170 -width 50 -height 50 -anchor nw \
        -bordermode ignore
    
#### aviframe_sub    
    place $base.fra88 \
        -in $base -x 511 -y 25 -width 320 -height 240 -anchor nw \
        -bordermode ignore 
    place $base.fra88.lab89 \
        -in $base.fra88 -x 0 -y 0 -width 320 -height 240 -anchor nw \
        -bordermode ignore
        
                
    imScilab:FireEvent $base <<Ready>>
    	
}

Window show .
Window show .avi

imScilab:avi_main


