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