############################
# 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:im2bw_Close} {} {
global imScilab
	set base .im2bw
	destroy $base
}

proc {imScilab:im2bw_setDefault} {} {
global imScilab
set imScilab(im2bw,par,level) 0.500
}

proc {imScilab:im2bw_callHelp} {} {
global imScilab
	ScilabEval "help im2bw;"
}

proc {imScilab:im2bw_getResult} {image map} {
global imScilab
set imScilab(im2bw,out) $imScilab(im2bw,FuncStyle)\(
	append imScilab(im2bw,out) $image\,$map

	append imScilab(im2bw,out) \,$imScilab(im2bw,par,level)
	append imScilab(im2bw,out) \)
}



proc {imScilab:im2bw_doOk_son} {image map i } {
global imScilab
        imScilab:im2bw_getResult $image $map
        set imScilab(firstprev$i) 0
        set imScilab(changeID$i) [expr $imScilab(changeID$i)+1]
        ScilabEval "                
        		
        		xset('window',$i);$image=$imScilab(im2bw,out); $map=2; imshow($image,$map); imagePush($i,$imScilab(changeID$i)); "
	set imScilab(image$i,storagetype) "binary"
	set imScilab(image$i,typestack$imScilab(changeID$i)) "binary"
	imScilab:image_change $i
}



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

proc {imScilab:im2bw_Insert_son} {image map} {
global imScilab
imScilab:im2bw_getResult $image $map
	set imScilab(im2bw,outputCode) $image=
	append imScilab(im2bw,outputCode) $imScilab(im2bw,out)\;
	append imScilab(im2bw,outputCode) imshow($image,2)\;
	import_data $imScilab(im2bw,outputCode)
}

##################	AVI Filter
proc {imScilab:im2bw_avi_ok} {} {
global imScilab
	imScilab:im2bw_getResult aviframe_sub_bsub aviframe_sub_bsub_map
	append imScilab(avibsubprocess) aviframe_sub_bsub=$imScilab(im2bw,out)\;
	imScilab:im2bw_getResult aviframe_sub_tdiff aviframe_sub_tdiff_map
	append imScilab(avitdiffprocess) aviframe_sub_tdiff=$imScilab(im2bw,out)\;
	imScilab:im2bw_getResult aviframe avimap
	append imScilab(aviprocess) aviframe=$imScilab(im2bw,out)\;
	ScilabEval "                                                       
	aviframe=$imScilab(im2bw,out);temp=aviframe;impreview(labid,temp,290,290);aviframe_temp=aviframe;impreview(avilabid,aviframe_temp,320,240);"
	set imScilab(aviframe,storagetype) "binary"
	set imScilab(aviframe,typestack$imScilab(changeID$i)) "binary"
	imScilab:avi_change
}

proc {imScilab:im2bw_avi_apply} {} {
global imScilab
	imScilab:im2bw_getResult aviframe avimap
	ScilabEval "temp=$imScilab(im2bw,out);labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);"
}

##################	
	
########	multi process
proc {imScilab:im2bw_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 1} {
		imScilab:im2bw_doOk_son image$i map$i $i }
	} else {
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:im2bw_doOk_son image0 map0 0 }
		image1 { imScilab:im2bw_doOk_son image1 map1 1 }
		image2 { imScilab:im2bw_doOk_son image2 map2 2 }
		image3 { imScilab:im2bw_doOk_son image3 map3 3 }
		AviFrame { imScilab:im2bw_avi_ok }
		} 	 
	}
}

proc {imScilab:im2bw_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 {
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:im2bw_doApply_son image0 map0 0 }
		image1 { imScilab:im2bw_doApply_son image1 map1 1 }
		image2 { imScilab:im2bw_doApply_son image2 map2 2 }
		image3 { imScilab:im2bw_doApply_son image3 map3 3 }
		AviFrame { imScilab:im2bw_avi_apply }
		} 	 
	}
}

proc {imScilab:im2bw_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 1} {
		imScilab:im2bw_Insert_son image$i map$i }
	} else {
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:im2bw_Insert_son image0 map0 }
		image1 { imScilab:im2bw_Insert_son image1 map1 }
		image2 { imScilab:im2bw_Insert_son image2 map2 }
		image3 { imScilab:im2bw_Insert_son image3 map3 }
		AviFrame { imScilab:im2bw_Insert_son aviframe avimap }
		} 	 
	}
}

