#------------------------------------------------------------------------
#
# 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.
#
#------------------------------------------------------------------------
# ppmtogif.vm
# Convert a single PPM to a GIF image
package require DvmBasic
package require DvmColor
package require DvmImap
package require DvmPnm
package require DvmGif

source ../lib/pnmlib.tcl

if {$argc < 2} {
    puts "Enter input PPM file :"
    set infile [gets stdin]
    puts "Enter output GIF file :"
    set outfile [gets stdin]
} else {
    set infile [lindex $argv 0]
    set outfile [lindex $argv 1]
}

set maxcolors 256

set bp   [bitparser_new]
set bs   [bitstream_new [file size $infile]]
set file [open $outfile w]
fconfigure $file -translation binary -buffersize 65535
bitparser_wrap $bp $bs

set imgs   [read_ppm $infile]
set red   [lindex $imgs 1]
set green [lindex $imgs 2]
set blue  [lindex $imgs 3]

set rmap [imagemap_new]
set gmap [imagemap_new]
set bmap [imagemap_new]

set qr [byte_new [byte_get_width $red] [byte_get_height $red]]
set qg [byte_new [byte_get_width $red] [byte_get_height $red]]
set qb [byte_new [byte_get_width $red] [byte_get_height $red]]
set indices [byte_new [byte_get_width $red] [byte_get_height $red]]

# Quantize the image to a legal number of colors, initialize the global color table, and convert
# the image to an index image
set cht [color_hash_table_new 15]
color_hash_table_clear $cht
rgb_to_256 $red $green $blue $cht $rmap $gmap $bmap
color_hash_table_free $cht

set cht [color_hash_table_new 16]
color_hash_table_clear $cht

# Uncomment the following line to quantize using hash table only.
# rgb_quant_with_hash_table $red $green $blue $cht $rmap $gmap $bmap $indices

set tree [vp_tree_new]
vp_tree_init $rmap $gmap $bmap $tree
rgb_quant_with_vp_tree $red $green $blue $tree $cht $rmap $gmap $bmap $indices
color_hash_table_free $cht

# Initialize the sequence header
set hdr [gif_seq_hdr_new]
gif_seq_hdr_set_width $hdr [byte_get_width $red]
gif_seq_hdr_set_height $hdr [byte_get_height $red]
gif_seq_hdr_set_ct_flag $hdr 1
gif_seq_hdr_set_version $hdr 87a
gif_seq_hdr_set_ct_size $hdr 256
gif_seq_hdr_set_aspect_ratio $hdr 0
gif_seq_hdr_set_resolution $hdr 3

# Encode the sequence header, global color table, and the application extension 
# indicating to loop the sequence
gif_seq_hdr_encode $hdr $bp
gif_ct_encode 256 $rmap $gmap $bmap $bp

# Initialize the image header
set img [gif_img_hdr_new]
gif_img_hdr_set_width $img [gif_seq_hdr_get_width $hdr]
gif_img_hdr_set_height $img [gif_seq_hdr_get_height $hdr]
gif_img_hdr_set_ct_flag $img 0
gif_img_hdr_set_left_position $img 0
gif_img_hdr_set_top_position $img 0
gif_img_hdr_set_interlaced $img 0
gif_img_hdr_set_graphic_control_flag $img 0

#Encode the image header and image
gif_img_hdr_encode $img $bp
gif_img_encode $hdr $img $indices $bp

#Encode the sequence trailer
gif_seq_trailer_encode $bp

bitstream_channel_write $bs $file 0 

close $file

byte_free $red
byte_free $green
byte_free $blue
imagemap_free $rmap
imagemap_free $gmap
imagemap_free $bmap