Imported Upstream version 0.9.0
[platform/upstream/libjxl.git] / tools / scripts / cjxl_bisect_bpp
1 #!/bin/sh
2 #
3 # Copyright (c) the JPEG XL Project Authors. All rights reserved.
4 #
5 # Use of this source code is governed by a BSD-style
6 # license that can be found in the LICENSE file.
7 #
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.)
11 #
12 # Usage: cjxl_bisect_size {input_filename} {output_filename} {target_bpp}
13
14 #
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.
18 #
19
20 input_filename=$1
21 output_filename=$2
22 target_size=$3
23
24 script_dir=$(dirname $(readlink -f $0))
25 bisect_tool=$(which bisector)
26 if [ -z $bisect_tool ] ; then
27   bisect_tool="${script_dir}/bisector"
28 fi
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.
31
32 cjxl_bin=${CJXL_BIN}
33 if [ -z $cjxl_bin ] ; then
34   cjxl_bin="cjxl"
35 fi
36
37 # Using `identify` from ImageMagick here.
38 num_pixels=$(identify -format "%w*%h\n" /tmp/baseball.png|bc)
39
40 # Allow 0.5% tolerance in size (--rtol=0.005).
41 exec $bisect_tool --var=BISECT --range=0.01,15.0 --target=$target_size \
42   --rtol_val=0.005 \
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" \
45   --verbosity=1