#!/usr/bin/tclsh

set fp [open "index.html" "w" 0644]

if [catch {open support-pre.html r} hfp] {

puts $fp {<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">}
puts $fp {<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>}
puts $fp "<title>ImageMagick Filters</title>"
puts $fp {<meta http-equiv="Content-Type" content="text/xml; charset=us-ascii"/>}
puts $fp {<meta http-equiv="Content-Language" content="en-us"/>}
puts $fp {<style type="text/css" media="all">.b {background-color:#cccccc}</style>}
puts $fp "</head>"
puts $fp "<body>"
} else {
    set hsize [file size support-pre.html]
    puts -nonewline $fp [read $hfp $hsize]
    close $hfp
}

set support_val {0.6 0.8 1.0 1.2 1.4}
set quality 85
set side 150
set recompute 1

set colcount [llength $support_val]
incr colcount

puts $fp "<table cellpadding=\"5\" border=\"1\" style=\"width:auto\">"
foreach imagefile [glob "traces.jpg"] {

    set w [exec identify -format "%w" $imagefile]
    set h [exec identify -format "%h" $imagefile]
    set side2 [expr {int($side * $h/$w)}]

    puts $fp "<tr><td colspan=\"$colcount\"><a href=\"$imagefile\">$imagefile</a> (Click for original image) size: [exec identify -format "%wx%h" $imagefile] reduced to: $side\x$side2</td></tr>"
    puts $fp "<tr style=\"text-align:center\"><td>Filter</td>"
    foreach support $support_val {
        if {$support == 1.0} {
            puts $fp "<td>(default)</td>"
        } else {
            puts $fp "<td>-support $support</td>"
        }
    }
    puts $fp "</tr>"

    foreach filter {Lanczos Point Box Triangle Hermite Hanning Hamming Blackman Gaussian Quadratic Cubic Catrom Mitchell Bessel Sinc} {
        puts $fp "<tr><td>$filter</td>"
        foreach support $support_val {
            puts "Generating using $filter filter with support $support"
            set result filtered/$filter\_s$support\_$imagefile
            if {$recompute} {
                exec convert $imagefile -filter $filter -support $support -resize $side\x -quality $quality $result
            }
            set filesize [file size $result]
            set filesize [expr round($filesize/1024.0*100.0)/100.0]
            puts $fp "<td class=\"b\"><img src=\"$result\" alt=\"\" width=\"$side\" height=\"[exec identify -format "%h" $result]\"/><br/>$filesize Kb</td>"
        }
        puts $fp "</tr>"
    }
}
puts $fp "</table>"
if [catch {open support-post.html r} hfp] {
    puts $fp {</body></html>}
} else {
    set hsize [file size support-post.html]
    puts -nonewline $fp [read $hfp $hsize]
    close $hfp
}


close $fp
