#------------------------------------------------------------------------
#
# Copyright (c) 1997-1998 by Cornell University.
# 
# See the file "license.txt" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------
package require DvmBasic
package require DvmPnm
package require DvmVision
source ../lib/pnmlib.tcl

if {$argc < 2} {
    puts "Enter input PPM file name :"
    set inname [gets stdin]
    puts "Enter output PBM file name :"
    set outname [gets stdin]
} else {
    set inname [lindex $argv 0]
    set outname [lindex $argv 1]
}

set hdrbyte [read_pgm $inname]
set hdr [lindex $hdrbyte 0]
set byte [lindex $hdrbyte 1]
set w [pnm_hdr_get_width $hdr]
set h [pnm_hdr_get_height $hdr]

set e1 [byte_new $w $h]
set e2 [byte_new $w $h]
set edge [bit_new $w $h]
set bit1 [bit_new $w $h]
set bit2 [bit_new $w $h]
set smth [byte_new $w $h]

byte_smooth $byte $smth 2

set vals [byte_edge_detect_sobel $smth $e1 $e2 90]
set vertical [lindex $vals 0]
set horizontal [lindex $vals 1]

bit_make_from_threshold_8 $e1 $bit1 $vertical 0
bit_make_from_threshold_8 $e2 $bit2 $horizontal 0
bit_union_8 $bit1 $bit2 $edge
write_pbm_8 $hdr $edge $outname

byte_free $byte
byte_free $smth
byte_free $e1
byte_free $e2
bit_free $bit1
bit_free $bit2
bit_free $edge