#!/bin/bash
#
# Developed by Fred Weinhaus 7/31/2018 .......... 7/31/2018
#
# ------------------------------------------------------------------------------
# 
# Licensing:
# 
# Copyright © Fred Weinhaus
# 
# My scripts are available free of charge for non-commercial use, ONLY.
# 
# For use of my scripts in commercial (for-profit) environments or 
# non-free applications, please contact me (Fred Weinhaus) for 
# licensing arrangements. My email address is fmw at alink dot net.
# 
# If you: 1) redistribute, 2) incorporate any of these scripts into other 
# free applications or 3) reprogram them in another scripting language, 
# then you must contact me for permission, especially if the result might 
# be used in a commercial or for-profit environment.
# 
# My scripts are also subject, in a subordinate manner, to the ImageMagick 
# license, which can be found at: http://www.imagemagick.org/script/license.php
# 
# ------------------------------------------------------------------------------
# 
####
#
# USAGE: sinescrolltext -t "text" [-w width ] [-f font] [-c color] [-b bgcolor ] 
# [-a amplitude ] [-r repeats] [-p phase] [-F frames] [-d delay] [-D direction]  
# [-m mode ] [-R range] [infile] outfile
# USAGE: sinescrolltext [-h or -help]
#
# OPTIONS:
#
# -t     text          text string to be used to create the animation; required argument
# -w     width         width of text in pixels; integer>0; default=500; height will 
#                      be determined from the text height and the amplitude
# -f     font          font to use for the text; font name or path to font file; 
#                      default=arial
# -c     color         color of text; any valid IM color is allowed; default=black
# -b     bgcolor       background color for the animation frames; any valid IM color 
#                      is allowed; default=white
# -a     amplitude     amplitude of the text sine wave as a multiplier of the text 
#                      height; float>=0; default=1
# -r     repeats       number of repeats of the sine waveform to span the image width;
#                      float>0; default=1; (typically in half or integer values)
# -p     phase         phase of the sinusoidal wave; choices are 0 and 90; default=0
# -F     frames        number of frames to generate in the animation; integer>0; 
#                      default=25
# -d     delay         animation delay between frames; integer>0; default=30 
# -D     direction     direction of the scrolling; choices are: left or right;
#                      default=left
# -m     mode          mode for the type of sine wave; choices are: constant (amplitude) 
#                      and tapered (to zero amplitude); default=constant
# -R     range         range of the sine taper as percent of width of the image; 
#                      1<=integer<=100; default=100 (tapered over the full width); 
#                      nominal value is 75 
#
# infile is optional and will be tiled (or cropped) to replace the background color
# 
###
#
# NAME: SINESCROLLTEXT
# 
# PURPOSE: Creates a sinusoidal curved scrolling text banner animation.
# 
# DESCRIPTION: SINESCROLLTEXT creates sinusoidal curved scrolling text banner animation.  
# The scrolling can be either to the right or left. The amplitude of the sine wave  
# can be either constant or tapered to zero.
# 
# 
# OPTIONS: 
# 
# -t text ... TEXT string to be used to create the animation. This is a required argument
# 
# -w width ... WIDTH of text in pixels. Values are integers>0. The default=500. The 
# image height will be determined from the text height and the amplitude.
# 
# -f font ... FONT to use for the text. Either the font name or path to font file may 
# be used. The default=arial.
# 
# -c color ... COLOR of the text. Any valid IM color is allowed. The default=black.
# 
# -b bgcolor ... BGCOLOR is the background color for the animation frames. Any valid 
# IM color is allowed. The default=white.
# 
# -a amplitude ... AMPLITUDE (height up or down) of the text sine wave as a multiplier 
# of the text height. Values are floats>=0. The default=1.
# 
# -r repeats ... REPEATS are the number of repeats of the sine waveform cycle to span the 
# image width. Values are floats>0. The default=1. (typically, in half or integer 
# values).
# 
# -p phase ... PHASE of the sinusoidal wave. The choices are 0 and 180. The default=0.
#
# -F frames ... FRAMES is the number of frames to generate in the animation. Values are 
# integers>0. The default=25.
# 
# -d delay ... DELAY is the animation delay between frames. Values are integers>0. The 
# default=30.
#  
# -D direction ... DIRECTION of the scrolling. Choices are: left (l) or right (r).
# The default=left.
# 
# -m mode ... MODE for the type of sine wave amplitude. The choices are: constant (c) 
#  and tapered (t) to zero amplitude. The default=constant.
# 
# -R range ... RANGE of the sine taper to zero amplitude as percent of width of the 
# image. Values are 1<=integer<=100. The default=100 (tapered over the full width). 
# A nominal value is 75. 
# 
# infile is optional and will be tiled (or cropped) to replace the background color.
# 
# CAVEAT: No guarantee that this script will work on all platforms, 
# nor that trapping of inconsistent parameters is complete and 
# foolproof. Use At Your Own Risk. 
# 
######
#

# set default values
text=""
width=500
font="arial"
color="black"
bgcolor="white"
amplitude=1
repeats=1
phase=0
frames=25
delay=30
direction="left"
mode="constant"
range=100

