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