#!/bin/bash
#
# Developed by Fred Weinhaus 8/20/2015 .......... revised 1/4/2024
#
# ------------------------------------------------------------------------------
# 
# 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: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit] 
# [-f fuzzval] [-b bgcolor] [-P process] [-a angle] [-r range] [-i intensity] [-e extent] 
# [-B bevel] [-A azimuth] [-E elevation] [-C contrast] [-S spread] 
# [-N newseed] [-M mix] [-G gain] infile outfile
# 
# USAGE: embroidery [-h or -help]
# 
# OPTIONS:
#
# -n     numcolors     number of desired or actual colors in image; integer>0; 
#                      default=8
# -p     pattern       weave pattern; choices are either linear (1) or 
#                      crosshatch (2); default=linear
# -t     thickness     weave thickness; integer>=1; default=2
# -g     graylimit     limit colors near black and near white to 
#                      gray(graylimit%) and gray(100%-graylimit%); 
#                      0<=integer<=100; default=20
# -f     fuzzval       fuzz value for recoloring near black and near white; 
#                      0<=integer<=100; default=5
# -b     bgcolor       actual background color in image; default is most the 
#                      frequent color
# -P     process       embroidering processing of background color; choices are: 
#                      yes, no, transparent and preserve; default=yes
# -a     angle         initial pattern angle for background color; 
#                      -360<=integer<=360; default=0
# -r     range         range of pattern angles; 0<=integer<=360; default=90
# -i     intensity     shadow intensity; higher is darker; 0 is no shadow; 
#                      0<=integer<=100; default=25
# -e     extent        shadow extent; integer>=0; 0 is no shadow; default=2
# -B     bevel         pattern bevel amount; integer>=0; default=4
# -A     azimuth       bevel azimuth angle; -360<=integer<=360; default=130
# -E     elevation     bevel elevation angle; 0<=integer<=90; default=30
# -C     contrast      bevel sigmoidal-contrast amount; float>=0; 
#                      default=0 for IM >= 6.7.9.5; otherwise default=1 
#                      (both cases no added contrast)
# -S     spread        pattern spread (diffusion); integer>=0; default=1
# -N     newseed       pattern new seed value; integer>=0; 
#                      default is random seed value
# -M     mix           mixing between before and after spread result; 
#                      0<=integer<=100; 100 is all spread; 0 is no spread; 
#                      default=100
# -G     gain          gain in pattern contrast; 0<=integer<=100; default=0
# 
###
# 
# NAME: EMBROIDERY
# 
# PURPOSE: To apply an embroidery effect to each color in an image.
# 
# DESCRIPTION: EMBROIDERY applies an embroidery effect to each color in an 
# image. The image must have limited number of colors or only the top most 
# frequent colors will be used. Each color will get the same pattern, but at 
# different rotation angles.
# 
# Arguments: 
# 
# -n numcolors ... NUMCOLORS is the number of desired or actual colors in the 
# image. Values are integers>0. The default=8.
# 
# -p pattern ... PATTERN is the weave pattern. The choices are either 
# linear (1) or crosshatch (2). The default=linear.
# 
# -t thickness ... THICKNESS is the weave thickness. Values are integers>=1. 
# The default=2.
# 
# -g graylimit ... GRAYLIMIT limits (converts) the colors near black and 
# near white to gray(graylimit%) and gray(100%-graylimit%). Values are  
# 0<=integers<=100. The default=20.
# 
# -f fuzzval ... FUZZVAL is the fuzz value used for recoloring near black and 
# near white. Values are 0<=integers<=100. The default=5.
# 
# -b bgcolor ... BGCOLOR is the background color in the image. The default 
# is most the frequent color.
# 
# -P process ... PROCESS is the embroidering processing for the background color. 
# The choices are: yes (y), no (n), transparent (t) or preserve (p). If no, the 
# background color will not be emroidered. If transparent, the background color in 
# the output image will be repaced with transparency. If preserve and there is a  
# non-opaque alpha channel, then the alpha channel will be preserved in the output 
# image and process will be reset to yes. If no or transparent options are used, you 
# may need to disable the shadow via -extent and/or -intensity. The default=yes
# 
# -a angle ... ANGLE is the initial pattern angle used for the most frequent  
# color. Values are -360<=integers<=360. The default=0 (horizontal)
# 
# -r range ... RANGE is the range of pattern angles over all the colors. 
# Values are 0<=integers<=360. The default=90 (deg).
# 
# -i intensity ... INTENSITY is the shadow intensity. Higher is darker. 
# 0 is no shadow. Values are 0<=integers<=100. The default=25.
# 
# -e extent ... EXTENT is the shadow extent in pixels. Values are integers>=0. 
# 0 is no shadow. The default=2.
# 
# -B bevel ... BEVEL is the pattern bevel amount. Values are integers>=0. 
# The default=4.
# 
# -A azimuth ... AZIMUTH is the bevel azimuth angle. Values are 
# -360<=integers<=360. The default=130.
# 
# -E elevation ... ELEVATION is the bevel elevation angle. Values are 
# 0<=integers<=90. The default=30.
# 
# -C contrast ... CONTRAST is the bevel sigmoidal-contrast amount. Values are
# floats>=0. The default=0 for IM >= 6.7.9.5; otherwise default=1 (no added contrast)
# 
# -S spread ... SPREAD is the pattern spread (diffusion). Values are 
# integers>=0 for IM >= 6.7.9.5; otherwise default=1. (no change)
# 
# -N newseed ... NEWSEED is the pattern (new) seed value. Values are 
# integers>=0. The default is a random seed value.
# 
# -M mix ... MIX is the mixing of the before and after spread result. Values 
# are 0<=integers<=100. 100 is all spread. 0 is no spread. The default=100
# 
# -G gain ... GAIN in pattern contrast. Values are 0<=integers<=100. The default=0.
# 
# NOTE: The input image should be completely opaque. If not and if a background color  
# is explicitly specified, then any transparency will be flattened against the  
# background color to remove it. Otherwise, the alpha channel will be disabled.
# 
# 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
numcolors=8			# number of desired or actual colors in image; integer>0; default=8
pattern="linear"	# weave pattern; linear or crosshatch
thickness=2			# weave thickness; integer>=1; default=2
graylimit=20		# limit near black and near white to gray(graylimit%) and gray(100%-graylimit%); 0<=integer<=100; default=20
fuzzval=5			# fuzz value for recoloring near black and near white; 0<=integer<=100; default=20
bgcolor=""			# background color in image; default is most frequent color
process="yes"		# background color embroidery processing; yes, no, transparent
angle=0				# initial angle of background color; -360<=integer<=360
range=90			# range of angles; 0<=integer<=360; default=90
intensity=25		# shadow intensity; higher is darker; 0 is no shadow; 0<=integer<=100; default=25
extent=2			# shadow extent; integer>=0; 0 is no shadow; default=2
bevel=4				# pattern bevel amount; integer>=0; default=4
azimuth=130			# bevel azimuth angle; -360<=integer<=360; default=130
elevation=30		# bevel elevation angle; 0<=integer<=90; default=30
contrast=""			# bevel sigmoidal-contrast amount; integer>=0; default=0 for IM >= 6.7.9.5; otherwise default=1.
spread=1			# pattern spread; integer>=0; default=1
newseed=""			# pattern new seed value; integer>=0; default is random value
mix=100				# mixing between before and after spread result; 0<=integer<=100; 100 is all spread; 0 is no spread; default=100
gain=0				# pattern contrast gain; 0<=integer<=100; default=0


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

