2 # Copyright (c) the JPEG XL Project Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
7 # End-to-end roundtrip tests for cjxl and djxl tools.
9 MYDIR=$(dirname $(realpath "$0"))
10 JPEGXL_TEST_DATA_PATH="${MYDIR}/../../testdata"
14 # Temporary files cleanup hooks.
17 if [[ ${#CLEANUP_FILES[@]} -ne 0 ]]; then
18 rm -rf "${CLEANUP_FILES[@]}"
21 trap 'retcode=$?; { set +x; } 2>/dev/null; cleanup' INT TERM EXIT
23 roundtrip_lossless_pnm_test() {
24 local infn="${JPEGXL_TEST_DATA_PATH}/$1"
25 local jxlfn="$(mktemp -p "$tmpdir")"
26 local outfn="$(mktemp -p "$tmpdir").${infn: -3}"
28 "${encoder}" "${infn}" "${jxlfn}" -d 0 -e 1
29 "${decoder}" "${jxlfn}" "${outfn}"
30 diff "${infn}" "${outfn}"
34 local infn="${JPEGXL_TEST_DATA_PATH}/$1"
37 local jxlfn="$(mktemp -p "$tmpdir")"
39 "${encoder}" "${infn}" "${jxlfn}" $encargs
41 if [ "${infn: -3}" == "jpg" ]; then
42 local outfn="$(mktemp -p "$tmpdir").jpg"
44 # Test losless jpeg reconstruction.
45 "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2
46 diff "${infn}" "${outfn}"
48 # Test decoding to pixels.
49 "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2 --pixels_to_jpeg
50 local dist="$("${comparator}" "${infn}" "${outfn}")"
51 python3 -c "import sys; sys.exit(not ${dist} > 0.0)"
52 python3 -c "import sys; sys.exit(not ${dist} < 0.005)"
54 # Test decoding to pixels by setting the --jpeg_quality flag.
55 "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2 --jpeg_quality 100
56 local dist="$("${comparator}" "${infn}" "${outfn}")"
57 python3 -c "import sys; sys.exit(not ${dist} > 0.0)"
58 python3 -c "import sys; sys.exit(not ${dist} < 0.005)"
60 # Test decoding to pixels by writing to a png.
61 outfn="$(mktemp -p "$tmpdir").png"
62 "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2
63 local dist="$("${comparator}" "${infn}" "${outfn}")"
64 python3 -c "import sys; sys.exit(not ${dist} > 0.0)"
65 python3 -c "import sys; sys.exit(not ${dist} < 0.005)"
67 # Test decoding to png.
68 local outfn="$(mktemp -p "$tmpdir").png"
69 "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2
70 local dist="$("${comparator}" "${infn}" "${outfn}")"
71 python3 -c "import sys; sys.exit(not ${dist} <= ${maxdist})"
73 # Test decoding to 16 bit png.
74 "${decoder}" "${jxlfn}" "${outfn}" --bits_per_sample 16
75 local dist="$("${comparator}" "${infn}" "${outfn}")"
76 python3 -c "import sys; sys.exit(not ${dist} <= ${maxdist} + 0.0005)"
78 # Test decoding to pfm.
79 local outfn="$(mktemp -p "$tmpdir").pfm"
80 "${decoder}" "${jxlfn}" "${outfn}"
81 local dist="$("${comparator}" "${infn}" "${outfn}")"
82 python3 -c "import sys; sys.exit(not ${dist} <= ${maxdist})"
84 # Test decoding to ppm.
85 local outfn="$(mktemp -p "$tmpdir").ppm"
86 "${decoder}" "${jxlfn}" "${outfn}"
87 local dist="$("${comparator}" "${infn}" "${outfn}")"
88 python3 -c "import sys; sys.exit(not ${dist} <= ${maxdist})"
90 # Test decoding to 16 bit ppm.
91 "${decoder}" "${jxlfn}" "${outfn}" --bits_per_sample 16
92 local dist="$("${comparator}" "${infn}" "${outfn}")"
93 python3 -c "import sys; sys.exit(not ${dist} <= ${maxdist} + 0.0005)"
95 # Test decoding to jpg.
96 outfn="$(mktemp -p "$tmpdir").jpg"
97 "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2
98 local dist="$("${comparator}" "${infn}" "${outfn}")"
99 python3 -c "import sys; sys.exit(not ${dist} <= ${maxdist} + 0.05)"
104 local tmpdir=$(mktemp -d)
105 CLEANUP_FILES+=("${tmpdir}")
107 local build_dir="${1:-}"
108 if [[ -z "${build_dir}" ]]; then
109 build_dir=$(realpath "${MYDIR}/../../build")
112 local encoder="${build_dir}/tools/cjxl"
113 local decoder="${build_dir}/tools/djxl"
114 local comparator="${build_dir}/tools/ssimulacra_main"
116 roundtrip_test "jxl/flower/flower_small.rgb.png" "-e 1" 0.02
117 roundtrip_test "jxl/flower/flower_small.rgb.png" "-e 1 -d 0.0" 0.0
118 roundtrip_test "jxl/flower/flower_small.rgb.depth8.ppm" \
119 "-e 1 --streaming_input" 0.02
120 roundtrip_test "jxl/flower/flower_small.rgb.depth8.ppm" \
121 "-e 1 -d 0.0 --streaming_input" 0.0
122 roundtrip_test "jxl/flower/flower_small.rgb.depth8.ppm" \
123 "-e 1 --streaming_output" 0.02
124 roundtrip_test "jxl/flower/flower_small.rgb.depth8.ppm" \
125 "-e 1 -d 0.0 --streaming_input --streaming_output" 0.0
126 roundtrip_test "jxl/flower/flower_cropped.jpg" "-e 1" 0.0
128 roundtrip_lossless_pnm_test "jxl/flower/flower_small.rgb.depth1.ppm"
129 roundtrip_lossless_pnm_test "jxl/flower/flower_small.g.depth1.pgm"
130 for i in `seq 2 16`; do
131 roundtrip_lossless_pnm_test "jxl/flower/flower_small.rgb.depth$i.ppm"
132 roundtrip_lossless_pnm_test "jxl/flower/flower_small.g.depth$i.pgm"
133 roundtrip_lossless_pnm_test "jxl/flower/flower_small.ga.depth$i.pam"
134 roundtrip_lossless_pnm_test "jxl/flower/flower_small.rgba.depth$i.pam"