#!/usr/bin/env bash # imgDiagnose.sh - run imgStats / imgDiff on PNG or PPM inputs. # PNGs are converted to PPM via ImageMagick into a sibling tmp file # so the C tools never see the PNG and Claude never has to Read it. # # Usage: # imgDiagnose.sh stats # single-image text summary # imgDiagnose.sh diff [--ascii] # pairwise text diff # # Inputs may be .png, .ppm, or .pgm. PNG inputs are converted via # `convert in.png ppm:tmpfile`. If two inputs differ in size, the # larger one is resized to match the smaller (nearest neighbour) so # differing PNG render scales (e.g. MAME 704x231 vs port 1120x768) # can still be diffed without a manual pre-step. set -euo pipefail # Tools live in port/tools/.c and are built by port/Makefile into # port/bin/. Resolve those paths from the script location. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PORT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" BIN_DIR="$PORT_DIR/bin" TMP_DIR="$PORT_DIR/../tmp" DIFF_BIN="$BIN_DIR/imgDiff" STATS_BIN="$BIN_DIR/imgStats" if [[ ! -x "$DIFF_BIN" || ! -x "$STATS_BIN" ]]; then (cd "$PORT_DIR" && make -s tools) >&2 fi toPpm() { local in="$1" local out="$2" local ext="${in##*.}" if [[ "$ext" == "ppm" || "$ext" == "pgm" ]]; then cp "$in" "$out" else convert "$in" -depth 8 "ppm:$out" fi } cmd="${1:-}" shift || true case "$cmd" in stats) if [[ $# -ne 1 ]]; then echo "usage: imgDiagnose.sh stats " >&2 exit 2 fi tmp=$(mktemp --tmpdir="$TMP_DIR" imgDiag.XXXXXX.ppm) trap 'rm -f "$tmp"' EXIT toPpm "$1" "$tmp" "$STATS_BIN" "$tmp" ;; diff) if [[ $# -lt 2 ]]; then echo "usage: imgDiagnose.sh diff [--ascii]" >&2 exit 2 fi a="$1"; shift b="$1"; shift extra=("$@") ta=$(mktemp --tmpdir="$TMP_DIR" imgDiagA.XXXXXX.ppm) tb=$(mktemp --tmpdir="$TMP_DIR" imgDiagB.XXXXXX.ppm) trap 'rm -f "$ta" "$tb"' EXIT toPpm "$a" "$ta" toPpm "$b" "$tb" # Match sizes: resize the larger to the smaller (nearest neighbour). wa=$(identify -format "%w" "$ta"); ha=$(identify -format "%h" "$ta") wb=$(identify -format "%w" "$tb"); hb=$(identify -format "%h" "$tb") if [[ "$wa" != "$wb" || "$ha" != "$hb" ]]; then if (( wa * ha >= wb * hb )); then convert "$ta" -filter point -resize "${wb}x${hb}!" -depth 8 "ppm:$ta" else convert "$tb" -filter point -resize "${wa}x${ha}!" -depth 8 "ppm:$tb" fi fi "$DIFF_BIN" "$ta" "$tb" "${extra[@]}" ;; *) cat >&2 < imgDiagnose.sh diff [--ascii] EOF exit 2 ;; esac