dir="$tmpdir/EMBROIDERY.$$"

# 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 40 ]
	then
	errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
	while [ $# -gt 0 ]
		do
		# get parameters
		case "$1" in
	  -h|-help)    # help information
				   echo ""
				   usage2
				   ;;
			-n)    # get numcolors
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID NUMCOLORS SPECIFICATION ---"
				   checkMinus "$1"
				   numcolors=`expr "$1" : '\([0-9]*\)'`
				   [ "$numcolors" = "" ] && errMsg "--- NUMCOLORS=$numcolors MUST BE AN INTEGER ---"
				   test1=`echo "$numcolors <= 0" | bc`
				   [ $test1 -eq 1 ] && errMsg "--- NUMCOLORS=$numcolors MUST BE AN INTEGER GREATER THAN 0 ---"
				   ;;
			-p)    # get  pattern
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID PATTERN SPECIFICATION ---"
				   checkMinus "$1"
				   pattern=`echo "$1" | tr '[A-Z]' '[a-z]'`
				   case "$pattern" in 
						linear|1) pattern="linear" ;;
						crosshatch|2) pattern="crosshatch" ;;
						*) errMsg "--- PATTERN=$pattern IS AN INVALID VALUE ---"  ;;
				   esac
				   ;;
			-t)    # get thickness
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID THICKNESS SPECIFICATION ---"
				   checkMinus "$1"
				   thickness=`expr "$1" : '\([0-9]*\)'`
				   [ "$thickness" = "" ] && errMsg "--- THICKNESS=$thickness MUST BE AN INTEGER ---"
				   test1=`echo "$thickness < 1" | bc`
				   [ $test1 -eq 1 ] && errMsg "--- THICKNESS=$thickness MUST BE AN INTEGER GREATER THAN 1 ---"
				   ;;
			-g)    # get graylimit
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID GRAYLIMIT SPECIFICATION ---"
				   checkMinus "$1"
				   graylimit=`expr "$1" : '\([0-9]*\)'`
				   [ "$graylimit" = "" ] && errMsg "--- GRAYLIMIT=$graylimit MUST BE AN INTEGER ---"
				   test1=`echo "$graylimit < 0" | bc`
				   test2=`echo "$graylimit > 100" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- GRAYLIMIT=$graylimit MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
				   ;;
			-f)    # get fuzzval
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID FUZZVAL SPECIFICATION ---"
				   checkMinus "$1"
				   fuzzval=`expr "$1" : '\([0-9]*\)'`
				   [ "$fuzzval" = "" ] && errMsg "--- FUZZVAL=$fuzzval MUST BE AN INTEGER ---"
				   test1=`echo "$fuzzval < 0" | bc`
				   test2=`echo "$fuzzval > 100" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- FUZZVAL=$fuzzval MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
				   ;;
			-b)    # get bgcolor
				   shift  # to get the next parameter - color
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID BGCOLOR SPECIFICATION ---"
				   checkMinus "$1"
				   bgcolor="$1"
				   ;;
			-P)    # get  process
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID PROCESS SPECIFICATION ---"
				   checkMinus "$1"
				   process=`echo "$1" | tr '[A-Z]' '[a-z]'`
				   case "$process" in 
						yes|y) process="yes" ;;
						no|n) process="no" ;;
						transparent|t) process="transparent" ;;
						preserve|p) process="preserve" ;;
						*) errMsg "--- PROCESS=$process IS AN INVALID VALUE ---"  ;;
				   esac
				   ;;
			-a)    # get angle
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   #errorMsg="--- INVALID ANGLE SPECIFICATION ---"
				   #checkMinus "$1"
				   angle=`expr "$1" : '\([-0-9]*\)'`
				   [ "$angle" = "" ] && errMsg "--- ANGLE=$angle MUST BE AN INTEGER ---"
				   test1=`echo "$angle < -360" | bc`
				   test2=`echo "$angle > 360" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- ANGLE=$angle MUST BE AN INTEGER BETWEEN -360 AND 360 ---"
				   ;;
			-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 AN INTEGER ---"
				   test1=`echo "$range < 0" | bc`
				   test2=`echo "$range > 360" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- RANGE=$range MUST BE AN INTEGER BETWEEN 0 AND 360 ---"
				   ;;
			-i)    # get intensity
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID INTENSITY SPECIFICATION ---"
				   checkMinus "$1"
				   intensity=`expr "$1" : '\([0-9]*\)'`
				   [ "$intensity" = "" ] && errMsg "--- INTENSITY=$intensity MUST BE AN INTEGER ---"
				   test1=`echo "$intensity < 0" | bc`
				   test2=`echo "$intensity > 100" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- INTENSITY=$intensity MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
				   ;;
			-e)    # get extent
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID EXTENT SPECIFICATION ---"
				   checkMinus "$1"
				   extent=`expr "$1" : '\([0-9]*\)'`
				   [ "$extent" = "" ] && errMsg "--- EXTENT=$extent MUST BE A NON-NEGATIVE INTEGER ---"
				   ;;
			-B)    # get bevel
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID BEVEL SPECIFICATION ---"
				   checkMinus "$1"
				   bevel=`expr "$1" : '\([0-9]*\)'`
				   [ "$bevel" = "" ] && errMsg "--- BEVEL=$bevel MUST BE A NON-NEGATIVE INTEGER ---"
				   ;;
			-A)    # get azimuth
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   #errorMsg="--- INVALID AZIMUTH SPECIFICATION ---"
				   #checkMinus "$1"
				   azimuth=`expr "$1" : '\([-0-9]*\)'`
				   [ "$azimuth" = "" ] && errMsg "--- AZIMUTH=$azimuth MUST BE AN INTEGER ---"
				   test1=`echo "$azimuth < -360" | bc`
				   test2=`echo "$azimuth > 360" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- AZIMUTH=$azimuth MUST BE AN INTEGER BETWEEN -360 AND 360 ---"
				   ;;
			-E)    # get elevation
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID ELEVATION SPECIFICATION ---"
				   checkMinus "$1"
				   elevation=`expr "$1" : '\([0-9]*\)'`
				   [ "$elevation" = "" ] && errMsg "--- ELEVATION=$elevation MUST BE AN INTEGER ---"
				   test1=`echo "$elevation < 0" | bc`
				   test2=`echo "$elevation > 90" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- ELEVATION=$elevation MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
				   ;;
			-C)    # get contrast
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID CONTAST SPECIFICATION ---"
				   checkMinus "$1"
				   contrast=`expr "$1" : '\([.0-9]*\)'`
				   [ "$contrast" = "" ] && errMsg "--- CONTAST=$contrast MUST BE A NON-NEGATIVE FLOAT ---"
				   ;;
			-S)    # get spread
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID SPREAD SPECIFICATION ---"
				   checkMinus "$1"
				   spread=`expr "$1" : '\([0-9]*\)'`
				   [ "$spread" = "" ] && errMsg "--- SPREAD=$spread MUST BE A NON-NEGATIVE INTEGER ---"
				   ;;
			-N)    # get newseed
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID NEWSEED SPECIFICATION ---"
				   checkMinus "$1"
				   newseed=`expr "$1" : '\([0-9]*\)'`
				   [ "$newseed" = "" ] && errMsg "--- NEWSEED=$newseed MUST BE A NON-NEGATIVE INTEGER ---"
				   ;;
			-M)    # get mix
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID MIX SPECIFICATION ---"
				   checkMinus "$1"
				   mix=`expr "$1" : '\([0-9]*\)'`
				   [ "$mix" = "" ] && errMsg "--- MIX=$mix MUST BE AN INTEGER ---"
				   test1=`echo "$mix < 0" | bc`
				   test2=`echo "$mix > 100" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- MIX=$mix MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
				   ;;
			-G)    # get gain
				   shift  # to get the next parameter
				   # test if parameter starts with minus sign 
				   errorMsg="--- INVALID GAIN SPECIFICATION ---"
				   checkMinus "$1"
				   gain=`expr "$1" : '\([0-9]*\)'`
				   [ "$gain" = "" ] && errMsg "--- GAIN=$gain MUST BE AN INTEGER ---"
				   test1=`echo "$gain < 0" | bc`
				   test2=`echo "$gain > 100" | bc`
				   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- GAIN=$gain MUST BE AN INTEGER BETWEEN 0 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
	infile="$1"
	outfile="$2"
