decode.sh (2211B)
1 #!/usr/bin/env bash 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 set -euo pipefail 9 10 decoder="$(dirname "$0")"/TAppDecoderHighBitDepthStatic 11 12 usage() { 13 echo "$0 [-v] <input.bin> <output.png>" >&2 14 exit 1 15 } 16 17 verbose=0 18 19 while getopts ':hv' arg; do 20 case "$arg" in 21 h) 22 usage 23 ;; 24 25 v) 26 verbose=1 27 ;; 28 29 \?) 30 echo "Unrecognized option -$OPTARG" >&2 31 exit 1 32 ;; 33 esac 34 done 35 shift $((OPTIND-1)) 36 37 if [ $# -lt 2 ]; then 38 usage 39 fi 40 41 run() { 42 if [ "$verbose" -eq 1 ]; then 43 "$@" 44 else 45 "$@" > /dev/null 2>&1 46 fi 47 } 48 49 input="$1" 50 output="$2" 51 52 bin="$(mktemp)" 53 yuv="$(mktemp)" 54 width_file="$(mktemp)" 55 height_file="$(mktemp)" 56 icc_file="$(mktemp --suffix=.icc)" 57 58 cleanup() { 59 rm -- "$bin" "$yuv" "$width_file" "$height_file" "$icc_file" 60 } 61 trap cleanup EXIT 62 63 unpack_program="$(cat <<'END' 64 use File::Copy; 65 my ($input, $bin, $width_file, $height_file, $icc_file) = @ARGV; 66 open my $input_fh, '<:raw', $input; 67 sysread($input_fh, my $size, 8) == 8 or die; 68 my ($width, $height) = unpack 'NN', $size; 69 open my $width_fh, '>', $width_file; 70 print {$width_fh} "$width\n"; 71 open my $height_fh, '>', $height_file; 72 print {$height_fh} "$height\n"; 73 sysread($input_fh, my $icc_size, 4) == 4 or die; 74 $icc_size = unpack 'N', $icc_size; 75 sysread($input_fh, my $icc_data, $icc_size) == $icc_size or die; 76 open my $icc_fh, '>', $icc_file; 77 print {$icc_fh} $icc_data; 78 copy $input_fh, $bin; 79 END 80 )" 81 run perl -Mstrict -Mwarnings -Mautodie -e "$unpack_program" -- "$input" "$bin" "$width_file" "$height_file" "$icc_file" 82 83 width="$(cat "$width_file")" 84 height="$(cat "$height_file")" 85 86 start="$EPOCHREALTIME" 87 run "$decoder" --OutputBitDepth=10 -b "$bin" -o "$yuv" 88 end="$EPOCHREALTIME" 89 90 elapsed="$(echo "$end - $start" | bc)" 91 run echo "Completed in $elapsed seconds" 92 93 echo "$elapsed" > "${output%.png}".time 94 95 run ffmpeg -hide_banner -f rawvideo -vcodec rawvideo -s "${width}x$height" -r 25 -pix_fmt yuv444p10le -i "$yuv" -pix_fmt rgb24 -vf scale=in_color_matrix=bt709 -y "$output" 96 if [ -s "$icc_file" ]; then 97 mogrify -profile "$icc_file" "$output" 98 fi