2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/channel.h"
55 #include "MagickCore/color.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/draw.h"
61 #include "MagickCore/draw-private.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/geometry.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/log.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/paint.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/pixel-private.h"
76 #include "MagickCore/property.h"
77 #include "MagickCore/resample.h"
78 #include "MagickCore/resample-private.h"
79 #include "MagickCore/resource_.h"
80 #include "MagickCore/string_.h"
81 #include "MagickCore/string-private.h"
82 #include "MagickCore/thread-private.h"
83 #include "MagickCore/token.h"
84 #include "MagickCore/transform.h"
85 #include "MagickCore/utility.h"
90 #define BezierQuantum 200
95 typedef struct _EdgeInfo
119 typedef struct _ElementInfo
129 typedef struct _PolygonInfo
147 typedef struct _PathInfo
157 Forward declarations.
159 static MagickBooleanType
160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
167 TracePath(PrimitiveInfo *,const char *);
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
172 const double,const MagickBooleanType,const MagickBooleanType),
173 TraceBezier(PrimitiveInfo *,const size_t),
174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
176 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
177 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
180 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % A c q u i r e D r a w I n f o %
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
195 % The format of the AcquireDrawInfo method is:
197 % DrawInfo *AcquireDrawInfo(void)
200 MagickExport DrawInfo *AcquireDrawInfo(void)
205 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
206 if (draw_info == (DrawInfo *) NULL)
207 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
208 GetDrawInfo((ImageInfo *) NULL,draw_info);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % C l o n e D r a w I n f o %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
224 % is specified, a new DrawInfo structure is created initialized to default
227 % The format of the CloneDrawInfo method is:
229 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
230 % const DrawInfo *draw_info)
232 % A description of each parameter follows:
234 % o image_info: the image info.
236 % o draw_info: the draw info.
239 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
240 const DrawInfo *draw_info)
248 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
249 if (clone_info == (DrawInfo *) NULL)
250 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
251 GetDrawInfo(image_info,clone_info);
252 if (draw_info == (DrawInfo *) NULL)
254 exception=AcquireExceptionInfo();
255 (void) CloneString(&clone_info->primitive,draw_info->primitive);
256 (void) CloneString(&clone_info->geometry,draw_info->geometry);
257 clone_info->viewbox=draw_info->viewbox;
258 clone_info->affine=draw_info->affine;
259 clone_info->gravity=draw_info->gravity;
260 clone_info->fill=draw_info->fill;
261 clone_info->stroke=draw_info->stroke;
262 clone_info->stroke_width=draw_info->stroke_width;
263 if (draw_info->fill_pattern != (Image *) NULL)
264 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
266 if (draw_info->stroke_pattern != (Image *) NULL)
267 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
268 MagickTrue,exception);
269 clone_info->stroke_antialias=draw_info->stroke_antialias;
270 clone_info->text_antialias=draw_info->text_antialias;
271 clone_info->fill_rule=draw_info->fill_rule;
272 clone_info->linecap=draw_info->linecap;
273 clone_info->linejoin=draw_info->linejoin;
274 clone_info->miterlimit=draw_info->miterlimit;
275 clone_info->dash_offset=draw_info->dash_offset;
276 clone_info->decorate=draw_info->decorate;
277 clone_info->compose=draw_info->compose;
278 (void) CloneString(&clone_info->text,draw_info->text);
279 (void) CloneString(&clone_info->font,draw_info->font);
280 (void) CloneString(&clone_info->metrics,draw_info->metrics);
281 (void) CloneString(&clone_info->family,draw_info->family);
282 clone_info->style=draw_info->style;
283 clone_info->stretch=draw_info->stretch;
284 clone_info->weight=draw_info->weight;
285 (void) CloneString(&clone_info->encoding,draw_info->encoding);
286 clone_info->pointsize=draw_info->pointsize;
287 clone_info->kerning=draw_info->kerning;
288 clone_info->interline_spacing=draw_info->interline_spacing;
289 clone_info->interword_spacing=draw_info->interword_spacing;
290 clone_info->direction=draw_info->direction;
291 (void) CloneString(&clone_info->density,draw_info->density);
292 clone_info->align=draw_info->align;
293 clone_info->undercolor=draw_info->undercolor;
294 clone_info->border_color=draw_info->border_color;
295 (void) CloneString(&clone_info->server_name,draw_info->server_name);
296 if (draw_info->dash_pattern != (double *) NULL)
301 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
302 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
303 sizeof(*clone_info->dash_pattern));
304 if (clone_info->dash_pattern == (double *) NULL)
305 ThrowFatalException(ResourceLimitFatalError,
306 "UnableToAllocateDashPattern");
307 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
308 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
310 clone_info->gradient=draw_info->gradient;
311 if (draw_info->gradient.stops != (StopInfo *) NULL)
316 number_stops=clone_info->gradient.number_stops;
317 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
318 number_stops,sizeof(*clone_info->gradient.stops));
319 if (clone_info->gradient.stops == (StopInfo *) NULL)
320 ThrowFatalException(ResourceLimitFatalError,
321 "UnableToAllocateDashPattern");
322 (void) CopyMagickMemory(clone_info->gradient.stops,
323 draw_info->gradient.stops,(size_t) number_stops*
324 sizeof(*clone_info->gradient.stops));
326 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
327 clone_info->bounds=draw_info->bounds;
328 clone_info->clip_units=draw_info->clip_units;
329 clone_info->render=draw_info->render;
330 clone_info->alpha=draw_info->alpha;
331 clone_info->element_reference=draw_info->element_reference;
332 clone_info->debug=IsEventLogging();
333 exception=DestroyExceptionInfo(exception);
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 + C o n v e r t P a t h T o P o l y g o n %
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % ConvertPathToPolygon() converts a path to the more efficient sorted
351 % The format of the ConvertPathToPolygon method is:
353 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
354 % const PathInfo *path_info)
356 % A description of each parameter follows:
358 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
359 % rendering form of type PolygonInfo.
361 % o draw_info: Specifies a pointer to an DrawInfo structure.
363 % o path_info: Specifies a pointer to an PathInfo structure.
368 #if defined(__cplusplus) || defined(c_plusplus)
372 static int CompareEdges(const void *x,const void *y)
374 register const EdgeInfo
381 p=(const EdgeInfo *) x;
382 q=(const EdgeInfo *) y;
383 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
385 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
387 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
389 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
391 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
392 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
397 #if defined(__cplusplus) || defined(c_plusplus)
401 static void LogPolygonInfo(const PolygonInfo *polygon_info)
410 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
411 p=polygon_info->edges;
412 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
414 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
416 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
417 p->direction != MagickFalse ? "down" : "up");
418 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
419 p->ghostline != MagickFalse ? "transparent" : "opaque");
420 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
421 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
422 p->bounds.x2,p->bounds.y2);
423 for (j=0; j < (ssize_t) p->number_points; j++)
424 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
425 p->points[j].x,p->points[j].y);
428 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
431 static void ReversePoints(PointInfo *points,const size_t number_points)
439 for (i=0; i < (ssize_t) (number_points >> 1); i++)
442 points[i]=points[number_points-(i+1)];
443 points[number_points-(i+1)]=point;
447 static PolygonInfo *ConvertPathToPolygon(
448 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
477 Convert a path to the more efficient sorted rendering form.
479 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
480 if (polygon_info == (PolygonInfo *) NULL)
481 return((PolygonInfo *) NULL);
483 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
484 sizeof(*polygon_info->edges));
485 if (polygon_info->edges == (EdgeInfo *) NULL)
486 return((PolygonInfo *) NULL);
489 ghostline=MagickFalse;
492 points=(PointInfo *) NULL;
493 (void) ResetMagickMemory(&point,0,sizeof(point));
494 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
495 for (i=0; path_info[i].code != EndCode; i++)
497 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
498 (path_info[i].code == GhostlineCode))
503 if ((points != (PointInfo *) NULL) && (n >= 2))
505 if (edge == number_edges)
508 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
509 polygon_info->edges,(size_t) number_edges,
510 sizeof(*polygon_info->edges));
511 if (polygon_info->edges == (EdgeInfo *) NULL)
512 return((PolygonInfo *) NULL);
514 polygon_info->edges[edge].number_points=(size_t) n;
515 polygon_info->edges[edge].scanline=(-1.0);
516 polygon_info->edges[edge].highwater=0;
517 polygon_info->edges[edge].ghostline=ghostline;
518 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
520 ReversePoints(points,(size_t) n);
521 polygon_info->edges[edge].points=points;
522 polygon_info->edges[edge].bounds=bounds;
523 polygon_info->edges[edge].bounds.y1=points[0].y;
524 polygon_info->edges[edge].bounds.y2=points[n-1].y;
525 points=(PointInfo *) NULL;
526 ghostline=MagickFalse;
529 if (points == (PointInfo *) NULL)
532 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
534 if (points == (PointInfo *) NULL)
535 return((PolygonInfo *) NULL);
537 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
538 point=path_info[i].point;
549 next_direction=((path_info[i].point.y > point.y) ||
550 ((path_info[i].point.y == point.y) &&
551 (path_info[i].point.x > point.x))) ? 1 : -1;
552 if ((points != (PointInfo *) NULL) && (direction != 0) &&
553 (direction != next_direction))
559 if (edge == number_edges)
562 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
563 polygon_info->edges,(size_t) number_edges,
564 sizeof(*polygon_info->edges));
565 if (polygon_info->edges == (EdgeInfo *) NULL)
566 return((PolygonInfo *) NULL);
568 polygon_info->edges[edge].number_points=(size_t) n;
569 polygon_info->edges[edge].scanline=(-1.0);
570 polygon_info->edges[edge].highwater=0;
571 polygon_info->edges[edge].ghostline=ghostline;
572 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
574 ReversePoints(points,(size_t) n);
575 polygon_info->edges[edge].points=points;
576 polygon_info->edges[edge].bounds=bounds;
577 polygon_info->edges[edge].bounds.y1=points[0].y;
578 polygon_info->edges[edge].bounds.y2=points[n-1].y;
580 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
582 if (points == (PointInfo *) NULL)
583 return((PolygonInfo *) NULL);
585 ghostline=MagickFalse;
591 direction=next_direction;
592 if (points == (PointInfo *) NULL)
594 if (n == (ssize_t) number_points)
597 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
599 if (points == (PointInfo *) NULL)
600 return((PolygonInfo *) NULL);
602 point=path_info[i].point;
604 if (point.x < bounds.x1)
606 if (point.x > bounds.x2)
610 if (points != (PointInfo *) NULL)
613 points=(PointInfo *) RelinquishMagickMemory(points);
616 if (edge == number_edges)
619 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
620 polygon_info->edges,(size_t) number_edges,
621 sizeof(*polygon_info->edges));
622 if (polygon_info->edges == (EdgeInfo *) NULL)
623 return((PolygonInfo *) NULL);
625 polygon_info->edges[edge].number_points=(size_t) n;
626 polygon_info->edges[edge].scanline=(-1.0);
627 polygon_info->edges[edge].highwater=0;
628 polygon_info->edges[edge].ghostline=ghostline;
629 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
631 ReversePoints(points,(size_t) n);
632 polygon_info->edges[edge].points=points;
633 polygon_info->edges[edge].bounds=bounds;
634 polygon_info->edges[edge].bounds.y1=points[0].y;
635 polygon_info->edges[edge].bounds.y2=points[n-1].y;
636 ghostline=MagickFalse;
640 polygon_info->number_edges=edge;
641 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
642 sizeof(*polygon_info->edges),CompareEdges);
643 if (IsEventLogging() != MagickFalse)
644 LogPolygonInfo(polygon_info);
645 return(polygon_info);
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 + C o n v e r t P r i m i t i v e T o P a t h %
657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
662 % The format of the ConvertPrimitiveToPath method is:
664 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
665 % const PrimitiveInfo *primitive_info)
667 % A description of each parameter follows:
669 % o Method ConvertPrimitiveToPath returns a vector path structure of type
672 % o draw_info: a structure of type DrawInfo.
674 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
679 static void LogPathInfo(const PathInfo *path_info)
681 register const PathInfo
684 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
685 for (p=path_info; p->code != EndCode; p++)
686 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
687 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
688 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
689 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
691 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
694 static PathInfo *ConvertPrimitiveToPath(
695 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
716 Converts a PrimitiveInfo structure into a vector path structure.
718 switch (primitive_info->primitive)
725 return((PathInfo *) NULL);
729 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
730 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
732 if (path_info == (PathInfo *) NULL)
733 return((PathInfo *) NULL);
741 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
744 if (coordinates <= 0)
746 coordinates=(ssize_t) primitive_info[i].coordinates;
747 p=primitive_info[i].point;
753 Eliminate duplicate points.
755 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
756 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
758 path_info[n].code=code;
759 path_info[n].point=primitive_info[i].point;
760 q=primitive_info[i].point;
765 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
766 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
769 Mark the p point as open if it does not match the q.
771 path_info[start].code=OpenCode;
772 path_info[n].code=GhostlineCode;
773 path_info[n].point=primitive_info[i].point;
775 path_info[n].code=LineToCode;
776 path_info[n].point=p;
779 path_info[n].code=EndCode;
780 path_info[n].point.x=0.0;
781 path_info[n].point.y=0.0;
782 if (IsEventLogging() != MagickFalse)
783 LogPathInfo(path_info);
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 % D e s t r o y D r a w I n f o %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
801 % The format of the DestroyDrawInfo method is:
803 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
805 % A description of each parameter follows:
807 % o draw_info: the draw info.
810 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
812 if (draw_info->debug != MagickFalse)
813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
814 assert(draw_info != (DrawInfo *) NULL);
815 assert(draw_info->signature == MagickSignature);
816 if (draw_info->primitive != (char *) NULL)
817 draw_info->primitive=DestroyString(draw_info->primitive);
818 if (draw_info->text != (char *) NULL)
819 draw_info->text=DestroyString(draw_info->text);
820 if (draw_info->geometry != (char *) NULL)
821 draw_info->geometry=DestroyString(draw_info->geometry);
822 if (draw_info->fill_pattern != (Image *) NULL)
823 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
824 if (draw_info->stroke_pattern != (Image *) NULL)
825 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
826 if (draw_info->font != (char *) NULL)
827 draw_info->font=DestroyString(draw_info->font);
828 if (draw_info->metrics != (char *) NULL)
829 draw_info->metrics=DestroyString(draw_info->metrics);
830 if (draw_info->family != (char *) NULL)
831 draw_info->family=DestroyString(draw_info->family);
832 if (draw_info->encoding != (char *) NULL)
833 draw_info->encoding=DestroyString(draw_info->encoding);
834 if (draw_info->density != (char *) NULL)
835 draw_info->density=DestroyString(draw_info->density);
836 if (draw_info->server_name != (char *) NULL)
837 draw_info->server_name=(char *)
838 RelinquishMagickMemory(draw_info->server_name);
839 if (draw_info->dash_pattern != (double *) NULL)
840 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
841 draw_info->dash_pattern);
842 if (draw_info->gradient.stops != (StopInfo *) NULL)
843 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
844 draw_info->gradient.stops);
845 if (draw_info->clip_mask != (char *) NULL)
846 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
847 draw_info->signature=(~MagickSignature);
848 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857 + D e s t r o y E d g e %
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 % DestroyEdge() destroys the specified polygon edge.
865 % The format of the DestroyEdge method is:
867 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
869 % A description of each parameter follows:
871 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
873 % o edge: the polygon edge number to destroy.
876 static size_t DestroyEdge(PolygonInfo *polygon_info,
879 assert(edge < polygon_info->number_edges);
880 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
881 polygon_info->edges[edge].points);
882 polygon_info->number_edges--;
883 if (edge < polygon_info->number_edges)
884 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
885 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
886 return(polygon_info->number_edges);
890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894 + D e s t r o y P o l y g o n I n f o %
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
900 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
902 % The format of the DestroyPolygonInfo method is:
904 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
906 % A description of each parameter follows:
908 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
911 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
916 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
917 polygon_info->edges[i].points=(PointInfo *)
918 RelinquishMagickMemory(polygon_info->edges[i].points);
919 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
920 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
924 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
928 % D r a w A f f i n e I m a g e %
932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934 % DrawAffineImage() composites the source over the destination image as
935 % dictated by the affine transform.
937 % The format of the DrawAffineImage method is:
939 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
940 % const AffineMatrix *affine,ExceptionInfo *exception)
942 % A description of each parameter follows:
944 % o image: the image.
946 % o source: the source image.
948 % o affine: the affine transform.
950 % o exception: return any errors or warnings in this structure.
954 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
955 const double y,const SegmentInfo *edge)
968 Determine left and right edges.
970 inverse_edge.x1=edge->x1;
971 inverse_edge.y1=edge->y1;
972 inverse_edge.x2=edge->x2;
973 inverse_edge.y2=edge->y2;
974 z=affine->ry*y+affine->tx;
975 if (affine->sx >= MagickEpsilon)
977 intercept=(-z/affine->sx);
979 if (x > inverse_edge.x1)
981 intercept=(-z+(double) image->columns)/affine->sx;
983 if (x < inverse_edge.x2)
987 if (affine->sx < -MagickEpsilon)
989 intercept=(-z+(double) image->columns)/affine->sx;
991 if (x > inverse_edge.x1)
993 intercept=(-z/affine->sx);
995 if (x < inverse_edge.x2)
999 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1001 inverse_edge.x2=edge->x1;
1002 return(inverse_edge);
1005 Determine top and bottom edges.
1007 z=affine->sy*y+affine->ty;
1008 if (affine->rx >= MagickEpsilon)
1010 intercept=(-z/affine->rx);
1012 if (x > inverse_edge.x1)
1014 intercept=(-z+(double) image->rows)/affine->rx;
1016 if (x < inverse_edge.x2)
1020 if (affine->rx < -MagickEpsilon)
1022 intercept=(-z+(double) image->rows)/affine->rx;
1024 if (x > inverse_edge.x1)
1026 intercept=(-z/affine->rx);
1028 if (x < inverse_edge.x2)
1032 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1034 inverse_edge.x2=edge->x2;
1035 return(inverse_edge);
1037 return(inverse_edge);
1040 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1048 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1050 inverse_affine.sx=determinant*affine->sy;
1051 inverse_affine.rx=determinant*(-affine->rx);
1052 inverse_affine.ry=determinant*(-affine->ry);
1053 inverse_affine.sy=determinant*affine->sx;
1054 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1056 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1058 return(inverse_affine);
1061 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1068 static inline double MagickMax(const double x,const double y)
1075 static inline double MagickMin(const double x,const double y)
1082 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1083 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1116 Determine bounding box.
1118 assert(image != (Image *) NULL);
1119 assert(image->signature == MagickSignature);
1120 if (image->debug != MagickFalse)
1121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1122 assert(source != (const Image *) NULL);
1123 assert(source->signature == MagickSignature);
1124 assert(affine != (AffineMatrix *) NULL);
1127 extent[1].x=(double) source->columns-1.0;
1129 extent[2].x=(double) source->columns-1.0;
1130 extent[2].y=(double) source->rows-1.0;
1132 extent[3].y=(double) source->rows-1.0;
1133 for (i=0; i < 4; i++)
1136 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1137 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1141 for (i=1; i < 4; i++)
1143 if (min.x > extent[i].x)
1145 if (min.y > extent[i].y)
1147 if (max.x < extent[i].x)
1149 if (max.y < extent[i].y)
1153 Affine transform image.
1155 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1156 return(MagickFalse);
1158 edge.x1=MagickMax(min.x,0.0);
1159 edge.y1=MagickMax(min.y,0.0);
1160 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1161 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1162 inverse_affine=InverseAffineMatrix(affine);
1163 GetPixelInfo(image,&zero);
1164 start=(ssize_t) ceil(edge.y1-0.5);
1165 stop=(ssize_t) floor(edge.y2+0.5);
1166 source_view=AcquireVirtualCacheView(source,exception);
1167 image_view=AcquireAuthenticCacheView(image,exception);
1168 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1169 #pragma omp parallel for schedule(static,4) shared(status) \
1170 magick_threads(source,image,1,1)
1172 for (y=start; y <= stop; y++)
1193 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1194 if (inverse_edge.x2 < inverse_edge.x1)
1196 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1197 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1199 if (q == (Quantum *) NULL)
1204 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1206 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1208 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1210 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1211 point.x,point.y,&pixel,exception);
1212 GetPixelInfoPixel(image,q,&composite);
1213 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1215 SetPixelInfoPixel(image,&composite,q);
1217 q+=GetPixelChannels(image);
1219 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1222 source_view=DestroyCacheView(source_view);
1223 image_view=DestroyCacheView(image_view);
1228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 + D r a w B o u n d i n g R e c t a n g l e s %
1236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1238 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1239 % is only useful for developers debugging the rendering algorithm.
1241 % The format of the DrawBoundingRectangles method is:
1243 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1244 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1246 % A description of each parameter follows:
1248 % o image: the image.
1250 % o draw_info: the draw info.
1252 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1254 % o exception: return any errors or warnings in this structure.
1257 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1258 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1283 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1284 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1286 resolution.x=DefaultResolution;
1287 resolution.y=DefaultResolution;
1288 if (clone_info->density != (char *) NULL)
1296 flags=ParseGeometry(clone_info->density,&geometry_info);
1297 resolution.x=geometry_info.rho;
1298 resolution.y=geometry_info.sigma;
1299 if ((flags & SigmaValue) == MagickFalse)
1300 resolution.y=resolution.x;
1302 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1303 clone_info->stroke_width/2.0;
1308 if (polygon_info != (PolygonInfo *) NULL)
1310 bounds=polygon_info->edges[0].bounds;
1311 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1313 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1314 bounds.x1=polygon_info->edges[i].bounds.x1;
1315 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1316 bounds.y1=polygon_info->edges[i].bounds.y1;
1317 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1318 bounds.x2=polygon_info->edges[i].bounds.x2;
1319 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1320 bounds.y2=polygon_info->edges[i].bounds.y2;
1323 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1324 image->columns ? (double) image->columns-1 : bounds.x1;
1326 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1327 image->rows ? (double) image->rows-1 : bounds.y1;
1329 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1330 image->columns ? (double) image->columns-1 : bounds.x2;
1332 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1333 image->rows ? (double) image->rows-1 : bounds.y2;
1334 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1336 if (polygon_info->edges[i].direction != 0)
1337 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1340 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1342 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1343 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1344 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1345 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1346 primitive_info[0].primitive=RectanglePrimitive;
1347 TraceRectangle(primitive_info,start,end);
1348 primitive_info[0].method=ReplaceMethod;
1349 coordinates=(ssize_t) primitive_info[0].coordinates;
1350 primitive_info[coordinates].primitive=UndefinedPrimitive;
1351 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1354 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1356 start.x=(double) (bounds.x1-mid);
1357 start.y=(double) (bounds.y1-mid);
1358 end.x=(double) (bounds.x2+mid);
1359 end.y=(double) (bounds.y2+mid);
1360 primitive_info[0].primitive=RectanglePrimitive;
1361 TraceRectangle(primitive_info,start,end);
1362 primitive_info[0].method=ReplaceMethod;
1363 coordinates=(ssize_t) primitive_info[0].coordinates;
1364 primitive_info[coordinates].primitive=UndefinedPrimitive;
1365 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1366 clone_info=DestroyDrawInfo(clone_info);
1370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % D r a w C l i p P a t h %
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % DrawClipPath() draws the clip path on the image mask.
1382 % The format of the DrawClipPath method is:
1384 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1385 % const char *name,ExceptionInfo *exception)
1387 % A description of each parameter follows:
1389 % o image: the image.
1391 % o draw_info: the draw info.
1393 % o name: the name of the clip path.
1395 % o exception: return any errors or warnings in this structure.
1398 MagickExport MagickBooleanType DrawClipPath(Image *image,
1399 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1402 filename[MaxTextExtent];
1416 assert(image != (Image *) NULL);
1417 assert(image->signature == MagickSignature);
1418 if (image->debug != MagickFalse)
1419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1420 assert(draw_info != (const DrawInfo *) NULL);
1421 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1422 value=GetImageArtifact(image,filename);
1423 if (value == (const char *) NULL)
1424 return(MagickFalse);
1425 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1426 if (clip_mask == (Image *) NULL)
1427 return(MagickFalse);
1428 (void) QueryColorCompliance("#0000",AllCompliance,
1429 &clip_mask->background_color,exception);
1430 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1431 (void) SetImageBackgroundColor(clip_mask,exception);
1432 if (image->debug != MagickFalse)
1433 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1434 draw_info->clip_mask);
1435 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1436 (void) CloneString(&clone_info->primitive,value);
1437 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1439 clone_info->clip_mask=(char *) NULL;
1440 status=NegateImage(clip_mask,MagickFalse,exception);
1441 (void) SetImageMask(image,clip_mask,exception);
1442 clip_mask=DestroyImage(clip_mask);
1443 status&=DrawImage(image,clone_info,exception);
1444 clone_info=DestroyDrawInfo(clone_info);
1445 if (image->debug != MagickFalse)
1446 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1447 return(status != 0 ? MagickTrue : MagickFalse);
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1455 + D r a w D a s h P o l y g o n %
1459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1462 % image while respecting the dash offset and dash pattern attributes.
1464 % The format of the DrawDashPolygon method is:
1466 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1467 % const PrimitiveInfo *primitive_info,Image *image,
1468 % ExceptionInfo *exception)
1470 % A description of each parameter follows:
1472 % o draw_info: the draw info.
1474 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1476 % o image: the image.
1478 % o exception: return any errors or warnings in this structure.
1481 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1482 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1514 assert(draw_info != (const DrawInfo *) NULL);
1515 if (image->debug != MagickFalse)
1516 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1517 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1518 clone_info->miterlimit=0;
1519 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1520 number_vertices=(size_t) i;
1521 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1522 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1523 if (dash_polygon == (PrimitiveInfo *) NULL)
1524 return(MagickFalse);
1525 dash_polygon[0]=primitive_info[0];
1526 scale=ExpandAffine(&draw_info->affine);
1527 length=scale*(draw_info->dash_pattern[0]-0.5);
1528 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1530 for (n=0; offset > 0.0; j=0)
1532 if (draw_info->dash_pattern[n] <= 0.0)
1534 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1535 if (offset > length)
1539 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1542 if (offset < length)
1554 for (i=1; i < (ssize_t) number_vertices; i++)
1556 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1557 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1558 maximum_length=hypot((double) dx,dy);
1562 if (draw_info->dash_pattern[n] == 0.0)
1564 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1566 for (total_length=0.0; (total_length+length) <= maximum_length; )
1568 total_length+=length;
1569 if ((n & 0x01) != 0)
1571 dash_polygon[0]=primitive_info[0];
1572 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1573 total_length/maximum_length);
1574 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1575 total_length/maximum_length);
1580 if ((j+1) > (ssize_t) (2*number_vertices))
1582 dash_polygon[j]=primitive_info[i-1];
1583 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1584 total_length/maximum_length);
1585 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1586 total_length/maximum_length);
1587 dash_polygon[j].coordinates=1;
1589 dash_polygon[0].coordinates=(size_t) j;
1590 dash_polygon[j].primitive=UndefinedPrimitive;
1591 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1594 if (draw_info->dash_pattern[n] == 0.0)
1596 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1598 length-=(maximum_length-total_length);
1599 if ((n & 0x01) != 0)
1601 dash_polygon[j]=primitive_info[i];
1602 dash_polygon[j].coordinates=1;
1605 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1607 dash_polygon[j]=primitive_info[i-1];
1608 dash_polygon[j].point.x+=MagickEpsilon;
1609 dash_polygon[j].point.y+=MagickEpsilon;
1610 dash_polygon[j].coordinates=1;
1612 dash_polygon[0].coordinates=(size_t) j;
1613 dash_polygon[j].primitive=UndefinedPrimitive;
1614 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1616 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1617 clone_info=DestroyDrawInfo(clone_info);
1618 if (image->debug != MagickFalse)
1619 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1620 return(status != 0 ? MagickTrue : MagickFalse);
1624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 % D r a w I m a g e %
1632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1634 % DrawImage() draws a graphic primitive on your image. The primitive
1635 % may be represented as a string or filename. Precede the filename with an
1636 % "at" sign (@) and the contents of the file are drawn on the image. You
1637 % can affect how text is drawn by setting one or more members of the draw
1640 % The format of the DrawImage method is:
1642 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1643 % ExceptionInfo *exception)
1645 % A description of each parameter follows:
1647 % o image: the image.
1649 % o draw_info: the draw info.
1651 % o exception: return any errors or warnings in this structure.
1655 static inline MagickBooleanType IsPoint(const char *point)
1663 value=StringToDouble(point,&p);
1664 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1667 static inline void TracePoint(PrimitiveInfo *primitive_info,
1668 const PointInfo point)
1670 primitive_info->coordinates=1;
1671 primitive_info->point=point;
1674 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1675 ExceptionInfo *exception)
1677 #define RenderImageTag "Render/Image"
1684 key[2*MaxTextExtent],
1685 keyword[MaxTextExtent],
1686 geometry[MaxTextExtent],
1687 name[MaxTextExtent],
1688 pattern[MaxTextExtent],
1741 Ensure the annotation info is valid.
1743 assert(image != (Image *) NULL);
1744 assert(image->signature == MagickSignature);
1745 if (image->debug != MagickFalse)
1746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1747 assert(draw_info != (DrawInfo *) NULL);
1748 assert(draw_info->signature == MagickSignature);
1749 if (image->debug != MagickFalse)
1750 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1751 if ((draw_info->primitive == (char *) NULL) ||
1752 (*draw_info->primitive == '\0'))
1753 return(MagickFalse);
1754 if (image->debug != MagickFalse)
1755 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1756 if (*draw_info->primitive != '@')
1757 primitive=AcquireString(draw_info->primitive);
1759 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1760 if (primitive == (char *) NULL)
1761 return(MagickFalse);
1762 primitive_extent=(double) strlen(primitive);
1763 (void) SetImageArtifact(image,"MVG",primitive);
1766 Allocate primitive info memory.
1768 graphic_context=(DrawInfo **) AcquireMagickMemory(
1769 sizeof(*graphic_context));
1770 if (graphic_context == (DrawInfo **) NULL)
1772 primitive=DestroyString(primitive);
1773 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1777 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1778 sizeof(*primitive_info));
1779 if (primitive_info == (PrimitiveInfo *) NULL)
1781 primitive=DestroyString(primitive);
1782 for ( ; n >= 0; n--)
1783 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1784 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1785 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1788 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1789 graphic_context[n]->viewbox=image->page;
1790 if ((image->page.width == 0) || (image->page.height == 0))
1792 graphic_context[n]->viewbox.width=image->columns;
1793 graphic_context[n]->viewbox.height=image->rows;
1795 token=AcquireString(primitive);
1796 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1798 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1799 return(MagickFalse);
1801 for (q=primitive; *q != '\0'; )
1804 Interpret graphic primitive.
1806 GetMagickToken(q,&q,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 GetMagickToken(q,&q,token);
1832 affine.sx=StringToDouble(token,(char **) NULL);
1833 GetMagickToken(q,&q,token);
1835 GetMagickToken(q,&q,token);
1836 affine.rx=StringToDouble(token,(char **) NULL);
1837 GetMagickToken(q,&q,token);
1839 GetMagickToken(q,&q,token);
1840 affine.ry=StringToDouble(token,(char **) NULL);
1841 GetMagickToken(q,&q,token);
1843 GetMagickToken(q,&q,token);
1844 affine.sy=StringToDouble(token,(char **) NULL);
1845 GetMagickToken(q,&q,token);
1847 GetMagickToken(q,&q,token);
1848 affine.tx=StringToDouble(token,(char **) NULL);
1849 GetMagickToken(q,&q,token);
1851 GetMagickToken(q,&q,token);
1852 affine.ty=StringToDouble(token,(char **) NULL);
1855 if (LocaleCompare("arc",keyword) == 0)
1857 primitive_type=ArcPrimitive;
1866 if (LocaleCompare("bezier",keyword) == 0)
1868 primitive_type=BezierPrimitive;
1871 if (LocaleCompare("border-color",keyword) == 0)
1873 GetMagickToken(q,&q,token);
1874 (void) QueryColorCompliance(token,AllCompliance,
1875 &graphic_context[n]->border_color,exception);
1884 if (LocaleCompare("clip-path",keyword) == 0)
1889 GetMagickToken(q,&q,token);
1890 (void) CloneString(&graphic_context[n]->clip_mask,token);
1891 (void) DrawClipPath(image,graphic_context[n],
1892 graphic_context[n]->clip_mask,exception);
1895 if (LocaleCompare("clip-rule",keyword) == 0)
1900 GetMagickToken(q,&q,token);
1901 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1903 if (fill_rule == -1)
1906 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1909 if (LocaleCompare("clip-units",keyword) == 0)
1914 GetMagickToken(q,&q,token);
1915 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1917 if (clip_units == -1)
1922 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1923 if (clip_units == ObjectBoundingBox)
1925 GetAffineMatrix(¤t);
1926 affine.sx=draw_info->bounds.x2;
1927 affine.sy=draw_info->bounds.y2;
1928 affine.tx=draw_info->bounds.x1;
1929 affine.ty=draw_info->bounds.y1;
1934 if (LocaleCompare("circle",keyword) == 0)
1936 primitive_type=CirclePrimitive;
1939 if (LocaleCompare("color",keyword) == 0)
1941 primitive_type=ColorPrimitive;
1950 if (LocaleCompare("decorate",keyword) == 0)
1955 GetMagickToken(q,&q,token);
1956 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1961 graphic_context[n]->decorate=(DecorationType) decorate;
1970 if (LocaleCompare("ellipse",keyword) == 0)
1972 primitive_type=EllipsePrimitive;
1975 if (LocaleCompare("encoding",keyword) == 0)
1977 GetMagickToken(q,&q,token);
1978 (void) CloneString(&graphic_context[n]->encoding,token);
1987 if (LocaleCompare("fill",keyword) == 0)
1989 GetMagickToken(q,&q,token);
1990 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1991 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1992 (void) DrawPatternPath(image,draw_info,token,
1993 &graphic_context[n]->fill_pattern,exception);
1996 status&=QueryColorCompliance(token,AllCompliance,
1997 &graphic_context[n]->fill,exception);
1998 if (status == MagickFalse)
2003 pattern_info=AcquireImageInfo();
2004 (void) CopyMagickString(pattern_info->filename,token,
2006 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2008 CatchException(exception);
2009 pattern_info=DestroyImageInfo(pattern_info);
2014 if (LocaleCompare("fill-alpha",keyword) == 0)
2016 GetMagickToken(q,&q,token);
2017 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2018 graphic_context[n]->fill.alpha=(double) QuantumRange*
2019 factor*StringToDouble(token,(char **) NULL);
2022 if (LocaleCompare("fill-rule",keyword) == 0)
2027 GetMagickToken(q,&q,token);
2028 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2030 if (fill_rule == -1)
2033 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2036 if (LocaleCompare("font",keyword) == 0)
2038 GetMagickToken(q,&q,token);
2039 (void) CloneString(&graphic_context[n]->font,token);
2040 if (LocaleCompare("none",token) == 0)
2041 graphic_context[n]->font=(char *)
2042 RelinquishMagickMemory(graphic_context[n]->font);
2045 if (LocaleCompare("font-family",keyword) == 0)
2047 GetMagickToken(q,&q,token);
2048 (void) CloneString(&graphic_context[n]->family,token);
2051 if (LocaleCompare("font-size",keyword) == 0)
2053 GetMagickToken(q,&q,token);
2054 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2057 if (LocaleCompare("font-stretch",keyword) == 0)
2062 GetMagickToken(q,&q,token);
2063 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2067 graphic_context[n]->stretch=(StretchType) stretch;
2070 if (LocaleCompare("font-style",keyword) == 0)
2075 GetMagickToken(q,&q,token);
2076 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2080 graphic_context[n]->style=(StyleType) style;
2083 if (LocaleCompare("font-weight",keyword) == 0)
2085 GetMagickToken(q,&q,token);
2086 graphic_context[n]->weight=StringToUnsignedLong(token);
2087 if (LocaleCompare(token,"all") == 0)
2088 graphic_context[n]->weight=0;
2089 if (LocaleCompare(token,"bold") == 0)
2090 graphic_context[n]->weight=700;
2091 if (LocaleCompare(token,"bolder") == 0)
2092 if (graphic_context[n]->weight <= 800)
2093 graphic_context[n]->weight+=100;
2094 if (LocaleCompare(token,"lighter") == 0)
2095 if (graphic_context[n]->weight >= 100)
2096 graphic_context[n]->weight-=100;
2097 if (LocaleCompare(token,"normal") == 0)
2098 graphic_context[n]->weight=400;
2107 if (LocaleCompare("gradient-units",keyword) == 0)
2109 GetMagickToken(q,&q,token);
2112 if (LocaleCompare("gravity",keyword) == 0)
2117 GetMagickToken(q,&q,token);
2118 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2122 graphic_context[n]->gravity=(GravityType) gravity;
2131 if (LocaleCompare("image",keyword) == 0)
2136 primitive_type=ImagePrimitive;
2137 GetMagickToken(q,&q,token);
2138 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2142 graphic_context[n]->compose=(CompositeOperator) compose;
2145 if (LocaleCompare("interline-spacing",keyword) == 0)
2147 GetMagickToken(q,&q,token);
2148 graphic_context[n]->interline_spacing=StringToDouble(token,
2152 if (LocaleCompare("interword-spacing",keyword) == 0)
2154 GetMagickToken(q,&q,token);
2155 graphic_context[n]->interword_spacing=StringToDouble(token,
2165 if (LocaleCompare("kerning",keyword) == 0)
2167 GetMagickToken(q,&q,token);
2168 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2177 if (LocaleCompare("line",keyword) == 0)
2178 primitive_type=LinePrimitive;
2186 if (LocaleCompare("matte",keyword) == 0)
2187 primitive_type=MattePrimitive;
2195 if (LocaleCompare("offset",keyword) == 0)
2197 GetMagickToken(q,&q,token);
2200 if (LocaleCompare("opacity",keyword) == 0)
2202 GetMagickToken(q,&q,token);
2203 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2204 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2205 QuantumScale*graphic_context[n]->alpha)*factor*
2206 StringToDouble(token,(char **) NULL))));
2207 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2208 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2217 if (LocaleCompare("path",keyword) == 0)
2219 primitive_type=PathPrimitive;
2222 if (LocaleCompare("point",keyword) == 0)
2224 primitive_type=PointPrimitive;
2227 if (LocaleCompare("polyline",keyword) == 0)
2229 primitive_type=PolylinePrimitive;
2232 if (LocaleCompare("polygon",keyword) == 0)
2234 primitive_type=PolygonPrimitive;
2237 if (LocaleCompare("pop",keyword) == 0)
2239 GetMagickToken(q,&q,token);
2240 if (LocaleCompare("clip-path",token) == 0)
2242 if (LocaleCompare("defs",token) == 0)
2244 if (LocaleCompare("gradient",token) == 0)
2246 if (LocaleCompare("graphic-context",token) == 0)
2250 (void) ThrowMagickException(exception,GetMagickModule(),
2251 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2255 if (graphic_context[n]->clip_mask != (char *) NULL)
2256 if (LocaleCompare(graphic_context[n]->clip_mask,
2257 graphic_context[n-1]->clip_mask) != 0)
2258 (void) SetImageMask(image,(Image *) NULL,exception);
2259 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2263 if (LocaleCompare("pattern",token) == 0)
2268 if (LocaleCompare("push",keyword) == 0)
2270 GetMagickToken(q,&q,token);
2271 if (LocaleCompare("clip-path",token) == 0)
2274 name[MaxTextExtent];
2276 GetMagickToken(q,&q,token);
2277 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2278 for (p=q; *q != '\0'; )
2280 GetMagickToken(q,&q,token);
2281 if (LocaleCompare(token,"pop") != 0)
2283 GetMagickToken(q,(const char **) NULL,token);
2284 if (LocaleCompare(token,"clip-path") != 0)
2288 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2289 (void) SetImageArtifact(image,name,token);
2290 GetMagickToken(q,&q,token);
2293 if (LocaleCompare("gradient",token) == 0)
2296 key[2*MaxTextExtent],
2297 name[MaxTextExtent],
2298 type[MaxTextExtent];
2303 GetMagickToken(q,&q,token);
2304 (void) CopyMagickString(name,token,MaxTextExtent);
2305 GetMagickToken(q,&q,token);
2306 (void) CopyMagickString(type,token,MaxTextExtent);
2307 GetMagickToken(q,&q,token);
2308 segment.x1=StringToDouble(token,(char **) NULL);
2309 GetMagickToken(q,&q,token);
2311 GetMagickToken(q,&q,token);
2312 segment.y1=StringToDouble(token,(char **) NULL);
2313 GetMagickToken(q,&q,token);
2315 GetMagickToken(q,&q,token);
2316 segment.x2=StringToDouble(token,(char **) NULL);
2317 GetMagickToken(q,&q,token);
2319 GetMagickToken(q,&q,token);
2320 segment.y2=StringToDouble(token,(char **) NULL);
2321 if (LocaleCompare(type,"radial") == 0)
2323 GetMagickToken(q,&q,token);
2325 GetMagickToken(q,&q,token);
2327 for (p=q; *q != '\0'; )
2329 GetMagickToken(q,&q,token);
2330 if (LocaleCompare(token,"pop") != 0)
2332 GetMagickToken(q,(const char **) NULL,token);
2333 if (LocaleCompare(token,"gradient") != 0)
2337 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2338 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2339 graphic_context[n]->affine.ry*segment.y1+
2340 graphic_context[n]->affine.tx;
2341 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2342 graphic_context[n]->affine.sy*segment.y1+
2343 graphic_context[n]->affine.ty;
2344 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2345 graphic_context[n]->affine.ry*segment.y2+
2346 graphic_context[n]->affine.tx;
2347 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2348 graphic_context[n]->affine.sy*segment.y2+
2349 graphic_context[n]->affine.ty;
2350 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2351 (void) SetImageArtifact(image,key,token);
2352 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2353 (void) FormatLocaleString(geometry,MaxTextExtent,
2354 "%gx%g%+.15g%+.15g",
2355 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2356 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2357 bounds.x1,bounds.y1);
2358 (void) SetImageArtifact(image,key,geometry);
2359 GetMagickToken(q,&q,token);
2362 if (LocaleCompare("pattern",token) == 0)
2367 GetMagickToken(q,&q,token);
2368 (void) CopyMagickString(name,token,MaxTextExtent);
2369 GetMagickToken(q,&q,token);
2370 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2372 GetMagickToken(q,&q,token);
2374 GetMagickToken(q,&q,token);
2375 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2377 GetMagickToken(q,&q,token);
2379 GetMagickToken(q,&q,token);
2380 bounds.width=(size_t) floor(StringToDouble(token,
2381 (char **) NULL)+0.5);
2382 GetMagickToken(q,&q,token);
2384 GetMagickToken(q,&q,token);
2385 bounds.height=(size_t) floor(StringToDouble(token,
2386 (char **) NULL)+0.5);
2387 for (p=q; *q != '\0'; )
2389 GetMagickToken(q,&q,token);
2390 if (LocaleCompare(token,"pop") != 0)
2392 GetMagickToken(q,(const char **) NULL,token);
2393 if (LocaleCompare(token,"pattern") != 0)
2397 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2398 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2399 (void) SetImageArtifact(image,key,token);
2400 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2401 (void) FormatLocaleString(geometry,MaxTextExtent,
2402 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2403 bounds.height,(double) bounds.x,(double) bounds.y);
2404 (void) SetImageArtifact(image,key,geometry);
2405 GetMagickToken(q,&q,token);
2408 if (LocaleCompare("graphic-context",token) == 0)
2411 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2412 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2413 if (graphic_context == (DrawInfo **) NULL)
2415 (void) ThrowMagickException(exception,GetMagickModule(),
2416 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2420 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2421 graphic_context[n-1]);
2424 if (LocaleCompare("defs",token) == 0)
2435 if (LocaleCompare("rectangle",keyword) == 0)
2437 primitive_type=RectanglePrimitive;
2440 if (LocaleCompare("rotate",keyword) == 0)
2442 GetMagickToken(q,&q,token);
2443 angle=StringToDouble(token,(char **) NULL);
2444 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2445 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2446 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2447 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2450 if (LocaleCompare("roundRectangle",keyword) == 0)
2452 primitive_type=RoundRectanglePrimitive;
2461 if (LocaleCompare("scale",keyword) == 0)
2463 GetMagickToken(q,&q,token);
2464 affine.sx=StringToDouble(token,(char **) NULL);
2465 GetMagickToken(q,&q,token);
2467 GetMagickToken(q,&q,token);
2468 affine.sy=StringToDouble(token,(char **) NULL);
2471 if (LocaleCompare("skewX",keyword) == 0)
2473 GetMagickToken(q,&q,token);
2474 angle=StringToDouble(token,(char **) NULL);
2475 affine.ry=sin(DegreesToRadians(angle));
2478 if (LocaleCompare("skewY",keyword) == 0)
2480 GetMagickToken(q,&q,token);
2481 angle=StringToDouble(token,(char **) NULL);
2482 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2485 if (LocaleCompare("stop-color",keyword) == 0)
2490 GetMagickToken(q,&q,token);
2491 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2493 (void) GradientImage(image,LinearGradient,ReflectSpread,
2494 &start_color,&stop_color,exception);
2495 start_color=stop_color;
2496 GetMagickToken(q,&q,token);
2499 if (LocaleCompare("stroke",keyword) == 0)
2501 GetMagickToken(q,&q,token);
2502 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2503 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2504 (void) DrawPatternPath(image,draw_info,token,
2505 &graphic_context[n]->stroke_pattern,exception);
2508 status&=QueryColorCompliance(token,AllCompliance,
2509 &graphic_context[n]->stroke,exception);
2510 if (status == MagickFalse)
2515 pattern_info=AcquireImageInfo();
2516 (void) CopyMagickString(pattern_info->filename,token,
2518 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2520 CatchException(exception);
2521 pattern_info=DestroyImageInfo(pattern_info);
2526 if (LocaleCompare("stroke-antialias",keyword) == 0)
2528 GetMagickToken(q,&q,token);
2529 graphic_context[n]->stroke_antialias=
2530 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2533 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2535 if (graphic_context[n]->dash_pattern != (double *) NULL)
2536 graphic_context[n]->dash_pattern=(double *)
2537 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2538 if (IsPoint(q) != MagickFalse)
2544 GetMagickToken(p,&p,token);
2546 GetMagickToken(p,&p,token);
2547 for (x=0; IsPoint(token) != MagickFalse; x++)
2549 GetMagickToken(p,&p,token);
2551 GetMagickToken(p,&p,token);
2553 graphic_context[n]->dash_pattern=(double *)
2554 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2555 sizeof(*graphic_context[n]->dash_pattern));
2556 if (graphic_context[n]->dash_pattern == (double *) NULL)
2558 (void) ThrowMagickException(exception,GetMagickModule(),
2559 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2563 for (j=0; j < x; j++)
2565 GetMagickToken(q,&q,token);
2567 GetMagickToken(q,&q,token);
2568 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2571 if ((x & 0x01) != 0)
2572 for ( ; j < (2*x); j++)
2573 graphic_context[n]->dash_pattern[j]=
2574 graphic_context[n]->dash_pattern[j-x];
2575 graphic_context[n]->dash_pattern[j]=0.0;
2578 GetMagickToken(q,&q,token);
2581 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2583 GetMagickToken(q,&q,token);
2584 graphic_context[n]->dash_offset=StringToDouble(token,
2588 if (LocaleCompare("stroke-linecap",keyword) == 0)
2593 GetMagickToken(q,&q,token);
2594 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2598 graphic_context[n]->linecap=(LineCap) linecap;
2601 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2606 GetMagickToken(q,&q,token);
2607 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2612 graphic_context[n]->linejoin=(LineJoin) linejoin;
2615 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2617 GetMagickToken(q,&q,token);
2618 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2621 if (LocaleCompare("stroke-opacity",keyword) == 0)
2623 GetMagickToken(q,&q,token);
2624 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2625 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2626 factor*StringToDouble(token,(char **) NULL);
2629 if (LocaleCompare("stroke-width",keyword) == 0)
2631 GetMagickToken(q,&q,token);
2632 graphic_context[n]->stroke_width=StringToDouble(token,
2642 if (LocaleCompare("text",keyword) == 0)
2644 primitive_type=TextPrimitive;
2647 if (LocaleCompare("text-align",keyword) == 0)
2652 GetMagickToken(q,&q,token);
2653 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2657 graphic_context[n]->align=(AlignType) align;
2660 if (LocaleCompare("text-anchor",keyword) == 0)
2665 GetMagickToken(q,&q,token);
2666 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2670 graphic_context[n]->align=(AlignType) align;
2673 if (LocaleCompare("text-antialias",keyword) == 0)
2675 GetMagickToken(q,&q,token);
2676 graphic_context[n]->text_antialias=
2677 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2680 if (LocaleCompare("text-undercolor",keyword) == 0)
2682 GetMagickToken(q,&q,token);
2683 (void) QueryColorCompliance(token,AllCompliance,
2684 &graphic_context[n]->undercolor,exception);
2687 if (LocaleCompare("translate",keyword) == 0)
2689 GetMagickToken(q,&q,token);
2690 affine.tx=StringToDouble(token,(char **) NULL);
2691 GetMagickToken(q,&q,token);
2693 GetMagickToken(q,&q,token);
2694 affine.ty=StringToDouble(token,(char **) NULL);
2703 if (LocaleCompare("viewbox",keyword) == 0)
2705 GetMagickToken(q,&q,token);
2706 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2707 (char **) NULL)-0.5);
2708 GetMagickToken(q,&q,token);
2710 GetMagickToken(q,&q,token);
2711 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2712 (char **) NULL)-0.5);
2713 GetMagickToken(q,&q,token);
2715 GetMagickToken(q,&q,token);
2716 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2717 token,(char **) NULL)+0.5);
2718 GetMagickToken(q,&q,token);
2720 GetMagickToken(q,&q,token);
2721 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2722 token,(char **) NULL)+0.5);
2734 if (status == MagickFalse)
2736 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2737 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2739 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2740 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2741 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2742 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2743 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2745 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2748 if (primitive_type == UndefinedPrimitive)
2750 if (image->debug != MagickFalse)
2751 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2756 Parse the primitive attributes.
2760 primitive_info[0].point.x=0.0;
2761 primitive_info[0].point.y=0.0;
2762 for (x=0; *q != '\0'; x++)
2767 if (IsPoint(q) == MagickFalse)
2769 GetMagickToken(q,&q,token);
2770 point.x=StringToDouble(token,(char **) NULL);
2771 GetMagickToken(q,&q,token);
2773 GetMagickToken(q,&q,token);
2774 point.y=StringToDouble(token,(char **) NULL);
2775 GetMagickToken(q,(const char **) NULL,token);
2777 GetMagickToken(q,&q,token);
2778 primitive_info[i].primitive=primitive_type;
2779 primitive_info[i].point=point;
2780 primitive_info[i].coordinates=0;
2781 primitive_info[i].method=FloodfillMethod;
2783 if (i < (ssize_t) number_points)
2786 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2787 (size_t) number_points,sizeof(*primitive_info));
2788 if (primitive_info == (PrimitiveInfo *) NULL)
2790 (void) ThrowMagickException(exception,GetMagickModule(),
2791 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2795 primitive_info[j].primitive=primitive_type;
2796 primitive_info[j].coordinates=(size_t) x;
2797 primitive_info[j].method=FloodfillMethod;
2798 primitive_info[j].text=(char *) NULL;
2800 Circumscribe primitive within a circle.
2802 bounds.x1=primitive_info[j].point.x;
2803 bounds.y1=primitive_info[j].point.y;
2804 bounds.x2=primitive_info[j].point.x;
2805 bounds.y2=primitive_info[j].point.y;
2806 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2808 point=primitive_info[j+k].point;
2809 if (point.x < bounds.x1)
2811 if (point.y < bounds.y1)
2813 if (point.x > bounds.x2)
2815 if (point.y > bounds.y2)
2819 Speculate how many points our primitive might consume.
2821 length=primitive_info[j].coordinates;
2822 switch (primitive_type)
2824 case RectanglePrimitive:
2829 case RoundRectanglePrimitive:
2831 length*=5+8*BezierQuantum;
2834 case BezierPrimitive:
2836 if (primitive_info[j].coordinates > 107)
2837 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2838 "TooManyBezierCoordinates","`%s'",token);
2839 length=BezierQuantum*primitive_info[j].coordinates;
2848 GetMagickToken(q,&q,token);
2851 for (s=token; *s != '\0'; s=t)
2856 value=StringToDouble(s,&t);
2865 length=length*BezierQuantum/2;
2868 case CirclePrimitive:
2870 case EllipsePrimitive:
2877 alpha=bounds.x2-bounds.x1;
2878 beta=bounds.y2-bounds.y1;
2879 radius=hypot((double) alpha,(double) beta);
2880 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2886 if ((size_t) (i+length) >= number_points)
2889 Resize based on speculative points required by primitive.
2891 number_points+=length+1;
2892 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2893 (size_t) number_points,sizeof(*primitive_info));
2894 if (primitive_info == (PrimitiveInfo *) NULL)
2896 (void) ThrowMagickException(exception,GetMagickModule(),
2897 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2902 switch (primitive_type)
2904 case PointPrimitive:
2907 if (primitive_info[j].coordinates != 1)
2912 TracePoint(primitive_info+j,primitive_info[j].point);
2913 i=(ssize_t) (j+primitive_info[j].coordinates);
2918 if (primitive_info[j].coordinates != 2)
2923 TraceLine(primitive_info+j,primitive_info[j].point,
2924 primitive_info[j+1].point);
2925 i=(ssize_t) (j+primitive_info[j].coordinates);
2928 case RectanglePrimitive:
2930 if (primitive_info[j].coordinates != 2)
2935 TraceRectangle(primitive_info+j,primitive_info[j].point,
2936 primitive_info[j+1].point);
2937 i=(ssize_t) (j+primitive_info[j].coordinates);
2940 case RoundRectanglePrimitive:
2942 if (primitive_info[j].coordinates != 3)
2947 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2948 primitive_info[j+1].point,primitive_info[j+2].point);
2949 i=(ssize_t) (j+primitive_info[j].coordinates);
2954 if (primitive_info[j].coordinates != 3)
2956 primitive_type=UndefinedPrimitive;
2959 TraceArc(primitive_info+j,primitive_info[j].point,
2960 primitive_info[j+1].point,primitive_info[j+2].point);
2961 i=(ssize_t) (j+primitive_info[j].coordinates);
2964 case EllipsePrimitive:
2966 if (primitive_info[j].coordinates != 3)
2971 TraceEllipse(primitive_info+j,primitive_info[j].point,
2972 primitive_info[j+1].point,primitive_info[j+2].point);
2973 i=(ssize_t) (j+primitive_info[j].coordinates);
2976 case CirclePrimitive:
2978 if (primitive_info[j].coordinates != 2)
2983 TraceCircle(primitive_info+j,primitive_info[j].point,
2984 primitive_info[j+1].point);
2985 i=(ssize_t) (j+primitive_info[j].coordinates);
2988 case PolylinePrimitive:
2990 case PolygonPrimitive:
2992 primitive_info[i]=primitive_info[j];
2993 primitive_info[i].coordinates=0;
2994 primitive_info[j].coordinates++;
2998 case BezierPrimitive:
3000 if (primitive_info[j].coordinates < 3)
3005 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3006 i=(ssize_t) (j+primitive_info[j].coordinates);
3011 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3014 case ColorPrimitive:
3015 case MattePrimitive:
3020 if (primitive_info[j].coordinates != 1)
3025 GetMagickToken(q,&q,token);
3026 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3030 primitive_info[j].method=(PaintMethod) method;
3035 if (primitive_info[j].coordinates != 1)
3041 GetMagickToken(q,&q,token);
3042 primitive_info[j].text=AcquireString(token);
3045 case ImagePrimitive:
3047 if (primitive_info[j].coordinates != 2)
3052 GetMagickToken(q,&q,token);
3053 primitive_info[j].text=AcquireString(token);
3057 if (primitive_info == (PrimitiveInfo *) NULL)
3059 if (image->debug != MagickFalse)
3060 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3061 if (status == MagickFalse)
3063 primitive_info[i].primitive=UndefinedPrimitive;
3069 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3071 point=primitive_info[i].point;
3072 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3073 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3074 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3075 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3076 point=primitive_info[i].point;
3077 if (point.x < graphic_context[n]->bounds.x1)
3078 graphic_context[n]->bounds.x1=point.x;
3079 if (point.y < graphic_context[n]->bounds.y1)
3080 graphic_context[n]->bounds.y1=point.y;
3081 if (point.x > graphic_context[n]->bounds.x2)
3082 graphic_context[n]->bounds.x2=point.x;
3083 if (point.y > graphic_context[n]->bounds.y2)
3084 graphic_context[n]->bounds.y2=point.y;
3085 if (primitive_info[i].primitive == ImagePrimitive)
3087 if (i >= (ssize_t) number_points)
3088 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3090 if (graphic_context[n]->render != MagickFalse)
3092 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3093 (LocaleCompare(graphic_context[n]->clip_mask,
3094 graphic_context[n-1]->clip_mask) != 0))
3095 status&=DrawClipPath(image,graphic_context[n],
3096 graphic_context[n]->clip_mask,exception);
3097 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3100 if (primitive_info->text != (char *) NULL)
3101 primitive_info->text=(char *) RelinquishMagickMemory(
3102 primitive_info->text);
3103 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3105 if (proceed == MagickFalse)
3110 if (image->debug != MagickFalse)
3111 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3113 Relinquish resources.
3115 token=DestroyString(token);
3116 if (primitive_info != (PrimitiveInfo *) NULL)
3117 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3118 primitive=DestroyString(primitive);
3119 for ( ; n >= 0; n--)
3120 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3121 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3122 if (status == MagickFalse)
3123 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3125 return(status != 0 ? MagickTrue : MagickFalse);
3129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3133 % D r a w G r a d i e n t I m a g e %
3137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3139 % DrawGradientImage() draws a linear gradient on the image.
3141 % The format of the DrawGradientImage method is:
3143 % MagickBooleanType DrawGradientImage(Image *image,
3144 % const DrawInfo *draw_info,ExceptionInfo *exception)
3146 % A description of each parameter follows:
3148 % o image: the image.
3150 % o draw_info: the draw info.
3152 % o exception: return any errors or warnings in this structure.
3156 static inline double GetStopColorOffset(const GradientInfo *gradient,
3157 const ssize_t x,const ssize_t y)
3159 switch (gradient->type)
3161 case UndefinedGradient:
3162 case LinearGradient:
3177 gradient_vector=(&gradient->gradient_vector);
3178 p.x=gradient_vector->x2-gradient_vector->x1;
3179 p.y=gradient_vector->y2-gradient_vector->y1;
3180 q.x=(double) x-gradient_vector->x1;
3181 q.y=(double) y-gradient_vector->y1;
3182 length=sqrt(q.x*q.x+q.y*q.y);
3183 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3184 gamma=PerceptibleReciprocal(gamma);
3185 scale=p.x*q.x+p.y*q.y;
3186 offset=gamma*scale*length;
3189 case RadialGradient:
3198 v.x=(double) x-gradient->center.x;
3199 v.y=(double) y-gradient->center.y;
3200 length=sqrt(v.x*v.x+v.y*v.y);
3201 if (gradient->spread == RepeatSpread)
3203 offset=length/gradient->radius;
3210 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3211 const DrawInfo *draw_info,ExceptionInfo *exception)
3241 Draw linear or radial gradient on image.
3243 assert(image != (Image *) NULL);
3244 assert(image->signature == MagickSignature);
3245 if (image->debug != MagickFalse)
3246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3247 assert(draw_info != (const DrawInfo *) NULL);
3248 gradient=(&draw_info->gradient);
3249 gradient_vector=(&gradient->gradient_vector);
3250 point.x=gradient_vector->x2-gradient_vector->x1;
3251 point.y=gradient_vector->y2-gradient_vector->y1;
3252 length=sqrt(point.x*point.x+point.y*point.y);
3253 bounding_box=gradient->bounding_box;
3255 GetPixelInfo(image,&zero);
3256 image_view=AcquireAuthenticCacheView(image,exception);
3257 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3258 #pragma omp parallel for schedule(static,4) shared(status) \
3259 magick_threads(image,image,1,1)
3261 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3281 if (status == MagickFalse)
3283 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3284 if (q == (Quantum *) NULL)
3291 offset=GetStopColorOffset(gradient,0,y);
3292 if (gradient->type != RadialGradient)
3294 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3296 GetPixelInfoPixel(image,q,&pixel);
3297 switch (gradient->spread)
3299 case UndefinedSpread:
3302 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3303 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3305 offset=GetStopColorOffset(gradient,x,y);
3306 if (gradient->type != RadialGradient)
3309 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3310 if (offset < gradient->stops[i].offset)
3312 if ((offset < 0.0) || (i == 0))
3313 composite=gradient->stops[0].color;
3315 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3316 composite=gradient->stops[gradient->number_stops-1].color;
3321 alpha=(offset-gradient->stops[i].offset)/
3322 (gradient->stops[j].offset-gradient->stops[i].offset);
3323 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3324 &gradient->stops[j].color,alpha,&composite);
3330 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3331 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3333 offset=GetStopColorOffset(gradient,x,y);
3334 if (gradient->type != RadialGradient)
3339 if ((ssize_t) fmod(offset,2.0) == 0)
3340 offset=fmod(offset,1.0);
3342 offset=1.0-fmod(offset,1.0);
3343 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3344 if (offset < gradient->stops[i].offset)
3347 composite=gradient->stops[0].color;
3349 if (i == (ssize_t) gradient->number_stops)
3350 composite=gradient->stops[gradient->number_stops-1].color;
3355 alpha=(offset-gradient->stops[i].offset)/
3356 (gradient->stops[j].offset-gradient->stops[i].offset);
3357 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3358 &gradient->stops[j].color,alpha,&composite);
3370 antialias=MagickFalse;
3372 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3373 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3375 offset=GetStopColorOffset(gradient,x,y);
3376 if (gradient->type == LinearGradient)
3378 repeat=fmod(offset,length);
3380 repeat=length-fmod(-repeat,length);
3382 repeat=fmod(offset,length);
3383 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3384 MagickTrue : MagickFalse;
3385 offset=repeat/length;
3389 repeat=fmod(offset,gradient->radius);
3391 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3393 repeat=fmod(offset,gradient->radius);
3394 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3396 offset=repeat/gradient->radius;
3399 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3400 if (offset < gradient->stops[i].offset)
3403 composite=gradient->stops[0].color;
3405 if (i == (ssize_t) gradient->number_stops)
3406 composite=gradient->stops[gradient->number_stops-1].color;
3411 alpha=(offset-gradient->stops[i].offset)/
3412 (gradient->stops[j].offset-gradient->stops[i].offset);
3413 if (antialias != MagickFalse)
3415 if (gradient->type == LinearGradient)
3416 alpha=length-repeat;
3418 alpha=gradient->radius-repeat;
3420 j=(ssize_t) gradient->number_stops-1L;
3422 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3423 &gradient->stops[j].color,alpha,&composite);
3428 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3430 SetPixelInfoPixel(image,&pixel,q);
3431 q+=GetPixelChannels(image);
3433 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3436 image_view=DestroyCacheView(image_view);
3441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3445 % D r a w P a t t e r n P a t h %
3449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3451 % DrawPatternPath() draws a pattern.
3453 % The format of the DrawPatternPath method is:
3455 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3456 % const char *name,Image **pattern,ExceptionInfo *exception)
3458 % A description of each parameter follows:
3460 % o image: the image.
3462 % o draw_info: the draw info.
3464 % o name: the pattern name.
3466 % o image: the image.
3468 % o exception: return any errors or warnings in this structure.
3471 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3472 const DrawInfo *draw_info,const char *name,Image **pattern,
3473 ExceptionInfo *exception)
3476 property[MaxTextExtent];
3491 assert(image != (Image *) NULL);
3492 assert(image->signature == MagickSignature);
3493 if (image->debug != MagickFalse)
3494 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3495 assert(draw_info != (const DrawInfo *) NULL);
3496 assert(name != (const char *) NULL);
3497 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3498 path=GetImageArtifact(image,property);
3499 if (path == (const char *) NULL)
3500 return(MagickFalse);
3501 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3502 geometry=GetImageArtifact(image,property);
3503 if (geometry == (const char *) NULL)
3504 return(MagickFalse);
3505 if ((*pattern) != (Image *) NULL)
3506 *pattern=DestroyImage(*pattern);
3507 image_info=AcquireImageInfo();
3508 image_info->size=AcquireString(geometry);
3509 *pattern=AcquireImage(image_info,exception);
3510 image_info=DestroyImageInfo(image_info);
3511 (void) QueryColorCompliance("#000000ff",AllCompliance,
3512 &(*pattern)->background_color,exception);
3513 (void) SetImageBackgroundColor(*pattern,exception);
3514 if (image->debug != MagickFalse)
3515 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3516 "begin pattern-path %s %s",name,geometry);
3517 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3518 clone_info->fill_pattern=NewImageList();
3519 clone_info->stroke_pattern=NewImageList();
3520 (void) CloneString(&clone_info->primitive,path);
3521 status=DrawImage(*pattern,clone_info,exception);
3522 clone_info=DestroyDrawInfo(clone_info);
3523 if (image->debug != MagickFalse)
3524 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3533 + D r a w P o l y g o n P r i m i t i v e %
3537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3539 % DrawPolygonPrimitive() draws a polygon on the image.
3541 % The format of the DrawPolygonPrimitive method is:
3543 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3544 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3545 % ExceptionInfo *exception)
3547 % A description of each parameter follows:
3549 % o image: the image.
3551 % o draw_info: the draw info.
3553 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3555 % o exception: return any errors or warnings in this structure.
3559 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3564 assert(polygon_info != (PolygonInfo **) NULL);
3565 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3566 if (polygon_info[i] != (PolygonInfo *) NULL)
3567 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3568 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3569 return(polygon_info);
3572 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3573 const PrimitiveInfo *primitive_info)
3576 *restrict path_info;
3587 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3588 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3589 sizeof(*polygon_info));
3590 if (polygon_info == (PolygonInfo **) NULL)
3591 return((PolygonInfo **) NULL);
3592 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3593 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3594 if (path_info == (PathInfo *) NULL)
3595 return(DestroyPolygonThreadSet(polygon_info));
3596 for (i=0; i < (ssize_t) number_threads; i++)
3598 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3599 if (polygon_info[i] == (PolygonInfo *) NULL)
3600 return(DestroyPolygonThreadSet(polygon_info));
3602 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3603 return(polygon_info);
3606 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3607 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3608 const ssize_t y,double *stroke_alpha)
3619 register const PointInfo
3633 Compute fill & stroke opacity for this (x,y) point.
3637 p=polygon_info->edges;
3638 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3640 if ((double) y <= (p->bounds.y1-mid-0.5))
3642 if ((double) y > (p->bounds.y2+mid+0.5))
3644 (void) DestroyEdge(polygon_info,(size_t) j);
3647 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3648 ((double) x > (p->bounds.x2+mid+0.5)))
3650 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3651 for ( ; i < (ssize_t) p->number_points; i++)
3653 if ((double) y <= (p->points[i-1].y-mid-0.5))
3655 if ((double) y > (p->points[i].y+mid+0.5))
3657 if (p->scanline != (double) y)
3659 p->scanline=(double) y;
3660 p->highwater=(size_t) i;
3663 Compute distance between a point and an edge.
3666 delta.x=(q+1)->x-q->x;
3667 delta.y=(q+1)->y-q->y;
3668 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3671 delta.x=(double) x-q->x;
3672 delta.y=(double) y-q->y;
3673 distance=delta.x*delta.x+delta.y*delta.y;
3677 alpha=delta.x*delta.x+delta.y*delta.y;
3680 delta.x=(double) x-(q+1)->x;
3681 delta.y=(double) y-(q+1)->y;
3682 distance=delta.x*delta.x+delta.y*delta.y;
3687 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3688 distance=alpha*beta*beta;
3692 Compute stroke & subpath opacity.
3695 if (p->ghostline == MagickFalse)
3698 if ((*stroke_alpha < 1.0) &&
3699 (distance <= ((alpha+0.25)*(alpha+0.25))))
3702 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3707 if (distance != 1.0)
3708 beta=sqrt((double) distance);
3710 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3711 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3715 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3717 if (distance <= 0.0)
3727 if (distance != 1.0)
3728 beta=sqrt(distance);
3731 if (subpath_alpha < (alpha*alpha))
3732 subpath_alpha=alpha*alpha;
3736 Compute fill opacity.
3738 if (fill == MagickFalse)
3740 if (subpath_alpha >= 1.0)
3743 Determine winding number.
3746 p=polygon_info->edges;
3747 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3749 if ((double) y <= p->bounds.y1)
3751 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3753 if ((double) x > p->bounds.x2)
3755 winding_number+=p->direction ? 1 : -1;
3758 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3759 for ( ; i < (ssize_t) p->number_points; i++)
3760 if ((double) y <= p->points[i].y)
3763 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3764 winding_number+=p->direction ? 1 : -1;
3766 if (fill_rule != NonZeroRule)
3768 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3772 if (MagickAbsoluteValue(winding_number) != 0)
3774 return(subpath_alpha);
3777 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3778 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3779 ExceptionInfo *exception)
3792 **restrict polygon_info;
3809 Compute bounding box.
3811 assert(image != (Image *) NULL);
3812 assert(image->signature == MagickSignature);
3813 if (image->debug != MagickFalse)
3814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3815 assert(draw_info != (DrawInfo *) NULL);
3816 assert(draw_info->signature == MagickSignature);
3817 assert(primitive_info != (PrimitiveInfo *) NULL);
3818 if (primitive_info->coordinates == 0)
3820 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3821 if (polygon_info == (PolygonInfo **) NULL)
3822 return(MagickFalse);
3823 DisableMSCWarning(4127)
3825 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3827 if (image->debug != MagickFalse)
3828 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3829 fill=(primitive_info->method == FillToBorderMethod) ||
3830 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3831 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3832 bounds=polygon_info[0]->edges[0].bounds;
3833 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3835 p=polygon_info[0]->edges+i;
3836 if (p->bounds.x1 < bounds.x1)
3837 bounds.x1=p->bounds.x1;
3838 if (p->bounds.y1 < bounds.y1)
3839 bounds.y1=p->bounds.y1;
3840 if (p->bounds.x2 > bounds.x2)
3841 bounds.x2=p->bounds.x2;
3842 if (p->bounds.y2 > bounds.y2)
3843 bounds.y2=p->bounds.y2;
3845 bounds.x1-=(mid+1.0);
3846 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3847 image->columns ? (double) image->columns-1 : bounds.x1;
3848 bounds.y1-=(mid+1.0);
3849 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3850 image->rows ? (double) image->rows-1 : bounds.y1;
3851 bounds.x2+=(mid+1.0);
3852 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3853 image->columns ? (double) image->columns-1 : bounds.x2;
3854 bounds.y2+=(mid+1.0);
3855 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3856 image->rows ? (double) image->rows-1 : bounds.y2;
3858 image_view=AcquireAuthenticCacheView(image,exception);
3859 if (primitive_info->coordinates == 1)
3864 start=(ssize_t) ceil(bounds.y1-0.5);
3865 stop=(ssize_t) floor(bounds.y2+0.5);
3866 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3867 #pragma omp parallel for schedule(static,4) shared(status) \
3868 magick_threads(image,image,1,1)
3870 for (y=start; y <= stop; y++)
3888 if (status == MagickFalse)
3890 start=(ssize_t) ceil(bounds.x1-0.5);
3891 stop=(ssize_t) floor(bounds.x2+0.5);
3893 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3895 if (q == (Quantum *) NULL)
3900 GetPixelInfo(image,&pixel);
3901 for ( ; x <= stop; x++)
3903 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3904 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3906 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3907 SetPixelInfoPixel(image,&pixel,q);
3909 q+=GetPixelChannels(image);
3911 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3912 if (sync == MagickFalse)
3915 image_view=DestroyCacheView(image_view);
3916 polygon_info=DestroyPolygonThreadSet(polygon_info);
3917 if (image->debug != MagickFalse)
3918 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3919 " end draw-polygon");
3923 Draw polygon or line.
3925 if (image->alpha_trait != BlendPixelTrait)
3926 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3927 start=(ssize_t) ceil(bounds.y1-0.5);
3928 stop=(ssize_t) floor(bounds.y2+0.5);
3929 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3930 #pragma omp parallel for schedule(static,4) shared(status) \
3931 magick_threads(image,image,1,1)
3933 for (y=start; y <= stop; y++)
3936 id = GetOpenMPThreadId();
3956 if (status == MagickFalse)
3958 start=(ssize_t) ceil(bounds.x1-0.5);
3959 stop=(ssize_t) floor(bounds.x2+0.5);
3960 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3962 if (q == (Quantum *) NULL)
3967 for (x=start; x <= stop; x++)
3972 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3974 if (draw_info->stroke_antialias == MagickFalse)
3976 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3977 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3979 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3980 fill_alpha=fill_alpha*fill_color.alpha;
3981 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
3982 GetPixelAlpha(image,q),q);
3983 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3984 stroke_alpha=stroke_alpha*stroke_color.alpha;
3985 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
3986 GetPixelAlpha(image,q),q);
3987 q+=GetPixelChannels(image);
3989 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3992 image_view=DestroyCacheView(image_view);
3993 polygon_info=DestroyPolygonThreadSet(polygon_info);
3994 if (image->debug != MagickFalse)
3995 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4004 % D r a w P r i m i t i v e %
4008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4010 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4012 % The format of the DrawPrimitive method is:
4014 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4015 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4017 % A description of each parameter follows:
4019 % o image: the image.
4021 % o draw_info: the draw info.
4023 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4025 % o exception: return any errors or warnings in this structure.
4029 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4055 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4056 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4057 switch (primitive_info->primitive)
4059 case PointPrimitive:
4061 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4062 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4063 methods[primitive_info->method]);
4066 case ColorPrimitive:
4068 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4069 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4070 methods[primitive_info->method]);
4073 case MattePrimitive:
4075 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4076 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4077 methods[primitive_info->method]);
4082 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4083 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4086 case ImagePrimitive:
4088 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4089 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4096 p=primitive_info[0].point;
4099 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4101 point=primitive_info[i].point;
4102 if (coordinates <= 0)
4104 coordinates=(ssize_t) primitive_info[i].coordinates;
4105 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4106 " begin open (%.20g)",(double) coordinates);
4109 point=primitive_info[i].point;
4110 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4111 (fabs(q.y-point.y) >= MagickEpsilon))
4112 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4113 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4115 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4116 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4119 if (coordinates > 0)
4121 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4122 (fabs(p.y-point.y) >= MagickEpsilon))
4123 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4124 (double) coordinates);
4126 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4127 (double) coordinates);
4131 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4132 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4133 ExceptionInfo *exception)
4148 if (image->debug != MagickFalse)
4150 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4151 " begin draw-primitive");
4152 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4153 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4154 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4155 draw_info->affine.tx,draw_info->affine.ty);
4157 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4158 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4159 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4160 (void) SetImageColorspace(image,sRGBColorspace,exception);
4162 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4163 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4164 image_view=AcquireAuthenticCacheView(image,exception);
4165 switch (primitive_info->primitive)
4167 case PointPrimitive:
4175 if ((y < 0) || (y >= (ssize_t) image->rows))
4177 if ((x < 0) || (x >= (ssize_t) image->columns))
4179 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4180 if (q == (Quantum *) NULL)
4182 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4183 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4184 (double) GetPixelAlpha(image,q),q);
4185 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4188 case ColorPrimitive:
4190 switch (primitive_info->method)
4201 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4202 if (q == (Quantum *) NULL)
4204 GetPixelInfo(image,&pixel);
4205 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4206 SetPixelInfoPixel(image,&pixel,q);
4207 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4219 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4221 for (y=0; y < (ssize_t) image->rows; y++)
4226 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4228 if (q == (Quantum *) NULL)
4230 for (x=0; x < (ssize_t) image->columns; x++)
4232 GetPixelInfoPixel(image,q,&pixel);
4233 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4235 q+=GetPixelChannels(image);
4238 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4239 SetPixelInfoPixel(image,&pixel,q);
4240 q+=GetPixelChannels(image);
4242 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4243 if (sync == MagickFalse)
4248 case FloodfillMethod:
4249 case FillToBorderMethod:
4254 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4256 if (primitive_info->method == FillToBorderMethod)
4258 target.red=(double) draw_info->border_color.red;
4259 target.green=(double) draw_info->border_color.green;
4260 target.blue=(double) draw_info->border_color.blue;
4262 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4263 primitive_info->method == FloodfillMethod ? MagickFalse :
4264 MagickTrue,exception);
4275 GetPixelInfo(image,&pixel);
4276 for (y=0; y < (ssize_t) image->rows; y++)
4284 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4286 if (q == (Quantum *) NULL)
4288 for (x=0; x < (ssize_t) image->columns; x++)
4290 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4291 SetPixelInfoPixel(image,&pixel,q);
4292 q+=GetPixelChannels(image);
4294 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4295 if (sync == MagickFalse)
4303 case MattePrimitive:
4305 if (image->alpha_trait != BlendPixelTrait)
4306 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4307 switch (primitive_info->method)
4318 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4319 if (q == (Quantum *) NULL)
4321 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4322 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4323 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4335 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4337 for (y=0; y < (ssize_t) image->rows; y++)
4345 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4347 if (q == (Quantum *) NULL)
4349 for (x=0; x < (ssize_t) image->columns; x++)
4351 GetPixelInfoPixel(image,q,&pixel);
4352 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4354 q+=GetPixelChannels(image);
4357 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4358 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4359 q+=GetPixelChannels(image);
4361 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4362 if (sync == MagickFalse)
4367 case FloodfillMethod:
4368 case FillToBorderMethod:
4376 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4378 if (primitive_info->method == FillToBorderMethod)
4380 target.red=(double) draw_info->border_color.red;
4381 target.green=(double) draw_info->border_color.green;
4382 target.blue=(double) draw_info->border_color.blue;
4384 channel_mask=SetImageChannelMask(image,AlphaChannel);
4385 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4386 primitive_info->method == FloodfillMethod ? MagickFalse :
4387 MagickTrue,exception);
4388 (void) SetImageChannelMask(image,channel_mask);
4399 for (y=0; y < (ssize_t) image->rows; y++)
4407 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4409 if (q == (Quantum *) NULL)
4411 for (x=0; x < (ssize_t) image->columns; x++)
4413 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4414 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4415 q+=GetPixelChannels(image);
4417 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4418 if (sync == MagickFalse)
4429 geometry[MaxTextExtent];
4434 if (primitive_info->text == (char *) NULL)
4436 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4437 (void) CloneString(&clone_info->text,primitive_info->text);
4438 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4439 primitive_info->point.x,primitive_info->point.y);
4440 (void) CloneString(&clone_info->geometry,geometry);
4441 status&=AnnotateImage(image,clone_info,exception);
4442 clone_info=DestroyDrawInfo(clone_info);
4445 case ImagePrimitive:
4451 composite_geometry[MaxTextExtent];
4466 if (primitive_info->text == (char *) NULL)
4468 clone_info=AcquireImageInfo();
4469 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4470 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4474 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4476 composite_image=ReadImage(clone_info,exception);
4478 clone_info=DestroyImageInfo(clone_info);
4479 if (composite_image == (Image *) NULL)
4481 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4482 NULL,(void *) NULL);
4483 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4484 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4485 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4486 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4489 geometry[MaxTextExtent];
4494 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4495 primitive_info[1].point.x,primitive_info[1].point.y);
4496 composite_image->filter=image->filter;
4497 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4500 if (composite_image->alpha_trait != BlendPixelTrait)
4501 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4503 if (draw_info->alpha != OpaqueAlpha)
4504 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4505 SetGeometry(image,&geometry);
4506 image->gravity=draw_info->gravity;
4509 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4510 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4511 composite_image->rows,(double) geometry.x,(double) geometry.y);
4512 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4513 affine=draw_info->affine;
4514 affine.tx=(double) geometry.x;
4515 affine.ty=(double) geometry.y;
4516 composite_image->interpolate=image->interpolate;
4517 if (draw_info->compose == OverCompositeOp)
4518 (void) DrawAffineImage(image,composite_image,&affine,exception);
4520 (void) CompositeImage(image,composite_image,draw_info->compose,
4521 MagickTrue,geometry.x,geometry.y,exception);
4522 composite_image=DestroyImage(composite_image);
4534 if (IsEventLogging() != MagickFalse)
4535 LogPrimitiveInfo(primitive_info);
4536 scale=ExpandAffine(&draw_info->affine);
4537 if ((draw_info->dash_pattern != (double *) NULL) &&
4538 (draw_info->dash_pattern[0] != 0.0) &&
4539 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4540 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4545 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4546 clone_info->stroke_width=0.0;
4547 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4548 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4550 clone_info=DestroyDrawInfo(clone_info);
4551 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4554 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4556 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4562 Draw strokes while respecting line cap/join attributes.
4564 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4566 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4567 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4568 MagickTrue : MagickFalse;
4569 i=(ssize_t) primitive_info[0].coordinates;
4570 if ((((draw_info->linecap == RoundCap) ||
4571 (closed_path != MagickFalse)) &&
4572 (draw_info->linejoin == RoundJoin)) ||
4573 (primitive_info[i].primitive != UndefinedPrimitive))
4575 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4579 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4580 clone_info->stroke_width=0.0;
4581 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4582 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4584 clone_info=DestroyDrawInfo(clone_info);
4585 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4588 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4592 image_view=DestroyCacheView(image_view);
4593 if (image->debug != MagickFalse)
4594 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4595 return(status != 0 ? MagickTrue : MagickFalse);
4599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4603 + D r a w S t r o k e P o l y g o n %
4607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4609 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4610 % the image while respecting the line cap and join attributes.
4612 % The format of the DrawStrokePolygon method is:
4614 % MagickBooleanType DrawStrokePolygon(Image *image,
4615 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4617 % A description of each parameter follows:
4619 % o image: the image.
4621 % o draw_info: the draw info.
4623 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4628 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4629 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4637 for (i=0; i < 4; i++)
4638 linecap[i]=(*primitive_info);
4639 linecap[0].coordinates=4;
4640 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4641 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4642 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4643 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4644 linecap[4].primitive=UndefinedPrimitive;
4645 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4648 static MagickBooleanType DrawStrokePolygon(Image *image,
4649 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4650 ExceptionInfo *exception)
4664 register const PrimitiveInfo
4669 Draw stroked polygon.
4671 if (image->debug != MagickFalse)
4672 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4673 " begin draw-stroke-polygon");
4674 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4675 clone_info->fill=draw_info->stroke;
4676 if (clone_info->fill_pattern != (Image *) NULL)
4677 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4678 if (clone_info->stroke_pattern != (Image *) NULL)
4679 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4680 MagickTrue,exception);
4681 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4682 clone_info->stroke_width=0.0;
4683 clone_info->fill_rule=NonZeroRule;
4685 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4687 stroke_polygon=TraceStrokePolygon(draw_info,p);
4688 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4691 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4692 q=p+p->coordinates-1;
4693 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4694 MagickTrue : MagickFalse;
4695 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4697 DrawRoundLinecap(image,draw_info,p,exception);
4698 DrawRoundLinecap(image,draw_info,q,exception);
4701 clone_info=DestroyDrawInfo(clone_info);
4702 if (image->debug != MagickFalse)
4703 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4704 " end draw-stroke-polygon");
4705 return(status != 0 ? MagickTrue : MagickFalse);
4709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4713 % G e t A f f i n e M a t r i x %
4717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4719 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4722 % The format of the GetAffineMatrix method is:
4724 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4726 % A description of each parameter follows:
4728 % o affine_matrix: the affine matrix.
4731 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4733 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4734 assert(affine_matrix != (AffineMatrix *) NULL);
4735 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4736 affine_matrix->sx=1.0;
4737 affine_matrix->sy=1.0;
4741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4745 + G e t D r a w I n f o %
4749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4751 % GetDrawInfo() initializes draw_info to default values from image_info.
4753 % The format of the GetDrawInfo method is:
4755 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4757 % A description of each parameter follows:
4759 % o image_info: the image info..
4761 % o draw_info: the draw info.
4764 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4773 Initialize draw attributes.
4775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4776 assert(draw_info != (DrawInfo *) NULL);
4777 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4778 GetAffineMatrix(&draw_info->affine);
4779 exception=AcquireExceptionInfo();
4780 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4782 (void) QueryColorCompliance("#FFFF",AllCompliance,&draw_info->stroke,
4784 draw_info->stroke_width=1.0;
4785 draw_info->alpha=OpaqueAlpha;
4786 draw_info->fill_rule=EvenOddRule;
4787 draw_info->linecap=ButtCap;
4788 draw_info->linejoin=MiterJoin;
4789 draw_info->miterlimit=10;
4790 draw_info->decorate=NoDecoration;
4791 draw_info->pointsize=12.0;
4792 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4793 draw_info->compose=OverCompositeOp;
4794 draw_info->render=MagickTrue;
4795 draw_info->debug=IsEventLogging();
4796 if (image_info != (ImageInfo *) NULL)
4798 draw_info->stroke_antialias=image_info->antialias;
4799 if (image_info->font != (char *) NULL)
4800 draw_info->font=AcquireString(image_info->font);
4801 if (image_info->density != (char *) NULL)
4802 draw_info->density=AcquireString(image_info->density);
4803 draw_info->text_antialias=image_info->antialias;
4804 if (image_info->pointsize != 0.0)
4805 draw_info->pointsize=image_info->pointsize;
4806 draw_info->border_color=image_info->border_color;
4807 if (image_info->server_name != (char *) NULL)
4808 draw_info->server_name=AcquireString(image_info->server_name);
4809 option=GetImageOption(image_info,"encoding");
4810 if (option != (const char *) NULL)
4811 (void) CloneString(&draw_info->encoding,option);
4812 option=GetImageOption(image_info,"kerning");
4813 if (option != (const char *) NULL)
4814 draw_info->kerning=StringToDouble(option,(char **) NULL);
4815 option=GetImageOption(image_info,"interline-spacing");
4816 if (option != (const char *) NULL)
4817 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4818 option=GetImageOption(image_info,"interword-spacing");
4819 if (option != (const char *) NULL)
4820 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4821 option=GetImageOption(image_info,"direction");
4822 if (option != (const char *) NULL)
4823 draw_info->direction=(DirectionType) ParseCommandOption(
4824 MagickDirectionOptions,MagickFalse,option);
4826 draw_info->direction=UndefinedDirection;
4827 option=GetImageOption(image_info,"fill");
4828 if (option != (const char *) NULL)
4829 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4831 option=GetImageOption(image_info,"stroke");
4832 if (option != (const char *) NULL)
4833 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4835 option=GetImageOption(image_info,"strokewidth");
4836 if (option != (const char *) NULL)
4837 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4838 option=GetImageOption(image_info,"undercolor");
4839 if (option != (const char *) NULL)
4840 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4842 option=GetImageOption(image_info,"gravity");
4843 if (option != (const char *) NULL)
4844 draw_info->gravity=(GravityType) ParseCommandOption(
4845 MagickGravityOptions,MagickFalse,option);
4847 exception=DestroyExceptionInfo(exception);
4848 draw_info->signature=MagickSignature;
4852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4856 + P e r m u t a t e %
4860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4862 % Permutate() returns the permuation of the (n,k).
4864 % The format of the Permutate method is:
4866 % void Permutate(ssize_t n,ssize_t k)
4868 % A description of each parameter follows:
4876 static inline double Permutate(const ssize_t n,const ssize_t k)
4885 for (i=k+1; i <= n; i++)
4887 for (i=1; i <= (n-k); i++)
4893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4897 + T r a c e P r i m i t i v e %
4901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4903 % TracePrimitive is a collection of methods for generating graphic
4904 % primitives such as arcs, ellipses, paths, etc.
4908 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4909 const PointInfo end,const PointInfo degrees)
4915 center.x=0.5*(end.x+start.x);
4916 center.y=0.5*(end.y+start.y);
4917 radii.x=fabs(center.x-start.x);
4918 radii.y=fabs(center.y-start.y);
4919 TraceEllipse(primitive_info,center,radii,degrees);
4922 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4923 const PointInfo end,const PointInfo arc,const double angle,
4924 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4943 register PrimitiveInfo
4952 if ((start.x == end.x) && (start.y == end.y))
4954 TracePoint(primitive_info,end);
4957 radii.x=fabs(arc.x);
4958 radii.y=fabs(arc.y);
4959 if ((radii.x == 0.0) || (radii.y == 0.0))
4961 TraceLine(primitive_info,start,end);
4964 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4965 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4966 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4967 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4968 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4970 if (delta < MagickEpsilon)
4972 TraceLine(primitive_info,start,end);
4977 radii.x*=sqrt((double) delta);
4978 radii.y*=sqrt((double) delta);
4980 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4981 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4982 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4983 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4984 alpha=points[1].x-points[0].x;
4985 beta=points[1].y-points[0].y;
4986 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
4991 factor=sqrt((double) factor);
4992 if (sweep == large_arc)
4995 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4996 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4997 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4998 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4999 if ((theta < 0.0) && (sweep != MagickFalse))
5000 theta+=(double) (2.0*MagickPI);
5002 if ((theta > 0.0) && (sweep == MagickFalse))
5003 theta-=(double) (2.0*MagickPI);
5004 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5007 for (i=0; i < (ssize_t) arc_segments; i++)
5009 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5010 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5011 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5012 sin(fmod((double) beta,DegreesToRadians(360.0)));
5013 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5014 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5015 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5016 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5017 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5018 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5019 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5020 theta/arc_segments),DegreesToRadians(360.0))));
5021 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5022 theta/arc_segments),DegreesToRadians(360.0))));
5023 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5024 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5025 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5026 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5027 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5028 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5029 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5031 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5033 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5035 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5037 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5039 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5041 if (i == (ssize_t) (arc_segments-1))
5046 primitive_info->coordinates=(size_t) (p-primitive_info);
5047 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5049 p->primitive=primitive_info->primitive;
5054 static void TraceBezier(PrimitiveInfo *primitive_info,
5055 const size_t number_coordinates)
5067 register PrimitiveInfo
5079 Allocate coeficients.
5081 quantum=number_coordinates;
5082 for (i=0; i < (ssize_t) number_coordinates; i++)
5084 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5086 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5087 if (alpha > (double) quantum)
5088 quantum=(size_t) alpha;
5089 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5090 if (alpha > (double) quantum)
5091 quantum=(size_t) alpha;
5094 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5095 (double) BezierQuantum);
5096 control_points=quantum*number_coordinates;
5097 coefficients=(double *) AcquireQuantumMemory((size_t)
5098 number_coordinates,sizeof(*coefficients));
5099 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5101 if ((coefficients == (double *) NULL) ||
5102 (points == (PointInfo *) NULL))
5103 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5105 Compute bezier points.
5107 end=primitive_info[number_coordinates-1].point;
5108 for (i=0; i < (ssize_t) number_coordinates; i++)
5109 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5111 for (i=0; i < (ssize_t) control_points; i++)
5116 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5117 for (j=0; j < (ssize_t) number_coordinates; j++)
5119 point.x+=alpha*coefficients[j]*p->point.x;
5120 point.y+=alpha*coefficients[j]*p->point.y;
5121 alpha*=weight/(1.0-weight);
5125 weight+=1.0/control_points;
5128 Bezier curves are just short segmented polys.
5131 for (i=0; i < (ssize_t) control_points; i++)
5133 TracePoint(p,points[i]);
5138 primitive_info->coordinates=(size_t) (p-primitive_info);
5139 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5141 p->primitive=primitive_info->primitive;
5144 points=(PointInfo *) RelinquishMagickMemory(points);
5145 coefficients=(double *) RelinquishMagickMemory(coefficients);
5148 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5149 const PointInfo end)
5160 alpha=end.x-start.x;
5162 radius=hypot((double) alpha,(double) beta);
5163 offset.x=(double) radius;
5164 offset.y=(double) radius;
5167 TraceEllipse(primitive_info,start,offset,degrees);
5170 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5171 const PointInfo stop,const PointInfo degrees)
5182 register PrimitiveInfo
5189 Ellipses are just short segmented polys.
5191 if ((stop.x == 0.0) && (stop.y == 0.0))
5193 TracePoint(primitive_info,start);
5196 delta=2.0/MagickMax(stop.x,stop.y);
5197 step=(double) (MagickPI/8.0);
5198 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5199 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5200 angle.x=DegreesToRadians(degrees.x);
5202 while (y < degrees.x)
5204 angle.y=(double) DegreesToRadians(y);
5205 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5207 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5208 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5209 TracePoint(p,point);
5212 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5213 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5214 TracePoint(p,point);
5216 primitive_info->coordinates=(size_t) (p-primitive_info);
5217 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5219 p->primitive=primitive_info->primitive;
5224 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5225 const PointInfo end)
5227 TracePoint(primitive_info,start);
5228 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5229 (fabs(start.y-end.y) < MagickEpsilon))
5231 primitive_info->primitive=PointPrimitive;
5232 primitive_info->coordinates=1;
5235 TracePoint(primitive_info+1,end);
5236 (primitive_info+1)->primitive=primitive_info->primitive;
5237 primitive_info->coordinates=2;
5240 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5243 token[MaxTextExtent];
5265 register PrimitiveInfo
5282 number_coordinates=0;
5284 (void) ResetMagickMemory(points,0,sizeof(*points));
5285 primitive_type=primitive_info->primitive;
5287 for (p=path; *p != '\0'; )
5289 while (isspace((int) ((unsigned char) *p)) != 0)
5293 last_attribute=attribute;
5294 attribute=(int) (*p++);
5315 GetMagickToken(p,&p,token);
5317 GetMagickToken(p,&p,token);
5318 arc.x=StringToDouble(token,(char **) NULL);
5319 GetMagickToken(p,&p,token);
5321 GetMagickToken(p,&p,token);
5322 arc.y=StringToDouble(token,(char **) NULL);
5323 GetMagickToken(p,&p,token);
5325 GetMagickToken(p,&p,token);
5326 angle=StringToDouble(token,(char **) NULL);
5327 GetMagickToken(p,&p,token);
5329 GetMagickToken(p,&p,token);
5330 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5331 GetMagickToken(p,&p,token);
5333 GetMagickToken(p,&p,token);
5334 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5335 GetMagickToken(p,&p,token);
5337 GetMagickToken(p,&p,token);
5338 x=StringToDouble(token,(char **) NULL);
5339 GetMagickToken(p,&p,token);
5341 GetMagickToken(p,&p,token);
5342 y=StringToDouble(token,(char **) NULL);
5343 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5344 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5345 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5348 while (isspace((int) ((unsigned char) *p)) != 0)
5352 } while (IsPoint(p) != MagickFalse);
5359 Compute bezier points.
5364 for (i=1; i < 4; i++)
5366 GetMagickToken(p,&p,token);
5368 GetMagickToken(p,&p,token);
5369 x=StringToDouble(token,(char **) NULL);
5370 GetMagickToken(p,&p,token);
5372 GetMagickToken(p,&p,token);
5373 y=StringToDouble(token,(char **) NULL);
5374 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5375 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5378 for (i=0; i < 4; i++)
5379 (q+i)->point=points[i];
5383 } while (IsPoint(p) != MagickFalse);
5391 GetMagickToken(p,&p,token);
5393 GetMagickToken(p,&p,token);
5394 x=StringToDouble(token,(char **) NULL);
5395 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5396 TracePoint(q,point);
5398 } while (IsPoint(p) != MagickFalse);
5406 GetMagickToken(p,&p,token);
5408 GetMagickToken(p,&p,token);
5409 x=StringToDouble(token,(char **) NULL);
5410 GetMagickToken(p,&p,token);
5412 GetMagickToken(p,&p,token);
5413 y=StringToDouble(token,(char **) NULL);
5414 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5415 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5416 TracePoint(q,point);
5418 } while (IsPoint(p) != MagickFalse);
5424 if (q != primitive_info)
5426 primitive_info->coordinates=(size_t) (q-primitive_info);
5427 number_coordinates+=primitive_info->coordinates;
5433 GetMagickToken(p,&p,token);
5435 GetMagickToken(p,&p,token);
5436 x=StringToDouble(token,(char **) NULL);
5437 GetMagickToken(p,&p,token);
5439 GetMagickToken(p,&p,token);
5440 y=StringToDouble(token,(char **) NULL);
5441 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5442 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5446 TracePoint(q,point);
5448 if ((i != 0) && (attribute == (int) 'M'))
5450 TracePoint(q,point);
5453 } while (IsPoint(p) != MagickFalse);
5460 Compute bezier points.
5465 for (i=1; i < 3; i++)
5467 GetMagickToken(p,&p,token);
5469 GetMagickToken(p,&p,token);
5470 x=StringToDouble(token,(char **) NULL);
5471 GetMagickToken(p,&p,token);
5473 GetMagickToken(p,&p,token);
5474 y=StringToDouble(token,(char **) NULL);
5477 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5478 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5481 for (i=0; i < 3; i++)
5482 (q+i)->point=points[i];
5486 } while (IsPoint(p) != MagickFalse);
5493 Compute bezier points.
5497 points[0]=points[3];
5498 points[1].x=2.0*points[3].x-points[2].x;
5499 points[1].y=2.0*points[3].y-points[2].y;
5500 for (i=2; i < 4; i++)
5502 GetMagickToken(p,&p,token);
5504 GetMagickToken(p,&p,token);
5505 x=StringToDouble(token,(char **) NULL);
5506 GetMagickToken(p,&p,token);
5508 GetMagickToken(p,&p,token);
5509 y=StringToDouble(token,(char **) NULL);
5512 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5513 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5516 if (strchr("CcSs",last_attribute) == (char *) NULL)
5518 points[0]=points[2];
5519 points[1]=points[3];
5521 for (i=0; i < 4; i++)
5522 (q+i)->point=points[i];
5526 } while (IsPoint(p) != MagickFalse);
5533 Compute bezier points.
5537 points[0]=points[2];
5538 points[1].x=2.0*points[2].x-points[1].x;
5539 points[1].y=2.0*points[2].y-points[1].y;
5540 for (i=2; i < 3; i++)
5542 GetMagickToken(p,&p,token);
5544 GetMagickToken(p,&p,token);
5545 x=StringToDouble(token,(char **) NULL);
5546 GetMagickToken(p,&p,token);
5548 GetMagickToken(p,&p,token);
5549 y=StringToDouble(token,(char **) NULL);
5550 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5551 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5554 if (strchr("QqTt",last_attribute) == (char *) NULL)
5556 points[0]=points[2];
5557 points[1]=points[3];
5559 for (i=0; i < 3; i++)
5560 (q+i)->point=points[i];
5564 } while (IsPoint(p) != MagickFalse);
5572 GetMagickToken(p,&p,token);
5574 GetMagickToken(p,&p,token);
5575 y=StringToDouble(token,(char **) NULL);
5576 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5577 TracePoint(q,point);
5579 } while (IsPoint(p) != MagickFalse);
5586 TracePoint(q,point);
5588 primitive_info->coordinates=(size_t) (q-primitive_info);
5589 number_coordinates+=primitive_info->coordinates;
5596 if (isalpha((int) ((unsigned char) attribute)) != 0)
5597 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5603 primitive_info->coordinates=(size_t) (q-primitive_info);
5604 number_coordinates+=primitive_info->coordinates;
5605 for (i=0; i < (ssize_t) number_coordinates; i++)
5608 q->primitive=primitive_type;
5610 q->method=FillToBorderMethod;
5613 return(number_coordinates);
5616 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5617 const PointInfo end)
5622 register PrimitiveInfo
5629 TracePoint(p,start);
5633 TracePoint(p,point);
5639 TracePoint(p,point);
5641 TracePoint(p,start);
5643 primitive_info->coordinates=(size_t) (p-primitive_info);
5644 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5646 p->primitive=primitive_info->primitive;
5651 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5652 const PointInfo start,const PointInfo end,PointInfo arc)
5659 register PrimitiveInfo
5666 offset.x=fabs(end.x-start.x);
5667 offset.y=fabs(end.y-start.y);
5668 if (arc.x > (0.5*offset.x))
5670 if (arc.y > (0.5*offset.y))
5672 point.x=start.x+offset.x-arc.x;
5673 point.y=start.y+arc.y;
5676 TraceEllipse(p,point,arc,degrees);
5678 point.x=start.x+offset.x-arc.x;
5679 point.y=start.y+offset.y-arc.y;
5682 TraceEllipse(p,point,arc,degrees);
5684 point.x=start.x+arc.x;
5685 point.y=start.y+offset.y-arc.y;
5688 TraceEllipse(p,point,arc,degrees);
5690 point.x=start.x+arc.x;
5691 point.y=start.y+arc.y;
5694 TraceEllipse(p,point,arc,degrees);
5696 TracePoint(p,primitive_info->point);
5698 primitive_info->coordinates=(size_t) (p-primitive_info);
5699 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5701 p->primitive=primitive_info->primitive;
5706 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5707 const size_t number_vertices,const double offset)
5724 for (i=1; i < (ssize_t) number_vertices; i++)
5726 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5727 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5728 if ((fabs((double) dx) >= MagickEpsilon) ||
5729 (fabs((double) dy) >= MagickEpsilon))
5732 if (i == (ssize_t) number_vertices)
5733 i=(ssize_t) number_vertices-1L;
5734 distance=hypot((double) dx,(double) dy);
5735 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5736 dx*(distance+offset)/distance);
5737 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5738 dy*(distance+offset)/distance);
5739 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5741 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5742 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5743 if ((fabs((double) dx) >= MagickEpsilon) ||
5744 (fabs((double) dy) >= MagickEpsilon))
5747 distance=hypot((double) dx,(double) dy);
5748 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5749 dx*(distance+offset)/distance);
5750 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5751 dy*(distance+offset)/distance);
5754 static inline double DrawEpsilonReciprocal(const double x)
5756 #define DrawEpsilon ((double) 1.0e-10)
5758 double sign = x < (double) 0.0 ? (double) -1.0 :
5760 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5761 (double) 1.0/DrawEpsilon));
5764 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5765 const PrimitiveInfo *primitive_info)
5767 typedef struct _LineSegment
5819 number_vertices=primitive_info->coordinates;
5820 max_strokes=2*number_vertices+6*BezierQuantum+360;
5821 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5823 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5825 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5826 number_vertices+2UL,sizeof(*polygon_primitive));
5827 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5828 (polygon_primitive == (PrimitiveInfo *) NULL))
5829 return((PrimitiveInfo *) NULL);
5830 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5831 number_vertices*sizeof(*polygon_primitive));
5833 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5834 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5835 MagickTrue : MagickFalse;
5836 if ((draw_info->linejoin == RoundJoin) ||
5837 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5839 polygon_primitive[number_vertices]=primitive_info[1];
5842 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5844 Compute the slope for the first line segment, p.
5848 for (n=1; n < (ssize_t) number_vertices; n++)
5850 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5851 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5852 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5855 if (n == (ssize_t) number_vertices)
5856 n=(ssize_t) number_vertices-1L;
5857 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5858 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5859 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5860 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5862 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5863 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5864 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5865 offset.y=(double) (offset.x*inverse_slope.p);
5866 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5868 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5869 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5870 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5871 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5872 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5873 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5874 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5875 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5879 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5880 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5881 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5882 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5883 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5884 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5885 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5886 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5889 Create strokes for the line join attribute: bevel, miter, round.
5893 path_q[p++]=box_q[0];
5894 path_p[q++]=box_p[0];
5895 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5898 Compute the slope for this line segment, q.
5900 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5901 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5902 dot_product=dx.q*dx.q+dy.q*dy.q;
5903 if (dot_product < 0.25)
5905 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5906 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5907 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5908 offset.y=(double) (offset.x*inverse_slope.q);
5909 dot_product=dy.q*offset.x-dx.q*offset.y;
5910 if (dot_product > 0.0)
5912 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5913 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5914 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5915 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5916 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5917 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5918 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5919 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5923 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5924 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5925 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5926 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5927 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5928 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5929 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5930 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5932 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5939 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5940 box_p[3].y)/(slope.p-slope.q));
5941 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5942 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5943 box_q[3].y)/(slope.p-slope.q));
5944 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5946 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5948 max_strokes+=6*BezierQuantum+360;
5949 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5951 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5953 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5955 polygon_primitive=(PrimitiveInfo *)
5956 RelinquishMagickMemory(polygon_primitive);
5957 return((PrimitiveInfo *) NULL);
5960 dot_product=dx.q*dy.p-dx.p*dy.q;
5961 if (dot_product <= 0.0)
5962 switch (draw_info->linejoin)
5966 path_q[q++]=box_q[1];
5967 path_q[q++]=box_q[2];
5968 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5969 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5970 if (dot_product <= miterlimit)
5971 path_p[p++]=box_p[4];
5974 path_p[p++]=box_p[1];
5975 path_p[p++]=box_p[2];
5981 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5982 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5983 if (dot_product <= miterlimit)
5985 path_q[q++]=box_q[4];
5986 path_p[p++]=box_p[4];
5990 path_q[q++]=box_q[1];
5991 path_q[q++]=box_q[2];
5992 path_p[p++]=box_p[1];
5993 path_p[p++]=box_p[2];
5999 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6000 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6001 if (dot_product <= miterlimit)
6002 path_p[p++]=box_p[4];
6005 path_p[p++]=box_p[1];
6006 path_p[p++]=box_p[2];
6008 center=polygon_primitive[n].point;
6009 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6010 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6011 if (theta.q < theta.p)
6012 theta.q+=(double) (2.0*MagickPI);
6013 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6014 (2.0*sqrt((double) (1.0/mid)))));
6015 path_q[q].x=box_q[1].x;
6016 path_q[q].y=box_q[1].y;
6018 for (j=1; j < (ssize_t) arc_segments; j++)
6020 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6021 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6022 (theta.p+delta_theta),DegreesToRadians(360.0))));
6023 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6024 (theta.p+delta_theta),DegreesToRadians(360.0))));
6027 path_q[q++]=box_q[2];
6034 switch (draw_info->linejoin)
6038 path_p[p++]=box_p[1];
6039 path_p[p++]=box_p[2];
6040 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6041 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6042 if (dot_product <= miterlimit)
6043 path_q[q++]=box_q[4];
6046 path_q[q++]=box_q[1];
6047 path_q[q++]=box_q[2];
6053 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6054 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6055 if (dot_product <= miterlimit)
6057 path_q[q++]=box_q[4];
6058 path_p[p++]=box_p[4];
6062 path_q[q++]=box_q[1];
6063 path_q[q++]=box_q[2];
6064 path_p[p++]=box_p[1];
6065 path_p[p++]=box_p[2];
6071 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6072 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6073 if (dot_product <= miterlimit)
6074 path_q[q++]=box_q[4];
6077 path_q[q++]=box_q[1];
6078 path_q[q++]=box_q[2];
6080 center=polygon_primitive[n].point;
6081 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6082 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6083 if (theta.p < theta.q)
6084 theta.p+=(double) (2.0*MagickPI);
6085 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6086 (2.0*sqrt((double) (1.0/mid)))));
6087 path_p[p++]=box_p[1];
6088 for (j=1; j < (ssize_t) arc_segments; j++)
6090 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6091 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6092 (theta.p+delta_theta),DegreesToRadians(360.0))));
6093 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6094 (theta.p+delta_theta),DegreesToRadians(360.0))));
6097 path_p[p++]=box_p[2];
6104 inverse_slope.p=inverse_slope.q;
6113 path_p[p++]=box_p[1];
6114 path_q[q++]=box_q[1];
6116 Trace stroked polygon.
6118 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6119 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6120 if (stroke_polygon != (PrimitiveInfo *) NULL)
6122 for (i=0; i < (ssize_t) p; i++)
6124 stroke_polygon[i]=polygon_primitive[0];
6125 stroke_polygon[i].point=path_p[i];
6127 if (closed_path != MagickFalse)
6129 stroke_polygon[i]=polygon_primitive[0];
6130 stroke_polygon[i].point=stroke_polygon[0].point;
6133 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6135 stroke_polygon[i]=polygon_primitive[0];
6136 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6138 if (closed_path != MagickFalse)
6140 stroke_polygon[i]=polygon_primitive[0];
6141 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6144 stroke_polygon[i]=polygon_primitive[0];
6145 stroke_polygon[i].point=stroke_polygon[0].point;
6147 stroke_polygon[i].primitive=UndefinedPrimitive;
6148 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6150 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6151 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6152 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6153 return(stroke_polygon);