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