#!/usr/bin/env bash
# ==========================================================================
#         ____            _                     _____           _
#        / ___| _   _ ___| |_ ___ _ __ ___     |_   _|__   ___ | |___
#        \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __|
#         ___) | |_| \__ \ ||  __/ | | | | |_____| | (_) | (_) | \__ \
#        |____/ \__, |___/\__\___|_| |_| |_|     |_|\___/ \___/|_|___/
#               |___/
#                             --- System-Tools ---
#                  https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# GIMP Scripts
# Copyright (C) 2013-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

# Bash options:
set -eu


# Example:
# Make-Mosaic Image1.jpeg Mosaic1.png 24 4 4 --type hexagons --surface rough
# Make-Mosaic Image1.jpeg Mosaic2.png 32 4 2 --type octagons --surface smooth


# ====== Handle arguments ===================================================
usage () {
   local exitCode="$1"
   echo >&2 "Usage: $0 input_filename output_filename tile_size tile_height tile_spacing [--type|-t squares|hexagons|octagons|triangles] [--surface|-s smooth|rough] [--neatness|-n neatness] [--lightdirection|-l direction] [-c|--colorvariation variation] [--verbose|-v] [--quiet|-q] [--help|-h]"
   echo >&2 "Example: $0 Image.jpeg Mosaic.png 24 4 4 --type hexagons --surface rough"
   exit "${exitCode}"
}

GETOPT="$(PATH=/usr/local/bin:$PATH which getopt)"
# shellcheck disable=SC2068
options="$(${GETOPT} -o t:s:n:l:c:vqh --long type:,surface:,neatness:,lightdirection:,colorvariation:,verbose,quiet,help -a -- "$@")"
eval set -- "${options}"

TILE_NEATNESS=0.65
TILE_TYPESTRING="hexagons"
TILE_TYPE="6"
TILE_SURFACE=0
LIGHT_DIRECTION=135
COLOR_VARIATION=0.2
VERBOSE=0
while [ $# -gt 0 ] ; do
   case "$1" in
      -t | --type)
         TILE_TYPESTRING="$2"
         shift 2
         ;;
      -s | --surface)
         if [ "$2" == "smooth" ] ; then
            TILE_SURFACE=0
         elif [ "$2" == "rough" ] ; then
            TILE_SURFACE=1
         else
            echo >&2 "Invalid tile surface: $2"
            exit 1
         fi
         shift 2
         ;;
      -n | --neatness)
         TILE_NEATNESS="$2"
         shift 2
         ;;
      -l | --lightdirection)
         LIGHT_DIRECTION="$2"
         shift 2
         ;;
      -c | --colorvariation)
         COLOR_VARIATION="$2"
         shift 2
         ;;
      -v | --verbose)
         VERBOSE=1
         shift
         ;;
      -q | --quiet)
         VERBOSE=0
         shift
         ;;
      -h | --help)
         usage 0
         ;;
      --)
         shift
         break
         ;;
      *)
         usage 1
         ;;
  esac
done

