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