2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2011 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/composite.h"
56 #include "MagickCore/composite-private.h"
57 #include "MagickCore/constitute.h"
58 #include "MagickCore/draw.h"
59 #include "MagickCore/draw-private.h"
60 #include "MagickCore/enhance.h"
61 #include "MagickCore/exception.h"
62 #include "MagickCore/exception-private.h"
63 #include "MagickCore/gem.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/image-private.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/monitor.h"
69 #include "MagickCore/monitor-private.h"
70 #include "MagickCore/option.h"
71 #include "MagickCore/paint.h"
72 #include "MagickCore/pixel-accessor.h"
73 #include "MagickCore/property.h"
74 #include "MagickCore/resample.h"
75 #include "MagickCore/resample-private.h"
76 #include "MagickCore/string_.h"
77 #include "MagickCore/string-private.h"
78 #include "MagickCore/thread-private.h"
79 #include "MagickCore/token.h"
80 #include "MagickCore/transform.h"
81 #include "MagickCore/utility.h"
86 #define BezierQuantum 200
91 typedef struct _EdgeInfo
115 typedef struct _ElementInfo
125 typedef struct _PolygonInfo
143 typedef struct _PathInfo
153 Forward declarations.
155 static MagickBooleanType
156 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *);
159 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
162 TracePath(PrimitiveInfo *,const char *);
165 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
166 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
167 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
168 TraceBezier(PrimitiveInfo *,const size_t),
169 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
170 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
172 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
173 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
175 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % A c q u i r e D r a w I n f o %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
190 % The format of the AcquireDrawInfo method is:
192 % DrawInfo *AcquireDrawInfo(void)
195 MagickExport DrawInfo *AcquireDrawInfo(void)
200 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
201 if (draw_info == (DrawInfo *) NULL)
202 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
203 GetDrawInfo((ImageInfo *) NULL,draw_info);
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % C l o n e D r a w I n f o %
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % CloneDrawInfo() makes a copy of the given draw info structure. If NULL
219 % is specified, a new image info structure is created initialized to
222 % The format of the CloneDrawInfo method is:
224 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
225 % const DrawInfo *draw_info)
227 % A description of each parameter follows:
229 % o image_info: the image info.
231 % o draw_info: the draw info.
234 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
235 const DrawInfo *draw_info)
240 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
241 if (clone_info == (DrawInfo *) NULL)
242 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
243 GetDrawInfo(image_info,clone_info);
244 if (draw_info == (DrawInfo *) NULL)
246 if (clone_info->primitive != (char *) NULL)
247 (void) CloneString(&clone_info->primitive,draw_info->primitive);
248 if (draw_info->geometry != (char *) NULL)
249 (void) CloneString(&clone_info->geometry,draw_info->geometry);
250 clone_info->viewbox=draw_info->viewbox;
251 clone_info->affine=draw_info->affine;
252 clone_info->gravity=draw_info->gravity;
253 clone_info->fill=draw_info->fill;
254 clone_info->stroke=draw_info->stroke;
255 clone_info->stroke_width=draw_info->stroke_width;
256 if (draw_info->fill_pattern != (Image *) NULL)
257 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
258 &draw_info->fill_pattern->exception);
259 if (draw_info->stroke_pattern != (Image *) NULL)
260 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
261 MagickTrue,&draw_info->stroke_pattern->exception);
262 clone_info->stroke_antialias=draw_info->stroke_antialias;
263 clone_info->text_antialias=draw_info->text_antialias;
264 clone_info->fill_rule=draw_info->fill_rule;
265 clone_info->linecap=draw_info->linecap;
266 clone_info->linejoin=draw_info->linejoin;
267 clone_info->miterlimit=draw_info->miterlimit;
268 clone_info->dash_offset=draw_info->dash_offset;
269 clone_info->decorate=draw_info->decorate;
270 clone_info->compose=draw_info->compose;
271 if (draw_info->text != (char *) NULL)
272 (void) CloneString(&clone_info->text,draw_info->text);
273 if (draw_info->font != (char *) NULL)
274 (void) CloneString(&clone_info->font,draw_info->font);
275 if (draw_info->metrics != (char *) NULL)
276 (void) CloneString(&clone_info->metrics,draw_info->metrics);
277 if (draw_info->family != (char *) NULL)
278 (void) CloneString(&clone_info->family,draw_info->family);
279 clone_info->style=draw_info->style;
280 clone_info->stretch=draw_info->stretch;
281 clone_info->weight=draw_info->weight;
282 if (draw_info->encoding != (char *) NULL)
283 (void) CloneString(&clone_info->encoding,draw_info->encoding);
284 clone_info->pointsize=draw_info->pointsize;
285 clone_info->kerning=draw_info->kerning;
286 clone_info->interline_spacing=draw_info->interline_spacing;
287 clone_info->interword_spacing=draw_info->interword_spacing;
288 clone_info->direction=draw_info->direction;
289 if (draw_info->density != (char *) NULL)
290 (void) CloneString(&clone_info->density,draw_info->density);
291 clone_info->align=draw_info->align;
292 clone_info->undercolor=draw_info->undercolor;
293 clone_info->border_color=draw_info->border_color;
294 if (draw_info->server_name != (char *) NULL)
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 if (draw_info->clip_mask != (char *) NULL)
327 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
328 clone_info->bounds=draw_info->bounds;
329 clone_info->clip_units=draw_info->clip_units;
330 clone_info->render=draw_info->render;
331 clone_info->alpha=draw_info->alpha;
332 clone_info->element_reference=draw_info->element_reference;
333 clone_info->debug=IsEventLogging();
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-MagickEpsilon) > q->points[0].y)
385 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
387 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
389 if ((p->points[0].x+MagickEpsilon) < 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) > MagickEpsilon) ||
755 (fabs(q.y-primitive_info[i].point.y) > MagickEpsilon))
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) <= MagickEpsilon) &&
765 (fabs(p.y-primitive_info[i].point.y) <= MagickEpsilon))
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)
941 % A description of each parameter follows:
943 % o image: the image.
945 % o source: the source image.
947 % o affine: the affine transform.
950 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
951 const double y,const SegmentInfo *edge)
964 Determine left and right edges.
966 inverse_edge.x1=edge->x1;
967 inverse_edge.y1=edge->y1;
968 inverse_edge.x2=edge->x2;
969 inverse_edge.y2=edge->y2;
970 z=affine->ry*y+affine->tx;
971 if (affine->sx > MagickEpsilon)
973 intercept=(-z/affine->sx);
974 x=intercept+MagickEpsilon;
975 if (x > inverse_edge.x1)
977 intercept=(-z+(double) image->columns)/affine->sx;
978 x=intercept-MagickEpsilon;
979 if (x < inverse_edge.x2)
983 if (affine->sx < -MagickEpsilon)
985 intercept=(-z+(double) image->columns)/affine->sx;
986 x=intercept+MagickEpsilon;
987 if (x > inverse_edge.x1)
989 intercept=(-z/affine->sx);
990 x=intercept-MagickEpsilon;
991 if (x < inverse_edge.x2)
995 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
997 inverse_edge.x2=edge->x1;
998 return(inverse_edge);
1001 Determine top and bottom edges.
1003 z=affine->sy*y+affine->ty;
1004 if (affine->rx > MagickEpsilon)
1006 intercept=(-z/affine->rx);
1007 x=intercept+MagickEpsilon;
1008 if (x > inverse_edge.x1)
1010 intercept=(-z+(double) image->rows)/affine->rx;
1011 x=intercept-MagickEpsilon;
1012 if (x < inverse_edge.x2)
1016 if (affine->rx < -MagickEpsilon)
1018 intercept=(-z+(double) image->rows)/affine->rx;
1019 x=intercept+MagickEpsilon;
1020 if (x > inverse_edge.x1)
1022 intercept=(-z/affine->rx);
1023 x=intercept-MagickEpsilon;
1024 if (x < inverse_edge.x2)
1028 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1030 inverse_edge.x2=edge->x2;
1031 return(inverse_edge);
1033 return(inverse_edge);
1036 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1044 determinant=1.0/(affine->sx*affine->sy-affine->rx*affine->ry);
1045 inverse_affine.sx=determinant*affine->sy;
1046 inverse_affine.rx=determinant*(-affine->rx);
1047 inverse_affine.ry=determinant*(-affine->ry);
1048 inverse_affine.sy=determinant*affine->sx;
1049 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1051 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1053 return(inverse_affine);
1056 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1063 static inline double MagickMax(const double x,const double y)
1070 static inline double MagickMin(const double x,const double y)
1077 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1078 const Image *source,const AffineMatrix *affine)
1112 Determine bounding box.
1114 assert(image != (Image *) NULL);
1115 assert(image->signature == MagickSignature);
1116 if (image->debug != MagickFalse)
1117 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1118 assert(source != (const Image *) NULL);
1119 assert(source->signature == MagickSignature);
1120 assert(affine != (AffineMatrix *) NULL);
1123 extent[1].x=(double) source->columns-1.0;
1125 extent[2].x=(double) source->columns-1.0;
1126 extent[2].y=(double) source->rows-1.0;
1128 extent[3].y=(double) source->rows-1.0;
1129 for (i=0; i < 4; i++)
1132 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1133 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1137 for (i=1; i < 4; i++)
1139 if (min.x > extent[i].x)
1141 if (min.y > extent[i].y)
1143 if (max.x < extent[i].x)
1145 if (max.y < extent[i].y)
1149 Affine transform image.
1151 exception=(&image->exception);
1152 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1153 return(MagickFalse);
1155 edge.x1=MagickMax(min.x,0.0);
1156 edge.y1=MagickMax(min.y,0.0);
1157 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1158 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1159 inverse_affine=InverseAffineMatrix(affine);
1160 GetPixelInfo(image,&zero);
1161 image_view=AcquireCacheView(image);
1162 source_view=AcquireCacheView(source);
1163 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1164 #pragma omp parallel for schedule(dynamic,4) shared(status)
1166 for (y=(ssize_t) ceil(edge.y1-0.5); y <= (ssize_t) floor(edge.y2+0.5); y++)
1187 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1188 if (inverse_edge.x2 < inverse_edge.x1)
1190 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1191 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
1192 inverse_edge.x1+0.5)+1),1,exception);
1193 if (q == (Quantum *) NULL)
1198 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1200 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1202 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1204 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1205 point.x,point.y,&pixel,exception);
1206 SetPixelInfo(image,q,&composite);
1207 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1209 SetPixelPixelInfo(image,&composite,q);
1211 q+=GetPixelChannels(image);
1213 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1216 source_view=DestroyCacheView(source_view);
1217 image_view=DestroyCacheView(image_view);
1222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226 + D r a w B o u n d i n g R e c t a n g l e s %
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1233 % is only useful for developers debugging the rendering algorithm.
1235 % The format of the DrawBoundingRectangles method is:
1237 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1238 % PolygonInfo *polygon_info)
1240 % A description of each parameter follows:
1242 % o image: the image.
1244 % o draw_info: the draw info.
1246 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1249 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1250 const PolygonInfo *polygon_info)
1275 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1276 (void) QueryColorDatabase("#0000",&clone_info->fill,&image->exception);
1277 resolution.x=DefaultResolution;
1278 resolution.y=DefaultResolution;
1279 if (clone_info->density != (char *) NULL)
1287 flags=ParseGeometry(clone_info->density,&geometry_info);
1288 resolution.x=geometry_info.rho;
1289 resolution.y=geometry_info.sigma;
1290 if ((flags & SigmaValue) == MagickFalse)
1291 resolution.y=resolution.x;
1293 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1294 clone_info->stroke_width/2.0;
1299 if (polygon_info != (PolygonInfo *) NULL)
1301 bounds=polygon_info->edges[0].bounds;
1302 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1304 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1305 bounds.x1=polygon_info->edges[i].bounds.x1;
1306 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1307 bounds.y1=polygon_info->edges[i].bounds.y1;
1308 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1309 bounds.x2=polygon_info->edges[i].bounds.x2;
1310 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1311 bounds.y2=polygon_info->edges[i].bounds.y2;
1314 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1315 image->columns ? (double) image->columns-1 : bounds.x1;
1317 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1318 image->rows ? (double) image->rows-1 : bounds.y1;
1320 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1321 image->columns ? (double) image->columns-1 : bounds.x2;
1323 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1324 image->rows ? (double) image->rows-1 : bounds.y2;
1325 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1327 if (polygon_info->edges[i].direction != 0)
1328 (void) QueryColorDatabase("red",&clone_info->stroke,
1331 (void) QueryColorDatabase("green",&clone_info->stroke,
1333 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1334 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1335 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1336 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1337 primitive_info[0].primitive=RectanglePrimitive;
1338 TraceRectangle(primitive_info,start,end);
1339 primitive_info[0].method=ReplaceMethod;
1340 coordinates=(ssize_t) primitive_info[0].coordinates;
1341 primitive_info[coordinates].primitive=UndefinedPrimitive;
1342 (void) DrawPrimitive(image,clone_info,primitive_info);
1345 (void) QueryColorDatabase("blue",&clone_info->stroke,&image->exception);
1346 start.x=(double) (bounds.x1-mid);
1347 start.y=(double) (bounds.y1-mid);
1348 end.x=(double) (bounds.x2+mid);
1349 end.y=(double) (bounds.y2+mid);
1350 primitive_info[0].primitive=RectanglePrimitive;
1351 TraceRectangle(primitive_info,start,end);
1352 primitive_info[0].method=ReplaceMethod;
1353 coordinates=(ssize_t) primitive_info[0].coordinates;
1354 primitive_info[coordinates].primitive=UndefinedPrimitive;
1355 (void) DrawPrimitive(image,clone_info,primitive_info);
1356 clone_info=DestroyDrawInfo(clone_info);
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 % D r a w C l i p P a t h %
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % DrawClipPath() draws the clip path on the image mask.
1372 % The format of the DrawClipPath method is:
1374 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1377 % A description of each parameter follows:
1379 % o image: the image.
1381 % o draw_info: the draw info.
1383 % o name: the name of the clip path.
1386 MagickExport MagickBooleanType DrawClipPath(Image *image,
1387 const DrawInfo *draw_info,const char *name)
1390 clip_mask[MaxTextExtent];
1401 assert(image != (Image *) NULL);
1402 assert(image->signature == MagickSignature);
1403 if (image->debug != MagickFalse)
1404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1405 assert(draw_info != (const DrawInfo *) NULL);
1406 (void) FormatLocaleString(clip_mask,MaxTextExtent,"%s",name);
1407 value=GetImageArtifact(image,clip_mask);
1408 if (value == (const char *) NULL)
1409 return(MagickFalse);
1410 if (image->clip_mask == (Image *) NULL)
1415 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,
1417 if (clip_mask == (Image *) NULL)
1418 return(MagickFalse);
1419 (void) SetImageClipMask(image,clip_mask);
1420 clip_mask=DestroyImage(clip_mask);
1422 (void) QueryColorDatabase("#00000000",&image->clip_mask->background_color,
1424 image->clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1425 (void) SetImageBackgroundColor(image->clip_mask);
1426 if (image->debug != MagickFalse)
1427 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1428 draw_info->clip_mask);
1429 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1430 (void) CloneString(&clone_info->primitive,value);
1431 (void) QueryColorDatabase("#ffffff",&clone_info->fill,&image->exception);
1432 clone_info->clip_mask=(char *) NULL;
1433 status=DrawImage(image->clip_mask,clone_info);
1434 status|=NegateImage(image->clip_mask,MagickFalse,&image->exception);
1435 clone_info=DestroyDrawInfo(clone_info);
1436 if (image->debug != MagickFalse)
1437 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1438 return(status != 0 ? MagickTrue : MagickFalse);
1442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1446 + D r a w D a s h P o l y g o n %
1450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1453 % image while respecting the dash offset and dash pattern attributes.
1455 % The format of the DrawDashPolygon method is:
1457 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1458 % const PrimitiveInfo *primitive_info,Image *image)
1460 % A description of each parameter follows:
1462 % o draw_info: the draw info.
1464 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1466 % o image: the image.
1470 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1471 const PrimitiveInfo *primitive_info,Image *image)
1492 register MagickRealType
1503 assert(draw_info != (const DrawInfo *) NULL);
1504 if (image->debug != MagickFalse)
1505 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1506 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1507 clone_info->miterlimit=0;
1508 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1509 number_vertices=(size_t) i;
1510 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1511 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1512 if (dash_polygon == (PrimitiveInfo *) NULL)
1513 return(MagickFalse);
1514 dash_polygon[0]=primitive_info[0];
1515 scale=ExpandAffine(&draw_info->affine);
1516 length=scale*(draw_info->dash_pattern[0]-0.5);
1517 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1519 for (n=0; offset > 0.0; j=0)
1521 if (draw_info->dash_pattern[n] <= 0.0)
1523 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1524 if (offset > length)
1528 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1531 if (offset < length)
1543 for (i=1; i < (ssize_t) number_vertices; i++)
1545 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1546 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1547 maximum_length=hypot((double) dx,dy);
1551 if (draw_info->dash_pattern[n] == 0.0)
1553 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1555 for (total_length=0.0; (total_length+length) < maximum_length; )
1557 total_length+=length;
1558 if ((n & 0x01) != 0)
1560 dash_polygon[0]=primitive_info[0];
1561 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1562 total_length/maximum_length);
1563 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1564 total_length/maximum_length);
1569 if ((j+1) > (ssize_t) (2*number_vertices))
1571 dash_polygon[j]=primitive_info[i-1];
1572 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1573 total_length/maximum_length);
1574 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1575 total_length/maximum_length);
1576 dash_polygon[j].coordinates=1;
1578 dash_polygon[0].coordinates=(size_t) j;
1579 dash_polygon[j].primitive=UndefinedPrimitive;
1580 status|=DrawStrokePolygon(image,clone_info,dash_polygon);
1583 if (draw_info->dash_pattern[n] == 0.0)
1585 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1587 length-=(maximum_length-total_length);
1588 if ((n & 0x01) != 0)
1590 dash_polygon[j]=primitive_info[i];
1591 dash_polygon[j].coordinates=1;
1594 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1596 dash_polygon[j]=primitive_info[i-1];
1597 dash_polygon[j].point.x+=MagickEpsilon;
1598 dash_polygon[j].point.y+=MagickEpsilon;
1599 dash_polygon[j].coordinates=1;
1601 dash_polygon[0].coordinates=(size_t) j;
1602 dash_polygon[j].primitive=UndefinedPrimitive;
1603 status|=DrawStrokePolygon(image,clone_info,dash_polygon);
1605 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1606 clone_info=DestroyDrawInfo(clone_info);
1607 if (image->debug != MagickFalse)
1608 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1609 return(status != 0 ? MagickTrue : MagickFalse);
1613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617 % D r a w I m a g e %
1621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1623 % DrawImage() draws a graphic primitive on your image. The primitive
1624 % may be represented as a string or filename. Precede the filename with an
1625 % "at" sign (@) and the contents of the file are drawn on the image. You
1626 % can affect how text is drawn by setting one or more members of the draw
1629 % The format of the DrawImage method is:
1631 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
1633 % A description of each parameter follows:
1635 % o image: the image.
1637 % o draw_info: the draw info.
1641 static inline MagickBooleanType IsPoint(const char *point)
1649 value=InterpretLocaleValue(point,&p);
1650 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1653 static inline void TracePoint(PrimitiveInfo *primitive_info,
1654 const PointInfo point)
1656 primitive_info->coordinates=1;
1657 primitive_info->point=point;
1660 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
1662 #define RenderImageTag "Render/Image"
1669 key[2*MaxTextExtent],
1670 keyword[MaxTextExtent],
1671 geometry[MaxTextExtent],
1672 name[MaxTextExtent],
1673 pattern[MaxTextExtent],
1724 Ensure the annotation info is valid.
1726 assert(image != (Image *) NULL);
1727 assert(image->signature == MagickSignature);
1728 if (image->debug != MagickFalse)
1729 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1730 assert(draw_info != (DrawInfo *) NULL);
1731 assert(draw_info->signature == MagickSignature);
1732 if (image->debug != MagickFalse)
1733 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1734 if ((draw_info->primitive == (char *) NULL) ||
1735 (*draw_info->primitive == '\0'))
1736 return(MagickFalse);
1737 if (image->debug != MagickFalse)
1738 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1739 if (*draw_info->primitive != '@')
1740 primitive=AcquireString(draw_info->primitive);
1742 primitive=FileToString(draw_info->primitive+1,~0,&image->exception);
1743 if (primitive == (char *) NULL)
1744 return(MagickFalse);
1745 primitive_extent=(MagickRealType) strlen(primitive);
1746 (void) SetImageArtifact(image,"MVG",primitive);
1749 Allocate primitive info memory.
1751 graphic_context=(DrawInfo **) AcquireMagickMemory(
1752 sizeof(*graphic_context));
1753 if (graphic_context == (DrawInfo **) NULL)
1755 primitive=DestroyString(primitive);
1756 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1760 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1761 sizeof(*primitive_info));
1762 if (primitive_info == (PrimitiveInfo *) NULL)
1764 primitive=DestroyString(primitive);
1765 for ( ; n >= 0; n--)
1766 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1767 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1768 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1771 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1772 graphic_context[n]->viewbox=image->page;
1773 if ((image->page.width == 0) || (image->page.height == 0))
1775 graphic_context[n]->viewbox.width=image->columns;
1776 graphic_context[n]->viewbox.height=image->rows;
1778 token=AcquireString(primitive);
1779 (void) QueryColorDatabase("#000000",&start_color,&image->exception);
1780 if (SetImageStorageClass(image,DirectClass,&image->exception) == MagickFalse)
1781 return(MagickFalse);
1783 for (q=primitive; *q != '\0'; )
1786 Interpret graphic primitive.
1788 GetMagickToken(q,&q,keyword);
1789 if (*keyword == '\0')
1791 if (*keyword == '#')
1796 while ((*q != '\n') && (*q != '\0'))
1800 p=q-strlen(keyword)-1;
1801 primitive_type=UndefinedPrimitive;
1802 current=graphic_context[n]->affine;
1803 GetAffineMatrix(&affine);
1811 if (LocaleCompare("affine",keyword) == 0)
1813 GetMagickToken(q,&q,token);
1814 affine.sx=InterpretLocaleValue(token,(char **) NULL);
1815 GetMagickToken(q,&q,token);
1817 GetMagickToken(q,&q,token);
1818 affine.rx=InterpretLocaleValue(token,(char **) NULL);
1819 GetMagickToken(q,&q,token);
1821 GetMagickToken(q,&q,token);
1822 affine.ry=InterpretLocaleValue(token,(char **) NULL);
1823 GetMagickToken(q,&q,token);
1825 GetMagickToken(q,&q,token);
1826 affine.sy=InterpretLocaleValue(token,(char **) NULL);
1827 GetMagickToken(q,&q,token);
1829 GetMagickToken(q,&q,token);
1830 affine.tx=InterpretLocaleValue(token,(char **) NULL);
1831 GetMagickToken(q,&q,token);
1833 GetMagickToken(q,&q,token);
1834 affine.ty=InterpretLocaleValue(token,(char **) NULL);
1837 if (LocaleCompare("arc",keyword) == 0)
1839 primitive_type=ArcPrimitive;
1848 if (LocaleCompare("bezier",keyword) == 0)
1850 primitive_type=BezierPrimitive;
1853 if (LocaleCompare("border-color",keyword) == 0)
1855 GetMagickToken(q,&q,token);
1856 (void) QueryColorDatabase(token,&graphic_context[n]->border_color,
1866 if (LocaleCompare("clip-path",keyword) == 0)
1871 GetMagickToken(q,&q,token);
1872 (void) CloneString(&graphic_context[n]->clip_mask,token);
1873 (void) DrawClipPath(image,graphic_context[n],
1874 graphic_context[n]->clip_mask);
1877 if (LocaleCompare("clip-rule",keyword) == 0)
1882 GetMagickToken(q,&q,token);
1883 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1885 if (fill_rule == -1)
1890 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1893 if (LocaleCompare("clip-units",keyword) == 0)
1898 GetMagickToken(q,&q,token);
1899 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1901 if (clip_units == -1)
1906 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1907 if (clip_units == ObjectBoundingBox)
1909 GetAffineMatrix(¤t);
1910 affine.sx=draw_info->bounds.x2;
1911 affine.sy=draw_info->bounds.y2;
1912 affine.tx=draw_info->bounds.x1;
1913 affine.ty=draw_info->bounds.y1;
1918 if (LocaleCompare("circle",keyword) == 0)
1920 primitive_type=CirclePrimitive;
1923 if (LocaleCompare("color",keyword) == 0)
1925 primitive_type=ColorPrimitive;
1934 if (LocaleCompare("decorate",keyword) == 0)
1939 GetMagickToken(q,&q,token);
1940 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1947 graphic_context[n]->decorate=(DecorationType) decorate;
1956 if (LocaleCompare("ellipse",keyword) == 0)
1958 primitive_type=EllipsePrimitive;
1961 if (LocaleCompare("encoding",keyword) == 0)
1963 GetMagickToken(q,&q,token);
1964 (void) CloneString(&graphic_context[n]->encoding,token);
1973 if (LocaleCompare("fill",keyword) == 0)
1975 GetMagickToken(q,&q,token);
1976 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1977 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1978 (void) DrawPatternPath(image,draw_info,token,
1979 &graphic_context[n]->fill_pattern);
1982 status=QueryColorDatabase(token,&graphic_context[n]->fill,
1984 if (status == MagickFalse)
1989 pattern_info=AcquireImageInfo();
1990 (void) CopyMagickString(pattern_info->filename,token,
1992 graphic_context[n]->fill_pattern=
1993 ReadImage(pattern_info,&image->exception);
1994 CatchException(&image->exception);
1995 pattern_info=DestroyImageInfo(pattern_info);
2000 if (LocaleCompare("fill-opacity",keyword) == 0)
2002 GetMagickToken(q,&q,token);
2003 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2004 graphic_context[n]->fill.alpha=ClampToQuantum((MagickRealType)
2005 QuantumRange*factor*InterpretLocaleValue(token,
2009 if (LocaleCompare("fill-rule",keyword) == 0)
2014 GetMagickToken(q,&q,token);
2015 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2017 if (fill_rule == -1)
2022 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2025 if (LocaleCompare("font",keyword) == 0)
2027 GetMagickToken(q,&q,token);
2028 (void) CloneString(&graphic_context[n]->font,token);
2029 if (LocaleCompare("none",token) == 0)
2030 graphic_context[n]->font=(char *)
2031 RelinquishMagickMemory(graphic_context[n]->font);
2034 if (LocaleCompare("font-family",keyword) == 0)
2036 GetMagickToken(q,&q,token);
2037 (void) CloneString(&graphic_context[n]->family,token);
2040 if (LocaleCompare("font-size",keyword) == 0)
2042 GetMagickToken(q,&q,token);
2043 graphic_context[n]->pointsize=InterpretLocaleValue(token,
2047 if (LocaleCompare("font-stretch",keyword) == 0)
2052 GetMagickToken(q,&q,token);
2053 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2059 graphic_context[n]->stretch=(StretchType) stretch;
2062 if (LocaleCompare("font-style",keyword) == 0)
2067 GetMagickToken(q,&q,token);
2068 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2074 graphic_context[n]->style=(StyleType) style;
2077 if (LocaleCompare("font-weight",keyword) == 0)
2079 GetMagickToken(q,&q,token);
2080 graphic_context[n]->weight=StringToUnsignedLong(token);
2081 if (LocaleCompare(token,"all") == 0)
2082 graphic_context[n]->weight=0;
2083 if (LocaleCompare(token,"bold") == 0)
2084 graphic_context[n]->weight=700;
2085 if (LocaleCompare(token,"bolder") == 0)
2086 if (graphic_context[n]->weight <= 800)
2087 graphic_context[n]->weight+=100;
2088 if (LocaleCompare(token,"lighter") == 0)
2089 if (graphic_context[n]->weight >= 100)
2090 graphic_context[n]->weight-=100;
2091 if (LocaleCompare(token,"normal") == 0)
2092 graphic_context[n]->weight=400;
2101 if (LocaleCompare("gradient-units",keyword) == 0)
2103 GetMagickToken(q,&q,token);
2106 if (LocaleCompare("gravity",keyword) == 0)
2111 GetMagickToken(q,&q,token);
2112 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2118 graphic_context[n]->gravity=(GravityType) gravity;
2127 if (LocaleCompare("image",keyword) == 0)
2132 primitive_type=ImagePrimitive;
2133 GetMagickToken(q,&q,token);
2134 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2140 graphic_context[n]->compose=(CompositeOperator) compose;
2143 if (LocaleCompare("interline-spacing",keyword) == 0)
2145 GetMagickToken(q,&q,token);
2146 graphic_context[n]->interline_spacing=InterpretLocaleValue(token,
2150 if (LocaleCompare("interword-spacing",keyword) == 0)
2152 GetMagickToken(q,&q,token);
2153 graphic_context[n]->interword_spacing=InterpretLocaleValue(token,
2163 if (LocaleCompare("kerning",keyword) == 0)
2165 GetMagickToken(q,&q,token);
2166 graphic_context[n]->kerning=InterpretLocaleValue(token,
2176 if (LocaleCompare("line",keyword) == 0)
2178 primitive_type=LinePrimitive;
2187 if (LocaleCompare("matte",keyword) == 0)
2189 primitive_type=MattePrimitive;
2198 if (LocaleCompare("offset",keyword) == 0)
2200 GetMagickToken(q,&q,token);
2203 if (LocaleCompare("opacity",keyword) == 0)
2205 GetMagickToken(q,&q,token);
2206 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2207 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2208 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
2209 factor*InterpretLocaleValue(token,(char **) NULL))));
2210 graphic_context[n]->fill.alpha=graphic_context[n]->alpha;
2211 graphic_context[n]->stroke.alpha=graphic_context[n]->alpha;
2220 if (LocaleCompare("path",keyword) == 0)
2222 primitive_type=PathPrimitive;
2225 if (LocaleCompare("point",keyword) == 0)
2227 primitive_type=PointPrimitive;
2230 if (LocaleCompare("polyline",keyword) == 0)
2232 primitive_type=PolylinePrimitive;
2235 if (LocaleCompare("polygon",keyword) == 0)
2237 primitive_type=PolygonPrimitive;
2240 if (LocaleCompare("pop",keyword) == 0)
2242 GetMagickToken(q,&q,token);
2243 if (LocaleCompare("clip-path",token) == 0)
2245 if (LocaleCompare("defs",token) == 0)
2247 if (LocaleCompare("gradient",token) == 0)
2249 if (LocaleCompare("graphic-context",token) == 0)
2253 (void) ThrowMagickException(&image->exception,
2254 GetMagickModule(),DrawError,
2255 "UnbalancedGraphicContextPushPop","`%s'",token);
2259 if (graphic_context[n]->clip_mask != (char *) NULL)
2260 if (LocaleCompare(graphic_context[n]->clip_mask,
2261 graphic_context[n-1]->clip_mask) != 0)
2262 (void) SetImageClipMask(image,(Image *) NULL);
2263 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2267 if (LocaleCompare("pattern",token) == 0)
2272 if (LocaleCompare("push",keyword) == 0)
2274 GetMagickToken(q,&q,token);
2275 if (LocaleCompare("clip-path",token) == 0)
2278 name[MaxTextExtent];
2280 GetMagickToken(q,&q,token);
2281 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2282 for (p=q; *q != '\0'; )
2284 GetMagickToken(q,&q,token);
2285 if (LocaleCompare(token,"pop") != 0)
2287 GetMagickToken(q,(const char **) NULL,token);
2288 if (LocaleCompare(token,"clip-path") != 0)
2292 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2293 (void) SetImageArtifact(image,name,token);
2294 GetMagickToken(q,&q,token);
2297 if (LocaleCompare("gradient",token) == 0)
2300 key[2*MaxTextExtent],
2301 name[MaxTextExtent],
2302 type[MaxTextExtent];
2307 GetMagickToken(q,&q,token);
2308 (void) CopyMagickString(name,token,MaxTextExtent);
2309 GetMagickToken(q,&q,token);
2310 (void) CopyMagickString(type,token,MaxTextExtent);
2311 GetMagickToken(q,&q,token);
2312 segment.x1=InterpretLocaleValue(token,(char **) NULL);
2313 GetMagickToken(q,&q,token);
2315 GetMagickToken(q,&q,token);
2316 segment.y1=InterpretLocaleValue(token,(char **) NULL);
2317 GetMagickToken(q,&q,token);
2319 GetMagickToken(q,&q,token);
2320 segment.x2=InterpretLocaleValue(token,(char **) NULL);
2321 GetMagickToken(q,&q,token);
2323 GetMagickToken(q,&q,token);
2324 segment.y2=InterpretLocaleValue(token,(char **) NULL);
2325 if (LocaleCompare(type,"radial") == 0)
2327 GetMagickToken(q,&q,token);
2329 GetMagickToken(q,&q,token);
2331 for (p=q; *q != '\0'; )
2333 GetMagickToken(q,&q,token);
2334 if (LocaleCompare(token,"pop") != 0)
2336 GetMagickToken(q,(const char **) NULL,token);
2337 if (LocaleCompare(token,"gradient") != 0)
2341 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2342 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2343 graphic_context[n]->affine.ry*segment.y1+
2344 graphic_context[n]->affine.tx;
2345 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2346 graphic_context[n]->affine.sy*segment.y1+
2347 graphic_context[n]->affine.ty;
2348 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2349 graphic_context[n]->affine.ry*segment.y2+
2350 graphic_context[n]->affine.tx;
2351 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2352 graphic_context[n]->affine.sy*segment.y2+
2353 graphic_context[n]->affine.ty;
2354 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2355 (void) SetImageArtifact(image,key,token);
2356 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2357 (void) FormatLocaleString(geometry,MaxTextExtent,
2358 "%gx%g%+.15g%+.15g",
2359 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2360 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2361 bounds.x1,bounds.y1);
2362 (void) SetImageArtifact(image,key,geometry);
2363 GetMagickToken(q,&q,token);
2366 if (LocaleCompare("pattern",token) == 0)
2371 GetMagickToken(q,&q,token);
2372 (void) CopyMagickString(name,token,MaxTextExtent);
2373 GetMagickToken(q,&q,token);
2374 bounds.x=(ssize_t) ceil(InterpretLocaleValue(token,
2375 (char **) NULL)-0.5);
2376 GetMagickToken(q,&q,token);
2378 GetMagickToken(q,&q,token);
2379 bounds.y=(ssize_t) ceil(InterpretLocaleValue(token,
2380 (char **) NULL)-0.5);
2381 GetMagickToken(q,&q,token);
2383 GetMagickToken(q,&q,token);
2384 bounds.width=(size_t) floor(InterpretLocaleValue(token,
2385 (char **) NULL)+0.5);
2386 GetMagickToken(q,&q,token);
2388 GetMagickToken(q,&q,token);
2389 bounds.height=(size_t) floor(InterpretLocaleValue(token,
2390 (char **) NULL)+0.5);
2391 for (p=q; *q != '\0'; )
2393 GetMagickToken(q,&q,token);
2394 if (LocaleCompare(token,"pop") != 0)
2396 GetMagickToken(q,(const char **) NULL,token);
2397 if (LocaleCompare(token,"pattern") != 0)
2401 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2402 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2403 (void) SetImageArtifact(image,key,token);
2404 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2405 (void) FormatLocaleString(geometry,MaxTextExtent,
2406 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2407 bounds.height,(double) bounds.x,(double) bounds.y);
2408 (void) SetImageArtifact(image,key,geometry);
2409 GetMagickToken(q,&q,token);
2412 if (LocaleCompare("graphic-context",token) == 0)
2415 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2416 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2417 if (graphic_context == (DrawInfo **) NULL)
2419 (void) ThrowMagickException(&image->exception,
2420 GetMagickModule(),ResourceLimitError,
2421 "MemoryAllocationFailed","`%s'",image->filename);
2424 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2425 graphic_context[n-1]);
2428 if (LocaleCompare("defs",token) == 0)
2439 if (LocaleCompare("rectangle",keyword) == 0)
2441 primitive_type=RectanglePrimitive;
2444 if (LocaleCompare("rotate",keyword) == 0)
2446 GetMagickToken(q,&q,token);
2447 angle=InterpretLocaleValue(token,(char **) NULL);
2448 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2449 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2450 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2451 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2454 if (LocaleCompare("roundRectangle",keyword) == 0)
2456 primitive_type=RoundRectanglePrimitive;
2465 if (LocaleCompare("scale",keyword) == 0)
2467 GetMagickToken(q,&q,token);
2468 affine.sx=InterpretLocaleValue(token,(char **) NULL);
2469 GetMagickToken(q,&q,token);
2471 GetMagickToken(q,&q,token);
2472 affine.sy=InterpretLocaleValue(token,(char **) NULL);
2475 if (LocaleCompare("skewX",keyword) == 0)
2477 GetMagickToken(q,&q,token);
2478 angle=InterpretLocaleValue(token,(char **) NULL);
2479 affine.ry=sin(DegreesToRadians(angle));
2482 if (LocaleCompare("skewY",keyword) == 0)
2484 GetMagickToken(q,&q,token);
2485 angle=InterpretLocaleValue(token,(char **) NULL);
2486 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2489 if (LocaleCompare("stop-color",keyword) == 0)
2494 GetMagickToken(q,&q,token);
2495 (void) QueryColorDatabase(token,&stop_color,&image->exception);
2496 (void) GradientImage(image,LinearGradient,ReflectSpread,
2497 &start_color,&stop_color,&image->exception);
2498 start_color=stop_color;
2499 GetMagickToken(q,&q,token);
2502 if (LocaleCompare("stroke",keyword) == 0)
2504 GetMagickToken(q,&q,token);
2505 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2506 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2507 (void) DrawPatternPath(image,draw_info,token,
2508 &graphic_context[n]->stroke_pattern);
2511 status=QueryColorDatabase(token,&graphic_context[n]->stroke,
2513 if (status == MagickFalse)
2518 pattern_info=AcquireImageInfo();
2519 (void) CopyMagickString(pattern_info->filename,token,
2521 graphic_context[n]->stroke_pattern=
2522 ReadImage(pattern_info,&image->exception);
2523 CatchException(&image->exception);
2524 pattern_info=DestroyImageInfo(pattern_info);
2529 if (LocaleCompare("stroke-antialias",keyword) == 0)
2531 GetMagickToken(q,&q,token);
2532 graphic_context[n]->stroke_antialias=
2533 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2536 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2538 if (graphic_context[n]->dash_pattern != (double *) NULL)
2539 graphic_context[n]->dash_pattern=(double *)
2540 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2541 if (IsPoint(q) != MagickFalse)
2547 GetMagickToken(p,&p,token);
2549 GetMagickToken(p,&p,token);
2550 for (x=0; IsPoint(token) != MagickFalse; x++)
2552 GetMagickToken(p,&p,token);
2554 GetMagickToken(p,&p,token);
2556 graphic_context[n]->dash_pattern=(double *)
2557 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2558 sizeof(*graphic_context[n]->dash_pattern));
2559 if (graphic_context[n]->dash_pattern == (double *) NULL)
2561 (void) ThrowMagickException(&image->exception,
2562 GetMagickModule(),ResourceLimitError,
2563 "MemoryAllocationFailed","`%s'",image->filename);
2566 for (j=0; j < x; j++)
2568 GetMagickToken(q,&q,token);
2570 GetMagickToken(q,&q,token);
2571 graphic_context[n]->dash_pattern[j]=InterpretLocaleValue(
2572 token,(char **) NULL);
2574 if ((x & 0x01) != 0)
2575 for ( ; j < (2*x); j++)
2576 graphic_context[n]->dash_pattern[j]=
2577 graphic_context[n]->dash_pattern[j-x];
2578 graphic_context[n]->dash_pattern[j]=0.0;
2581 GetMagickToken(q,&q,token);
2584 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2586 GetMagickToken(q,&q,token);
2587 graphic_context[n]->dash_offset=InterpretLocaleValue(token,
2591 if (LocaleCompare("stroke-linecap",keyword) == 0)
2596 GetMagickToken(q,&q,token);
2597 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2603 graphic_context[n]->linecap=(LineCap) linecap;
2606 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2611 GetMagickToken(q,&q,token);
2612 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,token);
2618 graphic_context[n]->linejoin=(LineJoin) linejoin;
2621 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2623 GetMagickToken(q,&q,token);
2624 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2627 if (LocaleCompare("stroke-opacity",keyword) == 0)
2629 GetMagickToken(q,&q,token);
2630 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2631 graphic_context[n]->stroke.alpha=ClampToQuantum((MagickRealType)
2632 QuantumRange*factor*InterpretLocaleValue(token,
2636 if (LocaleCompare("stroke-width",keyword) == 0)
2638 GetMagickToken(q,&q,token);
2639 graphic_context[n]->stroke_width=InterpretLocaleValue(token,
2649 if (LocaleCompare("text",keyword) == 0)
2651 primitive_type=TextPrimitive;
2654 if (LocaleCompare("text-align",keyword) == 0)
2659 GetMagickToken(q,&q,token);
2660 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2666 graphic_context[n]->align=(AlignType) align;
2669 if (LocaleCompare("text-anchor",keyword) == 0)
2674 GetMagickToken(q,&q,token);
2675 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) QueryColorDatabase(token,&graphic_context[n]->undercolor,
2698 if (LocaleCompare("translate",keyword) == 0)
2700 GetMagickToken(q,&q,token);
2701 affine.tx=InterpretLocaleValue(token,(char **) NULL);
2702 GetMagickToken(q,&q,token);
2704 GetMagickToken(q,&q,token);
2705 affine.ty=InterpretLocaleValue(token,(char **) NULL);
2714 if (LocaleCompare("viewbox",keyword) == 0)
2716 GetMagickToken(q,&q,token);
2717 graphic_context[n]->viewbox.x=(ssize_t) ceil(InterpretLocaleValue(
2718 token,(char **) NULL)-0.5);
2719 GetMagickToken(q,&q,token);
2721 GetMagickToken(q,&q,token);
2722 graphic_context[n]->viewbox.y=(ssize_t) ceil(InterpretLocaleValue(
2723 token,(char **) NULL)-0.5);
2724 GetMagickToken(q,&q,token);
2726 GetMagickToken(q,&q,token);
2727 graphic_context[n]->viewbox.width=(size_t) floor(
2728 InterpretLocaleValue(token,(char **) NULL)+0.5);
2729 GetMagickToken(q,&q,token);
2731 GetMagickToken(q,&q,token);
2732 graphic_context[n]->viewbox.height=(size_t) floor(
2733 InterpretLocaleValue(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=InterpretLocaleValue(token,(char **) NULL);
2782 GetMagickToken(q,&q,token);
2784 GetMagickToken(q,&q,token);
2785 point.y=InterpretLocaleValue(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(&image->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(&image->exception,GetMagickModule(),
2849 DrawError,"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=InterpretLocaleValue(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(&image->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);
3043 primitive_info[j].method=(PaintMethod) method;
3048 if (primitive_info[j].coordinates != 1)
3054 GetMagickToken(q,&q,token);
3055 primitive_info[j].text=AcquireString(token);
3058 case ImagePrimitive:
3060 if (primitive_info[j].coordinates != 2)
3065 GetMagickToken(q,&q,token);
3066 primitive_info[j].text=AcquireString(token);
3070 if (primitive_info == (PrimitiveInfo *) NULL)
3072 if (image->debug != MagickFalse)
3073 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3074 if (status == MagickFalse)
3076 primitive_info[i].primitive=UndefinedPrimitive;
3082 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3084 point=primitive_info[i].point;
3085 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3086 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3087 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3088 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3089 point=primitive_info[i].point;
3090 if (point.x < graphic_context[n]->bounds.x1)
3091 graphic_context[n]->bounds.x1=point.x;
3092 if (point.y < graphic_context[n]->bounds.y1)
3093 graphic_context[n]->bounds.y1=point.y;
3094 if (point.x > graphic_context[n]->bounds.x2)
3095 graphic_context[n]->bounds.x2=point.x;
3096 if (point.y > graphic_context[n]->bounds.y2)
3097 graphic_context[n]->bounds.y2=point.y;
3098 if (primitive_info[i].primitive == ImagePrimitive)
3100 if (i >= (ssize_t) number_points)
3101 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3103 if (graphic_context[n]->render != MagickFalse)
3105 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3106 (LocaleCompare(graphic_context[n]->clip_mask,
3107 graphic_context[n-1]->clip_mask) != 0))
3108 (void) DrawClipPath(image,graphic_context[n],
3109 graphic_context[n]->clip_mask);
3110 (void) DrawPrimitive(image,graphic_context[n],primitive_info);
3112 if (primitive_info->text != (char *) NULL)
3113 primitive_info->text=(char *) RelinquishMagickMemory(
3114 primitive_info->text);
3115 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3117 if (proceed == MagickFalse)
3120 if (image->debug != MagickFalse)
3121 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3123 Relinquish resources.
3125 token=DestroyString(token);
3126 if (primitive_info != (PrimitiveInfo *) NULL)
3127 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3128 primitive=DestroyString(primitive);
3129 for ( ; n >= 0; n--)
3130 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3131 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3132 if (status == MagickFalse)
3133 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3143 % D r a w G r a d i e n t I m a g e %
3147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3149 % DrawGradientImage() draws a linear gradient on the image.
3151 % The format of the DrawGradientImage method is:
3153 % MagickBooleanType DrawGradientImage(Image *image,
3154 % const DrawInfo *draw_info)
3156 % A description of each parameter follows:
3158 % o image: the image.
3160 % o _info: the draw info.
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=1.0/(gamma <= MagickEpsilon ? 1.0 : 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)
3252 Draw linear or radial gradient on image.
3254 assert(image != (Image *) NULL);
3255 assert(image->signature == MagickSignature);
3256 if (image->debug != MagickFalse)
3257 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3258 assert(draw_info != (const DrawInfo *) NULL);
3259 gradient=(&draw_info->gradient);
3260 gradient_vector=(&gradient->gradient_vector);
3261 point.x=gradient_vector->x2-gradient_vector->x1;
3262 point.y=gradient_vector->y2-gradient_vector->y1;
3263 length=sqrt(point.x*point.x+point.y*point.y);
3264 bounding_box=gradient->bounding_box;
3266 exception=(&image->exception);
3267 GetPixelInfo(image,&zero);
3268 image_view=AcquireCacheView(image);
3269 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3270 #pragma omp parallel for schedule(dynamic,4) shared(status)
3272 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3292 if (status == MagickFalse)
3294 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3295 if (q == (Quantum *) NULL)
3302 offset=GetStopColorOffset(gradient,0,y);
3303 if (gradient->type != RadialGradient)
3305 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3307 SetPixelInfo(image,q,&pixel);
3308 switch (gradient->spread)
3310 case UndefinedSpread:
3313 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3314 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3316 offset=GetStopColorOffset(gradient,x,y);
3317 if (gradient->type != RadialGradient)
3320 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3321 if (offset < gradient->stops[i].offset)
3323 if ((offset < 0.0) || (i == 0))
3324 composite=gradient->stops[0].color;
3326 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3327 composite=gradient->stops[gradient->number_stops-1].color;
3332 alpha=(offset-gradient->stops[i].offset)/
3333 (gradient->stops[j].offset-gradient->stops[i].offset);
3334 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3335 &gradient->stops[j].color,alpha,&composite);
3341 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3342 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3344 offset=GetStopColorOffset(gradient,x,y);
3345 if (gradient->type != RadialGradient)
3350 if ((ssize_t) fmod(offset,2.0) == 0)
3351 offset=fmod(offset,1.0);
3353 offset=1.0-fmod(offset,1.0);
3354 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3355 if (offset < gradient->stops[i].offset)
3358 composite=gradient->stops[0].color;
3360 if (i == (ssize_t) gradient->number_stops)
3361 composite=gradient->stops[gradient->number_stops-1].color;
3366 alpha=(offset-gradient->stops[i].offset)/
3367 (gradient->stops[j].offset-gradient->stops[i].offset);
3368 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3369 &gradient->stops[j].color,alpha,&composite);
3381 antialias=MagickFalse;
3383 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3384 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3386 offset=GetStopColorOffset(gradient,x,y);
3387 if (gradient->type == LinearGradient)
3389 repeat=fmod(offset,length);
3391 repeat=length-fmod(-repeat,length);
3393 repeat=fmod(offset,length);
3394 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3395 MagickTrue : MagickFalse;
3396 offset=repeat/length;
3400 repeat=fmod(offset,gradient->radius);
3402 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3404 repeat=fmod(offset,gradient->radius);
3405 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3407 offset=repeat/gradient->radius;
3410 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3411 if (offset < gradient->stops[i].offset)
3414 composite=gradient->stops[0].color;
3416 if (i == (ssize_t) gradient->number_stops)
3417 composite=gradient->stops[gradient->number_stops-1].color;
3422 alpha=(offset-gradient->stops[i].offset)/
3423 (gradient->stops[j].offset-gradient->stops[i].offset);
3424 if (antialias != MagickFalse)
3426 if (gradient->type == LinearGradient)
3427 alpha=length-repeat;
3429 alpha=gradient->radius-repeat;
3431 j=(ssize_t) gradient->number_stops-1L;
3433 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3434 &gradient->stops[j].color,alpha,&composite);
3439 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3441 SetPixelPixelInfo(image,&pixel,q);
3442 q+=GetPixelChannels(image);
3444 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3447 image_view=DestroyCacheView(image_view);
3452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3456 % D r a w P a t t e r n P a t h %
3460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3462 % DrawPatternPath() draws a pattern.
3464 % The format of the DrawPatternPath method is:
3466 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3467 % const char *name,Image **pattern)
3469 % A description of each parameter follows:
3471 % o image: the image.
3473 % o draw_info: the draw info.
3475 % o name: the pattern name.
3477 % o image: the image.
3480 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3481 const DrawInfo *draw_info,const char *name,Image **pattern)
3484 property[MaxTextExtent];
3499 assert(image != (Image *) NULL);
3500 assert(image->signature == MagickSignature);
3501 if (image->debug != MagickFalse)
3502 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3503 assert(draw_info != (const DrawInfo *) NULL);
3504 assert(name != (const char *) NULL);
3505 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3506 path=GetImageArtifact(image,property);
3507 if (path == (const char *) NULL)
3508 return(MagickFalse);
3509 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3510 geometry=GetImageArtifact(image,property);
3511 if (geometry == (const char *) NULL)
3512 return(MagickFalse);
3513 if ((*pattern) != (Image *) NULL)
3514 *pattern=DestroyImage(*pattern);
3515 image_info=AcquireImageInfo();
3516 image_info->size=AcquireString(geometry);
3517 *pattern=AcquireImage(image_info);
3518 image_info=DestroyImageInfo(image_info);
3519 (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
3521 (void) SetImageBackgroundColor(*pattern);
3522 if (image->debug != MagickFalse)
3523 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3524 "begin pattern-path %s %s",name,geometry);
3525 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3526 clone_info->fill_pattern=NewImageList();
3527 clone_info->stroke_pattern=NewImageList();
3528 (void) CloneString(&clone_info->primitive,path);
3529 status=DrawImage(*pattern,clone_info);
3530 clone_info=DestroyDrawInfo(clone_info);
3531 if (image->debug != MagickFalse)
3532 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3541 + D r a w P o l y g o n P r i m i t i v e %
3545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3547 % DrawPolygonPrimitive() draws a polygon on the image.
3549 % The format of the DrawPolygonPrimitive method is:
3551 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3552 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3554 % A description of each parameter follows:
3556 % o image: the image.
3558 % o draw_info: the draw info.
3560 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3564 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3569 assert(polygon_info != (PolygonInfo **) NULL);
3570 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3571 if (polygon_info[i] != (PolygonInfo *) NULL)
3572 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3573 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3574 return(polygon_info);
3577 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3578 const PrimitiveInfo *primitive_info)
3581 *restrict path_info;
3592 number_threads=GetOpenMPMaximumThreads();
3593 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3594 sizeof(*polygon_info));
3595 if (polygon_info == (PolygonInfo **) NULL)
3596 return((PolygonInfo **) NULL);
3597 (void) ResetMagickMemory(polygon_info,0,GetOpenMPMaximumThreads()*
3598 sizeof(*polygon_info));
3599 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3600 if (path_info == (PathInfo *) NULL)
3601 return(DestroyPolygonThreadSet(polygon_info));
3602 for (i=0; i < (ssize_t) number_threads; i++)
3604 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3605 if (polygon_info[i] == (PolygonInfo *) NULL)
3606 return(DestroyPolygonThreadSet(polygon_info));
3608 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3609 return(polygon_info);
3612 static MagickRealType GetPixelOpacity(PolygonInfo *polygon_info,
3613 const MagickRealType mid,const MagickBooleanType fill,
3614 const FillRule fill_rule,const double x,const double y,
3615 MagickRealType *stroke_opacity)
3629 register const PointInfo
3640 Compute fill & stroke opacity for this (x,y) point.
3642 *stroke_opacity=0.0;
3643 subpath_opacity=0.0;
3644 p=polygon_info->edges;
3645 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3647 if (y <= (p->bounds.y1-mid-0.5))
3649 if (y > (p->bounds.y2+mid+0.5))
3651 (void) DestroyEdge(polygon_info,(size_t) j);
3654 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3656 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3657 for ( ; i < (ssize_t) p->number_points; i++)
3659 if (y <= (p->points[i-1].y-mid-0.5))
3661 if (y > (p->points[i].y+mid+0.5))
3663 if (p->scanline != y)
3666 p->highwater=(size_t) i;
3669 Compute distance between a point and an edge.
3672 delta.x=(q+1)->x-q->x;
3673 delta.y=(q+1)->y-q->y;
3674 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3679 distance=delta.x*delta.x+delta.y*delta.y;
3683 alpha=delta.x*delta.x+delta.y*delta.y;
3688 distance=delta.x*delta.x+delta.y*delta.y;
3693 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3694 distance=alpha*beta*beta;
3698 Compute stroke & subpath opacity.
3701 if (p->ghostline == MagickFalse)
3704 if ((*stroke_opacity < 1.0) &&
3705 (distance <= ((alpha+0.25)*(alpha+0.25))))
3708 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3709 *stroke_opacity=1.0;
3713 if (distance != 1.0)
3714 beta=sqrt((double) distance);
3716 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3717 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3721 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3723 if (distance <= 0.0)
3725 subpath_opacity=1.0;
3733 if (distance != 1.0)
3734 beta=sqrt(distance);
3737 if (subpath_opacity < (alpha*alpha))
3738 subpath_opacity=alpha*alpha;
3742 Compute fill opacity.
3744 if (fill == MagickFalse)
3746 if (subpath_opacity >= 1.0)
3749 Determine winding number.
3752 p=polygon_info->edges;
3753 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3755 if (y <= p->bounds.y1)
3757 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3759 if (x > p->bounds.x2)
3761 winding_number+=p->direction ? 1 : -1;
3764 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3765 for ( ; i < (ssize_t) p->number_points; i++)
3766 if (y <= p->points[i].y)
3769 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3770 winding_number+=p->direction ? 1 : -1;
3772 if (fill_rule != NonZeroRule)
3774 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3778 if (MagickAbsoluteValue(winding_number) != 0)
3780 return(subpath_opacity);
3783 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3784 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3800 **restrict polygon_info;
3817 Compute bounding box.
3819 assert(image != (Image *) NULL);
3820 assert(image->signature == MagickSignature);
3821 if (image->debug != MagickFalse)
3822 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3823 assert(draw_info != (DrawInfo *) NULL);
3824 assert(draw_info->signature == MagickSignature);
3825 assert(primitive_info != (PrimitiveInfo *) NULL);
3826 if (primitive_info->coordinates == 0)
3828 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3829 if (polygon_info == (PolygonInfo **) NULL)
3830 return(MagickFalse);
3832 DrawBoundingRectangles(image,draw_info,polygon_info[0]);
3833 if (image->debug != MagickFalse)
3834 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3835 fill=(primitive_info->method == FillToBorderMethod) ||
3836 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3837 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3838 bounds=polygon_info[0]->edges[0].bounds;
3839 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3841 p=polygon_info[0]->edges+i;
3842 if (p->bounds.x1 < bounds.x1)
3843 bounds.x1=p->bounds.x1;
3844 if (p->bounds.y1 < bounds.y1)
3845 bounds.y1=p->bounds.y1;
3846 if (p->bounds.x2 > bounds.x2)
3847 bounds.x2=p->bounds.x2;
3848 if (p->bounds.y2 > bounds.y2)
3849 bounds.y2=p->bounds.y2;
3851 bounds.x1-=(mid+1.0);
3852 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3853 image->columns ? (double) image->columns-1.0 : bounds.x1;
3854 bounds.y1-=(mid+1.0);
3855 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3856 image->rows ? (double) image->rows-1.0 : bounds.y1;
3857 bounds.x2+=(mid+1.0);
3858 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3859 image->columns ? (double) image->columns-1.0 : bounds.x2;
3860 bounds.y2+=(mid+1.0);
3861 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3862 image->rows ? (double) image->rows-1.0 : bounds.y2;
3864 exception=(&image->exception);
3865 start=(ssize_t) ceil(bounds.x1-0.5);
3866 stop=(ssize_t) floor(bounds.x2+0.5);
3867 image_view=AcquireCacheView(image);
3868 if (primitive_info->coordinates == 1)
3873 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3874 #pragma omp parallel for schedule(dynamic,4) shared(status)
3876 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
3890 if (status == MagickFalse)
3893 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
3895 if (q == (Quantum *) NULL)
3900 for ( ; x <= stop; x++)
3902 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3903 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3905 (void) GetStrokeColor(draw_info,x,y,&pixel);
3906 SetPixelPacket(image,&pixel,q);
3908 q+=GetPixelChannels(image);
3910 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3911 if (sync == MagickFalse)
3914 image_view=DestroyCacheView(image_view);
3915 polygon_info=DestroyPolygonThreadSet(polygon_info);
3916 if (image->debug != MagickFalse)
3917 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3918 " end draw-polygon");
3922 Draw polygon or line.
3924 if (image->matte == MagickFalse)
3925 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3926 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3927 #pragma omp parallel for schedule(dynamic,4) shared(status)
3929 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
3932 id = GetOpenMPThreadId();
3948 if (status == MagickFalse)
3950 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-
3951 start+1),1,exception);
3952 if (q == (Quantum *) NULL)
3957 for (x=start; x <= stop; x++)
3962 fill_opacity=GetPixelOpacity(polygon_info[id],mid,fill,
3963 draw_info->fill_rule,(double) x,(double) y,&stroke_opacity);
3964 if (draw_info->stroke_antialias == MagickFalse)
3966 fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
3967 stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
3969 (void) GetFillColor(draw_info,x,y,&fill_color);
3970 fill_opacity=fill_opacity*fill_color.alpha;
3971 CompositePixelOver(image,&fill_color,fill_opacity,q,(MagickRealType)
3972 GetPixelAlpha(image,q),q);
3973 (void) GetStrokeColor(draw_info,x,y,&stroke_color);
3974 stroke_opacity=stroke_opacity*stroke_color.alpha;
3975 CompositePixelOver(image,&stroke_color,stroke_opacity,q,(MagickRealType)
3976 GetPixelAlpha(image,q),q);
3977 q+=GetPixelChannels(image);
3979 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3982 image_view=DestroyCacheView(image_view);
3983 polygon_info=DestroyPolygonThreadSet(polygon_info);
3984 if (image->debug != MagickFalse)
3985 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3994 % D r a w P r i m i t i v e %
3998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4000 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4002 % The format of the DrawPrimitive method is:
4004 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4005 % PrimitiveInfo *primitive_info)
4007 % A description of each parameter follows:
4009 % o image: the image.
4011 % o draw_info: the draw info.
4013 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4017 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4043 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4044 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4045 switch (primitive_info->primitive)
4047 case PointPrimitive:
4049 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4050 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4051 methods[primitive_info->method]);
4054 case ColorPrimitive:
4056 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4057 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4058 methods[primitive_info->method]);
4061 case MattePrimitive:
4063 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4064 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4065 methods[primitive_info->method]);
4070 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4071 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4074 case ImagePrimitive:
4076 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4077 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4084 p=primitive_info[0].point;
4087 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4089 point=primitive_info[i].point;
4090 if (coordinates <= 0)
4092 coordinates=(ssize_t) primitive_info[i].coordinates;
4093 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4094 " begin open (%.20g)",(double) coordinates);
4097 point=primitive_info[i].point;
4098 if ((fabs(q.x-point.x) > MagickEpsilon) ||
4099 (fabs(q.y-point.y) > MagickEpsilon))
4100 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4101 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4103 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4104 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4107 if (coordinates > 0)
4109 if ((fabs(p.x-point.x) > MagickEpsilon) ||
4110 (fabs(p.y-point.y) > MagickEpsilon))
4111 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4112 (double) coordinates);
4114 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4115 (double) coordinates);
4119 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4120 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4138 if (image->debug != MagickFalse)
4140 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4141 " begin draw-primitive");
4142 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4143 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4144 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4145 draw_info->affine.tx,draw_info->affine.ty);
4148 exception=(&image->exception);
4149 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4150 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4151 image_view=AcquireCacheView(image);
4152 switch (primitive_info->primitive)
4154 case PointPrimitive:
4162 if ((y < 0) || (y >= (ssize_t) image->rows))
4164 if ((x < 0) || (x >= (ssize_t) image->columns))
4166 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4167 if (q == (Quantum *) NULL)
4169 (void) GetFillColor(draw_info,x,y,&fill_color);
4170 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4171 (MagickRealType) GetPixelAlpha(image,q),q);
4172 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4175 case ColorPrimitive:
4177 switch (primitive_info->method)
4188 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4189 if (q == (Quantum *) NULL)
4191 (void) GetFillColor(draw_info,x,y,&pixel);
4192 SetPixelPacket(image,&pixel,q);
4193 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4205 (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4206 for (y=0; y < (ssize_t) image->rows; y++)
4211 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4213 if (q == (Quantum *) NULL)
4215 for (x=0; x < (ssize_t) image->columns; x++)
4217 GetPixelPacket(image,q,&pixel);
4218 if (IsFuzzyEquivalencePixelPacket(image,&pixel,&target) == MagickFalse)
4220 q+=GetPixelChannels(image);
4223 (void) GetFillColor(draw_info,x,y,&pixel);
4224 SetPixelPacket(image,&pixel,q);
4225 q+=GetPixelChannels(image);
4227 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4228 if (sync == MagickFalse)
4233 case FloodfillMethod:
4234 case FillToBorderMethod:
4239 (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4240 if (primitive_info->method == FillToBorderMethod)
4242 target.red=(MagickRealType) draw_info->border_color.red;
4243 target.green=(MagickRealType) draw_info->border_color.green;
4244 target.blue=(MagickRealType) draw_info->border_color.blue;
4246 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4247 primitive_info->method == FloodfillMethod ? MagickFalse :
4248 MagickTrue,exception);
4259 for (y=0; y < (ssize_t) image->rows; y++)
4267 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4269 if (q == (Quantum *) NULL)
4271 for (x=0; x < (ssize_t) image->columns; x++)
4273 (void) GetFillColor(draw_info,x,y,&pixel);
4274 SetPixelPacket(image,&pixel,q);
4275 q+=GetPixelChannels(image);
4277 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4278 if (sync == MagickFalse)
4286 case MattePrimitive:
4288 if (image->matte == MagickFalse)
4289 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4290 switch (primitive_info->method)
4301 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4302 if (q == (Quantum *) NULL)
4304 (void) GetFillColor(draw_info,x,y,&pixel);
4305 SetPixelAlpha(image,pixel.alpha,q);
4306 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4318 (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4319 for (y=0; y < (ssize_t) image->rows; y++)
4327 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4329 if (q == (Quantum *) NULL)
4331 for (x=0; x < (ssize_t) image->columns; x++)
4333 GetPixelPacket(image,q,&pixel);
4334 if (IsFuzzyEquivalencePixelPacket(image,&pixel,&target) == MagickFalse)
4336 q+=GetPixelChannels(image);
4339 (void) GetFillColor(draw_info,x,y,&pixel);
4340 SetPixelAlpha(image,pixel.alpha,q);
4341 q+=GetPixelChannels(image);
4343 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4344 if (sync == MagickFalse)
4349 case FloodfillMethod:
4350 case FillToBorderMethod:
4358 (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4359 if (primitive_info->method == FillToBorderMethod)
4361 target.red=(MagickRealType) draw_info->border_color.red;
4362 target.green=(MagickRealType) draw_info->border_color.green;
4363 target.blue=(MagickRealType) draw_info->border_color.blue;
4365 channel_mask=SetPixelChannelMask(image,AlphaChannel);
4366 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4367 primitive_info->method == FloodfillMethod ? MagickFalse :
4368 MagickTrue,exception);
4369 (void) SetPixelChannelMask(image,channel_mask);
4380 for (y=0; y < (ssize_t) image->rows; y++)
4388 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4390 if (q == (Quantum *) NULL)
4392 for (x=0; x < (ssize_t) image->columns; x++)
4394 (void) GetFillColor(draw_info,x,y,&pixel);
4395 SetPixelAlpha(image,pixel.alpha,q);
4396 q+=GetPixelChannels(image);
4398 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4399 if (sync == MagickFalse)
4410 geometry[MaxTextExtent];
4415 if (primitive_info->text == (char *) NULL)
4417 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4418 (void) CloneString(&clone_info->text,primitive_info->text);
4419 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4420 primitive_info->point.x,primitive_info->point.y);
4421 (void) CloneString(&clone_info->geometry,geometry);
4422 status=AnnotateImage(image,clone_info);
4423 clone_info=DestroyDrawInfo(clone_info);
4426 case ImagePrimitive:
4432 composite_geometry[MaxTextExtent];
4447 if (primitive_info->text == (char *) NULL)
4449 clone_info=AcquireImageInfo();
4450 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4451 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4455 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4457 composite_image=ReadImage(clone_info,&image->exception);
4459 clone_info=DestroyImageInfo(clone_info);
4460 if (composite_image == (Image *) NULL)
4462 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4463 NULL,(void *) NULL);
4464 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4465 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4466 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4467 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4470 geometry[MaxTextExtent];
4475 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4476 primitive_info[1].point.x,primitive_info[1].point.y);
4477 composite_image->filter=image->filter;
4478 (void) TransformImage(&composite_image,(char *) NULL,geometry);
4480 if (composite_image->matte == MagickFalse)
4481 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4483 if (draw_info->alpha != OpaqueAlpha)
4484 (void) SetImageOpacity(composite_image,draw_info->alpha);
4485 SetGeometry(image,&geometry);
4486 image->gravity=draw_info->gravity;
4489 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4490 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4491 composite_image->rows,(double) geometry.x,(double) geometry.y);
4492 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
4494 affine=draw_info->affine;
4495 affine.tx=(double) geometry.x;
4496 affine.ty=(double) geometry.y;
4497 composite_image->interpolate=image->interpolate;
4498 if (draw_info->compose == OverCompositeOp)
4499 (void) DrawAffineImage(image,composite_image,&affine);
4501 (void) CompositeImage(image,draw_info->compose,composite_image,
4502 geometry.x,geometry.y);
4503 composite_image=DestroyImage(composite_image);
4515 if (IsEventLogging() != MagickFalse)
4516 LogPrimitiveInfo(primitive_info);
4517 scale=ExpandAffine(&draw_info->affine);
4518 if ((draw_info->dash_pattern != (double *) NULL) &&
4519 (draw_info->dash_pattern[0] != 0.0) &&
4520 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
4521 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4526 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4527 clone_info->stroke_width=0.0;
4528 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4529 status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4530 clone_info=DestroyDrawInfo(clone_info);
4531 (void) DrawDashPolygon(draw_info,primitive_info,image);
4534 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4536 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4542 Draw strokes while respecting line cap/join attributes.
4544 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4546 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4547 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4548 MagickTrue : MagickFalse;
4549 i=(ssize_t) primitive_info[0].coordinates;
4550 if ((((draw_info->linecap == RoundCap) ||
4551 (closed_path != MagickFalse)) &&
4552 (draw_info->linejoin == RoundJoin)) ||
4553 (primitive_info[i].primitive != UndefinedPrimitive))
4555 (void) DrawPolygonPrimitive(image,draw_info,primitive_info);
4558 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4559 clone_info->stroke_width=0.0;
4560 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4561 status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4562 clone_info=DestroyDrawInfo(clone_info);
4563 status|=DrawStrokePolygon(image,draw_info,primitive_info);
4566 status=DrawPolygonPrimitive(image,draw_info,primitive_info);
4570 image_view=DestroyCacheView(image_view);
4571 if (image->debug != MagickFalse)
4572 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4573 return(status != 0 ? MagickTrue : MagickFalse);
4577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4581 + D r a w S t r o k e P o l y g o n %
4585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4587 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4588 % the image while respecting the line cap and join attributes.
4590 % The format of the DrawStrokePolygon method is:
4592 % MagickBooleanType DrawStrokePolygon(Image *image,
4593 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4595 % A description of each parameter follows:
4597 % o image: the image.
4599 % o draw_info: the draw info.
4601 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4606 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4607 const PrimitiveInfo *primitive_info)
4615 for (i=0; i < 4; i++)
4616 linecap[i]=(*primitive_info);
4617 linecap[0].coordinates=4;
4618 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4619 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4620 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4621 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4622 linecap[4].primitive=UndefinedPrimitive;
4623 (void) DrawPolygonPrimitive(image,draw_info,linecap);
4626 static MagickBooleanType DrawStrokePolygon(Image *image,
4627 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4639 register const PrimitiveInfo
4644 Draw stroked polygon.
4646 if (image->debug != MagickFalse)
4647 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4648 " begin draw-stroke-polygon");
4649 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4650 clone_info->fill=draw_info->stroke;
4651 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4652 clone_info->stroke_width=0.0;
4653 clone_info->fill_rule=NonZeroRule;
4655 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4657 stroke_polygon=TraceStrokePolygon(draw_info,p);
4658 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
4659 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4660 q=p+p->coordinates-1;
4661 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4662 MagickTrue : MagickFalse;
4663 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4665 DrawRoundLinecap(image,draw_info,p);
4666 DrawRoundLinecap(image,draw_info,q);
4669 clone_info=DestroyDrawInfo(clone_info);
4670 if (image->debug != MagickFalse)
4671 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4672 " end draw-stroke-polygon");
4677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4681 % G e t A f f i n e M a t r i x %
4685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4687 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4690 % The format of the GetAffineMatrix method is:
4692 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4694 % A description of each parameter follows:
4696 % o affine_matrix: the affine matrix.
4699 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4701 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4702 assert(affine_matrix != (AffineMatrix *) NULL);
4703 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4704 affine_matrix->sx=1.0;
4705 affine_matrix->sy=1.0;
4709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4713 + G e t D r a w I n f o %
4717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4719 % GetDrawInfo() initializes draw_info to default values.
4721 % The format of the GetDrawInfo method is:
4723 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4725 % A description of each parameter follows:
4727 % o image_info: the image info..
4729 % o draw_info: the draw info.
4732 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4744 Initialize draw attributes.
4746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4747 assert(draw_info != (DrawInfo *) NULL);
4748 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4749 clone_info=CloneImageInfo(image_info);
4750 GetAffineMatrix(&draw_info->affine);
4751 exception=AcquireExceptionInfo();
4752 (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
4753 (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
4754 draw_info->stroke_antialias=clone_info->antialias;
4755 draw_info->stroke_width=1.0;
4756 draw_info->alpha=OpaqueAlpha;
4757 draw_info->fill_rule=EvenOddRule;
4758 draw_info->linecap=ButtCap;
4759 draw_info->linejoin=MiterJoin;
4760 draw_info->miterlimit=10;
4761 draw_info->decorate=NoDecoration;
4762 if (clone_info->font != (char *) NULL)
4763 draw_info->font=AcquireString(clone_info->font);
4764 if (clone_info->density != (char *) NULL)
4765 draw_info->density=AcquireString(clone_info->density);
4766 draw_info->text_antialias=clone_info->antialias;
4767 draw_info->pointsize=12.0;
4768 if (clone_info->pointsize != 0.0)
4769 draw_info->pointsize=clone_info->pointsize;
4770 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4771 draw_info->border_color=clone_info->border_color;
4772 draw_info->compose=OverCompositeOp;
4773 if (clone_info->server_name != (char *) NULL)
4774 draw_info->server_name=AcquireString(clone_info->server_name);
4775 draw_info->render=MagickTrue;
4776 draw_info->debug=IsEventLogging();
4777 option=GetImageOption(clone_info,"encoding");
4778 if (option != (const char *) NULL)
4779 (void) CloneString(&draw_info->encoding,option);
4780 option=GetImageOption(clone_info,"kerning");
4781 if (option != (const char *) NULL)
4782 draw_info->kerning=InterpretLocaleValue(option,(char **) NULL);
4783 option=GetImageOption(clone_info,"interline-spacing");
4784 if (option != (const char *) NULL)
4785 draw_info->interline_spacing=InterpretLocaleValue(option,(char **) NULL);
4786 draw_info->direction=UndefinedDirection;
4787 option=GetImageOption(clone_info,"interword-spacing");
4788 if (option != (const char *) NULL)
4789 draw_info->interword_spacing=InterpretLocaleValue(option,(char **) NULL);
4790 option=GetImageOption(clone_info,"direction");
4791 if (option != (const char *) NULL)
4792 draw_info->direction=(DirectionType) ParseCommandOption(
4793 MagickDirectionOptions,MagickFalse,option);
4794 option=GetImageOption(clone_info,"fill");
4795 if (option != (const char *) NULL)
4796 (void) QueryColorDatabase(option,&draw_info->fill,exception);
4797 option=GetImageOption(clone_info,"stroke");
4798 if (option != (const char *) NULL)
4799 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
4800 option=GetImageOption(clone_info,"strokewidth");
4801 if (option != (const char *) NULL)
4802 draw_info->stroke_width=InterpretLocaleValue(option,(char **) NULL);
4803 option=GetImageOption(clone_info,"undercolor");
4804 if (option != (const char *) NULL)
4805 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
4806 option=GetImageOption(clone_info,"gravity");
4807 if (option != (const char *) NULL)
4808 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4809 MagickFalse,option);
4810 exception=DestroyExceptionInfo(exception);
4811 draw_info->signature=MagickSignature;
4812 clone_info=DestroyImageInfo(clone_info);
4816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4820 + P e r m u t a t e %
4824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4826 % Permutate() returns the permuation of the (n,k).
4828 % The format of the Permutate method is:
4830 % void Permutate(ssize_t n,ssize_t k)
4832 % A description of each parameter follows:
4840 static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
4849 for (i=k+1; i <= n; i++)
4851 for (i=1; i <= (n-k); i++)
4857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4861 + T r a c e P r i m i t i v e %
4865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4867 % TracePrimitive is a collection of methods for generating graphic
4868 % primitives such as arcs, ellipses, paths, etc.
4872 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4873 const PointInfo end,const PointInfo degrees)
4879 center.x=0.5*(end.x+start.x);
4880 center.y=0.5*(end.y+start.y);
4881 radii.x=fabs(center.x-start.x);
4882 radii.y=fabs(center.y-start.y);
4883 TraceEllipse(primitive_info,center,radii,degrees);
4886 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4887 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4888 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4903 register MagickRealType
4907 register PrimitiveInfo
4916 if ((start.x == end.x) && (start.y == end.y))
4918 TracePoint(primitive_info,end);
4921 radii.x=fabs(arc.x);
4922 radii.y=fabs(arc.y);
4923 if ((radii.x == 0.0) || (radii.y == 0.0))
4925 TraceLine(primitive_info,start,end);
4928 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4929 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4930 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4931 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4932 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4934 if (delta < MagickEpsilon)
4936 TraceLine(primitive_info,start,end);
4941 radii.x*=sqrt((double) delta);
4942 radii.y*=sqrt((double) delta);
4944 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4945 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4946 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4947 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4948 alpha=points[1].x-points[0].x;
4949 beta=points[1].y-points[0].y;
4950 factor=1.0/(alpha*alpha+beta*beta)-0.25;
4955 factor=sqrt((double) factor);
4956 if (sweep == large_arc)
4959 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4960 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4961 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4962 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4963 if ((theta < 0.0) && (sweep != MagickFalse))
4964 theta+=(MagickRealType) (2.0*MagickPI);
4966 if ((theta > 0.0) && (sweep == MagickFalse))
4967 theta-=(MagickRealType) (2.0*MagickPI);
4968 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
4971 for (i=0; i < (ssize_t) arc_segments; i++)
4973 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4974 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4975 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4976 sin(fmod((double) beta,DegreesToRadians(360.0)));
4977 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
4978 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
4979 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4980 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
4981 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
4982 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4983 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
4984 theta/arc_segments),DegreesToRadians(360.0))));
4985 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
4986 theta/arc_segments),DegreesToRadians(360.0))));
4987 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
4988 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4989 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
4990 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4991 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
4992 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
4993 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
4995 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
4997 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
4999 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5001 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5003 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5005 if (i == (ssize_t) (arc_segments-1))
5010 primitive_info->coordinates=(size_t) (p-primitive_info);
5011 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5013 p->primitive=primitive_info->primitive;
5018 static void TraceBezier(PrimitiveInfo *primitive_info,
5019 const size_t number_coordinates)
5031 register PrimitiveInfo
5043 Allocate coeficients.
5045 quantum=number_coordinates;
5046 for (i=0; i < (ssize_t) number_coordinates; i++)
5048 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5050 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5051 if (alpha > (MagickRealType) quantum)
5052 quantum=(size_t) alpha;
5053 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5054 if (alpha > (MagickRealType) quantum)
5055 quantum=(size_t) alpha;
5058 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5059 (double) BezierQuantum);
5060 control_points=quantum*number_coordinates;
5061 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5062 number_coordinates,sizeof(*coefficients));
5063 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5065 if ((coefficients == (MagickRealType *) NULL) ||
5066 (points == (PointInfo *) NULL))
5067 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5069 Compute bezier points.
5071 end=primitive_info[number_coordinates-1].point;
5072 for (i=0; i < (ssize_t) number_coordinates; i++)
5073 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5075 for (i=0; i < (ssize_t) control_points; i++)
5080 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5081 for (j=0; j < (ssize_t) number_coordinates; j++)
5083 point.x+=alpha*coefficients[j]*p->point.x;
5084 point.y+=alpha*coefficients[j]*p->point.y;
5085 alpha*=weight/(1.0-weight);
5089 weight+=1.0/control_points;
5092 Bezier curves are just short segmented polys.
5095 for (i=0; i < (ssize_t) control_points; i++)
5097 TracePoint(p,points[i]);
5102 primitive_info->coordinates=(size_t) (p-primitive_info);
5103 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5105 p->primitive=primitive_info->primitive;
5108 points=(PointInfo *) RelinquishMagickMemory(points);
5109 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5112 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5113 const PointInfo end)
5124 alpha=end.x-start.x;
5126 radius=hypot((double) alpha,(double) beta);
5127 offset.x=(double) radius;
5128 offset.y=(double) radius;
5131 TraceEllipse(primitive_info,start,offset,degrees);
5134 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5135 const PointInfo stop,const PointInfo degrees)
5146 register PrimitiveInfo
5153 Ellipses are just short segmented polys.
5155 if ((stop.x == 0.0) && (stop.y == 0.0))
5157 TracePoint(primitive_info,start);
5160 delta=2.0/MagickMax(stop.x,stop.y);
5161 step=(MagickRealType) (MagickPI/8.0);
5162 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5163 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5164 angle.x=DegreesToRadians(degrees.x);
5166 while (y < degrees.x)
5168 angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5169 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5171 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5172 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5173 TracePoint(p,point);
5176 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5177 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5178 TracePoint(p,point);
5180 primitive_info->coordinates=(size_t) (p-primitive_info);
5181 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5183 p->primitive=primitive_info->primitive;
5188 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5189 const PointInfo end)
5191 TracePoint(primitive_info,start);
5192 if ((fabs(start.x-end.x) <= MagickEpsilon) &&
5193 (fabs(start.y-end.y) <= MagickEpsilon))
5195 primitive_info->primitive=PointPrimitive;
5196 primitive_info->coordinates=1;
5199 TracePoint(primitive_info+1,end);
5200 (primitive_info+1)->primitive=primitive_info->primitive;
5201 primitive_info->coordinates=2;
5204 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5207 token[MaxTextExtent];
5229 register PrimitiveInfo
5244 number_coordinates=0;
5246 primitive_type=primitive_info->primitive;
5248 for (p=path; *p != '\0'; )
5250 while (isspace((int) ((unsigned char) *p)) != 0)
5254 last_attribute=attribute;
5255 attribute=(int) (*p++);
5276 GetMagickToken(p,&p,token);
5278 GetMagickToken(p,&p,token);
5279 arc.x=InterpretLocaleValue(token,(char **) NULL);
5280 GetMagickToken(p,&p,token);
5282 GetMagickToken(p,&p,token);
5283 arc.y=InterpretLocaleValue(token,(char **) NULL);
5284 GetMagickToken(p,&p,token);
5286 GetMagickToken(p,&p,token);
5287 angle=InterpretLocaleValue(token,(char **) NULL);
5288 GetMagickToken(p,&p,token);
5290 GetMagickToken(p,&p,token);
5291 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5292 GetMagickToken(p,&p,token);
5294 GetMagickToken(p,&p,token);
5295 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5296 GetMagickToken(p,&p,token);
5298 GetMagickToken(p,&p,token);
5299 x=InterpretLocaleValue(token,(char **) NULL);
5300 GetMagickToken(p,&p,token);
5302 GetMagickToken(p,&p,token);
5303 y=InterpretLocaleValue(token,(char **) NULL);
5304 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5305 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5306 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5309 } while (IsPoint(p) != MagickFalse);
5316 Compute bezier points.
5321 for (i=1; i < 4; i++)
5323 GetMagickToken(p,&p,token);
5325 GetMagickToken(p,&p,token);
5326 x=InterpretLocaleValue(token,(char **) NULL);
5327 GetMagickToken(p,&p,token);
5329 GetMagickToken(p,&p,token);
5330 y=InterpretLocaleValue(token,(char **) NULL);
5331 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5332 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5335 for (i=0; i < 4; i++)
5336 (q+i)->point=points[i];
5340 } while (IsPoint(p) != MagickFalse);
5348 GetMagickToken(p,&p,token);
5350 GetMagickToken(p,&p,token);
5351 x=InterpretLocaleValue(token,(char **) NULL);
5352 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5353 TracePoint(q,point);
5355 } while (IsPoint(p) != MagickFalse);
5363 GetMagickToken(p,&p,token);
5365 GetMagickToken(p,&p,token);
5366 x=InterpretLocaleValue(token,(char **) NULL);
5367 GetMagickToken(p,&p,token);
5369 GetMagickToken(p,&p,token);
5370 y=InterpretLocaleValue(token,(char **) NULL);
5371 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5372 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5373 TracePoint(q,point);
5375 } while (IsPoint(p) != MagickFalse);
5381 if (q != primitive_info)
5383 primitive_info->coordinates=(size_t) (q-primitive_info);
5384 number_coordinates+=primitive_info->coordinates;
5390 GetMagickToken(p,&p,token);
5392 GetMagickToken(p,&p,token);
5393 x=InterpretLocaleValue(token,(char **) NULL);
5394 GetMagickToken(p,&p,token);
5396 GetMagickToken(p,&p,token);
5397 y=InterpretLocaleValue(token,(char **) NULL);
5398 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5399 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5403 TracePoint(q,point);
5405 if ((i != 0) && (attribute == (int) 'M'))
5407 TracePoint(q,point);
5410 } while (IsPoint(p) != MagickFalse);
5417 Compute bezier points.
5422 for (i=1; i < 3; i++)
5424 GetMagickToken(p,&p,token);
5426 GetMagickToken(p,&p,token);
5427 x=InterpretLocaleValue(token,(char **) NULL);
5428 GetMagickToken(p,&p,token);
5430 GetMagickToken(p,&p,token);
5431 y=InterpretLocaleValue(token,(char **) NULL);
5434 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5435 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5438 for (i=0; i < 3; i++)
5439 (q+i)->point=points[i];
5443 } while (IsPoint(p) != MagickFalse);
5450 Compute bezier points.
5454 points[0]=points[3];
5455 points[1].x=2.0*points[3].x-points[2].x;
5456 points[1].y=2.0*points[3].y-points[2].y;
5457 for (i=2; i < 4; i++)
5459 GetMagickToken(p,&p,token);
5461 GetMagickToken(p,&p,token);
5462 x=InterpretLocaleValue(token,(char **) NULL);
5463 GetMagickToken(p,&p,token);
5465 GetMagickToken(p,&p,token);
5466 y=InterpretLocaleValue(token,(char **) NULL);
5469 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5470 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5473 if (strchr("CcSs",last_attribute) == (char *) NULL)
5475 points[0]=points[2];
5476 points[1]=points[3];
5478 for (i=0; i < 4; i++)
5479 (q+i)->point=points[i];
5483 } while (IsPoint(p) != MagickFalse);
5490 Compute bezier points.
5494 points[0]=points[2];
5495 points[1].x=2.0*points[2].x-points[1].x;
5496 points[1].y=2.0*points[2].y-points[1].y;
5497 for (i=2; i < 3; i++)
5499 GetMagickToken(p,&p,token);
5501 GetMagickToken(p,&p,token);
5502 x=InterpretLocaleValue(token,(char **) NULL);
5503 GetMagickToken(p,&p,token);
5505 GetMagickToken(p,&p,token);
5506 y=InterpretLocaleValue(token,(char **) NULL);
5507 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5508 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5511 if (strchr("QqTt",last_attribute) == (char *) NULL)
5513 points[0]=points[2];
5514 points[1]=points[3];
5516 for (i=0; i < 3; i++)
5517 (q+i)->point=points[i];
5521 } while (IsPoint(p) != MagickFalse);
5529 GetMagickToken(p,&p,token);
5531 GetMagickToken(p,&p,token);
5532 y=InterpretLocaleValue(token,(char **) NULL);
5533 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5534 TracePoint(q,point);
5536 } while (IsPoint(p) != MagickFalse);
5543 TracePoint(q,point);
5545 primitive_info->coordinates=(size_t) (q-primitive_info);
5546 number_coordinates+=primitive_info->coordinates;
5553 if (isalpha((int) ((unsigned char) attribute)) != 0)
5554 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5560 primitive_info->coordinates=(size_t) (q-primitive_info);
5561 number_coordinates+=primitive_info->coordinates;
5562 for (i=0; i < (ssize_t) number_coordinates; i++)
5565 q->primitive=primitive_type;
5567 q->method=FillToBorderMethod;
5570 return(number_coordinates);
5573 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5574 const PointInfo end)
5579 register PrimitiveInfo
5586 TracePoint(p,start);
5590 TracePoint(p,point);
5596 TracePoint(p,point);
5598 TracePoint(p,start);
5600 primitive_info->coordinates=(size_t) (p-primitive_info);
5601 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5603 p->primitive=primitive_info->primitive;
5608 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5609 const PointInfo start,const PointInfo end,PointInfo arc)
5616 register PrimitiveInfo
5623 offset.x=fabs(end.x-start.x);
5624 offset.y=fabs(end.y-start.y);
5625 if (arc.x > (0.5*offset.x))
5627 if (arc.y > (0.5*offset.y))
5629 point.x=start.x+offset.x-arc.x;
5630 point.y=start.y+arc.y;
5633 TraceEllipse(p,point,arc,degrees);
5635 point.x=start.x+offset.x-arc.x;
5636 point.y=start.y+offset.y-arc.y;
5639 TraceEllipse(p,point,arc,degrees);
5641 point.x=start.x+arc.x;
5642 point.y=start.y+offset.y-arc.y;
5645 TraceEllipse(p,point,arc,degrees);
5647 point.x=start.x+arc.x;
5648 point.y=start.y+arc.y;
5651 TraceEllipse(p,point,arc,degrees);
5653 TracePoint(p,primitive_info->point);
5655 primitive_info->coordinates=(size_t) (p-primitive_info);
5656 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5658 p->primitive=primitive_info->primitive;
5663 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5664 const size_t number_vertices,const MagickRealType offset)
5669 register MagickRealType
5681 for (i=1; i < (ssize_t) number_vertices; i++)
5683 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5684 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5685 if ((fabs((double) dx) >= MagickEpsilon) ||
5686 (fabs((double) dy) >= MagickEpsilon))
5689 if (i == (ssize_t) number_vertices)
5690 i=(ssize_t) number_vertices-1L;
5691 distance=hypot((double) dx,(double) dy);
5692 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5693 dx*(distance+offset)/distance);
5694 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5695 dy*(distance+offset)/distance);
5696 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5698 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5699 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5700 if ((fabs((double) dx) >= MagickEpsilon) ||
5701 (fabs((double) dy) >= MagickEpsilon))
5704 distance=hypot((double) dx,(double) dy);
5705 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5706 dx*(distance+offset)/distance);
5707 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5708 dy*(distance+offset)/distance);
5711 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5712 const PrimitiveInfo *primitive_info)
5714 typedef struct _LineSegment
5766 number_vertices=primitive_info->coordinates;
5767 max_strokes=2*number_vertices+6*BezierQuantum+360;
5768 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5770 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5772 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5773 number_vertices+2UL,sizeof(*polygon_primitive));
5774 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5775 (polygon_primitive == (PrimitiveInfo *) NULL))
5776 return((PrimitiveInfo *) NULL);
5777 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5778 number_vertices*sizeof(*polygon_primitive));
5780 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5781 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5782 MagickTrue : MagickFalse;
5783 if ((draw_info->linejoin == RoundJoin) ||
5784 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5786 polygon_primitive[number_vertices]=primitive_info[1];
5789 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5791 Compute the slope for the first line segment, p.
5795 for (n=1; n < (ssize_t) number_vertices; n++)
5797 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5798 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5799 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5802 if (n == (ssize_t) number_vertices)
5803 n=(ssize_t) number_vertices-1L;
5805 inverse_slope.p=0.0;
5806 if (fabs(dx.p) <= MagickEpsilon)
5809 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5811 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5814 if (fabs(dy.p) <= MagickEpsilon)
5817 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5819 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5824 inverse_slope.p=(-1.0/slope.p);
5826 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5827 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5829 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5830 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5831 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5832 offset.y=(double) (offset.x*inverse_slope.p);
5833 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5835 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5836 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5837 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5838 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5839 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5840 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5841 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5842 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5846 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5847 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5848 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5849 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5850 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5851 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5852 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5853 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5856 Create strokes for the line join attribute: bevel, miter, round.
5860 path_q[p++]=box_q[0];
5861 path_p[q++]=box_p[0];
5862 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5865 Compute the slope for this line segment, q.
5867 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5868 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5869 dot_product=dx.q*dx.q+dy.q*dy.q;
5870 if (dot_product < 0.25)
5873 inverse_slope.q=0.0;
5874 if (fabs(dx.q) < MagickEpsilon)
5877 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5879 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5882 if (fabs(dy.q) <= MagickEpsilon)
5885 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5887 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5892 inverse_slope.q=(-1.0/slope.q);
5894 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5895 offset.y=(double) (offset.x*inverse_slope.q);
5896 dot_product=dy.q*offset.x-dx.q*offset.y;
5897 if (dot_product > 0.0)
5899 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5900 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5901 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5902 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5903 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5904 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5905 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5906 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5910 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5911 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5912 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5913 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5914 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5915 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5916 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5917 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5919 if (fabs((double) (slope.p-slope.q)) <= MagickEpsilon)
5926 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5927 box_p[3].y)/(slope.p-slope.q));
5928 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5929 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5930 box_q[3].y)/(slope.p-slope.q));
5931 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5933 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5935 max_strokes+=6*BezierQuantum+360;
5936 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5938 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5940 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5942 polygon_primitive=(PrimitiveInfo *)
5943 RelinquishMagickMemory(polygon_primitive);
5944 return((PrimitiveInfo *) NULL);
5947 dot_product=dx.q*dy.p-dx.p*dy.q;
5948 if (dot_product <= 0.0)
5949 switch (draw_info->linejoin)
5953 path_q[q++]=box_q[1];
5954 path_q[q++]=box_q[2];
5955 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5956 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5957 if (dot_product <= miterlimit)
5958 path_p[p++]=box_p[4];
5961 path_p[p++]=box_p[1];
5962 path_p[p++]=box_p[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)
5972 path_q[q++]=box_q[4];
5973 path_p[p++]=box_p[4];
5977 path_q[q++]=box_q[1];
5978 path_q[q++]=box_q[2];
5979 path_p[p++]=box_p[1];
5980 path_p[p++]=box_p[2];
5986 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5987 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5988 if (dot_product <= miterlimit)
5989 path_p[p++]=box_p[4];
5992 path_p[p++]=box_p[1];
5993 path_p[p++]=box_p[2];
5995 center=polygon_primitive[n].point;
5996 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
5997 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
5998 if (theta.q < theta.p)
5999 theta.q+=(MagickRealType) (2.0*MagickPI);
6000 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6001 (2.0*sqrt((double) (1.0/mid)))));
6002 path_q[q].x=box_q[1].x;
6003 path_q[q].y=box_q[1].y;
6005 for (j=1; j < (ssize_t) arc_segments; j++)
6007 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6008 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6009 (theta.p+delta_theta),DegreesToRadians(360.0))));
6010 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6011 (theta.p+delta_theta),DegreesToRadians(360.0))));
6014 path_q[q++]=box_q[2];
6021 switch (draw_info->linejoin)
6025 path_p[p++]=box_p[1];
6026 path_p[p++]=box_p[2];
6027 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6028 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6029 if (dot_product <= miterlimit)
6030 path_q[q++]=box_q[4];
6033 path_q[q++]=box_q[1];
6034 path_q[q++]=box_q[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)
6044 path_q[q++]=box_q[4];
6045 path_p[p++]=box_p[4];
6049 path_q[q++]=box_q[1];
6050 path_q[q++]=box_q[2];
6051 path_p[p++]=box_p[1];
6052 path_p[p++]=box_p[2];
6058 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6059 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6060 if (dot_product <= miterlimit)
6061 path_q[q++]=box_q[4];
6064 path_q[q++]=box_q[1];
6065 path_q[q++]=box_q[2];
6067 center=polygon_primitive[n].point;
6068 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6069 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6070 if (theta.p < theta.q)
6071 theta.p+=(MagickRealType) (2.0*MagickPI);
6072 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6073 (2.0*sqrt((double) (1.0/mid)))));
6074 path_p[p++]=box_p[1];
6075 for (j=1; j < (ssize_t) arc_segments; j++)
6077 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6078 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6079 (theta.p+delta_theta),DegreesToRadians(360.0))));
6080 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6081 (theta.p+delta_theta),DegreesToRadians(360.0))));
6084 path_p[p++]=box_p[2];
6091 inverse_slope.p=inverse_slope.q;
6100 path_p[p++]=box_p[1];
6101 path_q[q++]=box_q[1];
6103 Trace stroked polygon.
6105 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6106 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6107 if (stroke_polygon != (PrimitiveInfo *) NULL)
6109 for (i=0; i < (ssize_t) p; i++)
6111 stroke_polygon[i]=polygon_primitive[0];
6112 stroke_polygon[i].point=path_p[i];
6114 if (closed_path != MagickFalse)
6116 stroke_polygon[i]=polygon_primitive[0];
6117 stroke_polygon[i].point=stroke_polygon[0].point;
6120 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6122 stroke_polygon[i]=polygon_primitive[0];
6123 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6125 if (closed_path != MagickFalse)
6127 stroke_polygon[i]=polygon_primitive[0];
6128 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6131 stroke_polygon[i]=polygon_primitive[0];
6132 stroke_polygon[i].point=stroke_polygon[0].point;
6134 stroke_polygon[i].primitive=UndefinedPrimitive;
6135 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6137 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6138 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6139 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6140 return(stroke_polygon);