############################
# code to load user images

imScilab:image:create_new_image "insert.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "insert_g.gif" \
    "user image" \
    "user"    
imScilab:image:create_new_image "default.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "default_g.gif" \
    "user image" \
    "user"        
imScilab:image:create_new_image "help.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "help_g.gif" \
    "user image" \
    "user"
imScilab:image:create_new_image "temp.gif" \
    "user image" \
    "user"    
imScilab:image:create_new_image "vip_banner_s.gif" \
    "user image" \
    "user"
#################################
# USER DEFINED PROCEDURES
#

proc {imScilab:edge_Close} {} {
global imScilab
	set base .edge
	destroy $base
}

proc {imScilab:edge_setDefault} {} {
global imScilab
set imScilab(edge,par,method) 'sobel'
set imScilab(edge,par,thresh) "0.5"
set imScilab(edge,par,direction) 'both'
}

proc {imScilab:edge_callHelp} {} {
global widget 
global imScilab
	ScilabEval "help edge;"
}

proc {imScilab:edge_getResult} {image} {
global imScilab
set imScilab(edge,out) $imScilab(edge,FuncStyle)\(
	append imScilab(edge,out) $image	
	foreach parameter {method thresh direction} {
		if { $imScilab(edge,par,$parameter) != {} } {
			append imScilab(edge,out) \,$parameter=$imScilab(edge,par,$parameter)	
		}
	}	
	append imScilab(edge,out) \)
}

proc {imScilab:edge_doOk_son} {image map i} {
global imScilab
        imScilab:edge_getResult $image
        set imScilab(firstprev$i) 0        
	set imScilab(changeID$i) [expr $imScilab(changeID$i)+1]       
        ScilabEval "                                 
                                                              
                  	xset('window',$i); xbasc(); $image=$imScilab(edge,out); $map=2; imshow($image,$map); imagePush($i,$imScilab(changeID$i)); "
	imScilab:image_change $i          	
}

proc {imScilab:edge_doApply_son} {image map i} {
global imScilab
        imScilab:edge_getResult $image
        set imScilab(firstprev$i) 0
        ScilabEval "                        
               		temp=$imScilab(edge,out);labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);"
}

proc {imScilab:edge_insert_son} {image map} {
global imScilab
imScilab:edge_getResult $image
	set imScilab(edge,outputCode) $image=
	append imScilab(edge,outputCode) $imScilab(edge,out)\;
	append imScilab(edge,outputCode) imshow($image,2)\;
	import_data $imScilab(edge,outputCode)
}

##################	AVI Filter
proc {imScilab:edge_avi_ok} {} {
global imScilab
	imScilab:edge_getResult aviframe_sub_bsub
	append imScilab(avibsubprocess) aviframe_sub_bsub=$imScilab(edge,out)\;
	imScilab:edge_getResult aviframe_sub_tdiff
	append imScilab(avitdiffprocess) aviframe_sub_tdiff=$imScilab(edge,out)\;
	imScilab:edge_getResult aviframe
	append imScilab(aviprocess) aviframe=$imScilab(edge,out)\;
	ScilabEval "                                                       
	aviframe=$imScilab(edge,out);temp=aviframe;impreview(labid,temp,290,290);aviframe_temp=aviframe;impreview(avilabid,aviframe,315,230);"
	imScilab:avi_change
}

proc {imScilab:edge_avi_apply} {} {
global imScilab
	imScilab:edge_getResult aviframe
	ScilabEval "temp=$imScilab(edge,out);labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);"
}	
##################

########	multi process
proc {imScilab:edge_doOk} {} {
global imScilab
	if {$imScilab(imagepath$imScilab(currimage)) == "" && $imScilab(aviprev) == 0} {
		tk_messageBox -message "WARNNING:NO IMAGE TO PROCESS!!"
		return 0
	}
	if {$imScilab(imageChoose) == "all" } {
		for {set i 0} {$i<$imScilab(imageNum)+1} {incr i} {
		if {$imScilab(image$i,storagetype) == "truecolor" || $imScilab(image$i,storagetype) == "indexed" } {
			continue;
		}
			imScilab:edge_doOk_son image$i map$i $i }
	} else {
		if {$imScilab(image$imScilab(currimage),storagetype) == "truecolor" || $imScilab(image$imScilab(currimage),storagetype) == "indexed" } {
			tk_messageBox -message "Process for binary image or grayscale image only!"
			return 0
		}
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:edge_doOk_son image0 map0 0 }
		image1 { imScilab:edge_doOk_son image1 map1 1 }
		image2 { imScilab:edge_doOk_son image2 map2 2 }
		image3 { imScilab:edge_doOk_son image3 map3 3 }	
		AviFrame { imScilab:edge_avi_ok }
		} 	 
	}
	imScilab:image_change
}

