Example code for Magick++ and SVG file?

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
vtagle-qsi
Posts: 12
Joined: 2008-09-05T16:30:03-07:00

Example code for Magick++ and SVG file?

Post by vtagle-qsi »

Hello,

I'm working on a project that needs to display SVG files so to that end, I've been trying to use the Magick++ API to read in the SVG file and then get the raw pixel data as RGBA and stuff it into a buffer to later display on the screen. This is what I've figured out I need so far:

Code: Select all

using namespace Magick; 
Image svgImage("test.svg"); 
Geometry geom = svgImage.size(); 

svgImage.modifyImage(); 
Pixels svgPixels(svgImage); 
PixelPacket* pixels = svgPixels.get(0, 0, geom.width(), geom.height()); 

unsigned char* pixelBuffer = new unsigned char[geom.width() * geom.height() * 4];
svgImage.writePixels(RGBAQuantum, pixelBuffer); 
Unfortunately, the pixel data I get is incorrect. My suspicion is that I'm missing a step in here but I can't for the life of me figure out what. The SVG file that I'm rendering is essentially a bar graph with a vertical gradient for the background so I don't think it's anything so complex that IM can handle it.

So basically, does anyone have any tips on using the Magick++ API to handle SVGs?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Example code for Magick++ and SVG file?

Post by magick »

Are you using Q16 of ImageMagick (convert -version)? If so you need to set the image depth from 16 to 8 (image.depth(8)). See if that helps.
vtagle-qsi
Posts: 12
Joined: 2008-09-05T16:30:03-07:00

Re: Example code for Magick++ and SVG file?

Post by vtagle-qsi »

Well, setting the image depth to 8 didn't help but for those who come after me, what I ended up doing was the following:

Code: Select all

// Load the SVG data into an image and use ImageMagick to "magick" it into PNG data.
// 
Magick::Image svgFile(filename);
svgImage.magick("png");

// Write the resulting PNG data to a Blob object which is essentially a memory buffer.
// 
Magick::Blob blob; 
svgImage.write(&blob); 
Then since I have code that decodes PNG image data from memory buffers, I used the data() and length() methods of the blob to pull the PNG data out and display it in my graphics engine. :D
Post Reply