# set directory for temporary files
dir="."    # suggestions are dir="." or dir="/tmp"

# set up functions to report Usage and Usage with Description
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
usage1() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -e '1,/^####/d;  /^###/g;  /^#/!q;  s/^#//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}
usage2() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -e '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}


# function to report error messages
errMsg()
	{
	echo ""
	echo $1
	echo ""
	usage1
	exit 1
	}


# function to test for minus at start of value of second part of option 1 or 2
checkMinus()
	{
	test=`echo "$1" | grep -c '^-.*$'`   # returns 1 if match; 0 otherwise
    [ $test -eq 1 ] && errMsg "$errorMsg"
	}

# test for correct number of arguments and get values
if [ $# -eq 0 ]
	then
	# help information
   echo ""
   usage2
   exit 0
elif [ $# -gt 28 ]
	then
	errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
	while [ $# -gt 0 ]
		do
			# get parameter values
			case "$1" in
		  -help|-h)    # help information
					   echo ""
					   usage2
					   exit 0
					   ;;
		   		-t)	   # get text
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID TEXT SPECIFICATION ---"
					   #checkMinus "$1"
			   		   text="$1"
			   		   ;;
				-f)    # get font
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID FONT SPECIFICATION ---"
					   checkMinus "$1"
					   font="$1"
		   			   ;;
				-w)    # get width
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID WIDTH SPECIFICATION ---"
					   checkMinus "$1"
					   width=`expr "$1" : '\([0-9]*\)'`
					   [ "$width" = "" ] && errMsg "--- WIDTH=$width MUST BE A NON-NEGATIVE INTEGER (with no sign) ---"
					   test1=`echo "$width == 0" | bc`
					   [ $test1 -eq 1 ] && errMsg "--- WIDTH=$width MUST BE A POSITIVE INTEGER ---"
					   ;;
				-c)    # get  color
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID COLOR SPECIFICATION ---"
					   checkMinus "$1"
					   color="$1"
					   ;;
				-b)    # get  bgcolor
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID BGCOLOR SPECIFICATION ---"
					   checkMinus "$1"
					   bgcolor="$1"
					   ;;
				-a)    # get amplitude
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID AMPLITUDE SPECIFICATION ---"
					   checkMinus "$1"
					   amplitude=`expr "$1" : '\([.0-9]*\)'`
					   [ "$amplitude" = "" ] && errMsg "--- AMPLITUDE=$amplitude MUST BE A NON-NEGATIVE FLOAT ---"
					   ;;
				-r)    # get repeats
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID REPEATS SPECIFICATION ---"
					   checkMinus "$1"
					   repeats=`expr "$1" : '\([.0-9]*\)'`
					   [ "$repeats" = "" ] && errMsg "--- REPEATS=$repeats MUST BE A NON-NEGATIVE FLOAT ---"
					   test1=`echo "$repeats == 0" | bc`
					   [ $test1 -eq 1 ] && errMsg "--- REPEATS=$repeats MUST BE A POSITIVE FLOAT ---"
					   ;;
			   	-p)    # phase
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID PHASE SPECIFICATION ---"
					   checkMinus "$1"
					   phase=`echo "$1" | tr "[:upper:]" "[:lower:]"`
					   case "$phase" in
					   		0) phase=0 ;;
					   		180) phase=180 ;;
					   		*) errMsg "--- PHASE=$phase IS NOT A VALID CHOICE ---" ;;
					   esac
					   ;;
				-F)    # get frames
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID FRAMES SPECIFICATION ---"
					   checkMinus "$1"
					   frames=`expr "$1" : '\([0-9]*\)'`
					   [ "$frames" = "" ] && errMsg "--- FRAMES=$frames MUST BE A NON-NEGATIVE INTEGER (with no sign) ---"
					   test1=`echo "$frames == 0" | bc`
					   [ $test1 -eq 1 ] && errMsg "--- FRAMES=$frames MUST BE A POSITIVE INTEGER ---"
					   ;;
				-d)    # get delay
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID DELAY SPECIFICATION ---"
					   checkMinus "$1"
					   delay=`expr "$1" : '\([0-9]*\)'`
					   [ "$delay" = "" ] && errMsg "--- DELAY=$delay MUST BE A NON-NEGATIVE INTEGER (with no sign) ---"
					   test1=`echo "$delay == 0" | bc`
					   [ $test1 -eq 1 ] && errMsg "--- DELAY=$delay MUST BE A POSITIVE INTEGER ---"
					   ;;
			   	-D)    # direction
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID DIRECTION SPECIFICATION ---"
					   checkMinus "$1"
					   direction=`echo "$1" | tr "[:upper:]" "[:lower:]"`
					   case "$direction" in
					   		left|l) direction="left" ;;
					   		right|r) direction="right" ;;
					   		*) errMsg "--- DIRECTION=$direction IS NOT A VALID CHOICE ---" ;;
					   esac
					   ;;
			   	-m)    # mode
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID MODE SPECIFICATION ---"
					   checkMinus "$1"
					   mode=`echo "$1" | tr "[:upper:]" "[:lower:]"`
					   case "$mode" in
					   		constant|c) mode="constant" ;;
					   		tapered|t) mode="tapered" ;;
					   		*) errMsg "--- MODE=$mode IS NOT A VALID CHOICE ---" ;;
					   esac
					   ;;
				-R)    # get range
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID RANGE SPECIFICATION ---"
					   checkMinus "$1"
					   range=`expr "$1" : '\([0-9]*\)'`
					   [ "$range" = "" ] && errMsg "--- RANGE=$range MUST BE A NON-NEGATIVE INTEGER ---"
					   test1=`echo "$range < 1" | bc`
					   test2=`echo "$range > 100" | bc`
					   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- RANGE=$range MUST BE AN INTEGER BETWEEN 1 AND 100 ---"
					   ;;
			 	-)    # STDIN and end of arguments
					   break
					   ;;
				-*)    # any other - argument
					   errMsg "--- UNKNOWN OPTION ---"
					   ;;
		     	 *)    # end of arguments
					   break
					   ;;
			esac
			shift   # next option
	done
	#
	# get infile and outfile
	if [ $# -eq 2 ]; then
		infile="$1"
		outfile="$2"
	elif [ $# -eq 1 ]; then
		outfile="$1"
	else
	errMsg "--- NO OUTPUT FILE SPECIFIED ---"
	fi
fi

# test that outfile provided
[ "$outfile" = "" ] && errMsg "NO OUTPUT FILE SPECIFIED"


# set up temporaries
tmp1A="$dir/sinescroll_1_$$.mpc"
tmp1B="$dir/sinescroll_1_$$.cache"
tmp2A="$dir/sinescroll_2_$$.mpc"
tmp2B="$dir/sinescroll_2_$$.cache"
tmp3A="$dir/sinescroll_3_$$.mpc"
tmp3B="$dir/sinescroll_3_$$.cache"
trap "rm -f $tmp1A $tmp1B $tmp2A $tmp2B $tmp3A $tmp3B;" 0
trap "rm -f $tmp1A $tmp1B $tmp2A $tmp2B $tmp3A $tmp3B; exit 1" 1 2 3 15
#trap "rm -f $tmp1A $tmp1B; exit 1" ERR

#invert range
range=$((100-range))
#echo "range=$range"

if [ "$direction" = "left" ]; then
	gravity1="west"
	gravity2="east"
	negating=""
	sgn="-"
elif [ "$direction" = "right" ]; then
	gravity1="east"
	gravity2="west"
	negating="-negate"
	sgn="+"
fi
#echo "gravity1=$gravity1; gravity2=$gravity2; negating=$negating; sgn=$sgn;"

if [ "$infile" != "" ]; then
	bgcolor="none"
fi

# process animation
wd=$width
convert -size ${wd}x -background "$bgcolor" -fill "$color"  -gravity center -font $font label:"$text" $tmp1A
hh=`convert -ping $tmp1A -format "%h" info:`
displace=`convert xc: -format "%[fx:$hh*$amplitude/2]" info:`
ht=`convert xc: -format "%[fx:ceil($hh+$amplitude*$hh)]" info:`
ww2=$((2*wd))
ww1=$((wd-1))


if [ "$infile" != "" ]; then
	convert -size ${wd}x${ht} tile:"$infile" $tmp3A
fi


# create sine image and extend with white on one side
# note first extent simply expands the height from the text height to the final height
if [ "$mode" = "tapered" ]; then
convert $tmp1A -extent ${wd}x${ht}! \
	-sparse-color barycentric "0,0 black $ww1,0 white" $negating -write mpr:grad \
	-function Sinusoid "$repeats,$phase" \
	\( mpr:grad -black-threshold ${range}% -level ${range}x100% \)  \
	+swap -define compose:args='1,0,-0.5,0.5' -compose mathematics -composite \
	-gravity $gravity1 -background white -extent ${ww2}x${ht}! $tmp2A
elif [ "$mode" = "constant" ]; then
convert $tmp1A -extent ${wd}x${ht}! \
	-sparse-color barycentric "0,0 black $ww1,0 white" \
	-function Sinusoid "$repeats,$phase" \
	-gravity $gravity1 -background white -extent ${ww2}x${ht}! $tmp2A
fi

# extent text image with bgcolor on the opposite side
convert $tmp1A -gravity $gravity2 -background "$bgcolor" -extent ${ww2}x${ht}! $tmp1A



if [ "$infile" != "" ]; then
	tileproc="$tmp3A +swap -gravity center -compose over -composite"
else
	tileproc=""
fi

# use subshell processing
(
for ((k=0; k<frames; k++)); do
roll=`convert xc: -format "%[fx:$k*round($ww2/$frames)]" info:`
convert \( $tmp1A -roll ${sgn}${roll}+0 \) $tmp2A -background "$bgcolor" -virtual-pixel background \
-define compose:args=0,$displace -compose displace -composite -compose over \
-gravity $gravity1 -crop ${wd}x${ht}+0+0 +repage $tileproc miff:- 
done
) | convert -delay $delay - -loop 0 "$outfile"


exit 0




