2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/channel.h"
55 #include "MagickCore/color.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/draw.h"
61 #include "MagickCore/draw-private.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/geometry.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/log.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/paint.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/pixel-private.h"
76 #include "MagickCore/property.h"
77 #include "MagickCore/resample.h"
78 #include "MagickCore/resample-private.h"
79 #include "MagickCore/resource_.h"
80 #include "MagickCore/string_.h"
81 #include "MagickCore/string-private.h"
82 #include "MagickCore/thread-private.h"
83 #include "MagickCore/token.h"
84 #include "MagickCore/transform.h"
85 #include "MagickCore/utility.h"
90 #define BezierQuantum 200
95 typedef struct _EdgeInfo
119 typedef struct _ElementInfo
129 typedef struct _PolygonInfo
147 typedef struct _PathInfo
157 Forward declarations.
159 static MagickBooleanType
160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
167 TracePath(PrimitiveInfo *,const char *);
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
172 const double,const MagickBooleanType,const MagickBooleanType),
173 TraceBezier(PrimitiveInfo *,const size_t),
174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
176 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
177 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
180 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % A c q u i r e D r a w I n f o %
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
195 % The format of the AcquireDrawInfo method is:
197 % DrawInfo *AcquireDrawInfo(void)
200 MagickExport DrawInfo *AcquireDrawInfo(void)
205 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
206 if (draw_info == (DrawInfo *) NULL)
207 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
208 GetDrawInfo((ImageInfo *) NULL,draw_info);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % C l o n e D r a w I n f o %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
224 % is specified, a new DrawInfo structure is created initialized to default
227 % The format of the CloneDrawInfo method is:
229 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
230 % const DrawInfo *draw_info)
232 % A description of each parameter follows:
234 % o image_info: the image info.
236 % o draw_info: the draw info.
239 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
240 const DrawInfo *draw_info)
248 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
249 if (clone_info == (DrawInfo *) NULL)
250 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
251 GetDrawInfo(image_info,clone_info);
252 if (draw_info == (DrawInfo *) NULL)
254 exception=AcquireExceptionInfo();
255 (void) CloneString(&clone_info->primitive,draw_info->primitive);
256 (void) CloneString(&clone_info->geometry,draw_info->geometry);
257 clone_info->viewbox=draw_info->viewbox;
258 clone_info->affine=draw_info->affine;
259 clone_info->gravity=draw_info->gravity;
260 clone_info->fill=draw_info->fill;
261 clone_info->stroke=draw_info->stroke;
262 clone_info->stroke_width=draw_info->stroke_width;
263 if (draw_info->fill_pattern != (Image *) NULL)
264 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
266 if (draw_info->stroke_pattern != (Image *) NULL)
267 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
268 MagickTrue,exception);
269 clone_info->stroke_antialias=draw_info->stroke_antialias;
270 clone_info->text_antialias=draw_info->text_antialias;
271 clone_info->fill_rule=draw_info->fill_rule;
272 clone_info->linecap=draw_info->linecap;
273 clone_info->linejoin=draw_info->linejoin;
274 clone_info->miterlimit=draw_info->miterlimit;
275 clone_info->dash_offset=draw_info->dash_offset;
276 clone_info->decorate=draw_info->decorate;
277 clone_info->compose=draw_info->compose;
278 (void) CloneString(&clone_info->text,draw_info->text);
279 (void) CloneString(&clone_info->font,draw_info->font);
280 (void) CloneString(&clone_info->metrics,draw_info->metrics);
281 (void) CloneString(&clone_info->family,draw_info->family);
282 clone_info->style=draw_info->style;
283 clone_info->stretch=draw_info->stretch;
284 clone_info->weight=draw_info->weight;
285 (void) CloneString(&clone_info->encoding,draw_info->encoding);
286 clone_info->pointsize=draw_info->pointsize;
287 clone_info->kerning=draw_info->kerning;
288 clone_info->interline_spacing=draw_info->interline_spacing;
289 clone_info->interword_spacing=draw_info->interword_spacing;
290 clone_info->direction=draw_info->direction;
291 (void) CloneString(&clone_info->density,draw_info->density);
292 clone_info->align=draw_info->align;
293 clone_info->undercolor=draw_info->undercolor;
294 clone_info->border_color=draw_info->border_color;
295 (void) CloneString(&clone_info->server_name,draw_info->server_name);
296 if (draw_info->dash_pattern != (double *) NULL)
301 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
302 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
303 sizeof(*clone_info->dash_pattern));
304 if (clone_info->dash_pattern == (double *) NULL)
305 ThrowFatalException(ResourceLimitFatalError,
306 "UnableToAllocateDashPattern");
307 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
308 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
310 clone_info->gradient=draw_info->gradient;
311 if (draw_info->gradient.stops != (StopInfo *) NULL)
316 number_stops=clone_info->gradient.number_stops;
317 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
318 number_stops,sizeof(*clone_info->gradient.stops));
319 if (clone_info->gradient.stops == (StopInfo *) NULL)
320 ThrowFatalException(ResourceLimitFatalError,
321 "UnableToAllocateDashPattern");
322 (void) CopyMagickMemory(clone_info->gradient.stops,
323 draw_info->gradient.stops,(size_t) number_stops*
324 sizeof(*clone_info->gradient.stops));
326 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
327 clone_info->bounds=draw_info->bounds;
328 clone_info->clip_units=draw_info->clip_units;
329 clone_info->render=draw_info->render;
330 clone_info->alpha=draw_info->alpha;
331 clone_info->element_reference=draw_info->element_reference;
332 clone_info->debug=IsEventLogging();
333 exception=DestroyExceptionInfo(exception);
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 + C o n v e r t P a t h T o P o l y g o n %
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % ConvertPathToPolygon() converts a path to the more efficient sorted
351 % The format of the ConvertPathToPolygon method is:
353 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
354 % const PathInfo *path_info)
356 % A description of each parameter follows:
358 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
359 % rendering form of type PolygonInfo.
361 % o draw_info: Specifies a pointer to an DrawInfo structure.
363 % o path_info: Specifies a pointer to an PathInfo structure.
368 #if defined(__cplusplus) || defined(c_plusplus)
372 static int CompareEdges(const void *x,const void *y)
374 register const EdgeInfo
381 p=(const EdgeInfo *) x;
382 q=(const EdgeInfo *) y;
383 if ((p->points[0].y-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,ExceptionInfo *exception)
941 % A description of each parameter follows:
943 % o image: the image.
945 % o source: the source image.
947 % o affine: the affine transform.
949 % o exception: return any errors or warnings in this structure.
953 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
954 const double y,const SegmentInfo *edge)
967 Determine left and right edges.
969 inverse_edge.x1=edge->x1;
970 inverse_edge.y1=edge->y1;
971 inverse_edge.x2=edge->x2;
972 inverse_edge.y2=edge->y2;
973 z=affine->ry*y+affine->tx;
974 if (affine->sx >= MagickEpsilon)
976 intercept=(-z/affine->sx);
978 if (x > inverse_edge.x1)
980 intercept=(-z+(double) image->columns)/affine->sx;
982 if (x < inverse_edge.x2)
986 if (affine->sx < -MagickEpsilon)
988 intercept=(-z+(double) image->columns)/affine->sx;
990 if (x > inverse_edge.x1)
992 intercept=(-z/affine->sx);
994 if (x < inverse_edge.x2)
998 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1000 inverse_edge.x2=edge->x1;
1001 return(inverse_edge);
1004 Determine top and bottom edges.
1006 z=affine->sy*y+affine->ty;
1007 if (affine->rx >= MagickEpsilon)
1009 intercept=(-z/affine->rx);
1011 if (x > inverse_edge.x1)
1013 intercept=(-z+(double) image->rows)/affine->rx;
1015 if (x < inverse_edge.x2)
1019 if (affine->rx < -MagickEpsilon)
1021 intercept=(-z+(double) image->rows)/affine->rx;
1023 if (x > inverse_edge.x1)
1025 intercept=(-z/affine->rx);
1027 if (x < inverse_edge.x2)
1031 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1033 inverse_edge.x2=edge->x2;
1034 return(inverse_edge);
1036 return(inverse_edge);
1039 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1047 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1049 inverse_affine.sx=determinant*affine->sy;
1050 inverse_affine.rx=determinant*(-affine->rx);
1051 inverse_affine.ry=determinant*(-affine->ry);
1052 inverse_affine.sy=determinant*affine->sx;
1053 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1055 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1057 return(inverse_affine);
1060 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1067 static inline double MagickMax(const double x,const double y)
1074 static inline double MagickMin(const double x,const double y)
1081 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1082 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1115 Determine bounding box.
1117 assert(image != (Image *) NULL);
1118 assert(image->signature == MagickSignature);
1119 if (image->debug != MagickFalse)
1120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1121 assert(source != (const Image *) NULL);
1122 assert(source->signature == MagickSignature);
1123 assert(affine != (AffineMatrix *) NULL);
1126 extent[1].x=(double) source->columns-1.0;
1128 extent[2].x=(double) source->columns-1.0;
1129 extent[2].y=(double) source->rows-1.0;
1131 extent[3].y=(double) source->rows-1.0;
1132 for (i=0; i < 4; i++)
1135 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1136 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1140 for (i=1; i < 4; i++)
1142 if (min.x > extent[i].x)
1144 if (min.y > extent[i].y)
1146 if (max.x < extent[i].x)
1148 if (max.y < extent[i].y)
1152 Affine transform image.
1154 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1155 return(MagickFalse);
1157 edge.x1=MagickMax(min.x,0.0);
1158 edge.y1=MagickMax(min.y,0.0);
1159 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1160 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1161 inverse_affine=InverseAffineMatrix(affine);
1162 GetPixelInfo(image,&zero);
1163 start=(ssize_t) ceil(edge.y1-0.5);
1164 stop=(ssize_t) floor(edge.y2+0.5);
1165 source_view=AcquireVirtualCacheView(source,exception);
1166 image_view=AcquireAuthenticCacheView(image,exception);
1167 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1168 #pragma omp parallel for schedule(static,4) shared(status) \
1169 magick_threads(source,image,1,1)
1171 for (y=start; y <= stop; y++)
1192 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1193 if (inverse_edge.x2 < inverse_edge.x1)
1195 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1196 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1198 if (q == (Quantum *) NULL)
1203 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1205 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1207 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1209 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1210 point.x,point.y,&pixel,exception);
1211 GetPixelInfoPixel(image,q,&composite);
1212 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1214 SetPixelInfoPixel(image,&composite,q);
1216 q+=GetPixelChannels(image);
1218 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1221 source_view=DestroyCacheView(source_view);
1222 image_view=DestroyCacheView(image_view);
1227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1231 + D r a w B o u n d i n g R e c t a n g l e s %
1235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1238 % is only useful for developers debugging the rendering algorithm.
1240 % The format of the DrawBoundingRectangles method is:
1242 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1243 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1245 % A description of each parameter follows:
1247 % o image: the image.
1249 % o draw_info: the draw info.
1251 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1253 % o exception: return any errors or warnings in this structure.
1256 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1257 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1282 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1283 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1285 resolution.x=DefaultResolution;
1286 resolution.y=DefaultResolution;
1287 if (clone_info->density != (char *) NULL)
1295 flags=ParseGeometry(clone_info->density,&geometry_info);
1296 resolution.x=geometry_info.rho;
1297 resolution.y=geometry_info.sigma;
1298 if ((flags & SigmaValue) == MagickFalse)
1299 resolution.y=resolution.x;
1301 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1302 clone_info->stroke_width/2.0;
1307 if (polygon_info != (PolygonInfo *) NULL)
1309 bounds=polygon_info->edges[0].bounds;
1310 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1312 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1313 bounds.x1=polygon_info->edges[i].bounds.x1;
1314 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1315 bounds.y1=polygon_info->edges[i].bounds.y1;
1316 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1317 bounds.x2=polygon_info->edges[i].bounds.x2;
1318 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1319 bounds.y2=polygon_info->edges[i].bounds.y2;
1322 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1323 image->columns ? (double) image->columns-1 : bounds.x1;
1325 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1326 image->rows ? (double) image->rows-1 : bounds.y1;
1328 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1329 image->columns ? (double) image->columns-1 : bounds.x2;
1331 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1332 image->rows ? (double) image->rows-1 : bounds.y2;
1333 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1335 if (polygon_info->edges[i].direction != 0)
1336 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1339 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1341 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1342 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1343 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1344 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1345 primitive_info[0].primitive=RectanglePrimitive;
1346 TraceRectangle(primitive_info,start,end);
1347 primitive_info[0].method=ReplaceMethod;
1348 coordinates=(ssize_t) primitive_info[0].coordinates;
1349 primitive_info[coordinates].primitive=UndefinedPrimitive;
1350 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1353 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1355 start.x=(double) (bounds.x1-mid);
1356 start.y=(double) (bounds.y1-mid);
1357 end.x=(double) (bounds.x2+mid);
1358 end.y=(double) (bounds.y2+mid);
1359 primitive_info[0].primitive=RectanglePrimitive;
1360 TraceRectangle(primitive_info,start,end);
1361 primitive_info[0].method=ReplaceMethod;
1362 coordinates=(ssize_t) primitive_info[0].coordinates;
1363 primitive_info[coordinates].primitive=UndefinedPrimitive;
1364 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1365 clone_info=DestroyDrawInfo(clone_info);
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 % D r a w C l i p P a t h %
1377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 % DrawClipPath() draws the clip path on the image mask.
1381 % The format of the DrawClipPath method is:
1383 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1384 % const char *name,ExceptionInfo *exception)
1386 % A description of each parameter follows:
1388 % o image: the image.
1390 % o draw_info: the draw info.
1392 % o name: the name of the clip path.
1394 % o exception: return any errors or warnings in this structure.
1397 MagickExport MagickBooleanType DrawClipPath(Image *image,
1398 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1401 filename[MaxTextExtent];
1415 assert(image != (Image *) NULL);
1416 assert(image->signature == MagickSignature);
1417 if (image->debug != MagickFalse)
1418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1419 assert(draw_info != (const DrawInfo *) NULL);
1420 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1421 value=GetImageArtifact(image,filename);
1422 if (value == (const char *) NULL)
1423 return(MagickFalse);
1424 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1425 if (clip_mask == (Image *) NULL)
1426 return(MagickFalse);
1427 (void) QueryColorCompliance("#0000",AllCompliance,
1428 &clip_mask->background_color,exception);
1429 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1430 (void) SetImageBackgroundColor(clip_mask,exception);
1431 if (image->debug != MagickFalse)
1432 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1433 draw_info->clip_mask);
1434 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1435 (void) CloneString(&clone_info->primitive,value);
1436 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1438 clone_info->clip_mask=(char *) NULL;
1439 status=NegateImage(clip_mask,MagickFalse,exception);
1440 (void) SetImageMask(image,clip_mask,exception);
1441 clip_mask=DestroyImage(clip_mask);
1442 status&=DrawImage(image,clone_info,exception);
1443 clone_info=DestroyDrawInfo(clone_info);
1444 if (image->debug != MagickFalse)
1445 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1446 return(status != 0 ? MagickTrue : MagickFalse);
1450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454 + D r a w D a s h P o l y g o n %
1458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1461 % image while respecting the dash offset and dash pattern attributes.
1463 % The format of the DrawDashPolygon method is:
1465 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1466 % const PrimitiveInfo *primitive_info,Image *image,
1467 % ExceptionInfo *exception)
1469 % A description of each parameter follows:
1471 % o draw_info: the draw info.
1473 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1475 % o image: the image.
1477 % o exception: return any errors or warnings in this structure.
1480 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1481 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1513 assert(draw_info != (const DrawInfo *) NULL);
1514 if (image->debug != MagickFalse)
1515 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1516 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1517 clone_info->miterlimit=0;
1518 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1519 number_vertices=(size_t) i;
1520 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1521 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1522 if (dash_polygon == (PrimitiveInfo *) NULL)
1523 return(MagickFalse);
1524 dash_polygon[0]=primitive_info[0];
1525 scale=ExpandAffine(&draw_info->affine);
1526 length=scale*(draw_info->dash_pattern[0]-0.5);
1527 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1529 for (n=0; offset > 0.0; j=0)
1531 if (draw_info->dash_pattern[n] <= 0.0)
1533 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1534 if (offset > length)
1538 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1541 if (offset < length)
1553 for (i=1; i < (ssize_t) number_vertices; i++)
1555 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1556 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1557 maximum_length=hypot((double) dx,dy);
1561 if (draw_info->dash_pattern[n] == 0.0)
1563 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1565 for (total_length=0.0; (total_length+length) <= maximum_length; )
1567 total_length+=length;
1568 if ((n & 0x01) != 0)
1570 dash_polygon[0]=primitive_info[0];
1571 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1572 total_length/maximum_length);
1573 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1574 total_length/maximum_length);
1579 if ((j+1) > (ssize_t) (2*number_vertices))
1581 dash_polygon[j]=primitive_info[i-1];
1582 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1583 total_length/maximum_length);
1584 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1585 total_length/maximum_length);
1586 dash_polygon[j].coordinates=1;
1588 dash_polygon[0].coordinates=(size_t) j;
1589 dash_polygon[j].primitive=UndefinedPrimitive;
1590 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1593 if (draw_info->dash_pattern[n] == 0.0)
1595 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1597 length-=(maximum_length-total_length);
1598 if ((n & 0x01) != 0)
1600 dash_polygon[j]=primitive_info[i];
1601 dash_polygon[j].coordinates=1;
1604 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1606 dash_polygon[j]=primitive_info[i-1];
1607 dash_polygon[j].point.x+=MagickEpsilon;
1608 dash_polygon[j].point.y+=MagickEpsilon;
1609 dash_polygon[j].coordinates=1;
1611 dash_polygon[0].coordinates=(size_t) j;
1612 dash_polygon[j].primitive=UndefinedPrimitive;
1613 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1615 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1616 clone_info=DestroyDrawInfo(clone_info);
1617 if (image->debug != MagickFalse)
1618 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1619 return(status != 0 ? MagickTrue : MagickFalse);
1623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 % D r a w I m a g e %
1631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1633 % DrawImage() draws a graphic primitive on your image. The primitive
1634 % may be represented as a string or filename. Precede the filename with an
1635 % "at" sign (@) and the contents of the file are drawn on the image. You
1636 % can affect how text is drawn by setting one or more members of the draw
1639 % The format of the DrawImage method is:
1641 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1642 % ExceptionInfo *exception)
1644 % A description of each parameter follows:
1646 % o image: the image.
1648 % o draw_info: the draw info.
1650 % o exception: return any errors or warnings in this structure.
1654 static inline MagickBooleanType IsPoint(const char *point)
1662 value=StringToDouble(point,&p);
1663 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1666 static inline void TracePoint(PrimitiveInfo *primitive_info,
1667 const PointInfo point)
1669 primitive_info->coordinates=1;
1670 primitive_info->point=point;
1673 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1674 ExceptionInfo *exception)
1676 #define RenderImageTag "Render/Image"
1683 key[2*MaxTextExtent],
1684 keyword[MaxTextExtent],
1685 geometry[MaxTextExtent],
1686 name[MaxTextExtent],
1687 pattern[MaxTextExtent],
1740 Ensure the annotation info is valid.
1742 assert(image != (Image *) NULL);
1743 assert(image->signature == MagickSignature);
1744 if (image->debug != MagickFalse)
1745 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1746 assert(draw_info != (DrawInfo *) NULL);
1747 assert(draw_info->signature == MagickSignature);
1748 if (image->debug != MagickFalse)
1749 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1750 if ((draw_info->primitive == (char *) NULL) ||
1751 (*draw_info->primitive == '\0'))
1752 return(MagickFalse);
1753 if (image->debug != MagickFalse)
1754 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1755 if (*draw_info->primitive != '@')
1756 primitive=AcquireString(draw_info->primitive);
1758 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1759 if (primitive == (char *) NULL)
1760 return(MagickFalse);
1761 primitive_extent=(double) strlen(primitive);
1762 (void) SetImageArtifact(image,"MVG",primitive);
1765 Allocate primitive info memory.
1767 graphic_context=(DrawInfo **) AcquireMagickMemory(
1768 sizeof(*graphic_context));
1769 if (graphic_context == (DrawInfo **) NULL)
1771 primitive=DestroyString(primitive);
1772 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1776 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1777 sizeof(*primitive_info));
1778 if (primitive_info == (PrimitiveInfo *) NULL)
1780 primitive=DestroyString(primitive);
1781 for ( ; n >= 0; n--)
1782 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1783 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1784 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1787 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1788 graphic_context[n]->viewbox=image->page;
1789 if ((image->page.width == 0) || (image->page.height == 0))
1791 graphic_context[n]->viewbox.width=image->columns;
1792 graphic_context[n]->viewbox.height=image->rows;
1794 token=AcquireString(primitive);
1795 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1797 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1798 return(MagickFalse);
1800 for (q=primitive; *q != '\0'; )
1803 Interpret graphic primitive.
1805 GetMagickToken(q,&q,keyword);
1806 if (*keyword == '\0')
1808 if (*keyword == '#')
1813 while ((*q != '\n') && (*q != '\0'))
1817 p=q-strlen(keyword)-1;
1818 primitive_type=UndefinedPrimitive;
1819 current=graphic_context[n]->affine;
1820 GetAffineMatrix(&affine);
1828 if (LocaleCompare("affine",keyword) == 0)
1830 GetMagickToken(q,&q,token);
1831 affine.sx=StringToDouble(token,(char **) NULL);
1832 GetMagickToken(q,&q,token);
1834 GetMagickToken(q,&q,token);
1835 affine.rx=StringToDouble(token,(char **) NULL);
1836 GetMagickToken(q,&q,token);
1838 GetMagickToken(q,&q,token);
1839 affine.ry=StringToDouble(token,(char **) NULL);
1840 GetMagickToken(q,&q,token);
1842 GetMagickToken(q,&q,token);
1843 affine.sy=StringToDouble(token,(char **) NULL);
1844 GetMagickToken(q,&q,token);
1846 GetMagickToken(q,&q,token);
1847 affine.tx=StringToDouble(token,(char **) NULL);
1848 GetMagickToken(q,&q,token);
1850 GetMagickToken(q,&q,token);
1851 affine.ty=StringToDouble(token,(char **) NULL);
1854 if (LocaleCompare("arc",keyword) == 0)
1856 primitive_type=ArcPrimitive;
1865 if (LocaleCompare("bezier",keyword) == 0)
1867 primitive_type=BezierPrimitive;
1870 if (LocaleCompare("border-color",keyword) == 0)
1872 GetMagickToken(q,&q,token);
1873 (void) QueryColorCompliance(token,AllCompliance,
1874 &graphic_context[n]->border_color,exception);
1883 if (LocaleCompare("clip-path",keyword) == 0)
1888 GetMagickToken(q,&q,token);
1889 (void) CloneString(&graphic_context[n]->clip_mask,token);
1890 (void) DrawClipPath(image,graphic_context[n],
1891 graphic_context[n]->clip_mask,exception);
1894 if (LocaleCompare("clip-rule",keyword) == 0)
1899 GetMagickToken(q,&q,token);
1900 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1902 if (fill_rule == -1)
1905 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1908 if (LocaleCompare("clip-units",keyword) == 0)
1913 GetMagickToken(q,&q,token);
1914 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1916 if (clip_units == -1)
1921 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1922 if (clip_units == ObjectBoundingBox)
1924 GetAffineMatrix(¤t);
1925 affine.sx=draw_info->bounds.x2;
1926 affine.sy=draw_info->bounds.y2;
1927 affine.tx=draw_info->bounds.x1;
1928 affine.ty=draw_info->bounds.y1;
1933 if (LocaleCompare("circle",keyword) == 0)
1935 primitive_type=CirclePrimitive;
1938 if (LocaleCompare("color",keyword) == 0)
1940 primitive_type=ColorPrimitive;
1949 if (LocaleCompare("decorate",keyword) == 0)
1954 GetMagickToken(q,&q,token);
1955 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1960 graphic_context[n]->decorate=(DecorationType) decorate;
1969 if (LocaleCompare("ellipse",keyword) == 0)
1971 primitive_type=EllipsePrimitive;
1974 if (LocaleCompare("encoding",keyword) == 0)
1976 GetMagickToken(q,&q,token);
1977 (void) CloneString(&graphic_context[n]->encoding,token);
1986 if (LocaleCompare("fill",keyword) == 0)
1988 GetMagickToken(q,&q,token);
1989 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1990 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1991 (void) DrawPatternPath(image,draw_info,token,
1992 &graphic_context[n]->fill_pattern,exception);
1995 status&=QueryColorCompliance(token,AllCompliance,
1996 &graphic_context[n]->fill,exception);
1997 if (status == MagickFalse)
2002 pattern_info=AcquireImageInfo();
2003 (void) CopyMagickString(pattern_info->filename,token,
2005 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2007 CatchException(exception);
2008 pattern_info=DestroyImageInfo(pattern_info);
2013 if (LocaleCompare("fill-alpha",keyword) == 0)
2015 GetMagickToken(q,&q,token);
2016 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2017 graphic_context[n]->fill.alpha=(double) QuantumRange*
2018 factor*StringToDouble(token,(char **) NULL);
2021 if (LocaleCompare("fill-rule",keyword) == 0)
2026 GetMagickToken(q,&q,token);
2027 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2029 if (fill_rule == -1)
2032 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2035 if (LocaleCompare("font",keyword) == 0)
2037 GetMagickToken(q,&q,token);
2038 (void) CloneString(&graphic_context[n]->font,token);
2039 if (LocaleCompare("none",token) == 0)
2040 graphic_context[n]->font=(char *)
2041 RelinquishMagickMemory(graphic_context[n]->font);
2044 if (LocaleCompare("font-family",keyword) == 0)
2046 GetMagickToken(q,&q,token);
2047 (void) CloneString(&graphic_context[n]->family,token);
2050 if (LocaleCompare("font-size",keyword) == 0)
2052 GetMagickToken(q,&q,token);
2053 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2056 if (LocaleCompare("font-stretch",keyword) == 0)
2061 GetMagickToken(q,&q,token);
2062 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2066 graphic_context[n]->stretch=(StretchType) stretch;
2069 if (LocaleCompare("font-style",keyword) == 0)
2074 GetMagickToken(q,&q,token);
2075 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2079 graphic_context[n]->style=(StyleType) style;
2082 if (LocaleCompare("font-weight",keyword) == 0)
2084 GetMagickToken(q,&q,token);
2085 graphic_context[n]->weight=StringToUnsignedLong(token);
2086 if (LocaleCompare(token,"all") == 0)
2087 graphic_context[n]->weight=0;
2088 if (LocaleCompare(token,"bold") == 0)
2089 graphic_context[n]->weight=700;
2090 if (LocaleCompare(token,"bolder") == 0)
2091 if (graphic_context[n]->weight <= 800)
2092 graphic_context[n]->weight+=100;
2093 if (LocaleCompare(token,"lighter") == 0)
2094 if (graphic_context[n]->weight >= 100)
2095 graphic_context[n]->weight-=100;
2096 if (LocaleCompare(token,"normal") == 0)
2097 graphic_context[n]->weight=400;
2106 if (LocaleCompare("gradient-units",keyword) == 0)
2108 GetMagickToken(q,&q,token);
2111 if (LocaleCompare("gravity",keyword) == 0)
2116 GetMagickToken(q,&q,token);
2117 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2121 graphic_context[n]->gravity=(GravityType) gravity;
2130 if (LocaleCompare("image",keyword) == 0)
2135 primitive_type=ImagePrimitive;
2136 GetMagickToken(q,&q,token);
2137 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2141 graphic_context[n]->compose=(CompositeOperator) compose;
2144 if (LocaleCompare("interline-spacing",keyword) == 0)
2146 GetMagickToken(q,&q,token);
2147 graphic_context[n]->interline_spacing=StringToDouble(token,
2151 if (LocaleCompare("interword-spacing",keyword) == 0)
2153 GetMagickToken(q,&q,token);
2154 graphic_context[n]->interword_spacing=StringToDouble(token,
2164 if (LocaleCompare("kerning",keyword) == 0)
2166 GetMagickToken(q,&q,token);
2167 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2176 if (LocaleCompare("line",keyword) == 0)
2177 primitive_type=LinePrimitive;
2185 if (LocaleCompare("matte",keyword) == 0)
2186 primitive_type=MattePrimitive;
2194 if (LocaleCompare("offset",keyword) == 0)
2196 GetMagickToken(q,&q,token);
2199 if (LocaleCompare("opacity",keyword) == 0)
2201 GetMagickToken(q,&q,token);
2202 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2203 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2204 QuantumScale*graphic_context[n]->alpha)*factor*
2205 StringToDouble(token,(char **) NULL))));
2206 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2207 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2216 if (LocaleCompare("path",keyword) == 0)
2218 primitive_type=PathPrimitive;
2221 if (LocaleCompare("point",keyword) == 0)
2223 primitive_type=PointPrimitive;
2226 if (LocaleCompare("polyline",keyword) == 0)
2228 primitive_type=PolylinePrimitive;
2231 if (LocaleCompare("polygon",keyword) == 0)
2233 primitive_type=PolygonPrimitive;
2236 if (LocaleCompare("pop",keyword) == 0)
2238 GetMagickToken(q,&q,token);
2239 if (LocaleCompare("clip-path",token) == 0)
2241 if (LocaleCompare("defs",token) == 0)
2243 if (LocaleCompare("gradient",token) == 0)
2245 if (LocaleCompare("graphic-context",token) == 0)
2249 (void) ThrowMagickException(exception,GetMagickModule(),
2250 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2254 if (graphic_context[n]->clip_mask != (char *) NULL)
2255 if (LocaleCompare(graphic_context[n]->clip_mask,
2256 graphic_context[n-1]->clip_mask) != 0)
2257 (void) SetImageMask(image,(Image *) NULL,exception);
2258 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2262 if (LocaleCompare("pattern",token) == 0)
2267 if (LocaleCompare("push",keyword) == 0)
2269 GetMagickToken(q,&q,token);
2270 if (LocaleCompare("clip-path",token) == 0)
2273 name[MaxTextExtent];
2275 GetMagickToken(q,&q,token);
2276 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2277 for (p=q; *q != '\0'; )
2279 GetMagickToken(q,&q,token);
2280 if (LocaleCompare(token,"pop") != 0)
2282 GetMagickToken(q,(const char **) NULL,token);
2283 if (LocaleCompare(token,"clip-path") != 0)
2287 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2288 (void) SetImageArtifact(image,name,token);
2289 GetMagickToken(q,&q,token);
2292 if (LocaleCompare("gradient",token) == 0)
2295 key[2*MaxTextExtent],
2296 name[MaxTextExtent],
2297 type[MaxTextExtent];
2302 GetMagickToken(q,&q,token);
2303 (void) CopyMagickString(name,token,MaxTextExtent);
2304 GetMagickToken(q,&q,token);
2305 (void) CopyMagickString(type,token,MaxTextExtent);
2306 GetMagickToken(q,&q,token);
2307 segment.x1=StringToDouble(token,(char **) NULL);
2308 GetMagickToken(q,&q,token);
2310 GetMagickToken(q,&q,token);
2311 segment.y1=StringToDouble(token,(char **) NULL);
2312 GetMagickToken(q,&q,token);
2314 GetMagickToken(q,&q,token);
2315 segment.x2=StringToDouble(token,(char **) NULL);
2316 GetMagickToken(q,&q,token);
2318 GetMagickToken(q,&q,token);
2319 segment.y2=StringToDouble(token,(char **) NULL);
2320 if (LocaleCompare(type,"radial") == 0)
2322 GetMagickToken(q,&q,token);
2324 GetMagickToken(q,&q,token);
2326 for (p=q; *q != '\0'; )
2328 GetMagickToken(q,&q,token);
2329 if (LocaleCompare(token,"pop") != 0)
2331 GetMagickToken(q,(const char **) NULL,token);
2332 if (LocaleCompare(token,"gradient") != 0)
2336 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2337 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2338 graphic_context[n]->affine.ry*segment.y1+
2339 graphic_context[n]->affine.tx;
2340 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2341 graphic_context[n]->affine.sy*segment.y1+
2342 graphic_context[n]->affine.ty;
2343 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2344 graphic_context[n]->affine.ry*segment.y2+
2345 graphic_context[n]->affine.tx;
2346 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2347 graphic_context[n]->affine.sy*segment.y2+
2348 graphic_context[n]->affine.ty;
2349 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2350 (void) SetImageArtifact(image,key,token);
2351 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2352 (void) FormatLocaleString(geometry,MaxTextExtent,
2353 "%gx%g%+.15g%+.15g",
2354 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2355 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2356 bounds.x1,bounds.y1);
2357 (void) SetImageArtifact(image,key,geometry);
2358 GetMagickToken(q,&q,token);
2361 if (LocaleCompare("pattern",token) == 0)
2366 GetMagickToken(q,&q,token);
2367 (void) CopyMagickString(name,token,MaxTextExtent);
2368 GetMagickToken(q,&q,token);
2369 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2371 GetMagickToken(q,&q,token);
2373 GetMagickToken(q,&q,token);
2374 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2376 GetMagickToken(q,&q,token);
2378 GetMagickToken(q,&q,token);
2379 bounds.width=(size_t) floor(StringToDouble(token,
2380 (char **) NULL)+0.5);
2381 GetMagickToken(q,&q,token);
2383 GetMagickToken(q,&q,token);
2384 bounds.height=(size_t) floor(StringToDouble(token,
2385 (char **) NULL)+0.5);
2386 for (p=q; *q != '\0'; )
2388 GetMagickToken(q,&q,token);
2389 if (LocaleCompare(token,"pop") != 0)
2391 GetMagickToken(q,(const char **) NULL,token);
2392 if (LocaleCompare(token,"pattern") != 0)
2396 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2397 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2398 (void) SetImageArtifact(image,key,token);
2399 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2400 (void) FormatLocaleString(geometry,MaxTextExtent,
2401 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2402 bounds.height,(double) bounds.x,(double) bounds.y);
2403 (void) SetImageArtifact(image,key,geometry);
2404 GetMagickToken(q,&q,token);
2407 if (LocaleCompare("graphic-context",token) == 0)
2410 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2411 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2412 if (graphic_context == (DrawInfo **) NULL)
2414 (void) ThrowMagickException(exception,GetMagickModule(),
2415 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2419 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2420 graphic_context[n-1]);
2423 if (LocaleCompare("defs",token) == 0)
2434 if (LocaleCompare("rectangle",keyword) == 0)
2436 primitive_type=RectanglePrimitive;
2439 if (LocaleCompare("rotate",keyword) == 0)
2441 GetMagickToken(q,&q,token);
2442 angle=StringToDouble(token,(char **) NULL);
2443 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2444 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2445 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2446 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2449 if (LocaleCompare("roundRectangle",keyword) == 0)
2451 primitive_type=RoundRectanglePrimitive;
2460 if (LocaleCompare("scale",keyword) == 0)
2462 GetMagickToken(q,&q,token);
2463 affine.sx=StringToDouble(token,(char **) NULL);
2464 GetMagickToken(q,&q,token);
2466 GetMagickToken(q,&q,token);
2467 affine.sy=StringToDouble(token,(char **) NULL);
2470 if (LocaleCompare("skewX",keyword) == 0)
2472 GetMagickToken(q,&q,token);
2473 angle=StringToDouble(token,(char **) NULL);
2474 affine.ry=sin(DegreesToRadians(angle));
2477 if (LocaleCompare("skewY",keyword) == 0)
2479 GetMagickToken(q,&q,token);
2480 angle=StringToDouble(token,(char **) NULL);
2481 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2484 if (LocaleCompare("stop-color",keyword) == 0)
2489 GetMagickToken(q,&q,token);
2490 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2492 (void) GradientImage(image,LinearGradient,ReflectSpread,
2493 &start_color,&stop_color,exception);
2494 start_color=stop_color;
2495 GetMagickToken(q,&q,token);
2498 if (LocaleCompare("stroke",keyword) == 0)
2500 GetMagickToken(q,&q,token);
2501 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2502 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2503 (void) DrawPatternPath(image,draw_info,token,
2504 &graphic_context[n]->stroke_pattern,exception);
2507 status&=QueryColorCompliance(token,AllCompliance,
2508 &graphic_context[n]->stroke,exception);
2509 if (status == MagickFalse)
2514 pattern_info=AcquireImageInfo();
2515 (void) CopyMagickString(pattern_info->filename,token,
2517 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2519 CatchException(exception);
2520 pattern_info=DestroyImageInfo(pattern_info);
2525 if (LocaleCompare("stroke-antialias",keyword) == 0)
2527 GetMagickToken(q,&q,token);
2528 graphic_context[n]->stroke_antialias=
2529 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2532 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2534 if (graphic_context[n]->dash_pattern != (double *) NULL)
2535 graphic_context[n]->dash_pattern=(double *)
2536 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2537 if (IsPoint(q) != MagickFalse)
2543 GetMagickToken(p,&p,token);
2545 GetMagickToken(p,&p,token);
2546 for (x=0; IsPoint(token) != MagickFalse; x++)
2548 GetMagickToken(p,&p,token);
2550 GetMagickToken(p,&p,token);
2552 graphic_context[n]->dash_pattern=(double *)
2553 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2554 sizeof(*graphic_context[n]->dash_pattern));
2555 if (graphic_context[n]->dash_pattern == (double *) NULL)
2557 (void) ThrowMagickException(exception,GetMagickModule(),
2558 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2562 for (j=0; j < x; j++)
2564 GetMagickToken(q,&q,token);
2566 GetMagickToken(q,&q,token);
2567 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2570 if ((x & 0x01) != 0)
2571 for ( ; j < (2*x); j++)
2572 graphic_context[n]->dash_pattern[j]=
2573 graphic_context[n]->dash_pattern[j-x];
2574 graphic_context[n]->dash_pattern[j]=0.0;
2577 GetMagickToken(q,&q,token);
2580 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2582 GetMagickToken(q,&q,token);
2583 graphic_context[n]->dash_offset=StringToDouble(token,
2587 if (LocaleCompare("stroke-linecap",keyword) == 0)
2592 GetMagickToken(q,&q,token);
2593 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2597 graphic_context[n]->linecap=(LineCap) linecap;
2600 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2605 GetMagickToken(q,&q,token);
2606 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2611 graphic_context[n]->linejoin=(LineJoin) linejoin;
2614 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2616 GetMagickToken(q,&q,token);
2617 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2620 if (LocaleCompare("stroke-opacity",keyword) == 0)
2622 GetMagickToken(q,&q,token);
2623 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2624 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2625 factor*StringToDouble(token,(char **) NULL);
2628 if (LocaleCompare("stroke-width",keyword) == 0)
2630 GetMagickToken(q,&q,token);
2631 graphic_context[n]->stroke_width=StringToDouble(token,
2641 if (LocaleCompare("text",keyword) == 0)
2643 primitive_type=TextPrimitive;
2646 if (LocaleCompare("text-align",keyword) == 0)
2651 GetMagickToken(q,&q,token);
2652 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2656 graphic_context[n]->align=(AlignType) align;
2659 if (LocaleCompare("text-anchor",keyword) == 0)
2664 GetMagickToken(q,&q,token);
2665 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2669 graphic_context[n]->align=(AlignType) align;
2672 if (LocaleCompare("text-antialias",keyword) == 0)
2674 GetMagickToken(q,&q,token);
2675 graphic_context[n]->text_antialias=
2676 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2679 if (LocaleCompare("text-undercolor",keyword) == 0)
2681 GetMagickToken(q,&q,token);
2682 (void) QueryColorCompliance(token,AllCompliance,
2683 &graphic_context[n]->undercolor,exception);
2686 if (LocaleCompare("translate",keyword) == 0)
2688 GetMagickToken(q,&q,token);
2689 affine.tx=StringToDouble(token,(char **) NULL);
2690 GetMagickToken(q,&q,token);
2692 GetMagickToken(q,&q,token);
2693 affine.ty=StringToDouble(token,(char **) NULL);
2702 if (LocaleCompare("viewbox",keyword) == 0)
2704 GetMagickToken(q,&q,token);
2705 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2706 (char **) NULL)-0.5);
2707 GetMagickToken(q,&q,token);
2709 GetMagickToken(q,&q,token);
2710 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2711 (char **) NULL)-0.5);
2712 GetMagickToken(q,&q,token);
2714 GetMagickToken(q,&q,token);
2715 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2716 token,(char **) NULL)+0.5);
2717 GetMagickToken(q,&q,token);
2719 GetMagickToken(q,&q,token);
2720 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2721 token,(char **) NULL)+0.5);
2733 if (status == MagickFalse)
2735 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2736 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2738 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2739 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2740 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2741 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2742 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2744 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2747 if (primitive_type == UndefinedPrimitive)
2749 if (image->debug != MagickFalse)
2750 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2755 Parse the primitive attributes.
2759 primitive_info[0].point.x=0.0;
2760 primitive_info[0].point.y=0.0;
2761 for (x=0; *q != '\0'; x++)
2766 if (IsPoint(q) == MagickFalse)
2768 GetMagickToken(q,&q,token);
2769 point.x=StringToDouble(token,(char **) NULL);
2770 GetMagickToken(q,&q,token);
2772 GetMagickToken(q,&q,token);
2773 point.y=StringToDouble(token,(char **) NULL);
2774 GetMagickToken(q,(const char **) NULL,token);
2776 GetMagickToken(q,&q,token);
2777 primitive_info[i].primitive=primitive_type;
2778 primitive_info[i].point=point;
2779 primitive_info[i].coordinates=0;
2780 primitive_info[i].method=FloodfillMethod;
2782 if (i < (ssize_t) number_points)
2785 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2786 (size_t) number_points,sizeof(*primitive_info));
2787 if (primitive_info == (PrimitiveInfo *) NULL)
2789 (void) ThrowMagickException(exception,GetMagickModule(),
2790 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2794 primitive_info[j].primitive=primitive_type;
2795 primitive_info[j].coordinates=(size_t) x;
2796 primitive_info[j].method=FloodfillMethod;
2797 primitive_info[j].text=(char *) NULL;
2799 Circumscribe primitive within a circle.
2801 bounds.x1=primitive_info[j].point.x;
2802 bounds.y1=primitive_info[j].point.y;
2803 bounds.x2=primitive_info[j].point.x;
2804 bounds.y2=primitive_info[j].point.y;
2805 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2807 point=primitive_info[j+k].point;
2808 if (point.x < bounds.x1)
2810 if (point.y < bounds.y1)
2812 if (point.x > bounds.x2)
2814 if (point.y > bounds.y2)
2818 Speculate how many points our primitive might consume.
2820 length=primitive_info[j].coordinates;
2821 switch (primitive_type)
2823 case RectanglePrimitive:
2828 case RoundRectanglePrimitive:
2830 length*=5+8*BezierQuantum;
2833 case BezierPrimitive:
2835 if (primitive_info[j].coordinates > 107)
2836 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2837 "TooManyBezierCoordinates","`%s'",token);
2838 length=BezierQuantum*primitive_info[j].coordinates;
2847 GetMagickToken(q,&q,token);
2850 for (s=token; *s != '\0'; s=t)
2855 value=StringToDouble(s,&t);
2864 length=length*BezierQuantum/2;
2867 case CirclePrimitive:
2869 case EllipsePrimitive:
2876 alpha=bounds.x2-bounds.x1;
2877 beta=bounds.y2-bounds.y1;
2878 radius=hypot((double) alpha,(double) beta);
2879 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2885 if ((size_t) (i+length) >= number_points)
2888 Resize based on speculative points required by primitive.
2890 number_points+=length+1;
2891 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2892 (size_t) number_points,sizeof(*primitive_info));
2893 if (primitive_info == (PrimitiveInfo *) NULL)
2895 (void) ThrowMagickException(exception,GetMagickModule(),
2896 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2901 switch (primitive_type)
2903 case PointPrimitive:
2906 if (primitive_info[j].coordinates != 1)
2911 TracePoint(primitive_info+j,primitive_info[j].point);
2912 i=(ssize_t) (j+primitive_info[j].coordinates);
2917 if (primitive_info[j].coordinates != 2)
2922 TraceLine(primitive_info+j,primitive_info[j].point,
2923 primitive_info[j+1].point);
2924 i=(ssize_t) (j+primitive_info[j].coordinates);
2927 case RectanglePrimitive:
2929 if (primitive_info[j].coordinates != 2)
2934 TraceRectangle(primitive_info+j,primitive_info[j].point,
2935 primitive_info[j+1].point);
2936 i=(ssize_t) (j+primitive_info[j].coordinates);
2939 case RoundRectanglePrimitive:
2941 if (primitive_info[j].coordinates != 3)
2946 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2947 primitive_info[j+1].point,primitive_info[j+2].point);
2948 i=(ssize_t) (j+primitive_info[j].coordinates);
2953 if (primitive_info[j].coordinates != 3)
2955 primitive_type=UndefinedPrimitive;
2958 TraceArc(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);
2963 case EllipsePrimitive:
2965 if (primitive_info[j].coordinates != 3)
2970 TraceEllipse(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 CirclePrimitive:
2977 if (primitive_info[j].coordinates != 2)
2982 TraceCircle(primitive_info+j,primitive_info[j].point,
2983 primitive_info[j+1].point);
2984 i=(ssize_t) (j+primitive_info[j].coordinates);
2987 case PolylinePrimitive:
2989 case PolygonPrimitive:
2991 primitive_info[i]=primitive_info[j];
2992 primitive_info[i].coordinates=0;
2993 primitive_info[j].coordinates++;
2997 case BezierPrimitive:
2999 if (primitive_info[j].coordinates < 3)
3004 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3005 i=(ssize_t) (j+primitive_info[j].coordinates);
3010 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3013 case ColorPrimitive:
3014 case MattePrimitive:
3019 if (primitive_info[j].coordinates != 1)
3024 GetMagickToken(q,&q,token);
3025 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3029 primitive_info[j].method=(PaintMethod) method;
3034 if (primitive_info[j].coordinates != 1)
3040 GetMagickToken(q,&q,token);
3041 primitive_info[j].text=AcquireString(token);
3044 case ImagePrimitive:
3046 if (primitive_info[j].coordinates != 2)
3051 GetMagickToken(q,&q,token);
3052 primitive_info[j].text=AcquireString(token);
3056 if (primitive_info == (PrimitiveInfo *) NULL)
3058 if (image->debug != MagickFalse)
3059 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3060 if (status == MagickFalse)
3062 primitive_info[i].primitive=UndefinedPrimitive;
3068 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3070 point=primitive_info[i].point;
3071 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3072 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3073 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3074 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3075 point=primitive_info[i].point;
3076 if (point.x < graphic_context[n]->bounds.x1)
3077 graphic_context[n]->bounds.x1=point.x;
3078 if (point.y < graphic_context[n]->bounds.y1)
3079 graphic_context[n]->bounds.y1=point.y;
3080 if (point.x > graphic_context[n]->bounds.x2)
3081 graphic_context[n]->bounds.x2=point.x;
3082 if (point.y > graphic_context[n]->bounds.y2)
3083 graphic_context[n]->bounds.y2=point.y;
3084 if (primitive_info[i].primitive == ImagePrimitive)
3086 if (i >= (ssize_t) number_points)
3087 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3089 if (graphic_context[n]->render != MagickFalse)
3091 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3092 (LocaleCompare(graphic_context[n]->clip_mask,
3093 graphic_context[n-1]->clip_mask) != 0))
3094 status&=DrawClipPath(image,graphic_context[n],
3095 graphic_context[n]->clip_mask,exception);
3096 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3099 if (primitive_info->text != (char *) NULL)
3100 primitive_info->text=(char *) RelinquishMagickMemory(
3101 primitive_info->text);
3102 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3104 if (proceed == MagickFalse)
3109 if (image->debug != MagickFalse)
3110 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3112 Relinquish resources.
3114 token=DestroyString(token);
3115 if (primitive_info != (PrimitiveInfo *) NULL)
3116 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3117 primitive=DestroyString(primitive);
3118 for ( ; n >= 0; n--)
3119 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3120 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3121 if (status == MagickFalse)
3122 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3124 return(status != 0 ? MagickTrue : MagickFalse);
3128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3132 % D r a w G r a d i e n t I m a g e %
3136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3138 % DrawGradientImage() draws a linear gradient on the image.
3140 % The format of the DrawGradientImage method is:
3142 % MagickBooleanType DrawGradientImage(Image *image,
3143 % const DrawInfo *draw_info,ExceptionInfo *exception)
3145 % A description of each parameter follows:
3147 % o image: the image.
3149 % o draw_info: the draw info.
3151 % o exception: return any errors or warnings in this structure.
3155 static inline double GetStopColorOffset(const GradientInfo *gradient,
3156 const ssize_t x,const ssize_t y)
3158 switch (gradient->type)
3160 case UndefinedGradient:
3161 case LinearGradient:
3176 gradient_vector=(&gradient->gradient_vector);
3177 p.x=gradient_vector->x2-gradient_vector->x1;
3178 p.y=gradient_vector->y2-gradient_vector->y1;
3179 q.x=(double) x-gradient_vector->x1;
3180 q.y=(double) y-gradient_vector->y1;
3181 length=sqrt(q.x*q.x+q.y*q.y);
3182 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3183 gamma=PerceptibleReciprocal(gamma);
3184 scale=p.x*q.x+p.y*q.y;
3185 offset=gamma*scale*length;
3188 case RadialGradient:
3197 v.x=(double) x-gradient->center.x;
3198 v.y=(double) y-gradient->center.y;
3199 length=sqrt(v.x*v.x+v.y*v.y);
3200 if (gradient->spread == RepeatSpread)
3202 offset=length/gradient->radius;
3209 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3210 const DrawInfo *draw_info,ExceptionInfo *exception)
3240 Draw linear or radial gradient on image.
3242 assert(image != (Image *) NULL);
3243 assert(image->signature == MagickSignature);
3244 if (image->debug != MagickFalse)
3245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3246 assert(draw_info != (const DrawInfo *) NULL);
3247 gradient=(&draw_info->gradient);
3248 gradient_vector=(&gradient->gradient_vector);
3249 point.x=gradient_vector->x2-gradient_vector->x1;
3250 point.y=gradient_vector->y2-gradient_vector->y1;
3251 length=sqrt(point.x*point.x+point.y*point.y);
3252 bounding_box=gradient->bounding_box;
3254 GetPixelInfo(image,&zero);
3255 image_view=AcquireAuthenticCacheView(image,exception);
3256 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3257 #pragma omp parallel for schedule(static,4) shared(status) \
3258 magick_threads(image,image,1,1)
3260 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3280 if (status == MagickFalse)
3282 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3283 if (q == (Quantum *) NULL)
3290 offset=GetStopColorOffset(gradient,0,y);
3291 if (gradient->type != RadialGradient)
3293 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3295 GetPixelInfoPixel(image,q,&pixel);
3296 switch (gradient->spread)
3298 case UndefinedSpread:
3301 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3302 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3304 offset=GetStopColorOffset(gradient,x,y);
3305 if (gradient->type != RadialGradient)
3308 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3309 if (offset < gradient->stops[i].offset)
3311 if ((offset < 0.0) || (i == 0))
3312 composite=gradient->stops[0].color;
3314 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3315 composite=gradient->stops[gradient->number_stops-1].color;
3320 alpha=(offset-gradient->stops[i].offset)/
3321 (gradient->stops[j].offset-gradient->stops[i].offset);
3322 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3323 &gradient->stops[j].color,alpha,&composite);
3329 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3330 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3332 offset=GetStopColorOffset(gradient,x,y);
3333 if (gradient->type != RadialGradient)
3338 if ((ssize_t) fmod(offset,2.0) == 0)
3339 offset=fmod(offset,1.0);
3341 offset=1.0-fmod(offset,1.0);
3342 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3343 if (offset < gradient->stops[i].offset)
3346 composite=gradient->stops[0].color;
3348 if (i == (ssize_t) gradient->number_stops)
3349 composite=gradient->stops[gradient->number_stops-1].color;
3354 alpha=(offset-gradient->stops[i].offset)/
3355 (gradient->stops[j].offset-gradient->stops[i].offset);
3356 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3357 &gradient->stops[j].color,alpha,&composite);
3369 antialias=MagickFalse;
3371 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3372 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3374 offset=GetStopColorOffset(gradient,x,y);
3375 if (gradient->type == LinearGradient)
3377 repeat=fmod(offset,length);
3379 repeat=length-fmod(-repeat,length);
3381 repeat=fmod(offset,length);
3382 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3383 MagickTrue : MagickFalse;
3384 offset=repeat/length;
3388 repeat=fmod(offset,gradient->radius);
3390 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3392 repeat=fmod(offset,gradient->radius);
3393 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3395 offset=repeat/gradient->radius;
3398 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3399 if (offset < gradient->stops[i].offset)
3402 composite=gradient->stops[0].color;
3404 if (i == (ssize_t) gradient->number_stops)
3405 composite=gradient->stops[gradient->number_stops-1].color;
3410 alpha=(offset-gradient->stops[i].offset)/
3411 (gradient->stops[j].offset-gradient->stops[i].offset);
3412 if (antialias != MagickFalse)
3414 if (gradient->type == LinearGradient)
3415 alpha=length-repeat;
3417 alpha=gradient->radius-repeat;
3419 j=(ssize_t) gradient->number_stops-1L;
3421 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3422 &gradient->stops[j].color,alpha,&composite);
3427 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3429 SetPixelInfoPixel(image,&pixel,q);
3430 q+=GetPixelChannels(image);
3432 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3435 image_view=DestroyCacheView(image_view);
3440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444 % D r a w P a t t e r n P a t h %
3448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3450 % DrawPatternPath() draws a pattern.
3452 % The format of the DrawPatternPath method is:
3454 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3455 % const char *name,Image **pattern,ExceptionInfo *exception)
3457 % A description of each parameter follows:
3459 % o image: the image.
3461 % o draw_info: the draw info.
3463 % o name: the pattern name.
3465 % o image: the image.
3467 % o exception: return any errors or warnings in this structure.
3470 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3471 const DrawInfo *draw_info,const char *name,Image **pattern,
3472 ExceptionInfo *exception)
3475 property[MaxTextExtent];
3490 assert(image != (Image *) NULL);
3491 assert(image->signature == MagickSignature);
3492 if (image->debug != MagickFalse)
3493 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3494 assert(draw_info != (const DrawInfo *) NULL);
3495 assert(name != (const char *) NULL);
3496 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3497 path=GetImageArtifact(image,property);
3498 if (path == (const char *) NULL)
3499 return(MagickFalse);
3500 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3501 geometry=GetImageArtifact(image,property);
3502 if (geometry == (const char *) NULL)
3503 return(MagickFalse);
3504 if ((*pattern) != (Image *) NULL)
3505 *pattern=DestroyImage(*pattern);
3506 image_info=AcquireImageInfo();
3507 image_info->size=AcquireString(geometry);
3508 *pattern=AcquireImage(image_info,exception);
3509 image_info=DestroyImageInfo(image_info);
3510 (void) QueryColorCompliance("#000000ff",AllCompliance,
3511 &(*pattern)->background_color,exception);
3512 (void) SetImageBackgroundColor(*pattern,exception);
3513 if (image->debug != MagickFalse)
3514 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3515 "begin pattern-path %s %s",name,geometry);
3516 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3517 clone_info->fill_pattern=NewImageList();
3518 clone_info->stroke_pattern=NewImageList();
3519 (void) CloneString(&clone_info->primitive,path);
3520 status=DrawImage(*pattern,clone_info,exception);
3521 clone_info=DestroyDrawInfo(clone_info);
3522 if (image->debug != MagickFalse)
3523 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3532 + D r a w P o l y g o n P r i m i t i v e %
3536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3538 % DrawPolygonPrimitive() draws a polygon on the image.
3540 % The format of the DrawPolygonPrimitive method is:
3542 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3543 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3544 % ExceptionInfo *exception)
3546 % A description of each parameter follows:
3548 % o image: the image.
3550 % o draw_info: the draw info.
3552 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3554 % o exception: return any errors or warnings in this structure.
3558 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3563 assert(polygon_info != (PolygonInfo **) NULL);
3564 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3565 if (polygon_info[i] != (PolygonInfo *) NULL)
3566 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3567 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3568 return(polygon_info);
3571 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3572 const PrimitiveInfo *primitive_info)
3575 *restrict path_info;
3586 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3587 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3588 sizeof(*polygon_info));
3589 if (polygon_info == (PolygonInfo **) NULL)
3590 return((PolygonInfo **) NULL);
3591 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3592 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3593 if (path_info == (PathInfo *) NULL)
3594 return(DestroyPolygonThreadSet(polygon_info));
3595 for (i=0; i < (ssize_t) number_threads; i++)
3597 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3598 if (polygon_info[i] == (PolygonInfo *) NULL)
3599 return(DestroyPolygonThreadSet(polygon_info));
3601 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3602 return(polygon_info);
3605 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3606 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3607 const ssize_t y,double *stroke_alpha)
3618 register const PointInfo
3632 Compute fill & stroke opacity for this (x,y) point.
3636 p=polygon_info->edges;
3637 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3639 if ((double) y <= (p->bounds.y1-mid-0.5))
3641 if ((double) y > (p->bounds.y2+mid+0.5))
3643 (void) DestroyEdge(polygon_info,(size_t) j);
3646 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3647 ((double) x > (p->bounds.x2+mid+0.5)))
3649 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3650 for ( ; i < (ssize_t) p->number_points; i++)
3652 if ((double) y <= (p->points[i-1].y-mid-0.5))
3654 if ((double) y > (p->points[i].y+mid+0.5))
3656 if (p->scanline != (double) y)
3658 p->scanline=(double) y;
3659 p->highwater=(size_t) i;
3662 Compute distance between a point and an edge.
3665 delta.x=(q+1)->x-q->x;
3666 delta.y=(q+1)->y-q->y;
3667 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3670 delta.x=(double) x-q->x;
3671 delta.y=(double) y-q->y;
3672 distance=delta.x*delta.x+delta.y*delta.y;
3676 alpha=delta.x*delta.x+delta.y*delta.y;
3679 delta.x=(double) x-(q+1)->x;
3680 delta.y=(double) y-(q+1)->y;
3681 distance=delta.x*delta.x+delta.y*delta.y;
3686 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3687 distance=alpha*beta*beta;
3691 Compute stroke & subpath opacity.
3694 if (p->ghostline == MagickFalse)
3697 if ((*stroke_alpha < 1.0) &&
3698 (distance <= ((alpha+0.25)*(alpha+0.25))))
3701 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3706 if (distance != 1.0)
3707 beta=sqrt((double) distance);
3709 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3710 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3714 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3716 if (distance <= 0.0)
3726 if (distance != 1.0)
3727 beta=sqrt(distance);
3730 if (subpath_alpha < (alpha*alpha))
3731 subpath_alpha=alpha*alpha;
3735 Compute fill opacity.
3737 if (fill == MagickFalse)
3739 if (subpath_alpha >= 1.0)
3742 Determine winding number.
3745 p=polygon_info->edges;
3746 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3748 if ((double) y <= p->bounds.y1)
3750 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3752 if ((double) x > p->bounds.x2)
3754 winding_number+=p->direction ? 1 : -1;
3757 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3758 for ( ; i < (ssize_t) p->number_points; i++)
3759 if ((double) y <= p->points[i].y)
3762 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3763 winding_number+=p->direction ? 1 : -1;
3765 if (fill_rule != NonZeroRule)
3767 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3771 if (MagickAbsoluteValue(winding_number) != 0)
3773 return(subpath_alpha);
3776 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3777 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3778 ExceptionInfo *exception)
3791 **restrict polygon_info;
3808 Compute bounding box.
3810 assert(image != (Image *) NULL);
3811 assert(image->signature == MagickSignature);
3812 if (image->debug != MagickFalse)
3813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3814 assert(draw_info != (DrawInfo *) NULL);
3815 assert(draw_info->signature == MagickSignature);
3816 assert(primitive_info != (PrimitiveInfo *) NULL);
3817 if (primitive_info->coordinates == 0)
3819 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3820 if (polygon_info == (PolygonInfo **) NULL)
3821 return(MagickFalse);
3823 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3824 if (image->debug != MagickFalse)
3825 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3826 fill=(primitive_info->method == FillToBorderMethod) ||
3827 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3828 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3829 bounds=polygon_info[0]->edges[0].bounds;
3830 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3832 p=polygon_info[0]->edges+i;
3833 if (p->bounds.x1 < bounds.x1)
3834 bounds.x1=p->bounds.x1;
3835 if (p->bounds.y1 < bounds.y1)
3836 bounds.y1=p->bounds.y1;
3837 if (p->bounds.x2 > bounds.x2)
3838 bounds.x2=p->bounds.x2;
3839 if (p->bounds.y2 > bounds.y2)
3840 bounds.y2=p->bounds.y2;
3842 bounds.x1-=(mid+1.0);
3843 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3844 image->columns ? (double) image->columns-1 : bounds.x1;
3845 bounds.y1-=(mid+1.0);
3846 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3847 image->rows ? (double) image->rows-1 : bounds.y1;
3848 bounds.x2+=(mid+1.0);
3849 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3850 image->columns ? (double) image->columns-1 : bounds.x2;
3851 bounds.y2+=(mid+1.0);
3852 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3853 image->rows ? (double) image->rows-1 : bounds.y2;
3855 image_view=AcquireAuthenticCacheView(image,exception);
3856 if (primitive_info->coordinates == 1)
3861 start=(ssize_t) ceil(bounds.y1-0.5);
3862 stop=(ssize_t) floor(bounds.y2+0.5);
3863 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3864 #pragma omp parallel for schedule(static,4) shared(status) \
3865 magick_threads(image,image,1,1)
3867 for (y=start; y <= stop; y++)
3885 if (status == MagickFalse)
3887 start=(ssize_t) ceil(bounds.x1-0.5);
3888 stop=(ssize_t) floor(bounds.x2+0.5);
3890 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3892 if (q == (Quantum *) NULL)
3897 GetPixelInfo(image,&pixel);
3898 for ( ; x <= stop; x++)
3900 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3901 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3903 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3904 SetPixelInfoPixel(image,&pixel,q);
3906 q+=GetPixelChannels(image);
3908 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3909 if (sync == MagickFalse)
3912 image_view=DestroyCacheView(image_view);
3913 polygon_info=DestroyPolygonThreadSet(polygon_info);
3914 if (image->debug != MagickFalse)
3915 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3916 " end draw-polygon");
3920 Draw polygon or line.
3922 if (image->alpha_trait != BlendPixelTrait)
3923 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3924 start=(ssize_t) ceil(bounds.y1-0.5);
3925 stop=(ssize_t) floor(bounds.y2+0.5);
3926 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3927 #pragma omp parallel for schedule(static,4) shared(status) \
3928 magick_threads(image,image,1,1)
3930 for (y=start; y <= stop; y++)
3933 id = GetOpenMPThreadId();
3953 if (status == MagickFalse)
3955 start=(ssize_t) ceil(bounds.x1-0.5);
3956 stop=(ssize_t) floor(bounds.x2+0.5);
3957 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3959 if (q == (Quantum *) NULL)
3964 for (x=start; x <= stop; x++)
3969 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3971 if (draw_info->stroke_antialias == MagickFalse)
3973 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3974 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3976 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3977 fill_alpha=fill_alpha*fill_color.alpha;
3978 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
3979 GetPixelAlpha(image,q),q);
3980 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3981 stroke_alpha=stroke_alpha*stroke_color.alpha;
3982 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
3983 GetPixelAlpha(image,q),q);
3984 q+=GetPixelChannels(image);
3986 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3989 image_view=DestroyCacheView(image_view);
3990 polygon_info=DestroyPolygonThreadSet(polygon_info);
3991 if (image->debug != MagickFalse)
3992 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4001 % D r a w P r i m i t i v e %
4005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4007 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4009 % The format of the DrawPrimitive method is:
4011 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4012 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4014 % A description of each parameter follows:
4016 % o image: the image.
4018 % o draw_info: the draw info.
4020 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4022 % o exception: return any errors or warnings in this structure.
4026 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4052 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4053 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4054 switch (primitive_info->primitive)
4056 case PointPrimitive:
4058 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4059 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4060 methods[primitive_info->method]);
4063 case ColorPrimitive:
4065 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4066 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4067 methods[primitive_info->method]);
4070 case MattePrimitive:
4072 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4073 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4074 methods[primitive_info->method]);
4079 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4080 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4083 case ImagePrimitive:
4085 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4086 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4093 p=primitive_info[0].point;
4096 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4098 point=primitive_info[i].point;
4099 if (coordinates <= 0)
4101 coordinates=(ssize_t) primitive_info[i].coordinates;
4102 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4103 " begin open (%.20g)",(double) coordinates);
4106 point=primitive_info[i].point;
4107 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4108 (fabs(q.y-point.y) >= MagickEpsilon))
4109 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4110 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4112 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4113 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4116 if (coordinates > 0)
4118 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4119 (fabs(p.y-point.y) >= MagickEpsilon))
4120 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4121 (double) coordinates);
4123 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4124 (double) coordinates);
4128 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4129 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4130 ExceptionInfo *exception)
4145 if (image->debug != MagickFalse)
4147 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4148 " begin draw-primitive");
4149 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4150 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4151 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4152 draw_info->affine.tx,draw_info->affine.ty);
4154 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4155 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4156 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4157 (void) SetImageColorspace(image,sRGBColorspace,exception);
4159 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4160 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4161 image_view=AcquireAuthenticCacheView(image,exception);
4162 switch (primitive_info->primitive)
4164 case PointPrimitive:
4172 if ((y < 0) || (y >= (ssize_t) image->rows))
4174 if ((x < 0) || (x >= (ssize_t) image->columns))
4176 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4177 if (q == (Quantum *) NULL)
4179 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4180 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4181 (double) GetPixelAlpha(image,q),q);
4182 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4185 case ColorPrimitive:
4187 switch (primitive_info->method)
4198 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4199 if (q == (Quantum *) NULL)
4201 GetPixelInfo(image,&pixel);
4202 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4203 SetPixelInfoPixel(image,&pixel,q);
4204 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4216 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4218 for (y=0; y < (ssize_t) image->rows; y++)
4223 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4225 if (q == (Quantum *) NULL)
4227 for (x=0; x < (ssize_t) image->columns; x++)
4229 GetPixelInfoPixel(image,q,&pixel);
4230 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4232 q+=GetPixelChannels(image);
4235 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4236 SetPixelInfoPixel(image,&pixel,q);
4237 q+=GetPixelChannels(image);
4239 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4240 if (sync == MagickFalse)
4245 case FloodfillMethod:
4246 case FillToBorderMethod:
4251 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4253 if (primitive_info->method == FillToBorderMethod)
4255 target.red=(double) draw_info->border_color.red;
4256 target.green=(double) draw_info->border_color.green;
4257 target.blue=(double) draw_info->border_color.blue;
4259 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4260 primitive_info->method == FloodfillMethod ? MagickFalse :
4261 MagickTrue,exception);
4272 GetPixelInfo(image,&pixel);
4273 for (y=0; y < (ssize_t) image->rows; y++)
4281 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4283 if (q == (Quantum *) NULL)
4285 for (x=0; x < (ssize_t) image->columns; x++)
4287 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4288 SetPixelInfoPixel(image,&pixel,q);
4289 q+=GetPixelChannels(image);
4291 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4292 if (sync == MagickFalse)
4300 case MattePrimitive:
4302 if (image->alpha_trait != BlendPixelTrait)
4303 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4304 switch (primitive_info->method)
4315 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4316 if (q == (Quantum *) NULL)
4318 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4319 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4320 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4332 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4334 for (y=0; y < (ssize_t) image->rows; y++)
4342 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4344 if (q == (Quantum *) NULL)
4346 for (x=0; x < (ssize_t) image->columns; x++)
4348 GetPixelInfoPixel(image,q,&pixel);
4349 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4351 q+=GetPixelChannels(image);
4354 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4355 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4356 q+=GetPixelChannels(image);
4358 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4359 if (sync == MagickFalse)
4364 case FloodfillMethod:
4365 case FillToBorderMethod:
4373 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4375 if (primitive_info->method == FillToBorderMethod)
4377 target.red=(double) draw_info->border_color.red;
4378 target.green=(double) draw_info->border_color.green;
4379 target.blue=(double) draw_info->border_color.blue;
4381 channel_mask=SetImageChannelMask(image,AlphaChannel);
4382 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4383 primitive_info->method == FloodfillMethod ? MagickFalse :
4384 MagickTrue,exception);
4385 (void) SetImageChannelMask(image,channel_mask);
4396 for (y=0; y < (ssize_t) image->rows; y++)
4404 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4406 if (q == (Quantum *) NULL)
4408 for (x=0; x < (ssize_t) image->columns; x++)
4410 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4411 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4412 q+=GetPixelChannels(image);
4414 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4415 if (sync == MagickFalse)
4426 geometry[MaxTextExtent];
4431 if (primitive_info->text == (char *) NULL)
4433 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4434 (void) CloneString(&clone_info->text,primitive_info->text);
4435 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4436 primitive_info->point.x,primitive_info->point.y);
4437 (void) CloneString(&clone_info->geometry,geometry);
4438 status&=AnnotateImage(image,clone_info,exception);
4439 clone_info=DestroyDrawInfo(clone_info);
4442 case ImagePrimitive:
4448 composite_geometry[MaxTextExtent];
4463 if (primitive_info->text == (char *) NULL)
4465 clone_info=AcquireImageInfo();
4466 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4467 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4471 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4473 composite_image=ReadImage(clone_info,exception);
4475 clone_info=DestroyImageInfo(clone_info);
4476 if (composite_image == (Image *) NULL)
4478 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4479 NULL,(void *) NULL);
4480 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4481 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4482 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4483 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4486 geometry[MaxTextExtent];
4491 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4492 primitive_info[1].point.x,primitive_info[1].point.y);
4493 composite_image->filter=image->filter;
4494 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4497 if (composite_image->alpha_trait != BlendPixelTrait)
4498 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4500 if (draw_info->alpha != OpaqueAlpha)
4501 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4502 SetGeometry(image,&geometry);
4503 image->gravity=draw_info->gravity;
4506 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4507 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4508 composite_image->rows,(double) geometry.x,(double) geometry.y);
4509 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4510 affine=draw_info->affine;
4511 affine.tx=(double) geometry.x;
4512 affine.ty=(double) geometry.y;
4513 composite_image->interpolate=image->interpolate;
4514 if (draw_info->compose == OverCompositeOp)
4515 (void) DrawAffineImage(image,composite_image,&affine,exception);
4517 (void) CompositeImage(image,composite_image,draw_info->compose,
4518 MagickTrue,geometry.x,geometry.y,exception);
4519 composite_image=DestroyImage(composite_image);
4531 if (IsEventLogging() != MagickFalse)
4532 LogPrimitiveInfo(primitive_info);
4533 scale=ExpandAffine(&draw_info->affine);
4534 if ((draw_info->dash_pattern != (double *) NULL) &&
4535 (draw_info->dash_pattern[0] != 0.0) &&
4536 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4537 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4542 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4543 clone_info->stroke_width=0.0;
4544 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4545 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4547 clone_info=DestroyDrawInfo(clone_info);
4548 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4551 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4553 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4559 Draw strokes while respecting line cap/join attributes.
4561 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4563 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4564 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4565 MagickTrue : MagickFalse;
4566 i=(ssize_t) primitive_info[0].coordinates;
4567 if ((((draw_info->linecap == RoundCap) ||
4568 (closed_path != MagickFalse)) &&
4569 (draw_info->linejoin == RoundJoin)) ||
4570 (primitive_info[i].primitive != UndefinedPrimitive))
4572 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4576 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4577 clone_info->stroke_width=0.0;
4578 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4579 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4581 clone_info=DestroyDrawInfo(clone_info);
4582 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4585 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4589 image_view=DestroyCacheView(image_view);
4590 if (image->debug != MagickFalse)
4591 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4592 return(status != 0 ? MagickTrue : MagickFalse);
4596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4600 + D r a w S t r o k e P o l y g o n %
4604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4606 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4607 % the image while respecting the line cap and join attributes.
4609 % The format of the DrawStrokePolygon method is:
4611 % MagickBooleanType DrawStrokePolygon(Image *image,
4612 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4614 % A description of each parameter follows:
4616 % o image: the image.
4618 % o draw_info: the draw info.
4620 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4625 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4626 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4634 for (i=0; i < 4; i++)
4635 linecap[i]=(*primitive_info);
4636 linecap[0].coordinates=4;
4637 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4638 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4639 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4640 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4641 linecap[4].primitive=UndefinedPrimitive;
4642 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4645 static MagickBooleanType DrawStrokePolygon(Image *image,
4646 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4647 ExceptionInfo *exception)
4661 register const PrimitiveInfo
4666 Draw stroked polygon.
4668 if (image->debug != MagickFalse)
4669 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4670 " begin draw-stroke-polygon");
4671 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4672 clone_info->fill=draw_info->stroke;
4673 if (clone_info->fill_pattern != (Image *) NULL)
4674 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4675 if (clone_info->stroke_pattern != (Image *) NULL)
4676 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4677 MagickTrue,exception);
4678 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4679 clone_info->stroke_width=0.0;
4680 clone_info->fill_rule=NonZeroRule;
4682 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4684 stroke_polygon=TraceStrokePolygon(draw_info,p);
4685 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4688 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4689 q=p+p->coordinates-1;
4690 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4691 MagickTrue : MagickFalse;
4692 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4694 DrawRoundLinecap(image,draw_info,p,exception);
4695 DrawRoundLinecap(image,draw_info,q,exception);
4698 clone_info=DestroyDrawInfo(clone_info);
4699 if (image->debug != MagickFalse)
4700 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4701 " end draw-stroke-polygon");
4702 return(status != 0 ? MagickTrue : MagickFalse);
4706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4710 % G e t A f f i n e M a t r i x %
4714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4716 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4719 % The format of the GetAffineMatrix method is:
4721 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4723 % A description of each parameter follows:
4725 % o affine_matrix: the affine matrix.
4728 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4731 assert(affine_matrix != (AffineMatrix *) NULL);
4732 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4733 affine_matrix->sx=1.0;
4734 affine_matrix->sy=1.0;
4738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4742 + G e t D r a w I n f o %
4746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4748 % GetDrawInfo() initializes draw_info to default values from image_info.
4750 % The format of the GetDrawInfo method is:
4752 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4754 % A description of each parameter follows:
4756 % o image_info: the image info..
4758 % o draw_info: the draw info.
4761 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4770 Initialize draw attributes.
4772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4773 assert(draw_info != (DrawInfo *) NULL);
4774 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4775 GetAffineMatrix(&draw_info->affine);
4776 exception=AcquireExceptionInfo();
4777 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4779 (void) QueryColorCompliance("#FFFF",AllCompliance,&draw_info->stroke,
4781 draw_info->stroke_width=1.0;
4782 draw_info->alpha=OpaqueAlpha;
4783 draw_info->fill_rule=EvenOddRule;
4784 draw_info->linecap=ButtCap;
4785 draw_info->linejoin=MiterJoin;
4786 draw_info->miterlimit=10;
4787 draw_info->decorate=NoDecoration;
4788 draw_info->pointsize=12.0;
4789 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4790 draw_info->compose=OverCompositeOp;
4791 draw_info->render=MagickTrue;
4792 draw_info->debug=IsEventLogging();
4793 if (image_info != (ImageInfo *) NULL)
4795 draw_info->stroke_antialias=image_info->antialias;
4796 if (image_info->font != (char *) NULL)
4797 draw_info->font=AcquireString(image_info->font);
4798 if (image_info->density != (char *) NULL)
4799 draw_info->density=AcquireString(image_info->density);
4800 draw_info->text_antialias=image_info->antialias;
4801 if (image_info->pointsize != 0.0)
4802 draw_info->pointsize=image_info->pointsize;
4803 draw_info->border_color=image_info->border_color;
4804 if (image_info->server_name != (char *) NULL)
4805 draw_info->server_name=AcquireString(image_info->server_name);
4806 option=GetImageOption(image_info,"encoding");
4807 if (option != (const char *) NULL)
4808 (void) CloneString(&draw_info->encoding,option);
4809 option=GetImageOption(image_info,"kerning");
4810 if (option != (const char *) NULL)
4811 draw_info->kerning=StringToDouble(option,(char **) NULL);
4812 option=GetImageOption(image_info,"interline-spacing");
4813 if (option != (const char *) NULL)
4814 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4815 option=GetImageOption(image_info,"interword-spacing");
4816 if (option != (const char *) NULL)
4817 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4818 option=GetImageOption(image_info,"direction");
4819 if (option != (const char *) NULL)
4820 draw_info->direction=(DirectionType) ParseCommandOption(
4821 MagickDirectionOptions,MagickFalse,option);
4823 draw_info->direction=UndefinedDirection;
4824 option=GetImageOption(image_info,"fill");
4825 if (option != (const char *) NULL)
4826 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4828 option=GetImageOption(image_info,"stroke");
4829 if (option != (const char *) NULL)
4830 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4832 option=GetImageOption(image_info,"strokewidth");
4833 if (option != (const char *) NULL)
4834 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4835 option=GetImageOption(image_info,"undercolor");
4836 if (option != (const char *) NULL)
4837 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4839 option=GetImageOption(image_info,"gravity");
4840 if (option != (const char *) NULL)
4841 draw_info->gravity=(GravityType) ParseCommandOption(
4842 MagickGravityOptions,MagickFalse,option);
4844 exception=DestroyExceptionInfo(exception);
4845 draw_info->signature=MagickSignature;
4849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4853 + P e r m u t a t e %
4857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4859 % Permutate() returns the permuation of the (n,k).
4861 % The format of the Permutate method is:
4863 % void Permutate(ssize_t n,ssize_t k)
4865 % A description of each parameter follows:
4873 static inline double Permutate(const ssize_t n,const ssize_t k)
4882 for (i=k+1; i <= n; i++)
4884 for (i=1; i <= (n-k); i++)
4890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4894 + T r a c e P r i m i t i v e %
4898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4900 % TracePrimitive is a collection of methods for generating graphic
4901 % primitives such as arcs, ellipses, paths, etc.
4905 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4906 const PointInfo end,const PointInfo degrees)
4912 center.x=0.5*(end.x+start.x);
4913 center.y=0.5*(end.y+start.y);
4914 radii.x=fabs(center.x-start.x);
4915 radii.y=fabs(center.y-start.y);
4916 TraceEllipse(primitive_info,center,radii,degrees);
4919 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4920 const PointInfo end,const PointInfo arc,const double angle,
4921 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4940 register PrimitiveInfo
4949 if ((start.x == end.x) && (start.y == end.y))
4951 TracePoint(primitive_info,end);
4954 radii.x=fabs(arc.x);
4955 radii.y=fabs(arc.y);
4956 if ((radii.x == 0.0) || (radii.y == 0.0))
4958 TraceLine(primitive_info,start,end);
4961 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4962 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4963 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4964 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4965 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4967 if (delta < MagickEpsilon)
4969 TraceLine(primitive_info,start,end);
4974 radii.x*=sqrt((double) delta);
4975 radii.y*=sqrt((double) delta);
4977 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4978 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4979 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4980 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4981 alpha=points[1].x-points[0].x;
4982 beta=points[1].y-points[0].y;
4983 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
4988 factor=sqrt((double) factor);
4989 if (sweep == large_arc)
4992 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4993 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4994 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4995 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4996 if ((theta < 0.0) && (sweep != MagickFalse))
4997 theta+=(double) (2.0*MagickPI);
4999 if ((theta > 0.0) && (sweep == MagickFalse))
5000 theta-=(double) (2.0*MagickPI);
5001 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5004 for (i=0; i < (ssize_t) arc_segments; i++)
5006 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5007 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5008 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5009 sin(fmod((double) beta,DegreesToRadians(360.0)));
5010 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5011 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5012 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5013 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5014 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5015 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5016 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5017 theta/arc_segments),DegreesToRadians(360.0))));
5018 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5019 theta/arc_segments),DegreesToRadians(360.0))));
5020 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5021 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5022 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5023 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5024 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5025 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5026 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5028 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5030 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5032 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5034 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5036 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5038 if (i == (ssize_t) (arc_segments-1))
5043 primitive_info->coordinates=(size_t) (p-primitive_info);
5044 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5046 p->primitive=primitive_info->primitive;
5051 static void TraceBezier(PrimitiveInfo *primitive_info,
5052 const size_t number_coordinates)
5064 register PrimitiveInfo
5076 Allocate coeficients.
5078 quantum=number_coordinates;
5079 for (i=0; i < (ssize_t) number_coordinates; i++)
5081 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5083 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5084 if (alpha > (double) quantum)
5085 quantum=(size_t) alpha;
5086 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5087 if (alpha > (double) quantum)
5088 quantum=(size_t) alpha;
5091 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5092 (double) BezierQuantum);
5093 control_points=quantum*number_coordinates;
5094 coefficients=(double *) AcquireQuantumMemory((size_t)
5095 number_coordinates,sizeof(*coefficients));
5096 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5098 if ((coefficients == (double *) NULL) ||
5099 (points == (PointInfo *) NULL))
5100 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5102 Compute bezier points.
5104 end=primitive_info[number_coordinates-1].point;
5105 for (i=0; i < (ssize_t) number_coordinates; i++)
5106 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5108 for (i=0; i < (ssize_t) control_points; i++)
5113 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5114 for (j=0; j < (ssize_t) number_coordinates; j++)
5116 point.x+=alpha*coefficients[j]*p->point.x;
5117 point.y+=alpha*coefficients[j]*p->point.y;
5118 alpha*=weight/(1.0-weight);
5122 weight+=1.0/control_points;
5125 Bezier curves are just short segmented polys.
5128 for (i=0; i < (ssize_t) control_points; i++)
5130 TracePoint(p,points[i]);
5135 primitive_info->coordinates=(size_t) (p-primitive_info);
5136 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5138 p->primitive=primitive_info->primitive;
5141 points=(PointInfo *) RelinquishMagickMemory(points);
5142 coefficients=(double *) RelinquishMagickMemory(coefficients);
5145 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5146 const PointInfo end)
5157 alpha=end.x-start.x;
5159 radius=hypot((double) alpha,(double) beta);
5160 offset.x=(double) radius;
5161 offset.y=(double) radius;
5164 TraceEllipse(primitive_info,start,offset,degrees);
5167 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5168 const PointInfo stop,const PointInfo degrees)
5179 register PrimitiveInfo
5186 Ellipses are just short segmented polys.
5188 if ((stop.x == 0.0) && (stop.y == 0.0))
5190 TracePoint(primitive_info,start);
5193 delta=2.0/MagickMax(stop.x,stop.y);
5194 step=(double) (MagickPI/8.0);
5195 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5196 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5197 angle.x=DegreesToRadians(degrees.x);
5199 while (y < degrees.x)
5201 angle.y=(double) DegreesToRadians(y);
5202 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5204 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5205 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5206 TracePoint(p,point);
5209 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5210 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5211 TracePoint(p,point);
5213 primitive_info->coordinates=(size_t) (p-primitive_info);
5214 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5216 p->primitive=primitive_info->primitive;
5221 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5222 const PointInfo end)
5224 TracePoint(primitive_info,start);
5225 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5226 (fabs(start.y-end.y) < MagickEpsilon))
5228 primitive_info->primitive=PointPrimitive;
5229 primitive_info->coordinates=1;
5232 TracePoint(primitive_info+1,end);
5233 (primitive_info+1)->primitive=primitive_info->primitive;
5234 primitive_info->coordinates=2;
5237 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5240 token[MaxTextExtent];
5262 register PrimitiveInfo
5279 number_coordinates=0;
5281 primitive_type=primitive_info->primitive;
5283 for (p=path; *p != '\0'; )
5285 while (isspace((int) ((unsigned char) *p)) != 0)
5289 last_attribute=attribute;
5290 attribute=(int) (*p++);
5311 GetMagickToken(p,&p,token);
5313 GetMagickToken(p,&p,token);
5314 arc.x=StringToDouble(token,(char **) NULL);
5315 GetMagickToken(p,&p,token);
5317 GetMagickToken(p,&p,token);
5318 arc.y=StringToDouble(token,(char **) NULL);
5319 GetMagickToken(p,&p,token);
5321 GetMagickToken(p,&p,token);
5322 angle=StringToDouble(token,(char **) NULL);
5323 GetMagickToken(p,&p,token);
5325 GetMagickToken(p,&p,token);
5326 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5327 GetMagickToken(p,&p,token);
5329 GetMagickToken(p,&p,token);
5330 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5331 GetMagickToken(p,&p,token);
5333 GetMagickToken(p,&p,token);
5334 x=StringToDouble(token,(char **) NULL);
5335 GetMagickToken(p,&p,token);
5337 GetMagickToken(p,&p,token);
5338 y=StringToDouble(token,(char **) NULL);
5339 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5340 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5341 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5344 while (isspace((int) ((unsigned char) *p)) != 0)
5348 } while (IsPoint(p) != MagickFalse);
5355 Compute bezier points.
5360 for (i=1; i < 4; i++)
5362 GetMagickToken(p,&p,token);
5364 GetMagickToken(p,&p,token);
5365 x=StringToDouble(token,(char **) NULL);
5366 GetMagickToken(p,&p,token);
5368 GetMagickToken(p,&p,token);
5369 y=StringToDouble(token,(char **) NULL);
5370 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5371 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5374 for (i=0; i < 4; i++)
5375 (q+i)->point=points[i];
5379 } while (IsPoint(p) != MagickFalse);
5387 GetMagickToken(p,&p,token);
5389 GetMagickToken(p,&p,token);
5390 x=StringToDouble(token,(char **) NULL);
5391 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5392 TracePoint(q,point);
5394 } while (IsPoint(p) != MagickFalse);
5402 GetMagickToken(p,&p,token);
5404 GetMagickToken(p,&p,token);
5405 x=StringToDouble(token,(char **) NULL);
5406 GetMagickToken(p,&p,token);
5408 GetMagickToken(p,&p,token);
5409 y=StringToDouble(token,(char **) NULL);
5410 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5411 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5412 TracePoint(q,point);
5414 } while (IsPoint(p) != MagickFalse);
5420 if (q != primitive_info)
5422 primitive_info->coordinates=(size_t) (q-primitive_info);
5423 number_coordinates+=primitive_info->coordinates;
5429 GetMagickToken(p,&p,token);
5431 GetMagickToken(p,&p,token);
5432 x=StringToDouble(token,(char **) NULL);
5433 GetMagickToken(p,&p,token);
5435 GetMagickToken(p,&p,token);
5436 y=StringToDouble(token,(char **) NULL);
5437 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5438 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5442 TracePoint(q,point);
5444 if ((i != 0) && (attribute == (int) 'M'))
5446 TracePoint(q,point);
5449 } while (IsPoint(p) != MagickFalse);
5456 Compute bezier points.
5461 for (i=1; i < 3; i++)
5463 GetMagickToken(p,&p,token);
5465 GetMagickToken(p,&p,token);
5466 x=StringToDouble(token,(char **) NULL);
5467 GetMagickToken(p,&p,token);
5469 GetMagickToken(p,&p,token);
5470 y=StringToDouble(token,(char **) NULL);
5473 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5474 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5477 for (i=0; i < 3; i++)
5478 (q+i)->point=points[i];
5482 } while (IsPoint(p) != MagickFalse);
5489 Compute bezier points.
5493 points[0]=points[3];
5494 points[1].x=2.0*points[3].x-points[2].x;
5495 points[1].y=2.0*points[3].y-points[2].y;
5496 for (i=2; i < 4; i++)
5498 GetMagickToken(p,&p,token);
5500 GetMagickToken(p,&p,token);
5501 x=StringToDouble(token,(char **) NULL);
5502 GetMagickToken(p,&p,token);
5504 GetMagickToken(p,&p,token);
5505 y=StringToDouble(token,(char **) NULL);
5508 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5509 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5512 if (strchr("CcSs",last_attribute) == (char *) NULL)
5514 points[0]=points[2];
5515 points[1]=points[3];
5517 for (i=0; i < 4; i++)
5518 (q+i)->point=points[i];
5522 } while (IsPoint(p) != MagickFalse);
5529 Compute bezier points.
5533 points[0]=points[2];
5534 points[1].x=2.0*points[2].x-points[1].x;
5535 points[1].y=2.0*points[2].y-points[1].y;
5536 for (i=2; i < 3; i++)
5538 GetMagickToken(p,&p,token);
5540 GetMagickToken(p,&p,token);
5541 x=StringToDouble(token,(char **) NULL);
5542 GetMagickToken(p,&p,token);
5544 GetMagickToken(p,&p,token);
5545 y=StringToDouble(token,(char **) NULL);
5546 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5547 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5550 if (strchr("QqTt",last_attribute) == (char *) NULL)
5552 points[0]=points[2];
5553 points[1]=points[3];
5555 for (i=0; i < 3; i++)
5556 (q+i)->point=points[i];
5560 } while (IsPoint(p) != MagickFalse);
5568 GetMagickToken(p,&p,token);
5570 GetMagickToken(p,&p,token);
5571 y=StringToDouble(token,(char **) NULL);
5572 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5573 TracePoint(q,point);
5575 } while (IsPoint(p) != MagickFalse);
5582 TracePoint(q,point);
5584 primitive_info->coordinates=(size_t) (q-primitive_info);
5585 number_coordinates+=primitive_info->coordinates;
5592 if (isalpha((int) ((unsigned char) attribute)) != 0)
5593 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5599 primitive_info->coordinates=(size_t) (q-primitive_info);
5600 number_coordinates+=primitive_info->coordinates;
5601 for (i=0; i < (ssize_t) number_coordinates; i++)
5604 q->primitive=primitive_type;
5606 q->method=FillToBorderMethod;
5609 return(number_coordinates);
5612 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5613 const PointInfo end)
5618 register PrimitiveInfo
5625 TracePoint(p,start);
5629 TracePoint(p,point);
5635 TracePoint(p,point);
5637 TracePoint(p,start);
5639 primitive_info->coordinates=(size_t) (p-primitive_info);
5640 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5642 p->primitive=primitive_info->primitive;
5647 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5648 const PointInfo start,const PointInfo end,PointInfo arc)
5655 register PrimitiveInfo
5662 offset.x=fabs(end.x-start.x);
5663 offset.y=fabs(end.y-start.y);
5664 if (arc.x > (0.5*offset.x))
5666 if (arc.y > (0.5*offset.y))
5668 point.x=start.x+offset.x-arc.x;
5669 point.y=start.y+arc.y;
5672 TraceEllipse(p,point,arc,degrees);
5674 point.x=start.x+offset.x-arc.x;
5675 point.y=start.y+offset.y-arc.y;
5678 TraceEllipse(p,point,arc,degrees);
5680 point.x=start.x+arc.x;
5681 point.y=start.y+offset.y-arc.y;
5684 TraceEllipse(p,point,arc,degrees);
5686 point.x=start.x+arc.x;
5687 point.y=start.y+arc.y;
5690 TraceEllipse(p,point,arc,degrees);
5692 TracePoint(p,primitive_info->point);
5694 primitive_info->coordinates=(size_t) (p-primitive_info);
5695 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5697 p->primitive=primitive_info->primitive;
5702 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5703 const size_t number_vertices,const double offset)
5720 for (i=1; i < (ssize_t) number_vertices; i++)
5722 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5723 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5724 if ((fabs((double) dx) >= MagickEpsilon) ||
5725 (fabs((double) dy) >= MagickEpsilon))
5728 if (i == (ssize_t) number_vertices)
5729 i=(ssize_t) number_vertices-1L;
5730 distance=hypot((double) dx,(double) dy);
5731 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5732 dx*(distance+offset)/distance);
5733 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5734 dy*(distance+offset)/distance);
5735 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5737 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5738 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5739 if ((fabs((double) dx) >= MagickEpsilon) ||
5740 (fabs((double) dy) >= MagickEpsilon))
5743 distance=hypot((double) dx,(double) dy);
5744 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5745 dx*(distance+offset)/distance);
5746 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5747 dy*(distance+offset)/distance);
5750 static inline double DrawEpsilonReciprocal(const double x)
5752 #define DrawEpsilon ((double) 1.0e-10)
5754 double sign = x < (double) 0.0 ? (double) -1.0 :
5756 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5757 (double) 1.0/DrawEpsilon));
5760 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5761 const PrimitiveInfo *primitive_info)
5763 typedef struct _LineSegment
5815 number_vertices=primitive_info->coordinates;
5816 max_strokes=2*number_vertices+6*BezierQuantum+360;
5817 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5819 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5821 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5822 number_vertices+2UL,sizeof(*polygon_primitive));
5823 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5824 (polygon_primitive == (PrimitiveInfo *) NULL))
5825 return((PrimitiveInfo *) NULL);
5826 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5827 number_vertices*sizeof(*polygon_primitive));
5829 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5830 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5831 MagickTrue : MagickFalse;
5832 if ((draw_info->linejoin == RoundJoin) ||
5833 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5835 polygon_primitive[number_vertices]=primitive_info[1];
5838 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5840 Compute the slope for the first line segment, p.
5844 for (n=1; n < (ssize_t) number_vertices; n++)
5846 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5847 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5848 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5851 if (n == (ssize_t) number_vertices)
5852 n=(ssize_t) number_vertices-1L;
5853 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5854 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5855 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5856 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5858 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5859 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5860 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5861 offset.y=(double) (offset.x*inverse_slope.p);
5862 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5864 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5865 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5866 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5867 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5868 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5869 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5870 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5871 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5875 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5876 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5877 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5878 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5879 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5880 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5881 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5882 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5885 Create strokes for the line join attribute: bevel, miter, round.
5889 path_q[p++]=box_q[0];
5890 path_p[q++]=box_p[0];
5891 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5894 Compute the slope for this line segment, q.
5896 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5897 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5898 dot_product=dx.q*dx.q+dy.q*dy.q;
5899 if (dot_product < 0.25)
5901 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5902 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5903 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5904 offset.y=(double) (offset.x*inverse_slope.q);
5905 dot_product=dy.q*offset.x-dx.q*offset.y;
5906 if (dot_product > 0.0)
5908 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5909 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5910 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5911 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5912 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5913 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5914 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5915 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5919 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5920 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5921 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5922 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5923 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5924 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5925 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5926 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5928 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5935 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5936 box_p[3].y)/(slope.p-slope.q));
5937 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5938 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5939 box_q[3].y)/(slope.p-slope.q));
5940 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5942 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5944 max_strokes+=6*BezierQuantum+360;
5945 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5947 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5949 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5951 polygon_primitive=(PrimitiveInfo *)
5952 RelinquishMagickMemory(polygon_primitive);
5953 return((PrimitiveInfo *) NULL);
5956 dot_product=dx.q*dy.p-dx.p*dy.q;
5957 if (dot_product <= 0.0)
5958 switch (draw_info->linejoin)
5962 path_q[q++]=box_q[1];
5963 path_q[q++]=box_q[2];
5964 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5965 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5966 if (dot_product <= miterlimit)
5967 path_p[p++]=box_p[4];
5970 path_p[p++]=box_p[1];
5971 path_p[p++]=box_p[2];
5977 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5978 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5979 if (dot_product <= miterlimit)
5981 path_q[q++]=box_q[4];
5982 path_p[p++]=box_p[4];
5986 path_q[q++]=box_q[1];
5987 path_q[q++]=box_q[2];
5988 path_p[p++]=box_p[1];
5989 path_p[p++]=box_p[2];
5995 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5996 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5997 if (dot_product <= miterlimit)
5998 path_p[p++]=box_p[4];
6001 path_p[p++]=box_p[1];
6002 path_p[p++]=box_p[2];
6004 center=polygon_primitive[n].point;
6005 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6006 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6007 if (theta.q < theta.p)
6008 theta.q+=(double) (2.0*MagickPI);
6009 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6010 (2.0*sqrt((double) (1.0/mid)))));
6011 path_q[q].x=box_q[1].x;
6012 path_q[q].y=box_q[1].y;
6014 for (j=1; j < (ssize_t) arc_segments; j++)
6016 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6017 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6018 (theta.p+delta_theta),DegreesToRadians(360.0))));
6019 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6020 (theta.p+delta_theta),DegreesToRadians(360.0))));
6023 path_q[q++]=box_q[2];
6030 switch (draw_info->linejoin)
6034 path_p[p++]=box_p[1];
6035 path_p[p++]=box_p[2];
6036 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6037 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6038 if (dot_product <= miterlimit)
6039 path_q[q++]=box_q[4];
6042 path_q[q++]=box_q[1];
6043 path_q[q++]=box_q[2];
6049 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6050 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6051 if (dot_product <= miterlimit)
6053 path_q[q++]=box_q[4];
6054 path_p[p++]=box_p[4];
6058 path_q[q++]=box_q[1];
6059 path_q[q++]=box_q[2];
6060 path_p[p++]=box_p[1];
6061 path_p[p++]=box_p[2];
6067 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6068 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6069 if (dot_product <= miterlimit)
6070 path_q[q++]=box_q[4];
6073 path_q[q++]=box_q[1];
6074 path_q[q++]=box_q[2];
6076 center=polygon_primitive[n].point;
6077 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6078 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6079 if (theta.p < theta.q)
6080 theta.p+=(double) (2.0*MagickPI);
6081 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6082 (2.0*sqrt((double) (1.0/mid)))));
6083 path_p[p++]=box_p[1];
6084 for (j=1; j < (ssize_t) arc_segments; j++)
6086 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6087 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6088 (theta.p+delta_theta),DegreesToRadians(360.0))));
6089 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6090 (theta.p+delta_theta),DegreesToRadians(360.0))));
6093 path_p[p++]=box_p[2];
6100 inverse_slope.p=inverse_slope.q;
6109 path_p[p++]=box_p[1];
6110 path_q[q++]=box_q[1];
6112 Trace stroked polygon.
6114 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6115 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6116 if (stroke_polygon != (PrimitiveInfo *) NULL)
6118 for (i=0; i < (ssize_t) p; i++)
6120 stroke_polygon[i]=polygon_primitive[0];
6121 stroke_polygon[i].point=path_p[i];
6123 if (closed_path != MagickFalse)
6125 stroke_polygon[i]=polygon_primitive[0];
6126 stroke_polygon[i].point=stroke_polygon[0].point;
6129 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6131 stroke_polygon[i]=polygon_primitive[0];
6132 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6134 if (closed_path != MagickFalse)
6136 stroke_polygon[i]=polygon_primitive[0];
6137 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6140 stroke_polygon[i]=polygon_primitive[0];
6141 stroke_polygon[i].point=stroke_polygon[0].point;
6143 stroke_polygon[i].primitive=UndefinedPrimitive;
6144 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6146 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6147 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6148 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6149 return(stroke_polygon);