reference/ocr-new/multi_zone.tcl
author viric@llimona
Thu, 18 May 2006 23:12:51 +0200
changeset 0 6b8091ca909a
permissions -rw-r--r--
Init from working directory of svn repository.


set zoneWindow .zoning_window
set HorizMerge 70
set VertMerge  70

set region_list [list]
set ordered_region_list [list]

set prev_reg_id 0
set region_data(0,x_final) 0
set region_data(0,y_final) 0
set region_data(0,x_init) -40
set region_data(0,y_init) -20

set cur_xoffset 0
set cur_yoffset 0


proc multiZone_open { filename } {
    global HorizMerge VertMerge cur_xoffset curyoffset
    
    set cur_xoffset 0
    set cur_yoffset 0
# 1 means success
    global IMAGE_DISPLAY_WIN ZONING_SCALE_FACTOR DISPLAY_IMAGE XV xvprocess \
	    multiZone_display_open zoneWindow
    
    set open 1
    
    if { $multiZone_display_open == $open } {
	focus .zoning_window
    } else {
	
	toplevel .zoning_window
	init_zoning_display 
    }
	
        
#   puts stdout "Opening $filename"
    zoned_page_open $filename
# Scale image to display    
    init_ZONING_SCALE_FACTOR


#   puts stdout "Done putting into page structure"
    if { 1 }  {
	set display_height [expr $ZONING_SCALE_FACTOR * [get_page_height]]
	set display_width [expr $ZONING_SCALE_FACTOR * [get_page_width]]
	wm geometry .zoning_window [expr int($display_width) +20]x[expr int($display_height) + 150]+300+100


	if { $DISPLAY_IMAGE == $XV } {
	    set xvprocess [exec xv $filename &]
	    puts stdout "xvprocess $xvprocess"
	} else {
# use the canvas...
	DESKEW
	zone_message  "Displaying Image"
	DISPLAY_INTERVALS .zoning_window.work_space $ZONING_SCALE_FACTOR
	zone_message  "Determining Line boundaries"
	FIND_LINES
	zone_message  " "
	}
    } else {
	popup_image_failure_win
    }
       

}

proc init_ZONING_SCALE_FACTOR { } {
    global ZONING_SCALE_FACTOR

    set ZONING_SCALE_FACTOR [expr 800.0/[get_page_height]]
    set temp [expr 800.0/[get_page_width]]
    puts $ZONING_SCALE_FACTOR
    puts $temp
    if {[expr $temp] < [expr $ZONING_SCALE_FACTOR] } {
	set ZONING_SCALE_FACTOR $temp
    }
    if { $ZONING_SCALE_FACTOR > 1} {
	set ZONING_SCALE_FACTOR 1
    }

}    


proc init_zoning_display { } {
    global ZONING_SCALE_FACTOR BACKGROUND FOREGROUND scroll_inc zoneWindow
    global menu_bar_height button_bar_height SMALLFONT FONT

    wm geometry $zoneWindow 600x900+300+100
    wm title $zoneWindow "CalZoning"
    wm positionfrom $zoneWindow user
    wm minsize $zoneWindow 500 300
    $zoneWindow configure -background $BACKGROUND

 
    set canvas_width 1000
    set canvas_height 1000

   canvas $zoneWindow.work_space -bg white -xscrollcommand \
	  "$zoneWindow.xscroller set" -yscrollcommand \
	  "$zoneWindow.yscroller set" -xscrollincrement \
	   $scroll_inc -cursor {crosshair black gray}  \
	   -width $canvas_width -height $canvas_height 
# two scrollbars
    scrollbar $zoneWindow.xscroller -command "$zoneWindow.work_space xview" -orient horizontal -background $BACKGROUND
    scrollbar $zoneWindow.yscroller -command "$zoneWindow.work_space yview" -background $BACKGROUND

    frame $zoneWindow.menu_bar -height $menu_bar_height -relief raised -bd 2 -background $BACKGROUND
    init_zoning_menu_bar

    frame $zoneWindow.message_bar -height $button_bar_height \
	    -background $BACKGROUND -relief raised -bd 2
    message $zoneWindow.message_bar.m  -background \
	    $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT \
	    -justify center 
    
    pack $zoneWindow.menu_bar -side top -fill x
    pack $zoneWindow.message_bar -side top -fill x
    pack $zoneWindow.message_bar.m  -fill x -fill y
    
    pack $zoneWindow.xscroller -side bottom -fill x
    
    pack  $zoneWindow.yscroller -side right -fill y 
    pack $zoneWindow.work_space -side top -fill x -fill y
    $zoneWindow.work_space configure -scrollregion { 0 0 5000 5000 }
    bind $zoneWindow.work_space <Double-3> { 
	set curx [.main_window.display.work_space canvasx %x] 
	set cury [.main_window.display.work_space canvasy %y] 
	set_active_zone $curx $cury
    }

    initialize_region_grab 

}


