#!/bin/bash
#
#	echo necesita gmic, imagemagick y ebsynth
#

[ "$1" == "" ] && echo "$(basename $0) textura.png forma.png salida.png # imagenes con fondo transparente" && exit

function degrade {
	# umbral # aumentar morfologia
	convert $1 matte:- | \
	convert - -threshold 75% -negate PNG:- | \
	convert - -morphology Dilate Octagon:2 /tmp/mask.png
	# crea los colores del borde
	gmic /tmp/mask.png fx_gradient2rgb 0,0,100,0,0,0,50,50 -o $2
	convert $2 -transparent black -alpha on $2
	# crea los colores faltantes
	gmic $2 fx_solidify_td 100,0,59,0,1,0,50,50 -o $2

	#border
	convert $1 matte:- | \
	convert - -threshold 50% -negate PNG:- | \
	convert - -morphology Erode Octagon:3 /tmp/mask.png
	# border
	convert $1 matte:- |\
	convert - -threshold 50% -negate PNG:- | \
	convert - /tmp/mask1.png
	#convert - -morphology Dilate Octagon:1 /tmp/mask1.png
	# borde de transicion
	convert /tmp/mask.png  -transparent black -alpha background /tmp/mask.png
	convert /tmp/mask1.png -transparent white -alpha background /tmp/mask1.png
	# unir
	composite  -compose Dst_Over /tmp/mask.png /tmp/mask1.png /tmp/mask.png
	# crear valores faltantes
	gmic /tmp/mask.png fx_solidify_td 100,0,59,0,1,0,50,50 -o /tmp/mask.png
	#unir
	composite  -compose Multiply /tmp/mask.png $2 $2
}

degrade $1 /tmp/a.png
degrade $2 /tmp/b.png

# ebsynth
ebsynth -style $1 -guide /tmp/a.png /tmp/b.png -weight 5 -output $3