proc {imScilab:edge_doApply} {} {
global imScilab
	if {$imScilab(imagepath$imScilab(currimage)) == "" && $imScilab(aviprev) == 0} {
		tk_messageBox -message "WARNNING:NO IMAGE TO PROCESS!!"
		return 0
	}
	if {$imScilab(imageChoose) == "all" } {
		tk_messageBox -message "Please choose a certain image for preview"
	} else {
		if {$imScilab(image$imScilab(currimage),storagetype) == "truecolor" || $imScilab(image$imScilab(currimage),storagetype) == "indexed" } {
			tk_messageBox -message "Process for binary image or grayscale image only!"
			return 0
		}
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:edge_doApply_son image0 map0 0 }
		image1 { imScilab:edge_doApply_son image1 map1 1 }
		image2 { imScilab:edge_doApply_son image2 map2 2 }
		image3 { imScilab:edge_doApply_son image3 map3 3 }
		AviFrame { imScilab:edge_avi_apply }	
		} 	 
	}
}

proc {imScilab:edge_Insert} {} {
global imScilab
	if {$imScilab(imagepath$imScilab(currimage)) == "" && $imScilab(aviprev) == 0} {
		tk_messageBox -message "WARNNING:NO IMAGE TO PROCESS!!"
		return 0
	}
	if {$imScilab(imageChoose) == "all" } {
		for {set i 0} {$i<$imScilab(imageNum)+1} {incr i} {
			imScilab:edge_Insert_son image$i map$i}
	} else {
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:edge_Insert_son image0 map0 }
		image1 { imScilab:edge_Insert_son image1 map1 }
		image2 { imScilab:edge_Insert_son image2 map2 }
		image3 { imScilab:edge_Insert_son image3 map3 }	
		AviFrame { imScilab:edge_getResult aviframe ; import_data aviframe=$imScilab(edge,out)\n }
		} 	 
	}
}
########
proc {imScilab:edge_expose_sub} {firstpreview} {
global imScilab
	set imScilab(labid) [winfo id .edge.fra34]
	if {$firstpreview == 0} {
    	ScilabEval "                                                                                     
        labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);"
    	} else {
    	if {$imScilab($imScilab(imageChoose),storagetype)!="indexed"} {
        ScilabEval "                                                                                     
        labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);"
        } else {
        ScilabEval "                                                                                     
        labid=TK_GetVar('imScilab(labid)');temp=ind2rgb(image$imScilab(currimage),map$imScilab(currimage));impreview(labid,temp,290,290);" }
        }
}

proc {imScilab:edge_expose} {} {
global imScilab
	set imScilab(labid) [winfo id .edge.fra34]
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:edge_expose_sub $imScilab(firstprev0) }
		image1 { imScilab:edge_expose_sub $imScilab(firstprev1) }
		image2 { imScilab:edge_expose_sub $imScilab(firstprev2) }
		image3 { imScilab:edge_expose_sub $imScilab(firstprev3) }
		AviFrame { ScilabEval "                                                                                     
        labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);" }
		} 	 
}

###########################################################
## Procedure:  init

proc {imScilab:edge_init} {} {
global imScilab
set imScilab(edge,FuncStyle) edge
set imScilab(edge,par,method) ""
set imScilab(edge,par,thresh) 0.5  
set imScilab(edge,par,direction) ""
if {$imScilab(imageChoose) == "AviFrame"} {
	ScilabEval "                                                                                   
	        temp=aviframe;"
	return
	}
if {$imScilab(imagepath$imScilab(currimage)) != "" } {
		ScilabEval "                                                                           
		temp=$imScilab(imageChoose);" 
	}
for {set i 0} {$i<4} {incr i 1} {
	set imScilab(firstprev$i) 1
	}
}

imScilab:edge_init

###########################################################
## Procedure:  main

