source pkgsNeeded.tcl

proc Max { a b } {
    if { $a > $b } {
        return $a
    } else {
        return $b
    }
}

proc AltDiff { img1 img2 } {
    global ADD SUB REPLACE

    $img1 GetImageInfo w1 h1
    $img2 GetImageInfo w2 h2

    set w [Max $w1 $w2]
    set h [Max $h1 $h2]

    poImageMode GetDrawMode savedMode

    set tempImg [poImage NewImage $w $h]
    set diffImg [poImage NewImage $w $h]
    $tempImg Blank
    $diffImg Blank

    poImgUtil SetDrawModeRGBA $SUB $SUB $SUB $SUB
    $tempImg CopyImage $img1
    $tempImg CopyRect $img2 0 0 $w $h 0 0
    $diffImg CopyImage $img2
    $diffImg CopyRect $img1 0 0 $w $h 0 0
    poImgUtil SetDrawModeRGBA $ADD $ADD $ADD $ADD
    $diffImg CopyRect $tempImg 0 0 $w $h 0 0

    poImageMode SetDrawMode $savedMode

    poImgUtil DeleteImg $tempImg
    return $diffImg
}

ui_init "t_diff.tcl"
SetFileTypes

PH "Image comparison functions"

set sw [poImgUtil NewSwatch]

set img1 [poImage NewImageFromFile "testIn/keksi$fInfo(suf)"]
set img2 [poImage NewImageFromFile "testIn/keksi$fInfo(suf)"]
$img1 GetImageInfo w h a g
set markImg [poImage NewImage $w $h $a $g]

# Generate a difference image out of two identical images.
# The difference image must be completely black and the number of marked
# pixels must be zero.
set fname "testOut/diffIdentical$fInfo(suf)"
set msg   "$fname: Difference of two identical images"
P $msg
set diffImg [poImgUtil DifferenceImage $img1 $img2]
$markImg MarkNonZeroPixels $diffImg 0 numMarked
if { $numMarked != 0 } {
    PE "Found $numMarked non-zero pixels (Should be zero)"
}
$diffImg WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $diffImg $msg
poImgUtil DeleteImg $diffImg

# Draw a green pixel in one of the images and then generate a difference
# image. The number of marked pixels must be 1.
# In this case we calculate a second difference image with the old method
# of subtracting and adding the channels (see AltDiff at the top of the file).
poImgUtil SetDrawColorRGB 0 1 0
$img2 DrawPixel 100 100

set fname "testOut/diffOnePixel$fInfo(suf)"
set msg   "$fname: Difference of nearly identical images"
P $msg
set diff1Img [::poImgUtil::DifferenceImage $img1 $img2]
set diff2Img [AltDiff $img1 $img2]
$markImg MarkNonZeroPixels $diff1Img 0 numMarked
if { $numMarked != 1 } {
    PE "Method1: Found $numMarked non-zero pixels (Should be 1)"
}
$markImg MarkNonZeroPixels $diff2Img 0 numMarked
if { $numMarked != 1 } {
    PE "Method2: Found $numMarked non-zero pixels (Should be 1)"
}
$diff1Img WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $diff1Img $msg
poImgUtil DeleteImg $diff1Img
poImgUtil DeleteImg $diff2Img

# Erode one of the images and then generate a difference image.
$img2 Erosion 3

set fname "testOut/diffErode$fInfo(suf)"
set msg   "$fname: Difference of original and eroded image"
P $msg
set diffImg [::poImgUtil::DifferenceImage $img1 $img2]
$diffImg WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $diffImg $msg
poImgUtil DeleteImg $diffImg

poImgUtil DeleteImg $img1
poImgUtil DeleteImg $img2

# Draw a synthetic difference image with 9 non-zero pixels
# for testing the MarkNonZeroPixels function.
set srcImg [poImage NewImage $w $h 1.0]
$srcImg Blank

poImgUtil SetDrawColorRGB [expr 1.0 / 256.0] 0 0
$srcImg DrawPixel 50 100
poImgUtil SetDrawColorRGB 1 0 0
$srcImg DrawPixel 60 100
poImgUtil SetDrawColorRGB 0 1 0
$srcImg DrawPixel 70 100
poImgUtil SetDrawColorRGB 0 0 1
$srcImg DrawPixel 80 100
poImgUtil SetDrawColorRGB 1 1 1
$srcImg DrawPixel 90 100
poImgUtil SetDrawColorRGB 0 1 0
$srcImg DrawPixel 0 0
$srcImg DrawPixel [expr $w-1] 0
$srcImg DrawPixel 0 [expr $h-1]
$srcImg DrawPixel [expr $w-1] [expr $h-1]

set msg "Synthetic difference image"
ui_addimg $srcImg $msg

set threshold 0
set fname "testOut/diffThres_$threshold$fInfo(suf)"
set msg   "$fname: Non-Zero pixels (Threshold: $threshold)"
P $msg
$markImg MarkNonZeroPixels $srcImg $threshold numMarked
if { $numMarked != 9 } {
    PE "Found $numMarked non-zero pixels (Should be 9)"
}
$markImg WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $markImg $msg

set threshold 1
set fname "testOut/diffThres_$threshold$fInfo(suf)"
set msg   "$fname: Non-Zero pixels (Threshold: $threshold)"
P $msg
$markImg MarkNonZeroPixels $srcImg $threshold numMarked
if { $numMarked != 8 } {
    PE "Found $numMarked non-zero pixels (Should be 8)"
}
$markImg WriteSimple $fname $fInfo(fmt) $fInfo(vsn)
ui_addimg $markImg $msg

poImgUtil DeleteImg $srcImg

PSec "Required time" [$sw lookup]

poImgUtil DeleteSwatch $sw
poImgUtil DeleteImg $markImg

PS
P "End of test"

ui_show

if { [lindex $argv 0] eq "auto" } {
    ui_exit
    exit 0
}
