# Test converting 32-bit float images 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_Convert-float"

set inFileList { "float-1chan-bu" "float-1chan-td" "float-3chan-bu" "float-3chan-td" }

foreach inFilePrefix $inFileList {
    set inFileName [format "%s.%s" $inFilePrefix "raw"]
    set inFilePath [file join "testIn" $inFileName]
    puts "Reading file $inFilePath ..."
    set rawDict [pawt::raw::ReadImageFile $inFilePath]

    set rawImgAsListTD [pawt GetImageAsList   rawDict -scanorder "TopDown"]
    set rawImgAsListBU [pawt GetImageAsList   rawDict -scanorder "BottomUp"]
    set rawImgAsMatTD  [pawt GetImageAsMatrix rawDict -scanorder "TopDown"]
    set rawImgAsMatBU  [pawt GetImageAsMatrix rawDict -scanorder "BottomUp"]

    set outRawName [format "%s_%s.%s" $testCase $inFilePrefix "raw"]
    set outRawPath [file join "testOut" $outRawName]
    puts "Writing file $outRawPath ..."
    pawt::raw::WriteImageFile $rawDict $outRawPath

    pawtTest CheckFile $inFilePath $outRawPath "Check $inFilePath vs. $outRawPath"


    set outFitsName [format "%s_%s.%s" $testCase $inFilePrefix "fits"]
    set outFitsPath [file join "testOut" $outFitsName]
    puts "Writing file $outFitsPath ..."
    pawt::fits::WriteImageFile $rawDict $outFitsPath

    set fitsDict [pawt::fits::ReadImageFile $outFitsPath]
    set fitsImgAsListTD [pawt GetImageAsList   fitsDict -scanorder "TopDown"]
    set fitsImgAsListBU [pawt GetImageAsList   fitsDict -scanorder "BottomUp"]
    set fitsImgAsMatTD  [pawt GetImageAsMatrix fitsDict -scanorder "TopDown"]
    set fitsImgAsMatBU  [pawt GetImageAsMatrix fitsDict -scanorder "BottomUp"]

    pawtTest CheckFloatList   $rawImgAsListTD $fitsImgAsListTD "Check RAW vs. FITS TD list"   5
    pawtTest CheckFloatList   $rawImgAsListBU $fitsImgAsListBU "Check RAW vs. FITS BU list"   5
    pawtTest CheckFloatMatrix $rawImgAsMatTD  $fitsImgAsMatTD  "Check RAW vs. FITS TD matrix" 5
    pawtTest CheckFloatMatrix $rawImgAsMatBU  $fitsImgAsMatBU  "Check RAW vs. FITS BU matrix" 5

   
    if { [string match "*3chan*" $inFilePrefix] } {
        set flirDict [pawtTest CreateDictUnsupported [pawt GetImageWidth rawDict] [pawt GetImageHeight rawDict]]
        set outFlirName "3-channel float FLIR images not supported"
    } else {
        set outFlirName [format "%s_%s.%s" $testCase $inFilePrefix "fpf"]
        set outFlirPath [file join "testOut" $outFlirName]
        puts "Writing file $outFlirPath ..."
        pawt::flir::WriteImageFile $rawDict $outFlirPath

        set flirDict [pawt::flir::ReadImageFile $outFlirPath]
        set flirImgAsListTD [pawt GetImageAsList   flirDict -scanorder "TopDown"]
        set flirImgAsListBU [pawt GetImageAsList   flirDict -scanorder "BottomUp"]
        set flirImgAsMatTD  [pawt GetImageAsMatrix flirDict -scanorder "TopDown"]
        set flirImgAsMatBU  [pawt GetImageAsMatrix flirDict -scanorder "BottomUp"]

        pawtTest CheckFloatList   $rawImgAsListTD $flirImgAsListTD "Check RAW vs. FLIR TD list"   5
        pawtTest CheckFloatList   $rawImgAsListBU $flirImgAsListBU "Check RAW vs. FLIR BU list"   5
        pawtTest CheckFloatMatrix $rawImgAsMatTD  $flirImgAsMatTD  "Check RAW vs. FLIR TD matrix" 5
        pawtTest CheckFloatMatrix $rawImgAsMatBU  $flirImgAsMatBU  "Check RAW vs. FLIR BU matrix" 5
    }

    set pgmbDict [pawtTest CreateDictUnsupported [pawt GetImageWidth rawDict] [pawt GetImageHeight rawDict]]
    set outPgmbName "PPM float binary images not supported"

    set pgmaDict [pawtTest CreateDictUnsupported [pawt GetImageWidth rawDict] [pawt GetImageHeight rawDict]]
    set outPgmaName "PPM float ASCII images not supported"

    pawtTest AddImgDicts $rawDict  $outRawName \
                         $fitsDict $outFitsName \
                         $flirDict $outFlirName \
                         $pgmbDict $outPgmbName \
                         $pgmaDict $outPgmaName \
                         -title $testCase -zoom 50
    puts ""
}


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