# Test handling 32-bit integer RAW images using the pawt package.
# Note, that the RAW format supports storing rows in TopDown and BottomUp order.
#
# 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 "02_Format-Raw-int"

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

set outFilePath [file join "testOut" [format "%s_1chan_TopDown.raw" $testCase]]
puts "Write  1-channel int image [file tail $outFilePath] (TopDown)"
pawt::raw::WriteImageFile $imgDict $outFilePath -scanorder "TopDown"

puts "Read   1-channel int image [file tail $outFilePath]"
set rawDict1 [pawt::raw::ReadImageFile $outFilePath]
set rawImgAsList [pawt GetImageAsList rawDict1]
pawtTest CheckList [pawtTest GetGreyListTD-int] $rawImgAsList "Check input vs. RAW-TopDown"
pawtTest AddImgDicts $imgDict "In: 1-channel int (TopDown)"  $rawDict1 "Out: 1-channel int (TopDown)"  -zoom 50 -title $testCase


set outFilePath [file join "testOut" [format "%s_1chan_BottomUp.raw" $testCase]]
puts "Write  1-channel int image [file tail $outFilePath] (BottomUp)"
pawt::raw::WriteImageFile $imgDict $outFilePath -scanorder "BottomUp"

puts "Read   1-channel int image [file tail $outFilePath]"
set rawDict2 [pawt::raw::ReadImageFile $outFilePath]
set rawImgAsList [pawt GetImageAsList rawDict2]
pawtTest CheckList [pawtTest GetGreyListBU-int] $rawImgAsList "Check input vs. RAW-BottomUp"
pawtTest AddImgDicts $imgDict "In: 1-channel int (BottomUp)"  $rawDict2 "Out: 1-channel int (BottomUp)"  -zoom 50 -title $testCase


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

set outFilePath [file join "testOut" [format "%s_3chan_TopDown.raw" $testCase]]
puts "Write  3-channel int image [file tail $outFilePath] (TopDown)"
pawt::raw::WriteImageFile $imgDict $outFilePath -scanorder "TopDown"

puts "Read   3-channel int image [file tail $outFilePath]"
set rawDict3 [pawt::raw::ReadImageFile $outFilePath]
set rawImgAsList [pawt GetImageAsList rawDict3]
pawtTest CheckList [pawtTest GetRgbListTD-int] $rawImgAsList "Check input vs. RAW-TopDown"
pawtTest AddImgDicts $imgDict "In: 3-channel int (TopDown)"  $rawDict3 "Out: 3-channel int (TopDown)"  -zoom 50 -title $testCase


set outFilePath [file join "testOut" [format "%s_3chan_BottomUp.raw" $testCase]]
puts "Write  3-channel int image [file tail $outFilePath] (BottomUp)"
pawt::raw::WriteImageFile $imgDict $outFilePath -scanorder "BottomUp"

puts "Read   3-channel int image [file tail $outFilePath]"
set rawDict4 [pawt::raw::ReadImageFile $outFilePath]
set rawImgAsList [pawt GetImageAsList rawDict4]
pawtTest CheckList [pawtTest GetRgbListBU-int] $rawImgAsList "Check input vs. RAW-BottomUp"
pawtTest AddImgDicts $imgDict "In: 3-channel int (BottomUp)"  $rawDict4 "Out: 3-channel int (BottomUp)"  -zoom 50 -title $testCase


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