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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

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

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

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

Post by dlemstra »

Would you mind posting the command line commands that you are trying to reproduce in the Magick++ API?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

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

Post 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!
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

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

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

inner-outer is optional and represents the desired colors which default to black and white
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
Pit Sullivan
Posts: 11
Joined: 2015-04-13T14:53:20-07:00
Authentication code: 6789

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

Post by Pit Sullivan »

That's cool! I got it. Thus I don't have to use MagickCore API for that. Thank you so much!
Post Reply