# Test handling 8-bit FITS images using the pawt package.
# Note, that the FITS format supports storing rows only in BottomUp order.
#
# 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 "02_Format-Fits-byte"

puts "\nCreate 1-channel byte image"
set imgDict [pawt CreateImage -width 6 -height 2 -pixeltype "byte" -numchan 1 -list [pawtTest GetGreyListTD-byte]]

set outFilePath [file join "testOut" [format "%s_1chan.fits" $testCase]]
puts "Write  1-channel byte image [file tail $outFilePath]"
pawt::fits::WriteImageFile $imgDict $outFilePath

puts "Read   1-channel byte image [file tail $outFilePath]"
set fitsDict1 [pawt::fits::ReadImageFile $outFilePath]
set fitsImgAsList [pawt GetImageAsList fitsDict1 -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetGreyListTD-byte] $fitsImgAsList "Check input vs. FITS"
pawtTest AddImgDicts $imgDict "In: 1-channel byte"  $fitsDict1 "Out: 1-channel byte"  -zoom 50 -title $testCase

puts "\nCreate 3-channel byte image"
set imgDict [pawt CreateImage -width 6 -height 2 -pixeltype "byte" -numchan 3 -list [pawtTest GetRgbListTD-byte]]

set outFilePath [file join "testOut" [format "%s_3chan.fits" $testCase]]
puts "Write  3-channel byte image [file tail $outFilePath]"
pawt::fits::WriteImageFile $imgDict $outFilePath

puts "Read   3-channel byte image [file tail $outFilePath]"
set fitsDict2 [pawt::fits::ReadImageFile $outFilePath]
set fitsImgAsList [pawt GetImageAsList fitsDict2 -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetRgbListTD-byte] $fitsImgAsList "Check input vs. FITS"
pawtTest AddImgDicts $imgDict "In: 3-channel byte"  $fitsDict2 "Out: 3-channel byte"  -zoom 50 -title $testCase


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