#------------------------------------------------------------------------
#
# 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 DvmImap
source  ../lib/pnmlib.tcl

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


set l [read_pgm $inname]
set hdr  [lindex $l 0]
set byte [lindex $l 1]
set out  [byte_new [pnm_hdr_get_width $hdr] [pnm_hdr_get_height $hdr]]
set map  [imagemap_new]
imagemap_init_histo_equal $byte $map
imagemap_apply $map $byte $out
write_pgm $hdr $out $outname

byte_free $byte
byte_free $out
imagemap_free $map
pnm_hdr_free $hdr