2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2012 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)
1120 Determine bounding box.
1122 assert(image != (Image *) NULL);
1123 assert(image->signature == MagickSignature);
1124 if (image->debug != MagickFalse)
1125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1126 assert(source != (const Image *) NULL);
1127 assert(source->signature == MagickSignature);
1128 assert(affine != (AffineMatrix *) NULL);
1131 extent[1].x=(double) source->columns-1.0;
1133 extent[2].x=(double) source->columns-1.0;
1134 extent[2].y=(double) source->rows-1.0;
1136 extent[3].y=(double) source->rows-1.0;
1137 for (i=0; i < 4; i++)
1140 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1141 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1145 for (i=1; i < 4; i++)
1147 if (min.x > extent[i].x)
1149 if (min.y > extent[i].y)
1151 if (max.x < extent[i].x)
1153 if (max.y < extent[i].y)
1157 Affine transform image.
1159 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1160 return(MagickFalse);
1162 edge.x1=MagickMax(min.x,0.0);
1163 edge.y1=MagickMax(min.y,0.0);
1164 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1165 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1166 inverse_affine=InverseAffineMatrix(affine);
1167 GetPixelInfo(image,&zero);
1168 start=(ssize_t) ceil(edge.y1-0.5);
1169 stop=(ssize_t) floor(edge.y2+0.5);
1170 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1171 height=(size_t) (floor(edge.y2+0.5)-ceil(edge.y1-0.5));
1172 width=(size_t) (floor(edge.x2+0.5)-ceil(edge.x1-0.5));
1174 source_view=AcquireVirtualCacheView(source,exception);
1175 image_view=AcquireAuthenticCacheView(image,exception);
1176 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1177 #pragma omp parallel for schedule(static,4) shared(status) \
1178 dynamic_number_threads(image,width,height,1)
1180 for (y=start; y <= stop; y++)
1201 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1202 if (inverse_edge.x2 < inverse_edge.x1)
1204 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1205 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) ceil(
1206 inverse_edge.x1-0.5)),1,exception);
1207 if (q == (Quantum *) NULL)
1212 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1214 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1216 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1218 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1219 point.x,point.y,&pixel,exception);
1220 GetPixelInfoPixel(image,q,&composite);
1221 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1223 SetPixelInfoPixel(image,&composite,q);
1225 q+=GetPixelChannels(image);
1227 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1230 source_view=DestroyCacheView(source_view);
1231 image_view=DestroyCacheView(image_view);
1236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1240 + D r a w B o u n d i n g R e c t a n g l e s %
1244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1246 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1247 % is only useful for developers debugging the rendering algorithm.
1249 % The format of the DrawBoundingRectangles method is:
1251 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1252 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1254 % A description of each parameter follows:
1256 % o image: the image.
1258 % o draw_info: the draw info.
1260 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1262 % o exception: return any errors or warnings in this structure.
1265 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1266 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1291 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1292 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1294 resolution.x=DefaultResolution;
1295 resolution.y=DefaultResolution;
1296 if (clone_info->density != (char *) NULL)
1304 flags=ParseGeometry(clone_info->density,&geometry_info);
1305 resolution.x=geometry_info.rho;
1306 resolution.y=geometry_info.sigma;
1307 if ((flags & SigmaValue) == MagickFalse)
1308 resolution.y=resolution.x;
1310 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1311 clone_info->stroke_width/2.0;
1316 if (polygon_info != (PolygonInfo *) NULL)
1318 bounds=polygon_info->edges[0].bounds;
1319 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1321 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1322 bounds.x1=polygon_info->edges[i].bounds.x1;
1323 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1324 bounds.y1=polygon_info->edges[i].bounds.y1;
1325 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1326 bounds.x2=polygon_info->edges[i].bounds.x2;
1327 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1328 bounds.y2=polygon_info->edges[i].bounds.y2;
1331 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1332 image->columns ? (double) image->columns-1 : bounds.x1;
1334 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1335 image->rows ? (double) image->rows-1 : bounds.y1;
1337 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1338 image->columns ? (double) image->columns-1 : bounds.x2;
1340 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1341 image->rows ? (double) image->rows-1 : bounds.y2;
1342 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1344 if (polygon_info->edges[i].direction != 0)
1345 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1348 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1350 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1351 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1352 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1353 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1354 primitive_info[0].primitive=RectanglePrimitive;
1355 TraceRectangle(primitive_info,start,end);
1356 primitive_info[0].method=ReplaceMethod;
1357 coordinates=(ssize_t) primitive_info[0].coordinates;
1358 primitive_info[coordinates].primitive=UndefinedPrimitive;
1359 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1362 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1364 start.x=(double) (bounds.x1-mid);
1365 start.y=(double) (bounds.y1-mid);
1366 end.x=(double) (bounds.x2+mid);
1367 end.y=(double) (bounds.y2+mid);
1368 primitive_info[0].primitive=RectanglePrimitive;
1369 TraceRectangle(primitive_info,start,end);
1370 primitive_info[0].method=ReplaceMethod;
1371 coordinates=(ssize_t) primitive_info[0].coordinates;
1372 primitive_info[coordinates].primitive=UndefinedPrimitive;
1373 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1374 clone_info=DestroyDrawInfo(clone_info);
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % D r a w C l i p P a t h %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % DrawClipPath() draws the clip path on the image mask.
1390 % The format of the DrawClipPath method is:
1392 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1393 % const char *name,ExceptionInfo *exception)
1395 % A description of each parameter follows:
1397 % o image: the image.
1399 % o draw_info: the draw info.
1401 % o name: the name of the clip path.
1403 % o exception: return any errors or warnings in this structure.
1406 MagickExport MagickBooleanType DrawClipPath(Image *image,
1407 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1410 filename[MaxTextExtent];
1424 assert(image != (Image *) NULL);
1425 assert(image->signature == MagickSignature);
1426 if (image->debug != MagickFalse)
1427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1428 assert(draw_info != (const DrawInfo *) NULL);
1429 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1430 value=GetImageArtifact(image,filename);
1431 if (value == (const char *) NULL)
1432 return(MagickFalse);
1433 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1434 if (clip_mask == (Image *) NULL)
1435 return(MagickFalse);
1436 (void) QueryColorCompliance("#0000",AllCompliance,
1437 &clip_mask->background_color,exception);
1438 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1439 (void) SetImageBackgroundColor(clip_mask,exception);
1440 if (image->debug != MagickFalse)
1441 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1442 draw_info->clip_mask);
1443 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1444 (void) CloneString(&clone_info->primitive,value);
1445 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1447 clone_info->clip_mask=(char *) NULL;
1448 status=NegateImage(clip_mask,MagickFalse,exception);
1449 (void) SetImageMask(image,clip_mask,exception);
1450 clip_mask=DestroyImage(clip_mask);
1451 status=DrawImage(image,clone_info,exception);
1452 clone_info=DestroyDrawInfo(clone_info);
1453 if (image->debug != MagickFalse)
1454 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1455 return(status != 0 ? MagickTrue : MagickFalse);
1459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 + D r a w D a s h P o l y g o n %
1467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1469 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1470 % image while respecting the dash offset and dash pattern attributes.
1472 % The format of the DrawDashPolygon method is:
1474 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1475 % const PrimitiveInfo *primitive_info,Image *image,
1476 % ExceptionInfo *exception)
1478 % A description of each parameter follows:
1480 % o draw_info: the draw info.
1482 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1484 % o image: the image.
1486 % o exception: return any errors or warnings in this structure.
1489 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1490 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1522 assert(draw_info != (const DrawInfo *) NULL);
1523 if (image->debug != MagickFalse)
1524 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1525 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1526 clone_info->miterlimit=0;
1527 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1528 number_vertices=(size_t) i;
1529 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1530 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1531 if (dash_polygon == (PrimitiveInfo *) NULL)
1532 return(MagickFalse);
1533 dash_polygon[0]=primitive_info[0];
1534 scale=ExpandAffine(&draw_info->affine);
1535 length=scale*(draw_info->dash_pattern[0]-0.5);
1536 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1538 for (n=0; offset > 0.0; j=0)
1540 if (draw_info->dash_pattern[n] <= 0.0)
1542 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1543 if (offset > length)
1547 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1550 if (offset < length)
1562 for (i=1; i < (ssize_t) number_vertices; i++)
1564 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1565 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1566 maximum_length=hypot((double) dx,dy);
1570 if (draw_info->dash_pattern[n] == 0.0)
1572 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1574 for (total_length=0.0; (total_length+length) <= maximum_length; )
1576 total_length+=length;
1577 if ((n & 0x01) != 0)
1579 dash_polygon[0]=primitive_info[0];
1580 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1581 total_length/maximum_length);
1582 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1583 total_length/maximum_length);
1588 if ((j+1) > (ssize_t) (2*number_vertices))
1590 dash_polygon[j]=primitive_info[i-1];
1591 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1592 total_length/maximum_length);
1593 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1594 total_length/maximum_length);
1595 dash_polygon[j].coordinates=1;
1597 dash_polygon[0].coordinates=(size_t) j;
1598 dash_polygon[j].primitive=UndefinedPrimitive;
1599 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1602 if (draw_info->dash_pattern[n] == 0.0)
1604 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1606 length-=(maximum_length-total_length);
1607 if ((n & 0x01) != 0)
1609 dash_polygon[j]=primitive_info[i];
1610 dash_polygon[j].coordinates=1;
1613 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1615 dash_polygon[j]=primitive_info[i-1];
1616 dash_polygon[j].point.x+=MagickEpsilon;
1617 dash_polygon[j].point.y+=MagickEpsilon;
1618 dash_polygon[j].coordinates=1;
1620 dash_polygon[0].coordinates=(size_t) j;
1621 dash_polygon[j].primitive=UndefinedPrimitive;
1622 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1624 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1625 clone_info=DestroyDrawInfo(clone_info);
1626 if (image->debug != MagickFalse)
1627 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1628 return(status != 0 ? MagickTrue : MagickFalse);
1632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636 % D r a w I m a g e %
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1642 % DrawImage() draws a graphic primitive on your image. The primitive
1643 % may be represented as a string or filename. Precede the filename with an
1644 % "at" sign (@) and the contents of the file are drawn on the image. You
1645 % can affect how text is drawn by setting one or more members of the draw
1648 % The format of the DrawImage method is:
1650 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1651 % ExceptionInfo *exception)
1653 % A description of each parameter follows:
1655 % o image: the image.
1657 % o draw_info: the draw info.
1659 % o exception: return any errors or warnings in this structure.
1663 static inline MagickBooleanType IsPoint(const char *point)
1671 value=StringToDouble(point,&p);
1672 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1675 static inline void TracePoint(PrimitiveInfo *primitive_info,
1676 const PointInfo point)
1678 primitive_info->coordinates=1;
1679 primitive_info->point=point;
1682 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1683 ExceptionInfo *exception)
1685 #define RenderImageTag "Render/Image"
1692 key[2*MaxTextExtent],
1693 keyword[MaxTextExtent],
1694 geometry[MaxTextExtent],
1695 name[MaxTextExtent],
1696 pattern[MaxTextExtent],
1747 Ensure the annotation info is valid.
1749 assert(image != (Image *) NULL);
1750 assert(image->signature == MagickSignature);
1751 if (image->debug != MagickFalse)
1752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1753 assert(draw_info != (DrawInfo *) NULL);
1754 assert(draw_info->signature == MagickSignature);
1755 if (image->debug != MagickFalse)
1756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1757 if ((draw_info->primitive == (char *) NULL) ||
1758 (*draw_info->primitive == '\0'))
1759 return(MagickFalse);
1760 if (image->debug != MagickFalse)
1761 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1762 if (*draw_info->primitive != '@')
1763 primitive=AcquireString(draw_info->primitive);
1765 primitive=FileToString(draw_info->primitive+1,~0,exception);
1766 if (primitive == (char *) NULL)
1767 return(MagickFalse);
1768 primitive_extent=(double) strlen(primitive);
1769 (void) SetImageArtifact(image,"MVG",primitive);
1772 Allocate primitive info memory.
1774 graphic_context=(DrawInfo **) AcquireMagickMemory(
1775 sizeof(*graphic_context));
1776 if (graphic_context == (DrawInfo **) NULL)
1778 primitive=DestroyString(primitive);
1779 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1783 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1784 sizeof(*primitive_info));
1785 if (primitive_info == (PrimitiveInfo *) NULL)
1787 primitive=DestroyString(primitive);
1788 for ( ; n >= 0; n--)
1789 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1790 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1791 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1794 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1795 graphic_context[n]->viewbox=image->page;
1796 if ((image->page.width == 0) || (image->page.height == 0))
1798 graphic_context[n]->viewbox.width=image->columns;
1799 graphic_context[n]->viewbox.height=image->rows;
1801 token=AcquireString(primitive);
1802 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1804 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1805 return(MagickFalse);
1807 for (q=primitive; *q != '\0'; )
1810 Interpret graphic primitive.
1812 GetMagickToken(q,&q,keyword);
1813 if (*keyword == '\0')
1815 if (*keyword == '#')
1820 while ((*q != '\n') && (*q != '\0'))
1824 p=q-strlen(keyword)-1;
1825 primitive_type=UndefinedPrimitive;
1826 current=graphic_context[n]->affine;
1827 GetAffineMatrix(&affine);
1835 if (LocaleCompare("affine",keyword) == 0)
1837 GetMagickToken(q,&q,token);
1838 affine.sx=StringToDouble(token,(char **) NULL);
1839 GetMagickToken(q,&q,token);
1841 GetMagickToken(q,&q,token);
1842 affine.rx=StringToDouble(token,(char **) NULL);
1843 GetMagickToken(q,&q,token);
1845 GetMagickToken(q,&q,token);
1846 affine.ry=StringToDouble(token,(char **) NULL);
1847 GetMagickToken(q,&q,token);
1849 GetMagickToken(q,&q,token);
1850 affine.sy=StringToDouble(token,(char **) NULL);
1851 GetMagickToken(q,&q,token);
1853 GetMagickToken(q,&q,token);
1854 affine.tx=StringToDouble(token,(char **) NULL);
1855 GetMagickToken(q,&q,token);
1857 GetMagickToken(q,&q,token);
1858 affine.ty=StringToDouble(token,(char **) NULL);
1861 if (LocaleCompare("arc",keyword) == 0)
1863 primitive_type=ArcPrimitive;
1872 if (LocaleCompare("bezier",keyword) == 0)
1874 primitive_type=BezierPrimitive;
1877 if (LocaleCompare("border-color",keyword) == 0)
1879 GetMagickToken(q,&q,token);
1880 (void) QueryColorCompliance(token,AllCompliance,
1881 &graphic_context[n]->border_color,exception);
1890 if (LocaleCompare("clip-path",keyword) == 0)
1895 GetMagickToken(q,&q,token);
1896 (void) CloneString(&graphic_context[n]->clip_mask,token);
1897 (void) DrawClipPath(image,graphic_context[n],
1898 graphic_context[n]->clip_mask,exception);
1901 if (LocaleCompare("clip-rule",keyword) == 0)
1906 GetMagickToken(q,&q,token);
1907 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1909 if (fill_rule == -1)
1912 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1915 if (LocaleCompare("clip-units",keyword) == 0)
1920 GetMagickToken(q,&q,token);
1921 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1923 if (clip_units == -1)
1928 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1929 if (clip_units == ObjectBoundingBox)
1931 GetAffineMatrix(¤t);
1932 affine.sx=draw_info->bounds.x2;
1933 affine.sy=draw_info->bounds.y2;
1934 affine.tx=draw_info->bounds.x1;
1935 affine.ty=draw_info->bounds.y1;
1940 if (LocaleCompare("circle",keyword) == 0)
1942 primitive_type=CirclePrimitive;
1945 if (LocaleCompare("color",keyword) == 0)
1947 primitive_type=ColorPrimitive;
1956 if (LocaleCompare("decorate",keyword) == 0)
1961 GetMagickToken(q,&q,token);
1962 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1967 graphic_context[n]->decorate=(DecorationType) decorate;
1976 if (LocaleCompare("ellipse",keyword) == 0)
1978 primitive_type=EllipsePrimitive;
1981 if (LocaleCompare("encoding",keyword) == 0)
1983 GetMagickToken(q,&q,token);
1984 (void) CloneString(&graphic_context[n]->encoding,token);
1993 if (LocaleCompare("fill",keyword) == 0)
1995 GetMagickToken(q,&q,token);
1996 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1997 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1998 (void) DrawPatternPath(image,draw_info,token,
1999 &graphic_context[n]->fill_pattern,exception);
2002 status=QueryColorCompliance(token,AllCompliance,
2003 &graphic_context[n]->fill,exception);
2004 if (status == MagickFalse)
2009 pattern_info=AcquireImageInfo();
2010 (void) CopyMagickString(pattern_info->filename,token,
2012 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2014 CatchException(exception);
2015 pattern_info=DestroyImageInfo(pattern_info);
2020 if (LocaleCompare("fill-alpha",keyword) == 0)
2022 GetMagickToken(q,&q,token);
2023 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2024 graphic_context[n]->fill.alpha=(double) QuantumRange*
2025 factor*StringToDouble(token,(char **) NULL);
2028 if (LocaleCompare("fill-rule",keyword) == 0)
2033 GetMagickToken(q,&q,token);
2034 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2036 if (fill_rule == -1)
2039 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2042 if (LocaleCompare("font",keyword) == 0)
2044 GetMagickToken(q,&q,token);
2045 (void) CloneString(&graphic_context[n]->font,token);
2046 if (LocaleCompare("none",token) == 0)
2047 graphic_context[n]->font=(char *)
2048 RelinquishMagickMemory(graphic_context[n]->font);
2051 if (LocaleCompare("font-family",keyword) == 0)
2053 GetMagickToken(q,&q,token);
2054 (void) CloneString(&graphic_context[n]->family,token);
2057 if (LocaleCompare("font-size",keyword) == 0)
2059 GetMagickToken(q,&q,token);
2060 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2063 if (LocaleCompare("font-stretch",keyword) == 0)
2068 GetMagickToken(q,&q,token);
2069 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2073 graphic_context[n]->stretch=(StretchType) stretch;
2076 if (LocaleCompare("font-style",keyword) == 0)
2081 GetMagickToken(q,&q,token);
2082 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2086 graphic_context[n]->style=(StyleType) style;
2089 if (LocaleCompare("font-weight",keyword) == 0)
2091 GetMagickToken(q,&q,token);
2092 graphic_context[n]->weight=StringToUnsignedLong(token);
2093 if (LocaleCompare(token,"all") == 0)
2094 graphic_context[n]->weight=0;
2095 if (LocaleCompare(token,"bold") == 0)
2096 graphic_context[n]->weight=700;
2097 if (LocaleCompare(token,"bolder") == 0)
2098 if (graphic_context[n]->weight <= 800)
2099 graphic_context[n]->weight+=100;
2100 if (LocaleCompare(token,"lighter") == 0)
2101 if (graphic_context[n]->weight >= 100)
2102 graphic_context[n]->weight-=100;
2103 if (LocaleCompare(token,"normal") == 0)
2104 graphic_context[n]->weight=400;
2113 if (LocaleCompare("gradient-units",keyword) == 0)
2115 GetMagickToken(q,&q,token);
2118 if (LocaleCompare("gravity",keyword) == 0)
2123 GetMagickToken(q,&q,token);
2124 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2128 graphic_context[n]->gravity=(GravityType) gravity;
2137 if (LocaleCompare("image",keyword) == 0)
2142 primitive_type=ImagePrimitive;
2143 GetMagickToken(q,&q,token);
2144 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2148 graphic_context[n]->compose=(CompositeOperator) compose;
2151 if (LocaleCompare("interline-spacing",keyword) == 0)
2153 GetMagickToken(q,&q,token);
2154 graphic_context[n]->interline_spacing=StringToDouble(token,
2158 if (LocaleCompare("interword-spacing",keyword) == 0)
2160 GetMagickToken(q,&q,token);
2161 graphic_context[n]->interword_spacing=StringToDouble(token,
2171 if (LocaleCompare("kerning",keyword) == 0)
2173 GetMagickToken(q,&q,token);
2174 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2183 if (LocaleCompare("line",keyword) == 0)
2184 primitive_type=LinePrimitive;
2192 if (LocaleCompare("matte",keyword) == 0)
2193 primitive_type=MattePrimitive;
2201 if (LocaleCompare("offset",keyword) == 0)
2203 GetMagickToken(q,&q,token);
2206 if (LocaleCompare("opacity",keyword) == 0)
2208 GetMagickToken(q,&q,token);
2209 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2210 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2211 QuantumScale*graphic_context[n]->alpha)*factor*
2212 StringToDouble(token,(char **) NULL))));
2213 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2214 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2223 if (LocaleCompare("path",keyword) == 0)
2225 primitive_type=PathPrimitive;
2228 if (LocaleCompare("point",keyword) == 0)
2230 primitive_type=PointPrimitive;
2233 if (LocaleCompare("polyline",keyword) == 0)
2235 primitive_type=PolylinePrimitive;
2238 if (LocaleCompare("polygon",keyword) == 0)
2240 primitive_type=PolygonPrimitive;
2243 if (LocaleCompare("pop",keyword) == 0)
2245 GetMagickToken(q,&q,token);
2246 if (LocaleCompare("clip-path",token) == 0)
2248 if (LocaleCompare("defs",token) == 0)
2250 if (LocaleCompare("gradient",token) == 0)
2252 if (LocaleCompare("graphic-context",token) == 0)
2256 (void) ThrowMagickException(exception,GetMagickModule(),
2257 DrawError,"UnbalancedGraphicContextPushPop","'%s'",token);
2261 if (graphic_context[n]->clip_mask != (char *) NULL)
2262 if (LocaleCompare(graphic_context[n]->clip_mask,
2263 graphic_context[n-1]->clip_mask) != 0)
2264 (void) SetImageMask(image,(Image *) NULL,exception);
2265 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2269 if (LocaleCompare("pattern",token) == 0)
2274 if (LocaleCompare("push",keyword) == 0)
2276 GetMagickToken(q,&q,token);
2277 if (LocaleCompare("clip-path",token) == 0)
2280 name[MaxTextExtent];
2282 GetMagickToken(q,&q,token);
2283 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2284 for (p=q; *q != '\0'; )
2286 GetMagickToken(q,&q,token);
2287 if (LocaleCompare(token,"pop") != 0)
2289 GetMagickToken(q,(const char **) NULL,token);
2290 if (LocaleCompare(token,"clip-path") != 0)
2294 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2295 (void) SetImageArtifact(image,name,token);
2296 GetMagickToken(q,&q,token);
2299 if (LocaleCompare("gradient",token) == 0)
2302 key[2*MaxTextExtent],
2303 name[MaxTextExtent],
2304 type[MaxTextExtent];
2309 GetMagickToken(q,&q,token);
2310 (void) CopyMagickString(name,token,MaxTextExtent);
2311 GetMagickToken(q,&q,token);
2312 (void) CopyMagickString(type,token,MaxTextExtent);
2313 GetMagickToken(q,&q,token);
2314 segment.x1=StringToDouble(token,(char **) NULL);
2315 GetMagickToken(q,&q,token);
2317 GetMagickToken(q,&q,token);
2318 segment.y1=StringToDouble(token,(char **) NULL);
2319 GetMagickToken(q,&q,token);
2321 GetMagickToken(q,&q,token);
2322 segment.x2=StringToDouble(token,(char **) NULL);
2323 GetMagickToken(q,&q,token);
2325 GetMagickToken(q,&q,token);
2326 segment.y2=StringToDouble(token,(char **) NULL);
2327 if (LocaleCompare(type,"radial") == 0)
2329 GetMagickToken(q,&q,token);
2331 GetMagickToken(q,&q,token);
2333 for (p=q; *q != '\0'; )
2335 GetMagickToken(q,&q,token);
2336 if (LocaleCompare(token,"pop") != 0)
2338 GetMagickToken(q,(const char **) NULL,token);
2339 if (LocaleCompare(token,"gradient") != 0)
2343 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2344 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2345 graphic_context[n]->affine.ry*segment.y1+
2346 graphic_context[n]->affine.tx;
2347 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2348 graphic_context[n]->affine.sy*segment.y1+
2349 graphic_context[n]->affine.ty;
2350 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2351 graphic_context[n]->affine.ry*segment.y2+
2352 graphic_context[n]->affine.tx;
2353 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2354 graphic_context[n]->affine.sy*segment.y2+
2355 graphic_context[n]->affine.ty;
2356 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2357 (void) SetImageArtifact(image,key,token);
2358 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2359 (void) FormatLocaleString(geometry,MaxTextExtent,
2360 "%gx%g%+.15g%+.15g",
2361 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2362 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2363 bounds.x1,bounds.y1);
2364 (void) SetImageArtifact(image,key,geometry);
2365 GetMagickToken(q,&q,token);
2368 if (LocaleCompare("pattern",token) == 0)
2373 GetMagickToken(q,&q,token);
2374 (void) CopyMagickString(name,token,MaxTextExtent);
2375 GetMagickToken(q,&q,token);
2376 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2378 GetMagickToken(q,&q,token);
2380 GetMagickToken(q,&q,token);
2381 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2383 GetMagickToken(q,&q,token);
2385 GetMagickToken(q,&q,token);
2386 bounds.width=(size_t) floor(StringToDouble(token,
2387 (char **) NULL)+0.5);
2388 GetMagickToken(q,&q,token);
2390 GetMagickToken(q,&q,token);
2391 bounds.height=(size_t) floor(StringToDouble(token,
2392 (char **) NULL)+0.5);
2393 for (p=q; *q != '\0'; )
2395 GetMagickToken(q,&q,token);
2396 if (LocaleCompare(token,"pop") != 0)
2398 GetMagickToken(q,(const char **) NULL,token);
2399 if (LocaleCompare(token,"pattern") != 0)
2403 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2404 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2405 (void) SetImageArtifact(image,key,token);
2406 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2407 (void) FormatLocaleString(geometry,MaxTextExtent,
2408 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2409 bounds.height,(double) bounds.x,(double) bounds.y);
2410 (void) SetImageArtifact(image,key,geometry);
2411 GetMagickToken(q,&q,token);
2414 if (LocaleCompare("graphic-context",token) == 0)
2417 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2418 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2419 if (graphic_context == (DrawInfo **) NULL)
2421 (void) ThrowMagickException(exception,GetMagickModule(),
2422 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2426 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2427 graphic_context[n-1]);
2430 if (LocaleCompare("defs",token) == 0)
2441 if (LocaleCompare("rectangle",keyword) == 0)
2443 primitive_type=RectanglePrimitive;
2446 if (LocaleCompare("rotate",keyword) == 0)
2448 GetMagickToken(q,&q,token);
2449 angle=StringToDouble(token,(char **) NULL);
2450 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2451 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2452 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2453 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2456 if (LocaleCompare("roundRectangle",keyword) == 0)
2458 primitive_type=RoundRectanglePrimitive;
2467 if (LocaleCompare("scale",keyword) == 0)
2469 GetMagickToken(q,&q,token);
2470 affine.sx=StringToDouble(token,(char **) NULL);
2471 GetMagickToken(q,&q,token);
2473 GetMagickToken(q,&q,token);
2474 affine.sy=StringToDouble(token,(char **) NULL);
2477 if (LocaleCompare("skewX",keyword) == 0)
2479 GetMagickToken(q,&q,token);
2480 angle=StringToDouble(token,(char **) NULL);
2481 affine.ry=sin(DegreesToRadians(angle));
2484 if (LocaleCompare("skewY",keyword) == 0)
2486 GetMagickToken(q,&q,token);
2487 angle=StringToDouble(token,(char **) NULL);
2488 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2491 if (LocaleCompare("stop-color",keyword) == 0)
2496 GetMagickToken(q,&q,token);
2497 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2499 (void) GradientImage(image,LinearGradient,ReflectSpread,
2500 &start_color,&stop_color,exception);
2501 start_color=stop_color;
2502 GetMagickToken(q,&q,token);
2505 if (LocaleCompare("stroke",keyword) == 0)
2507 GetMagickToken(q,&q,token);
2508 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2509 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2510 (void) DrawPatternPath(image,draw_info,token,
2511 &graphic_context[n]->stroke_pattern,exception);
2514 status=QueryColorCompliance(token,AllCompliance,
2515 &graphic_context[n]->stroke,exception);
2516 if (status == MagickFalse)
2521 pattern_info=AcquireImageInfo();
2522 (void) CopyMagickString(pattern_info->filename,token,
2524 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2526 CatchException(exception);
2527 pattern_info=DestroyImageInfo(pattern_info);
2532 if (LocaleCompare("stroke-antialias",keyword) == 0)
2534 GetMagickToken(q,&q,token);
2535 graphic_context[n]->stroke_antialias=
2536 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2539 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2541 if (graphic_context[n]->dash_pattern != (double *) NULL)
2542 graphic_context[n]->dash_pattern=(double *)
2543 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2544 if (IsPoint(q) != MagickFalse)
2550 GetMagickToken(p,&p,token);
2552 GetMagickToken(p,&p,token);
2553 for (x=0; IsPoint(token) != MagickFalse; x++)
2555 GetMagickToken(p,&p,token);
2557 GetMagickToken(p,&p,token);
2559 graphic_context[n]->dash_pattern=(double *)
2560 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2561 sizeof(*graphic_context[n]->dash_pattern));
2562 if (graphic_context[n]->dash_pattern == (double *) NULL)
2564 (void) ThrowMagickException(exception,GetMagickModule(),
2565 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2569 for (j=0; j < x; j++)
2571 GetMagickToken(q,&q,token);
2573 GetMagickToken(q,&q,token);
2574 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2577 if ((x & 0x01) != 0)
2578 for ( ; j < (2*x); j++)
2579 graphic_context[n]->dash_pattern[j]=
2580 graphic_context[n]->dash_pattern[j-x];
2581 graphic_context[n]->dash_pattern[j]=0.0;
2584 GetMagickToken(q,&q,token);
2587 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2589 GetMagickToken(q,&q,token);
2590 graphic_context[n]->dash_offset=StringToDouble(token,
2594 if (LocaleCompare("stroke-linecap",keyword) == 0)
2599 GetMagickToken(q,&q,token);
2600 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2604 graphic_context[n]->linecap=(LineCap) linecap;
2607 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2612 GetMagickToken(q,&q,token);
2613 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2618 graphic_context[n]->linejoin=(LineJoin) linejoin;
2621 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2623 GetMagickToken(q,&q,token);
2624 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2627 if (LocaleCompare("stroke-opacity",keyword) == 0)
2629 GetMagickToken(q,&q,token);
2630 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2631 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2632 factor*StringToDouble(token,(char **) NULL);
2635 if (LocaleCompare("stroke-width",keyword) == 0)
2637 GetMagickToken(q,&q,token);
2638 graphic_context[n]->stroke_width=StringToDouble(token,
2648 if (LocaleCompare("text",keyword) == 0)
2650 primitive_type=TextPrimitive;
2653 if (LocaleCompare("text-align",keyword) == 0)
2658 GetMagickToken(q,&q,token);
2659 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2663 graphic_context[n]->align=(AlignType) align;
2666 if (LocaleCompare("text-anchor",keyword) == 0)
2671 GetMagickToken(q,&q,token);
2672 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2676 graphic_context[n]->align=(AlignType) align;
2679 if (LocaleCompare("text-antialias",keyword) == 0)
2681 GetMagickToken(q,&q,token);
2682 graphic_context[n]->text_antialias=
2683 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2686 if (LocaleCompare("text-undercolor",keyword) == 0)
2688 GetMagickToken(q,&q,token);
2689 (void) QueryColorCompliance(token,AllCompliance,
2690 &graphic_context[n]->undercolor,exception);
2693 if (LocaleCompare("translate",keyword) == 0)
2695 GetMagickToken(q,&q,token);
2696 affine.tx=StringToDouble(token,(char **) NULL);
2697 GetMagickToken(q,&q,token);
2699 GetMagickToken(q,&q,token);
2700 affine.ty=StringToDouble(token,(char **) NULL);
2709 if (LocaleCompare("viewbox",keyword) == 0)
2711 GetMagickToken(q,&q,token);
2712 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2713 (char **) NULL)-0.5);
2714 GetMagickToken(q,&q,token);
2716 GetMagickToken(q,&q,token);
2717 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2718 (char **) NULL)-0.5);
2719 GetMagickToken(q,&q,token);
2721 GetMagickToken(q,&q,token);
2722 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2723 token,(char **) NULL)+0.5);
2724 GetMagickToken(q,&q,token);
2726 GetMagickToken(q,&q,token);
2727 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2728 token,(char **) NULL)+0.5);
2740 if (status == MagickFalse)
2742 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2743 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2745 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2746 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2747 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2748 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2749 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2751 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2754 if (primitive_type == UndefinedPrimitive)
2756 if (image->debug != MagickFalse)
2757 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2762 Parse the primitive attributes.
2766 primitive_info[0].point.x=0.0;
2767 primitive_info[0].point.y=0.0;
2768 for (x=0; *q != '\0'; x++)
2773 if (IsPoint(q) == MagickFalse)
2775 GetMagickToken(q,&q,token);
2776 point.x=StringToDouble(token,(char **) NULL);
2777 GetMagickToken(q,&q,token);
2779 GetMagickToken(q,&q,token);
2780 point.y=StringToDouble(token,(char **) NULL);
2781 GetMagickToken(q,(const char **) NULL,token);
2783 GetMagickToken(q,&q,token);
2784 primitive_info[i].primitive=primitive_type;
2785 primitive_info[i].point=point;
2786 primitive_info[i].coordinates=0;
2787 primitive_info[i].method=FloodfillMethod;
2789 if (i < (ssize_t) number_points)
2792 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2793 (size_t) number_points,sizeof(*primitive_info));
2794 if (primitive_info == (PrimitiveInfo *) NULL)
2796 (void) ThrowMagickException(exception,GetMagickModule(),
2797 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
2801 primitive_info[j].primitive=primitive_type;
2802 primitive_info[j].coordinates=(size_t) x;
2803 primitive_info[j].method=FloodfillMethod;
2804 primitive_info[j].text=(char *) NULL;
2806 Circumscribe primitive within a circle.
2808 bounds.x1=primitive_info[j].point.x;
2809 bounds.y1=primitive_info[j].point.y;
2810 bounds.x2=primitive_info[j].point.x;
2811 bounds.y2=primitive_info[j].point.y;
2812 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2814 point=primitive_info[j+k].point;
2815 if (point.x < bounds.x1)
2817 if (point.y < bounds.y1)
2819 if (point.x > bounds.x2)
2821 if (point.y > bounds.y2)
2825 Speculate how many points our primitive might consume.
2827 length=primitive_info[j].coordinates;
2828 switch (primitive_type)
2830 case RectanglePrimitive:
2835 case RoundRectanglePrimitive:
2837 length*=5+8*BezierQuantum;
2840 case BezierPrimitive:
2842 if (primitive_info[j].coordinates > 107)
2843 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2844 "TooManyBezierCoordinates","'%s'",token);
2845 length=BezierQuantum*primitive_info[j].coordinates;
2854 GetMagickToken(q,&q,token);
2857 for (s=token; *s != '\0'; s=t)
2862 value=StringToDouble(s,&t);
2871 length=length*BezierQuantum/2;
2874 case CirclePrimitive:
2876 case EllipsePrimitive:
2883 alpha=bounds.x2-bounds.x1;
2884 beta=bounds.y2-bounds.y1;
2885 radius=hypot((double) alpha,(double) beta);
2886 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2892 if ((size_t) (i+length) >= number_points)
2895 Resize based on speculative points required by primitive.
2897 number_points+=length+1;
2898 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2899 (size_t) number_points,sizeof(*primitive_info));
2900 if (primitive_info == (PrimitiveInfo *) NULL)
2902 (void) ThrowMagickException(exception,GetMagickModule(),
2903 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2908 switch (primitive_type)
2910 case PointPrimitive:
2913 if (primitive_info[j].coordinates != 1)
2918 TracePoint(primitive_info+j,primitive_info[j].point);
2919 i=(ssize_t) (j+primitive_info[j].coordinates);
2924 if (primitive_info[j].coordinates != 2)
2929 TraceLine(primitive_info+j,primitive_info[j].point,
2930 primitive_info[j+1].point);
2931 i=(ssize_t) (j+primitive_info[j].coordinates);
2934 case RectanglePrimitive:
2936 if (primitive_info[j].coordinates != 2)
2941 TraceRectangle(primitive_info+j,primitive_info[j].point,
2942 primitive_info[j+1].point);
2943 i=(ssize_t) (j+primitive_info[j].coordinates);
2946 case RoundRectanglePrimitive:
2948 if (primitive_info[j].coordinates != 3)
2953 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2954 primitive_info[j+1].point,primitive_info[j+2].point);
2955 i=(ssize_t) (j+primitive_info[j].coordinates);
2960 if (primitive_info[j].coordinates != 3)
2962 primitive_type=UndefinedPrimitive;
2965 TraceArc(primitive_info+j,primitive_info[j].point,
2966 primitive_info[j+1].point,primitive_info[j+2].point);
2967 i=(ssize_t) (j+primitive_info[j].coordinates);
2970 case EllipsePrimitive:
2972 if (primitive_info[j].coordinates != 3)
2977 TraceEllipse(primitive_info+j,primitive_info[j].point,
2978 primitive_info[j+1].point,primitive_info[j+2].point);
2979 i=(ssize_t) (j+primitive_info[j].coordinates);
2982 case CirclePrimitive:
2984 if (primitive_info[j].coordinates != 2)
2989 TraceCircle(primitive_info+j,primitive_info[j].point,
2990 primitive_info[j+1].point);
2991 i=(ssize_t) (j+primitive_info[j].coordinates);
2994 case PolylinePrimitive:
2996 case PolygonPrimitive:
2998 primitive_info[i]=primitive_info[j];
2999 primitive_info[i].coordinates=0;
3000 primitive_info[j].coordinates++;
3004 case BezierPrimitive:
3006 if (primitive_info[j].coordinates < 3)
3011 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3012 i=(ssize_t) (j+primitive_info[j].coordinates);
3017 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3020 case ColorPrimitive:
3021 case MattePrimitive:
3026 if (primitive_info[j].coordinates != 1)
3031 GetMagickToken(q,&q,token);
3032 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3036 primitive_info[j].method=(PaintMethod) method;
3041 if (primitive_info[j].coordinates != 1)
3047 GetMagickToken(q,&q,token);
3048 primitive_info[j].text=AcquireString(token);
3051 case ImagePrimitive:
3053 if (primitive_info[j].coordinates != 2)
3058 GetMagickToken(q,&q,token);
3059 primitive_info[j].text=AcquireString(token);
3063 if (primitive_info == (PrimitiveInfo *) NULL)
3065 if (image->debug != MagickFalse)
3066 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3067 if (status == MagickFalse)
3069 primitive_info[i].primitive=UndefinedPrimitive;
3075 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3077 point=primitive_info[i].point;
3078 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3079 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3080 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3081 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3082 point=primitive_info[i].point;
3083 if (point.x < graphic_context[n]->bounds.x1)
3084 graphic_context[n]->bounds.x1=point.x;
3085 if (point.y < graphic_context[n]->bounds.y1)
3086 graphic_context[n]->bounds.y1=point.y;
3087 if (point.x > graphic_context[n]->bounds.x2)
3088 graphic_context[n]->bounds.x2=point.x;
3089 if (point.y > graphic_context[n]->bounds.y2)
3090 graphic_context[n]->bounds.y2=point.y;
3091 if (primitive_info[i].primitive == ImagePrimitive)
3093 if (i >= (ssize_t) number_points)
3094 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3096 if (graphic_context[n]->render != MagickFalse)
3098 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3099 (LocaleCompare(graphic_context[n]->clip_mask,
3100 graphic_context[n-1]->clip_mask) != 0))
3101 (void) DrawClipPath(image,graphic_context[n],
3102 graphic_context[n]->clip_mask,exception);
3103 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3105 if (primitive_info->text != (char *) NULL)
3106 primitive_info->text=(char *) RelinquishMagickMemory(
3107 primitive_info->text);
3108 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3110 if (proceed == MagickFalse)
3113 if (image->debug != MagickFalse)
3114 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3116 Relinquish resources.
3118 token=DestroyString(token);
3119 if (primitive_info != (PrimitiveInfo *) NULL)
3120 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3121 primitive=DestroyString(primitive);
3122 for ( ; n >= 0; n--)
3123 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3124 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3125 if (status == MagickFalse)
3126 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3136 % D r a w G r a d i e n t I m a g e %
3140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3142 % DrawGradientImage() draws a linear gradient on the image.
3144 % The format of the DrawGradientImage method is:
3146 % MagickBooleanType DrawGradientImage(Image *image,
3147 % const DrawInfo *draw_info,ExceptionInfo *exception)
3149 % A description of each parameter follows:
3151 % o image: the image.
3153 % o draw_info: the draw info.
3155 % o exception: return any errors or warnings in this structure.
3159 static inline double GetStopColorOffset(const GradientInfo *gradient,
3160 const ssize_t x,const ssize_t y)
3162 switch (gradient->type)
3164 case UndefinedGradient:
3165 case LinearGradient:
3180 gradient_vector=(&gradient->gradient_vector);
3181 p.x=gradient_vector->x2-gradient_vector->x1;
3182 p.y=gradient_vector->y2-gradient_vector->y1;
3183 q.x=(double) x-gradient_vector->x1;
3184 q.y=(double) y-gradient_vector->y1;
3185 length=sqrt(q.x*q.x+q.y*q.y);
3186 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3187 gamma=PerceptibleReciprocal(gamma);
3188 scale=p.x*q.x+p.y*q.y;
3189 offset=gamma*scale*length;
3192 case RadialGradient:
3201 v.x=(double) x-gradient->center.x;
3202 v.y=(double) y-gradient->center.y;
3203 length=sqrt(v.x*v.x+v.y*v.y);
3204 if (gradient->spread == RepeatSpread)
3206 offset=length/gradient->radius;
3213 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3214 const DrawInfo *draw_info,ExceptionInfo *exception)
3240 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3250 Draw linear or radial gradient on image.
3252 assert(image != (Image *) NULL);
3253 assert(image->signature == MagickSignature);
3254 if (image->debug != MagickFalse)
3255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3256 assert(draw_info != (const DrawInfo *) NULL);
3257 gradient=(&draw_info->gradient);
3258 gradient_vector=(&gradient->gradient_vector);
3259 point.x=gradient_vector->x2-gradient_vector->x1;
3260 point.y=gradient_vector->y2-gradient_vector->y1;
3261 length=sqrt(point.x*point.x+point.y*point.y);
3262 bounding_box=gradient->bounding_box;
3264 GetPixelInfo(image,&zero);
3265 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3266 height=bounding_box.height-bounding_box.y;
3267 width=bounding_box.width-bounding_box.x;
3269 image_view=AcquireAuthenticCacheView(image,exception);
3270 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3271 #pragma omp parallel for schedule(static,4) shared(status) \
3272 dynamic_number_threads(image,width,height,1)
3274 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3294 if (status == MagickFalse)
3296 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3297 if (q == (Quantum *) NULL)
3304 offset=GetStopColorOffset(gradient,0,y);
3305 if (gradient->type != RadialGradient)
3307 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3309 GetPixelInfoPixel(image,q,&pixel);
3310 switch (gradient->spread)
3312 case UndefinedSpread:
3315 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3316 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3318 offset=GetStopColorOffset(gradient,x,y);
3319 if (gradient->type != RadialGradient)
3322 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3323 if (offset < gradient->stops[i].offset)
3325 if ((offset < 0.0) || (i == 0))
3326 composite=gradient->stops[0].color;
3328 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3329 composite=gradient->stops[gradient->number_stops-1].color;
3334 alpha=(offset-gradient->stops[i].offset)/
3335 (gradient->stops[j].offset-gradient->stops[i].offset);
3336 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3337 &gradient->stops[j].color,alpha,&composite);
3343 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3344 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3346 offset=GetStopColorOffset(gradient,x,y);
3347 if (gradient->type != RadialGradient)
3352 if ((ssize_t) fmod(offset,2.0) == 0)
3353 offset=fmod(offset,1.0);
3355 offset=1.0-fmod(offset,1.0);
3356 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3357 if (offset < gradient->stops[i].offset)
3360 composite=gradient->stops[0].color;
3362 if (i == (ssize_t) gradient->number_stops)
3363 composite=gradient->stops[gradient->number_stops-1].color;
3368 alpha=(offset-gradient->stops[i].offset)/
3369 (gradient->stops[j].offset-gradient->stops[i].offset);
3370 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3371 &gradient->stops[j].color,alpha,&composite);
3383 antialias=MagickFalse;
3385 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3386 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3388 offset=GetStopColorOffset(gradient,x,y);
3389 if (gradient->type == LinearGradient)
3391 repeat=fmod(offset,length);
3393 repeat=length-fmod(-repeat,length);
3395 repeat=fmod(offset,length);
3396 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3397 MagickTrue : MagickFalse;
3398 offset=repeat/length;
3402 repeat=fmod(offset,gradient->radius);
3404 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3406 repeat=fmod(offset,gradient->radius);
3407 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3409 offset=repeat/gradient->radius;
3412 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3413 if (offset < gradient->stops[i].offset)
3416 composite=gradient->stops[0].color;
3418 if (i == (ssize_t) gradient->number_stops)
3419 composite=gradient->stops[gradient->number_stops-1].color;
3424 alpha=(offset-gradient->stops[i].offset)/
3425 (gradient->stops[j].offset-gradient->stops[i].offset);
3426 if (antialias != MagickFalse)
3428 if (gradient->type == LinearGradient)
3429 alpha=length-repeat;
3431 alpha=gradient->radius-repeat;
3433 j=(ssize_t) gradient->number_stops-1L;
3435 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3436 &gradient->stops[j].color,alpha,&composite);
3441 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3443 SetPixelInfoPixel(image,&pixel,q);
3444 q+=GetPixelChannels(image);
3446 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3449 image_view=DestroyCacheView(image_view);
3454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3458 % D r a w P a t t e r n P a t h %
3462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3464 % DrawPatternPath() draws a pattern.
3466 % The format of the DrawPatternPath method is:
3468 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3469 % const char *name,Image **pattern,ExceptionInfo *exception)
3471 % A description of each parameter follows:
3473 % o image: the image.
3475 % o draw_info: the draw info.
3477 % o name: the pattern name.
3479 % o image: the image.
3481 % o exception: return any errors or warnings in this structure.
3484 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3485 const DrawInfo *draw_info,const char *name,Image **pattern,
3486 ExceptionInfo *exception)
3489 property[MaxTextExtent];
3504 assert(image != (Image *) NULL);
3505 assert(image->signature == MagickSignature);
3506 if (image->debug != MagickFalse)
3507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3508 assert(draw_info != (const DrawInfo *) NULL);
3509 assert(name != (const char *) NULL);
3510 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3511 path=GetImageArtifact(image,property);
3512 if (path == (const char *) NULL)
3513 return(MagickFalse);
3514 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3515 geometry=GetImageArtifact(image,property);
3516 if (geometry == (const char *) NULL)
3517 return(MagickFalse);
3518 if ((*pattern) != (Image *) NULL)
3519 *pattern=DestroyImage(*pattern);
3520 image_info=AcquireImageInfo();
3521 image_info->size=AcquireString(geometry);
3522 *pattern=AcquireImage(image_info,exception);
3523 image_info=DestroyImageInfo(image_info);
3524 (void) QueryColorCompliance("#00000000",AllCompliance,
3525 &(*pattern)->background_color,exception);
3526 (void) SetImageBackgroundColor(*pattern,exception);
3527 if (image->debug != MagickFalse)
3528 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3529 "begin pattern-path %s %s",name,geometry);
3530 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3531 clone_info->fill_pattern=NewImageList();
3532 clone_info->stroke_pattern=NewImageList();
3533 (void) CloneString(&clone_info->primitive,path);
3534 status=DrawImage(*pattern,clone_info,exception);
3535 clone_info=DestroyDrawInfo(clone_info);
3536 if (image->debug != MagickFalse)
3537 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3546 + D r a w P o l y g o n P r i m i t i v e %
3550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3552 % DrawPolygonPrimitive() draws a polygon on the image.
3554 % The format of the DrawPolygonPrimitive method is:
3556 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3557 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3558 % ExceptionInfo *exception)
3560 % A description of each parameter follows:
3562 % o image: the image.
3564 % o draw_info: the draw info.
3566 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3568 % o exception: return any errors or warnings in this structure.
3572 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3577 assert(polygon_info != (PolygonInfo **) NULL);
3578 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3579 if (polygon_info[i] != (PolygonInfo *) NULL)
3580 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3581 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3582 return(polygon_info);
3585 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3586 const PrimitiveInfo *primitive_info)
3589 *restrict path_info;
3600 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3601 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3602 sizeof(*polygon_info));
3603 if (polygon_info == (PolygonInfo **) NULL)
3604 return((PolygonInfo **) NULL);
3605 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3606 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3607 if (path_info == (PathInfo *) NULL)
3608 return(DestroyPolygonThreadSet(polygon_info));
3609 for (i=0; i < (ssize_t) number_threads; i++)
3611 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3612 if (polygon_info[i] == (PolygonInfo *) NULL)
3613 return(DestroyPolygonThreadSet(polygon_info));
3615 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3616 return(polygon_info);
3619 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3620 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3621 const ssize_t y,double *stroke_alpha)
3632 register const PointInfo
3646 Compute fill & stroke opacity for this (x,y) point.
3650 p=polygon_info->edges;
3651 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3653 if ((double) y <= (p->bounds.y1-mid-0.5))
3655 if ((double) y > (p->bounds.y2+mid+0.5))
3657 (void) DestroyEdge(polygon_info,(size_t) j);
3660 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3661 ((double) x > (p->bounds.x2+mid+0.5)))
3663 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3664 for ( ; i < (ssize_t) p->number_points; i++)
3666 if ((double) y <= (p->points[i-1].y-mid-0.5))
3668 if ((double) y > (p->points[i].y+mid+0.5))
3670 if (p->scanline != (double) y)
3672 p->scanline=(double) y;
3673 p->highwater=(size_t) i;
3676 Compute distance between a point and an edge.
3679 delta.x=(q+1)->x-q->x;
3680 delta.y=(q+1)->y-q->y;
3681 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3684 delta.x=(double) x-q->x;
3685 delta.y=(double) y-q->y;
3686 distance=delta.x*delta.x+delta.y*delta.y;
3690 alpha=delta.x*delta.x+delta.y*delta.y;
3693 delta.x=(double) x-(q+1)->x;
3694 delta.y=(double) y-(q+1)->y;
3695 distance=delta.x*delta.x+delta.y*delta.y;
3699 alpha=PerceptibleReciprocal(alpha);
3700 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3701 distance=alpha*beta*beta;
3705 Compute stroke & subpath opacity.
3708 if (p->ghostline == MagickFalse)
3711 if ((*stroke_alpha < 1.0) &&
3712 (distance <= ((alpha+0.25)*(alpha+0.25))))
3715 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3720 if (distance != 1.0)
3721 beta=sqrt((double) distance);
3723 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3724 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3728 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3730 if (distance <= 0.0)
3740 if (distance != 1.0)
3741 beta=sqrt(distance);
3744 if (subpath_alpha < (alpha*alpha))
3745 subpath_alpha=alpha*alpha;
3749 Compute fill opacity.
3751 if (fill == MagickFalse)
3753 if (subpath_alpha >= 1.0)
3756 Determine winding number.
3759 p=polygon_info->edges;
3760 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3762 if ((double) y <= p->bounds.y1)
3764 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3766 if ((double) x > p->bounds.x2)
3768 winding_number+=p->direction ? 1 : -1;
3771 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3772 for ( ; i < (ssize_t) p->number_points; i++)
3773 if ((double) y <= p->points[i].y)
3776 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3777 winding_number+=p->direction ? 1 : -1;
3779 if (fill_rule != NonZeroRule)
3781 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3785 if (MagickAbsoluteValue(winding_number) != 0)
3787 return(subpath_alpha);
3790 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3791 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3792 ExceptionInfo *exception)
3805 **restrict polygon_info;
3816 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3828 Compute bounding box.
3830 assert(image != (Image *) NULL);
3831 assert(image->signature == MagickSignature);
3832 if (image->debug != MagickFalse)
3833 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3834 assert(draw_info != (DrawInfo *) NULL);
3835 assert(draw_info->signature == MagickSignature);
3836 assert(primitive_info != (PrimitiveInfo *) NULL);
3837 if (primitive_info->coordinates == 0)
3839 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3840 if (polygon_info == (PolygonInfo **) NULL)
3841 return(MagickFalse);
3843 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3844 if (image->debug != MagickFalse)
3845 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3846 fill=(primitive_info->method == FillToBorderMethod) ||
3847 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3848 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3849 bounds=polygon_info[0]->edges[0].bounds;
3850 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3852 p=polygon_info[0]->edges+i;
3853 if (p->bounds.x1 < bounds.x1)
3854 bounds.x1=p->bounds.x1;
3855 if (p->bounds.y1 < bounds.y1)
3856 bounds.y1=p->bounds.y1;
3857 if (p->bounds.x2 > bounds.x2)
3858 bounds.x2=p->bounds.x2;
3859 if (p->bounds.y2 > bounds.y2)
3860 bounds.y2=p->bounds.y2;
3862 bounds.x1-=(mid+1.0);
3863 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3864 image->columns ? (double) image->columns-1 : bounds.x1;
3865 bounds.y1-=(mid+1.0);
3866 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3867 image->rows ? (double) image->rows-1 : bounds.y1;
3868 bounds.x2+=(mid+1.0);
3869 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3870 image->columns ? (double) image->columns-1 : bounds.x2;
3871 bounds.y2+=(mid+1.0);
3872 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3873 image->rows ? (double) image->rows-1 : bounds.y2;
3875 image_view=AcquireAuthenticCacheView(image,exception);
3876 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3877 height=(size_t) (floor(bounds.y2+0.5)-ceil(bounds.y1-0.5));
3878 width=(size_t) (floor(bounds.x2+0.5)-ceil(bounds.x1-0.5));
3880 if (primitive_info->coordinates == 1)
3885 start=(ssize_t) ceil(bounds.y1-0.5);
3886 stop=(ssize_t) floor(bounds.y2+0.5);
3887 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3888 #pragma omp parallel for schedule(static,4) shared(status) \
3889 dynamic_number_threads(image,width,height,1)
3891 for (y=start; y <= stop; y++)
3909 if (status == MagickFalse)
3911 start=(ssize_t) ceil(bounds.x1-0.5);
3912 stop=(ssize_t) floor(bounds.x2+0.5);
3914 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3916 if (q == (Quantum *) NULL)
3921 GetPixelInfo(image,&pixel);
3922 for ( ; x <= stop; x++)
3924 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3925 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3927 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3928 SetPixelInfoPixel(image,&pixel,q);
3930 q+=GetPixelChannels(image);
3932 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3933 if (sync == MagickFalse)
3936 image_view=DestroyCacheView(image_view);
3937 polygon_info=DestroyPolygonThreadSet(polygon_info);
3938 if (image->debug != MagickFalse)
3939 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3940 " end draw-polygon");
3944 Draw polygon or line.
3946 if (image->alpha_trait != BlendPixelTrait)
3947 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3948 start=(ssize_t) ceil(bounds.y1-0.5);
3949 stop=(ssize_t) floor(bounds.y2+0.5);
3950 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3951 #pragma omp parallel for schedule(static,4) shared(status) \
3952 dynamic_number_threads(image,width,height,1)
3954 for (y=start; y <= stop; y++)
3957 id = GetOpenMPThreadId();
3977 if (status == MagickFalse)
3979 start=(ssize_t) ceil(bounds.x1-0.5);
3980 stop=(ssize_t) floor(bounds.x2+0.5);
3981 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3983 if (q == (Quantum *) NULL)
3988 for (x=start; x <= stop; x++)
3993 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3995 if (draw_info->stroke_antialias == MagickFalse)
3997 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3998 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4000 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4001 fill_alpha=fill_alpha*fill_color.alpha;
4002 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4003 GetPixelAlpha(image,q),q);
4004 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
4005 stroke_alpha=stroke_alpha*stroke_color.alpha;
4006 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4007 GetPixelAlpha(image,q),q);
4008 q+=GetPixelChannels(image);
4010 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4013 image_view=DestroyCacheView(image_view);
4014 polygon_info=DestroyPolygonThreadSet(polygon_info);
4015 if (image->debug != MagickFalse)
4016 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4025 % D r a w P r i m i t i v e %
4029 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4031 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4033 % The format of the DrawPrimitive method is:
4035 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4036 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4038 % A description of each parameter follows:
4040 % o image: the image.
4042 % o draw_info: the draw info.
4044 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4046 % o exception: return any errors or warnings in this structure.
4050 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4076 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4077 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4078 switch (primitive_info->primitive)
4080 case PointPrimitive:
4082 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4083 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4084 methods[primitive_info->method]);
4087 case ColorPrimitive:
4089 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4090 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4091 methods[primitive_info->method]);
4094 case MattePrimitive:
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4097 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4098 methods[primitive_info->method]);
4103 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4104 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4107 case ImagePrimitive:
4109 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4110 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4117 p=primitive_info[0].point;
4120 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4122 point=primitive_info[i].point;
4123 if (coordinates <= 0)
4125 coordinates=(ssize_t) primitive_info[i].coordinates;
4126 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4127 " begin open (%.20g)",(double) coordinates);
4130 point=primitive_info[i].point;
4131 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4132 (fabs(q.y-point.y) >= MagickEpsilon))
4133 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4134 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4136 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4137 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4140 if (coordinates > 0)
4142 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4143 (fabs(p.y-point.y) >= MagickEpsilon))
4144 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4145 (double) coordinates);
4147 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4148 (double) coordinates);
4152 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4153 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4154 ExceptionInfo *exception)
4169 if (image->debug != MagickFalse)
4171 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4172 " begin draw-primitive");
4173 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4174 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4175 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4176 draw_info->affine.tx,draw_info->affine.ty);
4178 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4179 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4180 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4181 (void) SetImageColorspace(image,RGBColorspace,exception);
4183 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4184 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4185 image_view=AcquireAuthenticCacheView(image,exception);
4186 switch (primitive_info->primitive)
4188 case PointPrimitive:
4196 if ((y < 0) || (y >= (ssize_t) image->rows))
4198 if ((x < 0) || (x >= (ssize_t) image->columns))
4200 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4201 if (q == (Quantum *) NULL)
4203 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4204 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4205 (double) GetPixelAlpha(image,q),q);
4206 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4209 case ColorPrimitive:
4211 switch (primitive_info->method)
4222 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4223 if (q == (Quantum *) NULL)
4225 GetPixelInfo(image,&pixel);
4226 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4227 SetPixelInfoPixel(image,&pixel,q);
4228 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4240 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4242 for (y=0; y < (ssize_t) image->rows; y++)
4247 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4249 if (q == (Quantum *) NULL)
4251 for (x=0; x < (ssize_t) image->columns; x++)
4253 GetPixelInfoPixel(image,q,&pixel);
4254 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4256 q+=GetPixelChannels(image);
4259 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4260 SetPixelInfoPixel(image,&pixel,q);
4261 q+=GetPixelChannels(image);
4263 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4264 if (sync == MagickFalse)
4269 case FloodfillMethod:
4270 case FillToBorderMethod:
4275 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4277 if (primitive_info->method == FillToBorderMethod)
4279 target.red=(double) draw_info->border_color.red;
4280 target.green=(double) draw_info->border_color.green;
4281 target.blue=(double) draw_info->border_color.blue;
4283 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4284 primitive_info->method == FloodfillMethod ? MagickFalse :
4285 MagickTrue,exception);
4296 GetPixelInfo(image,&pixel);
4297 for (y=0; y < (ssize_t) image->rows; y++)
4305 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4307 if (q == (Quantum *) NULL)
4309 for (x=0; x < (ssize_t) image->columns; x++)
4311 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4312 SetPixelInfoPixel(image,&pixel,q);
4313 q+=GetPixelChannels(image);
4315 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4316 if (sync == MagickFalse)
4324 case MattePrimitive:
4326 if (image->alpha_trait != BlendPixelTrait)
4327 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4328 switch (primitive_info->method)
4339 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4340 if (q == (Quantum *) NULL)
4342 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4343 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4344 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4356 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4358 for (y=0; y < (ssize_t) image->rows; y++)
4366 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4368 if (q == (Quantum *) NULL)
4370 for (x=0; x < (ssize_t) image->columns; x++)
4372 GetPixelInfoPixel(image,q,&pixel);
4373 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4375 q+=GetPixelChannels(image);
4378 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4379 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4380 q+=GetPixelChannels(image);
4382 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4383 if (sync == MagickFalse)
4388 case FloodfillMethod:
4389 case FillToBorderMethod:
4397 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4399 if (primitive_info->method == FillToBorderMethod)
4401 target.red=(double) draw_info->border_color.red;
4402 target.green=(double) draw_info->border_color.green;
4403 target.blue=(double) draw_info->border_color.blue;
4405 channel_mask=SetImageChannelMask(image,AlphaChannel);
4406 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4407 primitive_info->method == FloodfillMethod ? MagickFalse :
4408 MagickTrue,exception);
4409 (void) SetImageChannelMask(image,channel_mask);
4420 for (y=0; y < (ssize_t) image->rows; y++)
4428 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4430 if (q == (Quantum *) NULL)
4432 for (x=0; x < (ssize_t) image->columns; x++)
4434 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4435 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4436 q+=GetPixelChannels(image);
4438 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4439 if (sync == MagickFalse)
4450 geometry[MaxTextExtent];
4455 if (primitive_info->text == (char *) NULL)
4457 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4458 (void) CloneString(&clone_info->text,primitive_info->text);
4459 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4460 primitive_info->point.x,primitive_info->point.y);
4461 (void) CloneString(&clone_info->geometry,geometry);
4462 status=AnnotateImage(image,clone_info,exception);
4463 clone_info=DestroyDrawInfo(clone_info);
4466 case ImagePrimitive:
4472 composite_geometry[MaxTextExtent];
4487 if (primitive_info->text == (char *) NULL)
4489 clone_info=AcquireImageInfo();
4490 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4491 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4495 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4497 composite_image=ReadImage(clone_info,exception);
4499 clone_info=DestroyImageInfo(clone_info);
4500 if (composite_image == (Image *) NULL)
4502 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4503 NULL,(void *) NULL);
4504 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4505 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4506 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4507 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4510 geometry[MaxTextExtent];
4515 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4516 primitive_info[1].point.x,primitive_info[1].point.y);
4517 composite_image->filter=image->filter;
4518 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4521 if (composite_image->alpha_trait != BlendPixelTrait)
4522 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4524 if (draw_info->alpha != OpaqueAlpha)
4525 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4526 SetGeometry(image,&geometry);
4527 image->gravity=draw_info->gravity;
4530 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4531 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4532 composite_image->rows,(double) geometry.x,(double) geometry.y);
4533 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4534 affine=draw_info->affine;
4535 affine.tx=(double) geometry.x;
4536 affine.ty=(double) geometry.y;
4537 composite_image->interpolate=image->interpolate;
4538 if (draw_info->compose == OverCompositeOp)
4539 (void) DrawAffineImage(image,composite_image,&affine,exception);
4541 (void) CompositeImage(image,composite_image,draw_info->compose,
4542 MagickTrue,geometry.x,geometry.y,exception);
4543 composite_image=DestroyImage(composite_image);
4555 if (IsEventLogging() != MagickFalse)
4556 LogPrimitiveInfo(primitive_info);
4557 scale=ExpandAffine(&draw_info->affine);
4558 if ((draw_info->dash_pattern != (double *) NULL) &&
4559 (draw_info->dash_pattern[0] != 0.0) &&
4560 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4561 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4566 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4567 clone_info->stroke_width=0.0;
4568 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4569 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4571 clone_info=DestroyDrawInfo(clone_info);
4572 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4575 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4577 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4583 Draw strokes while respecting line cap/join attributes.
4585 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4587 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4588 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4589 MagickTrue : MagickFalse;
4590 i=(ssize_t) primitive_info[0].coordinates;
4591 if ((((draw_info->linecap == RoundCap) ||
4592 (closed_path != MagickFalse)) &&
4593 (draw_info->linejoin == RoundJoin)) ||
4594 (primitive_info[i].primitive != UndefinedPrimitive))
4596 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4600 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4601 clone_info->stroke_width=0.0;
4602 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4603 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4605 clone_info=DestroyDrawInfo(clone_info);
4606 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4609 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4613 image_view=DestroyCacheView(image_view);
4614 if (image->debug != MagickFalse)
4615 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4616 return(status != 0 ? MagickTrue : MagickFalse);
4620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4624 + D r a w S t r o k e P o l y g o n %
4628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4630 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4631 % the image while respecting the line cap and join attributes.
4633 % The format of the DrawStrokePolygon method is:
4635 % MagickBooleanType DrawStrokePolygon(Image *image,
4636 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4638 % A description of each parameter follows:
4640 % o image: the image.
4642 % o draw_info: the draw info.
4644 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4649 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4650 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4658 for (i=0; i < 4; i++)
4659 linecap[i]=(*primitive_info);
4660 linecap[0].coordinates=4;
4661 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4662 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4663 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4664 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4665 linecap[4].primitive=UndefinedPrimitive;
4666 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4669 static MagickBooleanType DrawStrokePolygon(Image *image,
4670 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4671 ExceptionInfo *exception)
4683 register const PrimitiveInfo
4688 Draw stroked polygon.
4690 if (image->debug != MagickFalse)
4691 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4692 " begin draw-stroke-polygon");
4693 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4694 clone_info->fill=draw_info->stroke;
4695 if (clone_info->fill_pattern != (Image *) NULL)
4696 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4697 if (clone_info->stroke_pattern != (Image *) NULL)
4698 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4699 MagickTrue,exception);
4700 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4701 clone_info->stroke_width=0.0;
4702 clone_info->fill_rule=NonZeroRule;
4704 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4706 stroke_polygon=TraceStrokePolygon(draw_info,p);
4707 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4708 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4709 q=p+p->coordinates-1;
4710 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4711 MagickTrue : MagickFalse;
4712 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4714 DrawRoundLinecap(image,draw_info,p,exception);
4715 DrawRoundLinecap(image,draw_info,q,exception);
4718 clone_info=DestroyDrawInfo(clone_info);
4719 if (image->debug != MagickFalse)
4720 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4721 " end draw-stroke-polygon");
4726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4730 % G e t A f f i n e M a t r i x %
4734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4736 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4739 % The format of the GetAffineMatrix method is:
4741 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4743 % A description of each parameter follows:
4745 % o affine_matrix: the affine matrix.
4748 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4750 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4751 assert(affine_matrix != (AffineMatrix *) NULL);
4752 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4753 affine_matrix->sx=1.0;
4754 affine_matrix->sy=1.0;
4758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4762 + G e t D r a w I n f o %
4766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4768 % GetDrawInfo() initializes draw_info to default values from image_info.
4770 % The format of the GetDrawInfo method is:
4772 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4774 % A description of each parameter follows:
4776 % o image_info: the image info..
4778 % o draw_info: the draw info.
4781 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4790 Initialize draw attributes.
4792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4793 assert(draw_info != (DrawInfo *) NULL);
4794 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4795 GetAffineMatrix(&draw_info->affine);
4796 exception=AcquireExceptionInfo();
4797 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4799 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4801 draw_info->stroke_width=1.0;
4802 draw_info->alpha=OpaqueAlpha;
4803 draw_info->fill_rule=EvenOddRule;
4804 draw_info->linecap=ButtCap;
4805 draw_info->linejoin=MiterJoin;
4806 draw_info->miterlimit=10;
4807 draw_info->decorate=NoDecoration;
4808 draw_info->pointsize=12.0;
4809 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4810 draw_info->compose=OverCompositeOp;
4811 draw_info->render=MagickTrue;
4812 draw_info->debug=IsEventLogging();
4813 if (image_info != (ImageInfo *) NULL)
4815 draw_info->stroke_antialias=image_info->antialias;
4816 if (image_info->font != (char *) NULL)
4817 draw_info->font=AcquireString(image_info->font);
4818 if (image_info->density != (char *) NULL)
4819 draw_info->density=AcquireString(image_info->density);
4820 draw_info->text_antialias=image_info->antialias;
4821 if (image_info->pointsize != 0.0)
4822 draw_info->pointsize=image_info->pointsize;
4823 draw_info->border_color=image_info->border_color;
4824 if (image_info->server_name != (char *) NULL)
4825 draw_info->server_name=AcquireString(image_info->server_name);
4826 option=GetImageOption(image_info,"encoding");
4827 if (option != (const char *) NULL)
4828 (void) CloneString(&draw_info->encoding,option);
4829 option=GetImageOption(image_info,"kerning");
4830 if (option != (const char *) NULL)
4831 draw_info->kerning=StringToDouble(option,(char **) NULL);
4832 option=GetImageOption(image_info,"interline-spacing");
4833 if (option != (const char *) NULL)
4834 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4835 option=GetImageOption(image_info,"interword-spacing");
4836 if (option != (const char *) NULL)
4837 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4838 option=GetImageOption(image_info,"direction");
4839 if (option != (const char *) NULL)
4840 draw_info->direction=(DirectionType) ParseCommandOption(
4841 MagickDirectionOptions,MagickFalse,option);
4843 draw_info->direction=UndefinedDirection;
4844 option=GetImageOption(image_info,"fill");
4845 if (option != (const char *) NULL)
4846 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4848 option=GetImageOption(image_info,"stroke");
4849 if (option != (const char *) NULL)
4850 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4852 option=GetImageOption(image_info,"strokewidth");
4853 if (option != (const char *) NULL)
4854 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4855 option=GetImageOption(image_info,"undercolor");
4856 if (option != (const char *) NULL)
4857 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4859 option=GetImageOption(image_info,"gravity");
4860 if (option != (const char *) NULL)
4861 draw_info->gravity=(GravityType) ParseCommandOption(
4862 MagickGravityOptions,MagickFalse,option);
4864 exception=DestroyExceptionInfo(exception);
4865 draw_info->signature=MagickSignature;
4869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4873 + P e r m u t a t e %
4877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4879 % Permutate() returns the permuation of the (n,k).
4881 % The format of the Permutate method is:
4883 % void Permutate(ssize_t n,ssize_t k)
4885 % A description of each parameter follows:
4893 static inline double Permutate(const ssize_t n,const ssize_t k)
4902 for (i=k+1; i <= n; i++)
4904 for (i=1; i <= (n-k); i++)
4910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4914 + T r a c e P r i m i t i v e %
4918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4920 % TracePrimitive is a collection of methods for generating graphic
4921 % primitives such as arcs, ellipses, paths, etc.
4925 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4926 const PointInfo end,const PointInfo degrees)
4932 center.x=0.5*(end.x+start.x);
4933 center.y=0.5*(end.y+start.y);
4934 radii.x=fabs(center.x-start.x);
4935 radii.y=fabs(center.y-start.y);
4936 TraceEllipse(primitive_info,center,radii,degrees);
4939 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4940 const PointInfo end,const PointInfo arc,const double angle,
4941 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4960 register PrimitiveInfo
4969 if ((start.x == end.x) && (start.y == end.y))
4971 TracePoint(primitive_info,end);
4974 radii.x=fabs(arc.x);
4975 radii.y=fabs(arc.y);
4976 if ((radii.x == 0.0) || (radii.y == 0.0))
4978 TraceLine(primitive_info,start,end);
4981 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4982 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4983 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4984 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4985 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4987 if (delta < MagickEpsilon)
4989 TraceLine(primitive_info,start,end);
4994 radii.x*=sqrt((double) delta);
4995 radii.y*=sqrt((double) delta);
4997 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4998 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4999 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5000 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5001 alpha=points[1].x-points[0].x;
5002 beta=points[1].y-points[0].y;
5003 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5008 factor=sqrt((double) factor);
5009 if (sweep == large_arc)
5012 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5013 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5014 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5015 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5016 if ((theta < 0.0) && (sweep != MagickFalse))
5017 theta+=(double) (2.0*MagickPI);
5019 if ((theta > 0.0) && (sweep == MagickFalse))
5020 theta-=(double) (2.0*MagickPI);
5021 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5024 for (i=0; i < (ssize_t) arc_segments; i++)
5026 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5027 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5028 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5029 sin(fmod((double) beta,DegreesToRadians(360.0)));
5030 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5031 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5032 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5033 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5034 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5035 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5036 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5037 theta/arc_segments),DegreesToRadians(360.0))));
5038 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5039 theta/arc_segments),DegreesToRadians(360.0))));
5040 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5041 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5042 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5043 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5044 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5045 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5046 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5048 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5050 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5052 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5054 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5056 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5058 if (i == (ssize_t) (arc_segments-1))
5063 primitive_info->coordinates=(size_t) (p-primitive_info);
5064 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5066 p->primitive=primitive_info->primitive;
5071 static void TraceBezier(PrimitiveInfo *primitive_info,
5072 const size_t number_coordinates)
5084 register PrimitiveInfo
5096 Allocate coeficients.
5098 quantum=number_coordinates;
5099 for (i=0; i < (ssize_t) number_coordinates; i++)
5101 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5103 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5104 if (alpha > (double) quantum)
5105 quantum=(size_t) alpha;
5106 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5107 if (alpha > (double) quantum)
5108 quantum=(size_t) alpha;
5111 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5112 (double) BezierQuantum);
5113 control_points=quantum*number_coordinates;
5114 coefficients=(double *) AcquireQuantumMemory((size_t)
5115 number_coordinates,sizeof(*coefficients));
5116 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5118 if ((coefficients == (double *) NULL) ||
5119 (points == (PointInfo *) NULL))
5120 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5122 Compute bezier points.
5124 end=primitive_info[number_coordinates-1].point;
5125 for (i=0; i < (ssize_t) number_coordinates; i++)
5126 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5128 for (i=0; i < (ssize_t) control_points; i++)
5133 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5134 for (j=0; j < (ssize_t) number_coordinates; j++)
5136 point.x+=alpha*coefficients[j]*p->point.x;
5137 point.y+=alpha*coefficients[j]*p->point.y;
5138 alpha*=weight/(1.0-weight);
5142 weight+=1.0/control_points;
5145 Bezier curves are just short segmented polys.
5148 for (i=0; i < (ssize_t) control_points; i++)
5150 TracePoint(p,points[i]);
5155 primitive_info->coordinates=(size_t) (p-primitive_info);
5156 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5158 p->primitive=primitive_info->primitive;
5161 points=(PointInfo *) RelinquishMagickMemory(points);
5162 coefficients=(double *) RelinquishMagickMemory(coefficients);
5165 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5166 const PointInfo end)
5177 alpha=end.x-start.x;
5179 radius=hypot((double) alpha,(double) beta);
5180 offset.x=(double) radius;
5181 offset.y=(double) radius;
5184 TraceEllipse(primitive_info,start,offset,degrees);
5187 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5188 const PointInfo stop,const PointInfo degrees)
5199 register PrimitiveInfo
5206 Ellipses are just short segmented polys.
5208 if ((stop.x == 0.0) && (stop.y == 0.0))
5210 TracePoint(primitive_info,start);
5213 delta=2.0/MagickMax(stop.x,stop.y);
5214 step=(double) (MagickPI/8.0);
5215 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5216 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5217 angle.x=DegreesToRadians(degrees.x);
5219 while (y < degrees.x)
5221 angle.y=(double) DegreesToRadians(y);
5222 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5224 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5225 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5226 TracePoint(p,point);
5229 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5230 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5231 TracePoint(p,point);
5233 primitive_info->coordinates=(size_t) (p-primitive_info);
5234 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5236 p->primitive=primitive_info->primitive;
5241 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5242 const PointInfo end)
5244 TracePoint(primitive_info,start);
5245 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5246 (fabs(start.y-end.y) < MagickEpsilon))
5248 primitive_info->primitive=PointPrimitive;
5249 primitive_info->coordinates=1;
5252 TracePoint(primitive_info+1,end);
5253 (primitive_info+1)->primitive=primitive_info->primitive;
5254 primitive_info->coordinates=2;
5257 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5260 token[MaxTextExtent];
5282 register PrimitiveInfo
5297 number_coordinates=0;
5299 primitive_type=primitive_info->primitive;
5301 for (p=path; *p != '\0'; )
5303 while (isspace((int) ((unsigned char) *p)) != 0)
5307 last_attribute=attribute;
5308 attribute=(int) (*p++);
5329 GetMagickToken(p,&p,token);
5331 GetMagickToken(p,&p,token);
5332 arc.x=StringToDouble(token,(char **) NULL);
5333 GetMagickToken(p,&p,token);
5335 GetMagickToken(p,&p,token);
5336 arc.y=StringToDouble(token,(char **) NULL);
5337 GetMagickToken(p,&p,token);
5339 GetMagickToken(p,&p,token);
5340 angle=StringToDouble(token,(char **) NULL);
5341 GetMagickToken(p,&p,token);
5343 GetMagickToken(p,&p,token);
5344 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5345 GetMagickToken(p,&p,token);
5347 GetMagickToken(p,&p,token);
5348 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5349 GetMagickToken(p,&p,token);
5351 GetMagickToken(p,&p,token);
5352 x=StringToDouble(token,(char **) NULL);
5353 GetMagickToken(p,&p,token);
5355 GetMagickToken(p,&p,token);
5356 y=StringToDouble(token,(char **) NULL);
5357 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5358 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5359 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5362 while (isspace((int) ((unsigned char) *p)) != 0)
5366 } while (IsPoint(p) != MagickFalse);
5373 Compute bezier points.
5378 for (i=1; i < 4; i++)
5380 GetMagickToken(p,&p,token);
5382 GetMagickToken(p,&p,token);
5383 x=StringToDouble(token,(char **) NULL);
5384 GetMagickToken(p,&p,token);
5386 GetMagickToken(p,&p,token);
5387 y=StringToDouble(token,(char **) NULL);
5388 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5389 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5392 for (i=0; i < 4; i++)
5393 (q+i)->point=points[i];
5397 } while (IsPoint(p) != MagickFalse);
5405 GetMagickToken(p,&p,token);
5407 GetMagickToken(p,&p,token);
5408 x=StringToDouble(token,(char **) NULL);
5409 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5410 TracePoint(q,point);
5412 } while (IsPoint(p) != MagickFalse);
5420 GetMagickToken(p,&p,token);
5422 GetMagickToken(p,&p,token);
5423 x=StringToDouble(token,(char **) NULL);
5424 GetMagickToken(p,&p,token);
5426 GetMagickToken(p,&p,token);
5427 y=StringToDouble(token,(char **) NULL);
5428 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5429 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5430 TracePoint(q,point);
5432 } while (IsPoint(p) != MagickFalse);
5438 if (q != primitive_info)
5440 primitive_info->coordinates=(size_t) (q-primitive_info);
5441 number_coordinates+=primitive_info->coordinates;
5447 GetMagickToken(p,&p,token);
5449 GetMagickToken(p,&p,token);
5450 x=StringToDouble(token,(char **) NULL);
5451 GetMagickToken(p,&p,token);
5453 GetMagickToken(p,&p,token);
5454 y=StringToDouble(token,(char **) NULL);
5455 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5456 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5460 TracePoint(q,point);
5462 if ((i != 0) && (attribute == (int) 'M'))
5464 TracePoint(q,point);
5467 } while (IsPoint(p) != MagickFalse);
5474 Compute bezier points.
5479 for (i=1; i < 3; i++)
5481 GetMagickToken(p,&p,token);
5483 GetMagickToken(p,&p,token);
5484 x=StringToDouble(token,(char **) NULL);
5485 GetMagickToken(p,&p,token);
5487 GetMagickToken(p,&p,token);
5488 y=StringToDouble(token,(char **) NULL);
5491 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5492 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5495 for (i=0; i < 3; i++)
5496 (q+i)->point=points[i];
5500 } while (IsPoint(p) != MagickFalse);
5507 Compute bezier points.
5511 points[0]=points[3];
5512 points[1].x=2.0*points[3].x-points[2].x;
5513 points[1].y=2.0*points[3].y-points[2].y;
5514 for (i=2; i < 4; i++)
5516 GetMagickToken(p,&p,token);
5518 GetMagickToken(p,&p,token);
5519 x=StringToDouble(token,(char **) NULL);
5520 GetMagickToken(p,&p,token);
5522 GetMagickToken(p,&p,token);
5523 y=StringToDouble(token,(char **) NULL);
5526 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5527 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5530 if (strchr("CcSs",last_attribute) == (char *) NULL)
5532 points[0]=points[2];
5533 points[1]=points[3];
5535 for (i=0; i < 4; i++)
5536 (q+i)->point=points[i];
5540 } while (IsPoint(p) != MagickFalse);
5547 Compute bezier points.
5551 points[0]=points[2];
5552 points[1].x=2.0*points[2].x-points[1].x;
5553 points[1].y=2.0*points[2].y-points[1].y;
5554 for (i=2; i < 3; i++)
5556 GetMagickToken(p,&p,token);
5558 GetMagickToken(p,&p,token);
5559 x=StringToDouble(token,(char **) NULL);
5560 GetMagickToken(p,&p,token);
5562 GetMagickToken(p,&p,token);
5563 y=StringToDouble(token,(char **) NULL);
5564 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5565 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5568 if (strchr("QqTt",last_attribute) == (char *) NULL)
5570 points[0]=points[2];
5571 points[1]=points[3];
5573 for (i=0; i < 3; i++)
5574 (q+i)->point=points[i];
5578 } while (IsPoint(p) != MagickFalse);
5586 GetMagickToken(p,&p,token);
5588 GetMagickToken(p,&p,token);
5589 y=StringToDouble(token,(char **) NULL);
5590 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5591 TracePoint(q,point);
5593 } while (IsPoint(p) != MagickFalse);
5600 TracePoint(q,point);
5602 primitive_info->coordinates=(size_t) (q-primitive_info);
5603 number_coordinates+=primitive_info->coordinates;
5610 if (isalpha((int) ((unsigned char) attribute)) != 0)
5611 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5617 primitive_info->coordinates=(size_t) (q-primitive_info);
5618 number_coordinates+=primitive_info->coordinates;
5619 for (i=0; i < (ssize_t) number_coordinates; i++)
5622 q->primitive=primitive_type;
5624 q->method=FillToBorderMethod;
5627 return(number_coordinates);
5630 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5631 const PointInfo end)
5636 register PrimitiveInfo
5643 TracePoint(p,start);
5647 TracePoint(p,point);
5653 TracePoint(p,point);
5655 TracePoint(p,start);
5657 primitive_info->coordinates=(size_t) (p-primitive_info);
5658 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5660 p->primitive=primitive_info->primitive;
5665 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5666 const PointInfo start,const PointInfo end,PointInfo arc)
5673 register PrimitiveInfo
5680 offset.x=fabs(end.x-start.x);
5681 offset.y=fabs(end.y-start.y);
5682 if (arc.x > (0.5*offset.x))
5684 if (arc.y > (0.5*offset.y))
5686 point.x=start.x+offset.x-arc.x;
5687 point.y=start.y+arc.y;
5690 TraceEllipse(p,point,arc,degrees);
5692 point.x=start.x+offset.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+offset.y-arc.y;
5702 TraceEllipse(p,point,arc,degrees);
5704 point.x=start.x+arc.x;
5705 point.y=start.y+arc.y;
5708 TraceEllipse(p,point,arc,degrees);
5710 TracePoint(p,primitive_info->point);
5712 primitive_info->coordinates=(size_t) (p-primitive_info);
5713 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5715 p->primitive=primitive_info->primitive;
5720 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5721 const size_t number_vertices,const double offset)
5738 for (i=1; i < (ssize_t) number_vertices; i++)
5740 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5741 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5742 if ((fabs((double) dx) >= MagickEpsilon) ||
5743 (fabs((double) dy) >= MagickEpsilon))
5746 if (i == (ssize_t) number_vertices)
5747 i=(ssize_t) number_vertices-1L;
5748 distance=hypot((double) dx,(double) dy);
5749 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5750 dx*(distance+offset)/distance);
5751 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5752 dy*(distance+offset)/distance);
5753 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5755 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5756 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5757 if ((fabs((double) dx) >= MagickEpsilon) ||
5758 (fabs((double) dy) >= MagickEpsilon))
5761 distance=hypot((double) dx,(double) dy);
5762 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5763 dx*(distance+offset)/distance);
5764 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5765 dy*(distance+offset)/distance);
5768 static inline double DrawEpsilonReciprocal(const double x)
5770 #define DrawEpsilon ((double) 1.0e-10)
5772 double sign = x < (double) 0.0 ? (double) -1.0 :
5774 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5775 (double) 1.0/DrawEpsilon));
5778 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5779 const PrimitiveInfo *primitive_info)
5781 typedef struct _LineSegment
5833 number_vertices=primitive_info->coordinates;
5834 max_strokes=2*number_vertices+6*BezierQuantum+360;
5835 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5837 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5839 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5840 number_vertices+2UL,sizeof(*polygon_primitive));
5841 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5842 (polygon_primitive == (PrimitiveInfo *) NULL))
5843 return((PrimitiveInfo *) NULL);
5844 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5845 number_vertices*sizeof(*polygon_primitive));
5847 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5848 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5849 MagickTrue : MagickFalse;
5850 if ((draw_info->linejoin == RoundJoin) ||
5851 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5853 polygon_primitive[number_vertices]=primitive_info[1];
5856 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5858 Compute the slope for the first line segment, p.
5862 for (n=1; n < (ssize_t) number_vertices; n++)
5864 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5865 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5866 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5869 if (n == (ssize_t) number_vertices)
5870 n=(ssize_t) number_vertices-1L;
5871 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5872 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5873 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5874 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5876 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5877 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5878 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5879 offset.y=(double) (offset.x*inverse_slope.p);
5880 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5882 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5883 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5884 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5885 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5886 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5887 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5888 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5889 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5893 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5894 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5895 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5896 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5897 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5898 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5899 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5900 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5903 Create strokes for the line join attribute: bevel, miter, round.
5907 path_q[p++]=box_q[0];
5908 path_p[q++]=box_p[0];
5909 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5912 Compute the slope for this line segment, q.
5914 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5915 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5916 dot_product=dx.q*dx.q+dy.q*dy.q;
5917 if (dot_product < 0.25)
5919 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5920 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5921 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5922 offset.y=(double) (offset.x*inverse_slope.q);
5923 dot_product=dy.q*offset.x-dx.q*offset.y;
5924 if (dot_product > 0.0)
5926 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5927 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5928 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5929 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5930 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5931 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5932 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5933 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5937 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5938 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5939 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5940 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5941 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5942 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5943 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5944 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5946 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5953 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5954 box_p[3].y)/(slope.p-slope.q));
5955 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5956 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5957 box_q[3].y)/(slope.p-slope.q));
5958 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5960 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5962 max_strokes+=6*BezierQuantum+360;
5963 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5965 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5967 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5969 polygon_primitive=(PrimitiveInfo *)
5970 RelinquishMagickMemory(polygon_primitive);
5971 return((PrimitiveInfo *) NULL);
5974 dot_product=dx.q*dy.p-dx.p*dy.q;
5975 if (dot_product <= 0.0)
5976 switch (draw_info->linejoin)
5980 path_q[q++]=box_q[1];
5981 path_q[q++]=box_q[2];
5982 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5983 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5984 if (dot_product <= miterlimit)
5985 path_p[p++]=box_p[4];
5988 path_p[p++]=box_p[1];
5989 path_p[p++]=box_p[2];
5995 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5996 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5997 if (dot_product <= miterlimit)
5999 path_q[q++]=box_q[4];
6000 path_p[p++]=box_p[4];
6004 path_q[q++]=box_q[1];
6005 path_q[q++]=box_q[2];
6006 path_p[p++]=box_p[1];
6007 path_p[p++]=box_p[2];
6013 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6014 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6015 if (dot_product <= miterlimit)
6016 path_p[p++]=box_p[4];
6019 path_p[p++]=box_p[1];
6020 path_p[p++]=box_p[2];
6022 center=polygon_primitive[n].point;
6023 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6024 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6025 if (theta.q < theta.p)
6026 theta.q+=(double) (2.0*MagickPI);
6027 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6028 (2.0*sqrt((double) (1.0/mid)))));
6029 path_q[q].x=box_q[1].x;
6030 path_q[q].y=box_q[1].y;
6032 for (j=1; j < (ssize_t) arc_segments; j++)
6034 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6035 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6036 (theta.p+delta_theta),DegreesToRadians(360.0))));
6037 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6038 (theta.p+delta_theta),DegreesToRadians(360.0))));
6041 path_q[q++]=box_q[2];
6048 switch (draw_info->linejoin)
6052 path_p[p++]=box_p[1];
6053 path_p[p++]=box_p[2];
6054 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6055 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6056 if (dot_product <= miterlimit)
6057 path_q[q++]=box_q[4];
6060 path_q[q++]=box_q[1];
6061 path_q[q++]=box_q[2];
6067 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6068 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6069 if (dot_product <= miterlimit)
6071 path_q[q++]=box_q[4];
6072 path_p[p++]=box_p[4];
6076 path_q[q++]=box_q[1];
6077 path_q[q++]=box_q[2];
6078 path_p[p++]=box_p[1];
6079 path_p[p++]=box_p[2];
6085 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6086 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6087 if (dot_product <= miterlimit)
6088 path_q[q++]=box_q[4];
6091 path_q[q++]=box_q[1];
6092 path_q[q++]=box_q[2];
6094 center=polygon_primitive[n].point;
6095 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6096 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6097 if (theta.p < theta.q)
6098 theta.p+=(double) (2.0*MagickPI);
6099 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6100 (2.0*sqrt((double) (1.0/mid)))));
6101 path_p[p++]=box_p[1];
6102 for (j=1; j < (ssize_t) arc_segments; j++)
6104 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6105 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6106 (theta.p+delta_theta),DegreesToRadians(360.0))));
6107 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6108 (theta.p+delta_theta),DegreesToRadians(360.0))));
6111 path_p[p++]=box_p[2];
6118 inverse_slope.p=inverse_slope.q;
6127 path_p[p++]=box_p[1];
6128 path_q[q++]=box_q[1];
6130 Trace stroked polygon.
6132 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6133 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6134 if (stroke_polygon != (PrimitiveInfo *) NULL)
6136 for (i=0; i < (ssize_t) p; i++)
6138 stroke_polygon[i]=polygon_primitive[0];
6139 stroke_polygon[i].point=path_p[i];
6141 if (closed_path != MagickFalse)
6143 stroke_polygon[i]=polygon_primitive[0];
6144 stroke_polygon[i].point=stroke_polygon[0].point;
6147 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6149 stroke_polygon[i]=polygon_primitive[0];
6150 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6152 if (closed_path != MagickFalse)
6154 stroke_polygon[i]=polygon_primitive[0];
6155 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6158 stroke_polygon[i]=polygon_primitive[0];
6159 stroke_polygon[i].point=stroke_polygon[0].point;
6161 stroke_polygon[i].primitive=UndefinedPrimitive;
6162 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6164 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6165 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6166 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6167 return(stroke_polygon);