MagickCore  7.1.0
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 4296.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*PerceptibleReciprocal(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  quantum=sizeof(**mvg_info->primitive_info);
2276  extent=(double) mvg_info->offset+pad+(PrimitiveExtentPad+1)*quantum;
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+1),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  {
2291  (*mvg_info->primitive_info)[i].primitive=UndefinedPrimitive;
2292  (*mvg_info->primitive_info)[i].text=(char *) NULL;
2293  }
2294  return(MagickTrue);
2295  }
2296  }
2297  /*
2298  Reallocation failed, allocate a primitive to facilitate unwinding.
2299  */
2300  (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
2301  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2302  if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2304  *mvg_info->primitive_info);
2305  *mvg_info->primitive_info=(PrimitiveInfo *) AcquireCriticalMemory((size_t) (
2306  (PrimitiveExtentPad+1)*quantum));
2307  (void) memset(*mvg_info->primitive_info,0,(size_t) ((PrimitiveExtentPad+1)*
2308  quantum));
2309  *mvg_info->extent=1;
2310  mvg_info->offset=0;
2311  return(MagickFalse);
2312 }
2313 
2314 static inline double GetDrawValue(const char *magick_restrict string,
2315  char **magick_restrict sentinal)
2316 {
2317  char
2318  **magick_restrict q;
2319 
2320  double
2321  value;
2322 
2323  q=sentinal;
2324  value=InterpretLocaleValue(string,q);
2325  sentinal=q;
2326  return(value);
2327 }
2328 
2329 static int MVGMacroCompare(const void *target,const void *source)
2330 {
2331  const char
2332  *p,
2333  *q;
2334 
2335  p=(const char *) target;
2336  q=(const char *) source;
2337  return(strcmp(p,q));
2338 }
2339 
2340 static SplayTreeInfo *GetMVGMacros(const char *primitive)
2341 {
2342  char
2343  *macro,
2344  *token;
2345 
2346  const char
2347  *q;
2348 
2349  size_t
2350  extent;
2351 
2353  *macros;
2354 
2355  /*
2356  Scan graphic primitives for definitions and classes.
2357  */
2358  if (primitive == (const char *) NULL)
2359  return((SplayTreeInfo *) NULL);
2362  macro=AcquireString(primitive);
2363  token=AcquireString(primitive);
2364  extent=strlen(token)+MagickPathExtent;
2365  for (q=primitive; *q != '\0'; )
2366  {
2367  if (GetNextToken(q,&q,extent,token) < 1)
2368  break;
2369  if (*token == '\0')
2370  break;
2371  if (LocaleCompare("push",token) == 0)
2372  {
2373  const char
2374  *end,
2375  *start;
2376 
2377  (void) GetNextToken(q,&q,extent,token);
2378  if (*q == '"')
2379  {
2380  char
2381  name[MagickPathExtent];
2382 
2383  const char
2384  *p;
2385 
2386  ssize_t
2387  n;
2388 
2389  /*
2390  Named macro (e.g. push graphic-context "wheel").
2391  */
2392  (void) GetNextToken(q,&q,extent,token);
2393  start=q;
2394  end=q;
2395  (void) CopyMagickString(name,token,MagickPathExtent);
2396  n=1;
2397  for (p=q; *p != '\0'; )
2398  {
2399  if (GetNextToken(p,&p,extent,token) < 1)
2400  break;
2401  if (*token == '\0')
2402  break;
2403  if (LocaleCompare(token,"pop") == 0)
2404  {
2405  end=p-strlen(token)-1;
2406  n--;
2407  }
2408  if (LocaleCompare(token,"push") == 0)
2409  n++;
2410  if ((n == 0) && (end > start))
2411  {
2412  /*
2413  Extract macro.
2414  */
2415  (void) GetNextToken(p,&p,extent,token);
2416  (void) CopyMagickString(macro,start,(size_t) (end-start));
2417  (void) AddValueToSplayTree(macros,ConstantString(name),
2418  ConstantString(macro));
2419  break;
2420  }
2421  }
2422  }
2423  }
2424  }
2425  token=DestroyString(token);
2426  macro=DestroyString(macro);
2427  return(macros);
2428 }
2429 
2430 static inline MagickBooleanType IsPoint(const char *point)
2431 {
2432  char
2433  *p;
2434 
2435  double
2436  value;
2437 
2438  value=GetDrawValue(point,&p);
2439  return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2440  MagickTrue);
2441 }
2442 
2443 static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2444  const PointInfo point)
2445 {
2446  primitive_info->coordinates=1;
2447  primitive_info->closed_subpath=MagickFalse;
2448  primitive_info->point=point;
2449  return(MagickTrue);
2450 }
2451 
2453  const DrawInfo *draw_info,const size_t depth,ExceptionInfo *exception)
2454 {
2455 #define RenderImageTag "Render/Image"
2456 
2457  AffineMatrix
2458  affine,
2459  current;
2460 
2461  char
2462  keyword[MagickPathExtent],
2463  geometry[MagickPathExtent],
2464  *next_token,
2465  pattern[MagickPathExtent],
2466  *primitive,
2467  *token;
2468 
2469  const char
2470  *q;
2471 
2472  double
2473  angle,
2474  coordinates,
2475  cursor,
2476  factor,
2477  primitive_extent;
2478 
2479  DrawInfo
2480  *clone_info,
2481  **graphic_context;
2482 
2484  proceed;
2485 
2487  status;
2488 
2489  MVGInfo
2490  mvg_info;
2491 
2492  PointInfo
2493  point;
2494 
2496  *primitive_info;
2497 
2499  primitive_type;
2500 
2501  const char
2502  *p;
2503 
2504  ssize_t
2505  i,
2506  x;
2507 
2508  SegmentInfo
2509  bounds;
2510 
2511  size_t
2512  extent,
2513  number_points,
2514  number_stops;
2515 
2517  *macros;
2518 
2519  ssize_t
2520  defsDepth,
2521  j,
2522  k,
2523  n,
2524  symbolDepth;
2525 
2526  StopInfo
2527  *stops;
2528 
2529  TypeMetric
2530  metrics;
2531 
2532  assert(image != (Image *) NULL);
2533  assert(image->signature == MagickCoreSignature);
2534  if (image->debug != MagickFalse)
2535  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2536  assert(draw_info != (DrawInfo *) NULL);
2537  assert(draw_info->signature == MagickCoreSignature);
2538  if (image->debug != MagickFalse)
2539  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2540  if (depth > MagickMaxRecursionDepth)
2541  ThrowBinaryException(DrawError,"VectorGraphicsNestedTooDeeply",
2542  image->filename);
2543  if ((draw_info->primitive == (char *) NULL) ||
2544  (*draw_info->primitive == '\0'))
2545  return(MagickFalse);
2546  if (image->debug != MagickFalse)
2547  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2548  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2549  return(MagickFalse);
2550  if (image->alpha_trait == UndefinedPixelTrait)
2551  {
2552  status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2553  if (status == MagickFalse)
2554  return(MagickFalse);
2555  }
2556  if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2557  (*(draw_info->primitive+1) != '-') && (depth == 0))
2558  primitive=FileToString(draw_info->primitive+1,~0UL,exception);
2559  else
2560  primitive=AcquireString(draw_info->primitive);
2561  if (primitive == (char *) NULL)
2562  return(MagickFalse);
2563  primitive_extent=(double) strlen(primitive);
2564  (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2565  n=0;
2566  number_stops=0;
2567  stops=(StopInfo *) NULL;
2568  /*
2569  Allocate primitive info memory.
2570  */
2571  graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2572  if (graphic_context == (DrawInfo **) NULL)
2573  {
2574  primitive=DestroyString(primitive);
2575  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2576  image->filename);
2577  }
2578  number_points=(size_t) PrimitiveExtentPad;
2579  primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
2580  (number_points+1),sizeof(*primitive_info));
2581  if (primitive_info == (PrimitiveInfo *) NULL)
2582  {
2583  primitive=DestroyString(primitive);
2584  for ( ; n >= 0; n--)
2585  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2586  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2587  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2588  image->filename);
2589  }
2590  (void) memset(primitive_info,0,(size_t) (number_points+1)*
2591  sizeof(*primitive_info));
2592  (void) memset(&mvg_info,0,sizeof(mvg_info));
2593  mvg_info.primitive_info=(&primitive_info);
2594  mvg_info.extent=(&number_points);
2595  mvg_info.exception=exception;
2596  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2597  graphic_context[n]->viewbox=image->page;
2598  if ((image->page.width == 0) || (image->page.height == 0))
2599  {
2600  graphic_context[n]->viewbox.width=image->columns;
2601  graphic_context[n]->viewbox.height=image->rows;
2602  }
2603  token=AcquireString(primitive);
2604  extent=strlen(token)+MagickPathExtent;
2605  defsDepth=0;
2606  symbolDepth=0;
2607  cursor=0.0;
2608  macros=GetMVGMacros(primitive);
2609  status=MagickTrue;
2610  for (q=primitive; *q != '\0'; )
2611  {
2612  /*
2613  Interpret graphic primitive.
2614  */
2615  if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2616  break;
2617  if (*keyword == '\0')
2618  break;
2619  if (*keyword == '#')
2620  {
2621  /*
2622  Comment.
2623  */
2624  while ((*q != '\n') && (*q != '\0'))
2625  q++;
2626  continue;
2627  }
2628  p=q-strlen(keyword)-1;
2629  primitive_type=UndefinedPrimitive;
2630  current=graphic_context[n]->affine;
2631  GetAffineMatrix(&affine);
2632  *token='\0';
2633  switch (*keyword)
2634  {
2635  case ';':
2636  break;
2637  case 'a':
2638  case 'A':
2639  {
2640  if (LocaleCompare("affine",keyword) == 0)
2641  {
2642  (void) GetNextToken(q,&q,extent,token);
2643  affine.sx=GetDrawValue(token,&next_token);
2644  if (token == next_token)
2645  ThrowPointExpectedException(token,exception);
2646  (void) GetNextToken(q,&q,extent,token);
2647  if (*token == ',')
2648  (void) GetNextToken(q,&q,extent,token);
2649  affine.rx=GetDrawValue(token,&next_token);
2650  if (token == next_token)
2651  ThrowPointExpectedException(token,exception);
2652  (void) GetNextToken(q,&q,extent,token);
2653  if (*token == ',')
2654  (void) GetNextToken(q,&q,extent,token);
2655  affine.ry=GetDrawValue(token,&next_token);
2656  if (token == next_token)
2657  ThrowPointExpectedException(token,exception);
2658  (void) GetNextToken(q,&q,extent,token);
2659  if (*token == ',')
2660  (void) GetNextToken(q,&q,extent,token);
2661  affine.sy=GetDrawValue(token,&next_token);
2662  if (token == next_token)
2663  ThrowPointExpectedException(token,exception);
2664  (void) GetNextToken(q,&q,extent,token);
2665  if (*token == ',')
2666  (void) GetNextToken(q,&q,extent,token);
2667  affine.tx=GetDrawValue(token,&next_token);
2668  if (token == next_token)
2669  ThrowPointExpectedException(token,exception);
2670  (void) GetNextToken(q,&q,extent,token);
2671  if (*token == ',')
2672  (void) GetNextToken(q,&q,extent,token);
2673  affine.ty=GetDrawValue(token,&next_token);
2674  if (token == next_token)
2675  ThrowPointExpectedException(token,exception);
2676  break;
2677  }
2678  if (LocaleCompare("alpha",keyword) == 0)
2679  {
2680  primitive_type=AlphaPrimitive;
2681  break;
2682  }
2683  if (LocaleCompare("arc",keyword) == 0)
2684  {
2685  primitive_type=ArcPrimitive;
2686  break;
2687  }
2688  status=MagickFalse;
2689  break;
2690  }
2691  case 'b':
2692  case 'B':
2693  {
2694  if (LocaleCompare("bezier",keyword) == 0)
2695  {
2696  primitive_type=BezierPrimitive;
2697  break;
2698  }
2699  if (LocaleCompare("border-color",keyword) == 0)
2700  {
2701  (void) GetNextToken(q,&q,extent,token);
2702  status&=QueryColorCompliance(token,AllCompliance,
2703  &graphic_context[n]->border_color,exception);
2704  break;
2705  }
2706  status=MagickFalse;
2707  break;
2708  }
2709  case 'c':
2710  case 'C':
2711  {
2712  if (LocaleCompare("class",keyword) == 0)
2713  {
2714  const char
2715  *mvg_class;
2716 
2717  (void) GetNextToken(q,&q,extent,token);
2718  if (*token == '\0')
2719  {
2720  status=MagickFalse;
2721  break;
2722  }
2723  if (LocaleCompare(token,graphic_context[n]->id) == 0)
2724  break;
2725  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2726  if ((graphic_context[n]->render != MagickFalse) &&
2727  (mvg_class != (const char *) NULL) && (p > primitive))
2728  {
2729  char
2730  *elements;
2731 
2732  ssize_t
2733  offset;
2734 
2735  /*
2736  Inject class elements in stream.
2737  */
2738  offset=(ssize_t) (p-primitive);
2739  elements=AcquireString(primitive);
2740  elements[offset]='\0';
2741  (void) ConcatenateString(&elements,mvg_class);
2742  (void) ConcatenateString(&elements,"\n");
2743  (void) ConcatenateString(&elements,q);
2744  primitive=DestroyString(primitive);
2745  primitive=elements;
2746  q=primitive+offset;
2747  }
2748  break;
2749  }
2750  if (LocaleCompare("clip-path",keyword) == 0)
2751  {
2752  const char
2753  *clip_path;
2754 
2755  /*
2756  Take a node from within the MVG document, and duplicate it here.
2757  */
2758  (void) GetNextToken(q,&q,extent,token);
2759  if (*token == '\0')
2760  {
2761  status=MagickFalse;
2762  break;
2763  }
2764  (void) CloneString(&graphic_context[n]->clip_mask,token);
2765  clip_path=(const char *) GetValueFromSplayTree(macros,token);
2766  if (clip_path != (const char *) NULL)
2767  {
2768  if (graphic_context[n]->clipping_mask != (Image *) NULL)
2769  graphic_context[n]->clipping_mask=
2770  DestroyImage(graphic_context[n]->clipping_mask);
2771  graphic_context[n]->clipping_mask=DrawClippingMask(image,
2772  graphic_context[n],token,clip_path,exception);
2773  if (graphic_context[n]->compliance != SVGCompliance)
2774  {
2775  clip_path=(const char *) GetValueFromSplayTree(macros,
2776  graphic_context[n]->clip_mask);
2777  if (clip_path != (const char *) NULL)
2778  (void) SetImageArtifact(image,
2779  graphic_context[n]->clip_mask,clip_path);
2780  status&=DrawClipPath(image,graphic_context[n],
2781  graphic_context[n]->clip_mask,exception);
2782  }
2783  }
2784  break;
2785  }
2786  if (LocaleCompare("clip-rule",keyword) == 0)
2787  {
2788  ssize_t
2789  fill_rule;
2790 
2791  (void) GetNextToken(q,&q,extent,token);
2793  token);
2794  if (fill_rule == -1)
2795  {
2796  status=MagickFalse;
2797  break;
2798  }
2799  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2800  break;
2801  }
2802  if (LocaleCompare("clip-units",keyword) == 0)
2803  {
2804  ssize_t
2805  clip_units;
2806 
2807  (void) GetNextToken(q,&q,extent,token);
2809  token);
2810  if (clip_units == -1)
2811  {
2812  status=MagickFalse;
2813  break;
2814  }
2815  graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2816  if (clip_units == ObjectBoundingBox)
2817  {
2818  GetAffineMatrix(&current);
2819  affine.sx=draw_info->bounds.x2;
2820  affine.sy=draw_info->bounds.y2;
2821  affine.tx=draw_info->bounds.x1;
2822  affine.ty=draw_info->bounds.y1;
2823  break;
2824  }
2825  break;
2826  }
2827  if (LocaleCompare("circle",keyword) == 0)
2828  {
2829  primitive_type=CirclePrimitive;
2830  break;
2831  }
2832  if (LocaleCompare("color",keyword) == 0)
2833  {
2834  primitive_type=ColorPrimitive;
2835  break;
2836  }
2837  if (LocaleCompare("compliance",keyword) == 0)
2838  {
2839  /*
2840  MVG compliance associates a clipping mask with an image; SVG
2841  compliance associates a clipping mask with a graphics context.
2842  */
2843  (void) GetNextToken(q,&q,extent,token);
2844  graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2846  break;
2847  }
2848  if (LocaleCompare("currentColor",keyword) == 0)
2849  {
2850  (void) GetNextToken(q,&q,extent,token);
2851  break;
2852  }
2853  status=MagickFalse;
2854  break;
2855  }
2856  case 'd':
2857  case 'D':
2858  {
2859  if (LocaleCompare("decorate",keyword) == 0)
2860  {
2861  ssize_t
2862  decorate;
2863 
2864  (void) GetNextToken(q,&q,extent,token);
2866  token);
2867  if (decorate == -1)
2868  {
2869  status=MagickFalse;
2870  break;
2871  }
2872  graphic_context[n]->decorate=(DecorationType) decorate;
2873  break;
2874  }
2875  if (LocaleCompare("density",keyword) == 0)
2876  {
2877  (void) GetNextToken(q,&q,extent,token);
2878  (void) CloneString(&graphic_context[n]->density,token);
2879  break;
2880  }
2881  if (LocaleCompare("direction",keyword) == 0)
2882  {
2883  ssize_t
2884  direction;
2885 
2886  (void) GetNextToken(q,&q,extent,token);
2888  token);
2889  if (direction == -1)
2890  status=MagickFalse;
2891  else
2892  graphic_context[n]->direction=(DirectionType) direction;
2893  break;
2894  }
2895  status=MagickFalse;
2896  break;
2897  }
2898  case 'e':
2899  case 'E':
2900  {
2901  if (LocaleCompare("ellipse",keyword) == 0)
2902  {
2903  primitive_type=EllipsePrimitive;
2904  break;
2905  }
2906  if (LocaleCompare("encoding",keyword) == 0)
2907  {
2908  (void) GetNextToken(q,&q,extent,token);
2909  (void) CloneString(&graphic_context[n]->encoding,token);
2910  break;
2911  }
2912  status=MagickFalse;
2913  break;
2914  }
2915  case 'f':
2916  case 'F':
2917  {
2918  if (LocaleCompare("fill",keyword) == 0)
2919  {
2920  (void) GetNextToken(q,&q,extent,token);
2921  if (graphic_context[n]->clip_path != MagickFalse)
2922  break;
2923  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2924  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2925  (void) DrawPatternPath(image,draw_info,token,
2926  &graphic_context[n]->fill_pattern,exception);
2927  else
2928  {
2929  status&=QueryColorCompliance(token,AllCompliance,
2930  &graphic_context[n]->fill,exception);
2931  if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2932  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2933  }
2934  break;
2935  }
2936  if (LocaleCompare("fill-opacity",keyword) == 0)
2937  {
2938  double
2939  opacity;
2940 
2941  (void) GetNextToken(q,&q,extent,token);
2942  if (graphic_context[n]->clip_path != MagickFalse)
2943  break;
2944  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2945  opacity=MagickMin(MagickMax(factor*
2946  GetDrawValue(token,&next_token),0.0),1.0);
2947  if (token == next_token)
2948  ThrowPointExpectedException(token,exception);
2949  if (graphic_context[n]->compliance == SVGCompliance)
2950  graphic_context[n]->fill_alpha*=opacity;
2951  else
2952  graphic_context[n]->fill_alpha=QuantumRange*opacity;
2953  if (graphic_context[n]->fill.alpha != TransparentAlpha)
2954  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2955  else
2956  graphic_context[n]->fill.alpha=(MagickRealType)
2957  ClampToQuantum(QuantumRange*(1.0-opacity));
2958  break;
2959  }
2960  if (LocaleCompare("fill-rule",keyword) == 0)
2961  {
2962  ssize_t
2963  fill_rule;
2964 
2965  (void) GetNextToken(q,&q,extent,token);
2967  token);
2968  if (fill_rule == -1)
2969  {
2970  status=MagickFalse;
2971  break;
2972  }
2973  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2974  break;
2975  }
2976  if (LocaleCompare("font",keyword) == 0)
2977  {
2978  (void) GetNextToken(q,&q,extent,token);
2979  (void) CloneString(&graphic_context[n]->font,token);
2980  if (LocaleCompare("none",token) == 0)
2981  graphic_context[n]->font=(char *) RelinquishMagickMemory(
2982  graphic_context[n]->font);
2983  break;
2984  }
2985  if (LocaleCompare("font-family",keyword) == 0)
2986  {
2987  (void) GetNextToken(q,&q,extent,token);
2988  (void) CloneString(&graphic_context[n]->family,token);
2989  break;
2990  }
2991  if (LocaleCompare("font-size",keyword) == 0)
2992  {
2993  (void) GetNextToken(q,&q,extent,token);
2994  graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
2995  if (token == next_token)
2996  ThrowPointExpectedException(token,exception);
2997  break;
2998  }
2999  if (LocaleCompare("font-stretch",keyword) == 0)
3000  {
3001  ssize_t
3002  stretch;
3003 
3004  (void) GetNextToken(q,&q,extent,token);
3006  if (stretch == -1)
3007  {
3008  status=MagickFalse;
3009  break;
3010  }
3011  graphic_context[n]->stretch=(StretchType) stretch;
3012  break;
3013  }
3014  if (LocaleCompare("font-style",keyword) == 0)
3015  {
3016  ssize_t
3017  style;
3018 
3019  (void) GetNextToken(q,&q,extent,token);
3021  if (style == -1)
3022  {
3023  status=MagickFalse;
3024  break;
3025  }
3026  graphic_context[n]->style=(StyleType) style;
3027  break;
3028  }
3029  if (LocaleCompare("font-weight",keyword) == 0)
3030  {
3031  ssize_t
3032  weight;
3033 
3034  (void) GetNextToken(q,&q,extent,token);
3036  if (weight == -1)
3037  weight=(ssize_t) StringToUnsignedLong(token);
3038  graphic_context[n]->weight=(size_t) weight;
3039  break;
3040  }
3041  status=MagickFalse;
3042  break;
3043  }
3044  case 'g':
3045  case 'G':
3046  {
3047  if (LocaleCompare("gradient-units",keyword) == 0)
3048  {
3049  (void) GetNextToken(q,&q,extent,token);
3050  break;
3051  }
3052  if (LocaleCompare("gravity",keyword) == 0)
3053  {
3054  ssize_t
3055  gravity;
3056 
3057  (void) GetNextToken(q,&q,extent,token);
3059  if (gravity == -1)
3060  {
3061  status=MagickFalse;
3062  break;
3063  }
3064  graphic_context[n]->gravity=(GravityType) gravity;
3065  break;
3066  }
3067  status=MagickFalse;
3068  break;
3069  }
3070  case 'i':
3071  case 'I':
3072  {
3073  if (LocaleCompare("image",keyword) == 0)
3074  {
3075  ssize_t
3076  compose;
3077 
3078  primitive_type=ImagePrimitive;
3079  (void) GetNextToken(q,&q,extent,token);
3081  if (compose == -1)
3082  {
3083  status=MagickFalse;
3084  break;
3085  }
3086  graphic_context[n]->compose=(CompositeOperator) compose;
3087  break;
3088  }
3089  if (LocaleCompare("interline-spacing",keyword) == 0)
3090  {
3091  (void) GetNextToken(q,&q,extent,token);
3092  graphic_context[n]->interline_spacing=GetDrawValue(token,
3093  &next_token);
3094  if (token == next_token)
3095  ThrowPointExpectedException(token,exception);
3096  break;
3097  }
3098  if (LocaleCompare("interword-spacing",keyword) == 0)
3099  {
3100  (void) GetNextToken(q,&q,extent,token);
3101  graphic_context[n]->interword_spacing=GetDrawValue(token,
3102  &next_token);
3103  if (token == next_token)
3104  ThrowPointExpectedException(token,exception);
3105  break;
3106  }
3107  status=MagickFalse;
3108  break;
3109  }
3110  case 'k':
3111  case 'K':
3112  {
3113  if (LocaleCompare("kerning",keyword) == 0)
3114  {
3115  (void) GetNextToken(q,&q,extent,token);
3116  graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3117  if (token == next_token)
3118  ThrowPointExpectedException(token,exception);
3119  break;
3120  }
3121  status=MagickFalse;
3122  break;
3123  }
3124  case 'l':
3125  case 'L':
3126  {
3127  if (LocaleCompare("letter-spacing",keyword) == 0)
3128  {
3129  (void) GetNextToken(q,&q,extent,token);
3130  if (IsPoint(token) == MagickFalse)
3131  break;
3132  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3133  clone_info->text=AcquireString(" ");
3134  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
3135  graphic_context[n]->kerning=metrics.width*
3136  GetDrawValue(token,&next_token);
3137  clone_info=DestroyDrawInfo(clone_info);
3138  if (token == next_token)
3139  ThrowPointExpectedException(token,exception);
3140  break;
3141  }
3142  if (LocaleCompare("line",keyword) == 0)
3143  {
3144  primitive_type=LinePrimitive;
3145  break;
3146  }
3147  status=MagickFalse;
3148  break;
3149  }
3150  case 'm':
3151  case 'M':
3152  {
3153  if (LocaleCompare("mask",keyword) == 0)
3154  {
3155  const char
3156  *mask_path;
3157 
3158  /*
3159  Take a node from within the MVG document, and duplicate it here.
3160  */
3161  (void) GetNextToken(q,&q,extent,token);
3162  mask_path=(const char *) GetValueFromSplayTree(macros,token);
3163  if (mask_path != (const char *) NULL)
3164  {
3165  if (graphic_context[n]->composite_mask != (Image *) NULL)
3166  graphic_context[n]->composite_mask=
3167  DestroyImage(graphic_context[n]->composite_mask);
3168  graphic_context[n]->composite_mask=DrawCompositeMask(image,
3169  graphic_context[n],token,mask_path,exception);
3170  if (graphic_context[n]->compliance != SVGCompliance)
3171  status=SetImageMask(image,CompositePixelMask,
3172  graphic_context[n]->composite_mask,exception);
3173  }
3174  break;
3175  }
3176  status=MagickFalse;
3177  break;
3178  }
3179  case 'o':
3180  case 'O':
3181  {
3182  if (LocaleCompare("offset",keyword) == 0)
3183  {
3184  (void) GetNextToken(q,&q,extent,token);
3185  break;
3186  }
3187  if (LocaleCompare("opacity",keyword) == 0)
3188  {
3189  double
3190  opacity;
3191 
3192  (void) GetNextToken(q,&q,extent,token);
3193  if (graphic_context[n]->clip_path != MagickFalse)
3194  break;
3195  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3196  opacity=MagickMin(MagickMax(factor*
3197  GetDrawValue(token,&next_token),0.0),1.0);
3198  if (token == next_token)
3199  ThrowPointExpectedException(token,exception);
3200  if (graphic_context[n]->compliance == SVGCompliance)
3201  {
3202  graphic_context[n]->fill_alpha*=opacity;
3203  graphic_context[n]->stroke_alpha*=opacity;
3204  }
3205  else
3206  {
3207  graphic_context[n]->fill_alpha=QuantumRange*opacity;
3208  graphic_context[n]->stroke_alpha=QuantumRange*opacity;
3209  }
3210  break;
3211  }
3212  status=MagickFalse;
3213  break;
3214  }
3215  case 'p':
3216  case 'P':
3217  {
3218  if (LocaleCompare("path",keyword) == 0)
3219  {
3220  primitive_type=PathPrimitive;
3221  break;
3222  }
3223  if (LocaleCompare("point",keyword) == 0)
3224  {
3225  primitive_type=PointPrimitive;
3226  break;
3227  }
3228  if (LocaleCompare("polyline",keyword) == 0)
3229  {
3230  primitive_type=PolylinePrimitive;
3231  break;
3232  }
3233  if (LocaleCompare("polygon",keyword) == 0)
3234  {
3235  primitive_type=PolygonPrimitive;
3236  break;
3237  }
3238  if (LocaleCompare("pop",keyword) == 0)
3239  {
3240  if (GetNextToken(q,&q,extent,token) < 1)
3241  break;
3242  if (LocaleCompare("class",token) == 0)
3243  break;
3244  if (LocaleCompare("clip-path",token) == 0)
3245  break;
3246  if (LocaleCompare("defs",token) == 0)
3247  {
3248  defsDepth--;
3249  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3250  MagickTrue;
3251  break;
3252  }
3253  if (LocaleCompare("gradient",token) == 0)
3254  break;
3255  if (LocaleCompare("graphic-context",token) == 0)
3256  {
3257  if (n <= 0)
3258  {
3259  (void) ThrowMagickException(exception,GetMagickModule(),
3260  DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3261  status=MagickFalse;
3262  n=0;
3263  break;
3264  }
3265  if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3266  (graphic_context[n]->compliance != SVGCompliance))
3267  if (LocaleCompare(graphic_context[n]->clip_mask,
3268  graphic_context[n-1]->clip_mask) != 0)
3269  status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3270  exception);
3271  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3272  n--;
3273  break;
3274  }
3275  if (LocaleCompare("mask",token) == 0)
3276  break;
3277  if (LocaleCompare("pattern",token) == 0)
3278  break;
3279  if (LocaleCompare("symbol",token) == 0)
3280  {
3281  symbolDepth--;
3282  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3283  MagickTrue;
3284  break;
3285  }
3286  status=MagickFalse;
3287  break;
3288  }
3289  if (LocaleCompare("push",keyword) == 0)
3290  {
3291  if (GetNextToken(q,&q,extent,token) < 1)
3292  break;
3293  if (LocaleCompare("class",token) == 0)
3294  {
3295  /*
3296  Class context.
3297  */
3298  for (p=q; *q != '\0'; )
3299  {
3300  if (GetNextToken(q,&q,extent,token) < 1)
3301  break;
3302  if (LocaleCompare(token,"pop") != 0)
3303  continue;
3304  (void) GetNextToken(q,(const char **) NULL,extent,token);
3305  if (LocaleCompare(token,"class") != 0)
3306  continue;
3307  break;
3308  }
3309  (void) GetNextToken(q,&q,extent,token);
3310  break;
3311  }
3312  if (LocaleCompare("clip-path",token) == 0)
3313  {
3314  (void) GetNextToken(q,&q,extent,token);
3315  for (p=q; *q != '\0'; )
3316  {
3317  if (GetNextToken(q,&q,extent,token) < 1)
3318  break;
3319  if (LocaleCompare(token,"pop") != 0)
3320  continue;
3321  (void) GetNextToken(q,(const char **) NULL,extent,token);
3322  if (LocaleCompare(token,"clip-path") != 0)
3323  continue;
3324  break;
3325  }
3326  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3327  {
3328  status=MagickFalse;
3329  break;
3330  }
3331  (void) GetNextToken(q,&q,extent,token);
3332  break;
3333  }
3334  if (LocaleCompare("defs",token) == 0)
3335  {
3336  defsDepth++;
3337  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3338  MagickTrue;
3339  break;
3340  }
3341  if (LocaleCompare("gradient",token) == 0)
3342  {
3343  char
3344  key[2*MagickPathExtent],
3345  name[MagickPathExtent],
3346  type[MagickPathExtent];
3347 
3348  SegmentInfo
3349  segment;
3350 
3351  (void) GetNextToken(q,&q,extent,token);
3352  (void) CopyMagickString(name,token,MagickPathExtent);
3353  (void) GetNextToken(q,&q,extent,token);
3354  (void) CopyMagickString(type,token,MagickPathExtent);
3355  (void) GetNextToken(q,&q,extent,token);
3356  segment.x1=GetDrawValue(token,&next_token);
3357  if (token == next_token)
3358  ThrowPointExpectedException(token,exception);
3359  (void) GetNextToken(q,&q,extent,token);
3360  if (*token == ',')
3361  (void) GetNextToken(q,&q,extent,token);
3362  segment.y1=GetDrawValue(token,&next_token);
3363  if (token == next_token)
3364  ThrowPointExpectedException(token,exception);
3365  (void) GetNextToken(q,&q,extent,token);
3366  if (*token == ',')
3367  (void) GetNextToken(q,&q,extent,token);
3368  segment.x2=GetDrawValue(token,&next_token);
3369  if (token == next_token)
3370  ThrowPointExpectedException(token,exception);
3371  (void) GetNextToken(q,&q,extent,token);
3372  if (*token == ',')
3373  (void) GetNextToken(q,&q,extent,token);
3374  segment.y2=GetDrawValue(token,&next_token);
3375  if (token == next_token)
3376  ThrowPointExpectedException(token,exception);
3377  if (LocaleCompare(type,"radial") == 0)
3378  {
3379  (void) GetNextToken(q,&q,extent,token);
3380  if (*token == ',')
3381  (void) GetNextToken(q,&q,extent,token);
3382  }
3383  for (p=q; *q != '\0'; )
3384  {
3385  if (GetNextToken(q,&q,extent,token) < 1)
3386  break;
3387  if (LocaleCompare(token,"pop") != 0)
3388  continue;
3389  (void) GetNextToken(q,(const char **) NULL,extent,token);
3390  if (LocaleCompare(token,"gradient") != 0)
3391  continue;
3392  break;
3393  }
3394  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3395  {
3396  status=MagickFalse;
3397  break;
3398  }
3399  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3400  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3401  graphic_context[n]->affine.ry*segment.y1+
3402  graphic_context[n]->affine.tx;
3403  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3404  graphic_context[n]->affine.sy*segment.y1+
3405  graphic_context[n]->affine.ty;
3406  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3407  graphic_context[n]->affine.ry*segment.y2+
3408  graphic_context[n]->affine.tx;
3409  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3410  graphic_context[n]->affine.sy*segment.y2+
3411  graphic_context[n]->affine.ty;
3412  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3413  (void) SetImageArtifact(image,key,token);
3414  (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3415  (void) SetImageArtifact(image,key,type);
3416  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3417  name);
3418  (void) FormatLocaleString(geometry,MagickPathExtent,
3419  "%gx%g%+.15g%+.15g",
3420  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3421  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3422  bounds.x1,bounds.y1);
3423  (void) SetImageArtifact(image,key,geometry);
3424  (void) GetNextToken(q,&q,extent,token);
3425  break;
3426  }
3427  if (LocaleCompare("graphic-context",token) == 0)
3428  {
3429  n++;
3430  graphic_context=(DrawInfo **) ResizeQuantumMemory(
3431  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3432  if (graphic_context == (DrawInfo **) NULL)
3433  {
3434  (void) ThrowMagickException(exception,GetMagickModule(),
3435  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3436  image->filename);
3437  break;
3438  }
3439  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3440  graphic_context[n-1]);
3441  if (*q == '"')
3442  {
3443  (void) GetNextToken(q,&q,extent,token);
3444  (void) CloneString(&graphic_context[n]->id,token);
3445  }
3446  break;
3447  }
3448  if (LocaleCompare("mask",token) == 0)
3449  {
3450  (void) GetNextToken(q,&q,extent,token);
3451  break;
3452  }
3453  if (LocaleCompare("pattern",token) == 0)
3454  {
3455  char
3456  key[2*MagickPathExtent],
3457  name[MagickPathExtent];
3458 
3460  region;
3461 
3462  (void) GetNextToken(q,&q,extent,token);
3463  (void) CopyMagickString(name,token,MagickPathExtent);
3464  (void) GetNextToken(q,&q,extent,token);
3465  region.x=CastDoubleToLong(ceil(GetDrawValue(token,
3466  &next_token)-0.5));
3467  if (token == next_token)
3468  ThrowPointExpectedException(token,exception);
3469  (void) GetNextToken(q,&q,extent,token);
3470  if (*token == ',')
3471  (void) GetNextToken(q,&q,extent,token);
3472  region.y=CastDoubleToLong(ceil(GetDrawValue(token,
3473  &next_token)-0.5));
3474  if (token == next_token)
3475  ThrowPointExpectedException(token,exception);
3476  (void) GetNextToken(q,&q,extent,token);
3477  if (*token == ',')
3478  (void) GetNextToken(q,&q,extent,token);
3479  region.width=(size_t) CastDoubleToLong(floor(GetDrawValue(
3480  token,&next_token)+0.5));
3481  if (token == next_token)
3482  ThrowPointExpectedException(token,exception);
3483  (void) GetNextToken(q,&q,extent,token);
3484  if (*token == ',')
3485  (void) GetNextToken(q,&q,extent,token);
3486  region.height=(size_t) floor(GetDrawValue(token,&next_token)+
3487  0.5);
3488  if (token == next_token)
3489  ThrowPointExpectedException(token,exception);
3490  for (p=q; *q != '\0'; )
3491  {
3492  if (GetNextToken(q,&q,extent,token) < 1)
3493  break;
3494  if (LocaleCompare(token,"pop") != 0)
3495  continue;
3496  (void) GetNextToken(q,(const char **) NULL,extent,token);
3497  if (LocaleCompare(token,"pattern") != 0)
3498  continue;
3499  break;
3500  }
3501  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3502  {
3503  status=MagickFalse;
3504  break;
3505  }
3506  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3507  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3508  (void) SetImageArtifact(image,key,token);
3509  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3510  name);
3511  (void) FormatLocaleString(geometry,MagickPathExtent,
3512  "%.20gx%.20g%+.20g%+.20g",(double) region.width,(double)
3513  region.height,(double) region.x,(double) region.y);
3514  (void) SetImageArtifact(image,key,geometry);
3515  (void) GetNextToken(q,&q,extent,token);
3516  break;
3517  }
3518  if (LocaleCompare("symbol",token) == 0)
3519  {
3520  symbolDepth++;
3521  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3522  MagickTrue;
3523  break;
3524  }
3525  status=MagickFalse;
3526  break;
3527  }
3528  status=MagickFalse;
3529  break;
3530  }
3531  case 'r':
3532  case 'R':
3533  {
3534  if (LocaleCompare("rectangle",keyword) == 0)
3535  {
3536  primitive_type=RectanglePrimitive;
3537  break;
3538  }
3539  if (LocaleCompare("rotate",keyword) == 0)
3540  {
3541  (void) GetNextToken(q,&q,extent,token);
3542  angle=GetDrawValue(token,&next_token);
3543  if (token == next_token)
3544  ThrowPointExpectedException(token,exception);
3545  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3546  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3547  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3548  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3549  break;
3550  }
3551  if (LocaleCompare("roundRectangle",keyword) == 0)
3552  {
3553  primitive_type=RoundRectanglePrimitive;
3554  break;
3555  }
3556  status=MagickFalse;
3557  break;
3558  }
3559  case 's':
3560  case 'S':
3561  {
3562  if (LocaleCompare("scale",keyword) == 0)
3563  {
3564  (void) GetNextToken(q,&q,extent,token);
3565  affine.sx=GetDrawValue(token,&next_token);
3566  if (token == next_token)
3567  ThrowPointExpectedException(token,exception);
3568  (void) GetNextToken(q,&q,extent,token);
3569  if (*token == ',')
3570  (void) GetNextToken(q,&q,extent,token);
3571  affine.sy=GetDrawValue(token,&next_token);
3572  if (token == next_token)
3573  ThrowPointExpectedException(token,exception);
3574  break;
3575  }
3576  if (LocaleCompare("skewX",keyword) == 0)
3577  {
3578  (void) GetNextToken(q,&q,extent,token);
3579  angle=GetDrawValue(token,&next_token);
3580  if (token == next_token)
3581  ThrowPointExpectedException(token,exception);
3582  affine.ry=sin(DegreesToRadians(angle));
3583  break;
3584  }
3585  if (LocaleCompare("skewY",keyword) == 0)
3586  {
3587  (void) GetNextToken(q,&q,extent,token);
3588  angle=GetDrawValue(token,&next_token);
3589  if (token == next_token)
3590  ThrowPointExpectedException(token,exception);
3591  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3592  break;
3593  }
3594  if (LocaleCompare("stop-color",keyword) == 0)
3595  {
3596  PixelInfo
3597  stop_color;
3598 
3599  number_stops++;
3600  if (number_stops == 1)
3601  stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3602  else
3603  if (number_stops > 2)
3604  stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3605  sizeof(*stops));
3606  if (stops == (StopInfo *) NULL)
3607  {
3608  (void) ThrowMagickException(exception,GetMagickModule(),
3609  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3610  image->filename);
3611  break;
3612  }
3613  (void) GetNextToken(q,&q,extent,token);
3614  status&=QueryColorCompliance(token,AllCompliance,&stop_color,
3615  exception);
3616  stops[number_stops-1].color=stop_color;
3617  (void) GetNextToken(q,&q,extent,token);
3618  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3619  stops[number_stops-1].offset=factor*GetDrawValue(token,
3620  &next_token);
3621  if (token == next_token)
3622  ThrowPointExpectedException(token,exception);
3623  break;
3624  }
3625  if (LocaleCompare("stroke",keyword) == 0)
3626  {
3627  (void) GetNextToken(q,&q,extent,token);
3628  if (graphic_context[n]->clip_path != MagickFalse)
3629  break;
3630  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3631  if (GetImageArtifact(image,pattern) != (const char *) NULL)
3632  (void) DrawPatternPath(image,draw_info,token,
3633  &graphic_context[n]->stroke_pattern,exception);
3634  else
3635  {
3636  status&=QueryColorCompliance(token,AllCompliance,
3637  &graphic_context[n]->stroke,exception);
3638  if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3639  graphic_context[n]->stroke.alpha=
3640  graphic_context[n]->stroke_alpha;
3641  }
3642  break;
3643  }
3644  if (LocaleCompare("stroke-antialias",keyword) == 0)
3645  {
3646  (void) GetNextToken(q,&q,extent,token);
3647  graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3649  break;
3650  }
3651  if (LocaleCompare("stroke-dasharray",keyword) == 0)
3652  {
3653  if (graphic_context[n]->dash_pattern != (double *) NULL)
3654  graphic_context[n]->dash_pattern=(double *)
3655  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3656  if (IsPoint(q) != MagickFalse)
3657  {
3658  const char
3659  *r;
3660 
3661  r=q;
3662  (void) GetNextToken(r,&r,extent,token);
3663  if (*token == ',')
3664  (void) GetNextToken(r,&r,extent,token);
3665  for (x=0; IsPoint(token) != MagickFalse; x++)
3666  {
3667  (void) GetNextToken(r,&r,extent,token);
3668  if (*token == ',')
3669  (void) GetNextToken(r,&r,extent,token);
3670  }
3671  graphic_context[n]->dash_pattern=(double *)
3672  AcquireQuantumMemory((size_t) (2*x+2),
3673  sizeof(*graphic_context[n]->dash_pattern));
3674  if (graphic_context[n]->dash_pattern == (double *) NULL)
3675  {
3676  (void) ThrowMagickException(exception,GetMagickModule(),
3677  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3678  image->filename);
3679  status=MagickFalse;
3680  break;
3681  }
3682  (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3683  (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3684  for (j=0; j < x; j++)
3685  {
3686  (void) GetNextToken(q,&q,extent,token);
3687  if (*token == ',')
3688  (void) GetNextToken(q,&q,extent,token);
3689  graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3690  &next_token);
3691  if (token == next_token)
3692  ThrowPointExpectedException(token,exception);
3693  if (graphic_context[n]->dash_pattern[j] < 0.0)
3694  status=MagickFalse;
3695  }
3696  if ((x & 0x01) != 0)
3697  for ( ; j < (2*x); j++)
3698  graphic_context[n]->dash_pattern[j]=
3699  graphic_context[n]->dash_pattern[j-x];
3700  graphic_context[n]->dash_pattern[j]=0.0;
3701  break;
3702  }
3703  (void) GetNextToken(q,&q,extent,token);
3704  break;
3705  }
3706  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3707  {
3708  (void) GetNextToken(q,&q,extent,token);
3709  graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3710  if (token == next_token)
3711  ThrowPointExpectedException(token,exception);
3712  break;
3713  }
3714  if (LocaleCompare("stroke-linecap",keyword) == 0)
3715  {
3716  ssize_t
3717  linecap;
3718 
3719  (void) GetNextToken(q,&q,extent,token);
3721  if (linecap == -1)
3722  {
3723  status=MagickFalse;
3724  break;
3725  }
3726  graphic_context[n]->linecap=(LineCap) linecap;
3727  break;
3728  }
3729  if (LocaleCompare("stroke-linejoin",keyword) == 0)
3730  {
3731  ssize_t
3732  linejoin;
3733 
3734  (void) GetNextToken(q,&q,extent,token);
3736  token);
3737  if (linejoin == -1)
3738  {
3739  status=MagickFalse;
3740  break;
3741  }
3742  graphic_context[n]->linejoin=(LineJoin) linejoin;
3743  break;
3744  }
3745  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3746  {
3747  (void) GetNextToken(q,&q,extent,token);
3748  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3749  break;
3750  }
3751  if (LocaleCompare("stroke-opacity",keyword) == 0)
3752  {
3753  double
3754  opacity;
3755 
3756  (void) GetNextToken(q,&q,extent,token);
3757  if (graphic_context[n]->clip_path != MagickFalse)
3758  break;
3759  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3760  opacity=MagickMin(MagickMax(factor*
3761  GetDrawValue(token,&next_token),0.0),1.0);
3762  if (token == next_token)
3763  ThrowPointExpectedException(token,exception);
3764  if (graphic_context[n]->compliance == SVGCompliance)
3765  graphic_context[n]->stroke_alpha*=opacity;
3766  else
3767  graphic_context[n]->stroke_alpha=QuantumRange*opacity;
3768  if (graphic_context[n]->stroke.alpha != TransparentAlpha)
3769  graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3770  else
3771  graphic_context[n]->stroke.alpha=(MagickRealType)
3772  ClampToQuantum(QuantumRange*(1.0-opacity));
3773  break;
3774  }
3775  if (LocaleCompare("stroke-width",keyword) == 0)
3776  {
3777  (void) GetNextToken(q,&q,extent,token);
3778  if (graphic_context[n]->clip_path != MagickFalse)
3779  break;
3780  graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3781  if (token == next_token)
3782  ThrowPointExpectedException(token,exception);
3783  break;
3784  }
3785  status=MagickFalse;
3786  break;
3787  }
3788  case 't':
3789  case 'T':
3790  {
3791  if (LocaleCompare("text",keyword) == 0)
3792  {
3793  primitive_type=TextPrimitive;
3794  cursor=0.0;
3795  break;
3796  }
3797  if (LocaleCompare("text-align",keyword) == 0)
3798  {
3799  ssize_t
3800  align;
3801 
3802  (void) GetNextToken(q,&q,extent,token);
3804  if (align == -1)
3805  {
3806  status=MagickFalse;
3807  break;
3808  }
3809  graphic_context[n]->align=(AlignType) align;
3810  break;
3811  }
3812  if (LocaleCompare("text-anchor",keyword) == 0)
3813  {
3814  ssize_t
3815  align;
3816 
3817  (void) GetNextToken(q,&q,extent,token);
3819  if (align == -1)
3820  {
3821  status=MagickFalse;
3822  break;
3823  }
3824  graphic_context[n]->align=(AlignType) align;
3825  break;
3826  }
3827  if (LocaleCompare("text-antialias",keyword) == 0)
3828  {
3829  (void) GetNextToken(q,&q,extent,token);
3830  graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3832  break;
3833  }
3834  if (LocaleCompare("text-undercolor",keyword) == 0)
3835  {
3836  (void) GetNextToken(q,&q,extent,token);
3837  status&=QueryColorCompliance(token,AllCompliance,
3838  &graphic_context[n]->undercolor,exception);
3839  break;
3840  }
3841  if (LocaleCompare("translate",keyword) == 0)
3842  {
3843  (void) GetNextToken(q,&q,extent,token);
3844  affine.tx=GetDrawValue(token,&next_token);
3845  if (token == next_token)
3846  ThrowPointExpectedException(token,exception);
3847  (void) GetNextToken(q,&q,extent,token);
3848  if (*token == ',')
3849  (void) GetNextToken(q,&q,extent,token);
3850  affine.ty=GetDrawValue(token,&next_token);
3851  if (token == next_token)
3852  ThrowPointExpectedException(token,exception);
3853  cursor=0.0;
3854  break;
3855  }
3856  status=MagickFalse;
3857  break;
3858  }
3859  case 'u':
3860  case 'U':
3861  {
3862  if (LocaleCompare("use",keyword) == 0)
3863  {
3864  const char
3865  *use;
3866 
3867  /*
3868  Get a macro from the MVG document, and "use" it here.
3869  */
3870  (void) GetNextToken(q,&q,extent,token);
3871  use=(const char *) GetValueFromSplayTree(macros,token);
3872  if (use != (const char *) NULL)
3873  {
3874  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3875  (void) CloneString(&clone_info->primitive,use);
3876  status=RenderMVGContent(image,clone_info,depth+1,exception);
3877  clone_info=DestroyDrawInfo(clone_info);
3878  }
3879  break;
3880  }
3881  status=MagickFalse;
3882  break;
3883  }
3884  case 'v':
3885  case 'V':
3886  {
3887  if (LocaleCompare("viewbox",keyword) == 0)
3888  {
3889  (void) GetNextToken(q,&q,extent,token);
3890  graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3891  GetDrawValue(token,&next_token)-0.5));
3892  if (token == next_token)
3893  ThrowPointExpectedException(token,exception);
3894  (void) GetNextToken(q,&q,extent,token);
3895  if (*token == ',')
3896  (void) GetNextToken(q,&q,extent,token);
3897  graphic_context[n]->viewbox.y=CastDoubleToLong(ceil(
3898  GetDrawValue(token,&next_token)-0.5));
3899  if (token == next_token)
3900  ThrowPointExpectedException(token,exception);
3901  (void) GetNextToken(q,&q,extent,token);
3902  if (*token == ',')
3903  (void) GetNextToken(q,&q,extent,token);
3904  graphic_context[n]->viewbox.width=(size_t) CastDoubleToLong(
3905  floor(GetDrawValue(token,&next_token)+0.5));
3906  if (token == next_token)
3907  ThrowPointExpectedException(token,exception);
3908  (void) GetNextToken(q,&q,extent,token);
3909  if (*token == ',')
3910  (void) GetNextToken(q,&q,extent,token);
3911  graphic_context[n]->viewbox.height=(size_t) CastDoubleToLong(
3912  floor(GetDrawValue(token,&next_token)+0.5));
3913  if (token == next_token)
3914  ThrowPointExpectedException(token,exception);
3915  break;
3916  }
3917  status=MagickFalse;
3918  break;
3919  }
3920  case 'w':
3921  case 'W':
3922  {
3923  if (LocaleCompare("word-spacing",keyword) == 0)
3924  {
3925  (void) GetNextToken(q,&q,extent,token);
3926  graphic_context[n]->interword_spacing=GetDrawValue(token,
3927  &next_token);
3928  if (token == next_token)
3929  ThrowPointExpectedException(token,exception);
3930  break;
3931  }
3932  status=MagickFalse;
3933  break;
3934  }
3935  default:
3936  {
3937  status=MagickFalse;
3938  break;
3939  }
3940  }
3941  if (status == MagickFalse)
3942  break;
3943  if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3944  (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3945  (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3946  (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3947  {
3948  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3949  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3950  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3951  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3952  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3953  current.tx;
3954  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3955  current.ty;
3956  }
3957  if (primitive_type == UndefinedPrimitive)
3958  {
3959  if (*q == '\0')
3960  {
3961  if (number_stops > 1)
3962  {
3963  GradientType
3964  type;
3965 
3966  type=LinearGradient;
3967  if (draw_info->gradient.type == RadialGradient)
3968  type=RadialGradient;
3969  (void) GradientImage(image,type,PadSpread,stops,number_stops,
3970  exception);
3971  }
3972  if (number_stops > 0)
3973  stops=(StopInfo *) RelinquishMagickMemory(stops);
3974  }
3975  if ((image->debug != MagickFalse) && (q > p))
3976  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3977  (q-p-1),p);
3978  continue;
3979  }
3980  /*
3981  Parse the primitive attributes.
3982  */
3983  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3984  if ((primitive_info[i].primitive == TextPrimitive) ||
3985  (primitive_info[i].primitive == ImagePrimitive))
3986  if (primitive_info[i].text != (char *) NULL)
3987  primitive_info[i].text=DestroyString(primitive_info[i].text);
3988  i=0;
3989  mvg_info.offset=i;
3990  j=0;
3991  primitive_info[0].point.x=0.0;
3992  primitive_info[0].point.y=0.0;
3993  primitive_info[0].coordinates=0;
3994  primitive_info[0].method=FloodfillMethod;
3995  primitive_info[0].closed_subpath=MagickFalse;
3996  for (x=0; *q != '\0'; x++)
3997  {
3998  /*
3999  Define points.
4000  */
4001  if (IsPoint(q) == MagickFalse)
4002  break;
4003  (void) GetNextToken(q,&q,extent,token);
4004  point.x=GetDrawValue(token,&next_token);
4005  if (token == next_token)
4006  ThrowPointExpectedException(token,exception);
4007  (void) GetNextToken(q,&q,extent,token);
4008  if (*token == ',')
4009  (void) GetNextToken(q,&q,extent,token);
4010  point.y=GetDrawValue(token,&next_token);
4011  if (token == next_token)
4012  ThrowPointExpectedException(token,exception);
4013  (void) GetNextToken(q,(const char **) NULL,extent,token);
4014  if (*token == ',')
4015  (void) GetNextToken(q,&q,extent,token);
4016  primitive_info[i].primitive=primitive_type;
4017  primitive_info[i].point=point;
4018  primitive_info[i].coordinates=0;
4019  primitive_info[i].method=FloodfillMethod;
4020  primitive_info[i].closed_subpath=MagickFalse;
4021  i++;
4022  mvg_info.offset=i;
4023  if (i < (ssize_t) number_points)
4024  continue;
4025  status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4026  }
4027  if (status == MagickFalse)
4028  break;
4029  if ((primitive_info[j].primitive == TextPrimitive) ||
4030  (primitive_info[j].primitive == ImagePrimitive))
4031  if (primitive_info[j].text != (char *) NULL)
4032  primitive_info[j].text=DestroyString(primitive_info[j].text);
4033  primitive_info[j].primitive=primitive_type;
4034  primitive_info[j].coordinates=(size_t) x;
4035  primitive_info[j].method=FloodfillMethod;
4036  primitive_info[j].closed_subpath=MagickFalse;
4037  /*
4038  Circumscribe primitive within a circle.
4039  */
4040  bounds.x1=primitive_info[j].point.x;
4041  bounds.y1=primitive_info[j].point.y;
4042  bounds.x2=primitive_info[j].point.x;
4043  bounds.y2=primitive_info[j].point.y;
4044  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4045  {
4046  point=primitive_info[j+k].point;
4047  if (point.x < bounds.x1)
4048  bounds.x1=point.x;
4049  if (point.y < bounds.y1)
4050  bounds.y1=point.y;
4051  if (point.x > bounds.x2)
4052  bounds.x2=point.x;
4053  if (point.y > bounds.y2)
4054  bounds.y2=point.y;
4055  }
4056  /*
4057  Speculate how many points our primitive might consume.
4058  */
4059  coordinates=(double) primitive_info[j].coordinates;
4060  switch (primitive_type)
4061  {
4062  case RectanglePrimitive:
4063  {
4064  coordinates*=5.0;
4065  break;
4066  }
4068  {
4069  double
4070  alpha,
4071  beta,
4072  radius;
4073 
4074  alpha=bounds.x2-bounds.x1;
4075  beta=bounds.y2-bounds.y1;
4076  radius=hypot(alpha,beta);
4077  coordinates*=5.0;
4078  coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4079  BezierQuantum+360.0;
4080  break;
4081  }
4082  case BezierPrimitive:
4083  {
4084  coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4085  break;
4086  }
4087  case PathPrimitive:
4088  {
4089  char
4090  *s,
4091  *t;
4092 
4093  (void) GetNextToken(q,&q,extent,token);
4094  coordinates=1.0;
4095  t=token;
4096  for (s=token; *s != '\0'; s=t)
4097  {
4098  double
4099  value;
4100 
4101  value=GetDrawValue(s,&t);
4102  (void) value;
4103  if (s == t)
4104  {
4105  t++;
4106  continue;
4107  }
4108  coordinates++;
4109  }
4110  for (s=token; *s != '\0'; s++)
4111  if (strspn(s,"AaCcQqSsTt") != 0)
4112  coordinates+=(20.0*BezierQuantum)+360.0;
4113  break;
4114  }
4115  default:
4116  break;
4117  }
4118  if (status == MagickFalse)
4119  break;
4120  if (((size_t) (i+coordinates)) >= number_points)
4121  {
4122  /*
4123  Resize based on speculative points required by primitive.
4124  */
4125  number_points+=coordinates+1;
4126  if (number_points < (size_t) coordinates)
4127  {
4128  (void) ThrowMagickException(exception,GetMagickModule(),
4129  ResourceLimitError,"MemoryAllocationFailed","`%s'",
4130  image->filename);
4131  break;
4132  }
4133  mvg_info.offset=i;
4134  status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4135  }
4136  status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4137  if (status == MagickFalse)
4138  break;
4139  mvg_info.offset=j;
4140  switch (primitive_type)
4141  {
4142  case PointPrimitive:
4143  default:
4144  {
4145  if (primitive_info[j].coordinates != 1)
4146  {
4147  status=MagickFalse;
4148  break;
4149  }
4150  status&=TracePoint(primitive_info+j,primitive_info[j].point);
4151  i=(ssize_t) (j+primitive_info[j].coordinates);
4152  break;
4153  }
4154  case LinePrimitive:
4155  {
4156  double
4157  dx,
4158  dy,
4159  maximum_length;
4160 
4161  if (primitive_info[j].coordinates != 2)
4162  {
4163  status=MagickFalse;
4164  break;
4165  }
4166  dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
4167  dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
4168  maximum_length=hypot(dx,dy);
4169  if (maximum_length > (MaxBezierCoordinates/100.0))
4170  ThrowPointExpectedException(keyword,exception);
4171  status&=TraceLine(primitive_info+j,primitive_info[j].point,
4172  primitive_info[j+1].point);
4173  i=(ssize_t) (j+primitive_info[j].coordinates);
4174  break;
4175  }
4176  case RectanglePrimitive:
4177  {
4178  if (primitive_info[j].coordinates != 2)
4179  {
4180  status=MagickFalse;
4181  break;
4182  }
4183  status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4184  primitive_info[j+1].point);
4185  i=(ssize_t) (j+primitive_info[j].coordinates);
4186  break;
4187  }
4189  {
4190  if (primitive_info[j].coordinates != 3)
4191  {
4192  status=MagickFalse;
4193  break;
4194  }
4195  if ((primitive_info[j+2].point.x < 0.0) ||
4196  (primitive_info[j+2].point.y < 0.0))
4197  {
4198  status=MagickFalse;
4199  break;
4200  }
4201  if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4202  {
4203  status=MagickFalse;
4204  break;
4205  }
4206  if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4207  {
4208  status=MagickFalse;
4209  break;
4210  }
4211  status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4212  primitive_info[j+1].point,primitive_info[j+2].point);
4213  i=(ssize_t) (j+primitive_info[j].coordinates);
4214  break;
4215  }
4216  case ArcPrimitive:
4217  {
4218  if (primitive_info[j].coordinates != 3)
4219  {
4220  status=MagickFalse;
4221  break;
4222  }
4223  status&=TraceArc(&mvg_info,primitive_info[j].point,
4224  primitive_info[j+1].point,primitive_info[j+2].point);
4225  i=(ssize_t) (j+primitive_info[j].coordinates);
4226  break;
4227  }
4228  case EllipsePrimitive:
4229  {
4230  if (primitive_info[j].coordinates != 3)
4231  {
4232  status=MagickFalse;
4233  break;
4234  }
4235  if ((primitive_info[j+1].point.x < 0.0) ||
4236  (primitive_info[j+1].point.y < 0.0))
4237  {
4238  status=MagickFalse;
4239  break;
4240  }
4241  status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4242  primitive_info[j+1].point,primitive_info[j+2].point);
4243  i=(ssize_t) (j+primitive_info[j].coordinates);
4244  break;
4245  }
4246  case CirclePrimitive:
4247  {
4248  if (primitive_info[j].coordinates != 2)
4249  {
4250  status=MagickFalse;
4251  break;
4252  }
4253  status&=TraceCircle(&mvg_info,primitive_info[j].point,
4254  primitive_info[j+1].point);
4255  i=(ssize_t) (j+primitive_info[j].coordinates);
4256  break;
4257  }
4258  case PolylinePrimitive:
4259  {
4260  if (primitive_info[j].coordinates < 1)
4261  {
4262  status=MagickFalse;
4263  break;
4264  }
4265  break;
4266  }
4267  case PolygonPrimitive:
4268  {
4269  if (primitive_info[j].coordinates < 3)
4270  {
4271  status=MagickFalse;
4272  break;
4273  }
4274  primitive_info[i]=primitive_info[j];
4275  primitive_info[i].coordinates=0;
4276  primitive_info[j].coordinates++;
4277  primitive_info[j].closed_subpath=MagickTrue;
4278  i++;
4279  break;
4280  }
4281  case BezierPrimitive:
4282  {
4283  if (primitive_info[j].coordinates < 3)
4284  {
4285  status=MagickFalse;
4286  break;
4287  }
4288  status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4289  i=(ssize_t) (j+primitive_info[j].coordinates);
4290  break;
4291  }
4292  case PathPrimitive:
4293  {
4294  coordinates=(double) TracePath(&mvg_info,token,exception);
4295  if (coordinates < 0.0)
4296  {
4297  status=MagickFalse;
4298  break;
4299  }
4300  i=(ssize_t) (j+coordinates);
4301  break;
4302  }
4303  case AlphaPrimitive:
4304  case ColorPrimitive:
4305  {
4306  ssize_t
4307  method;
4308 
4309  if (primitive_info[j].coordinates != 1)
4310  {
4311  status=MagickFalse;
4312  break;
4313  }
4314  (void) GetNextToken(q,&q,extent,token);
4316  if (method == -1)
4317  {
4318  status=MagickFalse;
4319  break;
4320  }
4321  primitive_info[j].method=(PaintMethod) method;
4322  break;
4323  }
4324  case TextPrimitive:
4325  {
4326  if (primitive_info[j].coordinates != 1)
4327  {
4328  status=MagickFalse;
4329  break;
4330  }
4331  if (*token != ',')
4332  (void) GetNextToken(q,&q,extent,token);
4333  (void) CloneString(&primitive_info[j].text,token);
4334  /*
4335  Compute text cursor offset.
4336  */
4337  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4338  if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4339  (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4340  {
4341  mvg_info.point=primitive_info->point;
4342  primitive_info->point.x+=cursor;
4343  }
4344  else
4345  {
4346  mvg_info.point=primitive_info->point;
4347  cursor=0.0;
4348  }
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)
5441  {
5442  q+=