proc max { a b } {
    if { [expr $a] > [expr $b] } {
	return $a
    } else {
	return $b
    }
}

proc init_zoning_menu_bar { } {

# this command initializes the zoning menu bar

    global BACKGROUND FOREGROUND FONT zoneWindow

    #Zoning
    menubutton $zoneWindow.menu_bar.zoning -text "Zoning"\
	    -menu $zoneWindow.menu_bar.zoning.menu -borderwidth\
	    2 -background $BACKGROUND -foreground $FOREGROUND -font $FONT
    menu $zoneWindow.menu_bar.zoning.menu -background $BACKGROUND \
	    -foreground $FOREGROUND -font $FONT
    $zoneWindow.menu_bar.zoning.menu add command -label "AutoZone..." \
	    -command popup_autozone_menu 
    $zoneWindow.menu_bar.zoning.menu add command -label "Destroy Zones" \
	    -command destroy_all_regions


    button $zoneWindow.menu_bar.scale -text "Scale Factor" \
	     -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND \
	     -relief flat \
	     -font $FONT -command popup_scale_menu

#    button $zoneWindow.menu_bar.recognize -text "Recognize All" \
#	     -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND \
#	     -relief flat \
#	     -font $FONT -command {puts "Recognize All"}

    button  $zoneWindow.menu_bar.help -text "Help"  -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND -font $FONT -relief flat \
	    -command { init_zoning_help .main_window.menu_bar.help }

   button $zoneWindow.menu_bar.close -text "Close" \
	     -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND \
	     -relief flat \
	     -font $FONT -command close_zoned_document
 
    pack $zoneWindow.menu_bar.zoning $zoneWindow.menu_bar.scale  \
	    $zoneWindow.menu_bar.close -side left -padx 1m -pady 1m 
    pack $zoneWindow.menu_bar.help -side right -padx 1m -pady 1m 

 }



proc popup_scale_menu { } {
# a little box for the user to change the horizontal and vertial merging
# parameters and initiate automatic zoning 
    
    global BACKGROUND FOREGROUND SMALLFONT FONT ZONING_SCALE_FACTOR SELECT
    if { [winfo exists .scale] } {
	focus .scale
	return
    }

    toplevel .scale -background $BACKGROUND
    wm geometry .scale 200x100+350+150
    message .scale.m -text "Scale Factor\n"  -background \
	    $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT \
	    -justify center -width 200

        set gm .scale
        set var ZONING_SCALE_FACTOR
    	set varWindow  [string tolower $var]
	frame $gm.$varWindow 
	label $gm.$varWindow.l -text "Scale Factor" -width 13 -fg $FOREGROUND -background $BACKGROUND -font $FONT -justify left
	entry $gm.$varWindow.set -width 4 -relief sunken -textvariable $var -fg $FOREGROUND -background $BACKGROUND -font $FONT -selectbackground $SELECT
        $gm.$varWindow.set icursor 0
        $gm.$varWindow.set select range 0 10
        bind $gm.$varWindow.set  <Return> scale_zoned_page
	pack $gm.$varWindow.l -side left 
 	pack $gm.$varWindow.set -side right 
	pack $gm.$varWindow  -side top

    frame .scale.buttons
    button .scale.buttons.ok -text OK -command scale_zoned_page -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
    button .scale.buttons.cancel -text Cancel -command {destroy .scale}\
	    -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
    pack .scale.buttons.ok .scale.buttons.cancel -side left -expand 1 \
	    -fill x 	

    pack  .scale.buttons\
	   -side top -fill x
    
    
    focus $gm.$varWindow.set
    
}
 


