#------------------------------------------------------------------------
#
# 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.
#
#------------------------------------------------------------------------
# ppmstoanimgifs.vm
# Convert a list of PPMs to an animated GIF

package require DvmBasic
package require DvmPnm
package require DvmGif
package require DvmImap
package require DvmColor

source ../lib/pnmlib.tcl

if {$argc < 3} {
    puts "Enter input PPMs prefix :"
    set infile [gets stdin]
    puts "Enter input GIF name :"
    set outfile [gets stdin]
    puts "Enter number of images :"
    set numimages [gets stdin]
} else {
    set infile [lindex $argv 0]
    set outfile [lindex $argv 1]
    set numimages [lindex $argv 2]
}

set maxcolors 256

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

set bytes [read_ppm ${infile}000.ppm]
set red [lindex $bytes 1]
set green [lindex $bytes 2]
set blue [lindex $bytes 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 inds [byte_new [byte_get_width $red] [byte_get_height $red]]

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 $inds

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 $inds

# 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_ct_sorted $hdr 0
gif_seq_hdr_set_aspect_ratio $hdr 0
gif_seq_hdr_set_background_color $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
gif_seq_loop_encode $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_graphic_control_flag $img 1
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_ct_flag $img 0
gif_img_hdr_set_interlaced $img 0
gif_img_hdr_set_graphic_control_flag $img 0
gif_img_hdr_set_disposal_method $img 0
gif_img_hdr_set_user_input_flag $img 0
gif_img_hdr_set_transparent_color_flag $img 0
gif_img_hdr_set_delay_time $img 1
gif_img_hdr_encode $img $bp
gif_img_encode $hdr $img $inds $bp

for {set i 1} {$i < $numimages} {incr i} {
    set bytes [read_ppm [format "%s%03d.ppm" $infile $i]]
    set red [lindex $bytes 1]
    set green [lindex $bytes 2]
    set blue [lindex $bytes 3]
    rgb_quant_with_vp_tree $red $green $blue $tree $cht $rmap $gmap $bmap $inds
    # Encode the image header and image, we use the same color table for
    # all image
    gif_img_hdr_encode $img $bp
    gif_img_encode $hdr $img $inds $bp
    byte_free $red
    byte_free $green
    byte_free $blue
}

#Encode the sequence trailer
gif_seq_trailer_encode $bp

bitstream_channel_write $bs $file 0 
close $file