proc {imScilab:edge_main} {} {
::::vBalloon::imScilab:set_balloon .edge.fra39.fra28.but29 "Insert"
::::vBalloon::imScilab:set_balloon .edge.fra39.fra28.but30 "Default"
::::vBalloon::imScilab:set_balloon .edge.fra39.fra28.but31 "Help"
}

proc {Window} {args} {
global vTcl
    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
    }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists == "1" && $name != "."} {wm deiconify $name; return}
            if {[info procs vTclWindow(pre)$name] != ""} {
                eval "vTclWindow(pre)$name $newname $rest"
            }
            if {[info procs vTclWindow$name] != ""} {
                eval "vTclWindow$name $newname $rest"
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                eval "vTclWindow(post)$name $newname $rest"
            }
        }
        hide    { if $exists {wm withdraw $newname; return} }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

#################################
# VTCL GENERATED GUI PROCEDURES
#

proc vTclWindow. {base {container 0}} {
    if {$base == ""} {
        set base .
    }
    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    wm focusmodel $base passive
    wm geometry $base 200x200+132+174; update
    wm maxsize $base 1028 746
    wm minsize $base 115 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm withdraw $base
    wm title $base "vtcl"
    bindtags $base "$base Vtcl all"
    }
    ###################
    # SETTING GEOMETRY
    ###################
}

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

    global widget

    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    toplevel $base -class Toplevel \
        -relief ridge -height 530 -width 410 
    wm transient $base .main
    wm focusmodel $base passive
    wm geometry $base 410x530+500+146; update
    wm maxsize $base 410 530
    wm minsize $base 115 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm deiconify $base
    wm title $base "edge"
 
    }
        menubutton $base.men28 \
        -background white -height 26 -menu "$base.men28.m" -padx 5 -pady 4 \
        -relief sunken -text all -width 100 -textvariable imScilab(imageChoose)     
    menu $base.men28.m \
        -activeborderwidth 1 -borderwidth 2 -font {times 10 bold} -tearoff 0 
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image0 ; set imScilab(currimage) 0 ; ScilabEval "temp=$imScilab(imageChoose);" ;set imScilab(firstprev0) 1 ;imScilab:edge_expose} -label image0 -state $imScilab(imageState0)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image1 ; set imScilab(currimage) 1 ; ScilabEval "temp=$imScilab(imageChoose);";set imScilab(firstprev1) 1 ;imScilab:edge_expose} -label image1 -state $imScilab(imageState1)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image2 ; set imScilab(currimage) 2 ; ScilabEval "temp=$imScilab(imageChoose);";set imScilab(firstprev2) 1 ;imScilab:edge_expose} -label image2 -state $imScilab(imageState2)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image3 ; set imScilab(currimage) 3 ; ScilabEval "temp=$imScilab(imageChoose);";set imScilab(firstprev3) 1 ;imScilab:edge_expose} -label image3 -state $imScilab(imageState3)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) AviFrame ; set imScilab(currimage) 4 ;ScilabEval "temp=aviframe;";imScilab:edge_expose} -label AviFrame -state $imScilab(aviState)    
    $base.men28.m add command \
        -command { set imScilab(imageChoose) all } -label all 

    frame $base.fra34 \
    	-borderwidth 2 -relief sunken -height 275 -width 290 -background red           
    label $base.fra34.lab34 \
        -background white -relief sunken -text label -width 290 -height 275 \
        -image [imScilab:image:get_image "temp.gif" ] 
    bind $base.fra34 <Expose> {
    	imScilab:edge_expose
        }              
    button $base.but30 \
        -height 31 -pady 0 -text OK -width 84 \
        -command imScilab:edge_doOk
    button $base.but36 \
        -height 31 -pady 0 -text Exit -width 84 \
        -command imScilab:edge_Close
    button $base.but25 \
        -height 31 -pady 0 -text Preview -width 84 \
        -command imScilab:edge_doApply
    message $base.mes27 \
        -text "" -textvariable ImgSize -width 36
        
    frame $base.fra39 \
        -borderwidth 2 -relief groove -height 35 -width 390 
    label $base.fra39.lab40 \
        -height 0 -text VIP -width 65 -image [imScilab:image:get_image "vip_banner_s.gif"]    
    frame $base.fra39.fra26 \
        -borderwidth 2 -height 30 -width 296 
    message $base.fra39.fra26.mes27 \
        -text { edge detection } -width 236 -foreground #575c8c \
        -textvariable imScilab(edge,HelpMsg) 
        
    frame $base.fra39.fra28 \
        -borderwidth 2 -relief flat -height 30 -width 94 
