MagickCore  6.9.3
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-2016 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  *magick_restrict indexes;
1181 
1182  register ssize_t
1183  x;
1184 
1185  register PixelPacket
1186  *magick_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  ssize_t
2102  weight;
2103 
2104  GetMagickToken(q,&q,token);
2106  token);
2107  if (weight == -1)
2108  weight=StringToUnsignedLong(token);
2109  graphic_context[n]->weight=(size_t) weight;
2110  break;
2111  }
2112  status=MagickFalse;
2113  break;
2114  }
2115  case 'g':
2116  case 'G':
2117  {
2118  if (LocaleCompare("gradient-units",keyword) == 0)
2119  {
2120  GetMagickToken(q,&q,token);
2121  break;
2122  }
2123  if (LocaleCompare("gravity",keyword) == 0)
2124  {
2125  ssize_t
2126  gravity;
2127 
2128  GetMagickToken(q,&q,token);
2130  if (gravity == -1)
2131  {
2132  status=MagickFalse;
2133  break;
2134  }
2135  graphic_context[n]->gravity=(GravityType) gravity;
2136  break;
2137  }
2138  status=MagickFalse;
2139  break;
2140  }
2141  case 'i':
2142  case 'I':
2143  {
2144  if (LocaleCompare("image",keyword) == 0)
2145  {
2146  ssize_t
2147  compose;
2148 
2149  primitive_type=ImagePrimitive;
2150  GetMagickToken(q,&q,token);
2152  if (compose == -1)
2153  {
2154  status=MagickFalse;
2155  break;
2156  }
2157  graphic_context[n]->compose=(CompositeOperator) compose;
2158  break;
2159  }
2160  if (LocaleCompare("interline-spacing",keyword) == 0)
2161  {
2162  GetMagickToken(q,&q,token);
2163  graphic_context[n]->interline_spacing=StringToDouble(token,
2164  (char **) NULL);
2165  break;
2166  }
2167  if (LocaleCompare("interword-spacing",keyword) == 0)
2168  {
2169  GetMagickToken(q,&q,token);
2170  graphic_context[n]->interword_spacing=StringToDouble(token,
2171  (char **) NULL);
2172  break;
2173  }
2174  status=MagickFalse;
2175  break;
2176  }
2177  case 'k':
2178  case 'K':
2179  {
2180  if (LocaleCompare("kerning",keyword) == 0)
2181  {
2182  GetMagickToken(q,&q,token);
2183  graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2184  break;
2185  }
2186  status=MagickFalse;
2187  break;
2188  }
2189  case 'l':
2190  case 'L':
2191  {
2192  if (LocaleCompare("line",keyword) == 0)
2193  {
2194  primitive_type=LinePrimitive;
2195  break;
2196  }
2197  status=MagickFalse;
2198  break;
2199  }
2200  case 'm':
2201  case 'M':
2202  {
2203  if (LocaleCompare("matte",keyword) == 0)
2204  {
2205  primitive_type=MattePrimitive;
2206  break;
2207  }
2208  status=MagickFalse;
2209  break;
2210  }
2211  case 'o':
2212  case 'O':
2213  {
2214  if (LocaleCompare("offset",keyword) == 0)
2215  {
2216  GetMagickToken(q,&q,token);
2217  break;
2218  }
2219  if (LocaleCompare("opacity",keyword) == 0)
2220  {
2221  GetMagickToken(q,&q,token);
2222  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2223  graphic_context[n]->opacity=ClampToQuantum((MagickRealType)
2224  QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->opacity)*
2225  factor*StringToDouble(token,(char **) NULL))));
2226  graphic_context[n]->fill.opacity=graphic_context[n]->opacity;
2227  graphic_context[n]->stroke.opacity=graphic_context[n]->opacity;
2228  break;
2229  }
2230  status=MagickFalse;
2231  break;
2232  }
2233  case 'p':
2234  case 'P':
2235  {
2236  if (LocaleCompare("path",keyword) == 0)
2237  {
2238  primitive_type=PathPrimitive;
2239  break;
2240  }
2241  if (LocaleCompare("point",keyword) == 0)
2242  {
2243  primitive_type=PointPrimitive;
2244  break;
2245  }
2246  if (LocaleCompare("polyline",keyword) == 0)
2247  {
2248  primitive_type=PolylinePrimitive;
2249  break;
2250  }
2251  if (LocaleCompare("polygon",keyword) == 0)
2252  {
2253  primitive_type=PolygonPrimitive;
2254  break;
2255  }
2256  if (LocaleCompare("pop",keyword) == 0)
2257  {
2258  GetMagickToken(q,&q,token);
2259  if (LocaleCompare("clip-path",token) == 0)
2260  break;
2261  if (LocaleCompare("defs",token) == 0)
2262  break;
2263  if (LocaleCompare("gradient",token) == 0)
2264  break;
2265  if (LocaleCompare("graphic-context",token) == 0)
2266  {
2267  if (n <= 0)
2268  {
2269  (void) ThrowMagickException(&image->exception,
2271  "UnbalancedGraphicContextPushPop","`%s'",token);
2272  n=0;
2273  break;
2274  }
2275  if (graphic_context[n]->clip_mask != (char *) NULL)
2276  if (LocaleCompare(graphic_context[n]->clip_mask,
2277  graphic_context[n-1]->clip_mask) != 0)
2278  (void) SetImageClipMask(image,(Image *) NULL);
2279  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2280  n--;
2281  break;
2282  }
2283  if (LocaleCompare("pattern",token) == 0)
2284  break;
2285  status=MagickFalse;
2286  break;
2287  }
2288  if (LocaleCompare("push",keyword) == 0)
2289  {
2290  GetMagickToken(q,&q,token);
2291  if (LocaleCompare("clip-path",token) == 0)
2292  {
2293  char
2294  name[MaxTextExtent];
2295 
2296  GetMagickToken(q,&q,token);
2297  (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2298  for (p=q; *q != '\0'; )
2299  {
2300  GetMagickToken(q,&q,token);
2301  if (LocaleCompare(token,"pop") != 0)
2302  continue;
2303  GetMagickToken(q,(const char **) NULL,token);
2304  if (LocaleCompare(token,"clip-path") != 0)
2305  continue;
2306  break;
2307  }
2308  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2309  (void) SetImageArtifact(image,name,token);
2310  GetMagickToken(q,&q,token);
2311  break;
2312  }
2313  if (LocaleCompare("gradient",token) == 0)
2314  {
2315  char
2316  key[2*MaxTextExtent],
2317  name[MaxTextExtent],
2318  type[MaxTextExtent];
2319 
2320  SegmentInfo
2321  segment;
2322 
2323  GetMagickToken(q,&q,token);
2324  (void) CopyMagickString(name,token,MaxTextExtent);
2325  GetMagickToken(q,&q,token);
2326  (void) CopyMagickString(type,token,MaxTextExtent);
2327  GetMagickToken(q,&q,token);
2328  segment.x1=StringToDouble(token,(char **) NULL);
2329  GetMagickToken(q,&q,token);
2330  if (*token == ',')
2331  GetMagickToken(q,&q,token);
2332  segment.y1=StringToDouble(token,(char **) NULL);
2333  GetMagickToken(q,&q,token);
2334  if (*token == ',')
2335  GetMagickToken(q,&q,token);
2336  segment.x2=StringToDouble(token,(char **) NULL);
2337  GetMagickToken(q,&q,token);
2338  if (*token == ',')
2339  GetMagickToken(q,&q,token);
2340  segment.y2=StringToDouble(token,(char **) NULL);
2341  if (LocaleCompare(type,"radial") == 0)
2342  {
2343  GetMagickToken(q,&q,token);
2344  if (*token == ',')
2345  GetMagickToken(q,&q,token);
2346  }
2347  for (p=q; *q != '\0'; )
2348  {
2349  GetMagickToken(q,&q,token);
2350  if (LocaleCompare(token,"pop") != 0)
2351  continue;
2352  GetMagickToken(q,(const char **) NULL,token);
2353  if (LocaleCompare(token,"gradient") != 0)
2354  continue;
2355  break;
2356  }
2357  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2358  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2359  graphic_context[n]->affine.ry*segment.y1+
2360  graphic_context[n]->affine.tx;
2361  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2362  graphic_context[n]->affine.sy*segment.y1+
2363  graphic_context[n]->affine.ty;
2364  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2365  graphic_context[n]->affine.ry*segment.y2+
2366  graphic_context[n]->affine.tx;
2367  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2368  graphic_context[n]->affine.sy*segment.y2+
2369  graphic_context[n]->affine.ty;
2370  (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2371  (void) SetImageArtifact(image,key,token);
2372  (void) FormatLocaleString(key,MaxTextExtent,"%s-type",name);
2373  (void) SetImageArtifact(image,key,type);
2374  (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2375  (void) FormatLocaleString(geometry,MaxTextExtent,
2376  "%gx%g%+.15g%+.15g",
2377  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2378  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2379  bounds.x1,bounds.y1);
2380  (void) SetImageArtifact(image,key,geometry);
2381  GetMagickToken(q,&q,token);
2382  break;
2383  }
2384  if (LocaleCompare("pattern",token) == 0)
2385  {
2387  bounds;
2388 
2389  GetMagickToken(q,&q,token);
2390  (void) CopyMagickString(name,token,MaxTextExtent);
2391  GetMagickToken(q,&q,token);
2392  bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2393  0.5);
2394  GetMagickToken(q,&q,token);
2395  if (*token == ',')
2396  GetMagickToken(q,&q,token);
2397  bounds.y=(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.width=(size_t) floor(StringToDouble(token,
2403  (char **) NULL)+0.5);
2404  GetMagickToken(q,&q,token);
2405  if (*token == ',')
2406  GetMagickToken(q,&q,token);
2407  bounds.height=(size_t) floor(StringToDouble(token,
2408  (char **) NULL)+0.5);
2409  for (p=q; *q != '\0'; )
2410  {
2411  GetMagickToken(q,&q,token);
2412  if (LocaleCompare(token,"pop") != 0)
2413  continue;
2414  GetMagickToken(q,(const char **) NULL,token);
2415  if (LocaleCompare(token,"pattern") != 0)
2416  continue;
2417  break;
2418  }
2419  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2420  (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2421  (void) SetImageArtifact(image,key,token);
2422  (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2423  (void) FormatLocaleString(geometry,MaxTextExtent,
2424  "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2425  bounds.height,(double) bounds.x,(double) bounds.y);
2426  (void) SetImageArtifact(image,key,geometry);
2427  GetMagickToken(q,&q,token);
2428  break;
2429  }
2430  if (LocaleCompare("graphic-context",token) == 0)
2431  {
2432  n++;
2433  graphic_context=(DrawInfo **) ResizeQuantumMemory(
2434  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2435  if (graphic_context == (DrawInfo **) NULL)
2436  {
2437  (void) ThrowMagickException(&image->exception,
2439  "MemoryAllocationFailed","`%s'",image->filename);
2440  break;
2441  }
2442  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2443  graphic_context[n-1]);
2444  break;
2445  }
2446  if (LocaleCompare("defs",token) == 0)
2447  break;
2448  status=MagickFalse;
2449  break;
2450  }
2451  status=MagickFalse;
2452  break;
2453  }
2454  case 'r':
2455  case 'R':
2456  {
2457  if (LocaleCompare("rectangle",keyword) == 0)
2458  {
2459  primitive_type=RectanglePrimitive;
2460  break;
2461  }
2462  if (LocaleCompare("rotate",keyword) == 0)
2463  {
2464  GetMagickToken(q,&q,token);
2465  angle=StringToDouble(token,(char **) NULL);
2466  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2467  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2468  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2469  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2470  break;
2471  }
2472  if (LocaleCompare("roundRectangle",keyword) == 0)
2473  {
2474  primitive_type=RoundRectanglePrimitive;
2475  break;
2476  }
2477  status=MagickFalse;
2478  break;
2479  }
2480  case 's':
2481  case 'S':
2482  {
2483  if (LocaleCompare("scale",keyword) == 0)
2484  {
2485  GetMagickToken(q,&q,token);
2486  affine.sx=StringToDouble(token,(char **) NULL);
2487  GetMagickToken(q,&q,token);
2488  if (*token == ',')
2489  GetMagickToken(q,&q,token);
2490  affine.sy=StringToDouble(token,(char **) NULL);
2491  break;
2492  }
2493  if (LocaleCompare("skewX",keyword) == 0)
2494  {
2495  GetMagickToken(q,&q,token);
2496  angle=StringToDouble(token,(char **) NULL);
2497  affine.ry=sin(DegreesToRadians(angle));
2498  break;
2499  }
2500  if (LocaleCompare("skewY",keyword) == 0)
2501  {
2502  GetMagickToken(q,&q,token);
2503  angle=StringToDouble(token,(char **) NULL);
2504  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2505  break;
2506  }
2507  if (LocaleCompare("stop-color",keyword) == 0)
2508  {
2509  GradientType
2510  type;
2511 
2512  PixelPacket
2513  stop_color;
2514 
2515  GetMagickToken(q,&q,token);
2516  (void) QueryColorDatabase(token,&stop_color,&image->exception);
2517  type=LinearGradient;
2518  if (draw_info->gradient.type == RadialGradient)
2519  type=RadialGradient;
2520  (void) GradientImage(image,type,PadSpread,&start_color,
2521  &stop_color);
2522  start_color=stop_color;
2523  GetMagickToken(q,&q,token);
2524  break;
2525  }
2526  if (LocaleCompare("stroke",keyword) == 0)
2527  {
2528  GetMagickToken(q,&q,token);
2529  (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2530  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2531  (void) DrawPatternPath(image,draw_info,token,
2532  &graphic_context[n]->stroke_pattern);
2533  else
2534  {
2535  status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
2536  &image->exception);
2537  if (status == MagickFalse)
2538  {
2539  ImageInfo
2540  *pattern_info;
2541 
2542  pattern_info=AcquireImageInfo();
2543  (void) CopyMagickString(pattern_info->filename,token,
2544  MaxTextExtent);
2545  graphic_context[n]->stroke_pattern=
2546  ReadImage(pattern_info,&image->exception);
2547  CatchException(&image->exception);
2548  pattern_info=DestroyImageInfo(pattern_info);
2549  }
2550  }
2551  break;
2552  }
2553  if (LocaleCompare("stroke-antialias",keyword) == 0)
2554  {
2555  GetMagickToken(q,&q,token);
2556  graphic_context[n]->stroke_antialias=
2557  StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2558  break;
2559  }
2560  if (LocaleCompare("stroke-dasharray",keyword) == 0)
2561  {
2562  if (graphic_context[n]->dash_pattern != (double *) NULL)
2563  graphic_context[n]->dash_pattern=(double *)
2564  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2565  if (IsPoint(q) != MagickFalse)
2566  {
2567  const char
2568  *p;
2569 
2570  p=q;
2571  GetMagickToken(p,&p,token);
2572  if (*token == ',')
2573  GetMagickToken(p,&p,token);
2574  for (x=0; IsPoint(token) != MagickFalse; x++)
2575  {
2576  GetMagickToken(p,&p,token);
2577  if (*token == ',')
2578  GetMagickToken(p,&p,token);
2579  }
2580  graphic_context[n]->dash_pattern=(double *)
2581  AcquireQuantumMemory((size_t) (2UL*x+1UL),
2582  sizeof(*graphic_context[n]->dash_pattern));
2583  if (graphic_context[n]->dash_pattern == (double *) NULL)
2584  {
2585  (void) ThrowMagickException(&image->exception,
2587  "MemoryAllocationFailed","`%s'",image->filename);
2588  break;
2589  }
2590  for (j=0; j < x; j++)
2591  {
2592  GetMagickToken(q,&q,token);
2593  if (*token == ',')
2594  GetMagickToken(q,&q,token);
2595  graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2596  (char **) NULL);
2597  }
2598  if ((x & 0x01) != 0)
2599  for ( ; j < (2*x); j++)
2600  graphic_context[n]->dash_pattern[j]=
2601  graphic_context[n]->dash_pattern[j-x];
2602  graphic_context[n]->dash_pattern[j]=0.0;
2603  break;
2604  }
2605  GetMagickToken(q,&q,token);
2606  break;
2607  }
2608  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2609  {
2610  GetMagickToken(q,&q,token);
2611  graphic_context[n]->dash_offset=StringToDouble(token,
2612  (char **) NULL);
2613  break;
2614  }
2615  if (LocaleCompare("stroke-linecap",keyword) == 0)
2616  {
2617  ssize_t
2618  linecap;
2619 
2620  GetMagickToken(q,&q,token);
2622  if (linecap == -1)
2623  {
2624  status=MagickFalse;
2625  break;
2626  }
2627  graphic_context[n]->linecap=(LineCap) linecap;
2628  break;
2629  }
2630  if (LocaleCompare("stroke-linejoin",keyword) == 0)
2631  {
2632  ssize_t
2633  linejoin;
2634 
2635  GetMagickToken(q,&q,token);
2637  token);
2638  if (linejoin == -1)
2639  {
2640  status=MagickFalse;
2641  break;
2642  }
2643  graphic_context[n]->linejoin=(LineJoin) linejoin;
2644  break;
2645  }
2646  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2647  {
2648  GetMagickToken(q,&q,token);
2649  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2650  break;
2651  }
2652  if (LocaleCompare("stroke-opacity",keyword) == 0)
2653  {
2654  GetMagickToken(q,&q,token);
2655  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2656  graphic_context[n]->stroke.opacity=ClampToQuantum((MagickRealType)
2657  QuantumRange*(1.0-factor*StringToDouble(token,(char **) NULL)));
2658  break;
2659  }
2660  if (LocaleCompare("stroke-width",keyword) == 0)
2661  {
2662  GetMagickToken(q,&q,token);
2663  graphic_context[n]->stroke_width=StringToDouble(token,
2664  (char **) NULL);
2665  break;
2666  }
2667  status=MagickFalse;
2668  break;
2669  }
2670  case 't':
2671  case 'T':
2672  {
2673  if (LocaleCompare("text",keyword) == 0)
2674  {
2675  primitive_type=TextPrimitive;
2676  break;
2677  }
2678  if (LocaleCompare("text-align",keyword) == 0)
2679  {
2680  ssize_t
2681  align;
2682 
2683  GetMagickToken(q,&q,token);
2685  if (align == -1)
2686  {
2687  status=MagickFalse;
2688  break;
2689  }
2690  graphic_context[n]->align=(AlignType) align;
2691  break;
2692  }
2693  if (LocaleCompare("text-anchor",keyword) == 0)
2694  {
2695  ssize_t
2696  align;
2697 
2698  GetMagickToken(q,&q,token);
2700  if (align == -1)
2701  {
2702  status=MagickFalse;
2703  break;
2704  }
2705  graphic_context[n]->align=(AlignType) align;
2706  break;
2707  }
2708  if (LocaleCompare("text-antialias",keyword) == 0)
2709  {
2710  GetMagickToken(q,&q,token);
2711  graphic_context[n]->text_antialias=
2712  StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2713  break;
2714  }
2715  if (LocaleCompare("text-undercolor",keyword) == 0)
2716  {
2717  GetMagickToken(q,&q,token);
2718  (void) QueryColorDatabase(token,&graphic_context[n]->undercolor,
2719  &image->exception);
2720  break;
2721  }
2722  if (LocaleCompare("translate",keyword) == 0)
2723  {
2724  GetMagickToken(q,&q,token);
2725  affine.tx=StringToDouble(token,(char **) NULL);
2726  GetMagickToken(q,&q,token);
2727  if (*token == ',')
2728  GetMagickToken(q,&q,token);
2729  affine.ty=StringToDouble(token,(char **) NULL);
2730  break;
2731  }
2732  status=MagickFalse;
2733  break;
2734  }
2735  case 'v':
2736  case 'V':
2737  {
2738  if (LocaleCompare("viewbox",keyword) == 0)
2739  {
2740  GetMagickToken(q,&q,token);
2741  graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2742  (char **) NULL)-0.5);
2743  GetMagickToken(q,&q,token);
2744  if (*token == ',')
2745  GetMagickToken(q,&q,token);
2746  graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2747  (char **) NULL)-0.5);
2748  GetMagickToken(q,&q,token);
2749  if (*token == ',')
2750  GetMagickToken(q,&q,token);
2751  graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2752  token,(char **) NULL)+0.5);
2753  GetMagickToken(q,&q,token);
2754  if (*token == ',')
2755  GetMagickToken(q,&q,token);
2756  graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2757  token,(char **) NULL)+0.5);
2758  break;
2759  }
2760  status=MagickFalse;
2761  break;
2762  }
2763  default:
2764  {
2765  status=MagickFalse;
2766  break;
2767  }
2768  }
2769  if (status == MagickFalse)
2770  break;
2771  if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2772  (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2773  {
2774  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2775  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2776  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2777  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2778  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2779  current.tx;
2780  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2781  current.ty;
2782  }
2783  if (primitive_type == UndefinedPrimitive)
2784  {
2785  if (image->debug != MagickFalse)
2786  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2787  (int) (q-p),p);
2788  continue;
2789  }
2790  /*
2791  Parse the primitive attributes.
2792  */
2793  i=0;
2794  j=0;
2795  primitive_info[0].point.x=0.0;
2796  primitive_info[0].point.y=0.0;
2797  for (x=0; *q != '\0'; x++)
2798  {
2799  /*
2800  Define points.
2801  */
2802  if (IsPoint(q) == MagickFalse)
2803  break;
2804  GetMagickToken(q,&q,token);
2805  point.x=StringToDouble(token,(char **) NULL);
2806  GetMagickToken(q,&q,token);
2807  if (*token == ',')
2808  GetMagickToken(q,&q,token);
2809  point.y=StringToDouble(token,(char **) NULL);
2810  GetMagickToken(q,(const char **) NULL,token);
2811  if (*token == ',')
2812  GetMagickToken(q,&q,token);
2813  primitive_info[i].primitive=primitive_type;
2814  primitive_info[i].point=point;
2815  primitive_info[i].coordinates=0;
2816  primitive_info[i].method=FloodfillMethod;
2817  i++;
2818  if (i < (ssize_t) number_points)
2819  continue;
2820  number_points<<=1;
2821  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2822  (size_t) number_points,sizeof(*primitive_info));
2823  if (primitive_info == (PrimitiveInfo *) NULL)
2824  {
2826  ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2827  break;
2828  }
2829  }
2830  primitive_info[j].primitive=primitive_type;
2831  primitive_info[j].coordinates=(size_t) x;
2832  primitive_info[j].method=FloodfillMethod;
2833  primitive_info[j].text=(char *) NULL;
2834  /*
2835  Circumscribe primitive within a circle.
2836  */
2837  bounds.x1=primitive_info[j].point.x;
2838  bounds.y1=primitive_info[j].point.y;
2839  bounds.x2=primitive_info[j].point.x;
2840  bounds.y2=primitive_info[j].point.y;
2841  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2842  {
2843  point=primitive_info[j+k].point;
2844  if (point.x < bounds.x1)
2845  bounds.x1=point.x;
2846  if (point.y < bounds.y1)
2847  bounds.y1=point.y;
2848  if (point.x > bounds.x2)
2849  bounds.x2=point.x;
2850  if (point.y > bounds.y2)
2851  bounds.y2=point.y;
2852  }
2853  /*
2854  Speculate how many points our primitive might consume.
2855  */
2856  length=primitive_info[j].coordinates;
2857  switch (primitive_type)
2858  {
2859  case RectanglePrimitive:
2860  {
2861  length*=5;
2862  break;
2863  }
2865  {
2866  double
2867  alpha,
2868  beta,
2869  radius;
2870 
2871  alpha=bounds.x2-bounds.x1;
2872  beta=bounds.y2-bounds.y1;
2873  radius=hypot((double) alpha,(double) beta);
2874  length*=5;
2875  length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2876  break;
2877  }
2878  case BezierPrimitive:
2879  {
2880  if (primitive_info[j].coordinates > 107)
2882  DrawError,"TooManyBezierCoordinates","`%s'",token);
2883  length=BezierQuantum*primitive_info[j].coordinates;
2884  break;
2885  }
2886  case PathPrimitive:
2887  {
2888  char
2889  *s,
2890  *t;
2891 
2892  GetMagickToken(q,&q,token);
2893  length=1;
2894  t=token;
2895  for (s=token; *s != '\0'; s=t)
2896  {
2897  double
2898  value;
2899 
2900  value=StringToDouble(s,&t);
2901  (void) value;
2902  if (s == t)
2903  {
2904  t++;
2905  continue;
2906  }
2907  length++;
2908  }
2909  length=length*BezierQuantum/2;
2910  break;
2911  }
2912  case CirclePrimitive:
2913  case ArcPrimitive:
2914  case EllipsePrimitive:
2915  {
2916  double
2917  alpha,
2918  beta,
2919  radius;
2920 
2921  alpha=bounds.x2-bounds.x1;
2922  beta=bounds.y2-bounds.y1;
2923  radius=hypot((double) alpha,(double) beta);
2924  length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2925  break;
2926  }
2927  default:
2928  break;
2929  }
2930  if ((size_t) (i+length) >= number_points)
2931  {
2932  /*
2933  Resize based on speculative points required by primitive.
2934  */
2935  number_points+=length+1;
2936  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2937  (size_t) number_points,sizeof(*primitive_info));
2938  if (primitive_info == (PrimitiveInfo *) NULL)
2939  {
2941  ResourceLimitError,"MemoryAllocationFailed","`%s'",
2942  image->filename);
2943  break;
2944  }
2945  }
2946  switch (primitive_type)
2947  {
2948  case PointPrimitive:
2949  default:
2950  {
2951  if (primitive_info[j].coordinates != 1)
2952  {
2953  status=MagickFalse;
2954  break;
2955  }
2956  TracePoint(primitive_info+j,primitive_info[j].point);
2957  i=(ssize_t) (j+primitive_info[j].coordinates);
2958  break;
2959  }
2960  case LinePrimitive:
2961  {
2962  if (primitive_info[j].coordinates != 2)
2963  {
2964  status=MagickFalse;
2965  break;
2966  }
2967  TraceLine(primitive_info+j,primitive_info[j].point,
2968  primitive_info[j+1].point);
2969  i=(ssize_t) (j+primitive_info[j].coordinates);
2970  break;
2971  }
2972  case RectanglePrimitive:
2973  {
2974  if (primitive_info[j].coordinates != 2)
2975  {
2976  status=MagickFalse;
2977  break;
2978  }
2979  TraceRectangle(primitive_info+j,primitive_info[j].point,
2980  primitive_info[j+1].point);
2981  i=(ssize_t) (j+primitive_info[j].coordinates);
2982  break;
2983  }
2985  {
2986  if (primitive_info[j].coordinates != 3)
2987  {
2988  status=MagickFalse;
2989  break;
2990  }
2991  TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2992  primitive_info[j+1].point,primitive_info[j+2].point);
2993  i=(ssize_t) (j+primitive_info[j].coordinates);
2994  break;
2995  }
2996  case ArcPrimitive:
2997  {
2998  if (primitive_info[j].coordinates != 3)
2999  {
3000  primitive_type=UndefinedPrimitive;
3001  break;
3002  }
3003  TraceArc(primitive_info+j,primitive_info[j].point,
3004  primitive_info[j+1].point,primitive_info[j+2].point);
3005  i=(ssize_t) (j+primitive_info[j].coordinates);
3006  break;
3007  }
3008  case EllipsePrimitive:
3009  {
3010  if (primitive_info[j].coordinates != 3)
3011  {
3012  status=MagickFalse;
3013  break;
3014  }
3015  TraceEllipse(primitive_info+j,primitive_info[j].point,
3016  primitive_info[j+1].point,primitive_info[j+2].point);
3017  i=(ssize_t) (j+primitive_info[j].coordinates);
3018  break;
3019  }
3020  case CirclePrimitive:
3021  {
3022  if (primitive_info[j].coordinates != 2)
3023  {
3024  status=MagickFalse;
3025  break;
3026  }
3027  TraceCircle(primitive_info+j,primitive_info[j].point,
3028  primitive_info[j+1].point);
3029  i=(ssize_t) (j+primitive_info[j].coordinates);
3030  break;
3031  }
3032  case PolylinePrimitive:
3033  break;
3034  case PolygonPrimitive:
3035  {
3036  primitive_info[i]=primitive_info[j];
3037  primitive_info[i].coordinates=0;
3038  primitive_info[j].coordinates++;
3039  i++;
3040  break;
3041  }
3042  case BezierPrimitive:
3043  {
3044  if (primitive_info[j].coordinates < 3)
3045  {
3046  status=MagickFalse;
3047  break;
3048  }
3049  TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3050  i=(ssize_t) (j+primitive_info[j].coordinates);
3051  break;
3052  }
3053  case PathPrimitive:
3054  {
3055  i=(ssize_t) (j+TracePath(primitive_info+j,token));
3056  break;
3057  }
3058  case ColorPrimitive:
3059  case MattePrimitive:
3060  {
3061  ssize_t
3062  method;
3063 
3064  if (primitive_info[j].coordinates != 1)
3065  {
3066  status=MagickFalse;
3067  break;
3068  }
3069  GetMagickToken(q,&q,token);
3071  if (method == -1)
3072  {
3073  status=MagickFalse;
3074  break;
3075  }
3076  primitive_info[j].method=(PaintMethod) method;
3077  break;
3078  }
3079  case TextPrimitive:
3080  {
3081  if (primitive_info[j].coordinates != 1)
3082  {
3083  status=MagickFalse;
3084  break;
3085  }
3086  if (*token != ',')
3087  GetMagickToken(q,&q,token);
3088  primitive_info[j].text=AcquireString(token);
3089  break;
3090  }
3091  case ImagePrimitive:
3092  {
3093  if (primitive_info[j].coordinates != 2)
3094  {
3095  status=MagickFalse;
3096  break;
3097  }
3098  GetMagickToken(q,&q,token);
3099  primitive_info[j].text=AcquireString(token);
3100  break;
3101  }
3102  }
3103  if (primitive_info == (PrimitiveInfo *) NULL)
3104  break;
3105  if (image->debug != MagickFalse)
3106  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3107  if (status == MagickFalse)
3108  break;
3109  primitive_info[i].primitive=UndefinedPrimitive;
3110  if (i == 0)
3111  continue;
3112  /*
3113  Transform points.
3114  */
3115  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3116  {
3117  point=primitive_info[i].point;
3118  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3119  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3120  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3121  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3122  point=primitive_info[i].point;
3123  if (point.x < graphic_context[n]->bounds.x1)
3124  graphic_context[n]->bounds.x1=point.x;
3125  if (point.y < graphic_context[n]->bounds.y1)
3126  graphic_context[n]->bounds.y1=point.y;
3127  if (point.x > graphic_context[n]->bounds.x2)
3128  graphic_context[n]->bounds.x2=point.x;
3129  if (point.y > graphic_context[n]->bounds.y2)
3130  graphic_context[n]->bounds.y2=point.y;
3131  if (primitive_info[i].primitive == ImagePrimitive)
3132  break;
3133  if (i >= (ssize_t) number_points)
3134  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3135  }
3136  if (graphic_context[n]->render != MagickFalse)
3137  {
3138  if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3139  (LocaleCompare(graphic_context[n]->clip_mask,
3140  graphic_context[n-1]->clip_mask) != 0))
3141  status&=DrawClipPath(image,graphic_context[n],
3142  graphic_context[n]->clip_mask);
3143  status&=DrawPrimitive(image,graphic_context[n],primitive_info);
3144  }
3145  if (primitive_info->text != (char *) NULL)
3146  primitive_info->text=(char *) RelinquishMagickMemory(
3147  primitive_info->text);
3148  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3149  primitive_extent);
3150  if (proceed == MagickFalse)
3151  break;
3152  if (status == 0)
3153  break;
3154  }
3155  if (image->debug != MagickFalse)
3156  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3157  /*
3158  Relinquish resources.
3159  */
3160  token=DestroyString(token);
3161  if (primitive_info != (PrimitiveInfo *) NULL)
3162  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3163  primitive=DestroyString(primitive);
3164  for ( ; n >= 0; n--)
3165  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3166  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3167  if (status == MagickFalse)
3168  ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3169  keyword);
3170  return(status != 0 ? MagickTrue : MagickFalse);
3171 }
3172 
3173 /*
3174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3175 % %
3176 % %
3177 % %
3178 % D r a w G r a d i e n t I m a g e %
3179 % %
3180 % %
3181 % %
3182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3183 %
3184 % DrawGradientImage() draws a linear gradient on the image.
3185 %
3186 % The format of the DrawGradientImage method is:
3187 %
3188 % MagickBooleanType DrawGradientImage(Image *image,
3189 % const DrawInfo *draw_info)
3190 %
3191 % A description of each parameter follows:
3192 %
3193 % o image: the image.
3194 %
3195 % o _info: the draw info.
3196 %
3197 */
3198 
3199 static inline double GetStopColorOffset(const GradientInfo *gradient,
3200  const ssize_t x,const ssize_t y)
3201 {
3202  switch (gradient->type)
3203  {
3204  case UndefinedGradient:
3205  case LinearGradient:
3206  {
3207  double
3208  gamma,
3209  length,
3210  offset,
3211  scale;
3212 
3213  PointInfo
3214  p,
3215  q;
3216 
3217  const SegmentInfo
3218  *gradient_vector;
3219 
3220  gradient_vector=(&gradient->gradient_vector);
3221  p.x=gradient_vector->x2-gradient_vector->x1;
3222  p.y=gradient_vector->y2-gradient_vector->y1;
3223  q.x=(double) x-gradient_vector->x1;
3224  q.y=(double) y-gradient_vector->y1;
3225  length=sqrt(q.x*q.x+q.y*q.y);
3226  gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3227  gamma=PerceptibleReciprocal(gamma);
3228  scale=p.x*q.x+p.y*q.y;
3229  offset=gamma*scale*length;
3230  return(offset);
3231  }
3232  case RadialGradient:
3233  {
3234  PointInfo
3235  v;
3236 
3237  if (gradient->spread == RepeatSpread)
3238  {
3239  v.x=(double) x-gradient->center.x;
3240  v.y=(double) y-gradient->center.y;
3241  return(sqrt(v.x*v.x+v.y*v.y));
3242  }
3243  v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3244  gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3245  gradient->angle))))/gradient->radii.x;
3246  v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3247  gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3248  gradient->angle))))/gradient->radii.y;
3249  return(sqrt(v.x*v.x+v.y*v.y));
3250  }
3251  }
3252  return(0.0);
3253 }
3254 
3256  const DrawInfo *draw_info)
3257 {
3258  CacheView
3259  *image_view;
3260 
3261  const GradientInfo
3262  *gradient;
3263 
3264  const SegmentInfo
3265  *gradient_vector;
3266 
3267  double
3268  length;
3269 
3271  *exception;
3272 
3274  status;
3275 
3277  zero;
3278 
3279  PointInfo
3280  point;
3281 
3283  bounding_box;
3284 
3285  ssize_t
3286  y;
3287 
3288  /*
3289  Draw linear or radial gradient on image.
3290  */
3291  assert(image != (Image *) NULL);
3292  assert(image->signature == MagickSignature);
3293  if (image->debug != MagickFalse)
3294  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3295  assert(draw_info != (const DrawInfo *) NULL);
3296  gradient=(&draw_info->gradient);
3297  gradient_vector=(&gradient->gradient_vector);
3298  point.x=gradient_vector->x2-gradient_vector->x1;
3299  point.y=gradient_vector->y2-gradient_vector->y1;
3300  length=sqrt(point.x*point.x+point.y*point.y);
3301  bounding_box=gradient->bounding_box;
3302  status=MagickTrue;
3303  exception=(&image->exception);
3304  GetMagickPixelPacket(image,&zero);
3305  image_view=AcquireAuthenticCacheView(image,exception);
3306 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3307  #pragma omp parallel for schedule(static,4) shared(status) \
3308  magick_threads(image,image,1,1)
3309 #endif
3310  for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3311  {
3312  double
3313  alpha,
3314  offset;
3315 
3317  composite,
3318  pixel;
3319 
3320  register IndexPacket
3321  *magick_restrict indexes;
3322 
3323  register ssize_t
3324  i,
3325  x;
3326 
3327  register PixelPacket
3328  *magick_restrict q;
3329 
3330  ssize_t
3331  j;
3332 
3333  if (status == MagickFalse)
3334  continue;
3335  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3336  if (q == (PixelPacket *) NULL)
3337  {
3338  status=MagickFalse;
3339  continue;
3340  }
3341  indexes=GetCacheViewAuthenticIndexQueue(image_view);
3342  pixel=zero;
3343  composite=zero;
3344  offset=GetStopColorOffset(gradient,0,y);
3345  if (gradient->type != RadialGradient)
3346  offset/=length;
3347  for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3348  {
3349  SetMagickPixelPacket(image,q,indexes+x,&pixel);
3350  switch (gradient->spread)
3351  {
3352  case UndefinedSpread:
3353  case PadSpread:
3354  {
3355  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3356  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3357  {
3358  offset=GetStopColorOffset(gradient,x,y);
3359  if (gradient->type != RadialGradient)
3360  offset/=length;
3361  }
3362  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3363  if (offset < gradient->stops[i].offset)
3364  break;
3365  if ((offset < 0.0) || (i == 0))
3366  composite=gradient->stops[0].color;
3367  else
3368  if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3369  composite=gradient->stops[gradient->number_stops-1].color;
3370  else
3371  {
3372  j=i;
3373  i--;
3374  alpha=(offset-gradient->stops[i].offset)/
3375  (gradient->stops[j].offset-gradient->stops[i].offset);
3376  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3377  &gradient->stops[j].color,alpha,&composite);
3378  }
3379  break;
3380  }
3381  case ReflectSpread:
3382  {
3383  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3384  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3385  {
3386  offset=GetStopColorOffset(gradient,x,y);
3387  if (gradient->type != RadialGradient)
3388  offset/=length;
3389  }
3390  if (offset < 0.0)
3391  offset=(-offset);
3392  if ((ssize_t) fmod(offset,2.0) == 0)
3393  offset=fmod(offset,1.0);
3394  else
3395  offset=1.0-fmod(offset,1.0);
3396  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3397  if (offset < gradient->stops[i].offset)
3398  break;
3399  if (i == 0)
3400  composite=gradient->stops[0].color;
3401  else
3402  if (i == (ssize_t) gradient->number_stops)
3403  composite=gradient->stops[gradient->number_stops-1].color;
3404  else
3405  {
3406  j=i;
3407  i--;
3408  alpha=(offset-gradient->stops[i].offset)/
3409  (gradient->stops[j].offset-gradient->stops[i].offset);
3410  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3411  &gradient->stops[j].color,alpha,&composite);
3412  }
3413  break;
3414  }
3415  case RepeatSpread:
3416  {
3417  double
3418  repeat;
3419 
3421  antialias;
3422 
3423  antialias=MagickFalse;
3424  repeat=0.0;
3425  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3426  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3427  {
3428  offset=GetStopColorOffset(gradient,x,y);
3429  if (gradient->type == LinearGradient)
3430  {
3431  repeat=fmod(offset,length);
3432  if (repeat < 0.0)
3433  repeat=length-fmod(-repeat,length);
3434  else
3435  repeat=fmod(offset,length);
3436  antialias=(repeat < length) && ((repeat+1.0) > length) ?
3438  offset=repeat/length;
3439  }
3440  else
3441  {
3442  repeat=fmod(offset,(double) gradient->radius);
3443  if (repeat < 0.0)
3444  repeat=gradient->radius-fmod(-repeat,
3445  (double) gradient->radius);
3446  else
3447  repeat=fmod(offset,(double) gradient->radius);
3448  antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3449  MagickFalse;
3450  offset=repeat/gradient->radius;
3451  }
3452  }
3453  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3454  if (offset < gradient->stops[i].offset)
3455  break;
3456  if (i == 0)
3457  composite=gradient->stops[0].color;
3458  else
3459  if (i == (ssize_t) gradient->number_stops)
3460  composite=gradient->stops[gradient->number_stops-1].color;
3461  else
3462  {
3463  j=i;
3464  i--;
3465  alpha=(offset-gradient->stops[i].offset)/
3466  (gradient->stops[j].offset-gradient->stops[i].offset);
3467  if (antialias != MagickFalse)
3468  {
3469  if (gradient->type == LinearGradient)
3470  alpha=length-repeat;
3471  else
3472  alpha=gradient->radius-repeat;
3473  i=0;
3474  j=(ssize_t) gradient->number_stops-1L;
3475  }
3476  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3477  &gradient->stops[j].color,alpha,&composite);
3478  }
3479  break;
3480  }
3481  }
3482  MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
3483  pixel.opacity,&pixel);
3484  SetPixelPacket(image,&pixel,q,indexes+x);
3485  q++;
3486  }
3487  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3488  status=MagickFalse;
3489  }
3490  image_view=DestroyCacheView(image_view);
3491  return(status);
3492 }
3493 
3494 /*
3495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3496 % %
3497 % %
3498 % %
3499 % D r a w P a t t e r n P a t h %
3500 % %
3501 % %
3502 % %
3503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3504 %
3505 % DrawPatternPath() draws a pattern.
3506 %
3507 % The format of the DrawPatternPath method is:
3508 %
3509 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3510 % const char *name,Image **pattern)
3511 %
3512 % A description of each parameter follows:
3513 %
3514 % o image: the image.
3515 %
3516 % o draw_info: the draw info.
3517 %
3518 % o name: the pattern name.
3519 %
3520 % o image: the image.
3521 %
3522 */
3524  const DrawInfo *draw_info,const char *name,Image **pattern)
3525 {
3526  char
3527  property[MaxTextExtent];
3528 
3529  const char
3530  *geometry,
3531  *path,
3532  *type;
3533 
3534  DrawInfo
3535  *clone_info;
3536 
3537  ImageInfo
3538  *image_info;
3539 
3541  status;
3542 
3543  assert(image != (Image *) NULL);
3544  assert(image->signature == MagickSignature);
3545  if (image->debug != MagickFalse)
3546  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3547  assert(draw_info != (const DrawInfo *) NULL);
3548  assert(name != (const char *) NULL);
3549  (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3550  path=GetImageArtifact(image,property);
3551  if (path == (const char *) NULL)
3552  return(MagickFalse);
3553  (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3554  geometry=GetImageArtifact(image,property);
3555  if (geometry == (const char *) NULL)
3556  return(MagickFalse);
3557  if ((*pattern) != (Image *) NULL)
3558  *pattern=DestroyImage(*pattern);
3559  image_info=AcquireImageInfo();
3560  image_info->size=AcquireString(geometry);
3561  *pattern=AcquireImage(image_info);
3562  image_info=DestroyImageInfo(image_info);
3563  (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
3564  &image->exception);
3565  (void) SetImageBackgroundColor(*pattern);
3566  if (image->debug != MagickFalse)
3568  "begin pattern-path %s %s",name,geometry);
3569  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3570  clone_info->fill_pattern=NewImageList();
3571  clone_info->stroke_pattern=NewImageList();
3572  (void) FormatLocaleString(property,MaxTextExtent,"%s-type",name);
3573  type=GetImageArtifact(image,property);
3574  if (type != (const char *) NULL)
3577  (void) CloneString(&clone_info->primitive,path);
3578  status=DrawImage(*pattern,clone_info);
3579  clone_info=DestroyDrawInfo(clone_info);
3580  if (image->debug != MagickFalse)
3581  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3582  return(status);
3583 }
3584 
3585 /*
3586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3587 % %
3588 % %
3589 % %
3590 + D r a w P o l y g o n P r i m i t i v e %
3591 % %
3592 % %
3593 % %
3594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3595 %
3596 % DrawPolygonPrimitive() draws a polygon on the image.
3597 %
3598 % The format of the DrawPolygonPrimitive method is:
3599 %
3600 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3601 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3602 %
3603 % A description of each parameter follows:
3604 %
3605 % o image: the image.
3606 %
3607 % o draw_info: the draw info.
3608 %
3609 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3610 %
3611 */
3612 
3614 {
3615  register ssize_t
3616  i;
3617 
3618  assert(polygon_info != (PolygonInfo **) NULL);
3619  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3620  if (polygon_info[i] != (PolygonInfo *) NULL)
3621  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3622  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3623  return(polygon_info);
3624 }
3625 
3627  const PrimitiveInfo *primitive_info)
3628 {
3629  PathInfo
3630  *magick_restrict path_info;
3631 
3632  PolygonInfo
3633  **polygon_info;
3634 
3635  register ssize_t
3636  i;
3637 
3638  size_t
3639  number_threads;
3640 
3641  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3642  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3643  sizeof(*polygon_info));
3644  if (polygon_info == (PolygonInfo **) NULL)
3645  return((PolygonInfo **) NULL);
3646  (void) ResetMagickMemory(polygon_info,0,(size_t)
3647  GetMagickResourceLimit(ThreadResource)*sizeof(*polygon_info));
3648  path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3649  if (path_info == (PathInfo *) NULL)
3650  return(DestroyPolygonThreadSet(polygon_info));
3651  for (i=0; i < (ssize_t) number_threads; i++)
3652  {
3653  polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3654  if (polygon_info[i] == (PolygonInfo *) NULL)
3655  return(DestroyPolygonThreadSet(polygon_info));
3656  }
3657  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3658  return(polygon_info);
3659 }
3660 
3661 static double GetOpacityPixel(PolygonInfo *polygon_info,const double mid,
3662  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3663  const ssize_t y,double *stroke_opacity)
3664 {
3665  double
3666  alpha,
3667  beta,
3668  distance,
3669  subpath_opacity;
3670 
3671  PointInfo
3672  delta;
3673 
3674  register EdgeInfo
3675  *p;
3676 
3677  register const PointInfo
3678  *q;
3679 
3680  register ssize_t
3681  i;
3682 
3683  ssize_t
3684  j,
3685  winding_number;
3686 
3687  /*
3688  Compute fill & stroke opacity for this (x,y) point.
3689  */
3690  *stroke_opacity=0.0;
3691  subpath_opacity=0.0;
3692  p=polygon_info->edges;
3693  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3694  {
3695  if ((double) y <= (p->bounds.y1-mid-0.5))
3696  break;
3697  if ((double) y > (p->bounds.y2+mid+0.5))
3698  {
3699  (void) DestroyEdge(polygon_info,(size_t) j);
3700  continue;
3701  }
3702  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3703  ((double) x > (p->bounds.x2+mid+0.5)))
3704  continue;
3705  i=(ssize_t) MagickMax((double) p->highwater,1.0);
3706  for ( ; i < (ssize_t) p->number_points; i++)
3707  {
3708  if ((double) y <= (p->points[i-1].y-mid-0.5))
3709  break;
3710  if ((double) y > (p->points[i].y+mid+0.5))
3711  continue;
3712  if (p->scanline != (double) y)
3713  {
3714  p->scanline=(double) y;
3715  p->highwater=(size_t) i;
3716  }
3717  /*
3718  Compute distance between a point and an edge.
3719  */
3720  q=p->points+i-1;
3721  delta.x=(q+1)->x-q->x;
3722  delta.y=(q+1)->y-q->y;
3723  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3724  if (beta < 0.0)
3725  {
3726  delta.x=(double) x-q->x;
3727  delta.y=(double) y-q->y;
3728  distance=delta.x*delta.x+delta.y*delta.y;
3729  }
3730  else
3731  {
3732  alpha=delta.x*delta.x+delta.y*delta.y;
3733  if (beta > alpha)
3734  {
3735  delta.x=(double) x-(q+1)->x;
3736  delta.y=(double) y-(q+1)->y;
3737  distance=delta.x*delta.x+delta.y*delta.y;
3738  }
3739  else
3740  {
3741  alpha=1.0/alpha;
3742  beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3743  distance=alpha*beta*beta;
3744  }
3745  }
3746  /*
3747  Compute stroke & subpath opacity.
3748  */
3749  beta=0.0;
3750  if (p->ghostline == MagickFalse)
3751  {
3752  alpha=mid+0.5;
3753  if ((*stroke_opacity < 1.0) &&
3754  (distance <= ((alpha+0.25)*(alpha+0.25))))
3755  {
3756  alpha=mid-0.5;
3757  if (distance <= ((alpha+0.25)*(alpha+0.25)))
3758  *stroke_opacity=1.0;
3759  else
3760  {
3761  beta=1.0;
3762  if (distance != 1.0)
3763  beta=sqrt((double) distance);
3764  alpha=beta-mid-0.5;
3765  if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3766  *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3767  }
3768  }
3769  }
3770  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3771  continue;
3772  if (distance <= 0.0)
3773  {
3774  subpath_opacity=1.0;
3775  continue;
3776  }
3777  if (distance > 1.0)
3778  continue;
3779  if (beta == 0.0)
3780  {
3781  beta=1.0;
3782  if (distance != 1.0)
3783  beta=sqrt(distance);
3784  }
3785  alpha=beta-1.0;
3786  if (subpath_opacity < (alpha*alpha))
3787  subpath_opacity=alpha*alpha;
3788  }
3789  }
3790  /*
3791  Compute fill opacity.
3792  */
3793  if (fill == MagickFalse)
3794  return(0.0);
3795  if (subpath_opacity >= 1.0)
3796  return(1.0);
3797  /*
3798  Determine winding number.
3799  */
3800  winding_number=0;
3801  p=polygon_info->edges;
3802  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3803  {
3804  if ((double) y <= p->bounds.y1)
3805  break;
3806  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3807  continue;
3808  if ((double) x > p->bounds.x2)
3809  {
3810  winding_number+=p->direction ? 1 : -1;
3811  continue;
3812  }
3813  i=(ssize_t) MagickMax((double) p->highwater,1.0);
3814  for ( ; i < (ssize_t) p->number_points; i++)
3815  if ((double) y <= p->points[i].y)
3816  break;
3817  q=p->points+i-1;
3818  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3819  winding_number+=p->direction ? 1 : -1;
3820  }
3821  if (fill_rule != NonZeroRule)
3822  {
3823  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3824  return(1.0);
3825  }
3826  else
3827  if (MagickAbsoluteValue(winding_number) != 0)
3828  return(1.0);
3829  return(subpath_opacity);
3830 }
3831 
3833  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3834 {
3835  CacheView
3836  *image_view;
3837 
3838  double
3839  mid;
3840 
3842  *exception;
3843 
3845  fill,
3846  status;
3847 
3848  PolygonInfo
3849  **magick_restrict polygon_info;
3850 
3851  register EdgeInfo
3852  *p;
3853 
3854  register ssize_t
3855  i;
3856 
3857  SegmentInfo
3858  bounds;
3859 
3860  ssize_t
3861  start_y,
3862  stop_y,
3863  y;
3864 
3865  /*
3866  Compute bounding box.
3867  */
3868  assert(image != (Image *) NULL);
3869  assert(image->signature == MagickSignature);
3870  if (image->debug != MagickFalse)
3871  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3872  assert(draw_info != (DrawInfo *) NULL);
3873  assert(draw_info->signature == MagickSignature);
3874  assert(primitive_info != (PrimitiveInfo *) NULL);
3875  if (primitive_info->coordinates == 0)
3876  return(MagickTrue);
3877  polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3878  if (polygon_info == (PolygonInfo **) NULL)
3879  return(MagickFalse);
3880 DisableMSCWarning(4127)
3881  if (0)
3882  DrawBoundingRectangles(image,draw_info,polygon_info[0]);
3884  if (image->debug != MagickFalse)
3885  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3886  fill=(primitive_info->method == FillToBorderMethod) ||
3887  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3888  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3889  bounds=polygon_info[0]->edges[0].bounds;
3890  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3891  {
3892  p=polygon_info[0]->edges+i;
3893  if (p->bounds.x1 < bounds.x1)
3894  bounds.x1=p->bounds.x1;
3895  if (p->bounds.y1 < bounds.y1)
3896  bounds.y1=p->bounds.y1;
3897  if (p->bounds.x2 > bounds.x2)
3898  bounds.x2=p->bounds.x2;
3899  if (p->bounds.y2 > bounds.y2)
3900  bounds.y2=p->bounds.y2;
3901  }
3902  bounds.x1-=(mid+1.0);
3903  bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3904  image->columns ? (double) image->columns-1 : bounds.x1;
3905  bounds.y1-=(mid+1.0);
3906  bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3907  image->rows ? (double) image->rows-1 : bounds.y1;
3908  bounds.x2+=(mid+1.0);
3909  bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3910  image->columns ? (double) image->columns-1 : bounds.x2;
3911  bounds.y2+=(mid+1.0);
3912  bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3913  image->rows ? (double) image->rows-1 : bounds.y2;
3914  status=MagickTrue;
3915  exception=(&image->exception);
3916  image_view=AcquireAuthenticCacheView(image,exception);
3917  if ((primitive_info->coordinates == 1) ||
3918  (polygon_info[0]->number_edges == 0))
3919  {
3920  /*
3921  Draw point.
3922  */
3923  start_y=(ssize_t) ceil(bounds.y1-0.5);
3924  stop_y=(ssize_t) floor(bounds.y2+0.5);
3925 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3926  #pragma omp parallel for schedule(static,4) shared(status) \
3927  magick_threads(image,image,1,1)
3928 #endif
3929  for (y=start_y; y <= stop_y; y++)
3930  {
3932  sync;
3933 
3934  register PixelPacket
3935  *magick_restrict q;
3936 
3937  register ssize_t
3938  x;
3939 
3940  ssize_t
3941  start_x,
3942  stop_x;
3943 
3944  if (status == MagickFalse)
3945  continue;
3946  start_x=(ssize_t) ceil(bounds.x1-0.5);
3947  stop_x=(ssize_t) floor(bounds.x2+0.5);
3948  x=start_x;
3949  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
3950  exception);
3951  if (q == (PixelPacket *) NULL)
3952  {
3953  status=MagickFalse;
3954  continue;
3955  }
3956  for ( ; x <= stop_x; x++)
3957  {
3958  if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3959  (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3960  (void) GetFillColor(draw_info,x-start_x,y-start_y,q);
3961  q++;
3962  }
3963  sync=SyncCacheViewAuthenticPixels(image_view,exception);
3964  if (sync == MagickFalse)
3965  status=MagickFalse;
3966  }
3967  image_view=DestroyCacheView(image_view);
3968  polygon_info=DestroyPolygonThreadSet(polygon_info);
3969  if (image->debug != MagickFalse)
3971  " end draw-polygon");
3972  return(status);
3973  }
3974  /*
3975  Draw polygon or line.
3976  */
3977  if (image->matte == MagickFalse)
3979  start_y=(ssize_t) ceil(bounds.y1-0.5);
3980  stop_y=(ssize_t) floor(bounds.y2+0.5);
3981 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3982  #pragma omp parallel for schedule(static,4) shared(status) \
3983  magick_threads(image,image,1,1)
3984 #endif
3985  for (y=start_y; y <= stop_y; y++)
3986  {
3987  const int
3988  id = GetOpenMPThreadId();
3989 
3990  double
3991  fill_opacity,
3992  stroke_opacity;
3993 
3994  PixelPacket
3995  fill_color,
3996  stroke_color;
3997 
3998  register PixelPacket
3999  *magick_restrict q;
4000 
4001  register ssize_t
4002  x;
4003 
4004  ssize_t
4005  start_x,
4006  stop_x;
4007 
4008  if (status == MagickFalse)
4009  continue;
4010  start_x=(ssize_t) ceil(bounds.x1-0.5);
4011  stop_x=(ssize_t) floor(bounds.x2+0.5);
4012  q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+1),1,
4013  exception);
4014  if (q == (PixelPacket *) NULL)
4015  {
4016  status=MagickFalse;
4017  continue;
4018  }
4019  for (x=start_x; x <= stop_x; x++)
4020  {
4021  /*
4022  Fill and/or stroke.
4023  */
4024  fill_opacity=GetOpacityPixel(polygon_info[id],mid,fill,
4025  draw_info->fill_rule,x,y,&stroke_opacity);
4026  if (draw_info->stroke_antialias == MagickFalse)
4027  {
4028  fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
4029  stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
4030  }
4031  (void) GetFillColor(draw_info,x-start_x,y-start_y,&fill_color);
4032  fill_opacity=(double) (QuantumRange-fill_opacity*(QuantumRange-
4033  fill_color.opacity));
4034  MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
4035  (MagickRealType) q->opacity,q);
4036  (void) GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color);
4037  stroke_opacity=(double) (QuantumRange-stroke_opacity*(QuantumRange-
4038  stroke_color.opacity));
4039  MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
4040  (MagickRealType) q->opacity,q);
4041  q++;
4042  }
4043  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4044  status=MagickFalse;
4045  }
4046  image_view=DestroyCacheView(image_view);
4047  polygon_info=DestroyPolygonThreadSet(polygon_info);
4048  if (image->debug != MagickFalse)
4049  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4050  return(status);
4051 }
4052 
4053 /*
4054 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4055 % %
4056 % %
4057 % %
4058 % D r a w P r i m i t i v e %
4059 % %
4060 % %
4061 % %
4062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4063 %
4064 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4065 %
4066 % The format of the DrawPrimitive method is:
4067 %
4068 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4069 % PrimitiveInfo *primitive_info)
4070 %
4071 % A description of each parameter follows:
4072 %
4073 % o image: the image.
4074 %
4075 % o draw_info: the draw info.
4076 %
4077 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4078 %
4079 */
4080 
4081 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4082 {
4083  const char
4084  *methods[] =
4085  {
4086  "point",
4087  "replace",
4088  "floodfill",
4089  "filltoborder",
4090  "reset",
4091  "?"
4092  };
4093 
4094  PointInfo
4095  p,
4096  q,
4097  point;
4098 
4099  register ssize_t
4100  i,
4101  x;
4102 
4103  ssize_t
4104  coordinates,
4105  y;
4106 
4107  x=(ssize_t) ceil(primitive_info->point.x-0.5);
4108  y=(ssize_t) ceil(primitive_info->point.y-0.5);
4109  switch (primitive_info->primitive)
4110  {
4111  case PointPrimitive:
4112  {
4114  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4115  methods[primitive_info->method]);
4116  return;
4117  }
4118  case ColorPrimitive:
4119  {
4121  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4122  methods[primitive_info->method]);
4123  return;
4124  }
4125  case MattePrimitive:
4126  {
4128  "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4129  methods[primitive_info->method]);
4130  return;
4131  }
4132  case TextPrimitive:
4133  {
4135  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4136  return;
4137  }
4138  case ImagePrimitive:
4139  {
4141  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4142  return;
4143  }
4144  default:
4145  break;
4146  }
4147  coordinates=0;
4148  p=primitive_info[0].point;
4149  q.x=(-1.0);
4150  q.y=(-1.0);
4151  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4152  {
4153  point=primitive_info[i].point;
4154  if (coordinates <= 0)
4155  {
4156  coordinates=(ssize_t) primitive_info[i].coordinates;
4158  " begin open (%.20g)",(double) coordinates);
4159  p=point;
4160  }
4161  point=primitive_info[i].point;
4162  if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4163  (fabs(q.y-point.y) >= MagickEpsilon))
4165  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4166  else
4168  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4169  q=point;
4170  coordinates--;
4171  if (coordinates > 0)
4172  continue;
4173  if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4174  (fabs(p.y-point.y) >= MagickEpsilon))
4175  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4176  (double) coordinates);
4177  else
4178  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4179  (double) coordinates);
4180  }
4181 }
4182 
4184  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4185 {
4186  CacheView
4187  *image_view;
4188 
4190  *exception;
4191 
4193  status;
4194 
4195  register ssize_t
4196  i,
4197  x;
4198 
4199  ssize_t
4200  y;
4201 
4202  if (image->debug != MagickFalse)
4203  {
4205  " begin draw-primitive");
4207  " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4208  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4209  draw_info->affine.tx,draw_info->affine.ty);
4210  }
4211  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4212  ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
4213  (IsPixelGray(&draw_info->stroke) == MagickFalse)))
4214  (void) SetImageColorspace(image,sRGBColorspace);
4215  status=MagickTrue;
4216  exception=(&image->exception);
4217  x=(ssize_t) ceil(primitive_info->point.x-0.5);
4218  y=(ssize_t) ceil(primitive_info->point.y-0.5);
4219  image_view=AcquireAuthenticCacheView(image,exception);
4220  switch (primitive_info->primitive)
4221  {
4222  case PointPrimitive:
4223  {
4224  PixelPacket
4225  fill_color;
4226 
4227  PixelPacket
4228  *q;
4229 
4230  if ((y < 0) || (y >= (ssize_t) image->rows))
4231  break;
4232  if ((x < 0) || (x >= (ssize_t) image->columns))
4233  break;
4234  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4235  if (q == (PixelPacket *) NULL)
4236  break;
4237  (void) GetFillColor(draw_info,x,y,&fill_color);
4238  MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
4239  (MagickRealType) q->opacity,q);
4240  status&=SyncCacheViewAuthenticPixels(image_view,exception);
4241  break;
4242  }
4243  case ColorPrimitive:
4244  {
4245  switch (primitive_info->method)
4246  {
4247  case PointMethod:
4248  default:
4249  {
4250  PixelPacket
4251  *q;
4252 
4253  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4254  if (q == (PixelPacket *) NULL)
4255  break;
4256  (void) GetFillColor(draw_info,x,y,q);
4257  status&=SyncCacheViewAuthenticPixels(image_view,exception);
4258  break;
4259  }
4260  case ReplaceMethod:
4261  {
4263  sync;
4264 
4265  PixelPacket
4266  target;
4267 
4268  status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4269  for (y=0; y < (ssize_t) image->rows; y++)
4270  {
4271  register PixelPacket
4272  *magick_restrict q;
4273 
4274  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4275  exception);
4276  if (q == (PixelPacket *) NULL)
4277  break;
4278  for (x=0; x < (ssize_t) image->columns; x++)
4279  {
4280  if (IsColorSimilar(image,q,&target) == MagickFalse)
4281  {
4282  q++;
4283  continue;
4284  }
4285  (void) GetFillColor(draw_info,x,y,q);
4286  q++;
4287  }
4288  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4289  if (sync == MagickFalse)
4290  break;
4291  }
4292  break;
4293  }
4294  case FloodfillMethod:
4295  case FillToBorderMethod:
4296  {
4298  target;
4299 
4300  (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4301  if (primitive_info->method == FillToBorderMethod)
4302  {
4303  target.red=(MagickRealType) draw_info->border_color.red;
4304  target.green=(MagickRealType) draw_info->border_color.green;
4305  target.blue=(MagickRealType) draw_info->border_color.blue;
4306  }
4307  status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
4308  y,primitive_info->method == FloodfillMethod ? MagickFalse :
4309  MagickTrue);
4310  break;
4311  }
4312  case ResetMethod:
4313  {
4315  sync;
4316 
4317  for (y=0; y < (ssize_t) image->rows; y++)
4318  {
4319  register PixelPacket
4320  *magick_restrict q;
4321 
4322  register ssize_t
4323  x;
4324 
4325  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4326  exception);
4327  if (q == (PixelPacket *) NULL)
4328  break;
4329  for (x=0; x < (ssize_t) image->columns; x++)
4330  {
4331  (void) GetFillColor(draw_info,x,y,q);
4332  q++;
4333  }
4334  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4335  if (sync == MagickFalse)
4336  break;
4337  }
4338  break;
4339  }
4340  }
4341  break;
4342  }
4343  case MattePrimitive:
4344  {
4345  if (image->matte == MagickFalse)
4347  switch (primitive_info->method)
4348  {
4349  case PointMethod:
4350  default:
4351  {
4352  PixelPacket
4353  pixel;
4354 
4355  PixelPacket
4356  *q;
4357 
4358  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4359  if (q == (PixelPacket *) NULL)
4360  break;
4361  (void) GetFillColor(draw_info,x,y,&pixel);
4362  SetPixelOpacity(q,pixel.opacity);
4363  status&=SyncCacheViewAuthenticPixels(image_view,exception);
4364  break;
4365  }
4366  case ReplaceMethod:
4367  {
4369  sync;
4370 
4371  PixelPacket
4372  pixel,
4373  target;
4374 
4375  status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4376  for (y=0; y < (ssize_t) image->rows; y++)
4377  {
4378  register PixelPacket
4379  *magick_restrict q;
4380 
4381  register ssize_t
4382  x;
4383 
4384  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4385  exception);
4386  if (q == (PixelPacket *) NULL)
4387  break;
4388  for (x=0; x < (ssize_t) image->columns; x++)
4389  {
4390  if (IsColorSimilar(image,q,&target) == MagickFalse)
4391  {
4392  q++;
4393  continue;
4394  }
4395  (void) GetFillColor(draw_info,x,y,&pixel);
4396  SetPixelOpacity(q,pixel.opacity);
4397  q++;
4398  }
4399  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4400  if (sync == MagickFalse)
4401  break;
4402  }
4403  break;
4404  }
4405  case FloodfillMethod:
4406  case FillToBorderMethod:
4407  {
4409  target;
4410 
4411  (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4412  if (primitive_info->method == FillToBorderMethod)
4413  {
4414  target.red=(MagickRealType) draw_info->border_color.red;
4415  target.green=(MagickRealType) draw_info->border_color.green;
4416  target.blue=(MagickRealType) draw_info->border_color.blue;
4417  }
4418  status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
4419  y,primitive_info->method == FloodfillMethod ? MagickFalse :
4420  MagickTrue);
4421  break;
4422  }
4423  case ResetMethod:
4424  {
4426  sync;
4427 
4428  PixelPacket
4429  pixel;
4430 
4431  for (y=0; y < (ssize_t) image->rows; y++)
4432  {
4433  register PixelPacket
4434  *magick_restrict q;
4435 
4436  register ssize_t
4437  x;
4438 
4439  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4440  exception);
4441  if (q == (PixelPacket *) NULL)
4442  break;
4443  for (x=0; x < (ssize_t) image->columns; x++)
4444  {
4445  (void) GetFillColor(draw_info,x,y,&pixel);
4446  SetPixelOpacity(q,pixel.opacity);
4447  q++;
4448  }
4449  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4450  if (sync == MagickFalse)
4451  break;
4452  }
4453  break;
4454  }
4455  }
4456  break;
4457  }
4458  case TextPrimitive:
4459  {
4460  char
4461  geometry[MaxTextExtent];
4462 
4463  DrawInfo
4464  *clone_info;
4465 
4466  if (primitive_info->text == (char *) NULL)
4467  break;
4468  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4469  (void) CloneString(&clone_info->text,primitive_info->text);
4470  (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4471  primitive_info->point.x,primitive_info->point.y);
4472  (void) CloneString(&clone_info->geometry,geometry);
4473  status&=AnnotateImage(image,clone_info);
4474  clone_info=DestroyDrawInfo(clone_info);
4475  break;
4476  }
4477  case ImagePrimitive:
4478  {
4479  AffineMatrix
4480  affine;
4481 
4482  char
4483  composite_geometry[MaxTextExtent];
4484 
4485  Image
4486  *composite_image;
4487 
4488  ImageInfo
4489  *clone_info;
4490 
4492  geometry;
4493 
4494  ssize_t
4495  x1,
4496  y1;
4497 
4498  if (primitive_info->text == (char *) NULL)
4499  break;
4500  clone_info=AcquireImageInfo();
4501  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4502  composite_image=ReadInlineImage(clone_info,primitive_info->text,
4503  &image->exception);
4504  else
4505  {
4506  (void) CopyMagickString(clone_info->filename,primitive_info->text,
4507  MaxTextExtent);
4508  composite_image=ReadImage(clone_info,&image->exception);
4509  }
4510  clone_info=DestroyImageInfo(clone_info);
4511  if (composite_image == (Image *) NULL)
4512  break;
4513  (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4514  NULL,(void *) NULL);
4515  x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4516  y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4517  if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4518  ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4519  {
4520  char
4521  geometry[MaxTextExtent];
4522 
4523  /*
4524  Resize image.
4525  */
4526  (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4527  primitive_info[1].point.x,primitive_info[1].point.y);
4528  composite_image->filter=image->filter;
4529  (void) TransformImage(&composite_image,(char *) NULL,geometry);
4530  }
4531  if (composite_image->matte == MagickFalse)
4532  (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
4533  if (draw_info->opacity != OpaqueOpacity)
4534  (void) SetImageOpacity(composite_image,draw_info->opacity);
4535  SetGeometry(image,&geometry);
4536  image->gravity=draw_info->gravity;
4537  geometry.x=x;
4538  geometry.y=y;
4539  (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4540  "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4541  composite_image->rows,(double) geometry.x,(double) geometry.y);
4542  (void) ParseGravityGeometry(image,composite_geometry,&geometry,
4543  &image->exception);
4544  affine=draw_info->affine;
4545  affine.tx=(double) geometry.x;
4546  affine.ty=(double) geometry.y;
4547  composite_image->interpolate=image->interpolate;
4548  if (draw_info->compose == OverCompositeOp)
4549  (void) DrawAffineImage(image,composite_image,&affine);
4550  else
4551  (void) CompositeImage(image,draw_info->compose,composite_image,
4552  geometry.x,geometry.y);
4553  composite_image=DestroyImage(composite_image);
4554  break;
4555  }
4556  default:
4557  {
4558  double
4559  mid,
4560  scale;
4561 
4562  DrawInfo
4563  *clone_info;
4564 
4565  if (IsEventLogging() != MagickFalse)
4566  LogPrimitiveInfo(primitive_info);
4567  scale=ExpandAffine(&draw_info->affine);
4568  if ((draw_info->dash_pattern != (double *) NULL) &&
4569  (draw_info->dash_pattern[0] != 0.0) &&
4570  ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4571  (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
4572  {
4573  /*
4574  Draw dash polygon.
4575  */
4576  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4577  clone_info->stroke_width=0.0;
4578  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4579  status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
4580  clone_info=DestroyDrawInfo(clone_info);
4581  (void) DrawDashPolygon(draw_info,primitive_info,image);
4582  break;
4583  }
4584  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4585  if ((mid > 1.0) &&
4586  (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
4587  {
4589  closed_path;
4590 
4591  /*
4592  Draw strokes while respecting line cap/join attributes.
4593  */
4594  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4595  closed_path=
4596  (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4597  (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4599  i=(ssize_t) primitive_info[0].coordinates;
4600  if ((((draw_info->linecap == RoundCap) ||
4601  (closed_path != MagickFalse)) &&
4602  (draw_info->linejoin == RoundJoin)) ||
4603  (primitive_info[i].primitive != UndefinedPrimitive))
4604  {
4605  (void) DrawPolygonPrimitive(image,draw_info,primitive_info);
4606  break;
4607  }
4608  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4609  clone_info->stroke_width=0.0;
4610  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4611  status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
4612  clone_info=DestroyDrawInfo(clone_info);
4613  status&=DrawStrokePolygon(image,draw_info,primitive_info);
4614  break;
4615  }
4616  status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
4617  break;
4618  }
4619  }
4620  image_view=DestroyCacheView(image_view);
4621  if (image->debug != MagickFalse)
4622  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4623  return(status != 0 ? MagickTrue : MagickFalse);
4624 }
4625 
4626 /*
4627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4628 % %
4629 % %
4630 % %
4631 + D r a w S t r o k e P o l y g o n %
4632 % %
4633 % %
4634 % %
4635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4636 %
4637 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4638 % the image while respecting the line cap and join attributes.
4639 %
4640 % The format of the DrawStrokePolygon method is:
4641 %
4642 % MagickBooleanType DrawStrokePolygon(Image *image,
4643 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4644 %
4645 % A description of each parameter follows:
4646 %
4647 % o image: the image.
4648 %
4649 % o draw_info: the draw info.
4650 %
4651 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4652 %
4653 %
4654 */
4655 
4656 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4657  const PrimitiveInfo *primitive_info)
4658 {
4660  linecap[5];
4661 
4662  register ssize_t
4663  i;
4664 
4665  for (i=0; i < 4; i++)
4666  linecap[i]=(*primitive_info);
4667  linecap[0].coordinates=4;
4668  linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4669  linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4670  linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4671  linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4672  linecap[4].primitive=UndefinedPrimitive;
4673  (void) DrawPolygonPrimitive(image,draw_info,linecap);
4674 }
4675 
4677  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4678 {
4679  DrawInfo
4680  *clone_info;
4681 
4683  closed_path;
4684 
4686  status;
4687 
4689  *stroke_polygon;
4690 
4691  register const PrimitiveInfo
4692  *p,
4693  *q;
4694 
4695  /*
4696  Draw stroked polygon.
4697  */
4698  if (image->debug != MagickFalse)
4700  " begin draw-stroke-polygon");
4701  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4702  clone_info->fill=draw_info->stroke;
4703  if (clone_info->fill_pattern != (Image *) NULL)
4704  clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4705  if (clone_info->stroke_pattern != (Image *) NULL)
4706  clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4707  MagickTrue,&clone_info->stroke_pattern->exception);
4708  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4709  clone_info->stroke_width=0.0;
4710  clone_info->fill_rule=NonZeroRule;
4711  status=MagickTrue;
4712  for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4713  {
4714  stroke_polygon=TraceStrokePolygon(draw_info,p);
4715  status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
4716  if (status == 0)
4717  break;
4718  stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4719  q=p+p->coordinates-1;
4720  closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4722  if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4723  {
4724  DrawRoundLinecap(image,draw_info,p);
4725  DrawRoundLinecap(image,draw_info,q);
4726  }
4727  }
4728  clone_info=DestroyDrawInfo(clone_info);
4729  if (image->debug != MagickFalse)
4731  " end draw-stroke-polygon");
4732  return(status != 0 ? MagickTrue : MagickFalse);
4733 }
4734 
4735 /*
4736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4737 % %
4738 % %
4739 % %
4740 % G e t A f f i n e M a t r i x %
4741 % %
4742 % %
4743 % %
4744 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4745 %
4746 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4747 % matrix.
4748 %
4749 % The format of the GetAffineMatrix method is:
4750 %
4751 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4752 %
4753 % A description of each parameter follows:
4754 %
4755 % o affine_matrix: the affine matrix.
4756 %
4757 */
4759 {
4760  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4761  assert(affine_matrix != (AffineMatrix *) NULL);
4762  (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4763  affine_matrix->sx=1.0;
4764  affine_matrix->sy=1.0;
4765 }
4766 
4767 /*
4768 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4769 % %
4770 % %
4771 % %
4772 + G e t D r a w I n f o %
4773 % %
4774 % %
4775 % %
4776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4777 %
4778 % GetDrawInfo() initializes draw_info to default values from image_info.
4779 %
4780 % The format of the GetDrawInfo method is:
4781 %
4782 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4783 %
4784 % A description of each parameter follows:
4785 %
4786 % o image_info: the image info..
4787 %
4788 % o draw_info: the draw info.
4789 %
4790 */
4791 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4792 {
4793  const char
4794  *option;
4795 
4797  *exception;
4798 
4799  ImageInfo
4800  *clone_info;
4801 
4802  /*
4803  Initialize draw attributes.
4804  */
4805  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4806  assert(draw_info != (DrawInfo *) NULL);
4807  (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4808  clone_info=CloneImageInfo(image_info);
4809  GetAffineMatrix(&draw_info->affine);
4810  exception=AcquireExceptionInfo();
4811  (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
4812  (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
4813  draw_info->stroke_antialias=clone_info->antialias;
4814  draw_info->stroke_width=1.0;
4815  draw_info->opacity=OpaqueOpacity;
4816  draw_info->fill_rule=EvenOddRule;
4817  draw_info->linecap=ButtCap;
4818  draw_info->linejoin=MiterJoin;
4819  draw_info->miterlimit=10;
4820  draw_info->decorate=NoDecoration;
4821  if (clone_info->font != (char *) NULL)
4822  draw_info->font=AcquireString(clone_info->font);
4823  if (clone_info->density != (char *) NULL)
4824  draw_info->density=AcquireString(clone_info->density);
4825  draw_info->text_antialias=clone_info->antialias;
4826  draw_info->pointsize=12.0;
4827  if (clone_info->pointsize != 0.0)
4828  draw_info->pointsize=clone_info->pointsize;
4830  draw_info->border_color=clone_info->border_color;
4831  draw_info->compose=OverCompositeOp;
4832  if (clone_info->server_name != (char *) NULL)
4833  draw_info->server_name=AcquireString(clone_info->server_name);
4834  draw_info->render=MagickTrue;
4835  draw_info->debug=IsEventLogging();
4836  option=GetImageOption(clone_info,"direction");
4837  if (option != (const char *) NULL)
4840  else
4841  draw_info->direction=UndefinedDirection;
4842  option=GetImageOption(clone_info,"encoding");
4843  if (option != (const char *) NULL)
4844  (void) CloneString(&draw_info->encoding,option);
4845  option=GetImageOption(clone_info,"family");
4846  if (option != (const char *) NULL)
4847  (void) CloneString(&draw_info->family,option);
4848  option=GetImageOption(clone_info,"fill");
4849  if (option != (const char *) NULL)
4850  (void) QueryColorDatabase(option,&draw_info->fill,exception);
4851  option=GetImageOption(clone_info,"gravity");
4852  if (option != (const char *) NULL)
4854  MagickFalse,option);
4855  option=GetImageOption(clone_info,"interline-spacing");
4856  if (option != (const char *) NULL)
4857  draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4858  option=GetImageOption(clone_info,"interword-spacing");
4859  if (option != (const char *) NULL)
4860  draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4861  option=GetImageOption(clone_info,"kerning");
4862  if (option != (const char *) NULL)
4863  draw_info->kerning=StringToDouble(option,(char **) NULL);
4864  option=GetImageOption(clone_info,"stroke");
4865  if (option != (const char *) NULL)
4866  (void) QueryColorDatabase(option,&draw_info->stroke,exception);
4867  option=GetImageOption(clone_info,"strokewidth");
4868  if (option != (const char *) NULL)
4869  draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4870  option=GetImageOption(clone_info,"style");
4871  if (option != (const char *) NULL)
4873  MagickFalse,option);
4874  option=GetImageOption(clone_info,"undercolor");
4875  if (option != (const char *) NULL)
4876  (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
4877  option=GetImageOption(clone_info,"weight");
4878  if (option != (const char *) NULL)
4879  {
4880  ssize_t
4881  weight;
4882 
4884  if (weight == -1)
4885  weight=StringToUnsignedLong(option);
4886  draw_info->weight=(size_t) weight;
4887  }
4888  exception=DestroyExceptionInfo(exception);
4889  draw_info->signature=MagickSignature;
4890  clone_info=DestroyImageInfo(clone_info);
4891 }
4892 
4893 /*
4894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4895 % %
4896 % %
4897 % %
4898 + P e r m u t a t e %
4899 % %
4900 % %
4901 % %
4902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4903 %
4904 % Permutate() returns the permuation of the (n,k).
4905 %
4906 % The format of the Permutate method is:
4907 %
4908 % void Permutate(ssize_t n,ssize_t k)
4909 %
4910 % A description of each parameter follows:
4911 %
4912 % o n:
4913 %
4914 % o k:
4915 %
4916 %
4917 */
4918 static inline double Permutate(const ssize_t n,const ssize_t k)
4919 {
4920  double
4921  r;
4922 
4923  register ssize_t
4924  i;
4925 
4926  r=1.0;
4927  for (i=k+1; i <= n; i++)
4928  r*=i;
4929  for (i=1; i <= (n-k); i++)
4930  r/=i;
4931  return(r);
4932 }
4933 
4934 /*
4935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4936 % %
4937 % %
4938 % %
4939 + T r a c e P r i m i t i v e %
4940 % %
4941 % %
4942 % %
4943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4944 %
4945 % TracePrimitive is a collection of methods for generating graphic
4946 % primitives such as arcs, ellipses, paths, etc.
4947 %
4948 */
4949 
4950 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4951  const PointInfo end,const PointInfo degrees)
4952 {
4953  PointInfo
4954  center,
4955  radii;
4956 
4957  center.x=0.5*(end.x+start.x);
4958  center.y=0.5*(end.y+start.y);
4959  radii.x=fabs(center.x-start.x);
4960  radii.y=fabs(center.y-start.y);
4961  TraceEllipse(primitive_info,center,radii,degrees);
4962 }
4963 
4964 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4965  const PointInfo end,const PointInfo arc,const double angle,
4966  const MagickBooleanType large_arc,const MagickBooleanType sweep)
4967 {
4968  double
4969  alpha,
4970  beta,
4971  delta,
4972  factor,
4973  gamma,
4974  theta;
4975 
4976  PointInfo
4977  center,
4978  points[3],
4979  radii;
4980 
4981  register double
4982  cosine,
4983  sine;
4984 
4985  register PrimitiveInfo
4986  *p;
4987 
4988  register ssize_t
4989  i;
4990 
4991  size_t
4992  arc_segments;
4993 
4994  if ((start.x == end.x) && (start.y == end.y))
4995  {
4996  TracePoint(primitive_info,end);
4997  return;
4998  }
4999  radii.x=fabs(arc.x);
5000  radii.y=fabs(arc.y);
5001  if ((radii.x == 0.0) || (radii.y == 0.0))
5002  {
5003  TraceLine(primitive_info,start,end);
5004  return;
5005  }
5006  cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5007  sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5008  center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5009  center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5010  delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5011  (radii.y*radii.y);
5012  if (delta < MagickEpsilon)
5013  {
5014  TraceLine(primitive_info,start,end);
5015  return;
5016  }
5017  if (delta > 1.0)
5018  {
5019  radii.x*=sqrt((double) delta);
5020  radii.y*=sqrt((double) delta);
5021  }
5022  points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5023  points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5024  points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5025  points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5026  alpha=points[1].x-points[0].x;
5027  beta=points[1].y-points[0].y;
5028  factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5029  if (factor <= 0.0)
5030  factor=0.0;
5031  else
5032  {
5033  factor=sqrt((double) factor);
5034  if (sweep == large_arc)
5035  factor=(-factor);
5036  }
5037  center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5038  center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5039  alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5040  theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5041  if ((theta < 0.0) && (sweep != MagickFalse))
5042  theta+=2.0*MagickPI;
5043  else
5044  if ((theta > 0.0) && (sweep == MagickFalse))
5045  theta-=2.0*MagickPI;
5046  arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5047  MagickEpsilon))));
5048  p=primitive_info;
5049  for (i=0; i < (ssize_t) arc_segments; i++)
5050  {
5051  beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5052  gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5053  sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5054  sin(fmod((double) beta,DegreesToRadians(360.0)));
5055  points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5056  arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5057  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5058  points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5059  arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5060  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5061  points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5062  theta/arc_segments),DegreesToRadians(360.0))));
5063  points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5064  theta/arc_segments),DegreesToRadians(360.0))));
5065  points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5066  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5067  points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5068  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5069  p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5070  p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5071  (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5072  points[0].y);
5073  (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5074  points[0].y);
5075  (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5076  points[1].y);
5077  (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5078  points[1].y);
5079  (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5080  points[2].y);
5081  (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5082  points[2].y);
5083  if (i == (ssize_t) (arc_segments-1))
5084  (p+3)->point=end;
5085  TraceBezier(p,4);
5086  p+=p->coordinates;
5087  }
5088  primitive_info->coordinates=(size_t) (p-primitive_info);
5089  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5090  {
5091  p->primitive=primitive_info->primitive;
5092  p--;
5093  }
5094 }
5095 
5096 static void TraceBezier(PrimitiveInfo *primitive_info,
5097  const size_t number_coordinates)
5098 {
5099  double
5100  alpha,
5101  *coefficients,
5102  weight;
5103 
5104  PointInfo
5105  end,
5106  point,
5107  *points;
5108 
5109  register PrimitiveInfo
5110  *p;
5111 
5112  register ssize_t
5113  i,
5114  j;
5115 
5116  size_t
5117  control_points,
5118  quantum;
5119 
5120  /*
5121  Allocate coeficients.
5122  */
5123  quantum=number_coordinates;
5124  for (i=0; i < (ssize_t) number_coordinates; i++)
5125  {
5126  for (j=i+1; j < (ssize_t) number_coordinates; j++)
5127  {
5128  alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5129  if (alpha > (double) quantum)
5130  quantum=(size_t) alpha;
5131  alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5132  if (alpha > (double) quantum)
5133  quantum=(size_t) alpha;
5134  }
5135  }
5136  quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5137  (double) BezierQuantum);
5138  control_points=quantum*number_coordinates;
5139  coefficients=(double *) AcquireQuantumMemory((size_t)
5140  number_coordinates,sizeof(*coefficients));
5141  points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5142  sizeof(*points));
5143  if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
5144  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5145  /*
5146  Compute bezier points.
5147  */
5148  end=primitive_info[number_coordinates-1].point;
5149  for (i=0; i < (ssize_t) number_coordinates; i++)
5150  coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5151  weight=0.0;
5152  for (i=0; i < (ssize_t) control_points; i++)
5153  {
5154  p=primitive_info;
5155  point.x=0.0;
5156  point.y=0.0;
5157  alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5158  for (j=0; j < (ssize_t) number_coordinates; j++)
5159  {
5160  point.x+=alpha*coefficients[j]*p->point.x;
5161  point.y+=alpha*coefficients[j]*p->point.y;
5162  alpha*=weight/(1.0-weight);
5163  p++;
5164  }
5165  points[i]=point;
5166  weight+=1.0/control_points;
5167  }
5168  /*
5169  Bezier curves are just short segmented polys.
5170  */
5171  p=primitive_info;
5172  for (i=0; i < (ssize_t) control_points; i++)
5173  {
5174  TracePoint(p,points[i]);
5175  p+=p->coordinates;
5176  }
5177  TracePoint(p,end);
5178  p+=p->coordinates;
5179  primitive_info->coordinates=(size_t) (p-primitive_info);
5180  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5181  {
5182  p->primitive=primitive_info->primitive;
5183  p--;
5184  }
5185  points=(PointInfo *) RelinquishMagickMemory(points);
5186  coefficients=(double *) RelinquishMagickMemory(coefficients);
5187 }
5188 
5189 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5190  const PointInfo end)
5191 {
5192  double
5193  alpha,
5194  beta,
5195  radius;
5196 
5197  PointInfo
5198  offset,
5199  degrees;
5200 
5201  alpha=end.x-start.x;
5202  beta=end.y-start.y;
5203  radius=hypot((double) alpha,(double) beta);
5204  offset.x=(double) radius;
5205  offset.y=(double) radius;
5206  degrees.x=0.0;
5207  degrees.y=360.0;
5208  TraceEllipse(primitive_info,start,offset,degrees);
5209 }
5210 
5211 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5212  const PointInfo stop,const PointInfo degrees)
5213 {
5214  double
5215  delta,
5216  step,
5217  y;
5218 
5219  PointInfo
5220  angle,
5221  point;
5222 
5223  register PrimitiveInfo
5224  *p;
5225 
5226  register ssize_t
5227  i;
5228 
5229  /*
5230  Ellipses are just short segmented polys.
5231  */
5232  if ((stop.x == 0.0) && (stop.y == 0.0))
5233  {
5234  TracePoint(primitive_info,start);
5235  return;
5236  }
5237  delta=2.0/MagickMax(stop.x,stop.y);
5238  step=MagickPI/8.0;
5239  if ((delta >= 0.0) && (delta < (MagickPI/8.0)))
5240  step=MagickPI/(4*(MagickPI/delta/2+0.5));
5241  angle.x=DegreesToRadians(degrees.x);
5242  y=degrees.y;
5243  while (y < degrees.x)
5244  y+=360.0;
5245  angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5246  for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5247  {
5248  point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5249  point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5250  TracePoint(p,point);
5251  p+=p->coordinates;
5252  }
5253  point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5254  point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5255  TracePoint(p,point);
5256  p+=p->coordinates;
5257  primitive_info->coordinates=(size_t) (p-primitive_info);
5258  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5259  {
5260  p->primitive=primitive_info->primitive;
5261  p--;
5262  }
5263 }
5264 
5265 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5266  const PointInfo end)
5267 {
5268  TracePoint(primitive_info,start);
5269  if ((fabs(start.x-end.x) < MagickEpsilon) &&
5270  (fabs(start.y-end.y) < MagickEpsilon))
5271  {
5272  primitive_info->primitive=PointPrimitive;
5273  primitive_info->coordinates=1;
5274  return;
5275  }
5276  TracePoint(primitive_info+1,end);
5277  (primitive_info+1)->primitive=primitive_info->primitive;
5278  primitive_info->coordinates=2;
5279 }
5280 
5281 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5282 {
5283  char
5284  token[MaxTextExtent];
5285 
5286  const char
5287  *p;
5288 
5289  double
5290  x,
5291  y;
5292 
5293  int
5294  attribute,
5295  last_attribute;
5296 
5297  PointInfo
5298  end = {0.0, 0.0},
5299  points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5300  point = {0.0, 0.0},
5301  start = {0.0, 0.0};
5302 
5304  primitive_type;
5305 
5306  register PrimitiveInfo
5307  *q;
5308 
5309  register ssize_t
5310  i;
5311 
5312  size_t
5313  number_coordinates,
5314  z_count;
5315 
5316  attribute=0;
5317  number_coordinates=0;
5318  z_count=0;
5319  primitive_type=primitive_info->primitive;
5320  q=primitive_info;
5321  for (p=path; *p != '\0'; )
5322  {
5323  while (isspace((int) ((unsigned char) *p)) != 0)
5324  p++;
5325  if (*p == '\0')
5326  break;
5327  last_attribute=attribute;
5328  attribute=(int) (*p++);
5329  switch (attribute)
5330  {
5331  case 'a':
5332  case 'A':
5333  {
5334  double
5335  angle;
5336 
5338  large_arc,
5339  sweep;
5340 
5341  PointInfo
5342  arc;
5343 
5344  /*
5345  Compute arc points.
5346  */
5347  do
5348  {
5349  GetMagickToken(p,&p,token);
5350  if (*token == ',')
5351  GetMagickToken(p,&p,token);
5352  arc.x=StringToDouble(token,(char **) NULL);
5353  GetMagickToken(p,&p,token);
5354  if (*token == ',')
5355  GetMagickToken(p,&p,token);
5356  arc.y=StringToDouble(token,(char **) NULL);
5357  GetMagickToken(p,&p,token);
5358  if (*token == ',')
5359  GetMagickToken(p,&p,token);
5360  angle=StringToDouble(token,(char **) NULL);
5361  GetMagickToken(p,&p,token);
5362  if (*token == ',')
5363  GetMagickToken(p,&p,token);
5364  large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5365  GetMagickToken(p,&p,token);
5366  if (*token == ',')
5367