fi

# test that infile provided
[ "$infile" = "" ] && errMsg "--- NO INPUT FILE SPECIFIED ---"

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

mkdir "$dir" || errMsg "--- FAILED TO CREATE TEMPORARY FILE DIRECTORY ---"
trap "rm -rf $dir; exit 0" 0
trap "rm -rf $dir; exit 1" 1 2 3 15

# read the input image into the temporary cached image and test if valid
	convert -quiet "$infile" +repage -depth 8 $dir/tmpI.mpc ||
		errMsg "--- FILE $infile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO size  ---"

# preprocess for background and process situations
process2=""
if [ "$process" = "preserve" ]; then
	mean_alpha=`convert $dir/tmpI.mpc -alpha on -alpha extract -format "%[fx:mean]" info:`
	if [ "$mean_alpha" != "1" ]; then
		convert $dir/tmpI.mpc -alpha extract $dir/tmpA.mpc
		convert -quiet "$infile" +repage -alpha off $dir/tmpI.mpc
		# set process=yes to properly embroider all colors
		process="yes"
		# set process2 to preserve for us later to put back the alpha channel
		process2="preserve"
		
	fi

elif [ "$bgcolor" != "" ]; then
	convert $dir/tmpI.mpc -alpha on -background "$bgcolor" -alpha background -alpha off $dir/tmpI.mpc