###   insert
    button $base.fra39.fra28.but29 \
        -borderwidth 0 -relief groove -text insert \
        -command imScilab:edge_insert \
        -image [imScilab:image:get_image "insert_g.gif"] 
    bind $base.fra39.fra28.but29 <Enter> {
        .edge.fra39.fra28.but29 configure -borderwidth 2 \
        -image [imScilab:image:get_image "insert.gif"]
           set imScilab(edge,HelpMsg) "Insert to the codeEditor"
    }
    bind $base.fra39.fra28.but29 <Leave> {
        .edge.fra39.fra28.but29 configure -borderwidth 0 \
        -image [imScilab:image:get_image "insert_g.gif"]
           set imScilab(edge,HelpMsg) " edge detection "
    }          
###   default         
    button $base.fra39.fra28.but30 \
        -borderwidth 0 -relief groove -text default \
        -image [imScilab:image:get_image "default_g.gif"] \
        -command imScilab:edge_setDefault 
    bind $base.fra39.fra28.but30 <Enter> {
        .edge.fra39.fra28.but30 configure -borderwidth 2 \
        -image [imScilab:image:get_image "default.gif"]
           set imScilab(edge,HelpMsg) "Set default data"
    }
    bind $base.fra39.fra28.but30 <Leave> {
        .edge.fra39.fra28.but30 configure -borderwidth 0 \
        -image [imScilab:image:get_image "default_g.gif"]
           set imScilab(edge,HelpMsg) " edge detection "
    }         
###   help                
    button $base.fra39.fra28.but31 \
        -borderwidth 0 -relief groove -text help \
        -command imScilab:edge_callHelp \
        -image [imScilab:image:get_image "help_g.gif"]
    bind $base.fra39.fra28.but31 <Enter> {
        .edge.fra39.fra28.but31 configure -borderwidth 2 \
        -image [imScilab:image:get_image "help.gif"]
            set imScilab(edge,HelpMsg) "Help about edge"
    }
    bind $base.fra39.fra28.but31 <Leave> {
        .edge.fra39.fra28.but31 configure -borderwidth 0 \
        -image [imScilab:image:get_image "help_g.gif"]
           set imScilab(edge,HelpMsg) " edge detection "
    }  

