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