Imported Upstream version 0.7.0
[platform/upstream/libjxl.git] / tools / scripts / roundtrip_test.sh
1 #!/bin/bash
2 # Copyright (c) the JPEG XL Project Authors. All rights reserved.
3 #
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
6
7 # End-to-end roundtrip tests for cjxl and djxl tools.
8
9 MYDIR=$(dirname $(realpath "$0"))
10 JPEGXL_TEST_DATA_PATH="${MYDIR}/../../testdata"
11
12 set -eux
13
14 # Temporary files cleanup hooks.
15 CLEANUP_FILES=()
16 cleanup() {
17   if [[ ${#CLEANUP_FILES[@]} -ne 0 ]]; then
18     rm -rf "${CLEANUP_FILES[@]}"
19   fi
20 }
21 trap 'retcode=$?; { set +x; } 2>/dev/null; cleanup' INT TERM EXIT
22
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}"
27
28   "${encoder}" "${infn}" "${jxlfn}" -d 0 -e 1
29   "${decoder}" "${jxlfn}" "${outfn}"
30   diff "${infn}" "${outfn}"
31 }
32
33 roundtrip_test() {
34   local infn="${JPEGXL_TEST_DATA_PATH}/$1"
35   local encargs="$2"
36   local maxdist="$3"
37   local jxlfn="$(mktemp -p "$tmpdir")"
38
39   "${encoder}" "${infn}" "${jxlfn}" $encargs
40
41   if [ "${infn: -3}" == "jpg" ]; then
42       local outfn="$(mktemp -p "$tmpdir").jpg"
43
44       # Test losless jpeg reconstruction.
45       "${decoder}" "${jxlfn}" "${outfn}" --num_reps 2
46       diff "${infn}" "${outfn}"
47
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)"
53       
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)"
59
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)"
66   else
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})"
72
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)"
77
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})"
83
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})"
89
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)"
94
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)"
100   fi
101 }
102
103 main() {
104   local tmpdir=$(mktemp -d)
105   CLEANUP_FILES+=("${tmpdir}")
106
107   local build_dir="${1:-}"
108   if [[ -z "${build_dir}" ]]; then
109     build_dir=$(realpath "${MYDIR}/../../build")
110   fi
111
112   local encoder="${build_dir}/tools/cjxl"
113   local decoder="${build_dir}/tools/djxl"
114   local comparator="${build_dir}/tools/ssimulacra_main"
115
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
127
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"
135   done
136 }
137
138 main "$@"