# Test reading 64-bit image values as list and matrix 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 { [package vcompare [package version "Img"] 1.4.14] < 0 } {
    puts "Error: pawt test suite for double values needs Img > 1.4.13."
    exit 1
}

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

set testCase "01_Basic-ListMatrix-double"
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 "float" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetGreyListTD-double]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckFloatList [pawtTest GetGreyListTD-double] $imgAsList "Check lists" 5

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

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

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


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

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

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

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

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

pawtTest CheckString "0.0000" [pawt GetImageMinAsString    imgDict]    "Minimum String"
pawtTest CheckString "1.0000" [pawt GetImageMaxAsString    imgDict]    "Maximum String"
pawtTest CheckString  0.5000  [pawt GetImageMeanAsString   imgDict]    "Mean String"
pawtTest CheckString  0.3568  [pawt GetImageStdDevAsString imgDict]    "StdDev String"
pawtTest CheckString  0.5     [pawt GetImageMeanAsString   imgDict 1]  "Mean String (Prec: 1)"
pawtTest CheckString  0.4     [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 "float" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetRgbListTD-double]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckFloatList [pawtTest GetRgbListTD-double] $imgAsList "Check lists" 5

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

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

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


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

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

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

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

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

pawtTest CheckString "0.0000" [pawt GetImageMinAsString    imgDict]    "Minimum String"
pawtTest CheckString "1.0000" [pawt GetImageMaxAsString    imgDict]    "Maximum String"
pawtTest CheckString  0.4167  [pawt GetImageMeanAsString   imgDict]    "Mean String"
pawtTest CheckString  0.4551  [pawt GetImageStdDevAsString imgDict]    "StdDev String"
pawtTest CheckString  0.4     [pawt GetImageMeanAsString   imgDict 1]  "Mean String (Prec: 1)"
pawtTest CheckString  0.5     [pawt GetImageStdDevAsString imgDict 1]  "StdDev String (Prec: 1)"

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