# -*- tcl -*- tcl.tk//DSL tcltest//EN//2.0 tcl.tk//DSL tcltest//EN//2.0
## (c) 2023,2024 Andreas Kupries
# # ## ### ##### ######## ############# #####################
## AKTIVE. Upsampling rows, columns, bands by zero-stuffing
##         (Actually any desired fill value, 0 is default).

kt check Tcl     8.6 9
kt check tcltest 2

# # ## ### ##### ######## ############# #####################

kt local testing aktive

kt source support/util.tcl

# TODO :: Access unaligned to grid -- /select

# # ## ### ##### ######## ############# #####################
## Syntax

syntax {
    x  {src args}
    xy {src args}
    y  {src args}
    z  {src args}
} {aktive op sample fill}

# # ## ### ##### ######## ############# #####################
##
# input/gradient 3x4x2
##
##       0 ....   1......   2...... - 3 columns
##     --------  --------  --------
#	 1  1.5    2  2.5    3  3.5  0
#	 4  4.5    5  5.5    6  6.5  1
#	 7  7.5    8  8.5    9  9.5  2
#	10 10.5   11 11.5   12 12.5  3
##     --- ----  --- ----  --- ----  \ 4 rows
##      0. 1...   0. 1...   0. 1... - 2 bands
#
# Upsampling columns, rows, or bands

foreach {op domain expected} {
    x {6 4 2} {
	=  1  1.5 = 0 0 =  2  2.5 = 0 0 =  3  3.5 = 0 0 =
	=  4  4.5 = 0 0 =  5  5.5 = 0 0 =  6  6.5 = 0 0 =
	=  7  7.5 = 0 0 =  8  8.5 = 0 0 =  9  9.5 = 0 0 =
	= 10 10.5 = 0 0 = 11 11.5 = 0 0 = 12 12.5 = 0 0 =
    }
    y {3 8 2} {
	=  1  1.5 =  2  2.5 =  3  3.5 =
	=  0  0   =  0  0   =  0  0   =
	=  4  4.5 =  5  5.5 =  6  6.5 =
	=  0  0   =  0  0   =  0  0   =
	=  7  7.5 =  8  8.5 =  9  9.5 =
	=  0  0   =  0  0   =  0  0   =
	= 10 10.5 = 11 11.5 = 12 12.5 =
	=  0  0   =  0  0   =  0  0   =
    }
    z {3 4 4} {
	=  1 0  1.5 0 =  2 0  2.5 0 =  3 0  3.5 0 =
	=  4 0  4.5 0 =  5 0  5.5 0 =  6 0  6.5 0 =
	=  7 0  7.5 0 =  8 0  8.5 0 =  9 0  9.5 0 =
	= 10 0 10.5 0 = 11 0 11.5 0 = 12 0 12.5 0 =
    }
    xy {6 8 2} {
	=  1  1.5 = 0 0 =  2  2.5 = 0 0 =  3  3.5 = 0 0 =
	=  0  0   = 0 0 =  0  0   = 0 0 =  0  0   = 0 0  =
	=  4  4.5 = 0 0 =  5  5.5 = 0 0 =  6  6.5 = 0 0 =
	=  0  0   = 0 0 =  0  0   = 0 0 =  0  0   = 0 0  =
	=  7  7.5 = 0 0 =  8  8.5 = 0 0 =  9  9.5 = 0 0 =
	=  0  0   = 0 0 =  0  0   = 0 0 =  0  0   = 0 0  =
	= 10 10.5 = 0 0 = 11 11.5 = 0 0 = 12 12.5 = 0 0 =
	=  0  0   = 0 0 =  0  0   = 0 0 =  0  0   = 0 0  =
    }
} {
    set opd $op ; if {$op eq "xy"} { set opd y }

    test aktive-op-sample-fill-${op}-2.0 "aktive op sample fill $op 2x" -body {
	astcl aktive op sample fill $op [grad] by 2 fill 0
    } -match image -result [makei op::sample::fill::$opd 0 0 {*}$domain {by 2 fill 0.0} $expected]

    test aktive-op-sample-fill-${op}-2.1 "aktive op sample fill $op, dag" -body {
	dag aktive op sample fill $op [grad] by 2 fill 0
    } -match glob -result "op::sample::fill::$opd * {by 2 fill 0.0} {image::gradient *}"

    set in [grad]
    set id [aktive query id $in]

    test aktive-op-sample-fill-${op}-2.2 "aktive op sample fill $op, 1x is identity" -body {
	dag aktive op sample fill $op $in by 1 fill 0
    } -match glob -result "image::gradient $id *"

    unset in id

    # no reductions for xy yet
    if {$op ne "xy"} {
	test aktive-op-sample-fill-${op}-2.3 "aktive op sample fill $op, chain reduction, factors multiply, for same fill" -body {
	    dag aktive op sample fill $op [aktive op sample fill $op [grad] by 2 fill 0] by 3 fill 0
	} -match glob -result "op::sample::fill::$op * {by 6 fill 0.0} {image::gradient *}"

	test aktive-op-sample-fill-${op}-2.4 "aktive op sample fill $op, partial view" -body {
	   pixels aktive op select $op [aktive op sample fill $op [grad$op] by 3 fill 0] from 5 to 10
	} -match pixels -result {0 2 0 0 3 0}
    }

    ##
    # - -- --- ----- -------- ------------- ---------------------
    ## Slice and check in all directions

    lassign $domain w h d

    for {set row 0} {$row < $h} {incr row} {
	test aktive-op-sample-fill-${op}-2.5.r$row "aktive op sample fill $op, row slice" -body {
	    pixels aktive op select y [aktive op sample fill $op [grad] by 2 fill 0] from $row
	} -match pixels -result [slice-row $row $w $h $d $expected]
    }

    for {set col 0} {$col < $w} {incr col} {
	test aktive-op-sample-fill-${op}-2.5.c$col "aktive op sample fill $op, column slice" -body {
	    pixels aktive op select x [aktive op sample fill $op [grad] by 2 fill 0] from $col
	} -match pixels -result [slice-col $col $w $h $d $expected]
    }

    for {set band 0} {$band < $d} {incr band} {
	test aktive-op-sample-fill-${op}-2.5.b$band "aktive op sample fill $op, band slice" -body {
	    pixels aktive op select z [aktive op sample fill $op [grad] by 2 fill 0] from $band
	} -match pixels -result [slice-band $band $w $h $d $expected]
    }

    # - -- --- ----- -------- ------------- ---------------------
    unset w h d row col band
}

# # ## ### ##### ######## ############# #####################
cleanupTests
return
