2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2017 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 % https://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-private.h"
54 #include "MagickCore/cache-view.h"
55 #include "MagickCore/channel.h"
56 #include "MagickCore/color.h"
57 #include "MagickCore/colorspace-private.h"
58 #include "MagickCore/composite.h"
59 #include "MagickCore/composite-private.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/draw-private.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
65 #include "MagickCore/exception-private.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/image-private.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/log.h"
71 #include "MagickCore/monitor.h"
72 #include "MagickCore/monitor-private.h"
73 #include "MagickCore/option.h"
74 #include "MagickCore/paint.h"
75 #include "MagickCore/pixel-accessor.h"
76 #include "MagickCore/pixel-private.h"
77 #include "MagickCore/property.h"
78 #include "MagickCore/resample.h"
79 #include "MagickCore/resample-private.h"
80 #include "MagickCore/resource_.h"
81 #include "MagickCore/string_.h"
82 #include "MagickCore/string-private.h"
83 #include "MagickCore/thread-private.h"
84 #include "MagickCore/token.h"
85 #include "MagickCore/transform-private.h"
86 #include "MagickCore/utility.h"
91 #define BezierQuantum 200
92 #define DrawEpsilon (1.0e-10)
98 typedef struct _EdgeInfo
122 typedef struct _ElementInfo
132 typedef struct _PolygonInfo
150 typedef struct _PathInfo
160 Forward declarations.
162 static MagickBooleanType
163 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
167 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
170 TracePath(PrimitiveInfo *,const char *);
173 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
174 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
175 const double,const MagickBooleanType,const MagickBooleanType),
176 TraceBezier(PrimitiveInfo *,const size_t),
177 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,
180 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
181 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
182 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
184 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % A c q u i r e D r a w I n f o %
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
199 % The format of the AcquireDrawInfo method is:
201 % DrawInfo *AcquireDrawInfo(void)
204 MagickExport DrawInfo *AcquireDrawInfo(void)
209 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
210 if (draw_info == (DrawInfo *) NULL)
211 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
212 GetDrawInfo((ImageInfo *) NULL,draw_info);
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 % C l o n e D r a w I n f o %
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
228 % is specified, a new DrawInfo structure is created initialized to default
231 % The format of the CloneDrawInfo method is:
233 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
234 % const DrawInfo *draw_info)
236 % A description of each parameter follows:
238 % o image_info: the image info.
240 % o draw_info: the draw info.
243 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
244 const DrawInfo *draw_info)
252 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
253 if (clone_info == (DrawInfo *) NULL)
254 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
255 GetDrawInfo(image_info,clone_info);
256 if (draw_info == (DrawInfo *) NULL)
258 exception=AcquireExceptionInfo();
259 if (clone_info->primitive != (char *) NULL)
260 (void) CloneString(&clone_info->primitive,draw_info->primitive);
261 if (draw_info->geometry != (char *) NULL)
262 (void) CloneString(&clone_info->geometry,draw_info->geometry);
263 clone_info->viewbox=draw_info->viewbox;
264 clone_info->affine=draw_info->affine;
265 clone_info->gravity=draw_info->gravity;
266 clone_info->fill=draw_info->fill;
267 clone_info->stroke=draw_info->stroke;
268 clone_info->stroke_width=draw_info->stroke_width;
269 if (draw_info->fill_pattern != (Image *) NULL)
270 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
272 if (draw_info->stroke_pattern != (Image *) NULL)
273 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
274 MagickTrue,exception);
275 clone_info->stroke_antialias=draw_info->stroke_antialias;
276 clone_info->text_antialias=draw_info->text_antialias;
277 clone_info->fill_rule=draw_info->fill_rule;
278 clone_info->linecap=draw_info->linecap;
279 clone_info->linejoin=draw_info->linejoin;
280 clone_info->miterlimit=draw_info->miterlimit;
281 clone_info->dash_offset=draw_info->dash_offset;
282 clone_info->decorate=draw_info->decorate;
283 clone_info->compose=draw_info->compose;
284 if (draw_info->text != (char *) NULL)
285 (void) CloneString(&clone_info->text,draw_info->text);
286 if (draw_info->font != (char *) NULL)
287 (void) CloneString(&clone_info->font,draw_info->font);
288 if (draw_info->metrics != (char *) NULL)
289 (void) CloneString(&clone_info->metrics,draw_info->metrics);
290 if (draw_info->family != (char *) NULL)
291 (void) CloneString(&clone_info->family,draw_info->family);
292 clone_info->style=draw_info->style;
293 clone_info->stretch=draw_info->stretch;
294 clone_info->weight=draw_info->weight;
295 if (draw_info->encoding != (char *) NULL)
296 (void) CloneString(&clone_info->encoding,draw_info->encoding);
297 clone_info->pointsize=draw_info->pointsize;
298 clone_info->kerning=draw_info->kerning;
299 clone_info->interline_spacing=draw_info->interline_spacing;
300 clone_info->interword_spacing=draw_info->interword_spacing;
301 clone_info->direction=draw_info->direction;
302 if (draw_info->density != (char *) NULL)
303 (void) CloneString(&clone_info->density,draw_info->density);
304 clone_info->align=draw_info->align;
305 clone_info->undercolor=draw_info->undercolor;
306 clone_info->border_color=draw_info->border_color;
307 if (draw_info->server_name != (char *) NULL)
308 (void) CloneString(&clone_info->server_name,draw_info->server_name);
309 if (draw_info->dash_pattern != (double *) NULL)
314 for (x=0; fabs(draw_info->dash_pattern[x]) >= DrawEpsilon; x++) ;
315 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
316 sizeof(*clone_info->dash_pattern));
317 if (clone_info->dash_pattern == (double *) NULL)
318 ThrowFatalException(ResourceLimitFatalError,
319 "UnableToAllocateDashPattern");
320 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
321 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
323 clone_info->gradient=draw_info->gradient;
324 if (draw_info->gradient.stops != (StopInfo *) NULL)
329 number_stops=clone_info->gradient.number_stops;
330 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
331 number_stops,sizeof(*clone_info->gradient.stops));
332 if (clone_info->gradient.stops == (StopInfo *) NULL)
333 ThrowFatalException(ResourceLimitFatalError,
334 "UnableToAllocateDashPattern");
335 (void) CopyMagickMemory(clone_info->gradient.stops,
336 draw_info->gradient.stops,(size_t) number_stops*
337 sizeof(*clone_info->gradient.stops));
339 if (draw_info->clip_mask != (char *) NULL)
340 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
341 clone_info->bounds=draw_info->bounds;
342 clone_info->clip_units=draw_info->clip_units;
343 clone_info->render=draw_info->render;
344 clone_info->fill_alpha=draw_info->fill_alpha;
345 clone_info->stroke_alpha=draw_info->stroke_alpha;
346 clone_info->element_reference=draw_info->element_reference;
347 clone_info->debug=IsEventLogging();
348 exception=DestroyExceptionInfo(exception);
353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 + C o n v e r t P a t h T o P o l y g o n %
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363 % ConvertPathToPolygon() converts a path to the more efficient sorted
366 % The format of the ConvertPathToPolygon method is:
368 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
369 % const PathInfo *path_info)
371 % A description of each parameter follows:
373 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
374 % rendering form of type PolygonInfo.
376 % o draw_info: Specifies a pointer to an DrawInfo structure.
378 % o path_info: Specifies a pointer to an PathInfo structure.
383 #if defined(__cplusplus) || defined(c_plusplus)
387 static int CompareEdges(const void *x,const void *y)
389 register const EdgeInfo
396 p=(const EdgeInfo *) x;
397 q=(const EdgeInfo *) y;
398 if ((p->points[0].y-DrawEpsilon) > q->points[0].y)
400 if ((p->points[0].y+DrawEpsilon) < q->points[0].y)
402 if ((p->points[0].x-DrawEpsilon) > q->points[0].x)
404 if ((p->points[0].x+DrawEpsilon) < q->points[0].x)
406 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
407 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
412 #if defined(__cplusplus) || defined(c_plusplus)
416 static void LogPolygonInfo(const PolygonInfo *polygon_info)
425 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
426 p=polygon_info->edges;
427 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
429 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
431 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
432 p->direction != MagickFalse ? "down" : "up");
433 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
434 p->ghostline != MagickFalse ? "transparent" : "opaque");
435 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
436 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
437 p->bounds.x2,p->bounds.y2);
438 for (j=0; j < (ssize_t) p->number_points; j++)
439 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
440 p->points[j].x,p->points[j].y);
443 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
446 static void ReversePoints(PointInfo *points,const size_t number_points)
454 for (i=0; i < (ssize_t) (number_points >> 1); i++)
457 points[i]=points[number_points-(i+1)];
458 points[number_points-(i+1)]=point;
462 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
491 Convert a path to the more efficient sorted rendering form.
493 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
494 if (polygon_info == (PolygonInfo *) NULL)
495 return((PolygonInfo *) NULL);
497 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
498 sizeof(*polygon_info->edges));
499 if (polygon_info->edges == (EdgeInfo *) NULL)
500 return((PolygonInfo *) NULL);
501 (void) ResetMagickMemory(polygon_info->edges,0,number_edges*
502 sizeof(*polygon_info->edges));
505 ghostline=MagickFalse;
508 points=(PointInfo *) NULL;
509 (void) ResetMagickMemory(&point,0,sizeof(point));
510 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
511 for (i=0; path_info[i].code != EndCode; i++)
513 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
514 (path_info[i].code == GhostlineCode))
519 if ((points != (PointInfo *) NULL) && (n >= 2))
521 if (edge == number_edges)
524 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
525 polygon_info->edges,(size_t) number_edges,
526 sizeof(*polygon_info->edges));
527 if (polygon_info->edges == (EdgeInfo *) NULL)
528 return((PolygonInfo *) NULL);
530 polygon_info->edges[edge].number_points=(size_t) n;
531 polygon_info->edges[edge].scanline=(-1.0);
532 polygon_info->edges[edge].highwater=0;
533 polygon_info->edges[edge].ghostline=ghostline;
534 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
536 ReversePoints(points,(size_t) n);
537 polygon_info->edges[edge].points=points;
538 polygon_info->edges[edge].bounds=bounds;
539 polygon_info->edges[edge].bounds.y1=points[0].y;
540 polygon_info->edges[edge].bounds.y2=points[n-1].y;
541 points=(PointInfo *) NULL;
542 ghostline=MagickFalse;
545 if (points == (PointInfo *) NULL)
548 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
550 if (points == (PointInfo *) NULL)
551 return((PolygonInfo *) NULL);
553 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
554 point=path_info[i].point;
565 next_direction=((path_info[i].point.y > point.y) ||
566 ((fabs(path_info[i].point.y-point.y) < DrawEpsilon) &&
567 (path_info[i].point.x > point.x))) ? 1 : -1;
568 if ((points != (PointInfo *) NULL) && (direction != 0) &&
569 (direction != next_direction))
575 if (edge == number_edges)
578 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
579 polygon_info->edges,(size_t) number_edges,
580 sizeof(*polygon_info->edges));
581 if (polygon_info->edges == (EdgeInfo *) NULL)
582 return((PolygonInfo *) NULL);
584 polygon_info->edges[edge].number_points=(size_t) n;
585 polygon_info->edges[edge].scanline=(-1.0);
586 polygon_info->edges[edge].highwater=0;
587 polygon_info->edges[edge].ghostline=ghostline;
588 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
590 ReversePoints(points,(size_t) n);
591 polygon_info->edges[edge].points=points;
592 polygon_info->edges[edge].bounds=bounds;
593 polygon_info->edges[edge].bounds.y1=points[0].y;
594 polygon_info->edges[edge].bounds.y2=points[n-1].y;
596 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
598 if (points == (PointInfo *) NULL)
599 return((PolygonInfo *) NULL);
601 ghostline=MagickFalse;
607 direction=next_direction;
608 if (points == (PointInfo *) NULL)
610 if (n == (ssize_t) number_points)
613 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
615 if (points == (PointInfo *) NULL)
616 return((PolygonInfo *) NULL);
618 point=path_info[i].point;
620 if (point.x < bounds.x1)
622 if (point.x > bounds.x2)
626 if (points != (PointInfo *) NULL)
629 points=(PointInfo *) RelinquishMagickMemory(points);
632 if (edge == number_edges)
635 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
636 polygon_info->edges,(size_t) number_edges,
637 sizeof(*polygon_info->edges));
638 if (polygon_info->edges == (EdgeInfo *) NULL)
639 return((PolygonInfo *) NULL);
641 polygon_info->edges[edge].number_points=(size_t) n;
642 polygon_info->edges[edge].scanline=(-1.0);
643 polygon_info->edges[edge].highwater=0;
644 polygon_info->edges[edge].ghostline=ghostline;
645 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
647 ReversePoints(points,(size_t) n);
648 polygon_info->edges[edge].points=points;
649 polygon_info->edges[edge].bounds=bounds;
650 polygon_info->edges[edge].bounds.y1=points[0].y;
651 polygon_info->edges[edge].bounds.y2=points[n-1].y;
652 ghostline=MagickFalse;
656 polygon_info->number_edges=edge;
657 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
658 sizeof(*polygon_info->edges),CompareEdges);
659 if (IsEventLogging() != MagickFalse)
660 LogPolygonInfo(polygon_info);
661 return(polygon_info);
665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 + C o n v e r t P r i m i t i v e T o P a t h %
673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
678 % The format of the ConvertPrimitiveToPath method is:
680 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
681 % const PrimitiveInfo *primitive_info)
683 % A description of each parameter follows:
685 % o Method ConvertPrimitiveToPath returns a vector path structure of type
688 % o draw_info: a structure of type DrawInfo.
690 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
695 static void LogPathInfo(const PathInfo *path_info)
697 register const PathInfo
700 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
701 for (p=path_info; p->code != EndCode; p++)
702 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
703 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
704 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
705 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
707 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
710 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
731 Converts a PrimitiveInfo structure into a vector path structure.
733 switch (primitive_info->primitive)
740 return((PathInfo *) NULL);
744 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
745 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
747 if (path_info == (PathInfo *) NULL)
748 return((PathInfo *) NULL);
756 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
759 if (coordinates <= 0)
761 coordinates=(ssize_t) primitive_info[i].coordinates;
762 p=primitive_info[i].point;
768 Eliminate duplicate points.
770 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= DrawEpsilon) ||
771 (fabs(q.y-primitive_info[i].point.y) >= DrawEpsilon))
773 path_info[n].code=code;
774 path_info[n].point=primitive_info[i].point;
775 q=primitive_info[i].point;
780 if ((fabs(p.x-primitive_info[i].point.x) < DrawEpsilon) &&
781 (fabs(p.y-primitive_info[i].point.y) < DrawEpsilon))
784 Mark the p point as open if it does not match the q.
786 path_info[start].code=OpenCode;
787 path_info[n].code=GhostlineCode;
788 path_info[n].point=primitive_info[i].point;
790 path_info[n].code=LineToCode;
791 path_info[n].point=p;
794 path_info[n].code=EndCode;
795 path_info[n].point.x=0.0;
796 path_info[n].point.y=0.0;
797 if (IsEventLogging() != MagickFalse)
798 LogPathInfo(path_info);
803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
807 % D e s t r o y D r a w I n f o %
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
816 % The format of the DestroyDrawInfo method is:
818 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
820 % A description of each parameter follows:
822 % o draw_info: the draw info.
825 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
827 if (draw_info->debug != MagickFalse)
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
829 assert(draw_info != (DrawInfo *) NULL);
830 assert(draw_info->signature == MagickCoreSignature);
831 if (draw_info->primitive != (char *) NULL)
832 draw_info->primitive=DestroyString(draw_info->primitive);
833 if (draw_info->text != (char *) NULL)
834 draw_info->text=DestroyString(draw_info->text);
835 if (draw_info->geometry != (char *) NULL)
836 draw_info->geometry=DestroyString(draw_info->geometry);
837 if (draw_info->fill_pattern != (Image *) NULL)
838 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
839 if (draw_info->stroke_pattern != (Image *) NULL)
840 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
841 if (draw_info->font != (char *) NULL)
842 draw_info->font=DestroyString(draw_info->font);
843 if (draw_info->metrics != (char *) NULL)
844 draw_info->metrics=DestroyString(draw_info->metrics);
845 if (draw_info->family != (char *) NULL)
846 draw_info->family=DestroyString(draw_info->family);
847 if (draw_info->encoding != (char *) NULL)
848 draw_info->encoding=DestroyString(draw_info->encoding);
849 if (draw_info->density != (char *) NULL)
850 draw_info->density=DestroyString(draw_info->density);
851 if (draw_info->server_name != (char *) NULL)
852 draw_info->server_name=(char *)
853 RelinquishMagickMemory(draw_info->server_name);
854 if (draw_info->dash_pattern != (double *) NULL)
855 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
856 draw_info->dash_pattern);
857 if (draw_info->gradient.stops != (StopInfo *) NULL)
858 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
859 draw_info->gradient.stops);
860 if (draw_info->clip_mask != (char *) NULL)
861 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
862 draw_info->signature=(~MagickCoreSignature);
863 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
872 + D e s t r o y E d g e %
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % DestroyEdge() destroys the specified polygon edge.
880 % The format of the DestroyEdge method is:
882 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
884 % A description of each parameter follows:
886 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
888 % o edge: the polygon edge number to destroy.
891 static size_t DestroyEdge(PolygonInfo *polygon_info,
894 assert(edge < polygon_info->number_edges);
895 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
896 polygon_info->edges[edge].points);
897 polygon_info->number_edges--;
898 if (edge < polygon_info->number_edges)
899 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
900 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
901 return(polygon_info->number_edges);
905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 + D e s t r o y P o l y g o n I n f o %
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
917 % The format of the DestroyPolygonInfo method is:
919 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
921 % A description of each parameter follows:
923 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
926 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
931 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
932 polygon_info->edges[i].points=(PointInfo *)
933 RelinquishMagickMemory(polygon_info->edges[i].points);
934 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
935 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 % D r a w A f f i n e I m a g e %
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % DrawAffineImage() composites the source over the destination image as
950 % dictated by the affine transform.
952 % The format of the DrawAffineImage method is:
954 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
955 % const AffineMatrix *affine,ExceptionInfo *exception)
957 % A description of each parameter follows:
959 % o image: the image.
961 % o source: the source image.
963 % o affine: the affine transform.
965 % o exception: return any errors or warnings in this structure.
969 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
970 const double y,const SegmentInfo *edge)
983 Determine left and right edges.
985 inverse_edge.x1=edge->x1;
986 inverse_edge.y1=edge->y1;
987 inverse_edge.x2=edge->x2;
988 inverse_edge.y2=edge->y2;
989 z=affine->ry*y+affine->tx;
990 if (affine->sx >= DrawEpsilon)
992 intercept=(-z/affine->sx);
994 if (x > inverse_edge.x1)
996 intercept=(-z+(double) image->columns)/affine->sx;
998 if (x < inverse_edge.x2)
1002 if (affine->sx < -DrawEpsilon)
1004 intercept=(-z+(double) image->columns)/affine->sx;
1006 if (x > inverse_edge.x1)
1008 intercept=(-z/affine->sx);
1010 if (x < inverse_edge.x2)
1014 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1016 inverse_edge.x2=edge->x1;
1017 return(inverse_edge);
1020 Determine top and bottom edges.
1022 z=affine->sy*y+affine->ty;
1023 if (affine->rx >= DrawEpsilon)
1025 intercept=(-z/affine->rx);
1027 if (x > inverse_edge.x1)
1029 intercept=(-z+(double) image->rows)/affine->rx;
1031 if (x < inverse_edge.x2)
1035 if (affine->rx < -DrawEpsilon)
1037 intercept=(-z+(double) image->rows)/affine->rx;
1039 if (x > inverse_edge.x1)
1041 intercept=(-z/affine->rx);
1043 if (x < inverse_edge.x2)
1047 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1049 inverse_edge.x2=edge->x2;
1050 return(inverse_edge);
1052 return(inverse_edge);
1055 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1063 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1065 inverse_affine.sx=determinant*affine->sy;
1066 inverse_affine.rx=determinant*(-affine->rx);
1067 inverse_affine.ry=determinant*(-affine->ry);
1068 inverse_affine.sy=determinant*affine->sx;
1069 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1071 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1073 return(inverse_affine);
1076 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1077 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1109 Determine bounding box.
1111 assert(image != (Image *) NULL);
1112 assert(image->signature == MagickCoreSignature);
1113 if (image->debug != MagickFalse)
1114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1115 assert(source != (const Image *) NULL);
1116 assert(source->signature == MagickCoreSignature);
1117 assert(affine != (AffineMatrix *) NULL);
1120 extent[1].x=(double) source->columns-1.0;
1122 extent[2].x=(double) source->columns-1.0;
1123 extent[2].y=(double) source->rows-1.0;
1125 extent[3].y=(double) source->rows-1.0;
1126 for (i=0; i < 4; i++)
1132 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1133 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1137 for (i=1; i < 4; i++)
1139 if (min.x > extent[i].x)
1141 if (min.y > extent[i].y)
1143 if (max.x < extent[i].x)
1145 if (max.y < extent[i].y)
1149 Affine transform image.
1151 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1152 return(MagickFalse);
1154 edge.x1=MagickMax(min.x,0.0);
1155 edge.y1=MagickMax(min.y,0.0);
1156 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1157 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1158 inverse_affine=InverseAffineMatrix(affine);
1159 GetPixelInfo(image,&zero);
1160 start=(ssize_t) ceil(edge.y1-0.5);
1161 stop=(ssize_t) floor(edge.y2+0.5);
1162 source_view=AcquireVirtualCacheView(source,exception);
1163 image_view=AcquireAuthenticCacheView(image,exception);
1164 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1165 #pragma omp parallel for schedule(static,4) shared(status) \
1166 magick_threads(source,image,1,1)
1168 for (y=start; y <= stop; y++)
1189 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1190 if (inverse_edge.x2 < inverse_edge.x1)
1192 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1193 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1195 if (q == (Quantum *) NULL)
1200 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1202 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1204 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1206 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1207 point.x,point.y,&pixel,exception);
1208 GetPixelInfoPixel(image,q,&composite);
1209 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1211 SetPixelViaPixelInfo(image,&composite,q);
1213 q+=GetPixelChannels(image);
1215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1218 source_view=DestroyCacheView(source_view);
1219 image_view=DestroyCacheView(image_view);
1224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228 + D r a w B o u n d i n g R e c t a n g l e s %
1232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1234 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1235 % is only useful for developers debugging the rendering algorithm.
1237 % The format of the DrawBoundingRectangles method is:
1239 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1240 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1242 % A description of each parameter follows:
1244 % o image: the image.
1246 % o draw_info: the draw info.
1248 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1250 % o exception: return any errors or warnings in this structure.
1253 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1254 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1279 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1280 (void) QueryColorCompliance("#000F",AllCompliance,&clone_info->fill,
1284 if (clone_info->density != (char *) NULL)
1292 flags=ParseGeometry(clone_info->density,&geometry_info);
1293 resolution.x=geometry_info.rho;
1294 resolution.y=geometry_info.sigma;
1295 if ((flags & SigmaValue) == MagickFalse)
1296 resolution.y=resolution.x;
1298 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1299 clone_info->stroke_width/2.0;
1304 if (polygon_info != (PolygonInfo *) NULL)
1306 bounds=polygon_info->edges[0].bounds;
1307 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1309 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1310 bounds.x1=polygon_info->edges[i].bounds.x1;
1311 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1312 bounds.y1=polygon_info->edges[i].bounds.y1;
1313 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1314 bounds.x2=polygon_info->edges[i].bounds.x2;
1315 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1316 bounds.y2=polygon_info->edges[i].bounds.y2;
1319 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1320 image->columns ? (double) image->columns-1 : bounds.x1;
1322 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1323 image->rows ? (double) image->rows-1 : bounds.y1;
1325 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1326 image->columns ? (double) image->columns-1 : bounds.x2;
1328 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1329 image->rows ? (double) image->rows-1 : bounds.y2;
1330 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1332 if (polygon_info->edges[i].direction != 0)
1333 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1336 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1338 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1339 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1340 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1341 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1342 primitive_info[0].primitive=RectanglePrimitive;
1343 TraceRectangle(primitive_info,start,end);
1344 primitive_info[0].method=ReplaceMethod;
1345 coordinates=(ssize_t) primitive_info[0].coordinates;
1346 primitive_info[coordinates].primitive=UndefinedPrimitive;
1347 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1350 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1352 start.x=(double) (bounds.x1-mid);
1353 start.y=(double) (bounds.y1-mid);
1354 end.x=(double) (bounds.x2+mid);
1355 end.y=(double) (bounds.y2+mid);
1356 primitive_info[0].primitive=RectanglePrimitive;
1357 TraceRectangle(primitive_info,start,end);
1358 primitive_info[0].method=ReplaceMethod;
1359 coordinates=(ssize_t) primitive_info[0].coordinates;
1360 primitive_info[coordinates].primitive=UndefinedPrimitive;
1361 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1362 clone_info=DestroyDrawInfo(clone_info);
1366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % D r a w C l i p P a t h %
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 % DrawClipPath() draws the clip path on the image mask.
1378 % The format of the DrawClipPath method is:
1380 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1381 % const char *name,ExceptionInfo *exception)
1383 % A description of each parameter follows:
1385 % o image: the image.
1387 % o draw_info: the draw info.
1389 % o name: the name of the clip path.
1391 % o exception: return any errors or warnings in this structure.
1394 MagickExport MagickBooleanType DrawClipPath(Image *image,
1395 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1398 filename[MagickPathExtent];
1412 assert(image != (Image *) NULL);
1413 assert(image->signature == MagickCoreSignature);
1414 if (image->debug != MagickFalse)
1415 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1416 assert(draw_info != (const DrawInfo *) NULL);
1417 (void) FormatLocaleString(filename,MagickPathExtent,"%s",name);
1418 value=GetImageArtifact(image,filename);
1419 if (value == (const char *) NULL)
1420 return(MagickFalse);
1421 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1422 if (clip_mask == (Image *) NULL)
1423 return(MagickFalse);
1424 (void) QueryColorCompliance("#0000",AllCompliance,
1425 &clip_mask->background_color,exception);
1426 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1427 (void) SetImageBackgroundColor(clip_mask,exception);
1428 if (image->debug != MagickFalse)
1429 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1430 draw_info->clip_mask);
1431 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1432 (void) CloneString(&clone_info->primitive,value);
1433 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1435 clone_info->clip_mask=(char *) NULL;
1436 status=NegateImage(clip_mask,MagickFalse,exception);
1437 (void) SetImageMask(image,ReadPixelMask,clip_mask,exception);
1438 clip_mask=DestroyImage(clip_mask);
1439 status&=DrawImage(image,clone_info,exception);
1440 clone_info=DestroyDrawInfo(clone_info);
1441 if (image->debug != MagickFalse)
1442 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1443 return(status != 0 ? MagickTrue : MagickFalse);
1447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451 + D r a w D a s h P o l y g o n %
1455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1458 % image while respecting the dash offset and dash pattern attributes.
1460 % The format of the DrawDashPolygon method is:
1462 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1463 % const PrimitiveInfo *primitive_info,Image *image,
1464 % ExceptionInfo *exception)
1466 % A description of each parameter follows:
1468 % o draw_info: the draw info.
1470 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1472 % o image: the image.
1474 % o exception: return any errors or warnings in this structure.
1477 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1478 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1510 assert(draw_info != (const DrawInfo *) NULL);
1511 if (image->debug != MagickFalse)
1512 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1513 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1514 number_vertices=(size_t) i;
1515 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1516 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1517 if (dash_polygon == (PrimitiveInfo *) NULL)
1518 return(MagickFalse);
1519 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1520 clone_info->miterlimit=0;
1521 dash_polygon[0]=primitive_info[0];
1522 scale=ExpandAffine(&draw_info->affine);
1523 length=scale*(draw_info->dash_pattern[0]-0.5);
1524 offset=fabs(draw_info->dash_offset) >= DrawEpsilon ?
1525 scale*draw_info->dash_offset : 0.0;
1527 for (n=0; offset > 0.0; j=0)
1529 if (draw_info->dash_pattern[n] <= 0.0)
1531 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1532 if (offset > length)
1536 length=scale*(draw_info->dash_pattern[n]+0.5);
1539 if (offset < length)
1551 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1553 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1554 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1555 maximum_length=hypot((double) dx,dy);
1556 if (fabs(length) < DrawEpsilon)
1559 if (fabs(draw_info->dash_pattern[n]) < DrawEpsilon)
1561 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1563 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1565 total_length+=length;
1566 if ((n & 0x01) != 0)
1568 dash_polygon[0]=primitive_info[0];
1569 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1570 total_length/maximum_length);
1571 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1572 total_length/maximum_length);
1577 if ((j+1) > (ssize_t) (2*number_vertices))
1579 dash_polygon[j]=primitive_info[i-1];
1580 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1581 total_length/maximum_length);
1582 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1583 total_length/maximum_length);
1584 dash_polygon[j].coordinates=1;
1586 dash_polygon[0].coordinates=(size_t) j;
1587 dash_polygon[j].primitive=UndefinedPrimitive;
1588 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1591 if (fabs(draw_info->dash_pattern[n]) < DrawEpsilon)
1593 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1595 length-=(maximum_length-total_length);
1596 if ((n & 0x01) != 0)
1598 dash_polygon[j]=primitive_info[i];
1599 dash_polygon[j].coordinates=1;
1602 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1604 dash_polygon[j]=primitive_info[i-1];
1605 dash_polygon[j].point.x+=DrawEpsilon;
1606 dash_polygon[j].point.y+=DrawEpsilon;
1607 dash_polygon[j].coordinates=1;
1609 dash_polygon[0].coordinates=(size_t) j;
1610 dash_polygon[j].primitive=UndefinedPrimitive;
1611 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1613 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1614 clone_info=DestroyDrawInfo(clone_info);
1615 if (image->debug != MagickFalse)
1616 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1617 return(status != 0 ? MagickTrue : MagickFalse);
1621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1625 % D r a w I m a g e %
1629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1631 % DrawImage() draws a graphic primitive on your image. The primitive
1632 % may be represented as a string or filename. Precede the filename with an
1633 % "at" sign (@) and the contents of the file are drawn on the image. You
1634 % can affect how text is drawn by setting one or more members of the draw
1637 % The format of the DrawImage method is:
1639 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1640 % ExceptionInfo *exception)
1642 % A description of each parameter follows:
1644 % o image: the image.
1646 % o draw_info: the draw info.
1648 % o exception: return any errors or warnings in this structure.
1652 static inline MagickBooleanType IsPoint(const char *point)
1660 value=StringToDouble(point,&p);
1661 return((fabs(value) < DrawEpsilon) && (p == point) ? MagickFalse : MagickTrue);
1664 static inline void TracePoint(PrimitiveInfo *primitive_info,
1665 const PointInfo point)
1667 primitive_info->coordinates=1;
1668 primitive_info->point=point;
1671 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1672 ExceptionInfo *exception)
1674 #define RenderImageTag "Render/Image"
1681 keyword[MagickPathExtent],
1682 geometry[MagickPathExtent],
1684 pattern[MagickPathExtent],
1740 assert(image != (Image *) NULL);
1741 assert(image->signature == MagickCoreSignature);
1742 if (image->debug != MagickFalse)
1743 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1744 assert(draw_info != (DrawInfo *) NULL);
1745 assert(draw_info->signature == MagickCoreSignature);
1746 if (image->debug != MagickFalse)
1747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1748 if ((draw_info->primitive == (char *) NULL) ||
1749 (*draw_info->primitive == '\0'))
1750 return(MagickFalse);
1751 if (image->debug != MagickFalse)
1752 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1753 if (*draw_info->primitive != '@')
1754 primitive=AcquireString(draw_info->primitive);
1756 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1757 if (primitive == (char *) NULL)
1758 return(MagickFalse);
1759 primitive_extent=(double) strlen(primitive);
1760 (void) SetImageArtifact(image,"MVG",primitive);
1763 stops=(StopInfo *) NULL;
1765 Allocate primitive info memory.
1767 graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
1768 if (graphic_context == (DrawInfo **) NULL)
1770 primitive=DestroyString(primitive);
1771 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1775 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1776 sizeof(*primitive_info));
1777 if (primitive_info == (PrimitiveInfo *) NULL)
1779 primitive=DestroyString(primitive);
1780 for ( ; n >= 0; n--)
1781 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1782 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1783 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1786 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1787 graphic_context[n]->viewbox=image->page;
1788 if ((image->page.width == 0) || (image->page.height == 0))
1790 graphic_context[n]->viewbox.width=image->columns;
1791 graphic_context[n]->viewbox.height=image->rows;
1793 token=AcquireString(primitive);
1794 extent=strlen(token)+MagickPathExtent;
1795 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1796 return(MagickFalse);
1798 for (q=primitive; *q != '\0'; )
1801 Interpret graphic primitive.
1803 GetNextToken(q,&q,MagickPathExtent,keyword);
1804 if (*keyword == '\0')
1806 if (*keyword == '#')
1811 while ((*q != '\n') && (*q != '\0'))
1815 p=q-strlen(keyword)-1;
1816 primitive_type=UndefinedPrimitive;
1817 current=graphic_context[n]->affine;
1818 GetAffineMatrix(&affine);
1826 if (LocaleCompare("affine",keyword) == 0)
1828 GetNextToken(q,&q,extent,token);
1829 affine.sx=StringToDouble(token,&next_token);
1830 if (token == next_token)
1832 GetNextToken(q,&q,extent,token);
1834 GetNextToken(q,&q,extent,token);
1835 affine.rx=StringToDouble(token,&next_token);
1836 if (token == next_token)
1838 GetNextToken(q,&q,extent,token);
1840 GetNextToken(q,&q,extent,token);
1841 affine.ry=StringToDouble(token,&next_token);
1842 if (token == next_token)
1844 GetNextToken(q,&q,extent,token);
1846 GetNextToken(q,&q,extent,token);
1847 affine.sy=StringToDouble(token,&next_token);
1848 if (token == next_token)
1850 GetNextToken(q,&q,extent,token);
1852 GetNextToken(q,&q,extent,token);
1853 affine.tx=StringToDouble(token,&next_token);
1854 if (token == next_token)
1856 GetNextToken(q,&q,extent,token);
1858 GetNextToken(q,&q,extent,token);
1859 affine.ty=StringToDouble(token,&next_token);
1860 if (token == next_token)
1864 if (LocaleCompare("alpha",keyword) == 0)
1866 primitive_type=AlphaPrimitive;
1869 if (LocaleCompare("arc",keyword) == 0)
1871 primitive_type=ArcPrimitive;
1880 if (LocaleCompare("bezier",keyword) == 0)
1882 primitive_type=BezierPrimitive;
1885 if (LocaleCompare("border-color",keyword) == 0)
1887 GetNextToken(q,&q,extent,token);
1888 (void) QueryColorCompliance(token,AllCompliance,
1889 &graphic_context[n]->border_color,exception);
1898 if (LocaleCompare("clip-path",keyword) == 0)
1903 GetNextToken(q,&q,extent,token);
1904 (void) CloneString(&graphic_context[n]->clip_mask,token);
1905 (void) DrawClipPath(image,graphic_context[n],
1906 graphic_context[n]->clip_mask,exception);
1909 if (LocaleCompare("clip-rule",keyword) == 0)
1914 GetNextToken(q,&q,extent,token);
1915 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1917 if (fill_rule == -1)
1920 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1923 if (LocaleCompare("clip-units",keyword) == 0)
1928 GetNextToken(q,&q,extent,token);
1929 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1931 if (clip_units == -1)
1936 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1937 if (clip_units == ObjectBoundingBox)
1939 GetAffineMatrix(¤t);
1940 affine.sx=draw_info->bounds.x2;
1941 affine.sy=draw_info->bounds.y2;
1942 affine.tx=draw_info->bounds.x1;
1943 affine.ty=draw_info->bounds.y1;
1948 if (LocaleCompare("circle",keyword) == 0)
1950 primitive_type=CirclePrimitive;
1953 if (LocaleCompare("color",keyword) == 0)
1955 primitive_type=ColorPrimitive;
1964 if (LocaleCompare("decorate",keyword) == 0)
1969 GetNextToken(q,&q,extent,token);
1970 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1975 graphic_context[n]->decorate=(DecorationType) decorate;
1978 if (LocaleCompare("density",keyword) == 0)
1980 GetNextToken(q,&q,extent,token);
1981 (void) CloneString(&graphic_context[n]->density,token);
1984 if (LocaleCompare("direction",keyword) == 0)
1989 GetNextToken(q,&q,extent,token);
1990 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1992 if (direction == -1)
1995 graphic_context[n]->direction=(DirectionType) direction;
2004 if (LocaleCompare("ellipse",keyword) == 0)
2006 primitive_type=EllipsePrimitive;
2009 if (LocaleCompare("encoding",keyword) == 0)
2011 GetNextToken(q,&q,extent,token);
2012 (void) CloneString(&graphic_context[n]->encoding,token);
2021 if (LocaleCompare("fill",keyword) == 0)
2023 GetNextToken(q,&q,extent,token);
2024 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2025 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2026 (void) DrawPatternPath(image,draw_info,token,
2027 &graphic_context[n]->fill_pattern,exception);
2030 status&=QueryColorCompliance(token,AllCompliance,
2031 &graphic_context[n]->fill,exception);
2032 if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2033 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2034 if (status == MagickFalse)
2039 pattern_info=AcquireImageInfo();
2040 (void) CopyMagickString(pattern_info->filename,token,
2042 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2044 CatchException(exception);
2045 pattern_info=DestroyImageInfo(pattern_info);
2050 if (LocaleCompare("fill-opacity",keyword) == 0)
2052 GetNextToken(q,&q,extent,token);
2053 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2054 graphic_context[n]->fill.alpha=QuantumRange-ClampToQuantum(
2055 (MagickRealType) QuantumRange*(1.0-factor*StringToDouble(token,
2057 if (token == next_token)
2061 if (LocaleCompare("fill-rule",keyword) == 0)
2066 GetNextToken(q,&q,extent,token);
2067 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2069 if (fill_rule == -1)
2072 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2075 if (LocaleCompare("font",keyword) == 0)
2077 GetNextToken(q,&q,extent,token);
2078 (void) CloneString(&graphic_context[n]->font,token);
2079 if (LocaleCompare("none",token) == 0)
2080 graphic_context[n]->font=(char *) RelinquishMagickMemory(
2081 graphic_context[n]->font);
2084 if (LocaleCompare("font-family",keyword) == 0)
2086 GetNextToken(q,&q,extent,token);
2087 (void) CloneString(&graphic_context[n]->family,token);
2090 if (LocaleCompare("font-size",keyword) == 0)
2092 GetNextToken(q,&q,extent,token);
2093 graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2094 if (token == next_token)
2098 if (LocaleCompare("font-stretch",keyword) == 0)
2103 GetNextToken(q,&q,extent,token);
2104 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2108 graphic_context[n]->stretch=(StretchType) stretch;
2111 if (LocaleCompare("font-style",keyword) == 0)
2116 GetNextToken(q,&q,extent,token);
2117 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2121 graphic_context[n]->style=(StyleType) style;
2124 if (LocaleCompare("font-weight",keyword) == 0)
2129 GetNextToken(q,&q,extent,token);
2130 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
2132 weight=(ssize_t) StringToUnsignedLong(token);
2133 graphic_context[n]->weight=(size_t) weight;
2142 if (LocaleCompare("gradient-units",keyword) == 0)
2144 GetNextToken(q,&q,extent,token);
2147 if (LocaleCompare("gravity",keyword) == 0)
2152 GetNextToken(q,&q,extent,token);
2153 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2157 graphic_context[n]->gravity=(GravityType) gravity;
2166 if (LocaleCompare("image",keyword) == 0)
2171 primitive_type=ImagePrimitive;
2172 GetNextToken(q,&q,extent,token);
2173 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2177 graphic_context[n]->compose=(CompositeOperator) compose;
2180 if (LocaleCompare("interline-spacing",keyword) == 0)
2182 GetNextToken(q,&q,extent,token);
2183 graphic_context[n]->interline_spacing=StringToDouble(token,
2185 if (token == next_token)
2189 if (LocaleCompare("interword-spacing",keyword) == 0)
2191 GetNextToken(q,&q,extent,token);
2192 graphic_context[n]->interword_spacing=StringToDouble(token,
2194 if (token == next_token)
2204 if (LocaleCompare("kerning",keyword) == 0)
2206 GetNextToken(q,&q,extent,token);
2207 graphic_context[n]->kerning=StringToDouble(token,&next_token);
2208 if (token == next_token)
2218 if (LocaleCompare("line",keyword) == 0)
2219 primitive_type=LinePrimitive;
2227 if (LocaleCompare("offset",keyword) == 0)
2229 GetNextToken(q,&q,extent,token);
2232 if (LocaleCompare("opacity",keyword) == 0)
2234 GetNextToken(q,&q,extent,token);
2235 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2236 graphic_context[n]->alpha=QuantumRange*(1.0-(QuantumScale*
2237 graphic_context[n]->alpha*(1.0-factor*StringToDouble(token,
2239 graphic_context[n]->fill_alpha=QuantumRange*(1.0-(QuantumScale*
2240 graphic_context[n]->fill_alpha*(1.0-factor*StringToDouble(token,
2242 graphic_context[n]->stroke_alpha=QuantumRange*(1.0-(QuantumScale*
2243 graphic_context[n]->stroke_alpha*(1.0-factor*StringToDouble(token,
2245 if (token == next_token)
2255 if (LocaleCompare("path",keyword) == 0)
2257 primitive_type=PathPrimitive;
2260 if (LocaleCompare("point",keyword) == 0)
2262 primitive_type=PointPrimitive;
2265 if (LocaleCompare("polyline",keyword) == 0)
2267 primitive_type=PolylinePrimitive;
2270 if (LocaleCompare("polygon",keyword) == 0)
2272 primitive_type=PolygonPrimitive;
2275 if (LocaleCompare("pop",keyword) == 0)
2277 GetNextToken(q,&q,extent,token);
2278 if (LocaleCompare("clip-path",token) == 0)
2280 if (LocaleCompare("defs",token) == 0)
2282 if (LocaleCompare("gradient",token) == 0)
2284 if (LocaleCompare("graphic-context",token) == 0)
2288 (void) ThrowMagickException(exception,GetMagickModule(),
2289 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2294 if (graphic_context[n]->clip_mask != (char *) NULL)
2295 if (LocaleCompare(graphic_context[n]->clip_mask,
2296 graphic_context[n-1]->clip_mask) != 0)
2297 (void) SetImageMask(image,ReadPixelMask,(Image *) NULL,
2299 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2303 if (LocaleCompare("pattern",token) == 0)
2308 if (LocaleCompare("push",keyword) == 0)
2310 GetNextToken(q,&q,extent,token);
2311 if (LocaleCompare("clip-path",token) == 0)
2314 name[MagickPathExtent];
2316 GetNextToken(q,&q,extent,token);
2317 (void) FormatLocaleString(name,MagickPathExtent,"%s",token);
2318 for (p=q; *q != '\0'; )
2320 GetNextToken(q,&q,extent,token);
2321 if (LocaleCompare(token,"pop") != 0)
2323 GetNextToken(q,(const char **) NULL,extent,token);
2324 if (LocaleCompare(token,"clip-path") != 0)
2328 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2329 (void) SetImageArtifact(image,name,token);
2330 GetNextToken(q,&q,extent,token);
2333 if (LocaleCompare("gradient",token) == 0)
2336 key[2*MagickPathExtent],
2337 name[MagickPathExtent],
2338 type[MagickPathExtent];
2343 GetNextToken(q,&q,extent,token);
2344 (void) CopyMagickString(name,token,MagickPathExtent);
2345 GetNextToken(q,&q,extent,token);
2346 (void) CopyMagickString(type,token,MagickPathExtent);
2347 GetNextToken(q,&q,extent,token);
2348 segment.x1=StringToDouble(token,&next_token);
2349 if (token == next_token)
2351 GetNextToken(q,&q,extent,token);
2353 GetNextToken(q,&q,extent,token);
2354 segment.y1=StringToDouble(token,&next_token);
2355 if (token == next_token)
2357 GetNextToken(q,&q,extent,token);
2359 GetNextToken(q,&q,extent,token);
2360 segment.x2=StringToDouble(token,&next_token);
2361 if (token == next_token)
2363 GetNextToken(q,&q,extent,token);
2365 GetNextToken(q,&q,extent,token);
2366 segment.y2=StringToDouble(token,&next_token);
2367 if (token == next_token)
2369 if (LocaleCompare(type,"radial") == 0)
2371 GetNextToken(q,&q,extent,token);
2373 GetNextToken(q,&q,extent,token);
2375 for (p=q; *q != '\0'; )
2377 GetNextToken(q,&q,extent,token);
2378 if (LocaleCompare(token,"pop") != 0)
2380 GetNextToken(q,(const char **) NULL,extent,token);
2381 if (LocaleCompare(token,"gradient") != 0)
2385 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2386 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2387 graphic_context[n]->affine.ry*segment.y1+
2388 graphic_context[n]->affine.tx;
2389 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2390 graphic_context[n]->affine.sy*segment.y1+
2391 graphic_context[n]->affine.ty;
2392 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2393 graphic_context[n]->affine.ry*segment.y2+
2394 graphic_context[n]->affine.tx;
2395 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2396 graphic_context[n]->affine.sy*segment.y2+
2397 graphic_context[n]->affine.ty;
2398 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2399 (void) SetImageArtifact(image,key,token);
2400 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
2401 (void) SetImageArtifact(image,key,type);
2402 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2404 (void) FormatLocaleString(geometry,MagickPathExtent,
2405 "%gx%g%+.15g%+.15g",
2406 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2407 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2408 bounds.x1,bounds.y1);
2409 (void) SetImageArtifact(image,key,geometry);
2410 GetNextToken(q,&q,extent,token);
2413 if (LocaleCompare("pattern",token) == 0)
2416 key[2*MagickPathExtent],
2417 name[MagickPathExtent];
2422 GetNextToken(q,&q,extent,token);
2423 (void) CopyMagickString(name,token,MagickPathExtent);
2424 GetNextToken(q,&q,extent,token);
2425 pattern_bounds.x=(ssize_t) ceil(StringToDouble(token,
2427 if (token == next_token)
2429 GetNextToken(q,&q,extent,token);
2431 GetNextToken(q,&q,extent,token);
2432 pattern_bounds.y=(ssize_t) ceil(StringToDouble(token,
2434 if (token == next_token)
2436 GetNextToken(q,&q,extent,token);
2438 GetNextToken(q,&q,extent,token);
2439 pattern_bounds.width=(size_t) floor(StringToDouble(token,
2441 if (token == next_token)
2443 GetNextToken(q,&q,extent,token);
2445 GetNextToken(q,&q,extent,token);
2446 pattern_bounds.height=(size_t) floor(StringToDouble(token,
2448 if (token == next_token)
2450 for (p=q; *q != '\0'; )
2452 GetNextToken(q,&q,extent,token);
2453 if (LocaleCompare(token,"pop") != 0)
2455 GetNextToken(q,(const char **) NULL,extent,token);
2456 if (LocaleCompare(token,"pattern") != 0)
2460 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2461 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2462 (void) SetImageArtifact(image,key,token);
2463 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2465 (void) FormatLocaleString(geometry,MagickPathExtent,
2466 "%.20gx%.20g%+.20g%+.20g",(double)pattern_bounds.width,
2467 (double)pattern_bounds.height,(double)pattern_bounds.x,
2468 (double)pattern_bounds.y);
2469 (void) SetImageArtifact(image,key,geometry);
2470 GetNextToken(q,&q,extent,token);
2473 if (LocaleCompare("graphic-context",token) == 0)
2476 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2477 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2478 if (graphic_context == (DrawInfo **) NULL)
2480 (void) ThrowMagickException(exception,GetMagickModule(),
2481 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2485 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2486 graphic_context[n-1]);
2489 if (LocaleCompare("defs",token) == 0)
2500 if (LocaleCompare("rectangle",keyword) == 0)
2502 primitive_type=RectanglePrimitive;
2505 if (LocaleCompare("rotate",keyword) == 0)
2507 GetNextToken(q,&q,extent,token);
2508 angle=StringToDouble(token,&next_token);
2509 if (token == next_token)
2511 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2512 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2513 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2514 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2517 if (LocaleCompare("roundRectangle",keyword) == 0)
2519 primitive_type=RoundRectanglePrimitive;
2528 if (LocaleCompare("scale",keyword) == 0)
2530 GetNextToken(q,&q,extent,token);
2531 affine.sx=StringToDouble(token,&next_token);
2532 if (token == next_token)
2534 GetNextToken(q,&q,extent,token);
2536 GetNextToken(q,&q,extent,token);
2537 affine.sy=StringToDouble(token,&next_token);
2538 if (token == next_token)
2542 if (LocaleCompare("skewX",keyword) == 0)
2544 GetNextToken(q,&q,extent,token);
2545 angle=StringToDouble(token,&next_token);
2546 if (token == next_token)
2548 affine.ry=sin(DegreesToRadians(angle));
2551 if (LocaleCompare("skewY",keyword) == 0)
2553 GetNextToken(q,&q,extent,token);
2554 angle=StringToDouble(token,&next_token);
2555 if (token == next_token)
2557 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2560 if (LocaleCompare("stop-color",keyword) == 0)
2566 if (number_stops == 1)
2567 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
2568 else if (number_stops > 2)
2569 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
2571 if (stops == (StopInfo *) NULL)
2573 (void) ThrowMagickException(exception,GetMagickModule(),
2574 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2578 GetNextToken(q,&q,extent,token);
2579 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2581 stops[number_stops-1].color=stop_color;
2582 GetNextToken(q,&q,extent,token);
2583 stops[number_stops-1].offset=StringToDouble(token,&next_token);
2584 if (token == next_token)
2588 if (LocaleCompare("stroke",keyword) == 0)
2590 GetNextToken(q,&q,extent,token);
2591 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2592 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2593 (void) DrawPatternPath(image,draw_info,token,
2594 &graphic_context[n]->stroke_pattern,exception);
2597 status&=QueryColorCompliance(token,AllCompliance,
2598 &graphic_context[n]->stroke,exception);
2599 if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
2600 graphic_context[n]->stroke.alpha=
2601 graphic_context[n]->stroke_alpha;
2602 if (status == MagickFalse)
2607 pattern_info=AcquireImageInfo();
2608 (void) CopyMagickString(pattern_info->filename,token,
2610 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2612 CatchException(exception);
2613 pattern_info=DestroyImageInfo(pattern_info);
2618 if (LocaleCompare("stroke-antialias",keyword) == 0)
2620 GetNextToken(q,&q,extent,token);
2621 graphic_context[n]->stroke_antialias=
2622 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2625 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2627 if (graphic_context[n]->dash_pattern != (double *) NULL)
2628 graphic_context[n]->dash_pattern=(double *)
2629 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2630 if (IsPoint(q) != MagickFalse)
2636 GetNextToken(r,&r,extent,token);
2638 GetNextToken(r,&r,extent,token);
2639 for (x=0; IsPoint(token) != MagickFalse; x++)
2641 GetNextToken(r,&r,extent,token);
2643 GetNextToken(r,&r,extent,token);
2645 graphic_context[n]->dash_pattern=(double *)
2646 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2647 sizeof(*graphic_context[n]->dash_pattern));
2648 if (graphic_context[n]->dash_pattern == (double *) NULL)
2650 (void) ThrowMagickException(exception,GetMagickModule(),
2651 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2656 for (j=0; j < x; j++)
2658 GetNextToken(q,&q,extent,token);
2660 GetNextToken(q,&q,extent,token);
2661 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2663 if (token == next_token)
2665 if (graphic_context[n]->dash_pattern[j] < 0.0)
2668 if ((x & 0x01) != 0)
2669 for ( ; j < (2*x); j++)
2670 graphic_context[n]->dash_pattern[j]=
2671 graphic_context[n]->dash_pattern[j-x];
2672 graphic_context[n]->dash_pattern[j]=0.0;
2675 GetNextToken(q,&q,extent,token);
2678 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2680 GetNextToken(q,&q,extent,token);
2681 graphic_context[n]->dash_offset=StringToDouble(token,
2683 if (token == next_token)
2687 if (LocaleCompare("stroke-linecap",keyword) == 0)
2692 GetNextToken(q,&q,extent,token);
2693 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2697 graphic_context[n]->linecap=(LineCap) linecap;
2700 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2705 GetNextToken(q,&q,extent,token);
2706 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2711 graphic_context[n]->linejoin=(LineJoin) linejoin;
2714 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2716 GetNextToken(q,&q,extent,token);
2717 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2720 if (LocaleCompare("stroke-opacity",keyword) == 0)
2722 GetNextToken(q,&q,extent,token);
2723 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2724 graphic_context[n]->stroke.alpha=QuantumRange-ClampToQuantum(
2725 (MagickRealType) QuantumRange*(1.0-factor*StringToDouble(token,
2727 if (token == next_token)
2731 if (LocaleCompare("stroke-width",keyword) == 0)
2733 GetNextToken(q,&q,extent,token);
2734 graphic_context[n]->stroke_width=StringToDouble(token,&next_token);
2735 if (token == next_token)
2745 if (LocaleCompare("text",keyword) == 0)
2747 primitive_type=TextPrimitive;
2750 if (LocaleCompare("text-align",keyword) == 0)
2755 GetNextToken(q,&q,extent,token);
2756 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2760 graphic_context[n]->align=(AlignType) align;
2763 if (LocaleCompare("text-anchor",keyword) == 0)
2768 GetNextToken(q,&q,extent,token);
2769 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2773 graphic_context[n]->align=(AlignType) align;
2776 if (LocaleCompare("text-antialias",keyword) == 0)
2778 GetNextToken(q,&q,extent,token);
2779 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
2780 MagickTrue : MagickFalse;
2783 if (LocaleCompare("text-undercolor",keyword) == 0)
2785 GetNextToken(q,&q,extent,token);
2786 (void) QueryColorCompliance(token,AllCompliance,
2787 &graphic_context[n]->undercolor,exception);
2790 if (LocaleCompare("translate",keyword) == 0)
2792 GetNextToken(q,&q,extent,token);
2793 affine.tx=StringToDouble(token,&next_token);
2794 if (token == next_token)
2796 GetNextToken(q,&q,extent,token);
2798 GetNextToken(q,&q,extent,token);
2799 affine.ty=StringToDouble(token,&next_token);
2800 if (token == next_token)
2810 if (LocaleCompare("viewbox",keyword) == 0)
2812 GetNextToken(q,&q,extent,token);
2813 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2815 if (token == next_token)
2817 GetNextToken(q,&q,extent,token);
2819 GetNextToken(q,&q,extent,token);
2820 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2822 if (token == next_token)
2824 GetNextToken(q,&q,extent,token);
2826 GetNextToken(q,&q,extent,token);
2827 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2828 token,&next_token)+0.5);
2829 if (token == next_token)
2831 GetNextToken(q,&q,extent,token);
2833 GetNextToken(q,&q,extent,token);
2834 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2835 token,&next_token)+0.5);
2836 if (token == next_token)
2849 if (status == MagickFalse)
2851 if ((fabs(affine.sx-1.0) >= DrawEpsilon) ||
2852 (fabs(affine.rx) >= DrawEpsilon) || (fabs(affine.ry) >= DrawEpsilon) ||
2853 (fabs(affine.sy-1.0) >= DrawEpsilon) ||
2854 (fabs(affine.tx) >= DrawEpsilon) || (fabs(affine.ty) >= DrawEpsilon))
2856 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2857 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2858 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2859 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2860 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2862 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2865 if (primitive_type == UndefinedPrimitive)
2869 if (number_stops > 1)
2874 type=LinearGradient;
2875 if (draw_info->gradient.type == RadialGradient)
2876 type=RadialGradient;
2877 (void) GradientImage(image,type,PadSpread,stops,number_stops,
2880 if (number_stops > 0)
2881 stops=(StopInfo *) RelinquishMagickMemory(stops);
2883 if (image->debug != MagickFalse)
2884 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
2889 Parse the primitive attributes.
2893 primitive_info[0].point.x=0.0;
2894 primitive_info[0].point.y=0.0;
2895 for (x=0; *q != '\0'; x++)
2900 if (IsPoint(q) == MagickFalse)
2902 GetNextToken(q,&q,extent,token);
2903 point.x=StringToDouble(token,&next_token);
2904 if (token == next_token)
2906 GetNextToken(q,&q,extent,token);
2908 GetNextToken(q,&q,extent,token);
2909 point.y=StringToDouble(token,&next_token);
2910 if (token == next_token)
2912 GetNextToken(q,(const char **) NULL,extent,token);
2914 GetNextToken(q,&q,extent,token);
2915 primitive_info[i].primitive=primitive_type;
2916 primitive_info[i].point=point;
2917 primitive_info[i].coordinates=0;
2918 primitive_info[i].method=FloodfillMethod;
2920 if (i < (ssize_t) number_points)
2923 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2924 (size_t) number_points,sizeof(*primitive_info));
2925 if ((primitive_info == (PrimitiveInfo *) NULL) ||
2926 (number_points != (MagickSizeType) ((size_t) number_points)))
2927 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2930 primitive_info[j].primitive=primitive_type;
2931 primitive_info[j].coordinates=(size_t) x;
2932 primitive_info[j].method=FloodfillMethod;
2933 primitive_info[j].text=(char *) NULL;
2935 Circumscribe primitive within a circle.
2937 bounds.x1=primitive_info[j].point.x;
2938 bounds.y1=primitive_info[j].point.y;
2939 bounds.x2=primitive_info[j].point.x;
2940 bounds.y2=primitive_info[j].point.y;
2941 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2943 point=primitive_info[j+k].point;
2944 if (point.x < bounds.x1)
2946 if (point.y < bounds.y1)
2948 if (point.x > bounds.x2)
2950 if (point.y > bounds.y2)
2954 Speculate how many points our primitive might consume.
2956 length=primitive_info[j].coordinates;
2957 switch (primitive_type)
2959 case RectanglePrimitive:
2964 case RoundRectanglePrimitive:
2971 alpha=bounds.x2-bounds.x1;
2972 beta=bounds.y2-bounds.y1;
2973 radius=hypot((double) alpha,(double) beta);
2975 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2978 case BezierPrimitive:
2980 if (primitive_info[j].coordinates > 107)
2981 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2982 "TooManyBezierCoordinates","`%s'",token);
2983 length=BezierQuantum*primitive_info[j].coordinates;
2992 GetNextToken(q,&q,extent,token);
2995 for (s=token; *s != '\0'; s=t)
3000 value=StringToDouble(s,&t);
3009 length=length*BezierQuantum;
3012 case CirclePrimitive:
3014 case EllipsePrimitive:
3021 alpha=bounds.x2-bounds.x1;
3022 beta=bounds.y2-bounds.y1;
3023 radius=hypot((double) alpha,(double) beta);
3024 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
3030 if ((i+length) >= number_points)
3033 Resize based on speculative points required by primitive.
3035 number_points+=length+1;
3036 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
3037 (size_t) number_points,sizeof(*primitive_info));
3038 if ((primitive_info == (PrimitiveInfo *) NULL) ||
3039 (number_points != (MagickSizeType) ((size_t) number_points)))
3041 (void) ThrowMagickException(exception,GetMagickModule(),
3042 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3047 switch (primitive_type)
3049 case PointPrimitive:
3052 if (primitive_info[j].coordinates != 1)
3057 TracePoint(primitive_info+j,primitive_info[j].point);
3058 i=(ssize_t) (j+primitive_info[j].coordinates);
3063 if (primitive_info[j].coordinates != 2)
3068 TraceLine(primitive_info+j,primitive_info[j].point,
3069 primitive_info[j+1].point);
3070 i=(ssize_t) (j+primitive_info[j].coordinates);
3073 case RectanglePrimitive:
3075 if (primitive_info[j].coordinates != 2)
3080 TraceRectangle(primitive_info+j,primitive_info[j].point,
3081 primitive_info[j+1].point);
3082 i=(ssize_t) (j+primitive_info[j].coordinates);
3085 case RoundRectanglePrimitive:
3087 if (primitive_info[j].coordinates != 3)
3092 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
3093 primitive_info[j+1].point,primitive_info[j+2].point);
3094 i=(ssize_t) (j+primitive_info[j].coordinates);
3099 if (primitive_info[j].coordinates != 3)
3101 primitive_type=UndefinedPrimitive;
3104 TraceArc(primitive_info+j,primitive_info[j].point,
3105 primitive_info[j+1].point,primitive_info[j+2].point);
3106 i=(ssize_t) (j+primitive_info[j].coordinates);
3109 case EllipsePrimitive:
3111 if (primitive_info[j].coordinates != 3)
3116 TraceEllipse(primitive_info+j,primitive_info[j].point,
3117 primitive_info[j+1].point,primitive_info[j+2].point);
3118 i=(ssize_t) (j+primitive_info[j].coordinates);
3121 case CirclePrimitive:
3123 if (primitive_info[j].coordinates != 2)
3128 TraceCircle(primitive_info+j,primitive_info[j].point,
3129 primitive_info[j+1].point);
3130 i=(ssize_t) (j+primitive_info[j].coordinates);
3133 case PolylinePrimitive:
3135 case PolygonPrimitive:
3137 primitive_info[i]=primitive_info[j];
3138 primitive_info[i].coordinates=0;
3139 primitive_info[j].coordinates++;
3143 case BezierPrimitive:
3145 if (primitive_info[j].coordinates < 3)
3150 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3151 i=(ssize_t) (j+primitive_info[j].coordinates);
3156 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3159 case AlphaPrimitive:
3160 case ColorPrimitive:
3165 if (primitive_info[j].coordinates != 1)
3170 GetNextToken(q,&q,extent,token);
3171 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3175 primitive_info[j].method=(PaintMethod) method;
3180 if (primitive_info[j].coordinates != 1)
3186 GetNextToken(q,&q,extent,token);
3187 primitive_info[j].text=AcquireString(token);
3190 case ImagePrimitive:
3192 if (primitive_info[j].coordinates != 2)
3197 GetNextToken(q,&q,extent,token);
3198 primitive_info[j].text=AcquireString(token);
3202 if (primitive_info == (PrimitiveInfo *) NULL)
3204 if (image->debug != MagickFalse)
3205 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3206 if (status == MagickFalse)
3208 primitive_info[i].primitive=UndefinedPrimitive;
3214 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3216 point=primitive_info[i].point;
3217 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3218 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3219 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3220 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3221 point=primitive_info[i].point;
3222 if (point.x < graphic_context[n]->bounds.x1)
3223 graphic_context[n]->bounds.x1=point.x;
3224 if (point.y < graphic_context[n]->bounds.y1)
3225 graphic_context[n]->bounds.y1=point.y;
3226 if (point.x > graphic_context[n]->bounds.x2)
3227 graphic_context[n]->bounds.x2=point.x;
3228 if (point.y > graphic_context[n]->bounds.y2)
3229 graphic_context[n]->bounds.y2=point.y;
3230 if (primitive_info[i].primitive == ImagePrimitive)
3232 if (i >= (ssize_t) number_points)
3233 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3235 if (graphic_context[n]->render != MagickFalse)
3237 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3238 (LocaleCompare(graphic_context[n]->clip_mask,
3239 graphic_context[n-1]->clip_mask) != 0))
3240 status&=DrawClipPath(image,graphic_context[n],
3241 graphic_context[n]->clip_mask,exception);
3242 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3245 if (primitive_info->text != (char *) NULL)
3246 primitive_info->text=(char *) RelinquishMagickMemory(
3247 primitive_info->text);
3248 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3250 if (proceed == MagickFalse)
3255 if (image->debug != MagickFalse)
3256 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3258 Relinquish resources.
3260 token=DestroyString(token);
3261 if (primitive_info != (PrimitiveInfo *) NULL)
3262 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3263 primitive=DestroyString(primitive);
3264 for ( ; n >= 0; n--)
3265 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3266 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3267 if (status == MagickFalse)
3268 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3270 return(status != 0 ? MagickTrue : MagickFalse);
3274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3278 % D r a w G r a d i e n t I m a g e %
3282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3284 % DrawGradientImage() draws a linear gradient on the image.
3286 % The format of the DrawGradientImage method is:
3288 % MagickBooleanType DrawGradientImage(Image *image,
3289 % const DrawInfo *draw_info,ExceptionInfo *exception)
3291 % A description of each parameter follows:
3293 % o image: the image.
3295 % o draw_info: the draw info.
3297 % o exception: return any errors or warnings in this structure.
3301 static inline double GetStopColorOffset(const GradientInfo *gradient,
3302 const ssize_t x,const ssize_t y)
3304 switch (gradient->type)
3306 case UndefinedGradient:
3307 case LinearGradient:
3322 gradient_vector=(&gradient->gradient_vector);
3323 p.x=gradient_vector->x2-gradient_vector->x1;
3324 p.y=gradient_vector->y2-gradient_vector->y1;
3325 q.x=(double) x-gradient_vector->x1;
3326 q.y=(double) y-gradient_vector->y1;
3327 length=sqrt(q.x*q.x+q.y*q.y);
3328 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3329 gamma=PerceptibleReciprocal(gamma);
3330 scale=p.x*q.x+p.y*q.y;
3331 offset=gamma*scale*length;
3334 case RadialGradient:
3339 if (gradient->spread == RepeatSpread)
3341 v.x=(double) x-gradient->center.x;
3342 v.y=(double) y-gradient->center.y;
3343 return(sqrt(v.x*v.x+v.y*v.y));
3345 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3346 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3347 gradient->angle))))/gradient->radii.x;
3348 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3349 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3350 gradient->angle))))/gradient->radii.y;
3351 return(sqrt(v.x*v.x+v.y*v.y));
3357 static int StopInfoCompare(const void *x,const void *y)
3363 stop_1=(StopInfo *) x;
3364 stop_2=(StopInfo *) y;
3365 if (stop_1->offset > stop_2->offset)
3367 if (fabs(stop_1->offset-stop_2->offset) <= DrawEpsilon)
3372 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3373 const DrawInfo *draw_info,ExceptionInfo *exception)
3403 Draw linear or radial gradient on image.
3405 assert(image != (Image *) NULL);
3406 assert(image->signature == MagickCoreSignature);
3407 if (image->debug != MagickFalse)
3408 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3409 assert(draw_info != (const DrawInfo *) NULL);
3410 gradient=(&draw_info->gradient);
3411 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
3413 gradient_vector=(&gradient->gradient_vector);
3414 point.x=gradient_vector->x2-gradient_vector->x1;
3415 point.y=gradient_vector->y2-gradient_vector->y1;
3416 length=sqrt(point.x*point.x+point.y*point.y);
3417 bounding_box=gradient->bounding_box;
3419 GetPixelInfo(image,&zero);
3420 image_view=AcquireAuthenticCacheView(image,exception);
3421 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3422 #pragma omp parallel for schedule(static,4) shared(status) \
3423 magick_threads(image,image,1,1)
3425 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3445 if (status == MagickFalse)
3447 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3448 if (q == (Quantum *) NULL)
3455 offset=GetStopColorOffset(gradient,0,y);
3456 if (gradient->type != RadialGradient)
3458 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3460 GetPixelInfoPixel(image,q,&pixel);
3461 switch (gradient->spread)
3463 case UndefinedSpread:
3466 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3467 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3469 offset=GetStopColorOffset(gradient,x,y);
3470 if (gradient->type != RadialGradient)
3473 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3474 if (offset < gradient->stops[i].offset)
3476 if ((offset < 0.0) || (i == 0))
3477 composite=gradient->stops[0].color;
3479 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3480 composite=gradient->stops[gradient->number_stops-1].color;
3485 alpha=(offset-gradient->stops[i].offset)/
3486 (gradient->stops[j].offset-gradient->stops[i].offset);
3487 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3488 &gradient->stops[j].color,alpha,&composite);
3494 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3495 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3497 offset=GetStopColorOffset(gradient,x,y);
3498 if (gradient->type != RadialGradient)
3503 if ((ssize_t) fmod(offset,2.0) == 0)
3504 offset=fmod(offset,1.0);
3506 offset=1.0-fmod(offset,1.0);
3507 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3508 if (offset < gradient->stops[i].offset)
3511 composite=gradient->stops[0].color;
3513 if (i == (ssize_t) gradient->number_stops)
3514 composite=gradient->stops[gradient->number_stops-1].color;
3519 alpha=(offset-gradient->stops[i].offset)/
3520 (gradient->stops[j].offset-gradient->stops[i].offset);
3521 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3522 &gradient->stops[j].color,alpha,&composite);
3534 antialias=MagickFalse;
3536 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3537 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3539 offset=GetStopColorOffset(gradient,x,y);
3540 if (gradient->type == LinearGradient)
3542 repeat=fmod(offset,length);
3544 repeat=length-fmod(-repeat,length);
3546 repeat=fmod(offset,length);
3547 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3548 MagickTrue : MagickFalse;
3549 offset=repeat/length;
3553 repeat=fmod(offset,gradient->radius);
3555 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3557 repeat=fmod(offset,gradient->radius);
3558 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3560 offset=repeat/gradient->radius;
3563 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3564 if (offset < gradient->stops[i].offset)
3567 composite=gradient->stops[0].color;
3569 if (i == (ssize_t) gradient->number_stops)
3570 composite=gradient->stops[gradient->number_stops-1].color;
3575 alpha=(offset-gradient->stops[i].offset)/
3576 (gradient->stops[j].offset-gradient->stops[i].offset);
3577 if (antialias != MagickFalse)
3579 if (gradient->type == LinearGradient)
3580 alpha=length-repeat;
3582 alpha=gradient->radius-repeat;
3584 j=(ssize_t) gradient->number_stops-1L;
3586 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3587 &gradient->stops[j].color,alpha,&composite);
3592 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3594 SetPixelViaPixelInfo(image,&pixel,q);
3595 q+=GetPixelChannels(image);
3597 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3600 image_view=DestroyCacheView(image_view);
3605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3609 % D r a w P a t t e r n P a t h %
3613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3615 % DrawPatternPath() draws a pattern.
3617 % The format of the DrawPatternPath method is:
3619 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3620 % const char *name,Image **pattern,ExceptionInfo *exception)
3622 % A description of each parameter follows:
3624 % o image: the image.
3626 % o draw_info: the draw info.
3628 % o name: the pattern name.
3630 % o image: the image.
3632 % o exception: return any errors or warnings in this structure.
3635 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3636 const DrawInfo *draw_info,const char *name,Image **pattern,
3637 ExceptionInfo *exception)
3640 property[MagickPathExtent];
3656 assert(image != (Image *) NULL);
3657 assert(image->signature == MagickCoreSignature);
3658 if (image->debug != MagickFalse)
3659 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3660 assert(draw_info != (const DrawInfo *) NULL);
3661 assert(name != (const char *) NULL);
3662 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
3663 path=GetImageArtifact(image,property);
3664 if (path == (const char *) NULL)
3665 return(MagickFalse);
3666 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
3667 geometry=GetImageArtifact(image,property);
3668 if (geometry == (const char *) NULL)
3669 return(MagickFalse);
3670 if ((*pattern) != (Image *) NULL)
3671 *pattern=DestroyImage(*pattern);
3672 image_info=AcquireImageInfo();
3673 image_info->size=AcquireString(geometry);
3674 *pattern=AcquireImage(image_info,exception);
3675 image_info=DestroyImageInfo(image_info);
3676 (void) QueryColorCompliance("#000000ff",AllCompliance,
3677 &(*pattern)->background_color,exception);
3678 (void) SetImageBackgroundColor(*pattern,exception);
3679 if (image->debug != MagickFalse)
3680 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3681 "begin pattern-path %s %s",name,geometry);
3682 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3683 clone_info->fill_pattern=NewImageList();
3684 clone_info->stroke_pattern=NewImageList();
3685 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
3686 type=GetImageArtifact(image,property);
3687 if (type != (const char *) NULL)
3688 clone_info->gradient.type=(GradientType) ParseCommandOption(
3689 MagickGradientOptions,MagickFalse,type);
3690 (void) CloneString(&clone_info->primitive,path);
3691 status=DrawImage(*pattern,clone_info,exception);
3692 clone_info=DestroyDrawInfo(clone_info);
3693 if (image->debug != MagickFalse)
3694 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3703 + D r a w P o l y g o n P r i m i t i v e %
3707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3709 % DrawPolygonPrimitive() draws a polygon on the image.
3711 % The format of the DrawPolygonPrimitive method is:
3713 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3714 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3715 % ExceptionInfo *exception)
3717 % A description of each parameter follows:
3719 % o image: the image.
3721 % o draw_info: the draw info.
3723 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3725 % o exception: return any errors or warnings in this structure.
3729 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3734 assert(polygon_info != (PolygonInfo **) NULL);
3735 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3736 if (polygon_info[i] != (PolygonInfo *) NULL)
3737 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3738 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3739 return(polygon_info);
3742 static PolygonInfo **AcquirePolygonThreadSet(
3743 const PrimitiveInfo *primitive_info)
3746 *magick_restrict path_info;
3757 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3758 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3759 sizeof(*polygon_info));
3760 if (polygon_info == (PolygonInfo **) NULL)
3761 return((PolygonInfo **) NULL);
3762 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3763 path_info=ConvertPrimitiveToPath(primitive_info);
3764 if (path_info == (PathInfo *) NULL)
3765 return(DestroyPolygonThreadSet(polygon_info));
3766 for (i=0; i < (ssize_t) number_threads; i++)
3768 polygon_info[i]=ConvertPathToPolygon(path_info);
3769 if (polygon_info[i] == (PolygonInfo *) NULL)
3770 return(DestroyPolygonThreadSet(polygon_info));
3772 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3773 return(polygon_info);
3776 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3777 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3778 const ssize_t y,double *stroke_alpha)
3789 register const PointInfo
3803 Compute fill & stroke opacity for this (x,y) point.
3807 p=polygon_info->edges;
3808 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3810 if ((double) y <= (p->bounds.y1-mid-0.5))
3812 if ((double) y > (p->bounds.y2+mid+0.5))
3814 (void) DestroyEdge(polygon_info,(size_t) j);
3817 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3818 ((double) x > (p->bounds.x2+mid+0.5)))
3820 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3821 for ( ; i < (ssize_t) p->number_points; i++)
3823 if ((double) y <= (p->points[i-1].y-mid-0.5))
3825 if ((double) y > (p->points[i].y+mid+0.5))
3827 if (p->scanline != (double) y)
3829 p->scanline=(double) y;
3830 p->highwater=(size_t) i;
3833 Compute distance between a point and an edge.
3836 delta.x=(q+1)->x-q->x;
3837 delta.y=(q+1)->y-q->y;
3838 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3841 delta.x=(double) x-q->x;
3842 delta.y=(double) y-q->y;
3843 distance=delta.x*delta.x+delta.y*delta.y;
3847 alpha=delta.x*delta.x+delta.y*delta.y;
3850 delta.x=(double) x-(q+1)->x;
3851 delta.y=(double) y-(q+1)->y;
3852 distance=delta.x*delta.x+delta.y*delta.y;
3857 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3858 distance=alpha*beta*beta;
3862 Compute stroke & subpath opacity.
3865 if (p->ghostline == MagickFalse)
3868 if ((*stroke_alpha < 1.0) &&
3869 (distance <= ((alpha+0.25)*(alpha+0.25))))
3872 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3877 if (fabs(distance-1.0) >= DrawEpsilon)
3878 beta=sqrt((double) distance);
3880 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3881 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3885 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3887 if (distance <= 0.0)
3894 if (fabs(beta) < DrawEpsilon)
3897 if (fabs(distance-1.0) >= DrawEpsilon)
3898 beta=sqrt(distance);
3901 if (subpath_alpha < (alpha*alpha))
3902 subpath_alpha=alpha*alpha;
3906 Compute fill opacity.
3908 if (fill == MagickFalse)
3910 if (subpath_alpha >= 1.0)
3913 Determine winding number.
3916 p=polygon_info->edges;
3917 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3919 if ((double) y <= p->bounds.y1)
3921 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3923 if ((double) x > p->bounds.x2)
3925 winding_number+=p->direction ? 1 : -1;
3928 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3929 for ( ; i < (ssize_t) p->number_points; i++)
3930 if ((double) y <= p->points[i].y)
3933 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3934 winding_number+=p->direction ? 1 : -1;
3936 if (fill_rule != NonZeroRule)
3938 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3942 if (MagickAbsoluteValue(winding_number) != 0)
3944 return(subpath_alpha);
3947 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3948 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3949 ExceptionInfo *exception)
3962 **magick_restrict polygon_info;
3979 Compute bounding box.
3981 assert(image != (Image *) NULL);
3982 assert(image->signature == MagickCoreSignature);
3983 if (image->debug != MagickFalse)
3984 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3985 assert(draw_info != (DrawInfo *) NULL);
3986 assert(draw_info->signature == MagickCoreSignature);
3987 assert(primitive_info != (PrimitiveInfo *) NULL);
3988 if (primitive_info->coordinates == 0)
3990 polygon_info=AcquirePolygonThreadSet(primitive_info);
3991 if (polygon_info == (PolygonInfo **) NULL)
3992 return(MagickFalse);
3993 DisableMSCWarning(4127)
3995 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3997 if (image->debug != MagickFalse)
3998 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3999 fill=(primitive_info->method == FillToBorderMethod) ||
4000 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4001 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4002 bounds=polygon_info[0]->edges[0].bounds;
4003 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4005 p=polygon_info[0]->edges+i;
4006 if (p->bounds.x1 < bounds.x1)
4007 bounds.x1=p->bounds.x1;
4008 if (p->bounds.y1 < bounds.y1)
4009 bounds.y1=p->bounds.y1;
4010 if (p->bounds.x2 > bounds.x2)
4011 bounds.x2=p->bounds.x2;
4012 if (p->bounds.y2 > bounds.y2)
4013 bounds.y2=p->bounds.y2;
4015 bounds.x1-=(mid+1.0);
4016 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
4017 image->columns ? (double) image->columns-1 : bounds.x1;
4018 bounds.y1-=(mid+1.0);
4019 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
4020 image->rows ? (double) image->rows-1 : bounds.y1;
4021 bounds.x2+=(mid+1.0);
4022 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
4023 image->columns ? (double) image->columns-1 : bounds.x2;
4024 bounds.y2+=(mid+1.0);
4025 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
4026 image->rows ? (double) image->rows-1 : bounds.y2;
4028 image_view=AcquireAuthenticCacheView(image,exception);
4029 if ((primitive_info->coordinates == 1) ||
4030 (polygon_info[0]->number_edges == 0))
4035 start_y=(ssize_t) ceil(bounds.y1-0.5);
4036 stop_y=(ssize_t) floor(bounds.y2+0.5);
4037 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4038 #pragma omp parallel for schedule(static,4) shared(status) \
4039 magick_threads(image,image,1,1)
4041 for (y=start_y; y <= stop_y; y++)
4059 if (status == MagickFalse)
4061 start_x=(ssize_t) ceil(bounds.x1-0.5);
4062 stop_x=(ssize_t) floor(bounds.x2+0.5);
4064 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4066 if (q == (Quantum *) NULL)
4071 GetPixelInfo(image,&pixel);
4072 for ( ; x <= stop_x; x++)
4074 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4075 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4077 GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4078 SetPixelViaPixelInfo(image,&pixel,q);
4080 q+=GetPixelChannels(image);
4082 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4083 if (sync == MagickFalse)
4086 image_view=DestroyCacheView(image_view);
4087 polygon_info=DestroyPolygonThreadSet(polygon_info);
4088 if (image->debug != MagickFalse)
4089 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4090 " end draw-polygon");
4094 Draw polygon or line.
4096 if (image->alpha_trait == UndefinedPixelTrait)
4097 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4098 start_y=(ssize_t) ceil(bounds.y1-0.5);
4099 stop_y=(ssize_t) floor(bounds.y2+0.5);
4100 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4101 #pragma omp parallel for schedule(static,4) shared(status) \
4102 magick_threads(image,image,1,1)
4104 for (y=start_y; y <= stop_y; y++)
4107 id = GetOpenMPThreadId();
4127 if (status == MagickFalse)
4129 start_x=(ssize_t) ceil(bounds.x1-0.5);
4130 stop_x=(ssize_t) floor(bounds.x2+0.5);
4131 q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+ 1),1,exception);
4132 if (q == (Quantum *) NULL)
4137 for (x=start_x; x <= stop_x; x++)
4142 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4144 if (draw_info->stroke_antialias == MagickFalse)
4146 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4147 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4149 GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4150 fill_alpha=fill_alpha*fill_color.alpha;
4151 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4152 GetPixelAlpha(image,q),q);
4153 GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4154 stroke_alpha=stroke_alpha*stroke_color.alpha;
4155 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4156 GetPixelAlpha(image,q),q);
4157 q+=GetPixelChannels(image);
4159 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4162 image_view=DestroyCacheView(image_view);
4163 polygon_info=DestroyPolygonThreadSet(polygon_info);
4164 if (image->debug != MagickFalse)
4165 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4174 % D r a w P r i m i t i v e %
4178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4180 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4182 % The format of the DrawPrimitive method is:
4184 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4185 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4187 % A description of each parameter follows:
4189 % o image: the image.
4191 % o draw_info: the draw info.
4193 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4195 % o exception: return any errors or warnings in this structure.
4199 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4225 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4226 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4227 switch (primitive_info->primitive)
4229 case AlphaPrimitive:
4231 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4232 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4233 methods[primitive_info->method]);
4236 case ColorPrimitive:
4238 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4239 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4240 methods[primitive_info->method]);
4243 case ImagePrimitive:
4245 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4246 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4249 case PointPrimitive:
4251 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4252 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4253 methods[primitive_info->method]);
4258 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4259 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4266 p=primitive_info[0].point;
4269 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4271 point=primitive_info[i].point;
4272 if (coordinates <= 0)
4274 coordinates=(ssize_t) primitive_info[i].coordinates;
4275 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4276 " begin open (%.20g)",(double) coordinates);
4279 point=primitive_info[i].point;
4280 if ((fabs(q.x-point.x) >= DrawEpsilon) ||
4281 (fabs(q.y-point.y) >= DrawEpsilon))
4282 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4283 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4285 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4286 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4289 if (coordinates > 0)
4291 if ((fabs(p.x-point.x) >= DrawEpsilon) ||
4292 (fabs(p.y-point.y) >= DrawEpsilon))
4293 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4294 (double) coordinates);
4296 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4297 (double) coordinates);
4301 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4302 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4303 ExceptionInfo *exception)
4318 if (image->debug != MagickFalse)
4320 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4321 " begin draw-primitive");
4322 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4323 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
4324 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4325 draw_info->affine.tx,draw_info->affine.ty);
4327 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4328 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4329 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4330 (void) SetImageColorspace(image,sRGBColorspace,exception);
4332 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4333 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4334 image_view=AcquireAuthenticCacheView(image,exception);
4335 switch (primitive_info->primitive)
4337 case AlphaPrimitive:
4339 if (image->alpha_trait == UndefinedPixelTrait)
4340 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4341 switch (primitive_info->method)
4352 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4353 if (q == (Quantum *) NULL)
4355 GetFillColor(draw_info,x,y,&pixel,exception);
4356 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4357 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4369 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4371 GetPixelInfo(image,&pixel);
4372 for (y=0; y < (ssize_t) image->rows; y++)
4377 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4379 if (q == (Quantum *) NULL)
4381 for (x=0; x < (ssize_t) image->columns; x++)
4383 GetPixelInfoPixel(image,q,&pixel);
4384 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4386 q+=GetPixelChannels(image);
4389 GetFillColor(draw_info,x,y,&pixel,exception);
4390 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4391 q+=GetPixelChannels(image);
4393 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4394 if (sync == MagickFalse)
4399 case FloodfillMethod:
4400 case FillToBorderMethod:
4408 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4410 if (primitive_info->method == FillToBorderMethod)
4412 target.red=(double) draw_info->border_color.red;
4413 target.green=(double) draw_info->border_color.green;
4414 target.blue=(double) draw_info->border_color.blue;
4416 channel_mask=SetImageChannelMask(image,AlphaChannel);
4417 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4418 primitive_info->method == FloodfillMethod ? MagickFalse :
4419 MagickTrue,exception);
4420 (void) SetImageChannelMask(image,channel_mask);
4431 for (y=0; y < (ssize_t) image->rows; y++)
4436 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4438 if (q == (Quantum *) NULL)
4440 for (x=0; x < (ssize_t) image->columns; x++)
4442 GetFillColor(draw_info,x,y,&pixel,exception);
4443 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4444 q+=GetPixelChannels(image);
4446 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4447 if (sync == MagickFalse)
4455 case ColorPrimitive:
4457 switch (primitive_info->method)
4468 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4469 if (q == (Quantum *) NULL)
4471 GetPixelInfo(image,&pixel);
4472 GetFillColor(draw_info,x,y,&pixel,exception);
4473 SetPixelViaPixelInfo(image,&pixel,q);
4474 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4486 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4488 for (y=0; y < (ssize_t) image->rows; y++)
4493 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4495 if (q == (Quantum *) NULL)
4497 for (x=0; x < (ssize_t) image->columns; x++)
4499 GetPixelInfoPixel(image,q,&pixel);
4500 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4502 q+=GetPixelChannels(image);
4505 GetFillColor(draw_info,x,y,&pixel,exception);
4506 SetPixelViaPixelInfo(image,&pixel,q);
4507 q+=GetPixelChannels(image);
4509 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4510 if (sync == MagickFalse)
4515 case FloodfillMethod:
4516 case FillToBorderMethod:
4521 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4523 if (primitive_info->method == FillToBorderMethod)
4525 target.red=(double) draw_info->border_color.red;
4526 target.green=(double) draw_info->border_color.green;
4527 target.blue=(double) draw_info->border_color.blue;
4529 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4530 primitive_info->method == FloodfillMethod ? MagickFalse :
4531 MagickTrue,exception);
4542 GetPixelInfo(image,&pixel);
4543 for (y=0; y < (ssize_t) image->rows; y++)
4548 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4550 if (q == (Quantum *) NULL)
4552 for (x=0; x < (ssize_t) image->columns; x++)
4554 GetFillColor(draw_info,x,y,&pixel,exception);
4555 SetPixelViaPixelInfo(image,&pixel,q);
4556 q+=GetPixelChannels(image);
4558 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4559 if (sync == MagickFalse)
4567 case ImagePrimitive:
4573 composite_geometry[MagickPathExtent];
4588 if (primitive_info->text == (char *) NULL)
4590 clone_info=AcquireImageInfo();
4591 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4592 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4596 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4598 composite_image=ReadImage(clone_info,exception);
4600 clone_info=DestroyImageInfo(clone_info);
4601 if (composite_image == (Image *) NULL)
4603 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4604 NULL,(void *) NULL);
4605 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4606 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4607 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4608 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4613 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4614 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
4615 composite_image->filter=image->filter;
4616 (void) TransformImage(&composite_image,(char *) NULL,
4617 composite_geometry,exception);
4619 if (composite_image->alpha_trait == UndefinedPixelTrait)
4620 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4622 if (draw_info->alpha != OpaqueAlpha)
4623 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4624 SetGeometry(image,&geometry);
4625 image->gravity=draw_info->gravity;
4628 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4629 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4630 composite_image->rows,(double) geometry.x,(double) geometry.y);
4631 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4632 affine=draw_info->affine;
4633 affine.tx=(double) geometry.x;
4634 affine.ty=(double) geometry.y;
4635 composite_image->interpolate=image->interpolate;
4636 if (draw_info->compose == OverCompositeOp)
4637 (void) DrawAffineImage(image,composite_image,&affine,exception);
4639 (void) CompositeImage(image,composite_image,draw_info->compose,
4640 MagickTrue,geometry.x,geometry.y,exception);
4641 composite_image=DestroyImage(composite_image);
4644 case PointPrimitive:
4652 if ((y < 0) || (y >= (ssize_t) image->rows))
4654 if ((x < 0) || (x >= (ssize_t) image->columns))
4656 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4657 if (q == (Quantum *) NULL)
4659 GetFillColor(draw_info,x,y,&fill_color,exception);
4660 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4661 (double) GetPixelAlpha(image,q),q);
4662 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4668 geometry[MagickPathExtent];
4673 if (primitive_info->text == (char *) NULL)
4675 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4676 (void) CloneString(&clone_info->text,primitive_info->text);
4677 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4678 primitive_info->point.x,primitive_info->point.y);
4679 (void) CloneString(&clone_info->geometry,geometry);
4680 status&=AnnotateImage(image,clone_info,exception);
4681 clone_info=DestroyDrawInfo(clone_info);
4693 if (IsEventLogging() != MagickFalse)
4694 LogPrimitiveInfo(primitive_info);
4695 scale=ExpandAffine(&draw_info->affine);
4696 if ((draw_info->dash_pattern != (double *) NULL) &&
4697 (fabs(draw_info->dash_pattern[0]) >= DrawEpsilon) &&
4698 (fabs(scale*draw_info->stroke_width) >= DrawEpsilon) &&
4699 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4704 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4705 clone_info->stroke_width=0.0;
4706 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4707 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4709 clone_info=DestroyDrawInfo(clone_info);
4710 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4713 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4715 ((draw_info->stroke.alpha != (Quantum) TransparentAlpha) ||
4716 (draw_info->stroke_pattern != (Image *) NULL)))
4722 Draw strokes while respecting line cap/join attributes.
4724 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4726 (fabs(primitive_info[i-1].point.x-primitive_info[0].point.x) < DrawEpsilon) &&
4727 (fabs(primitive_info[i-1].point.y-primitive_info[0].point.y) < DrawEpsilon) ?
4728 MagickTrue : MagickFalse;
4729 i=(ssize_t) primitive_info[0].coordinates;
4730 if (((closed_path != MagickFalse) &&
4731 (draw_info->linejoin == RoundJoin)) ||
4732 (primitive_info[i].primitive != UndefinedPrimitive))
4734 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4738 if (draw_info->linecap == RoundCap)
4740 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4744 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4745 clone_info->stroke_width=0.0;
4746 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4747 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4749 clone_info=DestroyDrawInfo(clone_info);
4750 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4753 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4757 image_view=DestroyCacheView(image_view);
4758 if (image->debug != MagickFalse)
4759 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4760 return(status != 0 ? MagickTrue : MagickFalse);
4764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4768 + D r a w S t r o k e P o l y g o n %
4772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4774 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4775 % the image while respecting the line cap and join attributes.
4777 % The format of the DrawStrokePolygon method is:
4779 % MagickBooleanType DrawStrokePolygon(Image *image,
4780 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4782 % A description of each parameter follows:
4784 % o image: the image.
4786 % o draw_info: the draw info.
4788 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4793 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4794 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4802 for (i=0; i < 4; i++)
4803 linecap[i]=(*primitive_info);
4804 linecap[0].coordinates=4;
4805 linecap[1].point.x+=2.0*DrawEpsilon;
4806 linecap[2].point.x+=2.0*DrawEpsilon;
4807 linecap[2].point.y+=2.0*DrawEpsilon;
4808 linecap[3].point.y+=2.0*DrawEpsilon;
4809 linecap[4].primitive=UndefinedPrimitive;
4810 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4813 static MagickBooleanType DrawStrokePolygon(Image *image,
4814 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4815 ExceptionInfo *exception)
4829 register const PrimitiveInfo
4834 Draw stroked polygon.
4836 if (image->debug != MagickFalse)
4837 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4838 " begin draw-stroke-polygon");
4839 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4840 clone_info->fill=draw_info->stroke;
4841 if (clone_info->fill_pattern != (Image *) NULL)
4842 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4843 if (clone_info->stroke_pattern != (Image *) NULL)
4844 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4845 MagickTrue,exception);
4846 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4847 clone_info->stroke_width=0.0;
4848 clone_info->fill_rule=NonZeroRule;
4850 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4852 stroke_polygon=TraceStrokePolygon(draw_info,p);
4853 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4856 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4857 q=p+p->coordinates-1;
4858 closed_path=(fabs(q->point.x-p->point.x) < DrawEpsilon) &&
4859 (fabs(q->point.y-p->point.y) < DrawEpsilon) ? MagickTrue : MagickFalse;
4860 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4862 DrawRoundLinecap(image,draw_info,p,exception);
4863 DrawRoundLinecap(image,draw_info,q,exception);
4866 clone_info=DestroyDrawInfo(clone_info);
4867 if (image->debug != MagickFalse)
4868 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4869 " end draw-stroke-polygon");
4870 return(status != 0 ? MagickTrue : MagickFalse);
4874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4878 % G e t A f f i n e M a t r i x %
4882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4884 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4887 % The format of the GetAffineMatrix method is:
4889 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4891 % A description of each parameter follows:
4893 % o affine_matrix: the affine matrix.
4896 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4898 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4899 assert(affine_matrix != (AffineMatrix *) NULL);
4900 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4901 affine_matrix->sx=1.0;
4902 affine_matrix->sy=1.0;
4906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4910 + G e t D r a w I n f o %
4914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4916 % GetDrawInfo() initializes draw_info to default values from image_info.
4918 % The format of the GetDrawInfo method is:
4920 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4922 % A description of each parameter follows:
4924 % o image_info: the image info..
4926 % o draw_info: the draw info.
4929 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4944 Initialize draw attributes.
4946 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4947 assert(draw_info != (DrawInfo *) NULL);
4948 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4949 clone_info=CloneImageInfo(image_info);
4950 GetAffineMatrix(&draw_info->affine);
4951 exception=AcquireExceptionInfo();
4952 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4954 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4956 draw_info->stroke_width=1.0;
4957 draw_info->fill_rule=EvenOddRule;
4958 draw_info->alpha=OpaqueAlpha;
4959 draw_info->fill_alpha=OpaqueAlpha;
4960 draw_info->stroke_alpha=OpaqueAlpha;
4961 draw_info->linecap=ButtCap;
4962 draw_info->linejoin=MiterJoin;
4963 draw_info->miterlimit=10;
4964 draw_info->decorate=NoDecoration;
4965 draw_info->pointsize=12.0;
4966 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
4967 draw_info->compose=OverCompositeOp;
4968 draw_info->render=MagickTrue;
4969 draw_info->debug=IsEventLogging();
4970 draw_info->stroke_antialias=clone_info->antialias;
4971 if (clone_info->font != (char *) NULL)
4972 draw_info->font=AcquireString(clone_info->font);
4973 if (clone_info->density != (char *) NULL)
4974 draw_info->density=AcquireString(clone_info->density);
4975 draw_info->text_antialias=clone_info->antialias;
4976 if (fabs(clone_info->pointsize) >= DrawEpsilon)
4977 draw_info->pointsize=clone_info->pointsize;
4978 draw_info->border_color=clone_info->border_color;
4979 if (clone_info->server_name != (char *) NULL)
4980 draw_info->server_name=AcquireString(clone_info->server_name);
4981 option=GetImageOption(clone_info,"direction");
4982 if (option != (const char *) NULL)
4983 draw_info->direction=(DirectionType) ParseCommandOption(
4984 MagickDirectionOptions,MagickFalse,option);
4986 draw_info->direction=UndefinedDirection;
4987 option=GetImageOption(clone_info,"encoding");
4988 if (option != (const char *) NULL)
4989 (void) CloneString(&draw_info->encoding,option);
4990 option=GetImageOption(clone_info,"family");
4991 if (option != (const char *) NULL)
4992 (void) CloneString(&draw_info->family,option);
4993 option=GetImageOption(clone_info,"fill");
4994 if (option != (const char *) NULL)
4995 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4997 option=GetImageOption(clone_info,"gravity");
4998 if (option != (const char *) NULL)
4999 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5000 MagickFalse,option);
5001 option=GetImageOption(clone_info,"interline-spacing");
5002 if (option != (const char *) NULL)
5003 draw_info->interline_spacing=StringToDouble(option,&next_token);
5004 option=GetImageOption(clone_info,"interword-spacing");
5005 if (option != (const char *) NULL)
5006 draw_info->interword_spacing=StringToDouble(option,&next_token);
5007 option=GetImageOption(clone_info,"kerning");
5008 if (option != (const char *) NULL)
5009 draw_info->kerning=StringToDouble(option,&next_token);
5010 option=GetImageOption(clone_info,"stroke");
5011 if (option != (const char *) NULL)
5012 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
5014 option=GetImageOption(clone_info,"strokewidth");
5015 if (option != (const char *) NULL)
5016 draw_info->stroke_width=StringToDouble(option,&next_token);
5017 option=GetImageOption(clone_info,"style");
5018 if (option != (const char *) NULL)
5019 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
5020 MagickFalse,option);
5021 option=GetImageOption(clone_info,"undercolor");
5022 if (option != (const char *) NULL)
5023 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
5025 option=GetImageOption(clone_info,"weight");
5026 if (option != (const char *) NULL)
5031 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
5033 weight=(ssize_t) StringToUnsignedLong(option);
5034 draw_info->weight=(size_t) weight;
5036 exception=DestroyExceptionInfo(exception);
5037 draw_info->signature=MagickCoreSignature;
5038 clone_info=DestroyImageInfo(clone_info);
5042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5046 + P e r m u t a t e %
5050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5052 % Permutate() returns the permuation of the (n,k).
5054 % The format of the Permutate method is:
5056 % void Permutate(ssize_t n,ssize_t k)
5058 % A description of each parameter follows:
5066 static inline double Permutate(const ssize_t n,const ssize_t k)
5075 for (i=k+1; i <= n; i++)
5077 for (i=1; i <= (n-k); i++)
5083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5087 + T r a c e P r i m i t i v e %
5091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5093 % TracePrimitive is a collection of methods for generating graphic
5094 % primitives such as arcs, ellipses, paths, etc.
5098 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
5099 const PointInfo end,const PointInfo degrees)
5105 center.x=0.5*(end.x+start.x);
5106 center.y=0.5*(end.y+start.y);
5107 radii.x=fabs(center.x-start.x);
5108 radii.y=fabs(center.y-start.y);
5109 TraceEllipse(primitive_info,center,radii,degrees);
5112 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
5113 const PointInfo end,const PointInfo arc,const double angle,
5114 const MagickBooleanType large_arc,const MagickBooleanType sweep)
5133 register PrimitiveInfo
5142 if ((fabs(start.x-end.x) < DrawEpsilon) &&
5143 (fabs(start.y-end.y) < DrawEpsilon))
5145 TracePoint(primitive_info,end);
5148 radii.x=fabs(arc.x);
5149 radii.y=fabs(arc.y);
5150 if ((fabs(radii.x) < DrawEpsilon) || (fabs(radii.y) < DrawEpsilon))
5152 TraceLine(primitive_info,start,end);
5155 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5156 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5157 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5158 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5159 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5161 if (delta < DrawEpsilon)
5163 TraceLine(primitive_info,start,end);
5168 radii.x*=sqrt((double) delta);
5169 radii.y*=sqrt((double) delta);
5171 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5172 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5173 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5174 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5175 alpha=points[1].x-points[0].x;
5176 beta=points[1].y-points[0].y;
5177 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5182 factor=sqrt((double) factor);
5183 if (sweep == large_arc)
5186 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5187 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5188 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5189 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5190 if ((theta < 0.0) && (sweep != MagickFalse))
5191 theta+=2.0*MagickPI;
5193 if ((theta > 0.0) && (sweep == MagickFalse))
5194 theta-=2.0*MagickPI;
5195 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+DrawEpsilon))));
5197 for (i=0; i < (ssize_t) arc_segments; i++)
5199 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5200 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5201 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5202 sin(fmod((double) beta,DegreesToRadians(360.0)));
5203 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5204 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5205 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5206 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5207 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5208 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5209 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5210 theta/arc_segments),DegreesToRadians(360.0))));
5211 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5212 theta/arc_segments),DegreesToRadians(360.0))));
5213 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5214 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5215 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5216 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5217 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5218 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5219 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5221 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5223 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5225 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5227 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5229 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5231 if (i == (ssize_t) (arc_segments-1))
5236 primitive_info->coordinates=(size_t) (p-primitive_info);
5237 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5239 p->primitive=primitive_info->primitive;
5244 static void TraceBezier(PrimitiveInfo *primitive_info,
5245 const size_t number_coordinates)
5257 register PrimitiveInfo
5269 Allocate coeficients.
5271 quantum=number_coordinates;
5272 for (i=0; i < (ssize_t) number_coordinates; i++)
5274 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5276 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5277 if (alpha > (double) quantum)
5278 quantum=(size_t) alpha;
5279 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5280 if (alpha > (double) quantum)
5281 quantum=(size_t) alpha;
5284 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5285 (double) BezierQuantum);
5286 control_points=quantum*number_coordinates;
5287 coefficients=(double *) AcquireQuantumMemory((size_t)
5288 number_coordinates,sizeof(*coefficients));
5289 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5291 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
5292 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5294 Compute bezier points.
5296 end=primitive_info[number_coordinates-1].point;
5297 for (i=0; i < (ssize_t) number_coordinates; i++)
5298 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5300 for (i=0; i < (ssize_t) control_points; i++)
5305 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5306 for (j=0; j < (ssize_t) number_coordinates; j++)
5308 point.x+=alpha*coefficients[j]*p->point.x;
5309 point.y+=alpha*coefficients[j]*p->point.y;
5310 alpha*=weight/(1.0-weight);
5314 weight+=1.0/control_points;
5317 Bezier curves are just short segmented polys.
5320 for (i=0; i < (ssize_t) control_points; i++)
5322 TracePoint(p,points[i]);
5327 primitive_info->coordinates=(size_t) (p-primitive_info);
5328 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5330 p->primitive=primitive_info->primitive;
5333 points=(PointInfo *) RelinquishMagickMemory(points);
5334 coefficients=(double *) RelinquishMagickMemory(coefficients);
5337 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5338 const PointInfo end)
5349 alpha=end.x-start.x;
5351 radius=hypot((double) alpha,(double) beta);
5352 offset.x=(double) radius;
5353 offset.y=(double) radius;
5356 TraceEllipse(primitive_info,start,offset,degrees);
5359 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5360 const PointInfo stop,const PointInfo degrees)
5371 register PrimitiveInfo
5378 Ellipses are just short segmented polys.
5380 if ((fabs(stop.x) < DrawEpsilon) && (fabs(stop.y) < DrawEpsilon))
5382 TracePoint(primitive_info,start);
5385 delta=2.0/MagickMax(stop.x,stop.y);
5387 if ((delta >= 0.0) && (delta < (MagickPI/8.0)))
5388 step=MagickPI/(4*(MagickPI/delta/2+0.5));
5389 angle.x=DegreesToRadians(degrees.x);
5391 while (y < degrees.x)
5393 angle.y=DegreesToRadians(y);
5394 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5396 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5397 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5398 TracePoint(p,point);
5401 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5402 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5403 TracePoint(p,point);
5405 primitive_info->coordinates=(size_t) (p-primitive_info);
5406 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5408 p->primitive=primitive_info->primitive;
5413 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5414 const PointInfo end)
5416 TracePoint(primitive_info,start);
5417 if ((fabs(start.x-end.x) < DrawEpsilon) &&
5418 (fabs(start.y-end.y) < DrawEpsilon))
5420 primitive_info->primitive=PointPrimitive;
5421 primitive_info->coordinates=1;
5424 TracePoint(primitive_info+1,end);
5425 (primitive_info+1)->primitive=primitive_info->primitive;
5426 primitive_info->coordinates=2;
5429 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5433 token[MagickPathExtent];
5448 points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5455 register PrimitiveInfo
5466 number_coordinates=0;
5468 primitive_type=primitive_info->primitive;
5470 for (p=path; *p != '\0'; )
5472 while (isspace((int) ((unsigned char) *p)) != 0)
5476 last_attribute=attribute;
5477 attribute=(int) (*p++);
5498 GetNextToken(p,&p,MagickPathExtent,token);
5500 GetNextToken(p,&p,MagickPathExtent,token);
5501 arc.x=StringToDouble(token,&next_token);
5502 GetNextToken(p,&p,MagickPathExtent,token);
5504 GetNextToken(p,&p,MagickPathExtent,token);
5505 arc.y=StringToDouble(token,&next_token);
5506 GetNextToken(p,&p,MagickPathExtent,token);
5508 GetNextToken(p,&p,MagickPathExtent,token);
5509 angle=StringToDouble(token,&next_token);
5510 GetNextToken(p,&p,MagickPathExtent,token);
5512 GetNextToken(p,&p,MagickPathExtent,token);
5513 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5514 GetNextToken(p,&p,MagickPathExtent,token);
5516 GetNextToken(p,&p,MagickPathExtent,token);
5517 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5518 GetNextToken(p,&p,MagickPathExtent,token);
5520 GetNextToken(p,&p,MagickPathExtent,token);
5521 x=StringToDouble(token,&next_token);
5522 GetNextToken(p,&p,MagickPathExtent,token);
5524 GetNextToken(p,&p,MagickPathExtent,token);
5525 y=StringToDouble(token,&next_token);
5526 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5527 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5528 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5531 while (isspace((int) ((unsigned char) *p)) != 0)
5535 } while (IsPoint(p) != MagickFalse);
5542 Compute bezier points.
5547 for (i=1; i < 4; i++)
5549 GetNextToken(p,&p,MagickPathExtent,token);
5551 GetNextToken(p,&p,MagickPathExtent,token);
5552 x=StringToDouble(token,&next_token);
5553 GetNextToken(p,&p,MagickPathExtent,token);
5555 GetNextToken(p,&p,MagickPathExtent,token);
5556 y=StringToDouble(token,&next_token);
5557 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5558 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5561 for (i=0; i < 4; i++)
5562 (q+i)->point=points[i];
5566 } while (IsPoint(p) != MagickFalse);
5574 GetNextToken(p,&p,MagickPathExtent,token);
5576 GetNextToken(p,&p,MagickPathExtent,token);
5577 x=StringToDouble(token,&next_token);
5578 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5579 TracePoint(q,point);
5581 } while (IsPoint(p) != MagickFalse);
5589 GetNextToken(p,&p,MagickPathExtent,token);
5591 GetNextToken(p,&p,MagickPathExtent,token);
5592 x=StringToDouble(token,&next_token);
5593 GetNextToken(p,&p,MagickPathExtent,token);
5595 GetNextToken(p,&p,MagickPathExtent,token);
5596 y=StringToDouble(token,&next_token);
5597 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5598 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5599 TracePoint(q,point);
5601 } while (IsPoint(p) != MagickFalse);
5607 if (q != primitive_info)
5609 primitive_info->coordinates=(size_t) (q-primitive_info);
5610 number_coordinates+=primitive_info->coordinates;
5616 GetNextToken(p,&p,MagickPathExtent,token);
5618 GetNextToken(p,&p,MagickPathExtent,token);
5619 x=StringToDouble(token,&next_token);
5620 GetNextToken(p,&p,MagickPathExtent,token);
5622 GetNextToken(p,&p,MagickPathExtent,token);
5623 y=StringToDouble(token,&next_token);
5624 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5625 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5629 TracePoint(q,point);
5631 if ((i != 0) && (attribute == (int) 'M'))
5633 TracePoint(q,point);
5636 } while (IsPoint(p) != MagickFalse);
5643 Compute bezier points.
5648 for (i=1; i < 3; i++)
5650 GetNextToken(p,&p,MagickPathExtent,token);
5652 GetNextToken(p,&p,MagickPathExtent,token);
5653 x=StringToDouble(token,&next_token);
5654 GetNextToken(p,&p,MagickPathExtent,token);
5656 GetNextToken(p,&p,MagickPathExtent,token);
5657 y=StringToDouble(token,&next_token);
5660 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5661 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5664 for (i=0; i < 3; i++)
5665 (q+i)->point=points[i];
5669 } while (IsPoint(p) != MagickFalse);
5676 Compute bezier points.
5680 points[0]=points[3];
5681 points[1].x=2.0*points[3].x-points[2].x;
5682 points[1].y=2.0*points[3].y-points[2].y;
5683 for (i=2; i < 4; i++)
5685 GetNextToken(p,&p,MagickPathExtent,token);
5687 GetNextToken(p,&p,MagickPathExtent,token);
5688 x=StringToDouble(token,&next_token);
5689 GetNextToken(p,&p,MagickPathExtent,token);
5691 GetNextToken(p,&p,MagickPathExtent,token);
5692 y=StringToDouble(token,&next_token);
5695 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5696 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5699 if (strchr("CcSs",last_attribute) == (char *) NULL)
5704 for (i=0; i < 4; i++)
5705 (q+i)->point=points[i];
5709 } while (IsPoint(p) != MagickFalse);
5716 Compute bezier points.
5720 points[0]=points[2];
5721 points[1].x=2.0*points[2].x-points[1].x;
5722 points[1].y=2.0*points[2].y-points[1].y;
5723 for (i=2; i < 3; i++)
5725 GetNextToken(p,&p,MagickPathExtent,token);
5727 GetNextToken(p,&p,MagickPathExtent,token);
5728 x=StringToDouble(token,&next_token);
5729 GetNextToken(p,&p,MagickPathExtent,token);
5731 GetNextToken(p,&p,MagickPathExtent,token);
5732 y=StringToDouble(token,&next_token);
5733 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5734 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5737 if (strchr("QqTt",last_attribute) == (char *) NULL)
5742 for (i=0; i < 3; i++)
5743 (q+i)->point=points[i];
5747 } while (IsPoint(p) != MagickFalse);
5755 GetNextToken(p,&p,MagickPathExtent,token);
5757 GetNextToken(p,&p,MagickPathExtent,token);
5758 y=StringToDouble(token,&next_token);
5759 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5760 TracePoint(q,point);
5762 } while (IsPoint(p) != MagickFalse);
5769 TracePoint(q,point);
5771 primitive_info->coordinates=(size_t) (q-primitive_info);
5772 number_coordinates+=primitive_info->coordinates;
5779 if (isalpha((int) ((unsigned char) attribute)) != 0)
5780 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5786 primitive_info->coordinates=(size_t) (q-primitive_info);
5787 number_coordinates+=primitive_info->coordinates;
5788 for (i=0; i < (ssize_t) number_coordinates; i++)
5791 q->primitive=primitive_type;
5793 q->method=FillToBorderMethod;
5796 return(number_coordinates);
5799 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5800 const PointInfo end)
5805 register PrimitiveInfo
5812 TracePoint(p,start);
5816 TracePoint(p,point);
5822 TracePoint(p,point);
5824 TracePoint(p,start);
5826 primitive_info->coordinates=(size_t) (p-primitive_info);
5827 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5829 p->primitive=primitive_info->primitive;
5834 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5835 const PointInfo start,const PointInfo end,PointInfo arc)
5842 register PrimitiveInfo
5849 offset.x=fabs(end.x-start.x);
5850 offset.y=fabs(end.y-start.y);
5851 if (arc.x > (0.5*offset.x))
5853 if (arc.y > (0.5*offset.y))
5855 point.x=start.x+offset.x-arc.x;
5856 point.y=start.y+arc.y;
5859 TraceEllipse(p,point,arc,degrees);
5861 point.x=start.x+offset.x-arc.x;
5862 point.y=start.y+offset.y-arc.y;
5865 TraceEllipse(p,point,arc,degrees);
5867 point.x=start.x+arc.x;
5868 point.y=start.y+offset.y-arc.y;
5871 TraceEllipse(p,point,arc,degrees);
5873 point.x=start.x+arc.x;
5874 point.y=start.y+arc.y;
5877 TraceEllipse(p,point,arc,degrees);
5879 TracePoint(p,primitive_info->point);
5881 primitive_info->coordinates=(size_t) (p-primitive_info);
5882 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5884 p->primitive=primitive_info->primitive;
5889 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5890 const size_t number_vertices,const double offset)
5907 for (i=1; i < (ssize_t) number_vertices; i++)
5909 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5910 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5911 if ((fabs((double) dx) >= DrawEpsilon) ||
5912 (fabs((double) dy) >= DrawEpsilon))
5915 if (i == (ssize_t) number_vertices)
5916 i=(ssize_t) number_vertices-1L;
5917 distance=hypot((double) dx,(double) dy);
5918 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5919 dx*(distance+offset)/distance);
5920 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5921 dy*(distance+offset)/distance);
5922 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5924 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5925 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5926 if ((fabs((double) dx) >= DrawEpsilon) ||
5927 (fabs((double) dy) >= DrawEpsilon))
5930 distance=hypot((double) dx,(double) dy);
5931 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5932 dx*(distance+offset)/distance);
5933 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5934 dy*(distance+offset)/distance);
5937 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5938 const PrimitiveInfo *primitive_info)
5940 typedef struct _LineSegment
5992 number_vertices=primitive_info->coordinates;
5993 max_strokes=2*number_vertices+6*BezierQuantum+360;
5994 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5996 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5998 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5999 number_vertices+2UL,sizeof(*polygon_primitive));
6000 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
6001 (polygon_primitive == (PrimitiveInfo *) NULL))
6002 return((PrimitiveInfo *) NULL);
6003 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
6004 number_vertices*sizeof(*polygon_primitive));
6006 (fabs(primitive_info[number_vertices-1].point.x-primitive_info[0].point.x) < DrawEpsilon) &&
6007 (fabs(primitive_info[number_vertices-1].point.y-primitive_info[0].point.y) < DrawEpsilon) ?
6008 MagickTrue : MagickFalse;
6009 if (((draw_info->linejoin == RoundJoin) ||
6010 (draw_info->linejoin == MiterJoin)) && (closed_path != MagickFalse))
6012 polygon_primitive[number_vertices]=primitive_info[1];
6015 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
6017 Compute the slope for the first line segment, p.
6021 for (n=1; n < (ssize_t) number_vertices; n++)
6023 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
6024 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
6025 if ((fabs(dx.p) >= DrawEpsilon) || (fabs(dy.p) >= DrawEpsilon))
6028 if (n == (ssize_t) number_vertices)
6029 n=(ssize_t) number_vertices-1L;
6031 inverse_slope.p=0.0;
6032 if (fabs(dx.p) < DrawEpsilon)
6035 slope.p=dy.p < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6037 slope.p=dy.p < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6040 if (fabs(dy.p) < DrawEpsilon)
6043 inverse_slope.p=dx.p < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6045 inverse_slope.p=dx.p < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6050 inverse_slope.p=(-1.0/slope.p);
6052 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
6053 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
6054 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
6055 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
6056 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
6057 offset.y=(double) (offset.x*inverse_slope.p);
6058 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
6060 box_p[0].x=polygon_primitive[0].point.x-offset.x;
6061 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
6062 box_p[1].x=polygon_primitive[n].point.x-offset.x;
6063 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
6064 box_q[0].x=polygon_primitive[0].point.x+offset.x;
6065 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
6066 box_q[1].x=polygon_primitive[n].point.x+offset.x;
6067 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
6071 box_p[0].x=polygon_primitive[0].point.x+offset.x;
6072 box_p[0].y=polygon_primitive[0].point.y+offset.y;
6073 box_p[1].x=polygon_primitive[n].point.x+offset.x;
6074 box_p[1].y=polygon_primitive[n].point.y+offset.y;
6075 box_q[0].x=polygon_primitive[0].point.x-offset.x;
6076 box_q[0].y=polygon_primitive[0].point.y-offset.y;
6077 box_q[1].x=polygon_primitive[n].point.x-offset.x;
6078 box_q[1].y=polygon_primitive[n].point.y-offset.y;
6081 Create strokes for the line join attribute: bevel, miter, round.
6085 path_q[p++]=box_q[0];
6086 path_p[q++]=box_p[0];
6087 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
6090 Compute the slope for this line segment, q.
6092 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
6093 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
6094 dot_product=dx.q*dx.q+dy.q*dy.q;
6095 if (dot_product < 0.25)
6098 inverse_slope.q=0.0;
6099 if (fabs(dx.q) < DrawEpsilon)
6102 slope.q=dy.q < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6104 slope.q=dy.q < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6107 if (fabs(dy.q) < DrawEpsilon)
6110 inverse_slope.q=dx.q < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6112 inverse_slope.q=dx.q < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6117 inverse_slope.q=(-1.0/slope.q);
6119 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
6120 offset.y=(double) (offset.x*inverse_slope.q);
6121 dot_product=dy.q*offset.x-dx.q*offset.y;
6122 if (dot_product > 0.0)
6124 box_p[2].x=polygon_primitive[n].point.x-offset.x;
6125 box_p[2].y=polygon_primitive[n].point.y-offset.y;
6126 box_p[3].x=polygon_primitive[i].point.x-offset.x;
6127 box_p[3].y=polygon_primitive[i].point.y-offset.y;
6128 box_q[2].x=polygon_primitive[n].point.x+offset.x;
6129 box_q[2].y=polygon_primitive[n].point.y+offset.y;
6130 box_q[3].x=polygon_primitive[i].point.x+offset.x;
6131 box_q[3].y=polygon_primitive[i].point.y+offset.y;
6135 box_p[2].x=polygon_primitive[n].point.x+offset.x;
6136 box_p[2].y=polygon_primitive[n].point.y+offset.y;
6137 box_p[3].x=polygon_primitive[i].point.x+offset.x;
6138 box_p[3].y=polygon_primitive[i].point.y+offset.y;
6139 box_q[2].x=polygon_primitive[n].point.x-offset.x;
6140 box_q[2].y=polygon_primitive[n].point.y-offset.y;
6141 box_q[3].x=polygon_primitive[i].point.x-offset.x;
6142 box_q[3].y=polygon_primitive[i].point.y-offset.y;
6144 if (fabs((double) (slope.p-slope.q)) < DrawEpsilon)
6151 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
6152 box_p[3].y)/(slope.p-slope.q));
6153 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
6154 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
6155 box_q[3].y)/(slope.p-slope.q));
6156 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
6158 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
6160 if (~max_strokes < (6*BezierQuantum+360))
6162 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6163 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6167 max_strokes+=6*BezierQuantum+360;
6168 path_p=(PointInfo *) ResizeQuantumMemory(path_p,max_strokes,
6170 path_q=(PointInfo *) ResizeQuantumMemory(path_q,max_strokes,
6173 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
6175 if (path_p != (PointInfo *) NULL)
6176 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6177 if (path_q != (PointInfo *) NULL)
6178 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6179 polygon_primitive=(PrimitiveInfo *)
6180 RelinquishMagickMemory(polygon_primitive);
6181 return((PrimitiveInfo *) NULL);
6184 dot_product=dx.q*dy.p-dx.p*dy.q;
6185 if (dot_product <= 0.0)
6186 switch (draw_info->linejoin)
6190 path_q[q++]=box_q[1];
6191 path_q[q++]=box_q[2];
6192 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6193 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6194 if (dot_product <= miterlimit)
6195 path_p[p++]=box_p[4];
6198 path_p[p++]=box_p[1];
6199 path_p[p++]=box_p[2];
6205 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6206 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6207 if (dot_product <= miterlimit)
6209 path_q[q++]=box_q[4];
6210 path_p[p++]=box_p[4];
6214 path_q[q++]=box_q[1];
6215 path_q[q++]=box_q[2];
6216 path_p[p++]=box_p[1];
6217 path_p[p++]=box_p[2];
6223 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6224 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6225 if (dot_product <= miterlimit)
6226 path_p[p++]=box_p[4];
6229 path_p[p++]=box_p[1];
6230 path_p[p++]=box_p[2];
6232 center=polygon_primitive[n].point;
6233 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6234 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6235 if (theta.q < theta.p)
6236 theta.q+=2.0*MagickPI;
6237 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6238 (2.0*sqrt((double) (1.0/mid)))));
6239 path_q[q].x=box_q[1].x;
6240 path_q[q].y=box_q[1].y;
6242 for (j=1; j < (ssize_t) arc_segments; j++)
6244 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6245 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6246 (theta.p+delta_theta),DegreesToRadians(360.0))));
6247 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6248 (theta.p+delta_theta),DegreesToRadians(360.0))));
6251 path_q[q++]=box_q[2];
6258 switch (draw_info->linejoin)
6262 path_p[p++]=box_p[1];
6263 path_p[p++]=box_p[2];
6264 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6265 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6266 if (dot_product <= miterlimit)
6267 path_q[q++]=box_q[4];
6270 path_q[q++]=box_q[1];
6271 path_q[q++]=box_q[2];
6277 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6278 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6279 if (dot_product <= miterlimit)
6281 path_q[q++]=box_q[4];
6282 path_p[p++]=box_p[4];
6286 path_q[q++]=box_q[1];
6287 path_q[q++]=box_q[2];
6288 path_p[p++]=box_p[1];
6289 path_p[p++]=box_p[2];
6295 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6296 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6297 if (dot_product <= miterlimit)
6298 path_q[q++]=box_q[4];
6301 path_q[q++]=box_q[1];
6302 path_q[q++]=box_q[2];
6304 center=polygon_primitive[n].point;
6305 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6306 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6307 if (theta.p < theta.q)
6308 theta.p+=2.0*MagickPI;
6309 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6310 (2.0*sqrt((double) (1.0/mid)))));
6311 path_p[p++]=box_p[1];
6312 for (j=1; j < (ssize_t) arc_segments; j++)
6314 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6315 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6316 (theta.p+delta_theta),DegreesToRadians(360.0))));
6317 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6318 (theta.p+delta_theta),DegreesToRadians(360.0))));
6321 path_p[p++]=box_p[2];
6328 inverse_slope.p=inverse_slope.q;
6337 path_p[p++]=box_p[1];
6338 path_q[q++]=box_q[1];
6340 Trace stroked polygon.
6342 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6343 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6344 if (stroke_polygon != (PrimitiveInfo *) NULL)
6346 for (i=0; i < (ssize_t) p; i++)
6348 stroke_polygon[i]=polygon_primitive[0];
6349 stroke_polygon[i].point=path_p[i];
6351 if (closed_path != MagickFalse)
6353 stroke_polygon[i]=polygon_primitive[0];
6354 stroke_polygon[i].point=stroke_polygon[0].point;
6357 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6359 stroke_polygon[i]=polygon_primitive[0];
6360 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6362 if (closed_path != MagickFalse)
6364 stroke_polygon[i]=polygon_primitive[0];
6365 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6368 stroke_polygon[i]=polygon_primitive[0];
6369 stroke_polygon[i].point=stroke_polygon[0].point;
6371 stroke_polygon[i].primitive=UndefinedPrimitive;
6372 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6374 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6375 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6376 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6377 return(stroke_polygon);