#!/bin/bash # Verify that stack_png can handle reading from the same file multiple times. set -euo pipefail if [[ $# -ne 1 ]]; then echo "$0 " exit 1 fi STACK_PNG=$1 # Build input. INPUT_PIXELS=$(mktemp -p .) cat < "$INPUT_PIXELS" P2 4 3 255 255 255 255 255 255 255 255 255 255 255 255 255 EOT INPUT_ALPHA=$(mktemp -p .) cat < "$INPUT_ALPHA" P2 4 3 255 128 128 128 64 128 128 64 128 128 64 128 128 EOT INPUT_PNG=$(mktemp -p .) pnmtopng -alpha="$INPUT_ALPHA" "$INPUT_PIXELS" > "$INPUT_PNG" # Run tool. ACTUAL=$(mktemp -p .) "./$STACK_PNG" "$INPUT_PNG" "$INPUT_PNG" > "$ACTUAL" # Actual pixels should be the same as input pixels, # the only difference would be in the alpha channel. DIFF=$(mktemp -p .) pngtopam "$ACTUAL" \ | ppmtopgm \ | pamarith -difference "$INPUT_PIXELS" - > "$DIFF" if ! ( perl -e ' $bytes = join "", <>; die "Unexpected difference\n" unless( $bytes =~ /^P\d+\s\d+\s\d+\s\d+\s[\x00]*$/s );' "$DIFF" ); then rm -f "$INPUT_PIXELS" "$INPUT_ALPHA" "$INPUT_PNG" "$ACTUAL" "$DIFF" exit 1 fi # Compare alpha channel. cat < "$INPUT_ALPHA" P2 4 3 255 191 191 191 111 191 191 111 191 191 111 191 191 EOT pngtopam -alpha "$ACTUAL" \ | ppmtopgm \ | pamarith -difference "$INPUT_ALPHA" - > "$DIFF" if ! ( perl -e ' $bytes = join "", <>; die "Unexpected difference\n" unless( $bytes =~ /^P\d+\s\d+\s\d+\s\d+\s[\x00]*$/s );' "$DIFF" ); then rm -f "$INPUT_PIXELS" "$INPUT_ALPHA" "$INPUT_PNG" "$ACTUAL" "$DIFF" exit 1 fi # Do the same test again, but this time with input fed from stdin. # Output should be byte-for-byte identical with what we got when # reading from file. if ! ( cat "$INPUT_PNG" | "./$STACK_PNG" - - | diff -q "$ACTUAL" - ); then echo "$LINENO: Output mismatched when reading from stdin" rm -f "$INPUT_PIXELS" "$INPUT_ALPHA" "$INPUT_PNG" "$ACTUAL" "$DIFF" exit 1 fi rm -f "$INPUT_PIXELS" "$INPUT_ALPHA" "$INPUT_PNG" "$ACTUAL" "$DIFF" exit 0