2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/color.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/constitute.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/draw-private.h"
61 #include "MagickCore/enhance.h"
62 #include "MagickCore/exception.h"
63 #include "MagickCore/exception-private.h"
64 #include "MagickCore/gem.h"
65 #include "MagickCore/geometry.h"
66 #include "MagickCore/image-private.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/log.h"
69 #include "MagickCore/monitor.h"
70 #include "MagickCore/monitor-private.h"
71 #include "MagickCore/option.h"
72 #include "MagickCore/paint.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/pixel-private.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/resample.h"
77 #include "MagickCore/resample-private.h"
78 #include "MagickCore/resource_.h"
79 #include "MagickCore/string_.h"
80 #include "MagickCore/string-private.h"
81 #include "MagickCore/thread-private.h"
82 #include "MagickCore/token.h"
83 #include "MagickCore/transform.h"
84 #include "MagickCore/utility.h"
89 #define BezierQuantum 200
94 typedef struct _EdgeInfo
118 typedef struct _ElementInfo
128 typedef struct _PolygonInfo
146 typedef struct _PathInfo
156 Forward declarations.
158 static MagickBooleanType
159 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
163 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
166 TracePath(PrimitiveInfo *,const char *);
169 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
170 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
171 const double,const MagickBooleanType,const MagickBooleanType),
172 TraceBezier(PrimitiveInfo *,const size_t),
173 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
174 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
175 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
176 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
177 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
179 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % A c q u i r e D r a w I n f o %
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
194 % The format of the AcquireDrawInfo method is:
196 % DrawInfo *AcquireDrawInfo(void)
199 MagickExport DrawInfo *AcquireDrawInfo(void)
204 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
205 if (draw_info == (DrawInfo *) NULL)
206 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
207 GetDrawInfo((ImageInfo *) NULL,draw_info);
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % C l o n e D r a w I n f o %
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
223 % is specified, a new DrawInfo structure is created initialized to default
226 % The format of the CloneDrawInfo method is:
228 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
229 % const DrawInfo *draw_info)
231 % A description of each parameter follows:
233 % o image_info: the image info.
235 % o draw_info: the draw info.
238 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
239 const DrawInfo *draw_info)
247 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
248 if (clone_info == (DrawInfo *) NULL)
249 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
250 GetDrawInfo(image_info,clone_info);
251 if (draw_info == (DrawInfo *) NULL)
253 exception=AcquireExceptionInfo();
254 (void) CloneString(&clone_info->primitive,draw_info->primitive);
255 (void) CloneString(&clone_info->geometry,draw_info->geometry);
256 clone_info->viewbox=draw_info->viewbox;
257 clone_info->affine=draw_info->affine;
258 clone_info->gravity=draw_info->gravity;
259 clone_info->fill=draw_info->fill;
260 clone_info->stroke=draw_info->stroke;
261 clone_info->stroke_width=draw_info->stroke_width;
262 if (draw_info->fill_pattern != (Image *) NULL)
263 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
265 if (draw_info->stroke_pattern != (Image *) NULL)
266 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
267 MagickTrue,exception);
268 clone_info->stroke_antialias=draw_info->stroke_antialias;
269 clone_info->text_antialias=draw_info->text_antialias;
270 clone_info->fill_rule=draw_info->fill_rule;
271 clone_info->linecap=draw_info->linecap;
272 clone_info->linejoin=draw_info->linejoin;
273 clone_info->miterlimit=draw_info->miterlimit;
274 clone_info->dash_offset=draw_info->dash_offset;
275 clone_info->decorate=draw_info->decorate;
276 clone_info->compose=draw_info->compose;
277 (void) CloneString(&clone_info->text,draw_info->text);
278 (void) CloneString(&clone_info->font,draw_info->font);
279 (void) CloneString(&clone_info->metrics,draw_info->metrics);
280 (void) CloneString(&clone_info->family,draw_info->family);
281 clone_info->style=draw_info->style;
282 clone_info->stretch=draw_info->stretch;
283 clone_info->weight=draw_info->weight;
284 (void) CloneString(&clone_info->encoding,draw_info->encoding);
285 clone_info->pointsize=draw_info->pointsize;
286 clone_info->kerning=draw_info->kerning;
287 clone_info->interline_spacing=draw_info->interline_spacing;
288 clone_info->interword_spacing=draw_info->interword_spacing;
289 clone_info->direction=draw_info->direction;
290 (void) CloneString(&clone_info->density,draw_info->density);
291 clone_info->align=draw_info->align;
292 clone_info->undercolor=draw_info->undercolor;
293 clone_info->border_color=draw_info->border_color;
294 (void) CloneString(&clone_info->server_name,draw_info->server_name);
295 if (draw_info->dash_pattern != (double *) NULL)
300 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
301 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
302 sizeof(*clone_info->dash_pattern));
303 if (clone_info->dash_pattern == (double *) NULL)
304 ThrowFatalException(ResourceLimitFatalError,
305 "UnableToAllocateDashPattern");
306 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
307 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
309 clone_info->gradient=draw_info->gradient;
310 if (draw_info->gradient.stops != (StopInfo *) NULL)
315 number_stops=clone_info->gradient.number_stops;
316 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
317 number_stops,sizeof(*clone_info->gradient.stops));
318 if (clone_info->gradient.stops == (StopInfo *) NULL)
319 ThrowFatalException(ResourceLimitFatalError,
320 "UnableToAllocateDashPattern");
321 (void) CopyMagickMemory(clone_info->gradient.stops,
322 draw_info->gradient.stops,(size_t) number_stops*
323 sizeof(*clone_info->gradient.stops));
325 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
326 clone_info->bounds=draw_info->bounds;
327 clone_info->clip_units=draw_info->clip_units;
328 clone_info->render=draw_info->render;
329 clone_info->alpha=draw_info->alpha;
330 clone_info->element_reference=draw_info->element_reference;
331 clone_info->debug=IsEventLogging();
332 exception=DestroyExceptionInfo(exception);
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 + C o n v e r t P a t h T o P o l y g o n %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % ConvertPathToPolygon() converts a path to the more efficient sorted
350 % The format of the ConvertPathToPolygon method is:
352 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
353 % const PathInfo *path_info)
355 % A description of each parameter follows:
357 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
358 % rendering form of type PolygonInfo.
360 % o draw_info: Specifies a pointer to an DrawInfo structure.
362 % o path_info: Specifies a pointer to an PathInfo structure.
367 #if defined(__cplusplus) || defined(c_plusplus)
371 static int CompareEdges(const void *x,const void *y)
373 register const EdgeInfo
380 p=(const EdgeInfo *) x;
381 q=(const EdgeInfo *) y;
382 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
384 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
386 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
388 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
390 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
391 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
396 #if defined(__cplusplus) || defined(c_plusplus)
400 static void LogPolygonInfo(const PolygonInfo *polygon_info)
409 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
410 p=polygon_info->edges;
411 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
413 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
415 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
416 p->direction != MagickFalse ? "down" : "up");
417 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
418 p->ghostline != MagickFalse ? "transparent" : "opaque");
419 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
420 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
421 p->bounds.x2,p->bounds.y2);
422 for (j=0; j < (ssize_t) p->number_points; j++)
423 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
424 p->points[j].x,p->points[j].y);
427 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
430 static void ReversePoints(PointInfo *points,const size_t number_points)
438 for (i=0; i < (ssize_t) (number_points >> 1); i++)
441 points[i]=points[number_points-(i+1)];
442 points[number_points-(i+1)]=point;
446 static PolygonInfo *ConvertPathToPolygon(
447 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
476 Convert a path to the more efficient sorted rendering form.
478 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
479 if (polygon_info == (PolygonInfo *) NULL)
480 return((PolygonInfo *) NULL);
482 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
483 sizeof(*polygon_info->edges));
484 if (polygon_info->edges == (EdgeInfo *) NULL)
485 return((PolygonInfo *) NULL);
488 ghostline=MagickFalse;
491 points=(PointInfo *) NULL;
492 (void) ResetMagickMemory(&point,0,sizeof(point));
493 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
494 for (i=0; path_info[i].code != EndCode; i++)
496 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
497 (path_info[i].code == GhostlineCode))
502 if ((points != (PointInfo *) NULL) && (n >= 2))
504 if (edge == number_edges)
507 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
508 polygon_info->edges,(size_t) number_edges,
509 sizeof(*polygon_info->edges));
510 if (polygon_info->edges == (EdgeInfo *) NULL)
511 return((PolygonInfo *) NULL);
513 polygon_info->edges[edge].number_points=(size_t) n;
514 polygon_info->edges[edge].scanline=(-1.0);
515 polygon_info->edges[edge].highwater=0;
516 polygon_info->edges[edge].ghostline=ghostline;
517 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
519 ReversePoints(points,(size_t) n);
520 polygon_info->edges[edge].points=points;
521 polygon_info->edges[edge].bounds=bounds;
522 polygon_info->edges[edge].bounds.y1=points[0].y;
523 polygon_info->edges[edge].bounds.y2=points[n-1].y;
524 points=(PointInfo *) NULL;
525 ghostline=MagickFalse;
528 if (points == (PointInfo *) NULL)
531 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
533 if (points == (PointInfo *) NULL)
534 return((PolygonInfo *) NULL);
536 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
537 point=path_info[i].point;
548 next_direction=((path_info[i].point.y > point.y) ||
549 ((path_info[i].point.y == point.y) &&
550 (path_info[i].point.x > point.x))) ? 1 : -1;
551 if ((direction != 0) && (direction != next_direction))
557 if (edge == number_edges)
560 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
561 polygon_info->edges,(size_t) number_edges,
562 sizeof(*polygon_info->edges));
563 if (polygon_info->edges == (EdgeInfo *) NULL)
564 return((PolygonInfo *) NULL);
566 polygon_info->edges[edge].number_points=(size_t) n;
567 polygon_info->edges[edge].scanline=(-1.0);
568 polygon_info->edges[edge].highwater=0;
569 polygon_info->edges[edge].ghostline=ghostline;
570 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
572 ReversePoints(points,(size_t) n);
573 polygon_info->edges[edge].points=points;
574 polygon_info->edges[edge].bounds=bounds;
575 polygon_info->edges[edge].bounds.y1=points[0].y;
576 polygon_info->edges[edge].bounds.y2=points[n-1].y;
578 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
580 if (points == (PointInfo *) NULL)
581 return((PolygonInfo *) NULL);
583 ghostline=MagickFalse;
589 direction=next_direction;
590 if (points == (PointInfo *) NULL)
592 if (n == (ssize_t) number_points)
595 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
597 if (points == (PointInfo *) NULL)
598 return((PolygonInfo *) NULL);
600 point=path_info[i].point;
602 if (point.x < bounds.x1)
604 if (point.x > bounds.x2)
608 if (points != (PointInfo *) NULL)
611 points=(PointInfo *) RelinquishMagickMemory(points);
614 if (edge == number_edges)
617 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
618 polygon_info->edges,(size_t) number_edges,
619 sizeof(*polygon_info->edges));
620 if (polygon_info->edges == (EdgeInfo *) NULL)
621 return((PolygonInfo *) NULL);
623 polygon_info->edges[edge].number_points=(size_t) n;
624 polygon_info->edges[edge].scanline=(-1.0);
625 polygon_info->edges[edge].highwater=0;
626 polygon_info->edges[edge].ghostline=ghostline;
627 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
629 ReversePoints(points,(size_t) n);
630 polygon_info->edges[edge].points=points;
631 polygon_info->edges[edge].bounds=bounds;
632 polygon_info->edges[edge].bounds.y1=points[0].y;
633 polygon_info->edges[edge].bounds.y2=points[n-1].y;
634 ghostline=MagickFalse;
638 polygon_info->number_edges=edge;
639 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
640 sizeof(*polygon_info->edges),CompareEdges);
641 if (IsEventLogging() != MagickFalse)
642 LogPolygonInfo(polygon_info);
643 return(polygon_info);
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 + C o n v e r t P r i m i t i v e T o P a t h %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
660 % The format of the ConvertPrimitiveToPath method is:
662 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
663 % const PrimitiveInfo *primitive_info)
665 % A description of each parameter follows:
667 % o Method ConvertPrimitiveToPath returns a vector path structure of type
670 % o draw_info: a structure of type DrawInfo.
672 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
677 static void LogPathInfo(const PathInfo *path_info)
679 register const PathInfo
682 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
683 for (p=path_info; p->code != EndCode; p++)
684 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
685 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
686 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
687 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
689 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
692 static PathInfo *ConvertPrimitiveToPath(
693 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
714 Converts a PrimitiveInfo structure into a vector path structure.
716 switch (primitive_info->primitive)
723 return((PathInfo *) NULL);
727 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
728 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
730 if (path_info == (PathInfo *) NULL)
731 return((PathInfo *) NULL);
739 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
742 if (coordinates <= 0)
744 coordinates=(ssize_t) primitive_info[i].coordinates;
745 p=primitive_info[i].point;
751 Eliminate duplicate points.
753 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
754 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
756 path_info[n].code=code;
757 path_info[n].point=primitive_info[i].point;
758 q=primitive_info[i].point;
763 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
764 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
767 Mark the p point as open if it does not match the q.
769 path_info[start].code=OpenCode;
770 path_info[n].code=GhostlineCode;
771 path_info[n].point=primitive_info[i].point;
773 path_info[n].code=LineToCode;
774 path_info[n].point=p;
777 path_info[n].code=EndCode;
778 path_info[n].point.x=0.0;
779 path_info[n].point.y=0.0;
780 if (IsEventLogging() != MagickFalse)
781 LogPathInfo(path_info);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % D e s t r o y D r a w I n f o %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
799 % The format of the DestroyDrawInfo method is:
801 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
803 % A description of each parameter follows:
805 % o draw_info: the draw info.
808 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
810 if (draw_info->debug != MagickFalse)
811 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
812 assert(draw_info != (DrawInfo *) NULL);
813 assert(draw_info->signature == MagickSignature);
814 if (draw_info->primitive != (char *) NULL)
815 draw_info->primitive=DestroyString(draw_info->primitive);
816 if (draw_info->text != (char *) NULL)
817 draw_info->text=DestroyString(draw_info->text);
818 if (draw_info->geometry != (char *) NULL)
819 draw_info->geometry=DestroyString(draw_info->geometry);
820 if (draw_info->fill_pattern != (Image *) NULL)
821 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
822 if (draw_info->stroke_pattern != (Image *) NULL)
823 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
824 if (draw_info->font != (char *) NULL)
825 draw_info->font=DestroyString(draw_info->font);
826 if (draw_info->metrics != (char *) NULL)
827 draw_info->metrics=DestroyString(draw_info->metrics);
828 if (draw_info->family != (char *) NULL)
829 draw_info->family=DestroyString(draw_info->family);
830 if (draw_info->encoding != (char *) NULL)
831 draw_info->encoding=DestroyString(draw_info->encoding);
832 if (draw_info->density != (char *) NULL)
833 draw_info->density=DestroyString(draw_info->density);
834 if (draw_info->server_name != (char *) NULL)
835 draw_info->server_name=(char *)
836 RelinquishMagickMemory(draw_info->server_name);
837 if (draw_info->dash_pattern != (double *) NULL)
838 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
839 draw_info->dash_pattern);
840 if (draw_info->gradient.stops != (StopInfo *) NULL)
841 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
842 draw_info->gradient.stops);
843 if (draw_info->clip_mask != (char *) NULL)
844 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
845 draw_info->signature=(~MagickSignature);
846 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 + D e s t r o y E d g e %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % DestroyEdge() destroys the specified polygon edge.
863 % The format of the DestroyEdge method is:
865 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
867 % A description of each parameter follows:
869 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
871 % o edge: the polygon edge number to destroy.
874 static size_t DestroyEdge(PolygonInfo *polygon_info,
877 assert(edge < polygon_info->number_edges);
878 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
879 polygon_info->edges[edge].points);
880 polygon_info->number_edges--;
881 if (edge < polygon_info->number_edges)
882 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
883 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
884 return(polygon_info->number_edges);
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 + D e s t r o y P o l y g o n I n f o %
896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
900 % The format of the DestroyPolygonInfo method is:
902 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
904 % A description of each parameter follows:
906 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
909 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
914 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
915 polygon_info->edges[i].points=(PointInfo *)
916 RelinquishMagickMemory(polygon_info->edges[i].points);
917 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
918 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926 % D r a w A f f i n e I m a g e %
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 % DrawAffineImage() composites the source over the destination image as
933 % dictated by the affine transform.
935 % The format of the DrawAffineImage method is:
937 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
938 % const AffineMatrix *affine,ExceptionInfo *exception)
940 % A description of each parameter follows:
942 % o image: the image.
944 % o source: the source image.
946 % o affine: the affine transform.
948 % o exception: return any errors or warnings in this structure.
952 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
953 const double y,const SegmentInfo *edge)
966 Determine left and right edges.
968 inverse_edge.x1=edge->x1;
969 inverse_edge.y1=edge->y1;
970 inverse_edge.x2=edge->x2;
971 inverse_edge.y2=edge->y2;
972 z=affine->ry*y+affine->tx;
973 if (affine->sx >= MagickEpsilon)
975 intercept=(-z/affine->sx);
977 if (x > inverse_edge.x1)
979 intercept=(-z+(double) image->columns)/affine->sx;
981 if (x < inverse_edge.x2)
985 if (affine->sx < -MagickEpsilon)
987 intercept=(-z+(double) image->columns)/affine->sx;
989 if (x > inverse_edge.x1)
991 intercept=(-z/affine->sx);
993 if (x < inverse_edge.x2)
997 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
999 inverse_edge.x2=edge->x1;
1000 return(inverse_edge);
1003 Determine top and bottom edges.
1005 z=affine->sy*y+affine->ty;
1006 if (affine->rx >= MagickEpsilon)
1008 intercept=(-z/affine->rx);
1010 if (x > inverse_edge.x1)
1012 intercept=(-z+(double) image->rows)/affine->rx;
1014 if (x < inverse_edge.x2)
1018 if (affine->rx < -MagickEpsilon)
1020 intercept=(-z+(double) image->rows)/affine->rx;
1022 if (x > inverse_edge.x1)
1024 intercept=(-z/affine->rx);
1026 if (x < inverse_edge.x2)
1030 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1032 inverse_edge.x2=edge->x2;
1033 return(inverse_edge);
1035 return(inverse_edge);
1038 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1046 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1048 inverse_affine.sx=determinant*affine->sy;
1049 inverse_affine.rx=determinant*(-affine->rx);
1050 inverse_affine.ry=determinant*(-affine->ry);
1051 inverse_affine.sy=determinant*affine->sx;
1052 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1054 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1056 return(inverse_affine);
1059 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1066 static inline double MagickMax(const double x,const double y)
1073 static inline double MagickMin(const double x,const double y)
1080 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1081 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1108 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1119 Determine bounding box.
1121 assert(image != (Image *) NULL);
1122 assert(image->signature == MagickSignature);
1123 if (image->debug != MagickFalse)
1124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1125 assert(source != (const Image *) NULL);
1126 assert(source->signature == MagickSignature);
1127 assert(affine != (AffineMatrix *) NULL);
1130 extent[1].x=(double) source->columns-1.0;
1132 extent[2].x=(double) source->columns-1.0;
1133 extent[2].y=(double) source->rows-1.0;
1135 extent[3].y=(double) source->rows-1.0;
1136 for (i=0; i < 4; i++)
1139 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1140 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1144 for (i=1; i < 4; i++)
1146 if (min.x > extent[i].x)
1148 if (min.y > extent[i].y)
1150 if (max.x < extent[i].x)
1152 if (max.y < extent[i].y)
1156 Affine transform image.
1158 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1159 return(MagickFalse);
1161 edge.x1=MagickMax(min.x,0.0);
1162 edge.y1=MagickMax(min.y,0.0);
1163 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1164 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1165 inverse_affine=InverseAffineMatrix(affine);
1166 GetPixelInfo(image,&zero);
1167 start=(ssize_t) ceil(edge.y1-0.5);
1168 stop=(ssize_t) floor(edge.y2+0.5);
1169 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1170 height=(size_t) (floor(edge.y2+0.5)-ceil(edge.y1-0.5));
1172 source_view=AcquireVirtualCacheView(source,exception);
1173 image_view=AcquireAuthenticCacheView(image,exception);
1174 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1175 #pragma omp parallel for schedule(static,4) shared(status) \
1176 magick_threads(source,image,height,1)
1178 for (y=start; y <= stop; y++)
1199 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1200 if (inverse_edge.x2 < inverse_edge.x1)
1202 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1203 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) ceil(
1204 inverse_edge.x1-0.5)),1,exception);
1205 if (q == (Quantum *) NULL)
1210 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1212 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1214 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1216 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1217 point.x,point.y,&pixel,exception);
1218 GetPixelInfoPixel(image,q,&composite);
1219 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1221 SetPixelInfoPixel(image,&composite,q);
1223 q+=GetPixelChannels(image);
1225 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1228 source_view=DestroyCacheView(source_view);
1229 image_view=DestroyCacheView(image_view);
1234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1238 + D r a w B o u n d i n g R e c t a n g l e s %
1242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1244 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1245 % is only useful for developers debugging the rendering algorithm.
1247 % The format of the DrawBoundingRectangles method is:
1249 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1250 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1252 % A description of each parameter follows:
1254 % o image: the image.
1256 % o draw_info: the draw info.
1258 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1260 % o exception: return any errors or warnings in this structure.
1263 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1264 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1289 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1290 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1292 resolution.x=DefaultResolution;
1293 resolution.y=DefaultResolution;
1294 if (clone_info->density != (char *) NULL)
1302 flags=ParseGeometry(clone_info->density,&geometry_info);
1303 resolution.x=geometry_info.rho;
1304 resolution.y=geometry_info.sigma;
1305 if ((flags & SigmaValue) == MagickFalse)
1306 resolution.y=resolution.x;
1308 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1309 clone_info->stroke_width/2.0;
1314 if (polygon_info != (PolygonInfo *) NULL)
1316 bounds=polygon_info->edges[0].bounds;
1317 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1319 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1320 bounds.x1=polygon_info->edges[i].bounds.x1;
1321 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1322 bounds.y1=polygon_info->edges[i].bounds.y1;
1323 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1324 bounds.x2=polygon_info->edges[i].bounds.x2;
1325 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1326 bounds.y2=polygon_info->edges[i].bounds.y2;
1329 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1330 image->columns ? (double) image->columns-1 : bounds.x1;
1332 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1333 image->rows ? (double) image->rows-1 : bounds.y1;
1335 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1336 image->columns ? (double) image->columns-1 : bounds.x2;
1338 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1339 image->rows ? (double) image->rows-1 : bounds.y2;
1340 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1342 if (polygon_info->edges[i].direction != 0)
1343 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1346 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1348 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1349 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1350 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1351 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1352 primitive_info[0].primitive=RectanglePrimitive;
1353 TraceRectangle(primitive_info,start,end);
1354 primitive_info[0].method=ReplaceMethod;
1355 coordinates=(ssize_t) primitive_info[0].coordinates;
1356 primitive_info[coordinates].primitive=UndefinedPrimitive;
1357 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1360 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1362 start.x=(double) (bounds.x1-mid);
1363 start.y=(double) (bounds.y1-mid);
1364 end.x=(double) (bounds.x2+mid);
1365 end.y=(double) (bounds.y2+mid);
1366 primitive_info[0].primitive=RectanglePrimitive;
1367 TraceRectangle(primitive_info,start,end);
1368 primitive_info[0].method=ReplaceMethod;
1369 coordinates=(ssize_t) primitive_info[0].coordinates;
1370 primitive_info[coordinates].primitive=UndefinedPrimitive;
1371 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1372 clone_info=DestroyDrawInfo(clone_info);
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % D r a w C l i p P a t h %
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 % DrawClipPath() draws the clip path on the image mask.
1388 % The format of the DrawClipPath method is:
1390 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1391 % const char *name,ExceptionInfo *exception)
1393 % A description of each parameter follows:
1395 % o image: the image.
1397 % o draw_info: the draw info.
1399 % o name: the name of the clip path.
1401 % o exception: return any errors or warnings in this structure.
1404 MagickExport MagickBooleanType DrawClipPath(Image *image,
1405 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1408 filename[MaxTextExtent];
1422 assert(image != (Image *) NULL);
1423 assert(image->signature == MagickSignature);
1424 if (image->debug != MagickFalse)
1425 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1426 assert(draw_info != (const DrawInfo *) NULL);
1427 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1428 value=GetImageArtifact(image,filename);
1429 if (value == (const char *) NULL)
1430 return(MagickFalse);
1431 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1432 if (clip_mask == (Image *) NULL)
1433 return(MagickFalse);
1434 (void) QueryColorCompliance("#0000",AllCompliance,
1435 &clip_mask->background_color,exception);
1436 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1437 (void) SetImageBackgroundColor(clip_mask,exception);
1438 if (image->debug != MagickFalse)
1439 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1440 draw_info->clip_mask);
1441 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1442 (void) CloneString(&clone_info->primitive,value);
1443 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1445 clone_info->clip_mask=(char *) NULL;
1446 status=NegateImage(clip_mask,MagickFalse,exception);
1447 (void) SetImageMask(image,clip_mask,exception);
1448 clip_mask=DestroyImage(clip_mask);
1449 status=DrawImage(image,clone_info,exception);
1450 clone_info=DestroyDrawInfo(clone_info);
1451 if (image->debug != MagickFalse)
1452 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1453 return(status != 0 ? MagickTrue : MagickFalse);
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 + D r a w D a s h P o l y g o n %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1468 % image while respecting the dash offset and dash pattern attributes.
1470 % The format of the DrawDashPolygon method is:
1472 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1473 % const PrimitiveInfo *primitive_info,Image *image,
1474 % ExceptionInfo *exception)
1476 % A description of each parameter follows:
1478 % o draw_info: the draw info.
1480 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1482 % o image: the image.
1484 % o exception: return any errors or warnings in this structure.
1487 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1488 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1520 assert(draw_info != (const DrawInfo *) NULL);
1521 if (image->debug != MagickFalse)
1522 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1523 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1524 clone_info->miterlimit=0;
1525 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1526 number_vertices=(size_t) i;
1527 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1528 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1529 if (dash_polygon == (PrimitiveInfo *) NULL)
1530 return(MagickFalse);
1531 dash_polygon[0]=primitive_info[0];
1532 scale=ExpandAffine(&draw_info->affine);
1533 length=scale*(draw_info->dash_pattern[0]-0.5);
1534 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1536 for (n=0; offset > 0.0; j=0)
1538 if (draw_info->dash_pattern[n] <= 0.0)
1540 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1541 if (offset > length)
1545 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1548 if (offset < length)
1560 for (i=1; i < (ssize_t) number_vertices; i++)
1562 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1563 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1564 maximum_length=hypot((double) dx,dy);
1568 if (draw_info->dash_pattern[n] == 0.0)
1570 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1572 for (total_length=0.0; (total_length+length) <= maximum_length; )
1574 total_length+=length;
1575 if ((n & 0x01) != 0)
1577 dash_polygon[0]=primitive_info[0];
1578 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1579 total_length/maximum_length);
1580 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1581 total_length/maximum_length);
1586 if ((j+1) > (ssize_t) (2*number_vertices))
1588 dash_polygon[j]=primitive_info[i-1];
1589 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1590 total_length/maximum_length);
1591 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1592 total_length/maximum_length);
1593 dash_polygon[j].coordinates=1;
1595 dash_polygon[0].coordinates=(size_t) j;
1596 dash_polygon[j].primitive=UndefinedPrimitive;
1597 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1600 if (draw_info->dash_pattern[n] == 0.0)
1602 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1604 length-=(maximum_length-total_length);
1605 if ((n & 0x01) != 0)
1607 dash_polygon[j]=primitive_info[i];
1608 dash_polygon[j].coordinates=1;
1611 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1613 dash_polygon[j]=primitive_info[i-1];
1614 dash_polygon[j].point.x+=MagickEpsilon;
1615 dash_polygon[j].point.y+=MagickEpsilon;
1616 dash_polygon[j].coordinates=1;
1618 dash_polygon[0].coordinates=(size_t) j;
1619 dash_polygon[j].primitive=UndefinedPrimitive;
1620 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1622 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1623 clone_info=DestroyDrawInfo(clone_info);
1624 if (image->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1626 return(status != 0 ? MagickTrue : MagickFalse);
1630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1634 % D r a w I m a g e %
1638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1640 % DrawImage() draws a graphic primitive on your image. The primitive
1641 % may be represented as a string or filename. Precede the filename with an
1642 % "at" sign (@) and the contents of the file are drawn on the image. You
1643 % can affect how text is drawn by setting one or more members of the draw
1646 % The format of the DrawImage method is:
1648 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1649 % ExceptionInfo *exception)
1651 % A description of each parameter follows:
1653 % o image: the image.
1655 % o draw_info: the draw info.
1657 % o exception: return any errors or warnings in this structure.
1661 static inline MagickBooleanType IsPoint(const char *point)
1669 value=StringToDouble(point,&p);
1670 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1673 static inline void TracePoint(PrimitiveInfo *primitive_info,
1674 const PointInfo point)
1676 primitive_info->coordinates=1;
1677 primitive_info->point=point;
1680 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1681 ExceptionInfo *exception)
1683 #define RenderImageTag "Render/Image"
1690 key[2*MaxTextExtent],
1691 keyword[MaxTextExtent],
1692 geometry[MaxTextExtent],
1693 name[MaxTextExtent],
1694 pattern[MaxTextExtent],
1745 Ensure the annotation info is valid.
1747 assert(image != (Image *) NULL);
1748 assert(image->signature == MagickSignature);
1749 if (image->debug != MagickFalse)
1750 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1751 assert(draw_info != (DrawInfo *) NULL);
1752 assert(draw_info->signature == MagickSignature);
1753 if (image->debug != MagickFalse)
1754 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1755 if ((draw_info->primitive == (char *) NULL) ||
1756 (*draw_info->primitive == '\0'))
1757 return(MagickFalse);
1758 if (image->debug != MagickFalse)
1759 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1760 if (*draw_info->primitive != '@')
1761 primitive=AcquireString(draw_info->primitive);
1763 primitive=FileToString(draw_info->primitive+1,~0,exception);
1764 if (primitive == (char *) NULL)
1765 return(MagickFalse);
1766 primitive_extent=(double) strlen(primitive);
1767 (void) SetImageArtifact(image,"MVG",primitive);
1770 Allocate primitive info memory.
1772 graphic_context=(DrawInfo **) AcquireMagickMemory(
1773 sizeof(*graphic_context));
1774 if (graphic_context == (DrawInfo **) NULL)
1776 primitive=DestroyString(primitive);
1777 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1781 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1782 sizeof(*primitive_info));
1783 if (primitive_info == (PrimitiveInfo *) NULL)
1785 primitive=DestroyString(primitive);
1786 for ( ; n >= 0; n--)
1787 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1788 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1789 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1792 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1793 graphic_context[n]->viewbox=image->page;
1794 if ((image->page.width == 0) || (image->page.height == 0))
1796 graphic_context[n]->viewbox.width=image->columns;
1797 graphic_context[n]->viewbox.height=image->rows;
1799 token=AcquireString(primitive);
1800 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1802 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1803 return(MagickFalse);
1805 for (q=primitive; *q != '\0'; )
1808 Interpret graphic primitive.
1810 GetMagickToken(q,&q,keyword);
1811 if (*keyword == '\0')
1813 if (*keyword == '#')
1818 while ((*q != '\n') && (*q != '\0'))
1822 p=q-strlen(keyword)-1;
1823 primitive_type=UndefinedPrimitive;
1824 current=graphic_context[n]->affine;
1825 GetAffineMatrix(&affine);
1833 if (LocaleCompare("affine",keyword) == 0)
1835 GetMagickToken(q,&q,token);
1836 affine.sx=StringToDouble(token,(char **) NULL);
1837 GetMagickToken(q,&q,token);
1839 GetMagickToken(q,&q,token);
1840 affine.rx=StringToDouble(token,(char **) NULL);
1841 GetMagickToken(q,&q,token);
1843 GetMagickToken(q,&q,token);
1844 affine.ry=StringToDouble(token,(char **) NULL);
1845 GetMagickToken(q,&q,token);
1847 GetMagickToken(q,&q,token);
1848 affine.sy=StringToDouble(token,(char **) NULL);
1849 GetMagickToken(q,&q,token);
1851 GetMagickToken(q,&q,token);
1852 affine.tx=StringToDouble(token,(char **) NULL);
1853 GetMagickToken(q,&q,token);
1855 GetMagickToken(q,&q,token);
1856 affine.ty=StringToDouble(token,(char **) NULL);
1859 if (LocaleCompare("arc",keyword) == 0)
1861 primitive_type=ArcPrimitive;
1870 if (LocaleCompare("bezier",keyword) == 0)
1872 primitive_type=BezierPrimitive;
1875 if (LocaleCompare("border-color",keyword) == 0)
1877 GetMagickToken(q,&q,token);
1878 (void) QueryColorCompliance(token,AllCompliance,
1879 &graphic_context[n]->border_color,exception);
1888 if (LocaleCompare("clip-path",keyword) == 0)
1893 GetMagickToken(q,&q,token);
1894 (void) CloneString(&graphic_context[n]->clip_mask,token);
1895 (void) DrawClipPath(image,graphic_context[n],
1896 graphic_context[n]->clip_mask,exception);
1899 if (LocaleCompare("clip-rule",keyword) == 0)
1904 GetMagickToken(q,&q,token);
1905 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1907 if (fill_rule == -1)
1910 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1913 if (LocaleCompare("clip-units",keyword) == 0)
1918 GetMagickToken(q,&q,token);
1919 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1921 if (clip_units == -1)
1926 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1927 if (clip_units == ObjectBoundingBox)
1929 GetAffineMatrix(¤t);
1930 affine.sx=draw_info->bounds.x2;
1931 affine.sy=draw_info->bounds.y2;
1932 affine.tx=draw_info->bounds.x1;
1933 affine.ty=draw_info->bounds.y1;
1938 if (LocaleCompare("circle",keyword) == 0)
1940 primitive_type=CirclePrimitive;
1943 if (LocaleCompare("color",keyword) == 0)
1945 primitive_type=ColorPrimitive;
1954 if (LocaleCompare("decorate",keyword) == 0)
1959 GetMagickToken(q,&q,token);
1960 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1965 graphic_context[n]->decorate=(DecorationType) decorate;
1974 if (LocaleCompare("ellipse",keyword) == 0)
1976 primitive_type=EllipsePrimitive;
1979 if (LocaleCompare("encoding",keyword) == 0)
1981 GetMagickToken(q,&q,token);
1982 (void) CloneString(&graphic_context[n]->encoding,token);
1991 if (LocaleCompare("fill",keyword) == 0)
1993 GetMagickToken(q,&q,token);
1994 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1995 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1996 (void) DrawPatternPath(image,draw_info,token,
1997 &graphic_context[n]->fill_pattern,exception);
2000 status=QueryColorCompliance(token,AllCompliance,
2001 &graphic_context[n]->fill,exception);
2002 if (status == MagickFalse)
2007 pattern_info=AcquireImageInfo();
2008 (void) CopyMagickString(pattern_info->filename,token,
2010 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2012 CatchException(exception);
2013 pattern_info=DestroyImageInfo(pattern_info);
2018 if (LocaleCompare("fill-alpha",keyword) == 0)
2020 GetMagickToken(q,&q,token);
2021 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2022 graphic_context[n]->fill.alpha=(double) QuantumRange*
2023 factor*StringToDouble(token,(char **) NULL);
2026 if (LocaleCompare("fill-rule",keyword) == 0)
2031 GetMagickToken(q,&q,token);
2032 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2034 if (fill_rule == -1)
2037 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2040 if (LocaleCompare("font",keyword) == 0)
2042 GetMagickToken(q,&q,token);
2043 (void) CloneString(&graphic_context[n]->font,token);
2044 if (LocaleCompare("none",token) == 0)
2045 graphic_context[n]->font=(char *)
2046 RelinquishMagickMemory(graphic_context[n]->font);
2049 if (LocaleCompare("font-family",keyword) == 0)
2051 GetMagickToken(q,&q,token);
2052 (void) CloneString(&graphic_context[n]->family,token);
2055 if (LocaleCompare("font-size",keyword) == 0)
2057 GetMagickToken(q,&q,token);
2058 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2061 if (LocaleCompare("font-stretch",keyword) == 0)
2066 GetMagickToken(q,&q,token);
2067 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2071 graphic_context[n]->stretch=(StretchType) stretch;
2074 if (LocaleCompare("font-style",keyword) == 0)
2079 GetMagickToken(q,&q,token);
2080 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2084 graphic_context[n]->style=(StyleType) style;
2087 if (LocaleCompare("font-weight",keyword) == 0)
2089 GetMagickToken(q,&q,token);
2090 graphic_context[n]->weight=StringToUnsignedLong(token);
2091 if (LocaleCompare(token,"all") == 0)
2092 graphic_context[n]->weight=0;
2093 if (LocaleCompare(token,"bold") == 0)
2094 graphic_context[n]->weight=700;
2095 if (LocaleCompare(token,"bolder") == 0)
2096 if (graphic_context[n]->weight <= 800)
2097 graphic_context[n]->weight+=100;
2098 if (LocaleCompare(token,"lighter") == 0)
2099 if (graphic_context[n]->weight >= 100)
2100 graphic_context[n]->weight-=100;
2101 if (LocaleCompare(token,"normal") == 0)
2102 graphic_context[n]->weight=400;
2111 if (LocaleCompare("gradient-units",keyword) == 0)
2113 GetMagickToken(q,&q,token);
2116 if (LocaleCompare("gravity",keyword) == 0)
2121 GetMagickToken(q,&q,token);
2122 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2126 graphic_context[n]->gravity=(GravityType) gravity;
2135 if (LocaleCompare("image",keyword) == 0)
2140 primitive_type=ImagePrimitive;
2141 GetMagickToken(q,&q,token);
2142 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2146 graphic_context[n]->compose=(CompositeOperator) compose;
2149 if (LocaleCompare("interline-spacing",keyword) == 0)
2151 GetMagickToken(q,&q,token);
2152 graphic_context[n]->interline_spacing=StringToDouble(token,
2156 if (LocaleCompare("interword-spacing",keyword) == 0)
2158 GetMagickToken(q,&q,token);
2159 graphic_context[n]->interword_spacing=StringToDouble(token,
2169 if (LocaleCompare("kerning",keyword) == 0)
2171 GetMagickToken(q,&q,token);
2172 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2181 if (LocaleCompare("line",keyword) == 0)
2182 primitive_type=LinePrimitive;
2190 if (LocaleCompare("matte",keyword) == 0)
2191 primitive_type=MattePrimitive;
2199 if (LocaleCompare("offset",keyword) == 0)
2201 GetMagickToken(q,&q,token);
2204 if (LocaleCompare("opacity",keyword) == 0)
2206 GetMagickToken(q,&q,token);
2207 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2208 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2209 QuantumScale*graphic_context[n]->alpha)*factor*
2210 StringToDouble(token,(char **) NULL))));
2211 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2212 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2221 if (LocaleCompare("path",keyword) == 0)
2223 primitive_type=PathPrimitive;
2226 if (LocaleCompare("point",keyword) == 0)
2228 primitive_type=PointPrimitive;
2231 if (LocaleCompare("polyline",keyword) == 0)
2233 primitive_type=PolylinePrimitive;
2236 if (LocaleCompare("polygon",keyword) == 0)
2238 primitive_type=PolygonPrimitive;
2241 if (LocaleCompare("pop",keyword) == 0)
2243 GetMagickToken(q,&q,token);
2244 if (LocaleCompare("clip-path",token) == 0)
2246 if (LocaleCompare("defs",token) == 0)
2248 if (LocaleCompare("gradient",token) == 0)
2250 if (LocaleCompare("graphic-context",token) == 0)
2254 (void) ThrowMagickException(exception,GetMagickModule(),
2255 DrawError,"UnbalancedGraphicContextPushPop","'%s'",token);
2259 if (graphic_context[n]->clip_mask != (char *) NULL)
2260 if (LocaleCompare(graphic_context[n]->clip_mask,
2261 graphic_context[n-1]->clip_mask) != 0)
2262 (void) SetImageMask(image,(Image *) NULL,exception);
2263 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2267 if (LocaleCompare("pattern",token) == 0)
2272 if (LocaleCompare("push",keyword) == 0)
2274 GetMagickToken(q,&q,token);
2275 if (LocaleCompare("clip-path",token) == 0)
2278 name[MaxTextExtent];
2280 GetMagickToken(q,&q,token);
2281 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2282 for (p=q; *q != '\0'; )
2284 GetMagickToken(q,&q,token);
2285 if (LocaleCompare(token,"pop") != 0)
2287 GetMagickToken(q,(const char **) NULL,token);
2288 if (LocaleCompare(token,"clip-path") != 0)
2292 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2293 (void) SetImageArtifact(image,name,token);
2294 GetMagickToken(q,&q,token);
2297 if (LocaleCompare("gradient",token) == 0)
2300 key[2*MaxTextExtent],
2301 name[MaxTextExtent],
2302 type[MaxTextExtent];
2307 GetMagickToken(q,&q,token);
2308 (void) CopyMagickString(name,token,MaxTextExtent);
2309 GetMagickToken(q,&q,token);
2310 (void) CopyMagickString(type,token,MaxTextExtent);
2311 GetMagickToken(q,&q,token);
2312 segment.x1=StringToDouble(token,(char **) NULL);
2313 GetMagickToken(q,&q,token);
2315 GetMagickToken(q,&q,token);
2316 segment.y1=StringToDouble(token,(char **) NULL);
2317 GetMagickToken(q,&q,token);
2319 GetMagickToken(q,&q,token);
2320 segment.x2=StringToDouble(token,(char **) NULL);
2321 GetMagickToken(q,&q,token);
2323 GetMagickToken(q,&q,token);
2324 segment.y2=StringToDouble(token,(char **) NULL);
2325 if (LocaleCompare(type,"radial") == 0)
2327 GetMagickToken(q,&q,token);
2329 GetMagickToken(q,&q,token);
2331 for (p=q; *q != '\0'; )
2333 GetMagickToken(q,&q,token);
2334 if (LocaleCompare(token,"pop") != 0)
2336 GetMagickToken(q,(const char **) NULL,token);
2337 if (LocaleCompare(token,"gradient") != 0)
2341 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2342 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2343 graphic_context[n]->affine.ry*segment.y1+
2344 graphic_context[n]->affine.tx;
2345 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2346 graphic_context[n]->affine.sy*segment.y1+
2347 graphic_context[n]->affine.ty;
2348 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2349 graphic_context[n]->affine.ry*segment.y2+
2350 graphic_context[n]->affine.tx;
2351 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2352 graphic_context[n]->affine.sy*segment.y2+
2353 graphic_context[n]->affine.ty;
2354 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2355 (void) SetImageArtifact(image,key,token);
2356 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2357 (void) FormatLocaleString(geometry,MaxTextExtent,
2358 "%gx%g%+.15g%+.15g",
2359 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2360 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2361 bounds.x1,bounds.y1);
2362 (void) SetImageArtifact(image,key,geometry);
2363 GetMagickToken(q,&q,token);
2366 if (LocaleCompare("pattern",token) == 0)
2371 GetMagickToken(q,&q,token);
2372 (void) CopyMagickString(name,token,MaxTextExtent);
2373 GetMagickToken(q,&q,token);
2374 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2376 GetMagickToken(q,&q,token);
2378 GetMagickToken(q,&q,token);
2379 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2381 GetMagickToken(q,&q,token);
2383 GetMagickToken(q,&q,token);
2384 bounds.width=(size_t) floor(StringToDouble(token,
2385 (char **) NULL)+0.5);
2386 GetMagickToken(q,&q,token);
2388 GetMagickToken(q,&q,token);
2389 bounds.height=(size_t) floor(StringToDouble(token,
2390 (char **) NULL)+0.5);
2391 for (p=q; *q != '\0'; )
2393 GetMagickToken(q,&q,token);
2394 if (LocaleCompare(token,"pop") != 0)
2396 GetMagickToken(q,(const char **) NULL,token);
2397 if (LocaleCompare(token,"pattern") != 0)
2401 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2402 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2403 (void) SetImageArtifact(image,key,token);
2404 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2405 (void) FormatLocaleString(geometry,MaxTextExtent,
2406 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2407 bounds.height,(double) bounds.x,(double) bounds.y);
2408 (void) SetImageArtifact(image,key,geometry);
2409 GetMagickToken(q,&q,token);
2412 if (LocaleCompare("graphic-context",token) == 0)
2415 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2416 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2417 if (graphic_context == (DrawInfo **) NULL)
2419 (void) ThrowMagickException(exception,GetMagickModule(),
2420 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2424 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2425 graphic_context[n-1]);
2428 if (LocaleCompare("defs",token) == 0)
2439 if (LocaleCompare("rectangle",keyword) == 0)
2441 primitive_type=RectanglePrimitive;
2444 if (LocaleCompare("rotate",keyword) == 0)
2446 GetMagickToken(q,&q,token);
2447 angle=StringToDouble(token,(char **) NULL);
2448 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2449 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2450 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2451 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2454 if (LocaleCompare("roundRectangle",keyword) == 0)
2456 primitive_type=RoundRectanglePrimitive;
2465 if (LocaleCompare("scale",keyword) == 0)
2467 GetMagickToken(q,&q,token);
2468 affine.sx=StringToDouble(token,(char **) NULL);
2469 GetMagickToken(q,&q,token);
2471 GetMagickToken(q,&q,token);
2472 affine.sy=StringToDouble(token,(char **) NULL);
2475 if (LocaleCompare("skewX",keyword) == 0)
2477 GetMagickToken(q,&q,token);
2478 angle=StringToDouble(token,(char **) NULL);
2479 affine.ry=sin(DegreesToRadians(angle));
2482 if (LocaleCompare("skewY",keyword) == 0)
2484 GetMagickToken(q,&q,token);
2485 angle=StringToDouble(token,(char **) NULL);
2486 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2489 if (LocaleCompare("stop-color",keyword) == 0)
2494 GetMagickToken(q,&q,token);
2495 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2497 (void) GradientImage(image,LinearGradient,ReflectSpread,
2498 &start_color,&stop_color,exception);
2499 start_color=stop_color;
2500 GetMagickToken(q,&q,token);
2503 if (LocaleCompare("stroke",keyword) == 0)
2505 GetMagickToken(q,&q,token);
2506 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2507 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2508 (void) DrawPatternPath(image,draw_info,token,
2509 &graphic_context[n]->stroke_pattern,exception);
2512 status=QueryColorCompliance(token,AllCompliance,
2513 &graphic_context[n]->stroke,exception);
2514 if (status == MagickFalse)
2519 pattern_info=AcquireImageInfo();
2520 (void) CopyMagickString(pattern_info->filename,token,
2522 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2524 CatchException(exception);
2525 pattern_info=DestroyImageInfo(pattern_info);
2530 if (LocaleCompare("stroke-antialias",keyword) == 0)
2532 GetMagickToken(q,&q,token);
2533 graphic_context[n]->stroke_antialias=
2534 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2537 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2539 if (graphic_context[n]->dash_pattern != (double *) NULL)
2540 graphic_context[n]->dash_pattern=(double *)
2541 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2542 if (IsPoint(q) != MagickFalse)
2548 GetMagickToken(p,&p,token);
2550 GetMagickToken(p,&p,token);
2551 for (x=0; IsPoint(token) != MagickFalse; x++)
2553 GetMagickToken(p,&p,token);
2555 GetMagickToken(p,&p,token);
2557 graphic_context[n]->dash_pattern=(double *)
2558 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2559 sizeof(*graphic_context[n]->dash_pattern));
2560 if (graphic_context[n]->dash_pattern == (double *) NULL)
2562 (void) ThrowMagickException(exception,GetMagickModule(),
2563 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2567 for (j=0; j < x; j++)
2569 GetMagickToken(q,&q,token);
2571 GetMagickToken(q,&q,token);
2572 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2575 if ((x & 0x01) != 0)
2576 for ( ; j < (2*x); j++)
2577 graphic_context[n]->dash_pattern[j]=
2578 graphic_context[n]->dash_pattern[j-x];
2579 graphic_context[n]->dash_pattern[j]=0.0;
2582 GetMagickToken(q,&q,token);
2585 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2587 GetMagickToken(q,&q,token);
2588 graphic_context[n]->dash_offset=StringToDouble(token,
2592 if (LocaleCompare("stroke-linecap",keyword) == 0)
2597 GetMagickToken(q,&q,token);
2598 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2602 graphic_context[n]->linecap=(LineCap) linecap;
2605 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2610 GetMagickToken(q,&q,token);
2611 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2616 graphic_context[n]->linejoin=(LineJoin) linejoin;
2619 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2621 GetMagickToken(q,&q,token);
2622 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2625 if (LocaleCompare("stroke-opacity",keyword) == 0)
2627 GetMagickToken(q,&q,token);
2628 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2629 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2630 factor*StringToDouble(token,(char **) NULL);
2633 if (LocaleCompare("stroke-width",keyword) == 0)
2635 GetMagickToken(q,&q,token);
2636 graphic_context[n]->stroke_width=StringToDouble(token,
2646 if (LocaleCompare("text",keyword) == 0)
2648 primitive_type=TextPrimitive;
2651 if (LocaleCompare("text-align",keyword) == 0)
2656 GetMagickToken(q,&q,token);
2657 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2661 graphic_context[n]->align=(AlignType) align;
2664 if (LocaleCompare("text-anchor",keyword) == 0)
2669 GetMagickToken(q,&q,token);
2670 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2674 graphic_context[n]->align=(AlignType) align;
2677 if (LocaleCompare("text-antialias",keyword) == 0)
2679 GetMagickToken(q,&q,token);
2680 graphic_context[n]->text_antialias=
2681 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2684 if (LocaleCompare("text-undercolor",keyword) == 0)
2686 GetMagickToken(q,&q,token);
2687 (void) QueryColorCompliance(token,AllCompliance,
2688 &graphic_context[n]->undercolor,exception);
2691 if (LocaleCompare("translate",keyword) == 0)
2693 GetMagickToken(q,&q,token);
2694 affine.tx=StringToDouble(token,(char **) NULL);
2695 GetMagickToken(q,&q,token);
2697 GetMagickToken(q,&q,token);
2698 affine.ty=StringToDouble(token,(char **) NULL);
2707 if (LocaleCompare("viewbox",keyword) == 0)
2709 GetMagickToken(q,&q,token);
2710 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2711 (char **) NULL)-0.5);
2712 GetMagickToken(q,&q,token);
2714 GetMagickToken(q,&q,token);
2715 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2716 (char **) NULL)-0.5);
2717 GetMagickToken(q,&q,token);
2719 GetMagickToken(q,&q,token);
2720 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2721 token,(char **) NULL)+0.5);
2722 GetMagickToken(q,&q,token);
2724 GetMagickToken(q,&q,token);
2725 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2726 token,(char **) NULL)+0.5);
2738 if (status == MagickFalse)
2740 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2741 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2743 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2744 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2745 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2746 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2747 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2749 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2752 if (primitive_type == UndefinedPrimitive)
2754 if (image->debug != MagickFalse)
2755 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2760 Parse the primitive attributes.
2764 primitive_info[0].point.x=0.0;
2765 primitive_info[0].point.y=0.0;
2766 for (x=0; *q != '\0'; x++)
2771 if (IsPoint(q) == MagickFalse)
2773 GetMagickToken(q,&q,token);
2774 point.x=StringToDouble(token,(char **) NULL);
2775 GetMagickToken(q,&q,token);
2777 GetMagickToken(q,&q,token);
2778 point.y=StringToDouble(token,(char **) NULL);
2779 GetMagickToken(q,(const char **) NULL,token);
2781 GetMagickToken(q,&q,token);
2782 primitive_info[i].primitive=primitive_type;
2783 primitive_info[i].point=point;
2784 primitive_info[i].coordinates=0;
2785 primitive_info[i].method=FloodfillMethod;
2787 if (i < (ssize_t) number_points)
2790 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2791 (size_t) number_points,sizeof(*primitive_info));
2792 if (primitive_info == (PrimitiveInfo *) NULL)
2794 (void) ThrowMagickException(exception,GetMagickModule(),
2795 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
2799 primitive_info[j].primitive=primitive_type;
2800 primitive_info[j].coordinates=(size_t) x;
2801 primitive_info[j].method=FloodfillMethod;
2802 primitive_info[j].text=(char *) NULL;
2804 Circumscribe primitive within a circle.
2806 bounds.x1=primitive_info[j].point.x;
2807 bounds.y1=primitive_info[j].point.y;
2808 bounds.x2=primitive_info[j].point.x;
2809 bounds.y2=primitive_info[j].point.y;
2810 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2812 point=primitive_info[j+k].point;
2813 if (point.x < bounds.x1)
2815 if (point.y < bounds.y1)
2817 if (point.x > bounds.x2)
2819 if (point.y > bounds.y2)
2823 Speculate how many points our primitive might consume.
2825 length=primitive_info[j].coordinates;
2826 switch (primitive_type)
2828 case RectanglePrimitive:
2833 case RoundRectanglePrimitive:
2835 length*=5+8*BezierQuantum;
2838 case BezierPrimitive:
2840 if (primitive_info[j].coordinates > 107)
2841 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2842 "TooManyBezierCoordinates","'%s'",token);
2843 length=BezierQuantum*primitive_info[j].coordinates;
2852 GetMagickToken(q,&q,token);
2855 for (s=token; *s != '\0'; s=t)
2860 value=StringToDouble(s,&t);
2869 length=length*BezierQuantum/2;
2872 case CirclePrimitive:
2874 case EllipsePrimitive:
2881 alpha=bounds.x2-bounds.x1;
2882 beta=bounds.y2-bounds.y1;
2883 radius=hypot((double) alpha,(double) beta);
2884 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2890 if ((size_t) (i+length) >= number_points)
2893 Resize based on speculative points required by primitive.
2895 number_points+=length+1;
2896 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2897 (size_t) number_points,sizeof(*primitive_info));
2898 if (primitive_info == (PrimitiveInfo *) NULL)
2900 (void) ThrowMagickException(exception,GetMagickModule(),
2901 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2906 switch (primitive_type)
2908 case PointPrimitive:
2911 if (primitive_info[j].coordinates != 1)
2916 TracePoint(primitive_info+j,primitive_info[j].point);
2917 i=(ssize_t) (j+primitive_info[j].coordinates);
2922 if (primitive_info[j].coordinates != 2)
2927 TraceLine(primitive_info+j,primitive_info[j].point,
2928 primitive_info[j+1].point);
2929 i=(ssize_t) (j+primitive_info[j].coordinates);
2932 case RectanglePrimitive:
2934 if (primitive_info[j].coordinates != 2)
2939 TraceRectangle(primitive_info+j,primitive_info[j].point,
2940 primitive_info[j+1].point);
2941 i=(ssize_t) (j+primitive_info[j].coordinates);
2944 case RoundRectanglePrimitive:
2946 if (primitive_info[j].coordinates != 3)
2951 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2952 primitive_info[j+1].point,primitive_info[j+2].point);
2953 i=(ssize_t) (j+primitive_info[j].coordinates);
2958 if (primitive_info[j].coordinates != 3)
2960 primitive_type=UndefinedPrimitive;
2963 TraceArc(primitive_info+j,primitive_info[j].point,
2964 primitive_info[j+1].point,primitive_info[j+2].point);
2965 i=(ssize_t) (j+primitive_info[j].coordinates);
2968 case EllipsePrimitive:
2970 if (primitive_info[j].coordinates != 3)
2975 TraceEllipse(primitive_info+j,primitive_info[j].point,
2976 primitive_info[j+1].point,primitive_info[j+2].point);
2977 i=(ssize_t) (j+primitive_info[j].coordinates);
2980 case CirclePrimitive:
2982 if (primitive_info[j].coordinates != 2)
2987 TraceCircle(primitive_info+j,primitive_info[j].point,
2988 primitive_info[j+1].point);
2989 i=(ssize_t) (j+primitive_info[j].coordinates);
2992 case PolylinePrimitive:
2994 case PolygonPrimitive:
2996 primitive_info[i]=primitive_info[j];
2997 primitive_info[i].coordinates=0;
2998 primitive_info[j].coordinates++;
3002 case BezierPrimitive:
3004 if (primitive_info[j].coordinates < 3)
3009 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3010 i=(ssize_t) (j+primitive_info[j].coordinates);
3015 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3018 case ColorPrimitive:
3019 case MattePrimitive:
3024 if (primitive_info[j].coordinates != 1)
3029 GetMagickToken(q,&q,token);
3030 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3034 primitive_info[j].method=(PaintMethod) method;
3039 if (primitive_info[j].coordinates != 1)
3045 GetMagickToken(q,&q,token);
3046 primitive_info[j].text=AcquireString(token);
3049 case ImagePrimitive:
3051 if (primitive_info[j].coordinates != 2)
3056 GetMagickToken(q,&q,token);
3057 primitive_info[j].text=AcquireString(token);
3061 if (primitive_info == (PrimitiveInfo *) NULL)
3063 if (image->debug != MagickFalse)
3064 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3065 if (status == MagickFalse)
3067 primitive_info[i].primitive=UndefinedPrimitive;
3073 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3075 point=primitive_info[i].point;
3076 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3077 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3078 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3079 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3080 point=primitive_info[i].point;
3081 if (point.x < graphic_context[n]->bounds.x1)
3082 graphic_context[n]->bounds.x1=point.x;
3083 if (point.y < graphic_context[n]->bounds.y1)
3084 graphic_context[n]->bounds.y1=point.y;
3085 if (point.x > graphic_context[n]->bounds.x2)
3086 graphic_context[n]->bounds.x2=point.x;
3087 if (point.y > graphic_context[n]->bounds.y2)
3088 graphic_context[n]->bounds.y2=point.y;
3089 if (primitive_info[i].primitive == ImagePrimitive)
3091 if (i >= (ssize_t) number_points)
3092 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3094 if (graphic_context[n]->render != MagickFalse)
3096 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3097 (LocaleCompare(graphic_context[n]->clip_mask,
3098 graphic_context[n-1]->clip_mask) != 0))
3099 (void) DrawClipPath(image,graphic_context[n],
3100 graphic_context[n]->clip_mask,exception);
3101 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3103 if (primitive_info->text != (char *) NULL)
3104 primitive_info->text=(char *) RelinquishMagickMemory(
3105 primitive_info->text);
3106 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3108 if (proceed == MagickFalse)
3111 if (image->debug != MagickFalse)
3112 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3114 Relinquish resources.
3116 token=DestroyString(token);
3117 if (primitive_info != (PrimitiveInfo *) NULL)
3118 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3119 primitive=DestroyString(primitive);
3120 for ( ; n >= 0; n--)
3121 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3122 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3123 if (status == MagickFalse)
3124 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3134 % D r a w G r a d i e n t I m a g e %
3138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3140 % DrawGradientImage() draws a linear gradient on the image.
3142 % The format of the DrawGradientImage method is:
3144 % MagickBooleanType DrawGradientImage(Image *image,
3145 % const DrawInfo *draw_info,ExceptionInfo *exception)
3147 % A description of each parameter follows:
3149 % o image: the image.
3151 % o draw_info: the draw info.
3153 % o exception: return any errors or warnings in this structure.
3157 static inline double GetStopColorOffset(const GradientInfo *gradient,
3158 const ssize_t x,const ssize_t y)
3160 switch (gradient->type)
3162 case UndefinedGradient:
3163 case LinearGradient:
3178 gradient_vector=(&gradient->gradient_vector);
3179 p.x=gradient_vector->x2-gradient_vector->x1;
3180 p.y=gradient_vector->y2-gradient_vector->y1;
3181 q.x=(double) x-gradient_vector->x1;
3182 q.y=(double) y-gradient_vector->y1;
3183 length=sqrt(q.x*q.x+q.y*q.y);
3184 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3185 gamma=PerceptibleReciprocal(gamma);
3186 scale=p.x*q.x+p.y*q.y;
3187 offset=gamma*scale*length;
3190 case RadialGradient:
3199 v.x=(double) x-gradient->center.x;
3200 v.y=(double) y-gradient->center.y;
3201 length=sqrt(v.x*v.x+v.y*v.y);
3202 if (gradient->spread == RepeatSpread)
3204 offset=length/gradient->radius;
3211 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3212 const DrawInfo *draw_info,ExceptionInfo *exception)
3238 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3247 Draw linear or radial gradient on image.
3249 assert(image != (Image *) NULL);
3250 assert(image->signature == MagickSignature);
3251 if (image->debug != MagickFalse)
3252 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3253 assert(draw_info != (const DrawInfo *) NULL);
3254 gradient=(&draw_info->gradient);
3255 gradient_vector=(&gradient->gradient_vector);
3256 point.x=gradient_vector->x2-gradient_vector->x1;
3257 point.y=gradient_vector->y2-gradient_vector->y1;
3258 length=sqrt(point.x*point.x+point.y*point.y);
3259 bounding_box=gradient->bounding_box;
3261 GetPixelInfo(image,&zero);
3262 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3263 height=bounding_box.height-bounding_box.y;
3265 image_view=AcquireAuthenticCacheView(image,exception);
3266 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3267 #pragma omp parallel for schedule(static,4) shared(status) \
3268 magick_threads(image,image,height,1)
3270 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3290 if (status == MagickFalse)
3292 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3293 if (q == (Quantum *) NULL)
3300 offset=GetStopColorOffset(gradient,0,y);
3301 if (gradient->type != RadialGradient)
3303 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3305 GetPixelInfoPixel(image,q,&pixel);
3306 switch (gradient->spread)
3308 case UndefinedSpread:
3311 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3312 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3314 offset=GetStopColorOffset(gradient,x,y);
3315 if (gradient->type != RadialGradient)
3318 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3319 if (offset < gradient->stops[i].offset)
3321 if ((offset < 0.0) || (i == 0))
3322 composite=gradient->stops[0].color;
3324 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3325 composite=gradient->stops[gradient->number_stops-1].color;
3330 alpha=(offset-gradient->stops[i].offset)/
3331 (gradient->stops[j].offset-gradient->stops[i].offset);
3332 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3333 &gradient->stops[j].color,alpha,&composite);
3339 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3340 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3342 offset=GetStopColorOffset(gradient,x,y);
3343 if (gradient->type != RadialGradient)
3348 if ((ssize_t) fmod(offset,2.0) == 0)
3349 offset=fmod(offset,1.0);
3351 offset=1.0-fmod(offset,1.0);
3352 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3353 if (offset < gradient->stops[i].offset)
3356 composite=gradient->stops[0].color;
3358 if (i == (ssize_t) gradient->number_stops)
3359 composite=gradient->stops[gradient->number_stops-1].color;
3364 alpha=(offset-gradient->stops[i].offset)/
3365 (gradient->stops[j].offset-gradient->stops[i].offset);
3366 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3367 &gradient->stops[j].color,alpha,&composite);
3379 antialias=MagickFalse;
3381 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3382 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3384 offset=GetStopColorOffset(gradient,x,y);
3385 if (gradient->type == LinearGradient)
3387 repeat=fmod(offset,length);
3389 repeat=length-fmod(-repeat,length);
3391 repeat=fmod(offset,length);
3392 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3393 MagickTrue : MagickFalse;
3394 offset=repeat/length;
3398 repeat=fmod(offset,gradient->radius);
3400 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3402 repeat=fmod(offset,gradient->radius);
3403 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3405 offset=repeat/gradient->radius;
3408 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3409 if (offset < gradient->stops[i].offset)
3412 composite=gradient->stops[0].color;
3414 if (i == (ssize_t) gradient->number_stops)
3415 composite=gradient->stops[gradient->number_stops-1].color;
3420 alpha=(offset-gradient->stops[i].offset)/
3421 (gradient->stops[j].offset-gradient->stops[i].offset);
3422 if (antialias != MagickFalse)
3424 if (gradient->type == LinearGradient)
3425 alpha=length-repeat;
3427 alpha=gradient->radius-repeat;
3429 j=(ssize_t) gradient->number_stops-1L;
3431 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3432 &gradient->stops[j].color,alpha,&composite);
3437 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3439 SetPixelInfoPixel(image,&pixel,q);
3440 q+=GetPixelChannels(image);
3442 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3445 image_view=DestroyCacheView(image_view);
3450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3454 % D r a w P a t t e r n P a t h %
3458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3460 % DrawPatternPath() draws a pattern.
3462 % The format of the DrawPatternPath method is:
3464 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3465 % const char *name,Image **pattern,ExceptionInfo *exception)
3467 % A description of each parameter follows:
3469 % o image: the image.
3471 % o draw_info: the draw info.
3473 % o name: the pattern name.
3475 % o image: the image.
3477 % o exception: return any errors or warnings in this structure.
3480 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3481 const DrawInfo *draw_info,const char *name,Image **pattern,
3482 ExceptionInfo *exception)
3485 property[MaxTextExtent];
3500 assert(image != (Image *) NULL);
3501 assert(image->signature == MagickSignature);
3502 if (image->debug != MagickFalse)
3503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3504 assert(draw_info != (const DrawInfo *) NULL);
3505 assert(name != (const char *) NULL);
3506 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3507 path=GetImageArtifact(image,property);
3508 if (path == (const char *) NULL)
3509 return(MagickFalse);
3510 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3511 geometry=GetImageArtifact(image,property);
3512 if (geometry == (const char *) NULL)
3513 return(MagickFalse);
3514 if ((*pattern) != (Image *) NULL)
3515 *pattern=DestroyImage(*pattern);
3516 image_info=AcquireImageInfo();
3517 image_info->size=AcquireString(geometry);
3518 *pattern=AcquireImage(image_info,exception);
3519 image_info=DestroyImageInfo(image_info);
3520 (void) QueryColorCompliance("#00000000",AllCompliance,
3521 &(*pattern)->background_color,exception);
3522 (void) SetImageBackgroundColor(*pattern,exception);
3523 if (image->debug != MagickFalse)
3524 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3525 "begin pattern-path %s %s",name,geometry);
3526 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3527 clone_info->fill_pattern=NewImageList();
3528 clone_info->stroke_pattern=NewImageList();
3529 (void) CloneString(&clone_info->primitive,path);
3530 status=DrawImage(*pattern,clone_info,exception);
3531 clone_info=DestroyDrawInfo(clone_info);
3532 if (image->debug != MagickFalse)
3533 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3542 + D r a w P o l y g o n P r i m i t i v e %
3546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3548 % DrawPolygonPrimitive() draws a polygon on the image.
3550 % The format of the DrawPolygonPrimitive method is:
3552 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3553 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3554 % ExceptionInfo *exception)
3556 % A description of each parameter follows:
3558 % o image: the image.
3560 % o draw_info: the draw info.
3562 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3564 % o exception: return any errors or warnings in this structure.
3568 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3573 assert(polygon_info != (PolygonInfo **) NULL);
3574 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3575 if (polygon_info[i] != (PolygonInfo *) NULL)
3576 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3577 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3578 return(polygon_info);
3581 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3582 const PrimitiveInfo *primitive_info)
3585 *restrict path_info;
3596 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3597 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3598 sizeof(*polygon_info));
3599 if (polygon_info == (PolygonInfo **) NULL)
3600 return((PolygonInfo **) NULL);
3601 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3602 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3603 if (path_info == (PathInfo *) NULL)
3604 return(DestroyPolygonThreadSet(polygon_info));
3605 for (i=0; i < (ssize_t) number_threads; i++)
3607 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3608 if (polygon_info[i] == (PolygonInfo *) NULL)
3609 return(DestroyPolygonThreadSet(polygon_info));
3611 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3612 return(polygon_info);
3615 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3616 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3617 const ssize_t y,double *stroke_alpha)
3628 register const PointInfo
3642 Compute fill & stroke opacity for this (x,y) point.
3646 p=polygon_info->edges;
3647 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3649 if ((double) y <= (p->bounds.y1-mid-0.5))
3651 if ((double) y > (p->bounds.y2+mid+0.5))
3653 (void) DestroyEdge(polygon_info,(size_t) j);
3656 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3657 ((double) x > (p->bounds.x2+mid+0.5)))
3659 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3660 for ( ; i < (ssize_t) p->number_points; i++)
3662 if ((double) y <= (p->points[i-1].y-mid-0.5))
3664 if ((double) y > (p->points[i].y+mid+0.5))
3666 if (p->scanline != (double) y)
3668 p->scanline=(double) y;
3669 p->highwater=(size_t) i;
3672 Compute distance between a point and an edge.
3675 delta.x=(q+1)->x-q->x;
3676 delta.y=(q+1)->y-q->y;
3677 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3680 delta.x=(double) x-q->x;
3681 delta.y=(double) y-q->y;
3682 distance=delta.x*delta.x+delta.y*delta.y;
3686 alpha=delta.x*delta.x+delta.y*delta.y;
3689 delta.x=(double) x-(q+1)->x;
3690 delta.y=(double) y-(q+1)->y;
3691 distance=delta.x*delta.x+delta.y*delta.y;
3695 alpha=PerceptibleReciprocal(alpha);
3696 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3697 distance=alpha*beta*beta;
3701 Compute stroke & subpath opacity.
3704 if (p->ghostline == MagickFalse)
3707 if ((*stroke_alpha < 1.0) &&
3708 (distance <= ((alpha+0.25)*(alpha+0.25))))
3711 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3716 if (distance != 1.0)
3717 beta=sqrt((double) distance);
3719 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3720 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3724 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3726 if (distance <= 0.0)
3736 if (distance != 1.0)
3737 beta=sqrt(distance);
3740 if (subpath_alpha < (alpha*alpha))
3741 subpath_alpha=alpha*alpha;
3745 Compute fill opacity.
3747 if (fill == MagickFalse)
3749 if (subpath_alpha >= 1.0)
3752 Determine winding number.
3755 p=polygon_info->edges;
3756 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3758 if ((double) y <= p->bounds.y1)
3760 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3762 if ((double) x > p->bounds.x2)
3764 winding_number+=p->direction ? 1 : -1;
3767 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3768 for ( ; i < (ssize_t) p->number_points; i++)
3769 if ((double) y <= p->points[i].y)
3772 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3773 winding_number+=p->direction ? 1 : -1;
3775 if (fill_rule != NonZeroRule)
3777 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3781 if (MagickAbsoluteValue(winding_number) != 0)
3783 return(subpath_alpha);
3786 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3787 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3788 ExceptionInfo *exception)
3801 **restrict polygon_info;
3812 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3823 Compute bounding box.
3825 assert(image != (Image *) NULL);
3826 assert(image->signature == MagickSignature);
3827 if (image->debug != MagickFalse)
3828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3829 assert(draw_info != (DrawInfo *) NULL);
3830 assert(draw_info->signature == MagickSignature);
3831 assert(primitive_info != (PrimitiveInfo *) NULL);
3832 if (primitive_info->coordinates == 0)
3834 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3835 if (polygon_info == (PolygonInfo **) NULL)
3836 return(MagickFalse);
3838 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3839 if (image->debug != MagickFalse)
3840 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3841 fill=(primitive_info->method == FillToBorderMethod) ||
3842 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3843 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3844 bounds=polygon_info[0]->edges[0].bounds;
3845 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3847 p=polygon_info[0]->edges+i;
3848 if (p->bounds.x1 < bounds.x1)
3849 bounds.x1=p->bounds.x1;
3850 if (p->bounds.y1 < bounds.y1)
3851 bounds.y1=p->bounds.y1;
3852 if (p->bounds.x2 > bounds.x2)
3853 bounds.x2=p->bounds.x2;
3854 if (p->bounds.y2 > bounds.y2)
3855 bounds.y2=p->bounds.y2;
3857 bounds.x1-=(mid+1.0);
3858 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3859 image->columns ? (double) image->columns-1 : bounds.x1;
3860 bounds.y1-=(mid+1.0);
3861 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3862 image->rows ? (double) image->rows-1 : bounds.y1;
3863 bounds.x2+=(mid+1.0);
3864 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3865 image->columns ? (double) image->columns-1 : bounds.x2;
3866 bounds.y2+=(mid+1.0);
3867 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3868 image->rows ? (double) image->rows-1 : bounds.y2;
3870 image_view=AcquireAuthenticCacheView(image,exception);
3871 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3872 height=(size_t) (floor(bounds.y2+0.5)-ceil(bounds.y1-0.5));
3874 if (primitive_info->coordinates == 1)
3879 start=(ssize_t) ceil(bounds.y1-0.5);
3880 stop=(ssize_t) floor(bounds.y2+0.5);
3881 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3882 #pragma omp parallel for schedule(static,4) shared(status) \
3883 magick_threads(image,image,height,1)
3885 for (y=start; y <= stop; y++)
3903 if (status == MagickFalse)
3905 start=(ssize_t) ceil(bounds.x1-0.5);
3906 stop=(ssize_t) floor(bounds.x2+0.5);
3908 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3910 if (q == (Quantum *) NULL)
3915 GetPixelInfo(image,&pixel);
3916 for ( ; x <= stop; x++)
3918 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3919 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3921 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3922 SetPixelInfoPixel(image,&pixel,q);
3924 q+=GetPixelChannels(image);
3926 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3927 if (sync == MagickFalse)
3930 image_view=DestroyCacheView(image_view);
3931 polygon_info=DestroyPolygonThreadSet(polygon_info);
3932 if (image->debug != MagickFalse)
3933 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3934 " end draw-polygon");
3938 Draw polygon or line.
3940 if (image->alpha_trait != BlendPixelTrait)
3941 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3942 start=(ssize_t) ceil(bounds.y1-0.5);
3943 stop=(ssize_t) floor(bounds.y2+0.5);
3944 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3945 #pragma omp parallel for schedule(static,4) shared(status) \
3946 magick_threads(image,image,height,1)
3948 for (y=start; y <= stop; y++)
3951 id = GetOpenMPThreadId();
3971 if (status == MagickFalse)
3973 start=(ssize_t) ceil(bounds.x1-0.5);
3974 stop=(ssize_t) floor(bounds.x2+0.5);
3975 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3977 if (q == (Quantum *) NULL)
3982 for (x=start; x <= stop; x++)
3987 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3989 if (draw_info->stroke_antialias == MagickFalse)
3991 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3992 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3994 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3995 fill_alpha=fill_alpha*fill_color.alpha;
3996 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
3997 GetPixelAlpha(image,q),q);
3998 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3999 stroke_alpha=stroke_alpha*stroke_color.alpha;
4000 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4001 GetPixelAlpha(image,q),q);
4002 q+=GetPixelChannels(image);
4004 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4007 image_view=DestroyCacheView(image_view);
4008 polygon_info=DestroyPolygonThreadSet(polygon_info);
4009 if (image->debug != MagickFalse)
4010 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4019 % D r a w P r i m i t i v e %
4023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4025 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4027 % The format of the DrawPrimitive method is:
4029 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4030 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4032 % A description of each parameter follows:
4034 % o image: the image.
4036 % o draw_info: the draw info.
4038 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4040 % o exception: return any errors or warnings in this structure.
4044 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4070 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4071 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4072 switch (primitive_info->primitive)
4074 case PointPrimitive:
4076 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4077 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4078 methods[primitive_info->method]);
4081 case ColorPrimitive:
4083 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4084 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4085 methods[primitive_info->method]);
4088 case MattePrimitive:
4090 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4091 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4092 methods[primitive_info->method]);
4097 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4098 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4101 case ImagePrimitive:
4103 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4104 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4111 p=primitive_info[0].point;
4114 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4116 point=primitive_info[i].point;
4117 if (coordinates <= 0)
4119 coordinates=(ssize_t) primitive_info[i].coordinates;
4120 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4121 " begin open (%.20g)",(double) coordinates);
4124 point=primitive_info[i].point;
4125 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4126 (fabs(q.y-point.y) >= MagickEpsilon))
4127 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4128 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4130 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4131 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4134 if (coordinates > 0)
4136 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4137 (fabs(p.y-point.y) >= MagickEpsilon))
4138 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4139 (double) coordinates);
4141 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4142 (double) coordinates);
4146 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4147 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4148 ExceptionInfo *exception)
4163 if (image->debug != MagickFalse)
4165 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4166 " begin draw-primitive");
4167 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4168 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4169 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4170 draw_info->affine.tx,draw_info->affine.ty);
4172 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4173 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4174 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4175 (void) SetImageColorspace(image,RGBColorspace,exception);
4177 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4178 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4179 image_view=AcquireAuthenticCacheView(image,exception);
4180 switch (primitive_info->primitive)
4182 case PointPrimitive:
4190 if ((y < 0) || (y >= (ssize_t) image->rows))
4192 if ((x < 0) || (x >= (ssize_t) image->columns))
4194 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4195 if (q == (Quantum *) NULL)
4197 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4198 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4199 (double) GetPixelAlpha(image,q),q);
4200 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4203 case ColorPrimitive:
4205 switch (primitive_info->method)
4216 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4217 if (q == (Quantum *) NULL)
4219 GetPixelInfo(image,&pixel);
4220 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4221 SetPixelInfoPixel(image,&pixel,q);
4222 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4234 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4236 for (y=0; y < (ssize_t) image->rows; y++)
4241 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4243 if (q == (Quantum *) NULL)
4245 for (x=0; x < (ssize_t) image->columns; x++)
4247 GetPixelInfoPixel(image,q,&pixel);
4248 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4250 q+=GetPixelChannels(image);
4253 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4254 SetPixelInfoPixel(image,&pixel,q);
4255 q+=GetPixelChannels(image);
4257 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4258 if (sync == MagickFalse)
4263 case FloodfillMethod:
4264 case FillToBorderMethod:
4269 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4271 if (primitive_info->method == FillToBorderMethod)
4273 target.red=(double) draw_info->border_color.red;
4274 target.green=(double) draw_info->border_color.green;
4275 target.blue=(double) draw_info->border_color.blue;
4277 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4278 primitive_info->method == FloodfillMethod ? MagickFalse :
4279 MagickTrue,exception);
4290 GetPixelInfo(image,&pixel);
4291 for (y=0; y < (ssize_t) image->rows; y++)
4299 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4301 if (q == (Quantum *) NULL)
4303 for (x=0; x < (ssize_t) image->columns; x++)
4305 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4306 SetPixelInfoPixel(image,&pixel,q);
4307 q+=GetPixelChannels(image);
4309 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4310 if (sync == MagickFalse)
4318 case MattePrimitive:
4320 if (image->alpha_trait != BlendPixelTrait)
4321 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4322 switch (primitive_info->method)
4333 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4334 if (q == (Quantum *) NULL)
4336 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4337 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4338 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4350 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4352 for (y=0; y < (ssize_t) image->rows; y++)
4360 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4362 if (q == (Quantum *) NULL)
4364 for (x=0; x < (ssize_t) image->columns; x++)
4366 GetPixelInfoPixel(image,q,&pixel);
4367 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4369 q+=GetPixelChannels(image);
4372 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4373 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4374 q+=GetPixelChannels(image);
4376 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4377 if (sync == MagickFalse)
4382 case FloodfillMethod:
4383 case FillToBorderMethod:
4391 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4393 if (primitive_info->method == FillToBorderMethod)
4395 target.red=(double) draw_info->border_color.red;
4396 target.green=(double) draw_info->border_color.green;
4397 target.blue=(double) draw_info->border_color.blue;
4399 channel_mask=SetImageChannelMask(image,AlphaChannel);
4400 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4401 primitive_info->method == FloodfillMethod ? MagickFalse :
4402 MagickTrue,exception);
4403 (void) SetImageChannelMask(image,channel_mask);
4414 for (y=0; y < (ssize_t) image->rows; y++)
4422 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4424 if (q == (Quantum *) NULL)
4426 for (x=0; x < (ssize_t) image->columns; x++)
4428 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4429 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4430 q+=GetPixelChannels(image);
4432 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4433 if (sync == MagickFalse)
4444 geometry[MaxTextExtent];
4449 if (primitive_info->text == (char *) NULL)
4451 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4452 (void) CloneString(&clone_info->text,primitive_info->text);
4453 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4454 primitive_info->point.x,primitive_info->point.y);
4455 (void) CloneString(&clone_info->geometry,geometry);
4456 status=AnnotateImage(image,clone_info,exception);
4457 clone_info=DestroyDrawInfo(clone_info);
4460 case ImagePrimitive:
4466 composite_geometry[MaxTextExtent];
4481 if (primitive_info->text == (char *) NULL)
4483 clone_info=AcquireImageInfo();
4484 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4485 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4489 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4491 composite_image=ReadImage(clone_info,exception);
4493 clone_info=DestroyImageInfo(clone_info);
4494 if (composite_image == (Image *) NULL)
4496 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4497 NULL,(void *) NULL);
4498 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4499 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4500 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4501 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4504 geometry[MaxTextExtent];
4509 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4510 primitive_info[1].point.x,primitive_info[1].point.y);
4511 composite_image->filter=image->filter;
4512 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4515 if (composite_image->alpha_trait != BlendPixelTrait)
4516 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4518 if (draw_info->alpha != OpaqueAlpha)
4519 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4520 SetGeometry(image,&geometry);
4521 image->gravity=draw_info->gravity;
4524 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4525 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4526 composite_image->rows,(double) geometry.x,(double) geometry.y);
4527 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4528 affine=draw_info->affine;
4529 affine.tx=(double) geometry.x;
4530 affine.ty=(double) geometry.y;
4531 composite_image->interpolate=image->interpolate;
4532 if (draw_info->compose == OverCompositeOp)
4533 (void) DrawAffineImage(image,composite_image,&affine,exception);
4535 (void) CompositeImage(image,composite_image,draw_info->compose,
4536 MagickTrue,geometry.x,geometry.y,exception);
4537 composite_image=DestroyImage(composite_image);
4549 if (IsEventLogging() != MagickFalse)
4550 LogPrimitiveInfo(primitive_info);
4551 scale=ExpandAffine(&draw_info->affine);
4552 if ((draw_info->dash_pattern != (double *) NULL) &&
4553 (draw_info->dash_pattern[0] != 0.0) &&
4554 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4555 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4560 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4561 clone_info->stroke_width=0.0;
4562 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4563 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4565 clone_info=DestroyDrawInfo(clone_info);
4566 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4569 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4571 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4577 Draw strokes while respecting line cap/join attributes.
4579 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4581 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4582 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4583 MagickTrue : MagickFalse;
4584 i=(ssize_t) primitive_info[0].coordinates;
4585 if ((((draw_info->linecap == RoundCap) ||
4586 (closed_path != MagickFalse)) &&
4587 (draw_info->linejoin == RoundJoin)) ||
4588 (primitive_info[i].primitive != UndefinedPrimitive))
4590 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4594 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4595 clone_info->stroke_width=0.0;
4596 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4597 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4599 clone_info=DestroyDrawInfo(clone_info);
4600 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4603 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4607 image_view=DestroyCacheView(image_view);
4608 if (image->debug != MagickFalse)
4609 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4610 return(status != 0 ? MagickTrue : MagickFalse);
4614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4618 + D r a w S t r o k e P o l y g o n %
4622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4624 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4625 % the image while respecting the line cap and join attributes.
4627 % The format of the DrawStrokePolygon method is:
4629 % MagickBooleanType DrawStrokePolygon(Image *image,
4630 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4632 % A description of each parameter follows:
4634 % o image: the image.
4636 % o draw_info: the draw info.
4638 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4643 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4644 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4652 for (i=0; i < 4; i++)
4653 linecap[i]=(*primitive_info);
4654 linecap[0].coordinates=4;
4655 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4656 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4657 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4658 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4659 linecap[4].primitive=UndefinedPrimitive;
4660 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4663 static MagickBooleanType DrawStrokePolygon(Image *image,
4664 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4665 ExceptionInfo *exception)
4677 register const PrimitiveInfo
4682 Draw stroked polygon.
4684 if (image->debug != MagickFalse)
4685 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4686 " begin draw-stroke-polygon");
4687 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4688 clone_info->fill=draw_info->stroke;
4689 if (clone_info->fill_pattern != (Image *) NULL)
4690 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4691 if (clone_info->stroke_pattern != (Image *) NULL)
4692 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4693 MagickTrue,exception);
4694 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4695 clone_info->stroke_width=0.0;
4696 clone_info->fill_rule=NonZeroRule;
4698 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4700 stroke_polygon=TraceStrokePolygon(draw_info,p);
4701 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4702 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4703 q=p+p->coordinates-1;
4704 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4705 MagickTrue : MagickFalse;
4706 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4708 DrawRoundLinecap(image,draw_info,p,exception);
4709 DrawRoundLinecap(image,draw_info,q,exception);
4712 clone_info=DestroyDrawInfo(clone_info);
4713 if (image->debug != MagickFalse)
4714 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4715 " end draw-stroke-polygon");
4720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4724 % G e t A f f i n e M a t r i x %
4728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4730 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4733 % The format of the GetAffineMatrix method is:
4735 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4737 % A description of each parameter follows:
4739 % o affine_matrix: the affine matrix.
4742 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4744 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4745 assert(affine_matrix != (AffineMatrix *) NULL);
4746 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4747 affine_matrix->sx=1.0;
4748 affine_matrix->sy=1.0;
4752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4756 + G e t D r a w I n f o %
4760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4762 % GetDrawInfo() initializes draw_info to default values from image_info.
4764 % The format of the GetDrawInfo method is:
4766 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4768 % A description of each parameter follows:
4770 % o image_info: the image info..
4772 % o draw_info: the draw info.
4775 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4784 Initialize draw attributes.
4786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4787 assert(draw_info != (DrawInfo *) NULL);
4788 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4789 GetAffineMatrix(&draw_info->affine);
4790 exception=AcquireExceptionInfo();
4791 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4793 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4795 draw_info->stroke_width=1.0;
4796 draw_info->alpha=OpaqueAlpha;
4797 draw_info->fill_rule=EvenOddRule;
4798 draw_info->linecap=ButtCap;
4799 draw_info->linejoin=MiterJoin;
4800 draw_info->miterlimit=10;
4801 draw_info->decorate=NoDecoration;
4802 draw_info->pointsize=12.0;
4803 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4804 draw_info->compose=OverCompositeOp;
4805 draw_info->render=MagickTrue;
4806 draw_info->debug=IsEventLogging();
4807 if (image_info != (ImageInfo *) NULL)
4809 draw_info->stroke_antialias=image_info->antialias;
4810 if (image_info->font != (char *) NULL)
4811 draw_info->font=AcquireString(image_info->font);
4812 if (image_info->density != (char *) NULL)
4813 draw_info->density=AcquireString(image_info->density);
4814 draw_info->text_antialias=image_info->antialias;
4815 if (image_info->pointsize != 0.0)
4816 draw_info->pointsize=image_info->pointsize;
4817 draw_info->border_color=image_info->border_color;
4818 if (image_info->server_name != (char *) NULL)
4819 draw_info->server_name=AcquireString(image_info->server_name);
4820 option=GetImageOption(image_info,"encoding");
4821 if (option != (const char *) NULL)
4822 (void) CloneString(&draw_info->encoding,option);
4823 option=GetImageOption(image_info,"kerning");
4824 if (option != (const char *) NULL)
4825 draw_info->kerning=StringToDouble(option,(char **) NULL);
4826 option=GetImageOption(image_info,"interline-spacing");
4827 if (option != (const char *) NULL)
4828 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4829 option=GetImageOption(image_info,"interword-spacing");
4830 if (option != (const char *) NULL)
4831 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4832 option=GetImageOption(image_info,"direction");
4833 if (option != (const char *) NULL)
4834 draw_info->direction=(DirectionType) ParseCommandOption(
4835 MagickDirectionOptions,MagickFalse,option);
4837 draw_info->direction=UndefinedDirection;
4838 option=GetImageOption(image_info,"fill");
4839 if (option != (const char *) NULL)
4840 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4842 option=GetImageOption(image_info,"stroke");
4843 if (option != (const char *) NULL)
4844 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4846 option=GetImageOption(image_info,"strokewidth");
4847 if (option != (const char *) NULL)
4848 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4849 option=GetImageOption(image_info,"undercolor");
4850 if (option != (const char *) NULL)
4851 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4853 option=GetImageOption(image_info,"gravity");
4854 if (option != (const char *) NULL)
4855 draw_info->gravity=(GravityType) ParseCommandOption(
4856 MagickGravityOptions,MagickFalse,option);
4858 exception=DestroyExceptionInfo(exception);
4859 draw_info->signature=MagickSignature;
4863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4867 + P e r m u t a t e %
4871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4873 % Permutate() returns the permuation of the (n,k).
4875 % The format of the Permutate method is:
4877 % void Permutate(ssize_t n,ssize_t k)
4879 % A description of each parameter follows:
4887 static inline double Permutate(const ssize_t n,const ssize_t k)
4896 for (i=k+1; i <= n; i++)
4898 for (i=1; i <= (n-k); i++)
4904 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4908 + T r a c e P r i m i t i v e %
4912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4914 % TracePrimitive is a collection of methods for generating graphic
4915 % primitives such as arcs, ellipses, paths, etc.
4919 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4920 const PointInfo end,const PointInfo degrees)
4926 center.x=0.5*(end.x+start.x);
4927 center.y=0.5*(end.y+start.y);
4928 radii.x=fabs(center.x-start.x);
4929 radii.y=fabs(center.y-start.y);
4930 TraceEllipse(primitive_info,center,radii,degrees);
4933 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4934 const PointInfo end,const PointInfo arc,const double angle,
4935 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4954 register PrimitiveInfo
4963 if ((start.x == end.x) && (start.y == end.y))
4965 TracePoint(primitive_info,end);
4968 radii.x=fabs(arc.x);
4969 radii.y=fabs(arc.y);
4970 if ((radii.x == 0.0) || (radii.y == 0.0))
4972 TraceLine(primitive_info,start,end);
4975 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4976 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4977 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4978 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4979 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4981 if (delta < MagickEpsilon)
4983 TraceLine(primitive_info,start,end);
4988 radii.x*=sqrt((double) delta);
4989 radii.y*=sqrt((double) delta);
4991 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4992 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4993 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4994 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4995 alpha=points[1].x-points[0].x;
4996 beta=points[1].y-points[0].y;
4997 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5002 factor=sqrt((double) factor);
5003 if (sweep == large_arc)
5006 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5007 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5008 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5009 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5010 if ((theta < 0.0) && (sweep != MagickFalse))
5011 theta+=(double) (2.0*MagickPI);
5013 if ((theta > 0.0) && (sweep == MagickFalse))
5014 theta-=(double) (2.0*MagickPI);
5015 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5018 for (i=0; i < (ssize_t) arc_segments; i++)
5020 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5021 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5022 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5023 sin(fmod((double) beta,DegreesToRadians(360.0)));
5024 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5025 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5026 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5027 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5028 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5029 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5030 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5031 theta/arc_segments),DegreesToRadians(360.0))));
5032 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5033 theta/arc_segments),DegreesToRadians(360.0))));
5034 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5035 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5036 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5037 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5038 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5039 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5040 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5042 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5044 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5046 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5048 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5050 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5052 if (i == (ssize_t) (arc_segments-1))
5057 primitive_info->coordinates=(size_t) (p-primitive_info);
5058 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5060 p->primitive=primitive_info->primitive;
5065 static void TraceBezier(PrimitiveInfo *primitive_info,
5066 const size_t number_coordinates)
5078 register PrimitiveInfo
5090 Allocate coeficients.
5092 quantum=number_coordinates;
5093 for (i=0; i < (ssize_t) number_coordinates; i++)
5095 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5097 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5098 if (alpha > (double) quantum)
5099 quantum=(size_t) alpha;
5100 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5101 if (alpha > (double) quantum)
5102 quantum=(size_t) alpha;
5105 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5106 (double) BezierQuantum);
5107 control_points=quantum*number_coordinates;
5108 coefficients=(double *) AcquireQuantumMemory((size_t)
5109 number_coordinates,sizeof(*coefficients));
5110 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5112 if ((coefficients == (double *) NULL) ||
5113 (points == (PointInfo *) NULL))
5114 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5116 Compute bezier points.
5118 end=primitive_info[number_coordinates-1].point;
5119 for (i=0; i < (ssize_t) number_coordinates; i++)
5120 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5122 for (i=0; i < (ssize_t) control_points; i++)
5127 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5128 for (j=0; j < (ssize_t) number_coordinates; j++)
5130 point.x+=alpha*coefficients[j]*p->point.x;
5131 point.y+=alpha*coefficients[j]*p->point.y;
5132 alpha*=weight/(1.0-weight);
5136 weight+=1.0/control_points;
5139 Bezier curves are just short segmented polys.
5142 for (i=0; i < (ssize_t) control_points; i++)
5144 TracePoint(p,points[i]);
5149 primitive_info->coordinates=(size_t) (p-primitive_info);
5150 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5152 p->primitive=primitive_info->primitive;
5155 points=(PointInfo *) RelinquishMagickMemory(points);
5156 coefficients=(double *) RelinquishMagickMemory(coefficients);
5159 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5160 const PointInfo end)
5171 alpha=end.x-start.x;
5173 radius=hypot((double) alpha,(double) beta);
5174 offset.x=(double) radius;
5175 offset.y=(double) radius;
5178 TraceEllipse(primitive_info,start,offset,degrees);
5181 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5182 const PointInfo stop,const PointInfo degrees)
5193 register PrimitiveInfo
5200 Ellipses are just short segmented polys.
5202 if ((stop.x == 0.0) && (stop.y == 0.0))
5204 TracePoint(primitive_info,start);
5207 delta=2.0/MagickMax(stop.x,stop.y);
5208 step=(double) (MagickPI/8.0);
5209 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5210 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5211 angle.x=DegreesToRadians(degrees.x);
5213 while (y < degrees.x)
5215 angle.y=(double) DegreesToRadians(y);
5216 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5218 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5219 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5220 TracePoint(p,point);
5223 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5224 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5225 TracePoint(p,point);
5227 primitive_info->coordinates=(size_t) (p-primitive_info);
5228 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5230 p->primitive=primitive_info->primitive;
5235 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5236 const PointInfo end)
5238 TracePoint(primitive_info,start);
5239 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5240 (fabs(start.y-end.y) < MagickEpsilon))
5242 primitive_info->primitive=PointPrimitive;
5243 primitive_info->coordinates=1;
5246 TracePoint(primitive_info+1,end);
5247 (primitive_info+1)->primitive=primitive_info->primitive;
5248 primitive_info->coordinates=2;
5251 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5254 token[MaxTextExtent];
5276 register PrimitiveInfo
5291 number_coordinates=0;
5293 primitive_type=primitive_info->primitive;
5295 for (p=path; *p != '\0'; )
5297 while (isspace((int) ((unsigned char) *p)) != 0)
5301 last_attribute=attribute;
5302 attribute=(int) (*p++);
5323 GetMagickToken(p,&p,token);
5325 GetMagickToken(p,&p,token);
5326 arc.x=StringToDouble(token,(char **) NULL);
5327 GetMagickToken(p,&p,token);
5329 GetMagickToken(p,&p,token);
5330 arc.y=StringToDouble(token,(char **) NULL);
5331 GetMagickToken(p,&p,token);
5333 GetMagickToken(p,&p,token);
5334 angle=StringToDouble(token,(char **) NULL);
5335 GetMagickToken(p,&p,token);
5337 GetMagickToken(p,&p,token);
5338 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5339 GetMagickToken(p,&p,token);
5341 GetMagickToken(p,&p,token);
5342 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5343 GetMagickToken(p,&p,token);
5345 GetMagickToken(p,&p,token);
5346 x=StringToDouble(token,(char **) NULL);
5347 GetMagickToken(p,&p,token);
5349 GetMagickToken(p,&p,token);
5350 y=StringToDouble(token,(char **) NULL);
5351 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5352 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5353 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5356 while (isspace((int) ((unsigned char) *p)) != 0)
5360 } while (IsPoint(p) != MagickFalse);
5367 Compute bezier points.
5372 for (i=1; i < 4; i++)
5374 GetMagickToken(p,&p,token);
5376 GetMagickToken(p,&p,token);
5377 x=StringToDouble(token,(char **) NULL);
5378 GetMagickToken(p,&p,token);
5380 GetMagickToken(p,&p,token);
5381 y=StringToDouble(token,(char **) NULL);
5382 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5383 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5386 for (i=0; i < 4; i++)
5387 (q+i)->point=points[i];
5391 } while (IsPoint(p) != MagickFalse);
5399 GetMagickToken(p,&p,token);
5401 GetMagickToken(p,&p,token);
5402 x=StringToDouble(token,(char **) NULL);
5403 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5404 TracePoint(q,point);
5406 } while (IsPoint(p) != MagickFalse);
5414 GetMagickToken(p,&p,token);
5416 GetMagickToken(p,&p,token);
5417 x=StringToDouble(token,(char **) NULL);
5418 GetMagickToken(p,&p,token);
5420 GetMagickToken(p,&p,token);
5421 y=StringToDouble(token,(char **) NULL);
5422 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5423 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5424 TracePoint(q,point);
5426 } while (IsPoint(p) != MagickFalse);
5432 if (q != primitive_info)
5434 primitive_info->coordinates=(size_t) (q-primitive_info);
5435 number_coordinates+=primitive_info->coordinates;
5441 GetMagickToken(p,&p,token);
5443 GetMagickToken(p,&p,token);
5444 x=StringToDouble(token,(char **) NULL);
5445 GetMagickToken(p,&p,token);
5447 GetMagickToken(p,&p,token);
5448 y=StringToDouble(token,(char **) NULL);
5449 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5450 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5454 TracePoint(q,point);
5456 if ((i != 0) && (attribute == (int) 'M'))
5458 TracePoint(q,point);
5461 } while (IsPoint(p) != MagickFalse);
5468 Compute bezier points.
5473 for (i=1; i < 3; i++)
5475 GetMagickToken(p,&p,token);
5477 GetMagickToken(p,&p,token);
5478 x=StringToDouble(token,(char **) NULL);
5479 GetMagickToken(p,&p,token);
5481 GetMagickToken(p,&p,token);
5482 y=StringToDouble(token,(char **) NULL);
5485 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5486 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5489 for (i=0; i < 3; i++)
5490 (q+i)->point=points[i];
5494 } while (IsPoint(p) != MagickFalse);
5501 Compute bezier points.
5505 points[0]=points[3];
5506 points[1].x=2.0*points[3].x-points[2].x;
5507 points[1].y=2.0*points[3].y-points[2].y;
5508 for (i=2; i < 4; i++)
5510 GetMagickToken(p,&p,token);
5512 GetMagickToken(p,&p,token);
5513 x=StringToDouble(token,(char **) NULL);
5514 GetMagickToken(p,&p,token);
5516 GetMagickToken(p,&p,token);
5517 y=StringToDouble(token,(char **) NULL);
5520 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5521 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5524 if (strchr("CcSs",last_attribute) == (char *) NULL)
5526 points[0]=points[2];
5527 points[1]=points[3];
5529 for (i=0; i < 4; i++)
5530 (q+i)->point=points[i];
5534 } while (IsPoint(p) != MagickFalse);
5541 Compute bezier points.
5545 points[0]=points[2];
5546 points[1].x=2.0*points[2].x-points[1].x;
5547 points[1].y=2.0*points[2].y-points[1].y;
5548 for (i=2; i < 3; i++)
5550 GetMagickToken(p,&p,token);
5552 GetMagickToken(p,&p,token);
5553 x=StringToDouble(token,(char **) NULL);
5554 GetMagickToken(p,&p,token);
5556 GetMagickToken(p,&p,token);
5557 y=StringToDouble(token,(char **) NULL);
5558 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5559 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5562 if (strchr("QqTt",last_attribute) == (char *) NULL)
5564 points[0]=points[2];
5565 points[1]=points[3];
5567 for (i=0; i < 3; i++)
5568 (q+i)->point=points[i];
5572 } while (IsPoint(p) != MagickFalse);
5580 GetMagickToken(p,&p,token);
5582 GetMagickToken(p,&p,token);
5583 y=StringToDouble(token,(char **) NULL);
5584 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5585 TracePoint(q,point);
5587 } while (IsPoint(p) != MagickFalse);
5594 TracePoint(q,point);
5596 primitive_info->coordinates=(size_t) (q-primitive_info);
5597 number_coordinates+=primitive_info->coordinates;
5604 if (isalpha((int) ((unsigned char) attribute)) != 0)
5605 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5611 primitive_info->coordinates=(size_t) (q-primitive_info);
5612 number_coordinates+=primitive_info->coordinates;
5613 for (i=0; i < (ssize_t) number_coordinates; i++)
5616 q->primitive=primitive_type;
5618 q->method=FillToBorderMethod;
5621 return(number_coordinates);
5624 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5625 const PointInfo end)
5630 register PrimitiveInfo
5637 TracePoint(p,start);
5641 TracePoint(p,point);
5647 TracePoint(p,point);
5649 TracePoint(p,start);
5651 primitive_info->coordinates=(size_t) (p-primitive_info);
5652 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5654 p->primitive=primitive_info->primitive;
5659 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5660 const PointInfo start,const PointInfo end,PointInfo arc)
5667 register PrimitiveInfo
5674 offset.x=fabs(end.x-start.x);
5675 offset.y=fabs(end.y-start.y);
5676 if (arc.x > (0.5*offset.x))
5678 if (arc.y > (0.5*offset.y))
5680 point.x=start.x+offset.x-arc.x;
5681 point.y=start.y+arc.y;
5684 TraceEllipse(p,point,arc,degrees);
5686 point.x=start.x+offset.x-arc.x;
5687 point.y=start.y+offset.y-arc.y;
5690 TraceEllipse(p,point,arc,degrees);
5692 point.x=start.x+arc.x;
5693 point.y=start.y+offset.y-arc.y;
5696 TraceEllipse(p,point,arc,degrees);
5698 point.x=start.x+arc.x;
5699 point.y=start.y+arc.y;
5702 TraceEllipse(p,point,arc,degrees);
5704 TracePoint(p,primitive_info->point);
5706 primitive_info->coordinates=(size_t) (p-primitive_info);
5707 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5709 p->primitive=primitive_info->primitive;
5714 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5715 const size_t number_vertices,const double offset)
5732 for (i=1; i < (ssize_t) number_vertices; i++)
5734 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5735 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5736 if ((fabs((double) dx) >= MagickEpsilon) ||
5737 (fabs((double) dy) >= MagickEpsilon))
5740 if (i == (ssize_t) number_vertices)
5741 i=(ssize_t) number_vertices-1L;
5742 distance=hypot((double) dx,(double) dy);
5743 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5744 dx*(distance+offset)/distance);
5745 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5746 dy*(distance+offset)/distance);
5747 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5749 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5750 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5751 if ((fabs((double) dx) >= MagickEpsilon) ||
5752 (fabs((double) dy) >= MagickEpsilon))
5755 distance=hypot((double) dx,(double) dy);
5756 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5757 dx*(distance+offset)/distance);
5758 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5759 dy*(distance+offset)/distance);
5762 static inline double DrawEpsilonReciprocal(const double x)
5764 #define DrawEpsilon ((double) 1.0e-10)
5766 double sign = x < (double) 0.0 ? (double) -1.0 :
5768 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5769 (double) 1.0/DrawEpsilon));
5772 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5773 const PrimitiveInfo *primitive_info)
5775 typedef struct _LineSegment
5827 number_vertices=primitive_info->coordinates;
5828 max_strokes=2*number_vertices+6*BezierQuantum+360;
5829 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5831 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5833 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5834 number_vertices+2UL,sizeof(*polygon_primitive));
5835 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5836 (polygon_primitive == (PrimitiveInfo *) NULL))
5837 return((PrimitiveInfo *) NULL);
5838 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5839 number_vertices*sizeof(*polygon_primitive));
5841 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5842 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5843 MagickTrue : MagickFalse;
5844 if ((draw_info->linejoin == RoundJoin) ||
5845 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5847 polygon_primitive[number_vertices]=primitive_info[1];
5850 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5852 Compute the slope for the first line segment, p.
5856 for (n=1; n < (ssize_t) number_vertices; n++)
5858 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5859 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5860 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5863 if (n == (ssize_t) number_vertices)
5864 n=(ssize_t) number_vertices-1L;
5865 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5866 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5867 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5868 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5870 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5871 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5872 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5873 offset.y=(double) (offset.x*inverse_slope.p);
5874 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5876 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5877 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5878 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5879 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5880 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5881 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5882 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5883 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5887 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5888 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5889 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5890 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5891 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5892 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5893 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5894 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5897 Create strokes for the line join attribute: bevel, miter, round.
5901 path_q[p++]=box_q[0];
5902 path_p[q++]=box_p[0];
5903 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5906 Compute the slope for this line segment, q.
5908 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5909 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5910 dot_product=dx.q*dx.q+dy.q*dy.q;
5911 if (dot_product < 0.25)
5913 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5914 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5915 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5916 offset.y=(double) (offset.x*inverse_slope.q);
5917 dot_product=dy.q*offset.x-dx.q*offset.y;
5918 if (dot_product > 0.0)
5920 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5921 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5922 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5923 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5924 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5925 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5926 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5927 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5931 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5932 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5933 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5934 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5935 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5936 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5937 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5938 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5940 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5947 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5948 box_p[3].y)/(slope.p-slope.q));
5949 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5950 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5951 box_q[3].y)/(slope.p-slope.q));
5952 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5954 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5956 max_strokes+=6*BezierQuantum+360;
5957 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5959 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5961 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5963 polygon_primitive=(PrimitiveInfo *)
5964 RelinquishMagickMemory(polygon_primitive);
5965 return((PrimitiveInfo *) NULL);
5968 dot_product=dx.q*dy.p-dx.p*dy.q;
5969 if (dot_product <= 0.0)
5970 switch (draw_info->linejoin)
5974 path_q[q++]=box_q[1];
5975 path_q[q++]=box_q[2];
5976 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5977 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5978 if (dot_product <= miterlimit)
5979 path_p[p++]=box_p[4];
5982 path_p[p++]=box_p[1];
5983 path_p[p++]=box_p[2];
5989 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5990 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5991 if (dot_product <= miterlimit)
5993 path_q[q++]=box_q[4];
5994 path_p[p++]=box_p[4];
5998 path_q[q++]=box_q[1];
5999 path_q[q++]=box_q[2];
6000 path_p[p++]=box_p[1];
6001 path_p[p++]=box_p[2];
6007 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6008 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6009 if (dot_product <= miterlimit)
6010 path_p[p++]=box_p[4];
6013 path_p[p++]=box_p[1];
6014 path_p[p++]=box_p[2];
6016 center=polygon_primitive[n].point;
6017 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6018 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6019 if (theta.q < theta.p)
6020 theta.q+=(double) (2.0*MagickPI);
6021 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6022 (2.0*sqrt((double) (1.0/mid)))));
6023 path_q[q].x=box_q[1].x;
6024 path_q[q].y=box_q[1].y;
6026 for (j=1; j < (ssize_t) arc_segments; j++)
6028 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6029 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6030 (theta.p+delta_theta),DegreesToRadians(360.0))));
6031 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6032 (theta.p+delta_theta),DegreesToRadians(360.0))));
6035 path_q[q++]=box_q[2];
6042 switch (draw_info->linejoin)
6046 path_p[p++]=box_p[1];
6047 path_p[p++]=box_p[2];
6048 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6049 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6050 if (dot_product <= miterlimit)
6051 path_q[q++]=box_q[4];
6054 path_q[q++]=box_q[1];
6055 path_q[q++]=box_q[2];
6061 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6062 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6063 if (dot_product <= miterlimit)
6065 path_q[q++]=box_q[4];
6066 path_p[p++]=box_p[4];
6070 path_q[q++]=box_q[1];
6071 path_q[q++]=box_q[2];
6072 path_p[p++]=box_p[1];
6073 path_p[p++]=box_p[2];
6079 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6080 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6081 if (dot_product <= miterlimit)
6082 path_q[q++]=box_q[4];
6085 path_q[q++]=box_q[1];
6086 path_q[q++]=box_q[2];
6088 center=polygon_primitive[n].point;
6089 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6090 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6091 if (theta.p < theta.q)
6092 theta.p+=(double) (2.0*MagickPI);
6093 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6094 (2.0*sqrt((double) (1.0/mid)))));
6095 path_p[p++]=box_p[1];
6096 for (j=1; j < (ssize_t) arc_segments; j++)
6098 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6099 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6100 (theta.p+delta_theta),DegreesToRadians(360.0))));
6101 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6102 (theta.p+delta_theta),DegreesToRadians(360.0))));
6105 path_p[p++]=box_p[2];
6112 inverse_slope.p=inverse_slope.q;
6121 path_p[p++]=box_p[1];
6122 path_q[q++]=box_q[1];
6124 Trace stroked polygon.
6126 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6127 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6128 if (stroke_polygon != (PrimitiveInfo *) NULL)
6130 for (i=0; i < (ssize_t) p; i++)
6132 stroke_polygon[i]=polygon_primitive[0];
6133 stroke_polygon[i].point=path_p[i];
6135 if (closed_path != MagickFalse)
6137 stroke_polygon[i]=polygon_primitive[0];
6138 stroke_polygon[i].point=stroke_polygon[0].point;
6141 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6143 stroke_polygon[i]=polygon_primitive[0];
6144 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6146 if (closed_path != MagickFalse)
6148 stroke_polygon[i]=polygon_primitive[0];
6149 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6152 stroke_polygon[i]=polygon_primitive[0];
6153 stroke_polygon[i].point=stroke_polygon[0].point;
6155 stroke_polygon[i].primitive=UndefinedPrimitive;
6156 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6158 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6159 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6160 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6161 return(stroke_polygon);