source pkgsNeeded.tcl

ui_init "t_histo.tcl"
SetFileTypes

PH "Histogram command" 

set img [poImage NewImageFromFile "testIn/keksi$fInfo(suf)"]
$img Histogram $RED histo

set outimg [poImage NewImage 256 256]
$outimg Blank
poImgUtil SetDrawColorRGB 1 1 1 

set fname "testOut/histoKeksi$fInfo(suf)"
set msg "$fname: Printing histogram of RED channel of image keksi$fInfo(suf)"
P $msg
for { set i 0 } { $i < 256 } { incr i } {
    if { [lindex $histo $i] != 0 } {
	$outimg DrawLine $i 0 $i [lindex $histo $i]
    }
}
$outimg WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $outimg $msg

set imgSize 256
set img2 [poImage NewImage $imgSize $imgSize]
$img2 Blank
for { set i 0 } { $i < 256 } { incr i } {
    poImgUtil SetDrawColorRGB [expr (double ($i) / 255)] 0 0
    $img2 DrawLine $i 0 $i $i
}
set fname "testOut/histoImgIn$fInfo(suf)"
set msg "$fname: Generating image with linear color distribution"
P $msg
$img2 WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $img2 $msg
$img2 Histogram $RED histo2

$outimg Blank
poImgUtil SetDrawColorRGB 1 1 1 
set fname "testOut/histoImgOut$fInfo(suf)"
set msg "$fname: Histogram of RED channel of previous image"
P $msg
set sum 0
for { set i 0 } { $i < 256 } { incr i } {
    P "$i: [lindex $histo2 $i]"
    if { [lindex $histo2 $i] != 0 } {
	incr sum [lindex $histo2 $i]
	$outimg DrawLine $i 0 $i [lindex $histo2 $i]
    }
}
P "Sum of pixels: $sum"
set numPixels [expr $imgSize * $imgSize]
if { $sum != $numPixels } {
    PE "Number of histogram entries ($sum) does not match number of pixels ($numPixels)"
}
$outimg WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $outimg $msg

poImgUtil DeleteImg $img

PS
P "End of test"

ui_show

if { [lindex $argv 0] eq "auto" } {
    ui_exit
    exit 0
}
