#! /bin/zsh

# Jonathan Frech, 2021-01-22, 2021-01-23

H=8; W=16

set -e
cd "$(dirname "$(realpath "$0")")"

PROG=visualizing-cycles-in-row-major-transposition-encodings
clang++ -std=c++17 -Wall -Wextra -Wpedantic -Werror -O3 "$PROG.cpp" -o "$PROG"

printf 'h\w |'; for ((w = 1; w <= W; ++w)); do
    printf ' %3d  ' "$w"; done; printf '\n'
printf -- '---'; for ((w = 1; w <= W; ++w)); do
    printf -- '------'; done; printf '\n'

rm -rf ppms && mkdir -p ppms
for ((h = 1; h <= H; ++h)); do
    printf '%3d | ' "$h"
    for ((w = 1; w <= W; ++w)); do
        printf '%3d   ' \
            "$("./$PROG" "$h" "$w" 2>&1 \
                > "ppms/h$(printf '%03d' "$h")-w$(printf '%03d' "$w").ppm")"
    done
    printf '\n'
done
rm -f "$PROG"

rm -rf pngs && mkdir -p pngs
find ppms -mindepth 1 -maxdepth 1 -type f | grep '^ppms/[^.]\+\.ppm$' \
    | while read ppm; do
        convert "$ppm" -format p3 \
            -sample 10000% "pngs/$(basename "$ppm" .ppm).png"
done
