# Test reading and writing 16-bit images in different sizes using the pawt package.
#
# Copyright: 2021-2023 Paul Obermeier (obermeier@poSoft.de)
# Distributed under BSD license.

if { [file exists "TestUtils.tcl"] } {
    source "TestUtils.tcl"
}

package require pawt

if { ! [pawt::HavePkg "Img"] } {
    puts "Error: pawt test suite needs the Img extension."
    exit 1
}

if { ! [file isdirectory "testOut"] } {
    file mkdir "testOut"
}

set testCase "03_ReadWrite-short"
pawtTest AddImgDicts -title $testCase

set sizeList { 256 512 1024 2048 }
set scanList { "TopDown" "BottomUp" }

# Write images in different sizes and scan orders with timing.
foreach size $sizeList {
    set fillList [lrepeat [expr $size * $size] 12345]

    foreach scanOrder $scanList {
        set imgDict [pawt CreateImage -scanorder $scanOrder -width $size -height $size -list $fillList \
                          -pixeltype "short" -numchan 1]

        set outRawName [format "%s_%s-%d.%s" $testCase $scanOrder $size "raw"]
        set outRawPath [file join "testOut" $outRawName]
        puts -nonewline "Writing file $outRawPath ..."
        set startTime [clock clicks -milliseconds]
        pawt WriteImageFile $imgDict $outRawPath "raw"
        set endTime [clock clicks -milliseconds]
        puts "  [expr $endTime - $startTime] ms."

        set outFitsName [format "%s_%s-%d.%s" $testCase $scanOrder $size "fits"]
        set outFitsPath [file join "testOut" $outFitsName]
        puts -nonewline "Writing file $outFitsPath ..."
        set startTime [clock clicks -milliseconds]
        pawt WriteImageFile $imgDict $outFitsPath "fits"
        set endTime [clock clicks -milliseconds]
        puts "  [expr $endTime - $startTime] ms."

        set outFlirName [format "%s_%s-%d.%s" $testCase $scanOrder $size "fpf"]
        set outFlirPath [file join "testOut" $outFlirName]
        puts -nonewline "Writing file $outFlirPath ..."
        set startTime [clock clicks -milliseconds]
        pawt WriteImageFile $imgDict $outFlirPath "flir"
        set endTime [clock clicks -milliseconds]
        puts "  [expr $endTime - $startTime] ms."

        set outPgmbName [format "%s_%s-%d-Binary.%s" $testCase $scanOrder $size "pgm"]
        set outPgmbPath [file join "testOut" $outPgmbName]
        puts -nonewline "Writing file $outPgmbPath (-datatype binary) ..."
        set startTime [clock clicks -milliseconds]
        pawt WriteImageFile $imgDict $outPgmbPath "ppm" -datatype "binary"
        set endTime [clock clicks -milliseconds]
        puts "  [expr $endTime - $startTime] ms."

        set outPgmaName [format "%s_%s-%d-Ascii.%s" $testCase $scanOrder $size "pgm"]
        set outPgmaPath [file join "testOut" $outPgmaName]
        puts -nonewline "Writing file $outPgmaPath  (-datatype ascii) ..."
        set startTime [clock clicks -milliseconds]
        pawt WriteImageFile $imgDict $outPgmaPath "ppm" -datatype "ascii"
        set endTime [clock clicks -milliseconds]
        puts "  [expr $endTime - $startTime] ms."
    }
    puts ""
}

# Read generated images with timing.
set inFileList [lsort -dictionary [glob -nocomplain "testOut/03_ReadWrite*.*"]]
foreach inFilePath $inFileList {
    set inFileName [file tail $inFilePath]
    puts -nonewline "Reading file $inFilePath ..."
    set startTime [clock clicks -milliseconds]
    set imgDict [pawt ReadImageFile $inFilePath]
    set endTime [clock clicks -milliseconds]
    puts "  [expr $endTime - $startTime] ms."
}

if { [lindex $argv 0] eq "auto" } {
    update
    after 500
    exit 0
}
