#!/bin/bash # Compare the merging function of stack_png with pamcomp. # # Note that temporary files are generated relative to current directory # as opposed to the default place (usually /tmp). This is needed because # mingw can't access /tmp. set -euo pipefail if [[ $# -ne 4 ]]; then echo "$0 " exit 1 fi STACK_PNG=$1 UNDERLAY=$2 OVERLAY=$3 ALPHA=$4 # Compose expected image with netpbm tools. EXPECTED=$(mktemp -p .) pamcomp -alpha="$ALPHA" "$OVERLAY" "$UNDERLAY" > "$EXPECTED" INPUT1=$(mktemp -p .) INPUT2=$(mktemp -p .) ACTUAL=$(mktemp -p .) # Compose image with the program we want to test. pnmtopng "$UNDERLAY" > "$INPUT1" pnmtopng -alpha="$ALPHA" "$OVERLAY" > "$INPUT2" "./$STACK_PNG" "$INPUT1" "$INPUT2" > "$ACTUAL" # Before checking that output pixels are as expected, try running the same # program using stdin as input, and verify that output pixels are identical. PIPED_OUTPUT=$(mktemp -p .) cat "$INPUT1" | "./$STACK_PNG" - "$INPUT2" > "$PIPED_OUTPUT" if ! ( diff -q "$ACTUAL" "$PIPED_OUTPUT" ); then rm -f "$INPUT1" "$INPUT2" "$EXPECTED" "$ACTUAL" "$PIPED_OUTPUT" echo "Failed to read first image from stdin" exit 1 fi cat "$INPUT2" | "./$STACK_PNG" "$INPUT1" - > "$PIPED_OUTPUT" if ! ( diff -q "$ACTUAL" "$PIPED_OUTPUT" ); then rm -f "$INPUT1" "$INPUT2" "$EXPECTED" "$ACTUAL" "$PIPED_OUTPUT" echo "Failed to read second image from stdin" exit 1 fi DIFF=$(mktemp -p .) pngtopnm "$ACTUAL" | pamarith -difference "$EXPECTED" - > "$DIFF" # Check the output bytes in diff image, allowing for a bit of leeway in # minute differences. The differences are probably due to the slightly # different floating point constants used for gamma correction. if ! ( perl -e ' $bytes = join "", <>; die "Unexpected difference\n" unless( $bytes =~ /^P\d+\s\d+\s\d+\s\d+\s[\x00-\x02]*$/s ); ' "$DIFF" ) ; then rm -f "$INPUT1" "$INPUT2" "$EXPECTED" "$ACTUAL" "$PIPED_OUTPUT" "$DIFF" exit 1 fi rm -f "$INPUT1" "$INPUT2" "$EXPECTED" "$ACTUAL" "$PIPED_OUTPUT" "$DIFF" exit 0