]> 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,p->bounds.x2,
425       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   ExceptionInfo
1089     *exception;
1090
1091   long
1092     y;
1093
1094   MagickBooleanType
1095     status;
1096
1097   MagickPixelPacket
1098     zero;
1099
1100   PointInfo
1101     extent[4],
1102     min,
1103     max,
1104     point;
1105
1106   register long
1107     i;
1108
1109   ResampleFilter
1110     **resample_filter;
1111
1112   SegmentInfo
1113     edge;
1114
1115   CacheView
1116     *image_view,
1117     *source_view;
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=RoundToQuantum((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=(unsigned long) StringToLong(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=RoundToQuantum((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,"%gx%g%+f%+f",
2377                   MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2378                   MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2379                   bounds.x1,bounds.y1);
2380                 (void) SetImageArtifact(image,key,geometry);
2381                 GetMagickToken(q,&q,token);
2382                 break;
2383               }
2384             if (LocaleCompare("pattern",token) == 0)
2385               {
2386                 RectangleInfo
2387                   bounds;
2388
2389                 GetMagickToken(q,&q,token);
2390                 (void) CopyMagickString(name,token,MaxTextExtent);
2391                 GetMagickToken(q,&q,token);
2392                 bounds.x=(long) (StringToDouble(token)+0.5);
2393                 GetMagickToken(q,&q,token);
2394                 if (*token == ',')
2395                   GetMagickToken(q,&q,token);
2396                 bounds.y=(long) (StringToDouble(token)+0.5);
2397                 GetMagickToken(q,&q,token);
2398                 if (*token == ',')
2399                   GetMagickToken(q,&q,token);
2400                 bounds.width=(unsigned long) (StringToDouble(token)+0.5);
2401                 GetMagickToken(q,&q,token);
2402                 if (*token == ',')
2403                   GetMagickToken(q,&q,token);
2404                 bounds.height=(unsigned long) (StringToDouble(token)+0.5);
2405                 for (p=q; *q != '\0'; )
2406                 {
2407                   GetMagickToken(q,&q,token);
2408                   if (LocaleCompare(token,"pop") != 0)
2409                     continue;
2410                   GetMagickToken(q,(const char **) NULL,token);
2411                   if (LocaleCompare(token,"pattern") != 0)
2412                     continue;
2413                   break;
2414                 }
2415                 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2416                 (void) FormatMagickString(key,MaxTextExtent,"%s",name);
2417                 (void) SetImageArtifact(image,key,token);
2418                 (void) FormatMagickString(key,MaxTextExtent,"%s-geometry",name);
2419                 (void) FormatMagickString(geometry,MaxTextExtent,
2420                   "%lux%lu%+ld%+ld",bounds.width,bounds.height,bounds.x,
2421                   bounds.y);
2422                 (void) SetImageArtifact(image,key,geometry);
2423                 GetMagickToken(q,&q,token);
2424                 break;
2425               }
2426             if (LocaleCompare("graphic-context",token) == 0)
2427               {
2428                 n++;
2429                 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2430                   graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2431                 if (graphic_context == (DrawInfo **) NULL)
2432                   {
2433                     (void) ThrowMagickException(&image->exception,
2434                       GetMagickModule(),ResourceLimitError,
2435                       "MemoryAllocationFailed","`%s'",image->filename);
2436                     break;
2437                   }
2438                 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2439                   graphic_context[n-1]);
2440                 break;
2441               }
2442             if (LocaleCompare("defs",token) == 0)
2443               break;
2444             status=MagickFalse;
2445             break;
2446           }
2447         status=MagickFalse;
2448         break;
2449       }
2450       case 'r':
2451       case 'R':
2452       {
2453         if (LocaleCompare("rectangle",keyword) == 0)
2454           {
2455             primitive_type=RectanglePrimitive;
2456             break;
2457           }
2458         if (LocaleCompare("rotate",keyword) == 0)
2459           {
2460             GetMagickToken(q,&q,token);
2461             angle=StringToDouble(token);
2462             affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2463             affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2464             affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2465             affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2466             break;
2467           }
2468         if (LocaleCompare("roundRectangle",keyword) == 0)
2469           {
2470             primitive_type=RoundRectanglePrimitive;
2471             break;
2472           }
2473         status=MagickFalse;
2474         break;
2475       }
2476       case 's':
2477       case 'S':
2478       {
2479         if (LocaleCompare("scale",keyword) == 0)
2480           {
2481             GetMagickToken(q,&q,token);
2482             affine.sx=StringToDouble(token);
2483             GetMagickToken(q,&q,token);
2484             if (*token == ',')
2485               GetMagickToken(q,&q,token);
2486             affine.sy=StringToDouble(token);
2487             break;
2488           }
2489         if (LocaleCompare("skewX",keyword) == 0)
2490           {
2491             GetMagickToken(q,&q,token);
2492             angle=StringToDouble(token);
2493             affine.ry=sin(DegreesToRadians(angle));
2494             break;
2495           }
2496         if (LocaleCompare("skewY",keyword) == 0)
2497           {
2498             GetMagickToken(q,&q,token);
2499             angle=StringToDouble(token);
2500             affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2501             break;
2502           }
2503         if (LocaleCompare("stop-color",keyword) == 0)
2504           {
2505             PixelPacket
2506               stop_color;
2507
2508             GetMagickToken(q,&q,token);
2509             (void) QueryColorDatabase(token,&stop_color,&image->exception);
2510             (void) GradientImage(image,LinearGradient,ReflectSpread,
2511               &start_color,&stop_color);
2512             start_color=stop_color;
2513             GetMagickToken(q,&q,token);
2514             break;
2515           }
2516         if (LocaleCompare("stroke",keyword) == 0)
2517           {
2518             GetMagickToken(q,&q,token);
2519             (void) FormatMagickString(pattern,MaxTextExtent,"%s",token);
2520             if (GetImageArtifact(image,pattern) != (const char *) NULL)
2521               (void) DrawPatternPath(image,draw_info,token,
2522                 &graphic_context[n]->stroke_pattern);
2523             else
2524               {
2525                 status=QueryColorDatabase(token,&graphic_context[n]->stroke,
2526                   &image->exception);
2527                 if (status == MagickFalse)
2528                   {
2529                     ImageInfo
2530                       *pattern_info;
2531
2532                     pattern_info=AcquireImageInfo();
2533                     (void) CopyMagickString(pattern_info->filename,token,
2534                       MaxTextExtent);
2535                     graphic_context[n]->stroke_pattern=
2536                       ReadImage(pattern_info,&image->exception);
2537                     CatchException(&image->exception);
2538                     pattern_info=DestroyImageInfo(pattern_info);
2539                   }
2540               }
2541             break;
2542           }
2543         if (LocaleCompare("stroke-antialias",keyword) == 0)
2544           {
2545             GetMagickToken(q,&q,token);
2546             graphic_context[n]->stroke_antialias=
2547               StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2548             break;
2549           }
2550         if (LocaleCompare("stroke-dasharray",keyword) == 0)
2551           {
2552             if (graphic_context[n]->dash_pattern != (double *) NULL)
2553               graphic_context[n]->dash_pattern=(double *)
2554                 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2555             if (IsPoint(q) != MagickFalse)
2556               {
2557                 const char
2558                   *p;
2559
2560                 p=q;
2561                 GetMagickToken(p,&p,token);
2562                 if (*token == ',')
2563                   GetMagickToken(p,&p,token);
2564                 for (x=0; IsPoint(token) != MagickFalse; x++)
2565                 {
2566                   GetMagickToken(p,&p,token);
2567                   if (*token == ',')
2568                     GetMagickToken(p,&p,token);
2569                 }
2570                 graphic_context[n]->dash_pattern=(double *)
2571                   AcquireQuantumMemory((size_t) (2UL*x+1UL),
2572                   sizeof(*graphic_context[n]->dash_pattern));
2573                 if (graphic_context[n]->dash_pattern == (double *) NULL)
2574                   {
2575                     (void) ThrowMagickException(&image->exception,
2576                       GetMagickModule(),ResourceLimitError,
2577                       "MemoryAllocationFailed","`%s'",image->filename);
2578                     break;
2579                   }
2580                 for (j=0; j < x; j++)
2581                 {
2582                   GetMagickToken(q,&q,token);
2583                   if (*token == ',')
2584                     GetMagickToken(q,&q,token);
2585                   graphic_context[n]->dash_pattern[j]=StringToDouble(token);
2586                 }
2587                 if ((x & 0x01) != 0)
2588                   for ( ; j < (2*x); j++)
2589                     graphic_context[n]->dash_pattern[j]=
2590                       graphic_context[n]->dash_pattern[j-x];
2591                 graphic_context[n]->dash_pattern[j]=0.0;
2592                 break;
2593               }
2594             GetMagickToken(q,&q,token);
2595             break;
2596           }
2597         if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2598           {
2599             GetMagickToken(q,&q,token);
2600             graphic_context[n]->dash_offset=StringToDouble(token);
2601             break;
2602           }
2603         if (LocaleCompare("stroke-linecap",keyword) == 0)
2604           {
2605             long
2606               linecap;
2607
2608             GetMagickToken(q,&q,token);
2609             linecap=ParseMagickOption(MagickLineCapOptions,MagickFalse,token);
2610             if (linecap == -1)
2611               {
2612                 status=MagickFalse;
2613                 break;
2614               }
2615             graphic_context[n]->linecap=(LineCap) linecap;
2616             break;
2617           }
2618         if (LocaleCompare("stroke-linejoin",keyword) == 0)
2619           {
2620             long
2621               linejoin;
2622
2623             GetMagickToken(q,&q,token);
2624             linejoin=ParseMagickOption(MagickLineJoinOptions,MagickFalse,token);
2625             if (linejoin == -1)
2626               {
2627                 status=MagickFalse;
2628                 break;
2629               }
2630             graphic_context[n]->linejoin=(LineJoin) linejoin;
2631             break;
2632           }
2633         if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2634           {
2635             GetMagickToken(q,&q,token);
2636             graphic_context[n]->miterlimit=(unsigned long) StringToLong(token);
2637             break;
2638           }
2639         if (LocaleCompare("stroke-opacity",keyword) == 0)
2640           {
2641             GetMagickToken(q,&q,token);
2642             factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2643             graphic_context[n]->stroke.opacity=RoundToQuantum((MagickRealType)
2644               QuantumRange*(1.0-factor*StringToDouble(token)));
2645             break;
2646           }
2647         if (LocaleCompare("stroke-width",keyword) == 0)
2648           {
2649             GetMagickToken(q,&q,token);
2650             graphic_context[n]->stroke_width=StringToDouble(token);
2651             break;
2652           }
2653         status=MagickFalse;
2654         break;
2655       }
2656       case 't':
2657       case 'T':
2658       {
2659         if (LocaleCompare("text",keyword) == 0)
2660           {
2661             primitive_type=TextPrimitive;
2662             break;
2663           }
2664         if (LocaleCompare("text-align",keyword) == 0)
2665           {
2666             long
2667               align;
2668
2669             GetMagickToken(q,&q,token);
2670             align=ParseMagickOption(MagickAlignOptions,MagickFalse,token);
2671             if (align == -1)
2672               {
2673                 status=MagickFalse;
2674                 break;
2675               }
2676             graphic_context[n]->align=(AlignType) align;
2677             break;
2678           }
2679         if (LocaleCompare("text-anchor",keyword) == 0)
2680           {
2681             long
2682               align;
2683
2684             GetMagickToken(q,&q,token);
2685             align=ParseMagickOption(MagickAlignOptions,MagickFalse,token);
2686             if (align == -1)
2687               {
2688                 status=MagickFalse;
2689                 break;
2690               }
2691             graphic_context[n]->align=(AlignType) align;
2692             break;
2693           }
2694         if (LocaleCompare("text-antialias",keyword) == 0)
2695           {
2696             GetMagickToken(q,&q,token);
2697             graphic_context[n]->text_antialias=
2698               StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2699             break;
2700           }
2701         if (LocaleCompare("text-undercolor",keyword) == 0)
2702           {
2703             GetMagickToken(q,&q,token);
2704             (void) QueryColorDatabase(token,&graphic_context[n]->undercolor,
2705               &image->exception);
2706             break;
2707           }
2708         if (LocaleCompare("translate",keyword) == 0)
2709           {
2710             GetMagickToken(q,&q,token);
2711             affine.tx=StringToDouble(token);
2712             GetMagickToken(q,&q,token);
2713             if (*token == ',')
2714               GetMagickToken(q,&q,token);
2715             affine.ty=StringToDouble(token);
2716             break;
2717           }
2718         status=MagickFalse;
2719         break;
2720       }
2721       case 'v':
2722       case 'V':
2723       {
2724         if (LocaleCompare("viewbox",keyword) == 0)
2725           {
2726             GetMagickToken(q,&q,token);
2727             graphic_context[n]->viewbox.x=(long) (StringToDouble(token)+0.5);
2728             GetMagickToken(q,&q,token);
2729             if (*token == ',')
2730               GetMagickToken(q,&q,token);
2731             graphic_context[n]->viewbox.y=(long) (StringToDouble(token)+0.5);
2732             GetMagickToken(q,&q,token);
2733             if (*token == ',')
2734               GetMagickToken(q,&q,token);
2735             graphic_context[n]->viewbox.width=(unsigned long) (StringToDouble(token)+0.5);
2736             GetMagickToken(q,&q,token);
2737             if (*token == ',')
2738               GetMagickToken(q,&q,token);
2739             graphic_context[n]->viewbox.height=(unsigned long) (StringToDouble(token)+
2740               0.5);
2741             break;
2742           }
2743         status=MagickFalse;
2744         break;
2745       }
2746       default:
2747       {
2748         status=MagickFalse;
2749         break;
2750       }
2751     }
2752     if (status == MagickFalse)
2753       break;
2754     if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2755         (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2756       {
2757         graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2758         graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2759         graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2760         graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2761         graphic_context[n]->affine.tx=
2762           current.sx*affine.tx+current.ry*affine.ty+current.tx;
2763         graphic_context[n]->affine.ty=
2764           current.rx*affine.tx+current.sy*affine.ty+current.ty;
2765       }
2766     if (primitive_type == UndefinedPrimitive)
2767       {
2768         if (image->debug != MagickFalse)
2769           (void) LogMagickEvent(DrawEvent,GetMagickModule(),"  %.*s",
2770             (int) (q-p),p);
2771         continue;
2772       }
2773     /*
2774       Parse the primitive attributes.
2775     */
2776     i=0;
2777     j=0;
2778     primitive_info[0].point.x=0.0;
2779     primitive_info[0].point.y=0.0;
2780     for (x=0; *q != '\0'; x++)
2781     {
2782       /*
2783         Define points.
2784       */
2785       if (IsPoint(q) == MagickFalse)
2786         break;
2787       GetMagickToken(q,&q,token);
2788       point.x=StringToDouble(token);
2789       GetMagickToken(q,&q,token);
2790       if (*token == ',')
2791         GetMagickToken(q,&q,token);
2792       point.y=StringToDouble(token);
2793       GetMagickToken(q,(const char **) NULL,token);
2794       if (*token == ',')
2795         GetMagickToken(q,&q,token);
2796       primitive_info[i].primitive=primitive_type;
2797       primitive_info[i].point=point;
2798       primitive_info[i].coordinates=0;
2799       primitive_info[i].method=FloodfillMethod;
2800       i++;
2801       if (i < (long) number_points)
2802         continue;
2803       number_points<<=1;
2804       primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2805         (size_t) number_points,sizeof(*primitive_info));
2806       if (primitive_info == (PrimitiveInfo *) NULL)
2807         {
2808           (void) ThrowMagickException(&image->exception,GetMagickModule(),
2809             ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2810           break;
2811         }
2812     }
2813     primitive_info[j].primitive=primitive_type;
2814     primitive_info[j].coordinates=(unsigned long) x;
2815     primitive_info[j].method=FloodfillMethod;
2816     primitive_info[j].text=(char *) NULL;
2817     /*
2818       Circumscribe primitive within a circle.
2819     */
2820     bounds.x1=primitive_info[j].point.x;
2821     bounds.y1=primitive_info[j].point.y;
2822     bounds.x2=primitive_info[j].point.x;
2823     bounds.y2=primitive_info[j].point.y;
2824     for (k=1; k < (long) primitive_info[j].coordinates; k++)
2825     {
2826       point=primitive_info[j+k].point;
2827       if (point.x < bounds.x1)
2828         bounds.x1=point.x;
2829       if (point.y < bounds.y1)
2830         bounds.y1=point.y;
2831       if (point.x > bounds.x2)
2832         bounds.x2=point.x;
2833       if (point.y > bounds.y2)
2834         bounds.y2=point.y;
2835     }
2836     /*
2837       Speculate how many points our primitive might consume.
2838     */
2839     length=primitive_info[j].coordinates;
2840     switch (primitive_type)
2841     {
2842       case RectanglePrimitive:
2843       {
2844         length*=5;
2845         break;
2846       }
2847       case RoundRectanglePrimitive:
2848       {
2849         length*=5+4*BezierQuantum;
2850         break;
2851       }
2852       case BezierPrimitive:
2853       {
2854         if (primitive_info[j].coordinates > 107)
2855           (void) ThrowMagickException(&image->exception,GetMagickModule(),
2856             DrawError,"TooManyBezierCoordinates","`%s'",token);
2857         length=BezierQuantum*primitive_info[j].coordinates;
2858         break;
2859       }
2860       case PathPrimitive:
2861       {
2862         char
2863           *s,
2864           *t;
2865
2866         GetMagickToken(q,&q,token);
2867         k=1;
2868         t=token;
2869         for (s=token; *s != '\0'; s=t)
2870         {
2871           double
2872             value;
2873
2874           value=strtod(s,&t);
2875           if (s == t)
2876             {
2877               t++;
2878               continue;
2879             }
2880           k++;
2881         }
2882         length+=k*BezierQuantum;
2883         break;
2884       }
2885       case CirclePrimitive:
2886       case ArcPrimitive:
2887       case EllipsePrimitive:
2888       {
2889         MagickRealType
2890           alpha,
2891           beta,
2892           radius;
2893
2894         alpha=bounds.x2-bounds.x1;
2895         beta=bounds.y2-bounds.y1;
2896         radius=hypot((double) alpha,(double) beta);
2897         length=2*((size_t) (MagickPI*radius))+6*BezierQuantum+360+1;
2898         break;
2899       }
2900       default:
2901         break;
2902     }
2903     if ((unsigned long) (i+length) >= number_points)
2904       {
2905         /*
2906           Resize based on speculative points required by primitive.
2907         */
2908         while ((unsigned long) (i+length) >= number_points)
2909           number_points<<=1;
2910         primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2911           (size_t) number_points,sizeof(*primitive_info));
2912         if (primitive_info == (PrimitiveInfo *) NULL)
2913           {
2914             (void) ThrowMagickException(&image->exception,GetMagickModule(),
2915               ResourceLimitError,"MemoryAllocationFailed","`%s'",
2916               image->filename);
2917             break;
2918           }
2919       }
2920     switch (primitive_type)
2921     {
2922       case PointPrimitive:
2923       default:
2924       {
2925         if (primitive_info[j].coordinates != 1)
2926           {
2927             status=MagickFalse;
2928             break;
2929           }
2930         TracePoint(primitive_info+j,primitive_info[j].point);
2931         i=(long) (j+primitive_info[j].coordinates);
2932         break;
2933       }
2934       case LinePrimitive:
2935       {
2936         if (primitive_info[j].coordinates != 2)
2937           {
2938             status=MagickFalse;
2939             break;
2940           }
2941         TraceLine(primitive_info+j,primitive_info[j].point,
2942           primitive_info[j+1].point);
2943         i=(long) (j+primitive_info[j].coordinates);
2944         break;
2945       }
2946       case RectanglePrimitive:
2947       {
2948         if (primitive_info[j].coordinates != 2)
2949           {
2950             status=MagickFalse;
2951             break;
2952           }
2953         TraceRectangle(primitive_info+j,primitive_info[j].point,
2954           primitive_info[j+1].point);
2955         i=(long) (j+primitive_info[j].coordinates);
2956         break;
2957       }
2958       case RoundRectanglePrimitive:
2959       {
2960         if (primitive_info[j].coordinates != 3)
2961           {
2962             status=MagickFalse;
2963             break;
2964           }
2965         TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2966           primitive_info[j+1].point,primitive_info[j+2].point);
2967         i=(long) (j+primitive_info[j].coordinates);
2968         break;
2969       }
2970       case ArcPrimitive:
2971       {
2972         if (primitive_info[j].coordinates != 3)
2973           {
2974             primitive_type=UndefinedPrimitive;
2975             break;
2976           }
2977         TraceArc(primitive_info+j,primitive_info[j].point,
2978           primitive_info[j+1].point,primitive_info[j+2].point);
2979         i=(long) (j+primitive_info[j].coordinates);
2980         break;
2981       }
2982       case EllipsePrimitive:
2983       {
2984         if (primitive_info[j].coordinates != 3)
2985           {
2986             status=MagickFalse;
2987             break;
2988           }
2989         TraceEllipse(primitive_info+j,primitive_info[j].point,
2990           primitive_info[j+1].point,primitive_info[j+2].point);
2991         i=(long) (j+primitive_info[j].coordinates);
2992         break;
2993       }
2994       case CirclePrimitive:
2995       {
2996         if (primitive_info[j].coordinates != 2)
2997           {
2998             status=MagickFalse;
2999             break;
3000           }
3001         TraceCircle(primitive_info+j,primitive_info[j].point,
3002           primitive_info[j+1].point);
3003         i=(long) (j+primitive_info[j].coordinates);
3004         break;
3005       }
3006       case PolylinePrimitive:
3007         break;
3008       case PolygonPrimitive:
3009       {
3010         primitive_info[i]=primitive_info[j];
3011         primitive_info[i].coordinates=0;
3012         primitive_info[j].coordinates++;
3013         i++;
3014         break;
3015       }
3016       case BezierPrimitive:
3017       {
3018         if (primitive_info[j].coordinates < 3)
3019           {
3020             status=MagickFalse;
3021             break;
3022           }
3023         TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3024         i=(long) (j+primitive_info[j].coordinates);
3025         break;
3026       }
3027       case PathPrimitive:
3028       {
3029         i=(long) (j+TracePath(primitive_info+j,token));
3030         break;
3031       }
3032       case ColorPrimitive:
3033       case MattePrimitive:
3034       {
3035         long
3036           method;
3037
3038         if (primitive_info[j].coordinates != 1)
3039           {
3040             status=MagickFalse;
3041             break;
3042           }
3043         GetMagickToken(q,&q,token);
3044         method=ParseMagickOption(MagickMethodOptions,MagickFalse,token);
3045         if (method == -1)
3046           {
3047             status=MagickFalse;
3048             break;
3049           }
3050         primitive_info[j].method=(PaintMethod) method;
3051         break;
3052       }
3053       case TextPrimitive:
3054       {
3055         if (primitive_info[j].coordinates != 1)
3056           {
3057             status=MagickFalse;
3058             break;
3059           }
3060         if (*token != ',')
3061           GetMagickToken(q,&q,token);
3062         primitive_info[j].text=AcquireString(token);
3063         break;
3064       }
3065       case ImagePrimitive:
3066       {
3067         if (primitive_info[j].coordinates != 2)
3068           {
3069             status=MagickFalse;
3070             break;
3071           }
3072         GetMagickToken(q,&q,token);
3073         primitive_info[j].text=AcquireString(token);
3074         break;
3075       }
3076     }
3077     if (primitive_info == (PrimitiveInfo *) NULL)
3078       break;
3079     if (image->debug != MagickFalse)
3080       (void) LogMagickEvent(DrawEvent,GetMagickModule(),"  %.*s",(int) (q-p),p);
3081     if (status == MagickFalse)
3082       break;
3083     primitive_info[i].primitive=UndefinedPrimitive;
3084     if (i == 0)
3085       continue;
3086     /*
3087       Transform points.
3088     */
3089     for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3090     {
3091       point=primitive_info[i].point;
3092       primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3093         graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3094       primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3095         graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3096       point=primitive_info[i].point;
3097       if (point.x < graphic_context[n]->bounds.x1)
3098         graphic_context[n]->bounds.x1=point.x;
3099       if (point.y < graphic_context[n]->bounds.y1)
3100         graphic_context[n]->bounds.y1=point.y;
3101       if (point.x > graphic_context[n]->bounds.x2)
3102         graphic_context[n]->bounds.x2=point.x;
3103       if (point.y > graphic_context[n]->bounds.y2)
3104         graphic_context[n]->bounds.y2=point.y;
3105       if (primitive_info[i].primitive == ImagePrimitive)
3106         break;
3107     }
3108     if (i >= (long) number_points)
3109       ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3110     if (graphic_context[n]->render != MagickFalse)
3111       {
3112         if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3113             (LocaleCompare(graphic_context[n]->clip_mask,
3114              graphic_context[n-1]->clip_mask) != 0))
3115           (void) DrawClipPath(image,graphic_context[n],
3116             graphic_context[n]->clip_mask);
3117         (void) DrawPrimitive(image,graphic_context[n],primitive_info);
3118       }
3119     if (primitive_info->text != (char *) NULL)
3120       primitive_info->text=(char *) RelinquishMagickMemory(
3121         primitive_info->text);
3122     proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3123       primitive_extent);
3124     if (proceed == MagickFalse)
3125       break;
3126   }
3127   if (image->debug != MagickFalse)
3128     (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3129   /*
3130     Relinquish resources.
3131   */
3132   token=DestroyString(token);
3133   if (primitive_info != (PrimitiveInfo *) NULL)
3134     primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3135   primitive=DestroyString(primitive);
3136   for ( ; n >= 0; n--)
3137     graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3138   graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3139   if (status == MagickFalse)
3140     ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3141       keyword);
3142   return(status);
3143 }
3144 \f
3145 /*
3146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147 %                                                                             %
3148 %                                                                             %
3149 %                                                                             %
3150 %     D r a w G r a d i e n t I m a g e                                       %
3151 %                                                                             %
3152 %                                                                             %
3153 %                                                                             %
3154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155 %
3156 %  DrawGradientImage() draws a linear gradient on the image.
3157 %
3158 %  The format of the DrawGradientImage method is:
3159 %
3160 %      MagickBooleanType DrawGradientImage(Image *image,
3161 %        const DrawInfo *draw_info)
3162 %
3163 %  A description of each parameter follows:
3164 %
3165 %    o image: the image.
3166 %
3167 %    o _info: the draw info.
3168 %
3169 */
3170
3171 static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
3172   const long x,const long y)
3173 {
3174   switch (gradient->type)
3175   {
3176     case UndefinedGradient:
3177     case LinearGradient:
3178     {
3179       MagickRealType
3180         gamma,
3181         length,
3182         offset,
3183         scale;
3184
3185       PointInfo
3186         p,
3187         q;
3188
3189       const SegmentInfo
3190         *gradient_vector;
3191
3192       gradient_vector=(&gradient->gradient_vector);
3193       p.x=gradient_vector->x2-gradient_vector->x1;
3194       p.y=gradient_vector->y2-gradient_vector->y1;
3195       q.x=(double) x-gradient_vector->x1;
3196       q.y=(double) y-gradient_vector->y1;
3197       length=sqrt(q.x*q.x+q.y*q.y);
3198       gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3199       gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3200       scale=p.x*q.x+p.y*q.y;
3201       offset=gamma*scale*length;
3202       return(offset);
3203     }
3204     case RadialGradient:
3205     {
3206       MagickRealType
3207         length,
3208         offset;
3209
3210       PointInfo
3211         v;
3212
3213       v.x=(double) x-gradient->center.x;
3214       v.y=(double) y-gradient->center.y;
3215       length=sqrt(v.x*v.x+v.y*v.y);
3216       if (gradient->spread == RepeatSpread)
3217         return(length);
3218       offset=length/gradient->radius;
3219       return(offset);
3220     }
3221   }
3222   return(0.0);
3223 }
3224
3225 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3226   const DrawInfo *draw_info)
3227 {
3228   const GradientInfo
3229     *gradient;
3230
3231   const SegmentInfo
3232     *gradient_vector;
3233
3234   ExceptionInfo
3235     *exception;
3236
3237   long
3238     y;
3239
3240   MagickBooleanType
3241     status;
3242
3243   MagickPixelPacket
3244     zero;
3245
3246   MagickRealType
3247     length;
3248
3249   PointInfo
3250     point;
3251
3252   RectangleInfo
3253     bounding_box;
3254
3255   CacheView
3256     *image_view;
3257
3258   /*
3259     Draw linear or radial gradient on image.
3260   */
3261   assert(image != (Image *) NULL);
3262   assert(image->signature == MagickSignature);
3263   if (image->debug != MagickFalse)
3264     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3265   assert(draw_info != (const DrawInfo *) NULL);
3266   gradient=(&draw_info->gradient);
3267   gradient_vector=(&gradient->gradient_vector);
3268   point.x=gradient_vector->x2-gradient_vector->x1;
3269   point.y=gradient_vector->y2-gradient_vector->y1;
3270   length=sqrt(point.x*point.x+point.y*point.y);
3271   bounding_box=gradient->bounding_box;
3272   status=MagickTrue;
3273   exception=(&image->exception);
3274   GetMagickPixelPacket(image,&zero);
3275   image_view=AcquireCacheView(image);
3276 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3277   #pragma omp parallel for schedule(dynamic,4) shared(status)
3278 #endif
3279   for (y=bounding_box.y; y < (long) bounding_box.height; y++)
3280   {
3281     long
3282       j;
3283
3284     MagickPixelPacket
3285       composite,
3286       pixel;
3287
3288     MagickRealType
3289       alpha,
3290       offset;
3291
3292     register IndexPacket
3293       *restrict indexes;
3294
3295     register long
3296       i,
3297       x;
3298
3299     register PixelPacket
3300       *restrict q;
3301
3302     if (status == MagickFalse)
3303       continue;
3304     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3305     if (q == (PixelPacket *) NULL)
3306       {
3307         status=MagickFalse;
3308         continue;
3309       }
3310     indexes=GetCacheViewAuthenticIndexQueue(image_view);
3311     pixel=zero;
3312     composite=zero;
3313     offset=GetStopColorOffset(gradient,0,y);
3314     if (gradient->type != RadialGradient)
3315       offset/=length;
3316     for (x=bounding_box.x; x < (long) bounding_box.width; x++)
3317     {
3318       SetMagickPixelPacket(image,q,indexes+x,&pixel);
3319       switch (gradient->spread)
3320       {
3321         case UndefinedSpread:
3322         case PadSpread:
3323         {
3324           if ((x != (long) (gradient_vector->x1+0.5)) ||
3325               (y != (long) (gradient_vector->y1+0.5)))
3326             {
3327               offset=GetStopColorOffset(gradient,x,y);
3328               if (gradient->type != RadialGradient)
3329                 offset/=length;
3330             }
3331           for (i=0; i < (long) gradient->number_stops; i++)
3332             if (offset < gradient->stops[i].offset)
3333               break;
3334           if ((offset < 0.0) || (i == 0))
3335             composite=gradient->stops[0].color;
3336           else
3337             if ((offset > 1.0) || (i == (long) gradient->number_stops))
3338               composite=gradient->stops[gradient->number_stops-1].color;
3339             else
3340               {
3341                 j=i;
3342                 i--;
3343                 alpha=(offset-gradient->stops[i].offset)/
3344                   (gradient->stops[j].offset-gradient->stops[i].offset);
3345                 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3346                   &gradient->stops[j].color,alpha,&composite);
3347               }
3348           break;
3349         }
3350         case ReflectSpread:
3351         {
3352           if ((x != (long) (gradient_vector->x1+0.5)) ||
3353               (y != (long) (gradient_vector->y1+0.5)))
3354             {
3355               offset=GetStopColorOffset(gradient,x,y);
3356               if (gradient->type != RadialGradient)
3357                 offset/=length;
3358             }
3359           if (offset < 0.0)
3360             offset=(-offset);
3361           if ((long) fmod(offset,2.0) == 0)
3362             offset=fmod(offset,1.0);
3363           else
3364             offset=1.0-fmod(offset,1.0);
3365           for (i=0; i < (long) gradient->number_stops; i++)
3366             if (offset < gradient->stops[i].offset)
3367               break;
3368           if (i == 0)
3369             composite=gradient->stops[0].color;
3370           else
3371             if (i == (long) gradient->number_stops)
3372               composite=gradient->stops[gradient->number_stops-1].color;
3373             else
3374               {
3375                 j=i;
3376                 i--;
3377                 alpha=(offset-gradient->stops[i].offset)/
3378                   (gradient->stops[j].offset-gradient->stops[i].offset);
3379                 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3380                   &gradient->stops[j].color,alpha,&composite);
3381               }
3382           break;
3383         }
3384         case RepeatSpread:
3385         {
3386           MagickBooleanType
3387             antialias;
3388
3389           MagickRealType
3390             repeat;
3391
3392           antialias=MagickFalse;
3393           repeat=0.0;
3394           if ((x != (long) (gradient_vector->x1+0.5)) ||
3395               (y != (long) (gradient_vector->y1+0.5)))
3396             {
3397               offset=GetStopColorOffset(gradient,x,y);
3398               if (gradient->type == LinearGradient)
3399                 {
3400                   repeat=fmod(offset,length);
3401                   if (repeat < 0.0)
3402                     repeat=length-fmod(-repeat,length);
3403                   else
3404                     repeat=fmod(offset,length);
3405                   antialias=(repeat < length) && ((repeat+1.0) > length) ?
3406                     MagickTrue : MagickFalse;
3407                   offset=repeat/length;
3408                 }
3409               else
3410                 {
3411                   repeat=fmod(offset,gradient->radius);
3412                   if (repeat < 0.0)
3413                     repeat=gradient->radius-fmod(-repeat,gradient->radius);
3414                   else
3415                     repeat=fmod(offset,gradient->radius);
3416                   antialias=repeat+1.0 > gradient->radius ?
3417                     MagickTrue : MagickFalse;
3418                   offset=repeat/gradient->radius;
3419                 }
3420             }
3421           for (i=0; i < (long) gradient->number_stops; i++)
3422             if (offset < gradient->stops[i].offset)
3423               break;
3424           if (i == 0)
3425             composite=gradient->stops[0].color;
3426           else
3427             if (i == (long) gradient->number_stops)
3428               composite=gradient->stops[gradient->number_stops-1].color;
3429             else
3430               {
3431                 j=i;
3432                 i--;
3433                 alpha=(offset-gradient->stops[i].offset)/
3434                   (gradient->stops[j].offset-gradient->stops[i].offset);
3435                 if (antialias != MagickFalse)
3436                   {
3437                     if (gradient->type == LinearGradient)
3438                       alpha=length-repeat;
3439                     else
3440                       alpha=gradient->radius-repeat;
3441                     i=0;
3442                     j=(long) gradient->number_stops-1L;
3443                   }
3444                 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3445                   &gradient->stops[j].color,alpha,&composite);
3446               }
3447           break;
3448         }
3449       }
3450       MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
3451         pixel.opacity,&pixel);
3452       SetPixelPacket(image,&pixel,q,indexes+x);
3453       q++;
3454     }
3455     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3456       status=MagickFalse;
3457   }
3458   image_view=DestroyCacheView(image_view);
3459   return(status);
3460 }
3461 \f
3462 /*
3463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3464 %                                                                             %
3465 %                                                                             %
3466 %                                                                             %
3467 %   D r a w P a t t e r n P a t h                                             %
3468 %                                                                             %
3469 %                                                                             %
3470 %                                                                             %
3471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3472 %
3473 %  DrawPatternPath() draws a pattern.
3474 %
3475 %  The format of the DrawPatternPath method is:
3476 %
3477 %      MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3478 %        const char *name,Image **pattern)
3479 %
3480 %  A description of each parameter follows:
3481 %
3482 %    o image: the image.
3483 %
3484 %    o draw_info: the draw info.
3485 %
3486 %    o name: the pattern name.
3487 %
3488 %    o image: the image.
3489 %
3490 */
3491 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3492   const DrawInfo *draw_info,const char *name,Image **pattern)
3493 {
3494   char
3495     property[MaxTextExtent];
3496
3497   const char
3498     *geometry,
3499     *path;
3500
3501   DrawInfo
3502     *clone_info;
3503
3504   ImageInfo
3505     *image_info;
3506
3507   MagickBooleanType
3508     status;
3509
3510   assert(image != (Image *) NULL);
3511   assert(image->signature == MagickSignature);
3512   if (image->debug != MagickFalse)
3513     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3514   assert(draw_info != (const DrawInfo *) NULL);
3515   assert(name != (const char *) NULL);
3516   (void) FormatMagickString(property,MaxTextExtent,"%s",name);
3517   path=GetImageArtifact(image,property);
3518   if (path == (const char *) NULL)
3519     return(MagickFalse);
3520   (void) FormatMagickString(property,MaxTextExtent,"%s-geometry",name);
3521   geometry=GetImageArtifact(image,property);
3522   if (geometry == (const char *) NULL)
3523     return(MagickFalse);
3524   if ((*pattern) != (Image *) NULL)
3525     *pattern=DestroyImage(*pattern);
3526   image_info=AcquireImageInfo();
3527   image_info->size=AcquireString(geometry);
3528   *pattern=AcquireImage(image_info);
3529   image_info=DestroyImageInfo(image_info);
3530   (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
3531     &image->exception);
3532   (void) SetImageBackgroundColor(*pattern);
3533   if (image->debug != MagickFalse)
3534     (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3535       "begin pattern-path %s %s",name,geometry);
3536   clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3537   clone_info->fill_pattern=NewImageList();
3538   clone_info->stroke_pattern=NewImageList();
3539   (void) CloneString(&clone_info->primitive,path);
3540   status=DrawImage(*pattern,clone_info);
3541   clone_info=DestroyDrawInfo(clone_info);
3542   if (image->debug != MagickFalse)
3543     (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3544   return(status);
3545 }
3546 \f
3547 /*
3548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3549 %                                                                             %
3550 %                                                                             %
3551 %                                                                             %
3552 +   D r a w P o l y g o n P r i m i t i v e                                   %
3553 %                                                                             %
3554 %                                                                             %
3555 %                                                                             %
3556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3557 %
3558 %  DrawPolygonPrimitive() draws a polygon on the image.
3559 %
3560 %  The format of the DrawPolygonPrimitive method is:
3561 %
3562 %      MagickBooleanType DrawPolygonPrimitive(Image *image,
3563 %        const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3564 %
3565 %  A description of each parameter follows:
3566 %
3567 %    o image: the image.
3568 %
3569 %    o draw_info: the draw info.
3570 %
3571 %    o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3572 %
3573 */
3574
3575 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3576 {
3577   register long
3578     i;
3579
3580   assert(polygon_info != (PolygonInfo **) NULL);
3581   for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
3582     if (polygon_info[i] != (PolygonInfo *) NULL)
3583       polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3584   polygon_info=(PolygonInfo **) RelinquishAlignedMemory(polygon_info);
3585   return(polygon_info);
3586 }
3587
3588 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3589   const PrimitiveInfo *primitive_info)
3590 {
3591   PathInfo
3592     *path_info;
3593
3594   register long
3595     i;
3596
3597   PolygonInfo
3598     **polygon_info;
3599
3600   unsigned long
3601     number_threads;
3602
3603   number_threads=GetOpenMPMaximumThreads();
3604   polygon_info=(PolygonInfo **) AcquireAlignedMemory(number_threads,
3605     sizeof(*polygon_info));
3606   if (polygon_info == (PolygonInfo **) NULL)
3607     return((PolygonInfo **) NULL);
3608   (void) ResetMagickMemory(polygon_info,0,GetOpenMPMaximumThreads()*
3609     sizeof(*polygon_info));
3610   path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3611   if (path_info == (PathInfo *) NULL)
3612     return(DestroyPolygonThreadSet(polygon_info));
3613   for (i=0; i < (long) number_threads; i++)
3614   {
3615     polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3616     if (polygon_info[i] == (PolygonInfo *) NULL)
3617       return(DestroyPolygonThreadSet(polygon_info));
3618   }
3619   path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3620   return(polygon_info);
3621 }
3622
3623 static MagickRealType GetPixelOpacity(PolygonInfo *polygon_info,
3624   const MagickRealType mid,const MagickBooleanType fill,
3625   const FillRule fill_rule,const long x,const long y,
3626   MagickRealType *stroke_opacity)
3627 {
3628   int
3629     winding_number;
3630
3631   long
3632     j;
3633
3634   MagickRealType
3635     alpha,
3636     beta,
3637     distance,
3638     subpath_opacity;
3639
3640   PointInfo
3641     delta;
3642
3643   register EdgeInfo
3644     *p;
3645
3646   register const PointInfo
3647     *q;
3648
3649   register long
3650     i;
3651
3652   /*
3653     Compute fill & stroke opacity for this (x,y) point.
3654   */
3655   *stroke_opacity=0.0;
3656   subpath_opacity=0.0;
3657   p=polygon_info->edges;
3658   for (j=0; j < (long) polygon_info->number_edges; j++, p++)
3659   {
3660     if (y <= (p->bounds.y1-mid-0.5))
3661       break;
3662     if (y > (p->bounds.y2+mid+0.5))
3663       {
3664         (void) DestroyEdge(polygon_info,j);
3665         continue;
3666       }
3667     if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3668       continue;
3669     for (i=MagickMax(p->highwater,1); i < (long) p->number_points; i++)
3670     {
3671       if (y <= (p->points[i-1].y-mid-0.5))
3672         break;
3673       if (y > (p->points[i].y+mid+0.5))
3674         continue;
3675       if (p->scanline != y)
3676         {
3677           p->scanline=y;
3678           p->highwater=i;
3679         }
3680       /*
3681         Compute distance between a point and an edge.
3682       */
3683       q=p->points+i-1;
3684       delta.x=(q+1)->x-q->x;
3685       delta.y=(q+1)->y-q->y;
3686       beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3687       if (beta < 0.0)
3688         {
3689           delta.x=x-q->x;
3690           delta.y=y-q->y;
3691           distance=delta.x*delta.x+delta.y*delta.y;
3692         }
3693       else
3694         {
3695           alpha=delta.x*delta.x+delta.y*delta.y;
3696           if (beta > alpha)
3697             {
3698               delta.x=x-(q+1)->x;
3699               delta.y=y-(q+1)->y;
3700               distance=delta.x*delta.x+delta.y*delta.y;
3701             }
3702           else
3703             {
3704               alpha=1.0/alpha;
3705               beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3706               distance=alpha*beta*beta;
3707             }
3708         }
3709       /*
3710         Compute stroke & subpath opacity.
3711       */
3712       beta=0.0;
3713       if (p->ghostline == MagickFalse)
3714         {
3715           alpha=mid+0.5;
3716           if ((*stroke_opacity < 1.0) &&
3717               (distance <= ((alpha+0.25)*(alpha+0.25))))
3718             {
3719               alpha=mid-0.5;
3720               if (distance <= ((alpha+0.25)*(alpha+0.25)))
3721                 *stroke_opacity=1.0;
3722               else
3723                 {
3724                   beta=1.0;
3725                   if (distance != 1.0)
3726                     beta=sqrt((double) distance);
3727                   alpha=beta-mid-0.5;
3728                   if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3729                     *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3730                 }
3731             }
3732         }
3733       if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3734         continue;
3735       if (distance <= 0.0)
3736         {
3737           subpath_opacity=1.0;
3738           continue;
3739         }
3740       if (distance > 1.0)
3741         continue;
3742       if (beta == 0.0)
3743         {
3744           beta=1.0;
3745           if (distance != 1.0)
3746             beta=sqrt(distance);
3747         }
3748       alpha=beta-1.0;
3749       if (subpath_opacity < (alpha*alpha))
3750         subpath_opacity=alpha*alpha;
3751     }
3752   }
3753   /*
3754     Compute fill opacity.
3755   */
3756   if (fill == MagickFalse)
3757     return(0.0);
3758   if (subpath_opacity >= 1.0)
3759     return(1.0);
3760   /*
3761     Determine winding number.
3762   */
3763   winding_number=0;
3764   p=polygon_info->edges;
3765   for (j=0; j < (long) polygon_info->number_edges; j++, p++)
3766   {
3767     if (y <= p->bounds.y1)
3768       break;
3769     if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3770       continue;
3771     if (x > p->bounds.x2)
3772       {
3773         winding_number+=p->direction ? 1 : -1;
3774         continue;
3775       }
3776     for (i=MagickMax(p->highwater,1); i < (long) p->number_points; i++)
3777       if (y <= p->points[i].y)
3778         break;
3779     q=p->points+i-1;
3780     if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3781       winding_number+=p->direction ? 1 : -1;
3782   }
3783   if (fill_rule != NonZeroRule)
3784     {
3785       if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3786         return(1.0);
3787     }
3788   else
3789     if (MagickAbsoluteValue(winding_number) != 0)
3790       return(1.0);
3791   return(subpath_opacity);
3792 }
3793
3794 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3795   const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3796 {
3797   ExceptionInfo
3798     *exception;
3799
3800   long
3801     start,
3802     stop,
3803     y;
3804
3805   MagickBooleanType
3806     fill,
3807     status;
3808
3809   MagickRealType
3810     mid;
3811
3812   PolygonInfo
3813     **polygon_info;
3814
3815   register EdgeInfo
3816     *p;
3817
3818   register long
3819     i;
3820
3821   SegmentInfo
3822     bounds;
3823
3824   CacheView
3825     *image_view;
3826
3827   /*
3828     Compute bounding box.
3829   */
3830   assert(image != (Image *) NULL);
3831   assert(image->signature == MagickSignature);
3832   if (image->debug != MagickFalse)
3833     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3834   assert(draw_info != (DrawInfo *) NULL);
3835   assert(draw_info->signature == MagickSignature);
3836   assert(primitive_info != (PrimitiveInfo *) NULL);
3837   if (primitive_info->coordinates == 0)
3838     return(MagickTrue);
3839   polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3840   if (polygon_info == (PolygonInfo **) NULL)
3841     return(MagickFalse);
3842   if (0)
3843     DrawBoundingRectangles(image,draw_info,polygon_info[0]);
3844   if (image->debug != MagickFalse)
3845     (void) LogMagickEvent(DrawEvent,GetMagickModule(),"    begin draw-polygon");
3846   fill=(primitive_info->method == FillToBorderMethod) ||
3847     (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3848   mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3849   bounds=polygon_info[0]->edges[0].bounds;
3850   for (i=1; i < (long) polygon_info[0]->number_edges; i++)
3851   {
3852     p=polygon_info[0]->edges+i;
3853     if (p->bounds.x1 < bounds.x1)
3854       bounds.x1=p->bounds.x1;
3855     if (p->bounds.y1 < bounds.y1)
3856       bounds.y1=p->bounds.y1;
3857     if (p->bounds.x2 > bounds.x2)
3858       bounds.x2=p->bounds.x2;
3859     if (p->bounds.y2 > bounds.y2)
3860       bounds.y2=p->bounds.y2;
3861   }
3862   bounds.x1-=(mid+1.0);
3863   bounds.x1=bounds.x1 < 0.0 ? 0.0 : (unsigned long) (bounds.x1+0.5) >=
3864     image->columns ? (double) image->columns-1.0 : bounds.x1;
3865   bounds.y1-=(mid+1.0);
3866   bounds.y1=bounds.y1 < 0.0 ? 0.0 : (unsigned long) (bounds.y1+0.5) >=
3867     image->rows ? (double) image->rows-1.0 : bounds.y1;
3868   bounds.x2+=(mid+1.0);
3869   bounds.x2=bounds.x2 < 0.0 ? 0.0 : (unsigned long) (bounds.x2+0.5) >=
3870     image->columns ? (double) image->columns-1.0 : bounds.x2;
3871   bounds.y2+=(mid+1.0);
3872   bounds.y2=bounds.y2 < 0.0 ? 0.0 : (unsigned long) (bounds.y2+0.5) >=
3873     image->rows ? (double) image->rows-1.0 : bounds.y2;
3874   status=MagickTrue;
3875   exception=(&image->exception);
3876   start=(long) (bounds.x1+0.5);
3877   stop=(long) (bounds.x2+0.5);
3878   image_view=AcquireCacheView(image);
3879   if (primitive_info->coordinates == 1)
3880     {
3881       /*
3882         Draw point.
3883       */
3884 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3885   #pragma omp parallel for schedule(dynamic,4) shared(status)
3886 #endif
3887       for (y=(long) (bounds.y1+0.5); y <= (long) (bounds.y2+0.5); y++)
3888       {
3889         MagickBooleanType
3890           sync;
3891
3892         register long
3893           x;
3894
3895         register PixelPacket
3896           *restrict q;
3897
3898         if (status == MagickFalse)
3899           continue;
3900         x=start;
3901         q=GetCacheViewAuthenticPixels(image_view,x,y,(unsigned long) (stop-x+1),
3902           1,exception);
3903         if (q == (PixelPacket *) NULL)
3904           {
3905             status=MagickFalse;
3906             continue;
3907           }
3908         for ( ; x <= stop; x++)
3909         {
3910           if ((x == (long) (primitive_info->point.x+0.5)) &&
3911               (y == (long) (primitive_info->point.y+0.5)))
3912             (void) GetStrokeColor(draw_info,x,y,q);
3913           q++;
3914         }
3915         sync=SyncCacheViewAuthenticPixels(image_view,exception);
3916         if (sync == MagickFalse)
3917           status=MagickFalse;
3918       }
3919       image_view=DestroyCacheView(image_view);
3920       polygon_info=DestroyPolygonThreadSet(polygon_info);
3921       if (image->debug != MagickFalse)
3922         (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3923           "    end draw-polygon");
3924       return(status);
3925     }
3926   /*
3927     Draw polygon or line.
3928   */
3929   if (image->matte == MagickFalse)
3930     (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
3931 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3932   #pragma omp parallel for schedule(dynamic,4) shared(status)
3933 #endif
3934   for (y=(long) (bounds.y1+0.5); y <= (long) (bounds.y2+0.5); y++)
3935   {
3936     MagickRealType
3937       fill_opacity,
3938       stroke_opacity;
3939
3940     PixelPacket
3941       fill_color,
3942       stroke_color;
3943
3944     register long
3945       id,
3946       x;
3947
3948     register PixelPacket
3949       *restrict q;
3950
3951     if (status == MagickFalse)
3952       continue;
3953     q=GetCacheViewAuthenticPixels(image_view,start,y,(unsigned long) (stop-
3954       start+1),1,exception);
3955     if (q == (PixelPacket *) NULL)
3956       {
3957         status=MagickFalse;
3958         continue;
3959       }
3960     id=GetOpenMPThreadId();
3961     for (x=start; x <= stop; x++)
3962     {
3963       /*
3964         Fill and/or stroke.
3965       */
3966       fill_opacity=GetPixelOpacity(polygon_info[id],mid,fill,
3967         draw_info->fill_rule,x,y,&stroke_opacity);
3968       if (draw_info->stroke_antialias == MagickFalse)
3969         {
3970           fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
3971           stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
3972         }
3973       (void) GetFillColor(draw_info,x,y,&fill_color);
3974       fill_opacity=(MagickRealType) (QuantumRange-fill_opacity*(QuantumRange-
3975         fill_color.opacity));
3976       MagickCompositeOver(&fill_color,fill_opacity,q,(MagickRealType)
3977         q->opacity,q);
3978       (void) GetStrokeColor(draw_info,x,y,&stroke_color);
3979       stroke_opacity=(MagickRealType) (QuantumRange-stroke_opacity*
3980         (QuantumRange-stroke_color.opacity));
3981       MagickCompositeOver(&stroke_color,stroke_opacity,q,(MagickRealType)
3982         q->opacity,q);
3983       q++;
3984     }
3985     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3986       status=MagickFalse;
3987   }
3988   image_view=DestroyCacheView(image_view);
3989   polygon_info=DestroyPolygonThreadSet(polygon_info);
3990   if (image->debug != MagickFalse)
3991     (void) LogMagickEvent(DrawEvent,GetMagickModule(),"    end draw-polygon");
3992   return(status);
3993 }
3994 \f
3995 /*
3996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3997 %                                                                             %
3998 %                                                                             %
3999 %                                                                             %
4000 %   D r a w P r i m i t i v e                                                 %
4001 %                                                                             %
4002 %                                                                             %
4003 %                                                                             %
4004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4005 %
4006 %  DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4007 %
4008 %  The format of the DrawPrimitive method is:
4009 %
4010 %      MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4011 %        PrimitiveInfo *primitive_info)
4012 %
4013 %  A description of each parameter follows:
4014 %
4015 %    o image: the image.
4016 %
4017 %    o draw_info: the draw info.
4018 %
4019 %    o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4020 %
4021 */
4022
4023 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4024 {
4025   const char
4026     *methods[] =
4027     {
4028       "point",
4029       "replace",
4030       "floodfill",
4031       "filltoborder",
4032       "reset",
4033       "?"
4034     };
4035
4036   long
4037     coordinates,
4038     y;
4039
4040   PointInfo
4041     p,
4042     q,
4043     point;
4044
4045   register long
4046     i,
4047     x;
4048
4049   x=(long) (primitive_info->point.x+0.5);
4050   y=(long) (primitive_info->point.y+0.5);
4051   switch (primitive_info->primitive)
4052   {
4053     case PointPrimitive:
4054     {
4055       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4056         "PointPrimitive %ld,%ld %s",x,y,methods[primitive_info->method]);
4057       return;
4058     }
4059     case ColorPrimitive:
4060     {
4061       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4062         "ColorPrimitive %ld,%ld %s",x,y,methods[primitive_info->method]);
4063       return;
4064     }
4065     case MattePrimitive:
4066     {
4067       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4068         "MattePrimitive %ld,%ld %s",x,y,methods[primitive_info->method]);
4069       return;
4070     }
4071     case TextPrimitive:
4072     {
4073       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4074         "TextPrimitive %ld,%ld",x,y);
4075       return;
4076     }
4077     case ImagePrimitive:
4078     {
4079       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4080         "ImagePrimitive %ld,%ld",x,y);
4081       return;
4082     }
4083     default:
4084       break;
4085   }
4086   coordinates=0;
4087   p=primitive_info[0].point;
4088   q.x=(-1.0);
4089   q.y=(-1.0);
4090   for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4091   {
4092     point=primitive_info[i].point;
4093     if (coordinates <= 0)
4094       {
4095         coordinates=(long) primitive_info[i].coordinates;
4096         (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4097           "    begin open (%ld)",coordinates);
4098         p=point;
4099       }
4100     point=primitive_info[i].point;
4101     if ((fabs(q.x-point.x) > MagickEpsilon) ||
4102         (fabs(q.y-point.y) > MagickEpsilon))
4103       (void) LogMagickEvent(DrawEvent,GetMagickModule(),"      %ld: %g,%g",
4104         coordinates,point.x,point.y);
4105     else
4106       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4107         "      %ld: %g,%g (duplicate)",coordinates,point.x,point.y);
4108     q=point;
4109     coordinates--;
4110     if (coordinates > 0)
4111       continue;
4112     if ((fabs(p.x-point.x) > MagickEpsilon) ||
4113         (fabs(p.y-point.y) > MagickEpsilon))
4114       (void) LogMagickEvent(DrawEvent,GetMagickModule(),"    end last (%ld)",
4115         coordinates);
4116     else
4117       (void) LogMagickEvent(DrawEvent,GetMagickModule(),"    end open (%ld)",
4118         coordinates);
4119   }
4120 }
4121
4122 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4123   const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4124 {
4125   ExceptionInfo
4126     *exception;
4127
4128   long
4129     y;
4130
4131   MagickStatusType
4132     status;
4133
4134   register long
4135     i,
4136     x;
4137
4138   CacheView
4139     *image_view;
4140
4141   if (image->debug != MagickFalse)
4142     {
4143       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4144         "  begin draw-primitive");
4145       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4146         "    affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
4147         draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4148         draw_info->affine.tx,draw_info->affine.ty);
4149     }
4150   status=MagickTrue;
4151   exception=(&image->exception);
4152   x=(long) (primitive_info->point.x+0.5);
4153   y=(long) (primitive_info->point.y+0.5);
4154   image_view=AcquireCacheView(image);
4155   switch (primitive_info->primitive)
4156   {
4157     case PointPrimitive:
4158     {
4159       PixelPacket
4160         fill_color;
4161
4162       PixelPacket
4163         *q;
4164
4165       if ((y < 0) || (y >= (long) image->rows))
4166         break;
4167       if ((x < 0) || (x >= (long) image->columns))
4168         break;
4169       q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4170       if (q == (PixelPacket *) NULL)
4171         break;
4172       (void) GetFillColor(draw_info,x,y,&fill_color);
4173       MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
4174         (MagickRealType) q->opacity,q);
4175       (void) SyncCacheViewAuthenticPixels(image_view,exception);
4176       break;
4177     }
4178     case ColorPrimitive:
4179     {
4180       switch (primitive_info->method)
4181       {
4182         case PointMethod:
4183         default:
4184         {
4185           PixelPacket
4186             *q;
4187
4188           q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4189           if (q == (PixelPacket *) NULL)
4190             break;
4191           (void) GetFillColor(draw_info,x,y,q);
4192           (void) SyncCacheViewAuthenticPixels(image_view,exception);
4193           break;
4194         }
4195         case ReplaceMethod:
4196         {
4197           MagickBooleanType
4198             sync;
4199
4200           PixelPacket
4201             target;
4202
4203           (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4204           for (y=0; y < (long) image->rows; y++)
4205           {
4206             register PixelPacket
4207               *restrict q;
4208
4209             q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4210               exception);
4211             if (q == (PixelPacket *) NULL)
4212               break;
4213             for (x=0; x < (long) image->columns; x++)
4214             {
4215               if (IsColorSimilar(image,q,&target) == MagickFalse)
4216                 {
4217                   q++;
4218                   continue;
4219                 }
4220               (void) GetFillColor(draw_info,x,y,q);
4221               q++;
4222             }
4223             sync=SyncCacheViewAuthenticPixels(image_view,exception);
4224             if (sync == MagickFalse)
4225               break;
4226           }
4227           break;
4228         }
4229         case FloodfillMethod:
4230         case FillToBorderMethod:
4231         {
4232           MagickPixelPacket
4233             target;
4234
4235           (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4236           if (primitive_info->method == FillToBorderMethod)
4237             {
4238               target.red=(MagickRealType) draw_info->border_color.red;
4239               target.green=(MagickRealType) draw_info->border_color.green;
4240               target.blue=(MagickRealType) draw_info->border_color.blue;
4241             }
4242           (void) FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
4243             y,primitive_info->method == FloodfillMethod ? MagickFalse :
4244             MagickTrue);
4245           break;
4246         }
4247         case ResetMethod:
4248         {
4249           MagickBooleanType
4250             sync;
4251
4252           for (y=0; y < (long) image->rows; y++)
4253           {
4254             register long
4255               x;
4256
4257             register PixelPacket
4258               *restrict q;
4259
4260             q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4261               exception);
4262             if (q == (PixelPacket *) NULL)
4263               break;
4264             for (x=0; x < (long) image->columns; x++)
4265             {
4266               (void) GetFillColor(draw_info,x,y,q);
4267               q++;
4268             }
4269             sync=SyncCacheViewAuthenticPixels(image_view,exception);
4270             if (sync == MagickFalse)
4271               break;
4272           }
4273           break;
4274         }
4275       }
4276       break;
4277     }
4278     case MattePrimitive:
4279     {
4280       if (image->matte == MagickFalse)
4281         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
4282       switch (primitive_info->method)
4283       {
4284         case PointMethod:
4285         default:
4286         {
4287           PixelPacket
4288             pixel;
4289
4290           PixelPacket
4291             *q;
4292
4293           q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4294           if (q == (PixelPacket *) NULL)
4295             break;
4296           (void) GetFillColor(draw_info,x,y,&pixel);
4297           q->opacity=pixel.opacity;
4298           (void) SyncCacheViewAuthenticPixels(image_view,exception);
4299           break;
4300         }
4301         case ReplaceMethod:
4302         {
4303           MagickBooleanType
4304             sync;
4305
4306           PixelPacket
4307             pixel,
4308             target;
4309
4310           (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4311           for (y=0; y < (long) image->rows; y++)
4312           {
4313             register long
4314               x;
4315
4316             register PixelPacket
4317               *restrict q;
4318
4319             q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4320               exception);
4321             if (q == (PixelPacket *) NULL)
4322               break;
4323             for (x=0; x < (long) image->columns; x++)
4324             {
4325               if (IsColorSimilar(image,q,&target) == MagickFalse)
4326                 {
4327                   q++;
4328                   continue;
4329                 }
4330               (void) GetFillColor(draw_info,x,y,&pixel);
4331               q->opacity=pixel.opacity;
4332               q++;
4333             }
4334             sync=SyncCacheViewAuthenticPixels(image_view,exception);
4335             if (sync == MagickFalse)
4336               break;
4337           }
4338           break;
4339         }
4340         case FloodfillMethod:
4341         case FillToBorderMethod:
4342         {
4343           MagickPixelPacket
4344             target;
4345
4346           (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4347           if (primitive_info->method == FillToBorderMethod)
4348             {
4349               target.red=(MagickRealType) draw_info->border_color.red;
4350               target.green=(MagickRealType) draw_info->border_color.green;
4351               target.blue=(MagickRealType) draw_info->border_color.blue;
4352             }
4353           (void) FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,y,
4354             primitive_info->method == FloodfillMethod ? MagickFalse :
4355             MagickTrue);
4356           break;
4357         }
4358         case ResetMethod:
4359         {
4360           MagickBooleanType
4361             sync;
4362
4363           PixelPacket
4364             pixel;
4365
4366           for (y=0; y < (long) image->rows; y++)
4367           {
4368             register long
4369               x;
4370
4371             register PixelPacket
4372               *restrict q;
4373
4374             q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4375               exception);
4376             if (q == (PixelPacket *) NULL)
4377               break;
4378             for (x=0; x < (long) image->columns; x++)
4379             {
4380               (void) GetFillColor(draw_info,x,y,&pixel);
4381               q->opacity=pixel.opacity;
4382               q++;
4383             }
4384             sync=SyncCacheViewAuthenticPixels(image_view,exception);
4385             if (sync == MagickFalse)
4386               break;
4387           }
4388           break;
4389         }
4390       }
4391       break;
4392     }
4393     case TextPrimitive:
4394     {
4395       char
4396         geometry[MaxTextExtent];
4397
4398       DrawInfo
4399         *clone_info;
4400
4401       if (primitive_info->text == (char *) NULL)
4402         break;
4403       clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4404       (void) CloneString(&clone_info->text,primitive_info->text);
4405       (void) FormatMagickString(geometry,MaxTextExtent,"%+f%+f",
4406         primitive_info->point.x,primitive_info->point.y);
4407       (void) CloneString(&clone_info->geometry,geometry);
4408       status=AnnotateImage(image,clone_info);
4409       clone_info=DestroyDrawInfo(clone_info);
4410       break;
4411     }
4412     case ImagePrimitive:
4413     {
4414       AffineMatrix
4415         affine;
4416
4417       char
4418         composite_geometry[MaxTextExtent];
4419
4420       Image
4421         *composite_image;
4422
4423       ImageInfo
4424         *clone_info;
4425
4426       long
4427         x1,
4428         y1;
4429
4430       RectangleInfo
4431         geometry;
4432
4433       if (primitive_info->text == (char *) NULL)
4434         break;
4435       clone_info=AcquireImageInfo();
4436       if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4437         composite_image=ReadInlineImage(clone_info,primitive_info->text,
4438           &image->exception);
4439       else
4440         {
4441           (void) CopyMagickString(clone_info->filename,primitive_info->text,
4442             MaxTextExtent);
4443           composite_image=ReadImage(clone_info,&image->exception);
4444         }
4445       clone_info=DestroyImageInfo(clone_info);
4446       if (composite_image == (Image *) NULL)
4447         break;
4448       (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4449         NULL,(void *) NULL);
4450       x1=(long) (primitive_info[1].point.x+0.5);
4451       y1=(long) (primitive_info[1].point.y+0.5);
4452       if (((x1 != 0L) && (x1 != (long) composite_image->columns)) ||
4453           ((y1 != 0L) && (y1 != (long) composite_image->rows)))
4454         {
4455           char
4456             geometry[MaxTextExtent];
4457
4458           /*
4459             Resize image.
4460           */
4461           (void) FormatMagickString(geometry,MaxTextExtent,"%gx%g!",
4462             primitive_info[1].point.x,primitive_info[1].point.y);
4463           composite_image->filter=image->filter;
4464           (void) TransformImage(&composite_image,(char *) NULL,geometry);
4465         }
4466       if (composite_image->matte == MagickFalse)
4467         (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
4468       if (draw_info->opacity != OpaqueOpacity)
4469         (void) SetImageOpacity(composite_image,draw_info->opacity);
4470       SetGeometry(image,&geometry);
4471       image->gravity=draw_info->gravity;
4472       geometry.x=x;
4473       geometry.y=y;
4474       (void) FormatMagickString(composite_geometry,MaxTextExtent,
4475         "%lux%lu%+ld%+ld",composite_image->columns,composite_image->rows,
4476         geometry.x,geometry.y);
4477       (void) ParseGravityGeometry(image,composite_geometry,&geometry,
4478         &image->exception);
4479       affine=draw_info->affine;
4480       affine.tx=(double) geometry.x;
4481       affine.ty=(double) geometry.y;
4482       composite_image->interpolate=image->interpolate;
4483       if (draw_info->compose == OverCompositeOp)
4484         (void) DrawAffineImage(image,composite_image,&affine);
4485       else
4486         (void) CompositeImage(image,draw_info->compose,composite_image,
4487           geometry.x,geometry.y);
4488       composite_image=DestroyImage(composite_image);
4489       break;
4490     }
4491     default:
4492     {
4493       MagickRealType
4494         mid,
4495         scale;
4496
4497       DrawInfo
4498         *clone_info;
4499
4500       if (IsEventLogging() != MagickFalse)
4501         LogPrimitiveInfo(primitive_info);
4502       scale=ExpandAffine(&draw_info->affine);
4503       if ((draw_info->dash_pattern != (double *) NULL) &&
4504           (draw_info->dash_pattern[0] != 0.0) &&
4505           ((scale*draw_info->stroke_width) > MagickEpsilon) &&
4506           (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
4507         {
4508           /*
4509             Draw dash polygon.
4510           */
4511           clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4512           clone_info->stroke_width=0.0;
4513           clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4514           status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4515           clone_info=DestroyDrawInfo(clone_info);
4516           (void) DrawDashPolygon(draw_info,primitive_info,image);
4517           break;
4518         }
4519       mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4520       if ((mid > 1.0) &&
4521           (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
4522         {
4523           MagickBooleanType
4524             closed_path;
4525
4526           /*
4527             Draw strokes while respecting line cap/join attributes.
4528           */
4529           for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4530           closed_path=
4531             (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4532             (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4533             MagickTrue : MagickFalse;
4534           i=(long) primitive_info[0].coordinates;
4535           if ((((draw_info->linecap == RoundCap) ||
4536                 (closed_path != MagickFalse)) &&
4537                (draw_info->linejoin == RoundJoin)) ||
4538                (primitive_info[i].primitive != UndefinedPrimitive))
4539             {
4540               (void) DrawPolygonPrimitive(image,draw_info,primitive_info);
4541               break;
4542             }
4543           clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4544           clone_info->stroke_width=0.0;
4545           clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4546           status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4547           clone_info=DestroyDrawInfo(clone_info);
4548           status|=DrawStrokePolygon(image,draw_info,primitive_info);
4549           break;
4550         }
4551       status=DrawPolygonPrimitive(image,draw_info,primitive_info);
4552       break;
4553     }
4554   }
4555   image_view=DestroyCacheView(image_view);
4556   if (image->debug != MagickFalse)
4557     (void) LogMagickEvent(DrawEvent,GetMagickModule(),"  end draw-primitive");
4558   return(status != 0 ? MagickTrue : MagickFalse);
4559 }
4560 \f
4561 /*
4562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4563 %                                                                             %
4564 %                                                                             %
4565 %                                                                             %
4566 +   D r a w S t r o k e P o l y g o n                                         %
4567 %                                                                             %
4568 %                                                                             %
4569 %                                                                             %
4570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4571 %
4572 %  DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4573 %  the image while respecting the line cap and join attributes.
4574 %
4575 %  The format of the DrawStrokePolygon method is:
4576 %
4577 %      MagickBooleanType DrawStrokePolygon(Image *image,
4578 %        const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4579 %
4580 %  A description of each parameter follows:
4581 %
4582 %    o image: the image.
4583 %
4584 %    o draw_info: the draw info.
4585 %
4586 %    o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4587 %
4588 %
4589 */
4590
4591 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4592   const PrimitiveInfo *primitive_info)
4593 {
4594   PrimitiveInfo
4595     linecap[5];
4596
4597   register long
4598     i;
4599
4600   for (i=0; i < 4; i++)
4601     linecap[i]=(*primitive_info);
4602   linecap[0].coordinates=4;
4603   linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4604   linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4605   linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4606   linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4607   linecap[4].primitive=UndefinedPrimitive;
4608   (void) DrawPolygonPrimitive(image,draw_info,linecap);
4609 }
4610
4611 static MagickBooleanType DrawStrokePolygon(Image *image,
4612   const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4613 {
4614   DrawInfo
4615     *clone_info;
4616
4617   MagickBooleanType
4618     closed_path,
4619     status;
4620
4621   PrimitiveInfo
4622     *stroke_polygon;
4623
4624   register const PrimitiveInfo
4625     *p,
4626     *q;
4627
4628   /*
4629     Draw stroked polygon.
4630   */
4631   if (image->debug != MagickFalse)
4632     (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4633       "    begin draw-stroke-polygon");
4634   clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4635   clone_info->fill=draw_info->stroke;
4636   clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4637   clone_info->stroke_width=0.0;
4638   clone_info->fill_rule=NonZeroRule;
4639   status=MagickTrue;
4640   for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4641   {
4642     stroke_polygon=TraceStrokePolygon(draw_info,p);
4643     status=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
4644     stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4645     q=p+p->coordinates-1;
4646     closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4647       MagickTrue : MagickFalse;
4648     if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4649       {
4650         DrawRoundLinecap(image,draw_info,p);
4651         DrawRoundLinecap(image,draw_info,q);
4652       }
4653   }
4654   clone_info=DestroyDrawInfo(clone_info);
4655   if (image->debug != MagickFalse)
4656     (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4657       "    end draw-stroke-polygon");
4658   return(status);
4659 }
4660 \f
4661 /*
4662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4663 %                                                                             %
4664 %                                                                             %
4665 %                                                                             %
4666 %   G e t A f f i n e M a t r i x                                             %
4667 %                                                                             %
4668 %                                                                             %
4669 %                                                                             %
4670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4671 %
4672 %  GetAffineMatrix() returns an AffineMatrix initialized to the identity
4673 %  matrix.
4674 %
4675 %  The format of the GetAffineMatrix method is:
4676 %
4677 %      void GetAffineMatrix(AffineMatrix *affine_matrix)
4678 %
4679 %  A description of each parameter follows:
4680 %
4681 %    o affine_matrix: the affine matrix.
4682 %
4683 */
4684 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4685 {
4686   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4687   assert(affine_matrix != (AffineMatrix *) NULL);
4688   (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4689   affine_matrix->sx=1.0;
4690   affine_matrix->sy=1.0;
4691 }
4692 \f
4693 /*
4694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4695 %                                                                             %
4696 %                                                                             %
4697 %                                                                             %
4698 +   G e t D r a w I n f o                                                     %
4699 %                                                                             %
4700 %                                                                             %
4701 %                                                                             %
4702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4703 %
4704 %  GetDrawInfo() initializes draw_info to default values.
4705 %
4706 %  The format of the GetDrawInfo method is:
4707 %
4708 %      void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4709 %
4710 %  A description of each parameter follows:
4711 %
4712 %    o image_info: the image info..
4713 %
4714 %    o draw_info: the draw info.
4715 %
4716 */
4717 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4718 {
4719   const char
4720     *option;
4721
4722   ExceptionInfo
4723     *exception;
4724
4725   ImageInfo
4726     *clone_info;
4727
4728   /*
4729     Initialize draw attributes.
4730   */
4731   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4732   assert(draw_info != (DrawInfo *) NULL);
4733   (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4734   clone_info=CloneImageInfo(image_info);
4735   GetAffineMatrix(&draw_info->affine);
4736   exception=AcquireExceptionInfo();
4737   (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
4738   (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
4739   draw_info->stroke_antialias=clone_info->antialias;
4740   draw_info->stroke_width=1.0;
4741   draw_info->opacity=OpaqueOpacity;
4742   draw_info->fill_rule=EvenOddRule;
4743   draw_info->linecap=ButtCap;
4744   draw_info->linejoin=MiterJoin;
4745   draw_info->miterlimit=10;
4746   draw_info->decorate=NoDecoration;
4747   if (clone_info->font != (char *) NULL)
4748     draw_info->font=AcquireString(clone_info->font);
4749   if (clone_info->density != (char *) NULL)
4750     draw_info->density=AcquireString(clone_info->density);
4751   draw_info->text_antialias=clone_info->antialias;
4752   draw_info->pointsize=12.0;
4753   if (clone_info->pointsize != 0.0)
4754     draw_info->pointsize=clone_info->pointsize;
4755   draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
4756   draw_info->border_color=clone_info->border_color;
4757   draw_info->compose=OverCompositeOp;
4758   if (clone_info->server_name != (char *) NULL)
4759     draw_info->server_name=AcquireString(clone_info->server_name);
4760   draw_info->render=MagickTrue;
4761   draw_info->debug=IsEventLogging();
4762   option=GetImageOption(clone_info,"encoding");
4763   if (option != (const char *) NULL)
4764     (void) CloneString(&draw_info->encoding,option);
4765   option=GetImageOption(clone_info,"kerning");
4766   if (option != (const char *) NULL)
4767     draw_info->kerning=StringToDouble(option);
4768   option=GetImageOption(clone_info,"interline-spacing");
4769   if (option != (const char *) NULL)
4770     draw_info->interline_spacing=StringToDouble(option);
4771   option=GetImageOption(clone_info,"interword-spacing");
4772   if (option != (const char *) NULL)
4773     draw_info->interword_spacing=StringToDouble(option);
4774   option=GetImageOption(clone_info,"fill");
4775   if (option != (const char *) NULL)
4776     (void) QueryColorDatabase(option,&draw_info->fill,exception);
4777   option=GetImageOption(clone_info,"stroke");
4778   if (option != (const char *) NULL)
4779     (void) QueryColorDatabase(option,&draw_info->stroke,exception);
4780   option=GetImageOption(clone_info,"strokewidth");
4781   if (option != (const char *) NULL)
4782     draw_info->stroke_width=StringToDouble(option);
4783   option=GetImageOption(clone_info,"undercolor");
4784   if (option != (const char *) NULL)
4785     (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
4786   option=GetImageOption(clone_info,"gravity");
4787   if (option != (const char *) NULL)
4788     draw_info->gravity=(GravityType) ParseMagickOption(MagickGravityOptions,
4789       MagickFalse,option);
4790   exception=DestroyExceptionInfo(exception);
4791   draw_info->signature=MagickSignature;
4792   clone_info=DestroyImageInfo(clone_info);
4793 }
4794 \f
4795 /*
4796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4797 %                                                                             %
4798 %                                                                             %
4799 %                                                                             %
4800 +   P e r m u t a t e                                                         %
4801 %                                                                             %
4802 %                                                                             %
4803 %                                                                             %
4804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4805 %
4806 %  Permutate() returns the permuation of the (n,k).
4807 %
4808 %  The format of the Permutate method is:
4809 %
4810 %      void Permutate(long n,long k)
4811 %
4812 %  A description of each parameter follows:
4813 %
4814 %    o n:
4815 %
4816 %    o k:
4817 %
4818 %
4819 */
4820 static inline MagickRealType Permutate(const long n,const long k)
4821 {
4822   MagickRealType
4823     r;
4824
4825   register long
4826     i;
4827
4828   r=1.0;
4829   for (i=k+1; i <= n; i++)
4830     r*=i;
4831   for (i=1; i <= (n-k); i++)
4832     r/=i;
4833   return(r);
4834 }
4835 \f
4836 /*
4837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4838 %                                                                             %
4839 %                                                                             %
4840 %                                                                             %
4841 +   T r a c e P r i m i t i v e                                               %
4842 %                                                                             %
4843 %                                                                             %
4844 %                                                                             %
4845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4846 %
4847 %  TracePrimitive is a collection of methods for generating graphic
4848 %  primitives such as arcs, ellipses, paths, etc.
4849 %
4850 */
4851
4852 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4853   const PointInfo end,const PointInfo degrees)
4854 {
4855   PointInfo
4856     center,
4857     radii;
4858
4859   center.x=0.5*(end.x+start.x);
4860   center.y=0.5*(end.y+start.y);
4861   radii.x=fabs(center.x-start.x);
4862   radii.y=fabs(center.y-start.y);
4863   TraceEllipse(primitive_info,center,radii,degrees);
4864 }
4865
4866 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4867   const PointInfo end,const PointInfo arc,const MagickRealType angle,
4868   const MagickBooleanType large_arc,const MagickBooleanType sweep)
4869 {
4870   MagickRealType
4871     alpha,
4872     beta,
4873     delta,
4874     factor,
4875     gamma,
4876     theta;
4877
4878   PointInfo
4879     center,
4880     points[3],
4881     radii;
4882
4883   register MagickRealType
4884     cosine,
4885     sine;
4886
4887   register PrimitiveInfo
4888     *p;
4889
4890   register long
4891     i;
4892
4893   unsigned long
4894     arc_segments;
4895
4896   if ((start.x == end.x) && (start.y == end.y))
4897     {
4898       TracePoint(primitive_info,end);
4899       return;
4900     }
4901   radii.x=fabs(arc.x);
4902   radii.y=fabs(arc.y);
4903   if ((radii.x == 0.0) || (radii.y == 0.0))
4904     {
4905       TraceLine(primitive_info,start,end);
4906       return;
4907     }
4908   cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4909   sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4910   center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4911   center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4912   delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4913     (radii.y*radii.y);
4914   if (delta < MagickEpsilon)
4915     {
4916       TraceLine(primitive_info,start,end);
4917       return;
4918     }
4919   if (delta > 1.0)
4920     {
4921       radii.x*=sqrt((double) delta);
4922       radii.y*=sqrt((double) delta);
4923     }
4924   points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4925   points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4926   points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4927   points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4928   alpha=points[1].x-points[0].x;
4929   beta=points[1].y-points[0].y;
4930   factor=1.0/(alpha*alpha+beta*beta)-0.25;
4931   if (factor <= 0.0)
4932     factor=0.0;
4933   else
4934     {
4935       factor=sqrt((double) factor);
4936       if (sweep == large_arc)
4937         factor=(-factor);
4938     }
4939   center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4940   center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4941   alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4942   theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4943   if ((theta < 0.0) && (sweep != MagickFalse))
4944     theta+=(MagickRealType) (2.0*MagickPI);
4945   else
4946     if ((theta > 0.0) && (sweep == MagickFalse))
4947       theta-=(MagickRealType) (2.0*MagickPI);
4948   arc_segments=(unsigned long) ceil(fabs((double) (theta/(0.5*MagickPI+
4949     MagickEpsilon))));
4950   p=primitive_info;
4951   for (i=0; i < (long) arc_segments; i++)
4952   {
4953     beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4954     gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4955       sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4956       sin(fmod((double) beta,DegreesToRadians(360.0)));
4957     points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
4958       arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
4959       (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4960     points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
4961       arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
4962       (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4963     points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
4964       theta/arc_segments),DegreesToRadians(360.0))));
4965     points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
4966       theta/arc_segments),DegreesToRadians(360.0))));
4967     points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
4968       (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4969     points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
4970       (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4971     p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
4972     p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
4973     (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
4974       points[0].y);
4975     (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
4976       points[0].y);
4977     (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
4978       points[1].y);
4979     (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
4980       points[1].y);
4981     (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
4982       points[2].y);
4983     (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
4984       points[2].y);
4985     if (i == (long) (arc_segments-1))
4986       (p+3)->point=end;
4987     TraceBezier(p,4);
4988     p+=p->coordinates;
4989   }
4990   primitive_info->coordinates=(unsigned long) (p-primitive_info);
4991   for (i=0; i < (long) primitive_info->coordinates; i++)
4992   {
4993     p->primitive=primitive_info->primitive;
4994     p--;
4995   }
4996 }
4997
4998 static void TraceBezier(PrimitiveInfo *primitive_info,
4999   const unsigned long number_coordinates)
5000 {
5001   MagickRealType
5002     alpha,
5003     *coefficients,
5004     weight;
5005
5006   PointInfo
5007     end,
5008     point,
5009     *points;
5010
5011   register long
5012     i,
5013     j;
5014
5015   register PrimitiveInfo
5016     *p;
5017
5018   unsigned long
5019     control_points,
5020     quantum;
5021
5022   /*
5023     Allocate coeficients.
5024   */
5025   quantum=number_coordinates;
5026   for (i=0; i < (long) number_coordinates; i++)
5027   {
5028     for (j=i+1; j < (long) number_coordinates; j++)
5029     {
5030       alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5031       if (alpha > (MagickRealType) quantum)
5032         quantum=(unsigned long) alpha;
5033       alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5034       if (alpha > (MagickRealType) quantum)
5035         quantum=(unsigned long) alpha;
5036     }
5037   }
5038   quantum=(unsigned long) MagickMin((double) quantum/number_coordinates,
5039     (double) BezierQuantum);
5040   control_points=quantum*number_coordinates;
5041   coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5042     number_coordinates,sizeof(*coefficients));
5043   points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5044     sizeof(*points));
5045   if ((coefficients == (MagickRealType *) NULL) ||
5046       (points == (PointInfo *) NULL))
5047     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5048   /*
5049     Compute bezier points.
5050   */
5051   end=primitive_info[number_coordinates-1].point;
5052   for (i=0; i < (long) number_coordinates; i++)
5053     coefficients[i]=Permutate((long) number_coordinates-1,i);
5054   weight=0.0;
5055   for (i=0; i < (long) control_points; i++)
5056   {
5057     p=primitive_info;
5058     point.x=0.0;
5059     point.y=0.0;
5060     alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5061     for (j=0; j < (long) number_coordinates; j++)
5062     {
5063       point.x+=alpha*coefficients[j]*p->point.x;
5064       point.y+=alpha*coefficients[j]*p->point.y;
5065       alpha*=weight/(1.0-weight);
5066       p++;
5067     }
5068     points[i]=point;
5069     weight+=1.0/control_points;
5070   }
5071   /*
5072     Bezier curves are just short segmented polys.
5073   */
5074   p=primitive_info;
5075   for (i=0; i < (long) control_points; i++)
5076   {
5077     TracePoint(p,points[i]);
5078     p+=p->coordinates;
5079   }
5080   TracePoint(p,end);
5081   p+=p->coordinates;
5082   primitive_info->coordinates=(unsigned long) (p-primitive_info);
5083   for (i=0; i < (long) primitive_info->coordinates; i++)
5084   {
5085     p->primitive=primitive_info->primitive;
5086     p--;
5087   }
5088   points=(PointInfo *) RelinquishMagickMemory(points);
5089   coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5090 }
5091
5092 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5093   const PointInfo end)
5094 {
5095   MagickRealType
5096     alpha,
5097     beta,
5098     radius;
5099
5100   PointInfo
5101     offset,
5102     degrees;
5103
5104   alpha=end.x-start.x;
5105   beta=end.y-start.y;
5106   radius=hypot((double) alpha,(double) beta);
5107   offset.x=(double) radius;
5108   offset.y=(double) radius;
5109   degrees.x=0.0;
5110   degrees.y=360.0;
5111   TraceEllipse(primitive_info,start,offset,degrees);
5112 }
5113
5114 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5115   const PointInfo stop,const PointInfo degrees)
5116 {
5117   MagickRealType
5118     delta,
5119     step,
5120     y;
5121
5122   PointInfo
5123     angle,
5124     point;
5125
5126   register PrimitiveInfo
5127     *p;
5128
5129   register long
5130     i;
5131
5132   /*
5133     Ellipses are just short segmented polys.
5134   */
5135   if ((stop.x == 0.0) && (stop.y == 0.0))
5136     {
5137       TracePoint(primitive_info,start);
5138       return;
5139     }
5140   delta=2.0/MagickMax(stop.x,stop.y);
5141   step=(MagickRealType) (MagickPI/8.0);
5142   if (delta < (MagickPI/8.0))
5143     step=MagickPI/(4*(MagickPI/delta/2+0.5));
5144   angle.x=DegreesToRadians(degrees.x);
5145   y=degrees.y;
5146   while (y < degrees.x)
5147     y+=360.0;
5148   angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5149   for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5150   {
5151     point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5152     point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5153     TracePoint(p,point);
5154     p+=p->coordinates;
5155   }
5156   point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5157   point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5158   TracePoint(p,point);
5159   p+=p->coordinates;
5160   primitive_info->coordinates=(unsigned long) (p-primitive_info);
5161   for (i=0; i < (long) primitive_info->coordinates; i++)
5162   {
5163     p->primitive=primitive_info->primitive;
5164     p--;
5165   }
5166 }
5167
5168 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5169   const PointInfo end)
5170 {
5171   TracePoint(primitive_info,start);
5172   if ((fabs(start.x-end.x) <= MagickEpsilon) &&
5173       (fabs(start.y-end.y) <= MagickEpsilon))
5174     {
5175       primitive_info->primitive=PointPrimitive;
5176       primitive_info->coordinates=1;
5177       return;
5178     }
5179   TracePoint(primitive_info+1,end);
5180   (primitive_info+1)->primitive=primitive_info->primitive;
5181   primitive_info->coordinates=2;
5182 }
5183
5184 static unsigned long TracePath(PrimitiveInfo *primitive_info,const char *path)
5185 {
5186   char
5187     token[MaxTextExtent];
5188
5189   const char
5190     *p;
5191
5192   int
5193     attribute,
5194     last_attribute;
5195
5196   MagickRealType
5197     x,
5198     y;
5199
5200   PointInfo
5201     end,
5202     points[4],
5203     point,
5204     start;
5205
5206   PrimitiveType
5207     primitive_type;
5208
5209   register PrimitiveInfo
5210     *q;
5211
5212   register long
5213     i;
5214
5215   unsigned long
5216     number_coordinates,
5217     z_count;
5218
5219   attribute=0;
5220   point.x=0.0;
5221   point.y=0.0;
5222   start.x=0.0;
5223   start.y=0.0;
5224   number_coordinates=0;
5225   z_count=0;
5226   primitive_type=primitive_info->primitive;
5227   q=primitive_info;
5228   for (p=path; *p != '\0'; )
5229   {
5230     while (isspace((int) ((unsigned char) *p)) != 0)
5231       p++;
5232     if (*p == '\0')
5233       break;
5234     last_attribute=attribute;
5235     attribute=(int) (*p++);
5236     switch (attribute)
5237     {
5238       case 'a':
5239       case 'A':
5240       {
5241         MagickBooleanType
5242           large_arc,
5243           sweep;
5244
5245         MagickRealType
5246           angle;
5247
5248         PointInfo
5249           arc;
5250
5251         /*
5252           Compute arc points.
5253         */
5254         do
5255         {
5256           GetMagickToken(p,&p,token);
5257           if (*token == ',')
5258             GetMagickToken(p,&p,token);
5259           arc.x=StringToDouble(token);
5260           GetMagickToken(p,&p,token);
5261           if (*token == ',')
5262             GetMagickToken(p,&p,token);
5263           arc.y=StringToDouble(token);
5264           GetMagickToken(p,&p,token);
5265           if (*token == ',')
5266             GetMagickToken(p,&p,token);
5267           angle=StringToDouble(token);
5268           GetMagickToken(p,&p,token);
5269           if (*token == ',')
5270             GetMagickToken(p,&p,token);
5271           large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5272           GetMagickToken(p,&p,token);
5273           if (*token == ',')
5274             GetMagickToken(p,&p,token);
5275           sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5276           GetMagickToken(p,&p,token);
5277           if (*token == ',')
5278             GetMagickToken(p,&p,token);
5279           x=StringToDouble(token);
5280           GetMagickToken(p,&p,token);
5281           if (*token == ',')
5282             GetMagickToken(p,&p,token);
5283           y=StringToDouble(token);
5284           end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5285           end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5286           TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5287           q+=q->coordinates;
5288           point=end;
5289         } while (IsPoint(p) != MagickFalse);
5290         break;
5291       }
5292       case 'c':
5293       case 'C':
5294       {
5295         /*
5296           Compute bezier points.
5297         */
5298         do
5299         {
5300           points[0]=point;
5301           for (i=1; i < 4; i++)
5302           {
5303             GetMagickToken(p,&p,token);
5304             if (*token == ',')
5305               GetMagickToken(p,&p,token);
5306             x=StringToDouble(token);
5307             GetMagickToken(p,&p,token);
5308             if (*token == ',')
5309               GetMagickToken(p,&p,token);
5310             y=StringToDouble(token);
5311             end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5312             end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5313             points[i]=end;
5314           }
5315           for (i=0; i < 4; i++)
5316             (q+i)->point=points[i];
5317           TraceBezier(q,4);
5318           q+=q->coordinates;
5319           point=end;
5320         } while (IsPoint(p) != MagickFalse);
5321         break;
5322       }
5323       case 'H':
5324       case 'h':
5325       {
5326         do
5327         {
5328           GetMagickToken(p,&p,token);
5329           if (*token == ',')
5330             GetMagickToken(p,&p,token);
5331           x=StringToDouble(token);
5332           point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5333           TracePoint(q,point);
5334           q+=q->coordinates;
5335         } while (IsPoint(p) != MagickFalse);
5336         break;
5337       }
5338       case 'l':
5339       case 'L':
5340       {
5341         do
5342         {
5343           GetMagickToken(p,&p,token);
5344           if (*token == ',')
5345             GetMagickToken(p,&p,token);
5346           x=StringToDouble(token);
5347           GetMagickToken(p,&p,token);
5348           if (*token == ',')
5349             GetMagickToken(p,&p,token);
5350           y=StringToDouble(token);
5351           point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5352           point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5353           TracePoint(q,point);
5354           q+=q->coordinates;
5355         } while (IsPoint(p) != MagickFalse);
5356         break;
5357       }
5358       case 'M':
5359       case 'm':
5360       {
5361         if (q != primitive_info)
5362           {
5363             primitive_info->coordinates=(unsigned long) (q-primitive_info);
5364             number_coordinates+=primitive_info->coordinates;
5365             primitive_info=q;
5366           }
5367         i=0;
5368         do
5369         {
5370           GetMagickToken(p,&p,token);
5371           if (*token == ',')
5372             GetMagickToken(p,&p,token);
5373           x=StringToDouble(token);
5374           GetMagickToken(p,&p,token);
5375           if (*token == ',')
5376             GetMagickToken(p,&p,token);
5377           y=StringToDouble(token);
5378           point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5379           point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5380           if (i == 0)
5381             start=point;
5382           i++;
5383           TracePoint(q,point);
5384           q+=q->coordinates;
5385           if (attribute == (int) 'M')
5386             {
5387               TracePoint(q,point);
5388               q+=q->coordinates;
5389             }
5390         } while (IsPoint(p) != MagickFalse);
5391         break;
5392       }
5393       case 'q':
5394       case 'Q':
5395       {
5396         /*
5397           Compute bezier points.
5398         */
5399         do
5400         {
5401           points[0]=point;
5402           for (i=1; i < 3; i++)
5403           {
5404             GetMagickToken(p,&p,token);
5405             if (*token == ',')
5406               GetMagickToken(p,&p,token);
5407             x=StringToDouble(token);
5408             GetMagickToken(p,&p,token);
5409             if (*token == ',')
5410               GetMagickToken(p,&p,token);
5411             y=StringToDouble(token);
5412             if (*p == ',')
5413               p++;
5414             end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5415             end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5416             points[i]=end;
5417           }
5418           for (i=0; i < 3; i++)
5419             (q+i)->point=points[i];
5420           TraceBezier(q,3);
5421           q+=q->coordinates;
5422           point=end;
5423         } while (IsPoint(p) != MagickFalse);
5424         break;
5425       }
5426       case 's':
5427       case 'S':
5428       {
5429         /*
5430           Compute bezier points.
5431         */
5432         do
5433         {
5434           points[0]=points[3];
5435           points[1].x=2.0*points[3].x-points[2].x;
5436           points[1].y=2.0*points[3].y-points[2].y;
5437           for (i=2; i < 4; i++)
5438           {
5439             GetMagickToken(p,&p,token);
5440             if (*token == ',')
5441               GetMagickToken(p,&p,token);
5442             x=StringToDouble(token);
5443             GetMagickToken(p,&p,token);
5444             if (*token == ',')
5445               GetMagickToken(p,&p,token);
5446             y=StringToDouble(token);
5447             if (*p == ',')
5448               p++;
5449             end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5450             end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5451             points[i]=end;
5452           }
5453           if (strchr("CcSs",last_attribute) == (char *) NULL)
5454             {
5455               points[0]=points[2];
5456               points[1]=points[3];
5457             }
5458           for (i=0; i < 4; i++)
5459             (q+i)->point=points[i];
5460           TraceBezier(q,4);
5461           q+=q->coordinates;
5462           point=end;
5463         } while (IsPoint(p) != MagickFalse);
5464         break;
5465       }
5466       case 't':
5467       case 'T':
5468       {
5469         /*
5470           Compute bezier points.
5471         */
5472         do
5473         {
5474           points[0]=points[2];
5475           points[1].x=2.0*points[2].x-points[1].x;
5476           points[1].y=2.0*points[2].y-points[1].y;
5477           for (i=2; i < 3; i++)
5478           {
5479             GetMagickToken(p,&p,token);
5480             if (*token == ',')
5481               GetMagickToken(p,&p,token);
5482             x=StringToDouble(token);
5483             GetMagickToken(p,&p,token);
5484             if (*token == ',')
5485               GetMagickToken(p,&p,token);
5486             y=StringToDouble(token);
5487             end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5488             end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5489             points[i]=end;
5490           }
5491           if (strchr("QqTt",last_attribute) == (char *) NULL)
5492             {
5493               points[0]=points[2];
5494               points[1]=points[3];
5495             }
5496           for (i=0; i < 3; i++)
5497             (q+i)->point=points[i];
5498           TraceBezier(q,3);
5499           q+=q->coordinates;
5500           point=end;
5501         } while (IsPoint(p) != MagickFalse);
5502         break;
5503       }
5504       case 'v':
5505       case 'V':
5506       {
5507         do
5508         {
5509           GetMagickToken(p,&p,token);
5510           if (*token == ',')
5511             GetMagickToken(p,&p,token);
5512           y=StringToDouble(token);
5513           point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5514           TracePoint(q,point);
5515           q+=q->coordinates;
5516         } while (IsPoint(p) != MagickFalse);
5517         break;
5518       }
5519       case 'z':
5520       case 'Z':
5521       {
5522         point=start;
5523         TracePoint(q,point);
5524         q+=q->coordinates;
5525         primitive_info->coordinates=(unsigned long) (q-primitive_info);
5526         number_coordinates+=primitive_info->coordinates;
5527         primitive_info=q;
5528         z_count++;
5529         break;
5530       }
5531       default:
5532       {
5533         if (isalpha((int) ((unsigned char) attribute)) != 0)
5534           (void) fprintf(stderr,"attribute not recognized: %c\n",attribute);
5535         break;
5536       }
5537     }
5538   }
5539   primitive_info->coordinates=(unsigned long) (q-primitive_info);
5540   number_coordinates+=primitive_info->coordinates;
5541   for (i=0; i < (long) number_coordinates; i++)
5542   {
5543     q--;
5544     q->primitive=primitive_type;
5545     if (z_count > 1)
5546       q->method=FillToBorderMethod;
5547   }
5548   q=primitive_info;
5549   return(number_coordinates);
5550 }
5551
5552 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5553   const PointInfo end)
5554 {
5555   PointInfo
5556     point;
5557
5558   register PrimitiveInfo
5559     *p;
5560
5561   register long
5562     i;
5563
5564   p=primitive_info;
5565   TracePoint(p,start);
5566   p+=p->coordinates;
5567   point.x=start.x;
5568   point.y=end.y;
5569   TracePoint(p,point);
5570   p+=p->coordinates;
5571   TracePoint(p,end);
5572   p+=p->coordinates;
5573   point.x=end.x;
5574   point.y=start.y;
5575   TracePoint(p,point);
5576   p+=p->coordinates;
5577   TracePoint(p,start);
5578   p+=p->coordinates;
5579   primitive_info->coordinates=(unsigned long) (p-primitive_info);
5580   for (i=0; i < (long) primitive_info->coordinates; i++)
5581   {
5582     p->primitive=primitive_info->primitive;
5583     p--;
5584   }
5585 }
5586
5587 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5588   const PointInfo start,const PointInfo end,PointInfo arc)
5589 {
5590   PointInfo
5591     degrees,
5592     offset,
5593     point;
5594
5595   register PrimitiveInfo
5596     *p;
5597
5598   register long
5599     i;
5600
5601   p=primitive_info;
5602   offset.x=fabs(end.x-start.x);
5603   offset.y=fabs(end.y-start.y);
5604   if (arc.x > (0.5*offset.x))
5605     arc.x=0.5*offset.x;
5606   if (arc.y > (0.5*offset.y))
5607     arc.y=0.5*offset.y;
5608   point.x=start.x+offset.x-arc.x;
5609   point.y=start.y+arc.y;
5610   degrees.x=270.0;
5611   degrees.y=360.0;
5612   TraceEllipse(p,point,arc,degrees);
5613   p+=p->coordinates;
5614   point.x=start.x+offset.x-arc.x;
5615   point.y=start.y+offset.y-arc.y;
5616   degrees.x=0.0;
5617   degrees.y=90.0;
5618   TraceEllipse(p,point,arc,degrees);
5619   p+=p->coordinates;
5620   point.x=start.x+arc.x;
5621   point.y=start.y+offset.y-arc.y;
5622   degrees.x=90.0;
5623   degrees.y=180.0;
5624   TraceEllipse(p,point,arc,degrees);
5625   p+=p->coordinates;
5626   point.x=start.x+arc.x;
5627   point.y=start.y+arc.y;
5628   degrees.x=180.0;
5629   degrees.y=270.0;
5630   TraceEllipse(p,point,arc,degrees);
5631   p+=p->coordinates;
5632   TracePoint(p,primitive_info->point);
5633   p+=p->coordinates;
5634   primitive_info->coordinates=(unsigned long) (p-primitive_info);
5635   for (i=0; i < (long) primitive_info->coordinates; i++)
5636   {
5637     p->primitive=primitive_info->primitive;
5638     p--;
5639   }
5640 }
5641
5642 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5643   const unsigned long number_vertices,const MagickRealType offset)
5644 {
5645   MagickRealType
5646     distance;
5647
5648   long
5649     j;
5650
5651   register MagickRealType
5652     dx,
5653     dy;
5654
5655   register long
5656     i;
5657
5658   dx=0.0;
5659   dy=0.0;
5660   for (i=1; i < (long) number_vertices; i++)
5661   {
5662     dx=primitive_info[0].point.x-primitive_info[i].point.x;
5663     dy=primitive_info[0].point.y-primitive_info[i].point.y;
5664     if ((fabs((double) dx) >= MagickEpsilon) ||
5665         (fabs((double) dy) >= MagickEpsilon))
5666       break;
5667   }
5668   if (i == (long) number_vertices)
5669     i=(long) number_vertices-1L;
5670   distance=hypot((double) dx,(double) dy);
5671   primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5672     dx*(distance+offset)/distance);
5673   primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5674     dy*(distance+offset)/distance);
5675   for (j=(long) number_vertices-2; j >= 0;  j--)
5676   {
5677     dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5678     dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5679     if ((fabs((double) dx) >= MagickEpsilon) ||
5680         (fabs((double) dy) >= MagickEpsilon))
5681       break;
5682   }
5683   distance=hypot((double) dx,(double) dy);
5684   primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5685     dx*(distance+offset)/distance);
5686   primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5687     dy*(distance+offset)/distance);
5688 }
5689
5690 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5691   const PrimitiveInfo *primitive_info)
5692 {
5693   typedef struct _LineSegment
5694   {
5695     double
5696       p,
5697       q;
5698   } LineSegment;
5699
5700   LineSegment
5701     dx,
5702     dy,
5703     inverse_slope,
5704     slope,
5705     theta;
5706
5707   long
5708     j,
5709     n,
5710     p,
5711     q;
5712
5713   MagickBooleanType
5714     closed_path;
5715
5716   MagickRealType
5717     delta_theta,
5718     dot_product,
5719     mid,
5720     miterlimit;
5721
5722   PointInfo
5723     box_p[5],
5724     box_q[5],
5725     center,
5726     offset,
5727     *path_p,
5728     *path_q;
5729
5730   PrimitiveInfo
5731     *polygon_primitive,
5732     *stroke_polygon;
5733
5734   register long
5735     i;
5736
5737   unsigned long
5738     arc_segments,
5739     max_strokes,
5740     number_vertices;
5741
5742   /*
5743     Allocate paths.
5744   */
5745   number_vertices=primitive_info->coordinates;
5746   max_strokes=2*number_vertices+6*BezierQuantum+360;
5747   path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5748     sizeof(*path_p));
5749   path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5750     sizeof(*path_q));
5751   polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5752     number_vertices+2UL,sizeof(*polygon_primitive));
5753   if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5754       (polygon_primitive == (PrimitiveInfo *) NULL))
5755     return((PrimitiveInfo *) NULL);
5756   (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5757     number_vertices*sizeof(*polygon_primitive));
5758   closed_path=
5759     (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5760     (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5761     MagickTrue : MagickFalse;
5762   if ((draw_info->linejoin == RoundJoin) ||
5763       ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5764     {
5765       polygon_primitive[number_vertices]=primitive_info[1];
5766       number_vertices++;
5767     }
5768   polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5769   /*
5770     Compute the slope for the first line segment, p.
5771   */
5772   dx.p=0.0;
5773   dy.p=0.0;
5774   for (n=1; n < (long) number_vertices; n++)
5775   {
5776     dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5777     dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5778     if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5779       break;
5780   }
5781   if (n == (long) number_vertices)
5782     n=(long) number_vertices-1L;
5783   slope.p=0.0;
5784   inverse_slope.p=0.0;
5785   if (fabs(dx.p) <= MagickEpsilon)
5786     {
5787       if (dx.p >= 0.0)
5788         slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5789       else
5790         slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5791     }
5792   else
5793     if (fabs(dy.p) <= MagickEpsilon)
5794       {
5795         if (dy.p >= 0.0)
5796           inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5797         else
5798           inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5799       }
5800     else
5801       {
5802         slope.p=dy.p/dx.p;
5803         inverse_slope.p=(-1.0/slope.p);
5804       }
5805   mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5806   miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5807     mid*mid);
5808   if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5809     TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5810   offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5811   offset.y=(double) (offset.x*inverse_slope.p);
5812   if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5813     {
5814       box_p[0].x=polygon_primitive[0].point.x-offset.x;
5815       box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5816       box_p[1].x=polygon_primitive[n].point.x-offset.x;
5817       box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5818       box_q[0].x=polygon_primitive[0].point.x+offset.x;
5819       box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5820       box_q[1].x=polygon_primitive[n].point.x+offset.x;
5821       box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5822     }
5823   else
5824     {
5825       box_p[0].x=polygon_primitive[0].point.x+offset.x;
5826       box_p[0].y=polygon_primitive[0].point.y+offset.y;
5827       box_p[1].x=polygon_primitive[n].point.x+offset.x;
5828       box_p[1].y=polygon_primitive[n].point.y+offset.y;
5829       box_q[0].x=polygon_primitive[0].point.x-offset.x;
5830       box_q[0].y=polygon_primitive[0].point.y-offset.y;
5831       box_q[1].x=polygon_primitive[n].point.x-offset.x;
5832       box_q[1].y=polygon_primitive[n].point.y-offset.y;
5833     }
5834   /*
5835     Create strokes for the line join attribute: bevel, miter, round.
5836   */
5837   p=0;
5838   q=0;
5839   path_q[p++]=box_q[0];
5840   path_p[q++]=box_p[0];
5841   for (i=(long) n+1; i < (long) number_vertices; i++)
5842   {
5843     /*
5844       Compute the slope for this line segment, q.
5845     */
5846     dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5847     dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5848     dot_product=dx.q*dx.q+dy.q*dy.q;
5849     if (dot_product < 0.25)
5850       continue;
5851     slope.q=0.0;
5852     inverse_slope.q=0.0;
5853     if (fabs(dx.q) < MagickEpsilon)
5854       {
5855         if (dx.q >= 0.0)
5856           slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5857         else
5858           slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5859       }
5860     else
5861       if (fabs(dy.q) <= MagickEpsilon)
5862         {
5863           if (dy.q >= 0.0)
5864             inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5865           else
5866             inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5867         }
5868       else
5869         {
5870           slope.q=dy.q/dx.q;
5871           inverse_slope.q=(-1.0/slope.q);
5872         }
5873     offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5874     offset.y=(double) (offset.x*inverse_slope.q);
5875     dot_product=dy.q*offset.x-dx.q*offset.y;
5876     if (dot_product > 0.0)
5877       {
5878         box_p[2].x=polygon_primitive[n].point.x-offset.x;
5879         box_p[2].y=polygon_primitive[n].point.y-offset.y;
5880         box_p[3].x=polygon_primitive[i].point.x-offset.x;
5881         box_p[3].y=polygon_primitive[i].point.y-offset.y;
5882         box_q[2].x=polygon_primitive[n].point.x+offset.x;
5883         box_q[2].y=polygon_primitive[n].point.y+offset.y;
5884         box_q[3].x=polygon_primitive[i].point.x+offset.x;
5885         box_q[3].y=polygon_primitive[i].point.y+offset.y;
5886       }
5887     else
5888       {
5889         box_p[2].x=polygon_primitive[n].point.x+offset.x;
5890         box_p[2].y=polygon_primitive[n].point.y+offset.y;
5891         box_p[3].x=polygon_primitive[i].point.x+offset.x;
5892         box_p[3].y=polygon_primitive[i].point.y+offset.y;
5893         box_q[2].x=polygon_primitive[n].point.x-offset.x;
5894         box_q[2].y=polygon_primitive[n].point.y-offset.y;
5895         box_q[3].x=polygon_primitive[i].point.x-offset.x;
5896         box_q[3].y=polygon_primitive[i].point.y-offset.y;
5897       }
5898     if (fabs((double) (slope.p-slope.q)) <= MagickEpsilon)
5899       {
5900         box_p[4]=box_p[1];
5901         box_q[4]=box_q[1];
5902       }
5903     else
5904       {
5905         box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5906           box_p[3].y)/(slope.p-slope.q));
5907         box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5908         box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5909           box_q[3].y)/(slope.p-slope.q));
5910         box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5911       }
5912     if (q >= (long) (max_strokes-6*BezierQuantum-360))
5913       {
5914          max_strokes+=6*BezierQuantum+360;
5915          path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5916            sizeof(*path_p));
5917          path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5918            sizeof(*path_q));
5919          if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5920            {
5921              polygon_primitive=(PrimitiveInfo *)
5922                RelinquishMagickMemory(polygon_primitive);
5923              return((PrimitiveInfo *) NULL);
5924            }
5925       }
5926     dot_product=dx.q*dy.p-dx.p*dy.q;
5927     if (dot_product <= 0.0)
5928       switch (draw_info->linejoin)
5929       {
5930         case BevelJoin:
5931         {
5932           path_q[q++]=box_q[1];
5933           path_q[q++]=box_q[2];
5934           dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5935             (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5936           if (dot_product <= miterlimit)
5937             path_p[p++]=box_p[4];
5938           else
5939             {
5940               path_p[p++]=box_p[1];
5941               path_p[p++]=box_p[2];
5942             }
5943           break;
5944         }
5945         case MiterJoin:
5946         {
5947           dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5948             (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5949           if (dot_product <= miterlimit)
5950             {
5951               path_q[q++]=box_q[4];
5952               path_p[p++]=box_p[4];
5953             }
5954           else
5955             {
5956               path_q[q++]=box_q[1];
5957               path_q[q++]=box_q[2];
5958               path_p[p++]=box_p[1];
5959               path_p[p++]=box_p[2];
5960             }
5961           break;
5962         }
5963         case RoundJoin:
5964         {
5965           dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5966             (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5967           if (dot_product <= miterlimit)
5968             path_p[p++]=box_p[4];
5969           else
5970             {
5971               path_p[p++]=box_p[1];
5972               path_p[p++]=box_p[2];
5973             }
5974           center=polygon_primitive[n].point;
5975           theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
5976           theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
5977           if (theta.q < theta.p)
5978             theta.q+=(MagickRealType) (2.0*MagickPI);
5979           arc_segments=(unsigned long) ceil((double) ((theta.q-theta.p)/
5980             (2.0*sqrt((double) (1.0/mid)))));
5981           path_q[q].x=box_q[1].x;
5982           path_q[q].y=box_q[1].y;
5983           q++;
5984           for (j=1; j < (long) arc_segments; j++)
5985           {
5986             delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
5987             path_q[q].x=(double) (center.x+mid*cos(fmod((double)
5988               (theta.p+delta_theta),DegreesToRadians(360.0))));
5989             path_q[q].y=(double) (center.y+mid*sin(fmod((double)
5990               (theta.p+delta_theta),DegreesToRadians(360.0))));
5991             q++;
5992           }
5993           path_q[q++]=box_q[2];
5994           break;
5995         }
5996         default:
5997           break;
5998       }
5999     else
6000       switch (draw_info->linejoin)
6001       {
6002         case BevelJoin:
6003         {
6004           path_p[p++]=box_p[1];
6005           path_p[p++]=box_p[2];
6006           dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6007             (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6008           if (dot_product <= miterlimit)
6009             path_q[q++]=box_q[4];
6010           else
6011             {
6012               path_q[q++]=box_q[1];
6013               path_q[q++]=box_q[2];
6014             }
6015           break;
6016         }
6017         case MiterJoin:
6018         {
6019           dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6020             (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6021           if (dot_product <= miterlimit)
6022             {
6023               path_q[q++]=box_q[4];
6024               path_p[p++]=box_p[4];
6025             }
6026           else
6027             {
6028               path_q[q++]=box_q[1];
6029               path_q[q++]=box_q[2];
6030               path_p[p++]=box_p[1];
6031               path_p[p++]=box_p[2];
6032             }
6033           break;
6034         }
6035         case RoundJoin:
6036         {
6037           dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6038             (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6039           if (dot_product <= miterlimit)
6040             path_q[q++]=box_q[4];
6041           else
6042             {
6043               path_q[q++]=box_q[1];
6044               path_q[q++]=box_q[2];
6045             }
6046           center=polygon_primitive[n].point;
6047           theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6048           theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6049           if (theta.p < theta.q)
6050             theta.p+=(MagickRealType) (2.0*MagickPI);
6051           arc_segments=(unsigned long) ceil((double) ((theta.p-theta.q)/
6052             (2.0*sqrt((double) (1.0/mid)))));
6053           path_p[p++]=box_p[1];
6054           for (j=1; j < (long) arc_segments; j++)
6055           {
6056             delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6057             path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6058               (theta.p+delta_theta),DegreesToRadians(360.0))));
6059             path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6060               (theta.p+delta_theta),DegreesToRadians(360.0))));
6061             p++;
6062           }
6063           path_p[p++]=box_p[2];
6064           break;
6065         }
6066         default:
6067           break;
6068       }
6069     slope.p=slope.q;
6070     inverse_slope.p=inverse_slope.q;
6071     box_p[0]=box_p[2];
6072     box_p[1]=box_p[3];
6073     box_q[0]=box_q[2];
6074     box_q[1]=box_q[3];
6075     dx.p=dx.q;
6076     dy.p=dy.q;
6077     n=i;
6078   }
6079   path_p[p++]=box_p[1];
6080   path_q[q++]=box_q[1];
6081   /*
6082     Trace stroked polygon.
6083   */
6084   stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6085     (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6086   if (stroke_polygon != (PrimitiveInfo *) NULL)
6087     {
6088       for (i=0; i < (long) p; i++)
6089       {
6090         stroke_polygon[i]=polygon_primitive[0];
6091         stroke_polygon[i].point=path_p[i];
6092       }
6093       if (closed_path != MagickFalse)
6094         {
6095           stroke_polygon[i]=polygon_primitive[0];
6096           stroke_polygon[i].point=stroke_polygon[0].point;
6097           i++;
6098         }
6099       for ( ; i < (long) (p+q+closed_path); i++)
6100       {
6101         stroke_polygon[i]=polygon_primitive[0];
6102         stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6103       }
6104       if (closed_path != MagickFalse)
6105         {
6106           stroke_polygon[i]=polygon_primitive[0];
6107           stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6108           i++;
6109         }
6110       stroke_polygon[i]=polygon_primitive[0];
6111       stroke_polygon[i].point=stroke_polygon[0].point;
6112       i++;
6113       stroke_polygon[i].primitive=UndefinedPrimitive;
6114       stroke_polygon[0].coordinates=(unsigned long) (p+q+2*closed_path+1);
6115     }
6116   path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6117   path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6118   polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6119   return(stroke_polygon);
6120 }