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