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 MagickRealType,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 MagickRealType);
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 draw_info structure is created initialized to
224 % default values, according to the given image_info.
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=MagickEpsilonReciprocal(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)
1118 Determine bounding box.
1120 assert(image != (Image *) NULL);
1121 assert(image->signature == MagickSignature);
1122 if (image->debug != MagickFalse)
1123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1124 assert(source != (const Image *) NULL);
1125 assert(source->signature == MagickSignature);
1126 assert(affine != (AffineMatrix *) NULL);
1129 extent[1].x=(double) source->columns-1.0;
1131 extent[2].x=(double) source->columns-1.0;
1132 extent[2].y=(double) source->rows-1.0;
1134 extent[3].y=(double) source->rows-1.0;
1135 for (i=0; i < 4; i++)
1138 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1139 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1143 for (i=1; i < 4; i++)
1145 if (min.x > extent[i].x)
1147 if (min.y > extent[i].y)
1149 if (max.x < extent[i].x)
1151 if (max.y < extent[i].y)
1155 Affine transform image.
1157 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1158 return(MagickFalse);
1160 edge.x1=MagickMax(min.x,0.0);
1161 edge.y1=MagickMax(min.y,0.0);
1162 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1163 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1164 inverse_affine=InverseAffineMatrix(affine);
1165 GetPixelInfo(image,&zero);
1166 start=(ssize_t) ceil(edge.y1-0.5);
1167 stop=(ssize_t) ceil(edge.y2-0.5);
1168 height=(size_t) (floor(edge.y2+0.5)-ceil(edge.y1-0.5));
1169 width=(size_t) (floor(edge.x2+0.5)-ceil(edge.x1-0.5));
1170 source_view=AcquireVirtualCacheView(source,exception);
1171 image_view=AcquireAuthenticCacheView(image,exception);
1172 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1173 #pragma omp parallel for schedule(static) shared(status) \
1174 dynamic_number_threads(image,width,height,1)
1176 for (y=start; y <= stop; y++)
1197 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1198 if (inverse_edge.x2 < inverse_edge.x1)
1200 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1201 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
1202 inverse_edge.x1+0.5)+1),1,exception);
1203 if (q == (Quantum *) NULL)
1208 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1210 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1212 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1214 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1215 point.x,point.y,&pixel,exception);
1216 GetPixelInfoPixel(image,q,&composite);
1217 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1219 SetPixelInfoPixel(image,&composite,q);
1221 q+=GetPixelChannels(image);
1223 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1226 source_view=DestroyCacheView(source_view);
1227 image_view=DestroyCacheView(image_view);
1232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1236 + D r a w B o u n d i n g R e c t a n g l e s %
1240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1242 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1243 % is only useful for developers debugging the rendering algorithm.
1245 % The format of the DrawBoundingRectangles method is:
1247 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1248 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1250 % A description of each parameter follows:
1252 % o image: the image.
1254 % o draw_info: the draw info.
1256 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1258 % o exception: return any errors or warnings in this structure.
1261 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1262 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1287 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1288 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1290 resolution.x=DefaultResolution;
1291 resolution.y=DefaultResolution;
1292 if (clone_info->density != (char *) NULL)
1300 flags=ParseGeometry(clone_info->density,&geometry_info);
1301 resolution.x=geometry_info.rho;
1302 resolution.y=geometry_info.sigma;
1303 if ((flags & SigmaValue) == MagickFalse)
1304 resolution.y=resolution.x;
1306 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1307 clone_info->stroke_width/2.0;
1312 if (polygon_info != (PolygonInfo *) NULL)
1314 bounds=polygon_info->edges[0].bounds;
1315 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1317 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1318 bounds.x1=polygon_info->edges[i].bounds.x1;
1319 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1320 bounds.y1=polygon_info->edges[i].bounds.y1;
1321 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1322 bounds.x2=polygon_info->edges[i].bounds.x2;
1323 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1324 bounds.y2=polygon_info->edges[i].bounds.y2;
1327 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1328 image->columns ? (double) image->columns-1 : bounds.x1;
1330 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1331 image->rows ? (double) image->rows-1 : bounds.y1;
1333 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1334 image->columns ? (double) image->columns-1 : bounds.x2;
1336 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1337 image->rows ? (double) image->rows-1 : bounds.y2;
1338 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1340 if (polygon_info->edges[i].direction != 0)
1341 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1344 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1346 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1347 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1348 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1349 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1350 primitive_info[0].primitive=RectanglePrimitive;
1351 TraceRectangle(primitive_info,start,end);
1352 primitive_info[0].method=ReplaceMethod;
1353 coordinates=(ssize_t) primitive_info[0].coordinates;
1354 primitive_info[coordinates].primitive=UndefinedPrimitive;
1355 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1358 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1360 start.x=(double) (bounds.x1-mid);
1361 start.y=(double) (bounds.y1-mid);
1362 end.x=(double) (bounds.x2+mid);
1363 end.y=(double) (bounds.y2+mid);
1364 primitive_info[0].primitive=RectanglePrimitive;
1365 TraceRectangle(primitive_info,start,end);
1366 primitive_info[0].method=ReplaceMethod;
1367 coordinates=(ssize_t) primitive_info[0].coordinates;
1368 primitive_info[coordinates].primitive=UndefinedPrimitive;
1369 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1370 clone_info=DestroyDrawInfo(clone_info);
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378 % D r a w C l i p P a t h %
1382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 % DrawClipPath() draws the clip path on the image mask.
1386 % The format of the DrawClipPath method is:
1388 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1389 % const char *name,ExceptionInfo *exception)
1391 % A description of each parameter follows:
1393 % o image: the image.
1395 % o draw_info: the draw info.
1397 % o name: the name of the clip path.
1399 % o exception: return any errors or warnings in this structure.
1402 MagickExport MagickBooleanType DrawClipPath(Image *image,
1403 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1406 filename[MaxTextExtent];
1420 assert(image != (Image *) NULL);
1421 assert(image->signature == MagickSignature);
1422 if (image->debug != MagickFalse)
1423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1424 assert(draw_info != (const DrawInfo *) NULL);
1425 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1426 value=GetImageArtifact(image,filename);
1427 if (value == (const char *) NULL)
1428 return(MagickFalse);
1429 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1430 if (clip_mask == (Image *) NULL)
1431 return(MagickFalse);
1432 (void) QueryColorCompliance("#0000",AllCompliance,
1433 &clip_mask->background_color,exception);
1434 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1435 (void) SetImageBackgroundColor(clip_mask,exception);
1436 if (image->debug != MagickFalse)
1437 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1438 draw_info->clip_mask);
1439 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1440 (void) CloneString(&clone_info->primitive,value);
1441 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1443 clone_info->clip_mask=(char *) NULL;
1444 status=NegateImage(clip_mask,MagickFalse,exception);
1445 (void) SetImageMask(image,clip_mask,exception);
1446 clip_mask=DestroyImage(clip_mask);
1447 clone_info=DestroyDrawInfo(clone_info);
1448 status=DrawImage(image,clone_info,exception);
1449 if (image->debug != MagickFalse)
1450 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1451 return(status != 0 ? MagickTrue : MagickFalse);
1455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459 + D r a w D a s h P o l y g o n %
1463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1466 % image while respecting the dash offset and dash pattern attributes.
1468 % The format of the DrawDashPolygon method is:
1470 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1471 % const PrimitiveInfo *primitive_info,Image *image,
1472 % ExceptionInfo *exception)
1474 % A description of each parameter follows:
1476 % o draw_info: the draw info.
1478 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1480 % o image: the image.
1482 % o exception: return any errors or warnings in this structure.
1485 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1486 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1507 register MagickRealType
1518 assert(draw_info != (const DrawInfo *) NULL);
1519 if (image->debug != MagickFalse)
1520 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1521 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1522 clone_info->miterlimit=0;
1523 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1524 number_vertices=(size_t) i;
1525 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1526 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1527 if (dash_polygon == (PrimitiveInfo *) NULL)
1528 return(MagickFalse);
1529 dash_polygon[0]=primitive_info[0];
1530 scale=ExpandAffine(&draw_info->affine);
1531 length=scale*(draw_info->dash_pattern[0]-0.5);
1532 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1534 for (n=0; offset > 0.0; j=0)
1536 if (draw_info->dash_pattern[n] <= 0.0)
1538 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1539 if (offset > length)
1543 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1546 if (offset < length)
1558 for (i=1; i < (ssize_t) number_vertices; i++)
1560 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1561 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1562 maximum_length=hypot((double) dx,dy);
1566 if (draw_info->dash_pattern[n] == 0.0)
1568 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1570 for (total_length=0.0; (total_length+length) <= maximum_length; )
1572 total_length+=length;
1573 if ((n & 0x01) != 0)
1575 dash_polygon[0]=primitive_info[0];
1576 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1577 total_length/maximum_length);
1578 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1579 total_length/maximum_length);
1584 if ((j+1) > (ssize_t) (2*number_vertices))
1586 dash_polygon[j]=primitive_info[i-1];
1587 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1588 total_length/maximum_length);
1589 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1590 total_length/maximum_length);
1591 dash_polygon[j].coordinates=1;
1593 dash_polygon[0].coordinates=(size_t) j;
1594 dash_polygon[j].primitive=UndefinedPrimitive;
1595 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1598 if (draw_info->dash_pattern[n] == 0.0)
1600 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1602 length-=(maximum_length-total_length);
1603 if ((n & 0x01) != 0)
1605 dash_polygon[j]=primitive_info[i];
1606 dash_polygon[j].coordinates=1;
1609 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1611 dash_polygon[j]=primitive_info[i-1];
1612 dash_polygon[j].point.x+=MagickEpsilon;
1613 dash_polygon[j].point.y+=MagickEpsilon;
1614 dash_polygon[j].coordinates=1;
1616 dash_polygon[0].coordinates=(size_t) j;
1617 dash_polygon[j].primitive=UndefinedPrimitive;
1618 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1620 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1621 clone_info=DestroyDrawInfo(clone_info);
1622 if (image->debug != MagickFalse)
1623 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1624 return(status != 0 ? MagickTrue : MagickFalse);
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632 % D r a w I m a g e %
1636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638 % DrawImage() draws a graphic primitive on your image. The primitive
1639 % may be represented as a string or filename. Precede the filename with an
1640 % "at" sign (@) and the contents of the file are drawn on the image. You
1641 % can affect how text is drawn by setting one or more members of the draw
1644 % The format of the DrawImage method is:
1646 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1647 % ExceptionInfo *exception)
1649 % A description of each parameter follows:
1651 % o image: the image.
1653 % o draw_info: the draw info.
1655 % o exception: return any errors or warnings in this structure.
1659 static inline MagickBooleanType IsPoint(const char *point)
1667 value=StringToDouble(point,&p);
1668 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1671 static inline void TracePoint(PrimitiveInfo *primitive_info,
1672 const PointInfo point)
1674 primitive_info->coordinates=1;
1675 primitive_info->point=point;
1678 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1679 ExceptionInfo *exception)
1681 #define RenderImageTag "Render/Image"
1688 key[2*MaxTextExtent],
1689 keyword[MaxTextExtent],
1690 geometry[MaxTextExtent],
1691 name[MaxTextExtent],
1692 pattern[MaxTextExtent],
1743 Ensure the annotation info is valid.
1745 assert(image != (Image *) NULL);
1746 assert(image->signature == MagickSignature);
1747 if (image->debug != MagickFalse)
1748 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1749 assert(draw_info != (DrawInfo *) NULL);
1750 assert(draw_info->signature == MagickSignature);
1751 if (image->debug != MagickFalse)
1752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1753 if ((draw_info->primitive == (char *) NULL) ||
1754 (*draw_info->primitive == '\0'))
1755 return(MagickFalse);
1756 if (image->debug != MagickFalse)
1757 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1758 if (*draw_info->primitive != '@')
1759 primitive=AcquireString(draw_info->primitive);
1761 primitive=FileToString(draw_info->primitive+1,~0,exception);
1762 if (primitive == (char *) NULL)
1763 return(MagickFalse);
1764 primitive_extent=(MagickRealType) strlen(primitive);
1765 (void) SetImageArtifact(image,"MVG",primitive);
1768 Allocate primitive info memory.
1770 graphic_context=(DrawInfo **) AcquireMagickMemory(
1771 sizeof(*graphic_context));
1772 if (graphic_context == (DrawInfo **) NULL)
1774 primitive=DestroyString(primitive);
1775 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1779 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1780 sizeof(*primitive_info));
1781 if (primitive_info == (PrimitiveInfo *) NULL)
1783 primitive=DestroyString(primitive);
1784 for ( ; n >= 0; n--)
1785 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1786 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1787 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1790 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1791 graphic_context[n]->viewbox=image->page;
1792 if ((image->page.width == 0) || (image->page.height == 0))
1794 graphic_context[n]->viewbox.width=image->columns;
1795 graphic_context[n]->viewbox.height=image->rows;
1797 token=AcquireString(primitive);
1798 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1800 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1801 return(MagickFalse);
1803 for (q=primitive; *q != '\0'; )
1806 Interpret graphic primitive.
1808 GetMagickToken(q,&q,keyword);
1809 if (*keyword == '\0')
1811 if (*keyword == '#')
1816 while ((*q != '\n') && (*q != '\0'))
1820 p=q-strlen(keyword)-1;
1821 primitive_type=UndefinedPrimitive;
1822 current=graphic_context[n]->affine;
1823 GetAffineMatrix(&affine);
1831 if (LocaleCompare("affine",keyword) == 0)
1833 GetMagickToken(q,&q,token);
1834 affine.sx=StringToDouble(token,(char **) NULL);
1835 GetMagickToken(q,&q,token);
1837 GetMagickToken(q,&q,token);
1838 affine.rx=StringToDouble(token,(char **) NULL);
1839 GetMagickToken(q,&q,token);
1841 GetMagickToken(q,&q,token);
1842 affine.ry=StringToDouble(token,(char **) NULL);
1843 GetMagickToken(q,&q,token);
1845 GetMagickToken(q,&q,token);
1846 affine.sy=StringToDouble(token,(char **) NULL);
1847 GetMagickToken(q,&q,token);
1849 GetMagickToken(q,&q,token);
1850 affine.tx=StringToDouble(token,(char **) NULL);
1851 GetMagickToken(q,&q,token);
1853 GetMagickToken(q,&q,token);
1854 affine.ty=StringToDouble(token,(char **) NULL);
1857 if (LocaleCompare("arc",keyword) == 0)
1859 primitive_type=ArcPrimitive;
1868 if (LocaleCompare("bezier",keyword) == 0)
1870 primitive_type=BezierPrimitive;
1873 if (LocaleCompare("border-color",keyword) == 0)
1875 GetMagickToken(q,&q,token);
1876 (void) QueryColorCompliance(token,AllCompliance,
1877 &graphic_context[n]->border_color,exception);
1886 if (LocaleCompare("clip-path",keyword) == 0)
1891 GetMagickToken(q,&q,token);
1892 (void) CloneString(&graphic_context[n]->clip_mask,token);
1893 (void) DrawClipPath(image,graphic_context[n],
1894 graphic_context[n]->clip_mask,exception);
1897 if (LocaleCompare("clip-rule",keyword) == 0)
1902 GetMagickToken(q,&q,token);
1903 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1905 if (fill_rule == -1)
1908 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1911 if (LocaleCompare("clip-units",keyword) == 0)
1916 GetMagickToken(q,&q,token);
1917 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1919 if (clip_units == -1)
1924 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1925 if (clip_units == ObjectBoundingBox)
1927 GetAffineMatrix(¤t);
1928 affine.sx=draw_info->bounds.x2;
1929 affine.sy=draw_info->bounds.y2;
1930 affine.tx=draw_info->bounds.x1;
1931 affine.ty=draw_info->bounds.y1;
1936 if (LocaleCompare("circle",keyword) == 0)
1938 primitive_type=CirclePrimitive;
1941 if (LocaleCompare("color",keyword) == 0)
1943 primitive_type=ColorPrimitive;
1952 if (LocaleCompare("decorate",keyword) == 0)
1957 GetMagickToken(q,&q,token);
1958 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1963 graphic_context[n]->decorate=(DecorationType) decorate;
1972 if (LocaleCompare("ellipse",keyword) == 0)
1974 primitive_type=EllipsePrimitive;
1977 if (LocaleCompare("encoding",keyword) == 0)
1979 GetMagickToken(q,&q,token);
1980 (void) CloneString(&graphic_context[n]->encoding,token);
1989 if (LocaleCompare("fill",keyword) == 0)
1991 GetMagickToken(q,&q,token);
1992 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1993 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1994 (void) DrawPatternPath(image,draw_info,token,
1995 &graphic_context[n]->fill_pattern,exception);
1998 status=QueryColorCompliance(token,AllCompliance,
1999 &graphic_context[n]->fill,exception);
2000 if (status == MagickFalse)
2005 pattern_info=AcquireImageInfo();
2006 (void) CopyMagickString(pattern_info->filename,token,
2008 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2010 CatchException(exception);
2011 pattern_info=DestroyImageInfo(pattern_info);
2016 if (LocaleCompare("fill-alpha",keyword) == 0)
2018 GetMagickToken(q,&q,token);
2019 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2020 graphic_context[n]->fill.alpha=(MagickRealType) QuantumRange*
2021 factor*StringToDouble(token,(char **) NULL);
2024 if (LocaleCompare("fill-rule",keyword) == 0)
2029 GetMagickToken(q,&q,token);
2030 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2032 if (fill_rule == -1)
2035 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2038 if (LocaleCompare("font",keyword) == 0)
2040 GetMagickToken(q,&q,token);
2041 (void) CloneString(&graphic_context[n]->font,token);
2042 if (LocaleCompare("none",token) == 0)
2043 graphic_context[n]->font=(char *)
2044 RelinquishMagickMemory(graphic_context[n]->font);
2047 if (LocaleCompare("font-family",keyword) == 0)
2049 GetMagickToken(q,&q,token);
2050 (void) CloneString(&graphic_context[n]->family,token);
2053 if (LocaleCompare("font-size",keyword) == 0)
2055 GetMagickToken(q,&q,token);
2056 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2059 if (LocaleCompare("font-stretch",keyword) == 0)
2064 GetMagickToken(q,&q,token);
2065 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2069 graphic_context[n]->stretch=(StretchType) stretch;
2072 if (LocaleCompare("font-style",keyword) == 0)
2077 GetMagickToken(q,&q,token);
2078 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2082 graphic_context[n]->style=(StyleType) style;
2085 if (LocaleCompare("font-weight",keyword) == 0)
2087 GetMagickToken(q,&q,token);
2088 graphic_context[n]->weight=StringToUnsignedLong(token);
2089 if (LocaleCompare(token,"all") == 0)
2090 graphic_context[n]->weight=0;
2091 if (LocaleCompare(token,"bold") == 0)
2092 graphic_context[n]->weight=700;
2093 if (LocaleCompare(token,"bolder") == 0)
2094 if (graphic_context[n]->weight <= 800)
2095 graphic_context[n]->weight+=100;
2096 if (LocaleCompare(token,"lighter") == 0)
2097 if (graphic_context[n]->weight >= 100)
2098 graphic_context[n]->weight-=100;
2099 if (LocaleCompare(token,"normal") == 0)
2100 graphic_context[n]->weight=400;
2109 if (LocaleCompare("gradient-units",keyword) == 0)
2111 GetMagickToken(q,&q,token);
2114 if (LocaleCompare("gravity",keyword) == 0)
2119 GetMagickToken(q,&q,token);
2120 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2124 graphic_context[n]->gravity=(GravityType) gravity;
2133 if (LocaleCompare("image",keyword) == 0)
2138 primitive_type=ImagePrimitive;
2139 GetMagickToken(q,&q,token);
2140 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2144 graphic_context[n]->compose=(CompositeOperator) compose;
2147 if (LocaleCompare("interline-spacing",keyword) == 0)
2149 GetMagickToken(q,&q,token);
2150 graphic_context[n]->interline_spacing=StringToDouble(token,
2154 if (LocaleCompare("interword-spacing",keyword) == 0)
2156 GetMagickToken(q,&q,token);
2157 graphic_context[n]->interword_spacing=StringToDouble(token,
2167 if (LocaleCompare("kerning",keyword) == 0)
2169 GetMagickToken(q,&q,token);
2170 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2179 if (LocaleCompare("line",keyword) == 0)
2180 primitive_type=LinePrimitive;
2188 if (LocaleCompare("matte",keyword) == 0)
2189 primitive_type=MattePrimitive;
2197 if (LocaleCompare("offset",keyword) == 0)
2199 GetMagickToken(q,&q,token);
2202 if (LocaleCompare("opacity",keyword) == 0)
2204 GetMagickToken(q,&q,token);
2205 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2206 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2207 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
2208 factor*StringToDouble(token,(char **) NULL))));
2209 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2210 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2219 if (LocaleCompare("path",keyword) == 0)
2221 primitive_type=PathPrimitive;
2224 if (LocaleCompare("point",keyword) == 0)
2226 primitive_type=PointPrimitive;
2229 if (LocaleCompare("polyline",keyword) == 0)
2231 primitive_type=PolylinePrimitive;
2234 if (LocaleCompare("polygon",keyword) == 0)
2236 primitive_type=PolygonPrimitive;
2239 if (LocaleCompare("pop",keyword) == 0)
2241 GetMagickToken(q,&q,token);
2242 if (LocaleCompare("clip-path",token) == 0)
2244 if (LocaleCompare("defs",token) == 0)
2246 if (LocaleCompare("gradient",token) == 0)
2248 if (LocaleCompare("graphic-context",token) == 0)
2252 (void) ThrowMagickException(exception,GetMagickModule(),
2253 DrawError,"UnbalancedGraphicContextPushPop","'%s'",token);
2257 if (graphic_context[n]->clip_mask != (char *) NULL)
2258 if (LocaleCompare(graphic_context[n]->clip_mask,
2259 graphic_context[n-1]->clip_mask) != 0)
2260 (void) SetImageMask(image,(Image *) NULL,exception);
2261 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2265 if (LocaleCompare("pattern",token) == 0)
2270 if (LocaleCompare("push",keyword) == 0)
2272 GetMagickToken(q,&q,token);
2273 if (LocaleCompare("clip-path",token) == 0)
2276 name[MaxTextExtent];
2278 GetMagickToken(q,&q,token);
2279 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2280 for (p=q; *q != '\0'; )
2282 GetMagickToken(q,&q,token);
2283 if (LocaleCompare(token,"pop") != 0)
2285 GetMagickToken(q,(const char **) NULL,token);
2286 if (LocaleCompare(token,"clip-path") != 0)
2290 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2291 (void) SetImageArtifact(image,name,token);
2292 GetMagickToken(q,&q,token);
2295 if (LocaleCompare("gradient",token) == 0)
2298 key[2*MaxTextExtent],
2299 name[MaxTextExtent],
2300 type[MaxTextExtent];
2305 GetMagickToken(q,&q,token);
2306 (void) CopyMagickString(name,token,MaxTextExtent);
2307 GetMagickToken(q,&q,token);
2308 (void) CopyMagickString(type,token,MaxTextExtent);
2309 GetMagickToken(q,&q,token);
2310 segment.x1=StringToDouble(token,(char **) NULL);
2311 GetMagickToken(q,&q,token);
2313 GetMagickToken(q,&q,token);
2314 segment.y1=StringToDouble(token,(char **) NULL);
2315 GetMagickToken(q,&q,token);
2317 GetMagickToken(q,&q,token);
2318 segment.x2=StringToDouble(token,(char **) NULL);
2319 GetMagickToken(q,&q,token);
2321 GetMagickToken(q,&q,token);
2322 segment.y2=StringToDouble(token,(char **) NULL);
2323 if (LocaleCompare(type,"radial") == 0)
2325 GetMagickToken(q,&q,token);
2327 GetMagickToken(q,&q,token);
2329 for (p=q; *q != '\0'; )
2331 GetMagickToken(q,&q,token);
2332 if (LocaleCompare(token,"pop") != 0)
2334 GetMagickToken(q,(const char **) NULL,token);
2335 if (LocaleCompare(token,"gradient") != 0)
2339 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2340 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2341 graphic_context[n]->affine.ry*segment.y1+
2342 graphic_context[n]->affine.tx;
2343 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2344 graphic_context[n]->affine.sy*segment.y1+
2345 graphic_context[n]->affine.ty;
2346 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2347 graphic_context[n]->affine.ry*segment.y2+
2348 graphic_context[n]->affine.tx;
2349 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2350 graphic_context[n]->affine.sy*segment.y2+
2351 graphic_context[n]->affine.ty;
2352 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2353 (void) SetImageArtifact(image,key,token);
2354 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2355 (void) FormatLocaleString(geometry,MaxTextExtent,
2356 "%gx%g%+.15g%+.15g",
2357 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2358 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2359 bounds.x1,bounds.y1);
2360 (void) SetImageArtifact(image,key,geometry);
2361 GetMagickToken(q,&q,token);
2364 if (LocaleCompare("pattern",token) == 0)
2369 GetMagickToken(q,&q,token);
2370 (void) CopyMagickString(name,token,MaxTextExtent);
2371 GetMagickToken(q,&q,token);
2372 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2374 GetMagickToken(q,&q,token);
2376 GetMagickToken(q,&q,token);
2377 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2379 GetMagickToken(q,&q,token);
2381 GetMagickToken(q,&q,token);
2382 bounds.width=(size_t) floor(StringToDouble(token,
2383 (char **) NULL)+0.5);
2384 GetMagickToken(q,&q,token);
2386 GetMagickToken(q,&q,token);
2387 bounds.height=(size_t) floor(StringToDouble(token,
2388 (char **) NULL)+0.5);
2389 for (p=q; *q != '\0'; )
2391 GetMagickToken(q,&q,token);
2392 if (LocaleCompare(token,"pop") != 0)
2394 GetMagickToken(q,(const char **) NULL,token);
2395 if (LocaleCompare(token,"pattern") != 0)
2399 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2400 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2401 (void) SetImageArtifact(image,key,token);
2402 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2403 (void) FormatLocaleString(geometry,MaxTextExtent,
2404 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2405 bounds.height,(double) bounds.x,(double) bounds.y);
2406 (void) SetImageArtifact(image,key,geometry);
2407 GetMagickToken(q,&q,token);
2410 if (LocaleCompare("graphic-context",token) == 0)
2413 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2414 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2415 if (graphic_context == (DrawInfo **) NULL)
2417 (void) ThrowMagickException(exception,GetMagickModule(),
2418 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2422 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2423 graphic_context[n-1]);
2426 if (LocaleCompare("defs",token) == 0)
2437 if (LocaleCompare("rectangle",keyword) == 0)
2439 primitive_type=RectanglePrimitive;
2442 if (LocaleCompare("rotate",keyword) == 0)
2444 GetMagickToken(q,&q,token);
2445 angle=StringToDouble(token,(char **) NULL);
2446 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2447 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2448 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2449 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2452 if (LocaleCompare("roundRectangle",keyword) == 0)
2454 primitive_type=RoundRectanglePrimitive;
2463 if (LocaleCompare("scale",keyword) == 0)
2465 GetMagickToken(q,&q,token);
2466 affine.sx=StringToDouble(token,(char **) NULL);
2467 GetMagickToken(q,&q,token);
2469 GetMagickToken(q,&q,token);
2470 affine.sy=StringToDouble(token,(char **) NULL);
2473 if (LocaleCompare("skewX",keyword) == 0)
2475 GetMagickToken(q,&q,token);
2476 angle=StringToDouble(token,(char **) NULL);
2477 affine.ry=sin(DegreesToRadians(angle));
2480 if (LocaleCompare("skewY",keyword) == 0)
2482 GetMagickToken(q,&q,token);
2483 angle=StringToDouble(token,(char **) NULL);
2484 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2487 if (LocaleCompare("stop-color",keyword) == 0)
2492 GetMagickToken(q,&q,token);
2493 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2495 (void) GradientImage(image,LinearGradient,ReflectSpread,
2496 &start_color,&stop_color,exception);
2497 start_color=stop_color;
2498 GetMagickToken(q,&q,token);
2501 if (LocaleCompare("stroke",keyword) == 0)
2503 GetMagickToken(q,&q,token);
2504 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2505 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2506 (void) DrawPatternPath(image,draw_info,token,
2507 &graphic_context[n]->stroke_pattern,exception);
2510 status=QueryColorCompliance(token,AllCompliance,
2511 &graphic_context[n]->stroke,exception);
2512 if (status == MagickFalse)
2517 pattern_info=AcquireImageInfo();
2518 (void) CopyMagickString(pattern_info->filename,token,
2520 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2522 CatchException(exception);
2523 pattern_info=DestroyImageInfo(pattern_info);
2528 if (LocaleCompare("stroke-antialias",keyword) == 0)
2530 GetMagickToken(q,&q,token);
2531 graphic_context[n]->stroke_antialias=
2532 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2535 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2537 if (graphic_context[n]->dash_pattern != (double *) NULL)
2538 graphic_context[n]->dash_pattern=(double *)
2539 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2540 if (IsPoint(q) != MagickFalse)
2546 GetMagickToken(p,&p,token);
2548 GetMagickToken(p,&p,token);
2549 for (x=0; IsPoint(token) != MagickFalse; x++)
2551 GetMagickToken(p,&p,token);
2553 GetMagickToken(p,&p,token);
2555 graphic_context[n]->dash_pattern=(double *)
2556 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2557 sizeof(*graphic_context[n]->dash_pattern));
2558 if (graphic_context[n]->dash_pattern == (double *) NULL)
2560 (void) ThrowMagickException(exception,GetMagickModule(),
2561 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2565 for (j=0; j < x; j++)
2567 GetMagickToken(q,&q,token);
2569 GetMagickToken(q,&q,token);
2570 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2573 if ((x & 0x01) != 0)
2574 for ( ; j < (2*x); j++)
2575 graphic_context[n]->dash_pattern[j]=
2576 graphic_context[n]->dash_pattern[j-x];
2577 graphic_context[n]->dash_pattern[j]=0.0;
2580 GetMagickToken(q,&q,token);
2583 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2585 GetMagickToken(q,&q,token);
2586 graphic_context[n]->dash_offset=StringToDouble(token,
2590 if (LocaleCompare("stroke-linecap",keyword) == 0)
2595 GetMagickToken(q,&q,token);
2596 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2600 graphic_context[n]->linecap=(LineCap) linecap;
2603 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2608 GetMagickToken(q,&q,token);
2609 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2614 graphic_context[n]->linejoin=(LineJoin) linejoin;
2617 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2619 GetMagickToken(q,&q,token);
2620 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2623 if (LocaleCompare("stroke-opacity",keyword) == 0)
2625 GetMagickToken(q,&q,token);
2626 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2627 graphic_context[n]->stroke.alpha=(MagickRealType) QuantumRange*
2628 factor*StringToDouble(token,(char **) NULL);
2631 if (LocaleCompare("stroke-width",keyword) == 0)
2633 GetMagickToken(q,&q,token);
2634 graphic_context[n]->stroke_width=StringToDouble(token,
2644 if (LocaleCompare("text",keyword) == 0)
2646 primitive_type=TextPrimitive;
2649 if (LocaleCompare("text-align",keyword) == 0)
2654 GetMagickToken(q,&q,token);
2655 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2659 graphic_context[n]->align=(AlignType) align;
2662 if (LocaleCompare("text-anchor",keyword) == 0)
2667 GetMagickToken(q,&q,token);
2668 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2672 graphic_context[n]->align=(AlignType) align;
2675 if (LocaleCompare("text-antialias",keyword) == 0)
2677 GetMagickToken(q,&q,token);
2678 graphic_context[n]->text_antialias=
2679 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2682 if (LocaleCompare("text-undercolor",keyword) == 0)
2684 GetMagickToken(q,&q,token);
2685 (void) QueryColorCompliance(token,AllCompliance,
2686 &graphic_context[n]->undercolor,exception);
2689 if (LocaleCompare("translate",keyword) == 0)
2691 GetMagickToken(q,&q,token);
2692 affine.tx=StringToDouble(token,(char **) NULL);
2693 GetMagickToken(q,&q,token);
2695 GetMagickToken(q,&q,token);
2696 affine.ty=StringToDouble(token,(char **) NULL);
2705 if (LocaleCompare("viewbox",keyword) == 0)
2707 GetMagickToken(q,&q,token);
2708 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2709 (char **) NULL)-0.5);
2710 GetMagickToken(q,&q,token);
2712 GetMagickToken(q,&q,token);
2713 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2714 (char **) NULL)-0.5);
2715 GetMagickToken(q,&q,token);
2717 GetMagickToken(q,&q,token);
2718 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2719 token,(char **) NULL)+0.5);
2720 GetMagickToken(q,&q,token);
2722 GetMagickToken(q,&q,token);
2723 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2724 token,(char **) NULL)+0.5);
2736 if (status == MagickFalse)
2738 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2739 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2741 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2742 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2743 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2744 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2745 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2747 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2750 if (primitive_type == UndefinedPrimitive)
2752 if (image->debug != MagickFalse)
2753 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2758 Parse the primitive attributes.
2762 primitive_info[0].point.x=0.0;
2763 primitive_info[0].point.y=0.0;
2764 for (x=0; *q != '\0'; x++)
2769 if (IsPoint(q) == MagickFalse)
2771 GetMagickToken(q,&q,token);
2772 point.x=StringToDouble(token,(char **) NULL);
2773 GetMagickToken(q,&q,token);
2775 GetMagickToken(q,&q,token);
2776 point.y=StringToDouble(token,(char **) NULL);
2777 GetMagickToken(q,(const char **) NULL,token);
2779 GetMagickToken(q,&q,token);
2780 primitive_info[i].primitive=primitive_type;
2781 primitive_info[i].point=point;
2782 primitive_info[i].coordinates=0;
2783 primitive_info[i].method=FloodfillMethod;
2785 if (i < (ssize_t) number_points)
2788 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2789 (size_t) number_points,sizeof(*primitive_info));
2790 if (primitive_info == (PrimitiveInfo *) NULL)
2792 (void) ThrowMagickException(exception,GetMagickModule(),
2793 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
2797 primitive_info[j].primitive=primitive_type;
2798 primitive_info[j].coordinates=(size_t) x;
2799 primitive_info[j].method=FloodfillMethod;
2800 primitive_info[j].text=(char *) NULL;
2802 Circumscribe primitive within a circle.
2804 bounds.x1=primitive_info[j].point.x;
2805 bounds.y1=primitive_info[j].point.y;
2806 bounds.x2=primitive_info[j].point.x;
2807 bounds.y2=primitive_info[j].point.y;
2808 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2810 point=primitive_info[j+k].point;
2811 if (point.x < bounds.x1)
2813 if (point.y < bounds.y1)
2815 if (point.x > bounds.x2)
2817 if (point.y > bounds.y2)
2821 Speculate how many points our primitive might consume.
2823 length=primitive_info[j].coordinates;
2824 switch (primitive_type)
2826 case RectanglePrimitive:
2831 case RoundRectanglePrimitive:
2833 length*=5+8*BezierQuantum;
2836 case BezierPrimitive:
2838 if (primitive_info[j].coordinates > 107)
2839 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2840 "TooManyBezierCoordinates","'%s'",token);
2841 length=BezierQuantum*primitive_info[j].coordinates;
2850 GetMagickToken(q,&q,token);
2853 for (s=token; *s != '\0'; s=t)
2858 value=StringToDouble(s,&t);
2867 length=length*BezierQuantum/2;
2870 case CirclePrimitive:
2872 case EllipsePrimitive:
2879 alpha=bounds.x2-bounds.x1;
2880 beta=bounds.y2-bounds.y1;
2881 radius=hypot((double) alpha,(double) beta);
2882 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2888 if ((size_t) (i+length) >= number_points)
2891 Resize based on speculative points required by primitive.
2893 number_points+=length+1;
2894 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2895 (size_t) number_points,sizeof(*primitive_info));
2896 if (primitive_info == (PrimitiveInfo *) NULL)
2898 (void) ThrowMagickException(exception,GetMagickModule(),
2899 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2904 switch (primitive_type)
2906 case PointPrimitive:
2909 if (primitive_info[j].coordinates != 1)
2914 TracePoint(primitive_info+j,primitive_info[j].point);
2915 i=(ssize_t) (j+primitive_info[j].coordinates);
2920 if (primitive_info[j].coordinates != 2)
2925 TraceLine(primitive_info+j,primitive_info[j].point,
2926 primitive_info[j+1].point);
2927 i=(ssize_t) (j+primitive_info[j].coordinates);
2930 case RectanglePrimitive:
2932 if (primitive_info[j].coordinates != 2)
2937 TraceRectangle(primitive_info+j,primitive_info[j].point,
2938 primitive_info[j+1].point);
2939 i=(ssize_t) (j+primitive_info[j].coordinates);
2942 case RoundRectanglePrimitive:
2944 if (primitive_info[j].coordinates != 3)
2949 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2950 primitive_info[j+1].point,primitive_info[j+2].point);
2951 i=(ssize_t) (j+primitive_info[j].coordinates);
2956 if (primitive_info[j].coordinates != 3)
2958 primitive_type=UndefinedPrimitive;
2961 TraceArc(primitive_info+j,primitive_info[j].point,
2962 primitive_info[j+1].point,primitive_info[j+2].point);
2963 i=(ssize_t) (j+primitive_info[j].coordinates);
2966 case EllipsePrimitive:
2968 if (primitive_info[j].coordinates != 3)
2973 TraceEllipse(primitive_info+j,primitive_info[j].point,
2974 primitive_info[j+1].point,primitive_info[j+2].point);
2975 i=(ssize_t) (j+primitive_info[j].coordinates);
2978 case CirclePrimitive:
2980 if (primitive_info[j].coordinates != 2)
2985 TraceCircle(primitive_info+j,primitive_info[j].point,
2986 primitive_info[j+1].point);
2987 i=(ssize_t) (j+primitive_info[j].coordinates);
2990 case PolylinePrimitive:
2992 case PolygonPrimitive:
2994 primitive_info[i]=primitive_info[j];
2995 primitive_info[i].coordinates=0;
2996 primitive_info[j].coordinates++;
3000 case BezierPrimitive:
3002 if (primitive_info[j].coordinates < 3)
3007 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3008 i=(ssize_t) (j+primitive_info[j].coordinates);
3013 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3016 case ColorPrimitive:
3017 case MattePrimitive:
3022 if (primitive_info[j].coordinates != 1)
3027 GetMagickToken(q,&q,token);
3028 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3032 primitive_info[j].method=(PaintMethod) method;
3037 if (primitive_info[j].coordinates != 1)
3043 GetMagickToken(q,&q,token);
3044 primitive_info[j].text=AcquireString(token);
3047 case ImagePrimitive:
3049 if (primitive_info[j].coordinates != 2)
3054 GetMagickToken(q,&q,token);
3055 primitive_info[j].text=AcquireString(token);
3059 if (primitive_info == (PrimitiveInfo *) NULL)
3061 if (image->debug != MagickFalse)
3062 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3063 if (status == MagickFalse)
3065 primitive_info[i].primitive=UndefinedPrimitive;
3071 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3073 point=primitive_info[i].point;
3074 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3075 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3076 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3077 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3078 point=primitive_info[i].point;
3079 if (point.x < graphic_context[n]->bounds.x1)
3080 graphic_context[n]->bounds.x1=point.x;
3081 if (point.y < graphic_context[n]->bounds.y1)
3082 graphic_context[n]->bounds.y1=point.y;
3083 if (point.x > graphic_context[n]->bounds.x2)
3084 graphic_context[n]->bounds.x2=point.x;
3085 if (point.y > graphic_context[n]->bounds.y2)
3086 graphic_context[n]->bounds.y2=point.y;
3087 if (primitive_info[i].primitive == ImagePrimitive)
3089 if (i >= (ssize_t) number_points)
3090 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3092 if (graphic_context[n]->render != MagickFalse)
3094 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3095 (LocaleCompare(graphic_context[n]->clip_mask,
3096 graphic_context[n-1]->clip_mask) != 0))
3097 (void) DrawClipPath(image,graphic_context[n],
3098 graphic_context[n]->clip_mask,exception);
3099 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3101 if (primitive_info->text != (char *) NULL)
3102 primitive_info->text=(char *) RelinquishMagickMemory(
3103 primitive_info->text);
3104 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3106 if (proceed == MagickFalse)
3109 if (image->debug != MagickFalse)
3110 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3112 Relinquish resources.
3114 token=DestroyString(token);
3115 if (primitive_info != (PrimitiveInfo *) NULL)
3116 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3117 primitive=DestroyString(primitive);
3118 for ( ; n >= 0; n--)
3119 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3120 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3121 if (status == MagickFalse)
3122 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3132 % D r a w G r a d i e n t I m a g e %
3136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3138 % DrawGradientImage() draws a linear gradient on the image.
3140 % The format of the DrawGradientImage method is:
3142 % MagickBooleanType DrawGradientImage(Image *image,
3143 % const DrawInfo *draw_info,ExceptionInfo *exception)
3145 % A description of each parameter follows:
3147 % o image: the image.
3149 % o draw_info: the draw info.
3151 % o exception: return any errors or warnings in this structure.
3155 static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
3156 const ssize_t x,const ssize_t y)
3158 switch (gradient->type)
3160 case UndefinedGradient:
3161 case LinearGradient:
3176 gradient_vector=(&gradient->gradient_vector);
3177 p.x=gradient_vector->x2-gradient_vector->x1;
3178 p.y=gradient_vector->y2-gradient_vector->y1;
3179 q.x=(double) x-gradient_vector->x1;
3180 q.y=(double) y-gradient_vector->y1;
3181 length=sqrt(q.x*q.x+q.y*q.y);
3182 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3183 gamma=MagickEpsilonReciprocal(gamma);
3184 scale=p.x*q.x+p.y*q.y;
3185 offset=gamma*scale*length;
3188 case RadialGradient:
3197 v.x=(double) x-gradient->center.x;
3198 v.y=(double) y-gradient->center.y;
3199 length=sqrt(v.x*v.x+v.y*v.y);
3200 if (gradient->spread == RepeatSpread)
3202 offset=length/gradient->radius;
3209 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3210 const DrawInfo *draw_info,ExceptionInfo *exception)
3244 Draw linear or radial gradient on image.
3246 assert(image != (Image *) NULL);
3247 assert(image->signature == MagickSignature);
3248 if (image->debug != MagickFalse)
3249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3250 assert(draw_info != (const DrawInfo *) NULL);
3251 gradient=(&draw_info->gradient);
3252 gradient_vector=(&gradient->gradient_vector);
3253 point.x=gradient_vector->x2-gradient_vector->x1;
3254 point.y=gradient_vector->y2-gradient_vector->y1;
3255 length=sqrt(point.x*point.x+point.y*point.y);
3256 bounding_box=gradient->bounding_box;
3258 GetPixelInfo(image,&zero);
3259 height=bounding_box.height-bounding_box.y;
3260 width=bounding_box.width-bounding_box.x;
3261 image_view=AcquireAuthenticCacheView(image,exception);
3262 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3263 #pragma omp parallel for schedule(static) shared(status) \
3264 dynamic_number_threads(image,width,height,1)
3266 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3286 if (status == MagickFalse)
3288 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3289 if (q == (Quantum *) NULL)
3296 offset=GetStopColorOffset(gradient,0,y);
3297 if (gradient->type != RadialGradient)
3299 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3301 GetPixelInfoPixel(image,q,&pixel);
3302 switch (gradient->spread)
3304 case UndefinedSpread:
3307 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3308 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3310 offset=GetStopColorOffset(gradient,x,y);
3311 if (gradient->type != RadialGradient)
3314 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3315 if (offset < gradient->stops[i].offset)
3317 if ((offset < 0.0) || (i == 0))
3318 composite=gradient->stops[0].color;
3320 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3321 composite=gradient->stops[gradient->number_stops-1].color;
3326 alpha=(offset-gradient->stops[i].offset)/
3327 (gradient->stops[j].offset-gradient->stops[i].offset);
3328 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3329 &gradient->stops[j].color,alpha,&composite);
3335 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3336 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3338 offset=GetStopColorOffset(gradient,x,y);
3339 if (gradient->type != RadialGradient)
3344 if ((ssize_t) fmod(offset,2.0) == 0)
3345 offset=fmod(offset,1.0);
3347 offset=1.0-fmod(offset,1.0);
3348 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3349 if (offset < gradient->stops[i].offset)
3352 composite=gradient->stops[0].color;
3354 if (i == (ssize_t) gradient->number_stops)
3355 composite=gradient->stops[gradient->number_stops-1].color;
3360 alpha=(offset-gradient->stops[i].offset)/
3361 (gradient->stops[j].offset-gradient->stops[i].offset);
3362 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3363 &gradient->stops[j].color,alpha,&composite);
3375 antialias=MagickFalse;
3377 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3378 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3380 offset=GetStopColorOffset(gradient,x,y);
3381 if (gradient->type == LinearGradient)
3383 repeat=fmod(offset,length);
3385 repeat=length-fmod(-repeat,length);
3387 repeat=fmod(offset,length);
3388 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3389 MagickTrue : MagickFalse;
3390 offset=repeat/length;
3394 repeat=fmod(offset,gradient->radius);
3396 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3398 repeat=fmod(offset,gradient->radius);
3399 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3401 offset=repeat/gradient->radius;
3404 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3405 if (offset < gradient->stops[i].offset)
3408 composite=gradient->stops[0].color;
3410 if (i == (ssize_t) gradient->number_stops)
3411 composite=gradient->stops[gradient->number_stops-1].color;
3416 alpha=(offset-gradient->stops[i].offset)/
3417 (gradient->stops[j].offset-gradient->stops[i].offset);
3418 if (antialias != MagickFalse)
3420 if (gradient->type == LinearGradient)
3421 alpha=length-repeat;
3423 alpha=gradient->radius-repeat;
3425 j=(ssize_t) gradient->number_stops-1L;
3427 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3428 &gradient->stops[j].color,alpha,&composite);
3433 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3435 SetPixelInfoPixel(image,&pixel,q);
3436 q+=GetPixelChannels(image);
3438 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3441 image_view=DestroyCacheView(image_view);
3446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3450 % D r a w P a t t e r n P a t h %
3454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3456 % DrawPatternPath() draws a pattern.
3458 % The format of the DrawPatternPath method is:
3460 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3461 % const char *name,Image **pattern,ExceptionInfo *exception)
3463 % A description of each parameter follows:
3465 % o image: the image.
3467 % o draw_info: the draw info.
3469 % o name: the pattern name.
3471 % o image: the image.
3473 % o exception: return any errors or warnings in this structure.
3476 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3477 const DrawInfo *draw_info,const char *name,Image **pattern,
3478 ExceptionInfo *exception)
3481 property[MaxTextExtent];
3496 assert(image != (Image *) NULL);
3497 assert(image->signature == MagickSignature);
3498 if (image->debug != MagickFalse)
3499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3500 assert(draw_info != (const DrawInfo *) NULL);
3501 assert(name != (const char *) NULL);
3502 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3503 path=GetImageArtifact(image,property);
3504 if (path == (const char *) NULL)
3505 return(MagickFalse);
3506 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3507 geometry=GetImageArtifact(image,property);
3508 if (geometry == (const char *) NULL)
3509 return(MagickFalse);
3510 if ((*pattern) != (Image *) NULL)
3511 *pattern=DestroyImage(*pattern);
3512 image_info=AcquireImageInfo();
3513 image_info->size=AcquireString(geometry);
3514 *pattern=AcquireImage(image_info,exception);
3515 image_info=DestroyImageInfo(image_info);
3516 (void) QueryColorCompliance("#00000000",AllCompliance,
3517 &(*pattern)->background_color,exception);
3518 (void) SetImageBackgroundColor(*pattern,exception);
3519 if (image->debug != MagickFalse)
3520 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3521 "begin pattern-path %s %s",name,geometry);
3522 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3523 clone_info->fill_pattern=NewImageList();
3524 clone_info->stroke_pattern=NewImageList();
3525 (void) CloneString(&clone_info->primitive,path);
3526 status=DrawImage(*pattern,clone_info,exception);
3527 clone_info=DestroyDrawInfo(clone_info);
3528 if (image->debug != MagickFalse)
3529 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3538 + D r a w P o l y g o n P r i m i t i v e %
3542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3544 % DrawPolygonPrimitive() draws a polygon on the image.
3546 % The format of the DrawPolygonPrimitive method is:
3548 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3549 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3550 % ExceptionInfo *exception)
3552 % A description of each parameter follows:
3554 % o image: the image.
3556 % o draw_info: the draw info.
3558 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3560 % o exception: return any errors or warnings in this structure.
3564 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3569 assert(polygon_info != (PolygonInfo **) NULL);
3570 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3571 if (polygon_info[i] != (PolygonInfo *) NULL)
3572 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3573 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3574 return(polygon_info);
3577 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3578 const PrimitiveInfo *primitive_info)
3581 *restrict path_info;
3592 number_threads=GetOpenMPMaximumThreads();
3593 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3594 sizeof(*polygon_info));
3595 if (polygon_info == (PolygonInfo **) NULL)
3596 return((PolygonInfo **) NULL);
3597 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3598 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3599 if (path_info == (PathInfo *) NULL)
3600 return(DestroyPolygonThreadSet(polygon_info));
3601 for (i=0; i < (ssize_t) number_threads; i++)
3603 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3604 if (polygon_info[i] == (PolygonInfo *) NULL)
3605 return(DestroyPolygonThreadSet(polygon_info));
3607 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3608 return(polygon_info);
3611 static MagickRealType GetFillAlpha(PolygonInfo *polygon_info,
3612 const MagickRealType mid,const MagickBooleanType fill,
3613 const FillRule fill_rule,const double x,const double y,
3614 MagickRealType *stroke_alpha)
3628 register const PointInfo
3639 Compute fill & stroke opacity for this (x,y) point.
3643 p=polygon_info->edges;
3644 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3646 if (y <= (p->bounds.y1-mid-0.5))
3648 if (y > (p->bounds.y2+mid+0.5))
3650 (void) DestroyEdge(polygon_info,(size_t) j);
3653 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3655 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3656 for ( ; i < (ssize_t) p->number_points; i++)
3658 if (y <= (p->points[i-1].y-mid-0.5))
3660 if (y > (p->points[i].y+mid+0.5))
3662 if (p->scanline != y)
3665 p->highwater=(size_t) i;
3668 Compute distance between a point and an edge.
3671 delta.x=(q+1)->x-q->x;
3672 delta.y=(q+1)->y-q->y;
3673 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3678 distance=delta.x*delta.x+delta.y*delta.y;
3682 alpha=delta.x*delta.x+delta.y*delta.y;
3687 distance=delta.x*delta.x+delta.y*delta.y;
3691 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3692 distance=MagickEpsilonReciprocal(alpha)*beta*beta;
3696 Compute stroke & subpath opacity.
3699 if (p->ghostline == MagickFalse)
3702 if ((*stroke_alpha < 1.0) &&
3703 (distance <= ((alpha+0.25)*(alpha+0.25))))
3706 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3711 if (distance != 1.0)
3712 beta=sqrt((double) distance);
3714 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3715 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3719 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3721 if (distance <= 0.0)
3731 if (distance != 1.0)
3732 beta=sqrt(distance);
3735 if (subpath_alpha < (alpha*alpha))
3736 subpath_alpha=alpha*alpha;
3740 Compute fill opacity.
3742 if (fill == MagickFalse)
3744 if (subpath_alpha >= 1.0)
3747 Determine winding number.
3750 p=polygon_info->edges;
3751 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3753 if (y <= p->bounds.y1)
3755 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3757 if (x > p->bounds.x2)
3759 winding_number+=p->direction ? 1 : -1;
3762 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3763 for ( ; i < (ssize_t) p->number_points; i++)
3764 if (y <= p->points[i].y)
3767 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3768 winding_number+=p->direction ? 1 : -1;
3770 if (fill_rule != NonZeroRule)
3772 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3776 if (MagickAbsoluteValue(winding_number) != 0)
3778 return(subpath_alpha);
3781 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3782 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3783 ExceptionInfo *exception)
3796 **restrict polygon_info;
3817 Compute bounding box.
3819 assert(image != (Image *) NULL);
3820 assert(image->signature == MagickSignature);
3821 if (image->debug != MagickFalse)
3822 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3823 assert(draw_info != (DrawInfo *) NULL);
3824 assert(draw_info->signature == MagickSignature);
3825 assert(primitive_info != (PrimitiveInfo *) NULL);
3826 if (primitive_info->coordinates == 0)
3828 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3829 if (polygon_info == (PolygonInfo **) NULL)
3830 return(MagickFalse);
3832 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3833 if (image->debug != MagickFalse)
3834 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3835 fill=(primitive_info->method == FillToBorderMethod) ||
3836 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3837 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3838 bounds=polygon_info[0]->edges[0].bounds;
3839 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3841 p=polygon_info[0]->edges+i;
3842 if (p->bounds.x1 < bounds.x1)
3843 bounds.x1=p->bounds.x1;
3844 if (p->bounds.y1 < bounds.y1)
3845 bounds.y1=p->bounds.y1;
3846 if (p->bounds.x2 > bounds.x2)
3847 bounds.x2=p->bounds.x2;
3848 if (p->bounds.y2 > bounds.y2)
3849 bounds.y2=p->bounds.y2;
3851 bounds.x1-=(mid+1.0);
3852 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3853 image->columns ? (double) image->columns-1.0 : bounds.x1;
3854 bounds.y1-=(mid+1.0);
3855 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3856 image->rows ? (double) image->rows-1.0 : bounds.y1;
3857 bounds.x2+=(mid+1.0);
3858 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3859 image->columns ? (double) image->columns-1.0 : bounds.x2;
3860 bounds.y2+=(mid+1.0);
3861 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3862 image->rows ? (double) image->rows-1.0 : bounds.y2;
3864 image_view=AcquireAuthenticCacheView(image,exception);
3865 height=(size_t) (floor(bounds.y2+0.5)-ceil(bounds.y1-0.5));
3866 width=(size_t) (floor(bounds.x2+0.5)-ceil(bounds.x1-0.5));
3867 if (primitive_info->coordinates == 1)
3872 start=(ssize_t) ceil(bounds.y1-0.5);
3873 stop=(ssize_t) floor(bounds.y2+0.5);
3874 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3875 #pragma omp parallel for schedule(static) shared(status) \
3876 dynamic_number_threads(image,width,height,1)
3878 for (y=start; y <= stop; y++)
3896 if (status == MagickFalse)
3898 start=(ssize_t) ceil(bounds.x1-0.5);
3899 stop=(ssize_t) floor(bounds.x2+0.5);
3901 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
3903 if (q == (Quantum *) NULL)
3908 GetPixelInfo(image,&pixel);
3909 for ( ; x <= stop; x++)
3911 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3912 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3914 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3915 SetPixelInfoPixel(image,&pixel,q);
3917 q+=GetPixelChannels(image);
3919 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3920 if (sync == MagickFalse)
3923 image_view=DestroyCacheView(image_view);
3924 polygon_info=DestroyPolygonThreadSet(polygon_info);
3925 if (image->debug != MagickFalse)
3926 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3927 " end draw-polygon");
3931 Draw polygon or line.
3933 if (image->matte == MagickFalse)
3934 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3935 start=(ssize_t) ceil(bounds.y1-0.5);
3936 stop=(ssize_t) floor(bounds.y2+0.5);
3937 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3938 #pragma omp parallel for schedule(static) shared(status) \
3939 dynamic_number_threads(image,width,height,1)
3941 for (y=start; y <= stop; y++)
3944 id = GetOpenMPThreadId();
3964 if (status == MagickFalse)
3966 start=(ssize_t) ceil(bounds.x1-0.5);
3967 stop=(ssize_t) floor(bounds.x2+0.5);
3968 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3970 if (q == (Quantum *) NULL)
3975 for (x=start; x <= stop; x++)
3980 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3981 (double) x,(double) y,&stroke_alpha);
3982 if (draw_info->stroke_antialias == MagickFalse)
3984 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3985 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3987 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3988 fill_alpha=fill_alpha*fill_color.alpha;
3989 CompositePixelOver(image,&fill_color,fill_alpha,q,(MagickRealType)
3990 GetPixelAlpha(image,q),q);
3991 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3992 stroke_alpha=stroke_alpha*stroke_color.alpha;
3993 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(MagickRealType)
3994 GetPixelAlpha(image,q),q);
3995 q+=GetPixelChannels(image);
3997 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4000 image_view=DestroyCacheView(image_view);
4001 polygon_info=DestroyPolygonThreadSet(polygon_info);
4002 if (image->debug != MagickFalse)
4003 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4012 % D r a w P r i m i t i v e %
4016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4018 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4020 % The format of the DrawPrimitive method is:
4022 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4023 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4025 % A description of each parameter follows:
4027 % o image: the image.
4029 % o draw_info: the draw info.
4031 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4033 % o exception: return any errors or warnings in this structure.
4037 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4063 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4064 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4065 switch (primitive_info->primitive)
4067 case PointPrimitive:
4069 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4070 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4071 methods[primitive_info->method]);
4074 case ColorPrimitive:
4076 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4077 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4078 methods[primitive_info->method]);
4081 case MattePrimitive:
4083 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4084 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4085 methods[primitive_info->method]);
4090 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4091 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4094 case ImagePrimitive:
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4097 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4104 p=primitive_info[0].point;
4107 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4109 point=primitive_info[i].point;
4110 if (coordinates <= 0)
4112 coordinates=(ssize_t) primitive_info[i].coordinates;
4113 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4114 " begin open (%.20g)",(double) coordinates);
4117 point=primitive_info[i].point;
4118 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4119 (fabs(q.y-point.y) >= MagickEpsilon))
4120 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4121 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4123 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4124 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4127 if (coordinates > 0)
4129 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4130 (fabs(p.y-point.y) >= MagickEpsilon))
4131 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4132 (double) coordinates);
4134 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4135 (double) coordinates);
4139 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4140 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4141 ExceptionInfo *exception)
4156 if (image->debug != MagickFalse)
4158 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4159 " begin draw-primitive");
4160 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4161 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4162 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4163 draw_info->affine.tx,draw_info->affine.ty);
4165 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4166 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4167 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4168 (void) SetImageColorspace(image,sRGBColorspace,exception);
4170 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4171 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4172 image_view=AcquireAuthenticCacheView(image,exception);
4173 switch (primitive_info->primitive)
4175 case PointPrimitive:
4183 if ((y < 0) || (y >= (ssize_t) image->rows))
4185 if ((x < 0) || (x >= (ssize_t) image->columns))
4187 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4188 if (q == (Quantum *) NULL)
4190 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4191 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4192 (MagickRealType) GetPixelAlpha(image,q),q);
4193 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4196 case ColorPrimitive:
4198 switch (primitive_info->method)
4209 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4210 if (q == (Quantum *) NULL)
4212 GetPixelInfo(image,&pixel);
4213 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4214 SetPixelInfoPixel(image,&pixel,q);
4215 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4227 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4229 for (y=0; y < (ssize_t) image->rows; y++)
4234 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4236 if (q == (Quantum *) NULL)
4238 for (x=0; x < (ssize_t) image->columns; x++)
4240 GetPixelInfoPixel(image,q,&pixel);
4241 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4243 q+=GetPixelChannels(image);
4246 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4247 SetPixelInfoPixel(image,&pixel,q);
4248 q+=GetPixelChannels(image);
4250 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4251 if (sync == MagickFalse)
4256 case FloodfillMethod:
4257 case FillToBorderMethod:
4262 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4264 if (primitive_info->method == FillToBorderMethod)
4266 target.red=(MagickRealType) draw_info->border_color.red;
4267 target.green=(MagickRealType) draw_info->border_color.green;
4268 target.blue=(MagickRealType) draw_info->border_color.blue;
4270 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4271 primitive_info->method == FloodfillMethod ? MagickFalse :
4272 MagickTrue,exception);
4283 GetPixelInfo(image,&pixel);
4284 for (y=0; y < (ssize_t) image->rows; y++)
4292 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4294 if (q == (Quantum *) NULL)
4296 for (x=0; x < (ssize_t) image->columns; x++)
4298 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4299 SetPixelInfoPixel(image,&pixel,q);
4300 q+=GetPixelChannels(image);
4302 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4303 if (sync == MagickFalse)
4311 case MattePrimitive:
4313 if (image->matte == MagickFalse)
4314 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4315 switch (primitive_info->method)
4326 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4327 if (q == (Quantum *) NULL)
4329 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4330 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4331 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4343 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4345 for (y=0; y < (ssize_t) image->rows; y++)
4353 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4355 if (q == (Quantum *) NULL)
4357 for (x=0; x < (ssize_t) image->columns; x++)
4359 GetPixelInfoPixel(image,q,&pixel);
4360 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4362 q+=GetPixelChannels(image);
4365 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4366 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4367 q+=GetPixelChannels(image);
4369 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4370 if (sync == MagickFalse)
4375 case FloodfillMethod:
4376 case FillToBorderMethod:
4384 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4386 if (primitive_info->method == FillToBorderMethod)
4388 target.red=(MagickRealType) draw_info->border_color.red;
4389 target.green=(MagickRealType) draw_info->border_color.green;
4390 target.blue=(MagickRealType) draw_info->border_color.blue;
4392 channel_mask=SetPixelChannelMask(image,AlphaChannel);
4393 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4394 primitive_info->method == FloodfillMethod ? MagickFalse :
4395 MagickTrue,exception);
4396 (void) SetPixelChannelMask(image,channel_mask);
4407 for (y=0; y < (ssize_t) image->rows; y++)
4415 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4417 if (q == (Quantum *) NULL)
4419 for (x=0; x < (ssize_t) image->columns; x++)
4421 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4422 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4423 q+=GetPixelChannels(image);
4425 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4426 if (sync == MagickFalse)
4437 geometry[MaxTextExtent];
4442 if (primitive_info->text == (char *) NULL)
4444 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4445 (void) CloneString(&clone_info->text,primitive_info->text);
4446 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4447 primitive_info->point.x,primitive_info->point.y);
4448 (void) CloneString(&clone_info->geometry,geometry);
4449 status=AnnotateImage(image,clone_info,exception);
4450 clone_info=DestroyDrawInfo(clone_info);
4453 case ImagePrimitive:
4459 composite_geometry[MaxTextExtent];
4474 if (primitive_info->text == (char *) NULL)
4476 clone_info=AcquireImageInfo();
4477 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4478 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4482 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4484 composite_image=ReadImage(clone_info,exception);
4486 clone_info=DestroyImageInfo(clone_info);
4487 if (composite_image == (Image *) NULL)
4489 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4490 NULL,(void *) NULL);
4491 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4492 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4493 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4494 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4497 geometry[MaxTextExtent];
4502 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4503 primitive_info[1].point.x,primitive_info[1].point.y);
4504 composite_image->filter=image->filter;
4505 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4508 if (composite_image->matte == MagickFalse)
4509 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4511 if (draw_info->alpha != OpaqueAlpha)
4512 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4513 SetGeometry(image,&geometry);
4514 image->gravity=draw_info->gravity;
4517 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4518 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4519 composite_image->rows,(double) geometry.x,(double) geometry.y);
4520 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4521 affine=draw_info->affine;
4522 affine.tx=(double) geometry.x;
4523 affine.ty=(double) geometry.y;
4524 composite_image->interpolate=image->interpolate;
4525 if (draw_info->compose == OverCompositeOp)
4526 (void) DrawAffineImage(image,composite_image,&affine,exception);
4528 (void) CompositeImage(image,composite_image,draw_info->compose,
4529 MagickTrue,geometry.x,geometry.y,exception);
4530 composite_image=DestroyImage(composite_image);
4542 if (IsEventLogging() != MagickFalse)
4543 LogPrimitiveInfo(primitive_info);
4544 scale=ExpandAffine(&draw_info->affine);
4545 if ((draw_info->dash_pattern != (double *) NULL) &&
4546 (draw_info->dash_pattern[0] != 0.0) &&
4547 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
4548 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4553 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4554 clone_info->stroke_width=0.0;
4555 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4556 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4558 clone_info=DestroyDrawInfo(clone_info);
4559 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4562 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4564 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4570 Draw strokes while respecting line cap/join attributes.
4572 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4574 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4575 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4576 MagickTrue : MagickFalse;
4577 i=(ssize_t) primitive_info[0].coordinates;
4578 if ((((draw_info->linecap == RoundCap) ||
4579 (closed_path != MagickFalse)) &&
4580 (draw_info->linejoin == RoundJoin)) ||
4581 (primitive_info[i].primitive != UndefinedPrimitive))
4583 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4587 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4588 clone_info->stroke_width=0.0;
4589 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4590 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4592 clone_info=DestroyDrawInfo(clone_info);
4593 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4596 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4600 image_view=DestroyCacheView(image_view);
4601 if (image->debug != MagickFalse)
4602 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4603 return(status != 0 ? MagickTrue : MagickFalse);
4607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4611 + D r a w S t r o k e P o l y g o n %
4615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4617 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4618 % the image while respecting the line cap and join attributes.
4620 % The format of the DrawStrokePolygon method is:
4622 % MagickBooleanType DrawStrokePolygon(Image *image,
4623 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4625 % A description of each parameter follows:
4627 % o image: the image.
4629 % o draw_info: the draw info.
4631 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4636 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4637 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4645 for (i=0; i < 4; i++)
4646 linecap[i]=(*primitive_info);
4647 linecap[0].coordinates=4;
4648 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4649 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4650 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4651 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4652 linecap[4].primitive=UndefinedPrimitive;
4653 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4656 static MagickBooleanType DrawStrokePolygon(Image *image,
4657 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4658 ExceptionInfo *exception)
4670 register const PrimitiveInfo
4675 Draw stroked polygon.
4677 if (image->debug != MagickFalse)
4678 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4679 " begin draw-stroke-polygon");
4680 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4681 clone_info->fill=draw_info->stroke;
4682 if (clone_info->fill_pattern != (Image *) NULL)
4683 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4684 if (clone_info->stroke_pattern != (Image *) NULL)
4685 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4686 MagickTrue,exception);
4687 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4688 clone_info->stroke_width=0.0;
4689 clone_info->fill_rule=NonZeroRule;
4691 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4693 stroke_polygon=TraceStrokePolygon(draw_info,p);
4694 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4695 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4696 q=p+p->coordinates-1;
4697 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4698 MagickTrue : MagickFalse;
4699 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4701 DrawRoundLinecap(image,draw_info,p,exception);
4702 DrawRoundLinecap(image,draw_info,q,exception);
4705 clone_info=DestroyDrawInfo(clone_info);
4706 if (image->debug != MagickFalse)
4707 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4708 " end draw-stroke-polygon");
4713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4717 % G e t A f f i n e M a t r i x %
4721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4723 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4726 % The format of the GetAffineMatrix method is:
4728 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4730 % A description of each parameter follows:
4732 % o affine_matrix: the affine matrix.
4735 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4737 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4738 assert(affine_matrix != (AffineMatrix *) NULL);
4739 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4740 affine_matrix->sx=1.0;
4741 affine_matrix->sy=1.0;
4745 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4749 + G e t D r a w I n f o %
4753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4755 % GetDrawInfo() initializes draw_info to default values from image_info.
4757 % The format of the GetDrawInfo method is:
4759 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4761 % A description of each parameter follows:
4763 % o image_info: the image info..
4765 % o draw_info: the draw info.
4768 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4777 Initialize draw attributes.
4779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4780 assert(draw_info != (DrawInfo *) NULL);
4781 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4782 GetAffineMatrix(&draw_info->affine);
4783 exception=AcquireExceptionInfo();
4784 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4786 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4788 draw_info->stroke_width=1.0;
4789 draw_info->alpha=OpaqueAlpha;
4790 draw_info->fill_rule=EvenOddRule;
4791 draw_info->linecap=ButtCap;
4792 draw_info->linejoin=MiterJoin;
4793 draw_info->miterlimit=10;
4794 draw_info->decorate=NoDecoration;
4795 draw_info->pointsize=12.0;
4796 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4797 draw_info->compose=OverCompositeOp;
4798 draw_info->render=MagickTrue;
4799 draw_info->debug=IsEventLogging();
4800 if (image_info != (ImageInfo *) NULL)
4802 draw_info->stroke_antialias=image_info->antialias;
4803 if (image_info->font != (char *) NULL)
4804 draw_info->font=AcquireString(image_info->font);
4805 if (image_info->density != (char *) NULL)
4806 draw_info->density=AcquireString(image_info->density);
4807 draw_info->text_antialias=image_info->antialias;
4808 if (image_info->pointsize != 0.0)
4809 draw_info->pointsize=image_info->pointsize;
4810 draw_info->border_color=image_info->border_color;
4811 if (image_info->server_name != (char *) NULL)
4812 draw_info->server_name=AcquireString(image_info->server_name);
4813 option=GetImageOption(image_info,"encoding");
4814 if (option != (const char *) NULL)
4815 (void) CloneString(&draw_info->encoding,option);
4816 option=GetImageOption(image_info,"kerning");
4817 if (option != (const char *) NULL)
4818 draw_info->kerning=StringToDouble(option,(char **) NULL);
4819 option=GetImageOption(image_info,"interline-spacing");
4820 if (option != (const char *) NULL)
4821 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4822 option=GetImageOption(image_info,"interword-spacing");
4823 if (option != (const char *) NULL)
4824 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4825 option=GetImageOption(image_info,"direction");
4826 if (option != (const char *) NULL)
4827 draw_info->direction=(DirectionType) ParseCommandOption(
4828 MagickDirectionOptions,MagickFalse,option);
4830 draw_info->direction=UndefinedDirection;
4831 option=GetImageOption(image_info,"fill");
4832 if (option != (const char *) NULL)
4833 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4835 option=GetImageOption(image_info,"stroke");
4836 if (option != (const char *) NULL)
4837 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4839 option=GetImageOption(image_info,"strokewidth");
4840 if (option != (const char *) NULL)
4841 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4842 option=GetImageOption(image_info,"undercolor");
4843 if (option != (const char *) NULL)
4844 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4846 option=GetImageOption(image_info,"gravity");
4847 if (option != (const char *) NULL)
4848 draw_info->gravity=(GravityType) ParseCommandOption(
4849 MagickGravityOptions,MagickFalse,option);
4851 exception=DestroyExceptionInfo(exception);
4852 draw_info->signature=MagickSignature;
4856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4860 + P e r m u t a t e %
4864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4866 % Permutate() returns the permuation of the (n,k).
4868 % The format of the Permutate method is:
4870 % void Permutate(ssize_t n,ssize_t k)
4872 % A description of each parameter follows:
4880 static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
4889 for (i=k+1; i <= n; i++)
4891 for (i=1; i <= (n-k); i++)
4897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4901 + T r a c e P r i m i t i v e %
4905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4907 % TracePrimitive is a collection of methods for generating graphic
4908 % primitives such as arcs, ellipses, paths, etc.
4912 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4913 const PointInfo end,const PointInfo degrees)
4919 center.x=0.5*(end.x+start.x);
4920 center.y=0.5*(end.y+start.y);
4921 radii.x=fabs(center.x-start.x);
4922 radii.y=fabs(center.y-start.y);
4923 TraceEllipse(primitive_info,center,radii,degrees);
4926 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4927 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4928 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4943 register MagickRealType
4947 register PrimitiveInfo
4956 if ((start.x == end.x) && (start.y == end.y))
4958 TracePoint(primitive_info,end);
4961 radii.x=fabs(arc.x);
4962 radii.y=fabs(arc.y);
4963 if ((radii.x == 0.0) || (radii.y == 0.0))
4965 TraceLine(primitive_info,start,end);
4968 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4969 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4970 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4971 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4972 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4974 if (delta < MagickEpsilon)
4976 TraceLine(primitive_info,start,end);
4981 radii.x*=sqrt((double) delta);
4982 radii.y*=sqrt((double) delta);
4984 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4985 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4986 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4987 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4988 alpha=points[1].x-points[0].x;
4989 beta=points[1].y-points[0].y;
4990 factor=MagickEpsilonReciprocal(alpha*alpha+beta*beta)-0.25;
4995 factor=sqrt((double) factor);
4996 if (sweep == large_arc)
4999 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5000 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5001 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5002 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5003 if ((theta < 0.0) && (sweep != MagickFalse))
5004 theta+=(MagickRealType) (2.0*MagickPI);
5006 if ((theta > 0.0) && (sweep == MagickFalse))
5007 theta-=(MagickRealType) (2.0*MagickPI);
5008 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5011 for (i=0; i < (ssize_t) arc_segments; i++)
5013 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5014 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5015 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5016 sin(fmod((double) beta,DegreesToRadians(360.0)));
5017 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5018 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5019 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5020 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5021 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5022 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5023 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5024 theta/arc_segments),DegreesToRadians(360.0))));
5025 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5026 theta/arc_segments),DegreesToRadians(360.0))));
5027 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5028 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5029 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5030 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5031 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5032 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5033 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5035 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5037 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5039 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5041 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5043 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5045 if (i == (ssize_t) (arc_segments-1))
5050 primitive_info->coordinates=(size_t) (p-primitive_info);
5051 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5053 p->primitive=primitive_info->primitive;
5058 static void TraceBezier(PrimitiveInfo *primitive_info,
5059 const size_t number_coordinates)
5071 register PrimitiveInfo
5083 Allocate coeficients.
5085 quantum=number_coordinates;
5086 for (i=0; i < (ssize_t) number_coordinates; i++)
5088 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5090 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5091 if (alpha > (MagickRealType) quantum)
5092 quantum=(size_t) alpha;
5093 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5094 if (alpha > (MagickRealType) quantum)
5095 quantum=(size_t) alpha;
5098 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5099 (double) BezierQuantum);
5100 control_points=quantum*number_coordinates;
5101 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5102 number_coordinates,sizeof(*coefficients));
5103 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5105 if ((coefficients == (MagickRealType *) NULL) ||
5106 (points == (PointInfo *) NULL))
5107 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5109 Compute bezier points.
5111 end=primitive_info[number_coordinates-1].point;
5112 for (i=0; i < (ssize_t) number_coordinates; i++)
5113 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5115 for (i=0; i < (ssize_t) control_points; i++)
5120 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5121 for (j=0; j < (ssize_t) number_coordinates; j++)
5123 point.x+=alpha*coefficients[j]*p->point.x;
5124 point.y+=alpha*coefficients[j]*p->point.y;
5125 alpha*=weight/(1.0-weight);
5129 weight+=1.0/control_points;
5132 Bezier curves are just short segmented polys.
5135 for (i=0; i < (ssize_t) control_points; i++)
5137 TracePoint(p,points[i]);
5142 primitive_info->coordinates=(size_t) (p-primitive_info);
5143 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5145 p->primitive=primitive_info->primitive;
5148 points=(PointInfo *) RelinquishMagickMemory(points);
5149 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5152 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5153 const PointInfo end)
5164 alpha=end.x-start.x;
5166 radius=hypot((double) alpha,(double) beta);
5167 offset.x=(double) radius;
5168 offset.y=(double) radius;
5171 TraceEllipse(primitive_info,start,offset,degrees);
5174 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5175 const PointInfo stop,const PointInfo degrees)
5186 register PrimitiveInfo
5193 Ellipses are just short segmented polys.
5195 if ((stop.x == 0.0) && (stop.y == 0.0))
5197 TracePoint(primitive_info,start);
5200 delta=2.0/MagickMax(stop.x,stop.y);
5201 step=(MagickRealType) (MagickPI/8.0);
5202 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5203 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5204 angle.x=DegreesToRadians(degrees.x);
5206 while (y < degrees.x)
5208 angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5209 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5211 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5212 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5213 TracePoint(p,point);
5216 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5217 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5218 TracePoint(p,point);
5220 primitive_info->coordinates=(size_t) (p-primitive_info);
5221 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5223 p->primitive=primitive_info->primitive;
5228 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5229 const PointInfo end)
5231 TracePoint(primitive_info,start);
5232 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5233 (fabs(start.y-end.y) < MagickEpsilon))
5235 primitive_info->primitive=PointPrimitive;
5236 primitive_info->coordinates=1;
5239 TracePoint(primitive_info+1,end);
5240 (primitive_info+1)->primitive=primitive_info->primitive;
5241 primitive_info->coordinates=2;
5244 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5247 token[MaxTextExtent];
5269 register PrimitiveInfo
5284 number_coordinates=0;
5286 primitive_type=primitive_info->primitive;
5288 for (p=path; *p != '\0'; )
5290 while (isspace((int) ((unsigned char) *p)) != 0)
5294 last_attribute=attribute;
5295 attribute=(int) (*p++);
5316 GetMagickToken(p,&p,token);
5318 GetMagickToken(p,&p,token);
5319 arc.x=StringToDouble(token,(char **) NULL);
5320 GetMagickToken(p,&p,token);
5322 GetMagickToken(p,&p,token);
5323 arc.y=StringToDouble(token,(char **) NULL);
5324 GetMagickToken(p,&p,token);
5326 GetMagickToken(p,&p,token);
5327 angle=StringToDouble(token,(char **) NULL);
5328 GetMagickToken(p,&p,token);
5330 GetMagickToken(p,&p,token);
5331 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5332 GetMagickToken(p,&p,token);
5334 GetMagickToken(p,&p,token);
5335 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5336 GetMagickToken(p,&p,token);
5338 GetMagickToken(p,&p,token);
5339 x=StringToDouble(token,(char **) NULL);
5340 GetMagickToken(p,&p,token);
5342 GetMagickToken(p,&p,token);
5343 y=StringToDouble(token,(char **) NULL);
5344 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5345 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5346 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5349 while (isspace((int) ((unsigned char) *p)) != 0)
5353 } while (IsPoint(p) != MagickFalse);
5360 Compute bezier points.
5365 for (i=1; i < 4; i++)
5367 GetMagickToken(p,&p,token);
5369 GetMagickToken(p,&p,token);
5370 x=StringToDouble(token,(char **) NULL);
5371 GetMagickToken(p,&p,token);
5373 GetMagickToken(p,&p,token);
5374 y=StringToDouble(token,(char **) NULL);
5375 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5376 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5379 for (i=0; i < 4; i++)
5380 (q+i)->point=points[i];
5384 } while (IsPoint(p) != MagickFalse);
5392 GetMagickToken(p,&p,token);
5394 GetMagickToken(p,&p,token);
5395 x=StringToDouble(token,(char **) NULL);
5396 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5397 TracePoint(q,point);
5399 } while (IsPoint(p) != MagickFalse);
5407 GetMagickToken(p,&p,token);
5409 GetMagickToken(p,&p,token);
5410 x=StringToDouble(token,(char **) NULL);
5411 GetMagickToken(p,&p,token);
5413 GetMagickToken(p,&p,token);
5414 y=StringToDouble(token,(char **) NULL);
5415 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5416 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5417 TracePoint(q,point);
5419 } while (IsPoint(p) != MagickFalse);
5425 if (q != primitive_info)
5427 primitive_info->coordinates=(size_t) (q-primitive_info);
5428 number_coordinates+=primitive_info->coordinates;
5434 GetMagickToken(p,&p,token);
5436 GetMagickToken(p,&p,token);
5437 x=StringToDouble(token,(char **) NULL);
5438 GetMagickToken(p,&p,token);
5440 GetMagickToken(p,&p,token);
5441 y=StringToDouble(token,(char **) NULL);
5442 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5443 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5447 TracePoint(q,point);
5449 if ((i != 0) && (attribute == (int) 'M'))
5451 TracePoint(q,point);
5454 } while (IsPoint(p) != MagickFalse);
5461 Compute bezier points.
5466 for (i=1; i < 3; i++)
5468 GetMagickToken(p,&p,token);
5470 GetMagickToken(p,&p,token);
5471 x=StringToDouble(token,(char **) NULL);
5472 GetMagickToken(p,&p,token);
5474 GetMagickToken(p,&p,token);
5475 y=StringToDouble(token,(char **) NULL);
5478 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5479 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5482 for (i=0; i < 3; i++)
5483 (q+i)->point=points[i];
5487 } while (IsPoint(p) != MagickFalse);
5494 Compute bezier points.
5498 points[0]=points[3];
5499 points[1].x=2.0*points[3].x-points[2].x;
5500 points[1].y=2.0*points[3].y-points[2].y;
5501 for (i=2; i < 4; i++)
5503 GetMagickToken(p,&p,token);
5505 GetMagickToken(p,&p,token);
5506 x=StringToDouble(token,(char **) NULL);
5507 GetMagickToken(p,&p,token);
5509 GetMagickToken(p,&p,token);
5510 y=StringToDouble(token,(char **) NULL);
5513 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5514 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5517 if (strchr("CcSs",last_attribute) == (char *) NULL)
5519 points[0]=points[2];
5520 points[1]=points[3];
5522 for (i=0; i < 4; i++)
5523 (q+i)->point=points[i];
5527 } while (IsPoint(p) != MagickFalse);
5534 Compute bezier points.
5538 points[0]=points[2];
5539 points[1].x=2.0*points[2].x-points[1].x;
5540 points[1].y=2.0*points[2].y-points[1].y;
5541 for (i=2; i < 3; i++)
5543 GetMagickToken(p,&p,token);
5545 GetMagickToken(p,&p,token);
5546 x=StringToDouble(token,(char **) NULL);
5547 GetMagickToken(p,&p,token);
5549 GetMagickToken(p,&p,token);
5550 y=StringToDouble(token,(char **) NULL);
5551 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5552 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5555 if (strchr("QqTt",last_attribute) == (char *) NULL)
5557 points[0]=points[2];
5558 points[1]=points[3];
5560 for (i=0; i < 3; i++)
5561 (q+i)->point=points[i];
5565 } while (IsPoint(p) != MagickFalse);
5573 GetMagickToken(p,&p,token);
5575 GetMagickToken(p,&p,token);
5576 y=StringToDouble(token,(char **) NULL);
5577 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5578 TracePoint(q,point);
5580 } while (IsPoint(p) != MagickFalse);
5587 TracePoint(q,point);
5589 primitive_info->coordinates=(size_t) (q-primitive_info);
5590 number_coordinates+=primitive_info->coordinates;
5597 if (isalpha((int) ((unsigned char) attribute)) != 0)
5598 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5604 primitive_info->coordinates=(size_t) (q-primitive_info);
5605 number_coordinates+=primitive_info->coordinates;
5606 for (i=0; i < (ssize_t) number_coordinates; i++)
5609 q->primitive=primitive_type;
5611 q->method=FillToBorderMethod;
5614 return(number_coordinates);
5617 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5618 const PointInfo end)
5623 register PrimitiveInfo
5630 TracePoint(p,start);
5634 TracePoint(p,point);
5640 TracePoint(p,point);
5642 TracePoint(p,start);
5644 primitive_info->coordinates=(size_t) (p-primitive_info);
5645 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5647 p->primitive=primitive_info->primitive;
5652 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5653 const PointInfo start,const PointInfo end,PointInfo arc)
5660 register PrimitiveInfo
5667 offset.x=fabs(end.x-start.x);
5668 offset.y=fabs(end.y-start.y);
5669 if (arc.x > (0.5*offset.x))
5671 if (arc.y > (0.5*offset.y))
5673 point.x=start.x+offset.x-arc.x;
5674 point.y=start.y+arc.y;
5677 TraceEllipse(p,point,arc,degrees);
5679 point.x=start.x+offset.x-arc.x;
5680 point.y=start.y+offset.y-arc.y;
5683 TraceEllipse(p,point,arc,degrees);
5685 point.x=start.x+arc.x;
5686 point.y=start.y+offset.y-arc.y;
5689 TraceEllipse(p,point,arc,degrees);
5691 point.x=start.x+arc.x;
5692 point.y=start.y+arc.y;
5695 TraceEllipse(p,point,arc,degrees);
5697 TracePoint(p,primitive_info->point);
5699 primitive_info->coordinates=(size_t) (p-primitive_info);
5700 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5702 p->primitive=primitive_info->primitive;
5707 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5708 const size_t number_vertices,const MagickRealType offset)
5713 register MagickRealType
5725 for (i=1; i < (ssize_t) number_vertices; i++)
5727 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5728 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5729 if ((fabs((double) dx) >= MagickEpsilon) ||
5730 (fabs((double) dy) >= MagickEpsilon))
5733 if (i == (ssize_t) number_vertices)
5734 i=(ssize_t) number_vertices-1L;
5735 distance=hypot((double) dx,(double) dy);
5736 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5737 dx*(distance+offset)/distance);
5738 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5739 dy*(distance+offset)/distance);
5740 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5742 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5743 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5744 if ((fabs((double) dx) >= MagickEpsilon) ||
5745 (fabs((double) dy) >= MagickEpsilon))
5748 distance=hypot((double) dx,(double) dy);
5749 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5750 dx*(distance+offset)/distance);
5751 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5752 dy*(distance+offset)/distance);
5755 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5756 const PrimitiveInfo *primitive_info)
5758 typedef struct _LineSegment
5810 number_vertices=primitive_info->coordinates;
5811 max_strokes=2*number_vertices+6*BezierQuantum+360;
5812 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5814 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5816 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5817 number_vertices+2UL,sizeof(*polygon_primitive));
5818 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5819 (polygon_primitive == (PrimitiveInfo *) NULL))
5820 return((PrimitiveInfo *) NULL);
5821 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5822 number_vertices*sizeof(*polygon_primitive));
5824 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5825 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5826 MagickTrue : MagickFalse;
5827 if ((draw_info->linejoin == RoundJoin) ||
5828 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5830 polygon_primitive[number_vertices]=primitive_info[1];
5833 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5835 Compute the slope for the first line segment, p.
5839 for (n=1; n < (ssize_t) number_vertices; n++)
5841 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5842 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5843 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5846 if (n == (ssize_t) number_vertices)
5847 n=(ssize_t) number_vertices-1L;
5849 inverse_slope.p=0.0;
5850 if (fabs(dx.p) < MagickEpsilon)
5853 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5855 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5858 if (fabs(dy.p) < MagickEpsilon)
5861 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5863 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5868 inverse_slope.p=(-1.0/slope.p);
5870 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5871 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5873 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5874 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5875 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5876 offset.y=(double) (offset.x*inverse_slope.p);
5877 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5879 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5880 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5881 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5882 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5883 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5884 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5885 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5886 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5890 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5891 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5892 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5893 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5894 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5895 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5896 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5897 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5900 Create strokes for the line join attribute: bevel, miter, round.
5904 path_q[p++]=box_q[0];
5905 path_p[q++]=box_p[0];
5906 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5909 Compute the slope for this line segment, q.
5911 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5912 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5913 dot_product=dx.q*dx.q+dy.q*dy.q;
5914 if (dot_product < 0.25)
5917 inverse_slope.q=0.0;
5918 if (fabs(dx.q) < MagickEpsilon)
5921 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5923 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5926 if (fabs(dy.q) < MagickEpsilon)
5929 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5931 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5936 inverse_slope.q=(-1.0/slope.q);
5938 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5939 offset.y=(double) (offset.x*inverse_slope.q);
5940 dot_product=dy.q*offset.x-dx.q*offset.y;
5941 if (dot_product > 0.0)
5943 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5944 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5945 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5946 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5947 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5948 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5949 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5950 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5954 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5955 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5956 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5957 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5958 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5959 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5960 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5961 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5963 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5970 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5971 box_p[3].y)/(slope.p-slope.q));
5972 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5973 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5974 box_q[3].y)/(slope.p-slope.q));
5975 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5977 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5979 max_strokes+=6*BezierQuantum+360;
5980 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5982 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5984 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5986 polygon_primitive=(PrimitiveInfo *)
5987 RelinquishMagickMemory(polygon_primitive);
5988 return((PrimitiveInfo *) NULL);
5991 dot_product=dx.q*dy.p-dx.p*dy.q;
5992 if (dot_product <= 0.0)
5993 switch (draw_info->linejoin)
5997 path_q[q++]=box_q[1];
5998 path_q[q++]=box_q[2];
5999 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6000 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6001 if (dot_product <= miterlimit)
6002 path_p[p++]=box_p[4];
6005 path_p[p++]=box_p[1];
6006 path_p[p++]=box_p[2];
6012 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6013 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6014 if (dot_product <= miterlimit)
6016 path_q[q++]=box_q[4];
6017 path_p[p++]=box_p[4];
6021 path_q[q++]=box_q[1];
6022 path_q[q++]=box_q[2];
6023 path_p[p++]=box_p[1];
6024 path_p[p++]=box_p[2];
6030 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6031 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6032 if (dot_product <= miterlimit)
6033 path_p[p++]=box_p[4];
6036 path_p[p++]=box_p[1];
6037 path_p[p++]=box_p[2];
6039 center=polygon_primitive[n].point;
6040 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6041 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6042 if (theta.q < theta.p)
6043 theta.q+=(MagickRealType) (2.0*MagickPI);
6044 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6045 (2.0*sqrt((double) (1.0/mid)))));
6046 path_q[q].x=box_q[1].x;
6047 path_q[q].y=box_q[1].y;
6049 for (j=1; j < (ssize_t) arc_segments; j++)
6051 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6052 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6053 (theta.p+delta_theta),DegreesToRadians(360.0))));
6054 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6055 (theta.p+delta_theta),DegreesToRadians(360.0))));
6058 path_q[q++]=box_q[2];
6065 switch (draw_info->linejoin)
6069 path_p[p++]=box_p[1];
6070 path_p[p++]=box_p[2];
6071 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6072 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6073 if (dot_product <= miterlimit)
6074 path_q[q++]=box_q[4];
6077 path_q[q++]=box_q[1];
6078 path_q[q++]=box_q[2];
6084 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6085 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6086 if (dot_product <= miterlimit)
6088 path_q[q++]=box_q[4];
6089 path_p[p++]=box_p[4];
6093 path_q[q++]=box_q[1];
6094 path_q[q++]=box_q[2];
6095 path_p[p++]=box_p[1];
6096 path_p[p++]=box_p[2];
6102 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6103 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6104 if (dot_product <= miterlimit)
6105 path_q[q++]=box_q[4];
6108 path_q[q++]=box_q[1];
6109 path_q[q++]=box_q[2];
6111 center=polygon_primitive[n].point;
6112 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6113 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6114 if (theta.p < theta.q)
6115 theta.p+=(MagickRealType) (2.0*MagickPI);
6116 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6117 (2.0*sqrt((double) (1.0/mid)))));
6118 path_p[p++]=box_p[1];
6119 for (j=1; j < (ssize_t) arc_segments; j++)
6121 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6122 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6123 (theta.p+delta_theta),DegreesToRadians(360.0))));
6124 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6125 (theta.p+delta_theta),DegreesToRadians(360.0))));
6128 path_p[p++]=box_p[2];
6135 inverse_slope.p=inverse_slope.q;
6144 path_p[p++]=box_p[1];
6145 path_q[q++]=box_q[1];
6147 Trace stroked polygon.
6149 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6150 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6151 if (stroke_polygon != (PrimitiveInfo *) NULL)
6153 for (i=0; i < (ssize_t) p; i++)
6155 stroke_polygon[i]=polygon_primitive[0];
6156 stroke_polygon[i].point=path_p[i];
6158 if (closed_path != MagickFalse)
6160 stroke_polygon[i]=polygon_primitive[0];
6161 stroke_polygon[i].point=stroke_polygon[0].point;
6164 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6166 stroke_polygon[i]=polygon_primitive[0];
6167 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6169 if (closed_path != MagickFalse)
6171 stroke_polygon[i]=polygon_primitive[0];
6172 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6175 stroke_polygon[i]=polygon_primitive[0];
6176 stroke_polygon[i].point=stroke_polygon[0].point;
6178 stroke_polygon[i].primitive=UndefinedPrimitive;
6179 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6181 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6182 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6183 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6184 return(stroke_polygon);