# Test creating 32-bit (float) images using the pawt package.
#
# Copyright: 2021-2024 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 "01_Basic-Create-float"

puts "\nCreate image with default values ..."
set imgDict1 [pawt CreateImage -pixeltype "float"]
pawtTest CheckNumber 10         [pawt GetImageWidth         imgDict1]     "Default Width"
pawtTest CheckNumber 10         [pawt GetImageHeight        imgDict1]     "Default Height"
pawtTest CheckNumber  1         [pawt GetImageNumChannels   imgDict1]     "Default NumChan"
pawtTest CheckNumber  100       [pawt GetImageNumPixels     imgDict1]     "Default NumPixel"
pawtTest CheckNumber  4         [pawt GetImagePixelSize     imgDict1]     "Default PixelSize"
pawtTest CheckString  "float"   [pawt GetImagePixelType     imgDict1]     "Default PixelType"
pawtTest CheckString  "Intel"   [pawt GetImageByteOrder     imgDict1]     "Default ByteOrder"
pawtTest CheckString  "TopDown" [pawt GetImageScanOrder     imgDict1]     "Default ScanOrder"
pawtTest CheckNumber 0          [pawt GetImagePixel         imgDict1 0 0] "Default Value at (0,0)"
pawtTest CheckNumber 0          [pawt GetImagePixel         imgDict1 9 9] "Default Value at (9,9)"
pawtTest CheckString "0.0000"   [pawt GetImagePixelAsString imgDict1 0 0] "Default Value String at (0,0)"
pawtTest ShowImgDict $imgDict1 "Image with default values" -zoom 20

set outFileName [format "%s_%s.raw" $testCase "Default"]
set outFilePath [file join "testOut" $outFileName]
pawt WriteImageFile $imgDict1 $outFilePath "raw"

puts "\nCreate image with -fill ..."
set numPixel [expr 128 * 64]
set imgDict2 [pawt CreateImage -pixeltype "float" -width 128 -height 64 -scanorder "BottomUp" -byteorder "Intel" -fill 12345]
pawtTest CheckNumber 128          [pawt GetImageWidth         imgDict2]        "Fill Width"
pawtTest CheckNumber 64           [pawt GetImageHeight        imgDict2]        "Fill Height"
pawtTest CheckNumber 1            [pawt GetImageNumChannels   imgDict2]        "Fill NumChan"
pawtTest CheckNumber $numPixel    [pawt GetImageNumPixels     imgDict2]        "Fill NumPixel"
pawtTest CheckNumber 4            [pawt GetImagePixelSize     imgDict2]        "Fill PixelSize"
pawtTest CheckString "float"      [pawt GetImagePixelType     imgDict2]        "Fill PixelType"
pawtTest CheckString "Intel"      [pawt GetImageByteOrder     imgDict2]        "Fill ByteOrder"
pawtTest CheckString "BottomUp"   [pawt GetImageScanOrder     imgDict2]        "Fill ScanOrder"
pawtTest CheckNumber 12345        [pawt GetImagePixel         imgDict2 0 0]    "Fill Value at (0,0)"
pawtTest CheckNumber 12345        [pawt GetImagePixel         imgDict2 127 63] "Fill Value at (127,63)"
pawtTest CheckString "12345.0000" [pawt GetImagePixelAsString imgDict2 0 0]    "Fill Value String at (0,0)"
pawtTest ShowImgDict $imgDict2 "Image with -fill" -zoom 2

set outFileName [format "%s_%s.raw" $testCase "Fill"]
set outFilePath [file join "testOut" $outFileName]
pawt WriteImageFile $imgDict2 $outFilePath "raw"

set imgSize 256
set numPixel [expr $imgSize * $imgSize]

puts "\nCreate image with -scanorder TopDown -list ..."
set imgDict3 [pawt CreateImage -pixeltype "float" -width $imgSize -height $imgSize -scanorder "TopDown" -list [pawtTest CreateList-float]]
pawtTest CheckNumber $imgSize  [pawt GetImageWidth         imgDict3]         "List-TD Width"
pawtTest CheckNumber $imgSize  [pawt GetImageHeight        imgDict3]         "List-TD Height"
pawtTest CheckNumber 1         [pawt GetImageNumChannels   imgDict3]         "List-TD NumChan"
pawtTest CheckNumber $numPixel [pawt GetImageNumPixels     imgDict3]         "List-TD NumPixel"
pawtTest CheckNumber 4         [pawt GetImagePixelSize     imgDict3]         "List-TD PixelSize"
pawtTest CheckString "float"   [pawt GetImagePixelType     imgDict3]         "List-TD PixelType"
pawtTest CheckString "Intel"   [pawt GetImageByteOrder     imgDict3]         "List-TD ByteOrder"
pawtTest CheckString "TopDown" [pawt GetImageScanOrder     imgDict3]         "List-TD ScanOrder"
pawtTest CheckNumber 0         [pawt GetImagePixel         imgDict3 0 0]     "List-TD Value at (0,0)"
pawtTest CheckNumber 65535     [pawt GetImagePixel         imgDict3 255 255] "List-TD Value at (255,255)"
pawtTest CheckString "0.0000"  [pawt GetImagePixelAsString imgDict3 0 0]     "List-TD ValueStr at (0,0)"

set outFileName [format "%s_%s_TD.raw" $testCase "List"]
set outFilePath [file join "testOut" $outFileName]
pawt WriteImageFile $imgDict3 $outFilePath "raw"