##### #####                         
    frame $base.fra23 \
        -borderwidth 2 -relief flat -height 125 -width 300 
    message $base.fra23.mes24 \
        -text method -width 66 -relief flat
    menubutton $base.fra23.men25 \
        -background white -height 20 -menu "$base.fra23.men25.m" -padx 5 \
        -pady 4 -relief sunken -text {} -width 60 -textvariable imScilab(edge,par,method)
    menu $base.fra23.men25.m \
        -activeborderwidth 1 -borderwidth 2 -tearoff 0
    $base.fra23.men25.m add command \
        -command { set imScilab(edge,par,method) 'sobel' } \
        -label sobel
    $base.fra23.men25.m add command \
        -command { set imScilab(edge,par,method) 'prewitt' } \
        -label prewitt
    if {!$imScilab(isWindows)} {
    $base.fra23.men25.m add command \
        -command { set imScilab(edge,par,method) 'fftderiv' } \
        -label fftderiv
    }
    $base.fra23.men25.m add command \
        -command { set imScilab(edge,par,method) {} } \
        -label None        
    bind $base.fra23.men25 <Enter> {
            set imScilab(edge,HelpMsg) " method parameter "
    }
    bind $base.fra23.men25 <Leave> {
           set imScilab(edge,HelpMsg) " edge detection "
    }          
    message $base.fra23.mes27 \
        -relief flat -text direction -width 66
    menubutton $base.fra23.men28 \
        -background white -height 20 -menu "$base.fra23.men28.m" -padx 5 \
        -pady 4 -relief sunken -text {} -width 60 -textvariable imScilab(edge,par,direction) 
    menu $base.fra23.men28.m \
        -activeborderwidth 1 -borderwidth 2 -tearoff 0 
    $base.fra23.men28.m add command \
        -command { set imScilab(edge,par,direction) 'horizontal' } \
        -label horizontal
    $base.fra23.men28.m add command \
        -command { set imScilab(edge,par,direction) 'vertical' } \
        -label vertical
    $base.fra23.men28.m add command \
        -command { set imScilab(edge,par,direction) 'both' } \
        -label both   
    $base.fra23.men28.m add command \
        -command { set imScilab(edge,par,direction) {} } \
        -label None             
    bind $base.fra23.men28 <Enter> {
           set imScilab(edge,HelpMsg) "The direction to compute the image gradient"
    }
    bind $base.fra23.men28 <Leave> {
           set imScilab(edge,HelpMsg) " edge detection "
    }  
    scale $base.fra23.sca27 \
        -bigincrement 0.0 -from -1.0 -to 1.0 -troughcolor #b0c2a7 \
        -orient horizontal -resolution 0.01 -sliderlength 20 -tickinterval 0.0 \
        -variable imScilab(edge,par,thresh) -width 218 
    message $base.fra23.mes28 \
        -text thresh: -width 58         
    ###################
    # SETTING GEOMETRY
    ###################
    place $base.but30 \
        -in $base -x 315 -y 10 -width 84 -height 31 -anchor nw \
        -bordermode ignore 
    place $base.but36 \
        -in $base -x 315 -y 45 -width 84 -height 31 -anchor nw \
        -bordermode ignore 
    pack $base.fra39 \
        -in $base -anchor s -expand 1 -fill x -side bottom 
    place $base.fra39.lab40 \
        -in $base.fra39 -x 10 -y 16 -width 65 -height 30 -anchor w \
        -bordermode ignore
    place $base.fra39.fra26 \
    	-in $base.fra39 -x 75 -y 17 -width 235 -height 30 -anchor w \
        -bordermode ignore
    pack $base.fra39.fra26.mes27 \
        -in $base.fra39.fra26 -anchor center -expand 1 -fill x -side top 
    pack $base.fra39.fra28 \
        -in $base.fra39 -anchor center -expand 0 -fill x -side right
    place $base.fra39.fra28.but29 \
        -in $base.fra39.fra28 -x 2 -y 0 -width 30 -height 30 -anchor nw \
        -bordermode ignore 
    place $base.fra39.fra28.but30 \
        -in $base.fra39.fra28 -x 32 -y 0 -width 30 -height 30 -anchor nw \
        -bordermode ignore 
    place $base.fra39.fra28.but31 \
        -in $base.fra39.fra28 -x 62 -y 0 -width 30 -height 30 -anchor nw \
        -bordermode ignore 
    place $base.mes27 \
        -in $base -x 145 -y 300 -width 36 -height 21 -anchor nw \
        -bordermode ignore 
    place $base.men28 \
        -in $base -x 315 -y 115 -width 84 -height 26 -anchor nw \
        -bordermode ignore                 
    place $base.but25 \
        -in $base -x 315 -y 80 -width 84 -height 31 -anchor nw \
        -bordermode ignore 
    place $base.fra34 \
        -in $base -x 15 -y 5 -width 290 -height 290 -anchor nw \
        -bordermode ignore 
    place $base.fra34.lab34 \
        -in $base.fra34 -width 290 -height 290 -anchor nw \
        -bordermode ignore 
###### #####       
    place $base.fra23 \
        -in $base -x 15 -y 340 -width 300 -height 125 -anchor nw \
        -bordermode ignore 
    place $base.fra23.mes24 \
        -in $base.fra23 -x 5 -y 25 -width 66 -height 21 -anchor nw \
        -bordermode ignore 
    place $base.fra23.men25 \
        -in $base.fra23 -x 68 -y 25 -width 60 -height 20 -anchor nw \
        -bordermode ignore 
    place $base.fra23.mes27 \
        -in $base.fra23 -x 150 -y 25 -width 66 -height 21 -anchor nw \
        -bordermode ignore 
    place $base.fra23.men28 \
        -in $base.fra23 -x 215 -y 25 -width 60 -height 20 -anchor nw \
        -bordermode ignore 
    place $base.fra23.sca27 \
        -in $base.fra23 -x 64 -y 55 -width 218 -height 39 -anchor nw \
        -bordermode ignore 
    place $base.fra23.mes28 \
        -in $base.fra23 -x 8 -y 75 -width 58 -height 21 -anchor nw \
        -bordermode ignore    

}

Window show .
Window show .edge

imScilab:edge_main 
