2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/color.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/constitute.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/draw-private.h"
61 #include "MagickCore/enhance.h"
62 #include "MagickCore/exception.h"
63 #include "MagickCore/exception-private.h"
64 #include "MagickCore/gem.h"
65 #include "MagickCore/geometry.h"
66 #include "MagickCore/image-private.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/log.h"
69 #include "MagickCore/monitor.h"
70 #include "MagickCore/monitor-private.h"
71 #include "MagickCore/option.h"
72 #include "MagickCore/paint.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/pixel-private.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/resample.h"
77 #include "MagickCore/resample-private.h"
78 #include "MagickCore/resource_.h"
79 #include "MagickCore/string_.h"
80 #include "MagickCore/string-private.h"
81 #include "MagickCore/thread-private.h"
82 #include "MagickCore/token.h"
83 #include "MagickCore/transform.h"
84 #include "MagickCore/utility.h"
89 #define BezierQuantum 200
90 #define DrawEpsilon ((MagickRealType) 1.0e-12)
95 typedef struct _EdgeInfo
119 typedef struct _ElementInfo
129 typedef struct _PolygonInfo
147 typedef struct _PathInfo
157 Forward declarations.
159 static MagickBooleanType
160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
167 TracePath(PrimitiveInfo *,const char *);
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
172 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
173 TraceBezier(PrimitiveInfo *,const size_t),
174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
176 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
177 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
180 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % A c q u i r e D r a w I n f o %
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
195 % The format of the AcquireDrawInfo method is:
197 % DrawInfo *AcquireDrawInfo(void)
200 MagickExport DrawInfo *AcquireDrawInfo(void)
205 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
206 if (draw_info == (DrawInfo *) NULL)
207 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
208 GetDrawInfo((ImageInfo *) NULL,draw_info);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % C l o n e D r a w I n f o %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
224 % is specified, a new draw_info structure is created initialized to
225 % default values, according to the given image_info.
227 % The format of the CloneDrawInfo method is:
229 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
230 % const DrawInfo *draw_info)
232 % A description of each parameter follows:
234 % o image_info: the image info.
236 % o draw_info: the draw info.
239 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
240 const DrawInfo *draw_info)
248 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
249 if (clone_info == (DrawInfo *) NULL)
250 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
251 GetDrawInfo(image_info,clone_info);
252 if (draw_info == (DrawInfo *) NULL)
254 exception=AcquireExceptionInfo();
255 (void) CloneString(&clone_info->primitive,draw_info->primitive);
256 (void) CloneString(&clone_info->geometry,draw_info->geometry);
257 clone_info->viewbox=draw_info->viewbox;
258 clone_info->affine=draw_info->affine;
259 clone_info->gravity=draw_info->gravity;
260 clone_info->fill=draw_info->fill;
261 clone_info->stroke=draw_info->stroke;
262 clone_info->stroke_width=draw_info->stroke_width;
263 if (draw_info->fill_pattern != (Image *) NULL)
264 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
266 if (draw_info->stroke_pattern != (Image *) NULL)
267 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
268 MagickTrue,exception);
269 clone_info->stroke_antialias=draw_info->stroke_antialias;
270 clone_info->text_antialias=draw_info->text_antialias;
271 clone_info->fill_rule=draw_info->fill_rule;
272 clone_info->linecap=draw_info->linecap;
273 clone_info->linejoin=draw_info->linejoin;
274 clone_info->miterlimit=draw_info->miterlimit;
275 clone_info->dash_offset=draw_info->dash_offset;
276 clone_info->decorate=draw_info->decorate;
277 clone_info->compose=draw_info->compose;
278 (void) CloneString(&clone_info->text,draw_info->text);
279 (void) CloneString(&clone_info->font,draw_info->font);
280 (void) CloneString(&clone_info->metrics,draw_info->metrics);
281 (void) CloneString(&clone_info->family,draw_info->family);
282 clone_info->style=draw_info->style;
283 clone_info->stretch=draw_info->stretch;
284 clone_info->weight=draw_info->weight;
285 (void) CloneString(&clone_info->encoding,draw_info->encoding);
286 clone_info->pointsize=draw_info->pointsize;
287 clone_info->kerning=draw_info->kerning;
288 clone_info->interline_spacing=draw_info->interline_spacing;
289 clone_info->interword_spacing=draw_info->interword_spacing;
290 clone_info->direction=draw_info->direction;
291 (void) CloneString(&clone_info->density,draw_info->density);
292 clone_info->align=draw_info->align;
293 clone_info->undercolor=draw_info->undercolor;
294 clone_info->border_color=draw_info->border_color;
295 (void) CloneString(&clone_info->server_name,draw_info->server_name);
296 if (draw_info->dash_pattern != (double *) NULL)
301 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
302 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
303 sizeof(*clone_info->dash_pattern));
304 if (clone_info->dash_pattern == (double *) NULL)
305 ThrowFatalException(ResourceLimitFatalError,
306 "UnableToAllocateDashPattern");
307 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
308 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
310 clone_info->gradient=draw_info->gradient;
311 if (draw_info->gradient.stops != (StopInfo *) NULL)
316 number_stops=clone_info->gradient.number_stops;
317 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
318 number_stops,sizeof(*clone_info->gradient.stops));
319 if (clone_info->gradient.stops == (StopInfo *) NULL)
320 ThrowFatalException(ResourceLimitFatalError,
321 "UnableToAllocateDashPattern");
322 (void) CopyMagickMemory(clone_info->gradient.stops,
323 draw_info->gradient.stops,(size_t) number_stops*
324 sizeof(*clone_info->gradient.stops));
326 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
327 clone_info->bounds=draw_info->bounds;
328 clone_info->clip_units=draw_info->clip_units;
329 clone_info->render=draw_info->render;
330 clone_info->alpha=draw_info->alpha;
331 clone_info->element_reference=draw_info->element_reference;
332 clone_info->debug=IsEventLogging();
333 exception=DestroyExceptionInfo(exception);
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 + C o n v e r t P a t h T o P o l y g o n %
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % ConvertPathToPolygon() converts a path to the more efficient sorted
351 % The format of the ConvertPathToPolygon method is:
353 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
354 % const PathInfo *path_info)
356 % A description of each parameter follows:
358 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
359 % rendering form of type PolygonInfo.
361 % o draw_info: Specifies a pointer to an DrawInfo structure.
363 % o path_info: Specifies a pointer to an PathInfo structure.
368 #if defined(__cplusplus) || defined(c_plusplus)
372 static int CompareEdges(const void *x,const void *y)
374 register const EdgeInfo
381 p=(const EdgeInfo *) x;
382 q=(const EdgeInfo *) y;
383 if ((p->points[0].y-DrawEpsilon) > q->points[0].y)
385 if ((p->points[0].y+DrawEpsilon) < q->points[0].y)
387 if ((p->points[0].x-DrawEpsilon) > q->points[0].x)
389 if ((p->points[0].x+DrawEpsilon) < q->points[0].x)
391 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
392 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
397 #if defined(__cplusplus) || defined(c_plusplus)
401 static void LogPolygonInfo(const PolygonInfo *polygon_info)
410 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
411 p=polygon_info->edges;
412 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
414 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
416 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
417 p->direction != MagickFalse ? "down" : "up");
418 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
419 p->ghostline != MagickFalse ? "transparent" : "opaque");
420 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
421 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
422 p->bounds.x2,p->bounds.y2);
423 for (j=0; j < (ssize_t) p->number_points; j++)
424 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
425 p->points[j].x,p->points[j].y);
428 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
431 static void ReversePoints(PointInfo *points,const size_t number_points)
439 for (i=0; i < (ssize_t) (number_points >> 1); i++)
442 points[i]=points[number_points-(i+1)];
443 points[number_points-(i+1)]=point;
447 static PolygonInfo *ConvertPathToPolygon(
448 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
477 Convert a path to the more efficient sorted rendering form.
479 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
480 if (polygon_info == (PolygonInfo *) NULL)
481 return((PolygonInfo *) NULL);
483 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
484 sizeof(*polygon_info->edges));
485 if (polygon_info->edges == (EdgeInfo *) NULL)
486 return((PolygonInfo *) NULL);
489 ghostline=MagickFalse;
492 points=(PointInfo *) NULL;
493 (void) ResetMagickMemory(&point,0,sizeof(point));
494 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
495 for (i=0; path_info[i].code != EndCode; i++)
497 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
498 (path_info[i].code == GhostlineCode))
503 if ((points != (PointInfo *) NULL) && (n >= 2))
505 if (edge == number_edges)
508 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
509 polygon_info->edges,(size_t) number_edges,
510 sizeof(*polygon_info->edges));
511 if (polygon_info->edges == (EdgeInfo *) NULL)
512 return((PolygonInfo *) NULL);
514 polygon_info->edges[edge].number_points=(size_t) n;
515 polygon_info->edges[edge].scanline=(-1.0);
516 polygon_info->edges[edge].highwater=0;
517 polygon_info->edges[edge].ghostline=ghostline;
518 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
520 ReversePoints(points,(size_t) n);
521 polygon_info->edges[edge].points=points;
522 polygon_info->edges[edge].bounds=bounds;
523 polygon_info->edges[edge].bounds.y1=points[0].y;
524 polygon_info->edges[edge].bounds.y2=points[n-1].y;
525 points=(PointInfo *) NULL;
526 ghostline=MagickFalse;
529 if (points == (PointInfo *) NULL)
532 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
534 if (points == (PointInfo *) NULL)
535 return((PolygonInfo *) NULL);
537 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
538 point=path_info[i].point;
549 next_direction=((path_info[i].point.y > point.y) ||
550 ((path_info[i].point.y == point.y) &&
551 (path_info[i].point.x > point.x))) ? 1 : -1;
552 if ((direction != 0) && (direction != next_direction))
558 if (edge == number_edges)
561 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
562 polygon_info->edges,(size_t) number_edges,
563 sizeof(*polygon_info->edges));
564 if (polygon_info->edges == (EdgeInfo *) NULL)
565 return((PolygonInfo *) NULL);
567 polygon_info->edges[edge].number_points=(size_t) n;
568 polygon_info->edges[edge].scanline=(-1.0);
569 polygon_info->edges[edge].highwater=0;
570 polygon_info->edges[edge].ghostline=ghostline;
571 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
573 ReversePoints(points,(size_t) n);
574 polygon_info->edges[edge].points=points;
575 polygon_info->edges[edge].bounds=bounds;
576 polygon_info->edges[edge].bounds.y1=points[0].y;
577 polygon_info->edges[edge].bounds.y2=points[n-1].y;
579 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
581 if (points == (PointInfo *) NULL)
582 return((PolygonInfo *) NULL);
584 ghostline=MagickFalse;
590 direction=next_direction;
591 if (points == (PointInfo *) NULL)
593 if (n == (ssize_t) number_points)
596 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
598 if (points == (PointInfo *) NULL)
599 return((PolygonInfo *) NULL);
601 point=path_info[i].point;
603 if (point.x < bounds.x1)
605 if (point.x > bounds.x2)
609 if (points != (PointInfo *) NULL)
612 points=(PointInfo *) RelinquishMagickMemory(points);
615 if (edge == number_edges)
618 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
619 polygon_info->edges,(size_t) number_edges,
620 sizeof(*polygon_info->edges));
621 if (polygon_info->edges == (EdgeInfo *) NULL)
622 return((PolygonInfo *) NULL);
624 polygon_info->edges[edge].number_points=(size_t) n;
625 polygon_info->edges[edge].scanline=(-1.0);
626 polygon_info->edges[edge].highwater=0;
627 polygon_info->edges[edge].ghostline=ghostline;
628 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
630 ReversePoints(points,(size_t) n);
631 polygon_info->edges[edge].points=points;
632 polygon_info->edges[edge].bounds=bounds;
633 polygon_info->edges[edge].bounds.y1=points[0].y;
634 polygon_info->edges[edge].bounds.y2=points[n-1].y;
635 ghostline=MagickFalse;
639 polygon_info->number_edges=edge;
640 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
641 sizeof(*polygon_info->edges),CompareEdges);
642 if (IsEventLogging() != MagickFalse)
643 LogPolygonInfo(polygon_info);
644 return(polygon_info);
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 + C o n v e r t P r i m i t i v e T o P a t h %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
661 % The format of the ConvertPrimitiveToPath method is:
663 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
664 % const PrimitiveInfo *primitive_info)
666 % A description of each parameter follows:
668 % o Method ConvertPrimitiveToPath returns a vector path structure of type
671 % o draw_info: a structure of type DrawInfo.
673 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
678 static void LogPathInfo(const PathInfo *path_info)
680 register const PathInfo
683 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
684 for (p=path_info; p->code != EndCode; p++)
685 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
686 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
687 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
688 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
690 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
693 static PathInfo *ConvertPrimitiveToPath(
694 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
715 Converts a PrimitiveInfo structure into a vector path structure.
717 switch (primitive_info->primitive)
724 return((PathInfo *) NULL);
728 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
729 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
731 if (path_info == (PathInfo *) NULL)
732 return((PathInfo *) NULL);
740 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
743 if (coordinates <= 0)
745 coordinates=(ssize_t) primitive_info[i].coordinates;
746 p=primitive_info[i].point;
752 Eliminate duplicate points.
754 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= DrawEpsilon) ||
755 (fabs(q.y-primitive_info[i].point.y) >= DrawEpsilon))
757 path_info[n].code=code;
758 path_info[n].point=primitive_info[i].point;
759 q=primitive_info[i].point;
764 if ((fabs(p.x-primitive_info[i].point.x) < DrawEpsilon) &&
765 (fabs(p.y-primitive_info[i].point.y) < DrawEpsilon))
768 Mark the p point as open if it does not match the q.
770 path_info[start].code=OpenCode;
771 path_info[n].code=GhostlineCode;
772 path_info[n].point=primitive_info[i].point;
774 path_info[n].code=LineToCode;
775 path_info[n].point=p;
778 path_info[n].code=EndCode;
779 path_info[n].point.x=0.0;
780 path_info[n].point.y=0.0;
781 if (IsEventLogging() != MagickFalse)
782 LogPathInfo(path_info);
787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % D e s t r o y D r a w I n f o %
795 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
800 % The format of the DestroyDrawInfo method is:
802 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
804 % A description of each parameter follows:
806 % o draw_info: the draw info.
809 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
811 if (draw_info->debug != MagickFalse)
812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
813 assert(draw_info != (DrawInfo *) NULL);
814 assert(draw_info->signature == MagickSignature);
815 if (draw_info->primitive != (char *) NULL)
816 draw_info->primitive=DestroyString(draw_info->primitive);
817 if (draw_info->text != (char *) NULL)
818 draw_info->text=DestroyString(draw_info->text);
819 if (draw_info->geometry != (char *) NULL)
820 draw_info->geometry=DestroyString(draw_info->geometry);
821 if (draw_info->fill_pattern != (Image *) NULL)
822 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
823 if (draw_info->stroke_pattern != (Image *) NULL)
824 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
825 if (draw_info->font != (char *) NULL)
826 draw_info->font=DestroyString(draw_info->font);
827 if (draw_info->metrics != (char *) NULL)
828 draw_info->metrics=DestroyString(draw_info->metrics);
829 if (draw_info->family != (char *) NULL)
830 draw_info->family=DestroyString(draw_info->family);
831 if (draw_info->encoding != (char *) NULL)
832 draw_info->encoding=DestroyString(draw_info->encoding);
833 if (draw_info->density != (char *) NULL)
834 draw_info->density=DestroyString(draw_info->density);
835 if (draw_info->server_name != (char *) NULL)
836 draw_info->server_name=(char *)
837 RelinquishMagickMemory(draw_info->server_name);
838 if (draw_info->dash_pattern != (double *) NULL)
839 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
840 draw_info->dash_pattern);
841 if (draw_info->gradient.stops != (StopInfo *) NULL)
842 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
843 draw_info->gradient.stops);
844 if (draw_info->clip_mask != (char *) NULL)
845 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
846 draw_info->signature=(~MagickSignature);
847 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856 + D e s t r o y E d g e %
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % DestroyEdge() destroys the specified polygon edge.
864 % The format of the DestroyEdge method is:
866 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
868 % A description of each parameter follows:
870 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
872 % o edge: the polygon edge number to destroy.
875 static size_t DestroyEdge(PolygonInfo *polygon_info,
878 assert(edge < polygon_info->number_edges);
879 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
880 polygon_info->edges[edge].points);
881 polygon_info->number_edges--;
882 if (edge < polygon_info->number_edges)
883 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
884 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
885 return(polygon_info->number_edges);
889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893 + D e s t r o y P o l y g o n I n f o %
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
901 % The format of the DestroyPolygonInfo method is:
903 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
905 % A description of each parameter follows:
907 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
910 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
915 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
916 polygon_info->edges[i].points=(PointInfo *)
917 RelinquishMagickMemory(polygon_info->edges[i].points);
918 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
919 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % D r a w A f f i n e I m a g e %
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 % DrawAffineImage() composites the source over the destination image as
934 % dictated by the affine transform.
936 % The format of the DrawAffineImage method is:
938 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
939 % const AffineMatrix *affine,ExceptionInfo *exception)
941 % A description of each parameter follows:
943 % o image: the image.
945 % o source: the source image.
947 % o affine: the affine transform.
949 % o exception: return any errors or warnings in this structure.
953 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
954 const double y,const SegmentInfo *edge)
967 Determine left and right edges.
969 inverse_edge.x1=edge->x1;
970 inverse_edge.y1=edge->y1;
971 inverse_edge.x2=edge->x2;
972 inverse_edge.y2=edge->y2;
973 z=affine->ry*y+affine->tx;
974 if (affine->sx >= DrawEpsilon)
976 intercept=(-z/affine->sx);
978 if (x > inverse_edge.x1)
980 intercept=(-z+(double) image->columns)/affine->sx;
982 if (x < inverse_edge.x2)
986 if (affine->sx < -DrawEpsilon)
988 intercept=(-z+(double) image->columns)/affine->sx;
990 if (x > inverse_edge.x1)
992 intercept=(-z/affine->sx);
994 if (x < inverse_edge.x2)
998 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1000 inverse_edge.x2=edge->x1;
1001 return(inverse_edge);
1004 Determine top and bottom edges.
1006 z=affine->sy*y+affine->ty;
1007 if (affine->rx >= DrawEpsilon)
1009 intercept=(-z/affine->rx);
1011 if (x > inverse_edge.x1)
1013 intercept=(-z+(double) image->rows)/affine->rx;
1015 if (x < inverse_edge.x2)
1019 if (affine->rx < -DrawEpsilon)
1021 intercept=(-z+(double) image->rows)/affine->rx;
1023 if (x > inverse_edge.x1)
1025 intercept=(-z/affine->rx);
1027 if (x < inverse_edge.x2)
1031 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1033 inverse_edge.x2=edge->x2;
1034 return(inverse_edge);
1036 return(inverse_edge);
1039 static inline MagickRealType DrawEpsilonReciprocal(const MagickRealType x)
1041 MagickRealType sign = x < (MagickRealType) 0.0 ? (MagickRealType) -1.0 :
1042 (MagickRealType) 1.0;
1043 return((sign*x) > DrawEpsilon ? (MagickRealType) 1.0/x : sign*(
1044 (MagickRealType) 1.0/DrawEpsilon));
1047 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1055 determinant=DrawEpsilonReciprocal(affine->sx*affine->sy-affine->rx*
1057 inverse_affine.sx=determinant*affine->sy;
1058 inverse_affine.rx=determinant*(-affine->rx);
1059 inverse_affine.ry=determinant*(-affine->ry);
1060 inverse_affine.sy=determinant*affine->sx;
1061 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1063 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1065 return(inverse_affine);
1068 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1075 static inline double MagickMax(const double x,const double y)
1082 static inline double MagickMin(const double x,const double y)
1089 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1090 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1127 Determine bounding box.
1129 assert(image != (Image *) NULL);
1130 assert(image->signature == MagickSignature);
1131 if (image->debug != MagickFalse)
1132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1133 assert(source != (const Image *) NULL);
1134 assert(source->signature == MagickSignature);
1135 assert(affine != (AffineMatrix *) NULL);
1138 extent[1].x=(double) source->columns-1.0;
1140 extent[2].x=(double) source->columns-1.0;
1141 extent[2].y=(double) source->rows-1.0;
1143 extent[3].y=(double) source->rows-1.0;
1144 for (i=0; i < 4; i++)
1147 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1148 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1152 for (i=1; i < 4; i++)
1154 if (min.x > extent[i].x)
1156 if (min.y > extent[i].y)
1158 if (max.x < extent[i].x)
1160 if (max.y < extent[i].y)
1164 Affine transform image.
1166 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1167 return(MagickFalse);
1169 edge.x1=MagickMax(min.x,0.0);
1170 edge.y1=MagickMax(min.y,0.0);
1171 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1172 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1173 inverse_affine=InverseAffineMatrix(affine);
1174 GetPixelInfo(image,&zero);
1175 start=(ssize_t) ceil(edge.y1-0.5);
1176 stop=(ssize_t) ceil(edge.y2-0.5);
1177 height=(size_t) (floor(edge.y2+0.5)-ceil(edge.y1-0.5));
1178 width=(size_t) (floor(edge.x2+0.5)-ceil(edge.x1-0.5));
1179 source_view=AcquireVirtualCacheView(source,exception);
1180 image_view=AcquireAuthenticCacheView(image,exception);
1181 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1182 #pragma omp parallel for schedule(static) shared(status) \
1183 dynamic_number_threads(image,width,height,1)
1185 for (y=start; y <= stop; y++)
1206 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1207 if (inverse_edge.x2 < inverse_edge.x1)
1209 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1210 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
1211 inverse_edge.x1+0.5)+1),1,exception);
1212 if (q == (Quantum *) NULL)
1217 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1219 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1221 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1223 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1224 point.x,point.y,&pixel,exception);
1225 GetPixelInfoPixel(image,q,&composite);
1226 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1228 SetPixelInfoPixel(image,&composite,q);
1230 q+=GetPixelChannels(image);
1232 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1235 source_view=DestroyCacheView(source_view);
1236 image_view=DestroyCacheView(image_view);
1241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245 + D r a w B o u n d i n g R e c t a n g l e s %
1249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1251 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1252 % is only useful for developers debugging the rendering algorithm.
1254 % The format of the DrawBoundingRectangles method is:
1256 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1257 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1259 % A description of each parameter follows:
1261 % o image: the image.
1263 % o draw_info: the draw info.
1265 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1267 % o exception: return any errors or warnings in this structure.
1270 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1271 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1296 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1297 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1299 resolution.x=DefaultResolution;
1300 resolution.y=DefaultResolution;
1301 if (clone_info->density != (char *) NULL)
1309 flags=ParseGeometry(clone_info->density,&geometry_info);
1310 resolution.x=geometry_info.rho;
1311 resolution.y=geometry_info.sigma;
1312 if ((flags & SigmaValue) == MagickFalse)
1313 resolution.y=resolution.x;
1315 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1316 clone_info->stroke_width/2.0;
1321 if (polygon_info != (PolygonInfo *) NULL)
1323 bounds=polygon_info->edges[0].bounds;
1324 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1326 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1327 bounds.x1=polygon_info->edges[i].bounds.x1;
1328 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1329 bounds.y1=polygon_info->edges[i].bounds.y1;
1330 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1331 bounds.x2=polygon_info->edges[i].bounds.x2;
1332 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1333 bounds.y2=polygon_info->edges[i].bounds.y2;
1336 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1337 image->columns ? (double) image->columns-1 : bounds.x1;
1339 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1340 image->rows ? (double) image->rows-1 : bounds.y1;
1342 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1343 image->columns ? (double) image->columns-1 : bounds.x2;
1345 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1346 image->rows ? (double) image->rows-1 : bounds.y2;
1347 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1349 if (polygon_info->edges[i].direction != 0)
1350 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1353 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1355 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1356 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1357 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1358 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1359 primitive_info[0].primitive=RectanglePrimitive;
1360 TraceRectangle(primitive_info,start,end);
1361 primitive_info[0].method=ReplaceMethod;
1362 coordinates=(ssize_t) primitive_info[0].coordinates;
1363 primitive_info[coordinates].primitive=UndefinedPrimitive;
1364 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1367 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1369 start.x=(double) (bounds.x1-mid);
1370 start.y=(double) (bounds.y1-mid);
1371 end.x=(double) (bounds.x2+mid);
1372 end.y=(double) (bounds.y2+mid);
1373 primitive_info[0].primitive=RectanglePrimitive;
1374 TraceRectangle(primitive_info,start,end);
1375 primitive_info[0].method=ReplaceMethod;
1376 coordinates=(ssize_t) primitive_info[0].coordinates;
1377 primitive_info[coordinates].primitive=UndefinedPrimitive;
1378 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1379 clone_info=DestroyDrawInfo(clone_info);
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % D r a w C l i p P a t h %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % DrawClipPath() draws the clip path on the image mask.
1395 % The format of the DrawClipPath method is:
1397 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1398 % const char *name,ExceptionInfo *exception)
1400 % A description of each parameter follows:
1402 % o image: the image.
1404 % o draw_info: the draw info.
1406 % o name: the name of the clip path.
1408 % o exception: return any errors or warnings in this structure.
1411 MagickExport MagickBooleanType DrawClipPath(Image *image,
1412 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1415 filename[MaxTextExtent];
1429 assert(image != (Image *) NULL);
1430 assert(image->signature == MagickSignature);
1431 if (image->debug != MagickFalse)
1432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1433 assert(draw_info != (const DrawInfo *) NULL);
1434 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1435 value=GetImageArtifact(image,filename);
1436 if (value == (const char *) NULL)
1437 return(MagickFalse);
1438 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1439 if (clip_mask == (Image *) NULL)
1440 return(MagickFalse);
1441 (void) QueryColorCompliance("#0000",AllCompliance,
1442 &clip_mask->background_color,exception);
1443 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1444 (void) SetImageBackgroundColor(clip_mask,exception);
1445 if (image->debug != MagickFalse)
1446 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1447 draw_info->clip_mask);
1448 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1449 (void) CloneString(&clone_info->primitive,value);
1450 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1452 clone_info->clip_mask=(char *) NULL;
1453 status=NegateImage(clip_mask,MagickFalse,exception);
1454 (void) SetImageMask(image,clip_mask,exception);
1455 clip_mask=DestroyImage(clip_mask);
1456 clone_info=DestroyDrawInfo(clone_info);
1457 status=DrawImage(image,clone_info,exception);
1458 if (image->debug != MagickFalse)
1459 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1460 return(status != 0 ? MagickTrue : MagickFalse);
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1468 + D r a w D a s h P o l y g o n %
1472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1474 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1475 % image while respecting the dash offset and dash pattern attributes.
1477 % The format of the DrawDashPolygon method is:
1479 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1480 % const PrimitiveInfo *primitive_info,Image *image,
1481 % ExceptionInfo *exception)
1483 % A description of each parameter follows:
1485 % o draw_info: the draw info.
1487 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1489 % o image: the image.
1491 % o exception: return any errors or warnings in this structure.
1494 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1495 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1516 register MagickRealType
1527 assert(draw_info != (const DrawInfo *) NULL);
1528 if (image->debug != MagickFalse)
1529 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1530 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1531 clone_info->miterlimit=0;
1532 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1533 number_vertices=(size_t) i;
1534 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1535 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1536 if (dash_polygon == (PrimitiveInfo *) NULL)
1537 return(MagickFalse);
1538 dash_polygon[0]=primitive_info[0];
1539 scale=ExpandAffine(&draw_info->affine);
1540 length=scale*(draw_info->dash_pattern[0]-0.5);
1541 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1543 for (n=0; offset > 0.0; j=0)
1545 if (draw_info->dash_pattern[n] <= 0.0)
1547 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1548 if (offset > length)
1552 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1555 if (offset < length)
1567 for (i=1; i < (ssize_t) number_vertices; i++)
1569 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1570 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1571 maximum_length=hypot((double) dx,dy);
1575 if (draw_info->dash_pattern[n] == 0.0)
1577 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1579 for (total_length=0.0; (total_length+length) <= maximum_length; )
1581 total_length+=length;
1582 if ((n & 0x01) != 0)
1584 dash_polygon[0]=primitive_info[0];
1585 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1586 total_length/maximum_length);
1587 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1588 total_length/maximum_length);
1593 if ((j+1) > (ssize_t) (2*number_vertices))
1595 dash_polygon[j]=primitive_info[i-1];
1596 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1597 total_length/maximum_length);
1598 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1599 total_length/maximum_length);
1600 dash_polygon[j].coordinates=1;
1602 dash_polygon[0].coordinates=(size_t) j;
1603 dash_polygon[j].primitive=UndefinedPrimitive;
1604 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1607 if (draw_info->dash_pattern[n] == 0.0)
1609 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1611 length-=(maximum_length-total_length);
1612 if ((n & 0x01) != 0)
1614 dash_polygon[j]=primitive_info[i];
1615 dash_polygon[j].coordinates=1;
1618 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1620 dash_polygon[j]=primitive_info[i-1];
1621 dash_polygon[j].point.x+=DrawEpsilon;
1622 dash_polygon[j].point.y+=DrawEpsilon;
1623 dash_polygon[j].coordinates=1;
1625 dash_polygon[0].coordinates=(size_t) j;
1626 dash_polygon[j].primitive=UndefinedPrimitive;
1627 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1629 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1630 clone_info=DestroyDrawInfo(clone_info);
1631 if (image->debug != MagickFalse)
1632 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1633 return(status != 0 ? MagickTrue : MagickFalse);
1637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1641 % D r a w I m a g e %
1645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1647 % DrawImage() draws a graphic primitive on your image. The primitive
1648 % may be represented as a string or filename. Precede the filename with an
1649 % "at" sign (@) and the contents of the file are drawn on the image. You
1650 % can affect how text is drawn by setting one or more members of the draw
1653 % The format of the DrawImage method is:
1655 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1656 % ExceptionInfo *exception)
1658 % A description of each parameter follows:
1660 % o image: the image.
1662 % o draw_info: the draw info.
1664 % o exception: return any errors or warnings in this structure.
1668 static inline MagickBooleanType IsPoint(const char *point)
1676 value=StringToDouble(point,&p);
1677 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1680 static inline void TracePoint(PrimitiveInfo *primitive_info,
1681 const PointInfo point)
1683 primitive_info->coordinates=1;
1684 primitive_info->point=point;
1687 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1688 ExceptionInfo *exception)
1690 #define RenderImageTag "Render/Image"
1697 key[2*MaxTextExtent],
1698 keyword[MaxTextExtent],
1699 geometry[MaxTextExtent],
1700 name[MaxTextExtent],
1701 pattern[MaxTextExtent],
1752 Ensure the annotation info is valid.
1754 assert(image != (Image *) NULL);
1755 assert(image->signature == MagickSignature);
1756 if (image->debug != MagickFalse)
1757 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1758 assert(draw_info != (DrawInfo *) NULL);
1759 assert(draw_info->signature == MagickSignature);
1760 if (image->debug != MagickFalse)
1761 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1762 if ((draw_info->primitive == (char *) NULL) ||
1763 (*draw_info->primitive == '\0'))
1764 return(MagickFalse);
1765 if (image->debug != MagickFalse)
1766 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1767 if (*draw_info->primitive != '@')
1768 primitive=AcquireString(draw_info->primitive);
1770 primitive=FileToString(draw_info->primitive+1,~0,exception);
1771 if (primitive == (char *) NULL)
1772 return(MagickFalse);
1773 primitive_extent=(MagickRealType) strlen(primitive);
1774 (void) SetImageArtifact(image,"MVG",primitive);
1777 Allocate primitive info memory.
1779 graphic_context=(DrawInfo **) AcquireMagickMemory(
1780 sizeof(*graphic_context));
1781 if (graphic_context == (DrawInfo **) NULL)
1783 primitive=DestroyString(primitive);
1784 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1788 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1789 sizeof(*primitive_info));
1790 if (primitive_info == (PrimitiveInfo *) NULL)
1792 primitive=DestroyString(primitive);
1793 for ( ; n >= 0; n--)
1794 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1795 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1796 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1799 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1800 graphic_context[n]->viewbox=image->page;
1801 if ((image->page.width == 0) || (image->page.height == 0))
1803 graphic_context[n]->viewbox.width=image->columns;
1804 graphic_context[n]->viewbox.height=image->rows;
1806 token=AcquireString(primitive);
1807 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1809 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1810 return(MagickFalse);
1812 for (q=primitive; *q != '\0'; )
1815 Interpret graphic primitive.
1817 GetMagickToken(q,&q,keyword);
1818 if (*keyword == '\0')
1820 if (*keyword == '#')
1825 while ((*q != '\n') && (*q != '\0'))
1829 p=q-strlen(keyword)-1;
1830 primitive_type=UndefinedPrimitive;
1831 current=graphic_context[n]->affine;
1832 GetAffineMatrix(&affine);
1840 if (LocaleCompare("affine",keyword) == 0)
1842 GetMagickToken(q,&q,token);
1843 affine.sx=StringToDouble(token,(char **) NULL);
1844 GetMagickToken(q,&q,token);
1846 GetMagickToken(q,&q,token);
1847 affine.rx=StringToDouble(token,(char **) NULL);
1848 GetMagickToken(q,&q,token);
1850 GetMagickToken(q,&q,token);
1851 affine.ry=StringToDouble(token,(char **) NULL);
1852 GetMagickToken(q,&q,token);
1854 GetMagickToken(q,&q,token);
1855 affine.sy=StringToDouble(token,(char **) NULL);
1856 GetMagickToken(q,&q,token);
1858 GetMagickToken(q,&q,token);
1859 affine.tx=StringToDouble(token,(char **) NULL);
1860 GetMagickToken(q,&q,token);
1862 GetMagickToken(q,&q,token);
1863 affine.ty=StringToDouble(token,(char **) NULL);
1866 if (LocaleCompare("arc",keyword) == 0)
1868 primitive_type=ArcPrimitive;
1877 if (LocaleCompare("bezier",keyword) == 0)
1879 primitive_type=BezierPrimitive;
1882 if (LocaleCompare("border-color",keyword) == 0)
1884 GetMagickToken(q,&q,token);
1885 (void) QueryColorCompliance(token,AllCompliance,
1886 &graphic_context[n]->border_color,exception);
1895 if (LocaleCompare("clip-path",keyword) == 0)
1900 GetMagickToken(q,&q,token);
1901 (void) CloneString(&graphic_context[n]->clip_mask,token);
1902 (void) DrawClipPath(image,graphic_context[n],
1903 graphic_context[n]->clip_mask,exception);
1906 if (LocaleCompare("clip-rule",keyword) == 0)
1911 GetMagickToken(q,&q,token);
1912 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1914 if (fill_rule == -1)
1917 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1920 if (LocaleCompare("clip-units",keyword) == 0)
1925 GetMagickToken(q,&q,token);
1926 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1928 if (clip_units == -1)
1933 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1934 if (clip_units == ObjectBoundingBox)
1936 GetAffineMatrix(¤t);
1937 affine.sx=draw_info->bounds.x2;
1938 affine.sy=draw_info->bounds.y2;
1939 affine.tx=draw_info->bounds.x1;
1940 affine.ty=draw_info->bounds.y1;
1945 if (LocaleCompare("circle",keyword) == 0)
1947 primitive_type=CirclePrimitive;
1950 if (LocaleCompare("color",keyword) == 0)
1952 primitive_type=ColorPrimitive;
1961 if (LocaleCompare("decorate",keyword) == 0)
1966 GetMagickToken(q,&q,token);
1967 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1972 graphic_context[n]->decorate=(DecorationType) decorate;
1981 if (LocaleCompare("ellipse",keyword) == 0)
1983 primitive_type=EllipsePrimitive;
1986 if (LocaleCompare("encoding",keyword) == 0)
1988 GetMagickToken(q,&q,token);
1989 (void) CloneString(&graphic_context[n]->encoding,token);
1998 if (LocaleCompare("fill",keyword) == 0)
2000 GetMagickToken(q,&q,token);
2001 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2002 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2003 (void) DrawPatternPath(image,draw_info,token,
2004 &graphic_context[n]->fill_pattern,exception);
2007 status=QueryColorCompliance(token,AllCompliance,
2008 &graphic_context[n]->fill,exception);
2009 if (status == MagickFalse)
2014 pattern_info=AcquireImageInfo();
2015 (void) CopyMagickString(pattern_info->filename,token,
2017 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2019 CatchException(exception);
2020 pattern_info=DestroyImageInfo(pattern_info);
2025 if (LocaleCompare("fill-alpha",keyword) == 0)
2027 GetMagickToken(q,&q,token);
2028 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2029 graphic_context[n]->fill.alpha=(MagickRealType) QuantumRange*
2030 factor*StringToDouble(token,(char **) NULL);
2033 if (LocaleCompare("fill-rule",keyword) == 0)
2038 GetMagickToken(q,&q,token);
2039 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2041 if (fill_rule == -1)
2044 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2047 if (LocaleCompare("font",keyword) == 0)
2049 GetMagickToken(q,&q,token);
2050 (void) CloneString(&graphic_context[n]->font,token);
2051 if (LocaleCompare("none",token) == 0)
2052 graphic_context[n]->font=(char *)
2053 RelinquishMagickMemory(graphic_context[n]->font);
2056 if (LocaleCompare("font-family",keyword) == 0)
2058 GetMagickToken(q,&q,token);
2059 (void) CloneString(&graphic_context[n]->family,token);
2062 if (LocaleCompare("font-size",keyword) == 0)
2064 GetMagickToken(q,&q,token);
2065 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2068 if (LocaleCompare("font-stretch",keyword) == 0)
2073 GetMagickToken(q,&q,token);
2074 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2078 graphic_context[n]->stretch=(StretchType) stretch;
2081 if (LocaleCompare("font-style",keyword) == 0)
2086 GetMagickToken(q,&q,token);
2087 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2091 graphic_context[n]->style=(StyleType) style;
2094 if (LocaleCompare("font-weight",keyword) == 0)
2096 GetMagickToken(q,&q,token);
2097 graphic_context[n]->weight=StringToUnsignedLong(token);
2098 if (LocaleCompare(token,"all") == 0)
2099 graphic_context[n]->weight=0;
2100 if (LocaleCompare(token,"bold") == 0)
2101 graphic_context[n]->weight=700;
2102 if (LocaleCompare(token,"bolder") == 0)
2103 if (graphic_context[n]->weight <= 800)
2104 graphic_context[n]->weight+=100;
2105 if (LocaleCompare(token,"lighter") == 0)
2106 if (graphic_context[n]->weight >= 100)
2107 graphic_context[n]->weight-=100;
2108 if (LocaleCompare(token,"normal") == 0)
2109 graphic_context[n]->weight=400;
2118 if (LocaleCompare("gradient-units",keyword) == 0)
2120 GetMagickToken(q,&q,token);
2123 if (LocaleCompare("gravity",keyword) == 0)
2128 GetMagickToken(q,&q,token);
2129 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2133 graphic_context[n]->gravity=(GravityType) gravity;
2142 if (LocaleCompare("image",keyword) == 0)
2147 primitive_type=ImagePrimitive;
2148 GetMagickToken(q,&q,token);
2149 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2153 graphic_context[n]->compose=(CompositeOperator) compose;
2156 if (LocaleCompare("interline-spacing",keyword) == 0)
2158 GetMagickToken(q,&q,token);
2159 graphic_context[n]->interline_spacing=StringToDouble(token,
2163 if (LocaleCompare("interword-spacing",keyword) == 0)
2165 GetMagickToken(q,&q,token);
2166 graphic_context[n]->interword_spacing=StringToDouble(token,
2176 if (LocaleCompare("kerning",keyword) == 0)
2178 GetMagickToken(q,&q,token);
2179 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2188 if (LocaleCompare("line",keyword) == 0)
2189 primitive_type=LinePrimitive;
2197 if (LocaleCompare("matte",keyword) == 0)
2198 primitive_type=MattePrimitive;
2206 if (LocaleCompare("offset",keyword) == 0)
2208 GetMagickToken(q,&q,token);
2211 if (LocaleCompare("opacity",keyword) == 0)
2213 GetMagickToken(q,&q,token);
2214 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2215 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2216 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
2217 factor*StringToDouble(token,(char **) NULL))));
2218 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2219 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2228 if (LocaleCompare("path",keyword) == 0)
2230 primitive_type=PathPrimitive;
2233 if (LocaleCompare("point",keyword) == 0)
2235 primitive_type=PointPrimitive;
2238 if (LocaleCompare("polyline",keyword) == 0)
2240 primitive_type=PolylinePrimitive;
2243 if (LocaleCompare("polygon",keyword) == 0)
2245 primitive_type=PolygonPrimitive;
2248 if (LocaleCompare("pop",keyword) == 0)
2250 GetMagickToken(q,&q,token);
2251 if (LocaleCompare("clip-path",token) == 0)
2253 if (LocaleCompare("defs",token) == 0)
2255 if (LocaleCompare("gradient",token) == 0)
2257 if (LocaleCompare("graphic-context",token) == 0)
2261 (void) ThrowMagickException(exception,GetMagickModule(),
2262 DrawError,"UnbalancedGraphicContextPushPop","'%s'",token);
2266 if (graphic_context[n]->clip_mask != (char *) NULL)
2267 if (LocaleCompare(graphic_context[n]->clip_mask,
2268 graphic_context[n-1]->clip_mask) != 0)
2269 (void) SetImageMask(image,(Image *) NULL,exception);
2270 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2274 if (LocaleCompare("pattern",token) == 0)
2279 if (LocaleCompare("push",keyword) == 0)
2281 GetMagickToken(q,&q,token);
2282 if (LocaleCompare("clip-path",token) == 0)
2285 name[MaxTextExtent];
2287 GetMagickToken(q,&q,token);
2288 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2289 for (p=q; *q != '\0'; )
2291 GetMagickToken(q,&q,token);
2292 if (LocaleCompare(token,"pop") != 0)
2294 GetMagickToken(q,(const char **) NULL,token);
2295 if (LocaleCompare(token,"clip-path") != 0)
2299 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2300 (void) SetImageArtifact(image,name,token);
2301 GetMagickToken(q,&q,token);
2304 if (LocaleCompare("gradient",token) == 0)
2307 key[2*MaxTextExtent],
2308 name[MaxTextExtent],
2309 type[MaxTextExtent];
2314 GetMagickToken(q,&q,token);
2315 (void) CopyMagickString(name,token,MaxTextExtent);
2316 GetMagickToken(q,&q,token);
2317 (void) CopyMagickString(type,token,MaxTextExtent);
2318 GetMagickToken(q,&q,token);
2319 segment.x1=StringToDouble(token,(char **) NULL);
2320 GetMagickToken(q,&q,token);
2322 GetMagickToken(q,&q,token);
2323 segment.y1=StringToDouble(token,(char **) NULL);
2324 GetMagickToken(q,&q,token);
2326 GetMagickToken(q,&q,token);
2327 segment.x2=StringToDouble(token,(char **) NULL);
2328 GetMagickToken(q,&q,token);
2330 GetMagickToken(q,&q,token);
2331 segment.y2=StringToDouble(token,(char **) NULL);
2332 if (LocaleCompare(type,"radial") == 0)
2334 GetMagickToken(q,&q,token);
2336 GetMagickToken(q,&q,token);
2338 for (p=q; *q != '\0'; )
2340 GetMagickToken(q,&q,token);
2341 if (LocaleCompare(token,"pop") != 0)
2343 GetMagickToken(q,(const char **) NULL,token);
2344 if (LocaleCompare(token,"gradient") != 0)
2348 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2349 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2350 graphic_context[n]->affine.ry*segment.y1+
2351 graphic_context[n]->affine.tx;
2352 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2353 graphic_context[n]->affine.sy*segment.y1+
2354 graphic_context[n]->affine.ty;
2355 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2356 graphic_context[n]->affine.ry*segment.y2+
2357 graphic_context[n]->affine.tx;
2358 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2359 graphic_context[n]->affine.sy*segment.y2+
2360 graphic_context[n]->affine.ty;
2361 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2362 (void) SetImageArtifact(image,key,token);
2363 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2364 (void) FormatLocaleString(geometry,MaxTextExtent,
2365 "%gx%g%+.15g%+.15g",
2366 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2367 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2368 bounds.x1,bounds.y1);
2369 (void) SetImageArtifact(image,key,geometry);
2370 GetMagickToken(q,&q,token);
2373 if (LocaleCompare("pattern",token) == 0)
2378 GetMagickToken(q,&q,token);
2379 (void) CopyMagickString(name,token,MaxTextExtent);
2380 GetMagickToken(q,&q,token);
2381 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2383 GetMagickToken(q,&q,token);
2385 GetMagickToken(q,&q,token);
2386 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2388 GetMagickToken(q,&q,token);
2390 GetMagickToken(q,&q,token);
2391 bounds.width=(size_t) floor(StringToDouble(token,
2392 (char **) NULL)+0.5);
2393 GetMagickToken(q,&q,token);
2395 GetMagickToken(q,&q,token);
2396 bounds.height=(size_t) floor(StringToDouble(token,
2397 (char **) NULL)+0.5);
2398 for (p=q; *q != '\0'; )
2400 GetMagickToken(q,&q,token);
2401 if (LocaleCompare(token,"pop") != 0)
2403 GetMagickToken(q,(const char **) NULL,token);
2404 if (LocaleCompare(token,"pattern") != 0)
2408 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2409 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2410 (void) SetImageArtifact(image,key,token);
2411 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2412 (void) FormatLocaleString(geometry,MaxTextExtent,
2413 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2414 bounds.height,(double) bounds.x,(double) bounds.y);
2415 (void) SetImageArtifact(image,key,geometry);
2416 GetMagickToken(q,&q,token);
2419 if (LocaleCompare("graphic-context",token) == 0)
2422 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2423 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2424 if (graphic_context == (DrawInfo **) NULL)
2426 (void) ThrowMagickException(exception,GetMagickModule(),
2427 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2431 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2432 graphic_context[n-1]);
2435 if (LocaleCompare("defs",token) == 0)
2446 if (LocaleCompare("rectangle",keyword) == 0)
2448 primitive_type=RectanglePrimitive;
2451 if (LocaleCompare("rotate",keyword) == 0)
2453 GetMagickToken(q,&q,token);
2454 angle=StringToDouble(token,(char **) NULL);
2455 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2456 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2457 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2458 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2461 if (LocaleCompare("roundRectangle",keyword) == 0)
2463 primitive_type=RoundRectanglePrimitive;
2472 if (LocaleCompare("scale",keyword) == 0)
2474 GetMagickToken(q,&q,token);
2475 affine.sx=StringToDouble(token,(char **) NULL);
2476 GetMagickToken(q,&q,token);
2478 GetMagickToken(q,&q,token);
2479 affine.sy=StringToDouble(token,(char **) NULL);
2482 if (LocaleCompare("skewX",keyword) == 0)
2484 GetMagickToken(q,&q,token);
2485 angle=StringToDouble(token,(char **) NULL);
2486 affine.ry=sin(DegreesToRadians(angle));
2489 if (LocaleCompare("skewY",keyword) == 0)
2491 GetMagickToken(q,&q,token);
2492 angle=StringToDouble(token,(char **) NULL);
2493 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2496 if (LocaleCompare("stop-color",keyword) == 0)
2501 GetMagickToken(q,&q,token);
2502 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2504 (void) GradientImage(image,LinearGradient,ReflectSpread,
2505 &start_color,&stop_color,exception);
2506 start_color=stop_color;
2507 GetMagickToken(q,&q,token);
2510 if (LocaleCompare("stroke",keyword) == 0)
2512 GetMagickToken(q,&q,token);
2513 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2514 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2515 (void) DrawPatternPath(image,draw_info,token,
2516 &graphic_context[n]->stroke_pattern,exception);
2519 status=QueryColorCompliance(token,AllCompliance,
2520 &graphic_context[n]->stroke,exception);
2521 if (status == MagickFalse)
2526 pattern_info=AcquireImageInfo();
2527 (void) CopyMagickString(pattern_info->filename,token,
2529 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2531 CatchException(exception);
2532 pattern_info=DestroyImageInfo(pattern_info);
2537 if (LocaleCompare("stroke-antialias",keyword) == 0)
2539 GetMagickToken(q,&q,token);
2540 graphic_context[n]->stroke_antialias=
2541 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2544 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2546 if (graphic_context[n]->dash_pattern != (double *) NULL)
2547 graphic_context[n]->dash_pattern=(double *)
2548 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2549 if (IsPoint(q) != MagickFalse)
2555 GetMagickToken(p,&p,token);
2557 GetMagickToken(p,&p,token);
2558 for (x=0; IsPoint(token) != MagickFalse; x++)
2560 GetMagickToken(p,&p,token);
2562 GetMagickToken(p,&p,token);
2564 graphic_context[n]->dash_pattern=(double *)
2565 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2566 sizeof(*graphic_context[n]->dash_pattern));
2567 if (graphic_context[n]->dash_pattern == (double *) NULL)
2569 (void) ThrowMagickException(exception,GetMagickModule(),
2570 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2574 for (j=0; j < x; j++)
2576 GetMagickToken(q,&q,token);
2578 GetMagickToken(q,&q,token);
2579 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2582 if ((x & 0x01) != 0)
2583 for ( ; j < (2*x); j++)
2584 graphic_context[n]->dash_pattern[j]=
2585 graphic_context[n]->dash_pattern[j-x];
2586 graphic_context[n]->dash_pattern[j]=0.0;
2589 GetMagickToken(q,&q,token);
2592 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2594 GetMagickToken(q,&q,token);
2595 graphic_context[n]->dash_offset=StringToDouble(token,
2599 if (LocaleCompare("stroke-linecap",keyword) == 0)
2604 GetMagickToken(q,&q,token);
2605 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2609 graphic_context[n]->linecap=(LineCap) linecap;
2612 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2617 GetMagickToken(q,&q,token);
2618 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2623 graphic_context[n]->linejoin=(LineJoin) linejoin;
2626 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2628 GetMagickToken(q,&q,token);
2629 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2632 if (LocaleCompare("stroke-opacity",keyword) == 0)
2634 GetMagickToken(q,&q,token);
2635 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2636 graphic_context[n]->stroke.alpha=(MagickRealType) QuantumRange*
2637 factor*StringToDouble(token,(char **) NULL);
2640 if (LocaleCompare("stroke-width",keyword) == 0)
2642 GetMagickToken(q,&q,token);
2643 graphic_context[n]->stroke_width=StringToDouble(token,
2653 if (LocaleCompare("text",keyword) == 0)
2655 primitive_type=TextPrimitive;
2658 if (LocaleCompare("text-align",keyword) == 0)
2663 GetMagickToken(q,&q,token);
2664 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2668 graphic_context[n]->align=(AlignType) align;
2671 if (LocaleCompare("text-anchor",keyword) == 0)
2676 GetMagickToken(q,&q,token);
2677 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2681 graphic_context[n]->align=(AlignType) align;
2684 if (LocaleCompare("text-antialias",keyword) == 0)
2686 GetMagickToken(q,&q,token);
2687 graphic_context[n]->text_antialias=
2688 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2691 if (LocaleCompare("text-undercolor",keyword) == 0)
2693 GetMagickToken(q,&q,token);
2694 (void) QueryColorCompliance(token,AllCompliance,
2695 &graphic_context[n]->undercolor,exception);
2698 if (LocaleCompare("translate",keyword) == 0)
2700 GetMagickToken(q,&q,token);
2701 affine.tx=StringToDouble(token,(char **) NULL);
2702 GetMagickToken(q,&q,token);
2704 GetMagickToken(q,&q,token);
2705 affine.ty=StringToDouble(token,(char **) NULL);
2714 if (LocaleCompare("viewbox",keyword) == 0)
2716 GetMagickToken(q,&q,token);
2717 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2718 (char **) NULL)-0.5);
2719 GetMagickToken(q,&q,token);
2721 GetMagickToken(q,&q,token);
2722 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2723 (char **) NULL)-0.5);
2724 GetMagickToken(q,&q,token);
2726 GetMagickToken(q,&q,token);
2727 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2728 token,(char **) NULL)+0.5);
2729 GetMagickToken(q,&q,token);
2731 GetMagickToken(q,&q,token);
2732 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2733 token,(char **) NULL)+0.5);
2745 if (status == MagickFalse)
2747 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2748 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2750 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2751 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2752 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2753 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2754 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2756 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2759 if (primitive_type == UndefinedPrimitive)
2761 if (image->debug != MagickFalse)
2762 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2767 Parse the primitive attributes.
2771 primitive_info[0].point.x=0.0;
2772 primitive_info[0].point.y=0.0;
2773 for (x=0; *q != '\0'; x++)
2778 if (IsPoint(q) == MagickFalse)
2780 GetMagickToken(q,&q,token);
2781 point.x=StringToDouble(token,(char **) NULL);
2782 GetMagickToken(q,&q,token);
2784 GetMagickToken(q,&q,token);
2785 point.y=StringToDouble(token,(char **) NULL);
2786 GetMagickToken(q,(const char **) NULL,token);
2788 GetMagickToken(q,&q,token);
2789 primitive_info[i].primitive=primitive_type;
2790 primitive_info[i].point=point;
2791 primitive_info[i].coordinates=0;
2792 primitive_info[i].method=FloodfillMethod;
2794 if (i < (ssize_t) number_points)
2797 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2798 (size_t) number_points,sizeof(*primitive_info));
2799 if (primitive_info == (PrimitiveInfo *) NULL)
2801 (void) ThrowMagickException(exception,GetMagickModule(),
2802 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
2806 primitive_info[j].primitive=primitive_type;
2807 primitive_info[j].coordinates=(size_t) x;
2808 primitive_info[j].method=FloodfillMethod;
2809 primitive_info[j].text=(char *) NULL;
2811 Circumscribe primitive within a circle.
2813 bounds.x1=primitive_info[j].point.x;
2814 bounds.y1=primitive_info[j].point.y;
2815 bounds.x2=primitive_info[j].point.x;
2816 bounds.y2=primitive_info[j].point.y;
2817 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2819 point=primitive_info[j+k].point;
2820 if (point.x < bounds.x1)
2822 if (point.y < bounds.y1)
2824 if (point.x > bounds.x2)
2826 if (point.y > bounds.y2)
2830 Speculate how many points our primitive might consume.
2832 length=primitive_info[j].coordinates;
2833 switch (primitive_type)
2835 case RectanglePrimitive:
2840 case RoundRectanglePrimitive:
2842 length*=5+8*BezierQuantum;
2845 case BezierPrimitive:
2847 if (primitive_info[j].coordinates > 107)
2848 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2849 "TooManyBezierCoordinates","'%s'",token);
2850 length=BezierQuantum*primitive_info[j].coordinates;
2859 GetMagickToken(q,&q,token);
2862 for (s=token; *s != '\0'; s=t)
2867 value=StringToDouble(s,&t);
2876 length=length*BezierQuantum/2;
2879 case CirclePrimitive:
2881 case EllipsePrimitive:
2888 alpha=bounds.x2-bounds.x1;
2889 beta=bounds.y2-bounds.y1;
2890 radius=hypot((double) alpha,(double) beta);
2891 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2897 if ((size_t) (i+length) >= number_points)
2900 Resize based on speculative points required by primitive.
2902 number_points+=length+1;
2903 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2904 (size_t) number_points,sizeof(*primitive_info));
2905 if (primitive_info == (PrimitiveInfo *) NULL)
2907 (void) ThrowMagickException(exception,GetMagickModule(),
2908 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2913 switch (primitive_type)
2915 case PointPrimitive:
2918 if (primitive_info[j].coordinates != 1)
2923 TracePoint(primitive_info+j,primitive_info[j].point);
2924 i=(ssize_t) (j+primitive_info[j].coordinates);
2929 if (primitive_info[j].coordinates != 2)
2934 TraceLine(primitive_info+j,primitive_info[j].point,
2935 primitive_info[j+1].point);
2936 i=(ssize_t) (j+primitive_info[j].coordinates);
2939 case RectanglePrimitive:
2941 if (primitive_info[j].coordinates != 2)
2946 TraceRectangle(primitive_info+j,primitive_info[j].point,
2947 primitive_info[j+1].point);
2948 i=(ssize_t) (j+primitive_info[j].coordinates);
2951 case RoundRectanglePrimitive:
2953 if (primitive_info[j].coordinates != 3)
2958 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2959 primitive_info[j+1].point,primitive_info[j+2].point);
2960 i=(ssize_t) (j+primitive_info[j].coordinates);
2965 if (primitive_info[j].coordinates != 3)
2967 primitive_type=UndefinedPrimitive;
2970 TraceArc(primitive_info+j,primitive_info[j].point,
2971 primitive_info[j+1].point,primitive_info[j+2].point);
2972 i=(ssize_t) (j+primitive_info[j].coordinates);
2975 case EllipsePrimitive:
2977 if (primitive_info[j].coordinates != 3)
2982 TraceEllipse(primitive_info+j,primitive_info[j].point,
2983 primitive_info[j+1].point,primitive_info[j+2].point);
2984 i=(ssize_t) (j+primitive_info[j].coordinates);
2987 case CirclePrimitive:
2989 if (primitive_info[j].coordinates != 2)
2994 TraceCircle(primitive_info+j,primitive_info[j].point,
2995 primitive_info[j+1].point);
2996 i=(ssize_t) (j+primitive_info[j].coordinates);
2999 case PolylinePrimitive:
3001 case PolygonPrimitive:
3003 primitive_info[i]=primitive_info[j];
3004 primitive_info[i].coordinates=0;
3005 primitive_info[j].coordinates++;
3009 case BezierPrimitive:
3011 if (primitive_info[j].coordinates < 3)
3016 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3017 i=(ssize_t) (j+primitive_info[j].coordinates);
3022 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3025 case ColorPrimitive:
3026 case MattePrimitive:
3031 if (primitive_info[j].coordinates != 1)
3036 GetMagickToken(q,&q,token);
3037 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3041 primitive_info[j].method=(PaintMethod) method;
3046 if (primitive_info[j].coordinates != 1)
3052 GetMagickToken(q,&q,token);
3053 primitive_info[j].text=AcquireString(token);
3056 case ImagePrimitive:
3058 if (primitive_info[j].coordinates != 2)
3063 GetMagickToken(q,&q,token);
3064 primitive_info[j].text=AcquireString(token);
3068 if (primitive_info == (PrimitiveInfo *) NULL)
3070 if (image->debug != MagickFalse)
3071 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3072 if (status == MagickFalse)
3074 primitive_info[i].primitive=UndefinedPrimitive;
3080 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3082 point=primitive_info[i].point;
3083 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3084 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3085 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3086 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3087 point=primitive_info[i].point;
3088 if (point.x < graphic_context[n]->bounds.x1)
3089 graphic_context[n]->bounds.x1=point.x;
3090 if (point.y < graphic_context[n]->bounds.y1)
3091 graphic_context[n]->bounds.y1=point.y;
3092 if (point.x > graphic_context[n]->bounds.x2)
3093 graphic_context[n]->bounds.x2=point.x;
3094 if (point.y > graphic_context[n]->bounds.y2)
3095 graphic_context[n]->bounds.y2=point.y;
3096 if (primitive_info[i].primitive == ImagePrimitive)
3098 if (i >= (ssize_t) number_points)
3099 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3101 if (graphic_context[n]->render != MagickFalse)
3103 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3104 (LocaleCompare(graphic_context[n]->clip_mask,
3105 graphic_context[n-1]->clip_mask) != 0))
3106 (void) DrawClipPath(image,graphic_context[n],
3107 graphic_context[n]->clip_mask,exception);
3108 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3110 if (primitive_info->text != (char *) NULL)
3111 primitive_info->text=(char *) RelinquishMagickMemory(
3112 primitive_info->text);
3113 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3115 if (proceed == MagickFalse)
3118 if (image->debug != MagickFalse)
3119 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3121 Relinquish resources.
3123 token=DestroyString(token);
3124 if (primitive_info != (PrimitiveInfo *) NULL)
3125 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3126 primitive=DestroyString(primitive);
3127 for ( ; n >= 0; n--)
3128 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3129 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3130 if (status == MagickFalse)
3131 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3141 % D r a w G r a d i e n t I m a g e %
3145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147 % DrawGradientImage() draws a linear gradient on the image.
3149 % The format of the DrawGradientImage method is:
3151 % MagickBooleanType DrawGradientImage(Image *image,
3152 % const DrawInfo *draw_info,ExceptionInfo *exception)
3154 % A description of each parameter follows:
3156 % o image: the image.
3158 % o draw_info: the draw info.
3160 % o exception: return any errors or warnings in this structure.
3164 static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
3165 const ssize_t x,const ssize_t y)
3167 switch (gradient->type)
3169 case UndefinedGradient:
3170 case LinearGradient:
3185 gradient_vector=(&gradient->gradient_vector);
3186 p.x=gradient_vector->x2-gradient_vector->x1;
3187 p.y=gradient_vector->y2-gradient_vector->y1;
3188 q.x=(double) x-gradient_vector->x1;
3189 q.y=(double) y-gradient_vector->y1;
3190 length=sqrt(q.x*q.x+q.y*q.y);
3191 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3192 gamma=DrawEpsilonReciprocal(gamma);
3193 scale=p.x*q.x+p.y*q.y;
3194 offset=gamma*scale*length;
3197 case RadialGradient:
3206 v.x=(double) x-gradient->center.x;
3207 v.y=(double) y-gradient->center.y;
3208 length=sqrt(v.x*v.x+v.y*v.y);
3209 if (gradient->spread == RepeatSpread)
3211 offset=length/gradient->radius;
3218 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3219 const DrawInfo *draw_info,ExceptionInfo *exception)
3253 Draw linear or radial gradient on image.
3255 assert(image != (Image *) NULL);
3256 assert(image->signature == MagickSignature);
3257 if (image->debug != MagickFalse)
3258 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3259 assert(draw_info != (const DrawInfo *) NULL);
3260 gradient=(&draw_info->gradient);
3261 gradient_vector=(&gradient->gradient_vector);
3262 point.x=gradient_vector->x2-gradient_vector->x1;
3263 point.y=gradient_vector->y2-gradient_vector->y1;
3264 length=sqrt(point.x*point.x+point.y*point.y);
3265 bounding_box=gradient->bounding_box;
3267 GetPixelInfo(image,&zero);
3268 height=bounding_box.height-bounding_box.y;
3269 width=bounding_box.width-bounding_box.x;
3270 image_view=AcquireAuthenticCacheView(image,exception);
3271 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3272 #pragma omp parallel for schedule(static) shared(status) \
3273 dynamic_number_threads(image,width,height,1)
3275 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3295 if (status == MagickFalse)
3297 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3298 if (q == (Quantum *) NULL)
3305 offset=GetStopColorOffset(gradient,0,y);
3306 if (gradient->type != RadialGradient)
3308 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3310 GetPixelInfoPixel(image,q,&pixel);
3311 switch (gradient->spread)
3313 case UndefinedSpread:
3316 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3317 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3319 offset=GetStopColorOffset(gradient,x,y);
3320 if (gradient->type != RadialGradient)
3323 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3324 if (offset < gradient->stops[i].offset)
3326 if ((offset < 0.0) || (i == 0))
3327 composite=gradient->stops[0].color;
3329 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3330 composite=gradient->stops[gradient->number_stops-1].color;
3335 alpha=(offset-gradient->stops[i].offset)/
3336 (gradient->stops[j].offset-gradient->stops[i].offset);
3337 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3338 &gradient->stops[j].color,alpha,&composite);
3344 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3345 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3347 offset=GetStopColorOffset(gradient,x,y);
3348 if (gradient->type != RadialGradient)
3353 if ((ssize_t) fmod(offset,2.0) == 0)
3354 offset=fmod(offset,1.0);
3356 offset=1.0-fmod(offset,1.0);
3357 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3358 if (offset < gradient->stops[i].offset)
3361 composite=gradient->stops[0].color;
3363 if (i == (ssize_t) gradient->number_stops)
3364 composite=gradient->stops[gradient->number_stops-1].color;
3369 alpha=(offset-gradient->stops[i].offset)/
3370 (gradient->stops[j].offset-gradient->stops[i].offset);
3371 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3372 &gradient->stops[j].color,alpha,&composite);
3384 antialias=MagickFalse;
3386 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3387 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3389 offset=GetStopColorOffset(gradient,x,y);
3390 if (gradient->type == LinearGradient)
3392 repeat=fmod(offset,length);
3394 repeat=length-fmod(-repeat,length);
3396 repeat=fmod(offset,length);
3397 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3398 MagickTrue : MagickFalse;
3399 offset=repeat/length;
3403 repeat=fmod(offset,gradient->radius);
3405 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3407 repeat=fmod(offset,gradient->radius);
3408 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3410 offset=repeat/gradient->radius;
3413 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3414 if (offset < gradient->stops[i].offset)
3417 composite=gradient->stops[0].color;
3419 if (i == (ssize_t) gradient->number_stops)
3420 composite=gradient->stops[gradient->number_stops-1].color;
3425 alpha=(offset-gradient->stops[i].offset)/
3426 (gradient->stops[j].offset-gradient->stops[i].offset);
3427 if (antialias != MagickFalse)
3429 if (gradient->type == LinearGradient)
3430 alpha=length-repeat;
3432 alpha=gradient->radius-repeat;
3434 j=(ssize_t) gradient->number_stops-1L;
3436 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3437 &gradient->stops[j].color,alpha,&composite);
3442 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3444 SetPixelInfoPixel(image,&pixel,q);
3445 q+=GetPixelChannels(image);
3447 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3450 image_view=DestroyCacheView(image_view);
3455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3459 % D r a w P a t t e r n P a t h %
3463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3465 % DrawPatternPath() draws a pattern.
3467 % The format of the DrawPatternPath method is:
3469 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3470 % const char *name,Image **pattern,ExceptionInfo *exception)
3472 % A description of each parameter follows:
3474 % o image: the image.
3476 % o draw_info: the draw info.
3478 % o name: the pattern name.
3480 % o image: the image.
3482 % o exception: return any errors or warnings in this structure.
3485 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3486 const DrawInfo *draw_info,const char *name,Image **pattern,
3487 ExceptionInfo *exception)
3490 property[MaxTextExtent];
3505 assert(image != (Image *) NULL);
3506 assert(image->signature == MagickSignature);
3507 if (image->debug != MagickFalse)
3508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3509 assert(draw_info != (const DrawInfo *) NULL);
3510 assert(name != (const char *) NULL);
3511 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3512 path=GetImageArtifact(image,property);
3513 if (path == (const char *) NULL)
3514 return(MagickFalse);
3515 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3516 geometry=GetImageArtifact(image,property);
3517 if (geometry == (const char *) NULL)
3518 return(MagickFalse);
3519 if ((*pattern) != (Image *) NULL)
3520 *pattern=DestroyImage(*pattern);
3521 image_info=AcquireImageInfo();
3522 image_info->size=AcquireString(geometry);
3523 *pattern=AcquireImage(image_info,exception);
3524 image_info=DestroyImageInfo(image_info);
3525 (void) QueryColorCompliance("#00000000",AllCompliance,
3526 &(*pattern)->background_color,exception);
3527 (void) SetImageBackgroundColor(*pattern,exception);
3528 if (image->debug != MagickFalse)
3529 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3530 "begin pattern-path %s %s",name,geometry);
3531 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3532 clone_info->fill_pattern=NewImageList();
3533 clone_info->stroke_pattern=NewImageList();
3534 (void) CloneString(&clone_info->primitive,path);
3535 status=DrawImage(*pattern,clone_info,exception);
3536 clone_info=DestroyDrawInfo(clone_info);
3537 if (image->debug != MagickFalse)
3538 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3547 + D r a w P o l y g o n P r i m i t i v e %
3551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3553 % DrawPolygonPrimitive() draws a polygon on the image.
3555 % The format of the DrawPolygonPrimitive method is:
3557 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3558 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3559 % ExceptionInfo *exception)
3561 % A description of each parameter follows:
3563 % o image: the image.
3565 % o draw_info: the draw info.
3567 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3569 % o exception: return any errors or warnings in this structure.
3573 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3578 assert(polygon_info != (PolygonInfo **) NULL);
3579 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3580 if (polygon_info[i] != (PolygonInfo *) NULL)
3581 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3582 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3583 return(polygon_info);
3586 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3587 const PrimitiveInfo *primitive_info)
3590 *restrict path_info;
3601 number_threads=GetOpenMPMaximumThreads();
3602 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3603 sizeof(*polygon_info));
3604 if (polygon_info == (PolygonInfo **) NULL)
3605 return((PolygonInfo **) NULL);
3606 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3607 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3608 if (path_info == (PathInfo *) NULL)
3609 return(DestroyPolygonThreadSet(polygon_info));
3610 for (i=0; i < (ssize_t) number_threads; i++)
3612 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3613 if (polygon_info[i] == (PolygonInfo *) NULL)
3614 return(DestroyPolygonThreadSet(polygon_info));
3616 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3617 return(polygon_info);
3620 static MagickRealType GetFillAlpha(PolygonInfo *polygon_info,
3621 const MagickRealType mid,const MagickBooleanType fill,
3622 const FillRule fill_rule,const double x,const double y,
3623 MagickRealType *stroke_alpha)
3637 register const PointInfo
3648 Compute fill & stroke opacity for this (x,y) point.
3652 p=polygon_info->edges;
3653 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3655 if (y <= (p->bounds.y1-mid-0.5))
3657 if (y > (p->bounds.y2+mid+0.5))
3659 (void) DestroyEdge(polygon_info,(size_t) j);
3662 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3664 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3665 for ( ; i < (ssize_t) p->number_points; i++)
3667 if (y <= (p->points[i-1].y-mid-0.5))
3669 if (y > (p->points[i].y+mid+0.5))
3671 if (p->scanline != y)
3674 p->highwater=(size_t) i;
3677 Compute distance between a point and an edge.
3680 delta.x=(q+1)->x-q->x;
3681 delta.y=(q+1)->y-q->y;
3682 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3687 distance=delta.x*delta.x+delta.y*delta.y;
3691 alpha=delta.x*delta.x+delta.y*delta.y;
3696 distance=delta.x*delta.x+delta.y*delta.y;
3700 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3701 distance=DrawEpsilonReciprocal(alpha)*beta*beta;
3705 Compute stroke & subpath opacity.
3708 if (p->ghostline == MagickFalse)
3711 if ((*stroke_alpha < 1.0) &&
3712 (distance <= ((alpha+0.25)*(alpha+0.25))))
3715 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3720 if (distance != 1.0)
3721 beta=sqrt((double) distance);
3723 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3724 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3728 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3730 if (distance <= 0.0)
3740 if (distance != 1.0)
3741 beta=sqrt(distance);
3744 if (subpath_alpha < (alpha*alpha))
3745 subpath_alpha=alpha*alpha;
3749 Compute fill opacity.
3751 if (fill == MagickFalse)
3753 if (subpath_alpha >= 1.0)
3756 Determine winding number.
3759 p=polygon_info->edges;
3760 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3762 if (y <= p->bounds.y1)
3764 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3766 if (x > p->bounds.x2)
3768 winding_number+=p->direction ? 1 : -1;
3771 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3772 for ( ; i < (ssize_t) p->number_points; i++)
3773 if (y <= p->points[i].y)
3776 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3777 winding_number+=p->direction ? 1 : -1;
3779 if (fill_rule != NonZeroRule)
3781 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3785 if (MagickAbsoluteValue(winding_number) != 0)
3787 return(subpath_alpha);
3790 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3791 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3792 ExceptionInfo *exception)
3805 **restrict polygon_info;
3826 Compute bounding box.
3828 assert(image != (Image *) NULL);
3829 assert(image->signature == MagickSignature);
3830 if (image->debug != MagickFalse)
3831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3832 assert(draw_info != (DrawInfo *) NULL);
3833 assert(draw_info->signature == MagickSignature);
3834 assert(primitive_info != (PrimitiveInfo *) NULL);
3835 if (primitive_info->coordinates == 0)
3837 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3838 if (polygon_info == (PolygonInfo **) NULL)
3839 return(MagickFalse);
3841 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3842 if (image->debug != MagickFalse)
3843 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3844 fill=(primitive_info->method == FillToBorderMethod) ||
3845 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3846 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3847 bounds=polygon_info[0]->edges[0].bounds;
3848 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3850 p=polygon_info[0]->edges+i;
3851 if (p->bounds.x1 < bounds.x1)
3852 bounds.x1=p->bounds.x1;
3853 if (p->bounds.y1 < bounds.y1)
3854 bounds.y1=p->bounds.y1;
3855 if (p->bounds.x2 > bounds.x2)
3856 bounds.x2=p->bounds.x2;
3857 if (p->bounds.y2 > bounds.y2)
3858 bounds.y2=p->bounds.y2;
3860 bounds.x1-=(mid+1.0);
3861 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3862 image->columns ? (double) image->columns-1.0 : bounds.x1;
3863 bounds.y1-=(mid+1.0);
3864 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3865 image->rows ? (double) image->rows-1.0 : bounds.y1;
3866 bounds.x2+=(mid+1.0);
3867 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3868 image->columns ? (double) image->columns-1.0 : bounds.x2;
3869 bounds.y2+=(mid+1.0);
3870 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3871 image->rows ? (double) image->rows-1.0 : bounds.y2;
3873 image_view=AcquireAuthenticCacheView(image,exception);
3874 height=(size_t) (floor(bounds.y2+0.5)-ceil(bounds.y1-0.5));
3875 width=(size_t) (floor(bounds.x2+0.5)-ceil(bounds.x1-0.5));
3876 if (primitive_info->coordinates == 1)
3881 start=(ssize_t) ceil(bounds.y1-0.5);
3882 stop=(ssize_t) floor(bounds.y2+0.5);
3883 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3884 #pragma omp parallel for schedule(static) shared(status) \
3885 dynamic_number_threads(image,width,height,1)
3887 for (y=start; y <= stop; y++)
3905 if (status == MagickFalse)
3907 start=(ssize_t) ceil(bounds.x1-0.5);
3908 stop=(ssize_t) floor(bounds.x2+0.5);
3910 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
3912 if (q == (Quantum *) NULL)
3917 GetPixelInfo(image,&pixel);
3918 for ( ; x <= stop; x++)
3920 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3921 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3923 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3924 SetPixelInfoPixel(image,&pixel,q);
3926 q+=GetPixelChannels(image);
3928 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3929 if (sync == MagickFalse)
3932 image_view=DestroyCacheView(image_view);
3933 polygon_info=DestroyPolygonThreadSet(polygon_info);
3934 if (image->debug != MagickFalse)
3935 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3936 " end draw-polygon");
3940 Draw polygon or line.
3942 if (image->matte == MagickFalse)
3943 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3944 start=(ssize_t) ceil(bounds.y1-0.5);
3945 stop=(ssize_t) floor(bounds.y2+0.5);
3946 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3947 #pragma omp parallel for schedule(static) shared(status) \
3948 dynamic_number_threads(image,width,height,1)
3950 for (y=start; y <= stop; y++)
3953 id = GetOpenMPThreadId();
3973 if (status == MagickFalse)
3975 start=(ssize_t) ceil(bounds.x1-0.5);
3976 stop=(ssize_t) floor(bounds.x2+0.5);
3977 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3979 if (q == (Quantum *) NULL)
3984 for (x=start; x <= stop; x++)
3989 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3990 (double) x,(double) y,&stroke_alpha);
3991 if (draw_info->stroke_antialias == MagickFalse)
3993 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3994 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3996 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3997 fill_alpha=fill_alpha*fill_color.alpha;
3998 CompositePixelOver(image,&fill_color,fill_alpha,q,(MagickRealType)
3999 GetPixelAlpha(image,q),q);
4000 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
4001 stroke_alpha=stroke_alpha*stroke_color.alpha;
4002 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(MagickRealType)
4003 GetPixelAlpha(image,q),q);
4004 q+=GetPixelChannels(image);
4006 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4009 image_view=DestroyCacheView(image_view);
4010 polygon_info=DestroyPolygonThreadSet(polygon_info);
4011 if (image->debug != MagickFalse)
4012 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4021 % D r a w P r i m i t i v e %
4025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4027 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4029 % The format of the DrawPrimitive method is:
4031 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4032 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4034 % A description of each parameter follows:
4036 % o image: the image.
4038 % o draw_info: the draw info.
4040 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4042 % o exception: return any errors or warnings in this structure.
4046 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4072 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4073 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4074 switch (primitive_info->primitive)
4076 case PointPrimitive:
4078 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4079 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4080 methods[primitive_info->method]);
4083 case ColorPrimitive:
4085 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4086 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4087 methods[primitive_info->method]);
4090 case MattePrimitive:
4092 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4093 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4094 methods[primitive_info->method]);
4099 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4100 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4103 case ImagePrimitive:
4105 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4106 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4113 p=primitive_info[0].point;
4116 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4118 point=primitive_info[i].point;
4119 if (coordinates <= 0)
4121 coordinates=(ssize_t) primitive_info[i].coordinates;
4122 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4123 " begin open (%.20g)",(double) coordinates);
4126 point=primitive_info[i].point;
4127 if ((fabs(q.x-point.x) >= DrawEpsilon) ||
4128 (fabs(q.y-point.y) >= DrawEpsilon))
4129 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4130 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4132 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4133 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4136 if (coordinates > 0)
4138 if ((fabs(p.x-point.x) >= DrawEpsilon) ||
4139 (fabs(p.y-point.y) >= DrawEpsilon))
4140 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4141 (double) coordinates);
4143 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4144 (double) coordinates);
4148 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4149 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4150 ExceptionInfo *exception)
4165 if (image->debug != MagickFalse)
4167 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4168 " begin draw-primitive");
4169 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4170 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4171 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4172 draw_info->affine.tx,draw_info->affine.ty);
4174 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4175 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4176 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4177 (void) SetImageColorspace(image,sRGBColorspace,exception);
4179 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4180 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4181 image_view=AcquireAuthenticCacheView(image,exception);
4182 switch (primitive_info->primitive)
4184 case PointPrimitive:
4192 if ((y < 0) || (y >= (ssize_t) image->rows))
4194 if ((x < 0) || (x >= (ssize_t) image->columns))
4196 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4197 if (q == (Quantum *) NULL)
4199 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4200 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4201 (MagickRealType) GetPixelAlpha(image,q),q);
4202 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4205 case ColorPrimitive:
4207 switch (primitive_info->method)
4218 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4219 if (q == (Quantum *) NULL)
4221 GetPixelInfo(image,&pixel);
4222 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4223 SetPixelInfoPixel(image,&pixel,q);
4224 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4236 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4238 for (y=0; y < (ssize_t) image->rows; y++)
4243 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4245 if (q == (Quantum *) NULL)
4247 for (x=0; x < (ssize_t) image->columns; x++)
4249 GetPixelInfoPixel(image,q,&pixel);
4250 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4252 q+=GetPixelChannels(image);
4255 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4256 SetPixelInfoPixel(image,&pixel,q);
4257 q+=GetPixelChannels(image);
4259 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4260 if (sync == MagickFalse)
4265 case FloodfillMethod:
4266 case FillToBorderMethod:
4271 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4273 if (primitive_info->method == FillToBorderMethod)
4275 target.red=(MagickRealType) draw_info->border_color.red;
4276 target.green=(MagickRealType) draw_info->border_color.green;
4277 target.blue=(MagickRealType) draw_info->border_color.blue;
4279 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4280 primitive_info->method == FloodfillMethod ? MagickFalse :
4281 MagickTrue,exception);
4292 GetPixelInfo(image,&pixel);
4293 for (y=0; y < (ssize_t) image->rows; y++)
4301 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4303 if (q == (Quantum *) NULL)
4305 for (x=0; x < (ssize_t) image->columns; x++)
4307 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4308 SetPixelInfoPixel(image,&pixel,q);
4309 q+=GetPixelChannels(image);
4311 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4312 if (sync == MagickFalse)
4320 case MattePrimitive:
4322 if (image->matte == MagickFalse)
4323 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4324 switch (primitive_info->method)
4335 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4336 if (q == (Quantum *) NULL)
4338 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4339 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4340 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4352 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4354 for (y=0; y < (ssize_t) image->rows; y++)
4362 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4364 if (q == (Quantum *) NULL)
4366 for (x=0; x < (ssize_t) image->columns; x++)
4368 GetPixelInfoPixel(image,q,&pixel);
4369 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4371 q+=GetPixelChannels(image);
4374 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4375 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4376 q+=GetPixelChannels(image);
4378 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4379 if (sync == MagickFalse)
4384 case FloodfillMethod:
4385 case FillToBorderMethod:
4393 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4395 if (primitive_info->method == FillToBorderMethod)
4397 target.red=(MagickRealType) draw_info->border_color.red;
4398 target.green=(MagickRealType) draw_info->border_color.green;
4399 target.blue=(MagickRealType) draw_info->border_color.blue;
4401 channel_mask=SetPixelChannelMask(image,AlphaChannel);
4402 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4403 primitive_info->method == FloodfillMethod ? MagickFalse :
4404 MagickTrue,exception);
4405 (void) SetPixelChannelMask(image,channel_mask);
4416 for (y=0; y < (ssize_t) image->rows; y++)
4424 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4426 if (q == (Quantum *) NULL)
4428 for (x=0; x < (ssize_t) image->columns; x++)
4430 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4431 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4432 q+=GetPixelChannels(image);
4434 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4435 if (sync == MagickFalse)
4446 geometry[MaxTextExtent];
4451 if (primitive_info->text == (char *) NULL)
4453 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4454 (void) CloneString(&clone_info->text,primitive_info->text);
4455 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4456 primitive_info->point.x,primitive_info->point.y);
4457 (void) CloneString(&clone_info->geometry,geometry);
4458 status=AnnotateImage(image,clone_info,exception);
4459 clone_info=DestroyDrawInfo(clone_info);
4462 case ImagePrimitive:
4468 composite_geometry[MaxTextExtent];
4483 if (primitive_info->text == (char *) NULL)
4485 clone_info=AcquireImageInfo();
4486 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4487 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4491 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4493 composite_image=ReadImage(clone_info,exception);
4495 clone_info=DestroyImageInfo(clone_info);
4496 if (composite_image == (Image *) NULL)
4498 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4499 NULL,(void *) NULL);
4500 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4501 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4502 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4503 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4506 geometry[MaxTextExtent];
4511 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4512 primitive_info[1].point.x,primitive_info[1].point.y);
4513 composite_image->filter=image->filter;
4514 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4517 if (composite_image->matte == MagickFalse)
4518 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4520 if (draw_info->alpha != OpaqueAlpha)
4521 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4522 SetGeometry(image,&geometry);
4523 image->gravity=draw_info->gravity;
4526 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4527 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4528 composite_image->rows,(double) geometry.x,(double) geometry.y);
4529 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4530 affine=draw_info->affine;
4531 affine.tx=(double) geometry.x;
4532 affine.ty=(double) geometry.y;
4533 composite_image->interpolate=image->interpolate;
4534 if (draw_info->compose == OverCompositeOp)
4535 (void) DrawAffineImage(image,composite_image,&affine,exception);
4537 (void) CompositeImage(image,composite_image,draw_info->compose,
4538 MagickTrue,geometry.x,geometry.y,exception);
4539 composite_image=DestroyImage(composite_image);
4551 if (IsEventLogging() != MagickFalse)
4552 LogPrimitiveInfo(primitive_info);
4553 scale=ExpandAffine(&draw_info->affine);
4554 if ((draw_info->dash_pattern != (double *) NULL) &&
4555 (draw_info->dash_pattern[0] != 0.0) &&
4556 ((scale*draw_info->stroke_width) >= DrawEpsilon) &&
4557 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4562 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4563 clone_info->stroke_width=0.0;
4564 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4565 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4567 clone_info=DestroyDrawInfo(clone_info);
4568 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4571 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4573 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4579 Draw strokes while respecting line cap/join attributes.
4581 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4583 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4584 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4585 MagickTrue : MagickFalse;
4586 i=(ssize_t) primitive_info[0].coordinates;
4587 if ((((draw_info->linecap == RoundCap) ||
4588 (closed_path != MagickFalse)) &&
4589 (draw_info->linejoin == RoundJoin)) ||
4590 (primitive_info[i].primitive != UndefinedPrimitive))
4592 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4596 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4597 clone_info->stroke_width=0.0;
4598 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4599 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4601 clone_info=DestroyDrawInfo(clone_info);
4602 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4605 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4609 image_view=DestroyCacheView(image_view);
4610 if (image->debug != MagickFalse)
4611 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4612 return(status != 0 ? MagickTrue : MagickFalse);
4616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4620 + D r a w S t r o k e P o l y g o n %
4624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4626 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4627 % the image while respecting the line cap and join attributes.
4629 % The format of the DrawStrokePolygon method is:
4631 % MagickBooleanType DrawStrokePolygon(Image *image,
4632 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4634 % A description of each parameter follows:
4636 % o image: the image.
4638 % o draw_info: the draw info.
4640 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4645 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4646 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4654 for (i=0; i < 4; i++)
4655 linecap[i]=(*primitive_info);
4656 linecap[0].coordinates=4;
4657 linecap[1].point.x+=(double) (10.0*DrawEpsilon);
4658 linecap[2].point.x+=(double) (10.0*DrawEpsilon);
4659 linecap[2].point.y+=(double) (10.0*DrawEpsilon);
4660 linecap[3].point.y+=(double) (10.0*DrawEpsilon);
4661 linecap[4].primitive=UndefinedPrimitive;
4662 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4665 static MagickBooleanType DrawStrokePolygon(Image *image,
4666 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4667 ExceptionInfo *exception)
4679 register const PrimitiveInfo
4684 Draw stroked polygon.
4686 if (image->debug != MagickFalse)
4687 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4688 " begin draw-stroke-polygon");
4689 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4690 clone_info->fill=draw_info->stroke;
4691 if (clone_info->fill_pattern != (Image *) NULL)
4692 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4693 if (clone_info->stroke_pattern != (Image *) NULL)
4694 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4695 MagickTrue,exception);
4696 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4697 clone_info->stroke_width=0.0;
4698 clone_info->fill_rule=NonZeroRule;
4700 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4702 stroke_polygon=TraceStrokePolygon(draw_info,p);
4703 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4704 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4705 q=p+p->coordinates-1;
4706 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4707 MagickTrue : MagickFalse;
4708 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4710 DrawRoundLinecap(image,draw_info,p,exception);
4711 DrawRoundLinecap(image,draw_info,q,exception);
4714 clone_info=DestroyDrawInfo(clone_info);
4715 if (image->debug != MagickFalse)
4716 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4717 " end draw-stroke-polygon");
4722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4726 % G e t A f f i n e M a t r i x %
4730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4732 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4735 % The format of the GetAffineMatrix method is:
4737 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4739 % A description of each parameter follows:
4741 % o affine_matrix: the affine matrix.
4744 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4747 assert(affine_matrix != (AffineMatrix *) NULL);
4748 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4749 affine_matrix->sx=1.0;
4750 affine_matrix->sy=1.0;
4754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4758 + G e t D r a w I n f o %
4762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4764 % GetDrawInfo() initializes draw_info to default values from image_info.
4766 % The format of the GetDrawInfo method is:
4768 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4770 % A description of each parameter follows:
4772 % o image_info: the image info..
4774 % o draw_info: the draw info.
4777 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4786 Initialize draw attributes.
4788 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4789 assert(draw_info != (DrawInfo *) NULL);
4790 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4791 GetAffineMatrix(&draw_info->affine);
4792 exception=AcquireExceptionInfo();
4793 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4795 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4797 draw_info->stroke_width=1.0;
4798 draw_info->alpha=OpaqueAlpha;
4799 draw_info->fill_rule=EvenOddRule;
4800 draw_info->linecap=ButtCap;
4801 draw_info->linejoin=MiterJoin;
4802 draw_info->miterlimit=10;
4803 draw_info->decorate=NoDecoration;
4804 draw_info->pointsize=12.0;
4805 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4806 draw_info->compose=OverCompositeOp;
4807 draw_info->render=MagickTrue;
4808 draw_info->debug=IsEventLogging();
4809 if (image_info != (ImageInfo *) NULL)
4811 draw_info->stroke_antialias=image_info->antialias;
4812 if (image_info->font != (char *) NULL)
4813 draw_info->font=AcquireString(image_info->font);
4814 if (image_info->density != (char *) NULL)
4815 draw_info->density=AcquireString(image_info->density);
4816 draw_info->text_antialias=image_info->antialias;
4817 if (image_info->pointsize != 0.0)
4818 draw_info->pointsize=image_info->pointsize;
4819 draw_info->border_color=image_info->border_color;
4820 if (image_info->server_name != (char *) NULL)
4821 draw_info->server_name=AcquireString(image_info->server_name);
4822 option=GetImageOption(image_info,"encoding");
4823 if (option != (const char *) NULL)
4824 (void) CloneString(&draw_info->encoding,option);
4825 option=GetImageOption(image_info,"kerning");
4826 if (option != (const char *) NULL)
4827 draw_info->kerning=StringToDouble(option,(char **) NULL);
4828 option=GetImageOption(image_info,"interline-spacing");
4829 if (option != (const char *) NULL)
4830 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4831 option=GetImageOption(image_info,"interword-spacing");
4832 if (option != (const char *) NULL)
4833 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4834 option=GetImageOption(image_info,"direction");
4835 if (option != (const char *) NULL)
4836 draw_info->direction=(DirectionType) ParseCommandOption(
4837 MagickDirectionOptions,MagickFalse,option);
4839 draw_info->direction=UndefinedDirection;
4840 option=GetImageOption(image_info,"fill");
4841 if (option != (const char *) NULL)
4842 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4844 option=GetImageOption(image_info,"stroke");
4845 if (option != (const char *) NULL)
4846 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4848 option=GetImageOption(image_info,"strokewidth");
4849 if (option != (const char *) NULL)
4850 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4851 option=GetImageOption(image_info,"undercolor");
4852 if (option != (const char *) NULL)
4853 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4855 option=GetImageOption(image_info,"gravity");
4856 if (option != (const char *) NULL)
4857 draw_info->gravity=(GravityType) ParseCommandOption(
4858 MagickGravityOptions,MagickFalse,option);
4860 exception=DestroyExceptionInfo(exception);
4861 draw_info->signature=MagickSignature;
4865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4869 + P e r m u t a t e %
4873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4875 % Permutate() returns the permuation of the (n,k).
4877 % The format of the Permutate method is:
4879 % void Permutate(ssize_t n,ssize_t k)
4881 % A description of each parameter follows:
4889 static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
4898 for (i=k+1; i <= n; i++)
4900 for (i=1; i <= (n-k); i++)
4906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4910 + T r a c e P r i m i t i v e %
4914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4916 % TracePrimitive is a collection of methods for generating graphic
4917 % primitives such as arcs, ellipses, paths, etc.
4921 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4922 const PointInfo end,const PointInfo degrees)
4928 center.x=0.5*(end.x+start.x);
4929 center.y=0.5*(end.y+start.y);
4930 radii.x=fabs(center.x-start.x);
4931 radii.y=fabs(center.y-start.y);
4932 TraceEllipse(primitive_info,center,radii,degrees);
4935 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4936 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4937 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4952 register MagickRealType
4956 register PrimitiveInfo
4965 if ((start.x == end.x) && (start.y == end.y))
4967 TracePoint(primitive_info,end);
4970 radii.x=fabs(arc.x);
4971 radii.y=fabs(arc.y);
4972 if ((radii.x == 0.0) || (radii.y == 0.0))
4974 TraceLine(primitive_info,start,end);
4977 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4978 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4979 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4980 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4981 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4983 if (delta < DrawEpsilon)
4985 TraceLine(primitive_info,start,end);
4990 radii.x*=sqrt((double) delta);
4991 radii.y*=sqrt((double) delta);
4993 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4994 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4995 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4996 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4997 alpha=points[1].x-points[0].x;
4998 beta=points[1].y-points[0].y;
4999 factor=DrawEpsilonReciprocal(alpha*alpha+beta*beta)-0.25;
5004 factor=sqrt((double) factor);
5005 if (sweep == large_arc)
5008 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5009 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5010 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5011 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5012 if ((theta < 0.0) && (sweep != MagickFalse))
5013 theta+=(MagickRealType) (2.0*MagickPI);
5015 if ((theta > 0.0) && (sweep == MagickFalse))
5016 theta-=(MagickRealType) (2.0*MagickPI);
5017 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5020 for (i=0; i < (ssize_t) arc_segments; i++)
5022 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5023 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5024 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5025 sin(fmod((double) beta,DegreesToRadians(360.0)));
5026 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5027 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5028 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5029 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5030 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5031 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5032 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5033 theta/arc_segments),DegreesToRadians(360.0))));
5034 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5035 theta/arc_segments),DegreesToRadians(360.0))));
5036 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5037 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5038 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5039 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5040 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5041 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5042 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5044 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5046 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5048 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5050 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5052 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5054 if (i == (ssize_t) (arc_segments-1))
5059 primitive_info->coordinates=(size_t) (p-primitive_info);
5060 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5062 p->primitive=primitive_info->primitive;
5067 static void TraceBezier(PrimitiveInfo *primitive_info,
5068 const size_t number_coordinates)
5080 register PrimitiveInfo
5092 Allocate coeficients.
5094 quantum=number_coordinates;
5095 for (i=0; i < (ssize_t) number_coordinates; i++)
5097 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5099 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5100 if (alpha > (MagickRealType) quantum)
5101 quantum=(size_t) alpha;
5102 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5103 if (alpha > (MagickRealType) quantum)
5104 quantum=(size_t) alpha;
5107 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5108 (double) BezierQuantum);
5109 control_points=quantum*number_coordinates;
5110 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5111 number_coordinates,sizeof(*coefficients));
5112 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5114 if ((coefficients == (MagickRealType *) NULL) ||
5115 (points == (PointInfo *) NULL))
5116 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5118 Compute bezier points.
5120 end=primitive_info[number_coordinates-1].point;
5121 for (i=0; i < (ssize_t) number_coordinates; i++)
5122 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5124 for (i=0; i < (ssize_t) control_points; i++)
5129 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5130 for (j=0; j < (ssize_t) number_coordinates; j++)
5132 point.x+=alpha*coefficients[j]*p->point.x;
5133 point.y+=alpha*coefficients[j]*p->point.y;
5134 alpha*=weight/(1.0-weight);
5138 weight+=1.0/control_points;
5141 Bezier curves are just short segmented polys.
5144 for (i=0; i < (ssize_t) control_points; i++)
5146 TracePoint(p,points[i]);
5151 primitive_info->coordinates=(size_t) (p-primitive_info);
5152 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5154 p->primitive=primitive_info->primitive;
5157 points=(PointInfo *) RelinquishMagickMemory(points);
5158 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5161 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5162 const PointInfo end)
5173 alpha=end.x-start.x;
5175 radius=hypot((double) alpha,(double) beta);
5176 offset.x=(double) radius;
5177 offset.y=(double) radius;
5180 TraceEllipse(primitive_info,start,offset,degrees);
5183 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5184 const PointInfo stop,const PointInfo degrees)
5195 register PrimitiveInfo
5202 Ellipses are just short segmented polys.
5204 if ((stop.x == 0.0) && (stop.y == 0.0))
5206 TracePoint(primitive_info,start);
5209 delta=2.0/MagickMax(stop.x,stop.y);
5210 step=(MagickRealType) (MagickPI/8.0);
5211 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5212 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5213 angle.x=DegreesToRadians(degrees.x);
5215 while (y < degrees.x)
5217 angle.y=(double) (DegreesToRadians(y)-DrawEpsilon);
5218 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5220 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5221 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5222 TracePoint(p,point);
5225 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5226 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5227 TracePoint(p,point);
5229 primitive_info->coordinates=(size_t) (p-primitive_info);
5230 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5232 p->primitive=primitive_info->primitive;
5237 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5238 const PointInfo end)
5240 TracePoint(primitive_info,start);
5241 if ((fabs(start.x-end.x) < DrawEpsilon) &&
5242 (fabs(start.y-end.y) < DrawEpsilon))
5244 primitive_info->primitive=PointPrimitive;
5245 primitive_info->coordinates=1;
5248 TracePoint(primitive_info+1,end);
5249 (primitive_info+1)->primitive=primitive_info->primitive;
5250 primitive_info->coordinates=2;
5253 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5256 token[MaxTextExtent];
5278 register PrimitiveInfo
5293 number_coordinates=0;
5295 primitive_type=primitive_info->primitive;
5297 for (p=path; *p != '\0'; )
5299 while (isspace((int) ((unsigned char) *p)) != 0)
5303 last_attribute=attribute;
5304 attribute=(int) (*p++);
5325 GetMagickToken(p,&p,token);
5327 GetMagickToken(p,&p,token);
5328 arc.x=StringToDouble(token,(char **) NULL);
5329 GetMagickToken(p,&p,token);
5331 GetMagickToken(p,&p,token);
5332 arc.y=StringToDouble(token,(char **) NULL);
5333 GetMagickToken(p,&p,token);
5335 GetMagickToken(p,&p,token);
5336 angle=StringToDouble(token,(char **) NULL);
5337 GetMagickToken(p,&p,token);
5339 GetMagickToken(p,&p,token);
5340 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5341 GetMagickToken(p,&p,token);
5343 GetMagickToken(p,&p,token);
5344 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5345 GetMagickToken(p,&p,token);
5347 GetMagickToken(p,&p,token);
5348 x=StringToDouble(token,(char **) NULL);
5349 GetMagickToken(p,&p,token);
5351 GetMagickToken(p,&p,token);
5352 y=StringToDouble(token,(char **) NULL);
5353 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5354 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5355 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5358 while (isspace((int) ((unsigned char) *p)) != 0)
5362 } while (IsPoint(p) != MagickFalse);
5369 Compute bezier points.
5374 for (i=1; i < 4; i++)
5376 GetMagickToken(p,&p,token);
5378 GetMagickToken(p,&p,token);
5379 x=StringToDouble(token,(char **) NULL);
5380 GetMagickToken(p,&p,token);
5382 GetMagickToken(p,&p,token);
5383 y=StringToDouble(token,(char **) NULL);
5384 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5385 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5388 for (i=0; i < 4; i++)
5389 (q+i)->point=points[i];
5393 } while (IsPoint(p) != MagickFalse);
5401 GetMagickToken(p,&p,token);
5403 GetMagickToken(p,&p,token);
5404 x=StringToDouble(token,(char **) NULL);
5405 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5406 TracePoint(q,point);
5408 } while (IsPoint(p) != MagickFalse);
5416 GetMagickToken(p,&p,token);
5418 GetMagickToken(p,&p,token);
5419 x=StringToDouble(token,(char **) NULL);
5420 GetMagickToken(p,&p,token);
5422 GetMagickToken(p,&p,token);
5423 y=StringToDouble(token,(char **) NULL);
5424 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5425 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5426 TracePoint(q,point);
5428 } while (IsPoint(p) != MagickFalse);
5434 if (q != primitive_info)
5436 primitive_info->coordinates=(size_t) (q-primitive_info);
5437 number_coordinates+=primitive_info->coordinates;
5443 GetMagickToken(p,&p,token);
5445 GetMagickToken(p,&p,token);
5446 x=StringToDouble(token,(char **) NULL);
5447 GetMagickToken(p,&p,token);
5449 GetMagickToken(p,&p,token);
5450 y=StringToDouble(token,(char **) NULL);
5451 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5452 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5456 TracePoint(q,point);
5458 if ((i != 0) && (attribute == (int) 'M'))
5460 TracePoint(q,point);
5463 } while (IsPoint(p) != MagickFalse);
5470 Compute bezier points.
5475 for (i=1; i < 3; i++)
5477 GetMagickToken(p,&p,token);
5479 GetMagickToken(p,&p,token);
5480 x=StringToDouble(token,(char **) NULL);
5481 GetMagickToken(p,&p,token);
5483 GetMagickToken(p,&p,token);
5484 y=StringToDouble(token,(char **) NULL);
5487 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5488 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5491 for (i=0; i < 3; i++)
5492 (q+i)->point=points[i];
5496 } while (IsPoint(p) != MagickFalse);
5503 Compute bezier points.
5507 points[0]=points[3];
5508 points[1].x=2.0*points[3].x-points[2].x;
5509 points[1].y=2.0*points[3].y-points[2].y;
5510 for (i=2; i < 4; i++)
5512 GetMagickToken(p,&p,token);
5514 GetMagickToken(p,&p,token);
5515 x=StringToDouble(token,(char **) NULL);
5516 GetMagickToken(p,&p,token);
5518 GetMagickToken(p,&p,token);
5519 y=StringToDouble(token,(char **) NULL);
5522 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5523 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5526 if (strchr("CcSs",last_attribute) == (char *) NULL)
5528 points[0]=points[2];
5529 points[1]=points[3];
5531 for (i=0; i < 4; i++)
5532 (q+i)->point=points[i];
5536 } while (IsPoint(p) != MagickFalse);
5543 Compute bezier points.
5547 points[0]=points[2];
5548 points[1].x=2.0*points[2].x-points[1].x;
5549 points[1].y=2.0*points[2].y-points[1].y;
5550 for (i=2; i < 3; i++)
5552 GetMagickToken(p,&p,token);
5554 GetMagickToken(p,&p,token);
5555 x=StringToDouble(token,(char **) NULL);
5556 GetMagickToken(p,&p,token);
5558 GetMagickToken(p,&p,token);
5559 y=StringToDouble(token,(char **) NULL);
5560 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5561 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5564 if (strchr("QqTt",last_attribute) == (char *) NULL)
5566 points[0]=points[2];
5567 points[1]=points[3];
5569 for (i=0; i < 3; i++)
5570 (q+i)->point=points[i];
5574 } while (IsPoint(p) != MagickFalse);
5582 GetMagickToken(p,&p,token);
5584 GetMagickToken(p,&p,token);
5585 y=StringToDouble(token,(char **) NULL);
5586 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5587 TracePoint(q,point);
5589 } while (IsPoint(p) != MagickFalse);
5596 TracePoint(q,point);
5598 primitive_info->coordinates=(size_t) (q-primitive_info);
5599 number_coordinates+=primitive_info->coordinates;
5606 if (isalpha((int) ((unsigned char) attribute)) != 0)
5607 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5613 primitive_info->coordinates=(size_t) (q-primitive_info);
5614 number_coordinates+=primitive_info->coordinates;
5615 for (i=0; i < (ssize_t) number_coordinates; i++)
5618 q->primitive=primitive_type;
5620 q->method=FillToBorderMethod;
5623 return(number_coordinates);
5626 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5627 const PointInfo end)
5632 register PrimitiveInfo
5639 TracePoint(p,start);
5643 TracePoint(p,point);
5649 TracePoint(p,point);
5651 TracePoint(p,start);
5653 primitive_info->coordinates=(size_t) (p-primitive_info);
5654 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5656 p->primitive=primitive_info->primitive;
5661 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5662 const PointInfo start,const PointInfo end,PointInfo arc)
5669 register PrimitiveInfo
5676 offset.x=fabs(end.x-start.x);
5677 offset.y=fabs(end.y-start.y);
5678 if (arc.x > (0.5*offset.x))
5680 if (arc.y > (0.5*offset.y))
5682 point.x=start.x+offset.x-arc.x;
5683 point.y=start.y+arc.y;
5686 TraceEllipse(p,point,arc,degrees);
5688 point.x=start.x+offset.x-arc.x;
5689 point.y=start.y+offset.y-arc.y;
5692 TraceEllipse(p,point,arc,degrees);
5694 point.x=start.x+arc.x;
5695 point.y=start.y+offset.y-arc.y;
5698 TraceEllipse(p,point,arc,degrees);
5700 point.x=start.x+arc.x;
5701 point.y=start.y+arc.y;
5704 TraceEllipse(p,point,arc,degrees);
5706 TracePoint(p,primitive_info->point);
5708 primitive_info->coordinates=(size_t) (p-primitive_info);
5709 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5711 p->primitive=primitive_info->primitive;
5716 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5717 const size_t number_vertices,const MagickRealType offset)
5722 register MagickRealType
5734 for (i=1; i < (ssize_t) number_vertices; i++)
5736 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5737 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5738 if ((fabs((double) dx) >= DrawEpsilon) ||
5739 (fabs((double) dy) >= DrawEpsilon))
5742 if (i == (ssize_t) number_vertices)
5743 i=(ssize_t) number_vertices-1L;
5744 distance=hypot((double) dx,(double) dy);
5745 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5746 dx*(distance+offset)/distance);
5747 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5748 dy*(distance+offset)/distance);
5749 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5751 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5752 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5753 if ((fabs((double) dx) >= DrawEpsilon) ||
5754 (fabs((double) dy) >= DrawEpsilon))
5757 distance=hypot((double) dx,(double) dy);
5758 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5759 dx*(distance+offset)/distance);
5760 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5761 dy*(distance+offset)/distance);
5764 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5765 const PrimitiveInfo *primitive_info)
5767 typedef struct _LineSegment
5819 number_vertices=primitive_info->coordinates;
5820 max_strokes=2*number_vertices+6*BezierQuantum+360;
5821 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5823 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5825 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5826 number_vertices+2UL,sizeof(*polygon_primitive));
5827 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5828 (polygon_primitive == (PrimitiveInfo *) NULL))
5829 return((PrimitiveInfo *) NULL);
5830 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5831 number_vertices*sizeof(*polygon_primitive));
5833 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5834 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5835 MagickTrue : MagickFalse;
5836 if ((draw_info->linejoin == RoundJoin) ||
5837 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5839 polygon_primitive[number_vertices]=primitive_info[1];
5842 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5844 Compute the slope for the first line segment, p.
5848 for (n=1; n < (ssize_t) number_vertices; n++)
5850 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5851 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5852 if ((fabs(dx.p) >= DrawEpsilon) || (fabs(dy.p) >= DrawEpsilon))
5855 if (n == (ssize_t) number_vertices)
5856 n=(ssize_t) number_vertices-1L;
5857 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5858 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5859 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5860 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5862 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5863 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5864 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5865 offset.y=(double) (offset.x*inverse_slope.p);
5866 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5868 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5869 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5870 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5871 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5872 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5873 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5874 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5875 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5879 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5880 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5881 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5882 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5883 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5884 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5885 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5886 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5889 Create strokes for the line join attribute: bevel, miter, round.
5893 path_q[p++]=box_q[0];
5894 path_p[q++]=box_p[0];
5895 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5898 Compute the slope for this line segment, q.
5900 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5901 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5902 dot_product=dx.q*dx.q+dy.q*dy.q;
5903 if (dot_product < 0.25)
5905 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5906 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5907 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5908 offset.y=(double) (offset.x*inverse_slope.q);
5909 dot_product=dy.q*offset.x-dx.q*offset.y;
5910 if (dot_product > 0.0)
5912 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5913 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5914 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5915 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5916 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5917 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5918 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5919 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5923 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5924 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5925 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5926 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5927 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5928 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5929 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5930 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5932 if (fabs((double) (slope.p-slope.q)) < DrawEpsilon)
5939 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5940 box_p[3].y)/(slope.p-slope.q));
5941 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5942 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5943 box_q[3].y)/(slope.p-slope.q));
5944 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5946 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5948 max_strokes+=6*BezierQuantum+360;
5949 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5951 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5953 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5955 polygon_primitive=(PrimitiveInfo *)
5956 RelinquishMagickMemory(polygon_primitive);
5957 return((PrimitiveInfo *) NULL);
5960 dot_product=dx.q*dy.p-dx.p*dy.q;
5961 if (dot_product <= 0.0)
5962 switch (draw_info->linejoin)
5966 path_q[q++]=box_q[1];
5967 path_q[q++]=box_q[2];
5968 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5969 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5970 if (dot_product <= miterlimit)
5971 path_p[p++]=box_p[4];
5974 path_p[p++]=box_p[1];
5975 path_p[p++]=box_p[2];
5981 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5982 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5983 if (dot_product <= miterlimit)
5985 path_q[q++]=box_q[4];
5986 path_p[p++]=box_p[4];
5990 path_q[q++]=box_q[1];
5991 path_q[q++]=box_q[2];
5992 path_p[p++]=box_p[1];
5993 path_p[p++]=box_p[2];
5999 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6000 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6001 if (dot_product <= miterlimit)
6002 path_p[p++]=box_p[4];
6005 path_p[p++]=box_p[1];
6006 path_p[p++]=box_p[2];
6008 center=polygon_primitive[n].point;
6009 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6010 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6011 if (theta.q < theta.p)
6012 theta.q+=(MagickRealType) (2.0*MagickPI);
6013 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6014 (2.0*sqrt((double) (1.0/mid)))));
6015 path_q[q].x=box_q[1].x;
6016 path_q[q].y=box_q[1].y;
6018 for (j=1; j < (ssize_t) arc_segments; j++)
6020 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6021 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6022 (theta.p+delta_theta),DegreesToRadians(360.0))));
6023 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6024 (theta.p+delta_theta),DegreesToRadians(360.0))));
6027 path_q[q++]=box_q[2];
6034 switch (draw_info->linejoin)
6038 path_p[p++]=box_p[1];
6039 path_p[p++]=box_p[2];
6040 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6041 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6042 if (dot_product <= miterlimit)
6043 path_q[q++]=box_q[4];
6046 path_q[q++]=box_q[1];
6047 path_q[q++]=box_q[2];
6053 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6054 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6055 if (dot_product <= miterlimit)
6057 path_q[q++]=box_q[4];
6058 path_p[p++]=box_p[4];
6062 path_q[q++]=box_q[1];
6063 path_q[q++]=box_q[2];
6064 path_p[p++]=box_p[1];
6065 path_p[p++]=box_p[2];
6071 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6072 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6073 if (dot_product <= miterlimit)
6074 path_q[q++]=box_q[4];
6077 path_q[q++]=box_q[1];
6078 path_q[q++]=box_q[2];
6080 center=polygon_primitive[n].point;
6081 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6082 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6083 if (theta.p < theta.q)
6084 theta.p+=(MagickRealType) (2.0*MagickPI);
6085 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6086 (2.0*sqrt((double) (1.0/mid)))));
6087 path_p[p++]=box_p[1];
6088 for (j=1; j < (ssize_t) arc_segments; j++)
6090 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6091 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6092 (theta.p+delta_theta),DegreesToRadians(360.0))));
6093 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6094 (theta.p+delta_theta),DegreesToRadians(360.0))));
6097 path_p[p++]=box_p[2];
6104 inverse_slope.p=inverse_slope.q;
6113 path_p[p++]=box_p[1];
6114 path_q[q++]=box_q[1];
6116 Trace stroked polygon.
6118 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6119 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6120 if (stroke_polygon != (PrimitiveInfo *) NULL)
6122 for (i=0; i < (ssize_t) p; i++)
6124 stroke_polygon[i]=polygon_primitive[0];
6125 stroke_polygon[i].point=path_p[i];
6127 if (closed_path != MagickFalse)
6129 stroke_polygon[i]=polygon_primitive[0];
6130 stroke_polygon[i].point=stroke_polygon[0].point;
6133 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6135 stroke_polygon[i]=polygon_primitive[0];
6136 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6138 if (closed_path != MagickFalse)
6140 stroke_polygon[i]=polygon_primitive[0];
6141 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6144 stroke_polygon[i]=polygon_primitive[0];
6145 stroke_polygon[i].point=stroke_polygon[0].point;
6147 stroke_polygon[i].primitive=UndefinedPrimitive;
6148 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6150 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6151 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6152 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6153 return(stroke_polygon);