Page 1 of 1

How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T06:35:30-07:00
by Pit Sullivan
Hey, guys!
I've been looking for any image gradients methods in the Magick++ API but I can't find anything. I need to add a radial gradient to an image. Although I found ImageGradient method in MagickCore API, but I haven't seen into it yet. Is there any method in Magick++ API that does what I want or I should use that one from MagickCore API?
Thank you for all your support!
P.S. API version is Magick++ 6.9.1.1. Q16.

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T11:39:08-07:00
by dlemstra
Would you mind posting the command line commands that you are trying to reproduce in the Magick++ API?

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T11:53:10-07:00
by fmw42
In command line, one creates a radial gradient using:

Code: Select all

convert -size WxH radial-gradient: resultimage
See pseudo image formats at http://www.imagemagick.org/script/formats.php#pseudo

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T13:16:09-07:00
by Pit Sullivan
I have tried to reproduce this:

Code: Select all

convert -size WxH radial-gradient:inner-outer result
Here is a code I wrote that reproduces it:

Code: Select all

void retrieveColor(Magick::PixelPacket& to, const Magick::Color& from)
{
	to.blue = from.blueQuantum();
	to.green = from.greenQuantum();
	to.red = from.redQuantum();
	to.opacity = from.alphaQuantum();
}

void radialGradient( Magick::Image&  	 image,
				         const Magick::Color innerColor,
					      const Magick::Color outerColor )
{
	Magick::PixelPacket startColor, stopColor;

	retrieveColor(startColor, innerColor);
	retrieveColor(stopColor, outerColor);

	image.modifyImage();
	MagickCore::GradientImage( image.image(), 
				   			      MagickCore::RadialGradient,
				   			      MagickCore::PadSpread,
				   			      &startColor,
				   			      &stopColor                  );	
}
It works but I haven't tested it properly so there is a work to do. Any suggestions and/or admonishments are welcomed. Thank you guys for your help!

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T13:41:40-07:00
by dlemstra
'radial-gradient:inner-outer' is the name of the 'file' on the command line. So you can just do something like this:

Code: Select all

size_t W=100;
size_t H=100;
Magick::Image img;
img.size(Geometry(W, H));
img.read("radial-gradient:purple-yellow");
img.write("result.png");
edit: Updated my example after Fred his comment.

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T13:48:55-07:00
by fmw42
inner-outer is optional and represents the desired colors which default to black and white

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T14:29:56-07:00
by Pit Sullivan
dlemstra wrote:'radial-gradient:inner-outer' is the name of the 'file' on the command line. So you can just do something like this:

Code: Select all

size_t W=100;
size_t H=100;
Magick::Image img;
img.size(Geometry(W, H));
img.read("radial-gradient:purple-yellow");
img.write("result.png");
edit: Updated my example after Fred his comment.
Could you please explain what it is? You read radial-gradient:purple-yellow file and then save it as a result.png. But what is a radial-gradient:purple-yellow file? How did you get it?

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T14:42:12-07:00
by fmw42
radial-gradient: is an internal IM pseudo-image that IM creates on the fly with the colors specified. See http://www.imagemagick.org/script/formats.php#pseudo

Re: How do I use Magick++ API to create a radial gradient?

Posted: 2015-04-21T14:52:13-07:00
by Pit Sullivan
That's cool! I got it. Thus I don't have to use MagickCore API for that. Thank you so much!