3 # Copyright (c) the JPEG XL Project Authors. All rights reserved.
5 # Use of this source code is governed by a BSD-style
6 # license that can be found in the LICENSE file.
8 # Bisects JPEG XL encoding quality parameter to reach a given
9 # target bits-per-pixel value.
10 # (To be used directly, or as a template for tailored processing.)
12 # Usage: cjxl_bisect_size {input_filename} {output_filename} {target_bpp}
15 # We take the `bisector` tool from $PATH, or, if not available,
16 # try to locate it in the same directory as the current script.
17 # The `get_bpp` helper is taken from the same directory as the current script.
24 script_dir=$(dirname $(readlink -f $0))
25 bisect_tool=$(which bisector)
26 if [ -z $bisect_tool ] ; then
27 bisect_tool="${script_dir}/bisector"
29 jxl_get_bpp_helper="${script_dir}/jxl_get_bpp_helper"
30 # If $CJXL_BIN is set, we use this instead of looking for `cjxl` on $PATH.
33 if [ -z $cjxl_bin ] ; then
37 # Using `identify` from ImageMagick here.
38 num_pixels=$(identify -format "%w*%h\n" /tmp/baseball.png|bc)
40 # Allow 0.5% tolerance in size (--rtol=0.005).
41 exec $bisect_tool --var=BISECT --range=0.01,15.0 --target=$target_size \
43 --cmd="$cjxl_bin --distance=\$BISECT ${input_filename} ${output_filename}_bisect_\$BISECT.jxl ; (find ${output_filename}_bisect_\$BISECT.jxl -printf \"scale=10;%s/$num_pixels\n\" | bc -l)" \
44 --final="mv ${output_filename}_bisect_\$BISECT.jxl ${output_filename}; rm -f ${output_filename}_bisect_*.jxl" \