#!/bin/bash
#
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# prep-frames: the companion's generated images to the frames she shows.
#
#   prep-frames [ART_DIR]     default: ~/.local/share/keystone/companion/2b
#
# Reads ART_DIR/dress/*.png and ART_DIR/leotard/*.png, 1024x1024 on white,
# and writes ART_DIR/frames/<outfit>/<name>.png: background removed, trimmed
# to her, scaled by a quarter. One scale for every frame keeps her the same
# size whatever the pose, which scaling each to a fixed height would not:
# lying down would come out huge. Sprite.qml draws them at half size and
# stands each on its bottom edge.
#
# The background goes by flood fill from the four corners: the dark line art
# stops the fill, so the white hair and the white leotard inside it stay.

set -euo pipefail

art=${1:-$HOME/.local/share/keystone/companion/2b}
n=0
for outfit in dress leotard; do
    [[ -d $art/$outfit ]] || continue
    mkdir -p "$art/frames/$outfit"
    for src in "$art/$outfit"/*.png; do
        [[ -e $src ]] || continue
        read -r w h < <(magick identify -format '%w %h\n' "$src")
        magick "$src" -alpha set -fuzz 6% -fill none \
            -draw "color 0,0 floodfill" -draw "color $((w - 1)),0 floodfill" \
            -draw "color 0,$((h - 1)) floodfill" -draw "color $((w - 1)),$((h - 1)) floodfill" \
            -trim +repage -resize 25% "$art/frames/$outfit/$(basename "$src")"
        n=$((n + 1))
    done
done
echo "prep-frames: $n frames in $art/frames"
