Convert Cartoon script to MagicWand use for iOS

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
Post Reply
ostream
Posts: 1
Joined: 2013-10-05T02:16:26-07:00
Authentication code: 6789

Convert Cartoon script to MagicWand use for iOS

Post by ostream »

Hi, I am trying to convert script cartoon http://www.fmwconcepts.com/imagemagick/ ... /index.php for using on iOS App.

Code: Select all

# set default values
pattern=70			# segmentation pattern; 0<=integer<=100
numlevels=6			# numlevels; integer>=2
edgeamount=4		# edge amount; float>=0
brightness=100		# brightness; integer>=0
saturation=150		# saturation; integer>=0

# fixed arguments
edgewidth=2			# edge width; integer>=0
edgethresh=90		# edge threshold; 0<=integer<=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 -n '/^###/q;  /^#/!q;  s/^#//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}
usage2() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -n '/^######/q;  /^#/!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 12 ]
	then
	errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
	while [ $# -gt 0 ]
		do
			# get parameter values
			case "$1" in
		  -h|-help)    # help information
					   echo ""
					   usage2
					   exit 0
					   ;;
				-p)    # get pattern
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID PATTERN SPECIFICATION ---"
					   checkMinus "$1"
					   pattern=`expr "$1" : '\([0-9]*\)'`
					   [ "$pattern" = "" ] && errMsg "--- PATTERN=$pattern MUST BE A NON-NEGATIVE INTEGER VALUE (with no sign) ---"
					   test1=`echo "$pattern < 0" | bc`
					   test2=`echo "$pattern > 100" | bc`
					   [ $test1 -eq 1 -o $test2 -eq 1 ] && errMsg "--- PATTERN=$pattern MUST BE AN INTEGER BETWEEN 0 AND 100 ---"
					   ;;
				-n)    # get  numlevels
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID NUMLEVELS SPECIFICATION ---"
					   checkMinus "$1"
					   numlevels=`expr "$1" : '\([0-9]*\)'`
					   [ "$numlevels" = "" ] && errMsg "--- NUMLEVELS=$numlevels MUST BE A NON-NEGATIVE INTEGER VALUE (with no sign) ---"
					   test=`echo "$numlevels < 2" | bc`
					   [ $test -eq 1 ] && errMsg "--- NUMLEVELS=$numlevels MUST BE AN INTEGER GREATER THAN 1 ---"
					   ;;
				-e)    # get  edgeamount
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID EDGEAMOUNT SPECIFICATION ---"
					   checkMinus "$1"
					   edgeamount=`expr "$1" : '\([.0-9]*\)'`
					   [ "$edgeamount" = "" ] && errMsg "--- EDGEAMOUNT=$edgeamount MUST BE A NON-NEGATIVE FLOAT VALUE (with no sign) ---"
					   ;;
				-b)    # get brightness
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID BRIGHTNESS SPECIFICATION ---"
					   checkMinus "$1"
					   brightness=`expr "$1" : '\([0-9]*\)'`
					   [ "$brightness" = "" ] && errMsg "--- BRIGHTNESS=$brightness MUST BE A NON-NEGATIVE INTEGER VALUE (with no sign) ---"
					   ;;
				-s)    # get saturation
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID SATURATION SPECIFICATION ---"
					   checkMinus "$1"
					   saturation=`expr "$1" : '\([0-9]*\)'`
					   [ "$saturation" = "" ] && errMsg "--- SATURATION=$saturation MUST BE A NON-NEGATIVE INTEGER VALUE (with no sign) ---"
					   ;;
				 -)    # 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"

# setup temporary images
tmpA1="$dir/cartoon_1_$$.mpc"
tmpB1="$dir/cartoon_1_$$.cache"
tmpA2="$dir/cartoon_2_$$.mpc"
tmpB2="$dir/cartoon_2_$$.cache"
trap "rm -f $tmpA1 $tmpB1 $tmpA2 $tmpB2; exit 0" 0
trap "rm -f $tmpA1 $tmpB1 $tmpA2 $tmpB2; exit 1" 1 2 3 15

# 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' | head -n 1`

# colorspace RGB and sRGB swapped between 6.7.5.5 and 6.7.6.7 
# though probably not resolved until the latter
# then -colorspace gray changed to linear between 6.7.6.7 and 6.7.8.2 
# then -separate converted to linear gray channels between 6.7.6.7 and 6.7.8.2,
# though probably not resolved until the latter
# so -colorspace HSL/HSB -separate and -colorspace gray became linear
# but we need to use -set colorspace RGB before using them at appropriate times
# so that results stay as in original script
# The following was determined from various version tests using cartoon.
# with IM 6.7.4.10, 6.7.6.10, 6.7.7.7, 6.8.3.7
if [ "$im_version" -lt "06070607" -o "$im_version" -gt "06070707" ]; then
	setcspace="-set colorspace RGB"
else
	setcspace=""
fi
if [ "$im_version" -le "06070707" ]; then
	proc="-gamma 2.2"
else
	proc="-colorspace sRGB"
fi
# no need for setcspace for grayscale or channels after 6.8.5.4
if [ "$im_version" -gt "06080504" ]; then
	setcspace=""
	proc="-gamma 2.2"
fi

# read the input image into the temporary cached image, convert to depth 8 and selective blur
convert -quiet -regard-warnings "$infile" +repage -depth 8 -selective-blur 0x5+10% "$tmpA1" ||
	errMsg "--- FILE $infile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO size  ---"

ww=`identify -ping -format "%w" $tmpA1`
hh=`identify -ping -format "%h" $tmpA1`

# set up median filter
if [ "$im_version" -ge "06060806" ]; then
	medproc="-statistic median 3x3"
else
	medproc="-median 3"
fi


# convert to grayscale and posterize to create mask image
# convert $tmpA1 -level 0x$pattern% $setcspace -colorspace gray -posterize $numlevels -colorspace $cspace -depth 8 $tmpA2
convert $tmpA1 -level 0x$pattern% $setcspace -colorspace gray -posterize $numlevels -depth 8 $proc $tmpA2


# process image
# multiply the blurred posterized graycale mask with the smoothed input
# convert smoothed input to grayscale
# negate and blur
# colordodge composite the grayscale and negated/blurred version to make edgewidth image
# use power to amplify and then threshold and median filter
# multiply composite the edgewidth with the blended image
convert $tmpA1 \( $tmpA2 -blur 0x1 \) \
	\( -clone 0 -clone 1 -compose over -compose multiply -composite -modulate $brightness,$saturation,100 \) \
	\( -clone 0 $setcspace -colorspace gray \) \
	\( -clone 3 -negate -blur 0x${edgewidth} \) \
	\( -clone 3 -clone 4 -compose over -compose colordodge -composite \
		-evaluate pow $edgeamount -threshold $edgethresh% $medproc \) \
		-delete 0,1,3,4 -compose over -compose multiply -composite $outfile


exit 0	
Can we call convert command line on C++ Interface ? Please help me, tons of thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert Cartoon script to MagicWand use for iOS

Post by fmw42 »

If this is for commercial use, you will need to contact me about licensing the script. It is not free for commercial use.

I do not know much about C++ API, so someone else may have to answer that part.
Post Reply