// Last updated 2008/11/04 11:23

// "Gel" Effects example
// http://www.imagemagick.org/Usage/advanced/#gel_effects

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL,*mwc = NULL, *mwf = NULL;
	DrawingWand *dw = NULL;
	PixelWand *pw = NULL;
	
	MagickWandGenesis();
	
/* First step is to create the gel shape:
convert -size 100x60 xc:none \
          -fill red -draw 'circle    25,30  10,30' \
                    -draw 'circle    75,30  90,30' \
                    -draw 'rectangle 25,15  75,45' \
          gel_shape.png
*/

	/* Create a wand */
	mw = NewMagickWand();
	pw = NewPixelWand();
	dw = NewDrawingWand();

	MagickSetSize(mw,100,60);
	MagickReadImage(mw,"xc:none");

	PixelSetColor(pw,"red");
	DrawSetFillColor(dw,pw);
	DrawCircle(dw,25,30,10,30);
	DrawCircle(dw,75,30,90,30);
	DrawRectangle(dw,25,15,75,45);

	// Now we draw the Drawing wand on to the Magick Wand
	MagickDrawImage(mw,dw);

	MagickWriteImage(mw,"gel_shape.png");
	if(dw)dw = DestroyDrawingWand(dw);
	if(pw)pw = DestroyPixelWand(pw);
	if(mw)mw = DestroyMagickWand(mw);
		
/* Next step is to create the gel highlight:
convert gel_shape.png \
            \( +clone -fx A +matte  -blur 0x12  -shade 110x0 -normalize \
               -sigmoidal-contrast 16,60% -evaluate multiply .5 \
               -roll +5+10 +clone -compose Screen -composite \) \
            -compose In  -composite  gel_highlight.png
*/
	mw = NewMagickWand();
	MagickReadImage(mw,"gel_shape.png");

	mwc = CloneMagickWand(mw);
	mwf = MagickFxImage(mwc,"A");
	MagickSetImageAlphaChannel(mwf,DeactivateAlphaChannel);
	MagickBlurImage(mwf,0,12);
	MagickShadeImage(mwf,MagickTrue,110,0);

	MagickNormalizeImage(mwf);
	// The last argument is specified as a percentage on the command line
	// but is specified to the function as a percentage of the QuantumRange
	MagickSigmoidalContrastImage(mwf,MagickTrue,16,0.6*QuantumRange);

	MagickEvaluateImage(mwf,MultiplyEvaluateOperator,0.5);
	MagickRollImage(mwf,5,10);

	if(mwc)mwc = DestroyMagickWand(mwc);
	// The +clone operation copies the original but only so that
	// it can be used in the following composite operation, so we don't
	// actually need to do a clone, just reference the original image.
	MagickCompositeImage(mwf,mw,ScreenCompositeOp,0,0);

	MagickCompositeImage(mw,mwf,InCompositeOp,0,0);
	MagickWriteImage(mw,"gel_highlight.png");

	if(mw)mw = DestroyMagickWand(mw);
	if(mwc)mwc = DestroyMagickWand(mwc);
	if(mwf)mwf = DestroyMagickWand(mwf);
		
// Now create the gel border
/*
convert gel_highlight.png \
            \( +clone -fx A  +matte -blur 0x2  -shade 0x90 -normalize \
               -blur 0x2  -negate -evaluate multiply .4 -negate -roll -.5-1 \
               +clone  -compose Multiply -composite \) \
            -compose In  -composite  gel_border.png

*/
	mw = NewMagickWand();
	MagickReadImage(mw,"gel_highlight.png");
	mwc = CloneMagickWand(mw);
	mwf = MagickFxImage(mwc,"A");
	MagickSetImageAlphaChannel(mwf,DeactivateAlphaChannel);
	MagickBlurImage(mwf,0,2);
	MagickShadeImage(mwf,MagickTrue,0,90);
	MagickNormalizeImage(mwf);
	MagickBlurImage(mwf,0,2);
	MagickNegateImage(mwf,MagickFalse);
	MagickEvaluateImage(mwf,MultiplyEvaluateOperator,0.4);
	MagickNegateImage(mwf,MagickFalse);
	MagickRollImage(mwf,-.5,-1);
	MagickCompositeImage(mwf,mw,MultiplyCompositeOp,0,0);
	MagickCompositeImage(mw,mwf,InCompositeOp,0,0);
	MagickWriteImage(mw,"gel_border.png");
	if(mw)mw = DestroyMagickWand(mw);
	if(mwc)mwc = DestroyMagickWand(mwc);
	if(mwf)mwf = DestroyMagickWand(mwf);

// and finally the text and shadow effect
/*
convert gel_border.png \
            -font Candice  -pointsize 24  -fill white  -stroke black \
            -gravity Center  -annotate 0 "Gel"  -trim -repage 0x0+4+4 \
            \( +clone -background navy -shadow 80x4+4+4 \) +swap \
            -background none  -flatten    gel_button.png
*/
	mw = NewMagickWand();
	dw = NewDrawingWand();
	pw = NewPixelWand();
	MagickReadImage(mw,"gel_border.png");
	DrawSetFont(dw,"Lucida-Handwriting-Italic");
	DrawSetFontSize(dw,24);
	PixelSetColor(pw,"white");
	DrawSetFillColor(dw,pw);
	PixelSetColor(pw,"black");
	DrawSetStrokeColor(dw,pw);
	DrawSetGravity(dw,CenterGravity);
	// It is important to notice here that MagickAnnotateImage renders the text on
	// to the MagickWand, NOT the DrawingWand. It only uses the DrawingWand for font
	// and colour information etc.
	MagickAnnotateImage(mw,dw,0,0,0,"Gel");
	MagickTrimImage(mw,0);
	MagickResetImagePage(mw,"0x0+4+4");
	mwc = CloneMagickWand(mw);
	PixelSetColor(pw,"navy");
	MagickSetImageBackgroundColor(mwc,pw);
	MagickShadowImage(mwc,80,4,4,4);
	mwf = NewMagickWand();
	MagickAddImage(mwf,mwc);
	MagickAddImage(mwf,mw);
	if(mw)mw = DestroyMagickWand(mw);
	if(mwc)mwc = DestroyMagickWand(mwc);
	PixelSetColor(pw,"none");
	MagickSetImageBackgroundColor(mwf,pw);
	mw = MagickMergeImageLayers(mwf,FlattenLayer);
	MagickWriteImage(mw,"gel_button.png");

	/* Tidy up */
	if(mw)mw = DestroyMagickWand(mw);
	if(mwc)mwc = DestroyMagickWand(mwc);
	if(mwf)mwf = DestroyMagickWand(mwf);
	if(dw)dw = DestroyDrawingWand(dw);
	if(pw)pw = DestroyPixelWand(pw);
	MagickWandTerminus();
}