#!/usr/bin/env bash
# This script creates the test images from the `test_image_1200.xcf` GIMP image.
# This script is considered a “workbench tool”, it's not part of the build chain.
# The results are commited to the repository.
#
# The output has to be checked manually for validity. The output files are used 
# as test input and for visual comparison in the allmytoes test suite.
#
# The `test_images` dir must be the working dir when called.
#
# xcf2png and imagemagick need to be installed.
#
# Current "test-cat" from here: https://pixabay.com/de/vectors/katze-anime-h%C3%BCbsch-k%C3%A4tzchen-4772436/

set -e

base_xcf="test_image_1200.xcf"
base_png="test_image_1200.png"

# Use GIMP for creating the base of everything.
# Thanks to https://stackoverflow.com/a/62078408/13156783
{
cat <<EOF
(define (convert-xcf-png filename outpath)
    (let* (
            (image (car (gimp-xcf-load RUN-NONINTERACTIVE filename filename )))
            (drawable (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))
            )
        (begin (display "Exporting ")(display filename)(display " -> ")(display outpath)(newline))
        (file-png-save2 RUN-NONINTERACTIVE image drawable outpath outpath 0 9 0 0 0 0 0 0 0)
        (gimp-image-delete image)
    )
)

(gimp-message-set-handler 1) ; Messages to standard output
EOF

echo "(convert-xcf-png \"$base_xcf\" \"$base_png\")"

echo "(gimp-quit 0)"

} | gimp -i -b -


file="test_image_1200_16bit_rgb.png"
if [ ! -f "$file" ]; then
	convert $base_png PNG64:$file
fi

file="test_image_1200.jpg"
if [ ! -f "$file" ]; then
	convert $base_png $file
fi

file="test_image_thumb_normal.png"
if [ ! -f "$file" ]; then
	convert $base_png -resize 128x128 $file
fi

file="test_image_thumb_large.png"
if [ ! -f "$file" ]; then
	convert $base_png -resize 256x256 $file
fi

file="test_image_thumb_x-large.png"
if [ ! -f "$file" ]; then
	convert $base_png -resize 512x512 $file
fi

file="test_image_thumb_xx-large.png"
if [ ! -f "$file" ]; then
	convert $base_png -resize 1024x1024 $file
fi