proc popup_autozone_menu { } {
# a little box for the user to change the horizontal and vertial merging
# parameters and initiate automatic zoning 
    
    global BACKGROUND FOREGROUND SMALLFONT FONT HorizMerge VertMerge
    if { [winfo exists .autozone] } {
	focus .autozone
	return
    }

    toplevel .autozone -background $BACKGROUND
    wm geometry .autozone 250x225+300+150
    message .autozone.m -text "Merging Parameters\n"  -background \
	    $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT \
	    -justify center -width 250
    scale .autozone.hmerge -from 0 -to 255 -variable HorizMerge \
	    -orient horizontal -label "Horizontal Merge" \
	    -background $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT
    scale .autozone.vmerge -from 0 -to 255 -variable VertMerge \
	    -orient horizontal -label "Vertical Merge" -background \
	    $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT
 

    frame .autozone.buttons
    button .autozone.buttons.ok -text OK -command autozone -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
    button .autozone.buttons.cancel -text Cancel -command {destroy .autozone}\
	    -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
    pack .autozone.buttons.ok .autozone.buttons.cancel -side left -expand 1 \
	    -fill x 	
 
   pack .autozone.m .autozone.hmerge .autozone.vmerge .autozone.buttons \
	   -side top -fill x
}


proc autozone { } {
    global HorizMerge VertMerge zoneWindow

    zone_message "Zoning Document"
    destroy_all_regions
    AUTO_ZONE $HorizMerge $VertMerge
    destroy .autozone
}

proc zone_message { msg } {
    global zoneWindow
    $zoneWindow.message_bar.m configure -text $msg \
	 -width 500 -justify center
}

set x_init 0
set y_init 0
set x_final 0
set y_final 0

set prev_region_id 0


set started_region 0
set region_count 0

proc initialize_region_grab { } {
global window
#
#
# facilitates the grabbing of a rectangle of the window
# using mouse button 1
# canvas subwindow must be called $zoneWindow.work_space


    global x_init y_init x_final y_final started_region region_data region_list region_id arrow_in_progress current_arrow zoneWindow
    


    bind $zoneWindow.work_space <ButtonPress-1> {
	if [expr ! $started_region] {
	    puts stdout "window: $zoneWindow \n" 
	    grab set $zoneWindow
	    set x_init [$zoneWindow.work_space canvasx %x]
	    set y_init [$zoneWindow.work_space canvasy %y]
	    start_region $x_init $y_init

	}
    }

    bind $zoneWindow.work_space <ButtonRelease-1> {
	set x_final [$zoneWindow.work_space canvasx %x]
	set y_final [$zoneWindow.work_space canvasy %y]
	end_region $x_final $y_final
	ADD_ZONE $x_init $y_init $x_final $y_final
	
    }
    
    bind $zoneWindow.work_space <B2-Motion> {
	if $arrow_in_progress {
	    set curx [$zoneWindow.work_space canvasx %x] 
	    set cury [$zoneWindow.work_space canvasy %y]
	    $zoneWindow.work_space coords $current_arrow 0 0 $curx $cury
	}
    }
    bind $zoneWindow.work_space <B1-Motion> {
	if $started_region {

	    set curx [$zoneWindow.work_space canvasx %x] 
	    set cury [$zoneWindow.work_space canvasy %y]

	    $zoneWindow.work_space coords region$region_id $x_init $y_init $curx $cury


	} 
    }
    bind $zoneWindow <Enter> {
		    SWITCH_TO_ZONED_PAGE
    }

    bind $zoneWindow <Leave> {
	# on leaving the display, release control of the mouse etc.
	# maybe make it scroll instead?
	if $started_region {
	    grab release $zoneWindow
	    set started_region 0
	    $zoneWindow.work_space coords region$region_id 0 0 0 0  
	}
    }
}