else
	convert -quiet "$infile" +repage -alpha off $dir/tmpI.mpc
fi

# get IM version
im_version=`convert -list configure | \
	sed '/^LIB_VERSION_NUMBER */!d;  s//,/;  s/,/,0/g;  s/,0*\([0-9][0-9]\)/\1/g'`

# set default for -C to 1 for IM < 6.7.9.5
if [ "$im_version" -lt "06070905" -a "$contrast" = "" ]; then
	contrast=1
else
	contrast=0
fi

# get image dimensions
ww=`convert -ping $dir/tmpI.mpc -format "%w" info:`
hh=`convert -ping $dir/tmpI.mpc -format "%h" info:`

ww2=`convert xc: -format "%[fx:hypot($ww,$hh)]" info:`
hh2=$ww2

# get numcolors most frequently used colors
colorArr=(`convert $dir/tmpI.mpc -define histogram:unique-colors=true -format "%c" histogram:info: |\
sort -k 1 -nr | head -n $numcolors | sed -n "s/^.*\(#.*\) .*$/\1/p"`)
#echo "${colorArr[*]}"
#echo "${#colorArr[*]}"

# create color map
convert -size 1x1 xc:"${colorArr[0]}" $dir/tmpL.mpc
if [ $numcolors -gt 1 ]; then
	for ((i=1; i<numcolors; i++)); do
		convert $dir/tmpL.mpc \( -size 1x1 xc:"${colorArr[$i]}" \) +append $dir/tmpL.mpc
	done
