# Test reading 32-bit integer 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 { [package vcompare [package version "Img"] 1.4.14] < 0 } {
    puts "Error: pawt test suite for int values needs Img > 1.4.13."
    exit 1
}

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

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

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

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

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


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

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

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

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

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

pawtTest CheckString "0"               [pawt GetImageMinAsString    imgDict]    "Minimum String"
pawtTest CheckString "4294967295"      [pawt GetImageMaxAsString    imgDict]    "Maximum String"
pawtTest CheckString  2049161215.8333  [pawt GetImageMeanAsString   imgDict]    "Mean String"
pawtTest CheckString  1504682022.2387  [pawt GetImageStdDevAsString imgDict]    "StdDev String"
pawtTest CheckString  2049161215.8     [pawt GetImageMeanAsString   imgDict 1]  "Mean String (Prec: 1)"
pawtTest CheckString  1504682022.2     [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 "int" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetRgbListTD-int]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetRgbListTD-int] $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 "int" -width 6 -height 2 -scanorder "BottomUp" -list [pawtTest GetRgbListTD-int]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "BottomUp"]
pawtTest CheckList [pawtTest GetRgbListTD-int] $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 "int" -width 6 -height 2 -scanorder "TopDown" -list [pawtTest GetRgbListTD-int]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "BottomUp"]
pawtTest CheckList [pawtTest GetRgbListBU-int] $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 "int" -width 6 -height 2 -scanorder "BottomUp" -list [pawtTest GetRgbListTD-int]]
set imgAsList [pawt GetImageAsList imgDict -scanorder "TopDown"]
pawtTest CheckList [pawtTest GetRgbListBU-int] $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 "int" -width 6 -height 2 -scanorder "TopDown" -matrix [pawtTest GetRgbMatrixTD-int]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "TopDown"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixTD-int] $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 "int" -width 6 -height 2 -scanorder "BottomUp" -matrix [pawtTest GetRgbMatrixTD-int]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "BottomUp"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixTD-int] $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 "int" -width 6 -height 2 -scanorder "TopDown" -matrix [pawtTest GetRgbMatrixTD-int]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "BottomUp"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixBU-int] $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 "int" -width 6 -height 2 -scanorder "BottomUp" -matrix [pawtTest GetRgbMatrixTD-int]]
set imgAsMatrix [pawt GetImageAsMatrix imgDict -scanorder "TopDown"]
pawtTest CheckMatrix [pawtTest GetRgbMatrixBU-int] $imgAsMatrix "Check matrices"

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

pawtTest CheckString "0"               [pawt GetImageMinAsString    imgDict]    "Minimum String"
pawtTest CheckString "4294967295"      [pawt GetImageMaxAsString    imgDict]    "Maximum String"
pawtTest CheckString  1764989098.3333  [pawt GetImageMeanAsString   imgDict]    "Mean String"
pawtTest CheckString  1950928088.0132  [pawt GetImageStdDevAsString imgDict]    "StdDev String"
pawtTest CheckString  1764989098.3     [pawt GetImageMeanAsString   imgDict 1]  "Mean String (Prec: 1)"
pawtTest CheckString  1950928088.0     [pawt GetImageStdDevAsString imgDict 1]  "StdDev String (Prec: 1)"

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