set arrow_in_progress 0

proc make_region_buttons { reg_id } {
    global region_data kill_button_data next_button_data arrow_in_progress current_arrow zoneWindow

    set x_init $region_data($reg_id,x_init)
    set y_init $region_data($reg_id,y_init)

    set next_num [$zoneWindow.work_space create rectangle $x_init $y_init [expr $x_init + 40] [expr $y_init + 20]  -fill blue -tags "region$reg_id next_button$reg_id"]

    set next_button_data($next_num,reg_id) $reg_id
    $zoneWindow.work_space bind next_button$reg_id <Double-2> {
	set reg_id $next_button_data([$zoneWindow.work_space find withtag current],reg_id)
	if { $arrow_in_progress } {
	    set canvas_x [$zoneWindow.work_space canvasx %x] 
	    set canvas_y [$zoneWindow.work_space canvasy %y]
	    finish_arrow $reg_id $canvas_x $canvas_y
	} else {
	    set canvas_x [$zoneWindow.work_space canvasx %x] 
	    set canvas_y [$zoneWindow.work_space canvasy %y]

	    start_arrow  $reg_id $canvas_x $canvas_y
	    puts stdout "Starting an arrow at $canvas_x $canvas_y"
	}
    }
    set kill_num [$zoneWindow.work_space create rectangle [expr $x_init] $y_init [expr $x_init + 20] [expr $y_init + 20] -fill red  -tags "region$reg_id kill_button$reg_id"]
    set kill_button_data($kill_num,reg_id) $reg_id

    $zoneWindow.work_space bind kill_button$reg_id <Double-2> {
	set reg_id $kill_button_data([$zoneWindow.work_space find withtag current],reg_id)
	destroy_region $reg_id $zoneWindow.work_space
	
    }
}

proc start_region { x y} {
    global x_init y_init x_final y_final started_region region_data region_list region_id arrow_in_progress current_arrow zoneWindow region_list

    set x_init $x
    set y_init $y

    set region_id [$zoneWindow.work_space create rectangle $x_init $y_init $x_init $y_init -outline black -width 3 ]
    $zoneWindow.work_space itemconfigure $region_id -tags region$region_id
    
    lappend region_list $region_id
    set started_region 1

}


proc end_region { x y } {
    global x_init y_init x_final y_final started_region region_data region_list region_id arrow_in_progress current_arrow zoneWindow

        set x_final $x
        set y_final $y
	
	$zoneWindow.work_space coords region$region_id $x_init $y_init $x $y


	# if finishing a rectangle, initialize its stuff in the array
	if {$x_init <= $x_final} {
	    set region_data($region_id,x_init) $x_init
	    set region_data($region_id,x_final) $x_final
	} else {
	    set region_data($region_id,x_final) $x_init
	    set region_data($region_id,x_init) $x_final
	}
	if {$y_init <= $y_final} {
	    set region_data($region_id,y_init) $y_init
	    set region_data($region_id,y_final) $y_final
	} else {
	    set region_data($region_id,y_init) $y_final
	    set region_data($region_id,y_final) $y_init
	}
	
	set region_data($region_id,next_region_id) 0
	lappend region_list $region_id
	
	make_region_buttons  $region_id	    

	set started_region 0
	grab release $zoneWindow
    }




proc start_arrow { reg_id x_start y_start } {
    global arrow_in_progress next_button_data region_data current_arrow \
	    zoneWindow prev_reg_id ordered_region_list
    
  
    set path_name $zoneWindow.work_space
# start an arrow in the middle of the little red button
    
    

    set arrow [$zoneWindow.work_space create line \
	    [expr $region_data($prev_reg_id,x_init) + 40] \
	    [expr $region_data($prev_reg_id,y_init) + 20]\
	    \
	    $x_start $y_start -width 3 -arrow last \
	    -arrowshape {6.0m 8.0m 1.5m} -fill blue -tags arrow$reg_id]
#	    [expr $region_data($prev_reg_id,x_init) + 40] \
#	    [expr $region_data($prev_reg_id,y_final)]\

    set region_data($reg_id,arrow) $arrow
    #set arrow_in_progress 1
    set current_arrow $arrow
    
    set prev_reg_id $reg_id
    lappend ordered_region_list $reg_id
}