fi

# remap colors to remove aliased values
convert $dir/tmpI.mpc +dither -remap $dir/tmpL.mpc $dir/tmpI.mpc

# set up for bgcolor
if [ "$bgcolor" = "" ]; then
	bgcolor="${colorArr[0]}"
else
	bgcolor=`convert xc:"$bgcolor" -depth 8 txt:- | tail -n +2 | tr -cs "#0-9ABCDEF" " " | cut -d\  -f6`
fi
#echo "bgcolor=$bgcolor;"

# test colors to see if too black or too white and change colors by graylimit
# also recolor images if too bright|dark and too high saturation -- currently fixed values from testing primary and secondary colors
blackpt=$graylimit
whitept=$((100-graylimit))
recolor=""
for ((i=0; i<numcolors; i++)); do
	#echo "i=$i"
	#echo "color=${colorArr[$i]}"
	bri=`convert xc:"${colorArr[$i]}" -colorspace HSV -channel blue -separate +channel -alpha off -format "%[fx:round(100*u)]" info:`
	sat=`convert xc:"${colorArr[$i]}" -colorspace HSV -channel green -separate +channel -alpha off -format "%[fx:round(100*u)]" info:`
	wtest=`convert xc:"${colorArr[$i]}" -fuzz $fuzzval% -fill white -opaque white -format "%[pixel:u.p{0,0}]" info:`
	btest=`convert xc:"${colorArr[$i]}" -fuzz $fuzzval% -fill black -opaque black -format "%[pixel:u.p{0,0}]" info:`
	#echo "bri=$bri; sat=$sat; wtest=$wtest; btest=$btest;"

	if [ "$wtest" = "white" -o "$wtest" = "srgb(255,255,255)" ] && [ "$bgcolor" != "${colorArr[$i]}" -o "$process" = "yes" ]; then
		newcolor=`convert xc:"gray($whitept%)" -alpha off -depth 8 txt:- | tail -n +2 | sed -n 's/^.*\([#][^ ]*\).*$/\1/p'`
	 	recolor="$recolor -fuzz $fuzzval% -fill '$newcolor' -opaque white"
	 	#echo "newcolor=$newcolor;"
	elif [ "$btest" = "black" -o "$btest" = "srgb(0,0,0)" ] && [ "$bgcolor" != "${colorArr[$i]}" -o "$process" = "yes" ]; then
		newcolor=`convert xc:"gray($blackpt%)" -alpha off -depth 8 txt:- | tail -n +2 | sed -n 's/^.*\([#][^ ]*\).*$/\1/p'`
		recolor="$recolor -fuzz $fuzzval% -fill '$newcolor' -opaque black"
	 	#echo "newcolor=$newcolor;"
	elif [ $bri -gt 96 -a $sat -gt 95 ] && [ "$bgcolor" != "${colorArr[$i]}" -o "$process" = "yes" ]; then
		newcolor=`convert xc:"${colorArr[$i]}" -alpha off -modulate 80,100,100 -depth 8 txt:- | tail -n +2 | sed -n 's/^.*\([#][^ ]*\).*$/\1/p'`
		recolor="$recolor -fuzz 0% -fill '$newcolor' -opaque '${colorArr[$i]}'"
	 	#echo "newcolor=$newcolor;"
	elif [ $bri -lt 30 -a $sat -gt 95 ] && [ "$bgcolor" != "${colorArr[$i]}" -o "$process" = "yes" ]; then
		newcolor=`convert xc:"${colorArr[$i]}" -alpha off -modulate 130,100,100 -depth 8 txt:- | tail -n +2 | sed -n 's/^.*\([#][^ ]*\).*$/\1/p'`
		recolor="$recolor -fuzz 0% -fill '$newcolor' -opaque '${colorArr[$i]}'"
	 	#echo "newcolor=$newcolor;"
	fi
