MagickCore  7.0.10
draw.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % DDDD RRRR AAA W W %
7 % D D R R A A W W %
8 % D D RRRR AAAAA W W W %
9 % D D R RN A A WW WW %
10 % DDDD R R A A W W %
11 % %
12 % %
13 % MagickCore Image Drawing Methods %
14 % %
15 % %
16 % Software Design %
17 % Cristy %
18 % July 1998 %
19 % %
20 % %
21 % Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
23 % %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
26 % %
27 % https://imagemagick.org/script/license.php %
28 % %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
34 % %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
42 %
43 */
44 
45 /*
46  Include declarations.
47 */
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
54 #include "MagickCore/cache-view.h"
55 #include "MagickCore/channel.h"
56 #include "MagickCore/color.h"
58 #include "MagickCore/composite.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/draw.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/geometry.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/log.h"
72 #include "MagickCore/monitor.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/paint.h"
78 #include "MagickCore/property.h"
79 #include "MagickCore/resample.h"
81 #include "MagickCore/resource_.h"
82 #include "MagickCore/splay-tree.h"
83 #include "MagickCore/string_.h"
86 #include "MagickCore/token.h"
88 #include "MagickCore/utility.h"
89 
90 /*
91  Define declarations.
92 */
93 #define BezierQuantum 200
94 #define PrimitiveExtentPad 2053
95 #define MaxBezierCoordinates 67108864
96 #define ThrowPointExpectedException(token,exception) \
97 { \
98  (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
99  "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
100  status=MagickFalse; \
101  break; \
102 }
103 
104 /*
105  Typedef declarations.
106 */
107 typedef struct _EdgeInfo
108 {
111 
112  double
114 
115  PointInfo
117 
118  size_t
120 
121  ssize_t
123 
126 
127  size_t
129 } EdgeInfo;
130 
131 typedef struct _ElementInfo
132 {
133  double
134  cx,
135  cy,
136  major,
137  minor,
138  angle;
139 } ElementInfo;
140 
141 typedef struct _MVGInfo
142 {
145 
146  size_t
148 
149  ssize_t
151 
152  PointInfo
154 
157 } MVGInfo;
158 
159 typedef struct _PolygonInfo
160 {
161  EdgeInfo
163 
164  size_t
166 } PolygonInfo;
167 
168 typedef enum
169 {
175 } PathInfoCode;
176 
177 typedef struct _PathInfo
178 {
179  PointInfo
181 
184 } PathInfo;
185 
186 /*
187  Forward declarations.
188 */
189 static Image
190  *DrawClippingMask(Image *,const DrawInfo *,const char *,const char *,
191  ExceptionInfo *);
192 
193 static MagickBooleanType
194  DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
195  ExceptionInfo *),
196  RenderMVGContent(Image *,const DrawInfo *,const size_t,ExceptionInfo *),
197  TraceArc(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
198  TraceArcPath(MVGInfo *,const PointInfo,const PointInfo,const PointInfo,
199  const double,const MagickBooleanType,const MagickBooleanType),
200  TraceBezier(MVGInfo *,const size_t),
201  TraceCircle(MVGInfo *,const PointInfo,const PointInfo),
202  TraceEllipse(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
206  TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
207 
208 static PrimitiveInfo
209  *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
210 
211 static ssize_t
212  TracePath(MVGInfo *,const char *,ExceptionInfo *);
213 
214 /*
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % %
217 % %
218 % %
219 % A c q u i r e D r a w I n f o %
220 % %
221 % %
222 % %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 %
225 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
226 %
227 % The format of the AcquireDrawInfo method is:
228 %
229 % DrawInfo *AcquireDrawInfo(void)
230 %
231 */
233 {
234  DrawInfo
235  *draw_info;
236 
237  draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
238  GetDrawInfo((ImageInfo *) NULL,draw_info);
239  return(draw_info);
240 }
241 
242 /*
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % %
245 % %
246 % %
247 % C l o n e D r a w I n f o %
248 % %
249 % %
250 % %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 %
253 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
254 % is specified, a new DrawInfo structure is created initialized to default
255 % values.
256 %
257 % The format of the CloneDrawInfo method is:
258 %
259 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
260 % const DrawInfo *draw_info)
261 %
262 % A description of each parameter follows:
263 %
264 % o image_info: the image info.
265 %
266 % o draw_info: the draw info.
267 %
268 */
270  const DrawInfo *draw_info)
271 {
272  DrawInfo
273  *clone_info;
274 
276  *exception;
277 
278  clone_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*clone_info));
279  GetDrawInfo(image_info,clone_info);
280  if (draw_info == (DrawInfo *) NULL)
281  return(clone_info);
282  exception=AcquireExceptionInfo();
283  if (draw_info->id != (char *) NULL)
284  (void) CloneString(&clone_info->id,draw_info->id);
285  if (draw_info->primitive != (char *) NULL)
286  (void) CloneString(&clone_info->primitive,draw_info->primitive);
287  if (draw_info->geometry != (char *) NULL)
288  (void) CloneString(&clone_info->geometry,draw_info->geometry);
289  clone_info->compliance=draw_info->compliance;
290  clone_info->viewbox=draw_info->viewbox;
291  clone_info->affine=draw_info->affine;
292  clone_info->gravity=draw_info->gravity;
293  clone_info->fill=draw_info->fill;
294  clone_info->stroke=draw_info->stroke;
295  clone_info->stroke_width=draw_info->stroke_width;
296  if (draw_info->fill_pattern != (Image *) NULL)
297  clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
298  exception);
299  if (draw_info->stroke_pattern != (Image *) NULL)
300  clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
301  MagickTrue,exception);
302  clone_info->stroke_antialias=draw_info->stroke_antialias;
303  clone_info->text_antialias=draw_info->text_antialias;
304  clone_info->fill_rule=draw_info->fill_rule;
305  clone_info->linecap=draw_info->linecap;
306  clone_info->linejoin=draw_info->linejoin;
307  clone_info->miterlimit=draw_info->miterlimit;
308  clone_info->dash_offset=draw_info->dash_offset;
309  clone_info->decorate=draw_info->decorate;
310  clone_info->compose=draw_info->compose;
311  if (draw_info->text != (char *) NULL)
312  (void) CloneString(&clone_info->text,draw_info->text);
313  if (draw_info->font != (char *) NULL)
314  (void) CloneString(&clone_info->font,draw_info->font);
315  if (draw_info->metrics != (char *) NULL)
316  (void) CloneString(&clone_info->metrics,draw_info->metrics);
317  if (draw_info->family != (char *) NULL)
318  (void) CloneString(&clone_info->family,draw_info->family);
319  clone_info->style=draw_info->style;
320  clone_info->stretch=draw_info->stretch;
321  clone_info->weight=draw_info->weight;
322  if (draw_info->encoding != (char *) NULL)
323  (void) CloneString(&clone_info->encoding,draw_info->encoding);
324  clone_info->pointsize=draw_info->pointsize;
325  clone_info->kerning=draw_info->kerning;
326  clone_info->interline_spacing=draw_info->interline_spacing;
327  clone_info->interword_spacing=draw_info->interword_spacing;
328  clone_info->direction=draw_info->direction;
329  if (draw_info->density != (char *) NULL)
330  (void) CloneString(&clone_info->density,draw_info->density);
331  clone_info->align=draw_info->align;
332  clone_info->undercolor=draw_info->undercolor;
333  clone_info->border_color=draw_info->border_color;
334  if (draw_info->server_name != (char *) NULL)
335  (void) CloneString(&clone_info->server_name,draw_info->server_name);
336  if (draw_info->dash_pattern != (double *) NULL)
337  {
338  register ssize_t
339  x;
340 
341  for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
342  clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) (2*x+2),
343  sizeof(*clone_info->dash_pattern));
344  if (clone_info->dash_pattern == (double *) NULL)
346  "UnableToAllocateDashPattern");
347  (void) memset(clone_info->dash_pattern,0,(size_t) (2*x+2)*
348  sizeof(*clone_info->dash_pattern));
349  (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(size_t)
350  (x+1)*sizeof(*clone_info->dash_pattern));
351  }
352  clone_info->gradient=draw_info->gradient;
353  if (draw_info->gradient.stops != (StopInfo *) NULL)
354  {
355  size_t
356  number_stops;
357 
358  number_stops=clone_info->gradient.number_stops;
359  clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
360  number_stops,sizeof(*clone_info->gradient.stops));
361  if (clone_info->gradient.stops == (StopInfo *) NULL)
363  "UnableToAllocateDashPattern");
364  (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
365  (size_t) number_stops*sizeof(*clone_info->gradient.stops));
366  }
367  clone_info->bounds=draw_info->bounds;
368  clone_info->fill_alpha=draw_info->fill_alpha;
369  clone_info->stroke_alpha=draw_info->stroke_alpha;
370  clone_info->element_reference=draw_info->element_reference;
371  clone_info->clip_path=draw_info->clip_path;
372  clone_info->clip_units=draw_info->clip_units;
373  if (draw_info->clip_mask != (char *) NULL)
374  (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
375  if (draw_info->clipping_mask != (Image *) NULL)
376  clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
377  MagickTrue,exception);
378  if (draw_info->composite_mask != (Image *) NULL)
379  clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
380  MagickTrue,exception);
381  clone_info->render=draw_info->render;
382  clone_info->debug=IsEventLogging();
383  exception=DestroyExceptionInfo(exception);
384  return(clone_info);
385 }
386 
387 /*
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 % %
390 % %
391 % %
392 + C o n v e r t P a t h T o P o l y g o n %
393 % %
394 % %
395 % %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %
398 % ConvertPathToPolygon() converts a path to the more efficient sorted
399 % rendering form.
400 %
401 % The format of the ConvertPathToPolygon method is:
402 %
403 % PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
404 %
405 % A description of each parameter follows:
406 %
407 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
408 % rendering form of type PolygonInfo.
409 %
410 % o draw_info: Specifies a pointer to an DrawInfo structure.
411 %
412 % o path_info: Specifies a pointer to an PathInfo structure.
413 %
414 %
415 */
416 
417 #if defined(__cplusplus) || defined(c_plusplus)
418 extern "C" {
419 #endif
420 
421 static int DrawCompareEdges(const void *p_edge,const void *q_edge)
422 {
423 #define DrawCompareEdge(p,q) \
424 { \
425  if (((p)-(q)) < 0.0) \
426  return(-1); \
427  if (((p)-(q)) > 0.0) \
428  return(1); \
429 }
430 
431  register const PointInfo
432  *p,
433  *q;
434 
435  /*
436  Edge sorting for right-handed coordinate system.
437  */
438  p=((const EdgeInfo *) p_edge)->points;
439  q=((const EdgeInfo *) q_edge)->points;
440  DrawCompareEdge(p[0].y,q[0].y);
441  DrawCompareEdge(p[0].x,q[0].x);
442  DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
443  (q[1].x-q[0].x));
444  DrawCompareEdge(p[1].y,q[1].y);
445  DrawCompareEdge(p[1].x,q[1].x);
446  return(0);
447 }
448 
449 #if defined(__cplusplus) || defined(c_plusplus)
450 }
451 #endif
452 
453 static void LogPolygonInfo(const PolygonInfo *polygon_info)
454 {
455  register EdgeInfo
456  *p;
457 
458  register ssize_t
459  i,
460  j;
461 
462  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
463  p=polygon_info->edges;
464  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
465  {
466  (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
467  (double) i);
468  (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
469  p->direction != MagickFalse ? "down" : "up");
470  (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
471  p->ghostline != MagickFalse ? "transparent" : "opaque");
473  " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
474  p->bounds.x2,p->bounds.y2);
475  for (j=0; j < (ssize_t) p->number_points; j++)
476  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
477  p->points[j].x,p->points[j].y);
478  p++;
479  }
480  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
481 }
482 
483 static void ReversePoints(PointInfo *points,const size_t number_points)
484 {
485  PointInfo
486  point;
487 
488  register ssize_t
489  i;
490 
491  for (i=0; i < (ssize_t) (number_points >> 1); i++)
492  {
493  point=points[i];
494  points[i]=points[number_points-(i+1)];
495  points[number_points-(i+1)]=point;
496  }
497 }
498 
499 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
500 {
501  long
502  direction,
503  next_direction;
504 
505  PointInfo
506  point,
507  *points;
508 
510  *polygon_info;
511 
513  bounds;
514 
515  register ssize_t
516  i,
517  n;
518 
520  ghostline;
521 
522  size_t
523  edge,
524  number_edges,
525  number_points;
526 
527  /*
528  Convert a path to the more efficient sorted rendering form.
529  */
530  polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
531  if (polygon_info == (PolygonInfo *) NULL)
532  return((PolygonInfo *) NULL);
533  number_edges=16;
534  polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
535  sizeof(*polygon_info->edges));
536  if (polygon_info->edges == (EdgeInfo *) NULL)
537  return((PolygonInfo *) NULL);
538  (void) memset(polygon_info->edges,0,number_edges*
539  sizeof(*polygon_info->edges));
540  direction=0;
541  edge=0;
542  ghostline=MagickFalse;
543  n=0;
544  number_points=0;
545  points=(PointInfo *) NULL;
546  (void) memset(&point,0,sizeof(point));
547  (void) memset(&bounds,0,sizeof(bounds));
548  polygon_info->edges[edge].number_points=(size_t) n;
549  polygon_info->edges[edge].scanline=0.0;
550  polygon_info->edges[edge].highwater=0;
551  polygon_info->edges[edge].ghostline=ghostline;
552  polygon_info->edges[edge].direction=(ssize_t) direction;
553  polygon_info->edges[edge].points=points;
554  polygon_info->edges[edge].bounds=bounds;
555  polygon_info->number_edges=0;
556  for (i=0; path_info[i].code != EndCode; i++)
557  {
558  if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
559  (path_info[i].code == GhostlineCode))
560  {
561  /*
562  Move to.
563  */
564  if ((points != (PointInfo *) NULL) && (n >= 2))
565  {
566  if (edge == number_edges)
567  {
568  number_edges<<=1;
569  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
570  polygon_info->edges,(size_t) number_edges,
571  sizeof(*polygon_info->edges));
572  if (polygon_info->edges == (EdgeInfo *) NULL)
573  return((PolygonInfo *) NULL);
574  }
575  polygon_info->edges[edge].number_points=(size_t) n;
576  polygon_info->edges[edge].scanline=(-1.0);
577  polygon_info->edges[edge].highwater=0;
578  polygon_info->edges[edge].ghostline=ghostline;
579  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
580  if (direction < 0)
581  ReversePoints(points,(size_t) n);
582  polygon_info->edges[edge].points=points;
583  polygon_info->edges[edge].bounds=bounds;
584  polygon_info->edges[edge].bounds.y1=points[0].y;
585  polygon_info->edges[edge].bounds.y2=points[n-1].y;
586  points=(PointInfo *) NULL;
587  ghostline=MagickFalse;
588  edge++;
589  }
590  if (points == (PointInfo *) NULL)
591  {
592  number_points=16;
593  points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
594  sizeof(*points));
595  if (points == (PointInfo *) NULL)
596  return((PolygonInfo *) NULL);
597  }
598  ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
599  point=path_info[i].point;
600  points[0]=point;
601  bounds.x1=point.x;
602  bounds.x2=point.x;
603  direction=0;
604  n=1;
605  continue;
606  }
607  /*
608  Line to.
609  */
610  next_direction=((path_info[i].point.y > point.y) ||
611  ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
612  (path_info[i].point.x > point.x))) ? 1 : -1;
613  if ((points != (PointInfo *) NULL) && (direction != 0) &&
614  (direction != next_direction))
615  {
616  /*
617  New edge.
618  */
619  point=points[n-1];
620  if (edge == number_edges)
621  {
622  number_edges<<=1;
623  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
624  polygon_info->edges,(size_t) number_edges,
625  sizeof(*polygon_info->edges));
626  if (polygon_info->edges == (EdgeInfo *) NULL)
627  return((PolygonInfo *) NULL);
628  }
629  polygon_info->edges[edge].number_points=(size_t) n;
630  polygon_info->edges[edge].scanline=(-1.0);
631  polygon_info->edges[edge].highwater=0;
632  polygon_info->edges[edge].ghostline=ghostline;
633  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
634  if (direction < 0)
635  ReversePoints(points,(size_t) n);
636  polygon_info->edges[edge].points=points;
637  polygon_info->edges[edge].bounds=bounds;
638  polygon_info->edges[edge].bounds.y1=points[0].y;
639  polygon_info->edges[edge].bounds.y2=points[n-1].y;
640  number_points=16;
641  points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
642  sizeof(*points));
643  if (points == (PointInfo *) NULL)
644  return((PolygonInfo *) NULL);
645  n=1;
646  ghostline=MagickFalse;
647  points[0]=point;
648  bounds.x1=point.x;
649  bounds.x2=point.x;
650  edge++;
651  }
652  direction=next_direction;
653  if (points == (PointInfo *) NULL)
654  continue;
655  if (n == (ssize_t) number_points)
656  {
657  number_points<<=1;
658  points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
659  sizeof(*points));
660  if (points == (PointInfo *) NULL)
661  return((PolygonInfo *) NULL);
662  }
663  point=path_info[i].point;
664  points[n]=point;
665  if (point.x < bounds.x1)
666  bounds.x1=point.x;
667  if (point.x > bounds.x2)
668  bounds.x2=point.x;
669  n++;
670  }
671  if (points != (PointInfo *) NULL)
672  {
673  if (n < 2)
674  points=(PointInfo *) RelinquishMagickMemory(points);
675  else
676  {
677  if (edge == number_edges)
678  {
679  number_edges<<=1;
680  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
681  polygon_info->edges,(size_t) number_edges,
682  sizeof(*polygon_info->edges));
683  if (polygon_info->edges == (EdgeInfo *) NULL)
684  return((PolygonInfo *) NULL);
685  }
686  polygon_info->edges[edge].number_points=(size_t) n;
687  polygon_info->edges[edge].scanline=(-1.0);
688  polygon_info->edges[edge].highwater=0;
689  polygon_info->edges[edge].ghostline=ghostline;
690  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
691  if (direction < 0)
692  ReversePoints(points,(size_t) n);
693  polygon_info->edges[edge].points=points;
694  polygon_info->edges[edge].bounds=bounds;
695  polygon_info->edges[edge].bounds.y1=points[0].y;
696  polygon_info->edges[edge].bounds.y2=points[n-1].y;
697  ghostline=MagickFalse;
698  edge++;
699  }
700  }
701  polygon_info->number_edges=edge;
702  qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
703  sizeof(*polygon_info->edges),DrawCompareEdges);
704  if (IsEventLogging() != MagickFalse)
705  LogPolygonInfo(polygon_info);
706  return(polygon_info);
707 }
708 
709 /*
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 % %
712 % %
713 % %
714 + C o n v e r t P r i m i t i v e T o P a t h %
715 % %
716 % %
717 % %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 %
720 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
721 % path structure.
722 %
723 % The format of the ConvertPrimitiveToPath method is:
724 %
725 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
726 % const PrimitiveInfo *primitive_info)
727 %
728 % A description of each parameter follows:
729 %
730 % o Method ConvertPrimitiveToPath returns a vector path structure of type
731 % PathInfo.
732 %
733 % o draw_info: a structure of type DrawInfo.
734 %
735 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
736 %
737 %
738 */
739 
740 static void LogPathInfo(const PathInfo *path_info)
741 {
742  register const PathInfo
743  *p;
744 
745  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
746  for (p=path_info; p->code != EndCode; p++)
748  " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
749  "moveto ghostline" : p->code == OpenCode ? "moveto open" :
750  p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
751  "?");
752  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
753 }
754 
755 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
756 {
758  closed_subpath;
759 
760  PathInfo
761  *path_info;
762 
764  code;
765 
766  PointInfo
767  p,
768  q;
769 
770  register ssize_t
771  i,
772  n;
773 
774  ssize_t
775  coordinates,
776  start;
777 
778  /*
779  Converts a PrimitiveInfo structure into a vector path structure.
780  */
781  switch (primitive_info->primitive)
782  {
783  case AlphaPrimitive:
784  case ColorPrimitive:
785  case ImagePrimitive:
786  case PointPrimitive:
787  case TextPrimitive:
788  return((PathInfo *) NULL);
789  default:
790  break;
791  }
792  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
793  path_info=(PathInfo *) AcquireQuantumMemory((size_t) (3UL*i+1UL),
794  sizeof(*path_info));
795  if (path_info == (PathInfo *) NULL)
796  return((PathInfo *) NULL);
797  coordinates=0;
798  closed_subpath=MagickFalse;
799  n=0;
800  p.x=(-1.0);
801  p.y=(-1.0);
802  q.x=(-1.0);
803  q.y=(-1.0);
804  start=0;
805  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
806  {
807  code=LineToCode;
808  if (coordinates <= 0)
809  {
810  /*
811  New subpath.
812  */
813  coordinates=(ssize_t) primitive_info[i].coordinates;
814  p=primitive_info[i].point;
815  start=n;
816  code=MoveToCode;
817  closed_subpath=primitive_info[i].closed_subpath;
818  }
819  coordinates--;
820  if ((code == MoveToCode) || (coordinates <= 0) ||
821  (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
822  (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
823  {
824  /*
825  Eliminate duplicate points.
826  */
827  path_info[n].code=code;
828  path_info[n].point=primitive_info[i].point;
829  q=primitive_info[i].point;
830  n++;
831  }
832  if (coordinates > 0)
833  continue; /* next point in current subpath */
834  if (closed_subpath != MagickFalse)
835  {
836  closed_subpath=MagickFalse;
837  continue;
838  }
839  /*
840  Mark the p point as open if the subpath is not closed.
841  */
842  path_info[start].code=OpenCode;
843  path_info[n].code=GhostlineCode;
844  path_info[n].point=primitive_info[i].point;
845  n++;
846  path_info[n].code=LineToCode;
847  path_info[n].point=p;
848  n++;
849  }
850  path_info[n].code=EndCode;
851  path_info[n].point.x=0.0;
852  path_info[n].point.y=0.0;
853  if (IsEventLogging() != MagickFalse)
854  LogPathInfo(path_info);
855  path_info=(PathInfo *) ResizeQuantumMemory(path_info,(size_t) (n+1),
856  sizeof(*path_info));
857  return(path_info);
858 }
859 
860 /*
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % %
863 % %
864 % %
865 % D e s t r o y D r a w I n f o %
866 % %
867 % %
868 % %
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 %
871 % DestroyDrawInfo() deallocates memory associated with an DrawInfo structure.
872 %
873 % The format of the DestroyDrawInfo method is:
874 %
875 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
876 %
877 % A description of each parameter follows:
878 %
879 % o draw_info: the draw info.
880 %
881 */
883 {
884  assert(draw_info != (DrawInfo *) NULL);
885  if (draw_info->debug != MagickFalse)
886  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
887  assert(draw_info->signature == MagickCoreSignature);
888  if (draw_info->id != (char *) NULL)
889  draw_info->id=DestroyString(draw_info->id);
890  if (draw_info->primitive != (char *) NULL)
891  draw_info->primitive=DestroyString(draw_info->primitive);
892  if (draw_info->text != (char *) NULL)
893  draw_info->text=DestroyString(draw_info->text);
894  if (draw_info->geometry != (char *) NULL)
895  draw_info->geometry=DestroyString(draw_info->geometry);
896  if (draw_info->fill_pattern != (Image *) NULL)
897  draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
898  if (draw_info->stroke_pattern != (Image *) NULL)
899  draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
900  if (draw_info->font != (char *) NULL)
901  draw_info->font=DestroyString(draw_info->font);
902  if (draw_info->metrics != (char *) NULL)
903  draw_info->metrics=DestroyString(draw_info->metrics);
904  if (draw_info->family != (char *) NULL)
905  draw_info->family=DestroyString(draw_info->family);
906  if (draw_info->encoding != (char *) NULL)
907  draw_info->encoding=DestroyString(draw_info->encoding);
908  if (draw_info->density != (char *) NULL)
909  draw_info->density=DestroyString(draw_info->density);
910  if (draw_info->server_name != (char *) NULL)
911  draw_info->server_name=(char *)
913  if (draw_info->dash_pattern != (double *) NULL)
914  draw_info->dash_pattern=(double *) RelinquishMagickMemory(
915  draw_info->dash_pattern);
916  if (draw_info->gradient.stops != (StopInfo *) NULL)
918  draw_info->gradient.stops);
919  if (draw_info->clip_mask != (char *) NULL)
920  draw_info->clip_mask=DestroyString(draw_info->clip_mask);
921  if (draw_info->clipping_mask != (Image *) NULL)
922  draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
923  if (draw_info->composite_mask != (Image *) NULL)
924  draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
925  draw_info->signature=(~MagickCoreSignature);
926  draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
927  return(draw_info);
928 }
929 
930 /*
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 % %
933 % %
934 % %
935 + D e s t r o y E d g e %
936 % %
937 % %
938 % %
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 %
941 % DestroyEdge() destroys the specified polygon edge.
942 %
943 % The format of the DestroyEdge method is:
944 %
945 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
946 %
947 % A description of each parameter follows:
948 %
949 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
950 %
951 % o edge: the polygon edge number to destroy.
952 %
953 */
954 static size_t DestroyEdge(PolygonInfo *polygon_info,
955  const size_t edge)
956 {
957  assert(edge < polygon_info->number_edges);
958  polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
959  polygon_info->edges[edge].points);
960  polygon_info->number_edges--;
961  if (edge < polygon_info->number_edges)
962  (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
963  (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
964  return(polygon_info->number_edges);
965 }
966 
967 /*
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 % %
970 % %
971 % %
972 + D e s t r o y P o l y g o n I n f o %
973 % %
974 % %
975 % %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977 %
978 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
979 %
980 % The format of the DestroyPolygonInfo method is:
981 %
982 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
983 %
984 % A description of each parameter follows:
985 %
986 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
987 %
988 */
990 {
991  register ssize_t
992  i;
993 
994  if (polygon_info->edges != (EdgeInfo *) NULL)
995  {
996  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
997  polygon_info->edges[i].points=(PointInfo *)
998  RelinquishMagickMemory(polygon_info->edges[i].points);
999  polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
1000  polygon_info->edges);
1001  }
1002  return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
1003 }
1004 
1005 /*
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007 % %
1008 % %
1009 % %
1010 % D r a w A f f i n e I m a g e %
1011 % %
1012 % %
1013 % %
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 %
1016 % DrawAffineImage() composites the source over the destination image as
1017 % dictated by the affine transform.
1018 %
1019 % The format of the DrawAffineImage method is:
1020 %
1021 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
1022 % const AffineMatrix *affine,ExceptionInfo *exception)
1023 %
1024 % A description of each parameter follows:
1025 %
1026 % o image: the image.
1027 %
1028 % o source: the source image.
1029 %
1030 % o affine: the affine transform.
1031 %
1032 % o exception: return any errors or warnings in this structure.
1033 %
1034 */
1035 
1036 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
1037  const double y,const SegmentInfo *edge)
1038 {
1039  double
1040  intercept,
1041  z;
1042 
1043  register double
1044  x;
1045 
1046  SegmentInfo
1047  inverse_edge;
1048 
1049  /*
1050  Determine left and right edges.
1051  */
1052  inverse_edge.x1=edge->x1;
1053  inverse_edge.y1=edge->y1;
1054  inverse_edge.x2=edge->x2;
1055  inverse_edge.y2=edge->y2;
1056  z=affine->ry*y+affine->tx;
1057  if (affine->sx >= MagickEpsilon)
1058  {
1059  intercept=(-z/affine->sx);
1060  x=intercept;
1061  if (x > inverse_edge.x1)
1062  inverse_edge.x1=x;
1063  intercept=(-z+(double) image->columns)/affine->sx;
1064  x=intercept;
1065  if (x < inverse_edge.x2)
1066  inverse_edge.x2=x;
1067  }
1068  else
1069  if (affine->sx < -MagickEpsilon)
1070  {
1071  intercept=(-z+(double) image->columns)/affine->sx;
1072  x=intercept;
1073  if (x > inverse_edge.x1)
1074  inverse_edge.x1=x;
1075  intercept=(-z/affine->sx);
1076  x=intercept;
1077  if (x < inverse_edge.x2)
1078  inverse_edge.x2=x;
1079  }
1080  else
1081  if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1082  {
1083  inverse_edge.x2=edge->x1;
1084  return(inverse_edge);
1085  }
1086  /*
1087  Determine top and bottom edges.
1088  */
1089  z=affine->sy*y+affine->ty;
1090  if (affine->rx >= MagickEpsilon)
1091  {
1092  intercept=(-z/affine->rx);
1093  x=intercept;
1094  if (x > inverse_edge.x1)
1095  inverse_edge.x1=x;
1096  intercept=(-z+(double) image->rows)/affine->rx;
1097  x=intercept;
1098  if (x < inverse_edge.x2)
1099  inverse_edge.x2=x;
1100  }
1101  else
1102  if (affine->rx < -MagickEpsilon)
1103  {
1104  intercept=(-z+(double) image->rows)/affine->rx;
1105  x=intercept;
1106  if (x > inverse_edge.x1)
1107  inverse_edge.x1=x;
1108  intercept=(-z/affine->rx);
1109  x=intercept;
1110  if (x < inverse_edge.x2)
1111  inverse_edge.x2=x;
1112  }
1113  else
1114  if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1115  {
1116  inverse_edge.x2=edge->x2;
1117  return(inverse_edge);
1118  }
1119  return(inverse_edge);
1120 }
1121 
1123 {
1124  AffineMatrix
1125  inverse_affine;
1126 
1127  double
1128  determinant;
1129 
1130  determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1131  affine->ry);
1132  inverse_affine.sx=determinant*affine->sy;
1133  inverse_affine.rx=determinant*(-affine->rx);
1134  inverse_affine.ry=determinant*(-affine->ry);
1135  inverse_affine.sy=determinant*affine->sx;
1136  inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1137  inverse_affine.ry;
1138  inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1139  inverse_affine.sy;
1140  return(inverse_affine);
1141 }
1142 
1144  const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1145 {
1146  AffineMatrix
1147  inverse_affine;
1148 
1149  CacheView
1150  *image_view,
1151  *source_view;
1152 
1154  status;
1155 
1156  PixelInfo
1157  zero;
1158 
1159  PointInfo
1160  extent[4],
1161  min,
1162  max;
1163 
1164  register ssize_t
1165  i;
1166 
1167  SegmentInfo
1168  edge;
1169 
1170  ssize_t
1171  start,
1172  stop,
1173  y;
1174 
1175  /*
1176  Determine bounding box.
1177  */
1178  assert(image != (Image *) NULL);
1179  assert(image->signature == MagickCoreSignature);
1180  if (image->debug != MagickFalse)
1181  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1182  assert(source != (const Image *) NULL);
1183  assert(source->signature == MagickCoreSignature);
1184  assert(affine != (AffineMatrix *) NULL);
1185  extent[0].x=0.0;
1186  extent[0].y=0.0;
1187  extent[1].x=(double) source->columns-1.0;
1188  extent[1].y=0.0;
1189  extent[2].x=(double) source->columns-1.0;
1190  extent[2].y=(double) source->rows-1.0;
1191  extent[3].x=0.0;
1192  extent[3].y=(double) source->rows-1.0;
1193  for (i=0; i < 4; i++)
1194  {
1195  PointInfo
1196  point;
1197 
1198  point=extent[i];
1199  extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1200  extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1201  }
1202  min=extent[0];
1203  max=extent[0];
1204  for (i=1; i < 4; i++)
1205  {
1206  if (min.x > extent[i].x)
1207  min.x=extent[i].x;
1208  if (min.y > extent[i].y)
1209  min.y=extent[i].y;
1210  if (max.x < extent[i].x)
1211  max.x=extent[i].x;
1212  if (max.y < extent[i].y)
1213  max.y=extent[i].y;
1214  }
1215  /*
1216  Affine transform image.
1217  */
1218  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1219  return(MagickFalse);
1220  status=MagickTrue;
1221  edge.x1=MagickMax(min.x,0.0);
1222  edge.y1=MagickMax(min.y,0.0);
1223  edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1224  edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1225  inverse_affine=InverseAffineMatrix(affine);
1226  GetPixelInfo(image,&zero);
1227  start=(ssize_t) ceil(edge.y1-0.5);
1228  stop=(ssize_t) floor(edge.y2+0.5);
1229  source_view=AcquireVirtualCacheView(source,exception);
1230  image_view=AcquireAuthenticCacheView(image,exception);
1231 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1232  #pragma omp parallel for schedule(static) shared(status) \
1233  magick_number_threads(source,image,stop-start,1)
1234 #endif
1235  for (y=start; y <= stop; y++)
1236  {
1237  PixelInfo
1238  composite,
1239  pixel;
1240 
1241  PointInfo
1242  point;
1243 
1244  register ssize_t
1245  x;
1246 
1247  register Quantum
1248  *magick_restrict q;
1249 
1250  SegmentInfo
1251  inverse_edge;
1252 
1253  ssize_t
1254  x_offset;
1255 
1256  inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1257  if (inverse_edge.x2 < inverse_edge.x1)
1258  continue;
1259  q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1260  0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1261  1,exception);
1262  if (q == (Quantum *) NULL)
1263  continue;
1264  pixel=zero;
1265  composite=zero;
1266  x_offset=0;
1267  for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1268  {
1269  point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1270  inverse_affine.tx;
1271  point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1272  inverse_affine.ty;
1273  status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1274  point.x,point.y,&pixel,exception);
1275  if (status == MagickFalse)
1276  break;
1277  GetPixelInfoPixel(image,q,&composite);
1278  CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1279  &composite);
1280  SetPixelViaPixelInfo(image,&composite,q);
1281  x_offset++;
1282  q+=GetPixelChannels(image);
1283  }
1284  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1285  status=MagickFalse;
1286  }
1287  source_view=DestroyCacheView(source_view);
1288  image_view=DestroyCacheView(image_view);
1289  return(status);
1290 }
1291 
1292 /*
1293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1294 % %
1295 % %
1296 % %
1297 + D r a w B o u n d i n g R e c t a n g l e s %
1298 % %
1299 % %
1300 % %
1301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1302 %
1303 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1304 % is only useful for developers debugging the rendering algorithm.
1305 %
1306 % The format of the DrawBoundingRectangles method is:
1307 %
1308 % MagickBooleanType DrawBoundingRectangles(Image *image,
1309 % const DrawInfo *draw_info,PolygonInfo *polygon_info,
1310 % ExceptionInfo *exception)
1311 %
1312 % A description of each parameter follows:
1313 %
1314 % o image: the image.
1315 %
1316 % o draw_info: the draw info.
1317 %
1318 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1319 %
1320 % o exception: return any errors or warnings in this structure.
1321 %
1322 */
1323 
1325  const DrawInfo *draw_info,const PolygonInfo *polygon_info,
1326  ExceptionInfo *exception)
1327 {
1328  double
1329  mid;
1330 
1331  DrawInfo
1332  *clone_info;
1333 
1335  status;
1336 
1337  PointInfo
1338  end,
1339  resolution,
1340  start;
1341 
1343  primitive_info[6];
1344 
1345  register ssize_t
1346  i;
1347 
1348  SegmentInfo
1349  bounds;
1350 
1351  ssize_t
1352  coordinates;
1353 
1354  (void) memset(primitive_info,0,sizeof(primitive_info));
1355  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1356  status=QueryColorCompliance("#000F",AllCompliance,&clone_info->fill,
1357  exception);
1358  if (status == MagickFalse)
1359  {
1360  clone_info=DestroyDrawInfo(clone_info);
1361  return(MagickFalse);
1362  }
1363  resolution.x=96.0;
1364  resolution.y=96.0;
1365  if (clone_info->density != (char *) NULL)
1366  {
1367  GeometryInfo
1368  geometry_info;
1369 
1371  flags;
1372 
1373  flags=ParseGeometry(clone_info->density,&geometry_info);
1374  resolution.x=geometry_info.rho;
1375  resolution.y=geometry_info.sigma;
1376  if ((flags & SigmaValue) == MagickFalse)
1377  resolution.y=resolution.x;
1378  }
1379  mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1380  clone_info->stroke_width/2.0;
1381  bounds.x1=0.0;
1382  bounds.y1=0.0;
1383  bounds.x2=0.0;
1384  bounds.y2=0.0;
1385  if (polygon_info != (PolygonInfo *) NULL)
1386  {
1387  bounds=polygon_info->edges[0].bounds;
1388  for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1389  {
1390  if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1391  bounds.x1=polygon_info->edges[i].bounds.x1;
1392  if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1393  bounds.y1=polygon_info->edges[i].bounds.y1;
1394  if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1395  bounds.x2=polygon_info->edges[i].bounds.x2;
1396  if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1397  bounds.y2=polygon_info->edges[i].bounds.y2;
1398  }
1399  bounds.x1-=mid;
1400  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1401  image->columns ? (double) image->columns-1 : bounds.x1;
1402  bounds.y1-=mid;
1403  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1404  image->rows ? (double) image->rows-1 : bounds.y1;
1405  bounds.x2+=mid;
1406  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1407  image->columns ? (double) image->columns-1 : bounds.x2;
1408  bounds.y2+=mid;
1409  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1410  image->rows ? (double) image->rows-1 : bounds.y2;
1411  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1412  {
1413  if (polygon_info->edges[i].direction != 0)
1414  status=QueryColorCompliance("#f00",AllCompliance,&clone_info->stroke,
1415  exception);
1416  else
1417  status=QueryColorCompliance("#0f0",AllCompliance,&clone_info->stroke,
1418  exception);
1419  if (status == MagickFalse)
1420  break;
1421  start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1422  start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1423  end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1424  end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1425  primitive_info[0].primitive=RectanglePrimitive;
1426  status&=TraceRectangle(primitive_info,start,end);
1427  primitive_info[0].method=ReplaceMethod;
1428  coordinates=(ssize_t) primitive_info[0].coordinates;
1429  primitive_info[coordinates].primitive=UndefinedPrimitive;
1430  status=DrawPrimitive(image,clone_info,primitive_info,exception);
1431  if (status == MagickFalse)
1432  break;
1433  }
1434  if (i < (ssize_t) polygon_info->number_edges)
1435  {
1436  clone_info=DestroyDrawInfo(clone_info);
1437  return(status == 0 ? MagickFalse : MagickTrue);
1438  }
1439  }
1440  status=QueryColorCompliance("#00f",AllCompliance,&clone_info->stroke,
1441  exception);
1442  if (status == MagickFalse)
1443  {
1444  clone_info=DestroyDrawInfo(clone_info);
1445  return(MagickFalse);
1446  }
1447  start.x=(double) (bounds.x1-mid);
1448  start.y=(double) (bounds.y1-mid);
1449  end.x=(double) (bounds.x2+mid);
1450  end.y=(double) (bounds.y2+mid);
1451  primitive_info[0].primitive=RectanglePrimitive;
1452  status&=TraceRectangle(primitive_info,start,end);
1453  primitive_info[0].method=ReplaceMethod;
1454  coordinates=(ssize_t) primitive_info[0].coordinates;
1455  primitive_info[coordinates].primitive=UndefinedPrimitive;
1456  status=DrawPrimitive(image,clone_info,primitive_info,exception);
1457  clone_info=DestroyDrawInfo(clone_info);
1458  return(status == 0 ? MagickFalse : MagickTrue);
1459 }
1460 
1461 /*
1462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 % %
1464 % %
1465 % %
1466 % D r a w C l i p P a t h %
1467 % %
1468 % %
1469 % %
1470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471 %
1472 % DrawClipPath() draws the clip path on the image mask.
1473 %
1474 % The format of the DrawClipPath method is:
1475 %
1476 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1477 % const char *id,ExceptionInfo *exception)
1478 %
1479 % A description of each parameter follows:
1480 %
1481 % o image: the image.
1482 %
1483 % o draw_info: the draw info.
1484 %
1485 % o id: the clip path id.
1486 %
1487 % o exception: return any errors or warnings in this structure.
1488 %
1489 */
1491  const DrawInfo *draw_info,const char *id,ExceptionInfo *exception)
1492 {
1493  const char
1494  *clip_path;
1495 
1496  Image
1497  *clipping_mask;
1498 
1500  status;
1501 
1502  clip_path=GetImageArtifact(image,id);
1503  if (clip_path == (const char *) NULL)
1504  return(MagickFalse);
1505  clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1506  exception);
1507  if (clipping_mask == (Image *) NULL)
1508  return(MagickFalse);
1509  status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1510  clipping_mask=DestroyImage(clipping_mask);
1511  return(status);
1512 }
1513 
1514 /*
1515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1516 % %
1517 % %
1518 % %
1519 % D r a w C l i p p i n g M a s k %
1520 % %
1521 % %
1522 % %
1523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 %
1525 % DrawClippingMask() draws the clip path and returns it as an image clipping
1526 % mask.
1527 %
1528 % The format of the DrawClippingMask method is:
1529 %
1530 % Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1531 % const char *id,const char *clip_path,ExceptionInfo *exception)
1532 %
1533 % A description of each parameter follows:
1534 %
1535 % o image: the image.
1536 %
1537 % o draw_info: the draw info.
1538 %
1539 % o id: the clip path id.
1540 %
1541 % o clip_path: the clip path.
1542 %
1543 % o exception: return any errors or warnings in this structure.
1544 %
1545 */
1546 static Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1547  const char *id,const char *clip_path,ExceptionInfo *exception)
1548 {
1549  DrawInfo
1550  *clone_info;
1551 
1552  Image
1553  *clip_mask,
1554  *separate_mask;
1555 
1557  status;
1558 
1559  /*
1560  Draw a clip path.
1561  */
1562  assert(image != (Image *) NULL);
1563  assert(image->signature == MagickCoreSignature);
1564  if (image->debug != MagickFalse)
1565  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1566  assert(draw_info != (const DrawInfo *) NULL);
1567  clip_mask=AcquireImage((const ImageInfo *) NULL,exception);
1568  status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1569  if (status == MagickFalse)
1570  return(DestroyImage(clip_mask));
1571  status=SetImageMask(clip_mask,WritePixelMask,(Image *) NULL,exception);
1572  status=QueryColorCompliance("#0000",AllCompliance,
1573  &clip_mask->background_color,exception);
1576  status=SetImageBackgroundColor(clip_mask,exception);
1577  if (image->debug != MagickFalse)
1578  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1579  id);
1580  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1581  (void) CloneString(&clone_info->primitive,clip_path);
1582  status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1583  exception);
1584  if (clone_info->clip_mask != (char *) NULL)
1585  clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1586  status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1587  exception);
1588  clone_info->stroke_width=0.0;
1589  clone_info->alpha=OpaqueAlpha;
1590  clone_info->clip_path=MagickTrue;
1591  status=RenderMVGContent(clip_mask,clone_info,0,exception);
1592  clone_info=DestroyDrawInfo(clone_info);
1593  separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1594  if (separate_mask != (Image *) NULL)
1595  {
1596  clip_mask=DestroyImage(clip_mask);
1597  clip_mask=separate_mask;
1598  status=NegateImage(clip_mask,MagickFalse,exception);
1599  if (status == MagickFalse)
1600  clip_mask=DestroyImage(clip_mask);
1601  }
1602  if (status == MagickFalse)
1603  clip_mask=DestroyImage(clip_mask);
1604  if (image->debug != MagickFalse)
1605  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1606  return(clip_mask);
1607 }
1608 
1609 /*
1610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 % %
1612 % %
1613 % %
1614 % D r a w C o m p o s i t e M a s k %
1615 % %
1616 % %
1617 % %
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 %
1620 % DrawCompositeMask() draws the mask path and returns it as an image mask.
1621 %
1622 % The format of the DrawCompositeMask method is:
1623 %
1624 % Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1625 % const char *id,const char *mask_path,ExceptionInfo *exception)
1626 %
1627 % A description of each parameter follows:
1628 %
1629 % o image: the image.
1630 %
1631 % o draw_info: the draw info.
1632 %
1633 % o id: the mask path id.
1634 %
1635 % o mask_path: the mask path.
1636 %
1637 % o exception: return any errors or warnings in this structure.
1638 %
1639 */
1640 static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1641  const char *id,const char *mask_path,ExceptionInfo *exception)
1642 {
1643  Image
1644  *composite_mask,
1645  *separate_mask;
1646 
1647  DrawInfo
1648  *clone_info;
1649 
1651  status;
1652 
1653  /*
1654  Draw a mask path.
1655  */
1656  assert(image != (Image *) NULL);
1657  assert(image->signature == MagickCoreSignature);
1658  if (image->debug != MagickFalse)
1659  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1660  assert(draw_info != (const DrawInfo *) NULL);
1661  composite_mask=AcquireImage((const ImageInfo *) NULL,exception);
1662  status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1663  if (status == MagickFalse)
1664  return(DestroyImage(composite_mask));
1665  status=SetImageMask(composite_mask,CompositePixelMask,(Image *) NULL,
1666  exception);
1667  status=QueryColorCompliance("#0000",AllCompliance,
1668  &composite_mask->background_color,exception);
1671  (void) SetImageBackgroundColor(composite_mask,exception);
1672  if (image->debug != MagickFalse)
1673  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin mask-path %s",
1674  id);
1675  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1676  (void) CloneString(&clone_info->primitive,mask_path);
1677  status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1678  exception);
1679  status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1680  exception);
1681  clone_info->stroke_width=0.0;
1682  clone_info->alpha=OpaqueAlpha;
1683  status=RenderMVGContent(composite_mask,clone_info,0,exception);
1684  clone_info=DestroyDrawInfo(clone_info);
1685  separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1686  if (separate_mask != (Image *) NULL)
1687  {
1688  composite_mask=DestroyImage(composite_mask);
1689  composite_mask=separate_mask;
1690  status=NegateImage(composite_mask,MagickFalse,exception);
1691  if (status == MagickFalse)
1692  composite_mask=DestroyImage(composite_mask);
1693  }
1694  if (status == MagickFalse)
1695  composite_mask=DestroyImage(composite_mask);
1696  if (image->debug != MagickFalse)
1697  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
1698  return(composite_mask);
1699 }
1700 
1701 /*
1702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1703 % %
1704 % %
1705 % %
1706 + D r a w D a s h P o l y g o n %
1707 % %
1708 % %
1709 % %
1710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1711 %
1712 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1713 % image while respecting the dash offset and dash pattern attributes.
1714 %
1715 % The format of the DrawDashPolygon method is:
1716 %
1717 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1718 % const PrimitiveInfo *primitive_info,Image *image,
1719 % ExceptionInfo *exception)
1720 %
1721 % A description of each parameter follows:
1722 %
1723 % o draw_info: the draw info.
1724 %
1725 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1726 %
1727 % o image: the image.
1728 %
1729 % o exception: return any errors or warnings in this structure.
1730 %
1731 */
1733  const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1734 {
1735  double
1736  length,
1737  maximum_length,
1738  offset,
1739  scale,
1740  total_length;
1741 
1742  DrawInfo
1743  *clone_info;
1744 
1746  status;
1747 
1749  *dash_polygon;
1750 
1751  register double
1752  dx,
1753  dy;
1754 
1755  register ssize_t
1756  i;
1757 
1758  size_t
1759  number_vertices;
1760 
1761  ssize_t
1762  j,
1763  n;
1764 
1765  assert(draw_info != (const DrawInfo *) NULL);
1766  if (image->debug != MagickFalse)
1767  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1768  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1769  number_vertices=(size_t) i;
1770  dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1771  (2UL*number_vertices+32UL),sizeof(*dash_polygon));
1772  if (dash_polygon == (PrimitiveInfo *) NULL)
1773  return(MagickFalse);
1774  (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1775  sizeof(*dash_polygon));
1776  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1777  clone_info->miterlimit=0;
1778  dash_polygon[0]=primitive_info[0];
1779  scale=ExpandAffine(&draw_info->affine);
1780  length=scale*draw_info->dash_pattern[0];
1781  offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1782  scale*draw_info->dash_offset : 0.0;
1783  j=1;
1784  for (n=0; offset > 0.0; j=0)
1785  {
1786  if (draw_info->dash_pattern[n] <= 0.0)
1787  break;
1788  length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1789  if (offset > length)
1790  {
1791  offset-=length;
1792  n++;
1793  length=scale*draw_info->dash_pattern[n];
1794  continue;
1795  }
1796  if (offset < length)
1797  {
1798  length-=offset;
1799  offset=0.0;
1800  break;
1801  }
1802  offset=0.0;
1803  n++;
1804  }
1805  status=MagickTrue;
1806  maximum_length=0.0;
1807  total_length=0.0;
1808  for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1809  {
1810  dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1811  dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1812  maximum_length=hypot(dx,dy);
1813  if (maximum_length > (double) (MaxBezierCoordinates >> 2))
1814  break;
1815  if (fabs(length) < MagickEpsilon)
1816  {
1817  if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1818  n++;
1819  if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1820  n=0;
1821  length=scale*draw_info->dash_pattern[n];
1822  }
1823  for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1824  {
1825  total_length+=length;
1826  if ((n & 0x01) != 0)
1827  {
1828  dash_polygon[0]=primitive_info[0];
1829  dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1830  total_length*PerceptibleReciprocal(maximum_length));
1831  dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1832  total_length*PerceptibleReciprocal(maximum_length));
1833  j=1;
1834  }
1835  else
1836  {
1837  if ((j+1) > (ssize_t) number_vertices)
1838  break;
1839  dash_polygon[j]=primitive_info[i-1];
1840  dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1841  total_length*PerceptibleReciprocal(maximum_length));
1842  dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1843  total_length*PerceptibleReciprocal(maximum_length));
1844  dash_polygon[j].coordinates=1;
1845  j++;
1846  dash_polygon[0].coordinates=(size_t) j;
1847  dash_polygon[j].primitive=UndefinedPrimitive;
1848  status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1849  if (status == MagickFalse)
1850  break;
1851  }
1852  if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1853  n++;
1854  if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1855  n=0;
1856  length=scale*draw_info->dash_pattern[n];
1857  }
1858  length-=(maximum_length-total_length);
1859  if ((n & 0x01) != 0)
1860  continue;
1861  dash_polygon[j]=primitive_info[i];
1862  dash_polygon[j].coordinates=1;
1863  j++;
1864  }
1865  if ((status != MagickFalse) && (total_length < maximum_length) &&
1866  ((n & 0x01) == 0) && (j > 1))
1867  {
1868  dash_polygon[j]=primitive_info[i-1];
1869  dash_polygon[j].point.x+=MagickEpsilon;
1870  dash_polygon[j].point.y+=MagickEpsilon;
1871  dash_polygon[j].coordinates=1;
1872  j++;
1873  dash_polygon[0].coordinates=(size_t) j;
1874  dash_polygon[j].primitive=UndefinedPrimitive;
1875  status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1876  }
1877  dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1878  clone_info=DestroyDrawInfo(clone_info);
1879  if (image->debug != MagickFalse)
1880  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1881  return(status != 0 ? MagickTrue : MagickFalse);
1882 }
1883 
1884 /*
1885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1886 % %
1887 % %
1888 % %
1889 % D r a w G r a d i e n t I m a g e %
1890 % %
1891 % %
1892 % %
1893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1894 %
1895 % DrawGradientImage() draws a linear gradient on the image.
1896 %
1897 % The format of the DrawGradientImage method is:
1898 %
1899 % MagickBooleanType DrawGradientImage(Image *image,
1900 % const DrawInfo *draw_info,ExceptionInfo *exception)
1901 %
1902 % A description of each parameter follows:
1903 %
1904 % o image: the image.
1905 %
1906 % o draw_info: the draw info.
1907 %
1908 % o exception: return any errors or warnings in this structure.
1909 %
1910 */
1911 
1912 static inline double GetStopColorOffset(const GradientInfo *gradient,
1913  const ssize_t x,const ssize_t y)
1914 {
1915  switch (gradient->type)
1916  {
1917  case UndefinedGradient:
1918  case LinearGradient:
1919  {
1920  double
1921  gamma,
1922  length,
1923  offset,
1924  scale;
1925 
1926  PointInfo
1927  p,
1928  q;
1929 
1930  const SegmentInfo
1931  *gradient_vector;
1932 
1933  gradient_vector=(&gradient->gradient_vector);
1934  p.x=gradient_vector->x2-gradient_vector->x1;
1935  p.y=gradient_vector->y2-gradient_vector->y1;
1936  q.x=(double) x-gradient_vector->x1;
1937  q.y=(double) y-gradient_vector->y1;
1938  length=sqrt(q.x*q.x+q.y*q.y);
1939  gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1940  gamma=PerceptibleReciprocal(gamma);
1941  scale=p.x*q.x+p.y*q.y;
1942  offset=gamma*scale*length;
1943  return(offset);
1944  }
1945  case RadialGradient:
1946  {
1947  PointInfo
1948  v;
1949 
1950  if (gradient->spread == RepeatSpread)
1951  {
1952  v.x=(double) x-gradient->center.x;
1953  v.y=(double) y-gradient->center.y;
1954  return(sqrt(v.x*v.x+v.y*v.y));
1955  }
1956  v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1957  gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1958  gradient->angle))))*PerceptibleReciprocal(gradient->radii.x);
1959  v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1960  gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1961  gradient->angle))))*PerceptibleReciprocal(gradient->radii.y);
1962  return(sqrt(v.x*v.x+v.y*v.y));
1963  }
1964  }
1965  return(0.0);
1966 }
1967 
1968 static int StopInfoCompare(const void *x,const void *y)
1969 {
1970  StopInfo
1971  *stop_1,
1972  *stop_2;
1973 
1974  stop_1=(StopInfo *) x;
1975  stop_2=(StopInfo *) y;
1976  if (stop_1->offset > stop_2->offset)
1977  return(1);
1978  if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
1979  return(0);
1980  return(-1);
1981 }
1982 
1984  const DrawInfo *draw_info,ExceptionInfo *exception)
1985 {
1986  CacheView
1987  *image_view;
1988 
1989  const GradientInfo
1990  *gradient;
1991 
1992  const SegmentInfo
1993  *gradient_vector;
1994 
1995  double
1996  length;
1997 
1999  status;
2000 
2001  PixelInfo
2002  zero;
2003 
2004  PointInfo
2005  point;
2006 
2008  bounding_box;
2009 
2010  ssize_t
2011  y;
2012 
2013  /*
2014  Draw linear or radial gradient on image.
2015  */
2016  assert(image != (Image *) NULL);
2017  assert(image->signature == MagickCoreSignature);
2018  if (image->debug != MagickFalse)
2019  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2020  assert(draw_info != (const DrawInfo *) NULL);
2021  gradient=(&draw_info->gradient);
2022  qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
2023  StopInfoCompare);
2024  gradient_vector=(&gradient->gradient_vector);
2025  point.x=gradient_vector->x2-gradient_vector->x1;
2026  point.y=gradient_vector->y2-gradient_vector->y1;
2027  length=sqrt(point.x*point.x+point.y*point.y);
2028  bounding_box=gradient->bounding_box;
2029  status=MagickTrue;
2030  GetPixelInfo(image,&zero);
2031  image_view=AcquireAuthenticCacheView(image,exception);
2032 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2033  #pragma omp parallel for schedule(static) shared(status) \
2034  magick_number_threads(image,image,bounding_box.height-bounding_box.y,1)
2035 #endif
2036  for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
2037  {
2038  double
2039  alpha,
2040  offset;
2041 
2042  PixelInfo
2043  composite,
2044  pixel;
2045 
2046  register Quantum
2047  *magick_restrict q;
2048 
2049  register ssize_t
2050  i,
2051  x;
2052 
2053  ssize_t
2054  j;
2055 
2056  if (status == MagickFalse)
2057  continue;
2058  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2059  if (q == (Quantum *) NULL)
2060  {
2061  status=MagickFalse;
2062  continue;
2063  }
2064  pixel=zero;
2065  composite=zero;
2066  offset=GetStopColorOffset(gradient,0,y);
2067  if (gradient->type != RadialGradient)
2068  offset*=PerceptibleReciprocal(length);
2069  for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
2070  {
2071  GetPixelInfoPixel(image,q,&pixel);
2072  switch (gradient->spread)
2073  {
2074  case UndefinedSpread:
2075  case PadSpread:
2076  {
2077  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2078  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2079  {
2080  offset=GetStopColorOffset(gradient,x,y);
2081  if (gradient->type != RadialGradient)
2082  offset*=PerceptibleReciprocal(length);
2083  }
2084  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2085  if (offset < gradient->stops[i].offset)
2086  break;
2087  if ((offset < 0.0) || (i == 0))
2088  composite=gradient->stops[0].color;
2089  else
2090  if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2091  composite=gradient->stops[gradient->number_stops-1].color;
2092  else
2093  {
2094  j=i;
2095  i--;
2096  alpha=(offset-gradient->stops[i].offset)/
2097  (gradient->stops[j].offset-gradient->stops[i].offset);
2098  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2099  &gradient->stops[j].color,alpha,&composite);
2100  }
2101  break;
2102  }
2103  case ReflectSpread:
2104  {
2105  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2106  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2107  {
2108  offset=GetStopColorOffset(gradient,x,y);
2109  if (gradient->type != RadialGradient)
2110  offset*=PerceptibleReciprocal(length);
2111  }
2112  if (offset < 0.0)
2113  offset=(-offset);
2114  if ((ssize_t) fmod(offset,2.0) == 0)
2115  offset=fmod(offset,1.0);
2116  else
2117  offset=1.0-fmod(offset,1.0);
2118  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2119  if (offset < gradient->stops[i].offset)
2120  break;
2121  if (i == 0)
2122  composite=gradient->stops[0].color;
2123  else
2124  if (i == (ssize_t) gradient->number_stops)
2125  composite=gradient->stops[gradient->number_stops-1].color;
2126  else
2127  {
2128  j=i;
2129  i--;
2130  alpha=(offset-gradient->stops[i].offset)/
2131  (gradient->stops[j].offset-gradient->stops[i].offset);
2132  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2133  &gradient->stops[j].color,alpha,&composite);
2134  }
2135  break;
2136  }
2137  case RepeatSpread:
2138  {
2139  double
2140  repeat;
2141 
2143  antialias;
2144 
2145  antialias=MagickFalse;
2146  repeat=0.0;
2147  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2148  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2149  {
2150  offset=GetStopColorOffset(gradient,x,y);
2151  if (gradient->type == LinearGradient)
2152  {
2153  repeat=fmod(offset,length);
2154  if (repeat < 0.0)
2155  repeat=length-fmod(-repeat,length);
2156  else
2157  repeat=fmod(offset,length);
2158  antialias=(repeat < length) && ((repeat+1.0) > length) ?
2160  offset=PerceptibleReciprocal(length)*repeat;
2161  }
2162  else
2163  {
2164  repeat=fmod(offset,gradient->radius);
2165  if (repeat < 0.0)
2166  repeat=gradient->radius-fmod(-repeat,gradient->radius);
2167  else
2168  repeat=fmod(offset,gradient->radius);
2169  antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2170  MagickFalse;
2171  offset=repeat/gradient->radius;
2172  }
2173  }
2174  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2175  if (offset < gradient->stops[i].offset)
2176  break;
2177  if (i == 0)
2178  composite=gradient->stops[0].color;
2179  else
2180  if (i == (ssize_t) gradient->number_stops)
2181  composite=gradient->stops[gradient->number_stops-1].color;
2182  else
2183  {
2184  j=i;
2185  i--;
2186  alpha=(offset-gradient->stops[i].offset)/
2187  (gradient->stops[j].offset-gradient->stops[i].offset);
2188  if (antialias != MagickFalse)
2189  {
2190  if (gradient->type == LinearGradient)
2191  alpha=length-repeat;
2192  else
2193  alpha=gradient->radius-repeat;
2194  i=0;
2195  j=(ssize_t) gradient->number_stops-1L;
2196  }
2197  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2198  &gradient->stops[j].color,alpha,&composite);
2199  }
2200  break;
2201  }
2202  }
2203  CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2204  &pixel);
2205  SetPixelViaPixelInfo(image,&pixel,q);
2206  q+=GetPixelChannels(image);
2207  }
2208  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2209  status=MagickFalse;
2210  }
2211  image_view=DestroyCacheView(image_view);
2212  return(status);
2213 }
2214 
2215 /*
2216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2217 % %
2218 % %
2219 % %
2220 % D r a w I m a g e %
2221 % %
2222 % %
2223 % %
2224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2225 %
2226 % DrawImage() draws a graphic primitive on your image. The primitive
2227 % may be represented as a string or filename. Precede the filename with an
2228 % "at" sign (@) and the contents of the file are drawn on the image. You
2229 % can affect how text is drawn by setting one or more members of the draw
2230 % info structure.
2231 %
2232 % The format of the DrawImage method is:
2233 %
2234 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
2235 % ExceptionInfo *exception)
2236 %
2237 % A description of each parameter follows:
2238 %
2239 % o image: the image.
2240 %
2241 % o draw_info: the draw info.
2242 %
2243 % o exception: return any errors or warnings in this structure.
2244 %
2245 */
2246 
2248  const size_t pad)
2249 {
2250  double
2251  extent;
2252 
2253  size_t
2254  quantum;
2255 
2256  /*
2257  Check if there is enough storage for drawing pimitives.
2258  */
2259  extent=(double) mvg_info->offset+pad+PrimitiveExtentPad+1;
2260  quantum=sizeof(**mvg_info->primitive_info);
2261  if (((extent*quantum) < (double) SSIZE_MAX) &&
2262  ((extent*quantum) < (double) GetMaxMemoryRequest()))
2263  {
2264  if (extent <= (double) *mvg_info->extent)
2265  return(MagickTrue);
2267  *mvg_info->primitive_info,(size_t) extent,quantum);
2268  if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2269  {
2270  register ssize_t
2271  i;
2272 
2273  *mvg_info->extent=(size_t) extent;
2274  for (i=mvg_info->offset+1; i < (ssize_t) extent; i++)
2275  (*mvg_info->primitive_info)[i].primitive=UndefinedPrimitive;
2276  return(MagickTrue);
2277  }
2278  }
2279  /*
2280  Reallocation failed, allocate a primitive to facilitate unwinding.
2281  */
2282  (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
2283  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2284  if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2286  *mvg_info->primitive_info);
2288  PrimitiveExtentPad*quantum);
2289  (void) memset(*mvg_info->primitive_info,0,PrimitiveExtentPad*quantum);
2290  *mvg_info->extent=1;
2291  return(MagickFalse);
2292 }
2293 
2294 static inline double GetDrawValue(const char *magick_restrict string,
2295  char **magick_restrict sentinal)
2296 {
2297  char
2298  **magick_restrict q;
2299 
2300  double
2301  value;
2302 
2303  q=sentinal;
2304  value=InterpretLocaleValue(string,q);
2305  if ((IsNaN(value) != 0) || (value < -((double) SSIZE_MAX-512.0)) ||
2306  (value > ((double) SSIZE_MAX-512.0)))
2307  return(0.0);
2308  sentinal=q;
2309  return(value);
2310 }
2311 
2312 static int MVGMacroCompare(const void *target,const void *source)
2313 {
2314  const char
2315  *p,
2316  *q;
2317 
2318  p=(const char *) target;
2319  q=(const char *) source;
2320  return(strcmp(p,q));
2321 }
2322 
2323 static SplayTreeInfo *GetMVGMacros(const char *primitive)
2324 {
2325  char
2326  *macro,
2327  *token;
2328 
2329  const char
2330  *q;
2331 
2332  size_t
2333  extent;
2334 
2336  *macros;
2337 
2338  /*
2339  Scan graphic primitives for definitions and classes.
2340  */
2341  if (primitive == (const char *) NULL)
2342  return((SplayTreeInfo *) NULL);
2345  macro=AcquireString(primitive);
2346  token=AcquireString(primitive);
2347  extent=strlen(token)+MagickPathExtent;
2348  for (q=primitive; *q != '\0'; )
2349  {
2350  if (GetNextToken(q,&q,extent,token) < 1)
2351  break;
2352  if (*token == '\0')
2353  break;
2354  if (LocaleCompare("push",token) == 0)
2355  {
2356  register const char
2357  *end,
2358  *start;
2359 
2360  (void) GetNextToken(q,&q,extent,token);
2361  if (*q == '"')
2362  {
2363  char
2364  name[MagickPathExtent];
2365 
2366  const char
2367  *p;
2368 
2369  ssize_t
2370  n;
2371 
2372  /*
2373  Named macro (e.g. push graphic-context "wheel").
2374  */
2375  (void) GetNextToken(q,&q,extent,token);
2376  start=q;
2377  end=q;
2378  (void) CopyMagickString(name,token,MagickPathExtent);
2379  n=1;
2380  for (p=q; *p != '\0'; )
2381  {
2382  if (GetNextToken(p,&p,extent,token) < 1)
2383  break;
2384  if (*token == '\0')
2385  break;
2386  if (LocaleCompare(token,"pop") == 0)
2387  {
2388  end=p-strlen(token)-1;
2389  n--;
2390  }
2391  if (LocaleCompare(token,"push") == 0)
2392  n++;
2393  if ((n == 0) && (end > start))
2394  {
2395  /*
2396  Extract macro.
2397  */
2398  (void) GetNextToken(p,&p,extent,token);
2399  (void) CopyMagickString(macro,start,(size_t) (end-start));
2400  (void) AddValueToSplayTree(macros,ConstantString(name),
2401  ConstantString(macro));
2402  break;
2403  }
2404  }
2405  }
2406  }
2407  }
2408  token=DestroyString(token);
2409  macro=DestroyString(macro);
2410  return(macros);
2411 }
2412 
2413 static inline MagickBooleanType IsPoint(const char *point)
2414 {
2415  char
2416  *p;
2417 
2418  double
2419  value;
2420 
2421  value=GetDrawValue(point,&p);
2422  return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2423  MagickTrue);
2424 }
2425 
2426 static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2427  const PointInfo point)
2428 {
2429  primitive_info->coordinates=1;
2430  primitive_info->closed_subpath=MagickFalse;
2431  primitive_info->point=point;
2432  return(MagickTrue);
2433 }
2434 
2436  const DrawInfo *draw_info,const size_t depth,ExceptionInfo *exception)
2437 {
2438 #define RenderImageTag "Render/Image"
2439 
2440  AffineMatrix
2441  affine,
2442  current;
2443 
2444  char
2445  keyword[MagickPathExtent],
2446  geometry[MagickPathExtent],
2447  *next_token,
2448  pattern[MagickPathExtent],
2449  *primitive,
2450  *token;
2451 
2452  const char
2453  *q;
2454 
2455  double
2456  angle,
2457  coordinates,
2458  cursor,
2459  factor,
2460  primitive_extent;
2461 
2462  DrawInfo
2463  *clone_info,
2464  **graphic_context;
2465 
2467  proceed;
2468 
2470  status;
2471 
2472  MVGInfo
2473  mvg_info;
2474 
2475  PointInfo
2476  point;
2477 
2479  *primitive_info;
2480 
2482  primitive_type;
2483 
2484  register const char
2485  *p;
2486 
2487  register ssize_t
2488  i,
2489  x;
2490 
2491  SegmentInfo
2492  bounds;
2493 
2494  size_t
2495  extent,
2496  number_points,
2497  number_stops;
2498 
2500  *macros;
2501 
2502  ssize_t
2503  defsDepth,
2504  j,
2505  k,
2506  n,
2507  symbolDepth;
2508 
2509  StopInfo
2510  *stops;
2511 
2512  TypeMetric
2513  metrics;
2514 
2515  assert(image != (Image *) NULL);
2516  assert(image->signature == MagickCoreSignature);
2517  if (image->debug != MagickFalse)
2518  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2519  assert(draw_info != (DrawInfo *) NULL);
2520  assert(draw_info->signature == MagickCoreSignature);
2521  if (image->debug != MagickFalse)
2522  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2523  if (depth > MagickMaxRecursionDepth)
2524  ThrowBinaryException(DrawError,"VectorGraphicsNestedTooDeeply",
2525  image->filename);
2526  if ((draw_info->primitive == (char *) NULL) ||
2527  (*draw_info->primitive == '\0'))
2528  return(MagickFalse);
2529  if (image->debug != MagickFalse)
2530  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2531  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2532  return(MagickFalse);
2533  if (image->alpha_trait == UndefinedPixelTrait)
2534  {
2535  status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2536  if (status == MagickFalse)
2537  return(MagickFalse);
2538  }
2539  if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2540  (*(draw_info->primitive+1) != '-') && (depth == 0))
2541  primitive=FileToString(draw_info->primitive+1,~0UL,exception);
2542  else
2543  primitive=AcquireString(draw_info->primitive);
2544  if (primitive == (char *) NULL)
2545  return(MagickFalse);
2546  primitive_extent=(double) strlen(primitive);
2547  (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2548  n=0;
2549  number_stops=0;
2550  stops=(StopInfo *) NULL;
2551  /*
2552  Allocate primitive info memory.
2553  */
2554  graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2555  if (graphic_context == (DrawInfo **) NULL)
2556  {
2557  primitive=DestroyString(primitive);
2558  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2559  image->filename);
2560  }
2561  number_points=PrimitiveExtentPad;
2562  primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
2563  sizeof(*primitive_info));
2564  if (primitive_info == (PrimitiveInfo *) NULL)
2565  {
2566  primitive=DestroyString(primitive);
2567  for ( ; n >= 0; n--)
2568  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2569  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2570  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2571  image->filename);
2572  }
2573  (void) memset(primitive_info,0,(size_t) number_points*
2574  sizeof(*primitive_info));
2575  (void) memset(&mvg_info,0,sizeof(mvg_info));
2576  mvg_info.primitive_info=(&primitive_info);
2577  mvg_info.extent=(&number_points);
2578  mvg_info.exception=exception;
2579  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2580  graphic_context[n]->viewbox=image->page;
2581  if ((image->page.width == 0) || (image->page.height == 0))
2582  {
2583  graphic_context[n]->viewbox.width=image->columns;
2584  graphic_context[n]->viewbox.height=image->rows;
2585  }
2586  token=AcquireString(primitive);
2587  extent=strlen(token)+MagickPathExtent;
2588  defsDepth=0;
2589  symbolDepth=0;
2590  cursor=0.0;
2591  macros=GetMVGMacros(primitive);
2592  status=MagickTrue;
2593  for (q=primitive; *q != '\0'; )
2594  {
2595  /*
2596  Interpret graphic primitive.
2597  */
2598  if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2599  break;
2600  if (*keyword == '\0')
2601  break;
2602  if (*keyword == '#')
2603  {
2604  /*
2605  Comment.
2606  */
2607  while ((*q != '\n') && (*q != '\0'))
2608  q++;
2609  continue;
2610  }
2611  p=q-strlen(keyword)-1;
2612  primitive_type=UndefinedPrimitive;
2613  current=graphic_context[n]->affine;
2614  GetAffineMatrix(&affine);
2615  *token='\0';
2616  switch (*keyword)
2617  {
2618  case ';':
2619  break;
2620  case 'a':
2621  case 'A':
2622  {
2623  if (LocaleCompare("affine",keyword) == 0)
2624  {
2625  (void) GetNextToken(q,&q,extent,token);
2626  affine.sx=GetDrawValue(token,&next_token);
2627  if (token == next_token)
2628  ThrowPointExpectedException(token,exception);
2629  (void) GetNextToken(q,&q,extent,token);
2630  if (*token == ',')
2631  (void) GetNextToken(q,&q,extent,token);
2632  affine.rx=GetDrawValue(token,&next_token);
2633  if (token == next_token)
2634  ThrowPointExpectedException(token,exception);
2635  (void) GetNextToken(q,&q,extent,token);
2636  if (*token == ',')
2637  (void) GetNextToken(q,&q,extent,token);
2638  affine.ry=GetDrawValue(token,&next_token);
2639  if (token == next_token)
2640  ThrowPointExpectedException(token,exception);
2641  (void) GetNextToken(q,&q,extent,token);
2642  if (*token == ',')
2643  (void) GetNextToken(q,&q,extent,token);
2644  affine.sy=GetDrawValue(token,&next_token);
2645  if (token == next_token)
2646  ThrowPointExpectedException(token,exception);
2647  (void) GetNextToken(q,&q,extent,token);
2648  if (*token == ',')
2649  (void) GetNextToken(q,&q,extent,token);
2650  affine.tx=GetDrawValue(token,&next_token);
2651  if (token == next_token)
2652  ThrowPointExpectedException(token,exception);
2653  (void) GetNextToken(q,&q,extent,token);
2654  if (*token == ',')
2655  (void) GetNextToken(q,&q,extent,token);
2656  affine.ty=GetDrawValue(token,&next_token);
2657  if (token == next_token)
2658  ThrowPointExpectedException(token,exception);
2659  break;
2660  }
2661  if (LocaleCompare("alpha",keyword) == 0)
2662  {
2663  primitive_type=AlphaPrimitive;
2664  break;
2665  }
2666  if (LocaleCompare("arc",keyword) == 0)
2667  {
2668  primitive_type=ArcPrimitive;
2669  break;
2670  }
2671  status=MagickFalse;
2672  break;
2673  }
2674  case 'b':
2675  case 'B':
2676  {
2677  if (LocaleCompare("bezier",keyword) == 0)
2678  {
2679  primitive_type=BezierPrimitive;
2680  break;
2681  }
2682  if (LocaleCompare("border-color",keyword) == 0)
2683  {
2684  (void) GetNextToken(q,&q,extent,token);
2685  status&=QueryColorCompliance(token,AllCompliance,
2686  &graphic_context[n]->border_color,exception);
2687  break;
2688  }
2689  status=MagickFalse;
2690  break;
2691  }
2692  case 'c':
2693  case 'C':
2694  {
2695  if (LocaleCompare("class",keyword) == 0)
2696  {
2697  const char
2698  *mvg_class;
2699 
2700  (void) GetNextToken(q,&q,extent,token);
2701  if (*token == '\0')
2702  {
2703  status=MagickFalse;
2704  break;
2705  }
2706  if (LocaleCompare(token,graphic_context[n]->id) == 0)
2707  break;
2708  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2709  if ((mvg_class != (const char *) NULL) && (p > primitive))
2710  {
2711  char
2712  *elements;
2713 
2714  ssize_t
2715  offset;
2716 
2717  /*
2718  Inject class elements in stream.
2719  */
2720  offset=(ssize_t) (p-primitive);
2721  elements=AcquireString(primitive);
2722  elements[offset]='\0';
2723  (void) ConcatenateString(&elements,mvg_class);
2724  (void) ConcatenateString(&elements,"\n");
2725  (void) ConcatenateString(&elements,q);
2726  primitive=DestroyString(primitive);
2727  primitive=elements;
2728  q=primitive+offset;
2729  }
2730  break;
2731  }
2732  if (LocaleCompare("clip-path",keyword) == 0)
2733  {
2734  const char
2735  *clip_path;
2736 
2737  /*
2738  Take a node from within the MVG document, and duplicate it here.
2739  */
2740  (void) GetNextToken(q,&q,extent,token);
2741  if (*token == '\0')
2742  {
2743  status=MagickFalse;
2744  break;
2745  }
2746  (void) CloneString(&graphic_context[n]->clip_mask,token);
2747  clip_path=(const char *) GetValueFromSplayTree(macros,token);
2748  if (clip_path != (const char *) NULL)
2749  {
2750  if (graphic_context[n]->clipping_mask != (Image *) NULL)
2751  graphic_context[n]->clipping_mask=
2752  DestroyImage(graphic_context[n]->clipping_mask);
2753  graphic_context[n]->clipping_mask=DrawClippingMask(image,
2754  graphic_context[n],token,clip_path,exception);
2755  if (graphic_context[n]->compliance != SVGCompliance)
2756  {
2757  clip_path=(const char *) GetValueFromSplayTree(macros,
2758  graphic_context[n]->clip_mask);
2759  if (clip_path != (const char *) NULL)
2760  (void) SetImageArtifact(image,
2761  graphic_context[n]->clip_mask,clip_path);
2762  status&=DrawClipPath(image,graphic_context[n],
2763  graphic_context[n]->clip_mask,exception);
2764  }
2765  }
2766  break;
2767  }
2768  if (LocaleCompare("clip-rule",keyword) == 0)
2769  {
2770  ssize_t
2771  fill_rule;
2772 
2773  (void) GetNextToken(q,&q,extent,token);
2775  token);
2776  if (fill_rule == -1)
2777  {
2778  status=MagickFalse;
2779  break;
2780  }
2781  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2782  break;
2783  }
2784  if (LocaleCompare("clip-units",keyword) == 0)
2785  {
2786  ssize_t
2787  clip_units;
2788 
2789  (void) GetNextToken(q,&q,extent,token);
2791  token);
2792  if (clip_units == -1)
2793  {
2794  status=MagickFalse;
2795  break;
2796  }
2797  graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2798  if (clip_units == ObjectBoundingBox)
2799  {
2800  GetAffineMatrix(&current);
2801  affine.sx=draw_info->bounds.x2;
2802  affine.sy=draw_info->bounds.y2;
2803  affine.tx=draw_info->bounds.x1;
2804  affine.ty=draw_info->bounds.y1;
2805  break;
2806  }
2807  break;
2808  }
2809  if (LocaleCompare("circle",keyword) == 0)
2810  {
2811  primitive_type=CirclePrimitive;
2812  break;
2813  }
2814  if (LocaleCompare("color",keyword) == 0)
2815  {
2816  primitive_type=ColorPrimitive;
2817  break;
2818  }
2819  if (LocaleCompare("compliance",keyword) == 0)
2820  {
2821  /*
2822  MVG compliance associates a clipping mask with an image; SVG
2823  compliance associates a clipping mask with a graphics context.
2824  */
2825  (void) GetNextToken(q,&q,extent,token);
2826  graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2828  break;
2829  }
2830  if (LocaleCompare("currentColor",keyword) == 0)
2831  {
2832  (void) GetNextToken(q,&q,extent,token);
2833  break;
2834  }
2835  status=MagickFalse;
2836  break;
2837  }
2838  case 'd':
2839  case 'D':
2840  {
2841  if (LocaleCompare("decorate",keyword) == 0)
2842  {
2843  ssize_t
2844  decorate;
2845 
2846  (void) GetNextToken(q,&q,extent,token);
2848  token);
2849  if (decorate == -1)
2850  {
2851  status=MagickFalse;
2852  break;
2853  }
2854  graphic_context[n]->decorate=(DecorationType) decorate;
2855  break;
2856  }
2857  if (LocaleCompare("density",keyword) == 0)
2858  {
2859  (void) GetNextToken(q,&q,extent,token);
2860  (void) CloneString(&graphic_context[n]->density,token);
2861  break;
2862  }
2863  if (LocaleCompare("direction",keyword) == 0)
2864  {
2865  ssize_t
2866  direction;
2867 
2868  (void) GetNextToken(q,&q,extent,token);
2870  token);
2871  if (direction == -1)
2872  status=MagickFalse;
2873  else
2874  graphic_context[n]->direction=(DirectionType) direction;
2875  break;
2876  }
2877  status=MagickFalse;
2878  break;
2879  }
2880  case 'e':
2881  case 'E':
2882  {
2883  if (LocaleCompare("ellipse",keyword) == 0)
2884  {
2885  primitive_type=EllipsePrimitive;
2886  break;
2887  }
2888  if (LocaleCompare("encoding",keyword) == 0)
2889  {
2890  (void) GetNextToken(q,&q,extent,token);
2891  (void) CloneString(&graphic_context[n]->encoding,token);
2892  break;
2893  }
2894  status=MagickFalse;
2895  break;
2896  }
2897  case 'f':
2898  case 'F':
2899  {
2900  if (LocaleCompare("fill",keyword) == 0)
2901  {
2902  (void) GetNextToken(q,&q,extent,token);
2903  if (graphic_context[n]->clip_path != MagickFalse)
2904  break;
2905  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2906  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2907  (void) DrawPatternPath(image,draw_info,token,
2908  &graphic_context[n]->fill_pattern,exception);
2909  else
2910  {
2911  status&=QueryColorCompliance(token,AllCompliance,
2912  &graphic_context[n]->fill,exception);
2913  if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2914  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2915  }
2916  break;
2917  }
2918  if (LocaleCompare("fill-opacity",keyword) == 0)
2919  {
2920  double
2921  opacity;
2922 
2923  (void) GetNextToken(q,&q,extent,token);
2924  if (graphic_context[n]->clip_path != MagickFalse)
2925  break;
2926  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2927  opacity=MagickMin(MagickMax(factor*
2928  GetDrawValue(token,&next_token),0.0),1.0);
2929  if (token == next_token)
2930  ThrowPointExpectedException(token,exception);
2931  if (graphic_context[n]->compliance == SVGCompliance)
2932  graphic_context[n]->fill_alpha*=opacity;
2933  else
2934  graphic_context[n]->fill_alpha=QuantumRange*opacity;
2935  if (graphic_context[n]->fill.alpha != TransparentAlpha)
2936  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2937  else
2938  graphic_context[n]->fill.alpha=(MagickRealType)
2939  ClampToQuantum(QuantumRange*(1.0-opacity));
2940  break;
2941  }
2942  if (LocaleCompare("fill-rule",keyword) == 0)
2943  {
2944  ssize_t
2945  fill_rule;
2946 
2947  (void) GetNextToken(q,&q,extent,token);
2949  token);
2950  if (fill_rule == -1)
2951  {
2952  status=MagickFalse;
2953  break;
2954  }
2955  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2956  break;
2957  }
2958  if (LocaleCompare("font",keyword) == 0)
2959  {
2960  (void) GetNextToken(q,&q,extent,token);
2961  (void) CloneString(&graphic_context[n]->font,token);
2962  if (LocaleCompare("none",token) == 0)
2963  graphic_context[n]->font=(char *) RelinquishMagickMemory(
2964  graphic_context[n]->font);
2965  break;
2966  }
2967  if (LocaleCompare("font-family",keyword) == 0)
2968  {
2969  (void) GetNextToken(q,&q,extent,token);
2970  (void) CloneString(&graphic_context[n]->family,token);
2971  break;
2972  }
2973  if (LocaleCompare("font-size",keyword) == 0)
2974  {
2975  (void) GetNextToken(q,&q,extent,token);
2976  graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
2977  if (token == next_token)
2978  ThrowPointExpectedException(token,exception);
2979  break;
2980  }
2981  if (LocaleCompare("font-stretch",keyword) == 0)
2982  {
2983  ssize_t
2984  stretch;
2985 
2986  (void) GetNextToken(q,&q,extent,token);
2988  if (stretch == -1)
2989  {
2990  status=MagickFalse;
2991  break;
2992  }
2993  graphic_context[n]->stretch=(StretchType) stretch;
2994  break;
2995  }
2996  if (LocaleCompare("font-style",keyword) == 0)
2997  {
2998  ssize_t
2999  style;
3000 
3001  (void) GetNextToken(q,&q,extent,token);
3003  if (style == -1)
3004  {
3005  status=MagickFalse;
3006  break;
3007  }
3008  graphic_context[n]->style=(StyleType) style;
3009  break;
3010  }
3011  if (LocaleCompare("font-weight",keyword) == 0)
3012  {
3013  ssize_t
3014  weight;
3015 
3016  (void) GetNextToken(q,&q,extent,token);
3018  if (weight == -1)
3019  weight=(ssize_t) StringToUnsignedLong(token);
3020  graphic_context[n]->weight=(size_t) weight;
3021  break;
3022  }
3023  status=MagickFalse;
3024  break;
3025  }
3026  case 'g':
3027  case 'G':
3028  {
3029  if (LocaleCompare("gradient-units",keyword) == 0)
3030  {
3031  (void) GetNextToken(q,&q,extent,token);
3032  break;
3033  }
3034  if (LocaleCompare("gravity",keyword) == 0)
3035  {
3036  ssize_t
3037  gravity;
3038 
3039  (void) GetNextToken(q,&q,extent,token);
3041  if (gravity == -1)
3042  {
3043  status=MagickFalse;
3044  break;
3045  }
3046  graphic_context[n]->gravity=(GravityType) gravity;
3047  break;
3048  }
3049  status=MagickFalse;
3050  break;
3051  }
3052  case 'i':
3053  case 'I':
3054  {
3055  if (LocaleCompare("image",keyword) == 0)
3056  {
3057  ssize_t
3058  compose;
3059 
3060  primitive_type=ImagePrimitive;
3061  (void) GetNextToken(q,&q,extent,token);
3063  if (compose == -1)
3064  {
3065  status=MagickFalse;
3066  break;
3067  }
3068  graphic_context[n]->compose=(CompositeOperator) compose;
3069  break;
3070  }
3071  if (LocaleCompare("interline-spacing",keyword) == 0)
3072  {
3073  (void) GetNextToken(q,&q,extent,token);
3074  graphic_context[n]->interline_spacing=GetDrawValue(token,
3075  &next_token);
3076  if (token == next_token)
3077  ThrowPointExpectedException(token,exception);
3078  break;
3079  }
3080  if (LocaleCompare("interword-spacing",keyword) == 0)
3081  {
3082  (void) GetNextToken(q,&q,extent,token);
3083  graphic_context[n]->interword_spacing=GetDrawValue(token,
3084  &next_token);
3085  if (token == next_token)
3086  ThrowPointExpectedException(token,exception);
3087  break;
3088  }
3089  status=MagickFalse;
3090  break;
3091  }
3092  case 'k':
3093  case 'K':
3094  {
3095  if (LocaleCompare("kerning",keyword) == 0)
3096  {
3097  (void) GetNextToken(q,&q,extent,token);
3098  graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3099  if (token == next_token)
3100  ThrowPointExpectedException(token,exception);
3101  break;
3102  }
3103  status=MagickFalse;
3104  break;
3105  }
3106  case 'l':
3107  case 'L':
3108  {
3109  if (LocaleCompare("letter-spacing",keyword) == 0)
3110  {
3111  (void) GetNextToken(q,&q,extent,token);
3112  if (IsPoint(token) == MagickFalse)
3113  break;
3114  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3115  clone_info->text=AcquireString(" ");
3116  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
3117  graphic_context[n]->kerning=metrics.width*
3118  GetDrawValue(token,&next_token);
3119  clone_info=DestroyDrawInfo(clone_info);
3120  if (token == next_token)
3121  ThrowPointExpectedException(token,exception);
3122  break;
3123  }
3124  if (LocaleCompare("line",keyword) == 0)
3125  {
3126  primitive_type=LinePrimitive;
3127  break;
3128  }
3129  status=MagickFalse;
3130  break;
3131  }
3132  case 'm':
3133  case 'M':
3134  {
3135  if (LocaleCompare("mask",keyword) == 0)
3136  {
3137  const char
3138  *mask_path;
3139 
3140  /*
3141  Take a node from within the MVG document, and duplicate it here.
3142  */
3143  (void) GetNextToken(q,&q,extent,token);
3144  mask_path=(const char *) GetValueFromSplayTree(macros,token);
3145  if (mask_path != (const char *) NULL)
3146  {
3147  if (graphic_context[n]->composite_mask != (Image *) NULL)
3148  graphic_context[n]->composite_mask=
3149  DestroyImage(graphic_context[n]->composite_mask);
3150  graphic_context[n]->composite_mask=DrawCompositeMask(image,
3151  graphic_context[n],token,mask_path,exception);
3152  if (graphic_context[n]->compliance != SVGCompliance)
3153  status=SetImageMask(image,CompositePixelMask,
3154  graphic_context[n]->composite_mask,exception);
3155  }
3156  break;
3157  }
3158  status=MagickFalse;
3159  break;
3160  }
3161  case 'o':
3162  case 'O':
3163  {
3164  if (LocaleCompare("offset",keyword) == 0)
3165  {
3166  (void) GetNextToken(q,&q,extent,token);
3167  break;
3168  }
3169  if (LocaleCompare("opacity",keyword) == 0)
3170  {
3171  double
3172  opacity;
3173 
3174  (void) GetNextToken(q,&q,extent,token);
3175  if (graphic_context[n]->clip_path != MagickFalse)
3176  break;
3177  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3178  opacity=MagickMin(MagickMax(factor*
3179  GetDrawValue(token,&next_token),0.0),1.0);
3180  if (token == next_token)
3181  ThrowPointExpectedException(token,exception);
3182  if (graphic_context[n]->compliance == SVGCompliance)
3183  {
3184  graphic_context[n]->fill_alpha*=opacity;
3185  graphic_context[n]->stroke_alpha*=opacity;
3186  }
3187  else
3188  {
3189  graphic_context[n]->fill_alpha=QuantumRange*opacity;
3190  graphic_context[n]->stroke_alpha=QuantumRange*opacity;
3191  }
3192  break;
3193  }
3194  status=MagickFalse;
3195  break;
3196  }
3197  case 'p':
3198  case 'P':
3199  {
3200  if (LocaleCompare("path",keyword) == 0)
3201  {
3202  primitive_type=PathPrimitive;
3203  break;
3204  }
3205  if (LocaleCompare("point",keyword) == 0)
3206  {
3207  primitive_type=PointPrimitive;
3208  break;
3209  }
3210  if (LocaleCompare("polyline",keyword) == 0)
3211  {
3212  primitive_type=PolylinePrimitive;
3213  break;
3214  }
3215  if (LocaleCompare("polygon",keyword) == 0)
3216  {
3217  primitive_type=PolygonPrimitive;
3218  break;
3219  }
3220  if (LocaleCompare("pop",keyword) == 0)
3221  {
3222  if (GetNextToken(q,&q,extent,token) < 1)
3223  break;
3224  if (LocaleCompare("class",token) == 0)
3225  break;
3226  if (LocaleCompare("clip-path",token) == 0)
3227  break;
3228  if (LocaleCompare("defs",token) == 0)
3229  {
3230  defsDepth--;
3231  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3232  MagickTrue;
3233  break;
3234  }
3235  if (LocaleCompare("gradient",token) == 0)
3236  break;
3237  if (LocaleCompare("graphic-context",token) == 0)
3238  {
3239  if (n <= 0)
3240  {
3241  (void) ThrowMagickException(exception,GetMagickModule(),
3242  DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3243  status=MagickFalse;
3244  n=0;
3245  break;
3246  }
3247  if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3248  (graphic_context[n]->compliance != SVGCompliance))
3249  if (LocaleCompare(graphic_context[n]->clip_mask,
3250  graphic_context[n-1]->clip_mask) != 0)
3251  status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3252  exception);
3253  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3254  n--;
3255  break;
3256  }
3257  if (LocaleCompare("mask",token) == 0)
3258  break;
3259  if (LocaleCompare("pattern",token) == 0)
3260  break;
3261  if (LocaleCompare("symbol",token) == 0)
3262  {
3263  symbolDepth--;
3264  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3265  MagickTrue;
3266  break;
3267  }
3268  status=MagickFalse;
3269  break;
3270  }
3271  if (LocaleCompare("push",keyword) == 0)
3272  {
3273  if (GetNextToken(q,&q,extent,token) < 1)
3274  break;
3275  if (LocaleCompare("class",token) == 0)
3276  {
3277  /*
3278  Class context.
3279  */
3280  for (p=q; *q != '\0'; )
3281  {
3282  if (GetNextToken(q,&q,extent,token) < 1)
3283  break;
3284  if (LocaleCompare(token,"pop") != 0)
3285  continue;
3286  (void) GetNextToken(q,(const char **) NULL,extent,token);
3287  if (LocaleCompare(token,"class") != 0)
3288  continue;
3289  break;
3290  }
3291  (void) GetNextToken(q,&q,extent,token);
3292  break;
3293  }
3294  if (LocaleCompare("clip-path",token) == 0)
3295  {
3296  (void) GetNextToken(q,&q,extent,token);
3297  for (p=q; *q != '\0'; )
3298  {
3299  if (GetNextToken(q,&q,extent,token) < 1)
3300  break;
3301  if (LocaleCompare(token,"pop") != 0)
3302  continue;
3303  (void) GetNextToken(q,(const char **) NULL,extent,token);
3304  if (LocaleCompare(token,"clip-path") != 0)
3305  continue;
3306  break;
3307  }
3308  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3309  {
3310  status=MagickFalse;
3311  break;
3312  }
3313  (void) GetNextToken(q,&q,extent,token);
3314  break;
3315  }
3316  if (LocaleCompare("defs",token) == 0)
3317  {
3318  defsDepth++;
3319  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3320  MagickTrue;
3321  break;
3322  }
3323  if (LocaleCompare("gradient",token) == 0)
3324  {
3325  char
3326  key[2*MagickPathExtent],
3327  name[MagickPathExtent],
3328  type[MagickPathExtent];
3329 
3330  SegmentInfo
3331  segment;
3332 
3333  (void) GetNextToken(q,&q,extent,token);
3334  (void) CopyMagickString(name,token,MagickPathExtent);
3335  (void) GetNextToken(q,&q,extent,token);
3336  (void) CopyMagickString(type,token,MagickPathExtent);
3337  (void) GetNextToken(q,&q,extent,token);
3338  segment.x1=GetDrawValue(token,&next_token);
3339  if (token == next_token)
3340  ThrowPointExpectedException(token,exception);
3341  (void) GetNextToken(q,&q,extent,token);
3342  if (*token == ',')
3343  (void) GetNextToken(q,&q,extent,token);
3344  segment.y1=GetDrawValue(token,&next_token);
3345  if (token == next_token)
3346  ThrowPointExpectedException(token,exception);
3347  (void) GetNextToken(q,&q,extent,token);
3348  if (*token == ',')
3349  (void) GetNextToken(q,&q,extent,token);
3350  segment.x2=GetDrawValue(token,&next_token);
3351  if (token == next_token)
3352  ThrowPointExpectedException(token,exception);
3353  (void) GetNextToken(q,&q,extent,token);
3354  if (*token == ',')
3355  (void) GetNextToken(q,&q,extent,token);
3356  segment.y2=GetDrawValue(token,&next_token);
3357  if (token == next_token)
3358  ThrowPointExpectedException(token,exception);
3359  if (LocaleCompare(type,"radial") == 0)
3360  {
3361  (void) GetNextToken(q,&q,extent,token);
3362  if (*token == ',')
3363  (void) GetNextToken(q,&q,extent,token);
3364  }
3365  for (p=q; *q != '\0'; )
3366  {
3367  if (GetNextToken(q,&q,extent,token) < 1)
3368  break;
3369  if (LocaleCompare(token,"pop") != 0)
3370  continue;
3371  (void) GetNextToken(q,(const char **) NULL,extent,token);
3372  if (LocaleCompare(token,"gradient") != 0)
3373  continue;
3374  break;
3375  }
3376  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3377  {
3378  status=MagickFalse;
3379  break;
3380  }
3381  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3382  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3383  graphic_context[n]->affine.ry*segment.y1+
3384  graphic_context[n]->affine.tx;
3385  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3386  graphic_context[n]->affine.sy*segment.y1+
3387  graphic_context[n]->affine.ty;
3388  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3389  graphic_context[n]->affine.ry*segment.y2+
3390  graphic_context[n]->affine.tx;
3391  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3392  graphic_context[n]->affine.sy*segment.y2+
3393  graphic_context[n]->affine.ty;
3394  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3395  (void) SetImageArtifact(image,key,token);
3396  (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3397  (void) SetImageArtifact(image,key,type);
3398  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3399  name);
3400  (void) FormatLocaleString(geometry,MagickPathExtent,
3401  "%gx%g%+.15g%+.15g",
3402  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3403  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3404  bounds.x1,bounds.y1);
3405  (void) SetImageArtifact(image,key,geometry);
3406  (void) GetNextToken(q,&q,extent,token);
3407  break;
3408  }
3409  if (LocaleCompare("graphic-context",token) == 0)
3410  {
3411  n++;
3412  graphic_context=(DrawInfo **) ResizeQuantumMemory(
3413  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3414  if (graphic_context == (DrawInfo **) NULL)
3415  {
3416  (void) ThrowMagickException(exception,GetMagickModule(),
3417  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3418  image->filename);
3419  break;
3420  }
3421  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3422  graphic_context[n-1]);
3423  if (*q == '"')
3424  {
3425  (void) GetNextToken(q,&q,extent,token);
3426  (void) CloneString(&graphic_context[n]->id,token);
3427  }
3428  break;
3429  }
3430  if (LocaleCompare("mask",token) == 0)
3431  {
3432  (void) GetNextToken(q,&q,extent,token);
3433  break;
3434  }
3435  if (LocaleCompare("pattern",token) == 0)
3436  {
3437  char
3438  key[2*MagickPathExtent],
3439  name[MagickPathExtent];
3440 
3442  bounds;
3443 
3444  (void) GetNextToken(q,&q,extent,token);
3445  (void) CopyMagickString(name,token,MagickPathExtent);
3446  (void) GetNextToken(q,&q,extent,token);
3447  bounds.x=(ssize_t) ceil(GetDrawValue(token,&next_token)-0.5);
3448  if (token == next_token)
3449  ThrowPointExpectedException(token,exception);
3450  (void) GetNextToken(q,&q,extent,token);
3451  if (*token == ',')
3452  (void) GetNextToken(q,&q,extent,token);
3453  bounds.y=(ssize_t) ceil(GetDrawValue(token,&next_token)-0.5);
3454  if (token == next_token)
3455  ThrowPointExpectedException(token,exception);
3456  (void) GetNextToken(q,&q,extent,token);
3457  if (*token == ',')
3458  (void) GetNextToken(q,&q,extent,token);
3459  bounds.width=(size_t) floor(GetDrawValue(token,&next_token)+
3460  0.5);
3461  if (token == next_token)
3462  ThrowPointExpectedException(token,exception);
3463  (void) GetNextToken(q,&q,extent,token);
3464  if (*token == ',')
3465  (void) GetNextToken(q,&q,extent,token);
3466  bounds.height=(size_t) floor(GetDrawValue(token,&next_token)+
3467  0.5);
3468  if (token == next_token)
3469  ThrowPointExpectedException(token,exception);
3470  for (p=q; *q != '\0'; )
3471  {
3472  if (GetNextToken(q,&q,extent,token) < 1)
3473  break;
3474  if (LocaleCompare(token,"pop") != 0)
3475  continue;
3476  (void) GetNextToken(q,(const char **) NULL,extent,token);
3477  if (LocaleCompare(token,"pattern") != 0)
3478  continue;
3479  break;
3480  }
3481  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3482  {
3483  status=MagickFalse;
3484  break;
3485  }
3486  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3487  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3488  (void) SetImageArtifact(image,key,token);
3489  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3490  name);
3491  (void) FormatLocaleString(geometry,MagickPathExtent,
3492  "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3493  bounds.height,(double) bounds.x,(double) bounds.y);
3494  (void) SetImageArtifact(image,key,geometry);
3495  (void) GetNextToken(q,&q,extent,token);
3496  break;
3497  }
3498  if (LocaleCompare("symbol",token) == 0)
3499  {
3500  symbolDepth++;
3501  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3502  MagickTrue;
3503  break;
3504  }
3505  status=MagickFalse;
3506  break;
3507  }
3508  status=MagickFalse;
3509  break;
3510  }
3511  case 'r':
3512  case 'R':
3513  {
3514  if (LocaleCompare("rectangle",keyword) == 0)
3515  {
3516  primitive_type=RectanglePrimitive;
3517  break;
3518  }
3519  if (LocaleCompare("rotate",keyword) == 0)
3520  {
3521  (void) GetNextToken(q,&q,extent,token);
3522  angle=GetDrawValue(token,&next_token);
3523  if (token == next_token)
3524  ThrowPointExpectedException(token,exception);
3525  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3526  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3527  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3528  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3529  break;
3530  }
3531  if (LocaleCompare("roundRectangle",keyword) == 0)
3532  {
3533  primitive_type=RoundRectanglePrimitive;
3534  break;
3535  }
3536  status=MagickFalse;
3537  break;
3538  }
3539  case 's':
3540  case 'S':
3541  {
3542  if (LocaleCompare("scale",keyword) == 0)
3543  {
3544  (void) GetNextToken(q,&q,extent,token);
3545  affine.sx=GetDrawValue(token,&next_token);
3546  if (token == next_token)
3547  ThrowPointExpectedException(token,exception);
3548  (void) GetNextToken(q,&q,extent,token);
3549  if (*token == ',')
3550  (void) GetNextToken(q,&q,extent,token);
3551  affine.sy=GetDrawValue(token,&next_token);
3552  if (token == next_token)
3553  ThrowPointExpectedException(token,exception);
3554  break;
3555  }
3556  if (LocaleCompare("skewX",keyword) == 0)
3557  {
3558  (void) GetNextToken(q,&q,extent,token);
3559  angle=GetDrawValue(token,&next_token);
3560  if (token == next_token)
3561  ThrowPointExpectedException(token,exception);
3562  affine.ry=sin(DegreesToRadians(angle));
3563  break;
3564  }
3565  if (LocaleCompare("skewY",keyword) == 0)
3566  {
3567  (void) GetNextToken(q,&q,extent,token);
3568  angle=GetDrawValue(token,&next_token);
3569  if (token == next_token)
3570  ThrowPointExpectedException(token,exception);
3571  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3572  break;
3573  }
3574  if (LocaleCompare("stop-color",keyword) == 0)
3575  {
3576  PixelInfo
3577  stop_color;
3578 
3579  number_stops++;
3580  if (number_stops == 1)
3581  stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3582  else
3583  if (number_stops > 2)
3584  stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3585  sizeof(*stops));
3586  if (stops == (StopInfo *) NULL)
3587  {
3588  (void) ThrowMagickException(exception,GetMagickModule(),
3589  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3590  image->filename);
3591  break;
3592  }
3593  (void) GetNextToken(q,&q,extent,token);
3594  status&=QueryColorCompliance(token,AllCompliance,&stop_color,
3595  exception);
3596  stops[number_stops-1].color=stop_color;
3597  (void) GetNextToken(q,&q,extent,token);
3598  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3599  stops[number_stops-1].offset=factor*GetDrawValue(token,
3600  &next_token);
3601  if (token == next_token)
3602  ThrowPointExpectedException(token,exception);
3603  break;
3604  }
3605  if (LocaleCompare("stroke",keyword) == 0)
3606  {
3607  (void) GetNextToken(q,&q,extent,token);
3608  if (graphic_context[n]->clip_path != MagickFalse)
3609  break;
3610  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3611  if (GetImageArtifact(image,pattern) != (const char *) NULL)
3612  (void) DrawPatternPath(image,draw_info,token,
3613  &graphic_context[n]->stroke_pattern,exception);
3614  else
3615  {
3616  status&=QueryColorCompliance(token,AllCompliance,
3617  &graphic_context[n]->stroke,exception);
3618  if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3619  graphic_context[n]->stroke.alpha=
3620  graphic_context[n]->stroke_alpha;
3621  }
3622  break;
3623  }
3624  if (LocaleCompare("stroke-antialias",keyword) == 0)
3625  {
3626  (void) GetNextToken(q,&q,extent,token);
3627  graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3629  break;
3630  }
3631  if (LocaleCompare("stroke-dasharray",keyword) == 0)
3632  {
3633  if (graphic_context[n]->dash_pattern != (double *) NULL)
3634  graphic_context[n]->dash_pattern=(double *)
3635  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3636  if (IsPoint(q) != MagickFalse)
3637  {
3638  const char
3639  *r;
3640 
3641  r=q;
3642  (void) GetNextToken(r,&r,extent,token);
3643  if (*token == ',')
3644  (void) GetNextToken(r,&r,extent,token);
3645  for (x=0; IsPoint(token) != MagickFalse; x++)
3646  {
3647  (void) GetNextToken(r,&r,extent,token);
3648  if (*token == ',')
3649  (void) GetNextToken(r,&r,extent,token);
3650  }
3651  graphic_context[n]->dash_pattern=(double *)
3652  AcquireQuantumMemory((size_t) (2*x+2),
3653  sizeof(*graphic_context[n]->dash_pattern));
3654  if (graphic_context[n]->dash_pattern == (double *) NULL)
3655  {
3656  (void) ThrowMagickException(exception,GetMagickModule(),
3657  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3658  image->filename);
3659  status=MagickFalse;
3660  break;
3661  }
3662  (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3663  (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3664  for (j=0; j < x; j++)
3665  {
3666  (void) GetNextToken(q,&q,extent,token);
3667  if (*token == ',')
3668  (void) GetNextToken(q,&q,extent,token);
3669  graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3670  &next_token);
3671  if (token == next_token)
3672  ThrowPointExpectedException(token,exception);
3673  if (graphic_context[n]->dash_pattern[j] < 0.0)
3674  status=MagickFalse;
3675  }
3676  if ((x & 0x01) != 0)
3677  for ( ; j < (2*x); j++)
3678  graphic_context[n]->dash_pattern[j]=
3679  graphic_context[n]->dash_pattern[j-x];
3680  graphic_context[n]->dash_pattern[j]=0.0;
3681  break;
3682  }
3683  (void) GetNextToken(q,&q,extent,token);
3684  break;
3685  }
3686  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3687  {
3688  (void) GetNextToken(q,&q,extent,token);
3689  graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3690  if (token == next_token)
3691  ThrowPointExpectedException(token,exception);
3692  break;
3693  }
3694  if (LocaleCompare("stroke-linecap",keyword) == 0)
3695  {
3696  ssize_t
3697  linecap;
3698 
3699  (void) GetNextToken(q,&q,extent,token);
3701  if (linecap == -1)
3702  {
3703  status=MagickFalse;
3704  break;
3705  }
3706  graphic_context[n]->linecap=(LineCap) linecap;
3707  break;
3708  }
3709  if (LocaleCompare("stroke-linejoin",keyword) == 0)
3710  {
3711  ssize_t
3712  linejoin;
3713 
3714  (void) GetNextToken(q,&q,extent,token);
3716  token);
3717  if (linejoin == -1)
3718  {
3719  status=MagickFalse;
3720  break;
3721  }
3722  graphic_context[n]->linejoin=(LineJoin) linejoin;
3723  break;
3724  }
3725  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3726  {
3727  (void) GetNextToken(q,&q,extent,token);
3728  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3729  break;
3730  }
3731  if (LocaleCompare("stroke-opacity",keyword) == 0)
3732  {
3733  double
3734  opacity;
3735 
3736  (void) GetNextToken(q,&q,extent,token);
3737  if (graphic_context[n]->clip_path != MagickFalse)
3738  break;
3739  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3740  opacity=MagickMin(MagickMax(factor*
3741  GetDrawValue(token,&next_token),0.0),1.0);
3742  if (token == next_token)
3743  ThrowPointExpectedException(token,exception);
3744  if (graphic_context[n]->compliance == SVGCompliance)
3745  graphic_context[n]->stroke_alpha*=opacity;
3746  else
3747  graphic_context[n]->stroke_alpha=QuantumRange*opacity;
3748  if (graphic_context[n]->stroke.alpha != TransparentAlpha)
3749  graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3750  else
3751  graphic_context[n]->stroke.alpha=(MagickRealType)
3752  ClampToQuantum(QuantumRange*(1.0-opacity));
3753  break;
3754  }
3755  if (LocaleCompare("stroke-width",keyword) == 0)
3756  {
3757  (void) GetNextToken(q,&q,extent,token);
3758  if (graphic_context[n]->clip_path != MagickFalse)
3759  break;
3760  graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3761  if (token == next_token)
3762  ThrowPointExpectedException(token,exception);
3763  break;
3764  }
3765  status=MagickFalse;
3766  break;
3767  }
3768  case 't':
3769  case 'T':
3770  {
3771  if (LocaleCompare("text",keyword) == 0)
3772  {
3773  primitive_type=TextPrimitive;
3774  cursor=0.0;
3775  break;
3776  }
3777  if (LocaleCompare("text-align",keyword) == 0)
3778  {
3779  ssize_t
3780  align;
3781 
3782  (void) GetNextToken(q,&q,extent,token);
3784  if (align == -1)
3785  {
3786  status=MagickFalse;
3787  break;
3788  }
3789  graphic_context[n]->align=(AlignType) align;
3790  break;
3791  }
3792  if (LocaleCompare("text-anchor",keyword) == 0)
3793  {
3794  ssize_t
3795  align;
3796 
3797  (void) GetNextToken(q,&q,extent,token);
3799  if (align == -1)
3800  {
3801  status=MagickFalse;
3802  break;
3803  }
3804  graphic_context[n]->align=(AlignType) align;
3805  break;
3806  }
3807  if (LocaleCompare("text-antialias",keyword) == 0)
3808  {
3809  (void) GetNextToken(q,&q,extent,token);
3810  graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3812  break;
3813  }
3814  if (LocaleCompare("text-undercolor",keyword) == 0)
3815  {
3816  (void) GetNextToken(q,&q,extent,token);
3817  status&=QueryColorCompliance(token,AllCompliance,
3818  &graphic_context[n]->undercolor,exception);
3819  break;
3820  }
3821  if (LocaleCompare("translate",keyword) == 0)
3822  {
3823  (void) GetNextToken(q,&q,extent,token);
3824  affine.tx=GetDrawValue(token,&next_token);
3825  if (token == next_token)
3826  ThrowPointExpectedException(token,exception);
3827  (void) GetNextToken(q,&q,extent,token);
3828  if (*token == ',')
3829  (void) GetNextToken(q,&q,extent,token);
3830  affine.ty=GetDrawValue(token,&next_token);
3831  if (token == next_token)
3832  ThrowPointExpectedException(token,exception);
3833  cursor=0.0;
3834  break;
3835  }
3836  status=MagickFalse;
3837  break;
3838  }
3839  case 'u':
3840  case 'U':
3841  {
3842  if (LocaleCompare("use",keyword) == 0)
3843  {
3844  const char
3845  *use;
3846 
3847  /*
3848  Get a macro from the MVG document, and "use" it here.
3849  */
3850  (void) GetNextToken(q,&q,extent,token);
3851  use=(const char *) GetValueFromSplayTree(macros,token);
3852  if (use != (const char *) NULL)
3853  {
3854  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3855  (void) CloneString(&clone_info->primitive,use);
3856  status=RenderMVGContent(image,clone_info,depth+1,exception);
3857  clone_info=DestroyDrawInfo(clone_info);
3858  }
3859  break;
3860  }
3861  status=MagickFalse;
3862  break;
3863  }
3864  case 'v':
3865  case 'V':
3866  {
3867  if (LocaleCompare("viewbox",keyword) == 0)
3868  {
3869  (void) GetNextToken(q,&q,extent,token);
3870  graphic_context[n]->viewbox.x=(ssize_t) ceil(GetDrawValue(token,
3871  &next_token)-0.5);
3872  if (token == next_token)
3873  ThrowPointExpectedException(token,exception);
3874  (void) GetNextToken(q,&q,extent,token);
3875  if (*token == ',')
3876  (void) GetNextToken(q,&q,extent,token);
3877  graphic_context[n]->viewbox.y=(ssize_t) ceil(GetDrawValue(token,
3878  &next_token)-0.5);
3879  if (token == next_token)
3880  ThrowPointExpectedException(token,exception);
3881  (void) GetNextToken(q,&q,extent,token);
3882  if (*token == ',')
3883  (void) GetNextToken(q,&q,extent,token);
3884  graphic_context[n]->viewbox.width=(size_t) floor(GetDrawValue(
3885  token,&next_token)+0.5);
3886  if (token == next_token)
3887  ThrowPointExpectedException(token,exception);
3888  (void) GetNextToken(q,&q,extent,token);
3889  if (*token == ',')
3890  (void) GetNextToken(q,&q,extent,token);
3891  graphic_context[n]->viewbox.height=(size_t) floor(GetDrawValue(
3892  token,&next_token)+0.5);
3893  if (token == next_token)
3894  ThrowPointExpectedException(token,exception);
3895  break;
3896  }
3897  status=MagickFalse;
3898  break;
3899  }
3900  case 'w':
3901  case 'W':
3902  {
3903  if (LocaleCompare("word-spacing",keyword) == 0)
3904  {
3905  (void) GetNextToken(q,&q,extent,token);
3906  graphic_context[n]->interword_spacing=GetDrawValue(token,
3907  &next_token);
3908  if (token == next_token)
3909  ThrowPointExpectedException(token,exception);
3910  break;
3911  }
3912  status=MagickFalse;
3913  break;
3914  }
3915  default:
3916  {
3917  status=MagickFalse;
3918  break;
3919  }
3920  }
3921  if (status == MagickFalse)
3922  break;
3923  if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3924  (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3925  (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3926  (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3927  {
3928  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3929  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3930  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3931  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3932  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3933  current.tx;
3934  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3935  current.ty;
3936  }
3937  if (primitive_type == UndefinedPrimitive)
3938  {
3939  if (*q == '\0')
3940  {
3941  if (number_stops > 1)
3942  {
3943  GradientType
3944  type;
3945 
3946  type=LinearGradient;
3947  if (draw_info->gradient.type == RadialGradient)
3948  type=RadialGradient;
3949  (void) GradientImage(image,type,PadSpread,stops,number_stops,
3950  exception);
3951  }
3952  if (number_stops > 0)
3953  stops=(StopInfo *) RelinquishMagickMemory(stops);
3954  }
3955  if ((image->debug != MagickFalse) && (q > p))
3956  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3957  (q-p-1),p);
3958  continue;
3959  }
3960  /*
3961  Parse the primitive attributes.
3962  */
3963  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3964  if ((primitive_info[i].primitive == TextPrimitive) ||
3965  (primitive_info[i].primitive == ImagePrimitive))
3966  if (primitive_info[i].text != (char *) NULL)
3967  primitive_info[i].text=DestroyString(primitive_info[i].text);
3968  i=0;
3969  mvg_info.offset=i;
3970  j=0;
3971  primitive_info[0].point.x=0.0;
3972  primitive_info[0].point.y=0.0;
3973  primitive_info[0].coordinates=0;
3974  primitive_info[0].method=FloodfillMethod;
3975  primitive_info[0].closed_subpath=MagickFalse;
3976  for (x=0; *q != '\0'; x++)
3977  {
3978  /*
3979  Define points.
3980  */
3981  if (IsPoint(q) == MagickFalse)
3982  break;
3983  (void) GetNextToken(q,&q,extent,token);
3984  point.x=GetDrawValue(token,&next_token);
3985  if (token == next_token)
3986  ThrowPointExpectedException(token,exception);
3987  (void) GetNextToken(q,&q,extent,token);
3988  if (*token == ',')
3989  (void) GetNextToken(q,&q,extent,token);
3990  point.y=GetDrawValue(token,&next_token);
3991  if (token == next_token)
3992  ThrowPointExpectedException(token,exception);
3993  (void) GetNextToken(q,(const char **) NULL,extent,token);
3994  if (*token == ',')
3995  (void) GetNextToken(q,&q,extent,token);
3996  primitive_info[i].primitive=primitive_type;
3997  primitive_info[i].point=point;
3998  primitive_info[i].coordinates=0;
3999  primitive_info[i].method=FloodfillMethod;
4000  primitive_info[i].closed_subpath=MagickFalse;
4001  i++;
4002  mvg_info.offset=i;
4003  if (i < (ssize_t) number_points)
4004  continue;
4005  status&=CheckPrimitiveExtent(&mvg_info,number_points);
4006  }
4007  if (status == MagickFalse)
4008  break;
4009  if ((primitive_info[j].primitive == TextPrimitive) ||
4010  (primitive_info[j].primitive == ImagePrimitive))
4011  if (primitive_info[j].text != (char *) NULL)
4012  primitive_info[j].text=DestroyString(primitive_info[j].text);
4013  primitive_info[j].primitive=primitive_type;
4014  primitive_info[j].coordinates=(size_t) x;
4015  primitive_info[j].method=FloodfillMethod;
4016  primitive_info[j].closed_subpath=MagickFalse;
4017  /*
4018  Circumscribe primitive within a circle.
4019  */
4020  bounds.x1=primitive_info[j].point.x;
4021  bounds.y1=primitive_info[j].point.y;
4022  bounds.x2=primitive_info[j].point.x;
4023  bounds.y2=primitive_info[j].point.y;
4024  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4025  {
4026  point=primitive_info[j+k].point;
4027  if (point.x < bounds.x1)
4028  bounds.x1=point.x;
4029  if (point.y < bounds.y1)
4030  bounds.y1=point.y;
4031  if (point.x > bounds.x2)
4032  bounds.x2=point.x;
4033  if (point.y > bounds.y2)
4034  bounds.y2=point.y;
4035  }
4036  /*
4037  Speculate how many points our primitive might consume.
4038  */
4039  coordinates=(double) primitive_info[j].coordinates;
4040  switch (primitive_type)
4041  {
4042  case RectanglePrimitive:
4043  {
4044  coordinates*=5.0;
4045  break;
4046  }
4048  {
4049  double
4050  alpha,
4051  beta,
4052  radius;
4053 
4054  alpha=bounds.x2-bounds.x1;
4055  beta=bounds.y2-bounds.y1;
4056  radius=hypot(alpha,beta);
4057  coordinates*=5.0;
4058  coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4059  BezierQuantum+360.0;
4060  break;
4061  }
4062  case BezierPrimitive:
4063  {
4064  coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4065  if (primitive_info[j].coordinates > (108.0*BezierQuantum))
4066  {
4067  (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
4068  "TooManyBezierCoordinates","`%s'",token);
4069  status=MagickFalse;
4070  break;
4071  }
4072  break;
4073  }
4074  case PathPrimitive:
4075  {
4076  char
4077  *s,
4078  *t;
4079 
4080  (void) GetNextToken(q,&q,extent,token);
4081  coordinates=1.0;
4082  t=token;
4083  for (s=token; *s != '\0'; s=t)
4084  {
4085  double
4086  value;
4087 
4088  value=GetDrawValue(s,&t);
4089  (void) value;
4090  if (s == t)
4091  {
4092  t++;
4093  continue;
4094  }
4095  coordinates++;
4096  }
4097  for (s=token; *s != '\0'; s++)
4098  if (strspn(s,"AaCcQqSsTt") != 0)
4099  coordinates+=(20.0*BezierQuantum)+360.0;
4100  break;
4101  }
4102  default:
4103  break;
4104  }
4105  if (status == MagickFalse)
4106  break;
4107  if (((size_t) (i+coordinates)) >= number_points)
4108  {
4109  /*
4110  Resize based on speculative points required by primitive.
4111  */
4112  number_points+=coordinates+1;
4113  if (number_points < (size_t) coordinates)
4114  {
4115  (void) ThrowMagickException(exception,GetMagickModule(),
4116  ResourceLimitError,"MemoryAllocationFailed","`%s'",
4117  image->filename);
4118  break;
4119  }
4120  mvg_info.offset=i;
4121  status&=CheckPrimitiveExtent(&mvg_info,number_points);
4122  }
4123  status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4124  if (status == MagickFalse)
4125  break;
4126  mvg_info.offset=j;
4127  switch (primitive_type)
4128  {
4129  case PointPrimitive:
4130  default:
4131  {
4132  if (primitive_info[j].coordinates != 1)
4133  {
4134  status=MagickFalse;
4135  break;
4136  }
4137  status&=TracePoint(primitive_info+j,primitive_info[j].point);
4138  i=(ssize_t) (j+primitive_info[j].coordinates);
4139  break;
4140  }
4141  case LinePrimitive:
4142  {
4143  double
4144  dx,
4145  dy,
4146  maximum_length;
4147 
4148  if (primitive_info[j].coordinates != 2)
4149  {
4150  status=MagickFalse;
4151  break;
4152  }
4153  dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
4154  dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
4155  maximum_length=hypot(dx,dy);
4156  if (maximum_length > (MaxBezierCoordinates/100.0))
4157  ThrowPointExpectedException(keyword,exception);
4158  status&=TraceLine(primitive_info+j,primitive_info[j].point,
4159  primitive_info[j+1].point);
4160  i=(ssize_t) (j+primitive_info[j].coordinates);
4161  break;
4162  }
4163  case RectanglePrimitive:
4164  {
4165  if (primitive_info[j].coordinates != 2)
4166  {
4167  status=MagickFalse;
4168  break;
4169  }
4170  status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4171  primitive_info[j+1].point);
4172  i=(ssize_t) (j+primitive_info[j].coordinates);
4173  break;
4174  }
4176  {
4177  if (primitive_info[j].coordinates != 3)
4178  {
4179  status=MagickFalse;
4180  break;
4181  }
4182  if ((primitive_info[j+2].point.x < 0.0) ||
4183  (primitive_info[j+2].point.y < 0.0))
4184  {
4185  status=MagickFalse;
4186  break;
4187  }
4188  if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4189  {
4190  status=MagickFalse;
4191  break;
4192  }
4193  if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4194  {
4195  status=MagickFalse;
4196  break;
4197  }
4198  status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4199  primitive_info[j+1].point,primitive_info[j+2].point);
4200  i=(ssize_t) (j+primitive_info[j].coordinates);
4201  break;
4202  }
4203  case ArcPrimitive:
4204  {
4205  if (primitive_info[j].coordinates != 3)
4206  {
4207  status=MagickFalse;
4208  break;
4209  }
4210  status&=TraceArc(&mvg_info,primitive_info[j].point,
4211  primitive_info[j+1].point,primitive_info[j+2].point);
4212  i=(ssize_t) (j+primitive_info[j].coordinates);
4213  break;
4214  }
4215  case EllipsePrimitive:
4216  {
4217  if (primitive_info[j].coordinates != 3)
4218  {
4219  status=MagickFalse;
4220  break;
4221  }
4222  if ((primitive_info[j+1].point.x < 0.0) ||
4223  (primitive_info[j+1].point.y < 0.0))
4224  {
4225  status=MagickFalse;
4226  break;
4227  }
4228  status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4229  primitive_info[j+1].point,primitive_info[j+2].point);
4230  i=(ssize_t) (j+primitive_info[j].coordinates);
4231  break;
4232  }
4233  case CirclePrimitive:
4234  {
4235  if (primitive_info[j].coordinates != 2)
4236  {
4237  status=MagickFalse;
4238  break;
4239  }
4240  status&=TraceCircle(&mvg_info,primitive_info[j].point,
4241  primitive_info[j+1].point);
4242  i=(ssize_t) (j+primitive_info[j].coordinates);
4243  break;
4244  }
4245  case PolylinePrimitive:
4246  {
4247  if (primitive_info[j].coordinates < 1)
4248  {
4249  status=MagickFalse;
4250  break;
4251  }
4252  break;
4253  }
4254  case PolygonPrimitive:
4255  {
4256  if (primitive_info[j].coordinates < 3)
4257  {
4258  status=MagickFalse;
4259  break;
4260  }
4261  primitive_info[i]=primitive_info[j];
4262  primitive_info[i].coordinates=0;
4263  primitive_info[j].coordinates++;
4264  primitive_info[j].closed_subpath=MagickTrue;
4265  i++;
4266  break;
4267  }
4268  case BezierPrimitive:
4269  {
4270  if (primitive_info[j].coordinates < 3)
4271  {
4272  status=MagickFalse;
4273  break;
4274  }
4275  status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4276  i=(ssize_t) (j+primitive_info[j].coordinates);
4277  break;
4278  }
4279  case PathPrimitive:
4280  {
4281  coordinates=(double) TracePath(&mvg_info,token,exception);
4282  if (coordinates < 0.0)
4283  {
4284  status=MagickFalse;
4285  break;
4286  }
4287  i=(ssize_t) (j+coordinates);
4288  break;
4289  }
4290  case AlphaPrimitive:
4291  case ColorPrimitive:
4292  {
4293  ssize_t
4294  method;
4295 
4296  if (primitive_info[j].coordinates != 1)
4297  {
4298  status=MagickFalse;
4299  break;
4300  }
4301  (void) GetNextToken(q,&q,extent,token);
4303  if (method == -1)
4304  {
4305  status=MagickFalse;
4306  break;
4307  }
4308  primitive_info[j].method=(PaintMethod) method;
4309  break;
4310  }
4311  case TextPrimitive:
4312  {
4313  char
4314  geometry[MagickPathExtent];
4315 
4316  if (primitive_info[j].coordinates != 1)
4317  {
4318  status=MagickFalse;
4319  break;
4320  }
4321  if (*token != ',')
4322  (void) GetNextToken(q,&q,extent,token);
4323  (void) CloneString(&primitive_info[j].text,token);
4324  /*
4325  Compute text cursor offset.
4326  */
4327  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4328  if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4329  (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4330  {
4331  mvg_info.point=primitive_info->point;
4332  primitive_info->point.x+=cursor;
4333  }
4334  else
4335  {
4336  mvg_info.point=primitive_info->point;
4337  cursor=0.0;
4338  }
4339  (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4340  primitive_info->point.x,primitive_info->point.y);
4341  clone_info->render=MagickFalse;
4342  clone_info->text=AcquireString(token);
4343  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
4344  clone_info=DestroyDrawInfo(clone_info);
4345  cursor+=metrics.width;
4346  if (graphic_context[n]->compliance != SVGCompliance)
4347  cursor=0.0;
4348  break;
4349  }
4350  case ImagePrimitive:
4351  {
4352  if (primitive_info[j].coordinates != 2)
4353  {
4354  status=MagickFalse;
4355  break;
4356  }
4357  (void) GetNextToken(q,&q,extent,token);
4358  (void) CloneString(&primitive_info[j].text,token);
4359  break;
4360  }
4361  }
4362  mvg_info.offset=i;
4363  if (status == 0)
4364  break;
4365  primitive_info[i].primitive=UndefinedPrimitive;
4366  if ((image->debug != MagickFalse) && (q > p))
4367  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p-1),
4368  p);
4369  /*
4370  Sanity check.
4371  */
4372  status&=CheckPrimitiveExtent(&mvg_info,(size_t)
4373  ExpandAffine(&graphic_context[n]->affine));
4374  if (status == 0)
4375  break;
4376  status&=CheckPrimitiveExtent(&mvg_info,(size_t)
4377  graphic_context[n]->stroke_width);
4378  if (status == 0)
4379  break;
4380  if (i == 0)
4381  continue;
4382  /*
4383  Transform points.
4384  */
4385  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4386  {
4387  point=primitive_info[i].point;
4388  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4389  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4390  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4391  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4392  point=primitive_info[i].point;
4393  if (point.x < graphic_context[n]->bounds.x1)
4394  graphic_context[n]->bounds.x1=point.x;
4395  if (point.y < graphic_context[n]->bounds.y1)
4396  graphic_context[n]->bounds.y1=point.y;
4397  if (point.x > graphic_context[n]->bounds.x2)
4398  graphic_context[n]->bounds.x2=point.x;
4399  if (point.y > graphic_context[n]->bounds.y2)
4400  graphic_context[n]->bounds.y2=point.y;
4401  if (primitive_info[i].primitive == ImagePrimitive)
4402  break;
4403  if (i >= (ssize_t) number_points)
4404  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4405  }
4406  if (graphic_context[n]->render != MagickFalse)
4407  {
4408  if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4409  (graphic_context[n]->clip_mask != (char *) NULL) &&
4410  (LocaleCompare(graphic_context[n]->clip_mask,
4411  graphic_context[n-1]->clip_mask) != 0))
4412  {
4413  const char
4414  *clip_path;
4415 
4416  clip_path=(const char *) GetValueFromSplayTree(macros,
4417  graphic_context[n]->clip_mask);
4418  if (clip_path != (const char *) NULL)
4419  (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4420  clip_path);
4421  status&=DrawClipPath(image,graphic_context[n],
4422  graphic_context[n]->clip_mask,exception);
4423  }
4424  status&=DrawPrimitive(image,graphic_context[n],primitive_info,
4425  exception);
4426  }
4427  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4428  primitive_extent);
4429  if (proceed == MagickFalse)
4430  break;
4431  if (status == 0)
4432  break;
4433  }
4434  if (image->debug != MagickFalse)
4435  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4436  /*
4437  Relinquish resources.
4438  */
4439  macros=DestroySplayTree(macros);
4440  token=DestroyString(token);
4441  if (primitive_info != (PrimitiveInfo *) NULL)
4442  {
4443  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4444  if ((primitive_info[i].primitive == TextPrimitive) ||
4445  (primitive_info[i].primitive == ImagePrimitive))
4446  if (primitive_info[i].text != (char *) NULL)
4447  primitive_info[i].text=DestroyString(primitive_info[i].text);
4448  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4449  }
4450  primitive=DestroyString(primitive);
4451  if (stops != (StopInfo *) NULL)
4452  stops=(StopInfo *) RelinquishMagickMemory(stops);
4453  for ( ; n >= 0; n--)
4454  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4455  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4456  if (status == MagickFalse)
4457  ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
4458  keyword);
4459  return(status != 0 ? MagickTrue : MagickFalse);
4460 }
4461 
4463  ExceptionInfo *exception)
4464 {
4465  return(RenderMVGContent(image,draw_info,0,exception));
4466 }
4467 
4468 /*
4469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4470 % %
4471 % %
4472 % %
4473 % D r a w P a t t e r n P a t h %
4474 % %
4475 % %
4476 % %
4477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4478 %
4479 % DrawPatternPath() draws a pattern.
4480 %
4481 % The format of the DrawPatternPath method is:
4482 %
4483 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4484 % const char *name,Image **pattern,ExceptionInfo *exception)
4485 %
4486 % A description of each parameter follows:
4487 %
4488 % o image: the image.
4489 %
4490 % o draw_info: the draw info.
4491 %
4492 % o name: the pattern name.
4493 %
4494 % o image: the image.
4495 %
4496 % o exception: return any errors or warnings in this structure.
4497 %
4498 */
4500  const DrawInfo *draw_info,const char *name,Image **pattern,
4501  ExceptionInfo *exception)
4502 {
4503  char
4504  property[MagickPathExtent];
4505 
4506  const char
4507  *geometry,
4508  *path,
4509  *type;
4510 
4511  DrawInfo
4512  *clone_info;
4513 
4514  ImageInfo
4515  *image_info;
4516 
4518  status;
4519 
4520  assert(image != (Image *) NULL);
4521  assert(image->signature == MagickCoreSignature);
4522  if (image->debug != MagickFalse)
4523  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4524  assert(draw_info != (const DrawInfo *) NULL);
4525  assert(name != (const char *) NULL);
4526  (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
4527  path=GetImageArtifact(image,property);
4528  if (path == (const char *) NULL)
4529  return(MagickFalse);
4530  (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
4531  geometry=GetImageArtifact(image,property);
4532  if (geometry == (const char *) NULL)
4533  return(MagickFalse);
4534  if ((*pattern) != (Image *) NULL)
4535  *pattern=DestroyImage(*pattern);
4536  image_info=AcquireImageInfo();
4537  image_info->size=AcquireString(geometry);
4538  *pattern=AcquireImage(image_info,exception);
4539  image_info=DestroyImageInfo(image_info);
4540  (void) QueryColorCompliance("#00000000",AllCompliance,
4541  &(*pattern)->background_color,exception);
4542  (void) SetImageBackgroundColor(*pattern,exception);
4543  if (image->debug != MagickFalse)
4545  "begin pattern-path %s %s",name,geometry);
4546  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4547  if (clone_info->fill_pattern != (Image *) NULL)
4548  clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4549  if (clone_info->stroke_pattern != (Image *) NULL)
4550  clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4551  (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
4552  type=GetImageArtifact(image,property);
4553  if (type != (const char *) NULL)
4556  (void) CloneString(&clone_info->primitive,path);
4557  status=RenderMVGContent(*pattern,clone_info,0,exception);
4558  clone_info=DestroyDrawInfo(clone_info);
4559  if (image->debug != MagickFalse)
4560  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4561  return(status);
4562 }
4563 
4564 /*
4565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4566 % %
4567 % %
4568 % %
4569 + D r a w P o l y g o n P r i m i t i v e %
4570 % %
4571 % %
4572 % %
4573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4574 %
4575 % DrawPolygonPrimitive() draws a polygon on the image.
4576 %
4577 % The format of the DrawPolygonPrimitive method is:
4578 %
4579 % MagickBooleanType DrawPolygonPrimitive(Image *image,
4580 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4581 % ExceptionInfo *exception)
4582 %
4583 % A description of each parameter follows:
4584 %
4585 % o image: the image.
4586 %
4587 % o draw_info: the draw info.
4588 %
4589 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4590 %
4591 % o exception: return any errors or warnings in this structure.
4592 %
4593 */
4594 
4596 {
4597  register ssize_t
4598  i;
4599 
4600  assert(polygon_info != (PolygonInfo **) NULL);
4601  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4602  if (polygon_info[i] != (PolygonInfo *) NULL)
4603  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4604  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4605  return(polygon_info);
4606 }
4607 
4609  const PrimitiveInfo *primitive_info)
4610 {
4611  PathInfo
4612  *magick_restrict path_info;
4613 
4614  PolygonInfo
4615  **polygon_info;
4616 
4617  register ssize_t
4618  i;
4619 
4620  size_t
4621  number_threads;
4622 
4623  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4624  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4625  sizeof(*polygon_info));
4626  if (polygon_info == (PolygonInfo **) NULL)
4627  return((PolygonInfo **) NULL);
4628  (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4629  path_info=ConvertPrimitiveToPath(primitive_info);
4630  if (path_info == (PathInfo *) NULL)
4631  return(DestroyPolygonThreadSet(polygon_info));
4632  for (i=0; i < (ssize_t) number_threads; i++)
4633  {
4634  polygon_info[i]=ConvertPathToPolygon(path_info);
4635  if (polygon_info[i] == (PolygonInfo *) NULL)
4636  return(DestroyPolygonThreadSet(polygon_info));
4637  }
4638  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4639  return(polygon_info);
4640 }
4641 
4642 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
4643  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4644  const ssize_t y,double *stroke_alpha)
4645 {
4646  double
4647  alpha,
4648  beta,
4649  distance,
4650  subpath_alpha;
4651 
4652  PointInfo
4653  delta;
4654 
4655  register const PointInfo
4656  *q;
4657 
4658  register EdgeInfo
4659  *p;
4660 
4661  register ssize_t
4662  i;
4663 
4664  ssize_t
4665  j,
4666  winding_number;
4667 
4668  /*
4669  Compute fill & stroke opacity for this (x,y) point.
4670  */
4671  *stroke_alpha=0.0;
4672  subpath_alpha=0.0;
4673  p=polygon_info->edges;
4674  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4675  {
4676  if ((double) y <= (p->bounds.y1-mid-0.5))
4677  break;
4678  if ((double) y > (p->bounds.y2+mid+0.5))
4679  {
4680  (void) DestroyEdge(polygon_info,(size_t) j);
4681  continue;
4682  }
4683  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4684  ((double) x > (p->bounds.x2+mid+0.5)))
4685  continue;
4686  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4687  for ( ; i < (ssize_t) p->number_points; i++)
4688  {
4689  if ((double) y <= (p->points[i-1].y-mid-0.5))
4690  break;
4691  if ((double) y > (p->points[i].y+mid+0.5))
4692  continue;
4693  if (p->scanline != (double) y)
4694  {
4695  p->scanline=(double) y;
4696  p->highwater=(size_t) i;
4697  }
4698  /*
4699  Compute distance between a point and an edge.
4700  */
4701  q=p->points+i-1;
4702  delta.x=(q+1)->x-q->x;
4703  delta.y=(q+1)->y-q->y;
4704  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4705  if (beta <= 0.0)
4706  {
4707  delta.x=(double) x-q->x;
4708  delta.y=(double) y-q->y;
4709  distance=delta.x*delta.x+delta.y*delta.y;
4710  }
4711  else
4712  {
4713  alpha=delta.x*delta.x+delta.y*delta.y;
4714  if (beta >= alpha)
4715  {
4716  delta.x=(double) x-(q+1)->x;
4717  delta.y=(double) y-(q+1)->y;
4718  distance=delta.x*delta.x+delta.y*delta.y;
4719  }
4720  else
4721  {
4722  alpha=PerceptibleReciprocal(alpha);
4723  beta=delta.x*(y-q->y)-delta.y*(x-q->x)+MagickEpsilon;
4724  distance=alpha*beta*beta;
4725  }
4726  }
4727  /*
4728  Compute stroke & subpath opacity.
4729  */
4730  beta=0.0;
4731  if (p->ghostline == MagickFalse)
4732  {
4733  alpha=mid+0.5;
4734  if ((*stroke_alpha < 1.0) &&
4735  (distance <= ((alpha+0.25)*(alpha+0.25))))
4736  {
4737  alpha=mid-0.5;
4738  if (distance <= ((alpha+0.25)*(alpha+0.25)))
4739  *stroke_alpha=1.0;
4740  else
4741  {
4742  beta=1.0;
4743  if (fabs(distance-1.0) >= MagickEpsilon)
4744  beta=sqrt((double) distance);
4745  alpha=beta-mid-0.5;
4746  if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4747  *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4748  }
4749  }
4750  }
4751  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4752  continue;
4753  if (distance <= 0.0)
4754  {
4755  subpath_alpha=1.0;
4756  continue;
4757  }
4758  if (distance > 1.0)
4759  continue;
4760  if (fabs(beta) < MagickEpsilon)
4761  {
4762  beta=1.0;
4763  if (fabs(distance-1.0) >= MagickEpsilon)
4764  beta=sqrt(distance);
4765  }
4766  alpha=beta-1.0;
4767  if (subpath_alpha < (alpha*alpha))
4768  subpath_alpha=alpha*alpha;
4769  }
4770  }
4771  /*
4772  Compute fill opacity.
4773  */
4774  if (fill == MagickFalse)
4775  return(0.0);
4776  if (subpath_alpha >= 1.0)
4777  return(1.0);
4778  /*
4779  Determine winding number.
4780  */
4781  winding_number=0;
4782  p=polygon_info->edges;
4783  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4784  {
4785  if ((double) y <= p->bounds.y1)
4786  break;
4787  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4788  continue;
4789  if ((double) x > p->bounds.x2)
4790  {
4791  winding_number+=p->direction ? 1 : -1;
4792  continue;
4793  }
4794  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4795  for ( ; i < (ssize_t) (p->number_points-1); i++)
4796  if ((double) y <= p->points[i].y)
4797  break;
4798  q=p->points+i-1;
4799  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4800  winding_number+=p->direction ? 1 : -1;
4801  }
4802  if (fill_rule != NonZeroRule)
4803  {
4804  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4805  return(1.0);
4806  }
4807  else
4808  if (MagickAbsoluteValue(winding_number) != 0)
4809  return(1.0);
4810  return(subpath_alpha);
4811 }
4812 
4814  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4815  ExceptionInfo *exception)
4816 {
4817  CacheView
4818  *image_view;
4819 
4820  const char
4821  *artifact;
4822 
4824  fill,
4825  status;
4826 
4827  double
4828  mid;
4829 
4830  PolygonInfo
4831  **magick_restrict polygon_info;
4832 
4833  register EdgeInfo
4834  *p;
4835 
4836  register ssize_t
4837  i;
4838 
4839  SegmentInfo
4840  bounds;
4841 
4842  ssize_t
4843  start_y,
4844  stop_y,
4845  y;
4846 
4847  assert(image != (Image *) NULL);
4848  assert(image->signature == MagickCoreSignature);
4849  if (image->debug != MagickFalse)
4850  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4851  assert(draw_info != (DrawInfo *) NULL);
4852  assert(draw_info->signature == MagickCoreSignature);
4853  assert(primitive_info != (PrimitiveInfo *) NULL);
4854  if (primitive_info->coordinates <= 1)
4855  return(MagickTrue);
4856  /*
4857  Compute bounding box.
4858  */
4859  polygon_info=AcquirePolygonThreadSet(primitive_info);
4860  if (polygon_info == (PolygonInfo **) NULL)
4861  return(MagickFalse);
4862  if (image->debug != MagickFalse)
4863  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4864  fill=(primitive_info->method == FillToBorderMethod) ||
4865  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4866  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4867  bounds=polygon_info[0]->edges[0].bounds;
4868  artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
4869  if (IsStringTrue(artifact) != MagickFalse)
4870  (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
4871  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4872  {
4873  p=polygon_info[0]->edges+i;
4874  if (p->bounds.x1 < bounds.x1)
4875  bounds.x1=p->bounds.x1;
4876  if (p->bounds.y1 < bounds.y1)
4877  bounds.y1=p->bounds.y1;
4878  if (p->bounds.x2 > bounds.x2)
4879  bounds.x2=p->bounds.x2;
4880  if (p->bounds.y2 > bounds.y2)
4881  bounds.y2=p->bounds.y2;
4882  }
4883  bounds.x1-=(mid+1.0);
4884  bounds.y1-=(mid+1.0);
4885  bounds.x2+=(mid+1.0);
4886  bounds.y2+=(mid+1.0);
4887  if ((bounds.x1 >= (double) image->columns) ||
4888  (bounds.y1 >= (double) image->rows) ||
4889  (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
4890  {
4891  polygon_info=DestroyPolygonThreadSet(polygon_info);
4892  return(MagickTrue); /* virtual polygon */
4893  }
4894  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
4895  (double) image->columns-1.0 : bounds.x1;
4896  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
4897  (double) image->rows-1.0 : bounds.y1;
4898  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
4899  (double) image->columns-1.0 : bounds.x2;
4900  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
4901  (double) image->rows-1.0 : bounds.y2;
4902  status=MagickTrue;
4903  image_view=AcquireAuthenticCacheView(image,exception);
4904  if ((primitive_info->coordinates == 1) ||
4905  (polygon_info[0]->number_edges == 0))
4906  {
4907  /*
4908  Draw point.
4909  */
4910  start_y=(ssize_t) ceil(bounds.y1-0.5);
4911  stop_y=(ssize_t) floor(bounds.y2+0.5);
4912 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4913  #pragma omp parallel for schedule(static) shared(status) \
4914  magick_number_threads(image,image,stop_y-start_y+1,1)
4915 #endif
4916  for (y=start_y; y <= stop_y; y++)
4917  {
4919  sync;
4920 
4921  PixelInfo
4922  pixel;
4923 
4924  register ssize_t
4925  x;
4926 
4927  register Quantum
4928  *magick_restrict q;
4929 
4930  ssize_t
4931  start_x,
4932  stop_x;
4933 
4934  if (status == MagickFalse)
4935  continue;
4936  start_x=(ssize_t) ceil(bounds.x1-0.5);
4937  stop_x=(ssize_t) floor(bounds.x2+0.5);
4938  x=start_x;
4939  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4940  exception);
4941  if (q == (Quantum *) NULL)
4942  {
4943  status=MagickFalse;
4944  continue;
4945  }
4946  GetPixelInfo(image,&pixel);
4947  for ( ; x <= stop_x; x++)
4948  {
4949  if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4950  (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4951  {
4952  GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4953  SetPixelViaPixelInfo(image,&pixel,q);
4954  }
4955  q+=GetPixelChannels(image);
4956  }
4957  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4958  if (sync == MagickFalse)
4959  status=MagickFalse;
4960  }
4961  image_view=DestroyCacheView(image_view);
4962  polygon_info=DestroyPolygonThreadSet(polygon_info);
4963  if (image->debug != MagickFalse)
4965  " end draw-polygon");
4966  return(status);
4967  }
4968  /*
4969  Draw polygon or line.
4970  */
4971  start_y=(ssize_t) ceil(bounds.y1-0.5);
4972  stop_y=(ssize_t) floor(bounds.y2+0.5);
4973 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4974  #pragma omp parallel for schedule(static) shared(status) \
4975  magick_number_threads(image,image,stop_y-start_y+1,1)
4976 #endif
4977  for (y=start_y; y <= stop_y; y++)
4978  {
4979  const int
4980  id = GetOpenMPThreadId();
4981 
4982  register Quantum
4983  *magick_restrict q;
4984 
4985  register ssize_t
4986  x;
4987 
4988  ssize_t
4989  start_x,
4990  stop_x;
4991 
4992  if (status == MagickFalse)
4993  continue;
4994  start_x=(ssize_t) ceil(bounds.x1-0.5);
4995  stop_x=(ssize_t) floor(bounds.x2+0.5);
4996  q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+
4997  1),1,exception);
4998  if (q == (Quantum *) NULL)
4999  {
5000  status=MagickFalse;
5001  continue;
5002  }
5003  for (x=start_x; x <= stop_x; x++)
5004  {
5005  double
5006  fill_alpha,
5007  stroke_alpha;
5008 
5009  PixelInfo
5010  fill_color,
5011  stroke_color;
5012 
5013  /*
5014  Fill and/or stroke.
5015  */
5016  fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
5017  x,y,&stroke_alpha);
5018  if (draw_info->stroke_antialias == MagickFalse)
5019  {
5020  fill_alpha=fill_alpha > 0.5 ? 1.0 : 0.0;
5021  stroke_alpha=stroke_alpha > 0.5 ? 1.0 : 0.0;
5022  }
5023  GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
5024  CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5025  (double) GetPixelAlpha(image,q),q);
5026  GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
5027  CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5028  (double) GetPixelAlpha(image,q),q);
5029  q+=GetPixelChannels(image);
5030  }
5031  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5032  status=MagickFalse;
5033  }
5034  image_view=DestroyCacheView(image_view);
5035  polygon_info=DestroyPolygonThreadSet(polygon_info);
5036  if (image->debug != MagickFalse)
5037  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5038  return(status);
5039 }
5040 
5041 /*
5042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5043 % %
5044 % %
5045 % %
5046 % D r a w P r i m i t i v e %
5047 % %
5048 % %
5049 % %
5050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5051 %
5052 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5053 %
5054 % The format of the DrawPrimitive method is:
5055 %
5056 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5057 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
5058 %
5059 % A description of each parameter follows:
5060 %
5061 % o image: the image.
5062 %
5063 % o draw_info: the draw info.
5064 %
5065 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5066 %
5067 % o exception: return any errors or warnings in this structure.
5068 %
5069 */
5070 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5071 {
5072  const char
5073  *methods[] =
5074  {
5075  "point",
5076  "replace",
5077  "floodfill",
5078  "filltoborder",
5079  "reset",
5080  "?"
5081  };
5082 
5083  PointInfo
5084  p,
5085  point,
5086  q;
5087 
5088  register ssize_t
5089  i,
5090  x;
5091 
5092  ssize_t
5093  coordinates,
5094  y;
5095 
5096  x=(ssize_t) ceil(primitive_info->point.x-0.5);
5097  y=(ssize_t) ceil(primitive_info->point.y-0.5);
5098  switch (primitive_info->primitive)
5099  {
5100  case AlphaPrimitive:
5101  {
5103  "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
5104  methods[primitive_info->method]);
5105  return;
5106  }
5107  case ColorPrimitive:
5108  {
5110  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5111  methods[primitive_info->method]);
5112  return;
5113  }
5114  case ImagePrimitive:
5115  {
5117  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5118  return;
5119  }
5120  case PointPrimitive:
5121  {
5123  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5124  methods[primitive_info->method]);
5125  return;
5126  }
5127  case TextPrimitive:
5128  {
5130  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5131  return;
5132  }
5133  default:
5134  break;
5135  }
5136  coordinates=0;
5137  p=primitive_info[0].point;
5138  q.x=(-1.0);
5139  q.y=(-1.0);
5140  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5141  {
5142  point=primitive_info[i].point;
5143  if (coordinates <= 0)
5144  {
5145  coordinates=(ssize_t) primitive_info[i].coordinates;
5147  " begin open (%.20g)",(double) coordinates);
5148  p=point;
5149  }
5150  point=primitive_info[i].point;
5151  if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5152  (fabs(q.y-point.y) >= MagickEpsilon))
5154  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5155  else
5157  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5158  q=point;
5159  coordinates--;
5160  if (coordinates > 0)
5161  continue;
5162  if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5163  (fabs(p.y-point.y) >= MagickEpsilon))
5164  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5165  (double) coordinates);
5166  else
5167  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5168  (double) coordinates);
5169  }
5170 }
5171 
5173  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5174  ExceptionInfo *exception)
5175 {
5176  CacheView
5177  *image_view;
5178 
5180  status;
5181 
5182  register ssize_t
5183  i,
5184  x;
5185 
5186  ssize_t
5187  y;
5188 
5189  if (image->debug != MagickFalse)
5190  {
5192  " begin draw-primitive");
5194  " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5195  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5196  draw_info->affine.tx,draw_info->affine.ty);
5197  }
5198  status=MagickTrue;
5199  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5200  ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5201  (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5202  status&=SetImageColorspace(image,sRGBColorspace,exception);
5203  if (draw_info->compliance == SVGCompliance)
5204  {
5205  status&=SetImageMask(image,WritePixelMask,draw_info->clipping_mask,
5206  exception);
5207  status&=SetImageMask(image,CompositePixelMask,draw_info->composite_mask,
5208  exception);
5209  }
5210  x=(ssize_t) ceil(primitive_info->point.x-0.5);
5211  y=(ssize_t) ceil(primitive_info->point.y-0.5);
5212  image_view=AcquireAuthenticCacheView(image,exception);
5213  switch (primitive_info->primitive)
5214  {
5215  case AlphaPrimitive:
5216  {
5217  if (image->alpha_trait == UndefinedPixelTrait)
5218  status&=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
5219  switch (primitive_info->method)
5220  {
5221  case PointMethod:
5222  default:
5223  {
5224  PixelInfo
5225  pixel;
5226 
5227  register Quantum
5228  *q;
5229 
5230  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5231  if (q == (Quantum *) NULL)
5232  break;
5233  GetFillColor(draw_info,x,y,&pixel,exception);
5234  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5235  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5236  break;
5237  }
5238  case ReplaceMethod:
5239  {
5240  PixelInfo
5241  pixel,
5242  target;
5243 
5244  status&=GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5245  exception);
5246  GetPixelInfo(image,&pixel);
5247  for (y=0; y < (ssize_t) image->rows; y++)
5248  {
5249  register Quantum
5250  *magick_restrict q;
5251 
5252  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5253  exception);
5254  if (q == (Quantum *) NULL)
5255  break;
5256  for (x=0; x < (ssize_t) image->columns; x++)
5257  {
5258  GetPixelInfoPixel(image,q,&pixel);
5259  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5260  {
5261  q+=GetPixelChannels(image);
5262  continue;
5263  }
5264  GetFillColor(draw_info,x,y,&pixel,exception);
5265  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5266  q+=GetPixelChannels(image);
5267  }
5268  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5269  if (status == MagickFalse)
5270  break;
5271  }
5272  break;
5273  }
5274  case FloodfillMethod:
5275  case FillToBorderMethod:
5276  {
5277  ChannelType
5278  channel_mask;
5279 
5280  PixelInfo
5281  target;
5282 
5284  &target,exception);
5285  if (primitive_info->method == FillToBorderMethod)
5286  {
5287  target.red=(double) draw_info->border_color.red;
5288  target.green=(double) draw_info->border_color.green;
5289  target.blue=(double) draw_info->border_color.blue;
5290  }
5291  channel_mask=SetImageChannelMask(image,AlphaChannel);
5292  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5293  primitive_info->method == FloodfillMethod ? MagickFalse :
5294  MagickTrue,exception);
5295  (void) SetImageChannelMask(image,channel_mask);
5296  break;
5297  }
5298  case ResetMethod:
5299  {
5300  PixelInfo
5301  pixel;
5302 
5303  for (y=0; y < (ssize_t) image->rows; y++)
5304  {
5305  register Quantum
5306  *magick_restrict q;
5307 
5308  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5309  exception);
5310  if (q == (Quantum *) NULL)
5311  break;
5312  for (x=0; x < (ssize_t) image->columns; x++)
5313  {
5314  GetFillColor(draw_info,x,y,&pixel,exception);
5315  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5316  q+=GetPixelChannels(image);
5317  }
5318  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5319  if (status == MagickFalse)
5320  break;
5321  }
5322  break;
5323  }
5324  }
5325  break;
5326  }
5327  case ColorPrimitive:
5328  {
5329  switch (primitive_info->method)
5330  {
5331  case PointMethod:
5332  default:
5333  {
5334  PixelInfo
5335  pixel;
5336 
5337  register Quantum
5338  *q;
5339 
5340  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5341  if (q == (Quantum *) NULL)
5342  break;
5343  GetPixelInfo(image,&pixel);
5344  GetFillColor(draw_info,x,y,&pixel,exception);
5345  SetPixelViaPixelInfo(image,&pixel,q);
5346  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5347  break;
5348  }
5349  case ReplaceMethod:
5350  {
5351  PixelInfo
5352  pixel,
5353  target;
5354 
5355  status&=GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5356  exception);
5357  for (y=0; y < (ssize_t) image->rows; y++)
5358  {
5359  register Quantum
5360  *magick_restrict q;
5361 
5362  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5363  exception);
5364  if (q == (Quantum *) NULL)
5365  break;
5366  for (x=0; x < (ssize_t) image->columns; x++)
5367  {
5368  GetPixelInfoPixel(image,q,&pixel);
5369  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5370  {
5371  q+=GetPixelChannels(image);
5372  continue;
5373  }
5374  GetFillColor(draw_info,x,y,&pixel,exception);
5375  SetPixelViaPixelInfo(image,&pixel,q);
5376  q+=GetPixelChannels(image);
5377  }
5378  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5379  if (status == MagickFalse)
5380  break;
5381  }
5382  break;
5383  }
5384  case FloodfillMethod:
5385  case FillToBorderMethod:
5386  {
5387  PixelInfo
5388  target;
5389 
5391  &target,exception);
5392  if (primitive_info->method == FillToBorderMethod)
5393  {
5394  target.red=(double) draw_info->border_color.red;
5395  target.green=(double) draw_info->border_color.green;
5396  target.blue=(double) draw_info->border_color.blue;
5397  }
5398  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5399  primitive_info->method == FloodfillMethod ? MagickFalse :
5400  MagickTrue,exception);
5401  break;
5402  }
5403  case ResetMethod:
5404  {
5405  PixelInfo
5406  pixel;
5407 
5408  GetPixelInfo(image,&pixel);
5409  for (y=0; y < (ssize_t) image->rows; y++)
5410  {
5411  register Quantum
5412  *magick_restrict q;
5413 
5414  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5415  exception);
5416  if (q == (Quantum *) NULL)
5417  break;
5418  for (x=0; x < (ssize_t) image->columns; x++)
5419  {
5420  GetFillColor(draw_info,x,y,&pixel,exception);
5421  SetPixelViaPixelInfo(image,&pixel,q);
5422  q+=GetPixelChannels(image);
5423  }
5424  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5425  if (status == MagickFalse)
5426  break;
5427  }
5428  break;
5429  }
5430  }
5431  break;
5432  }
5433  case ImagePrimitive:
5434  {
5435  AffineMatrix
5436  affine;
5437 
5438  char
5439  composite_geometry[MagickPathExtent];
5440 
5441  Image
5442  *composite_image,
5443  *composite_images;
5444 
5445  ImageInfo
5446  *clone_info;
5447 
5449  geometry;
5450 
5451  ssize_t
5452  x1,
5453  y1;
5454 
5455  if (primitive_info->text == (char *) NULL)
5456  break;
5457  clone_info=AcquireImageInfo();
5458  composite_images=(Image *) NULL;
5459  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5460  composite_images=ReadInlineImage(clone_info,primitive_info->text,
5461  exception);
5462  else
5463  if (*primitive_info->text != '\0')
5464  {
5465  (void) CopyMagickString(clone_info->filename,primitive_info->text,
5467  status&=SetImageInfo(clone_info,0,exception);
5468  if ((LocaleNCompare(clone_info->magick,"http",4) == 0) ||
5469  (LocaleCompare(clone_info->magick,"mpri") == 0))
5470  (void) CopyMagickString(clone_info->filename,primitive_info->text,
5472  if (clone_info->size != (char *) NULL)
5473  clone_info->size=DestroyString(clone_info->size);
5474  if (clone_info->extract != (char *) NULL)
5475  clone_info->extract=DestroyString(clone_info->extract);
5476  composite_images=ReadImage(clone_info,exception);
5477  }
5478  clone_info=DestroyImageInfo(clone_info);
5479  if (composite_images == (Image *) NULL)
5480  {