#! /bin/bash

# Usage: run-test <build-path>

P=${1:-`pwd -P`}

DIM=(
    '32 32'
    '800 640'
    '1024 1024'
    '1920 1280'
)

COL=(
    'red       -M solid  --red 0xffff'
    'green     -M solid  --green 0xffff'
    'blue      -M solid  --blue 0xffff'
    'random-0  -M random --seed 0'
    'random-23 -M random --seed 23'
    'random-42 -M random --seed 42'
)

FMT=(
    gbrg
    bggr
    grbg
    rggb
)

DIM_FMT_COL=(
    '32 32 gbrg red_col0   -M column --column 2 --red 0xfffe'
    '32 32 gbrg red_col1   -M column --column 4 --red 0xfffe'
    '32 32 gbrg blue_col0  -M column --column 3 --blue 0xfffe'
    '32 32 gbrg blue_col1  -M column --column 5 --blue 0xfffe'
    '32 32 gbrg green_col0 -M column --column 2 --green 0xfffe'
    '32 32 gbrg green_col1 -M column --column 3 --green 0xfffe'
    '32 32 gbrg col4       -M column --column 6 --red 0x04 --blue 0x08 --green 0x10'
    "32 32 gbrg red_rnd    -M solid  --red   0xffff 4 5 0xfffe"
    "32 32 gbrg blue_rnd   -M solid  --blue  0xffff 5 6 0xfffe"
    "32 32 gbrg green_rnd0 -M solid  --green 0xffff 5 5 0xfffe"

#     "2048 2048 gbrg random_gbrg -M random --seed 0"
#     "2048 2048 bggr random_bggr -M random --seed 0"
#     "2048 2048 grbg random_grbg -M random --seed 0"
#     "2048 2048 rggb random_rggb -M random --seed 0"
)

EXACT_METHODS=(
    c
    cc
    c-opt
    neon
)

ERROR_CNT=0
SUCCESS_CNT=0

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

tmp=`mktemp -d -t bayer-test.XXXXXX` || exit 1
trap "rm -rf $tmp" EXIT

function info {
    local fmt=$1
    shift

    printf "$fmt\n" "$@"
}

function run {
    libtool --mode=execute ${DEBUG:-env} ${QEMU:-} "$@"
}

function error {
    local fmt=$1
    shift

    printf "ERROR: $fmt\n" "$@"

    let ++ERROR_CNT
}

# <input> <width> <height> <fmt> [<args>*]
function gen_image {
    local fname=$1
    local width=$2
    local height=$3
    local format=$4

    shift 4

    info "Creating image '%s' (%dx%d, args '%s')" "$fname" "$width" "$height" "$*"

    ${QEMU:-} $P/testsuite/gen-image --width "$width" --height "$height" \
			   --format "$format" --bpp 8 "$@" > "$tmp/$fname"

    echo "8"       > "$tmp/$fname:bpp"
    echo "$width"  > "$tmp/$fname:width"
    echo "$height" > "$tmp/$fname:height"
    echo "$format" > "$tmp/$fname:format"
}

# Usage: run_gen_test <name> <input> [<args>*]
function run_test {
    local test_name=$1
    local fname=$2
    local width
    local height
    local bpp
    local format
    local args

    shift 2

    args=( "$@" )

    read width  < "$tmp/$fname:width"
    read height < "$tmp/$fname:height"
    read bpp    < "$tmp/$fname:bpp"
    read format < "$tmp/$fname:format"

    info "Test '%s' on '%s' (%dx%d@%d)" "$test_name" "$fname" "$width" "$height" "$bpp"

    local out=$tmp/$fname:conv:

    rm -f "$out"*

    local m
    for m in "${EXACT_METHODS[@]}"; do
	eval set -- $m

	local method=$1

	run bayer2rgb --width "$width" --height "$height" --input-bpp "$bpp" \
	    --method "$method" --input-format="$format" --silent \
	    "${args[@]}" "$tmp/$fname" "${out}${method}"
    done

    local f
    local ref_img=
    for f in "${out}"*; do
	if test -z "$ref_img"; then
	    ref_img=$f
	    continue
	fi

	cmp -l "$ref_img" "$f" >&2 || error "'$ref_img' and '$f' differ"
    done

    rm -f "$out"*
}

set -e
shopt -s nullglob

images=()
for dim in "${DIM[@]}"; do
    eval set -- $dim

    w=$1
    h=$2

    for col in "${COL[@]}"; do
	eval set -- $col
	base=$1
	shift

	args=( $@ )

	for fmt in "${FMT[@]}"; do
	    img=${base}_${fmt}_${w}x${h}

	    gen_image "$img" "$w" "$h" "$fmt" "${args[@]}"
	    images+=( $img )
	done
    done
done

for dim in "${DIM_FMT_COL[@]}"; do
    eval set -- $dim

    w=$1
    h=$2
    fmt=$3
    base=$4

    shift 4

    args=( $@ )
    img=${base}_${fmt}_${w}x${h}

    gen_image "$img" "$w" "$h" "$fmt" "${args[@]}"
    images+=( $img )
done


for img in "${images[@]}"; do
    run_test "simple" "$img" --quality -all
    run_test "round2" "$img" --quality +round2
    run_test "round4" "$img" --quality +round4
    run_test "round"  "$img" --quality +round
done

test 0 -eq "$ERROR_CNT"