if [ $# -ne 5 ] ; then
   usage 1
fi
INPUT_FILENAME="$1"
OUTPUT_FILENAME="$2"
TILE_SIZE=$3
TILE_HEIGHT=$4
TILE_SPACING=$5

if [ ! -f "${INPUT_FILENAME}" ] ; then
   echo >&2 "ERROR: Input file ${INPUT_FILENAME} does not exist!"
   exit 1
fi
if [[ ! "${TILE_SIZE}" =~ ^[0-9]+$ ]] ; then
   echo >&2 "ERROR: Invalid tile size!"
   exit 1
fi
if [[ ! "${TILE_HEIGHT}" =~ ^[0-9]+$ ]] ; then
   echo >&2 "ERROR: Invalid tile height!"
   exit 1
fi
if [[ ! "${TILE_SPACING}" =~ ^[0-9]+$ ]] ; then
   echo >&2 "ERROR: Invalid tile spacing!"
   exit 1
fi
if [ "${TILE_TYPESTRING}" = "squares" ] ; then
   TILE_TYPE=0
elif [ "${TILE_TYPESTRING}" = "hexagons" ] ; then
   TILE_TYPE=1
elif [ "${TILE_TYPESTRING}" = "octagons" ] ; then
   TILE_TYPE=2
elif [ "${TILE_TYPESTRING}" = "triangles" ] ; then
   TILE_TYPE=3
else
   echo >&2 "Invalid tile type ${TILE_TYPESTRING}!"
   exit 1
fi
if [[ ! "${TILE_NEATNESS}" =~ ^([0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
   echo >&2 "ERROR: Invalid tile neatness ${TILE_NEATNESS}!"
   exit 1
fi
if [[ ! "${COLOR_VARIATION}" =~ ^([0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
   echo >&2 "ERROR: Invalid color variation ${COLOR_VARIATION}!"
   exit 1
fi
if [[ ! "${LIGHT_DIRECTION}" =~ ^([0-9]+)$ ]] ; then
   echo >&2 "ERROR: Invalid light direction ${LIGHT_DIRECTION}!"
   exit 1
fi


# ====== Obtain GIMP call options ===========================================
if ! whereis gimp-console >/dev/null ; then
   echo >&2 "ERROR: Gimp is not available!"
   exit 1
fi
GIMP_VERSION="$(LANG=C gimp-console --version | sed -e 's/GNU Image Manipulation Program version //')"
if [[ "${GIMP_VERSION}" =~ ^[012] ]] ; then
   # Gimp 2.x:
   GIMP_OPTIONS="--new-instance --no-interface --no-splash --batch-interpreter plug-in-script-fu-eval --batch -"
else
   # Gimp 3.x:
   GIMP_OPTIONS="--quit --new-instance --no-interface --no-splash --batch-interpreter plug-in-script-fu-eval --batch -"
fi

# This script does not use fonts, or gradients/palettes/brushes:
GIMP_OPTIONS="${GIMP_OPTIONS} --no-fonts --no-data"


# ====== Call GIMP ==========================================================
rm -f "${OUTPUT_FILENAME}"
# shellcheck disable=SC2086
( cat <<EOF
(let*
   ; ------ Initialise and load image ---------------------------------------
   ((inputFileName    "${INPUT_FILENAME}")
    (outputFileName   "${OUTPUT_FILENAME}")
    (tileType         ${TILE_TYPE})
    (tileTypeString   "${TILE_TYPESTRING}")
    (tileSize         ${TILE_SIZE})
    (tileHeight       ${TILE_HEIGHT})
    (tileSpacing      ${TILE_SPACING})
    (tileNeatness     ${TILE_NEATNESS})
    (tileSurface      ${TILE_SURFACE})
    (lightDirection   ${LIGHT_DIRECTION})
    (colorVariation   ${COLOR_VARIATION})
    (image            (car (gimp-file-load RUN-NONINTERACTIVE inputFileName inputFileName)))
    (imageType        (if (not (defined? 'gimp-image-get-active-layer))
                         ; New Gimp 3.0 API:
                         (car (gimp-image-get-base-type image))
                         ; Old Gimp 2.x API:
                         (car (gimp-image-base-type image)))
                      )
    (layer            (if (not (defined? 'gimp-image-get-active-layer))
                         ; New Gimp 3.0 API:
                         (car (list (vector-ref (car (gimp-image-get-selected-layers image)) 0)))
                         ; Old Gimp 2.x API:
                         (car (gimp-image-get-active-layer image)))
                      )
   )

   ; ------ Apply filter ----------------------------------------------------
   ; First, make sure that the image is RGB, not indexed color mode!
   (if (not (= RGB imageType))
      (gimp-image-convert-rgb image))

   ; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   ; GEGL Mosaic options: https://gegl.org/operations/gegl-mosaic.html
   ; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   (cond( (not(defined? 'plug-in-mosaic))
      ; New Gimp 3.0 API:
      (gimp-drawable-merge-new-filter layer "gegl:mosaic" 0 LAYER-MODE-REPLACE 1.0
          "tile-size"       tileSize
          "tile-height"     tileHeight
          "tile-spacing"    tileSpacing
          "tile-neatness"   tileNeatness
          "antialiasing"    TRUE
          "light-dir"       lightDirection
          "color-variation" colorVariation
          "tile-type"       tileTypeString
          "tile-surface"    tileSurface
          "seed"            0
      )
   )
   (else
      ; Old Gimp 2.x API:
      (plug-in-mosaic RUN-NONINTERACTIVE image layer
         tileSize tileHeight tileSpacing
         tileNeatness TRUE lightDirection colorVariation TRUE TRUE
         tileType tileSurface FALSE
   )))

   ; ------ Save result -----------------------------------------------------
   (if (defined? 'gimp-drawable-merge-new-filter)
      (begin  ; New Gimp >= 3.0 API:
         ; FIXME: This does not work when running in Gimp 2.x!
         ; (file-png-export
         ;    #:run-mode         RUN-NONINTERACTIVE
         ;    #:image            image
         ;    #:file             outputFileName
         ;    #:options          -1
         ;    #:interlaced       TRUE
         ;    #:compression      6
         ;    #:bkgd             TRUE
         ;    #:offs             FALSE
         ;    #:phys             TRUE
         ;    #:time             TRUE
         ;    #:save-transparent FALSE
         ;    #:optimize-palette TRUE
         ; )
         (file-png-export RUN-NONINTERACTIVE image
            outputFileName -1
            TRUE 6 TRUE FALSE TRUE TRUE FALSE TRUE)
      )
      (begin  ; Old Gimp < 3.0 API:
         (file-png-save2 RUN-NONINTERACTIVE image
            (car (gimp-image-get-active-layer image))
            outputFileName outputFileName
            TRUE 6 TRUE TRUE FALSE TRUE TRUE FALSE TRUE)
      )
   )

   ; ------ Clean up --------------------------------------------------------
   (gimp-image-delete image)
)
(gimp-quit TRUE)
EOF
) | env LANG=C HOME=/tmp gimp-console ${GIMP_OPTIONS} 2>&1 | \
(
   if [ ${VERBOSE} -ne 0 ] ; then
      cat
   else
      grep -vE "^ts>|(#t)|^Copyright|Welcome to (GIMP|TinyScheme)|^using gegl copy|scriptfu-WARNING|-WARNING|^Please use named arguments:|^script quit with code:|^$" || true
   fi
)


# ====== Check result =======================================================
if [ ! -e "${OUTPUT_FILENAME}" ] ; then
   echo >&2 "ERROR: ${OUTPUT_FILENAME} has not been produced. Something went wrong!"
   exit 1
fi