done
#echo "$recolor"

# recolor image
eval 'convert $dir/tmpI.mpc '$recolor' -depth 8 $dir/tmpR.mpc'

# do embroidery for each color at a different angle

# set up ang and anginc
anginc=`convert xc: -format "%[fx:$range/$numcolors]" info:`
if [ "$pattern" = "linear" ]; then
	ang=`convert xc: -format "%[fx:-45+$angle]" info:`
elif [ "$pattern" = "crosshatch" ]; then
	ang=`convert xc: -format "%[fx:-90+$angle]" info:`
fi
#echo "ang=$ang"

# set up for newseed
if [ "$newseed" != "" ]; then
	seeding="-seed $newseed"
else
	seeding=""
fi

# set up for spreading
if [ $spread -eq 0 ]; then
	sproc=""
	mproc=""
else
	sproc="-clone 0 $seeding -spread $spread"
	mproc="-define compose:args=$mix -compose blend -composite"
fi

# set up sigmoidal contrasting for gain
if [ "$gain" = "0" ]; then
	gaining=""
else
	gaining="-sigmoidal-contrast $gain,50%"
fi

# create tiled pattern image
if [ "$pattern" = "linear" ]; then
	thick1=$thickness
	thick2=$((2*thick1))
	thick3=$((3*thick1))
	fullthick=$((4*thick1))
	convert -size ${thickness}x${fullthick} gradient: -rotate 270 \
	\( -clone 0 -roll +${thick1}+0 \) \( -clone 0 -roll +${thick2}+0 \) \( -clone 0 -roll +${thick3}+0 \) -append \
	-write mpr:tile +delete -size ${ww2}x${hh2} tile:mpr:tile \
	\( $sproc \) \
	$mproc \
	$dir/tmpP.mpc
