# Test comparing 64-bit double images 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 double values needs Img > 1.4.13."
    exit 1
}

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

set testCase "03_Diff-double"

set inFileList { "double-1chan-td" "double-3chan-td" }

# Number of different pixels when comparing against black image.
set diffPixels 10

set count  0
set winNum 1
foreach inFilePrefix $inFileList {
    set inFileName [format "%s.%s" $inFilePrefix "raw"]
    set inFilePath [file join "testIn" $inFileName]
    puts "Reading file $inFilePath ..."
    set imgDict1 [pawt::raw::ReadImageFile $inFilePath]
    set photo1   [pawt GetImageAsPhoto imgDict1]
    set photo1Diff [image create photo]
    $photo1Diff copy $photo1

    set index [string first "-td" $inFilePath]
    set inFilePath2 [string replace $inFilePath $index [expr $index + 2] "-bu"]
    puts "Reading file $inFilePath2 ..."
    set imgDict2 [pawt::raw::ReadImageFile $inFilePath2]
    set photo2   [pawt GetImageAsPhoto imgDict2]
    set photo2Diff [image create photo]
    $photo2Diff copy $photo2

    set width     [pawt GetImageWidth       imgDict1]
    set height    [pawt GetImageHeight      imgDict1]
    set pixelType [pawt GetImagePixelType   imgDict1]
    set numChan   [pawt GetImageNumChannels imgDict1]
    set imgDictBlack   [pawt CreateImage -width $width -height $height -pixeltype $pixelType -numchan $numChan -fill 0]
    set photoBlack     [pawt GetImageAsPhoto imgDictBlack]
    set photoBlackDiff [image create photo]
    $photoBlackDiff copy $photo1

    puts -nonewline "Comparing two identical images ... "
    set numDiffs [pawt DiffImages imgDict1 imgDict1 -photo $photo1Diff]
    pawtTest CheckNumber 0 $numDiffs "Different pixels"
    pawtTest AddImgPhotos $photo1 "Left image"  $photo1 "Right image"  $photo1Diff "Difference image" \
             -title "$testCase Part $winNum" -window $winNum -zoom 50

    puts -nonewline "Comparing two identical images with different scan order ... "
    set numDiffs [pawt DiffImages imgDict1 imgDict2 -photo $photo2Diff]
    pawtTest CheckNumber 0 $numDiffs "Different pixels"
    pawtTest AddImgPhotos $photo1 "Left image"  $photo2 "Right image"  $photo2Diff "Difference image" \
             -title "$testCase Part $winNum" -window $winNum -zoom 50

    puts -nonewline "Comparing test image with black image ... "
    set numDiffs [pawt DiffImages imgDict1 imgDictBlack -photo $photoBlackDiff]
    pawtTest CheckNumber $diffPixels $numDiffs "Different pixels"
    pawtTest AddImgPhotos $photo1 "Left image"  $photoBlack "Right image"  $photoBlackDiff "Difference image" \
             -title "$testCase Part $winNum" -window $winNum -zoom 50

    puts -nonewline "Comparing test image with black image (no photo) ... "
    set numDiffs [pawt DiffImages imgDict1 imgDictBlack]
    pawtTest CheckNumber $diffPixels $numDiffs "Different pixels"

    puts "Comparing test image with black image using coroutine ... "
    coroutine CoroDiffImages pawt DiffImages imgDict1 imgDictBlack -photo $photoBlackDiff
    while { true } {
        set yieldList [CoroDiffImages]
        if { [llength $yieldList] == 2 } {
            lassign $yieldList numDiff percent
            puts -nonewline "\b\b\b$percent%" ; flush stdout
        } else {
            puts ""
            pawtTest CheckNumber $diffPixels $numDiff "Different pixels"
            break
        }
    }
    incr count
    if { $count % 1 == 0 } {
        incr winNum
    }
}


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