2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-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-6)
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; draw_info->dash_pattern[x] != 0.0; 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->alpha=draw_info->alpha;
345 clone_info->element_reference=draw_info->element_reference;
346 clone_info->debug=IsEventLogging();
347 exception=DestroyExceptionInfo(exception);
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 + C o n v e r t P a t h T o P o l y g o n %
360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 % ConvertPathToPolygon() converts a path to the more efficient sorted
365 % The format of the ConvertPathToPolygon method is:
367 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
368 % const PathInfo *path_info)
370 % A description of each parameter follows:
372 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
373 % rendering form of type PolygonInfo.
375 % o draw_info: Specifies a pointer to an DrawInfo structure.
377 % o path_info: Specifies a pointer to an PathInfo structure.
382 #if defined(__cplusplus) || defined(c_plusplus)
386 static int CompareEdges(const void *x,const void *y)
388 register const EdgeInfo
395 p=(const EdgeInfo *) x;
396 q=(const EdgeInfo *) y;
397 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
399 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
401 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
403 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
405 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
406 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
411 #if defined(__cplusplus) || defined(c_plusplus)
415 static void LogPolygonInfo(const PolygonInfo *polygon_info)
424 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
425 p=polygon_info->edges;
426 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
428 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
430 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
431 p->direction != MagickFalse ? "down" : "up");
432 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
433 p->ghostline != MagickFalse ? "transparent" : "opaque");
434 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
435 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
436 p->bounds.x2,p->bounds.y2);
437 for (j=0; j < (ssize_t) p->number_points; j++)
438 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
439 p->points[j].x,p->points[j].y);
442 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
445 static void ReversePoints(PointInfo *points,const size_t number_points)
453 for (i=0; i < (ssize_t) (number_points >> 1); i++)
456 points[i]=points[number_points-(i+1)];
457 points[number_points-(i+1)]=point;
461 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
490 Convert a path to the more efficient sorted rendering form.
492 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
493 if (polygon_info == (PolygonInfo *) NULL)
494 return((PolygonInfo *) NULL);
496 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
497 sizeof(*polygon_info->edges));
498 if (polygon_info->edges == (EdgeInfo *) NULL)
499 return((PolygonInfo *) NULL);
500 (void) ResetMagickMemory(polygon_info->edges,0,number_edges*
501 sizeof(*polygon_info->edges));
504 ghostline=MagickFalse;
507 points=(PointInfo *) NULL;
508 (void) ResetMagickMemory(&point,0,sizeof(point));
509 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
510 for (i=0; path_info[i].code != EndCode; i++)
512 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
513 (path_info[i].code == GhostlineCode))
518 if ((points != (PointInfo *) NULL) && (n >= 2))
520 if (edge == number_edges)
523 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
524 polygon_info->edges,(size_t) number_edges,
525 sizeof(*polygon_info->edges));
526 if (polygon_info->edges == (EdgeInfo *) NULL)
527 return((PolygonInfo *) NULL);
529 polygon_info->edges[edge].number_points=(size_t) n;
530 polygon_info->edges[edge].scanline=(-1.0);
531 polygon_info->edges[edge].highwater=0;
532 polygon_info->edges[edge].ghostline=ghostline;
533 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
535 ReversePoints(points,(size_t) n);
536 polygon_info->edges[edge].points=points;
537 polygon_info->edges[edge].bounds=bounds;
538 polygon_info->edges[edge].bounds.y1=points[0].y;
539 polygon_info->edges[edge].bounds.y2=points[n-1].y;
540 points=(PointInfo *) NULL;
541 ghostline=MagickFalse;
544 if (points == (PointInfo *) NULL)
547 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
549 if (points == (PointInfo *) NULL)
550 return((PolygonInfo *) NULL);
552 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
553 point=path_info[i].point;
564 next_direction=((path_info[i].point.y > point.y) ||
565 ((path_info[i].point.y == point.y) &&
566 (path_info[i].point.x > point.x))) ? 1 : -1;
567 if ((points != (PointInfo *) NULL) && (direction != 0) &&
568 (direction != next_direction))
574 if (edge == number_edges)
577 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
578 polygon_info->edges,(size_t) number_edges,
579 sizeof(*polygon_info->edges));
580 if (polygon_info->edges == (EdgeInfo *) NULL)
581 return((PolygonInfo *) NULL);
583 polygon_info->edges[edge].number_points=(size_t) n;
584 polygon_info->edges[edge].scanline=(-1.0);
585 polygon_info->edges[edge].highwater=0;
586 polygon_info->edges[edge].ghostline=ghostline;
587 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
589 ReversePoints(points,(size_t) n);
590 polygon_info->edges[edge].points=points;
591 polygon_info->edges[edge].bounds=bounds;
592 polygon_info->edges[edge].bounds.y1=points[0].y;
593 polygon_info->edges[edge].bounds.y2=points[n-1].y;
595 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
597 if (points == (PointInfo *) NULL)
598 return((PolygonInfo *) NULL);
600 ghostline=MagickFalse;
606 direction=next_direction;
607 if (points == (PointInfo *) NULL)
609 if (n == (ssize_t) number_points)
612 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
614 if (points == (PointInfo *) NULL)
615 return((PolygonInfo *) NULL);
617 point=path_info[i].point;
619 if (point.x < bounds.x1)
621 if (point.x > bounds.x2)
625 if (points != (PointInfo *) NULL)
628 points=(PointInfo *) RelinquishMagickMemory(points);
631 if (edge == number_edges)
634 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
635 polygon_info->edges,(size_t) number_edges,
636 sizeof(*polygon_info->edges));
637 if (polygon_info->edges == (EdgeInfo *) NULL)
638 return((PolygonInfo *) NULL);
640 polygon_info->edges[edge].number_points=(size_t) n;
641 polygon_info->edges[edge].scanline=(-1.0);
642 polygon_info->edges[edge].highwater=0;
643 polygon_info->edges[edge].ghostline=ghostline;
644 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
646 ReversePoints(points,(size_t) n);
647 polygon_info->edges[edge].points=points;
648 polygon_info->edges[edge].bounds=bounds;
649 polygon_info->edges[edge].bounds.y1=points[0].y;
650 polygon_info->edges[edge].bounds.y2=points[n-1].y;
651 ghostline=MagickFalse;
655 polygon_info->number_edges=edge;
656 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
657 sizeof(*polygon_info->edges),CompareEdges);
658 if (IsEventLogging() != MagickFalse)
659 LogPolygonInfo(polygon_info);
660 return(polygon_info);
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 + C o n v e r t P r i m i t i v e T o P a t h %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
677 % The format of the ConvertPrimitiveToPath method is:
679 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
680 % const PrimitiveInfo *primitive_info)
682 % A description of each parameter follows:
684 % o Method ConvertPrimitiveToPath returns a vector path structure of type
687 % o draw_info: a structure of type DrawInfo.
689 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
694 static void LogPathInfo(const PathInfo *path_info)
696 register const PathInfo
699 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
700 for (p=path_info; p->code != EndCode; p++)
701 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
702 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
703 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
704 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
706 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
709 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
730 Converts a PrimitiveInfo structure into a vector path structure.
732 switch (primitive_info->primitive)
739 return((PathInfo *) NULL);
743 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
744 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
746 if (path_info == (PathInfo *) NULL)
747 return((PathInfo *) NULL);
755 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
758 if (coordinates <= 0)
760 coordinates=(ssize_t) primitive_info[i].coordinates;
761 p=primitive_info[i].point;
767 Eliminate duplicate points.
769 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
770 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
772 path_info[n].code=code;
773 path_info[n].point=primitive_info[i].point;
774 q=primitive_info[i].point;
779 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
780 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
783 Mark the p point as open if it does not match the q.
785 path_info[start].code=OpenCode;
786 path_info[n].code=GhostlineCode;
787 path_info[n].point=primitive_info[i].point;
789 path_info[n].code=LineToCode;
790 path_info[n].point=p;
793 path_info[n].code=EndCode;
794 path_info[n].point.x=0.0;
795 path_info[n].point.y=0.0;
796 if (IsEventLogging() != MagickFalse)
797 LogPathInfo(path_info);
802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % D e s t r o y D r a w I n f o %
810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
815 % The format of the DestroyDrawInfo method is:
817 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
819 % A description of each parameter follows:
821 % o draw_info: the draw info.
824 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
826 if (draw_info->debug != MagickFalse)
827 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
828 assert(draw_info != (DrawInfo *) NULL);
829 assert(draw_info->signature == MagickCoreSignature);
830 if (draw_info->primitive != (char *) NULL)
831 draw_info->primitive=DestroyString(draw_info->primitive);
832 if (draw_info->text != (char *) NULL)
833 draw_info->text=DestroyString(draw_info->text);
834 if (draw_info->geometry != (char *) NULL)
835 draw_info->geometry=DestroyString(draw_info->geometry);
836 if (draw_info->fill_pattern != (Image *) NULL)
837 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
838 if (draw_info->stroke_pattern != (Image *) NULL)
839 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
840 if (draw_info->font != (char *) NULL)
841 draw_info->font=DestroyString(draw_info->font);
842 if (draw_info->metrics != (char *) NULL)
843 draw_info->metrics=DestroyString(draw_info->metrics);
844 if (draw_info->family != (char *) NULL)
845 draw_info->family=DestroyString(draw_info->family);
846 if (draw_info->encoding != (char *) NULL)
847 draw_info->encoding=DestroyString(draw_info->encoding);
848 if (draw_info->density != (char *) NULL)
849 draw_info->density=DestroyString(draw_info->density);
850 if (draw_info->server_name != (char *) NULL)
851 draw_info->server_name=(char *)
852 RelinquishMagickMemory(draw_info->server_name);
853 if (draw_info->dash_pattern != (double *) NULL)
854 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
855 draw_info->dash_pattern);
856 if (draw_info->gradient.stops != (StopInfo *) NULL)
857 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
858 draw_info->gradient.stops);
859 if (draw_info->clip_mask != (char *) NULL)
860 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
861 draw_info->signature=(~MagickCoreSignature);
862 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 + D e s t r o y E d g e %
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 % DestroyEdge() destroys the specified polygon edge.
879 % The format of the DestroyEdge method is:
881 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
883 % A description of each parameter follows:
885 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
887 % o edge: the polygon edge number to destroy.
890 static size_t DestroyEdge(PolygonInfo *polygon_info,
893 assert(edge < polygon_info->number_edges);
894 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
895 polygon_info->edges[edge].points);
896 polygon_info->number_edges--;
897 if (edge < polygon_info->number_edges)
898 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
899 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
900 return(polygon_info->number_edges);
904 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 + D e s t r o y P o l y g o n I n f o %
912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
914 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
916 % The format of the DestroyPolygonInfo method is:
918 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
920 % A description of each parameter follows:
922 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
925 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
930 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
931 polygon_info->edges[i].points=(PointInfo *)
932 RelinquishMagickMemory(polygon_info->edges[i].points);
933 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
934 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 % D r a w A f f i n e I m a g e %
946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 % DrawAffineImage() composites the source over the destination image as
949 % dictated by the affine transform.
951 % The format of the DrawAffineImage method is:
953 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
954 % const AffineMatrix *affine,ExceptionInfo *exception)
956 % A description of each parameter follows:
958 % o image: the image.
960 % o source: the source image.
962 % o affine: the affine transform.
964 % o exception: return any errors or warnings in this structure.
968 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
969 const double y,const SegmentInfo *edge)
982 Determine left and right edges.
984 inverse_edge.x1=edge->x1;
985 inverse_edge.y1=edge->y1;
986 inverse_edge.x2=edge->x2;
987 inverse_edge.y2=edge->y2;
988 z=affine->ry*y+affine->tx;
989 if (affine->sx >= MagickEpsilon)
991 intercept=(-z/affine->sx);
993 if (x > inverse_edge.x1)
995 intercept=(-z+(double) image->columns)/affine->sx;
997 if (x < inverse_edge.x2)
1001 if (affine->sx < -MagickEpsilon)
1003 intercept=(-z+(double) image->columns)/affine->sx;
1005 if (x > inverse_edge.x1)
1007 intercept=(-z/affine->sx);
1009 if (x < inverse_edge.x2)
1013 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1015 inverse_edge.x2=edge->x1;
1016 return(inverse_edge);
1019 Determine top and bottom edges.
1021 z=affine->sy*y+affine->ty;
1022 if (affine->rx >= MagickEpsilon)
1024 intercept=(-z/affine->rx);
1026 if (x > inverse_edge.x1)
1028 intercept=(-z+(double) image->rows)/affine->rx;
1030 if (x < inverse_edge.x2)
1034 if (affine->rx < -MagickEpsilon)
1036 intercept=(-z+(double) image->rows)/affine->rx;
1038 if (x > inverse_edge.x1)
1040 intercept=(-z/affine->rx);
1042 if (x < inverse_edge.x2)
1046 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1048 inverse_edge.x2=edge->x2;
1049 return(inverse_edge);
1051 return(inverse_edge);
1054 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1062 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1064 inverse_affine.sx=determinant*affine->sy;
1065 inverse_affine.rx=determinant*(-affine->rx);
1066 inverse_affine.ry=determinant*(-affine->ry);
1067 inverse_affine.sy=determinant*affine->sx;
1068 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1070 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1072 return(inverse_affine);
1075 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1076 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1108 Determine bounding box.
1110 assert(image != (Image *) NULL);
1111 assert(image->signature == MagickCoreSignature);
1112 if (image->debug != MagickFalse)
1113 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1114 assert(source != (const Image *) NULL);
1115 assert(source->signature == MagickCoreSignature);
1116 assert(affine != (AffineMatrix *) NULL);
1119 extent[1].x=(double) source->columns-1.0;
1121 extent[2].x=(double) source->columns-1.0;
1122 extent[2].y=(double) source->rows-1.0;
1124 extent[3].y=(double) source->rows-1.0;
1125 for (i=0; i < 4; i++)
1131 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1132 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1136 for (i=1; i < 4; i++)
1138 if (min.x > extent[i].x)
1140 if (min.y > extent[i].y)
1142 if (max.x < extent[i].x)
1144 if (max.y < extent[i].y)
1148 Affine transform image.
1150 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1151 return(MagickFalse);
1153 edge.x1=MagickMax(min.x,0.0);
1154 edge.y1=MagickMax(min.y,0.0);
1155 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1156 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1157 inverse_affine=InverseAffineMatrix(affine);
1158 GetPixelInfo(image,&zero);
1159 start=(ssize_t) ceil(edge.y1-0.5);
1160 stop=(ssize_t) floor(edge.y2+0.5);
1161 source_view=AcquireVirtualCacheView(source,exception);
1162 image_view=AcquireAuthenticCacheView(image,exception);
1163 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1164 #pragma omp parallel for schedule(static,4) shared(status) \
1165 magick_threads(source,image,1,1)
1167 for (y=start; y <= stop; y++)
1188 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1189 if (inverse_edge.x2 < inverse_edge.x1)
1191 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1192 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1194 if (q == (Quantum *) NULL)
1199 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1201 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1203 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1205 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1206 point.x,point.y,&pixel,exception);
1207 GetPixelInfoPixel(image,q,&composite);
1208 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1210 SetPixelViaPixelInfo(image,&composite,q);
1212 q+=GetPixelChannels(image);
1214 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1217 source_view=DestroyCacheView(source_view);
1218 image_view=DestroyCacheView(image_view);
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227 + D r a w B o u n d i n g R e c t a n g l e s %
1231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1234 % is only useful for developers debugging the rendering algorithm.
1236 % The format of the DrawBoundingRectangles method is:
1238 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1239 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1241 % A description of each parameter follows:
1243 % o image: the image.
1245 % o draw_info: the draw info.
1247 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1249 % o exception: return any errors or warnings in this structure.
1252 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1253 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1278 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1279 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1281 resolution.x=DefaultResolution;
1282 resolution.y=DefaultResolution;
1283 if (clone_info->density != (char *) NULL)
1291 flags=ParseGeometry(clone_info->density,&geometry_info);
1292 resolution.x=geometry_info.rho;
1293 resolution.y=geometry_info.sigma;
1294 if ((flags & SigmaValue) == MagickFalse)
1295 resolution.y=resolution.x;
1297 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1298 clone_info->stroke_width/2.0;
1303 if (polygon_info != (PolygonInfo *) NULL)
1305 bounds=polygon_info->edges[0].bounds;
1306 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1308 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1309 bounds.x1=polygon_info->edges[i].bounds.x1;
1310 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1311 bounds.y1=polygon_info->edges[i].bounds.y1;
1312 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1313 bounds.x2=polygon_info->edges[i].bounds.x2;
1314 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1315 bounds.y2=polygon_info->edges[i].bounds.y2;
1318 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1319 image->columns ? (double) image->columns-1 : bounds.x1;
1321 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1322 image->rows ? (double) image->rows-1 : bounds.y1;
1324 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1325 image->columns ? (double) image->columns-1 : bounds.x2;
1327 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1328 image->rows ? (double) image->rows-1 : bounds.y2;
1329 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1331 if (polygon_info->edges[i].direction != 0)
1332 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1335 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1337 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1338 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1339 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1340 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1341 primitive_info[0].primitive=RectanglePrimitive;
1342 TraceRectangle(primitive_info,start,end);
1343 primitive_info[0].method=ReplaceMethod;
1344 coordinates=(ssize_t) primitive_info[0].coordinates;
1345 primitive_info[coordinates].primitive=UndefinedPrimitive;
1346 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1349 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1351 start.x=(double) (bounds.x1-mid);
1352 start.y=(double) (bounds.y1-mid);
1353 end.x=(double) (bounds.x2+mid);
1354 end.y=(double) (bounds.y2+mid);
1355 primitive_info[0].primitive=RectanglePrimitive;
1356 TraceRectangle(primitive_info,start,end);
1357 primitive_info[0].method=ReplaceMethod;
1358 coordinates=(ssize_t) primitive_info[0].coordinates;
1359 primitive_info[coordinates].primitive=UndefinedPrimitive;
1360 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1361 clone_info=DestroyDrawInfo(clone_info);
1365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 % D r a w C l i p P a t h %
1373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375 % DrawClipPath() draws the clip path on the image mask.
1377 % The format of the DrawClipPath method is:
1379 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1380 % const char *name,ExceptionInfo *exception)
1382 % A description of each parameter follows:
1384 % o image: the image.
1386 % o draw_info: the draw info.
1388 % o name: the name of the clip path.
1390 % o exception: return any errors or warnings in this structure.
1393 MagickExport MagickBooleanType DrawClipPath(Image *image,
1394 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1397 filename[MagickPathExtent];
1411 assert(image != (Image *) NULL);
1412 assert(image->signature == MagickCoreSignature);
1413 if (image->debug != MagickFalse)
1414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1415 assert(draw_info != (const DrawInfo *) NULL);
1416 (void) FormatLocaleString(filename,MagickPathExtent,"%s",name);
1417 value=GetImageArtifact(image,filename);
1418 if (value == (const char *) NULL)
1419 return(MagickFalse);
1420 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1421 if (clip_mask == (Image *) NULL)
1422 return(MagickFalse);
1423 (void) QueryColorCompliance("#0000",AllCompliance,
1424 &clip_mask->background_color,exception);
1425 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1426 (void) SetImageBackgroundColor(clip_mask,exception);
1427 if (image->debug != MagickFalse)
1428 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1429 draw_info->clip_mask);
1430 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1431 (void) CloneString(&clone_info->primitive,value);
1432 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1434 clone_info->clip_mask=(char *) NULL;
1435 status=NegateImage(clip_mask,MagickFalse,exception);
1436 (void) SetImageMask(image,ReadPixelMask,clip_mask,exception);
1437 clip_mask=DestroyImage(clip_mask);
1438 status&=DrawImage(image,clone_info,exception);
1439 clone_info=DestroyDrawInfo(clone_info);
1440 if (image->debug != MagickFalse)
1441 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1442 return(status != 0 ? MagickTrue : MagickFalse);
1446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450 + D r a w D a s h P o l y g o n %
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1457 % image while respecting the dash offset and dash pattern attributes.
1459 % The format of the DrawDashPolygon method is:
1461 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1462 % const PrimitiveInfo *primitive_info,Image *image,
1463 % ExceptionInfo *exception)
1465 % A description of each parameter follows:
1467 % o draw_info: the draw info.
1469 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1471 % o image: the image.
1473 % o exception: return any errors or warnings in this structure.
1476 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1477 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1509 assert(draw_info != (const DrawInfo *) NULL);
1510 if (image->debug != MagickFalse)
1511 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1512 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1513 number_vertices=(size_t) i;
1514 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1515 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1516 if (dash_polygon == (PrimitiveInfo *) NULL)
1517 return(MagickFalse);
1518 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1519 clone_info->miterlimit=0;
1520 dash_polygon[0]=primitive_info[0];
1521 scale=ExpandAffine(&draw_info->affine);
1522 length=scale*(draw_info->dash_pattern[0]-0.5);
1523 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1525 for (n=0; offset > 0.0; j=0)
1527 if (draw_info->dash_pattern[n] <= 0.0)
1529 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1530 if (offset > length)
1534 length=scale*(draw_info->dash_pattern[n]+0.5);
1537 if (offset < length)
1549 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1551 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1552 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1553 maximum_length=hypot((double) dx,dy);
1557 if (draw_info->dash_pattern[n] == 0.0)
1559 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1561 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1563 total_length+=length;
1564 if ((n & 0x01) != 0)
1566 dash_polygon[0]=primitive_info[0];
1567 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1568 total_length/maximum_length);
1569 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1570 total_length/maximum_length);
1575 if ((j+1) > (ssize_t) (2*number_vertices))
1577 dash_polygon[j]=primitive_info[i-1];
1578 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1579 total_length/maximum_length);
1580 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1581 total_length/maximum_length);
1582 dash_polygon[j].coordinates=1;
1584 dash_polygon[0].coordinates=(size_t) j;
1585 dash_polygon[j].primitive=UndefinedPrimitive;
1586 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1589 if (draw_info->dash_pattern[n] == 0.0)
1591 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1593 length-=(maximum_length-total_length);
1594 if ((n & 0x01) != 0)
1596 dash_polygon[j]=primitive_info[i];
1597 dash_polygon[j].coordinates=1;
1600 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1602 dash_polygon[j]=primitive_info[i-1];
1603 dash_polygon[j].point.x+=MagickEpsilon;
1604 dash_polygon[j].point.y+=MagickEpsilon;
1605 dash_polygon[j].coordinates=1;
1607 dash_polygon[0].coordinates=(size_t) j;
1608 dash_polygon[j].primitive=UndefinedPrimitive;
1609 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1611 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1612 clone_info=DestroyDrawInfo(clone_info);
1613 if (image->debug != MagickFalse)
1614 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1615 return(status != 0 ? MagickTrue : MagickFalse);
1619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1623 % D r a w I m a g e %
1627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629 % DrawImage() draws a graphic primitive on your image. The primitive
1630 % may be represented as a string or filename. Precede the filename with an
1631 % "at" sign (@) and the contents of the file are drawn on the image. You
1632 % can affect how text is drawn by setting one or more members of the draw
1635 % The format of the DrawImage method is:
1637 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1638 % ExceptionInfo *exception)
1640 % A description of each parameter follows:
1642 % o image: the image.
1644 % o draw_info: the draw info.
1646 % o exception: return any errors or warnings in this structure.
1650 static inline MagickBooleanType IsPoint(const char *point)
1658 value=StringToDouble(point,&p);
1659 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1662 static inline void TracePoint(PrimitiveInfo *primitive_info,
1663 const PointInfo point)
1665 primitive_info->coordinates=1;
1666 primitive_info->point=point;
1669 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1670 ExceptionInfo *exception)
1672 #define RenderImageTag "Render/Image"
1679 keyword[MagickPathExtent],
1680 geometry[MagickPathExtent],
1682 pattern[MagickPathExtent],
1739 Ensure the annotation info is valid.
1741 assert(image != (Image *) NULL);
1742 assert(image->signature == MagickCoreSignature);
1743 if (image->debug != MagickFalse)
1744 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1745 assert(draw_info != (DrawInfo *) NULL);
1746 assert(draw_info->signature == MagickCoreSignature);
1747 if (image->debug != MagickFalse)
1748 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1749 if ((draw_info->primitive == (char *) NULL) ||
1750 (*draw_info->primitive == '\0'))
1751 return(MagickFalse);
1752 if (image->debug != MagickFalse)
1753 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1754 if (*draw_info->primitive != '@')
1755 primitive=AcquireString(draw_info->primitive);
1757 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1758 if (primitive == (char *) NULL)
1759 return(MagickFalse);
1760 primitive_extent=(double) strlen(primitive);
1761 (void) SetImageArtifact(image,"MVG",primitive);
1764 stops=(StopInfo *) NULL;
1766 Allocate primitive info memory.
1768 graphic_context=(DrawInfo **) AcquireMagickMemory(
1769 sizeof(*graphic_context));
1770 if (graphic_context == (DrawInfo **) NULL)
1772 primitive=DestroyString(primitive);
1773 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1777 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1778 sizeof(*primitive_info));
1779 if (primitive_info == (PrimitiveInfo *) NULL)
1781 primitive=DestroyString(primitive);
1782 for ( ; n >= 0; n--)
1783 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1784 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1785 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1788 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1789 graphic_context[n]->viewbox=image->page;
1790 if ((image->page.width == 0) || (image->page.height == 0))
1792 graphic_context[n]->viewbox.width=image->columns;
1793 graphic_context[n]->viewbox.height=image->rows;
1795 token=AcquireString(primitive);
1796 extent=strlen(token)+MagickPathExtent;
1797 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1798 return(MagickFalse);
1800 for (q=primitive; *q != '\0'; )
1803 Interpret graphic primitive.
1805 GetNextToken(q,&q,MagickPathExtent,keyword);
1806 if (*keyword == '\0')
1808 if (*keyword == '#')
1813 while ((*q != '\n') && (*q != '\0'))
1817 p=q-strlen(keyword)-1;
1818 primitive_type=UndefinedPrimitive;
1819 current=graphic_context[n]->affine;
1820 GetAffineMatrix(&affine);
1828 if (LocaleCompare("affine",keyword) == 0)
1830 GetNextToken(q,&q,extent,token);
1831 affine.sx=StringToDouble(token,&next_token);
1832 if (token == next_token)
1834 GetNextToken(q,&q,extent,token);
1836 GetNextToken(q,&q,extent,token);
1837 affine.rx=StringToDouble(token,&next_token);
1838 if (token == next_token)
1840 GetNextToken(q,&q,extent,token);
1842 GetNextToken(q,&q,extent,token);
1843 affine.ry=StringToDouble(token,&next_token);
1844 if (token == next_token)
1846 GetNextToken(q,&q,extent,token);
1848 GetNextToken(q,&q,extent,token);
1849 affine.sy=StringToDouble(token,&next_token);
1850 if (token == next_token)
1852 GetNextToken(q,&q,extent,token);
1854 GetNextToken(q,&q,extent,token);
1855 affine.tx=StringToDouble(token,&next_token);
1856 if (token == next_token)
1858 GetNextToken(q,&q,extent,token);
1860 GetNextToken(q,&q,extent,token);
1861 affine.ty=StringToDouble(token,&next_token);
1862 if (token == next_token)
1866 if (LocaleCompare("alpha",keyword) == 0)
1868 primitive_type=AlphaPrimitive;
1871 if (LocaleCompare("arc",keyword) == 0)
1873 primitive_type=ArcPrimitive;
1882 if (LocaleCompare("bezier",keyword) == 0)
1884 primitive_type=BezierPrimitive;
1887 if (LocaleCompare("border-color",keyword) == 0)
1889 GetNextToken(q,&q,extent,token);
1890 (void) QueryColorCompliance(token,AllCompliance,
1891 &graphic_context[n]->border_color,exception);
1900 if (LocaleCompare("clip-path",keyword) == 0)
1905 GetNextToken(q,&q,extent,token);
1906 (void) CloneString(&graphic_context[n]->clip_mask,token);
1907 (void) DrawClipPath(image,graphic_context[n],
1908 graphic_context[n]->clip_mask,exception);
1911 if (LocaleCompare("clip-rule",keyword) == 0)
1916 GetNextToken(q,&q,extent,token);
1917 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1919 if (fill_rule == -1)
1922 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1925 if (LocaleCompare("clip-units",keyword) == 0)
1930 GetNextToken(q,&q,extent,token);
1931 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1933 if (clip_units == -1)
1938 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1939 if (clip_units == ObjectBoundingBox)
1941 GetAffineMatrix(¤t);
1942 affine.sx=draw_info->bounds.x2;
1943 affine.sy=draw_info->bounds.y2;
1944 affine.tx=draw_info->bounds.x1;
1945 affine.ty=draw_info->bounds.y1;
1950 if (LocaleCompare("circle",keyword) == 0)
1952 primitive_type=CirclePrimitive;
1955 if (LocaleCompare("color",keyword) == 0)
1957 primitive_type=ColorPrimitive;
1966 if (LocaleCompare("decorate",keyword) == 0)
1971 GetNextToken(q,&q,extent,token);
1972 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1977 graphic_context[n]->decorate=(DecorationType) decorate;
1980 if (LocaleCompare("density",keyword) == 0)
1982 GetNextToken(q,&q,extent,token);
1983 (void) CloneString(&graphic_context[n]->density,token);
1986 if (LocaleCompare("direction",keyword) == 0)
1991 GetNextToken(q,&q,extent,token);
1992 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1994 if (direction == -1)
1997 graphic_context[n]->direction=(DirectionType) direction;
2006 if (LocaleCompare("ellipse",keyword) == 0)
2008 primitive_type=EllipsePrimitive;
2011 if (LocaleCompare("encoding",keyword) == 0)
2013 GetNextToken(q,&q,extent,token);
2014 (void) CloneString(&graphic_context[n]->encoding,token);
2023 if (LocaleCompare("fill",keyword) == 0)
2025 GetNextToken(q,&q,extent,token);
2026 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2027 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2028 (void) DrawPatternPath(image,draw_info,token,
2029 &graphic_context[n]->fill_pattern,exception);
2032 status&=QueryColorCompliance(token,AllCompliance,
2033 &graphic_context[n]->fill,exception);
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=(double) QuantumRange*
2055 factor*StringToDouble(token,&next_token);
2056 if (token == next_token)
2060 if (LocaleCompare("fill-rule",keyword) == 0)
2065 GetNextToken(q,&q,extent,token);
2066 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2068 if (fill_rule == -1)
2071 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2074 if (LocaleCompare("font",keyword) == 0)
2076 GetNextToken(q,&q,extent,token);
2077 (void) CloneString(&graphic_context[n]->font,token);
2078 if (LocaleCompare("none",token) == 0)
2079 graphic_context[n]->font=(char *)
2080 RelinquishMagickMemory(graphic_context[n]->font);
2083 if (LocaleCompare("font-family",keyword) == 0)
2085 GetNextToken(q,&q,extent,token);
2086 (void) CloneString(&graphic_context[n]->family,token);
2089 if (LocaleCompare("font-size",keyword) == 0)
2091 GetNextToken(q,&q,extent,token);
2092 graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2093 if (token == next_token)
2097 if (LocaleCompare("font-stretch",keyword) == 0)
2102 GetNextToken(q,&q,extent,token);
2103 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2107 graphic_context[n]->stretch=(StretchType) stretch;
2110 if (LocaleCompare("font-style",keyword) == 0)
2115 GetNextToken(q,&q,extent,token);
2116 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2120 graphic_context[n]->style=(StyleType) style;
2123 if (LocaleCompare("font-weight",keyword) == 0)
2128 GetNextToken(q,&q,extent,token);
2129 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
2131 weight=(ssize_t) StringToUnsignedLong(token);
2132 graphic_context[n]->weight=(size_t) weight;
2141 if (LocaleCompare("gradient-units",keyword) == 0)
2143 GetNextToken(q,&q,extent,token);
2146 if (LocaleCompare("gravity",keyword) == 0)
2151 GetNextToken(q,&q,extent,token);
2152 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2156 graphic_context[n]->gravity=(GravityType) gravity;
2165 if (LocaleCompare("image",keyword) == 0)
2170 primitive_type=ImagePrimitive;
2171 GetNextToken(q,&q,extent,token);
2172 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2176 graphic_context[n]->compose=(CompositeOperator) compose;
2179 if (LocaleCompare("interline-spacing",keyword) == 0)
2181 GetNextToken(q,&q,extent,token);
2182 graphic_context[n]->interline_spacing=StringToDouble(token,
2184 if (token == next_token)
2188 if (LocaleCompare("interword-spacing",keyword) == 0)
2190 GetNextToken(q,&q,extent,token);
2191 graphic_context[n]->interword_spacing=StringToDouble(token,
2193 if (token == next_token)
2203 if (LocaleCompare("kerning",keyword) == 0)
2205 GetNextToken(q,&q,extent,token);
2206 graphic_context[n]->kerning=StringToDouble(token,&next_token);
2207 if (token == next_token)
2217 if (LocaleCompare("line",keyword) == 0)
2218 primitive_type=LinePrimitive;
2226 if (LocaleCompare("offset",keyword) == 0)
2228 GetNextToken(q,&q,extent,token);
2231 if (LocaleCompare("opacity",keyword) == 0)
2233 GetNextToken(q,&q,extent,token);
2234 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2235 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2236 QuantumScale*graphic_context[n]->alpha)*factor*
2237 StringToDouble(token,&next_token))));
2238 if (token == next_token)
2240 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2241 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2250 if (LocaleCompare("path",keyword) == 0)
2252 primitive_type=PathPrimitive;
2255 if (LocaleCompare("point",keyword) == 0)
2257 primitive_type=PointPrimitive;
2260 if (LocaleCompare("polyline",keyword) == 0)
2262 primitive_type=PolylinePrimitive;
2265 if (LocaleCompare("polygon",keyword) == 0)
2267 primitive_type=PolygonPrimitive;
2270 if (LocaleCompare("pop",keyword) == 0)
2272 GetNextToken(q,&q,extent,token);
2273 if (LocaleCompare("clip-path",token) == 0)
2275 if (LocaleCompare("defs",token) == 0)
2277 if (LocaleCompare("gradient",token) == 0)
2279 if (LocaleCompare("graphic-context",token) == 0)
2283 (void) ThrowMagickException(exception,GetMagickModule(),
2284 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2289 if (graphic_context[n]->clip_mask != (char *) NULL)
2290 if (LocaleCompare(graphic_context[n]->clip_mask,
2291 graphic_context[n-1]->clip_mask) != 0)
2292 (void) SetImageMask(image,ReadPixelMask,(Image *) NULL,
2294 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2298 if (LocaleCompare("pattern",token) == 0)
2303 if (LocaleCompare("push",keyword) == 0)
2305 GetNextToken(q,&q,extent,token);
2306 if (LocaleCompare("clip-path",token) == 0)
2309 name[MagickPathExtent];
2311 GetNextToken(q,&q,extent,token);
2312 (void) FormatLocaleString(name,MagickPathExtent,"%s",token);
2313 for (p=q; *q != '\0'; )
2315 GetNextToken(q,&q,extent,token);
2316 if (LocaleCompare(token,"pop") != 0)
2318 GetNextToken(q,(const char **) NULL,extent,token);
2319 if (LocaleCompare(token,"clip-path") != 0)
2323 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2324 (void) SetImageArtifact(image,name,token);
2325 GetNextToken(q,&q,extent,token);
2328 if (LocaleCompare("gradient",token) == 0)
2331 key[2*MagickPathExtent],
2332 name[MagickPathExtent],
2333 type[MagickPathExtent];
2338 GetNextToken(q,&q,extent,token);
2339 (void) CopyMagickString(name,token,MagickPathExtent);
2340 GetNextToken(q,&q,extent,token);
2341 (void) CopyMagickString(type,token,MagickPathExtent);
2342 GetNextToken(q,&q,extent,token);
2343 segment.x1=StringToDouble(token,&next_token);
2344 if (token == next_token)
2346 GetNextToken(q,&q,extent,token);
2348 GetNextToken(q,&q,extent,token);
2349 segment.y1=StringToDouble(token,&next_token);
2350 if (token == next_token)
2352 GetNextToken(q,&q,extent,token);
2354 GetNextToken(q,&q,extent,token);
2355 segment.x2=StringToDouble(token,&next_token);
2356 if (token == next_token)
2358 GetNextToken(q,&q,extent,token);
2360 GetNextToken(q,&q,extent,token);
2361 segment.y2=StringToDouble(token,&next_token);
2362 if (token == next_token)
2364 if (LocaleCompare(type,"radial") == 0)
2366 GetNextToken(q,&q,extent,token);
2368 GetNextToken(q,&q,extent,token);
2370 for (p=q; *q != '\0'; )
2372 GetNextToken(q,&q,extent,token);
2373 if (LocaleCompare(token,"pop") != 0)
2375 GetNextToken(q,(const char **) NULL,extent,token);
2376 if (LocaleCompare(token,"gradient") != 0)
2380 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2381 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2382 graphic_context[n]->affine.ry*segment.y1+
2383 graphic_context[n]->affine.tx;
2384 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2385 graphic_context[n]->affine.sy*segment.y1+
2386 graphic_context[n]->affine.ty;
2387 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2388 graphic_context[n]->affine.ry*segment.y2+
2389 graphic_context[n]->affine.tx;
2390 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2391 graphic_context[n]->affine.sy*segment.y2+
2392 graphic_context[n]->affine.ty;
2393 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2394 (void) SetImageArtifact(image,key,token);
2395 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
2396 (void) SetImageArtifact(image,key,type);
2397 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2399 (void) FormatLocaleString(geometry,MagickPathExtent,
2400 "%gx%g%+.15g%+.15g",
2401 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2402 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2403 bounds.x1,bounds.y1);
2404 (void) SetImageArtifact(image,key,geometry);
2405 GetNextToken(q,&q,extent,token);
2408 if (LocaleCompare("pattern",token) == 0)
2411 key[2*MagickPathExtent],
2412 name[MagickPathExtent];
2417 GetNextToken(q,&q,extent,token);
2418 (void) CopyMagickString(name,token,MagickPathExtent);
2419 GetNextToken(q,&q,extent,token);
2420 pattern_bounds.x=(ssize_t) ceil(StringToDouble(token,
2422 if (token == next_token)
2424 GetNextToken(q,&q,extent,token);
2426 GetNextToken(q,&q,extent,token);
2427 pattern_bounds.y=(ssize_t) ceil(StringToDouble(token,
2429 if (token == next_token)
2431 GetNextToken(q,&q,extent,token);
2433 GetNextToken(q,&q,extent,token);
2434 pattern_bounds.width=(size_t) floor(StringToDouble(token,
2436 if (token == next_token)
2438 GetNextToken(q,&q,extent,token);
2440 GetNextToken(q,&q,extent,token);
2441 pattern_bounds.height=(size_t) floor(StringToDouble(token,
2443 if (token == next_token)
2445 for (p=q; *q != '\0'; )
2447 GetNextToken(q,&q,extent,token);
2448 if (LocaleCompare(token,"pop") != 0)
2450 GetNextToken(q,(const char **) NULL,extent,token);
2451 if (LocaleCompare(token,"pattern") != 0)
2455 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2456 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2457 (void) SetImageArtifact(image,key,token);
2458 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2460 (void) FormatLocaleString(geometry,MagickPathExtent,
2461 "%.20gx%.20g%+.20g%+.20g",(double)pattern_bounds.width,
2462 (double)pattern_bounds.height,(double)pattern_bounds.x,
2463 (double)pattern_bounds.y);
2464 (void) SetImageArtifact(image,key,geometry);
2465 GetNextToken(q,&q,extent,token);
2468 if (LocaleCompare("graphic-context",token) == 0)
2471 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2472 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2473 if (graphic_context == (DrawInfo **) NULL)
2475 (void) ThrowMagickException(exception,GetMagickModule(),
2476 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2480 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2481 graphic_context[n-1]);
2484 if (LocaleCompare("defs",token) == 0)
2495 if (LocaleCompare("rectangle",keyword) == 0)
2497 primitive_type=RectanglePrimitive;
2500 if (LocaleCompare("rotate",keyword) == 0)
2502 GetNextToken(q,&q,extent,token);
2503 angle=StringToDouble(token,&next_token);
2504 if (token == next_token)
2506 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2507 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2508 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2509 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2512 if (LocaleCompare("roundRectangle",keyword) == 0)
2514 primitive_type=RoundRectanglePrimitive;
2523 if (LocaleCompare("scale",keyword) == 0)
2525 GetNextToken(q,&q,extent,token);
2526 affine.sx=StringToDouble(token,&next_token);
2527 if (token == next_token)
2529 GetNextToken(q,&q,extent,token);
2531 GetNextToken(q,&q,extent,token);
2532 affine.sy=StringToDouble(token,&next_token);
2533 if (token == next_token)
2537 if (LocaleCompare("skewX",keyword) == 0)
2539 GetNextToken(q,&q,extent,token);
2540 angle=StringToDouble(token,&next_token);
2541 if (token == next_token)
2543 affine.ry=sin(DegreesToRadians(angle));
2546 if (LocaleCompare("skewY",keyword) == 0)
2548 GetNextToken(q,&q,extent,token);
2549 angle=StringToDouble(token,&next_token);
2550 if (token == next_token)
2552 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2555 if (LocaleCompare("stop-color",keyword) == 0)
2561 if (number_stops == 1)
2562 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
2563 else if (number_stops > 2)
2564 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
2566 if (stops == (StopInfo *) NULL)
2568 (void) ThrowMagickException(exception,GetMagickModule(),
2569 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2573 GetNextToken(q,&q,extent,token);
2574 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2576 stops[number_stops-1].color=stop_color;
2577 GetNextToken(q,&q,extent,token);
2578 stops[number_stops-1].offset=StringToDouble(token,&next_token);
2579 if (token == next_token)
2583 if (LocaleCompare("stroke",keyword) == 0)
2585 GetNextToken(q,&q,extent,token);
2586 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2587 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2588 (void) DrawPatternPath(image,draw_info,token,
2589 &graphic_context[n]->stroke_pattern,exception);
2592 status&=QueryColorCompliance(token,AllCompliance,
2593 &graphic_context[n]->stroke,exception);
2594 if (status == MagickFalse)
2599 pattern_info=AcquireImageInfo();
2600 (void) CopyMagickString(pattern_info->filename,token,
2602 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2604 CatchException(exception);
2605 pattern_info=DestroyImageInfo(pattern_info);
2610 if (LocaleCompare("stroke-antialias",keyword) == 0)
2612 GetNextToken(q,&q,extent,token);
2613 graphic_context[n]->stroke_antialias=
2614 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2617 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2619 if (graphic_context[n]->dash_pattern != (double *) NULL)
2620 graphic_context[n]->dash_pattern=(double *)
2621 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2622 if (IsPoint(q) != MagickFalse)
2628 GetNextToken(r,&r,extent,token);
2630 GetNextToken(r,&r,extent,token);
2631 for (x=0; IsPoint(token) != MagickFalse; x++)
2633 GetNextToken(r,&r,extent,token);
2635 GetNextToken(r,&r,extent,token);
2637 graphic_context[n]->dash_pattern=(double *)
2638 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2639 sizeof(*graphic_context[n]->dash_pattern));
2640 if (graphic_context[n]->dash_pattern == (double *) NULL)
2642 (void) ThrowMagickException(exception,GetMagickModule(),
2643 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2648 for (j=0; j < x; j++)
2650 GetNextToken(q,&q,extent,token);
2652 GetNextToken(q,&q,extent,token);
2653 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2655 if (token == next_token)
2657 if (graphic_context[n]->dash_pattern[j] < 0.0)
2660 if ((x & 0x01) != 0)
2661 for ( ; j < (2*x); j++)
2662 graphic_context[n]->dash_pattern[j]=
2663 graphic_context[n]->dash_pattern[j-x];
2664 graphic_context[n]->dash_pattern[j]=0.0;
2667 GetNextToken(q,&q,extent,token);
2670 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2672 GetNextToken(q,&q,extent,token);
2673 graphic_context[n]->dash_offset=StringToDouble(token,
2675 if (token == next_token)
2679 if (LocaleCompare("stroke-linecap",keyword) == 0)
2684 GetNextToken(q,&q,extent,token);
2685 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2689 graphic_context[n]->linecap=(LineCap) linecap;
2692 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2697 GetNextToken(q,&q,extent,token);
2698 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2703 graphic_context[n]->linejoin=(LineJoin) linejoin;
2706 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2708 GetNextToken(q,&q,extent,token);
2709 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2712 if (LocaleCompare("stroke-opacity",keyword) == 0)
2714 GetNextToken(q,&q,extent,token);
2715 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2716 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2717 factor*StringToDouble(token,&next_token);
2718 if (token == next_token)
2722 if (LocaleCompare("stroke-width",keyword) == 0)
2724 GetNextToken(q,&q,extent,token);
2725 graphic_context[n]->stroke_width=StringToDouble(token,
2727 if (token == next_token)
2737 if (LocaleCompare("text",keyword) == 0)
2739 primitive_type=TextPrimitive;
2742 if (LocaleCompare("text-align",keyword) == 0)
2747 GetNextToken(q,&q,extent,token);
2748 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2752 graphic_context[n]->align=(AlignType) align;
2755 if (LocaleCompare("text-anchor",keyword) == 0)
2760 GetNextToken(q,&q,extent,token);
2761 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2765 graphic_context[n]->align=(AlignType) align;
2768 if (LocaleCompare("text-antialias",keyword) == 0)
2770 GetNextToken(q,&q,extent,token);
2771 graphic_context[n]->text_antialias=
2772 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2775 if (LocaleCompare("text-undercolor",keyword) == 0)
2777 GetNextToken(q,&q,extent,token);
2778 (void) QueryColorCompliance(token,AllCompliance,
2779 &graphic_context[n]->undercolor,exception);
2782 if (LocaleCompare("translate",keyword) == 0)
2784 GetNextToken(q,&q,extent,token);
2785 affine.tx=StringToDouble(token,&next_token);
2786 if (token == next_token)
2788 GetNextToken(q,&q,extent,token);
2790 GetNextToken(q,&q,extent,token);
2791 affine.ty=StringToDouble(token,&next_token);
2792 if (token == next_token)
2802 if (LocaleCompare("viewbox",keyword) == 0)
2804 GetNextToken(q,&q,extent,token);
2805 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2807 if (token == next_token)
2809 GetNextToken(q,&q,extent,token);
2811 GetNextToken(q,&q,extent,token);
2812 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2814 if (token == next_token)
2816 GetNextToken(q,&q,extent,token);
2818 GetNextToken(q,&q,extent,token);
2819 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2820 token,&next_token)+0.5);
2821 if (token == next_token)
2823 GetNextToken(q,&q,extent,token);
2825 GetNextToken(q,&q,extent,token);
2826 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2827 token,&next_token)+0.5);
2828 if (token == next_token)
2841 if (status == MagickFalse)
2843 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2844 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2846 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2847 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2848 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2849 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2850 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2852 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2855 if (primitive_type == UndefinedPrimitive)
2859 if (number_stops > 1)
2864 type=LinearGradient;
2865 if (draw_info->gradient.type == RadialGradient)
2866 type=RadialGradient;
2867 (void) GradientImage(image,type,PadSpread,stops,number_stops,
2870 if (number_stops > 0)
2871 stops=(StopInfo *) RelinquishMagickMemory(stops);
2873 if (image->debug != MagickFalse)
2874 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2879 Parse the primitive attributes.
2883 primitive_info[0].point.x=0.0;
2884 primitive_info[0].point.y=0.0;
2885 for (x=0; *q != '\0'; x++)
2890 if (IsPoint(q) == MagickFalse)
2892 GetNextToken(q,&q,extent,token);
2893 point.x=StringToDouble(token,&next_token);
2894 if (token == next_token)
2896 GetNextToken(q,&q,extent,token);
2898 GetNextToken(q,&q,extent,token);
2899 point.y=StringToDouble(token,&next_token);
2900 if (token == next_token)
2902 GetNextToken(q,(const char **) NULL,extent,token);
2904 GetNextToken(q,&q,extent,token);
2905 primitive_info[i].primitive=primitive_type;
2906 primitive_info[i].point=point;
2907 primitive_info[i].coordinates=0;
2908 primitive_info[i].method=FloodfillMethod;
2910 if (i < (ssize_t) number_points)
2913 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2914 (size_t) number_points,sizeof(*primitive_info));
2915 if ((primitive_info == (PrimitiveInfo *) NULL) ||
2916 (number_points != (MagickSizeType) ((size_t) number_points)))
2918 (void) ThrowMagickException(exception,GetMagickModule(),
2919 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2923 primitive_info[j].primitive=primitive_type;
2924 primitive_info[j].coordinates=(size_t) x;
2925 primitive_info[j].method=FloodfillMethod;
2926 primitive_info[j].text=(char *) NULL;
2928 Circumscribe primitive within a circle.
2930 bounds.x1=primitive_info[j].point.x;
2931 bounds.y1=primitive_info[j].point.y;
2932 bounds.x2=primitive_info[j].point.x;
2933 bounds.y2=primitive_info[j].point.y;
2934 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2936 point=primitive_info[j+k].point;
2937 if (point.x < bounds.x1)
2939 if (point.y < bounds.y1)
2941 if (point.x > bounds.x2)
2943 if (point.y > bounds.y2)
2947 Speculate how many points our primitive might consume.
2949 length=primitive_info[j].coordinates;
2950 switch (primitive_type)
2952 case RectanglePrimitive:
2957 case RoundRectanglePrimitive:
2964 alpha=bounds.x2-bounds.x1;
2965 beta=bounds.y2-bounds.y1;
2966 radius=hypot((double) alpha,(double) beta);
2968 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2971 case BezierPrimitive:
2973 if (primitive_info[j].coordinates > 107)
2974 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2975 "TooManyBezierCoordinates","`%s'",token);
2976 length=BezierQuantum*primitive_info[j].coordinates;
2985 GetNextToken(q,&q,extent,token);
2988 for (s=token; *s != '\0'; s=t)
2993 value=StringToDouble(s,&t);
3002 length=length*BezierQuantum/2;
3005 case CirclePrimitive:
3007 case EllipsePrimitive:
3014 alpha=bounds.x2-bounds.x1;
3015 beta=bounds.y2-bounds.y1;
3016 radius=hypot((double) alpha,(double) beta);
3017 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
3023 if ((i+length) >= number_points)
3026 Resize based on speculative points required by primitive.
3028 number_points+=length+1;
3029 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
3030 (size_t) number_points,sizeof(*primitive_info));
3031 if ((primitive_info == (PrimitiveInfo *) NULL) ||
3032 (number_points != (MagickSizeType) ((size_t) number_points)))
3034 (void) ThrowMagickException(exception,GetMagickModule(),
3035 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3040 switch (primitive_type)
3042 case PointPrimitive:
3045 if (primitive_info[j].coordinates != 1)
3050 TracePoint(primitive_info+j,primitive_info[j].point);
3051 i=(ssize_t) (j+primitive_info[j].coordinates);
3056 if (primitive_info[j].coordinates != 2)
3061 TraceLine(primitive_info+j,primitive_info[j].point,
3062 primitive_info[j+1].point);
3063 i=(ssize_t) (j+primitive_info[j].coordinates);
3066 case RectanglePrimitive:
3068 if (primitive_info[j].coordinates != 2)
3073 TraceRectangle(primitive_info+j,primitive_info[j].point,
3074 primitive_info[j+1].point);
3075 i=(ssize_t) (j+primitive_info[j].coordinates);
3078 case RoundRectanglePrimitive:
3080 if (primitive_info[j].coordinates != 3)
3085 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
3086 primitive_info[j+1].point,primitive_info[j+2].point);
3087 i=(ssize_t) (j+primitive_info[j].coordinates);
3092 if (primitive_info[j].coordinates != 3)
3094 primitive_type=UndefinedPrimitive;
3097 TraceArc(primitive_info+j,primitive_info[j].point,
3098 primitive_info[j+1].point,primitive_info[j+2].point);
3099 i=(ssize_t) (j+primitive_info[j].coordinates);
3102 case EllipsePrimitive:
3104 if (primitive_info[j].coordinates != 3)
3109 TraceEllipse(primitive_info+j,primitive_info[j].point,
3110 primitive_info[j+1].point,primitive_info[j+2].point);
3111 i=(ssize_t) (j+primitive_info[j].coordinates);
3114 case CirclePrimitive:
3116 if (primitive_info[j].coordinates != 2)
3121 TraceCircle(primitive_info+j,primitive_info[j].point,
3122 primitive_info[j+1].point);
3123 i=(ssize_t) (j+primitive_info[j].coordinates);
3126 case PolylinePrimitive:
3128 case PolygonPrimitive:
3130 primitive_info[i]=primitive_info[j];
3131 primitive_info[i].coordinates=0;
3132 primitive_info[j].coordinates++;
3136 case BezierPrimitive:
3138 if (primitive_info[j].coordinates < 3)
3143 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3144 i=(ssize_t) (j+primitive_info[j].coordinates);
3149 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3152 case AlphaPrimitive:
3153 case ColorPrimitive:
3158 if (primitive_info[j].coordinates != 1)
3163 GetNextToken(q,&q,extent,token);
3164 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3168 primitive_info[j].method=(PaintMethod) method;
3173 if (primitive_info[j].coordinates != 1)
3179 GetNextToken(q,&q,extent,token);
3180 primitive_info[j].text=AcquireString(token);
3183 case ImagePrimitive:
3185 if (primitive_info[j].coordinates != 2)
3190 GetNextToken(q,&q,extent,token);
3191 primitive_info[j].text=AcquireString(token);
3195 if (primitive_info == (PrimitiveInfo *) NULL)
3197 if (image->debug != MagickFalse)
3198 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3199 if (status == MagickFalse)
3201 primitive_info[i].primitive=UndefinedPrimitive;
3207 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3209 point=primitive_info[i].point;
3210 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3211 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3212 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3213 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3214 point=primitive_info[i].point;
3215 if (point.x < graphic_context[n]->bounds.x1)
3216 graphic_context[n]->bounds.x1=point.x;
3217 if (point.y < graphic_context[n]->bounds.y1)
3218 graphic_context[n]->bounds.y1=point.y;
3219 if (point.x > graphic_context[n]->bounds.x2)
3220 graphic_context[n]->bounds.x2=point.x;
3221 if (point.y > graphic_context[n]->bounds.y2)
3222 graphic_context[n]->bounds.y2=point.y;
3223 if (primitive_info[i].primitive == ImagePrimitive)
3225 if (i >= (ssize_t) number_points)
3226 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3228 if (graphic_context[n]->render != MagickFalse)
3230 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3231 (LocaleCompare(graphic_context[n]->clip_mask,
3232 graphic_context[n-1]->clip_mask) != 0))
3233 status&=DrawClipPath(image,graphic_context[n],
3234 graphic_context[n]->clip_mask,exception);
3235 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3238 if (primitive_info->text != (char *) NULL)
3239 primitive_info->text=(char *) RelinquishMagickMemory(
3240 primitive_info->text);
3241 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3243 if (proceed == MagickFalse)
3248 if (image->debug != MagickFalse)
3249 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3251 Relinquish resources.
3253 token=DestroyString(token);
3254 if (primitive_info != (PrimitiveInfo *) NULL)
3255 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3256 primitive=DestroyString(primitive);
3257 for ( ; n >= 0; n--)
3258 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3259 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3260 if (status == MagickFalse)
3261 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3263 return(status != 0 ? MagickTrue : MagickFalse);
3267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3271 % D r a w G r a d i e n t I m a g e %
3275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3277 % DrawGradientImage() draws a linear gradient on the image.
3279 % The format of the DrawGradientImage method is:
3281 % MagickBooleanType DrawGradientImage(Image *image,
3282 % const DrawInfo *draw_info,ExceptionInfo *exception)
3284 % A description of each parameter follows:
3286 % o image: the image.
3288 % o draw_info: the draw info.
3290 % o exception: return any errors or warnings in this structure.
3294 static inline double GetStopColorOffset(const GradientInfo *gradient,
3295 const ssize_t x,const ssize_t y)
3297 switch (gradient->type)
3299 case UndefinedGradient:
3300 case LinearGradient:
3315 gradient_vector=(&gradient->gradient_vector);
3316 p.x=gradient_vector->x2-gradient_vector->x1;
3317 p.y=gradient_vector->y2-gradient_vector->y1;
3318 q.x=(double) x-gradient_vector->x1;
3319 q.y=(double) y-gradient_vector->y1;
3320 length=sqrt(q.x*q.x+q.y*q.y);
3321 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3322 gamma=PerceptibleReciprocal(gamma);
3323 scale=p.x*q.x+p.y*q.y;
3324 offset=gamma*scale*length;
3327 case RadialGradient:
3332 if (gradient->spread == RepeatSpread)
3334 v.x=(double) x-gradient->center.x;
3335 v.y=(double) y-gradient->center.y;
3336 return(sqrt(v.x*v.x+v.y*v.y));
3338 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3339 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3340 gradient->angle))))/gradient->radii.x;
3341 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3342 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3343 gradient->angle))))/gradient->radii.y;
3344 return(sqrt(v.x*v.x+v.y*v.y));
3350 static int StopInfoCompare(const void *x,const void *y)
3356 stop_1=(StopInfo *) x;
3357 stop_2=(StopInfo *) y;
3358 if (stop_1->offset > stop_2->offset)
3360 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
3365 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3366 const DrawInfo *draw_info,ExceptionInfo *exception)
3396 Draw linear or radial gradient on image.
3398 assert(image != (Image *) NULL);
3399 assert(image->signature == MagickCoreSignature);
3400 if (image->debug != MagickFalse)
3401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3402 assert(draw_info != (const DrawInfo *) NULL);
3403 gradient=(&draw_info->gradient);
3404 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
3406 gradient_vector=(&gradient->gradient_vector);
3407 point.x=gradient_vector->x2-gradient_vector->x1;
3408 point.y=gradient_vector->y2-gradient_vector->y1;
3409 length=sqrt(point.x*point.x+point.y*point.y);
3410 bounding_box=gradient->bounding_box;
3412 GetPixelInfo(image,&zero);
3413 image_view=AcquireAuthenticCacheView(image,exception);
3414 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3415 #pragma omp parallel for schedule(static,4) shared(status) \
3416 magick_threads(image,image,1,1)
3418 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3438 if (status == MagickFalse)
3440 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3441 if (q == (Quantum *) NULL)
3448 offset=GetStopColorOffset(gradient,0,y);
3449 if (gradient->type != RadialGradient)
3451 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3453 GetPixelInfoPixel(image,q,&pixel);
3454 switch (gradient->spread)
3456 case UndefinedSpread:
3459 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3460 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3462 offset=GetStopColorOffset(gradient,x,y);
3463 if (gradient->type != RadialGradient)
3466 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3467 if (offset < gradient->stops[i].offset)
3469 if ((offset < 0.0) || (i == 0))
3470 composite=gradient->stops[0].color;
3472 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3473 composite=gradient->stops[gradient->number_stops-1].color;
3478 alpha=(offset-gradient->stops[i].offset)/
3479 (gradient->stops[j].offset-gradient->stops[i].offset);
3480 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3481 &gradient->stops[j].color,alpha,&composite);
3487 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3488 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3490 offset=GetStopColorOffset(gradient,x,y);
3491 if (gradient->type != RadialGradient)
3496 if ((ssize_t) fmod(offset,2.0) == 0)
3497 offset=fmod(offset,1.0);
3499 offset=1.0-fmod(offset,1.0);
3500 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3501 if (offset < gradient->stops[i].offset)
3504 composite=gradient->stops[0].color;
3506 if (i == (ssize_t) gradient->number_stops)
3507 composite=gradient->stops[gradient->number_stops-1].color;
3512 alpha=(offset-gradient->stops[i].offset)/
3513 (gradient->stops[j].offset-gradient->stops[i].offset);
3514 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3515 &gradient->stops[j].color,alpha,&composite);
3527 antialias=MagickFalse;
3529 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3530 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3532 offset=GetStopColorOffset(gradient,x,y);
3533 if (gradient->type == LinearGradient)
3535 repeat=fmod(offset,length);
3537 repeat=length-fmod(-repeat,length);
3539 repeat=fmod(offset,length);
3540 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3541 MagickTrue : MagickFalse;
3542 offset=repeat/length;
3546 repeat=fmod(offset,gradient->radius);
3548 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3550 repeat=fmod(offset,gradient->radius);
3551 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3553 offset=repeat/gradient->radius;
3556 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3557 if (offset < gradient->stops[i].offset)
3560 composite=gradient->stops[0].color;
3562 if (i == (ssize_t) gradient->number_stops)
3563 composite=gradient->stops[gradient->number_stops-1].color;
3568 alpha=(offset-gradient->stops[i].offset)/
3569 (gradient->stops[j].offset-gradient->stops[i].offset);
3570 if (antialias != MagickFalse)
3572 if (gradient->type == LinearGradient)
3573 alpha=length-repeat;
3575 alpha=gradient->radius-repeat;
3577 j=(ssize_t) gradient->number_stops-1L;
3579 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3580 &gradient->stops[j].color,alpha,&composite);
3585 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3587 SetPixelViaPixelInfo(image,&pixel,q);
3588 q+=GetPixelChannels(image);
3590 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3593 image_view=DestroyCacheView(image_view);
3598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3602 % D r a w P a t t e r n P a t h %
3606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3608 % DrawPatternPath() draws a pattern.
3610 % The format of the DrawPatternPath method is:
3612 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3613 % const char *name,Image **pattern,ExceptionInfo *exception)
3615 % A description of each parameter follows:
3617 % o image: the image.
3619 % o draw_info: the draw info.
3621 % o name: the pattern name.
3623 % o image: the image.
3625 % o exception: return any errors or warnings in this structure.
3628 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3629 const DrawInfo *draw_info,const char *name,Image **pattern,
3630 ExceptionInfo *exception)
3633 property[MagickPathExtent];
3649 assert(image != (Image *) NULL);
3650 assert(image->signature == MagickCoreSignature);
3651 if (image->debug != MagickFalse)
3652 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3653 assert(draw_info != (const DrawInfo *) NULL);
3654 assert(name != (const char *) NULL);
3655 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
3656 path=GetImageArtifact(image,property);
3657 if (path == (const char *) NULL)
3658 return(MagickFalse);
3659 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
3660 geometry=GetImageArtifact(image,property);
3661 if (geometry == (const char *) NULL)
3662 return(MagickFalse);
3663 if ((*pattern) != (Image *) NULL)
3664 *pattern=DestroyImage(*pattern);
3665 image_info=AcquireImageInfo();
3666 image_info->size=AcquireString(geometry);
3667 *pattern=AcquireImage(image_info,exception);
3668 image_info=DestroyImageInfo(image_info);
3669 (void) QueryColorCompliance("#000000ff",AllCompliance,
3670 &(*pattern)->background_color,exception);
3671 (void) SetImageBackgroundColor(*pattern,exception);
3672 if (image->debug != MagickFalse)
3673 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3674 "begin pattern-path %s %s",name,geometry);
3675 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3676 clone_info->fill_pattern=NewImageList();
3677 clone_info->stroke_pattern=NewImageList();
3678 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
3679 type=GetImageArtifact(image,property);
3680 if (type != (const char *) NULL)
3681 clone_info->gradient.type=(GradientType) ParseCommandOption(
3682 MagickGradientOptions,MagickFalse,type);
3683 (void) CloneString(&clone_info->primitive,path);
3684 status=DrawImage(*pattern,clone_info,exception);
3685 clone_info=DestroyDrawInfo(clone_info);
3686 if (image->debug != MagickFalse)
3687 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3696 + D r a w P o l y g o n P r i m i t i v e %
3700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3702 % DrawPolygonPrimitive() draws a polygon on the image.
3704 % The format of the DrawPolygonPrimitive method is:
3706 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3707 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3708 % ExceptionInfo *exception)
3710 % A description of each parameter follows:
3712 % o image: the image.
3714 % o draw_info: the draw info.
3716 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3718 % o exception: return any errors or warnings in this structure.
3722 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3727 assert(polygon_info != (PolygonInfo **) NULL);
3728 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3729 if (polygon_info[i] != (PolygonInfo *) NULL)
3730 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3731 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3732 return(polygon_info);
3735 static PolygonInfo **AcquirePolygonThreadSet(
3736 const PrimitiveInfo *primitive_info)
3739 *magick_restrict path_info;
3750 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3751 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3752 sizeof(*polygon_info));
3753 if (polygon_info == (PolygonInfo **) NULL)
3754 return((PolygonInfo **) NULL);
3755 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3756 path_info=ConvertPrimitiveToPath(primitive_info);
3757 if (path_info == (PathInfo *) NULL)
3758 return(DestroyPolygonThreadSet(polygon_info));
3759 for (i=0; i < (ssize_t) number_threads; i++)
3761 polygon_info[i]=ConvertPathToPolygon(path_info);
3762 if (polygon_info[i] == (PolygonInfo *) NULL)
3763 return(DestroyPolygonThreadSet(polygon_info));
3765 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3766 return(polygon_info);
3769 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3770 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3771 const ssize_t y,double *stroke_alpha)
3782 register const PointInfo
3796 Compute fill & stroke opacity for this (x,y) point.
3800 p=polygon_info->edges;
3801 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3803 if ((double) y <= (p->bounds.y1-mid-0.5))
3805 if ((double) y > (p->bounds.y2+mid+0.5))
3807 (void) DestroyEdge(polygon_info,(size_t) j);
3810 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3811 ((double) x > (p->bounds.x2+mid+0.5)))
3813 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3814 for ( ; i < (ssize_t) p->number_points; i++)
3816 if ((double) y <= (p->points[i-1].y-mid-0.5))
3818 if ((double) y > (p->points[i].y+mid+0.5))
3820 if (p->scanline != (double) y)
3822 p->scanline=(double) y;
3823 p->highwater=(size_t) i;
3826 Compute distance between a point and an edge.
3829 delta.x=(q+1)->x-q->x;
3830 delta.y=(q+1)->y-q->y;
3831 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3834 delta.x=(double) x-q->x;
3835 delta.y=(double) y-q->y;
3836 distance=delta.x*delta.x+delta.y*delta.y;
3840 alpha=delta.x*delta.x+delta.y*delta.y;
3843 delta.x=(double) x-(q+1)->x;
3844 delta.y=(double) y-(q+1)->y;
3845 distance=delta.x*delta.x+delta.y*delta.y;
3850 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3851 distance=alpha*beta*beta;
3855 Compute stroke & subpath opacity.
3858 if (p->ghostline == MagickFalse)
3861 if ((*stroke_alpha < 1.0) &&
3862 (distance <= ((alpha+0.25)*(alpha+0.25))))
3865 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3870 if (distance != 1.0)
3871 beta=sqrt((double) distance);
3873 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3874 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3878 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3880 if (distance <= 0.0)
3890 if (distance != 1.0)
3891 beta=sqrt(distance);
3894 if (subpath_alpha < (alpha*alpha))
3895 subpath_alpha=alpha*alpha;
3899 Compute fill opacity.
3901 if (fill == MagickFalse)
3903 if (subpath_alpha >= 1.0)
3906 Determine winding number.
3909 p=polygon_info->edges;
3910 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3912 if ((double) y <= p->bounds.y1)
3914 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3916 if ((double) x > p->bounds.x2)
3918 winding_number+=p->direction ? 1 : -1;
3921 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3922 for ( ; i < (ssize_t) p->number_points; i++)
3923 if ((double) y <= p->points[i].y)
3926 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3927 winding_number+=p->direction ? 1 : -1;
3929 if (fill_rule != NonZeroRule)
3931 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3935 if (MagickAbsoluteValue(winding_number) != 0)
3937 return(subpath_alpha);
3940 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3941 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3942 ExceptionInfo *exception)
3955 **magick_restrict polygon_info;
3972 Compute bounding box.
3974 assert(image != (Image *) NULL);
3975 assert(image->signature == MagickCoreSignature);
3976 if (image->debug != MagickFalse)
3977 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3978 assert(draw_info != (DrawInfo *) NULL);
3979 assert(draw_info->signature == MagickCoreSignature);
3980 assert(primitive_info != (PrimitiveInfo *) NULL);
3981 if (primitive_info->coordinates == 0)
3983 polygon_info=AcquirePolygonThreadSet(primitive_info);
3984 if (polygon_info == (PolygonInfo **) NULL)
3985 return(MagickFalse);
3986 DisableMSCWarning(4127)
3988 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3990 if (image->debug != MagickFalse)
3991 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3992 fill=(primitive_info->method == FillToBorderMethod) ||
3993 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3994 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3995 bounds=polygon_info[0]->edges[0].bounds;
3996 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3998 p=polygon_info[0]->edges+i;
3999 if (p->bounds.x1 < bounds.x1)
4000 bounds.x1=p->bounds.x1;
4001 if (p->bounds.y1 < bounds.y1)
4002 bounds.y1=p->bounds.y1;
4003 if (p->bounds.x2 > bounds.x2)
4004 bounds.x2=p->bounds.x2;
4005 if (p->bounds.y2 > bounds.y2)
4006 bounds.y2=p->bounds.y2;
4008 bounds.x1-=(mid+1.0);
4009 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
4010 image->columns ? (double) image->columns-1 : bounds.x1;
4011 bounds.y1-=(mid+1.0);
4012 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
4013 image->rows ? (double) image->rows-1 : bounds.y1;
4014 bounds.x2+=(mid+1.0);
4015 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
4016 image->columns ? (double) image->columns-1 : bounds.x2;
4017 bounds.y2+=(mid+1.0);
4018 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
4019 image->rows ? (double) image->rows-1 : bounds.y2;
4021 image_view=AcquireAuthenticCacheView(image,exception);
4022 if ((primitive_info->coordinates == 1) ||
4023 (polygon_info[0]->number_edges == 0))
4028 start_y=(ssize_t) ceil(bounds.y1-0.5);
4029 stop_y=(ssize_t) floor(bounds.y2+0.5);
4030 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4031 #pragma omp parallel for schedule(static,4) shared(status) \
4032 magick_threads(image,image,1,1)
4034 for (y=start_y; y <= stop_y; y++)
4052 if (status == MagickFalse)
4054 start_x=(ssize_t) ceil(bounds.x1-0.5);
4055 stop_x=(ssize_t) floor(bounds.x2+0.5);
4057 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4059 if (q == (Quantum *) NULL)
4064 GetPixelInfo(image,&pixel);
4065 for ( ; x <= stop_x; x++)
4067 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4068 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4070 GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4071 SetPixelViaPixelInfo(image,&pixel,q);
4073 q+=GetPixelChannels(image);
4075 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4076 if (sync == MagickFalse)
4079 image_view=DestroyCacheView(image_view);
4080 polygon_info=DestroyPolygonThreadSet(polygon_info);
4081 if (image->debug != MagickFalse)
4082 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4083 " end draw-polygon");
4087 Draw polygon or line.
4089 if (image->alpha_trait == UndefinedPixelTrait)
4090 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4091 start_y=(ssize_t) ceil(bounds.y1-0.5);
4092 stop_y=(ssize_t) floor(bounds.y2+0.5);
4093 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4094 #pragma omp parallel for schedule(static,4) shared(status) \
4095 magick_threads(image,image,1,1)
4097 for (y=start_y; y <= stop_y; y++)
4100 id = GetOpenMPThreadId();
4120 if (status == MagickFalse)
4122 start_x=(ssize_t) ceil(bounds.x1-0.5);
4123 stop_x=(ssize_t) floor(bounds.x2+0.5);
4124 q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+1),1,
4126 if (q == (Quantum *) NULL)
4131 for (x=start_x; x <= stop_x; x++)
4136 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4138 if (draw_info->stroke_antialias == MagickFalse)
4140 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4141 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4143 GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4144 fill_alpha=fill_alpha*fill_color.alpha;
4145 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4146 GetPixelAlpha(image,q),q);
4147 GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4148 stroke_alpha=stroke_alpha*stroke_color.alpha;
4149 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4150 GetPixelAlpha(image,q),q);
4151 q+=GetPixelChannels(image);
4153 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4156 image_view=DestroyCacheView(image_view);
4157 polygon_info=DestroyPolygonThreadSet(polygon_info);
4158 if (image->debug != MagickFalse)
4159 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4168 % D r a w P r i m i t i v e %
4172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4174 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4176 % The format of the DrawPrimitive method is:
4178 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4179 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4181 % A description of each parameter follows:
4183 % o image: the image.
4185 % o draw_info: the draw info.
4187 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4189 % o exception: return any errors or warnings in this structure.
4193 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4219 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4220 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4221 switch (primitive_info->primitive)
4223 case AlphaPrimitive:
4225 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4226 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4227 methods[primitive_info->method]);
4230 case ColorPrimitive:
4232 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4233 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4234 methods[primitive_info->method]);
4237 case ImagePrimitive:
4239 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4240 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4243 case PointPrimitive:
4245 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4246 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4247 methods[primitive_info->method]);
4252 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4253 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4260 p=primitive_info[0].point;
4263 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4265 point=primitive_info[i].point;
4266 if (coordinates <= 0)
4268 coordinates=(ssize_t) primitive_info[i].coordinates;
4269 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4270 " begin open (%.20g)",(double) coordinates);
4273 point=primitive_info[i].point;
4274 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4275 (fabs(q.y-point.y) >= MagickEpsilon))
4276 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4277 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4279 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4280 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4283 if (coordinates > 0)
4285 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4286 (fabs(p.y-point.y) >= MagickEpsilon))
4287 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4288 (double) coordinates);
4290 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4291 (double) coordinates);
4295 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4296 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4297 ExceptionInfo *exception)
4312 if (image->debug != MagickFalse)
4314 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4315 " begin draw-primitive");
4316 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4317 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
4318 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4319 draw_info->affine.tx,draw_info->affine.ty);
4321 if (SyncImagePixelCache(image,exception) == MagickFalse)
4322 return(MagickFalse);
4323 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4324 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4325 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4326 (void) SetImageColorspace(image,sRGBColorspace,exception);
4328 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4329 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4330 image_view=AcquireAuthenticCacheView(image,exception);
4331 switch (primitive_info->primitive)
4333 case AlphaPrimitive:
4335 if (image->alpha_trait == UndefinedPixelTrait)
4336 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4337 switch (primitive_info->method)
4348 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4349 if (q == (Quantum *) NULL)
4351 GetFillColor(draw_info,x,y,&pixel,exception);
4352 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4353 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4365 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4367 GetPixelInfo(image,&pixel);
4368 for (y=0; y < (ssize_t) image->rows; y++)
4373 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4375 if (q == (Quantum *) NULL)
4377 for (x=0; x < (ssize_t) image->columns; x++)
4379 GetPixelInfoPixel(image,q,&pixel);
4380 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4382 q+=GetPixelChannels(image);
4385 GetFillColor(draw_info,x,y,&pixel,exception);
4386 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4387 q+=GetPixelChannels(image);
4389 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4390 if (sync == MagickFalse)
4395 case FloodfillMethod:
4396 case FillToBorderMethod:
4404 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4406 if (primitive_info->method == FillToBorderMethod)
4408 target.red=(double) draw_info->border_color.red;
4409 target.green=(double) draw_info->border_color.green;
4410 target.blue=(double) draw_info->border_color.blue;
4412 channel_mask=SetImageChannelMask(image,AlphaChannel);
4413 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4414 primitive_info->method == FloodfillMethod ? MagickFalse :
4415 MagickTrue,exception);
4416 (void) SetImageChannelMask(image,channel_mask);
4427 for (y=0; y < (ssize_t) image->rows; y++)
4432 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4434 if (q == (Quantum *) NULL)
4436 for (x=0; x < (ssize_t) image->columns; x++)
4438 GetFillColor(draw_info,x,y,&pixel,exception);
4439 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4440 q+=GetPixelChannels(image);
4442 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4443 if (sync == MagickFalse)
4451 case ColorPrimitive:
4453 switch (primitive_info->method)
4464 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4465 if (q == (Quantum *) NULL)
4467 GetPixelInfo(image,&pixel);
4468 GetFillColor(draw_info,x,y,&pixel,exception);
4469 SetPixelViaPixelInfo(image,&pixel,q);
4470 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4482 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4484 for (y=0; y < (ssize_t) image->rows; y++)
4489 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4491 if (q == (Quantum *) NULL)
4493 for (x=0; x < (ssize_t) image->columns; x++)
4495 GetPixelInfoPixel(image,q,&pixel);
4496 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4498 q+=GetPixelChannels(image);
4501 GetFillColor(draw_info,x,y,&pixel,exception);
4502 SetPixelViaPixelInfo(image,&pixel,q);
4503 q+=GetPixelChannels(image);
4505 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4506 if (sync == MagickFalse)
4511 case FloodfillMethod:
4512 case FillToBorderMethod:
4517 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4519 if (primitive_info->method == FillToBorderMethod)
4521 target.red=(double) draw_info->border_color.red;
4522 target.green=(double) draw_info->border_color.green;
4523 target.blue=(double) draw_info->border_color.blue;
4525 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4526 primitive_info->method == FloodfillMethod ? MagickFalse :
4527 MagickTrue,exception);
4538 GetPixelInfo(image,&pixel);
4539 for (y=0; y < (ssize_t) image->rows; y++)
4544 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4546 if (q == (Quantum *) NULL)
4548 for (x=0; x < (ssize_t) image->columns; x++)
4550 GetFillColor(draw_info,x,y,&pixel,exception);
4551 SetPixelViaPixelInfo(image,&pixel,q);
4552 q+=GetPixelChannels(image);
4554 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4555 if (sync == MagickFalse)
4563 case ImagePrimitive:
4569 composite_geometry[MagickPathExtent];
4584 if (primitive_info->text == (char *) NULL)
4586 clone_info=AcquireImageInfo();
4587 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4588 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4592 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4594 composite_image=ReadImage(clone_info,exception);
4596 clone_info=DestroyImageInfo(clone_info);
4597 if (composite_image == (Image *) NULL)
4599 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4600 NULL,(void *) NULL);
4601 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4602 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4603 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4604 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4609 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4610 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
4611 composite_image->filter=image->filter;
4612 (void) TransformImage(&composite_image,(char *) NULL,
4613 composite_geometry,exception);
4615 if (composite_image->alpha_trait == UndefinedPixelTrait)
4616 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4618 if (draw_info->alpha != OpaqueAlpha)
4619 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4620 SetGeometry(image,&geometry);
4621 image->gravity=draw_info->gravity;
4624 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4625 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4626 composite_image->rows,(double) geometry.x,(double) geometry.y);
4627 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4628 affine=draw_info->affine;
4629 affine.tx=(double) geometry.x;
4630 affine.ty=(double) geometry.y;
4631 composite_image->interpolate=image->interpolate;
4632 if (draw_info->compose == OverCompositeOp)
4633 (void) DrawAffineImage(image,composite_image,&affine,exception);
4635 (void) CompositeImage(image,composite_image,draw_info->compose,
4636 MagickTrue,geometry.x,geometry.y,exception);
4637 composite_image=DestroyImage(composite_image);
4640 case PointPrimitive:
4648 if ((y < 0) || (y >= (ssize_t) image->rows))
4650 if ((x < 0) || (x >= (ssize_t) image->columns))
4652 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4653 if (q == (Quantum *) NULL)
4655 GetFillColor(draw_info,x,y,&fill_color,exception);
4656 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4657 (double) GetPixelAlpha(image,q),q);
4658 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4664 geometry[MagickPathExtent];
4669 if (primitive_info->text == (char *) NULL)
4671 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4672 (void) CloneString(&clone_info->text,primitive_info->text);
4673 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4674 primitive_info->point.x,primitive_info->point.y);
4675 (void) CloneString(&clone_info->geometry,geometry);
4676 status&=AnnotateImage(image,clone_info,exception);
4677 clone_info=DestroyDrawInfo(clone_info);
4689 if (IsEventLogging() != MagickFalse)
4690 LogPrimitiveInfo(primitive_info);
4691 scale=ExpandAffine(&draw_info->affine);
4692 if ((draw_info->dash_pattern != (double *) NULL) &&
4693 (draw_info->dash_pattern[0] != 0.0) &&
4694 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4695 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4700 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4701 clone_info->stroke_width=0.0;
4702 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4703 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4705 clone_info=DestroyDrawInfo(clone_info);
4706 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4709 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4711 ((draw_info->stroke.alpha != (Quantum) TransparentAlpha) ||
4712 (draw_info->stroke_pattern != (Image *) NULL)))
4718 Draw strokes while respecting line cap/join attributes.
4720 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4722 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4723 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4724 MagickTrue : MagickFalse;
4725 i=(ssize_t) primitive_info[0].coordinates;
4726 if ((((draw_info->linecap == RoundCap) ||
4727 (closed_path != MagickFalse)) &&
4728 (draw_info->linejoin == RoundJoin)) ||
4729 (primitive_info[i].primitive != UndefinedPrimitive))
4731 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4735 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4736 clone_info->stroke_width=0.0;
4737 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4738 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4740 clone_info=DestroyDrawInfo(clone_info);
4741 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4744 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4748 image_view=DestroyCacheView(image_view);
4749 if (image->debug != MagickFalse)
4750 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4751 return(status != 0 ? MagickTrue : MagickFalse);
4755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4759 + D r a w S t r o k e P o l y g o n %
4763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4765 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4766 % the image while respecting the line cap and join attributes.
4768 % The format of the DrawStrokePolygon method is:
4770 % MagickBooleanType DrawStrokePolygon(Image *image,
4771 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4773 % A description of each parameter follows:
4775 % o image: the image.
4777 % o draw_info: the draw info.
4779 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4784 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4785 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4793 for (i=0; i < 4; i++)
4794 linecap[i]=(*primitive_info);
4795 linecap[0].coordinates=4;
4796 linecap[1].point.x+=DrawEpsilon;
4797 linecap[2].point.x+=DrawEpsilon;
4798 linecap[2].point.y+=DrawEpsilon;
4799 linecap[3].point.y+=DrawEpsilon;
4800 linecap[4].primitive=UndefinedPrimitive;
4801 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4804 static MagickBooleanType DrawStrokePolygon(Image *image,
4805 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4806 ExceptionInfo *exception)
4820 register const PrimitiveInfo
4825 Draw stroked polygon.
4827 if (image->debug != MagickFalse)
4828 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4829 " begin draw-stroke-polygon");
4830 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4831 clone_info->fill=draw_info->stroke;
4832 if (clone_info->fill_pattern != (Image *) NULL)
4833 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4834 if (clone_info->stroke_pattern != (Image *) NULL)
4835 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4836 MagickTrue,exception);
4837 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4838 clone_info->stroke_width=0.0;
4839 clone_info->fill_rule=NonZeroRule;
4841 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4843 stroke_polygon=TraceStrokePolygon(draw_info,p);
4844 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4847 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4848 q=p+p->coordinates-1;
4849 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4850 MagickTrue : MagickFalse;
4851 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4853 DrawRoundLinecap(image,draw_info,p,exception);
4854 DrawRoundLinecap(image,draw_info,q,exception);
4857 clone_info=DestroyDrawInfo(clone_info);
4858 if (image->debug != MagickFalse)
4859 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4860 " end draw-stroke-polygon");
4861 return(status != 0 ? MagickTrue : MagickFalse);
4865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4869 % G e t A f f i n e M a t r i x %
4873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4875 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4878 % The format of the GetAffineMatrix method is:
4880 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4882 % A description of each parameter follows:
4884 % o affine_matrix: the affine matrix.
4887 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4889 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4890 assert(affine_matrix != (AffineMatrix *) NULL);
4891 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4892 affine_matrix->sx=1.0;
4893 affine_matrix->sy=1.0;
4897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4901 + G e t D r a w I n f o %
4905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4907 % GetDrawInfo() initializes draw_info to default values from image_info.
4909 % The format of the GetDrawInfo method is:
4911 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4913 % A description of each parameter follows:
4915 % o image_info: the image info..
4917 % o draw_info: the draw info.
4920 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4935 Initialize draw attributes.
4937 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4938 assert(draw_info != (DrawInfo *) NULL);
4939 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4940 clone_info=CloneImageInfo(image_info);
4941 GetAffineMatrix(&draw_info->affine);
4942 exception=AcquireExceptionInfo();
4943 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4945 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4947 draw_info->stroke_width=1.0;
4948 draw_info->alpha=OpaqueAlpha;
4949 draw_info->fill_rule=EvenOddRule;
4950 draw_info->linecap=ButtCap;
4951 draw_info->linejoin=MiterJoin;
4952 draw_info->miterlimit=10;
4953 draw_info->decorate=NoDecoration;
4954 draw_info->pointsize=12.0;
4955 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
4956 draw_info->compose=OverCompositeOp;
4957 draw_info->render=MagickTrue;
4958 draw_info->debug=IsEventLogging();
4959 draw_info->stroke_antialias=clone_info->antialias;
4960 if (clone_info->font != (char *) NULL)
4961 draw_info->font=AcquireString(clone_info->font);
4962 if (clone_info->density != (char *) NULL)
4963 draw_info->density=AcquireString(clone_info->density);
4964 draw_info->text_antialias=clone_info->antialias;
4965 if (clone_info->pointsize != 0.0)
4966 draw_info->pointsize=clone_info->pointsize;
4967 draw_info->border_color=clone_info->border_color;
4968 if (clone_info->server_name != (char *) NULL)
4969 draw_info->server_name=AcquireString(clone_info->server_name);
4970 option=GetImageOption(clone_info,"direction");
4971 if (option != (const char *) NULL)
4972 draw_info->direction=(DirectionType) ParseCommandOption(
4973 MagickDirectionOptions,MagickFalse,option);
4975 draw_info->direction=UndefinedDirection;
4976 option=GetImageOption(clone_info,"encoding");
4977 if (option != (const char *) NULL)
4978 (void) CloneString(&draw_info->encoding,option);
4979 option=GetImageOption(clone_info,"family");
4980 if (option != (const char *) NULL)
4981 (void) CloneString(&draw_info->family,option);
4982 option=GetImageOption(clone_info,"fill");
4983 if (option != (const char *) NULL)
4984 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4986 option=GetImageOption(clone_info,"gravity");
4987 if (option != (const char *) NULL)
4988 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4989 MagickFalse,option);
4990 option=GetImageOption(clone_info,"interline-spacing");
4991 if (option != (const char *) NULL)
4992 draw_info->interline_spacing=StringToDouble(option,&next_token);
4993 option=GetImageOption(clone_info,"interword-spacing");
4994 if (option != (const char *) NULL)
4995 draw_info->interword_spacing=StringToDouble(option,&next_token);
4996 option=GetImageOption(clone_info,"kerning");
4997 if (option != (const char *) NULL)
4998 draw_info->kerning=StringToDouble(option,&next_token);
4999 option=GetImageOption(clone_info,"stroke");
5000 if (option != (const char *) NULL)
5001 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
5003 option=GetImageOption(clone_info,"strokewidth");
5004 if (option != (const char *) NULL)
5005 draw_info->stroke_width=StringToDouble(option,&next_token);
5006 option=GetImageOption(clone_info,"style");
5007 if (option != (const char *) NULL)
5008 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
5009 MagickFalse,option);
5010 option=GetImageOption(clone_info,"undercolor");
5011 if (option != (const char *) NULL)
5012 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
5014 option=GetImageOption(clone_info,"weight");
5015 if (option != (const char *) NULL)
5020 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
5022 weight=(ssize_t) StringToUnsignedLong(option);
5023 draw_info->weight=(size_t) weight;
5025 exception=DestroyExceptionInfo(exception);
5026 draw_info->signature=MagickCoreSignature;
5027 clone_info=DestroyImageInfo(clone_info);
5031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5035 + P e r m u t a t e %
5039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5041 % Permutate() returns the permuation of the (n,k).
5043 % The format of the Permutate method is:
5045 % void Permutate(ssize_t n,ssize_t k)
5047 % A description of each parameter follows:
5055 static inline double Permutate(const ssize_t n,const ssize_t k)
5064 for (i=k+1; i <= n; i++)
5066 for (i=1; i <= (n-k); i++)
5072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5076 + T r a c e P r i m i t i v e %
5080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5082 % TracePrimitive is a collection of methods for generating graphic
5083 % primitives such as arcs, ellipses, paths, etc.
5087 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
5088 const PointInfo end,const PointInfo degrees)
5094 center.x=0.5*(end.x+start.x);
5095 center.y=0.5*(end.y+start.y);
5096 radii.x=fabs(center.x-start.x);
5097 radii.y=fabs(center.y-start.y);
5098 TraceEllipse(primitive_info,center,radii,degrees);
5101 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
5102 const PointInfo end,const PointInfo arc,const double angle,
5103 const MagickBooleanType large_arc,const MagickBooleanType sweep)
5122 register PrimitiveInfo
5131 if ((start.x == end.x) && (start.y == end.y))
5133 TracePoint(primitive_info,end);
5136 radii.x=fabs(arc.x);
5137 radii.y=fabs(arc.y);
5138 if ((radii.x == 0.0) || (radii.y == 0.0))
5140 TraceLine(primitive_info,start,end);
5143 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5144 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5145 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5146 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5147 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5149 if (delta < MagickEpsilon)
5151 TraceLine(primitive_info,start,end);
5156 radii.x*=sqrt((double) delta);
5157 radii.y*=sqrt((double) delta);
5159 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5160 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5161 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5162 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5163 alpha=points[1].x-points[0].x;
5164 beta=points[1].y-points[0].y;
5165 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5170 factor=sqrt((double) factor);
5171 if (sweep == large_arc)
5174 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5175 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5176 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5177 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5178 if ((theta < 0.0) && (sweep != MagickFalse))
5179 theta+=(double) (2.0*MagickPI);
5181 if ((theta > 0.0) && (sweep == MagickFalse))
5182 theta-=(double) (2.0*MagickPI);
5183 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5186 for (i=0; i < (ssize_t) arc_segments; i++)
5188 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5189 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5190 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5191 sin(fmod((double) beta,DegreesToRadians(360.0)));
5192 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5193 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5194 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5195 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5196 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5197 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5198 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5199 theta/arc_segments),DegreesToRadians(360.0))));
5200 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5201 theta/arc_segments),DegreesToRadians(360.0))));
5202 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5203 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5204 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5205 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5206 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5207 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5208 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5210 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5212 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5214 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5216 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5218 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5220 if (i == (ssize_t) (arc_segments-1))
5225 primitive_info->coordinates=(size_t) (p-primitive_info);
5226 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5228 p->primitive=primitive_info->primitive;
5233 static void TraceBezier(PrimitiveInfo *primitive_info,
5234 const size_t number_coordinates)
5246 register PrimitiveInfo
5258 Allocate coeficients.
5260 quantum=number_coordinates;
5261 for (i=0; i < (ssize_t) number_coordinates; i++)
5263 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5265 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5266 if (alpha > (double) quantum)
5267 quantum=(size_t) alpha;
5268 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5269 if (alpha > (double) quantum)
5270 quantum=(size_t) alpha;
5273 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5274 (double) BezierQuantum);
5275 control_points=quantum*number_coordinates;
5276 coefficients=(double *) AcquireQuantumMemory((size_t)
5277 number_coordinates,sizeof(*coefficients));
5278 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5280 if ((coefficients == (double *) NULL) ||
5281 (points == (PointInfo *) NULL))
5282 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5284 Compute bezier points.
5286 end=primitive_info[number_coordinates-1].point;
5287 for (i=0; i < (ssize_t) number_coordinates; i++)
5288 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5290 for (i=0; i < (ssize_t) control_points; i++)
5295 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5296 for (j=0; j < (ssize_t) number_coordinates; j++)
5298 point.x+=alpha*coefficients[j]*p->point.x;
5299 point.y+=alpha*coefficients[j]*p->point.y;
5300 alpha*=weight/(1.0-weight);
5304 weight+=1.0/control_points;
5307 Bezier curves are just short segmented polys.
5310 for (i=0; i < (ssize_t) control_points; i++)
5312 TracePoint(p,points[i]);
5317 primitive_info->coordinates=(size_t) (p-primitive_info);
5318 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5320 p->primitive=primitive_info->primitive;
5323 points=(PointInfo *) RelinquishMagickMemory(points);
5324 coefficients=(double *) RelinquishMagickMemory(coefficients);
5327 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5328 const PointInfo end)
5339 alpha=end.x-start.x;
5341 radius=hypot((double) alpha,(double) beta);
5342 offset.x=(double) radius;
5343 offset.y=(double) radius;
5346 TraceEllipse(primitive_info,start,offset,degrees);
5349 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5350 const PointInfo stop,const PointInfo degrees)
5361 register PrimitiveInfo
5368 Ellipses are just short segmented polys.
5370 if ((stop.x == 0.0) && (stop.y == 0.0))
5372 TracePoint(primitive_info,start);
5375 delta=2.0/MagickMax(stop.x,stop.y);
5376 step=(double) (MagickPI/8.0);
5377 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5378 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5379 angle.x=DegreesToRadians(degrees.x);
5381 while (y < degrees.x)
5383 angle.y=(double) DegreesToRadians(y);
5384 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5386 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5387 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5388 TracePoint(p,point);
5391 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5392 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5393 TracePoint(p,point);
5395 primitive_info->coordinates=(size_t) (p-primitive_info);
5396 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5398 p->primitive=primitive_info->primitive;
5403 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5404 const PointInfo end)
5406 TracePoint(primitive_info,start);
5407 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5408 (fabs(start.y-end.y) < MagickEpsilon))
5410 primitive_info->primitive=PointPrimitive;
5411 primitive_info->coordinates=1;
5414 TracePoint(primitive_info+1,end);
5415 (primitive_info+1)->primitive=primitive_info->primitive;
5416 primitive_info->coordinates=2;
5419 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5423 token[MagickPathExtent];
5438 points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5445 register PrimitiveInfo
5456 number_coordinates=0;
5458 primitive_type=primitive_info->primitive;
5460 for (p=path; *p != '\0'; )
5462 while (isspace((int) ((unsigned char) *p)) != 0)
5466 last_attribute=attribute;
5467 attribute=(int) (*p++);
5488 GetNextToken(p,&p,MagickPathExtent,token);
5490 GetNextToken(p,&p,MagickPathExtent,token);
5491 arc.x=StringToDouble(token,&next_token);
5492 GetNextToken(p,&p,MagickPathExtent,token);
5494 GetNextToken(p,&p,MagickPathExtent,token);
5495 arc.y=StringToDouble(token,&next_token);
5496 GetNextToken(p,&p,MagickPathExtent,token);
5498 GetNextToken(p,&p,MagickPathExtent,token);
5499 angle=StringToDouble(token,&next_token);
5500 GetNextToken(p,&p,MagickPathExtent,token);
5502 GetNextToken(p,&p,MagickPathExtent,token);
5503 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5504 GetNextToken(p,&p,MagickPathExtent,token);
5506 GetNextToken(p,&p,MagickPathExtent,token);
5507 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5508 GetNextToken(p,&p,MagickPathExtent,token);
5510 GetNextToken(p,&p,MagickPathExtent,token);
5511 x=StringToDouble(token,&next_token);
5512 GetNextToken(p,&p,MagickPathExtent,token);
5514 GetNextToken(p,&p,MagickPathExtent,token);
5515 y=StringToDouble(token,&next_token);
5516 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5517 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5518 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5521 while (isspace((int) ((unsigned char) *p)) != 0)
5525 } while (IsPoint(p) != MagickFalse);
5532 Compute bezier points.
5537 for (i=1; i < 4; i++)
5539 GetNextToken(p,&p,MagickPathExtent,token);
5541 GetNextToken(p,&p,MagickPathExtent,token);
5542 x=StringToDouble(token,&next_token);
5543 GetNextToken(p,&p,MagickPathExtent,token);
5545 GetNextToken(p,&p,MagickPathExtent,token);
5546 y=StringToDouble(token,&next_token);
5547 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5548 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5551 for (i=0; i < 4; i++)
5552 (q+i)->point=points[i];
5556 } while (IsPoint(p) != MagickFalse);
5564 GetNextToken(p,&p,MagickPathExtent,token);
5566 GetNextToken(p,&p,MagickPathExtent,token);
5567 x=StringToDouble(token,&next_token);
5568 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5569 TracePoint(q,point);
5571 } while (IsPoint(p) != MagickFalse);
5579 GetNextToken(p,&p,MagickPathExtent,token);
5581 GetNextToken(p,&p,MagickPathExtent,token);
5582 x=StringToDouble(token,&next_token);
5583 GetNextToken(p,&p,MagickPathExtent,token);
5585 GetNextToken(p,&p,MagickPathExtent,token);
5586 y=StringToDouble(token,&next_token);
5587 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5588 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5589 TracePoint(q,point);
5591 } while (IsPoint(p) != MagickFalse);
5597 if (q != primitive_info)
5599 primitive_info->coordinates=(size_t) (q-primitive_info);
5600 number_coordinates+=primitive_info->coordinates;
5606 GetNextToken(p,&p,MagickPathExtent,token);
5608 GetNextToken(p,&p,MagickPathExtent,token);
5609 x=StringToDouble(token,&next_token);
5610 GetNextToken(p,&p,MagickPathExtent,token);
5612 GetNextToken(p,&p,MagickPathExtent,token);
5613 y=StringToDouble(token,&next_token);
5614 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5615 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5619 TracePoint(q,point);
5621 if ((i != 0) && (attribute == (int) 'M'))
5623 TracePoint(q,point);
5626 } while (IsPoint(p) != MagickFalse);
5633 Compute bezier points.
5638 for (i=1; i < 3; i++)
5640 GetNextToken(p,&p,MagickPathExtent,token);
5642 GetNextToken(p,&p,MagickPathExtent,token);
5643 x=StringToDouble(token,&next_token);
5644 GetNextToken(p,&p,MagickPathExtent,token);
5646 GetNextToken(p,&p,MagickPathExtent,token);
5647 y=StringToDouble(token,&next_token);
5650 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5651 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5654 for (i=0; i < 3; i++)
5655 (q+i)->point=points[i];
5659 } while (IsPoint(p) != MagickFalse);
5666 Compute bezier points.
5670 points[0]=points[3];
5671 points[1].x=2.0*points[3].x-points[2].x;
5672 points[1].y=2.0*points[3].y-points[2].y;
5673 for (i=2; i < 4; i++)
5675 GetNextToken(p,&p,MagickPathExtent,token);
5677 GetNextToken(p,&p,MagickPathExtent,token);
5678 x=StringToDouble(token,&next_token);
5679 GetNextToken(p,&p,MagickPathExtent,token);
5681 GetNextToken(p,&p,MagickPathExtent,token);
5682 y=StringToDouble(token,&next_token);
5685 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5686 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5689 if (strchr("CcSs",last_attribute) == (char *) NULL)
5694 for (i=0; i < 4; i++)
5695 (q+i)->point=points[i];
5699 } while (IsPoint(p) != MagickFalse);
5706 Compute bezier points.
5710 points[0]=points[2];
5711 points[1].x=2.0*points[2].x-points[1].x;
5712 points[1].y=2.0*points[2].y-points[1].y;
5713 for (i=2; i < 3; i++)
5715 GetNextToken(p,&p,MagickPathExtent,token);
5717 GetNextToken(p,&p,MagickPathExtent,token);
5718 x=StringToDouble(token,&next_token);
5719 GetNextToken(p,&p,MagickPathExtent,token);
5721 GetNextToken(p,&p,MagickPathExtent,token);
5722 y=StringToDouble(token,&next_token);
5723 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5724 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5727 if (strchr("QqTt",last_attribute) == (char *) NULL)
5732 for (i=0; i < 3; i++)
5733 (q+i)->point=points[i];
5737 } while (IsPoint(p) != MagickFalse);
5745 GetNextToken(p,&p,MagickPathExtent,token);
5747 GetNextToken(p,&p,MagickPathExtent,token);
5748 y=StringToDouble(token,&next_token);
5749 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5750 TracePoint(q,point);
5752 } while (IsPoint(p) != MagickFalse);
5759 TracePoint(q,point);
5761 primitive_info->coordinates=(size_t) (q-primitive_info);
5762 number_coordinates+=primitive_info->coordinates;
5769 if (isalpha((int) ((unsigned char) attribute)) != 0)
5770 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5776 primitive_info->coordinates=(size_t) (q-primitive_info);
5777 number_coordinates+=primitive_info->coordinates;
5778 for (i=0; i < (ssize_t) number_coordinates; i++)
5781 q->primitive=primitive_type;
5783 q->method=FillToBorderMethod;
5786 return(number_coordinates);
5789 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5790 const PointInfo end)
5795 register PrimitiveInfo
5802 TracePoint(p,start);
5806 TracePoint(p,point);
5812 TracePoint(p,point);
5814 TracePoint(p,start);
5816 primitive_info->coordinates=(size_t) (p-primitive_info);
5817 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5819 p->primitive=primitive_info->primitive;
5824 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5825 const PointInfo start,const PointInfo end,PointInfo arc)
5832 register PrimitiveInfo
5839 offset.x=fabs(end.x-start.x);
5840 offset.y=fabs(end.y-start.y);
5841 if (arc.x > (0.5*offset.x))
5843 if (arc.y > (0.5*offset.y))
5845 point.x=start.x+offset.x-arc.x;
5846 point.y=start.y+arc.y;
5849 TraceEllipse(p,point,arc,degrees);
5851 point.x=start.x+offset.x-arc.x;
5852 point.y=start.y+offset.y-arc.y;
5855 TraceEllipse(p,point,arc,degrees);
5857 point.x=start.x+arc.x;
5858 point.y=start.y+offset.y-arc.y;
5861 TraceEllipse(p,point,arc,degrees);
5863 point.x=start.x+arc.x;
5864 point.y=start.y+arc.y;
5867 TraceEllipse(p,point,arc,degrees);
5869 TracePoint(p,primitive_info->point);
5871 primitive_info->coordinates=(size_t) (p-primitive_info);
5872 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5874 p->primitive=primitive_info->primitive;
5879 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5880 const size_t number_vertices,const double offset)
5897 for (i=1; i < (ssize_t) number_vertices; i++)
5899 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5900 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5901 if ((fabs((double) dx) >= MagickEpsilon) ||
5902 (fabs((double) dy) >= MagickEpsilon))
5905 if (i == (ssize_t) number_vertices)
5906 i=(ssize_t) number_vertices-1L;
5907 distance=hypot((double) dx,(double) dy);
5908 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5909 dx*(distance+offset)/distance);
5910 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5911 dy*(distance+offset)/distance);
5912 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5914 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5915 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5916 if ((fabs((double) dx) >= MagickEpsilon) ||
5917 (fabs((double) dy) >= MagickEpsilon))
5920 distance=hypot((double) dx,(double) dy);
5921 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5922 dx*(distance+offset)/distance);
5923 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5924 dy*(distance+offset)/distance);
5927 static inline double DrawEpsilonReciprocal(const double x)
5929 double sign = x < 0.0 ? -1.0 : 1.0;
5930 return((sign*x) >= DrawEpsilon ? 1.0/x : sign*(1.0/DrawEpsilon));
5933 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5934 const PrimitiveInfo *primitive_info)
5936 typedef struct _LineSegment
5988 number_vertices=primitive_info->coordinates;
5989 max_strokes=2*number_vertices+6*BezierQuantum+360;
5990 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5992 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5994 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5995 number_vertices+2UL,sizeof(*polygon_primitive));
5996 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5997 (polygon_primitive == (PrimitiveInfo *) NULL))
5998 return((PrimitiveInfo *) NULL);
5999 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
6000 number_vertices*sizeof(*polygon_primitive));
6002 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
6003 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
6004 MagickTrue : MagickFalse;
6005 if ((draw_info->linejoin == RoundJoin) ||
6006 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
6008 polygon_primitive[number_vertices]=primitive_info[1];
6011 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
6013 Compute the slope for the first line segment, p.
6017 for (n=1; n < (ssize_t) number_vertices; n++)
6019 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
6020 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
6021 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
6024 if (n == (ssize_t) number_vertices)
6025 n=(ssize_t) number_vertices-1L;
6027 inverse_slope.p=0.0;
6028 if (fabs(dx.p) < MagickEpsilon)
6031 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
6033 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
6036 if (fabs(dy.p) < MagickEpsilon)
6039 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
6041 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
6046 inverse_slope.p=(-1.0/slope.p);
6048 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
6049 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
6051 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
6052 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
6053 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
6054 offset.y=(double) (offset.x*inverse_slope.p);
6055 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
6057 box_p[0].x=polygon_primitive[0].point.x-offset.x;
6058 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
6059 box_p[1].x=polygon_primitive[n].point.x-offset.x;
6060 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
6061 box_q[0].x=polygon_primitive[0].point.x+offset.x;
6062 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
6063 box_q[1].x=polygon_primitive[n].point.x+offset.x;
6064 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
6068 box_p[0].x=polygon_primitive[0].point.x+offset.x;
6069 box_p[0].y=polygon_primitive[0].point.y+offset.y;
6070 box_p[1].x=polygon_primitive[n].point.x+offset.x;
6071 box_p[1].y=polygon_primitive[n].point.y+offset.y;
6072 box_q[0].x=polygon_primitive[0].point.x-offset.x;
6073 box_q[0].y=polygon_primitive[0].point.y-offset.y;
6074 box_q[1].x=polygon_primitive[n].point.x-offset.x;
6075 box_q[1].y=polygon_primitive[n].point.y-offset.y;
6078 Create strokes for the line join attribute: bevel, miter, round.
6082 path_q[p++]=box_q[0];
6083 path_p[q++]=box_p[0];
6084 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
6087 Compute the slope for this line segment, q.
6089 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
6090 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
6091 dot_product=dx.q*dx.q+dy.q*dy.q;
6092 if (dot_product < 0.25)
6095 inverse_slope.q=0.0;
6096 if (fabs(dx.q) < MagickEpsilon)
6099 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
6101 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
6104 if (fabs(dy.q) < MagickEpsilon)
6107 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
6109 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
6114 inverse_slope.q=(-1.0/slope.q);
6116 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
6117 offset.y=(double) (offset.x*inverse_slope.q);
6118 dot_product=dy.q*offset.x-dx.q*offset.y;
6119 if (dot_product > 0.0)
6121 box_p[2].x=polygon_primitive[n].point.x-offset.x;
6122 box_p[2].y=polygon_primitive[n].point.y-offset.y;
6123 box_p[3].x=polygon_primitive[i].point.x-offset.x;
6124 box_p[3].y=polygon_primitive[i].point.y-offset.y;
6125 box_q[2].x=polygon_primitive[n].point.x+offset.x;
6126 box_q[2].y=polygon_primitive[n].point.y+offset.y;
6127 box_q[3].x=polygon_primitive[i].point.x+offset.x;
6128 box_q[3].y=polygon_primitive[i].point.y+offset.y;
6132 box_p[2].x=polygon_primitive[n].point.x+offset.x;
6133 box_p[2].y=polygon_primitive[n].point.y+offset.y;
6134 box_p[3].x=polygon_primitive[i].point.x+offset.x;
6135 box_p[3].y=polygon_primitive[i].point.y+offset.y;
6136 box_q[2].x=polygon_primitive[n].point.x-offset.x;
6137 box_q[2].y=polygon_primitive[n].point.y-offset.y;
6138 box_q[3].x=polygon_primitive[i].point.x-offset.x;
6139 box_q[3].y=polygon_primitive[i].point.y-offset.y;
6141 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
6148 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
6149 box_p[3].y)/(slope.p-slope.q));
6150 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
6151 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
6152 box_q[3].y)/(slope.p-slope.q));
6153 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
6155 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
6157 if (~max_strokes < (6*BezierQuantum+360))
6159 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6160 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6164 max_strokes+=6*BezierQuantum+360;
6165 path_p=(PointInfo *) ResizeQuantumMemory(path_p,max_strokes,
6167 path_q=(PointInfo *) ResizeQuantumMemory(path_q,max_strokes,
6170 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
6172 if (path_p != (PointInfo *) NULL)
6173 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6174 if (path_q != (PointInfo *) NULL)
6175 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6176 polygon_primitive=(PrimitiveInfo *)
6177 RelinquishMagickMemory(polygon_primitive);
6178 return((PrimitiveInfo *) NULL);
6181 dot_product=dx.q*dy.p-dx.p*dy.q;
6182 if (dot_product <= 0.0)
6183 switch (draw_info->linejoin)
6187 path_q[q++]=box_q[1];
6188 path_q[q++]=box_q[2];
6189 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6190 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6191 if (dot_product <= miterlimit)
6192 path_p[p++]=box_p[4];
6195 path_p[p++]=box_p[1];
6196 path_p[p++]=box_p[2];
6202 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6203 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6204 if (dot_product <= miterlimit)
6206 path_q[q++]=box_q[4];
6207 path_p[p++]=box_p[4];
6211 path_q[q++]=box_q[1];
6212 path_q[q++]=box_q[2];
6213 path_p[p++]=box_p[1];
6214 path_p[p++]=box_p[2];
6220 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6221 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6222 if (dot_product <= miterlimit)
6223 path_p[p++]=box_p[4];
6226 path_p[p++]=box_p[1];
6227 path_p[p++]=box_p[2];
6229 center=polygon_primitive[n].point;
6230 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6231 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6232 if (theta.q < theta.p)
6233 theta.q+=(double) (2.0*MagickPI);
6234 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6235 (2.0*sqrt((double) (1.0/mid)))));
6236 path_q[q].x=box_q[1].x;
6237 path_q[q].y=box_q[1].y;
6239 for (j=1; j < (ssize_t) arc_segments; j++)
6241 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6242 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6243 (theta.p+delta_theta),DegreesToRadians(360.0))));
6244 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6245 (theta.p+delta_theta),DegreesToRadians(360.0))));
6248 path_q[q++]=box_q[2];
6255 switch (draw_info->linejoin)
6259 path_p[p++]=box_p[1];
6260 path_p[p++]=box_p[2];
6261 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6262 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6263 if (dot_product <= miterlimit)
6264 path_q[q++]=box_q[4];
6267 path_q[q++]=box_q[1];
6268 path_q[q++]=box_q[2];
6274 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6275 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6276 if (dot_product <= miterlimit)
6278 path_q[q++]=box_q[4];
6279 path_p[p++]=box_p[4];
6283 path_q[q++]=box_q[1];
6284 path_q[q++]=box_q[2];
6285 path_p[p++]=box_p[1];
6286 path_p[p++]=box_p[2];
6292 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6293 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6294 if (dot_product <= miterlimit)
6295 path_q[q++]=box_q[4];
6298 path_q[q++]=box_q[1];
6299 path_q[q++]=box_q[2];
6301 center=polygon_primitive[n].point;
6302 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6303 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6304 if (theta.p < theta.q)
6305 theta.p+=(double) (2.0*MagickPI);
6306 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6307 (2.0*sqrt((double) (1.0/mid)))));
6308 path_p[p++]=box_p[1];
6309 for (j=1; j < (ssize_t) arc_segments; j++)
6311 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6312 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6313 (theta.p+delta_theta),DegreesToRadians(360.0))));
6314 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6315 (theta.p+delta_theta),DegreesToRadians(360.0))));
6318 path_p[p++]=box_p[2];
6325 inverse_slope.p=inverse_slope.q;
6334 path_p[p++]=box_p[1];
6335 path_q[q++]=box_q[1];
6337 Trace stroked polygon.
6339 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6340 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6341 if (stroke_polygon != (PrimitiveInfo *) NULL)
6343 for (i=0; i < (ssize_t) p; i++)
6345 stroke_polygon[i]=polygon_primitive[0];
6346 stroke_polygon[i].point=path_p[i];
6348 if (closed_path != MagickFalse)
6350 stroke_polygon[i]=polygon_primitive[0];
6351 stroke_polygon[i].point=stroke_polygon[0].point;
6354 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6356 stroke_polygon[i]=polygon_primitive[0];
6357 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6359 if (closed_path != MagickFalse)
6361 stroke_polygon[i]=polygon_primitive[0];
6362 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6365 stroke_polygon[i]=polygon_primitive[0];
6366 stroke_polygon[i].point=stroke_polygon[0].point;
6368 stroke_polygon[i].primitive=UndefinedPrimitive;
6369 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6371 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6372 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6373 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6374 return(stroke_polygon);