proc {imScilab:im2bw_expose_sub} {firstpreview} {
global imScilab
	set imScilab(labid) [winfo id .im2bw.fra34.lab34]
	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:im2bw_expose} {} {
global imScilab
	set imScilab(labid) [winfo id .im2bw.fra34.lab34]
	switch -exact $imScilab(imageChoose) {
		image0 { imScilab:im2bw_expose_sub $imScilab(firstprev0) }
		image1 { imScilab:im2bw_expose_sub $imScilab(firstprev1) }
		image2 { imScilab:im2bw_expose_sub $imScilab(firstprev2) }
		image3 { imScilab:im2bw_expose_sub $imScilab(firstprev3) }
		AviFrame { ScilabEval "                                                                                     
        labid=TK_GetVar('imScilab(labid)');impreview(labid,temp,290,290);" }
		} 	 
}

########


###########################################################
## Procedure:  init
proc {imScilab:im2bw_init} {} {
global imScilab
set imScilab(im2bw,FuncStyle) im2bw
set imScilab(im2bw,par,level) 0.500
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:im2bw_init


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

proc {imScilab:im2bw_main} {} {
::::vBalloon::imScilab:set_balloon .im2bw.fra39.fra50.but36 "Insert"
::::vBalloon::imScilab:set_balloon .im2bw.fra39.fra50.but37 "Default"
::::vBalloon::imScilab:set_balloon .im2bw.fra39.fra50.but38 "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.im2bw {base {container 0}} {
    global imScilab
    if {$base == ""} {
        set base .im2bw
    } 
    if {[winfo exists $base] && (!$container)} {
        wm deiconify $base; return
    }

    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    toplevel $base -class Toplevel \
        -relief ridge
    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 "IM2BW"
  
    }
        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);" ;imScilab:im2bw_expose} -label image0 -state $imScilab(imageState0)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image1 ; set imScilab(currimage) 1 ; ScilabEval "temp=$imScilab(imageChoose);";imScilab:im2bw_expose} -label image1 -state $imScilab(imageState1)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image2 ; set imScilab(currimage) 2 ; ScilabEval "temp=$imScilab(imageChoose);";imScilab:im2bw_expose} -label image2 -state $imScilab(imageState2)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) image3 ; set imScilab(currimage) 3 ; ScilabEval "temp=$imScilab(imageChoose);";imScilab:im2bw_expose} -label image3 -state $imScilab(imageState3)
    $base.men28.m add command \
        -command { set imScilab(imageChoose) AviFrame ; ScilabEval "temp=aviframe;";imScilab:im2bw_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 290 -width 290 -background red
    label $base.fra34.lab34 \
        -borderwidth 2 -relief sunken -height 290 -width 290 -background white \
        -image [imScilab:image:get_image "temp.gif" ]
    bind $base.fra34 <Expose> {
    	imScilab:im2bw_expose
        }
    button $base.but30 \
        -height 31 -pady 0 -text OK -width 84 \
        -command imScilab:im2bw_doOk 
    button $base.but36 \
        -height 31 -pady 0 -text Exit -width 84 \
        -command imScilab:im2bw_Close
    button $base.but37 \
        -height 31 -pady 0 -text Preview -width 84 \
        -command imScilab:im2bw_doApply 
              
    frame $base.fra23 \
        -borderwidth 2 -height 180 -width 290 -relief flat  
    scale $base.fra23.sca28 \
        -bigincrement 0.0 -from 0.0 -orient horizontal -resolution 0.001 -troughcolor #b0c2a7 \
        -tickinterval 0.0 -to 1.0 -variable imScilab(im2bw,par,level) -width 290           
    label $base.fra23.lab42 \
        -height 0 -text level -width 33
    entry $base.fra23.ent43 \
        -background white -textvariable imScilab(im2bw,par,level) -width 10 
    
    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.fra49 \
    	-borderwidth 2 -relief flat -height 30 -width 250
    message $base.fra39.fra49.mes26 \
        -text {convert images to binary by thresholding} -width 206 -foreground #575c8c \
        -textvariable imScilab(im2bw,HelpMsg)
   
    frame $base.fra39.fra50 \
        -borderwidth 2 -height 30 -width 94
###	insert
    button $base.fra39.fra50.but36 \
        -borderwidth 0 \
        -command imScilab:im2bw_Insert \
        -image [imScilab:image:get_image "insert_g.gif"] \
        -relief groove \
        -text insert
    bind $base.fra39.fra50.but36 <Enter> {
        .im2bw.fra39.fra50.but36 configure -borderwidth 2 \
        -image [imScilab:image:get_image "insert.gif"]
           set imScilab(im2bw,HelpMsg) "Insert to the codeEditor"
    }
    bind $base.fra39.fra50.but36 <Leave> {
        .im2bw.fra39.fra50.but36 configure -borderwidth 0 \
        -image [imScilab:image:get_image "insert_g.gif"]
    set imScilab(im2bw,HelpMsg) "Convert images to Binary by thresholding"
    }
###	default
    button $base.fra39.fra50.but37 \
        -borderwidth 0 \
        -image [imScilab:image:get_image "default_g.gif"] \
        -command imScilab:im2bw_setDefault \
        -relief groove \
        -text default
    bind $base.fra39.fra50.but37 <Enter> {
        .im2bw.fra39.fra50.but37 configure -borderwidth 2 \
        -image [imScilab:image:get_image "default.gif"]
            set imScilab(im2bw,HelpMsg) "Set default data"
    }
    bind $base.fra39.fra50.but37 <Leave> {
        .im2bw.fra39.fra50.but37 configure -borderwidth 0 \
        -image [imScilab:image:get_image "default_g.gif"]
    set imScilab(im2bw,HelpMsg) "Convert images to Binary by thresholding"
    }
###	help
    button $base.fra39.fra50.but38 \
        -borderwidth 0 \
        -command imScilab:im2bw_callHelp \
        -image [imScilab:image:get_image "help_g.gif"] \
        -relief groove \
        -text help
    bind $base.fra39.fra50.but38 <Enter> {
        .im2bw.fra39.fra50.but38 configure -borderwidth 2 \
        -image [imScilab:image:get_image "help.gif"]
            set imScilab(im2bw,HelpMsg) "Help about im2bw"
    }
    bind $base.fra39.fra50.but38 <Leave> {
        .im2bw.fra39.fra50.but38 configure -borderwidth 0 \
        -image [imScilab:image:get_image "help_g.gif"]
    set imScilab(im2bw,HelpMsg) "Convert images to Binary by thresholding"
    }
    
        
    ###################
    # SETTING GEOMETRY
    ###################
    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.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 
    place $base.but37 \
        -in $base -x 315 -y 80 -width 84 -height 31 -anchor nw \
        -bordermode ignore
        
    place $base.men28 \
        -in $base -x 315 -y 115 -width 84 -height 26 -anchor nw \
        -bordermode ignore    
  
    place $base.fra23 \
        -in $base -x 15 -y 310 -width 290 -height 180 -anchor nw \
        -bordermode ignore    
    place $base.fra23.sca28 \
        -in $base.fra23 -x 3 -y 50 -width 285 -height 35 -anchor nw \
        -bordermode ignore          
    place $base.fra23.lab42 \
        -in $base.fra23 -x 1 -y 20 -width 56 -height 21 -anchor nw \
        -bordermode ignore 
    pack $base.fra23.ent43 \
        -in $base.fra23 -anchor ne -expand 0 -side right -fill none -pady 20    
    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.fra49 \
    	-in $base.fra39 -x 75 -y 17 -width 235 -height 30 -anchor w \
        -bordermode ignore
###
    pack $base.fra39.fra49.mes26 \
        -in $base.fra39.fra49 -anchor center -expand 1 -fill both \
        -side top        
    pack $base.fra39.fra50\
        -in $base.fra39 -anchor center -expand 0 -fill x -side right 
    place $base.fra39.fra50.but36 \
        -x 2 -y 0 -width 30 -height 30 -anchor nw -bordermode ignore 
    place $base.fra39.fra50.but37 \
        -x 32 -y 0 -width 30 -height 30 -anchor nw -bordermode ignore 
    place $base.fra39.fra50.but38 \
        -x 62 -y 0 -width 30 -height 30 -anchor nw -bordermode ignore 

}

Window show .
Window show .im2bw

imScilab:im2bw_main 
