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-10)
98 typedef struct _EdgeInfo
122 typedef struct _ElementInfo
132 typedef struct _PolygonInfo
150 typedef struct _PathInfo
160 Forward declarations.
162 static MagickBooleanType
163 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
167 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
170 TracePath(PrimitiveInfo *,const char *);
173 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
174 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
175 const double,const MagickBooleanType,const MagickBooleanType),
176 TraceBezier(PrimitiveInfo *,const size_t),
177 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,
180 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
181 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
182 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
184 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % A c q u i r e D r a w I n f o %
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
199 % The format of the AcquireDrawInfo method is:
201 % DrawInfo *AcquireDrawInfo(void)
204 MagickExport DrawInfo *AcquireDrawInfo(void)
209 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
210 if (draw_info == (DrawInfo *) NULL)
211 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
212 GetDrawInfo((ImageInfo *) NULL,draw_info);
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 % C l o n e D r a w I n f o %
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
228 % is specified, a new DrawInfo structure is created initialized to default
231 % The format of the CloneDrawInfo method is:
233 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
234 % const DrawInfo *draw_info)
236 % A description of each parameter follows:
238 % o image_info: the image info.
240 % o draw_info: the draw info.
243 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
244 const DrawInfo *draw_info)
252 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
253 if (clone_info == (DrawInfo *) NULL)
254 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
255 GetDrawInfo(image_info,clone_info);
256 if (draw_info == (DrawInfo *) NULL)
258 exception=AcquireExceptionInfo();
259 if (clone_info->primitive != (char *) NULL)
260 (void) CloneString(&clone_info->primitive,draw_info->primitive);
261 if (draw_info->geometry != (char *) NULL)
262 (void) CloneString(&clone_info->geometry,draw_info->geometry);
263 clone_info->viewbox=draw_info->viewbox;
264 clone_info->affine=draw_info->affine;
265 clone_info->gravity=draw_info->gravity;
266 clone_info->fill=draw_info->fill;
267 clone_info->stroke=draw_info->stroke;
268 clone_info->stroke_width=draw_info->stroke_width;
269 if (draw_info->fill_pattern != (Image *) NULL)
270 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
272 if (draw_info->stroke_pattern != (Image *) NULL)
273 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
274 MagickTrue,exception);
275 clone_info->stroke_antialias=draw_info->stroke_antialias;
276 clone_info->text_antialias=draw_info->text_antialias;
277 clone_info->fill_rule=draw_info->fill_rule;
278 clone_info->linecap=draw_info->linecap;
279 clone_info->linejoin=draw_info->linejoin;
280 clone_info->miterlimit=draw_info->miterlimit;
281 clone_info->dash_offset=draw_info->dash_offset;
282 clone_info->decorate=draw_info->decorate;
283 clone_info->compose=draw_info->compose;
284 if (draw_info->text != (char *) NULL)
285 (void) CloneString(&clone_info->text,draw_info->text);
286 if (draw_info->font != (char *) NULL)
287 (void) CloneString(&clone_info->font,draw_info->font);
288 if (draw_info->metrics != (char *) NULL)
289 (void) CloneString(&clone_info->metrics,draw_info->metrics);
290 if (draw_info->family != (char *) NULL)
291 (void) CloneString(&clone_info->family,draw_info->family);
292 clone_info->style=draw_info->style;
293 clone_info->stretch=draw_info->stretch;
294 clone_info->weight=draw_info->weight;
295 if (draw_info->encoding != (char *) NULL)
296 (void) CloneString(&clone_info->encoding,draw_info->encoding);
297 clone_info->pointsize=draw_info->pointsize;
298 clone_info->kerning=draw_info->kerning;
299 clone_info->interline_spacing=draw_info->interline_spacing;
300 clone_info->interword_spacing=draw_info->interword_spacing;
301 clone_info->direction=draw_info->direction;
302 if (draw_info->density != (char *) NULL)
303 (void) CloneString(&clone_info->density,draw_info->density);
304 clone_info->align=draw_info->align;
305 clone_info->undercolor=draw_info->undercolor;
306 clone_info->border_color=draw_info->border_color;
307 if (draw_info->server_name != (char *) NULL)
308 (void) CloneString(&clone_info->server_name,draw_info->server_name);
309 if (draw_info->dash_pattern != (double *) NULL)
314 for (x=0; fabs(draw_info->dash_pattern[x]) >= DrawEpsilon; x++) ;
315 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
316 sizeof(*clone_info->dash_pattern));
317 if (clone_info->dash_pattern == (double *) NULL)
318 ThrowFatalException(ResourceLimitFatalError,
319 "UnableToAllocateDashPattern");
320 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
321 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
323 clone_info->gradient=draw_info->gradient;
324 if (draw_info->gradient.stops != (StopInfo *) NULL)
329 number_stops=clone_info->gradient.number_stops;
330 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
331 number_stops,sizeof(*clone_info->gradient.stops));
332 if (clone_info->gradient.stops == (StopInfo *) NULL)
333 ThrowFatalException(ResourceLimitFatalError,
334 "UnableToAllocateDashPattern");
335 (void) CopyMagickMemory(clone_info->gradient.stops,
336 draw_info->gradient.stops,(size_t) number_stops*
337 sizeof(*clone_info->gradient.stops));
339 if (draw_info->clip_mask != (char *) NULL)
340 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
341 clone_info->bounds=draw_info->bounds;
342 clone_info->clip_units=draw_info->clip_units;
343 clone_info->render=draw_info->render;
344 clone_info->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-DrawEpsilon) > q->points[0].y)
399 if ((p->points[0].y+DrawEpsilon) < q->points[0].y)
401 if ((p->points[0].x-DrawEpsilon) > q->points[0].x)
403 if ((p->points[0].x+DrawEpsilon) < 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 ((fabs(path_info[i].point.y-point.y) < DrawEpsilon) &&
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) >= DrawEpsilon) ||
770 (fabs(q.y-primitive_info[i].point.y) >= DrawEpsilon))
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) < DrawEpsilon) &&
780 (fabs(p.y-primitive_info[i].point.y) < DrawEpsilon))
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 >= DrawEpsilon)
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 < -DrawEpsilon)
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 >= DrawEpsilon)
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 < -DrawEpsilon)
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=fabs(draw_info->dash_offset) >= DrawEpsilon ?
1524 scale*draw_info->dash_offset : 0.0;
1526 for (n=0; offset > 0.0; j=0)
1528 if (draw_info->dash_pattern[n] <= 0.0)
1530 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1531 if (offset > length)
1535 length=scale*(draw_info->dash_pattern[n]+0.5);
1538 if (offset < length)
1550 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1552 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1553 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1554 maximum_length=hypot((double) dx,dy);
1555 if (fabs(length) < DrawEpsilon)
1558 if (fabs(draw_info->dash_pattern[n]) < DrawEpsilon)
1560 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1562 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1564 total_length+=length;
1565 if ((n & 0x01) != 0)
1567 dash_polygon[0]=primitive_info[0];
1568 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1569 total_length/maximum_length);
1570 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1571 total_length/maximum_length);
1576 if ((j+1) > (ssize_t) (2*number_vertices))
1578 dash_polygon[j]=primitive_info[i-1];
1579 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1580 total_length/maximum_length);
1581 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1582 total_length/maximum_length);
1583 dash_polygon[j].coordinates=1;
1585 dash_polygon[0].coordinates=(size_t) j;
1586 dash_polygon[j].primitive=UndefinedPrimitive;
1587 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1590 if (fabs(draw_info->dash_pattern[n]) < DrawEpsilon)
1592 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1594 length-=(maximum_length-total_length);
1595 if ((n & 0x01) != 0)
1597 dash_polygon[j]=primitive_info[i];
1598 dash_polygon[j].coordinates=1;
1601 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1603 dash_polygon[j]=primitive_info[i-1];
1604 dash_polygon[j].point.x+=DrawEpsilon;
1605 dash_polygon[j].point.y+=DrawEpsilon;
1606 dash_polygon[j].coordinates=1;
1608 dash_polygon[0].coordinates=(size_t) j;
1609 dash_polygon[j].primitive=UndefinedPrimitive;
1610 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1612 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1613 clone_info=DestroyDrawInfo(clone_info);
1614 if (image->debug != MagickFalse)
1615 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1616 return(status != 0 ? MagickTrue : MagickFalse);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % D r a w I m a g e %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % DrawImage() draws a graphic primitive on your image. The primitive
1631 % may be represented as a string or filename. Precede the filename with an
1632 % "at" sign (@) and the contents of the file are drawn on the image. You
1633 % can affect how text is drawn by setting one or more members of the draw
1636 % The format of the DrawImage method is:
1638 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1639 % ExceptionInfo *exception)
1641 % A description of each parameter follows:
1643 % o image: the image.
1645 % o draw_info: the draw info.
1647 % o exception: return any errors or warnings in this structure.
1651 static inline MagickBooleanType IsPoint(const char *point)
1659 value=StringToDouble(point,&p);
1660 return((fabs(value) < DrawEpsilon) && (p == point) ? MagickFalse : MagickTrue);
1663 static inline void TracePoint(PrimitiveInfo *primitive_info,
1664 const PointInfo point)
1666 primitive_info->coordinates=1;
1667 primitive_info->point=point;
1670 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1671 ExceptionInfo *exception)
1673 #define RenderImageTag "Render/Image"
1680 keyword[MagickPathExtent],
1681 geometry[MagickPathExtent],
1683 pattern[MagickPathExtent],
1740 Ensure the annotation info is valid.
1742 assert(image != (Image *) NULL);
1743 assert(image->signature == MagickCoreSignature);
1744 if (image->debug != MagickFalse)
1745 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1746 assert(draw_info != (DrawInfo *) NULL);
1747 assert(draw_info->signature == MagickCoreSignature);
1748 if (image->debug != MagickFalse)
1749 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1750 if ((draw_info->primitive == (char *) NULL) ||
1751 (*draw_info->primitive == '\0'))
1752 return(MagickFalse);
1753 if (image->debug != MagickFalse)
1754 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1755 if (*draw_info->primitive != '@')
1756 primitive=AcquireString(draw_info->primitive);
1758 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1759 if (primitive == (char *) NULL)
1760 return(MagickFalse);
1761 primitive_extent=(double) strlen(primitive);
1762 (void) SetImageArtifact(image,"MVG",primitive);
1765 stops=(StopInfo *) NULL;
1767 Allocate primitive info memory.
1769 graphic_context=(DrawInfo **) AcquireMagickMemory(
1770 sizeof(*graphic_context));
1771 if (graphic_context == (DrawInfo **) NULL)
1773 primitive=DestroyString(primitive);
1774 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1778 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1779 sizeof(*primitive_info));
1780 if (primitive_info == (PrimitiveInfo *) NULL)
1782 primitive=DestroyString(primitive);
1783 for ( ; n >= 0; n--)
1784 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1785 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1786 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1789 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1790 graphic_context[n]->viewbox=image->page;
1791 if ((image->page.width == 0) || (image->page.height == 0))
1793 graphic_context[n]->viewbox.width=image->columns;
1794 graphic_context[n]->viewbox.height=image->rows;
1796 token=AcquireString(primitive);
1797 extent=strlen(token)+MagickPathExtent;
1798 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1799 return(MagickFalse);
1801 for (q=primitive; *q != '\0'; )
1804 Interpret graphic primitive.
1806 GetNextToken(q,&q,MagickPathExtent,keyword);
1807 if (*keyword == '\0')
1809 if (*keyword == '#')
1814 while ((*q != '\n') && (*q != '\0'))
1818 p=q-strlen(keyword)-1;
1819 primitive_type=UndefinedPrimitive;
1820 current=graphic_context[n]->affine;
1821 GetAffineMatrix(&affine);
1829 if (LocaleCompare("affine",keyword) == 0)
1831 GetNextToken(q,&q,extent,token);
1832 affine.sx=StringToDouble(token,&next_token);
1833 if (token == next_token)
1835 GetNextToken(q,&q,extent,token);
1837 GetNextToken(q,&q,extent,token);
1838 affine.rx=StringToDouble(token,&next_token);
1839 if (token == next_token)
1841 GetNextToken(q,&q,extent,token);
1843 GetNextToken(q,&q,extent,token);
1844 affine.ry=StringToDouble(token,&next_token);
1845 if (token == next_token)
1847 GetNextToken(q,&q,extent,token);
1849 GetNextToken(q,&q,extent,token);
1850 affine.sy=StringToDouble(token,&next_token);
1851 if (token == next_token)
1853 GetNextToken(q,&q,extent,token);
1855 GetNextToken(q,&q,extent,token);
1856 affine.tx=StringToDouble(token,&next_token);
1857 if (token == next_token)
1859 GetNextToken(q,&q,extent,token);
1861 GetNextToken(q,&q,extent,token);
1862 affine.ty=StringToDouble(token,&next_token);
1863 if (token == next_token)
1867 if (LocaleCompare("alpha",keyword) == 0)
1869 primitive_type=AlphaPrimitive;
1872 if (LocaleCompare("arc",keyword) == 0)
1874 primitive_type=ArcPrimitive;
1883 if (LocaleCompare("bezier",keyword) == 0)
1885 primitive_type=BezierPrimitive;
1888 if (LocaleCompare("border-color",keyword) == 0)
1890 GetNextToken(q,&q,extent,token);
1891 (void) QueryColorCompliance(token,AllCompliance,
1892 &graphic_context[n]->border_color,exception);
1901 if (LocaleCompare("clip-path",keyword) == 0)
1906 GetNextToken(q,&q,extent,token);
1907 (void) CloneString(&graphic_context[n]->clip_mask,token);
1908 (void) DrawClipPath(image,graphic_context[n],
1909 graphic_context[n]->clip_mask,exception);
1912 if (LocaleCompare("clip-rule",keyword) == 0)
1917 GetNextToken(q,&q,extent,token);
1918 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1920 if (fill_rule == -1)
1923 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1926 if (LocaleCompare("clip-units",keyword) == 0)
1931 GetNextToken(q,&q,extent,token);
1932 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1934 if (clip_units == -1)
1939 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1940 if (clip_units == ObjectBoundingBox)
1942 GetAffineMatrix(¤t);
1943 affine.sx=draw_info->bounds.x2;
1944 affine.sy=draw_info->bounds.y2;
1945 affine.tx=draw_info->bounds.x1;
1946 affine.ty=draw_info->bounds.y1;
1951 if (LocaleCompare("circle",keyword) == 0)
1953 primitive_type=CirclePrimitive;
1956 if (LocaleCompare("color",keyword) == 0)
1958 primitive_type=ColorPrimitive;
1967 if (LocaleCompare("decorate",keyword) == 0)
1972 GetNextToken(q,&q,extent,token);
1973 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1978 graphic_context[n]->decorate=(DecorationType) decorate;
1981 if (LocaleCompare("density",keyword) == 0)
1983 GetNextToken(q,&q,extent,token);
1984 (void) CloneString(&graphic_context[n]->density,token);
1987 if (LocaleCompare("direction",keyword) == 0)
1992 GetNextToken(q,&q,extent,token);
1993 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1995 if (direction == -1)
1998 graphic_context[n]->direction=(DirectionType) direction;
2007 if (LocaleCompare("ellipse",keyword) == 0)
2009 primitive_type=EllipsePrimitive;
2012 if (LocaleCompare("encoding",keyword) == 0)
2014 GetNextToken(q,&q,extent,token);
2015 (void) CloneString(&graphic_context[n]->encoding,token);
2024 if (LocaleCompare("fill",keyword) == 0)
2026 GetNextToken(q,&q,extent,token);
2027 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2028 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2029 (void) DrawPatternPath(image,draw_info,token,
2030 &graphic_context[n]->fill_pattern,exception);
2036 alpha=graphic_context[n]->fill.alpha;
2037 status&=QueryColorCompliance(token,AllCompliance,
2038 &graphic_context[n]->fill,exception);
2039 graphic_context[n]->fill.alpha=alpha;
2040 if (status == MagickFalse)
2045 pattern_info=AcquireImageInfo();
2046 (void) CopyMagickString(pattern_info->filename,token,
2048 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2050 CatchException(exception);
2051 pattern_info=DestroyImageInfo(pattern_info);
2056 if (LocaleCompare("fill-opacity",keyword) == 0)
2058 GetNextToken(q,&q,extent,token);
2059 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2060 graphic_context[n]->fill.alpha=(double) QuantumRange*
2061 factor*StringToDouble(token,&next_token);
2062 if (token == next_token)
2066 if (LocaleCompare("fill-rule",keyword) == 0)
2071 GetNextToken(q,&q,extent,token);
2072 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2074 if (fill_rule == -1)
2077 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2080 if (LocaleCompare("font",keyword) == 0)
2082 GetNextToken(q,&q,extent,token);
2083 (void) CloneString(&graphic_context[n]->font,token);
2084 if (LocaleCompare("none",token) == 0)
2085 graphic_context[n]->font=(char *) RelinquishMagickMemory(
2086 graphic_context[n]->font);
2089 if (LocaleCompare("font-family",keyword) == 0)
2091 GetNextToken(q,&q,extent,token);
2092 (void) CloneString(&graphic_context[n]->family,token);
2095 if (LocaleCompare("font-size",keyword) == 0)
2097 GetNextToken(q,&q,extent,token);
2098 graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2099 if (token == next_token)
2103 if (LocaleCompare("font-stretch",keyword) == 0)
2108 GetNextToken(q,&q,extent,token);
2109 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2113 graphic_context[n]->stretch=(StretchType) stretch;
2116 if (LocaleCompare("font-style",keyword) == 0)
2121 GetNextToken(q,&q,extent,token);
2122 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2126 graphic_context[n]->style=(StyleType) style;
2129 if (LocaleCompare("font-weight",keyword) == 0)
2134 GetNextToken(q,&q,extent,token);
2135 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
2137 weight=(ssize_t) StringToUnsignedLong(token);
2138 graphic_context[n]->weight=(size_t) weight;
2147 if (LocaleCompare("gradient-units",keyword) == 0)
2149 GetNextToken(q,&q,extent,token);
2152 if (LocaleCompare("gravity",keyword) == 0)
2157 GetNextToken(q,&q,extent,token);
2158 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2162 graphic_context[n]->gravity=(GravityType) gravity;
2171 if (LocaleCompare("image",keyword) == 0)
2176 primitive_type=ImagePrimitive;
2177 GetNextToken(q,&q,extent,token);
2178 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2182 graphic_context[n]->compose=(CompositeOperator) compose;
2185 if (LocaleCompare("interline-spacing",keyword) == 0)
2187 GetNextToken(q,&q,extent,token);
2188 graphic_context[n]->interline_spacing=StringToDouble(token,
2190 if (token == next_token)
2194 if (LocaleCompare("interword-spacing",keyword) == 0)
2196 GetNextToken(q,&q,extent,token);
2197 graphic_context[n]->interword_spacing=StringToDouble(token,
2199 if (token == next_token)
2209 if (LocaleCompare("kerning",keyword) == 0)
2211 GetNextToken(q,&q,extent,token);
2212 graphic_context[n]->kerning=StringToDouble(token,&next_token);
2213 if (token == next_token)
2223 if (LocaleCompare("line",keyword) == 0)
2224 primitive_type=LinePrimitive;
2232 if (LocaleCompare("offset",keyword) == 0)
2234 GetNextToken(q,&q,extent,token);
2237 if (LocaleCompare("opacity",keyword) == 0)
2239 GetNextToken(q,&q,extent,token);
2240 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2241 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-(
2242 QuantumScale*graphic_context[n]->alpha*(1.0-factor*
2243 StringToDouble(token,&next_token)))));
2244 if (token == next_token)
2246 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2247 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2256 if (LocaleCompare("path",keyword) == 0)
2258 primitive_type=PathPrimitive;
2261 if (LocaleCompare("point",keyword) == 0)
2263 primitive_type=PointPrimitive;
2266 if (LocaleCompare("polyline",keyword) == 0)
2268 primitive_type=PolylinePrimitive;
2271 if (LocaleCompare("polygon",keyword) == 0)
2273 primitive_type=PolygonPrimitive;
2276 if (LocaleCompare("pop",keyword) == 0)
2278 GetNextToken(q,&q,extent,token);
2279 if (LocaleCompare("clip-path",token) == 0)
2281 if (LocaleCompare("defs",token) == 0)
2283 if (LocaleCompare("gradient",token) == 0)
2285 if (LocaleCompare("graphic-context",token) == 0)
2289 (void) ThrowMagickException(exception,GetMagickModule(),
2290 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2295 if (graphic_context[n]->clip_mask != (char *) NULL)
2296 if (LocaleCompare(graphic_context[n]->clip_mask,
2297 graphic_context[n-1]->clip_mask) != 0)
2298 (void) SetImageMask(image,ReadPixelMask,(Image *) NULL,
2300 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2304 if (LocaleCompare("pattern",token) == 0)
2309 if (LocaleCompare("push",keyword) == 0)
2311 GetNextToken(q,&q,extent,token);
2312 if (LocaleCompare("clip-path",token) == 0)
2315 name[MagickPathExtent];
2317 GetNextToken(q,&q,extent,token);
2318 (void) FormatLocaleString(name,MagickPathExtent,"%s",token);
2319 for (p=q; *q != '\0'; )
2321 GetNextToken(q,&q,extent,token);
2322 if (LocaleCompare(token,"pop") != 0)
2324 GetNextToken(q,(const char **) NULL,extent,token);
2325 if (LocaleCompare(token,"clip-path") != 0)
2329 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2330 (void) SetImageArtifact(image,name,token);
2331 GetNextToken(q,&q,extent,token);
2334 if (LocaleCompare("gradient",token) == 0)
2337 key[2*MagickPathExtent],
2338 name[MagickPathExtent],
2339 type[MagickPathExtent];
2344 GetNextToken(q,&q,extent,token);
2345 (void) CopyMagickString(name,token,MagickPathExtent);
2346 GetNextToken(q,&q,extent,token);
2347 (void) CopyMagickString(type,token,MagickPathExtent);
2348 GetNextToken(q,&q,extent,token);
2349 segment.x1=StringToDouble(token,&next_token);
2350 if (token == next_token)
2352 GetNextToken(q,&q,extent,token);
2354 GetNextToken(q,&q,extent,token);
2355 segment.y1=StringToDouble(token,&next_token);
2356 if (token == next_token)
2358 GetNextToken(q,&q,extent,token);
2360 GetNextToken(q,&q,extent,token);
2361 segment.x2=StringToDouble(token,&next_token);
2362 if (token == next_token)
2364 GetNextToken(q,&q,extent,token);
2366 GetNextToken(q,&q,extent,token);
2367 segment.y2=StringToDouble(token,&next_token);
2368 if (token == next_token)
2370 if (LocaleCompare(type,"radial") == 0)
2372 GetNextToken(q,&q,extent,token);
2374 GetNextToken(q,&q,extent,token);
2376 for (p=q; *q != '\0'; )
2378 GetNextToken(q,&q,extent,token);
2379 if (LocaleCompare(token,"pop") != 0)
2381 GetNextToken(q,(const char **) NULL,extent,token);
2382 if (LocaleCompare(token,"gradient") != 0)
2386 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2387 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2388 graphic_context[n]->affine.ry*segment.y1+
2389 graphic_context[n]->affine.tx;
2390 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2391 graphic_context[n]->affine.sy*segment.y1+
2392 graphic_context[n]->affine.ty;
2393 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2394 graphic_context[n]->affine.ry*segment.y2+
2395 graphic_context[n]->affine.tx;
2396 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2397 graphic_context[n]->affine.sy*segment.y2+
2398 graphic_context[n]->affine.ty;
2399 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2400 (void) SetImageArtifact(image,key,token);
2401 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
2402 (void) SetImageArtifact(image,key,type);
2403 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2405 (void) FormatLocaleString(geometry,MagickPathExtent,
2406 "%gx%g%+.15g%+.15g",
2407 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2408 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2409 bounds.x1,bounds.y1);
2410 (void) SetImageArtifact(image,key,geometry);
2411 GetNextToken(q,&q,extent,token);
2414 if (LocaleCompare("pattern",token) == 0)
2417 key[2*MagickPathExtent],
2418 name[MagickPathExtent];
2423 GetNextToken(q,&q,extent,token);
2424 (void) CopyMagickString(name,token,MagickPathExtent);
2425 GetNextToken(q,&q,extent,token);
2426 pattern_bounds.x=(ssize_t) ceil(StringToDouble(token,
2428 if (token == next_token)
2430 GetNextToken(q,&q,extent,token);
2432 GetNextToken(q,&q,extent,token);
2433 pattern_bounds.y=(ssize_t) ceil(StringToDouble(token,
2435 if (token == next_token)
2437 GetNextToken(q,&q,extent,token);
2439 GetNextToken(q,&q,extent,token);
2440 pattern_bounds.width=(size_t) floor(StringToDouble(token,
2442 if (token == next_token)
2444 GetNextToken(q,&q,extent,token);
2446 GetNextToken(q,&q,extent,token);
2447 pattern_bounds.height=(size_t) floor(StringToDouble(token,
2449 if (token == next_token)
2451 for (p=q; *q != '\0'; )
2453 GetNextToken(q,&q,extent,token);
2454 if (LocaleCompare(token,"pop") != 0)
2456 GetNextToken(q,(const char **) NULL,extent,token);
2457 if (LocaleCompare(token,"pattern") != 0)
2461 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2462 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2463 (void) SetImageArtifact(image,key,token);
2464 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2466 (void) FormatLocaleString(geometry,MagickPathExtent,
2467 "%.20gx%.20g%+.20g%+.20g",(double)pattern_bounds.width,
2468 (double)pattern_bounds.height,(double)pattern_bounds.x,
2469 (double)pattern_bounds.y);
2470 (void) SetImageArtifact(image,key,geometry);
2471 GetNextToken(q,&q,extent,token);
2474 if (LocaleCompare("graphic-context",token) == 0)
2477 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2478 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2479 if (graphic_context == (DrawInfo **) NULL)
2481 (void) ThrowMagickException(exception,GetMagickModule(),
2482 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2486 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2487 graphic_context[n-1]);
2490 if (LocaleCompare("defs",token) == 0)
2501 if (LocaleCompare("rectangle",keyword) == 0)
2503 primitive_type=RectanglePrimitive;
2506 if (LocaleCompare("rotate",keyword) == 0)
2508 GetNextToken(q,&q,extent,token);
2509 angle=StringToDouble(token,&next_token);
2510 if (token == next_token)
2512 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2513 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2514 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2515 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2518 if (LocaleCompare("roundRectangle",keyword) == 0)
2520 primitive_type=RoundRectanglePrimitive;
2529 if (LocaleCompare("scale",keyword) == 0)
2531 GetNextToken(q,&q,extent,token);
2532 affine.sx=StringToDouble(token,&next_token);
2533 if (token == next_token)
2535 GetNextToken(q,&q,extent,token);
2537 GetNextToken(q,&q,extent,token);
2538 affine.sy=StringToDouble(token,&next_token);
2539 if (token == next_token)
2543 if (LocaleCompare("skewX",keyword) == 0)
2545 GetNextToken(q,&q,extent,token);
2546 angle=StringToDouble(token,&next_token);
2547 if (token == next_token)
2549 affine.ry=sin(DegreesToRadians(angle));
2552 if (LocaleCompare("skewY",keyword) == 0)
2554 GetNextToken(q,&q,extent,token);
2555 angle=StringToDouble(token,&next_token);
2556 if (token == next_token)
2558 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2561 if (LocaleCompare("stop-color",keyword) == 0)
2567 if (number_stops == 1)
2568 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
2569 else if (number_stops > 2)
2570 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
2572 if (stops == (StopInfo *) NULL)
2574 (void) ThrowMagickException(exception,GetMagickModule(),
2575 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2579 GetNextToken(q,&q,extent,token);
2580 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2582 stops[number_stops-1].color=stop_color;
2583 GetNextToken(q,&q,extent,token);
2584 stops[number_stops-1].offset=StringToDouble(token,&next_token);
2585 if (token == next_token)
2589 if (LocaleCompare("stroke",keyword) == 0)
2591 GetNextToken(q,&q,extent,token);
2592 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2593 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2594 (void) DrawPatternPath(image,draw_info,token,
2595 &graphic_context[n]->stroke_pattern,exception);
2601 alpha=graphic_context[n]->stroke.alpha;
2602 status&=QueryColorCompliance(token,AllCompliance,
2603 &graphic_context[n]->stroke,exception);
2604 graphic_context[n]->stroke.alpha=alpha;
2605 if (status == MagickFalse)
2610 pattern_info=AcquireImageInfo();
2611 (void) CopyMagickString(pattern_info->filename,token,
2613 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2615 CatchException(exception);
2616 pattern_info=DestroyImageInfo(pattern_info);
2621 if (LocaleCompare("stroke-antialias",keyword) == 0)
2623 GetNextToken(q,&q,extent,token);
2624 graphic_context[n]->stroke_antialias=
2625 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2628 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2630 if (graphic_context[n]->dash_pattern != (double *) NULL)
2631 graphic_context[n]->dash_pattern=(double *)
2632 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2633 if (IsPoint(q) != MagickFalse)
2639 GetNextToken(r,&r,extent,token);
2641 GetNextToken(r,&r,extent,token);
2642 for (x=0; IsPoint(token) != MagickFalse; x++)
2644 GetNextToken(r,&r,extent,token);
2646 GetNextToken(r,&r,extent,token);
2648 graphic_context[n]->dash_pattern=(double *)
2649 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2650 sizeof(*graphic_context[n]->dash_pattern));
2651 if (graphic_context[n]->dash_pattern == (double *) NULL)
2653 (void) ThrowMagickException(exception,GetMagickModule(),
2654 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2659 for (j=0; j < x; j++)
2661 GetNextToken(q,&q,extent,token);
2663 GetNextToken(q,&q,extent,token);
2664 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2666 if (token == next_token)
2668 if (graphic_context[n]->dash_pattern[j] < 0.0)
2671 if ((x & 0x01) != 0)
2672 for ( ; j < (2*x); j++)
2673 graphic_context[n]->dash_pattern[j]=
2674 graphic_context[n]->dash_pattern[j-x];
2675 graphic_context[n]->dash_pattern[j]=0.0;
2678 GetNextToken(q,&q,extent,token);
2681 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2683 GetNextToken(q,&q,extent,token);
2684 graphic_context[n]->dash_offset=StringToDouble(token,
2686 if (token == next_token)
2690 if (LocaleCompare("stroke-linecap",keyword) == 0)
2695 GetNextToken(q,&q,extent,token);
2696 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2700 graphic_context[n]->linecap=(LineCap) linecap;
2703 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2708 GetNextToken(q,&q,extent,token);
2709 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2714 graphic_context[n]->linejoin=(LineJoin) linejoin;
2717 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2719 GetNextToken(q,&q,extent,token);
2720 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2723 if (LocaleCompare("stroke-opacity",keyword) == 0)
2725 GetNextToken(q,&q,extent,token);
2726 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2727 graphic_context[n]->stroke.alpha=(double) QuantumRange*(1.0-factor*
2728 StringToDouble(token,&next_token));
2729 if (token == next_token)
2733 if (LocaleCompare("stroke-width",keyword) == 0)
2735 GetNextToken(q,&q,extent,token);
2736 graphic_context[n]->stroke_width=StringToDouble(token,&next_token);
2737 if (token == next_token)
2747 if (LocaleCompare("text",keyword) == 0)
2749 primitive_type=TextPrimitive;
2752 if (LocaleCompare("text-align",keyword) == 0)
2757 GetNextToken(q,&q,extent,token);
2758 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2762 graphic_context[n]->align=(AlignType) align;
2765 if (LocaleCompare("text-anchor",keyword) == 0)
2770 GetNextToken(q,&q,extent,token);
2771 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2775 graphic_context[n]->align=(AlignType) align;
2778 if (LocaleCompare("text-antialias",keyword) == 0)
2780 GetNextToken(q,&q,extent,token);
2781 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
2782 MagickTrue : MagickFalse;
2785 if (LocaleCompare("text-undercolor",keyword) == 0)
2787 GetNextToken(q,&q,extent,token);
2788 (void) QueryColorCompliance(token,AllCompliance,
2789 &graphic_context[n]->undercolor,exception);
2792 if (LocaleCompare("translate",keyword) == 0)
2794 GetNextToken(q,&q,extent,token);
2795 affine.tx=StringToDouble(token,&next_token);
2796 if (token == next_token)
2798 GetNextToken(q,&q,extent,token);
2800 GetNextToken(q,&q,extent,token);
2801 affine.ty=StringToDouble(token,&next_token);
2802 if (token == next_token)
2812 if (LocaleCompare("viewbox",keyword) == 0)
2814 GetNextToken(q,&q,extent,token);
2815 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2817 if (token == next_token)
2819 GetNextToken(q,&q,extent,token);
2821 GetNextToken(q,&q,extent,token);
2822 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2824 if (token == next_token)
2826 GetNextToken(q,&q,extent,token);
2828 GetNextToken(q,&q,extent,token);
2829 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2830 token,&next_token)+0.5);
2831 if (token == next_token)
2833 GetNextToken(q,&q,extent,token);
2835 GetNextToken(q,&q,extent,token);
2836 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2837 token,&next_token)+0.5);
2838 if (token == next_token)
2851 if (status == MagickFalse)
2853 if ((fabs(affine.sx-1.0) >= DrawEpsilon) ||
2854 (fabs(affine.rx) >= DrawEpsilon) ||
2855 (fabs(affine.ry) >= DrawEpsilon) ||
2856 (fabs(affine.sy-1.0) >= DrawEpsilon) ||
2857 (fabs(affine.tx) >= DrawEpsilon) ||
2858 (fabs(affine.ty) >= DrawEpsilon))
2860 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2861 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2862 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2863 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2864 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2866 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2869 if (primitive_type == UndefinedPrimitive)
2873 if (number_stops > 1)
2878 type=LinearGradient;
2879 if (draw_info->gradient.type == RadialGradient)
2880 type=RadialGradient;
2881 (void) GradientImage(image,type,PadSpread,stops,number_stops,
2884 if (number_stops > 0)
2885 stops=(StopInfo *) RelinquishMagickMemory(stops);
2887 if (image->debug != MagickFalse)
2888 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
2893 Parse the primitive attributes.
2897 primitive_info[0].point.x=0.0;
2898 primitive_info[0].point.y=0.0;
2899 for (x=0; *q != '\0'; x++)
2904 if (IsPoint(q) == MagickFalse)
2906 GetNextToken(q,&q,extent,token);
2907 point.x=StringToDouble(token,&next_token);
2908 if (token == next_token)
2910 GetNextToken(q,&q,extent,token);
2912 GetNextToken(q,&q,extent,token);
2913 point.y=StringToDouble(token,&next_token);
2914 if (token == next_token)
2916 GetNextToken(q,(const char **) NULL,extent,token);
2918 GetNextToken(q,&q,extent,token);
2919 primitive_info[i].primitive=primitive_type;
2920 primitive_info[i].point=point;
2921 primitive_info[i].coordinates=0;
2922 primitive_info[i].method=FloodfillMethod;
2924 if (i < (ssize_t) number_points)
2927 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2928 (size_t) number_points,sizeof(*primitive_info));
2929 if ((primitive_info == (PrimitiveInfo *) NULL) ||
2930 (number_points != (MagickSizeType) ((size_t) number_points)))
2932 (void) ThrowMagickException(exception,GetMagickModule(),
2933 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2937 primitive_info[j].primitive=primitive_type;
2938 primitive_info[j].coordinates=(size_t) x;
2939 primitive_info[j].method=FloodfillMethod;
2940 primitive_info[j].text=(char *) NULL;
2942 Circumscribe primitive within a circle.
2944 bounds.x1=primitive_info[j].point.x;
2945 bounds.y1=primitive_info[j].point.y;
2946 bounds.x2=primitive_info[j].point.x;
2947 bounds.y2=primitive_info[j].point.y;
2948 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2950 point=primitive_info[j+k].point;
2951 if (point.x < bounds.x1)
2953 if (point.y < bounds.y1)
2955 if (point.x > bounds.x2)
2957 if (point.y > bounds.y2)
2961 Speculate how many points our primitive might consume.
2963 length=primitive_info[j].coordinates;
2964 switch (primitive_type)
2966 case RectanglePrimitive:
2971 case RoundRectanglePrimitive:
2978 alpha=bounds.x2-bounds.x1;
2979 beta=bounds.y2-bounds.y1;
2980 radius=hypot((double) alpha,(double) beta);
2982 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2985 case BezierPrimitive:
2987 if (primitive_info[j].coordinates > 107)
2988 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2989 "TooManyBezierCoordinates","`%s'",token);
2990 length=BezierQuantum*primitive_info[j].coordinates;
2999 GetNextToken(q,&q,extent,token);
3002 for (s=token; *s != '\0'; s=t)
3007 value=StringToDouble(s,&t);
3016 length=length*BezierQuantum/2;
3019 case CirclePrimitive:
3021 case EllipsePrimitive:
3028 alpha=bounds.x2-bounds.x1;
3029 beta=bounds.y2-bounds.y1;
3030 radius=hypot((double) alpha,(double) beta);
3031 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
3037 if ((i+length) >= number_points)
3040 Resize based on speculative points required by primitive.
3042 number_points+=length+1;
3043 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
3044 (size_t) number_points,sizeof(*primitive_info));
3045 if ((primitive_info == (PrimitiveInfo *) NULL) ||
3046 (number_points != (MagickSizeType) ((size_t) number_points)))
3048 (void) ThrowMagickException(exception,GetMagickModule(),
3049 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3054 switch (primitive_type)
3056 case PointPrimitive:
3059 if (primitive_info[j].coordinates != 1)
3064 TracePoint(primitive_info+j,primitive_info[j].point);
3065 i=(ssize_t) (j+primitive_info[j].coordinates);
3070 if (primitive_info[j].coordinates != 2)
3075 TraceLine(primitive_info+j,primitive_info[j].point,
3076 primitive_info[j+1].point);
3077 i=(ssize_t) (j+primitive_info[j].coordinates);
3080 case RectanglePrimitive:
3082 if (primitive_info[j].coordinates != 2)
3087 TraceRectangle(primitive_info+j,primitive_info[j].point,
3088 primitive_info[j+1].point);
3089 i=(ssize_t) (j+primitive_info[j].coordinates);
3092 case RoundRectanglePrimitive:
3094 if (primitive_info[j].coordinates != 3)
3099 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
3100 primitive_info[j+1].point,primitive_info[j+2].point);
3101 i=(ssize_t) (j+primitive_info[j].coordinates);
3106 if (primitive_info[j].coordinates != 3)
3108 primitive_type=UndefinedPrimitive;
3111 TraceArc(primitive_info+j,primitive_info[j].point,
3112 primitive_info[j+1].point,primitive_info[j+2].point);
3113 i=(ssize_t) (j+primitive_info[j].coordinates);
3116 case EllipsePrimitive:
3118 if (primitive_info[j].coordinates != 3)
3123 TraceEllipse(primitive_info+j,primitive_info[j].point,
3124 primitive_info[j+1].point,primitive_info[j+2].point);
3125 i=(ssize_t) (j+primitive_info[j].coordinates);
3128 case CirclePrimitive:
3130 if (primitive_info[j].coordinates != 2)
3135 TraceCircle(primitive_info+j,primitive_info[j].point,
3136 primitive_info[j+1].point);
3137 i=(ssize_t) (j+primitive_info[j].coordinates);
3140 case PolylinePrimitive:
3142 case PolygonPrimitive:
3144 primitive_info[i]=primitive_info[j];
3145 primitive_info[i].coordinates=0;
3146 primitive_info[j].coordinates++;
3150 case BezierPrimitive:
3152 if (primitive_info[j].coordinates < 3)
3157 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3158 i=(ssize_t) (j+primitive_info[j].coordinates);
3163 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3166 case AlphaPrimitive:
3167 case ColorPrimitive:
3172 if (primitive_info[j].coordinates != 1)
3177 GetNextToken(q,&q,extent,token);
3178 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3182 primitive_info[j].method=(PaintMethod) method;
3187 if (primitive_info[j].coordinates != 1)
3193 GetNextToken(q,&q,extent,token);
3194 primitive_info[j].text=AcquireString(token);
3197 case ImagePrimitive:
3199 if (primitive_info[j].coordinates != 2)
3204 GetNextToken(q,&q,extent,token);
3205 primitive_info[j].text=AcquireString(token);
3209 if (primitive_info == (PrimitiveInfo *) NULL)
3211 if (image->debug != MagickFalse)
3212 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3213 if (status == MagickFalse)
3215 primitive_info[i].primitive=UndefinedPrimitive;
3221 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3223 point=primitive_info[i].point;
3224 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3225 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3226 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3227 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3228 point=primitive_info[i].point;
3229 if (point.x < graphic_context[n]->bounds.x1)
3230 graphic_context[n]->bounds.x1=point.x;
3231 if (point.y < graphic_context[n]->bounds.y1)
3232 graphic_context[n]->bounds.y1=point.y;
3233 if (point.x > graphic_context[n]->bounds.x2)
3234 graphic_context[n]->bounds.x2=point.x;
3235 if (point.y > graphic_context[n]->bounds.y2)
3236 graphic_context[n]->bounds.y2=point.y;
3237 if (primitive_info[i].primitive == ImagePrimitive)
3239 if (i >= (ssize_t) number_points)
3240 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3242 if (graphic_context[n]->render != MagickFalse)
3244 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3245 (LocaleCompare(graphic_context[n]->clip_mask,
3246 graphic_context[n-1]->clip_mask) != 0))
3247 status&=DrawClipPath(image,graphic_context[n],
3248 graphic_context[n]->clip_mask,exception);
3249 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3252 if (primitive_info->text != (char *) NULL)
3253 primitive_info->text=(char *) RelinquishMagickMemory(
3254 primitive_info->text);
3255 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3257 if (proceed == MagickFalse)
3262 if (image->debug != MagickFalse)
3263 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3265 Relinquish resources.
3267 token=DestroyString(token);
3268 if (primitive_info != (PrimitiveInfo *) NULL)
3269 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3270 primitive=DestroyString(primitive);
3271 for ( ; n >= 0; n--)
3272 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3273 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3274 if (status == MagickFalse)
3275 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3277 return(status != 0 ? MagickTrue : MagickFalse);
3281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3285 % D r a w G r a d i e n t I m a g e %
3289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3291 % DrawGradientImage() draws a linear gradient on the image.
3293 % The format of the DrawGradientImage method is:
3295 % MagickBooleanType DrawGradientImage(Image *image,
3296 % const DrawInfo *draw_info,ExceptionInfo *exception)
3298 % A description of each parameter follows:
3300 % o image: the image.
3302 % o draw_info: the draw info.
3304 % o exception: return any errors or warnings in this structure.
3308 static inline double GetStopColorOffset(const GradientInfo *gradient,
3309 const ssize_t x,const ssize_t y)
3311 switch (gradient->type)
3313 case UndefinedGradient:
3314 case LinearGradient:
3329 gradient_vector=(&gradient->gradient_vector);
3330 p.x=gradient_vector->x2-gradient_vector->x1;
3331 p.y=gradient_vector->y2-gradient_vector->y1;
3332 q.x=(double) x-gradient_vector->x1;
3333 q.y=(double) y-gradient_vector->y1;
3334 length=sqrt(q.x*q.x+q.y*q.y);
3335 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3336 gamma=PerceptibleReciprocal(gamma);
3337 scale=p.x*q.x+p.y*q.y;
3338 offset=gamma*scale*length;
3341 case RadialGradient:
3346 if (gradient->spread == RepeatSpread)
3348 v.x=(double) x-gradient->center.x;
3349 v.y=(double) y-gradient->center.y;
3350 return(sqrt(v.x*v.x+v.y*v.y));
3352 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3353 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3354 gradient->angle))))/gradient->radii.x;
3355 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3356 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3357 gradient->angle))))/gradient->radii.y;
3358 return(sqrt(v.x*v.x+v.y*v.y));
3364 static int StopInfoCompare(const void *x,const void *y)
3370 stop_1=(StopInfo *) x;
3371 stop_2=(StopInfo *) y;
3372 if (stop_1->offset > stop_2->offset)
3374 if (fabs(stop_1->offset-stop_2->offset) <= DrawEpsilon)
3379 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3380 const DrawInfo *draw_info,ExceptionInfo *exception)
3410 Draw linear or radial gradient on image.
3412 assert(image != (Image *) NULL);
3413 assert(image->signature == MagickCoreSignature);
3414 if (image->debug != MagickFalse)
3415 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3416 assert(draw_info != (const DrawInfo *) NULL);
3417 gradient=(&draw_info->gradient);
3418 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
3420 gradient_vector=(&gradient->gradient_vector);
3421 point.x=gradient_vector->x2-gradient_vector->x1;
3422 point.y=gradient_vector->y2-gradient_vector->y1;
3423 length=sqrt(point.x*point.x+point.y*point.y);
3424 bounding_box=gradient->bounding_box;
3426 GetPixelInfo(image,&zero);
3427 image_view=AcquireAuthenticCacheView(image,exception);
3428 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3429 #pragma omp parallel for schedule(static,4) shared(status) \
3430 magick_threads(image,image,1,1)
3432 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3452 if (status == MagickFalse)
3454 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3455 if (q == (Quantum *) NULL)
3462 offset=GetStopColorOffset(gradient,0,y);
3463 if (gradient->type != RadialGradient)
3465 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3467 GetPixelInfoPixel(image,q,&pixel);
3468 switch (gradient->spread)
3470 case UndefinedSpread:
3473 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3474 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3476 offset=GetStopColorOffset(gradient,x,y);
3477 if (gradient->type != RadialGradient)
3480 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3481 if (offset < gradient->stops[i].offset)
3483 if ((offset < 0.0) || (i == 0))
3484 composite=gradient->stops[0].color;
3486 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3487 composite=gradient->stops[gradient->number_stops-1].color;
3492 alpha=(offset-gradient->stops[i].offset)/
3493 (gradient->stops[j].offset-gradient->stops[i].offset);
3494 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3495 &gradient->stops[j].color,alpha,&composite);
3501 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3502 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3504 offset=GetStopColorOffset(gradient,x,y);
3505 if (gradient->type != RadialGradient)
3510 if ((ssize_t) fmod(offset,2.0) == 0)
3511 offset=fmod(offset,1.0);
3513 offset=1.0-fmod(offset,1.0);
3514 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3515 if (offset < gradient->stops[i].offset)
3518 composite=gradient->stops[0].color;
3520 if (i == (ssize_t) gradient->number_stops)
3521 composite=gradient->stops[gradient->number_stops-1].color;
3526 alpha=(offset-gradient->stops[i].offset)/
3527 (gradient->stops[j].offset-gradient->stops[i].offset);
3528 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3529 &gradient->stops[j].color,alpha,&composite);
3541 antialias=MagickFalse;
3543 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3544 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3546 offset=GetStopColorOffset(gradient,x,y);
3547 if (gradient->type == LinearGradient)
3549 repeat=fmod(offset,length);
3551 repeat=length-fmod(-repeat,length);
3553 repeat=fmod(offset,length);
3554 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3555 MagickTrue : MagickFalse;
3556 offset=repeat/length;
3560 repeat=fmod(offset,gradient->radius);
3562 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3564 repeat=fmod(offset,gradient->radius);
3565 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3567 offset=repeat/gradient->radius;
3570 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3571 if (offset < gradient->stops[i].offset)
3574 composite=gradient->stops[0].color;
3576 if (i == (ssize_t) gradient->number_stops)
3577 composite=gradient->stops[gradient->number_stops-1].color;
3582 alpha=(offset-gradient->stops[i].offset)/
3583 (gradient->stops[j].offset-gradient->stops[i].offset);
3584 if (antialias != MagickFalse)
3586 if (gradient->type == LinearGradient)
3587 alpha=length-repeat;
3589 alpha=gradient->radius-repeat;
3591 j=(ssize_t) gradient->number_stops-1L;
3593 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3594 &gradient->stops[j].color,alpha,&composite);
3599 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3601 SetPixelViaPixelInfo(image,&pixel,q);
3602 q+=GetPixelChannels(image);
3604 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3607 image_view=DestroyCacheView(image_view);
3612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3616 % D r a w P a t t e r n P a t h %
3620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3622 % DrawPatternPath() draws a pattern.
3624 % The format of the DrawPatternPath method is:
3626 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3627 % const char *name,Image **pattern,ExceptionInfo *exception)
3629 % A description of each parameter follows:
3631 % o image: the image.
3633 % o draw_info: the draw info.
3635 % o name: the pattern name.
3637 % o image: the image.
3639 % o exception: return any errors or warnings in this structure.
3642 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3643 const DrawInfo *draw_info,const char *name,Image **pattern,
3644 ExceptionInfo *exception)
3647 property[MagickPathExtent];
3663 assert(image != (Image *) NULL);
3664 assert(image->signature == MagickCoreSignature);
3665 if (image->debug != MagickFalse)
3666 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3667 assert(draw_info != (const DrawInfo *) NULL);
3668 assert(name != (const char *) NULL);
3669 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
3670 path=GetImageArtifact(image,property);
3671 if (path == (const char *) NULL)
3672 return(MagickFalse);
3673 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
3674 geometry=GetImageArtifact(image,property);
3675 if (geometry == (const char *) NULL)
3676 return(MagickFalse);
3677 if ((*pattern) != (Image *) NULL)
3678 *pattern=DestroyImage(*pattern);
3679 image_info=AcquireImageInfo();
3680 image_info->size=AcquireString(geometry);
3681 *pattern=AcquireImage(image_info,exception);
3682 image_info=DestroyImageInfo(image_info);
3683 (void) QueryColorCompliance("#000000ff",AllCompliance,
3684 &(*pattern)->background_color,exception);
3685 (void) SetImageBackgroundColor(*pattern,exception);
3686 if (image->debug != MagickFalse)
3687 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3688 "begin pattern-path %s %s",name,geometry);
3689 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3690 clone_info->fill_pattern=NewImageList();
3691 clone_info->stroke_pattern=NewImageList();
3692 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
3693 type=GetImageArtifact(image,property);
3694 if (type != (const char *) NULL)
3695 clone_info->gradient.type=(GradientType) ParseCommandOption(
3696 MagickGradientOptions,MagickFalse,type);
3697 (void) CloneString(&clone_info->primitive,path);
3698 status=DrawImage(*pattern,clone_info,exception);
3699 clone_info=DestroyDrawInfo(clone_info);
3700 if (image->debug != MagickFalse)
3701 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3710 + D r a w P o l y g o n P r i m i t i v e %
3714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3716 % DrawPolygonPrimitive() draws a polygon on the image.
3718 % The format of the DrawPolygonPrimitive method is:
3720 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3721 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3722 % ExceptionInfo *exception)
3724 % A description of each parameter follows:
3726 % o image: the image.
3728 % o draw_info: the draw info.
3730 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3732 % o exception: return any errors or warnings in this structure.
3736 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3741 assert(polygon_info != (PolygonInfo **) NULL);
3742 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3743 if (polygon_info[i] != (PolygonInfo *) NULL)
3744 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3745 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3746 return(polygon_info);
3749 static PolygonInfo **AcquirePolygonThreadSet(
3750 const PrimitiveInfo *primitive_info)
3753 *magick_restrict path_info;
3764 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3765 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3766 sizeof(*polygon_info));
3767 if (polygon_info == (PolygonInfo **) NULL)
3768 return((PolygonInfo **) NULL);
3769 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3770 path_info=ConvertPrimitiveToPath(primitive_info);
3771 if (path_info == (PathInfo *) NULL)
3772 return(DestroyPolygonThreadSet(polygon_info));
3773 for (i=0; i < (ssize_t) number_threads; i++)
3775 polygon_info[i]=ConvertPathToPolygon(path_info);
3776 if (polygon_info[i] == (PolygonInfo *) NULL)
3777 return(DestroyPolygonThreadSet(polygon_info));
3779 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3780 return(polygon_info);
3783 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3784 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3785 const ssize_t y,double *stroke_alpha)
3796 register const PointInfo
3810 Compute fill & stroke opacity for this (x,y) point.
3814 p=polygon_info->edges;
3815 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3817 if ((double) y <= (p->bounds.y1-mid-0.5))
3819 if ((double) y > (p->bounds.y2+mid+0.5))
3821 (void) DestroyEdge(polygon_info,(size_t) j);
3824 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3825 ((double) x > (p->bounds.x2+mid+0.5)))
3827 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3828 for ( ; i < (ssize_t) p->number_points; i++)
3830 if ((double) y <= (p->points[i-1].y-mid-0.5))
3832 if ((double) y > (p->points[i].y+mid+0.5))
3834 if (p->scanline != (double) y)
3836 p->scanline=(double) y;
3837 p->highwater=(size_t) i;
3840 Compute distance between a point and an edge.
3843 delta.x=(q+1)->x-q->x;
3844 delta.y=(q+1)->y-q->y;
3845 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3848 delta.x=(double) x-q->x;
3849 delta.y=(double) y-q->y;
3850 distance=delta.x*delta.x+delta.y*delta.y;
3854 alpha=delta.x*delta.x+delta.y*delta.y;
3857 delta.x=(double) x-(q+1)->x;
3858 delta.y=(double) y-(q+1)->y;
3859 distance=delta.x*delta.x+delta.y*delta.y;
3864 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3865 distance=alpha*beta*beta;
3869 Compute stroke & subpath opacity.
3872 if (p->ghostline == MagickFalse)
3875 if ((*stroke_alpha < 1.0) &&
3876 (distance <= ((alpha+0.25)*(alpha+0.25))))
3879 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3884 if (fabs(distance-1.0) >= DrawEpsilon)
3885 beta=sqrt((double) distance);
3887 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3888 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3892 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3894 if (distance <= 0.0)
3901 if (fabs(beta) < DrawEpsilon)
3904 if (fabs(distance-1.0) >= DrawEpsilon)
3905 beta=sqrt(distance);
3908 if (subpath_alpha < (alpha*alpha))
3909 subpath_alpha=alpha*alpha;
3913 Compute fill opacity.
3915 if (fill == MagickFalse)
3917 if (subpath_alpha >= 1.0)
3920 Determine winding number.
3923 p=polygon_info->edges;
3924 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3926 if ((double) y <= p->bounds.y1)
3928 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3930 if ((double) x > p->bounds.x2)
3932 winding_number+=p->direction ? 1 : -1;
3935 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3936 for ( ; i < (ssize_t) p->number_points; i++)
3937 if ((double) y <= p->points[i].y)
3940 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3941 winding_number+=p->direction ? 1 : -1;
3943 if (fill_rule != NonZeroRule)
3945 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3949 if (MagickAbsoluteValue(winding_number) != 0)
3951 return(subpath_alpha);
3954 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3955 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3956 ExceptionInfo *exception)
3969 **magick_restrict polygon_info;
3986 Compute bounding box.
3988 assert(image != (Image *) NULL);
3989 assert(image->signature == MagickCoreSignature);
3990 if (image->debug != MagickFalse)
3991 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3992 assert(draw_info != (DrawInfo *) NULL);
3993 assert(draw_info->signature == MagickCoreSignature);
3994 assert(primitive_info != (PrimitiveInfo *) NULL);
3995 if (primitive_info->coordinates == 0)
3997 polygon_info=AcquirePolygonThreadSet(primitive_info);
3998 if (polygon_info == (PolygonInfo **) NULL)
3999 return(MagickFalse);
4000 DisableMSCWarning(4127)
4002 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
4004 if (image->debug != MagickFalse)
4005 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4006 fill=(primitive_info->method == FillToBorderMethod) ||
4007 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4008 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4009 bounds=polygon_info[0]->edges[0].bounds;
4010 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4012 p=polygon_info[0]->edges+i;
4013 if (p->bounds.x1 < bounds.x1)
4014 bounds.x1=p->bounds.x1;
4015 if (p->bounds.y1 < bounds.y1)
4016 bounds.y1=p->bounds.y1;
4017 if (p->bounds.x2 > bounds.x2)
4018 bounds.x2=p->bounds.x2;
4019 if (p->bounds.y2 > bounds.y2)
4020 bounds.y2=p->bounds.y2;
4022 bounds.x1-=(mid+1.0);
4023 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
4024 image->columns ? (double) image->columns-1 : bounds.x1;
4025 bounds.y1-=(mid+1.0);
4026 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
4027 image->rows ? (double) image->rows-1 : bounds.y1;
4028 bounds.x2+=(mid+1.0);
4029 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
4030 image->columns ? (double) image->columns-1 : bounds.x2;
4031 bounds.y2+=(mid+1.0);
4032 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
4033 image->rows ? (double) image->rows-1 : bounds.y2;
4035 image_view=AcquireAuthenticCacheView(image,exception);
4036 if ((primitive_info->coordinates == 1) ||
4037 (polygon_info[0]->number_edges == 0))
4042 start_y=(ssize_t) ceil(bounds.y1-0.5);
4043 stop_y=(ssize_t) floor(bounds.y2+0.5);
4044 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4045 #pragma omp parallel for schedule(static,4) shared(status) \
4046 magick_threads(image,image,1,1)
4048 for (y=start_y; y <= stop_y; y++)
4066 if (status == MagickFalse)
4068 start_x=(ssize_t) ceil(bounds.x1-0.5);
4069 stop_x=(ssize_t) floor(bounds.x2+0.5);
4071 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4073 if (q == (Quantum *) NULL)
4078 GetPixelInfo(image,&pixel);
4079 for ( ; x <= stop_x; x++)
4081 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4082 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4084 GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4085 SetPixelViaPixelInfo(image,&pixel,q);
4087 q+=GetPixelChannels(image);
4089 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4090 if (sync == MagickFalse)
4093 image_view=DestroyCacheView(image_view);
4094 polygon_info=DestroyPolygonThreadSet(polygon_info);
4095 if (image->debug != MagickFalse)
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4097 " end draw-polygon");
4101 Draw polygon or line.
4103 if (image->alpha_trait == UndefinedPixelTrait)
4104 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4105 start_y=(ssize_t) ceil(bounds.y1-0.5);
4106 stop_y=(ssize_t) floor(bounds.y2+0.5);
4107 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4108 #pragma omp parallel for schedule(static,4) shared(status) \
4109 magick_threads(image,image,1,1)
4111 for (y=start_y; y <= stop_y; y++)
4114 id = GetOpenMPThreadId();
4134 if (status == MagickFalse)
4136 start_x=(ssize_t) ceil(bounds.x1-0.5);
4137 stop_x=(ssize_t) floor(bounds.x2+0.5);
4138 q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+1),1,
4140 if (q == (Quantum *) NULL)
4145 for (x=start_x; x <= stop_x; x++)
4150 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4152 if (draw_info->stroke_antialias == MagickFalse)
4154 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4155 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4157 GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4158 fill_alpha=fill_alpha*fill_color.alpha;
4159 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4160 GetPixelAlpha(image,q),q);
4161 GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4162 stroke_alpha=stroke_alpha*stroke_color.alpha;
4163 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4164 GetPixelAlpha(image,q),q);
4165 q+=GetPixelChannels(image);
4167 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4170 image_view=DestroyCacheView(image_view);
4171 polygon_info=DestroyPolygonThreadSet(polygon_info);
4172 if (image->debug != MagickFalse)
4173 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4182 % D r a w P r i m i t i v e %
4186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4188 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4190 % The format of the DrawPrimitive method is:
4192 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4193 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4195 % A description of each parameter follows:
4197 % o image: the image.
4199 % o draw_info: the draw info.
4201 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4203 % o exception: return any errors or warnings in this structure.
4207 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4233 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4234 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4235 switch (primitive_info->primitive)
4237 case AlphaPrimitive:
4239 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4240 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4241 methods[primitive_info->method]);
4244 case ColorPrimitive:
4246 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4247 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4248 methods[primitive_info->method]);
4251 case ImagePrimitive:
4253 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4254 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4257 case PointPrimitive:
4259 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4260 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4261 methods[primitive_info->method]);
4266 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4267 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4274 p=primitive_info[0].point;
4277 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4279 point=primitive_info[i].point;
4280 if (coordinates <= 0)
4282 coordinates=(ssize_t) primitive_info[i].coordinates;
4283 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4284 " begin open (%.20g)",(double) coordinates);
4287 point=primitive_info[i].point;
4288 if ((fabs(q.x-point.x) >= DrawEpsilon) ||
4289 (fabs(q.y-point.y) >= DrawEpsilon))
4290 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4291 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4293 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4294 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4297 if (coordinates > 0)
4299 if ((fabs(p.x-point.x) >= DrawEpsilon) ||
4300 (fabs(p.y-point.y) >= DrawEpsilon))
4301 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4302 (double) coordinates);
4304 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4305 (double) coordinates);
4309 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4310 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4311 ExceptionInfo *exception)
4326 if (image->debug != MagickFalse)
4328 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4329 " begin draw-primitive");
4330 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4331 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
4332 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4333 draw_info->affine.tx,draw_info->affine.ty);
4335 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4336 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4337 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4338 (void) SetImageColorspace(image,sRGBColorspace,exception);
4340 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4341 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4342 image_view=AcquireAuthenticCacheView(image,exception);
4343 switch (primitive_info->primitive)
4345 case AlphaPrimitive:
4347 if (image->alpha_trait == UndefinedPixelTrait)
4348 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4349 switch (primitive_info->method)
4360 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4361 if (q == (Quantum *) NULL)
4363 GetFillColor(draw_info,x,y,&pixel,exception);
4364 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4365 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4377 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4379 GetPixelInfo(image,&pixel);
4380 for (y=0; y < (ssize_t) image->rows; y++)
4385 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4387 if (q == (Quantum *) NULL)
4389 for (x=0; x < (ssize_t) image->columns; x++)
4391 GetPixelInfoPixel(image,q,&pixel);
4392 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4394 q+=GetPixelChannels(image);
4397 GetFillColor(draw_info,x,y,&pixel,exception);
4398 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4399 q+=GetPixelChannels(image);
4401 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4402 if (sync == MagickFalse)
4407 case FloodfillMethod:
4408 case FillToBorderMethod:
4416 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4418 if (primitive_info->method == FillToBorderMethod)
4420 target.red=(double) draw_info->border_color.red;
4421 target.green=(double) draw_info->border_color.green;
4422 target.blue=(double) draw_info->border_color.blue;
4424 channel_mask=SetImageChannelMask(image,AlphaChannel);
4425 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4426 primitive_info->method == FloodfillMethod ? MagickFalse :
4427 MagickTrue,exception);
4428 (void) SetImageChannelMask(image,channel_mask);
4439 for (y=0; y < (ssize_t) image->rows; y++)
4444 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4446 if (q == (Quantum *) NULL)
4448 for (x=0; x < (ssize_t) image->columns; x++)
4450 GetFillColor(draw_info,x,y,&pixel,exception);
4451 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4452 q+=GetPixelChannels(image);
4454 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4455 if (sync == MagickFalse)
4463 case ColorPrimitive:
4465 switch (primitive_info->method)
4476 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4477 if (q == (Quantum *) NULL)
4479 GetPixelInfo(image,&pixel);
4480 GetFillColor(draw_info,x,y,&pixel,exception);
4481 SetPixelViaPixelInfo(image,&pixel,q);
4482 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4494 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4496 for (y=0; y < (ssize_t) image->rows; y++)
4501 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4503 if (q == (Quantum *) NULL)
4505 for (x=0; x < (ssize_t) image->columns; x++)
4507 GetPixelInfoPixel(image,q,&pixel);
4508 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4510 q+=GetPixelChannels(image);
4513 GetFillColor(draw_info,x,y,&pixel,exception);
4514 SetPixelViaPixelInfo(image,&pixel,q);
4515 q+=GetPixelChannels(image);
4517 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4518 if (sync == MagickFalse)
4523 case FloodfillMethod:
4524 case FillToBorderMethod:
4529 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4531 if (primitive_info->method == FillToBorderMethod)
4533 target.red=(double) draw_info->border_color.red;
4534 target.green=(double) draw_info->border_color.green;
4535 target.blue=(double) draw_info->border_color.blue;
4537 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4538 primitive_info->method == FloodfillMethod ? MagickFalse :
4539 MagickTrue,exception);
4550 GetPixelInfo(image,&pixel);
4551 for (y=0; y < (ssize_t) image->rows; y++)
4556 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4558 if (q == (Quantum *) NULL)
4560 for (x=0; x < (ssize_t) image->columns; x++)
4562 GetFillColor(draw_info,x,y,&pixel,exception);
4563 SetPixelViaPixelInfo(image,&pixel,q);
4564 q+=GetPixelChannels(image);
4566 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4567 if (sync == MagickFalse)
4575 case ImagePrimitive:
4581 composite_geometry[MagickPathExtent];
4596 if (primitive_info->text == (char *) NULL)
4598 clone_info=AcquireImageInfo();
4599 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4600 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4604 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4606 composite_image=ReadImage(clone_info,exception);
4608 clone_info=DestroyImageInfo(clone_info);
4609 if (composite_image == (Image *) NULL)
4611 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4612 NULL,(void *) NULL);
4613 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4614 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4615 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4616 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4621 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4622 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
4623 composite_image->filter=image->filter;
4624 (void) TransformImage(&composite_image,(char *) NULL,
4625 composite_geometry,exception);
4627 if (composite_image->alpha_trait == UndefinedPixelTrait)
4628 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4630 if (draw_info->alpha != OpaqueAlpha)
4631 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4632 SetGeometry(image,&geometry);
4633 image->gravity=draw_info->gravity;
4636 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4637 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4638 composite_image->rows,(double) geometry.x,(double) geometry.y);
4639 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4640 affine=draw_info->affine;
4641 affine.tx=(double) geometry.x;
4642 affine.ty=(double) geometry.y;
4643 composite_image->interpolate=image->interpolate;
4644 if (draw_info->compose == OverCompositeOp)
4645 (void) DrawAffineImage(image,composite_image,&affine,exception);
4647 (void) CompositeImage(image,composite_image,draw_info->compose,
4648 MagickTrue,geometry.x,geometry.y,exception);
4649 composite_image=DestroyImage(composite_image);
4652 case PointPrimitive:
4660 if ((y < 0) || (y >= (ssize_t) image->rows))
4662 if ((x < 0) || (x >= (ssize_t) image->columns))
4664 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4665 if (q == (Quantum *) NULL)
4667 GetFillColor(draw_info,x,y,&fill_color,exception);
4668 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4669 (double) GetPixelAlpha(image,q),q);
4670 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4676 geometry[MagickPathExtent];
4681 if (primitive_info->text == (char *) NULL)
4683 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4684 (void) CloneString(&clone_info->text,primitive_info->text);
4685 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4686 primitive_info->point.x,primitive_info->point.y);
4687 (void) CloneString(&clone_info->geometry,geometry);
4688 status&=AnnotateImage(image,clone_info,exception);
4689 clone_info=DestroyDrawInfo(clone_info);
4701 if (IsEventLogging() != MagickFalse)
4702 LogPrimitiveInfo(primitive_info);
4703 scale=ExpandAffine(&draw_info->affine);
4704 if ((draw_info->dash_pattern != (double *) NULL) &&
4705 (fabs(draw_info->dash_pattern[0]) >= DrawEpsilon) &&
4706 (fabs(scale*draw_info->stroke_width) >= DrawEpsilon) &&
4707 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4712 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4713 clone_info->stroke_width=0.0;
4714 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4715 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4717 clone_info=DestroyDrawInfo(clone_info);
4718 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4721 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4723 ((draw_info->stroke.alpha != (Quantum) TransparentAlpha) ||
4724 (draw_info->stroke_pattern != (Image *) NULL)))
4730 Draw strokes while respecting line cap/join attributes.
4732 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4734 (fabs(primitive_info[i-1].point.x-primitive_info[0].point.x) < DrawEpsilon) &&
4735 (fabs(primitive_info[i-1].point.y-primitive_info[0].point.y) < DrawEpsilon) ?
4736 MagickTrue : MagickFalse;
4737 i=(ssize_t) primitive_info[0].coordinates;
4738 if (((closed_path != MagickFalse) &&
4739 (draw_info->linejoin == RoundJoin)) ||
4740 (primitive_info[i].primitive != UndefinedPrimitive))
4742 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4746 if (draw_info->linecap == RoundCap)
4748 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4752 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4753 clone_info->stroke_width=0.0;
4754 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4755 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4757 clone_info=DestroyDrawInfo(clone_info);
4758 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4761 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4765 image_view=DestroyCacheView(image_view);
4766 if (image->debug != MagickFalse)
4767 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4768 return(status != 0 ? MagickTrue : MagickFalse);
4772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4776 + D r a w S t r o k e P o l y g o n %
4780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4782 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4783 % the image while respecting the line cap and join attributes.
4785 % The format of the DrawStrokePolygon method is:
4787 % MagickBooleanType DrawStrokePolygon(Image *image,
4788 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4790 % A description of each parameter follows:
4792 % o image: the image.
4794 % o draw_info: the draw info.
4796 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4801 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4802 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4810 for (i=0; i < 4; i++)
4811 linecap[i]=(*primitive_info);
4812 linecap[0].coordinates=4;
4813 linecap[1].point.x+=2.0*DrawEpsilon;
4814 linecap[2].point.x+=2.0*DrawEpsilon;
4815 linecap[2].point.y+=2.0*DrawEpsilon;
4816 linecap[3].point.y+=2.0*DrawEpsilon;
4817 linecap[4].primitive=UndefinedPrimitive;
4818 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4821 static MagickBooleanType DrawStrokePolygon(Image *image,
4822 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4823 ExceptionInfo *exception)
4837 register const PrimitiveInfo
4842 Draw stroked polygon.
4844 if (image->debug != MagickFalse)
4845 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4846 " begin draw-stroke-polygon");
4847 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4848 clone_info->fill=draw_info->stroke;
4849 if (clone_info->fill_pattern != (Image *) NULL)
4850 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4851 if (clone_info->stroke_pattern != (Image *) NULL)
4852 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4853 MagickTrue,exception);
4854 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4855 clone_info->stroke_width=0.0;
4856 clone_info->fill_rule=NonZeroRule;
4858 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4860 stroke_polygon=TraceStrokePolygon(draw_info,p);
4861 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4864 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4865 q=p+p->coordinates-1;
4866 closed_path=(fabs(q->point.x-p->point.x) < DrawEpsilon) &&
4867 (fabs(q->point.y-p->point.y) < DrawEpsilon) ? MagickTrue : MagickFalse;
4868 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4870 DrawRoundLinecap(image,draw_info,p,exception);
4871 DrawRoundLinecap(image,draw_info,q,exception);
4874 clone_info=DestroyDrawInfo(clone_info);
4875 if (image->debug != MagickFalse)
4876 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4877 " end draw-stroke-polygon");
4878 return(status != 0 ? MagickTrue : MagickFalse);
4882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4886 % G e t A f f i n e M a t r i x %
4890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4892 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4895 % The format of the GetAffineMatrix method is:
4897 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4899 % A description of each parameter follows:
4901 % o affine_matrix: the affine matrix.
4904 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4907 assert(affine_matrix != (AffineMatrix *) NULL);
4908 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4909 affine_matrix->sx=1.0;
4910 affine_matrix->sy=1.0;
4914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4918 + G e t D r a w I n f o %
4922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4924 % GetDrawInfo() initializes draw_info to default values from image_info.
4926 % The format of the GetDrawInfo method is:
4928 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4930 % A description of each parameter follows:
4932 % o image_info: the image info..
4934 % o draw_info: the draw info.
4937 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4952 Initialize draw attributes.
4954 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4955 assert(draw_info != (DrawInfo *) NULL);
4956 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4957 clone_info=CloneImageInfo(image_info);
4958 GetAffineMatrix(&draw_info->affine);
4959 exception=AcquireExceptionInfo();
4960 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4962 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4964 draw_info->stroke_width=1.0;
4965 draw_info->alpha=OpaqueAlpha;
4966 draw_info->fill_rule=EvenOddRule;
4967 draw_info->linecap=ButtCap;
4968 draw_info->linejoin=MiterJoin;
4969 draw_info->miterlimit=10;
4970 draw_info->decorate=NoDecoration;
4971 draw_info->pointsize=12.0;
4972 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
4973 draw_info->compose=OverCompositeOp;
4974 draw_info->render=MagickTrue;
4975 draw_info->debug=IsEventLogging();
4976 draw_info->stroke_antialias=clone_info->antialias;
4977 if (clone_info->font != (char *) NULL)
4978 draw_info->font=AcquireString(clone_info->font);
4979 if (clone_info->density != (char *) NULL)
4980 draw_info->density=AcquireString(clone_info->density);
4981 draw_info->text_antialias=clone_info->antialias;
4982 if (fabs(clone_info->pointsize) >= DrawEpsilon)
4983 draw_info->pointsize=clone_info->pointsize;
4984 draw_info->border_color=clone_info->border_color;
4985 if (clone_info->server_name != (char *) NULL)
4986 draw_info->server_name=AcquireString(clone_info->server_name);
4987 option=GetImageOption(clone_info,"direction");
4988 if (option != (const char *) NULL)
4989 draw_info->direction=(DirectionType) ParseCommandOption(
4990 MagickDirectionOptions,MagickFalse,option);
4992 draw_info->direction=UndefinedDirection;
4993 option=GetImageOption(clone_info,"encoding");
4994 if (option != (const char *) NULL)
4995 (void) CloneString(&draw_info->encoding,option);
4996 option=GetImageOption(clone_info,"family");
4997 if (option != (const char *) NULL)
4998 (void) CloneString(&draw_info->family,option);
4999 option=GetImageOption(clone_info,"fill");
5000 if (option != (const char *) NULL)
5001 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
5003 option=GetImageOption(clone_info,"gravity");
5004 if (option != (const char *) NULL)
5005 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5006 MagickFalse,option);
5007 option=GetImageOption(clone_info,"interline-spacing");
5008 if (option != (const char *) NULL)
5009 draw_info->interline_spacing=StringToDouble(option,&next_token);
5010 option=GetImageOption(clone_info,"interword-spacing");
5011 if (option != (const char *) NULL)
5012 draw_info->interword_spacing=StringToDouble(option,&next_token);
5013 option=GetImageOption(clone_info,"kerning");
5014 if (option != (const char *) NULL)
5015 draw_info->kerning=StringToDouble(option,&next_token);
5016 option=GetImageOption(clone_info,"stroke");
5017 if (option != (const char *) NULL)
5018 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
5020 option=GetImageOption(clone_info,"strokewidth");
5021 if (option != (const char *) NULL)
5022 draw_info->stroke_width=StringToDouble(option,&next_token);
5023 option=GetImageOption(clone_info,"style");
5024 if (option != (const char *) NULL)
5025 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
5026 MagickFalse,option);
5027 option=GetImageOption(clone_info,"undercolor");
5028 if (option != (const char *) NULL)
5029 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
5031 option=GetImageOption(clone_info,"weight");
5032 if (option != (const char *) NULL)
5037 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
5039 weight=(ssize_t) StringToUnsignedLong(option);
5040 draw_info->weight=(size_t) weight;
5042 exception=DestroyExceptionInfo(exception);
5043 draw_info->signature=MagickCoreSignature;
5044 clone_info=DestroyImageInfo(clone_info);
5048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5052 + P e r m u t a t e %
5056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5058 % Permutate() returns the permuation of the (n,k).
5060 % The format of the Permutate method is:
5062 % void Permutate(ssize_t n,ssize_t k)
5064 % A description of each parameter follows:
5072 static inline double Permutate(const ssize_t n,const ssize_t k)
5081 for (i=k+1; i <= n; i++)
5083 for (i=1; i <= (n-k); i++)
5089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5093 + T r a c e P r i m i t i v e %
5097 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5099 % TracePrimitive is a collection of methods for generating graphic
5100 % primitives such as arcs, ellipses, paths, etc.
5104 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
5105 const PointInfo end,const PointInfo degrees)
5111 center.x=0.5*(end.x+start.x);
5112 center.y=0.5*(end.y+start.y);
5113 radii.x=fabs(center.x-start.x);
5114 radii.y=fabs(center.y-start.y);
5115 TraceEllipse(primitive_info,center,radii,degrees);
5118 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
5119 const PointInfo end,const PointInfo arc,const double angle,
5120 const MagickBooleanType large_arc,const MagickBooleanType sweep)
5139 register PrimitiveInfo
5148 if ((fabs(start.x-end.x) < DrawEpsilon) &&
5149 (fabs(start.y-end.y) < DrawEpsilon))
5151 TracePoint(primitive_info,end);
5154 radii.x=fabs(arc.x);
5155 radii.y=fabs(arc.y);
5156 if ((fabs(radii.x) < DrawEpsilon) || (fabs(radii.y) < DrawEpsilon))
5158 TraceLine(primitive_info,start,end);
5161 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5162 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5163 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5164 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5165 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5167 if (delta < DrawEpsilon)
5169 TraceLine(primitive_info,start,end);
5174 radii.x*=sqrt((double) delta);
5175 radii.y*=sqrt((double) delta);
5177 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5178 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5179 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5180 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5181 alpha=points[1].x-points[0].x;
5182 beta=points[1].y-points[0].y;
5183 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5188 factor=sqrt((double) factor);
5189 if (sweep == large_arc)
5192 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5193 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5194 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5195 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5196 if ((theta < 0.0) && (sweep != MagickFalse))
5197 theta+=(double) (2.0*MagickPI);
5199 if ((theta > 0.0) && (sweep == MagickFalse))
5200 theta-=(double) (2.0*MagickPI);
5201 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5204 for (i=0; i < (ssize_t) arc_segments; i++)
5206 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5207 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5208 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5209 sin(fmod((double) beta,DegreesToRadians(360.0)));
5210 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5211 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5212 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5213 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5214 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5215 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5216 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5217 theta/arc_segments),DegreesToRadians(360.0))));
5218 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5219 theta/arc_segments),DegreesToRadians(360.0))));
5220 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5221 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5222 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5223 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5224 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5225 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5226 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5228 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5230 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5232 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5234 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5236 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5238 if (i == (ssize_t) (arc_segments-1))
5243 primitive_info->coordinates=(size_t) (p-primitive_info);
5244 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5246 p->primitive=primitive_info->primitive;
5251 static void TraceBezier(PrimitiveInfo *primitive_info,
5252 const size_t number_coordinates)
5264 register PrimitiveInfo
5276 Allocate coeficients.
5278 quantum=number_coordinates;
5279 for (i=0; i < (ssize_t) number_coordinates; i++)
5281 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5283 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5284 if (alpha > (double) quantum)
5285 quantum=(size_t) alpha;
5286 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5287 if (alpha > (double) quantum)
5288 quantum=(size_t) alpha;
5291 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5292 (double) BezierQuantum);
5293 control_points=quantum*number_coordinates;
5294 coefficients=(double *) AcquireQuantumMemory((size_t)
5295 number_coordinates,sizeof(*coefficients));
5296 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5298 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
5299 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5301 Compute bezier points.
5303 end=primitive_info[number_coordinates-1].point;
5304 for (i=0; i < (ssize_t) number_coordinates; i++)
5305 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5307 for (i=0; i < (ssize_t) control_points; i++)
5312 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5313 for (j=0; j < (ssize_t) number_coordinates; j++)
5315 point.x+=alpha*coefficients[j]*p->point.x;
5316 point.y+=alpha*coefficients[j]*p->point.y;
5317 alpha*=weight/(1.0-weight);
5321 weight+=1.0/control_points;
5324 Bezier curves are just short segmented polys.
5327 for (i=0; i < (ssize_t) control_points; i++)
5329 TracePoint(p,points[i]);
5334 primitive_info->coordinates=(size_t) (p-primitive_info);
5335 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5337 p->primitive=primitive_info->primitive;
5340 points=(PointInfo *) RelinquishMagickMemory(points);
5341 coefficients=(double *) RelinquishMagickMemory(coefficients);
5344 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5345 const PointInfo end)
5356 alpha=end.x-start.x;
5358 radius=hypot((double) alpha,(double) beta);
5359 offset.x=(double) radius;
5360 offset.y=(double) radius;
5363 TraceEllipse(primitive_info,start,offset,degrees);
5366 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5367 const PointInfo stop,const PointInfo degrees)
5378 register PrimitiveInfo
5385 Ellipses are just short segmented polys.
5387 if ((fabs(stop.x) < DrawEpsilon) && (fabs(stop.y) < DrawEpsilon))
5389 TracePoint(primitive_info,start);
5392 delta=2.0/MagickMax(stop.x,stop.y);
5393 step=(double) (MagickPI/8.0);
5394 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5395 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5396 angle.x=DegreesToRadians(degrees.x);
5398 while (y < degrees.x)
5400 angle.y=(double) DegreesToRadians(y);
5401 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5403 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5404 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5405 TracePoint(p,point);
5408 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5409 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5410 TracePoint(p,point);
5412 primitive_info->coordinates=(size_t) (p-primitive_info);
5413 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5415 p->primitive=primitive_info->primitive;
5420 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5421 const PointInfo end)
5423 TracePoint(primitive_info,start);
5424 if ((fabs(start.x-end.x) < DrawEpsilon) &&
5425 (fabs(start.y-end.y) < DrawEpsilon))
5427 primitive_info->primitive=PointPrimitive;
5428 primitive_info->coordinates=1;
5431 TracePoint(primitive_info+1,end);
5432 (primitive_info+1)->primitive=primitive_info->primitive;
5433 primitive_info->coordinates=2;
5436 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5440 token[MagickPathExtent];
5455 points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5462 register PrimitiveInfo
5473 number_coordinates=0;
5475 primitive_type=primitive_info->primitive;
5477 for (p=path; *p != '\0'; )
5479 while (isspace((int) ((unsigned char) *p)) != 0)
5483 last_attribute=attribute;
5484 attribute=(int) (*p++);
5505 GetNextToken(p,&p,MagickPathExtent,token);
5507 GetNextToken(p,&p,MagickPathExtent,token);
5508 arc.x=StringToDouble(token,&next_token);
5509 GetNextToken(p,&p,MagickPathExtent,token);
5511 GetNextToken(p,&p,MagickPathExtent,token);
5512 arc.y=StringToDouble(token,&next_token);
5513 GetNextToken(p,&p,MagickPathExtent,token);
5515 GetNextToken(p,&p,MagickPathExtent,token);
5516 angle=StringToDouble(token,&next_token);
5517 GetNextToken(p,&p,MagickPathExtent,token);
5519 GetNextToken(p,&p,MagickPathExtent,token);
5520 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5521 GetNextToken(p,&p,MagickPathExtent,token);
5523 GetNextToken(p,&p,MagickPathExtent,token);
5524 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5525 GetNextToken(p,&p,MagickPathExtent,token);
5527 GetNextToken(p,&p,MagickPathExtent,token);
5528 x=StringToDouble(token,&next_token);
5529 GetNextToken(p,&p,MagickPathExtent,token);
5531 GetNextToken(p,&p,MagickPathExtent,token);
5532 y=StringToDouble(token,&next_token);
5533 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5534 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5535 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5538 while (isspace((int) ((unsigned char) *p)) != 0)
5542 } while (IsPoint(p) != MagickFalse);
5549 Compute bezier points.
5554 for (i=1; i < 4; i++)
5556 GetNextToken(p,&p,MagickPathExtent,token);
5558 GetNextToken(p,&p,MagickPathExtent,token);
5559 x=StringToDouble(token,&next_token);
5560 GetNextToken(p,&p,MagickPathExtent,token);
5562 GetNextToken(p,&p,MagickPathExtent,token);
5563 y=StringToDouble(token,&next_token);
5564 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5565 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5568 for (i=0; i < 4; i++)
5569 (q+i)->point=points[i];
5573 } while (IsPoint(p) != MagickFalse);
5581 GetNextToken(p,&p,MagickPathExtent,token);
5583 GetNextToken(p,&p,MagickPathExtent,token);
5584 x=StringToDouble(token,&next_token);
5585 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5586 TracePoint(q,point);
5588 } while (IsPoint(p) != MagickFalse);
5596 GetNextToken(p,&p,MagickPathExtent,token);
5598 GetNextToken(p,&p,MagickPathExtent,token);
5599 x=StringToDouble(token,&next_token);
5600 GetNextToken(p,&p,MagickPathExtent,token);
5602 GetNextToken(p,&p,MagickPathExtent,token);
5603 y=StringToDouble(token,&next_token);
5604 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5605 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5606 TracePoint(q,point);
5608 } while (IsPoint(p) != MagickFalse);
5614 if (q != primitive_info)
5616 primitive_info->coordinates=(size_t) (q-primitive_info);
5617 number_coordinates+=primitive_info->coordinates;
5623 GetNextToken(p,&p,MagickPathExtent,token);
5625 GetNextToken(p,&p,MagickPathExtent,token);
5626 x=StringToDouble(token,&next_token);
5627 GetNextToken(p,&p,MagickPathExtent,token);
5629 GetNextToken(p,&p,MagickPathExtent,token);
5630 y=StringToDouble(token,&next_token);
5631 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5632 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5636 TracePoint(q,point);
5638 if ((i != 0) && (attribute == (int) 'M'))
5640 TracePoint(q,point);
5643 } while (IsPoint(p) != MagickFalse);
5650 Compute bezier points.
5655 for (i=1; i < 3; i++)
5657 GetNextToken(p,&p,MagickPathExtent,token);
5659 GetNextToken(p,&p,MagickPathExtent,token);
5660 x=StringToDouble(token,&next_token);
5661 GetNextToken(p,&p,MagickPathExtent,token);
5663 GetNextToken(p,&p,MagickPathExtent,token);
5664 y=StringToDouble(token,&next_token);
5667 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5668 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5671 for (i=0; i < 3; i++)
5672 (q+i)->point=points[i];
5676 } while (IsPoint(p) != MagickFalse);
5683 Compute bezier points.
5687 points[0]=points[3];
5688 points[1].x=2.0*points[3].x-points[2].x;
5689 points[1].y=2.0*points[3].y-points[2].y;
5690 for (i=2; i < 4; i++)
5692 GetNextToken(p,&p,MagickPathExtent,token);
5694 GetNextToken(p,&p,MagickPathExtent,token);
5695 x=StringToDouble(token,&next_token);
5696 GetNextToken(p,&p,MagickPathExtent,token);
5698 GetNextToken(p,&p,MagickPathExtent,token);
5699 y=StringToDouble(token,&next_token);
5702 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5703 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5706 if (strchr("CcSs",last_attribute) == (char *) NULL)
5711 for (i=0; i < 4; i++)
5712 (q+i)->point=points[i];
5716 } while (IsPoint(p) != MagickFalse);
5723 Compute bezier points.
5727 points[0]=points[2];
5728 points[1].x=2.0*points[2].x-points[1].x;
5729 points[1].y=2.0*points[2].y-points[1].y;
5730 for (i=2; i < 3; i++)
5732 GetNextToken(p,&p,MagickPathExtent,token);
5734 GetNextToken(p,&p,MagickPathExtent,token);
5735 x=StringToDouble(token,&next_token);
5736 GetNextToken(p,&p,MagickPathExtent,token);
5738 GetNextToken(p,&p,MagickPathExtent,token);
5739 y=StringToDouble(token,&next_token);
5740 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5741 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5744 if (strchr("QqTt",last_attribute) == (char *) NULL)
5749 for (i=0; i < 3; i++)
5750 (q+i)->point=points[i];
5754 } while (IsPoint(p) != MagickFalse);
5762 GetNextToken(p,&p,MagickPathExtent,token);
5764 GetNextToken(p,&p,MagickPathExtent,token);
5765 y=StringToDouble(token,&next_token);
5766 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5767 TracePoint(q,point);
5769 } while (IsPoint(p) != MagickFalse);
5776 TracePoint(q,point);
5778 primitive_info->coordinates=(size_t) (q-primitive_info);
5779 number_coordinates+=primitive_info->coordinates;
5786 if (isalpha((int) ((unsigned char) attribute)) != 0)
5787 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5793 primitive_info->coordinates=(size_t) (q-primitive_info);
5794 number_coordinates+=primitive_info->coordinates;
5795 for (i=0; i < (ssize_t) number_coordinates; i++)
5798 q->primitive=primitive_type;
5800 q->method=FillToBorderMethod;
5803 return(number_coordinates);
5806 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5807 const PointInfo end)
5812 register PrimitiveInfo
5819 TracePoint(p,start);
5823 TracePoint(p,point);
5829 TracePoint(p,point);
5831 TracePoint(p,start);
5833 primitive_info->coordinates=(size_t) (p-primitive_info);
5834 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5836 p->primitive=primitive_info->primitive;
5841 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5842 const PointInfo start,const PointInfo end,PointInfo arc)
5849 register PrimitiveInfo
5856 offset.x=fabs(end.x-start.x);
5857 offset.y=fabs(end.y-start.y);
5858 if (arc.x > (0.5*offset.x))
5860 if (arc.y > (0.5*offset.y))
5862 point.x=start.x+offset.x-arc.x;
5863 point.y=start.y+arc.y;
5866 TraceEllipse(p,point,arc,degrees);
5868 point.x=start.x+offset.x-arc.x;
5869 point.y=start.y+offset.y-arc.y;
5872 TraceEllipse(p,point,arc,degrees);
5874 point.x=start.x+arc.x;
5875 point.y=start.y+offset.y-arc.y;
5878 TraceEllipse(p,point,arc,degrees);
5880 point.x=start.x+arc.x;
5881 point.y=start.y+arc.y;
5884 TraceEllipse(p,point,arc,degrees);
5886 TracePoint(p,primitive_info->point);
5888 primitive_info->coordinates=(size_t) (p-primitive_info);
5889 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5891 p->primitive=primitive_info->primitive;
5896 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5897 const size_t number_vertices,const double offset)
5914 for (i=1; i < (ssize_t) number_vertices; i++)
5916 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5917 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5918 if ((fabs((double) dx) >= DrawEpsilon) ||
5919 (fabs((double) dy) >= DrawEpsilon))
5922 if (i == (ssize_t) number_vertices)
5923 i=(ssize_t) number_vertices-1L;
5924 distance=hypot((double) dx,(double) dy);
5925 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5926 dx*(distance+offset)/distance);
5927 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5928 dy*(distance+offset)/distance);
5929 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5931 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5932 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5933 if ((fabs((double) dx) >= DrawEpsilon) ||
5934 (fabs((double) dy) >= DrawEpsilon))
5937 distance=hypot((double) dx,(double) dy);
5938 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5939 dx*(distance+offset)/distance);
5940 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5941 dy*(distance+offset)/distance);
5944 static inline double DrawEpsilonReciprocal(const double x)
5946 double sign = x < 0.0 ? -1.0 : 1.0;
5947 return((sign*x) >= DrawEpsilon ? 1.0/x : sign*(1.0/DrawEpsilon));
5950 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5951 const PrimitiveInfo *primitive_info)
5953 typedef struct _LineSegment
6005 number_vertices=primitive_info->coordinates;
6006 max_strokes=2*number_vertices+6*BezierQuantum+360;
6007 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
6009 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
6011 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6012 number_vertices+2UL,sizeof(*polygon_primitive));
6013 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
6014 (polygon_primitive == (PrimitiveInfo *) NULL))
6015 return((PrimitiveInfo *) NULL);
6016 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
6017 number_vertices*sizeof(*polygon_primitive));
6019 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
6020 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
6021 MagickTrue : MagickFalse;
6022 if ((draw_info->linejoin == RoundJoin) ||
6023 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
6025 polygon_primitive[number_vertices]=primitive_info[1];
6028 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
6030 Compute the slope for the first line segment, p.
6034 for (n=1; n < (ssize_t) number_vertices; n++)
6036 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
6037 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
6038 if ((fabs(dx.p) >= DrawEpsilon) || (fabs(dy.p) >= DrawEpsilon))
6041 if (n == (ssize_t) number_vertices)
6042 n=(ssize_t) number_vertices-1L;
6044 inverse_slope.p=0.0;
6045 if (fabs(dx.p) < DrawEpsilon)
6048 slope.p=dy.p < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6050 slope.p=dy.p < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6053 if (fabs(dy.p) < DrawEpsilon)
6056 inverse_slope.p=dx.p < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6058 inverse_slope.p=dx.p < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6063 inverse_slope.p=(-1.0/slope.p);
6065 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
6066 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
6068 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
6069 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
6070 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
6071 offset.y=(double) (offset.x*inverse_slope.p);
6072 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
6074 box_p[0].x=polygon_primitive[0].point.x-offset.x;
6075 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
6076 box_p[1].x=polygon_primitive[n].point.x-offset.x;
6077 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
6078 box_q[0].x=polygon_primitive[0].point.x+offset.x;
6079 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
6080 box_q[1].x=polygon_primitive[n].point.x+offset.x;
6081 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
6085 box_p[0].x=polygon_primitive[0].point.x+offset.x;
6086 box_p[0].y=polygon_primitive[0].point.y+offset.y;
6087 box_p[1].x=polygon_primitive[n].point.x+offset.x;
6088 box_p[1].y=polygon_primitive[n].point.y+offset.y;
6089 box_q[0].x=polygon_primitive[0].point.x-offset.x;
6090 box_q[0].y=polygon_primitive[0].point.y-offset.y;
6091 box_q[1].x=polygon_primitive[n].point.x-offset.x;
6092 box_q[1].y=polygon_primitive[n].point.y-offset.y;
6095 Create strokes for the line join attribute: bevel, miter, round.
6099 path_q[p++]=box_q[0];
6100 path_p[q++]=box_p[0];
6101 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
6104 Compute the slope for this line segment, q.
6106 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
6107 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
6108 dot_product=dx.q*dx.q+dy.q*dy.q;
6109 if (dot_product < 0.25)
6112 inverse_slope.q=0.0;
6113 if (fabs(dx.q) < DrawEpsilon)
6116 slope.q=dy.q < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6118 slope.q=dy.q < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6121 if (fabs(dy.q) < DrawEpsilon)
6124 inverse_slope.q=dx.q < 0.0 ? -1.0/DrawEpsilon : 1.0/DrawEpsilon;
6126 inverse_slope.q=dx.q < 0.0 ? 1.0/DrawEpsilon : -1.0/DrawEpsilon;
6131 inverse_slope.q=(-1.0/slope.q);
6133 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
6134 offset.y=(double) (offset.x*inverse_slope.q);
6135 dot_product=dy.q*offset.x-dx.q*offset.y;
6136 if (dot_product > 0.0)
6138 box_p[2].x=polygon_primitive[n].point.x-offset.x;
6139 box_p[2].y=polygon_primitive[n].point.y-offset.y;
6140 box_p[3].x=polygon_primitive[i].point.x-offset.x;
6141 box_p[3].y=polygon_primitive[i].point.y-offset.y;
6142 box_q[2].x=polygon_primitive[n].point.x+offset.x;
6143 box_q[2].y=polygon_primitive[n].point.y+offset.y;
6144 box_q[3].x=polygon_primitive[i].point.x+offset.x;
6145 box_q[3].y=polygon_primitive[i].point.y+offset.y;
6149 box_p[2].x=polygon_primitive[n].point.x+offset.x;
6150 box_p[2].y=polygon_primitive[n].point.y+offset.y;
6151 box_p[3].x=polygon_primitive[i].point.x+offset.x;
6152 box_p[3].y=polygon_primitive[i].point.y+offset.y;
6153 box_q[2].x=polygon_primitive[n].point.x-offset.x;
6154 box_q[2].y=polygon_primitive[n].point.y-offset.y;
6155 box_q[3].x=polygon_primitive[i].point.x-offset.x;
6156 box_q[3].y=polygon_primitive[i].point.y-offset.y;
6158 if (fabs((double) (slope.p-slope.q)) < DrawEpsilon)
6165 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
6166 box_p[3].y)/(slope.p-slope.q));
6167 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
6168 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
6169 box_q[3].y)/(slope.p-slope.q));
6170 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
6172 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
6174 if (~max_strokes < (6*BezierQuantum+360))
6176 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6177 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6181 max_strokes+=6*BezierQuantum+360;
6182 path_p=(PointInfo *) ResizeQuantumMemory(path_p,max_strokes,
6184 path_q=(PointInfo *) ResizeQuantumMemory(path_q,max_strokes,
6187 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
6189 if (path_p != (PointInfo *) NULL)
6190 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6191 if (path_q != (PointInfo *) NULL)
6192 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6193 polygon_primitive=(PrimitiveInfo *)
6194 RelinquishMagickMemory(polygon_primitive);
6195 return((PrimitiveInfo *) NULL);
6198 dot_product=dx.q*dy.p-dx.p*dy.q;
6199 if (dot_product <= 0.0)
6200 switch (draw_info->linejoin)
6204 path_q[q++]=box_q[1];
6205 path_q[q++]=box_q[2];
6206 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6207 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6208 if (dot_product <= miterlimit)
6209 path_p[p++]=box_p[4];
6212 path_p[p++]=box_p[1];
6213 path_p[p++]=box_p[2];
6219 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6220 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6221 if (dot_product <= miterlimit)
6223 path_q[q++]=box_q[4];
6224 path_p[p++]=box_p[4];
6228 path_q[q++]=box_q[1];
6229 path_q[q++]=box_q[2];
6230 path_p[p++]=box_p[1];
6231 path_p[p++]=box_p[2];
6237 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6238 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6239 if (dot_product <= miterlimit)
6240 path_p[p++]=box_p[4];
6243 path_p[p++]=box_p[1];
6244 path_p[p++]=box_p[2];
6246 center=polygon_primitive[n].point;
6247 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6248 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6249 if (theta.q < theta.p)
6250 theta.q+=(double) (2.0*MagickPI);
6251 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6252 (2.0*sqrt((double) (1.0/mid)))));
6253 path_q[q].x=box_q[1].x;
6254 path_q[q].y=box_q[1].y;
6256 for (j=1; j < (ssize_t) arc_segments; j++)
6258 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6259 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6260 (theta.p+delta_theta),DegreesToRadians(360.0))));
6261 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6262 (theta.p+delta_theta),DegreesToRadians(360.0))));
6265 path_q[q++]=box_q[2];
6272 switch (draw_info->linejoin)
6276 path_p[p++]=box_p[1];
6277 path_p[p++]=box_p[2];
6278 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6279 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6280 if (dot_product <= miterlimit)
6281 path_q[q++]=box_q[4];
6284 path_q[q++]=box_q[1];
6285 path_q[q++]=box_q[2];
6291 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6292 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6293 if (dot_product <= miterlimit)
6295 path_q[q++]=box_q[4];
6296 path_p[p++]=box_p[4];
6300 path_q[q++]=box_q[1];
6301 path_q[q++]=box_q[2];
6302 path_p[p++]=box_p[1];
6303 path_p[p++]=box_p[2];
6309 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6310 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6311 if (dot_product <= miterlimit)
6312 path_q[q++]=box_q[4];
6315 path_q[q++]=box_q[1];
6316 path_q[q++]=box_q[2];
6318 center=polygon_primitive[n].point;
6319 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6320 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6321 if (theta.p < theta.q)
6322 theta.p+=(double) (2.0*MagickPI);
6323 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6324 (2.0*sqrt((double) (1.0/mid)))));
6325 path_p[p++]=box_p[1];
6326 for (j=1; j < (ssize_t) arc_segments; j++)
6328 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6329 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6330 (theta.p+delta_theta),DegreesToRadians(360.0))));
6331 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6332 (theta.p+delta_theta),DegreesToRadians(360.0))));
6335 path_p[p++]=box_p[2];
6342 inverse_slope.p=inverse_slope.q;
6351 path_p[p++]=box_p[1];
6352 path_q[q++]=box_q[1];
6354 Trace stroked polygon.
6356 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6357 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6358 if (stroke_polygon != (PrimitiveInfo *) NULL)
6360 for (i=0; i < (ssize_t) p; i++)
6362 stroke_polygon[i]=polygon_primitive[0];
6363 stroke_polygon[i].point=path_p[i];
6365 if (closed_path != MagickFalse)
6367 stroke_polygon[i]=polygon_primitive[0];
6368 stroke_polygon[i].point=stroke_polygon[0].point;
6371 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6373 stroke_polygon[i]=polygon_primitive[0];
6374 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6376 if (closed_path != MagickFalse)
6378 stroke_polygon[i]=polygon_primitive[0];
6379 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6382 stroke_polygon[i]=polygon_primitive[0];
6383 stroke_polygon[i].point=stroke_polygon[0].point;
6385 stroke_polygon[i].primitive=UndefinedPrimitive;
6386 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6388 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6389 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6390 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6391 return(stroke_polygon);