From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- testfiles/cli_tests/l2compare.sh | 91 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 testfiles/cli_tests/l2compare.sh (limited to 'testfiles/cli_tests/l2compare.sh') diff --git a/testfiles/cli_tests/l2compare.sh b/testfiles/cli_tests/l2compare.sh new file mode 100755 index 0000000..cbb5540 --- /dev/null +++ b/testfiles/cli_tests/l2compare.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Convert an image (or a single page of a PDF/PostScript document) to a bitmap +# and calculate the relative root-mean-squared (L2) distance from the reference. +# +# Authors: +# Rafael Siejakowski +# +# Copyright (C) 2022 Authors +# +# Released under GNU GPL v2+, read the file 'COPYING' for more information. +# + +MY_LOCATION=$(dirname "$0") +source "${MY_LOCATION}/../utils/functions.sh" + +ensure_command "convert" +ensure_command "compare" +ensure_command "bc" +ensure_command "cp" + +OUTPUT_FILENAME="$1" +OUTPUT_PAGE="$2" +REFERENCE_FILENAME="$3" +PERCENTAGE_DIFFERENCE_ALLOWED="$4" +DPI="$5" + +if [ ! -f "${OUTPUT_FILENAME}" ] +then + echo "Error: Test file '${OUTPUT_FILENAME}' not found." + exit 1 +fi + +if [ ! -f "${REFERENCE_FILENAME}" ] +then + echo "Error: Reference file '${REFERENCE_FILENAME}' not found." + exit 1 +fi + +# Convert the output file to the PNG format +CONVERSION_OPTIONS="-colorspace RGB" + +# Extract a page from multipage PS/PDF if requested +OUTFILE_SUFFIX="" +if [[ "x$OUTPUT_PAGE" != "x" ]] +then + OUTFILE_SUFFIX="[${OUTPUT_PAGE}]" # Use ImageMagick's bracket operator +fi + +DPI_OPTION="" +if [[ "x$DPI" != "x" ]] +then + DPI_OPTION="-density $DPI" +fi + +if [[ $(identify -format "%m" "${OUTPUT_FILENAME}") != "PNG" ]] +then + if ! convert $DPI_OPTION "${OUTPUT_FILENAME}${OUTFILE_SUFFIX}" $CONVERSION_OPTIONS "${OUTPUT_FILENAME}-output.png" + then + echo "Warning: Failed to convert test file '${OUTPUT_FILENAME}' to PNG format. Skipping comparison test." + exit 42 + fi +else + cp "${OUTPUT_FILENAME}" "${OUTPUT_FILENAME}-output.png" +fi + + +# Copy the reference file +cp "${REFERENCE_FILENAME}" "${OUTPUT_FILENAME}-reference.png" + +# Compare the two files +COMPARE_OUTPUT=$(compare 2>&1 -metric RMSE "${OUTPUT_FILENAME}-output.png" "${OUTPUT_FILENAME}-reference.png" \ + "${OUTPUT_FILENAME}-diff.png") +RELATIVE_ERROR=$(get_compare_result "$COMPARE_OUTPUT") +PERCENTAGE_ERROR=$(fraction_to_percentage "$RELATIVE_ERROR") + +if (( $(is_relative_error_within_tolerance "$RELATIVE_ERROR" "$PERCENTAGE_DIFFERENCE_ALLOWED") )) +then + # Test passed: print stats and clean up the files. + echo "Fuzzy comparison PASSED; error of ${PERCENTAGE_ERROR}% is within ${PERCENTAGE_DIFFERENCE_ALLOWED}% tolerance." + for FILE in ${OUTPUT_FILENAME}{,-reference.png,-output.png,-diff.png} + do + rm -f "${FILE}" + done +else + # Test failed! + echo "Fuzzy comparison FAILED; error of ${PERCENTAGE_ERROR}% exceeds ${PERCENTAGE_DIFFERENCE_ALLOWED}% tolerance." + exit 1 +fi + -- cgit v1.2.3