# Test reading 16-bit image values as list and matrix 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-ListMatrix-short"
pawtTest AddImgDicts -title $testCase

puts "\nCreate 1-channel image from list with scanorder TopDown. Get as list with scanorder TopDown."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetGreyListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetGreyListTD-short] $imgAsList "Check lists"

puts "\nCreate 1-channel image from list with scanorder BottomUp. Get as list with scanorder BottomUp."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -list [pawtTest GetGreyListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "BottomUp"]
pawtTest CheckList [pawtTest GetGreyListTD-short] $imgAsList "Check lists"

puts "\nCreate 1-channel image from list with scanorder TopDown. Get as list with scanorder BottomUp."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetGreyListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "BottomUp"]
pawtTest CheckList [pawtTest GetGreyListBU-short] $imgAsList "Check lists"

puts "\nCreate 1-channel image from list with scanorder BottomUp. Get as list with scanorder TopDown."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -list [pawtTest GetGreyListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetGreyListBU-short] $imgAsList "Check lists"


puts "\nCreate 1-channel image from matrix with scanorder TopDown. Get as matrix with scanorder TopDown."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -matrix [pawtTest GetGreyMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "TopDown"]
pawtTest CheckMatrix [pawtTest GetGreyMatrixTD-short] $imgAsMatrix "Check matrices"

puts "\nCreate 1-channel image from matrix with scanorder BottomUp. Get as matrix with scanorder BottomUp."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -matrix [pawtTest GetGreyMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "BottomUp"]
pawtTest CheckMatrix [pawtTest GetGreyMatrixTD-short] $imgAsMatrix "Check matrices"

puts "\nCreate 1-channel image from matrix with scanorder TopDown. Get as matrix with scanorder BottomUp."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -matrix [pawtTest GetGreyMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "BottomUp"]
pawtTest CheckMatrix [pawtTest GetGreyMatrixBU-short] $imgAsMatrix "Check matrices"

puts "\nCreate 1-channel image from matrix with scanorder BottomUp. Get as matrix with scanorder TopDown."
set imgDict [pawt CreateImage -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -matrix [pawtTest GetGreyMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "TopDown"]
pawtTest CheckMatrix [pawtTest GetGreyMatrixBU-short] $imgAsMatrix "Check matrices"

set minMaxList  [pawt GetImageMinMax     imgDict]
set meanStdList [pawt GetImageMeanStdDev imgDict]
pawtTest CheckNumber 0            [lindex $minMaxList 0]   "Minimum"
pawtTest CheckNumber 65535        [lindex $minMaxList 1]   "Maximum"
# Mean and StdDev reference values computed with Octave.
pawtTest CheckFloat  32589.16667  [lindex $meanStdList 0]  "Mean"               5
pawtTest CheckFloat  23325.80904  [lindex $meanStdList 1]  "Standard Deviation" 5

pawtTest CheckString "0"          [pawt GetImageMinAsString    imgDict]    "Minimum String"
pawtTest CheckString "65535"      [pawt GetImageMaxAsString    imgDict]    "Maximum String"
pawtTest CheckString  32589.1667  [pawt GetImageMeanAsString   imgDict]    "Mean String"
pawtTest CheckString  23325.8090  [pawt GetImageStdDevAsString imgDict]    "StdDev String"
pawtTest CheckString  32589.2     [pawt GetImageMeanAsString   imgDict 1]  "Mean String (Prec: 1)"
pawtTest CheckString  23325.8     [pawt GetImageStdDevAsString imgDict 1]  "StdDev String (Prec: 1)"



puts "\nCreate 3-channel image from list with scanorder TopDown. Get as list with scanorder TopDown."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetRgbListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetRgbListTD-short] $imgAsList "Check lists"

puts "\nCreate 3-channel image from list with scanorder BottomUp. Get as list with scanorder BottomUp."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -list [pawtTest GetRgbListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "BottomUp"]
pawtTest CheckList [pawtTest GetRgbListTD-short] $imgAsList "Check lists"

puts "\nCreate 3-channel image from list with scanorder TopDown. Get as list with scanorder BottomUp."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetRgbListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "BottomUp"]
pawtTest CheckList [pawtTest GetRgbListBU-short] $imgAsList "Check lists"

puts "\nCreate 3-channel image from list with scanorder BottomUp. Get as list with scanorder TopDown."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -list [pawtTest GetRgbListTD-short]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetRgbListBU-short] $imgAsList "Check lists"


puts "\nCreate 3-channel image from matrix with scanorder TopDown. Get as matrix with scanorder TopDown."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -matrix [pawtTest GetRgbMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "TopDown"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixTD-short] $imgAsMatrix "Check matrices"

puts "\nCreate 3-channel image from matrix with scanorder BottomUp. Get as matrix with scanorder BottomUp."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -matrix [pawtTest GetRgbMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "BottomUp"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixTD-short] $imgAsMatrix "Check matrices"

puts "\nCreate 3-channel image from matrix with scanorder TopDown. Get as matrix with scanorder BottomUp."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "TopDown" -matrix [pawtTest GetRgbMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "BottomUp"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixBU-short] $imgAsMatrix "Check matrices"

puts "\nCreate 3-channel image from matrix with scanorder BottomUp. Get as matrix with scanorder TopDown."
set imgDict [pawt CreateImage -numchan 3 -pixeltype "short" -width 6 -height 2 -scanorder "BottomUp" -matrix [pawtTest GetRgbMatrixTD-short]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "TopDown"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixBU-short] $imgAsMatrix "Check matrices"

set minMaxList  [pawt GetImageMinMax     imgDict]
set meanStdList [pawt GetImageMeanStdDev imgDict]
pawtTest CheckNumber 0            [lindex $minMaxList 0]   "Minimum"
pawtTest CheckNumber 65535        [lindex $minMaxList 1]   "Maximum"
# Mean and StdDev reference values computed with Octave.
pawtTest CheckFloat  26845.0  [lindex $meanStdList 0]  "Mean"               5
pawtTest CheckFloat  29758.30727  [lindex $meanStdList 1]  "Standard Deviation" 5

pawtTest CheckString "0"          [pawt GetImageMinAsString    imgDict]    "Minimum String"
pawtTest CheckString "65535"      [pawt GetImageMaxAsString    imgDict]    "Maximum String"
pawtTest CheckString  26845.0000  [pawt GetImageMeanAsString   imgDict]    "Mean String"
pawtTest CheckString  29758.3073  [pawt GetImageStdDevAsString imgDict]    "StdDev String"
pawtTest CheckString  26845.0     [pawt GetImageMeanAsString   imgDict 1]  "Mean String (Prec: 1)"
pawtTest CheckString  29758.3     [pawt GetImageStdDevAsString imgDict 1]  "StdDev String (Prec: 1)"

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