elif [ "$pattern" = "crosshatch" ]; then
	thick1=$(($thickness+3))
	convert -size ${thick1}x${thick1} gradient: -rotate 270 \
	\( -clone 0 -flop \)  -append \
	-write mpr:tile +delete -size ${ww2}x${hh2} tile:mpr:tile \
	\( $sproc \) \
	$mproc \
	$dir/tmpP.mpc
fi
#echo "thick1=$thick1; thick2=$thick2; thick3=$thick3; fullthick=$fullthick;"


# rotate tile pattern and composite and and make all other color regions transparent
# note in the bevel section, need to add -alpha deactivate ... -alpha on for IM 7, since IM 7 does not persist the alpha automatically as in IM 6.
# but including it does not affect IM 6 whether it is there or not
for ((j=0; j<numcolors; j++)); do
	#echo "color=${colorArr[$j]}; bgcolor=$bgcolor; process=$process;"
	if [ "${colorArr[$j]}" = "$bgcolor" -a "$process" = "no" ]; then
		convert $dir/tmpI.mpc +transparent "${colorArr[$j]}" $dir/tmpI_$j.mpc
		ang=`convert xc: -format "%[fx:$ang+$anginc]" info:`
	elif [ "${colorArr[$j]}" = "$bgcolor" -a "$process" = "transparent" ]; then
		convert $dir/tmpI.mpc -alpha set -alpha transparent $dir/tmpI_$j.mpc
		ang=`convert xc: -format "%[fx:$ang+$anginc]" info:`
	elif [ "${colorArr[$j]}" = "$bgcolor" -o $bevel -eq 0 ]; then
		convert $dir/tmpR.mpc \
		\( $dir/tmpI.mpc +transparent "${colorArr[$j]}" -alpha extract \) \
		\( $dir/tmpP.mpc -rotate $ang +repage -gravity center -crop ${ww}x${hh}+0+0 +repage \) \
		\( -clone 2 $gaining -clone 0 +swap -compose softlight -composite \) \
		-delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite \
		\
		\( -clone 0 -background black -shadow ${intensity}x${extent}+0+0 -channel A -level 0,50% +channel \) \
		+swap +repage -gravity center -compose over -composite \
		$dir/tmpI_$j.mpc
		ang=`convert xc: -format "%[fx:$ang+$anginc]" info:`
	else
		if [ "$im_version" -ge "07000000" ]; then
			set_alpha="-alpha deactivate"
		else
			set_alpha="-alpha on"
		fi
		convert $dir/tmpR.mpc \
		\( $dir/tmpI.mpc +transparent "${colorArr[$j]}" -alpha extract \) \
		\( $dir/tmpP.mpc -rotate $ang +repage -gravity center -crop ${ww}x${hh}+0+0 +repage \) \
		\( -clone 2 $gaining -clone 0 +swap -compose softlight -composite \) \
		-delete 0,2 +swap -compose over -alpha off -compose copy_opacity -composite \
		\
		\( +clone -alpha extract -write mpr:alpha -blur 0x$bevel -shade ${azimuth}x${elevation} \
			mpr:alpha -alpha off -compose copy_opacity -composite -alpha background \
			$set_alpha -auto-level -function polynomial 3.5,-5.05,2.05,0.25 -sigmoidal-contrast ${contrast}x50% \) \
		-compose over -compose Hardlight -composite \
		\
		\( -clone 0 -background black -shadow ${intensity}x${extent}+0+0 -channel A -level 0,50% +channel \) \
		+swap +repage -gravity center -compose over -composite \
		$dir/tmpI_$j.mpc
		ang=`convert xc: -format "%[fx:$ang+$anginc]" info:`
	fi
done

imagelist=""
for ((k=0; k<numcolors; k++)); do
#convert $dir/tmpI_$k.mpc tmpI_$k.png
	imagelist="$imagelist $dir/tmpI_$k.mpc"
done

# flatten images
if [ "$process2" = "preserve" ]; then
	convert $imagelist -background none -flatten -gravity center -crop ${ww}x${hh}+0+0 +repage \
		$dir/tmpA.mpc -alpha off -compose over -compose copy_opacity -composite "$outfile"
else
	convert $imagelist -background none -flatten -gravity center -crop ${ww}x${hh}+0+0 +repage "$outfile"
fi

exit 0
