DrawPolygon doesn't seem to work for me. Where is my error?

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
JohnF

DrawPolygon doesn't seem to work for me. Where is my error?

Post by JohnF »

I'm trying to port an old program of mine to use MagickWand for drawing, but I can't get DrawPolygon to work.
The following code:

Code: Select all

#define WIDTH  4000
#define HEIGHT  3000

void mwtest(void)
  {
  MagickWand   *image;
  PixelWand    *colour;
  DrawingWand  *drawing;
  PointInfo ptest[5] = {{1000, 750}, {3000, 750}, {3000, 2250}, {1000, 2250}, {1000, 750}}; 

  MagickWandGenesis();
  image  = NewMagickWand(); 
  colour = NewPixelWand();
  PixelSetColor(colour, "rgb(200, 200, 255)");
  if (!MagickNewImage(image, WIDTH, HEIGHT, colour)) return;
  drawing = NewDrawingWand();
  PixelSetColor(colour, "black");
  DrawSetStrokeColor(drawing, colour);
  PixelSetColor(colour, "white");
  DrawSetFillColor(drawing, colour);
  DrawPolygon(drawing,5,ptest);
  MagickWriteImage(image, "mwtest.png");
  MagickWandTerminus();
  }
doesn't do what I expect. I get the image created and filled with the background colour, but I don't see the polygon.

I'm sure I'm making some fundamental mistake, but I can't see what it is ...
JohnF

Re: DrawPolygon doesn't seem to work for me. Where is my error?

Post by JohnF »

I was right - it was something fundamental :-(

There was no association between the drawing commands and the image.
A call to MagickDrawImage solved that.
JohnF

Re: DrawPolygon doesn't seem to work for me. Where is my error?

Post by JohnF »

Further update - there appears to be an n^2 algorithm somewhere in the DrawPolygon/MagickDrawImage process.

I've got several thousand polygons that I want to render (some with many thousands of points).
If I call DrawPolygon multiple times, and only call MagickDrawImage once at the end, it takes forever
(I don't know how long - I don't have the patience to find out).
If, however, I call MagickDrawImage once every couple of hundred polygons or so (and then
re-initialise the drawing wand) everything completes in a reasonable amount of time.

Is this to be expected? Is the subdivision approach that I use (breaking the full list into smaller chunks)
a reasonable workaround? And if so, are there any guidelines as to what is a reasonable number of
polygons (or line segments) to render with a call to MagickDrawImage?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: DrawPolygon doesn't seem to work for me. Where is my error?

Post by magick »

The rendering algorithm is O(n^2). The only expected speed-ups are shorter point lists in the polygon or use a multi-core system where ImageMagick renders one point per core simultaneously. An alternative is to generate SVG polygons and render with rsvg which is likely O(n).
Post Reply