2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/color.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/constitute.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/draw-private.h"
61 #include "MagickCore/enhance.h"
62 #include "MagickCore/exception.h"
63 #include "MagickCore/exception-private.h"
64 #include "MagickCore/gem.h"
65 #include "MagickCore/geometry.h"
66 #include "MagickCore/image-private.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/log.h"
69 #include "MagickCore/monitor.h"
70 #include "MagickCore/monitor-private.h"
71 #include "MagickCore/option.h"
72 #include "MagickCore/paint.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/pixel-private.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/resample.h"
77 #include "MagickCore/resample-private.h"
78 #include "MagickCore/resource_.h"
79 #include "MagickCore/string_.h"
80 #include "MagickCore/string-private.h"
81 #include "MagickCore/thread-private.h"
82 #include "MagickCore/token.h"
83 #include "MagickCore/transform.h"
84 #include "MagickCore/utility.h"
89 #define BezierQuantum 200
94 typedef struct _EdgeInfo
118 typedef struct _ElementInfo
128 typedef struct _PolygonInfo
146 typedef struct _PathInfo
156 Forward declarations.
158 static MagickBooleanType
159 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
163 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
166 TracePath(PrimitiveInfo *,const char *);
169 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
170 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
171 const double,const MagickBooleanType,const MagickBooleanType),
172 TraceBezier(PrimitiveInfo *,const size_t),
173 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
174 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
175 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
176 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
177 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
179 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % A c q u i r e D r a w I n f o %
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
194 % The format of the AcquireDrawInfo method is:
196 % DrawInfo *AcquireDrawInfo(void)
199 MagickExport DrawInfo *AcquireDrawInfo(void)
204 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
205 if (draw_info == (DrawInfo *) NULL)
206 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
207 GetDrawInfo((ImageInfo *) NULL,draw_info);
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % C l o n e D r a w I n f o %
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
223 % is specified, a new DrawInfo structure is created initialized to default
226 % The format of the CloneDrawInfo method is:
228 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
229 % const DrawInfo *draw_info)
231 % A description of each parameter follows:
233 % o image_info: the image info.
235 % o draw_info: the draw info.
238 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
239 const DrawInfo *draw_info)
247 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
248 if (clone_info == (DrawInfo *) NULL)
249 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
250 GetDrawInfo(image_info,clone_info);
251 if (draw_info == (DrawInfo *) NULL)
253 exception=AcquireExceptionInfo();
254 (void) CloneString(&clone_info->primitive,draw_info->primitive);
255 (void) CloneString(&clone_info->geometry,draw_info->geometry);
256 clone_info->viewbox=draw_info->viewbox;
257 clone_info->affine=draw_info->affine;
258 clone_info->gravity=draw_info->gravity;
259 clone_info->fill=draw_info->fill;
260 clone_info->stroke=draw_info->stroke;
261 clone_info->stroke_width=draw_info->stroke_width;
262 if (draw_info->fill_pattern != (Image *) NULL)
263 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
265 if (draw_info->stroke_pattern != (Image *) NULL)
266 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
267 MagickTrue,exception);
268 clone_info->stroke_antialias=draw_info->stroke_antialias;
269 clone_info->text_antialias=draw_info->text_antialias;
270 clone_info->fill_rule=draw_info->fill_rule;
271 clone_info->linecap=draw_info->linecap;
272 clone_info->linejoin=draw_info->linejoin;
273 clone_info->miterlimit=draw_info->miterlimit;
274 clone_info->dash_offset=draw_info->dash_offset;
275 clone_info->decorate=draw_info->decorate;
276 clone_info->compose=draw_info->compose;
277 (void) CloneString(&clone_info->text,draw_info->text);
278 (void) CloneString(&clone_info->font,draw_info->font);
279 (void) CloneString(&clone_info->metrics,draw_info->metrics);
280 (void) CloneString(&clone_info->family,draw_info->family);
281 clone_info->style=draw_info->style;
282 clone_info->stretch=draw_info->stretch;
283 clone_info->weight=draw_info->weight;
284 (void) CloneString(&clone_info->encoding,draw_info->encoding);
285 clone_info->pointsize=draw_info->pointsize;
286 clone_info->kerning=draw_info->kerning;
287 clone_info->interline_spacing=draw_info->interline_spacing;
288 clone_info->interword_spacing=draw_info->interword_spacing;
289 clone_info->direction=draw_info->direction;
290 (void) CloneString(&clone_info->density,draw_info->density);
291 clone_info->align=draw_info->align;
292 clone_info->undercolor=draw_info->undercolor;
293 clone_info->border_color=draw_info->border_color;
294 (void) CloneString(&clone_info->server_name,draw_info->server_name);
295 if (draw_info->dash_pattern != (double *) NULL)
300 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
301 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
302 sizeof(*clone_info->dash_pattern));
303 if (clone_info->dash_pattern == (double *) NULL)
304 ThrowFatalException(ResourceLimitFatalError,
305 "UnableToAllocateDashPattern");
306 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
307 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
309 clone_info->gradient=draw_info->gradient;
310 if (draw_info->gradient.stops != (StopInfo *) NULL)
315 number_stops=clone_info->gradient.number_stops;
316 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
317 number_stops,sizeof(*clone_info->gradient.stops));
318 if (clone_info->gradient.stops == (StopInfo *) NULL)
319 ThrowFatalException(ResourceLimitFatalError,
320 "UnableToAllocateDashPattern");
321 (void) CopyMagickMemory(clone_info->gradient.stops,
322 draw_info->gradient.stops,(size_t) number_stops*
323 sizeof(*clone_info->gradient.stops));
325 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
326 clone_info->bounds=draw_info->bounds;
327 clone_info->clip_units=draw_info->clip_units;
328 clone_info->render=draw_info->render;
329 clone_info->alpha=draw_info->alpha;
330 clone_info->element_reference=draw_info->element_reference;
331 clone_info->debug=IsEventLogging();
332 exception=DestroyExceptionInfo(exception);
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 + C o n v e r t P a t h T o P o l y g o n %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % ConvertPathToPolygon() converts a path to the more efficient sorted
350 % The format of the ConvertPathToPolygon method is:
352 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
353 % const PathInfo *path_info)
355 % A description of each parameter follows:
357 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
358 % rendering form of type PolygonInfo.
360 % o draw_info: Specifies a pointer to an DrawInfo structure.
362 % o path_info: Specifies a pointer to an PathInfo structure.
367 #if defined(__cplusplus) || defined(c_plusplus)
371 static int CompareEdges(const void *x,const void *y)
373 register const EdgeInfo
380 p=(const EdgeInfo *) x;
381 q=(const EdgeInfo *) y;
382 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
384 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
386 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
388 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
390 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
391 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
396 #if defined(__cplusplus) || defined(c_plusplus)
400 static void LogPolygonInfo(const PolygonInfo *polygon_info)
409 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
410 p=polygon_info->edges;
411 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
413 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
415 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
416 p->direction != MagickFalse ? "down" : "up");
417 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
418 p->ghostline != MagickFalse ? "transparent" : "opaque");
419 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
420 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
421 p->bounds.x2,p->bounds.y2);
422 for (j=0; j < (ssize_t) p->number_points; j++)
423 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
424 p->points[j].x,p->points[j].y);
427 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
430 static void ReversePoints(PointInfo *points,const size_t number_points)
438 for (i=0; i < (ssize_t) (number_points >> 1); i++)
441 points[i]=points[number_points-(i+1)];
442 points[number_points-(i+1)]=point;
446 static PolygonInfo *ConvertPathToPolygon(
447 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
476 Convert a path to the more efficient sorted rendering form.
478 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
479 if (polygon_info == (PolygonInfo *) NULL)
480 return((PolygonInfo *) NULL);
482 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
483 sizeof(*polygon_info->edges));
484 if (polygon_info->edges == (EdgeInfo *) NULL)
485 return((PolygonInfo *) NULL);
488 ghostline=MagickFalse;
491 points=(PointInfo *) NULL;
492 (void) ResetMagickMemory(&point,0,sizeof(point));
493 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
494 for (i=0; path_info[i].code != EndCode; i++)
496 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
497 (path_info[i].code == GhostlineCode))
502 if ((points != (PointInfo *) NULL) && (n >= 2))
504 if (edge == number_edges)
507 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
508 polygon_info->edges,(size_t) number_edges,
509 sizeof(*polygon_info->edges));
510 if (polygon_info->edges == (EdgeInfo *) NULL)
511 return((PolygonInfo *) NULL);
513 polygon_info->edges[edge].number_points=(size_t) n;
514 polygon_info->edges[edge].scanline=(-1.0);
515 polygon_info->edges[edge].highwater=0;
516 polygon_info->edges[edge].ghostline=ghostline;
517 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
519 ReversePoints(points,(size_t) n);
520 polygon_info->edges[edge].points=points;
521 polygon_info->edges[edge].bounds=bounds;
522 polygon_info->edges[edge].bounds.y1=points[0].y;
523 polygon_info->edges[edge].bounds.y2=points[n-1].y;
524 points=(PointInfo *) NULL;
525 ghostline=MagickFalse;
528 if (points == (PointInfo *) NULL)
531 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
533 if (points == (PointInfo *) NULL)
534 return((PolygonInfo *) NULL);
536 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
537 point=path_info[i].point;
548 next_direction=((path_info[i].point.y > point.y) ||
549 ((path_info[i].point.y == point.y) &&
550 (path_info[i].point.x > point.x))) ? 1 : -1;
551 if ((direction != 0) && (direction != next_direction))
557 if (edge == number_edges)
560 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
561 polygon_info->edges,(size_t) number_edges,
562 sizeof(*polygon_info->edges));
563 if (polygon_info->edges == (EdgeInfo *) NULL)
564 return((PolygonInfo *) NULL);
566 polygon_info->edges[edge].number_points=(size_t) n;
567 polygon_info->edges[edge].scanline=(-1.0);
568 polygon_info->edges[edge].highwater=0;
569 polygon_info->edges[edge].ghostline=ghostline;
570 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
572 ReversePoints(points,(size_t) n);
573 polygon_info->edges[edge].points=points;
574 polygon_info->edges[edge].bounds=bounds;
575 polygon_info->edges[edge].bounds.y1=points[0].y;
576 polygon_info->edges[edge].bounds.y2=points[n-1].y;
578 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
580 if (points == (PointInfo *) NULL)
581 return((PolygonInfo *) NULL);
583 ghostline=MagickFalse;
589 direction=next_direction;
590 if (points == (PointInfo *) NULL)
592 if (n == (ssize_t) number_points)
595 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
597 if (points == (PointInfo *) NULL)
598 return((PolygonInfo *) NULL);
600 point=path_info[i].point;
602 if (point.x < bounds.x1)
604 if (point.x > bounds.x2)
608 if (points != (PointInfo *) NULL)
611 points=(PointInfo *) RelinquishMagickMemory(points);
614 if (edge == number_edges)
617 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
618 polygon_info->edges,(size_t) number_edges,
619 sizeof(*polygon_info->edges));
620 if (polygon_info->edges == (EdgeInfo *) NULL)
621 return((PolygonInfo *) NULL);
623 polygon_info->edges[edge].number_points=(size_t) n;
624 polygon_info->edges[edge].scanline=(-1.0);
625 polygon_info->edges[edge].highwater=0;
626 polygon_info->edges[edge].ghostline=ghostline;
627 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
629 ReversePoints(points,(size_t) n);
630 polygon_info->edges[edge].points=points;
631 polygon_info->edges[edge].bounds=bounds;
632 polygon_info->edges[edge].bounds.y1=points[0].y;
633 polygon_info->edges[edge].bounds.y2=points[n-1].y;
634 ghostline=MagickFalse;
638 polygon_info->number_edges=edge;
639 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
640 sizeof(*polygon_info->edges),CompareEdges);
641 if (IsEventLogging() != MagickFalse)
642 LogPolygonInfo(polygon_info);
643 return(polygon_info);
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 + C o n v e r t P r i m i t i v e T o P a t h %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
660 % The format of the ConvertPrimitiveToPath method is:
662 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
663 % const PrimitiveInfo *primitive_info)
665 % A description of each parameter follows:
667 % o Method ConvertPrimitiveToPath returns a vector path structure of type
670 % o draw_info: a structure of type DrawInfo.
672 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
677 static void LogPathInfo(const PathInfo *path_info)
679 register const PathInfo
682 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
683 for (p=path_info; p->code != EndCode; p++)
684 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
685 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
686 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
687 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
689 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
692 static PathInfo *ConvertPrimitiveToPath(
693 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
714 Converts a PrimitiveInfo structure into a vector path structure.
716 switch (primitive_info->primitive)
723 return((PathInfo *) NULL);
727 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
728 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
730 if (path_info == (PathInfo *) NULL)
731 return((PathInfo *) NULL);
739 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
742 if (coordinates <= 0)
744 coordinates=(ssize_t) primitive_info[i].coordinates;
745 p=primitive_info[i].point;
751 Eliminate duplicate points.
753 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
754 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
756 path_info[n].code=code;
757 path_info[n].point=primitive_info[i].point;
758 q=primitive_info[i].point;
763 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
764 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
767 Mark the p point as open if it does not match the q.
769 path_info[start].code=OpenCode;
770 path_info[n].code=GhostlineCode;
771 path_info[n].point=primitive_info[i].point;
773 path_info[n].code=LineToCode;
774 path_info[n].point=p;
777 path_info[n].code=EndCode;
778 path_info[n].point.x=0.0;
779 path_info[n].point.y=0.0;
780 if (IsEventLogging() != MagickFalse)
781 LogPathInfo(path_info);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % D e s t r o y D r a w I n f o %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
799 % The format of the DestroyDrawInfo method is:
801 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
803 % A description of each parameter follows:
805 % o draw_info: the draw info.
808 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
810 if (draw_info->debug != MagickFalse)
811 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
812 assert(draw_info != (DrawInfo *) NULL);
813 assert(draw_info->signature == MagickSignature);
814 if (draw_info->primitive != (char *) NULL)
815 draw_info->primitive=DestroyString(draw_info->primitive);
816 if (draw_info->text != (char *) NULL)
817 draw_info->text=DestroyString(draw_info->text);
818 if (draw_info->geometry != (char *) NULL)
819 draw_info->geometry=DestroyString(draw_info->geometry);
820 if (draw_info->fill_pattern != (Image *) NULL)
821 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
822 if (draw_info->stroke_pattern != (Image *) NULL)
823 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
824 if (draw_info->font != (char *) NULL)
825 draw_info->font=DestroyString(draw_info->font);
826 if (draw_info->metrics != (char *) NULL)
827 draw_info->metrics=DestroyString(draw_info->metrics);
828 if (draw_info->family != (char *) NULL)
829 draw_info->family=DestroyString(draw_info->family);
830 if (draw_info->encoding != (char *) NULL)
831 draw_info->encoding=DestroyString(draw_info->encoding);
832 if (draw_info->density != (char *) NULL)
833 draw_info->density=DestroyString(draw_info->density);
834 if (draw_info->server_name != (char *) NULL)
835 draw_info->server_name=(char *)
836 RelinquishMagickMemory(draw_info->server_name);
837 if (draw_info->dash_pattern != (double *) NULL)
838 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
839 draw_info->dash_pattern);
840 if (draw_info->gradient.stops != (StopInfo *) NULL)
841 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
842 draw_info->gradient.stops);
843 if (draw_info->clip_mask != (char *) NULL)
844 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
845 draw_info->signature=(~MagickSignature);
846 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 + D e s t r o y E d g e %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % DestroyEdge() destroys the specified polygon edge.
863 % The format of the DestroyEdge method is:
865 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
867 % A description of each parameter follows:
869 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
871 % o edge: the polygon edge number to destroy.
874 static size_t DestroyEdge(PolygonInfo *polygon_info,
877 assert(edge < polygon_info->number_edges);
878 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
879 polygon_info->edges[edge].points);
880 polygon_info->number_edges--;
881 if (edge < polygon_info->number_edges)
882 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
883 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
884 return(polygon_info->number_edges);
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 + D e s t r o y P o l y g o n I n f o %
896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
900 % The format of the DestroyPolygonInfo method is:
902 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
904 % A description of each parameter follows:
906 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
909 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
914 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
915 polygon_info->edges[i].points=(PointInfo *)
916 RelinquishMagickMemory(polygon_info->edges[i].points);
917 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
918 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926 % D r a w A f f i n e I m a g e %
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 % DrawAffineImage() composites the source over the destination image as
933 % dictated by the affine transform.
935 % The format of the DrawAffineImage method is:
937 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
938 % const AffineMatrix *affine,ExceptionInfo *exception)
940 % A description of each parameter follows:
942 % o image: the image.
944 % o source: the source image.
946 % o affine: the affine transform.
948 % o exception: return any errors or warnings in this structure.
952 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
953 const double y,const SegmentInfo *edge)
966 Determine left and right edges.
968 inverse_edge.x1=edge->x1;
969 inverse_edge.y1=edge->y1;
970 inverse_edge.x2=edge->x2;
971 inverse_edge.y2=edge->y2;
972 z=affine->ry*y+affine->tx;
973 if (affine->sx >= MagickEpsilon)
975 intercept=(-z/affine->sx);
977 if (x > inverse_edge.x1)
979 intercept=(-z+(double) image->columns)/affine->sx;
981 if (x < inverse_edge.x2)
985 if (affine->sx < -MagickEpsilon)
987 intercept=(-z+(double) image->columns)/affine->sx;
989 if (x > inverse_edge.x1)
991 intercept=(-z/affine->sx);
993 if (x < inverse_edge.x2)
997 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
999 inverse_edge.x2=edge->x1;
1000 return(inverse_edge);
1003 Determine top and bottom edges.
1005 z=affine->sy*y+affine->ty;
1006 if (affine->rx >= MagickEpsilon)
1008 intercept=(-z/affine->rx);
1010 if (x > inverse_edge.x1)
1012 intercept=(-z+(double) image->rows)/affine->rx;
1014 if (x < inverse_edge.x2)
1018 if (affine->rx < -MagickEpsilon)
1020 intercept=(-z+(double) image->rows)/affine->rx;
1022 if (x > inverse_edge.x1)
1024 intercept=(-z/affine->rx);
1026 if (x < inverse_edge.x2)
1030 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1032 inverse_edge.x2=edge->x2;
1033 return(inverse_edge);
1035 return(inverse_edge);
1038 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1046 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1048 inverse_affine.sx=determinant*affine->sy;
1049 inverse_affine.rx=determinant*(-affine->rx);
1050 inverse_affine.ry=determinant*(-affine->ry);
1051 inverse_affine.sy=determinant*affine->sx;
1052 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1054 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1056 return(inverse_affine);
1059 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1066 static inline double MagickMax(const double x,const double y)
1073 static inline double MagickMin(const double x,const double y)
1080 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1081 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1114 Determine bounding box.
1116 assert(image != (Image *) NULL);
1117 assert(image->signature == MagickSignature);
1118 if (image->debug != MagickFalse)
1119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1120 assert(source != (const Image *) NULL);
1121 assert(source->signature == MagickSignature);
1122 assert(affine != (AffineMatrix *) NULL);
1125 extent[1].x=(double) source->columns-1.0;
1127 extent[2].x=(double) source->columns-1.0;
1128 extent[2].y=(double) source->rows-1.0;
1130 extent[3].y=(double) source->rows-1.0;
1131 for (i=0; i < 4; i++)
1134 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1135 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1139 for (i=1; i < 4; i++)
1141 if (min.x > extent[i].x)
1143 if (min.y > extent[i].y)
1145 if (max.x < extent[i].x)
1147 if (max.y < extent[i].y)
1151 Affine transform image.
1153 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1154 return(MagickFalse);
1156 edge.x1=MagickMax(min.x,0.0);
1157 edge.y1=MagickMax(min.y,0.0);
1158 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1159 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1160 inverse_affine=InverseAffineMatrix(affine);
1161 GetPixelInfo(image,&zero);
1162 start=(ssize_t) ceil(edge.y1-0.5);
1163 stop=(ssize_t) floor(edge.y2+0.5);
1164 source_view=AcquireVirtualCacheView(source,exception);
1165 image_view=AcquireAuthenticCacheView(image,exception);
1166 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1167 #pragma omp parallel for schedule(static,4) shared(status) \
1168 magick_threads(source,image,1,1)
1170 for (y=start; y <= stop; y++)
1191 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1192 if (inverse_edge.x2 < inverse_edge.x1)
1194 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1195 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1197 if (q == (Quantum *) NULL)
1202 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1204 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1206 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1208 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1209 point.x,point.y,&pixel,exception);
1210 GetPixelInfoPixel(image,q,&composite);
1211 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1213 SetPixelInfoPixel(image,&composite,q);
1215 q+=GetPixelChannels(image);
1217 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1220 source_view=DestroyCacheView(source_view);
1221 image_view=DestroyCacheView(image_view);
1226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1230 + D r a w B o u n d i n g R e c t a n g l e s %
1234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1236 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1237 % is only useful for developers debugging the rendering algorithm.
1239 % The format of the DrawBoundingRectangles method is:
1241 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1242 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1244 % A description of each parameter follows:
1246 % o image: the image.
1248 % o draw_info: the draw info.
1250 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1252 % o exception: return any errors or warnings in this structure.
1255 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1256 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1281 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1282 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1284 resolution.x=DefaultResolution;
1285 resolution.y=DefaultResolution;
1286 if (clone_info->density != (char *) NULL)
1294 flags=ParseGeometry(clone_info->density,&geometry_info);
1295 resolution.x=geometry_info.rho;
1296 resolution.y=geometry_info.sigma;
1297 if ((flags & SigmaValue) == MagickFalse)
1298 resolution.y=resolution.x;
1300 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1301 clone_info->stroke_width/2.0;
1306 if (polygon_info != (PolygonInfo *) NULL)
1308 bounds=polygon_info->edges[0].bounds;
1309 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1311 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1312 bounds.x1=polygon_info->edges[i].bounds.x1;
1313 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1314 bounds.y1=polygon_info->edges[i].bounds.y1;
1315 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1316 bounds.x2=polygon_info->edges[i].bounds.x2;
1317 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1318 bounds.y2=polygon_info->edges[i].bounds.y2;
1321 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1322 image->columns ? (double) image->columns-1 : bounds.x1;
1324 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1325 image->rows ? (double) image->rows-1 : bounds.y1;
1327 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1328 image->columns ? (double) image->columns-1 : bounds.x2;
1330 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1331 image->rows ? (double) image->rows-1 : bounds.y2;
1332 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1334 if (polygon_info->edges[i].direction != 0)
1335 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1338 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1340 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1341 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1342 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1343 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1344 primitive_info[0].primitive=RectanglePrimitive;
1345 TraceRectangle(primitive_info,start,end);
1346 primitive_info[0].method=ReplaceMethod;
1347 coordinates=(ssize_t) primitive_info[0].coordinates;
1348 primitive_info[coordinates].primitive=UndefinedPrimitive;
1349 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1352 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1354 start.x=(double) (bounds.x1-mid);
1355 start.y=(double) (bounds.y1-mid);
1356 end.x=(double) (bounds.x2+mid);
1357 end.y=(double) (bounds.y2+mid);
1358 primitive_info[0].primitive=RectanglePrimitive;
1359 TraceRectangle(primitive_info,start,end);
1360 primitive_info[0].method=ReplaceMethod;
1361 coordinates=(ssize_t) primitive_info[0].coordinates;
1362 primitive_info[coordinates].primitive=UndefinedPrimitive;
1363 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1364 clone_info=DestroyDrawInfo(clone_info);
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372 % D r a w C l i p P a t h %
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378 % DrawClipPath() draws the clip path on the image mask.
1380 % The format of the DrawClipPath method is:
1382 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1383 % const char *name,ExceptionInfo *exception)
1385 % A description of each parameter follows:
1387 % o image: the image.
1389 % o draw_info: the draw info.
1391 % o name: the name of the clip path.
1393 % o exception: return any errors or warnings in this structure.
1396 MagickExport MagickBooleanType DrawClipPath(Image *image,
1397 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1400 filename[MaxTextExtent];
1414 assert(image != (Image *) NULL);
1415 assert(image->signature == MagickSignature);
1416 if (image->debug != MagickFalse)
1417 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1418 assert(draw_info != (const DrawInfo *) NULL);
1419 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1420 value=GetImageArtifact(image,filename);
1421 if (value == (const char *) NULL)
1422 return(MagickFalse);
1423 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1424 if (clip_mask == (Image *) NULL)
1425 return(MagickFalse);
1426 (void) QueryColorCompliance("#0000",AllCompliance,
1427 &clip_mask->background_color,exception);
1428 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1429 (void) SetImageBackgroundColor(clip_mask,exception);
1430 if (image->debug != MagickFalse)
1431 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1432 draw_info->clip_mask);
1433 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1434 (void) CloneString(&clone_info->primitive,value);
1435 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1437 clone_info->clip_mask=(char *) NULL;
1438 status=NegateImage(clip_mask,MagickFalse,exception);
1439 (void) SetImageMask(image,clip_mask,exception);
1440 clip_mask=DestroyImage(clip_mask);
1441 status=DrawImage(image,clone_info,exception);
1442 clone_info=DestroyDrawInfo(clone_info);
1443 if (image->debug != MagickFalse)
1444 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1445 return(status != 0 ? MagickTrue : MagickFalse);
1449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 + D r a w D a s h P o l y g o n %
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1460 % image while respecting the dash offset and dash pattern attributes.
1462 % The format of the DrawDashPolygon method is:
1464 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1465 % const PrimitiveInfo *primitive_info,Image *image,
1466 % ExceptionInfo *exception)
1468 % A description of each parameter follows:
1470 % o draw_info: the draw info.
1472 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1474 % o image: the image.
1476 % o exception: return any errors or warnings in this structure.
1479 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1480 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1512 assert(draw_info != (const DrawInfo *) NULL);
1513 if (image->debug != MagickFalse)
1514 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1515 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1516 clone_info->miterlimit=0;
1517 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1518 number_vertices=(size_t) i;
1519 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1520 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1521 if (dash_polygon == (PrimitiveInfo *) NULL)
1522 return(MagickFalse);
1523 dash_polygon[0]=primitive_info[0];
1524 scale=ExpandAffine(&draw_info->affine);
1525 length=scale*(draw_info->dash_pattern[0]-0.5);
1526 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1528 for (n=0; offset > 0.0; j=0)
1530 if (draw_info->dash_pattern[n] <= 0.0)
1532 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1533 if (offset > length)
1537 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1540 if (offset < length)
1552 for (i=1; i < (ssize_t) number_vertices; i++)
1554 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1555 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1556 maximum_length=hypot((double) dx,dy);
1560 if (draw_info->dash_pattern[n] == 0.0)
1562 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1564 for (total_length=0.0; (total_length+length) <= maximum_length; )
1566 total_length+=length;
1567 if ((n & 0x01) != 0)
1569 dash_polygon[0]=primitive_info[0];
1570 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1571 total_length/maximum_length);
1572 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1573 total_length/maximum_length);
1578 if ((j+1) > (ssize_t) (2*number_vertices))
1580 dash_polygon[j]=primitive_info[i-1];
1581 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1582 total_length/maximum_length);
1583 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1584 total_length/maximum_length);
1585 dash_polygon[j].coordinates=1;
1587 dash_polygon[0].coordinates=(size_t) j;
1588 dash_polygon[j].primitive=UndefinedPrimitive;
1589 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1592 if (draw_info->dash_pattern[n] == 0.0)
1594 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1596 length-=(maximum_length-total_length);
1597 if ((n & 0x01) != 0)
1599 dash_polygon[j]=primitive_info[i];
1600 dash_polygon[j].coordinates=1;
1603 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1605 dash_polygon[j]=primitive_info[i-1];
1606 dash_polygon[j].point.x+=MagickEpsilon;
1607 dash_polygon[j].point.y+=MagickEpsilon;
1608 dash_polygon[j].coordinates=1;
1610 dash_polygon[0].coordinates=(size_t) j;
1611 dash_polygon[j].primitive=UndefinedPrimitive;
1612 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1614 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1615 clone_info=DestroyDrawInfo(clone_info);
1616 if (image->debug != MagickFalse)
1617 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1618 return(status != 0 ? MagickTrue : MagickFalse);
1622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1626 % D r a w I m a g e %
1630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632 % DrawImage() draws a graphic primitive on your image. The primitive
1633 % may be represented as a string or filename. Precede the filename with an
1634 % "at" sign (@) and the contents of the file are drawn on the image. You
1635 % can affect how text is drawn by setting one or more members of the draw
1638 % The format of the DrawImage method is:
1640 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1641 % ExceptionInfo *exception)
1643 % A description of each parameter follows:
1645 % o image: the image.
1647 % o draw_info: the draw info.
1649 % o exception: return any errors or warnings in this structure.
1653 static inline MagickBooleanType IsPoint(const char *point)
1661 value=StringToDouble(point,&p);
1662 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1665 static inline void TracePoint(PrimitiveInfo *primitive_info,
1666 const PointInfo point)
1668 primitive_info->coordinates=1;
1669 primitive_info->point=point;
1672 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1673 ExceptionInfo *exception)
1675 #define RenderImageTag "Render/Image"
1682 key[2*MaxTextExtent],
1683 keyword[MaxTextExtent],
1684 geometry[MaxTextExtent],
1685 name[MaxTextExtent],
1686 pattern[MaxTextExtent],
1737 Ensure the annotation info is valid.
1739 assert(image != (Image *) NULL);
1740 assert(image->signature == MagickSignature);
1741 if (image->debug != MagickFalse)
1742 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1743 assert(draw_info != (DrawInfo *) NULL);
1744 assert(draw_info->signature == MagickSignature);
1745 if (image->debug != MagickFalse)
1746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1747 if ((draw_info->primitive == (char *) NULL) ||
1748 (*draw_info->primitive == '\0'))
1749 return(MagickFalse);
1750 if (image->debug != MagickFalse)
1751 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1752 if (*draw_info->primitive != '@')
1753 primitive=AcquireString(draw_info->primitive);
1755 primitive=FileToString(draw_info->primitive+1,~0,exception);
1756 if (primitive == (char *) NULL)
1757 return(MagickFalse);
1758 primitive_extent=(double) strlen(primitive);
1759 (void) SetImageArtifact(image,"MVG",primitive);
1762 Allocate primitive info memory.
1764 graphic_context=(DrawInfo **) AcquireMagickMemory(
1765 sizeof(*graphic_context));
1766 if (graphic_context == (DrawInfo **) NULL)
1768 primitive=DestroyString(primitive);
1769 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1773 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1774 sizeof(*primitive_info));
1775 if (primitive_info == (PrimitiveInfo *) NULL)
1777 primitive=DestroyString(primitive);
1778 for ( ; n >= 0; n--)
1779 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1780 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1781 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1784 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1785 graphic_context[n]->viewbox=image->page;
1786 if ((image->page.width == 0) || (image->page.height == 0))
1788 graphic_context[n]->viewbox.width=image->columns;
1789 graphic_context[n]->viewbox.height=image->rows;
1791 token=AcquireString(primitive);
1792 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1794 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1795 return(MagickFalse);
1797 for (q=primitive; *q != '\0'; )
1800 Interpret graphic primitive.
1802 GetMagickToken(q,&q,keyword);
1803 if (*keyword == '\0')
1805 if (*keyword == '#')
1810 while ((*q != '\n') && (*q != '\0'))
1814 p=q-strlen(keyword)-1;
1815 primitive_type=UndefinedPrimitive;
1816 current=graphic_context[n]->affine;
1817 GetAffineMatrix(&affine);
1825 if (LocaleCompare("affine",keyword) == 0)
1827 GetMagickToken(q,&q,token);
1828 affine.sx=StringToDouble(token,(char **) NULL);
1829 GetMagickToken(q,&q,token);
1831 GetMagickToken(q,&q,token);
1832 affine.rx=StringToDouble(token,(char **) NULL);
1833 GetMagickToken(q,&q,token);
1835 GetMagickToken(q,&q,token);
1836 affine.ry=StringToDouble(token,(char **) NULL);
1837 GetMagickToken(q,&q,token);
1839 GetMagickToken(q,&q,token);
1840 affine.sy=StringToDouble(token,(char **) NULL);
1841 GetMagickToken(q,&q,token);
1843 GetMagickToken(q,&q,token);
1844 affine.tx=StringToDouble(token,(char **) NULL);
1845 GetMagickToken(q,&q,token);
1847 GetMagickToken(q,&q,token);
1848 affine.ty=StringToDouble(token,(char **) NULL);
1851 if (LocaleCompare("arc",keyword) == 0)
1853 primitive_type=ArcPrimitive;
1862 if (LocaleCompare("bezier",keyword) == 0)
1864 primitive_type=BezierPrimitive;
1867 if (LocaleCompare("border-color",keyword) == 0)
1869 GetMagickToken(q,&q,token);
1870 (void) QueryColorCompliance(token,AllCompliance,
1871 &graphic_context[n]->border_color,exception);
1880 if (LocaleCompare("clip-path",keyword) == 0)
1885 GetMagickToken(q,&q,token);
1886 (void) CloneString(&graphic_context[n]->clip_mask,token);
1887 (void) DrawClipPath(image,graphic_context[n],
1888 graphic_context[n]->clip_mask,exception);
1891 if (LocaleCompare("clip-rule",keyword) == 0)
1896 GetMagickToken(q,&q,token);
1897 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1899 if (fill_rule == -1)
1902 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1905 if (LocaleCompare("clip-units",keyword) == 0)
1910 GetMagickToken(q,&q,token);
1911 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1913 if (clip_units == -1)
1918 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1919 if (clip_units == ObjectBoundingBox)
1921 GetAffineMatrix(¤t);
1922 affine.sx=draw_info->bounds.x2;
1923 affine.sy=draw_info->bounds.y2;
1924 affine.tx=draw_info->bounds.x1;
1925 affine.ty=draw_info->bounds.y1;
1930 if (LocaleCompare("circle",keyword) == 0)
1932 primitive_type=CirclePrimitive;
1935 if (LocaleCompare("color",keyword) == 0)
1937 primitive_type=ColorPrimitive;
1946 if (LocaleCompare("decorate",keyword) == 0)
1951 GetMagickToken(q,&q,token);
1952 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1957 graphic_context[n]->decorate=(DecorationType) decorate;
1966 if (LocaleCompare("ellipse",keyword) == 0)
1968 primitive_type=EllipsePrimitive;
1971 if (LocaleCompare("encoding",keyword) == 0)
1973 GetMagickToken(q,&q,token);
1974 (void) CloneString(&graphic_context[n]->encoding,token);
1983 if (LocaleCompare("fill",keyword) == 0)
1985 GetMagickToken(q,&q,token);
1986 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1987 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1988 (void) DrawPatternPath(image,draw_info,token,
1989 &graphic_context[n]->fill_pattern,exception);
1992 status=QueryColorCompliance(token,AllCompliance,
1993 &graphic_context[n]->fill,exception);
1994 if (status == MagickFalse)
1999 pattern_info=AcquireImageInfo();
2000 (void) CopyMagickString(pattern_info->filename,token,
2002 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2004 CatchException(exception);
2005 pattern_info=DestroyImageInfo(pattern_info);
2010 if (LocaleCompare("fill-alpha",keyword) == 0)
2012 GetMagickToken(q,&q,token);
2013 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2014 graphic_context[n]->fill.alpha=(double) QuantumRange*
2015 factor*StringToDouble(token,(char **) NULL);
2018 if (LocaleCompare("fill-rule",keyword) == 0)
2023 GetMagickToken(q,&q,token);
2024 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2026 if (fill_rule == -1)
2029 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2032 if (LocaleCompare("font",keyword) == 0)
2034 GetMagickToken(q,&q,token);
2035 (void) CloneString(&graphic_context[n]->font,token);
2036 if (LocaleCompare("none",token) == 0)
2037 graphic_context[n]->font=(char *)
2038 RelinquishMagickMemory(graphic_context[n]->font);
2041 if (LocaleCompare("font-family",keyword) == 0)
2043 GetMagickToken(q,&q,token);
2044 (void) CloneString(&graphic_context[n]->family,token);
2047 if (LocaleCompare("font-size",keyword) == 0)
2049 GetMagickToken(q,&q,token);
2050 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2053 if (LocaleCompare("font-stretch",keyword) == 0)
2058 GetMagickToken(q,&q,token);
2059 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2063 graphic_context[n]->stretch=(StretchType) stretch;
2066 if (LocaleCompare("font-style",keyword) == 0)
2071 GetMagickToken(q,&q,token);
2072 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2076 graphic_context[n]->style=(StyleType) style;
2079 if (LocaleCompare("font-weight",keyword) == 0)
2081 GetMagickToken(q,&q,token);
2082 graphic_context[n]->weight=StringToUnsignedLong(token);
2083 if (LocaleCompare(token,"all") == 0)
2084 graphic_context[n]->weight=0;
2085 if (LocaleCompare(token,"bold") == 0)
2086 graphic_context[n]->weight=700;
2087 if (LocaleCompare(token,"bolder") == 0)
2088 if (graphic_context[n]->weight <= 800)
2089 graphic_context[n]->weight+=100;
2090 if (LocaleCompare(token,"lighter") == 0)
2091 if (graphic_context[n]->weight >= 100)
2092 graphic_context[n]->weight-=100;
2093 if (LocaleCompare(token,"normal") == 0)
2094 graphic_context[n]->weight=400;
2103 if (LocaleCompare("gradient-units",keyword) == 0)
2105 GetMagickToken(q,&q,token);
2108 if (LocaleCompare("gravity",keyword) == 0)
2113 GetMagickToken(q,&q,token);
2114 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2118 graphic_context[n]->gravity=(GravityType) gravity;
2127 if (LocaleCompare("image",keyword) == 0)
2132 primitive_type=ImagePrimitive;
2133 GetMagickToken(q,&q,token);
2134 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2138 graphic_context[n]->compose=(CompositeOperator) compose;
2141 if (LocaleCompare("interline-spacing",keyword) == 0)
2143 GetMagickToken(q,&q,token);
2144 graphic_context[n]->interline_spacing=StringToDouble(token,
2148 if (LocaleCompare("interword-spacing",keyword) == 0)
2150 GetMagickToken(q,&q,token);
2151 graphic_context[n]->interword_spacing=StringToDouble(token,
2161 if (LocaleCompare("kerning",keyword) == 0)
2163 GetMagickToken(q,&q,token);
2164 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2173 if (LocaleCompare("line",keyword) == 0)
2174 primitive_type=LinePrimitive;
2182 if (LocaleCompare("matte",keyword) == 0)
2183 primitive_type=MattePrimitive;
2191 if (LocaleCompare("offset",keyword) == 0)
2193 GetMagickToken(q,&q,token);
2196 if (LocaleCompare("opacity",keyword) == 0)
2198 GetMagickToken(q,&q,token);
2199 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2200 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2201 QuantumScale*graphic_context[n]->alpha)*factor*
2202 StringToDouble(token,(char **) NULL))));
2203 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2204 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2213 if (LocaleCompare("path",keyword) == 0)
2215 primitive_type=PathPrimitive;
2218 if (LocaleCompare("point",keyword) == 0)
2220 primitive_type=PointPrimitive;
2223 if (LocaleCompare("polyline",keyword) == 0)
2225 primitive_type=PolylinePrimitive;
2228 if (LocaleCompare("polygon",keyword) == 0)
2230 primitive_type=PolygonPrimitive;
2233 if (LocaleCompare("pop",keyword) == 0)
2235 GetMagickToken(q,&q,token);
2236 if (LocaleCompare("clip-path",token) == 0)
2238 if (LocaleCompare("defs",token) == 0)
2240 if (LocaleCompare("gradient",token) == 0)
2242 if (LocaleCompare("graphic-context",token) == 0)
2246 (void) ThrowMagickException(exception,GetMagickModule(),
2247 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2251 if (graphic_context[n]->clip_mask != (char *) NULL)
2252 if (LocaleCompare(graphic_context[n]->clip_mask,
2253 graphic_context[n-1]->clip_mask) != 0)
2254 (void) SetImageMask(image,(Image *) NULL,exception);
2255 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2259 if (LocaleCompare("pattern",token) == 0)
2264 if (LocaleCompare("push",keyword) == 0)
2266 GetMagickToken(q,&q,token);
2267 if (LocaleCompare("clip-path",token) == 0)
2270 name[MaxTextExtent];
2272 GetMagickToken(q,&q,token);
2273 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2274 for (p=q; *q != '\0'; )
2276 GetMagickToken(q,&q,token);
2277 if (LocaleCompare(token,"pop") != 0)
2279 GetMagickToken(q,(const char **) NULL,token);
2280 if (LocaleCompare(token,"clip-path") != 0)
2284 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2285 (void) SetImageArtifact(image,name,token);
2286 GetMagickToken(q,&q,token);
2289 if (LocaleCompare("gradient",token) == 0)
2292 key[2*MaxTextExtent],
2293 name[MaxTextExtent],
2294 type[MaxTextExtent];
2299 GetMagickToken(q,&q,token);
2300 (void) CopyMagickString(name,token,MaxTextExtent);
2301 GetMagickToken(q,&q,token);
2302 (void) CopyMagickString(type,token,MaxTextExtent);
2303 GetMagickToken(q,&q,token);
2304 segment.x1=StringToDouble(token,(char **) NULL);
2305 GetMagickToken(q,&q,token);
2307 GetMagickToken(q,&q,token);
2308 segment.y1=StringToDouble(token,(char **) NULL);
2309 GetMagickToken(q,&q,token);
2311 GetMagickToken(q,&q,token);
2312 segment.x2=StringToDouble(token,(char **) NULL);
2313 GetMagickToken(q,&q,token);
2315 GetMagickToken(q,&q,token);
2316 segment.y2=StringToDouble(token,(char **) NULL);
2317 if (LocaleCompare(type,"radial") == 0)
2319 GetMagickToken(q,&q,token);
2321 GetMagickToken(q,&q,token);
2323 for (p=q; *q != '\0'; )
2325 GetMagickToken(q,&q,token);
2326 if (LocaleCompare(token,"pop") != 0)
2328 GetMagickToken(q,(const char **) NULL,token);
2329 if (LocaleCompare(token,"gradient") != 0)
2333 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2334 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2335 graphic_context[n]->affine.ry*segment.y1+
2336 graphic_context[n]->affine.tx;
2337 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2338 graphic_context[n]->affine.sy*segment.y1+
2339 graphic_context[n]->affine.ty;
2340 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2341 graphic_context[n]->affine.ry*segment.y2+
2342 graphic_context[n]->affine.tx;
2343 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2344 graphic_context[n]->affine.sy*segment.y2+
2345 graphic_context[n]->affine.ty;
2346 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2347 (void) SetImageArtifact(image,key,token);
2348 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2349 (void) FormatLocaleString(geometry,MaxTextExtent,
2350 "%gx%g%+.15g%+.15g",
2351 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2352 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2353 bounds.x1,bounds.y1);
2354 (void) SetImageArtifact(image,key,geometry);
2355 GetMagickToken(q,&q,token);
2358 if (LocaleCompare("pattern",token) == 0)
2363 GetMagickToken(q,&q,token);
2364 (void) CopyMagickString(name,token,MaxTextExtent);
2365 GetMagickToken(q,&q,token);
2366 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2368 GetMagickToken(q,&q,token);
2370 GetMagickToken(q,&q,token);
2371 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2373 GetMagickToken(q,&q,token);
2375 GetMagickToken(q,&q,token);
2376 bounds.width=(size_t) floor(StringToDouble(token,
2377 (char **) NULL)+0.5);
2378 GetMagickToken(q,&q,token);
2380 GetMagickToken(q,&q,token);
2381 bounds.height=(size_t) floor(StringToDouble(token,
2382 (char **) NULL)+0.5);
2383 for (p=q; *q != '\0'; )
2385 GetMagickToken(q,&q,token);
2386 if (LocaleCompare(token,"pop") != 0)
2388 GetMagickToken(q,(const char **) NULL,token);
2389 if (LocaleCompare(token,"pattern") != 0)
2393 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2394 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2395 (void) SetImageArtifact(image,key,token);
2396 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2397 (void) FormatLocaleString(geometry,MaxTextExtent,
2398 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2399 bounds.height,(double) bounds.x,(double) bounds.y);
2400 (void) SetImageArtifact(image,key,geometry);
2401 GetMagickToken(q,&q,token);
2404 if (LocaleCompare("graphic-context",token) == 0)
2407 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2408 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2409 if (graphic_context == (DrawInfo **) NULL)
2411 (void) ThrowMagickException(exception,GetMagickModule(),
2412 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2416 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2417 graphic_context[n-1]);
2420 if (LocaleCompare("defs",token) == 0)
2431 if (LocaleCompare("rectangle",keyword) == 0)
2433 primitive_type=RectanglePrimitive;
2436 if (LocaleCompare("rotate",keyword) == 0)
2438 GetMagickToken(q,&q,token);
2439 angle=StringToDouble(token,(char **) NULL);
2440 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2441 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2442 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2443 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2446 if (LocaleCompare("roundRectangle",keyword) == 0)
2448 primitive_type=RoundRectanglePrimitive;
2457 if (LocaleCompare("scale",keyword) == 0)
2459 GetMagickToken(q,&q,token);
2460 affine.sx=StringToDouble(token,(char **) NULL);
2461 GetMagickToken(q,&q,token);
2463 GetMagickToken(q,&q,token);
2464 affine.sy=StringToDouble(token,(char **) NULL);
2467 if (LocaleCompare("skewX",keyword) == 0)
2469 GetMagickToken(q,&q,token);
2470 angle=StringToDouble(token,(char **) NULL);
2471 affine.ry=sin(DegreesToRadians(angle));
2474 if (LocaleCompare("skewY",keyword) == 0)
2476 GetMagickToken(q,&q,token);
2477 angle=StringToDouble(token,(char **) NULL);
2478 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2481 if (LocaleCompare("stop-color",keyword) == 0)
2486 GetMagickToken(q,&q,token);
2487 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2489 (void) GradientImage(image,LinearGradient,ReflectSpread,
2490 &start_color,&stop_color,exception);
2491 start_color=stop_color;
2492 GetMagickToken(q,&q,token);
2495 if (LocaleCompare("stroke",keyword) == 0)
2497 GetMagickToken(q,&q,token);
2498 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2499 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2500 (void) DrawPatternPath(image,draw_info,token,
2501 &graphic_context[n]->stroke_pattern,exception);
2504 status=QueryColorCompliance(token,AllCompliance,
2505 &graphic_context[n]->stroke,exception);
2506 if (status == MagickFalse)
2511 pattern_info=AcquireImageInfo();
2512 (void) CopyMagickString(pattern_info->filename,token,
2514 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2516 CatchException(exception);
2517 pattern_info=DestroyImageInfo(pattern_info);
2522 if (LocaleCompare("stroke-antialias",keyword) == 0)
2524 GetMagickToken(q,&q,token);
2525 graphic_context[n]->stroke_antialias=
2526 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2529 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2531 if (graphic_context[n]->dash_pattern != (double *) NULL)
2532 graphic_context[n]->dash_pattern=(double *)
2533 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2534 if (IsPoint(q) != MagickFalse)
2540 GetMagickToken(p,&p,token);
2542 GetMagickToken(p,&p,token);
2543 for (x=0; IsPoint(token) != MagickFalse; x++)
2545 GetMagickToken(p,&p,token);
2547 GetMagickToken(p,&p,token);
2549 graphic_context[n]->dash_pattern=(double *)
2550 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2551 sizeof(*graphic_context[n]->dash_pattern));
2552 if (graphic_context[n]->dash_pattern == (double *) NULL)
2554 (void) ThrowMagickException(exception,GetMagickModule(),
2555 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2559 for (j=0; j < x; j++)
2561 GetMagickToken(q,&q,token);
2563 GetMagickToken(q,&q,token);
2564 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2567 if ((x & 0x01) != 0)
2568 for ( ; j < (2*x); j++)
2569 graphic_context[n]->dash_pattern[j]=
2570 graphic_context[n]->dash_pattern[j-x];
2571 graphic_context[n]->dash_pattern[j]=0.0;
2574 GetMagickToken(q,&q,token);
2577 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2579 GetMagickToken(q,&q,token);
2580 graphic_context[n]->dash_offset=StringToDouble(token,
2584 if (LocaleCompare("stroke-linecap",keyword) == 0)
2589 GetMagickToken(q,&q,token);
2590 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2594 graphic_context[n]->linecap=(LineCap) linecap;
2597 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2602 GetMagickToken(q,&q,token);
2603 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2608 graphic_context[n]->linejoin=(LineJoin) linejoin;
2611 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2613 GetMagickToken(q,&q,token);
2614 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2617 if (LocaleCompare("stroke-opacity",keyword) == 0)
2619 GetMagickToken(q,&q,token);
2620 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2621 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2622 factor*StringToDouble(token,(char **) NULL);
2625 if (LocaleCompare("stroke-width",keyword) == 0)
2627 GetMagickToken(q,&q,token);
2628 graphic_context[n]->stroke_width=StringToDouble(token,
2638 if (LocaleCompare("text",keyword) == 0)
2640 primitive_type=TextPrimitive;
2643 if (LocaleCompare("text-align",keyword) == 0)
2648 GetMagickToken(q,&q,token);
2649 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2653 graphic_context[n]->align=(AlignType) align;
2656 if (LocaleCompare("text-anchor",keyword) == 0)
2661 GetMagickToken(q,&q,token);
2662 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2666 graphic_context[n]->align=(AlignType) align;
2669 if (LocaleCompare("text-antialias",keyword) == 0)
2671 GetMagickToken(q,&q,token);
2672 graphic_context[n]->text_antialias=
2673 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2676 if (LocaleCompare("text-undercolor",keyword) == 0)
2678 GetMagickToken(q,&q,token);
2679 (void) QueryColorCompliance(token,AllCompliance,
2680 &graphic_context[n]->undercolor,exception);
2683 if (LocaleCompare("translate",keyword) == 0)
2685 GetMagickToken(q,&q,token);
2686 affine.tx=StringToDouble(token,(char **) NULL);
2687 GetMagickToken(q,&q,token);
2689 GetMagickToken(q,&q,token);
2690 affine.ty=StringToDouble(token,(char **) NULL);
2699 if (LocaleCompare("viewbox",keyword) == 0)
2701 GetMagickToken(q,&q,token);
2702 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2703 (char **) NULL)-0.5);
2704 GetMagickToken(q,&q,token);
2706 GetMagickToken(q,&q,token);
2707 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2708 (char **) NULL)-0.5);
2709 GetMagickToken(q,&q,token);
2711 GetMagickToken(q,&q,token);
2712 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2713 token,(char **) NULL)+0.5);
2714 GetMagickToken(q,&q,token);
2716 GetMagickToken(q,&q,token);
2717 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2718 token,(char **) NULL)+0.5);
2730 if (status == MagickFalse)
2732 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2733 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2735 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2736 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2737 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2738 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2739 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2741 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2744 if (primitive_type == UndefinedPrimitive)
2746 if (image->debug != MagickFalse)
2747 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2752 Parse the primitive attributes.
2756 primitive_info[0].point.x=0.0;
2757 primitive_info[0].point.y=0.0;
2758 for (x=0; *q != '\0'; x++)
2763 if (IsPoint(q) == MagickFalse)
2765 GetMagickToken(q,&q,token);
2766 point.x=StringToDouble(token,(char **) NULL);
2767 GetMagickToken(q,&q,token);
2769 GetMagickToken(q,&q,token);
2770 point.y=StringToDouble(token,(char **) NULL);
2771 GetMagickToken(q,(const char **) NULL,token);
2773 GetMagickToken(q,&q,token);
2774 primitive_info[i].primitive=primitive_type;
2775 primitive_info[i].point=point;
2776 primitive_info[i].coordinates=0;
2777 primitive_info[i].method=FloodfillMethod;
2779 if (i < (ssize_t) number_points)
2782 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2783 (size_t) number_points,sizeof(*primitive_info));
2784 if (primitive_info == (PrimitiveInfo *) NULL)
2786 (void) ThrowMagickException(exception,GetMagickModule(),
2787 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2791 primitive_info[j].primitive=primitive_type;
2792 primitive_info[j].coordinates=(size_t) x;
2793 primitive_info[j].method=FloodfillMethod;
2794 primitive_info[j].text=(char *) NULL;
2796 Circumscribe primitive within a circle.
2798 bounds.x1=primitive_info[j].point.x;
2799 bounds.y1=primitive_info[j].point.y;
2800 bounds.x2=primitive_info[j].point.x;
2801 bounds.y2=primitive_info[j].point.y;
2802 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2804 point=primitive_info[j+k].point;
2805 if (point.x < bounds.x1)
2807 if (point.y < bounds.y1)
2809 if (point.x > bounds.x2)
2811 if (point.y > bounds.y2)
2815 Speculate how many points our primitive might consume.
2817 length=primitive_info[j].coordinates;
2818 switch (primitive_type)
2820 case RectanglePrimitive:
2825 case RoundRectanglePrimitive:
2827 length*=5+8*BezierQuantum;
2830 case BezierPrimitive:
2832 if (primitive_info[j].coordinates > 107)
2833 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2834 "TooManyBezierCoordinates","`%s'",token);
2835 length=BezierQuantum*primitive_info[j].coordinates;
2844 GetMagickToken(q,&q,token);
2847 for (s=token; *s != '\0'; s=t)
2852 value=StringToDouble(s,&t);
2861 length=length*BezierQuantum/2;
2864 case CirclePrimitive:
2866 case EllipsePrimitive:
2873 alpha=bounds.x2-bounds.x1;
2874 beta=bounds.y2-bounds.y1;
2875 radius=hypot((double) alpha,(double) beta);
2876 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2882 if ((size_t) (i+length) >= number_points)
2885 Resize based on speculative points required by primitive.
2887 number_points+=length+1;
2888 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2889 (size_t) number_points,sizeof(*primitive_info));
2890 if (primitive_info == (PrimitiveInfo *) NULL)
2892 (void) ThrowMagickException(exception,GetMagickModule(),
2893 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2898 switch (primitive_type)
2900 case PointPrimitive:
2903 if (primitive_info[j].coordinates != 1)
2908 TracePoint(primitive_info+j,primitive_info[j].point);
2909 i=(ssize_t) (j+primitive_info[j].coordinates);
2914 if (primitive_info[j].coordinates != 2)
2919 TraceLine(primitive_info+j,primitive_info[j].point,
2920 primitive_info[j+1].point);
2921 i=(ssize_t) (j+primitive_info[j].coordinates);
2924 case RectanglePrimitive:
2926 if (primitive_info[j].coordinates != 2)
2931 TraceRectangle(primitive_info+j,primitive_info[j].point,
2932 primitive_info[j+1].point);
2933 i=(ssize_t) (j+primitive_info[j].coordinates);
2936 case RoundRectanglePrimitive:
2938 if (primitive_info[j].coordinates != 3)
2943 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2944 primitive_info[j+1].point,primitive_info[j+2].point);
2945 i=(ssize_t) (j+primitive_info[j].coordinates);
2950 if (primitive_info[j].coordinates != 3)
2952 primitive_type=UndefinedPrimitive;
2955 TraceArc(primitive_info+j,primitive_info[j].point,
2956 primitive_info[j+1].point,primitive_info[j+2].point);
2957 i=(ssize_t) (j+primitive_info[j].coordinates);
2960 case EllipsePrimitive:
2962 if (primitive_info[j].coordinates != 3)
2967 TraceEllipse(primitive_info+j,primitive_info[j].point,
2968 primitive_info[j+1].point,primitive_info[j+2].point);
2969 i=(ssize_t) (j+primitive_info[j].coordinates);
2972 case CirclePrimitive:
2974 if (primitive_info[j].coordinates != 2)
2979 TraceCircle(primitive_info+j,primitive_info[j].point,
2980 primitive_info[j+1].point);
2981 i=(ssize_t) (j+primitive_info[j].coordinates);
2984 case PolylinePrimitive:
2986 case PolygonPrimitive:
2988 primitive_info[i]=primitive_info[j];
2989 primitive_info[i].coordinates=0;
2990 primitive_info[j].coordinates++;
2994 case BezierPrimitive:
2996 if (primitive_info[j].coordinates < 3)
3001 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3002 i=(ssize_t) (j+primitive_info[j].coordinates);
3007 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3010 case ColorPrimitive:
3011 case MattePrimitive:
3016 if (primitive_info[j].coordinates != 1)
3021 GetMagickToken(q,&q,token);
3022 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3026 primitive_info[j].method=(PaintMethod) method;
3031 if (primitive_info[j].coordinates != 1)
3037 GetMagickToken(q,&q,token);
3038 primitive_info[j].text=AcquireString(token);
3041 case ImagePrimitive:
3043 if (primitive_info[j].coordinates != 2)
3048 GetMagickToken(q,&q,token);
3049 primitive_info[j].text=AcquireString(token);
3053 if (primitive_info == (PrimitiveInfo *) NULL)
3055 if (image->debug != MagickFalse)
3056 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3057 if (status == MagickFalse)
3059 primitive_info[i].primitive=UndefinedPrimitive;
3065 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3067 point=primitive_info[i].point;
3068 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3069 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3070 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3071 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3072 point=primitive_info[i].point;
3073 if (point.x < graphic_context[n]->bounds.x1)
3074 graphic_context[n]->bounds.x1=point.x;
3075 if (point.y < graphic_context[n]->bounds.y1)
3076 graphic_context[n]->bounds.y1=point.y;
3077 if (point.x > graphic_context[n]->bounds.x2)
3078 graphic_context[n]->bounds.x2=point.x;
3079 if (point.y > graphic_context[n]->bounds.y2)
3080 graphic_context[n]->bounds.y2=point.y;
3081 if (primitive_info[i].primitive == ImagePrimitive)
3083 if (i >= (ssize_t) number_points)
3084 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3086 if (graphic_context[n]->render != MagickFalse)
3088 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3089 (LocaleCompare(graphic_context[n]->clip_mask,
3090 graphic_context[n-1]->clip_mask) != 0))
3091 (void) DrawClipPath(image,graphic_context[n],
3092 graphic_context[n]->clip_mask,exception);
3093 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3095 if (primitive_info->text != (char *) NULL)
3096 primitive_info->text=(char *) RelinquishMagickMemory(
3097 primitive_info->text);
3098 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3100 if (proceed == MagickFalse)
3103 if (image->debug != MagickFalse)
3104 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3106 Relinquish resources.
3108 token=DestroyString(token);
3109 if (primitive_info != (PrimitiveInfo *) NULL)
3110 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3111 primitive=DestroyString(primitive);
3112 for ( ; n >= 0; n--)
3113 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3114 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3115 if (status == MagickFalse)
3116 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3126 % D r a w G r a d i e n t I m a g e %
3130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3132 % DrawGradientImage() draws a linear gradient on the image.
3134 % The format of the DrawGradientImage method is:
3136 % MagickBooleanType DrawGradientImage(Image *image,
3137 % const DrawInfo *draw_info,ExceptionInfo *exception)
3139 % A description of each parameter follows:
3141 % o image: the image.
3143 % o draw_info: the draw info.
3145 % o exception: return any errors or warnings in this structure.
3149 static inline double GetStopColorOffset(const GradientInfo *gradient,
3150 const ssize_t x,const ssize_t y)
3152 switch (gradient->type)
3154 case UndefinedGradient:
3155 case LinearGradient:
3170 gradient_vector=(&gradient->gradient_vector);
3171 p.x=gradient_vector->x2-gradient_vector->x1;
3172 p.y=gradient_vector->y2-gradient_vector->y1;
3173 q.x=(double) x-gradient_vector->x1;
3174 q.y=(double) y-gradient_vector->y1;
3175 length=sqrt(q.x*q.x+q.y*q.y);
3176 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3177 gamma=PerceptibleReciprocal(gamma);
3178 scale=p.x*q.x+p.y*q.y;
3179 offset=gamma*scale*length;
3182 case RadialGradient:
3191 v.x=(double) x-gradient->center.x;
3192 v.y=(double) y-gradient->center.y;
3193 length=sqrt(v.x*v.x+v.y*v.y);
3194 if (gradient->spread == RepeatSpread)
3196 offset=length/gradient->radius;
3203 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3204 const DrawInfo *draw_info,ExceptionInfo *exception)
3234 Draw linear or radial gradient on image.
3236 assert(image != (Image *) NULL);
3237 assert(image->signature == MagickSignature);
3238 if (image->debug != MagickFalse)
3239 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3240 assert(draw_info != (const DrawInfo *) NULL);
3241 gradient=(&draw_info->gradient);
3242 gradient_vector=(&gradient->gradient_vector);
3243 point.x=gradient_vector->x2-gradient_vector->x1;
3244 point.y=gradient_vector->y2-gradient_vector->y1;
3245 length=sqrt(point.x*point.x+point.y*point.y);
3246 bounding_box=gradient->bounding_box;
3248 GetPixelInfo(image,&zero);
3249 image_view=AcquireAuthenticCacheView(image,exception);
3250 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3251 #pragma omp parallel for schedule(static,4) shared(status) \
3252 magick_threads(image,image,1,1)
3254 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3274 if (status == MagickFalse)
3276 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3277 if (q == (Quantum *) NULL)
3284 offset=GetStopColorOffset(gradient,0,y);
3285 if (gradient->type != RadialGradient)
3287 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3289 GetPixelInfoPixel(image,q,&pixel);
3290 switch (gradient->spread)
3292 case UndefinedSpread:
3295 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3296 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3298 offset=GetStopColorOffset(gradient,x,y);
3299 if (gradient->type != RadialGradient)
3302 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3303 if (offset < gradient->stops[i].offset)
3305 if ((offset < 0.0) || (i == 0))
3306 composite=gradient->stops[0].color;
3308 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3309 composite=gradient->stops[gradient->number_stops-1].color;
3314 alpha=(offset-gradient->stops[i].offset)/
3315 (gradient->stops[j].offset-gradient->stops[i].offset);
3316 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3317 &gradient->stops[j].color,alpha,&composite);
3323 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3324 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3326 offset=GetStopColorOffset(gradient,x,y);
3327 if (gradient->type != RadialGradient)
3332 if ((ssize_t) fmod(offset,2.0) == 0)
3333 offset=fmod(offset,1.0);
3335 offset=1.0-fmod(offset,1.0);
3336 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3337 if (offset < gradient->stops[i].offset)
3340 composite=gradient->stops[0].color;
3342 if (i == (ssize_t) gradient->number_stops)
3343 composite=gradient->stops[gradient->number_stops-1].color;
3348 alpha=(offset-gradient->stops[i].offset)/
3349 (gradient->stops[j].offset-gradient->stops[i].offset);
3350 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3351 &gradient->stops[j].color,alpha,&composite);
3363 antialias=MagickFalse;
3365 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3366 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3368 offset=GetStopColorOffset(gradient,x,y);
3369 if (gradient->type == LinearGradient)
3371 repeat=fmod(offset,length);
3373 repeat=length-fmod(-repeat,length);
3375 repeat=fmod(offset,length);
3376 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3377 MagickTrue : MagickFalse;
3378 offset=repeat/length;
3382 repeat=fmod(offset,gradient->radius);
3384 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3386 repeat=fmod(offset,gradient->radius);
3387 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3389 offset=repeat/gradient->radius;
3392 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3393 if (offset < gradient->stops[i].offset)
3396 composite=gradient->stops[0].color;
3398 if (i == (ssize_t) gradient->number_stops)
3399 composite=gradient->stops[gradient->number_stops-1].color;
3404 alpha=(offset-gradient->stops[i].offset)/
3405 (gradient->stops[j].offset-gradient->stops[i].offset);
3406 if (antialias != MagickFalse)
3408 if (gradient->type == LinearGradient)
3409 alpha=length-repeat;
3411 alpha=gradient->radius-repeat;
3413 j=(ssize_t) gradient->number_stops-1L;
3415 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3416 &gradient->stops[j].color,alpha,&composite);
3421 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3423 SetPixelInfoPixel(image,&pixel,q);
3424 q+=GetPixelChannels(image);
3426 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3429 image_view=DestroyCacheView(image_view);
3434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3438 % D r a w P a t t e r n P a t h %
3442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444 % DrawPatternPath() draws a pattern.
3446 % The format of the DrawPatternPath method is:
3448 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3449 % const char *name,Image **pattern,ExceptionInfo *exception)
3451 % A description of each parameter follows:
3453 % o image: the image.
3455 % o draw_info: the draw info.
3457 % o name: the pattern name.
3459 % o image: the image.
3461 % o exception: return any errors or warnings in this structure.
3464 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3465 const DrawInfo *draw_info,const char *name,Image **pattern,
3466 ExceptionInfo *exception)
3469 property[MaxTextExtent];
3484 assert(image != (Image *) NULL);
3485 assert(image->signature == MagickSignature);
3486 if (image->debug != MagickFalse)
3487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3488 assert(draw_info != (const DrawInfo *) NULL);
3489 assert(name != (const char *) NULL);
3490 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3491 path=GetImageArtifact(image,property);
3492 if (path == (const char *) NULL)
3493 return(MagickFalse);
3494 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3495 geometry=GetImageArtifact(image,property);
3496 if (geometry == (const char *) NULL)
3497 return(MagickFalse);
3498 if ((*pattern) != (Image *) NULL)
3499 *pattern=DestroyImage(*pattern);
3500 image_info=AcquireImageInfo();
3501 image_info->size=AcquireString(geometry);
3502 *pattern=AcquireImage(image_info,exception);
3503 image_info=DestroyImageInfo(image_info);
3504 (void) QueryColorCompliance("#000000ff",AllCompliance,
3505 &(*pattern)->background_color,exception);
3506 (void) SetImageBackgroundColor(*pattern,exception);
3507 if (image->debug != MagickFalse)
3508 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3509 "begin pattern-path %s %s",name,geometry);
3510 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3511 clone_info->fill_pattern=NewImageList();
3512 clone_info->stroke_pattern=NewImageList();
3513 (void) CloneString(&clone_info->primitive,path);
3514 status=DrawImage(*pattern,clone_info,exception);
3515 clone_info=DestroyDrawInfo(clone_info);
3516 if (image->debug != MagickFalse)
3517 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3526 + D r a w P o l y g o n P r i m i t i v e %
3530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3532 % DrawPolygonPrimitive() draws a polygon on the image.
3534 % The format of the DrawPolygonPrimitive method is:
3536 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3537 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3538 % ExceptionInfo *exception)
3540 % A description of each parameter follows:
3542 % o image: the image.
3544 % o draw_info: the draw info.
3546 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3548 % o exception: return any errors or warnings in this structure.
3552 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3557 assert(polygon_info != (PolygonInfo **) NULL);
3558 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3559 if (polygon_info[i] != (PolygonInfo *) NULL)
3560 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3561 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3562 return(polygon_info);
3565 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3566 const PrimitiveInfo *primitive_info)
3569 *restrict path_info;
3580 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3581 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3582 sizeof(*polygon_info));
3583 if (polygon_info == (PolygonInfo **) NULL)
3584 return((PolygonInfo **) NULL);
3585 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3586 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3587 if (path_info == (PathInfo *) NULL)
3588 return(DestroyPolygonThreadSet(polygon_info));
3589 for (i=0; i < (ssize_t) number_threads; i++)
3591 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3592 if (polygon_info[i] == (PolygonInfo *) NULL)
3593 return(DestroyPolygonThreadSet(polygon_info));
3595 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3596 return(polygon_info);
3599 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3600 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3601 const ssize_t y,double *stroke_alpha)
3612 register const PointInfo
3626 Compute fill & stroke opacity for this (x,y) point.
3630 p=polygon_info->edges;
3631 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3633 if ((double) y <= (p->bounds.y1-mid-0.5))
3635 if ((double) y > (p->bounds.y2+mid+0.5))
3637 (void) DestroyEdge(polygon_info,(size_t) j);
3640 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3641 ((double) x > (p->bounds.x2+mid+0.5)))
3643 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3644 for ( ; i < (ssize_t) p->number_points; i++)
3646 if ((double) y <= (p->points[i-1].y-mid-0.5))
3648 if ((double) y > (p->points[i].y+mid+0.5))
3650 if (p->scanline != (double) y)
3652 p->scanline=(double) y;
3653 p->highwater=(size_t) i;
3656 Compute distance between a point and an edge.
3659 delta.x=(q+1)->x-q->x;
3660 delta.y=(q+1)->y-q->y;
3661 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3664 delta.x=(double) x-q->x;
3665 delta.y=(double) y-q->y;
3666 distance=delta.x*delta.x+delta.y*delta.y;
3670 alpha=delta.x*delta.x+delta.y*delta.y;
3673 delta.x=(double) x-(q+1)->x;
3674 delta.y=(double) y-(q+1)->y;
3675 distance=delta.x*delta.x+delta.y*delta.y;
3680 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3681 distance=alpha*beta*beta;
3685 Compute stroke & subpath opacity.
3688 if (p->ghostline == MagickFalse)
3691 if ((*stroke_alpha < 1.0) &&
3692 (distance <= ((alpha+0.25)*(alpha+0.25))))
3695 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3700 if (distance != 1.0)
3701 beta=sqrt((double) distance);
3703 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3704 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3708 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3710 if (distance <= 0.0)
3720 if (distance != 1.0)
3721 beta=sqrt(distance);
3724 if (subpath_alpha < (alpha*alpha))
3725 subpath_alpha=alpha*alpha;
3729 Compute fill opacity.
3731 if (fill == MagickFalse)
3733 if (subpath_alpha >= 1.0)
3736 Determine winding number.
3739 p=polygon_info->edges;
3740 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3742 if ((double) y <= p->bounds.y1)
3744 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3746 if ((double) x > p->bounds.x2)
3748 winding_number+=p->direction ? 1 : -1;
3751 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3752 for ( ; i < (ssize_t) p->number_points; i++)
3753 if ((double) y <= p->points[i].y)
3756 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3757 winding_number+=p->direction ? 1 : -1;
3759 if (fill_rule != NonZeroRule)
3761 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3765 if (MagickAbsoluteValue(winding_number) != 0)
3767 return(subpath_alpha);
3770 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3771 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3772 ExceptionInfo *exception)
3785 **restrict polygon_info;
3802 Compute bounding box.
3804 assert(image != (Image *) NULL);
3805 assert(image->signature == MagickSignature);
3806 if (image->debug != MagickFalse)
3807 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3808 assert(draw_info != (DrawInfo *) NULL);
3809 assert(draw_info->signature == MagickSignature);
3810 assert(primitive_info != (PrimitiveInfo *) NULL);
3811 if (primitive_info->coordinates == 0)
3813 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3814 if (polygon_info == (PolygonInfo **) NULL)
3815 return(MagickFalse);
3817 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3818 if (image->debug != MagickFalse)
3819 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3820 fill=(primitive_info->method == FillToBorderMethod) ||
3821 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3822 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3823 bounds=polygon_info[0]->edges[0].bounds;
3824 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3826 p=polygon_info[0]->edges+i;
3827 if (p->bounds.x1 < bounds.x1)
3828 bounds.x1=p->bounds.x1;
3829 if (p->bounds.y1 < bounds.y1)
3830 bounds.y1=p->bounds.y1;
3831 if (p->bounds.x2 > bounds.x2)
3832 bounds.x2=p->bounds.x2;
3833 if (p->bounds.y2 > bounds.y2)
3834 bounds.y2=p->bounds.y2;
3836 bounds.x1-=(mid+1.0);
3837 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3838 image->columns ? (double) image->columns-1 : bounds.x1;
3839 bounds.y1-=(mid+1.0);
3840 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3841 image->rows ? (double) image->rows-1 : bounds.y1;
3842 bounds.x2+=(mid+1.0);
3843 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3844 image->columns ? (double) image->columns-1 : bounds.x2;
3845 bounds.y2+=(mid+1.0);
3846 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3847 image->rows ? (double) image->rows-1 : bounds.y2;
3849 image_view=AcquireAuthenticCacheView(image,exception);
3850 if (primitive_info->coordinates == 1)
3855 start=(ssize_t) ceil(bounds.y1-0.5);
3856 stop=(ssize_t) floor(bounds.y2+0.5);
3857 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3858 #pragma omp parallel for schedule(static,4) shared(status) \
3859 magick_threads(image,image,1,1)
3861 for (y=start; y <= stop; y++)
3879 if (status == MagickFalse)
3881 start=(ssize_t) ceil(bounds.x1-0.5);
3882 stop=(ssize_t) floor(bounds.x2+0.5);
3884 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3886 if (q == (Quantum *) NULL)
3891 GetPixelInfo(image,&pixel);
3892 for ( ; x <= stop; x++)
3894 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3895 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3897 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3898 SetPixelInfoPixel(image,&pixel,q);
3900 q+=GetPixelChannels(image);
3902 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3903 if (sync == MagickFalse)
3906 image_view=DestroyCacheView(image_view);
3907 polygon_info=DestroyPolygonThreadSet(polygon_info);
3908 if (image->debug != MagickFalse)
3909 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3910 " end draw-polygon");
3914 Draw polygon or line.
3916 if (image->alpha_trait != BlendPixelTrait)
3917 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3918 start=(ssize_t) ceil(bounds.y1-0.5);
3919 stop=(ssize_t) floor(bounds.y2+0.5);
3920 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3921 #pragma omp parallel for schedule(static,4) shared(status) \
3922 magick_threads(image,image,1,1)
3924 for (y=start; y <= stop; y++)
3927 id = GetOpenMPThreadId();
3947 if (status == MagickFalse)
3949 start=(ssize_t) ceil(bounds.x1-0.5);
3950 stop=(ssize_t) floor(bounds.x2+0.5);
3951 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3953 if (q == (Quantum *) NULL)
3958 for (x=start; x <= stop; x++)
3963 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3965 if (draw_info->stroke_antialias == MagickFalse)
3967 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3968 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3970 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3971 fill_alpha=fill_alpha*fill_color.alpha;
3972 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
3973 GetPixelAlpha(image,q),q);
3974 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3975 stroke_alpha=stroke_alpha*stroke_color.alpha;
3976 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
3977 GetPixelAlpha(image,q),q);
3978 q+=GetPixelChannels(image);
3980 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3983 image_view=DestroyCacheView(image_view);
3984 polygon_info=DestroyPolygonThreadSet(polygon_info);
3985 if (image->debug != MagickFalse)
3986 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3995 % D r a w P r i m i t i v e %
3999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4001 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4003 % The format of the DrawPrimitive method is:
4005 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4006 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4008 % A description of each parameter follows:
4010 % o image: the image.
4012 % o draw_info: the draw info.
4014 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4016 % o exception: return any errors or warnings in this structure.
4020 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4046 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4047 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4048 switch (primitive_info->primitive)
4050 case PointPrimitive:
4052 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4053 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4054 methods[primitive_info->method]);
4057 case ColorPrimitive:
4059 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4060 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4061 methods[primitive_info->method]);
4064 case MattePrimitive:
4066 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4067 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4068 methods[primitive_info->method]);
4073 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4074 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4077 case ImagePrimitive:
4079 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4080 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4087 p=primitive_info[0].point;
4090 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4092 point=primitive_info[i].point;
4093 if (coordinates <= 0)
4095 coordinates=(ssize_t) primitive_info[i].coordinates;
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4097 " begin open (%.20g)",(double) coordinates);
4100 point=primitive_info[i].point;
4101 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4102 (fabs(q.y-point.y) >= MagickEpsilon))
4103 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4104 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4106 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4107 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4110 if (coordinates > 0)
4112 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4113 (fabs(p.y-point.y) >= MagickEpsilon))
4114 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4115 (double) coordinates);
4117 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4118 (double) coordinates);
4122 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4123 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4124 ExceptionInfo *exception)
4139 if (image->debug != MagickFalse)
4141 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4142 " begin draw-primitive");
4143 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4144 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4145 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4146 draw_info->affine.tx,draw_info->affine.ty);
4148 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4149 ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
4150 (IsPixelGray(&draw_info->stroke) == MagickFalse)))
4151 (void) TransformImageColorspace(image,sRGBColorspace,exception);
4153 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4154 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4155 image_view=AcquireAuthenticCacheView(image,exception);
4156 switch (primitive_info->primitive)
4158 case PointPrimitive:
4166 if ((y < 0) || (y >= (ssize_t) image->rows))
4168 if ((x < 0) || (x >= (ssize_t) image->columns))
4170 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4171 if (q == (Quantum *) NULL)
4173 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4174 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4175 (double) GetPixelAlpha(image,q),q);
4176 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4179 case ColorPrimitive:
4181 switch (primitive_info->method)
4192 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4193 if (q == (Quantum *) NULL)
4195 GetPixelInfo(image,&pixel);
4196 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4197 SetPixelInfoPixel(image,&pixel,q);
4198 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4210 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4212 for (y=0; y < (ssize_t) image->rows; y++)
4217 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4219 if (q == (Quantum *) NULL)
4221 for (x=0; x < (ssize_t) image->columns; x++)
4223 GetPixelInfoPixel(image,q,&pixel);
4224 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4226 q+=GetPixelChannels(image);
4229 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4230 SetPixelInfoPixel(image,&pixel,q);
4231 q+=GetPixelChannels(image);
4233 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4234 if (sync == MagickFalse)
4239 case FloodfillMethod:
4240 case FillToBorderMethod:
4245 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4247 if (primitive_info->method == FillToBorderMethod)
4249 target.red=(double) draw_info->border_color.red;
4250 target.green=(double) draw_info->border_color.green;
4251 target.blue=(double) draw_info->border_color.blue;
4253 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4254 primitive_info->method == FloodfillMethod ? MagickFalse :
4255 MagickTrue,exception);
4266 GetPixelInfo(image,&pixel);
4267 for (y=0; y < (ssize_t) image->rows; y++)
4275 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4277 if (q == (Quantum *) NULL)
4279 for (x=0; x < (ssize_t) image->columns; x++)
4281 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4282 SetPixelInfoPixel(image,&pixel,q);
4283 q+=GetPixelChannels(image);
4285 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4286 if (sync == MagickFalse)
4294 case MattePrimitive:
4296 if (image->alpha_trait != BlendPixelTrait)
4297 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4298 switch (primitive_info->method)
4309 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4310 if (q == (Quantum *) NULL)
4312 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4313 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4314 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4326 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4328 for (y=0; y < (ssize_t) image->rows; y++)
4336 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4338 if (q == (Quantum *) NULL)
4340 for (x=0; x < (ssize_t) image->columns; x++)
4342 GetPixelInfoPixel(image,q,&pixel);
4343 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4345 q+=GetPixelChannels(image);
4348 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4349 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4350 q+=GetPixelChannels(image);
4352 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4353 if (sync == MagickFalse)
4358 case FloodfillMethod:
4359 case FillToBorderMethod:
4367 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4369 if (primitive_info->method == FillToBorderMethod)
4371 target.red=(double) draw_info->border_color.red;
4372 target.green=(double) draw_info->border_color.green;
4373 target.blue=(double) draw_info->border_color.blue;
4375 channel_mask=SetImageChannelMask(image,AlphaChannel);
4376 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4377 primitive_info->method == FloodfillMethod ? MagickFalse :
4378 MagickTrue,exception);
4379 (void) SetImageChannelMask(image,channel_mask);
4390 for (y=0; y < (ssize_t) image->rows; y++)
4398 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4400 if (q == (Quantum *) NULL)
4402 for (x=0; x < (ssize_t) image->columns; x++)
4404 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4405 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4406 q+=GetPixelChannels(image);
4408 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4409 if (sync == MagickFalse)
4420 geometry[MaxTextExtent];
4425 if (primitive_info->text == (char *) NULL)
4427 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4428 (void) CloneString(&clone_info->text,primitive_info->text);
4429 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4430 primitive_info->point.x,primitive_info->point.y);
4431 (void) CloneString(&clone_info->geometry,geometry);
4432 status=AnnotateImage(image,clone_info,exception);
4433 clone_info=DestroyDrawInfo(clone_info);
4436 case ImagePrimitive:
4442 composite_geometry[MaxTextExtent];
4457 if (primitive_info->text == (char *) NULL)
4459 clone_info=AcquireImageInfo();
4460 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4461 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4465 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4467 composite_image=ReadImage(clone_info,exception);
4469 clone_info=DestroyImageInfo(clone_info);
4470 if (composite_image == (Image *) NULL)
4472 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4473 NULL,(void *) NULL);
4474 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4475 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4476 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4477 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4480 geometry[MaxTextExtent];
4485 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4486 primitive_info[1].point.x,primitive_info[1].point.y);
4487 composite_image->filter=image->filter;
4488 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4491 if (composite_image->alpha_trait != BlendPixelTrait)
4492 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4494 if (draw_info->alpha != OpaqueAlpha)
4495 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4496 SetGeometry(image,&geometry);
4497 image->gravity=draw_info->gravity;
4500 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4501 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4502 composite_image->rows,(double) geometry.x,(double) geometry.y);
4503 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4504 affine=draw_info->affine;
4505 affine.tx=(double) geometry.x;
4506 affine.ty=(double) geometry.y;
4507 composite_image->interpolate=image->interpolate;
4508 if (draw_info->compose == OverCompositeOp)
4509 (void) DrawAffineImage(image,composite_image,&affine,exception);
4511 (void) CompositeImage(image,composite_image,draw_info->compose,
4512 MagickTrue,geometry.x,geometry.y,exception);
4513 composite_image=DestroyImage(composite_image);
4525 if (IsEventLogging() != MagickFalse)
4526 LogPrimitiveInfo(primitive_info);
4527 scale=ExpandAffine(&draw_info->affine);
4528 if ((draw_info->dash_pattern != (double *) NULL) &&
4529 (draw_info->dash_pattern[0] != 0.0) &&
4530 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4531 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4536 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4537 clone_info->stroke_width=0.0;
4538 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4539 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4541 clone_info=DestroyDrawInfo(clone_info);
4542 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4545 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4547 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4553 Draw strokes while respecting line cap/join attributes.
4555 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4557 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4558 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4559 MagickTrue : MagickFalse;
4560 i=(ssize_t) primitive_info[0].coordinates;
4561 if ((((draw_info->linecap == RoundCap) ||
4562 (closed_path != MagickFalse)) &&
4563 (draw_info->linejoin == RoundJoin)) ||
4564 (primitive_info[i].primitive != UndefinedPrimitive))
4566 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4570 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4571 clone_info->stroke_width=0.0;
4572 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4573 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4575 clone_info=DestroyDrawInfo(clone_info);
4576 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4579 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4583 image_view=DestroyCacheView(image_view);
4584 if (image->debug != MagickFalse)
4585 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4586 return(status != 0 ? MagickTrue : MagickFalse);
4590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4594 + D r a w S t r o k e P o l y g o n %
4598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4600 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4601 % the image while respecting the line cap and join attributes.
4603 % The format of the DrawStrokePolygon method is:
4605 % MagickBooleanType DrawStrokePolygon(Image *image,
4606 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4608 % A description of each parameter follows:
4610 % o image: the image.
4612 % o draw_info: the draw info.
4614 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4619 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4620 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4628 for (i=0; i < 4; i++)
4629 linecap[i]=(*primitive_info);
4630 linecap[0].coordinates=4;
4631 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4632 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4633 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4634 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4635 linecap[4].primitive=UndefinedPrimitive;
4636 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4639 static MagickBooleanType DrawStrokePolygon(Image *image,
4640 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4641 ExceptionInfo *exception)
4653 register const PrimitiveInfo
4658 Draw stroked polygon.
4660 if (image->debug != MagickFalse)
4661 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4662 " begin draw-stroke-polygon");
4663 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4664 clone_info->fill=draw_info->stroke;
4665 if (clone_info->fill_pattern != (Image *) NULL)
4666 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4667 if (clone_info->stroke_pattern != (Image *) NULL)
4668 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4669 MagickTrue,exception);
4670 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4671 clone_info->stroke_width=0.0;
4672 clone_info->fill_rule=NonZeroRule;
4674 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4676 stroke_polygon=TraceStrokePolygon(draw_info,p);
4677 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4678 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4679 q=p+p->coordinates-1;
4680 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4681 MagickTrue : MagickFalse;
4682 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4684 DrawRoundLinecap(image,draw_info,p,exception);
4685 DrawRoundLinecap(image,draw_info,q,exception);
4688 clone_info=DestroyDrawInfo(clone_info);
4689 if (image->debug != MagickFalse)
4690 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4691 " end draw-stroke-polygon");
4696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4700 % G e t A f f i n e M a t r i x %
4704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4706 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4709 % The format of the GetAffineMatrix method is:
4711 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4713 % A description of each parameter follows:
4715 % o affine_matrix: the affine matrix.
4718 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4720 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4721 assert(affine_matrix != (AffineMatrix *) NULL);
4722 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4723 affine_matrix->sx=1.0;
4724 affine_matrix->sy=1.0;
4728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4732 + G e t D r a w I n f o %
4736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4738 % GetDrawInfo() initializes draw_info to default values from image_info.
4740 % The format of the GetDrawInfo method is:
4742 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4744 % A description of each parameter follows:
4746 % o image_info: the image info..
4748 % o draw_info: the draw info.
4751 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4760 Initialize draw attributes.
4762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4763 assert(draw_info != (DrawInfo *) NULL);
4764 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4765 GetAffineMatrix(&draw_info->affine);
4766 exception=AcquireExceptionInfo();
4767 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4769 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4771 draw_info->stroke_width=1.0;
4772 draw_info->alpha=OpaqueAlpha;
4773 draw_info->fill_rule=EvenOddRule;
4774 draw_info->linecap=ButtCap;
4775 draw_info->linejoin=MiterJoin;
4776 draw_info->miterlimit=10;
4777 draw_info->decorate=NoDecoration;
4778 draw_info->pointsize=12.0;
4779 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4780 draw_info->compose=OverCompositeOp;
4781 draw_info->render=MagickTrue;
4782 draw_info->debug=IsEventLogging();
4783 if (image_info != (ImageInfo *) NULL)
4785 draw_info->stroke_antialias=image_info->antialias;
4786 if (image_info->font != (char *) NULL)
4787 draw_info->font=AcquireString(image_info->font);
4788 if (image_info->density != (char *) NULL)
4789 draw_info->density=AcquireString(image_info->density);
4790 draw_info->text_antialias=image_info->antialias;
4791 if (image_info->pointsize != 0.0)
4792 draw_info->pointsize=image_info->pointsize;
4793 draw_info->border_color=image_info->border_color;
4794 if (image_info->server_name != (char *) NULL)
4795 draw_info->server_name=AcquireString(image_info->server_name);
4796 option=GetImageOption(image_info,"encoding");
4797 if (option != (const char *) NULL)
4798 (void) CloneString(&draw_info->encoding,option);
4799 option=GetImageOption(image_info,"kerning");
4800 if (option != (const char *) NULL)
4801 draw_info->kerning=StringToDouble(option,(char **) NULL);
4802 option=GetImageOption(image_info,"interline-spacing");
4803 if (option != (const char *) NULL)
4804 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4805 option=GetImageOption(image_info,"interword-spacing");
4806 if (option != (const char *) NULL)
4807 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4808 option=GetImageOption(image_info,"direction");
4809 if (option != (const char *) NULL)
4810 draw_info->direction=(DirectionType) ParseCommandOption(
4811 MagickDirectionOptions,MagickFalse,option);
4813 draw_info->direction=UndefinedDirection;
4814 option=GetImageOption(image_info,"fill");
4815 if (option != (const char *) NULL)
4816 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4818 option=GetImageOption(image_info,"stroke");
4819 if (option != (const char *) NULL)
4820 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4822 option=GetImageOption(image_info,"strokewidth");
4823 if (option != (const char *) NULL)
4824 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4825 option=GetImageOption(image_info,"undercolor");
4826 if (option != (const char *) NULL)
4827 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4829 option=GetImageOption(image_info,"gravity");
4830 if (option != (const char *) NULL)
4831 draw_info->gravity=(GravityType) ParseCommandOption(
4832 MagickGravityOptions,MagickFalse,option);
4834 exception=DestroyExceptionInfo(exception);
4835 draw_info->signature=MagickSignature;
4839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4843 + P e r m u t a t e %
4847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4849 % Permutate() returns the permuation of the (n,k).
4851 % The format of the Permutate method is:
4853 % void Permutate(ssize_t n,ssize_t k)
4855 % A description of each parameter follows:
4863 static inline double Permutate(const ssize_t n,const ssize_t k)
4872 for (i=k+1; i <= n; i++)
4874 for (i=1; i <= (n-k); i++)
4880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4884 + T r a c e P r i m i t i v e %
4888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4890 % TracePrimitive is a collection of methods for generating graphic
4891 % primitives such as arcs, ellipses, paths, etc.
4895 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4896 const PointInfo end,const PointInfo degrees)
4902 center.x=0.5*(end.x+start.x);
4903 center.y=0.5*(end.y+start.y);
4904 radii.x=fabs(center.x-start.x);
4905 radii.y=fabs(center.y-start.y);
4906 TraceEllipse(primitive_info,center,radii,degrees);
4909 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4910 const PointInfo end,const PointInfo arc,const double angle,
4911 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4930 register PrimitiveInfo
4939 if ((start.x == end.x) && (start.y == end.y))
4941 TracePoint(primitive_info,end);
4944 radii.x=fabs(arc.x);
4945 radii.y=fabs(arc.y);
4946 if ((radii.x == 0.0) || (radii.y == 0.0))
4948 TraceLine(primitive_info,start,end);
4951 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4952 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4953 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4954 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4955 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4957 if (delta < MagickEpsilon)
4959 TraceLine(primitive_info,start,end);
4964 radii.x*=sqrt((double) delta);
4965 radii.y*=sqrt((double) delta);
4967 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4968 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4969 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4970 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4971 alpha=points[1].x-points[0].x;
4972 beta=points[1].y-points[0].y;
4973 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
4978 factor=sqrt((double) factor);
4979 if (sweep == large_arc)
4982 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4983 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4984 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4985 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4986 if ((theta < 0.0) && (sweep != MagickFalse))
4987 theta+=(double) (2.0*MagickPI);
4989 if ((theta > 0.0) && (sweep == MagickFalse))
4990 theta-=(double) (2.0*MagickPI);
4991 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
4994 for (i=0; i < (ssize_t) arc_segments; i++)
4996 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4997 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4998 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4999 sin(fmod((double) beta,DegreesToRadians(360.0)));
5000 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5001 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5002 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5003 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5004 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5005 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5006 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5007 theta/arc_segments),DegreesToRadians(360.0))));
5008 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5009 theta/arc_segments),DegreesToRadians(360.0))));
5010 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5011 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5012 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5013 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5014 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5015 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5016 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5018 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5020 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5022 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5024 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5026 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5028 if (i == (ssize_t) (arc_segments-1))
5033 primitive_info->coordinates=(size_t) (p-primitive_info);
5034 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5036 p->primitive=primitive_info->primitive;
5041 static void TraceBezier(PrimitiveInfo *primitive_info,
5042 const size_t number_coordinates)
5054 register PrimitiveInfo
5066 Allocate coeficients.
5068 quantum=number_coordinates;
5069 for (i=0; i < (ssize_t) number_coordinates; i++)
5071 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5073 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5074 if (alpha > (double) quantum)
5075 quantum=(size_t) alpha;
5076 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5077 if (alpha > (double) quantum)
5078 quantum=(size_t) alpha;
5081 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5082 (double) BezierQuantum);
5083 control_points=quantum*number_coordinates;
5084 coefficients=(double *) AcquireQuantumMemory((size_t)
5085 number_coordinates,sizeof(*coefficients));
5086 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5088 if ((coefficients == (double *) NULL) ||
5089 (points == (PointInfo *) NULL))
5090 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5092 Compute bezier points.
5094 end=primitive_info[number_coordinates-1].point;
5095 for (i=0; i < (ssize_t) number_coordinates; i++)
5096 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5098 for (i=0; i < (ssize_t) control_points; i++)
5103 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5104 for (j=0; j < (ssize_t) number_coordinates; j++)
5106 point.x+=alpha*coefficients[j]*p->point.x;
5107 point.y+=alpha*coefficients[j]*p->point.y;
5108 alpha*=weight/(1.0-weight);
5112 weight+=1.0/control_points;
5115 Bezier curves are just short segmented polys.
5118 for (i=0; i < (ssize_t) control_points; i++)
5120 TracePoint(p,points[i]);
5125 primitive_info->coordinates=(size_t) (p-primitive_info);
5126 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5128 p->primitive=primitive_info->primitive;
5131 points=(PointInfo *) RelinquishMagickMemory(points);
5132 coefficients=(double *) RelinquishMagickMemory(coefficients);
5135 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5136 const PointInfo end)
5147 alpha=end.x-start.x;
5149 radius=hypot((double) alpha,(double) beta);
5150 offset.x=(double) radius;
5151 offset.y=(double) radius;
5154 TraceEllipse(primitive_info,start,offset,degrees);
5157 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5158 const PointInfo stop,const PointInfo degrees)
5169 register PrimitiveInfo
5176 Ellipses are just short segmented polys.
5178 if ((stop.x == 0.0) && (stop.y == 0.0))
5180 TracePoint(primitive_info,start);
5183 delta=2.0/MagickMax(stop.x,stop.y);
5184 step=(double) (MagickPI/8.0);
5185 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5186 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5187 angle.x=DegreesToRadians(degrees.x);
5189 while (y < degrees.x)
5191 angle.y=(double) DegreesToRadians(y);
5192 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5194 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5195 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5196 TracePoint(p,point);
5199 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5200 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5201 TracePoint(p,point);
5203 primitive_info->coordinates=(size_t) (p-primitive_info);
5204 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5206 p->primitive=primitive_info->primitive;
5211 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5212 const PointInfo end)
5214 TracePoint(primitive_info,start);
5215 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5216 (fabs(start.y-end.y) < MagickEpsilon))
5218 primitive_info->primitive=PointPrimitive;
5219 primitive_info->coordinates=1;
5222 TracePoint(primitive_info+1,end);
5223 (primitive_info+1)->primitive=primitive_info->primitive;
5224 primitive_info->coordinates=2;
5227 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5230 token[MaxTextExtent];
5252 register PrimitiveInfo
5267 number_coordinates=0;
5269 primitive_type=primitive_info->primitive;
5271 for (p=path; *p != '\0'; )
5273 while (isspace((int) ((unsigned char) *p)) != 0)
5277 last_attribute=attribute;
5278 attribute=(int) (*p++);
5299 GetMagickToken(p,&p,token);
5301 GetMagickToken(p,&p,token);
5302 arc.x=StringToDouble(token,(char **) NULL);
5303 GetMagickToken(p,&p,token);
5305 GetMagickToken(p,&p,token);
5306 arc.y=StringToDouble(token,(char **) NULL);
5307 GetMagickToken(p,&p,token);
5309 GetMagickToken(p,&p,token);
5310 angle=StringToDouble(token,(char **) NULL);
5311 GetMagickToken(p,&p,token);
5313 GetMagickToken(p,&p,token);
5314 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5315 GetMagickToken(p,&p,token);
5317 GetMagickToken(p,&p,token);
5318 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5319 GetMagickToken(p,&p,token);
5321 GetMagickToken(p,&p,token);
5322 x=StringToDouble(token,(char **) NULL);
5323 GetMagickToken(p,&p,token);
5325 GetMagickToken(p,&p,token);
5326 y=StringToDouble(token,(char **) NULL);
5327 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5328 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5329 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5332 while (isspace((int) ((unsigned char) *p)) != 0)
5336 } while (IsPoint(p) != MagickFalse);
5343 Compute bezier points.
5348 for (i=1; i < 4; i++)
5350 GetMagickToken(p,&p,token);
5352 GetMagickToken(p,&p,token);
5353 x=StringToDouble(token,(char **) NULL);
5354 GetMagickToken(p,&p,token);
5356 GetMagickToken(p,&p,token);
5357 y=StringToDouble(token,(char **) NULL);
5358 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5359 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5362 for (i=0; i < 4; i++)
5363 (q+i)->point=points[i];
5367 } while (IsPoint(p) != MagickFalse);
5375 GetMagickToken(p,&p,token);
5377 GetMagickToken(p,&p,token);
5378 x=StringToDouble(token,(char **) NULL);
5379 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5380 TracePoint(q,point);
5382 } while (IsPoint(p) != MagickFalse);
5390 GetMagickToken(p,&p,token);
5392 GetMagickToken(p,&p,token);
5393 x=StringToDouble(token,(char **) NULL);
5394 GetMagickToken(p,&p,token);
5396 GetMagickToken(p,&p,token);
5397 y=StringToDouble(token,(char **) NULL);
5398 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5399 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5400 TracePoint(q,point);
5402 } while (IsPoint(p) != MagickFalse);
5408 if (q != primitive_info)
5410 primitive_info->coordinates=(size_t) (q-primitive_info);
5411 number_coordinates+=primitive_info->coordinates;
5417 GetMagickToken(p,&p,token);
5419 GetMagickToken(p,&p,token);
5420 x=StringToDouble(token,(char **) NULL);
5421 GetMagickToken(p,&p,token);
5423 GetMagickToken(p,&p,token);
5424 y=StringToDouble(token,(char **) NULL);
5425 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5426 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5430 TracePoint(q,point);
5432 if ((i != 0) && (attribute == (int) 'M'))
5434 TracePoint(q,point);
5437 } while (IsPoint(p) != MagickFalse);
5444 Compute bezier points.
5449 for (i=1; i < 3; i++)
5451 GetMagickToken(p,&p,token);
5453 GetMagickToken(p,&p,token);
5454 x=StringToDouble(token,(char **) NULL);
5455 GetMagickToken(p,&p,token);
5457 GetMagickToken(p,&p,token);
5458 y=StringToDouble(token,(char **) NULL);
5461 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5462 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5465 for (i=0; i < 3; i++)
5466 (q+i)->point=points[i];
5470 } while (IsPoint(p) != MagickFalse);
5477 Compute bezier points.
5481 points[0]=points[3];
5482 points[1].x=2.0*points[3].x-points[2].x;
5483 points[1].y=2.0*points[3].y-points[2].y;
5484 for (i=2; i < 4; i++)
5486 GetMagickToken(p,&p,token);
5488 GetMagickToken(p,&p,token);
5489 x=StringToDouble(token,(char **) NULL);
5490 GetMagickToken(p,&p,token);
5492 GetMagickToken(p,&p,token);
5493 y=StringToDouble(token,(char **) NULL);
5496 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5497 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5500 if (strchr("CcSs",last_attribute) == (char *) NULL)
5502 points[0]=points[2];
5503 points[1]=points[3];
5505 for (i=0; i < 4; i++)
5506 (q+i)->point=points[i];
5510 } while (IsPoint(p) != MagickFalse);
5517 Compute bezier points.
5521 points[0]=points[2];
5522 points[1].x=2.0*points[2].x-points[1].x;
5523 points[1].y=2.0*points[2].y-points[1].y;
5524 for (i=2; i < 3; i++)
5526 GetMagickToken(p,&p,token);
5528 GetMagickToken(p,&p,token);
5529 x=StringToDouble(token,(char **) NULL);
5530 GetMagickToken(p,&p,token);
5532 GetMagickToken(p,&p,token);
5533 y=StringToDouble(token,(char **) NULL);
5534 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5535 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5538 if (strchr("QqTt",last_attribute) == (char *) NULL)
5540 points[0]=points[2];
5541 points[1]=points[3];
5543 for (i=0; i < 3; i++)
5544 (q+i)->point=points[i];
5548 } while (IsPoint(p) != MagickFalse);
5556 GetMagickToken(p,&p,token);
5558 GetMagickToken(p,&p,token);
5559 y=StringToDouble(token,(char **) NULL);
5560 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5561 TracePoint(q,point);
5563 } while (IsPoint(p) != MagickFalse);
5570 TracePoint(q,point);
5572 primitive_info->coordinates=(size_t) (q-primitive_info);
5573 number_coordinates+=primitive_info->coordinates;
5580 if (isalpha((int) ((unsigned char) attribute)) != 0)
5581 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5587 primitive_info->coordinates=(size_t) (q-primitive_info);
5588 number_coordinates+=primitive_info->coordinates;
5589 for (i=0; i < (ssize_t) number_coordinates; i++)
5592 q->primitive=primitive_type;
5594 q->method=FillToBorderMethod;
5597 return(number_coordinates);
5600 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5601 const PointInfo end)
5606 register PrimitiveInfo
5613 TracePoint(p,start);
5617 TracePoint(p,point);
5623 TracePoint(p,point);
5625 TracePoint(p,start);
5627 primitive_info->coordinates=(size_t) (p-primitive_info);
5628 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5630 p->primitive=primitive_info->primitive;
5635 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5636 const PointInfo start,const PointInfo end,PointInfo arc)
5643 register PrimitiveInfo
5650 offset.x=fabs(end.x-start.x);
5651 offset.y=fabs(end.y-start.y);
5652 if (arc.x > (0.5*offset.x))
5654 if (arc.y > (0.5*offset.y))
5656 point.x=start.x+offset.x-arc.x;
5657 point.y=start.y+arc.y;
5660 TraceEllipse(p,point,arc,degrees);
5662 point.x=start.x+offset.x-arc.x;
5663 point.y=start.y+offset.y-arc.y;
5666 TraceEllipse(p,point,arc,degrees);
5668 point.x=start.x+arc.x;
5669 point.y=start.y+offset.y-arc.y;
5672 TraceEllipse(p,point,arc,degrees);
5674 point.x=start.x+arc.x;
5675 point.y=start.y+arc.y;
5678 TraceEllipse(p,point,arc,degrees);
5680 TracePoint(p,primitive_info->point);
5682 primitive_info->coordinates=(size_t) (p-primitive_info);
5683 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5685 p->primitive=primitive_info->primitive;
5690 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5691 const size_t number_vertices,const double offset)
5708 for (i=1; i < (ssize_t) number_vertices; i++)
5710 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5711 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5712 if ((fabs((double) dx) >= MagickEpsilon) ||
5713 (fabs((double) dy) >= MagickEpsilon))
5716 if (i == (ssize_t) number_vertices)
5717 i=(ssize_t) number_vertices-1L;
5718 distance=hypot((double) dx,(double) dy);
5719 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5720 dx*(distance+offset)/distance);
5721 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5722 dy*(distance+offset)/distance);
5723 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5725 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5726 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5727 if ((fabs((double) dx) >= MagickEpsilon) ||
5728 (fabs((double) dy) >= MagickEpsilon))
5731 distance=hypot((double) dx,(double) dy);
5732 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5733 dx*(distance+offset)/distance);
5734 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5735 dy*(distance+offset)/distance);
5738 static inline double DrawEpsilonReciprocal(const double x)
5740 #define DrawEpsilon ((double) 1.0e-10)
5742 double sign = x < (double) 0.0 ? (double) -1.0 :
5744 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5745 (double) 1.0/DrawEpsilon));
5748 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5749 const PrimitiveInfo *primitive_info)
5751 typedef struct _LineSegment
5803 number_vertices=primitive_info->coordinates;
5804 max_strokes=2*number_vertices+6*BezierQuantum+360;
5805 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5807 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5809 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5810 number_vertices+2UL,sizeof(*polygon_primitive));
5811 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5812 (polygon_primitive == (PrimitiveInfo *) NULL))
5813 return((PrimitiveInfo *) NULL);
5814 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5815 number_vertices*sizeof(*polygon_primitive));
5817 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5818 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5819 MagickTrue : MagickFalse;
5820 if ((draw_info->linejoin == RoundJoin) ||
5821 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5823 polygon_primitive[number_vertices]=primitive_info[1];
5826 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5828 Compute the slope for the first line segment, p.
5832 for (n=1; n < (ssize_t) number_vertices; n++)
5834 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5835 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5836 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5839 if (n == (ssize_t) number_vertices)
5840 n=(ssize_t) number_vertices-1L;
5841 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5842 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5843 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5844 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5846 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5847 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5848 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5849 offset.y=(double) (offset.x*inverse_slope.p);
5850 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5852 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5853 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5854 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5855 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5856 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5857 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5858 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5859 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5863 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5864 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5865 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5866 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5867 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5868 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5869 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5870 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5873 Create strokes for the line join attribute: bevel, miter, round.
5877 path_q[p++]=box_q[0];
5878 path_p[q++]=box_p[0];
5879 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5882 Compute the slope for this line segment, q.
5884 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5885 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5886 dot_product=dx.q*dx.q+dy.q*dy.q;
5887 if (dot_product < 0.25)
5889 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5890 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5891 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5892 offset.y=(double) (offset.x*inverse_slope.q);
5893 dot_product=dy.q*offset.x-dx.q*offset.y;
5894 if (dot_product > 0.0)
5896 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5897 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5898 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5899 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5900 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5901 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5902 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5903 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5907 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5908 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5909 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5910 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5911 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5912 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5913 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5914 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5916 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5923 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5924 box_p[3].y)/(slope.p-slope.q));
5925 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5926 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5927 box_q[3].y)/(slope.p-slope.q));
5928 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5930 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5932 max_strokes+=6*BezierQuantum+360;
5933 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5935 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5937 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5939 polygon_primitive=(PrimitiveInfo *)
5940 RelinquishMagickMemory(polygon_primitive);
5941 return((PrimitiveInfo *) NULL);
5944 dot_product=dx.q*dy.p-dx.p*dy.q;
5945 if (dot_product <= 0.0)
5946 switch (draw_info->linejoin)
5950 path_q[q++]=box_q[1];
5951 path_q[q++]=box_q[2];
5952 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5953 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5954 if (dot_product <= miterlimit)
5955 path_p[p++]=box_p[4];
5958 path_p[p++]=box_p[1];
5959 path_p[p++]=box_p[2];
5965 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5966 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5967 if (dot_product <= miterlimit)
5969 path_q[q++]=box_q[4];
5970 path_p[p++]=box_p[4];
5974 path_q[q++]=box_q[1];
5975 path_q[q++]=box_q[2];
5976 path_p[p++]=box_p[1];
5977 path_p[p++]=box_p[2];
5983 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5984 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5985 if (dot_product <= miterlimit)
5986 path_p[p++]=box_p[4];
5989 path_p[p++]=box_p[1];
5990 path_p[p++]=box_p[2];
5992 center=polygon_primitive[n].point;
5993 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
5994 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
5995 if (theta.q < theta.p)
5996 theta.q+=(double) (2.0*MagickPI);
5997 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
5998 (2.0*sqrt((double) (1.0/mid)))));
5999 path_q[q].x=box_q[1].x;
6000 path_q[q].y=box_q[1].y;
6002 for (j=1; j < (ssize_t) arc_segments; j++)
6004 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6005 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6006 (theta.p+delta_theta),DegreesToRadians(360.0))));
6007 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6008 (theta.p+delta_theta),DegreesToRadians(360.0))));
6011 path_q[q++]=box_q[2];
6018 switch (draw_info->linejoin)
6022 path_p[p++]=box_p[1];
6023 path_p[p++]=box_p[2];
6024 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6025 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6026 if (dot_product <= miterlimit)
6027 path_q[q++]=box_q[4];
6030 path_q[q++]=box_q[1];
6031 path_q[q++]=box_q[2];
6037 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6038 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6039 if (dot_product <= miterlimit)
6041 path_q[q++]=box_q[4];
6042 path_p[p++]=box_p[4];
6046 path_q[q++]=box_q[1];
6047 path_q[q++]=box_q[2];
6048 path_p[p++]=box_p[1];
6049 path_p[p++]=box_p[2];
6055 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6056 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6057 if (dot_product <= miterlimit)
6058 path_q[q++]=box_q[4];
6061 path_q[q++]=box_q[1];
6062 path_q[q++]=box_q[2];
6064 center=polygon_primitive[n].point;
6065 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6066 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6067 if (theta.p < theta.q)
6068 theta.p+=(double) (2.0*MagickPI);
6069 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6070 (2.0*sqrt((double) (1.0/mid)))));
6071 path_p[p++]=box_p[1];
6072 for (j=1; j < (ssize_t) arc_segments; j++)
6074 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6075 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6076 (theta.p+delta_theta),DegreesToRadians(360.0))));
6077 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6078 (theta.p+delta_theta),DegreesToRadians(360.0))));
6081 path_p[p++]=box_p[2];
6088 inverse_slope.p=inverse_slope.q;
6097 path_p[p++]=box_p[1];
6098 path_q[q++]=box_q[1];
6100 Trace stroked polygon.
6102 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6103 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6104 if (stroke_polygon != (PrimitiveInfo *) NULL)
6106 for (i=0; i < (ssize_t) p; i++)
6108 stroke_polygon[i]=polygon_primitive[0];
6109 stroke_polygon[i].point=path_p[i];
6111 if (closed_path != MagickFalse)
6113 stroke_polygon[i]=polygon_primitive[0];
6114 stroke_polygon[i].point=stroke_polygon[0].point;
6117 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6119 stroke_polygon[i]=polygon_primitive[0];
6120 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6122 if (closed_path != MagickFalse)
6124 stroke_polygon[i]=polygon_primitive[0];
6125 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6128 stroke_polygon[i]=polygon_primitive[0];
6129 stroke_polygon[i].point=stroke_polygon[0].point;
6131 stroke_polygon[i].primitive=UndefinedPrimitive;
6132 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6134 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6135 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6136 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6137 return(stroke_polygon);