# Test handling 8-bit PPM images using the pawt package.
# Note, that the PPM format supports storing rows only in TopDown order.
#
# 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 { ! [file isdirectory "testOut"] } {
    file mkdir "testOut"
}

set testCase "02_Format-Ppm-byte"

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

set outFilePath [file join "testOut" [format "%s_1chan_Ascii.pgm" $testCase]]
puts "Write  1-channel byte image [file tail $outFilePath] (Ascii)"
pawt::ppm::WriteImageFile $imgDict $outFilePath -datatype "ascii"

puts "Read   1-channel byte image [file tail $outFilePath]"
set pgmDict1 [pawt::ppm::ReadImageFile $outFilePath]
set pgmImgAsList [pawt GetImageAsList pgmDict1]
pawtTest CheckList [pawtTest GetGreyListTD-byte] $pgmImgAsList "Check input vs. PGM-Ascii"
pawtTest AddImgDicts $imgDict "In: 1-channel byte"  $pgmDict1 "Out: 1-channel byte (Ascii)"  -zoom 50 -title $testCase

set outFilePath [file join "testOut" [format "%s_1chan_Binary.pgm" $testCase]]
puts "Write  1-channel byte image [file tail $outFilePath] (Binary)"
pawt::ppm::WriteImageFile $imgDict $outFilePath -datatype "binary"

puts "Read   1-channel byte image [file tail $outFilePath]"
set pgmDict2 [pawt::ppm::ReadImageFile $outFilePath]
set pgmImgAsList [pawt GetImageAsList pgmDict2]
pawtTest CheckList [pawtTest GetGreyListTD-byte] $pgmImgAsList "Check input vs. PGM-Binary"
pawtTest AddImgDicts $imgDict "In: 1-channel byte"  $pgmDict2 "Out: 1-channel byte (Binary)" -zoom 50 -title $testCase


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

set outFilePath [file join "testOut" [format "%s_3chan_Ascii.ppm" $testCase]]
puts "Write  3-channel byte image [file tail $outFilePath] (Ascii)"
pawt::ppm::WriteImageFile $imgDict $outFilePath -datatype "ascii"

puts "Read   3-channel byte image [file tail $outFilePath]"
set ppmDict1 [pawt::ppm::ReadImageFile $outFilePath]
set ppmImgAsList [pawt GetImageAsList ppmDict1]
pawtTest CheckList [pawtTest GetRgbListTD-byte] $ppmImgAsList "Check input vs. PPM-Ascii"
pawtTest AddImgDicts $imgDict "In: 3-channel byte"  $ppmDict1 "Out: 3-channel byte (Ascii)" -zoom 50 -title $testCase


set outFilePath [file join "testOut" [format "%s_3chan_Binary.ppm" $testCase]]
puts "Write  3-channel byte image [file tail $outFilePath] (Binary)"
pawt::ppm::WriteImageFile $imgDict $outFilePath -datatype "binary"

puts "Read   3-channel byte image [file tail $outFilePath]"
set ppmDict2 [pawt::ppm::ReadImageFile $outFilePath]
set ppmImgAsList [pawt GetImageAsList ppmDict2]
pawtTest CheckList [pawtTest GetRgbListTD-byte] $ppmImgAsList "Check input vs. PPM-Binary"
pawtTest AddImgDicts $imgDict "In: 3-channel byte"  $ppmDict2 "Out: 3-channel byte (Binary)" -zoom 50 -title $testCase


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