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