#!/usr/bin/env bash
# Measure result files: bytes, pixel size, SSIM against the original scaled to the result's size (ffmpeg ssim, lanczos), and fit against a 1,024-byte and a 1,000-byte KB.
# Usage: bash measure.sh <originals dir> <results dir> [prefix filter]
set -uo pipefail
orig=${1:?originals dir}; res=${2:?results dir}
printf "file\tkb_target\tbytes\twidth\theight\tssim\tfits_1024\tfits_1000\n"
for f in "$res"/*.jpg; do
  b=$(basename "$f" .jpg); name=${b%%-*}; t=${b##*-}
  src=$(ls "$orig/$name".* 2>/dev/null | head -n 1)
  case "$t" in custom*) kb=${t#custom};; *) kb=$t;; esac
  bytes=$(wc -c < "$f" | tr -d " ")
  dims=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 "$f")
  w=${dims%,*}; h=${dims#*,}
  ssim=$(ffmpeg -v info -i "$f" -i "$src" -lavfi "[1:v]scale=$w:$h:flags=lanczos[r];[0:v][r]ssim" -f null - 2>&1 | grep -o "All:[0-9.]*" | tail -n 1 | cut -d: -f2)
  f1024=no; [ "$bytes" -le $((kb*1024)) ] && f1024=yes
  f1000=no; [ "$bytes" -le $((kb*1000)) ] && f1000=yes
  printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "$b" "$kb" "$bytes" "$w" "$h" "$ssim" "$f1024" "$f1000"
done
