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