puts "\nCreate image with -scanorder BottomUp -list ..."
set imgDict4 [pawt CreateImage -pixeltype "float" -width $imgSize -height $imgSize -scanorder "BottomUp" -list [pawtTest CreateList-float]]
pawtTest CheckNumber $imgSize     [pawt GetImageWidth         imgDict4]         "List-BU Width"
pawtTest CheckNumber $imgSize     [pawt GetImageHeight        imgDict4]         "List-BU Height"
pawtTest CheckNumber 1            [pawt GetImageNumChannels   imgDict4]         "List-BU NumChan"
pawtTest CheckNumber $numPixel    [pawt GetImageNumPixels     imgDict4]         "List-BU NumPixel"
pawtTest CheckNumber 4            [pawt GetImagePixelSize     imgDict4]         "List-BU PixelSize"
pawtTest CheckString "float"      [pawt GetImagePixelType     imgDict4]         "List-BU PixelType"
pawtTest CheckString "Intel"      [pawt GetImageByteOrder     imgDict4]         "List-BU ByteOrder"
pawtTest CheckString "BottomUp"   [pawt GetImageScanOrder     imgDict4]         "List-BU ScanOrder"
pawtTest CheckNumber 65280        [pawt GetImagePixel         imgDict4 0 0]     "List-BU Value at (0,0)"
pawtTest CheckNumber 255          [pawt GetImagePixel         imgDict4 255 255] "List-BU Value at (255,255)"
pawtTest CheckString "65280.0000" [pawt GetImagePixelAsString imgDict4 0 0]     "List-BU Value String at (0,0)"
pawtTest AddImgDicts $imgDict3 "Image with -scanorder TopDown -list"  $imgDict4 "Image with -scanorder BottomUp -list" -title $testCase

set outFileName [format "%s_%s_BU.raw" $testCase "List"]
set outFilePath [file join "testOut" $outFileName]
pawt WriteImageFile $imgDict4 $outFilePath "raw"

puts "\nCreate image with -scanorder TopDown -matrix ..."
set imgDict5 [pawt CreateImage -pixeltype "float" -width $imgSize -height $imgSize -scanorder "TopDown" -matrix [pawtTest CreateMatrix-float]]
pawtTest CheckNumber $imgSize  [pawt GetImageWidth         imgDict5]         "Matrix-TD Width"
pawtTest CheckNumber $imgSize  [pawt GetImageHeight        imgDict5]         "Matrix-TD Height"
pawtTest CheckNumber 1         [pawt GetImageNumChannels   imgDict5]         "Matrix-TD NumChan"
pawtTest CheckNumber $numPixel [pawt GetImageNumPixels     imgDict5]         "Matrix-TD NumPixel"
pawtTest CheckNumber 4         [pawt GetImagePixelSize     imgDict5]         "Matrix-TD PixelSize"
pawtTest CheckString "float"   [pawt GetImagePixelType     imgDict5]         "Matrix-TD PixelType"
pawtTest CheckString "Intel"   [pawt GetImageByteOrder     imgDict5]         "Matrix-TD ByteOrder"
pawtTest CheckString "TopDown" [pawt GetImageScanOrder     imgDict5]         "Matrix-TD ScanOrder"
pawtTest CheckNumber 0         [pawt GetImagePixel         imgDict5 0 0]     "Matrix-TD Value at (0,0)"
pawtTest CheckNumber 65535     [pawt GetImagePixel         imgDict5 255 255] "Matrix-TD Value at (255,255)"
pawtTest CheckString "0.0000"  [pawt GetImagePixelAsString imgDict5 0 0]     "Matrix-TD Value String at (0,0)"

set outFileName [format "%s_%s_TD.raw" $testCase "Matrix"]
set outFilePath [file join "testOut" $outFileName]
pawt WriteImageFile $imgDict5 $outFilePath "raw"

puts "\nCreate image with -scanorder BottomUp -matrix ..."
set imgDict6 [pawt CreateImage -pixeltype "float" -width $imgSize -height $imgSize -scanorder "BottomUp" -matrix [pawtTest CreateMatrix-float]]
pawtTest CheckNumber $imgSize     [pawt GetImageWidth         imgDict6]         "Matrix-BU Width"
pawtTest CheckNumber $imgSize     [pawt GetImageHeight        imgDict6]         "Matrix-BU Height"
pawtTest CheckNumber 1            [pawt GetImageNumChannels   imgDict6]         "Matrix-BU NumChan"
pawtTest CheckNumber $numPixel    [pawt GetImageNumPixels     imgDict6]         "Matrix-BU NumPixel"
pawtTest CheckNumber 4            [pawt GetImagePixelSize     imgDict6]         "Matrix-BU PixelSize"
pawtTest CheckString "float"      [pawt GetImagePixelType     imgDict6]         "Matrix-BU PixelType"
pawtTest CheckString "Intel"      [pawt GetImageByteOrder     imgDict6]         "Matrix-BU ByteOrder"
pawtTest CheckString "BottomUp"   [pawt GetImageScanOrder     imgDict6]         "Matrix-BU ScanOrder"
pawtTest CheckNumber 65280        [pawt GetImagePixel         imgDict6 0 0]     "Matrix-BU Value at (0,0)"
pawtTest CheckNumber 255          [pawt GetImagePixel         imgDict6 255 255] "Matrix-BU Value at (255,255)"
pawtTest CheckString "65280.0000" [pawt GetImagePixelAsString imgDict6 0 0]     "Matrix-BU Value String at (0,0)"
pawtTest AddImgDicts $imgDict5 "Image with -scanorder TopDown -matrix"  $imgDict6 "Image with -scanorder BottomUp -matrix" -title $testCase

set outFileName [format "%s_%s_BU.raw" $testCase "Matrix"]
set outFilePath [file join "testOut" $outFileName]
pawt WriteImageFile $imgDict6 $outFilePath "raw"

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