Watermarking image

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

Not sure if this is exactly what you're after but this code will read two images and use the Dissolve operator to composite one image over the other.
I've put the two images on my webpage so that this code will fetch them directly. This code was written on windows but with minor changes should work on other OS.

Pete

Code: Select all

#include <windows.h>
#include <wand/magick_wand.h>
// To get definition of magickwand  for magick_wand->images
#include <wand/magick-wand-private.h>

void test_wand(void)
{
	MagickWand *magick_wand = NULL,*moth_wand = NULL;
	double comp_opacity,moth_opacity;
	Quantum c_opacity,m_opacity;

	/* 
		For detailed info about MagickWand functions and their parameters see:
		http://studio.imagemagick.org/script/magick-wand.php
	*/

	/*	When comp_opacity is 0.0:
		moth_opacity = 1.0 completely invisible
		moth_opacity = 0.0 fully visible
		I've used doubles to specify the opacities and then I translate these into
		Quantum values. You can use Quantum values directly.
	*/
	comp_opacity = 0.0;
	/* The moth will be partially visible */
	moth_opacity = 0.6;


	/* convert the (double) opacities to Quantum for the setimageopacity 
		I'm using a Q8 version so a Quantum will be an unsigned char and
		QuantumRange will be 255 
	*/

	c_opacity = (Quantum)RoundToQuantum((MagickRealType)comp_opacity * QuantumRange);
	m_opacity = (Quantum)RoundToQuantum((MagickRealType)moth_opacity * QuantumRange);

	// The Big Bang
	MagickWandGenesis();

	magick_wand = NewMagickWand();
	// Read the image from the web.
	MagickReadImage(magick_wand,"http://members.shaw.ca/el_supremo/CHSP.JPG");
	// Strip any profiles, thumbnails etc.
	MagickStripImage(magick_wand);
	/* for the setimageopacity function I want the matte to be off so that
		opacity is *set*. If matte is already on then it is *blended*.
		So, force the opacity off (just in case I've read a PNG or other format
		which does have a matte layer). Then use SetImageOpacity to force matte on 
		and set the opacity value
	*/
	MagickSetImageMatte(magick_wand,0);
	// Set the opacity of the background to c_opacity and turn matte on.
	SetImageOpacity(magick_wand->images,c_opacity);

	moth_wand = NewMagickWand();

	MagickReadImage(moth_wand,"http://members.shaw.ca/el_supremo/MOTH.JPG");
	MagickStripImage(moth_wand);
	// Do the same with the matte and opacity in the overlay image
	MagickSetImageMatte(moth_wand,0);
	/* Turn matte on and set opacity of the moth image to m_opacity */
	SetImageOpacity(moth_wand->images,m_opacity);

	// and do the Dissolve composite operation
	MagickCompositeImage(magick_wand,moth_wand,DissolveCompositeOp,0,0);

	// Write the image 
	MagickWriteImage(magick_wand,"composite.jpg");

	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	if(moth_wand)moth_wand = DestroyMagickWand(moth_wand);

	// Armageddon
	MagickWandTerminus();
}
Post Reply