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