proc finish_arrow { reg_id x_end y_end} {
    global arrow_in_progress next_button_data region_data current_arrow \
	    zoneWindow
    
  
    set path_name $zoneWindow.work_space
# end an arrow in the middle of the little blue button
   
  

    set arrow [$zoneWindow.work_space create line $last_arrow_x $last_arrow_y $x_end $y_end  -width 3 -arrow last -arrowshape {6.0m 8.0m 1.5m} -fill blue -tags arrow$reg_id]

    set region_data($reg_id,arrow) $arrow
    set arrow_in_progress 0
    set current_arrow $arrow
}



proc destroy_region { reg_id path_name } {
    global region_list ordered_region_list region_data prev_reg_id

    set curx [expr [expr $region_data($reg_id,x_init) \
	    + $region_data($reg_id,x_final)] /2]
    set cury [expr [expr $region_data($reg_id,y_init) \
	    + $region_data($reg_id,y_final)] /2]
    REMOVE_ZONE $curx $cury

    $path_name delete region$reg_id
    set region_list [ldelete $region_list $reg_id]

    # Now we have to delete all of the ordering
    foreach reg $ordered_region_list {
    $path_name delete arrow$reg
    set ordered_region_list [ldelete $ordered_region_list $reg]
    }
    set prev_reg_id 0
}

proc destroy_all_regions { } {
    global region_data region_list zoneWindow prev_reg_id

#    puts [array get region_list]
    foreach reg $region_list {
	destroy_region $reg $zoneWindow.work_space
    }

    
}

proc scale_zoned_page { } {
        
    global ZONING_SCALE_FACTOR
    .zoning_window.work_space delete all
    set display_height [expr $ZONING_SCALE_FACTOR * [get_page_height]]
    set display_width [expr $ZONING_SCALE_FACTOR * [get_page_width]]
    wm geometry .zoning_window [expr int($display_width)]x[expr int($display_height)]+300+100
    zone_message  "Displaying Image"
    DISPLAY_INTERVALS .zoning_window.work_space $ZONING_SCALE_FACTOR
    zone_message  "Determining Line boundaries"
    FIND_LINES
    zone_message  " "
    destroy .scale
}


proc set_active_zone { x y } {
    global ZONING_SCALE_FACTOR SCALE_FACTOR cur_xoffset cur_yoffset

    
    SET_ACTIVE_PAGE [expr int( [expr $x / $ZONING_SCALE_FACTOR ] ) ]\
	    [expr int( [expr $y / $ZONING_SCALE_FACTOR ] ) ]  


}


proc close_zoned_document { } {
    global zoneWindow cur_xoffset cur_yoffset

    set cur_xoffset 0
    set cur_yoffset 0
    .main_window.edit_window.text_part delete 1.0 end
    .main_window.display.work_space delete all

    destroy $zoneWindow
    set COLORED_WORDS {}
    DEALLOCATE_PAGE

}

proc init_zoning_help { path } {
    global FOREGROUND BACKGROUND FONT


    
   
    if { [winfo exists $help] } {
	focus $help
	return
    }

    set help [toplevel .zoning_help ]
    wm title $help "CalZoning Help"
#    wm geometry $help  400x500+600+150
    message $help.msg -background white -foreground $FOREGROUND -font $FONT\
	    -width 600  -text "\n\
	    Delete a Zone - <Double-2> on red button\n\
            Draw arrow to Zone - <Double-2> on blue button\n\
            Create a Zone - <Button-1> drag and release\n\
            Activate a Zone - <Double-2> within region (not on buttons)\n"


            
	                 
    pack $help.msg -fill x -fill y -expand true
    
}


proc ldelete {list value} {

    set ix [lsearch -exact $list $value]
    if { $ix >= 0 } {
	return [lreplace $list $ix $ix]
    } else {
	return $list
    }
}