#!/bin/bash

cd "${0%/*}"




shrink_image () {

local tw ht fw fh dest path path2 iw ih pw ph dest rc final

tw=$1   ; shift
th=$1   ; shift
path=$1 ; shift
fw=$1   ; shift
fh=$1   ; shift
dest=$1 ; shift

if [ -z "$dest" ] ; then
  #  replace original with scaled
  dest="$path"
else
  if [ -d "$dest" ] ; then
    # save scaled to different folder, using same name
    dest="${dest}/${path##*/}"
  else
    # save scaled to specified path
    test
  fi
  if [ "$dest" == "$path" ] ; then
    echo "specified scaled is same as original path" 1>&2
    false ; return
  fi
fi

if [ ! -f "$path" ] ; then
  echo "original not found: \"$path\"" 1>&2
  false ; return
elif [ -f "$dest" ] ; then
  echo "scaled already exists: \"$dest\"" 1>&2
  false ; return
fi

echo
echo "   path: \"$path\""
echo "   dest: \"$dest\""
echo "   orig: $fw x $fh"
echo "   dest: \"$dest\""



# determine size of scaled down image before adding padding (must fit within final image size)
# also determine size of padding
# try fitting with and padding height
((iw=tw))
((ih=fh*iw/fw))
((pw=0))
#((ph=(th-ih/2)))
((ph=(th-ih)/2))
if [ $ph == 0 ] ; then
  # scaled ratio is same as original
  echo "scaling: fit width and height (no padding required)"
elif [ $ih -le $th ] ; then
  echo "scaling: fit width, pad height"
else
  # that will be too high, so fit height and pad width
  ((ih=th))
  ((iw=fw*ih/fh))
  ((ph=0))
#  ((pw=(tw-iw/2)))
  ((pw=(tw-iw)/2))
  echo "scaling: fit height, pad width"
fi

echo " scaled: $iw x $ih"
echo "padding: $pw x $ph"

if [ "$dest" == "$path" ] ; then
  final="$dest"
  dest="temp_${dest}"
fi
echo "  final: \"$final\""

echo


# ffmpeg -y -i "max/pic tall.png" -vf "scale=40:130,format=pix_fmts=rgba,pad=200:130:80:0:color=0xFFFFFFFF" "thumb/pic tall.png"
# first x,y is scaled down image size before adding pad
# second x,y is final image size
# third x,y is size of pads on each side

# specify pad color "FFFFFF00" to pad with transparent pixels (GIF/PNG/TIFF output files only)
#ffmpeg -y -i "$path" -vf "scale=$iw:$ih,format=pix_fmts=rgba,pad=$tw:$th:$pw:$ph:color=0xFFFFFF00" "$path2"

echo "CMD: ffmpeg -y -i \"$path\" -vf \"scale=$iw:$ih,format=pix_fmts=rgba,pad=$tw:$th:$pw:$ph:color=0xFFFFFFFF\" \"$dest\""
ffmpeg -y -i "$path" -vf "scale=$iw:$ih,format=pix_fmts=rgba,pad=$tw:$th:$pw:$ph:color=0xFFFFFFFF" "$dest"
rc=$?
if [ "$rc" != 0 ] ; then
  echo "FFMPEG returned code $rc"
elif [ ! -f "$dest" ] ; then
  echo "FFMPEG returned OK but did not create file at \"$dest\"" 1>&2
  false ; return
fi

# replace original if necessary
if [ -n "$final" ] ; then
  if [ -f "$final" ] ; then
    rm "$final"
  fi
  mv "$dest" "$final"
fi

true ; return


}




clear

shrink_image 200 130 "max/pic average.png" 1665 1039 "thumb"
shrink_image 200 130 "max/pic tall.png"     559 1279 "thumb"
shrink_image 200 130 "max/pic wide.png"    2265  284 "thumb"
shrink_image 200 130 "max/pic no pad.png"   846  550 "thumb"

shrink_image 1024 768 "max/pic average.png" 1665 1039 "full"
shrink_image 1024 768 "max/pic tall.png"     559 1279 "full"
shrink_image 1024 768 "max/pic wide.png"    2265  284 "full"
shrink_image 1024 768 "max/pic no pad.png"   846  550 "full"













