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