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;
1964 if (LocaleCompare("direction",keyword) == 0)
1969 GetMagickToken(q,&q,token);
1970 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1972 if (direction == -1)
1975 graphic_context[n]->direction=(DirectionType) direction;
1984 if (LocaleCompare("ellipse",keyword) == 0)
1986 primitive_type=EllipsePrimitive;
1989 if (LocaleCompare("encoding",keyword) == 0)
1991 GetMagickToken(q,&q,token);
1992 (void) CloneString(&graphic_context[n]->encoding,token);
2001 if (LocaleCompare("fill",keyword) == 0)
2003 GetMagickToken(q,&q,token);
2004 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2005 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2006 (void) DrawPatternPath(image,draw_info,token,
2007 &graphic_context[n]->fill_pattern,exception);
2010 status&=QueryColorCompliance(token,AllCompliance,
2011 &graphic_context[n]->fill,exception);
2012 if (status == MagickFalse)
2017 pattern_info=AcquireImageInfo();
2018 (void) CopyMagickString(pattern_info->filename,token,
2020 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2022 CatchException(exception);
2023 pattern_info=DestroyImageInfo(pattern_info);
2028 if (LocaleCompare("fill-alpha",keyword) == 0)
2030 GetMagickToken(q,&q,token);
2031 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2032 graphic_context[n]->fill.alpha=(double) QuantumRange*
2033 factor*StringToDouble(token,(char **) NULL);
2036 if (LocaleCompare("fill-rule",keyword) == 0)
2041 GetMagickToken(q,&q,token);
2042 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2044 if (fill_rule == -1)
2047 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2050 if (LocaleCompare("font",keyword) == 0)
2052 GetMagickToken(q,&q,token);
2053 (void) CloneString(&graphic_context[n]->font,token);
2054 if (LocaleCompare("none",token) == 0)
2055 graphic_context[n]->font=(char *)
2056 RelinquishMagickMemory(graphic_context[n]->font);
2059 if (LocaleCompare("font-family",keyword) == 0)
2061 GetMagickToken(q,&q,token);
2062 (void) CloneString(&graphic_context[n]->family,token);
2065 if (LocaleCompare("font-size",keyword) == 0)
2067 GetMagickToken(q,&q,token);
2068 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2071 if (LocaleCompare("font-stretch",keyword) == 0)
2076 GetMagickToken(q,&q,token);
2077 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2081 graphic_context[n]->stretch=(StretchType) stretch;
2084 if (LocaleCompare("font-style",keyword) == 0)
2089 GetMagickToken(q,&q,token);
2090 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2094 graphic_context[n]->style=(StyleType) style;
2097 if (LocaleCompare("font-weight",keyword) == 0)
2099 GetMagickToken(q,&q,token);
2100 graphic_context[n]->weight=StringToUnsignedLong(token);
2101 if (LocaleCompare(token,"all") == 0)
2102 graphic_context[n]->weight=0;
2103 if (LocaleCompare(token,"bold") == 0)
2104 graphic_context[n]->weight=700;
2105 if (LocaleCompare(token,"bolder") == 0)
2106 if (graphic_context[n]->weight <= 800)
2107 graphic_context[n]->weight+=100;
2108 if (LocaleCompare(token,"lighter") == 0)
2109 if (graphic_context[n]->weight >= 100)
2110 graphic_context[n]->weight-=100;
2111 if (LocaleCompare(token,"normal") == 0)
2112 graphic_context[n]->weight=400;
2121 if (LocaleCompare("gradient-units",keyword) == 0)
2123 GetMagickToken(q,&q,token);
2126 if (LocaleCompare("gravity",keyword) == 0)
2131 GetMagickToken(q,&q,token);
2132 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2136 graphic_context[n]->gravity=(GravityType) gravity;
2145 if (LocaleCompare("image",keyword) == 0)
2150 primitive_type=ImagePrimitive;
2151 GetMagickToken(q,&q,token);
2152 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2156 graphic_context[n]->compose=(CompositeOperator) compose;
2159 if (LocaleCompare("interline-spacing",keyword) == 0)
2161 GetMagickToken(q,&q,token);
2162 graphic_context[n]->interline_spacing=StringToDouble(token,
2166 if (LocaleCompare("interword-spacing",keyword) == 0)
2168 GetMagickToken(q,&q,token);
2169 graphic_context[n]->interword_spacing=StringToDouble(token,
2179 if (LocaleCompare("kerning",keyword) == 0)
2181 GetMagickToken(q,&q,token);
2182 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2191 if (LocaleCompare("line",keyword) == 0)
2192 primitive_type=LinePrimitive;
2200 if (LocaleCompare("matte",keyword) == 0)
2201 primitive_type=MattePrimitive;
2209 if (LocaleCompare("offset",keyword) == 0)
2211 GetMagickToken(q,&q,token);
2214 if (LocaleCompare("opacity",keyword) == 0)
2216 GetMagickToken(q,&q,token);
2217 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2218 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2219 QuantumScale*graphic_context[n]->alpha)*factor*
2220 StringToDouble(token,(char **) NULL))));
2221 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2222 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2231 if (LocaleCompare("path",keyword) == 0)
2233 primitive_type=PathPrimitive;
2236 if (LocaleCompare("point",keyword) == 0)
2238 primitive_type=PointPrimitive;
2241 if (LocaleCompare("polyline",keyword) == 0)
2243 primitive_type=PolylinePrimitive;
2246 if (LocaleCompare("polygon",keyword) == 0)
2248 primitive_type=PolygonPrimitive;
2251 if (LocaleCompare("pop",keyword) == 0)
2253 GetMagickToken(q,&q,token);
2254 if (LocaleCompare("clip-path",token) == 0)
2256 if (LocaleCompare("defs",token) == 0)
2258 if (LocaleCompare("gradient",token) == 0)
2260 if (LocaleCompare("graphic-context",token) == 0)
2264 (void) ThrowMagickException(exception,GetMagickModule(),
2265 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2269 if (graphic_context[n]->clip_mask != (char *) NULL)
2270 if (LocaleCompare(graphic_context[n]->clip_mask,
2271 graphic_context[n-1]->clip_mask) != 0)
2272 (void) SetImageMask(image,(Image *) NULL,exception);
2273 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2277 if (LocaleCompare("pattern",token) == 0)
2282 if (LocaleCompare("push",keyword) == 0)
2284 GetMagickToken(q,&q,token);
2285 if (LocaleCompare("clip-path",token) == 0)
2288 name[MaxTextExtent];
2290 GetMagickToken(q,&q,token);
2291 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2292 for (p=q; *q != '\0'; )
2294 GetMagickToken(q,&q,token);
2295 if (LocaleCompare(token,"pop") != 0)
2297 GetMagickToken(q,(const char **) NULL,token);
2298 if (LocaleCompare(token,"clip-path") != 0)
2302 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2303 (void) SetImageArtifact(image,name,token);
2304 GetMagickToken(q,&q,token);
2307 if (LocaleCompare("gradient",token) == 0)
2310 key[2*MaxTextExtent],
2311 name[MaxTextExtent],
2312 type[MaxTextExtent];
2317 GetMagickToken(q,&q,token);
2318 (void) CopyMagickString(name,token,MaxTextExtent);
2319 GetMagickToken(q,&q,token);
2320 (void) CopyMagickString(type,token,MaxTextExtent);
2321 GetMagickToken(q,&q,token);
2322 segment.x1=StringToDouble(token,(char **) NULL);
2323 GetMagickToken(q,&q,token);
2325 GetMagickToken(q,&q,token);
2326 segment.y1=StringToDouble(token,(char **) NULL);
2327 GetMagickToken(q,&q,token);
2329 GetMagickToken(q,&q,token);
2330 segment.x2=StringToDouble(token,(char **) NULL);
2331 GetMagickToken(q,&q,token);
2333 GetMagickToken(q,&q,token);
2334 segment.y2=StringToDouble(token,(char **) NULL);
2335 if (LocaleCompare(type,"radial") == 0)
2337 GetMagickToken(q,&q,token);
2339 GetMagickToken(q,&q,token);
2341 for (p=q; *q != '\0'; )
2343 GetMagickToken(q,&q,token);
2344 if (LocaleCompare(token,"pop") != 0)
2346 GetMagickToken(q,(const char **) NULL,token);
2347 if (LocaleCompare(token,"gradient") != 0)
2351 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2352 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2353 graphic_context[n]->affine.ry*segment.y1+
2354 graphic_context[n]->affine.tx;
2355 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2356 graphic_context[n]->affine.sy*segment.y1+
2357 graphic_context[n]->affine.ty;
2358 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2359 graphic_context[n]->affine.ry*segment.y2+
2360 graphic_context[n]->affine.tx;
2361 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2362 graphic_context[n]->affine.sy*segment.y2+
2363 graphic_context[n]->affine.ty;
2364 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2365 (void) SetImageArtifact(image,key,token);
2366 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2367 (void) FormatLocaleString(geometry,MaxTextExtent,
2368 "%gx%g%+.15g%+.15g",
2369 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2370 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2371 bounds.x1,bounds.y1);
2372 (void) SetImageArtifact(image,key,geometry);
2373 GetMagickToken(q,&q,token);
2376 if (LocaleCompare("pattern",token) == 0)
2381 GetMagickToken(q,&q,token);
2382 (void) CopyMagickString(name,token,MaxTextExtent);
2383 GetMagickToken(q,&q,token);
2384 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2386 GetMagickToken(q,&q,token);
2388 GetMagickToken(q,&q,token);
2389 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2391 GetMagickToken(q,&q,token);
2393 GetMagickToken(q,&q,token);
2394 bounds.width=(size_t) floor(StringToDouble(token,
2395 (char **) NULL)+0.5);
2396 GetMagickToken(q,&q,token);
2398 GetMagickToken(q,&q,token);
2399 bounds.height=(size_t) floor(StringToDouble(token,
2400 (char **) NULL)+0.5);
2401 for (p=q; *q != '\0'; )
2403 GetMagickToken(q,&q,token);
2404 if (LocaleCompare(token,"pop") != 0)
2406 GetMagickToken(q,(const char **) NULL,token);
2407 if (LocaleCompare(token,"pattern") != 0)
2411 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2412 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2413 (void) SetImageArtifact(image,key,token);
2414 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2415 (void) FormatLocaleString(geometry,MaxTextExtent,
2416 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2417 bounds.height,(double) bounds.x,(double) bounds.y);
2418 (void) SetImageArtifact(image,key,geometry);
2419 GetMagickToken(q,&q,token);
2422 if (LocaleCompare("graphic-context",token) == 0)
2425 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2426 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2427 if (graphic_context == (DrawInfo **) NULL)
2429 (void) ThrowMagickException(exception,GetMagickModule(),
2430 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2434 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2435 graphic_context[n-1]);
2438 if (LocaleCompare("defs",token) == 0)
2449 if (LocaleCompare("rectangle",keyword) == 0)
2451 primitive_type=RectanglePrimitive;
2454 if (LocaleCompare("rotate",keyword) == 0)
2456 GetMagickToken(q,&q,token);
2457 angle=StringToDouble(token,(char **) NULL);
2458 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2459 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2460 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2461 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2464 if (LocaleCompare("roundRectangle",keyword) == 0)
2466 primitive_type=RoundRectanglePrimitive;
2475 if (LocaleCompare("scale",keyword) == 0)
2477 GetMagickToken(q,&q,token);
2478 affine.sx=StringToDouble(token,(char **) NULL);
2479 GetMagickToken(q,&q,token);
2481 GetMagickToken(q,&q,token);
2482 affine.sy=StringToDouble(token,(char **) NULL);
2485 if (LocaleCompare("skewX",keyword) == 0)
2487 GetMagickToken(q,&q,token);
2488 angle=StringToDouble(token,(char **) NULL);
2489 affine.ry=sin(DegreesToRadians(angle));
2492 if (LocaleCompare("skewY",keyword) == 0)
2494 GetMagickToken(q,&q,token);
2495 angle=StringToDouble(token,(char **) NULL);
2496 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2499 if (LocaleCompare("stop-color",keyword) == 0)
2504 GetMagickToken(q,&q,token);
2505 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2507 (void) GradientImage(image,LinearGradient,ReflectSpread,
2508 &start_color,&stop_color,exception);
2509 start_color=stop_color;
2510 GetMagickToken(q,&q,token);
2513 if (LocaleCompare("stroke",keyword) == 0)
2515 GetMagickToken(q,&q,token);
2516 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2517 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2518 (void) DrawPatternPath(image,draw_info,token,
2519 &graphic_context[n]->stroke_pattern,exception);
2522 status&=QueryColorCompliance(token,AllCompliance,
2523 &graphic_context[n]->stroke,exception);
2524 if (status == MagickFalse)
2529 pattern_info=AcquireImageInfo();
2530 (void) CopyMagickString(pattern_info->filename,token,
2532 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2534 CatchException(exception);
2535 pattern_info=DestroyImageInfo(pattern_info);
2540 if (LocaleCompare("stroke-antialias",keyword) == 0)
2542 GetMagickToken(q,&q,token);
2543 graphic_context[n]->stroke_antialias=
2544 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2547 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2549 if (graphic_context[n]->dash_pattern != (double *) NULL)
2550 graphic_context[n]->dash_pattern=(double *)
2551 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2552 if (IsPoint(q) != MagickFalse)
2558 GetMagickToken(p,&p,token);
2560 GetMagickToken(p,&p,token);
2561 for (x=0; IsPoint(token) != MagickFalse; x++)
2563 GetMagickToken(p,&p,token);
2565 GetMagickToken(p,&p,token);
2567 graphic_context[n]->dash_pattern=(double *)
2568 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2569 sizeof(*graphic_context[n]->dash_pattern));
2570 if (graphic_context[n]->dash_pattern == (double *) NULL)
2572 (void) ThrowMagickException(exception,GetMagickModule(),
2573 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2577 for (j=0; j < x; j++)
2579 GetMagickToken(q,&q,token);
2581 GetMagickToken(q,&q,token);
2582 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2585 if ((x & 0x01) != 0)
2586 for ( ; j < (2*x); j++)
2587 graphic_context[n]->dash_pattern[j]=
2588 graphic_context[n]->dash_pattern[j-x];
2589 graphic_context[n]->dash_pattern[j]=0.0;
2592 GetMagickToken(q,&q,token);
2595 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2597 GetMagickToken(q,&q,token);
2598 graphic_context[n]->dash_offset=StringToDouble(token,
2602 if (LocaleCompare("stroke-linecap",keyword) == 0)
2607 GetMagickToken(q,&q,token);
2608 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2612 graphic_context[n]->linecap=(LineCap) linecap;
2615 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2620 GetMagickToken(q,&q,token);
2621 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2626 graphic_context[n]->linejoin=(LineJoin) linejoin;
2629 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2631 GetMagickToken(q,&q,token);
2632 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2635 if (LocaleCompare("stroke-opacity",keyword) == 0)
2637 GetMagickToken(q,&q,token);
2638 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2639 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2640 factor*StringToDouble(token,(char **) NULL);
2643 if (LocaleCompare("stroke-width",keyword) == 0)
2645 GetMagickToken(q,&q,token);
2646 graphic_context[n]->stroke_width=StringToDouble(token,
2656 if (LocaleCompare("text",keyword) == 0)
2658 primitive_type=TextPrimitive;
2661 if (LocaleCompare("text-align",keyword) == 0)
2666 GetMagickToken(q,&q,token);
2667 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2671 graphic_context[n]->align=(AlignType) align;
2674 if (LocaleCompare("text-anchor",keyword) == 0)
2679 GetMagickToken(q,&q,token);
2680 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2684 graphic_context[n]->align=(AlignType) align;
2687 if (LocaleCompare("text-antialias",keyword) == 0)
2689 GetMagickToken(q,&q,token);
2690 graphic_context[n]->text_antialias=
2691 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2694 if (LocaleCompare("text-undercolor",keyword) == 0)
2696 GetMagickToken(q,&q,token);
2697 (void) QueryColorCompliance(token,AllCompliance,
2698 &graphic_context[n]->undercolor,exception);
2701 if (LocaleCompare("translate",keyword) == 0)
2703 GetMagickToken(q,&q,token);
2704 affine.tx=StringToDouble(token,(char **) NULL);
2705 GetMagickToken(q,&q,token);
2707 GetMagickToken(q,&q,token);
2708 affine.ty=StringToDouble(token,(char **) NULL);
2717 if (LocaleCompare("viewbox",keyword) == 0)
2719 GetMagickToken(q,&q,token);
2720 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2721 (char **) NULL)-0.5);
2722 GetMagickToken(q,&q,token);
2724 GetMagickToken(q,&q,token);
2725 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2726 (char **) NULL)-0.5);
2727 GetMagickToken(q,&q,token);
2729 GetMagickToken(q,&q,token);
2730 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2731 token,(char **) NULL)+0.5);
2732 GetMagickToken(q,&q,token);
2734 GetMagickToken(q,&q,token);
2735 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2736 token,(char **) NULL)+0.5);
2748 if (status == MagickFalse)
2750 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2751 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2753 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2754 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2755 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2756 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2757 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2759 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2762 if (primitive_type == UndefinedPrimitive)
2764 if (image->debug != MagickFalse)
2765 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2770 Parse the primitive attributes.
2774 primitive_info[0].point.x=0.0;
2775 primitive_info[0].point.y=0.0;
2776 for (x=0; *q != '\0'; x++)
2781 if (IsPoint(q) == MagickFalse)
2783 GetMagickToken(q,&q,token);
2784 point.x=StringToDouble(token,(char **) NULL);
2785 GetMagickToken(q,&q,token);
2787 GetMagickToken(q,&q,token);
2788 point.y=StringToDouble(token,(char **) NULL);
2789 GetMagickToken(q,(const char **) NULL,token);
2791 GetMagickToken(q,&q,token);
2792 primitive_info[i].primitive=primitive_type;
2793 primitive_info[i].point=point;
2794 primitive_info[i].coordinates=0;
2795 primitive_info[i].method=FloodfillMethod;
2797 if (i < (ssize_t) number_points)
2800 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2801 (size_t) number_points,sizeof(*primitive_info));
2802 if (primitive_info == (PrimitiveInfo *) NULL)
2804 (void) ThrowMagickException(exception,GetMagickModule(),
2805 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2809 primitive_info[j].primitive=primitive_type;
2810 primitive_info[j].coordinates=(size_t) x;
2811 primitive_info[j].method=FloodfillMethod;
2812 primitive_info[j].text=(char *) NULL;
2814 Circumscribe primitive within a circle.
2816 bounds.x1=primitive_info[j].point.x;
2817 bounds.y1=primitive_info[j].point.y;
2818 bounds.x2=primitive_info[j].point.x;
2819 bounds.y2=primitive_info[j].point.y;
2820 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2822 point=primitive_info[j+k].point;
2823 if (point.x < bounds.x1)
2825 if (point.y < bounds.y1)
2827 if (point.x > bounds.x2)
2829 if (point.y > bounds.y2)
2833 Speculate how many points our primitive might consume.
2835 length=primitive_info[j].coordinates;
2836 switch (primitive_type)
2838 case RectanglePrimitive:
2843 case RoundRectanglePrimitive:
2845 length*=5+8*BezierQuantum;
2848 case BezierPrimitive:
2850 if (primitive_info[j].coordinates > 107)
2851 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2852 "TooManyBezierCoordinates","`%s'",token);
2853 length=BezierQuantum*primitive_info[j].coordinates;
2862 GetMagickToken(q,&q,token);
2865 for (s=token; *s != '\0'; s=t)
2870 value=StringToDouble(s,&t);
2879 length=length*BezierQuantum/2;
2882 case CirclePrimitive:
2884 case EllipsePrimitive:
2891 alpha=bounds.x2-bounds.x1;
2892 beta=bounds.y2-bounds.y1;
2893 radius=hypot((double) alpha,(double) beta);
2894 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2900 if ((size_t) (i+length) >= number_points)
2903 Resize based on speculative points required by primitive.
2905 number_points+=length+1;
2906 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2907 (size_t) number_points,sizeof(*primitive_info));
2908 if (primitive_info == (PrimitiveInfo *) NULL)
2910 (void) ThrowMagickException(exception,GetMagickModule(),
2911 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2916 switch (primitive_type)
2918 case PointPrimitive:
2921 if (primitive_info[j].coordinates != 1)
2926 TracePoint(primitive_info+j,primitive_info[j].point);
2927 i=(ssize_t) (j+primitive_info[j].coordinates);
2932 if (primitive_info[j].coordinates != 2)
2937 TraceLine(primitive_info+j,primitive_info[j].point,
2938 primitive_info[j+1].point);
2939 i=(ssize_t) (j+primitive_info[j].coordinates);
2942 case RectanglePrimitive:
2944 if (primitive_info[j].coordinates != 2)
2949 TraceRectangle(primitive_info+j,primitive_info[j].point,
2950 primitive_info[j+1].point);
2951 i=(ssize_t) (j+primitive_info[j].coordinates);
2954 case RoundRectanglePrimitive:
2956 if (primitive_info[j].coordinates != 3)
2961 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2962 primitive_info[j+1].point,primitive_info[j+2].point);
2963 i=(ssize_t) (j+primitive_info[j].coordinates);
2968 if (primitive_info[j].coordinates != 3)
2970 primitive_type=UndefinedPrimitive;
2973 TraceArc(primitive_info+j,primitive_info[j].point,
2974 primitive_info[j+1].point,primitive_info[j+2].point);
2975 i=(ssize_t) (j+primitive_info[j].coordinates);
2978 case EllipsePrimitive:
2980 if (primitive_info[j].coordinates != 3)
2985 TraceEllipse(primitive_info+j,primitive_info[j].point,
2986 primitive_info[j+1].point,primitive_info[j+2].point);
2987 i=(ssize_t) (j+primitive_info[j].coordinates);
2990 case CirclePrimitive:
2992 if (primitive_info[j].coordinates != 2)
2997 TraceCircle(primitive_info+j,primitive_info[j].point,
2998 primitive_info[j+1].point);
2999 i=(ssize_t) (j+primitive_info[j].coordinates);
3002 case PolylinePrimitive:
3004 case PolygonPrimitive:
3006 primitive_info[i]=primitive_info[j];
3007 primitive_info[i].coordinates=0;
3008 primitive_info[j].coordinates++;
3012 case BezierPrimitive:
3014 if (primitive_info[j].coordinates < 3)
3019 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3020 i=(ssize_t) (j+primitive_info[j].coordinates);
3025 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3028 case ColorPrimitive:
3029 case MattePrimitive:
3034 if (primitive_info[j].coordinates != 1)
3039 GetMagickToken(q,&q,token);
3040 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3044 primitive_info[j].method=(PaintMethod) method;
3049 if (primitive_info[j].coordinates != 1)
3055 GetMagickToken(q,&q,token);
3056 primitive_info[j].text=AcquireString(token);
3059 case ImagePrimitive:
3061 if (primitive_info[j].coordinates != 2)
3066 GetMagickToken(q,&q,token);
3067 primitive_info[j].text=AcquireString(token);
3071 if (primitive_info == (PrimitiveInfo *) NULL)
3073 if (image->debug != MagickFalse)
3074 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3075 if (status == MagickFalse)
3077 primitive_info[i].primitive=UndefinedPrimitive;
3083 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3085 point=primitive_info[i].point;
3086 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3087 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3088 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3089 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3090 point=primitive_info[i].point;
3091 if (point.x < graphic_context[n]->bounds.x1)
3092 graphic_context[n]->bounds.x1=point.x;
3093 if (point.y < graphic_context[n]->bounds.y1)
3094 graphic_context[n]->bounds.y1=point.y;
3095 if (point.x > graphic_context[n]->bounds.x2)
3096 graphic_context[n]->bounds.x2=point.x;
3097 if (point.y > graphic_context[n]->bounds.y2)
3098 graphic_context[n]->bounds.y2=point.y;
3099 if (primitive_info[i].primitive == ImagePrimitive)
3101 if (i >= (ssize_t) number_points)
3102 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3104 if (graphic_context[n]->render != MagickFalse)
3106 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3107 (LocaleCompare(graphic_context[n]->clip_mask,
3108 graphic_context[n-1]->clip_mask) != 0))
3109 status&=DrawClipPath(image,graphic_context[n],
3110 graphic_context[n]->clip_mask,exception);
3111 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3114 if (primitive_info->text != (char *) NULL)
3115 primitive_info->text=(char *) RelinquishMagickMemory(
3116 primitive_info->text);
3117 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3119 if (proceed == MagickFalse)
3124 if (image->debug != MagickFalse)
3125 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3127 Relinquish resources.
3129 token=DestroyString(token);
3130 if (primitive_info != (PrimitiveInfo *) NULL)
3131 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3132 primitive=DestroyString(primitive);
3133 for ( ; n >= 0; n--)
3134 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3135 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3136 if (status == MagickFalse)
3137 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3139 return(status != 0 ? MagickTrue : MagickFalse);
3143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147 % D r a w G r a d i e n t I m a g e %
3151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3153 % DrawGradientImage() draws a linear gradient on the image.
3155 % The format of the DrawGradientImage method is:
3157 % MagickBooleanType DrawGradientImage(Image *image,
3158 % const DrawInfo *draw_info,ExceptionInfo *exception)
3160 % A description of each parameter follows:
3162 % o image: the image.
3164 % o draw_info: the draw info.
3166 % o exception: return any errors or warnings in this structure.
3170 static inline double GetStopColorOffset(const GradientInfo *gradient,
3171 const ssize_t x,const ssize_t y)
3173 switch (gradient->type)
3175 case UndefinedGradient:
3176 case LinearGradient:
3191 gradient_vector=(&gradient->gradient_vector);
3192 p.x=gradient_vector->x2-gradient_vector->x1;
3193 p.y=gradient_vector->y2-gradient_vector->y1;
3194 q.x=(double) x-gradient_vector->x1;
3195 q.y=(double) y-gradient_vector->y1;
3196 length=sqrt(q.x*q.x+q.y*q.y);
3197 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3198 gamma=PerceptibleReciprocal(gamma);
3199 scale=p.x*q.x+p.y*q.y;
3200 offset=gamma*scale*length;
3203 case RadialGradient:
3212 v.x=(double) x-gradient->center.x;
3213 v.y=(double) y-gradient->center.y;
3214 length=sqrt(v.x*v.x+v.y*v.y);
3215 if (gradient->spread == RepeatSpread)
3217 offset=length/gradient->radius;
3224 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3225 const DrawInfo *draw_info,ExceptionInfo *exception)
3255 Draw linear or radial gradient on image.
3257 assert(image != (Image *) NULL);
3258 assert(image->signature == MagickSignature);
3259 if (image->debug != MagickFalse)
3260 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3261 assert(draw_info != (const DrawInfo *) NULL);
3262 gradient=(&draw_info->gradient);
3263 gradient_vector=(&gradient->gradient_vector);
3264 point.x=gradient_vector->x2-gradient_vector->x1;
3265 point.y=gradient_vector->y2-gradient_vector->y1;
3266 length=sqrt(point.x*point.x+point.y*point.y);
3267 bounding_box=gradient->bounding_box;
3269 GetPixelInfo(image,&zero);
3270 image_view=AcquireAuthenticCacheView(image,exception);
3271 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3272 #pragma omp parallel for schedule(static,4) shared(status) \
3273 magick_threads(image,image,1,1)
3275 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3295 if (status == MagickFalse)
3297 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3298 if (q == (Quantum *) NULL)
3305 offset=GetStopColorOffset(gradient,0,y);
3306 if (gradient->type != RadialGradient)
3308 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3310 GetPixelInfoPixel(image,q,&pixel);
3311 switch (gradient->spread)
3313 case UndefinedSpread:
3316 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3317 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3319 offset=GetStopColorOffset(gradient,x,y);
3320 if (gradient->type != RadialGradient)
3323 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3324 if (offset < gradient->stops[i].offset)
3326 if ((offset < 0.0) || (i == 0))
3327 composite=gradient->stops[0].color;
3329 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3330 composite=gradient->stops[gradient->number_stops-1].color;
3335 alpha=(offset-gradient->stops[i].offset)/
3336 (gradient->stops[j].offset-gradient->stops[i].offset);
3337 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3338 &gradient->stops[j].color,alpha,&composite);
3344 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3345 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3347 offset=GetStopColorOffset(gradient,x,y);
3348 if (gradient->type != RadialGradient)
3353 if ((ssize_t) fmod(offset,2.0) == 0)
3354 offset=fmod(offset,1.0);
3356 offset=1.0-fmod(offset,1.0);
3357 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3358 if (offset < gradient->stops[i].offset)
3361 composite=gradient->stops[0].color;
3363 if (i == (ssize_t) gradient->number_stops)
3364 composite=gradient->stops[gradient->number_stops-1].color;
3369 alpha=(offset-gradient->stops[i].offset)/
3370 (gradient->stops[j].offset-gradient->stops[i].offset);
3371 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3372 &gradient->stops[j].color,alpha,&composite);
3384 antialias=MagickFalse;
3386 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3387 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3389 offset=GetStopColorOffset(gradient,x,y);
3390 if (gradient->type == LinearGradient)
3392 repeat=fmod(offset,length);
3394 repeat=length-fmod(-repeat,length);
3396 repeat=fmod(offset,length);
3397 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3398 MagickTrue : MagickFalse;
3399 offset=repeat/length;
3403 repeat=fmod(offset,gradient->radius);
3405 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3407 repeat=fmod(offset,gradient->radius);
3408 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3410 offset=repeat/gradient->radius;
3413 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3414 if (offset < gradient->stops[i].offset)
3417 composite=gradient->stops[0].color;
3419 if (i == (ssize_t) gradient->number_stops)
3420 composite=gradient->stops[gradient->number_stops-1].color;
3425 alpha=(offset-gradient->stops[i].offset)/
3426 (gradient->stops[j].offset-gradient->stops[i].offset);
3427 if (antialias != MagickFalse)
3429 if (gradient->type == LinearGradient)
3430 alpha=length-repeat;
3432 alpha=gradient->radius-repeat;
3434 j=(ssize_t) gradient->number_stops-1L;
3436 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3437 &gradient->stops[j].color,alpha,&composite);
3442 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3444 SetPixelInfoPixel(image,&pixel,q);
3445 q+=GetPixelChannels(image);
3447 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3450 image_view=DestroyCacheView(image_view);
3455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3459 % D r a w P a t t e r n P a t h %
3463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3465 % DrawPatternPath() draws a pattern.
3467 % The format of the DrawPatternPath method is:
3469 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3470 % const char *name,Image **pattern,ExceptionInfo *exception)
3472 % A description of each parameter follows:
3474 % o image: the image.
3476 % o draw_info: the draw info.
3478 % o name: the pattern name.
3480 % o image: the image.
3482 % o exception: return any errors or warnings in this structure.
3485 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3486 const DrawInfo *draw_info,const char *name,Image **pattern,
3487 ExceptionInfo *exception)
3490 property[MaxTextExtent];
3505 assert(image != (Image *) NULL);
3506 assert(image->signature == MagickSignature);
3507 if (image->debug != MagickFalse)
3508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3509 assert(draw_info != (const DrawInfo *) NULL);
3510 assert(name != (const char *) NULL);
3511 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3512 path=GetImageArtifact(image,property);
3513 if (path == (const char *) NULL)
3514 return(MagickFalse);
3515 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3516 geometry=GetImageArtifact(image,property);
3517 if (geometry == (const char *) NULL)
3518 return(MagickFalse);
3519 if ((*pattern) != (Image *) NULL)
3520 *pattern=DestroyImage(*pattern);
3521 image_info=AcquireImageInfo();
3522 image_info->size=AcquireString(geometry);
3523 *pattern=AcquireImage(image_info,exception);
3524 image_info=DestroyImageInfo(image_info);
3525 (void) QueryColorCompliance("#000000ff",AllCompliance,
3526 &(*pattern)->background_color,exception);
3527 (void) SetImageBackgroundColor(*pattern,exception);
3528 if (image->debug != MagickFalse)
3529 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3530 "begin pattern-path %s %s",name,geometry);
3531 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3532 clone_info->fill_pattern=NewImageList();
3533 clone_info->stroke_pattern=NewImageList();
3534 (void) CloneString(&clone_info->primitive,path);
3535 status=DrawImage(*pattern,clone_info,exception);
3536 clone_info=DestroyDrawInfo(clone_info);
3537 if (image->debug != MagickFalse)
3538 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3547 + D r a w P o l y g o n P r i m i t i v e %
3551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3553 % DrawPolygonPrimitive() draws a polygon on the image.
3555 % The format of the DrawPolygonPrimitive method is:
3557 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3558 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3559 % ExceptionInfo *exception)
3561 % A description of each parameter follows:
3563 % o image: the image.
3565 % o draw_info: the draw info.
3567 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3569 % o exception: return any errors or warnings in this structure.
3573 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3578 assert(polygon_info != (PolygonInfo **) NULL);
3579 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3580 if (polygon_info[i] != (PolygonInfo *) NULL)
3581 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3582 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3583 return(polygon_info);
3586 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3587 const PrimitiveInfo *primitive_info)
3590 *restrict path_info;
3601 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3602 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3603 sizeof(*polygon_info));
3604 if (polygon_info == (PolygonInfo **) NULL)
3605 return((PolygonInfo **) NULL);
3606 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3607 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3608 if (path_info == (PathInfo *) NULL)
3609 return(DestroyPolygonThreadSet(polygon_info));
3610 for (i=0; i < (ssize_t) number_threads; i++)
3612 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3613 if (polygon_info[i] == (PolygonInfo *) NULL)
3614 return(DestroyPolygonThreadSet(polygon_info));
3616 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3617 return(polygon_info);
3620 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3621 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3622 const ssize_t y,double *stroke_alpha)
3633 register const PointInfo
3647 Compute fill & stroke opacity for this (x,y) point.
3651 p=polygon_info->edges;
3652 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3654 if ((double) y <= (p->bounds.y1-mid-0.5))
3656 if ((double) y > (p->bounds.y2+mid+0.5))
3658 (void) DestroyEdge(polygon_info,(size_t) j);
3661 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3662 ((double) x > (p->bounds.x2+mid+0.5)))
3664 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3665 for ( ; i < (ssize_t) p->number_points; i++)
3667 if ((double) y <= (p->points[i-1].y-mid-0.5))
3669 if ((double) y > (p->points[i].y+mid+0.5))
3671 if (p->scanline != (double) y)
3673 p->scanline=(double) y;
3674 p->highwater=(size_t) i;
3677 Compute distance between a point and an edge.
3680 delta.x=(q+1)->x-q->x;
3681 delta.y=(q+1)->y-q->y;
3682 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3685 delta.x=(double) x-q->x;
3686 delta.y=(double) y-q->y;
3687 distance=delta.x*delta.x+delta.y*delta.y;
3691 alpha=delta.x*delta.x+delta.y*delta.y;
3694 delta.x=(double) x-(q+1)->x;
3695 delta.y=(double) y-(q+1)->y;
3696 distance=delta.x*delta.x+delta.y*delta.y;
3701 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3702 distance=alpha*beta*beta;
3706 Compute stroke & subpath opacity.
3709 if (p->ghostline == MagickFalse)
3712 if ((*stroke_alpha < 1.0) &&
3713 (distance <= ((alpha+0.25)*(alpha+0.25))))
3716 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3721 if (distance != 1.0)
3722 beta=sqrt((double) distance);
3724 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3725 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3729 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3731 if (distance <= 0.0)
3741 if (distance != 1.0)
3742 beta=sqrt(distance);
3745 if (subpath_alpha < (alpha*alpha))
3746 subpath_alpha=alpha*alpha;
3750 Compute fill opacity.
3752 if (fill == MagickFalse)
3754 if (subpath_alpha >= 1.0)
3757 Determine winding number.
3760 p=polygon_info->edges;
3761 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3763 if ((double) y <= p->bounds.y1)
3765 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3767 if ((double) x > p->bounds.x2)
3769 winding_number+=p->direction ? 1 : -1;
3772 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3773 for ( ; i < (ssize_t) p->number_points; i++)
3774 if ((double) y <= p->points[i].y)
3777 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3778 winding_number+=p->direction ? 1 : -1;
3780 if (fill_rule != NonZeroRule)
3782 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3786 if (MagickAbsoluteValue(winding_number) != 0)
3788 return(subpath_alpha);
3791 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3792 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3793 ExceptionInfo *exception)
3806 **restrict polygon_info;
3823 Compute bounding box.
3825 assert(image != (Image *) NULL);
3826 assert(image->signature == MagickSignature);
3827 if (image->debug != MagickFalse)
3828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3829 assert(draw_info != (DrawInfo *) NULL);
3830 assert(draw_info->signature == MagickSignature);
3831 assert(primitive_info != (PrimitiveInfo *) NULL);
3832 if (primitive_info->coordinates == 0)
3834 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3835 if (polygon_info == (PolygonInfo **) NULL)
3836 return(MagickFalse);
3837 DisableMSCWarning(4127)
3839 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3841 if (image->debug != MagickFalse)
3842 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3843 fill=(primitive_info->method == FillToBorderMethod) ||
3844 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3845 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3846 bounds=polygon_info[0]->edges[0].bounds;
3847 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3849 p=polygon_info[0]->edges+i;
3850 if (p->bounds.x1 < bounds.x1)
3851 bounds.x1=p->bounds.x1;
3852 if (p->bounds.y1 < bounds.y1)
3853 bounds.y1=p->bounds.y1;
3854 if (p->bounds.x2 > bounds.x2)
3855 bounds.x2=p->bounds.x2;
3856 if (p->bounds.y2 > bounds.y2)
3857 bounds.y2=p->bounds.y2;
3859 bounds.x1-=(mid+1.0);
3860 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3861 image->columns ? (double) image->columns-1 : bounds.x1;
3862 bounds.y1-=(mid+1.0);
3863 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3864 image->rows ? (double) image->rows-1 : bounds.y1;
3865 bounds.x2+=(mid+1.0);
3866 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3867 image->columns ? (double) image->columns-1 : bounds.x2;
3868 bounds.y2+=(mid+1.0);
3869 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3870 image->rows ? (double) image->rows-1 : bounds.y2;
3872 image_view=AcquireAuthenticCacheView(image,exception);
3873 if (primitive_info->coordinates == 1)
3878 start=(ssize_t) ceil(bounds.y1-0.5);
3879 stop=(ssize_t) floor(bounds.y2+0.5);
3880 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3881 #pragma omp parallel for schedule(static,4) shared(status) \
3882 magick_threads(image,image,1,1)
3884 for (y=start; y <= stop; y++)
3902 if (status == MagickFalse)
3904 start=(ssize_t) ceil(bounds.x1-0.5);
3905 stop=(ssize_t) floor(bounds.x2+0.5);
3907 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3909 if (q == (Quantum *) NULL)
3914 GetPixelInfo(image,&pixel);
3915 for ( ; x <= stop; x++)
3917 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3918 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3920 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3921 SetPixelInfoPixel(image,&pixel,q);
3923 q+=GetPixelChannels(image);
3925 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3926 if (sync == MagickFalse)
3929 image_view=DestroyCacheView(image_view);
3930 polygon_info=DestroyPolygonThreadSet(polygon_info);
3931 if (image->debug != MagickFalse)
3932 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3933 " end draw-polygon");
3937 Draw polygon or line.
3939 if (image->alpha_trait != BlendPixelTrait)
3940 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3941 start=(ssize_t) ceil(bounds.y1-0.5);
3942 stop=(ssize_t) floor(bounds.y2+0.5);
3943 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3944 #pragma omp parallel for schedule(static,4) shared(status) \
3945 magick_threads(image,image,1,1)
3947 for (y=start; y <= stop; y++)
3950 id = GetOpenMPThreadId();
3970 if (status == MagickFalse)
3972 start=(ssize_t) ceil(bounds.x1-0.5);
3973 stop=(ssize_t) floor(bounds.x2+0.5);
3974 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3976 if (q == (Quantum *) NULL)
3981 for (x=start; x <= stop; x++)
3986 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3988 if (draw_info->stroke_antialias == MagickFalse)
3990 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3991 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3993 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3994 fill_alpha=fill_alpha*fill_color.alpha;
3995 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
3996 GetPixelAlpha(image,q),q);
3997 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3998 stroke_alpha=stroke_alpha*stroke_color.alpha;
3999 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4000 GetPixelAlpha(image,q),q);
4001 q+=GetPixelChannels(image);
4003 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4006 image_view=DestroyCacheView(image_view);
4007 polygon_info=DestroyPolygonThreadSet(polygon_info);
4008 if (image->debug != MagickFalse)
4009 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4018 % D r a w P r i m i t i v e %
4022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4024 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4026 % The format of the DrawPrimitive method is:
4028 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4029 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4031 % A description of each parameter follows:
4033 % o image: the image.
4035 % o draw_info: the draw info.
4037 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4039 % o exception: return any errors or warnings in this structure.
4043 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4069 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4070 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4071 switch (primitive_info->primitive)
4073 case PointPrimitive:
4075 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4076 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4077 methods[primitive_info->method]);
4080 case ColorPrimitive:
4082 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4083 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4084 methods[primitive_info->method]);
4087 case MattePrimitive:
4089 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4090 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4091 methods[primitive_info->method]);
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4097 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4100 case ImagePrimitive:
4102 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4103 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4110 p=primitive_info[0].point;
4113 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4115 point=primitive_info[i].point;
4116 if (coordinates <= 0)
4118 coordinates=(ssize_t) primitive_info[i].coordinates;
4119 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4120 " begin open (%.20g)",(double) coordinates);
4123 point=primitive_info[i].point;
4124 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4125 (fabs(q.y-point.y) >= MagickEpsilon))
4126 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4127 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4129 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4130 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4133 if (coordinates > 0)
4135 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4136 (fabs(p.y-point.y) >= MagickEpsilon))
4137 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4138 (double) coordinates);
4140 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4141 (double) coordinates);
4145 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4146 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4147 ExceptionInfo *exception)
4162 if (image->debug != MagickFalse)
4164 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4165 " begin draw-primitive");
4166 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4167 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4168 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4169 draw_info->affine.tx,draw_info->affine.ty);
4171 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4172 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4173 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4174 (void) SetImageColorspace(image,sRGBColorspace,exception);
4176 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4177 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4178 image_view=AcquireAuthenticCacheView(image,exception);
4179 switch (primitive_info->primitive)
4181 case PointPrimitive:
4189 if ((y < 0) || (y >= (ssize_t) image->rows))
4191 if ((x < 0) || (x >= (ssize_t) image->columns))
4193 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4194 if (q == (Quantum *) NULL)
4196 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4197 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4198 (double) GetPixelAlpha(image,q),q);
4199 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4202 case ColorPrimitive:
4204 switch (primitive_info->method)
4215 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4216 if (q == (Quantum *) NULL)
4218 GetPixelInfo(image,&pixel);
4219 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4220 SetPixelInfoPixel(image,&pixel,q);
4221 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4233 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4235 for (y=0; y < (ssize_t) image->rows; y++)
4240 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4242 if (q == (Quantum *) NULL)
4244 for (x=0; x < (ssize_t) image->columns; x++)
4246 GetPixelInfoPixel(image,q,&pixel);
4247 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4249 q+=GetPixelChannels(image);
4252 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4253 SetPixelInfoPixel(image,&pixel,q);
4254 q+=GetPixelChannels(image);
4256 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4257 if (sync == MagickFalse)
4262 case FloodfillMethod:
4263 case FillToBorderMethod:
4268 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4270 if (primitive_info->method == FillToBorderMethod)
4272 target.red=(double) draw_info->border_color.red;
4273 target.green=(double) draw_info->border_color.green;
4274 target.blue=(double) draw_info->border_color.blue;
4276 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4277 primitive_info->method == FloodfillMethod ? MagickFalse :
4278 MagickTrue,exception);
4289 GetPixelInfo(image,&pixel);
4290 for (y=0; y < (ssize_t) image->rows; y++)
4298 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4300 if (q == (Quantum *) NULL)
4302 for (x=0; x < (ssize_t) image->columns; x++)
4304 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4305 SetPixelInfoPixel(image,&pixel,q);
4306 q+=GetPixelChannels(image);
4308 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4309 if (sync == MagickFalse)
4317 case MattePrimitive:
4319 if (image->alpha_trait != BlendPixelTrait)
4320 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4321 switch (primitive_info->method)
4332 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4333 if (q == (Quantum *) NULL)
4335 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4336 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4337 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4349 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4351 for (y=0; y < (ssize_t) image->rows; y++)
4359 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4361 if (q == (Quantum *) NULL)
4363 for (x=0; x < (ssize_t) image->columns; x++)
4365 GetPixelInfoPixel(image,q,&pixel);
4366 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4368 q+=GetPixelChannels(image);
4371 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4372 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4373 q+=GetPixelChannels(image);
4375 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4376 if (sync == MagickFalse)
4381 case FloodfillMethod:
4382 case FillToBorderMethod:
4390 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4392 if (primitive_info->method == FillToBorderMethod)
4394 target.red=(double) draw_info->border_color.red;
4395 target.green=(double) draw_info->border_color.green;
4396 target.blue=(double) draw_info->border_color.blue;
4398 channel_mask=SetImageChannelMask(image,AlphaChannel);
4399 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4400 primitive_info->method == FloodfillMethod ? MagickFalse :
4401 MagickTrue,exception);
4402 (void) SetImageChannelMask(image,channel_mask);
4413 for (y=0; y < (ssize_t) image->rows; y++)
4421 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4423 if (q == (Quantum *) NULL)
4425 for (x=0; x < (ssize_t) image->columns; x++)
4427 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4428 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4429 q+=GetPixelChannels(image);
4431 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4432 if (sync == MagickFalse)
4443 geometry[MaxTextExtent];
4448 if (primitive_info->text == (char *) NULL)
4450 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4451 (void) CloneString(&clone_info->text,primitive_info->text);
4452 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4453 primitive_info->point.x,primitive_info->point.y);
4454 (void) CloneString(&clone_info->geometry,geometry);
4455 status&=AnnotateImage(image,clone_info,exception);
4456 clone_info=DestroyDrawInfo(clone_info);
4459 case ImagePrimitive:
4465 composite_geometry[MaxTextExtent];
4480 if (primitive_info->text == (char *) NULL)
4482 clone_info=AcquireImageInfo();
4483 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4484 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4488 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4490 composite_image=ReadImage(clone_info,exception);
4492 clone_info=DestroyImageInfo(clone_info);
4493 if (composite_image == (Image *) NULL)
4495 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4496 NULL,(void *) NULL);
4497 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4498 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4499 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4500 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4503 geometry[MaxTextExtent];
4508 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4509 primitive_info[1].point.x,primitive_info[1].point.y);
4510 composite_image->filter=image->filter;
4511 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4514 if (composite_image->alpha_trait != BlendPixelTrait)
4515 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4517 if (draw_info->alpha != OpaqueAlpha)
4518 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4519 SetGeometry(image,&geometry);
4520 image->gravity=draw_info->gravity;
4523 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4524 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4525 composite_image->rows,(double) geometry.x,(double) geometry.y);
4526 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4527 affine=draw_info->affine;
4528 affine.tx=(double) geometry.x;
4529 affine.ty=(double) geometry.y;
4530 composite_image->interpolate=image->interpolate;
4531 if (draw_info->compose == OverCompositeOp)
4532 (void) DrawAffineImage(image,composite_image,&affine,exception);
4534 (void) CompositeImage(image,composite_image,draw_info->compose,
4535 MagickTrue,geometry.x,geometry.y,exception);
4536 composite_image=DestroyImage(composite_image);
4548 if (IsEventLogging() != MagickFalse)
4549 LogPrimitiveInfo(primitive_info);
4550 scale=ExpandAffine(&draw_info->affine);
4551 if ((draw_info->dash_pattern != (double *) NULL) &&
4552 (draw_info->dash_pattern[0] != 0.0) &&
4553 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4554 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4559 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4560 clone_info->stroke_width=0.0;
4561 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4562 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4564 clone_info=DestroyDrawInfo(clone_info);
4565 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4568 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4570 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4576 Draw strokes while respecting line cap/join attributes.
4578 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4580 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4581 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4582 MagickTrue : MagickFalse;
4583 i=(ssize_t) primitive_info[0].coordinates;
4584 if ((((draw_info->linecap == RoundCap) ||
4585 (closed_path != MagickFalse)) &&
4586 (draw_info->linejoin == RoundJoin)) ||
4587 (primitive_info[i].primitive != UndefinedPrimitive))
4589 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4593 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4594 clone_info->stroke_width=0.0;
4595 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4596 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4598 clone_info=DestroyDrawInfo(clone_info);
4599 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4602 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4606 image_view=DestroyCacheView(image_view);
4607 if (image->debug != MagickFalse)
4608 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4609 return(status != 0 ? MagickTrue : MagickFalse);
4613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4617 + D r a w S t r o k e P o l y g o n %
4621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4623 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4624 % the image while respecting the line cap and join attributes.
4626 % The format of the DrawStrokePolygon method is:
4628 % MagickBooleanType DrawStrokePolygon(Image *image,
4629 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4631 % A description of each parameter follows:
4633 % o image: the image.
4635 % o draw_info: the draw info.
4637 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4642 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4643 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4651 for (i=0; i < 4; i++)
4652 linecap[i]=(*primitive_info);
4653 linecap[0].coordinates=4;
4654 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4655 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4656 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4657 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4658 linecap[4].primitive=UndefinedPrimitive;
4659 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4662 static MagickBooleanType DrawStrokePolygon(Image *image,
4663 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4664 ExceptionInfo *exception)
4678 register const PrimitiveInfo
4683 Draw stroked polygon.
4685 if (image->debug != MagickFalse)
4686 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4687 " begin draw-stroke-polygon");
4688 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4689 clone_info->fill=draw_info->stroke;
4690 if (clone_info->fill_pattern != (Image *) NULL)
4691 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4692 if (clone_info->stroke_pattern != (Image *) NULL)
4693 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4694 MagickTrue,exception);
4695 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4696 clone_info->stroke_width=0.0;
4697 clone_info->fill_rule=NonZeroRule;
4699 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4701 stroke_polygon=TraceStrokePolygon(draw_info,p);
4702 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4705 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4706 q=p+p->coordinates-1;
4707 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4708 MagickTrue : MagickFalse;
4709 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4711 DrawRoundLinecap(image,draw_info,p,exception);
4712 DrawRoundLinecap(image,draw_info,q,exception);
4715 clone_info=DestroyDrawInfo(clone_info);
4716 if (image->debug != MagickFalse)
4717 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4718 " end draw-stroke-polygon");
4719 return(status != 0 ? MagickTrue : MagickFalse);
4723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4727 % G e t A f f i n e M a t r i x %
4731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4733 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4736 % The format of the GetAffineMatrix method is:
4738 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4740 % A description of each parameter follows:
4742 % o affine_matrix: the affine matrix.
4745 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4748 assert(affine_matrix != (AffineMatrix *) NULL);
4749 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4750 affine_matrix->sx=1.0;
4751 affine_matrix->sy=1.0;
4755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4759 + G e t D r a w I n f o %
4763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4765 % GetDrawInfo() initializes draw_info to default values from image_info.
4767 % The format of the GetDrawInfo method is:
4769 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4771 % A description of each parameter follows:
4773 % o image_info: the image info..
4775 % o draw_info: the draw info.
4778 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4787 Initialize draw attributes.
4789 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4790 assert(draw_info != (DrawInfo *) NULL);
4791 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4792 GetAffineMatrix(&draw_info->affine);
4793 exception=AcquireExceptionInfo();
4794 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4796 (void) QueryColorCompliance("#FFFF",AllCompliance,&draw_info->stroke,
4798 draw_info->stroke_width=1.0;
4799 draw_info->alpha=OpaqueAlpha;
4800 draw_info->fill_rule=EvenOddRule;
4801 draw_info->linecap=ButtCap;
4802 draw_info->linejoin=MiterJoin;
4803 draw_info->miterlimit=10;
4804 draw_info->decorate=NoDecoration;
4805 draw_info->pointsize=12.0;
4806 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4807 draw_info->compose=OverCompositeOp;
4808 draw_info->render=MagickTrue;
4809 draw_info->debug=IsEventLogging();
4810 if (image_info != (ImageInfo *) NULL)
4812 draw_info->stroke_antialias=image_info->antialias;
4813 if (image_info->font != (char *) NULL)
4814 draw_info->font=AcquireString(image_info->font);
4815 if (image_info->density != (char *) NULL)
4816 draw_info->density=AcquireString(image_info->density);
4817 draw_info->text_antialias=image_info->antialias;
4818 if (image_info->pointsize != 0.0)
4819 draw_info->pointsize=image_info->pointsize;
4820 draw_info->border_color=image_info->border_color;
4821 if (image_info->server_name != (char *) NULL)
4822 draw_info->server_name=AcquireString(image_info->server_name);
4823 option=GetImageOption(image_info,"encoding");
4824 if (option != (const char *) NULL)
4825 (void) CloneString(&draw_info->encoding,option);
4826 option=GetImageOption(image_info,"kerning");
4827 if (option != (const char *) NULL)
4828 draw_info->kerning=StringToDouble(option,(char **) NULL);
4829 option=GetImageOption(image_info,"interline-spacing");
4830 if (option != (const char *) NULL)
4831 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4832 option=GetImageOption(image_info,"interword-spacing");
4833 if (option != (const char *) NULL)
4834 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4835 option=GetImageOption(image_info,"direction");
4836 if (option != (const char *) NULL)
4837 draw_info->direction=(DirectionType) ParseCommandOption(
4838 MagickDirectionOptions,MagickFalse,option);
4840 draw_info->direction=UndefinedDirection;
4841 option=GetImageOption(image_info,"fill");
4842 if (option != (const char *) NULL)
4843 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4845 option=GetImageOption(image_info,"stroke");
4846 if (option != (const char *) NULL)
4847 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4849 option=GetImageOption(image_info,"strokewidth");
4850 if (option != (const char *) NULL)
4851 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4852 option=GetImageOption(image_info,"undercolor");
4853 if (option != (const char *) NULL)
4854 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4856 option=GetImageOption(image_info,"gravity");
4857 if (option != (const char *) NULL)
4858 draw_info->gravity=(GravityType) ParseCommandOption(
4859 MagickGravityOptions,MagickFalse,option);
4861 exception=DestroyExceptionInfo(exception);
4862 draw_info->signature=MagickSignature;
4866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4870 + P e r m u t a t e %
4874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4876 % Permutate() returns the permuation of the (n,k).
4878 % The format of the Permutate method is:
4880 % void Permutate(ssize_t n,ssize_t k)
4882 % A description of each parameter follows:
4890 static inline double Permutate(const ssize_t n,const ssize_t k)
4899 for (i=k+1; i <= n; i++)
4901 for (i=1; i <= (n-k); i++)
4907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4911 + T r a c e P r i m i t i v e %
4915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4917 % TracePrimitive is a collection of methods for generating graphic
4918 % primitives such as arcs, ellipses, paths, etc.
4922 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4923 const PointInfo end,const PointInfo degrees)
4929 center.x=0.5*(end.x+start.x);
4930 center.y=0.5*(end.y+start.y);
4931 radii.x=fabs(center.x-start.x);
4932 radii.y=fabs(center.y-start.y);
4933 TraceEllipse(primitive_info,center,radii,degrees);
4936 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4937 const PointInfo end,const PointInfo arc,const double angle,
4938 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4957 register PrimitiveInfo
4966 if ((start.x == end.x) && (start.y == end.y))
4968 TracePoint(primitive_info,end);
4971 radii.x=fabs(arc.x);
4972 radii.y=fabs(arc.y);
4973 if ((radii.x == 0.0) || (radii.y == 0.0))
4975 TraceLine(primitive_info,start,end);
4978 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4979 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4980 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4981 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4982 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4984 if (delta < MagickEpsilon)
4986 TraceLine(primitive_info,start,end);
4991 radii.x*=sqrt((double) delta);
4992 radii.y*=sqrt((double) delta);
4994 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4995 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4996 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4997 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4998 alpha=points[1].x-points[0].x;
4999 beta=points[1].y-points[0].y;
5000 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5005 factor=sqrt((double) factor);
5006 if (sweep == large_arc)
5009 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5010 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5011 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5012 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5013 if ((theta < 0.0) && (sweep != MagickFalse))
5014 theta+=(double) (2.0*MagickPI);
5016 if ((theta > 0.0) && (sweep == MagickFalse))
5017 theta-=(double) (2.0*MagickPI);
5018 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5021 for (i=0; i < (ssize_t) arc_segments; i++)
5023 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5024 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5025 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5026 sin(fmod((double) beta,DegreesToRadians(360.0)));
5027 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5028 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5029 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5030 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5031 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5032 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5033 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5034 theta/arc_segments),DegreesToRadians(360.0))));
5035 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5036 theta/arc_segments),DegreesToRadians(360.0))));
5037 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5038 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5039 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5040 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5041 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5042 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5043 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5045 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5047 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5049 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5051 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5053 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5055 if (i == (ssize_t) (arc_segments-1))
5060 primitive_info->coordinates=(size_t) (p-primitive_info);
5061 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5063 p->primitive=primitive_info->primitive;
5068 static void TraceBezier(PrimitiveInfo *primitive_info,
5069 const size_t number_coordinates)
5081 register PrimitiveInfo
5093 Allocate coeficients.
5095 quantum=number_coordinates;
5096 for (i=0; i < (ssize_t) number_coordinates; i++)
5098 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5100 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5101 if (alpha > (double) quantum)
5102 quantum=(size_t) alpha;
5103 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5104 if (alpha > (double) quantum)
5105 quantum=(size_t) alpha;
5108 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5109 (double) BezierQuantum);
5110 control_points=quantum*number_coordinates;
5111 coefficients=(double *) AcquireQuantumMemory((size_t)
5112 number_coordinates,sizeof(*coefficients));
5113 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5115 if ((coefficients == (double *) NULL) ||
5116 (points == (PointInfo *) NULL))
5117 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5119 Compute bezier points.
5121 end=primitive_info[number_coordinates-1].point;
5122 for (i=0; i < (ssize_t) number_coordinates; i++)
5123 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5125 for (i=0; i < (ssize_t) control_points; i++)
5130 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5131 for (j=0; j < (ssize_t) number_coordinates; j++)
5133 point.x+=alpha*coefficients[j]*p->point.x;
5134 point.y+=alpha*coefficients[j]*p->point.y;
5135 alpha*=weight/(1.0-weight);
5139 weight+=1.0/control_points;
5142 Bezier curves are just short segmented polys.
5145 for (i=0; i < (ssize_t) control_points; i++)
5147 TracePoint(p,points[i]);
5152 primitive_info->coordinates=(size_t) (p-primitive_info);
5153 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5155 p->primitive=primitive_info->primitive;
5158 points=(PointInfo *) RelinquishMagickMemory(points);
5159 coefficients=(double *) RelinquishMagickMemory(coefficients);
5162 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5163 const PointInfo end)
5174 alpha=end.x-start.x;
5176 radius=hypot((double) alpha,(double) beta);
5177 offset.x=(double) radius;
5178 offset.y=(double) radius;
5181 TraceEllipse(primitive_info,start,offset,degrees);
5184 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5185 const PointInfo stop,const PointInfo degrees)
5196 register PrimitiveInfo
5203 Ellipses are just short segmented polys.
5205 if ((stop.x == 0.0) && (stop.y == 0.0))
5207 TracePoint(primitive_info,start);
5210 delta=2.0/MagickMax(stop.x,stop.y);
5211 step=(double) (MagickPI/8.0);
5212 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5213 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5214 angle.x=DegreesToRadians(degrees.x);
5216 while (y < degrees.x)
5218 angle.y=(double) DegreesToRadians(y);
5219 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5221 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5222 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5223 TracePoint(p,point);
5226 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5227 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5228 TracePoint(p,point);
5230 primitive_info->coordinates=(size_t) (p-primitive_info);
5231 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5233 p->primitive=primitive_info->primitive;
5238 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5239 const PointInfo end)
5241 TracePoint(primitive_info,start);
5242 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5243 (fabs(start.y-end.y) < MagickEpsilon))
5245 primitive_info->primitive=PointPrimitive;
5246 primitive_info->coordinates=1;
5249 TracePoint(primitive_info+1,end);
5250 (primitive_info+1)->primitive=primitive_info->primitive;
5251 primitive_info->coordinates=2;
5254 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5257 token[MaxTextExtent];
5279 register PrimitiveInfo
5296 number_coordinates=0;
5298 (void) ResetMagickMemory(points,0,sizeof(*points));
5299 primitive_type=primitive_info->primitive;
5301 for (p=path; *p != '\0'; )
5303 while (isspace((int) ((unsigned char) *p)) != 0)
5307 last_attribute=attribute;
5308 attribute=(int) (*p++);
5329 GetMagickToken(p,&p,token);
5331 GetMagickToken(p,&p,token);
5332 arc.x=StringToDouble(token,(char **) NULL);
5333 GetMagickToken(p,&p,token);
5335 GetMagickToken(p,&p,token);
5336 arc.y=StringToDouble(token,(char **) NULL);
5337 GetMagickToken(p,&p,token);
5339 GetMagickToken(p,&p,token);
5340 angle=StringToDouble(token,(char **) NULL);
5341 GetMagickToken(p,&p,token);
5343 GetMagickToken(p,&p,token);
5344 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5345 GetMagickToken(p,&p,token);
5347 GetMagickToken(p,&p,token);
5348 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5349 GetMagickToken(p,&p,token);
5351 GetMagickToken(p,&p,token);
5352 x=StringToDouble(token,(char **) NULL);
5353 GetMagickToken(p,&p,token);
5355 GetMagickToken(p,&p,token);
5356 y=StringToDouble(token,(char **) NULL);
5357 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5358 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5359 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5362 while (isspace((int) ((unsigned char) *p)) != 0)
5366 } while (IsPoint(p) != MagickFalse);
5373 Compute bezier points.
5378 for (i=1; i < 4; i++)
5380 GetMagickToken(p,&p,token);
5382 GetMagickToken(p,&p,token);
5383 x=StringToDouble(token,(char **) NULL);
5384 GetMagickToken(p,&p,token);
5386 GetMagickToken(p,&p,token);
5387 y=StringToDouble(token,(char **) NULL);
5388 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5389 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5392 for (i=0; i < 4; i++)
5393 (q+i)->point=points[i];
5397 } while (IsPoint(p) != MagickFalse);
5405 GetMagickToken(p,&p,token);
5407 GetMagickToken(p,&p,token);
5408 x=StringToDouble(token,(char **) NULL);
5409 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5410 TracePoint(q,point);
5412 } while (IsPoint(p) != MagickFalse);
5420 GetMagickToken(p,&p,token);
5422 GetMagickToken(p,&p,token);
5423 x=StringToDouble(token,(char **) NULL);
5424 GetMagickToken(p,&p,token);
5426 GetMagickToken(p,&p,token);
5427 y=StringToDouble(token,(char **) NULL);
5428 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5429 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5430 TracePoint(q,point);
5432 } while (IsPoint(p) != MagickFalse);
5438 if (q != primitive_info)
5440 primitive_info->coordinates=(size_t) (q-primitive_info);
5441 number_coordinates+=primitive_info->coordinates;
5447 GetMagickToken(p,&p,token);
5449 GetMagickToken(p,&p,token);
5450 x=StringToDouble(token,(char **) NULL);
5451 GetMagickToken(p,&p,token);
5453 GetMagickToken(p,&p,token);
5454 y=StringToDouble(token,(char **) NULL);
5455 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5456 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5460 TracePoint(q,point);
5462 if ((i != 0) && (attribute == (int) 'M'))
5464 TracePoint(q,point);
5467 } while (IsPoint(p) != MagickFalse);
5474 Compute bezier points.
5479 for (i=1; i < 3; i++)
5481 GetMagickToken(p,&p,token);
5483 GetMagickToken(p,&p,token);
5484 x=StringToDouble(token,(char **) NULL);
5485 GetMagickToken(p,&p,token);
5487 GetMagickToken(p,&p,token);
5488 y=StringToDouble(token,(char **) NULL);
5491 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5492 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5495 for (i=0; i < 3; i++)
5496 (q+i)->point=points[i];
5500 } while (IsPoint(p) != MagickFalse);
5507 Compute bezier points.
5511 points[0]=points[3];
5512 points[1].x=2.0*points[3].x-points[2].x;
5513 points[1].y=2.0*points[3].y-points[2].y;
5514 for (i=2; i < 4; i++)
5516 GetMagickToken(p,&p,token);
5518 GetMagickToken(p,&p,token);
5519 x=StringToDouble(token,(char **) NULL);
5520 GetMagickToken(p,&p,token);
5522 GetMagickToken(p,&p,token);
5523 y=StringToDouble(token,(char **) NULL);
5526 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5527 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5530 if (strchr("CcSs",last_attribute) == (char *) NULL)
5532 points[0]=points[2];
5533 points[1]=points[3];
5535 for (i=0; i < 4; i++)
5536 (q+i)->point=points[i];
5540 } while (IsPoint(p) != MagickFalse);
5547 Compute bezier points.
5551 points[0]=points[2];
5552 points[1].x=2.0*points[2].x-points[1].x;
5553 points[1].y=2.0*points[2].y-points[1].y;
5554 for (i=2; i < 3; i++)
5556 GetMagickToken(p,&p,token);
5558 GetMagickToken(p,&p,token);
5559 x=StringToDouble(token,(char **) NULL);
5560 GetMagickToken(p,&p,token);
5562 GetMagickToken(p,&p,token);
5563 y=StringToDouble(token,(char **) NULL);
5564 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5565 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5568 if (strchr("QqTt",last_attribute) == (char *) NULL)
5570 points[0]=points[2];
5571 points[1]=points[3];
5573 for (i=0; i < 3; i++)
5574 (q+i)->point=points[i];
5578 } while (IsPoint(p) != MagickFalse);
5586 GetMagickToken(p,&p,token);
5588 GetMagickToken(p,&p,token);
5589 y=StringToDouble(token,(char **) NULL);
5590 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5591 TracePoint(q,point);
5593 } while (IsPoint(p) != MagickFalse);
5600 TracePoint(q,point);
5602 primitive_info->coordinates=(size_t) (q-primitive_info);
5603 number_coordinates+=primitive_info->coordinates;
5610 if (isalpha((int) ((unsigned char) attribute)) != 0)
5611 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5617 primitive_info->coordinates=(size_t) (q-primitive_info);
5618 number_coordinates+=primitive_info->coordinates;
5619 for (i=0; i < (ssize_t) number_coordinates; i++)
5622 q->primitive=primitive_type;
5624 q->method=FillToBorderMethod;
5627 return(number_coordinates);
5630 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5631 const PointInfo end)
5636 register PrimitiveInfo
5643 TracePoint(p,start);
5647 TracePoint(p,point);
5653 TracePoint(p,point);
5655 TracePoint(p,start);
5657 primitive_info->coordinates=(size_t) (p-primitive_info);
5658 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5660 p->primitive=primitive_info->primitive;
5665 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5666 const PointInfo start,const PointInfo end,PointInfo arc)
5673 register PrimitiveInfo
5680 offset.x=fabs(end.x-start.x);
5681 offset.y=fabs(end.y-start.y);
5682 if (arc.x > (0.5*offset.x))
5684 if (arc.y > (0.5*offset.y))
5686 point.x=start.x+offset.x-arc.x;
5687 point.y=start.y+arc.y;
5690 TraceEllipse(p,point,arc,degrees);
5692 point.x=start.x+offset.x-arc.x;
5693 point.y=start.y+offset.y-arc.y;
5696 TraceEllipse(p,point,arc,degrees);
5698 point.x=start.x+arc.x;
5699 point.y=start.y+offset.y-arc.y;
5702 TraceEllipse(p,point,arc,degrees);
5704 point.x=start.x+arc.x;
5705 point.y=start.y+arc.y;
5708 TraceEllipse(p,point,arc,degrees);
5710 TracePoint(p,primitive_info->point);
5712 primitive_info->coordinates=(size_t) (p-primitive_info);
5713 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5715 p->primitive=primitive_info->primitive;
5720 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5721 const size_t number_vertices,const double offset)
5738 for (i=1; i < (ssize_t) number_vertices; i++)
5740 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5741 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5742 if ((fabs((double) dx) >= MagickEpsilon) ||
5743 (fabs((double) dy) >= MagickEpsilon))
5746 if (i == (ssize_t) number_vertices)
5747 i=(ssize_t) number_vertices-1L;
5748 distance=hypot((double) dx,(double) dy);
5749 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5750 dx*(distance+offset)/distance);
5751 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5752 dy*(distance+offset)/distance);
5753 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5755 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5756 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5757 if ((fabs((double) dx) >= MagickEpsilon) ||
5758 (fabs((double) dy) >= MagickEpsilon))
5761 distance=hypot((double) dx,(double) dy);
5762 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5763 dx*(distance+offset)/distance);
5764 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5765 dy*(distance+offset)/distance);
5768 static inline double DrawEpsilonReciprocal(const double x)
5770 #define DrawEpsilon ((double) 1.0e-10)
5772 double sign = x < (double) 0.0 ? (double) -1.0 :
5774 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5775 (double) 1.0/DrawEpsilon));
5778 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5779 const PrimitiveInfo *primitive_info)
5781 typedef struct _LineSegment
5833 number_vertices=primitive_info->coordinates;
5834 max_strokes=2*number_vertices+6*BezierQuantum+360;
5835 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5837 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5839 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5840 number_vertices+2UL,sizeof(*polygon_primitive));
5841 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5842 (polygon_primitive == (PrimitiveInfo *) NULL))
5843 return((PrimitiveInfo *) NULL);
5844 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5845 number_vertices*sizeof(*polygon_primitive));
5847 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5848 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5849 MagickTrue : MagickFalse;
5850 if ((draw_info->linejoin == RoundJoin) ||
5851 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5853 polygon_primitive[number_vertices]=primitive_info[1];
5856 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5858 Compute the slope for the first line segment, p.
5862 for (n=1; n < (ssize_t) number_vertices; n++)
5864 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5865 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5866 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5869 if (n == (ssize_t) number_vertices)
5870 n=(ssize_t) number_vertices-1L;
5871 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5872 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5873 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5874 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5876 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5877 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5878 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5879 offset.y=(double) (offset.x*inverse_slope.p);
5880 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5882 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5883 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5884 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5885 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5886 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5887 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5888 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5889 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5893 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5894 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5895 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5896 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5897 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5898 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5899 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5900 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5903 Create strokes for the line join attribute: bevel, miter, round.
5907 path_q[p++]=box_q[0];
5908 path_p[q++]=box_p[0];
5909 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5912 Compute the slope for this line segment, q.
5914 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5915 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5916 dot_product=dx.q*dx.q+dy.q*dy.q;
5917 if (dot_product < 0.25)
5919 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5920 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5921 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5922 offset.y=(double) (offset.x*inverse_slope.q);
5923 dot_product=dy.q*offset.x-dx.q*offset.y;
5924 if (dot_product > 0.0)
5926 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5927 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5928 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5929 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5930 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5931 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5932 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5933 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5937 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5938 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5939 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5940 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5941 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5942 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5943 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5944 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5946 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5953 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5954 box_p[3].y)/(slope.p-slope.q));
5955 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5956 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5957 box_q[3].y)/(slope.p-slope.q));
5958 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5960 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5962 max_strokes+=6*BezierQuantum+360;
5963 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5965 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5967 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5969 polygon_primitive=(PrimitiveInfo *)
5970 RelinquishMagickMemory(polygon_primitive);
5971 return((PrimitiveInfo *) NULL);
5974 dot_product=dx.q*dy.p-dx.p*dy.q;
5975 if (dot_product <= 0.0)
5976 switch (draw_info->linejoin)
5980 path_q[q++]=box_q[1];
5981 path_q[q++]=box_q[2];
5982 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5983 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5984 if (dot_product <= miterlimit)
5985 path_p[p++]=box_p[4];
5988 path_p[p++]=box_p[1];
5989 path_p[p++]=box_p[2];
5995 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5996 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5997 if (dot_product <= miterlimit)
5999 path_q[q++]=box_q[4];
6000 path_p[p++]=box_p[4];
6004 path_q[q++]=box_q[1];
6005 path_q[q++]=box_q[2];
6006 path_p[p++]=box_p[1];
6007 path_p[p++]=box_p[2];
6013 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6014 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6015 if (dot_product <= miterlimit)
6016 path_p[p++]=box_p[4];
6019 path_p[p++]=box_p[1];
6020 path_p[p++]=box_p[2];
6022 center=polygon_primitive[n].point;
6023 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6024 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6025 if (theta.q < theta.p)
6026 theta.q+=(double) (2.0*MagickPI);
6027 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6028 (2.0*sqrt((double) (1.0/mid)))));
6029 path_q[q].x=box_q[1].x;
6030 path_q[q].y=box_q[1].y;
6032 for (j=1; j < (ssize_t) arc_segments; j++)
6034 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6035 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6036 (theta.p+delta_theta),DegreesToRadians(360.0))));
6037 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6038 (theta.p+delta_theta),DegreesToRadians(360.0))));
6041 path_q[q++]=box_q[2];
6048 switch (draw_info->linejoin)
6052 path_p[p++]=box_p[1];
6053 path_p[p++]=box_p[2];
6054 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6055 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6056 if (dot_product <= miterlimit)
6057 path_q[q++]=box_q[4];
6060 path_q[q++]=box_q[1];
6061 path_q[q++]=box_q[2];
6067 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6068 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6069 if (dot_product <= miterlimit)
6071 path_q[q++]=box_q[4];
6072 path_p[p++]=box_p[4];
6076 path_q[q++]=box_q[1];
6077 path_q[q++]=box_q[2];
6078 path_p[p++]=box_p[1];
6079 path_p[p++]=box_p[2];
6085 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6086 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6087 if (dot_product <= miterlimit)
6088 path_q[q++]=box_q[4];
6091 path_q[q++]=box_q[1];
6092 path_q[q++]=box_q[2];
6094 center=polygon_primitive[n].point;
6095 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6096 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6097 if (theta.p < theta.q)
6098 theta.p+=(double) (2.0*MagickPI);
6099 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6100 (2.0*sqrt((double) (1.0/mid)))));
6101 path_p[p++]=box_p[1];
6102 for (j=1; j < (ssize_t) arc_segments; j++)
6104 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6105 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6106 (theta.p+delta_theta),DegreesToRadians(360.0))));
6107 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6108 (theta.p+delta_theta),DegreesToRadians(360.0))));
6111 path_p[p++]=box_p[2];
6118 inverse_slope.p=inverse_slope.q;
6127 path_p[p++]=box_p[1];
6128 path_q[q++]=box_q[1];
6130 Trace stroked polygon.
6132 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6133 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6134 if (stroke_polygon != (PrimitiveInfo *) NULL)
6136 for (i=0; i < (ssize_t) p; i++)
6138 stroke_polygon[i]=polygon_primitive[0];
6139 stroke_polygon[i].point=path_p[i];
6141 if (closed_path != MagickFalse)
6143 stroke_polygon[i]=polygon_primitive[0];
6144 stroke_polygon[i].point=stroke_polygon[0].point;
6147 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6149 stroke_polygon[i]=polygon_primitive[0];
6150 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6152 if (closed_path != MagickFalse)
6154 stroke_polygon[i]=polygon_primitive[0];
6155 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6158 stroke_polygon[i]=polygon_primitive[0];
6159 stroke_polygon[i].point=stroke_polygon[0].point;
6161 stroke_polygon[i].primitive=UndefinedPrimitive;
6162 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6164 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6165 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6166 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6167 return(stroke_polygon);