2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2011 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/color.h"
55 #include "MagickCore/composite.h"
56 #include "MagickCore/composite-private.h"
57 #include "MagickCore/constitute.h"
58 #include "MagickCore/draw.h"
59 #include "MagickCore/draw-private.h"
60 #include "MagickCore/enhance.h"
61 #include "MagickCore/exception.h"
62 #include "MagickCore/exception-private.h"
63 #include "MagickCore/gem.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/image-private.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/monitor.h"
69 #include "MagickCore/monitor-private.h"
70 #include "MagickCore/option.h"
71 #include "MagickCore/paint.h"
72 #include "MagickCore/pixel-accessor.h"
73 #include "MagickCore/property.h"
74 #include "MagickCore/resample.h"
75 #include "MagickCore/resample-private.h"
76 #include "MagickCore/string_.h"
77 #include "MagickCore/string-private.h"
78 #include "MagickCore/thread-private.h"
79 #include "MagickCore/token.h"
80 #include "MagickCore/transform.h"
81 #include "MagickCore/utility.h"
86 #define BezierQuantum 200
91 typedef struct _EdgeInfo
115 typedef struct _ElementInfo
125 typedef struct _PolygonInfo
143 typedef struct _PathInfo
153 Forward declarations.
155 static MagickBooleanType
156 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *);
159 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
162 TracePath(PrimitiveInfo *,const char *);
165 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
166 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
167 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
168 TraceBezier(PrimitiveInfo *,const size_t),
169 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
170 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
172 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
173 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
175 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % A c q u i r e D r a w I n f o %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
190 % The format of the AcquireDrawInfo method is:
192 % DrawInfo *AcquireDrawInfo(void)
195 MagickExport DrawInfo *AcquireDrawInfo(void)
200 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
201 if (draw_info == (DrawInfo *) NULL)
202 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
203 GetDrawInfo((ImageInfo *) NULL,draw_info);
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % C l o n e D r a w I n f o %
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % CloneDrawInfo() makes a copy of the given draw info structure. If NULL
219 % is specified, a new image info structure is created initialized to
222 % The format of the CloneDrawInfo method is:
224 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
225 % const DrawInfo *draw_info)
227 % A description of each parameter follows:
229 % o image_info: the image info.
231 % o draw_info: the draw info.
234 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
235 const DrawInfo *draw_info)
240 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
241 if (clone_info == (DrawInfo *) NULL)
242 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
243 GetDrawInfo(image_info,clone_info);
244 if (draw_info == (DrawInfo *) NULL)
246 if (clone_info->primitive != (char *) NULL)
247 (void) CloneString(&clone_info->primitive,draw_info->primitive);
248 if (draw_info->geometry != (char *) NULL)
249 (void) CloneString(&clone_info->geometry,draw_info->geometry);
250 clone_info->viewbox=draw_info->viewbox;
251 clone_info->affine=draw_info->affine;
252 clone_info->gravity=draw_info->gravity;
253 clone_info->fill=draw_info->fill;
254 clone_info->stroke=draw_info->stroke;
255 clone_info->stroke_width=draw_info->stroke_width;
256 if (draw_info->fill_pattern != (Image *) NULL)
257 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
258 &draw_info->fill_pattern->exception);
259 if (draw_info->stroke_pattern != (Image *) NULL)
260 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
261 MagickTrue,&draw_info->stroke_pattern->exception);
262 clone_info->stroke_antialias=draw_info->stroke_antialias;
263 clone_info->text_antialias=draw_info->text_antialias;
264 clone_info->fill_rule=draw_info->fill_rule;
265 clone_info->linecap=draw_info->linecap;
266 clone_info->linejoin=draw_info->linejoin;
267 clone_info->miterlimit=draw_info->miterlimit;
268 clone_info->dash_offset=draw_info->dash_offset;
269 clone_info->decorate=draw_info->decorate;
270 clone_info->compose=draw_info->compose;
271 if (draw_info->text != (char *) NULL)
272 (void) CloneString(&clone_info->text,draw_info->text);
273 if (draw_info->font != (char *) NULL)
274 (void) CloneString(&clone_info->font,draw_info->font);
275 if (draw_info->metrics != (char *) NULL)
276 (void) CloneString(&clone_info->metrics,draw_info->metrics);
277 if (draw_info->family != (char *) NULL)
278 (void) CloneString(&clone_info->family,draw_info->family);
279 clone_info->style=draw_info->style;
280 clone_info->stretch=draw_info->stretch;
281 clone_info->weight=draw_info->weight;
282 if (draw_info->encoding != (char *) NULL)
283 (void) CloneString(&clone_info->encoding,draw_info->encoding);
284 clone_info->pointsize=draw_info->pointsize;
285 clone_info->kerning=draw_info->kerning;
286 clone_info->interline_spacing=draw_info->interline_spacing;
287 clone_info->interword_spacing=draw_info->interword_spacing;
288 clone_info->direction=draw_info->direction;
289 if (draw_info->density != (char *) NULL)
290 (void) CloneString(&clone_info->density,draw_info->density);
291 clone_info->align=draw_info->align;
292 clone_info->undercolor=draw_info->undercolor;
293 clone_info->border_color=draw_info->border_color;
294 if (draw_info->server_name != (char *) NULL)
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 if (draw_info->clip_mask != (char *) NULL)
327 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
328 clone_info->bounds=draw_info->bounds;
329 clone_info->clip_units=draw_info->clip_units;
330 clone_info->render=draw_info->render;
331 clone_info->alpha=draw_info->alpha;
332 clone_info->element_reference=draw_info->element_reference;
333 clone_info->debug=IsEventLogging();
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 ((direction != 0) && (direction != next_direction))
558 if (edge == number_edges)
561 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
562 polygon_info->edges,(size_t) number_edges,
563 sizeof(*polygon_info->edges));
564 if (polygon_info->edges == (EdgeInfo *) NULL)
565 return((PolygonInfo *) NULL);
567 polygon_info->edges[edge].number_points=(size_t) n;
568 polygon_info->edges[edge].scanline=(-1.0);
569 polygon_info->edges[edge].highwater=0;
570 polygon_info->edges[edge].ghostline=ghostline;
571 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
573 ReversePoints(points,(size_t) n);
574 polygon_info->edges[edge].points=points;
575 polygon_info->edges[edge].bounds=bounds;
576 polygon_info->edges[edge].bounds.y1=points[0].y;
577 polygon_info->edges[edge].bounds.y2=points[n-1].y;
579 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
581 if (points == (PointInfo *) NULL)
582 return((PolygonInfo *) NULL);
584 ghostline=MagickFalse;
590 direction=next_direction;
591 if (points == (PointInfo *) NULL)
593 if (n == (ssize_t) number_points)
596 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
598 if (points == (PointInfo *) NULL)
599 return((PolygonInfo *) NULL);
601 point=path_info[i].point;
603 if (point.x < bounds.x1)
605 if (point.x > bounds.x2)
609 if (points != (PointInfo *) NULL)
612 points=(PointInfo *) RelinquishMagickMemory(points);
615 if (edge == number_edges)
618 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
619 polygon_info->edges,(size_t) number_edges,
620 sizeof(*polygon_info->edges));
621 if (polygon_info->edges == (EdgeInfo *) NULL)
622 return((PolygonInfo *) NULL);
624 polygon_info->edges[edge].number_points=(size_t) n;
625 polygon_info->edges[edge].scanline=(-1.0);
626 polygon_info->edges[edge].highwater=0;
627 polygon_info->edges[edge].ghostline=ghostline;
628 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
630 ReversePoints(points,(size_t) n);
631 polygon_info->edges[edge].points=points;
632 polygon_info->edges[edge].bounds=bounds;
633 polygon_info->edges[edge].bounds.y1=points[0].y;
634 polygon_info->edges[edge].bounds.y2=points[n-1].y;
635 ghostline=MagickFalse;
639 polygon_info->number_edges=edge;
640 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
641 sizeof(*polygon_info->edges),CompareEdges);
642 if (IsEventLogging() != MagickFalse)
643 LogPolygonInfo(polygon_info);
644 return(polygon_info);
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 + C o n v e r t P r i m i t i v e T o P a t h %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
661 % The format of the ConvertPrimitiveToPath method is:
663 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
664 % const PrimitiveInfo *primitive_info)
666 % A description of each parameter follows:
668 % o Method ConvertPrimitiveToPath returns a vector path structure of type
671 % o draw_info: a structure of type DrawInfo.
673 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
678 static void LogPathInfo(const PathInfo *path_info)
680 register const PathInfo
683 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
684 for (p=path_info; p->code != EndCode; p++)
685 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
686 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
687 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
688 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
690 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
693 static PathInfo *ConvertPrimitiveToPath(
694 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
715 Converts a PrimitiveInfo structure into a vector path structure.
717 switch (primitive_info->primitive)
724 return((PathInfo *) NULL);
728 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
729 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
731 if (path_info == (PathInfo *) NULL)
732 return((PathInfo *) NULL);
740 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
743 if (coordinates <= 0)
745 coordinates=(ssize_t) primitive_info[i].coordinates;
746 p=primitive_info[i].point;
752 Eliminate duplicate points.
754 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) > MagickEpsilon) ||
755 (fabs(q.y-primitive_info[i].point.y) > MagickEpsilon))
757 path_info[n].code=code;
758 path_info[n].point=primitive_info[i].point;
759 q=primitive_info[i].point;
764 if ((fabs(p.x-primitive_info[i].point.x) <= MagickEpsilon) &&
765 (fabs(p.y-primitive_info[i].point.y) <= MagickEpsilon))
768 Mark the p point as open if it does not match the q.
770 path_info[start].code=OpenCode;
771 path_info[n].code=GhostlineCode;
772 path_info[n].point=primitive_info[i].point;
774 path_info[n].code=LineToCode;
775 path_info[n].point=p;
778 path_info[n].code=EndCode;
779 path_info[n].point.x=0.0;
780 path_info[n].point.y=0.0;
781 if (IsEventLogging() != MagickFalse)
782 LogPathInfo(path_info);
787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % D e s t r o y D r a w I n f o %
795 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
800 % The format of the DestroyDrawInfo method is:
802 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
804 % A description of each parameter follows:
806 % o draw_info: the draw info.
809 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
811 if (draw_info->debug != MagickFalse)
812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
813 assert(draw_info != (DrawInfo *) NULL);
814 assert(draw_info->signature == MagickSignature);
815 if (draw_info->primitive != (char *) NULL)
816 draw_info->primitive=DestroyString(draw_info->primitive);
817 if (draw_info->text != (char *) NULL)
818 draw_info->text=DestroyString(draw_info->text);
819 if (draw_info->geometry != (char *) NULL)
820 draw_info->geometry=DestroyString(draw_info->geometry);
821 if (draw_info->fill_pattern != (Image *) NULL)
822 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
823 if (draw_info->stroke_pattern != (Image *) NULL)
824 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
825 if (draw_info->font != (char *) NULL)
826 draw_info->font=DestroyString(draw_info->font);
827 if (draw_info->metrics != (char *) NULL)
828 draw_info->metrics=DestroyString(draw_info->metrics);
829 if (draw_info->family != (char *) NULL)
830 draw_info->family=DestroyString(draw_info->family);
831 if (draw_info->encoding != (char *) NULL)
832 draw_info->encoding=DestroyString(draw_info->encoding);
833 if (draw_info->density != (char *) NULL)
834 draw_info->density=DestroyString(draw_info->density);
835 if (draw_info->server_name != (char *) NULL)
836 draw_info->server_name=(char *)
837 RelinquishMagickMemory(draw_info->server_name);
838 if (draw_info->dash_pattern != (double *) NULL)
839 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
840 draw_info->dash_pattern);
841 if (draw_info->gradient.stops != (StopInfo *) NULL)
842 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
843 draw_info->gradient.stops);
844 if (draw_info->clip_mask != (char *) NULL)
845 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
846 draw_info->signature=(~MagickSignature);
847 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856 + D e s t r o y E d g e %
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % DestroyEdge() destroys the specified polygon edge.
864 % The format of the DestroyEdge method is:
866 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
868 % A description of each parameter follows:
870 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
872 % o edge: the polygon edge number to destroy.
875 static size_t DestroyEdge(PolygonInfo *polygon_info,
878 assert(edge < polygon_info->number_edges);
879 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
880 polygon_info->edges[edge].points);
881 polygon_info->number_edges--;
882 if (edge < polygon_info->number_edges)
883 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
884 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
885 return(polygon_info->number_edges);
889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893 + D e s t r o y P o l y g o n I n f o %
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
901 % The format of the DestroyPolygonInfo method is:
903 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
905 % A description of each parameter follows:
907 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
910 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
915 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
916 polygon_info->edges[i].points=(PointInfo *)
917 RelinquishMagickMemory(polygon_info->edges[i].points);
918 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
919 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % D r a w A f f i n e I m a g e %
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 % DrawAffineImage() composites the source over the destination image as
934 % dictated by the affine transform.
936 % The format of the DrawAffineImage method is:
938 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
939 % const AffineMatrix *affine)
941 % A description of each parameter follows:
943 % o image: the image.
945 % o source: the source image.
947 % o affine: the affine transform.
950 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
951 const double y,const SegmentInfo *edge)
964 Determine left and right edges.
966 inverse_edge.x1=edge->x1;
967 inverse_edge.y1=edge->y1;
968 inverse_edge.x2=edge->x2;
969 inverse_edge.y2=edge->y2;
970 z=affine->ry*y+affine->tx;
971 if (affine->sx > MagickEpsilon)
973 intercept=(-z/affine->sx);
974 x=intercept+MagickEpsilon;
975 if (x > inverse_edge.x1)
977 intercept=(-z+(double) image->columns)/affine->sx;
978 x=intercept-MagickEpsilon;
979 if (x < inverse_edge.x2)
983 if (affine->sx < -MagickEpsilon)
985 intercept=(-z+(double) image->columns)/affine->sx;
986 x=intercept+MagickEpsilon;
987 if (x > inverse_edge.x1)
989 intercept=(-z/affine->sx);
990 x=intercept-MagickEpsilon;
991 if (x < inverse_edge.x2)
995 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
997 inverse_edge.x2=edge->x1;
998 return(inverse_edge);
1001 Determine top and bottom edges.
1003 z=affine->sy*y+affine->ty;
1004 if (affine->rx > MagickEpsilon)
1006 intercept=(-z/affine->rx);
1007 x=intercept+MagickEpsilon;
1008 if (x > inverse_edge.x1)
1010 intercept=(-z+(double) image->rows)/affine->rx;
1011 x=intercept-MagickEpsilon;
1012 if (x < inverse_edge.x2)
1016 if (affine->rx < -MagickEpsilon)
1018 intercept=(-z+(double) image->rows)/affine->rx;
1019 x=intercept+MagickEpsilon;
1020 if (x > inverse_edge.x1)
1022 intercept=(-z/affine->rx);
1023 x=intercept-MagickEpsilon;
1024 if (x < inverse_edge.x2)
1028 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1030 inverse_edge.x2=edge->x2;
1031 return(inverse_edge);
1033 return(inverse_edge);
1036 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1044 determinant=1.0/(affine->sx*affine->sy-affine->rx*affine->ry);
1045 inverse_affine.sx=determinant*affine->sy;
1046 inverse_affine.rx=determinant*(-affine->rx);
1047 inverse_affine.ry=determinant*(-affine->ry);
1048 inverse_affine.sy=determinant*affine->sx;
1049 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1051 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1053 return(inverse_affine);
1056 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1063 static inline double MagickMax(const double x,const double y)
1070 static inline double MagickMin(const double x,const double y)
1077 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1078 const Image *source,const AffineMatrix *affine)
1112 Determine bounding box.
1114 assert(image != (Image *) NULL);
1115 assert(image->signature == MagickSignature);
1116 if (image->debug != MagickFalse)
1117 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1118 assert(source != (const Image *) NULL);
1119 assert(source->signature == MagickSignature);
1120 assert(affine != (AffineMatrix *) NULL);
1123 extent[1].x=(double) source->columns-1.0;
1125 extent[2].x=(double) source->columns-1.0;
1126 extent[2].y=(double) source->rows-1.0;
1128 extent[3].y=(double) source->rows-1.0;
1129 for (i=0; i < 4; i++)
1132 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1133 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1137 for (i=1; i < 4; i++)
1139 if (min.x > extent[i].x)
1141 if (min.y > extent[i].y)
1143 if (max.x < extent[i].x)
1145 if (max.y < extent[i].y)
1149 Affine transform image.
1151 exception=(&image->exception);
1152 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1153 return(MagickFalse);
1155 edge.x1=MagickMax(min.x,0.0);
1156 edge.y1=MagickMax(min.y,0.0);
1157 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1158 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1159 inverse_affine=InverseAffineMatrix(affine);
1160 GetPixelInfo(image,&zero);
1161 image_view=AcquireCacheView(image);
1162 source_view=AcquireCacheView(source);
1163 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1164 #pragma omp parallel for schedule(dynamic,4) shared(status)
1166 for (y=(ssize_t) ceil(edge.y1-0.5); y <= (ssize_t) floor(edge.y2+0.5); y++)
1187 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1188 if (inverse_edge.x2 < inverse_edge.x1)
1190 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1191 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
1192 inverse_edge.x1+0.5)+1),1,exception);
1193 if (q == (Quantum *) NULL)
1198 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1200 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1202 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1204 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1205 point.x,point.y,&pixel,exception);
1206 SetPixelInfo(image,q,&composite);
1207 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1209 SetPixelPixelInfo(image,&composite,q);
1211 q+=GetPixelChannels(image);
1213 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1216 source_view=DestroyCacheView(source_view);
1217 image_view=DestroyCacheView(image_view);
1222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226 + D r a w B o u n d i n g R e c t a n g l e s %
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1233 % is only useful for developers debugging the rendering algorithm.
1235 % The format of the DrawBoundingRectangles method is:
1237 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1238 % PolygonInfo *polygon_info)
1240 % A description of each parameter follows:
1242 % o image: the image.
1244 % o draw_info: the draw info.
1246 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1249 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1250 const PolygonInfo *polygon_info)
1275 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1276 (void) QueryColorDatabase("#0000",&clone_info->fill,&image->exception);
1277 resolution.x=DefaultResolution;
1278 resolution.y=DefaultResolution;
1279 if (clone_info->density != (char *) NULL)
1287 flags=ParseGeometry(clone_info->density,&geometry_info);
1288 resolution.x=geometry_info.rho;
1289 resolution.y=geometry_info.sigma;
1290 if ((flags & SigmaValue) == MagickFalse)
1291 resolution.y=resolution.x;
1293 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1294 clone_info->stroke_width/2.0;
1299 if (polygon_info != (PolygonInfo *) NULL)
1301 bounds=polygon_info->edges[0].bounds;
1302 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1304 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1305 bounds.x1=polygon_info->edges[i].bounds.x1;
1306 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1307 bounds.y1=polygon_info->edges[i].bounds.y1;
1308 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1309 bounds.x2=polygon_info->edges[i].bounds.x2;
1310 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1311 bounds.y2=polygon_info->edges[i].bounds.y2;
1314 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1315 image->columns ? (double) image->columns-1 : bounds.x1;
1317 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1318 image->rows ? (double) image->rows-1 : bounds.y1;
1320 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1321 image->columns ? (double) image->columns-1 : bounds.x2;
1323 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1324 image->rows ? (double) image->rows-1 : bounds.y2;
1325 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1327 if (polygon_info->edges[i].direction != 0)
1328 (void) QueryColorDatabase("red",&clone_info->stroke,
1331 (void) QueryColorDatabase("green",&clone_info->stroke,
1333 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1334 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1335 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1336 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1337 primitive_info[0].primitive=RectanglePrimitive;
1338 TraceRectangle(primitive_info,start,end);
1339 primitive_info[0].method=ReplaceMethod;
1340 coordinates=(ssize_t) primitive_info[0].coordinates;
1341 primitive_info[coordinates].primitive=UndefinedPrimitive;
1342 (void) DrawPrimitive(image,clone_info,primitive_info);
1345 (void) QueryColorDatabase("blue",&clone_info->stroke,&image->exception);
1346 start.x=(double) (bounds.x1-mid);
1347 start.y=(double) (bounds.y1-mid);
1348 end.x=(double) (bounds.x2+mid);
1349 end.y=(double) (bounds.y2+mid);
1350 primitive_info[0].primitive=RectanglePrimitive;
1351 TraceRectangle(primitive_info,start,end);
1352 primitive_info[0].method=ReplaceMethod;
1353 coordinates=(ssize_t) primitive_info[0].coordinates;
1354 primitive_info[coordinates].primitive=UndefinedPrimitive;
1355 (void) DrawPrimitive(image,clone_info,primitive_info);
1356 clone_info=DestroyDrawInfo(clone_info);
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 % D r a w C l i p P a t h %
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % DrawClipPath() draws the clip path on the image mask.
1372 % The format of the DrawClipPath method is:
1374 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1375 % const char *name,ExceptionInfo *exception)
1377 % A description of each parameter follows:
1379 % o image: the image.
1381 % o draw_info: the draw info.
1383 % o name: the name of the clip path.
1385 % o exception: return any errors or warnings in this structure.
1388 MagickExport MagickBooleanType DrawClipPath(Image *image,
1389 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1392 clip_mask[MaxTextExtent];
1403 assert(image != (Image *) NULL);
1404 assert(image->signature == MagickSignature);
1405 if (image->debug != MagickFalse)
1406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1407 assert(draw_info != (const DrawInfo *) NULL);
1408 (void) FormatLocaleString(clip_mask,MaxTextExtent,"%s",name);
1409 value=GetImageArtifact(image,clip_mask);
1410 if (value == (const char *) NULL)
1411 return(MagickFalse);
1412 if (image->clip_mask == (Image *) NULL)
1417 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,
1419 if (clip_mask == (Image *) NULL)
1420 return(MagickFalse);
1421 (void) SetImageClipMask(image,clip_mask,exception);
1422 clip_mask=DestroyImage(clip_mask);
1424 (void) QueryColorDatabase("#00000000",&image->clip_mask->background_color,
1426 image->clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1427 (void) SetImageBackgroundColor(image->clip_mask);
1428 if (image->debug != MagickFalse)
1429 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1430 draw_info->clip_mask);
1431 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1432 (void) CloneString(&clone_info->primitive,value);
1433 (void) QueryColorDatabase("#ffffff",&clone_info->fill,&image->exception);
1434 clone_info->clip_mask=(char *) NULL;
1435 status=DrawImage(image->clip_mask,clone_info,exception);
1436 status|=NegateImage(image->clip_mask,MagickFalse,&image->exception);
1437 clone_info=DestroyDrawInfo(clone_info);
1438 if (image->debug != MagickFalse)
1439 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1440 return(status != 0 ? MagickTrue : MagickFalse);
1444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448 + D r a w D a s h P o l y g o n %
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1455 % image while respecting the dash offset and dash pattern attributes.
1457 % The format of the DrawDashPolygon method is:
1459 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1460 % const PrimitiveInfo *primitive_info,Image *image)
1462 % A description of each parameter follows:
1464 % o draw_info: the draw info.
1466 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1468 % o image: the image.
1472 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1473 const PrimitiveInfo *primitive_info,Image *image)
1494 register MagickRealType
1505 assert(draw_info != (const DrawInfo *) NULL);
1506 if (image->debug != MagickFalse)
1507 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1508 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1509 clone_info->miterlimit=0;
1510 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1511 number_vertices=(size_t) i;
1512 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1513 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1514 if (dash_polygon == (PrimitiveInfo *) NULL)
1515 return(MagickFalse);
1516 dash_polygon[0]=primitive_info[0];
1517 scale=ExpandAffine(&draw_info->affine);
1518 length=scale*(draw_info->dash_pattern[0]-0.5);
1519 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1521 for (n=0; offset > 0.0; j=0)
1523 if (draw_info->dash_pattern[n] <= 0.0)
1525 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1526 if (offset > length)
1530 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1533 if (offset < length)
1545 for (i=1; i < (ssize_t) number_vertices; i++)
1547 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1548 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1549 maximum_length=hypot((double) dx,dy);
1553 if (draw_info->dash_pattern[n] == 0.0)
1555 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1557 for (total_length=0.0; (total_length+length) < maximum_length; )
1559 total_length+=length;
1560 if ((n & 0x01) != 0)
1562 dash_polygon[0]=primitive_info[0];
1563 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1564 total_length/maximum_length);
1565 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1566 total_length/maximum_length);
1571 if ((j+1) > (ssize_t) (2*number_vertices))
1573 dash_polygon[j]=primitive_info[i-1];
1574 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1575 total_length/maximum_length);
1576 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1577 total_length/maximum_length);
1578 dash_polygon[j].coordinates=1;
1580 dash_polygon[0].coordinates=(size_t) j;
1581 dash_polygon[j].primitive=UndefinedPrimitive;
1582 status|=DrawStrokePolygon(image,clone_info,dash_polygon);
1585 if (draw_info->dash_pattern[n] == 0.0)
1587 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1589 length-=(maximum_length-total_length);
1590 if ((n & 0x01) != 0)
1592 dash_polygon[j]=primitive_info[i];
1593 dash_polygon[j].coordinates=1;
1596 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1598 dash_polygon[j]=primitive_info[i-1];
1599 dash_polygon[j].point.x+=MagickEpsilon;
1600 dash_polygon[j].point.y+=MagickEpsilon;
1601 dash_polygon[j].coordinates=1;
1603 dash_polygon[0].coordinates=(size_t) j;
1604 dash_polygon[j].primitive=UndefinedPrimitive;
1605 status|=DrawStrokePolygon(image,clone_info,dash_polygon);
1607 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1608 clone_info=DestroyDrawInfo(clone_info);
1609 if (image->debug != MagickFalse)
1610 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1611 return(status != 0 ? MagickTrue : MagickFalse);
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 % D r a w I m a g e %
1623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1625 % DrawImage() draws a graphic primitive on your image. The primitive
1626 % may be represented as a string or filename. Precede the filename with an
1627 % "at" sign (@) and the contents of the file are drawn on the image. You
1628 % can affect how text is drawn by setting one or more members of the draw
1631 % The format of the DrawImage method is:
1633 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1634 % ExceptionInfo *exception)
1636 % A description of each parameter follows:
1638 % o image: the image.
1640 % o draw_info: the draw info.
1642 % o exception: return any errors or warnings in this structure.
1646 static inline MagickBooleanType IsPoint(const char *point)
1654 value=InterpretLocaleValue(point,&p);
1655 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1658 static inline void TracePoint(PrimitiveInfo *primitive_info,
1659 const PointInfo point)
1661 primitive_info->coordinates=1;
1662 primitive_info->point=point;
1665 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1666 ExceptionInfo *exception)
1668 #define RenderImageTag "Render/Image"
1675 key[2*MaxTextExtent],
1676 keyword[MaxTextExtent],
1677 geometry[MaxTextExtent],
1678 name[MaxTextExtent],
1679 pattern[MaxTextExtent],
1730 Ensure the annotation info is valid.
1732 assert(image != (Image *) NULL);
1733 assert(image->signature == MagickSignature);
1734 if (image->debug != MagickFalse)
1735 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1736 assert(draw_info != (DrawInfo *) NULL);
1737 assert(draw_info->signature == MagickSignature);
1738 if (image->debug != MagickFalse)
1739 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1740 if ((draw_info->primitive == (char *) NULL) ||
1741 (*draw_info->primitive == '\0'))
1742 return(MagickFalse);
1743 if (image->debug != MagickFalse)
1744 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1745 if (*draw_info->primitive != '@')
1746 primitive=AcquireString(draw_info->primitive);
1748 primitive=FileToString(draw_info->primitive+1,~0,&image->exception);
1749 if (primitive == (char *) NULL)
1750 return(MagickFalse);
1751 primitive_extent=(MagickRealType) strlen(primitive);
1752 (void) SetImageArtifact(image,"MVG",primitive);
1755 Allocate primitive info memory.
1757 graphic_context=(DrawInfo **) AcquireMagickMemory(
1758 sizeof(*graphic_context));
1759 if (graphic_context == (DrawInfo **) NULL)
1761 primitive=DestroyString(primitive);
1762 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1766 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1767 sizeof(*primitive_info));
1768 if (primitive_info == (PrimitiveInfo *) NULL)
1770 primitive=DestroyString(primitive);
1771 for ( ; n >= 0; n--)
1772 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1773 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1774 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1777 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1778 graphic_context[n]->viewbox=image->page;
1779 if ((image->page.width == 0) || (image->page.height == 0))
1781 graphic_context[n]->viewbox.width=image->columns;
1782 graphic_context[n]->viewbox.height=image->rows;
1784 token=AcquireString(primitive);
1785 (void) QueryColorDatabase("#000000",&start_color,&image->exception);
1786 if (SetImageStorageClass(image,DirectClass,&image->exception) == MagickFalse)
1787 return(MagickFalse);
1789 for (q=primitive; *q != '\0'; )
1792 Interpret graphic primitive.
1794 GetMagickToken(q,&q,keyword);
1795 if (*keyword == '\0')
1797 if (*keyword == '#')
1802 while ((*q != '\n') && (*q != '\0'))
1806 p=q-strlen(keyword)-1;
1807 primitive_type=UndefinedPrimitive;
1808 current=graphic_context[n]->affine;
1809 GetAffineMatrix(&affine);
1817 if (LocaleCompare("affine",keyword) == 0)
1819 GetMagickToken(q,&q,token);
1820 affine.sx=InterpretLocaleValue(token,(char **) NULL);
1821 GetMagickToken(q,&q,token);
1823 GetMagickToken(q,&q,token);
1824 affine.rx=InterpretLocaleValue(token,(char **) NULL);
1825 GetMagickToken(q,&q,token);
1827 GetMagickToken(q,&q,token);
1828 affine.ry=InterpretLocaleValue(token,(char **) NULL);
1829 GetMagickToken(q,&q,token);
1831 GetMagickToken(q,&q,token);
1832 affine.sy=InterpretLocaleValue(token,(char **) NULL);
1833 GetMagickToken(q,&q,token);
1835 GetMagickToken(q,&q,token);
1836 affine.tx=InterpretLocaleValue(token,(char **) NULL);
1837 GetMagickToken(q,&q,token);
1839 GetMagickToken(q,&q,token);
1840 affine.ty=InterpretLocaleValue(token,(char **) NULL);
1843 if (LocaleCompare("arc",keyword) == 0)
1845 primitive_type=ArcPrimitive;
1854 if (LocaleCompare("bezier",keyword) == 0)
1856 primitive_type=BezierPrimitive;
1859 if (LocaleCompare("border-color",keyword) == 0)
1861 GetMagickToken(q,&q,token);
1862 (void) QueryColorDatabase(token,&graphic_context[n]->border_color,
1872 if (LocaleCompare("clip-path",keyword) == 0)
1877 GetMagickToken(q,&q,token);
1878 (void) CloneString(&graphic_context[n]->clip_mask,token);
1879 (void) DrawClipPath(image,graphic_context[n],
1880 graphic_context[n]->clip_mask,exception);
1883 if (LocaleCompare("clip-rule",keyword) == 0)
1888 GetMagickToken(q,&q,token);
1889 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1891 if (fill_rule == -1)
1896 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1899 if (LocaleCompare("clip-units",keyword) == 0)
1904 GetMagickToken(q,&q,token);
1905 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1907 if (clip_units == -1)
1912 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1913 if (clip_units == ObjectBoundingBox)
1915 GetAffineMatrix(¤t);
1916 affine.sx=draw_info->bounds.x2;
1917 affine.sy=draw_info->bounds.y2;
1918 affine.tx=draw_info->bounds.x1;
1919 affine.ty=draw_info->bounds.y1;
1924 if (LocaleCompare("circle",keyword) == 0)
1926 primitive_type=CirclePrimitive;
1929 if (LocaleCompare("color",keyword) == 0)
1931 primitive_type=ColorPrimitive;
1940 if (LocaleCompare("decorate",keyword) == 0)
1945 GetMagickToken(q,&q,token);
1946 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1953 graphic_context[n]->decorate=(DecorationType) decorate;
1962 if (LocaleCompare("ellipse",keyword) == 0)
1964 primitive_type=EllipsePrimitive;
1967 if (LocaleCompare("encoding",keyword) == 0)
1969 GetMagickToken(q,&q,token);
1970 (void) CloneString(&graphic_context[n]->encoding,token);
1979 if (LocaleCompare("fill",keyword) == 0)
1981 GetMagickToken(q,&q,token);
1982 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1983 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1984 (void) DrawPatternPath(image,draw_info,token,
1985 &graphic_context[n]->fill_pattern,exception);
1988 status=QueryColorDatabase(token,&graphic_context[n]->fill,
1990 if (status == MagickFalse)
1995 pattern_info=AcquireImageInfo();
1996 (void) CopyMagickString(pattern_info->filename,token,
1998 graphic_context[n]->fill_pattern=
1999 ReadImage(pattern_info,&image->exception);
2000 CatchException(&image->exception);
2001 pattern_info=DestroyImageInfo(pattern_info);
2006 if (LocaleCompare("fill-opacity",keyword) == 0)
2008 GetMagickToken(q,&q,token);
2009 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2010 graphic_context[n]->fill.alpha=ClampToQuantum((MagickRealType)
2011 QuantumRange*factor*InterpretLocaleValue(token,
2015 if (LocaleCompare("fill-rule",keyword) == 0)
2020 GetMagickToken(q,&q,token);
2021 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2023 if (fill_rule == -1)
2028 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2031 if (LocaleCompare("font",keyword) == 0)
2033 GetMagickToken(q,&q,token);
2034 (void) CloneString(&graphic_context[n]->font,token);
2035 if (LocaleCompare("none",token) == 0)
2036 graphic_context[n]->font=(char *)
2037 RelinquishMagickMemory(graphic_context[n]->font);
2040 if (LocaleCompare("font-family",keyword) == 0)
2042 GetMagickToken(q,&q,token);
2043 (void) CloneString(&graphic_context[n]->family,token);
2046 if (LocaleCompare("font-size",keyword) == 0)
2048 GetMagickToken(q,&q,token);
2049 graphic_context[n]->pointsize=InterpretLocaleValue(token,
2053 if (LocaleCompare("font-stretch",keyword) == 0)
2058 GetMagickToken(q,&q,token);
2059 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2065 graphic_context[n]->stretch=(StretchType) stretch;
2068 if (LocaleCompare("font-style",keyword) == 0)
2073 GetMagickToken(q,&q,token);
2074 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2080 graphic_context[n]->style=(StyleType) style;
2083 if (LocaleCompare("font-weight",keyword) == 0)
2085 GetMagickToken(q,&q,token);
2086 graphic_context[n]->weight=StringToUnsignedLong(token);
2087 if (LocaleCompare(token,"all") == 0)
2088 graphic_context[n]->weight=0;
2089 if (LocaleCompare(token,"bold") == 0)
2090 graphic_context[n]->weight=700;
2091 if (LocaleCompare(token,"bolder") == 0)
2092 if (graphic_context[n]->weight <= 800)
2093 graphic_context[n]->weight+=100;
2094 if (LocaleCompare(token,"lighter") == 0)
2095 if (graphic_context[n]->weight >= 100)
2096 graphic_context[n]->weight-=100;
2097 if (LocaleCompare(token,"normal") == 0)
2098 graphic_context[n]->weight=400;
2107 if (LocaleCompare("gradient-units",keyword) == 0)
2109 GetMagickToken(q,&q,token);
2112 if (LocaleCompare("gravity",keyword) == 0)
2117 GetMagickToken(q,&q,token);
2118 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2124 graphic_context[n]->gravity=(GravityType) gravity;
2133 if (LocaleCompare("image",keyword) == 0)
2138 primitive_type=ImagePrimitive;
2139 GetMagickToken(q,&q,token);
2140 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2146 graphic_context[n]->compose=(CompositeOperator) compose;
2149 if (LocaleCompare("interline-spacing",keyword) == 0)
2151 GetMagickToken(q,&q,token);
2152 graphic_context[n]->interline_spacing=InterpretLocaleValue(token,
2156 if (LocaleCompare("interword-spacing",keyword) == 0)
2158 GetMagickToken(q,&q,token);
2159 graphic_context[n]->interword_spacing=InterpretLocaleValue(token,
2169 if (LocaleCompare("kerning",keyword) == 0)
2171 GetMagickToken(q,&q,token);
2172 graphic_context[n]->kerning=InterpretLocaleValue(token,
2182 if (LocaleCompare("line",keyword) == 0)
2184 primitive_type=LinePrimitive;
2193 if (LocaleCompare("matte",keyword) == 0)
2195 primitive_type=MattePrimitive;
2204 if (LocaleCompare("offset",keyword) == 0)
2206 GetMagickToken(q,&q,token);
2209 if (LocaleCompare("opacity",keyword) == 0)
2211 GetMagickToken(q,&q,token);
2212 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2213 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2214 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
2215 factor*InterpretLocaleValue(token,(char **) NULL))));
2216 graphic_context[n]->fill.alpha=graphic_context[n]->alpha;
2217 graphic_context[n]->stroke.alpha=graphic_context[n]->alpha;
2226 if (LocaleCompare("path",keyword) == 0)
2228 primitive_type=PathPrimitive;
2231 if (LocaleCompare("point",keyword) == 0)
2233 primitive_type=PointPrimitive;
2236 if (LocaleCompare("polyline",keyword) == 0)
2238 primitive_type=PolylinePrimitive;
2241 if (LocaleCompare("polygon",keyword) == 0)
2243 primitive_type=PolygonPrimitive;
2246 if (LocaleCompare("pop",keyword) == 0)
2248 GetMagickToken(q,&q,token);
2249 if (LocaleCompare("clip-path",token) == 0)
2251 if (LocaleCompare("defs",token) == 0)
2253 if (LocaleCompare("gradient",token) == 0)
2255 if (LocaleCompare("graphic-context",token) == 0)
2259 (void) ThrowMagickException(&image->exception,
2260 GetMagickModule(),DrawError,
2261 "UnbalancedGraphicContextPushPop","`%s'",token);
2265 if (graphic_context[n]->clip_mask != (char *) NULL)
2266 if (LocaleCompare(graphic_context[n]->clip_mask,
2267 graphic_context[n-1]->clip_mask) != 0)
2268 (void) SetImageClipMask(image,(Image *) NULL,exception);
2269 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2273 if (LocaleCompare("pattern",token) == 0)
2278 if (LocaleCompare("push",keyword) == 0)
2280 GetMagickToken(q,&q,token);
2281 if (LocaleCompare("clip-path",token) == 0)
2284 name[MaxTextExtent];
2286 GetMagickToken(q,&q,token);
2287 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2288 for (p=q; *q != '\0'; )
2290 GetMagickToken(q,&q,token);
2291 if (LocaleCompare(token,"pop") != 0)
2293 GetMagickToken(q,(const char **) NULL,token);
2294 if (LocaleCompare(token,"clip-path") != 0)
2298 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2299 (void) SetImageArtifact(image,name,token);
2300 GetMagickToken(q,&q,token);
2303 if (LocaleCompare("gradient",token) == 0)
2306 key[2*MaxTextExtent],
2307 name[MaxTextExtent],
2308 type[MaxTextExtent];
2313 GetMagickToken(q,&q,token);
2314 (void) CopyMagickString(name,token,MaxTextExtent);
2315 GetMagickToken(q,&q,token);
2316 (void) CopyMagickString(type,token,MaxTextExtent);
2317 GetMagickToken(q,&q,token);
2318 segment.x1=InterpretLocaleValue(token,(char **) NULL);
2319 GetMagickToken(q,&q,token);
2321 GetMagickToken(q,&q,token);
2322 segment.y1=InterpretLocaleValue(token,(char **) NULL);
2323 GetMagickToken(q,&q,token);
2325 GetMagickToken(q,&q,token);
2326 segment.x2=InterpretLocaleValue(token,(char **) NULL);
2327 GetMagickToken(q,&q,token);
2329 GetMagickToken(q,&q,token);
2330 segment.y2=InterpretLocaleValue(token,(char **) NULL);
2331 if (LocaleCompare(type,"radial") == 0)
2333 GetMagickToken(q,&q,token);
2335 GetMagickToken(q,&q,token);
2337 for (p=q; *q != '\0'; )
2339 GetMagickToken(q,&q,token);
2340 if (LocaleCompare(token,"pop") != 0)
2342 GetMagickToken(q,(const char **) NULL,token);
2343 if (LocaleCompare(token,"gradient") != 0)
2347 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2348 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2349 graphic_context[n]->affine.ry*segment.y1+
2350 graphic_context[n]->affine.tx;
2351 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2352 graphic_context[n]->affine.sy*segment.y1+
2353 graphic_context[n]->affine.ty;
2354 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2355 graphic_context[n]->affine.ry*segment.y2+
2356 graphic_context[n]->affine.tx;
2357 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2358 graphic_context[n]->affine.sy*segment.y2+
2359 graphic_context[n]->affine.ty;
2360 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2361 (void) SetImageArtifact(image,key,token);
2362 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2363 (void) FormatLocaleString(geometry,MaxTextExtent,
2364 "%gx%g%+.15g%+.15g",
2365 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2366 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2367 bounds.x1,bounds.y1);
2368 (void) SetImageArtifact(image,key,geometry);
2369 GetMagickToken(q,&q,token);
2372 if (LocaleCompare("pattern",token) == 0)
2377 GetMagickToken(q,&q,token);
2378 (void) CopyMagickString(name,token,MaxTextExtent);
2379 GetMagickToken(q,&q,token);
2380 bounds.x=(ssize_t) ceil(InterpretLocaleValue(token,
2381 (char **) NULL)-0.5);
2382 GetMagickToken(q,&q,token);
2384 GetMagickToken(q,&q,token);
2385 bounds.y=(ssize_t) ceil(InterpretLocaleValue(token,
2386 (char **) NULL)-0.5);
2387 GetMagickToken(q,&q,token);
2389 GetMagickToken(q,&q,token);
2390 bounds.width=(size_t) floor(InterpretLocaleValue(token,
2391 (char **) NULL)+0.5);
2392 GetMagickToken(q,&q,token);
2394 GetMagickToken(q,&q,token);
2395 bounds.height=(size_t) floor(InterpretLocaleValue(token,
2396 (char **) NULL)+0.5);
2397 for (p=q; *q != '\0'; )
2399 GetMagickToken(q,&q,token);
2400 if (LocaleCompare(token,"pop") != 0)
2402 GetMagickToken(q,(const char **) NULL,token);
2403 if (LocaleCompare(token,"pattern") != 0)
2407 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2408 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2409 (void) SetImageArtifact(image,key,token);
2410 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2411 (void) FormatLocaleString(geometry,MaxTextExtent,
2412 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2413 bounds.height,(double) bounds.x,(double) bounds.y);
2414 (void) SetImageArtifact(image,key,geometry);
2415 GetMagickToken(q,&q,token);
2418 if (LocaleCompare("graphic-context",token) == 0)
2421 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2422 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2423 if (graphic_context == (DrawInfo **) NULL)
2425 (void) ThrowMagickException(&image->exception,
2426 GetMagickModule(),ResourceLimitError,
2427 "MemoryAllocationFailed","`%s'",image->filename);
2430 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2431 graphic_context[n-1]);
2434 if (LocaleCompare("defs",token) == 0)
2445 if (LocaleCompare("rectangle",keyword) == 0)
2447 primitive_type=RectanglePrimitive;
2450 if (LocaleCompare("rotate",keyword) == 0)
2452 GetMagickToken(q,&q,token);
2453 angle=InterpretLocaleValue(token,(char **) NULL);
2454 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2455 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2456 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2457 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2460 if (LocaleCompare("roundRectangle",keyword) == 0)
2462 primitive_type=RoundRectanglePrimitive;
2471 if (LocaleCompare("scale",keyword) == 0)
2473 GetMagickToken(q,&q,token);
2474 affine.sx=InterpretLocaleValue(token,(char **) NULL);
2475 GetMagickToken(q,&q,token);
2477 GetMagickToken(q,&q,token);
2478 affine.sy=InterpretLocaleValue(token,(char **) NULL);
2481 if (LocaleCompare("skewX",keyword) == 0)
2483 GetMagickToken(q,&q,token);
2484 angle=InterpretLocaleValue(token,(char **) NULL);
2485 affine.ry=sin(DegreesToRadians(angle));
2488 if (LocaleCompare("skewY",keyword) == 0)
2490 GetMagickToken(q,&q,token);
2491 angle=InterpretLocaleValue(token,(char **) NULL);
2492 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2495 if (LocaleCompare("stop-color",keyword) == 0)
2500 GetMagickToken(q,&q,token);
2501 (void) QueryColorDatabase(token,&stop_color,&image->exception);
2502 (void) GradientImage(image,LinearGradient,ReflectSpread,
2503 &start_color,&stop_color,&image->exception);
2504 start_color=stop_color;
2505 GetMagickToken(q,&q,token);
2508 if (LocaleCompare("stroke",keyword) == 0)
2510 GetMagickToken(q,&q,token);
2511 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2512 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2513 (void) DrawPatternPath(image,draw_info,token,
2514 &graphic_context[n]->stroke_pattern,exception);
2517 status=QueryColorDatabase(token,&graphic_context[n]->stroke,
2519 if (status == MagickFalse)
2524 pattern_info=AcquireImageInfo();
2525 (void) CopyMagickString(pattern_info->filename,token,
2527 graphic_context[n]->stroke_pattern=
2528 ReadImage(pattern_info,&image->exception);
2529 CatchException(&image->exception);
2530 pattern_info=DestroyImageInfo(pattern_info);
2535 if (LocaleCompare("stroke-antialias",keyword) == 0)
2537 GetMagickToken(q,&q,token);
2538 graphic_context[n]->stroke_antialias=
2539 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2542 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2544 if (graphic_context[n]->dash_pattern != (double *) NULL)
2545 graphic_context[n]->dash_pattern=(double *)
2546 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2547 if (IsPoint(q) != MagickFalse)
2553 GetMagickToken(p,&p,token);
2555 GetMagickToken(p,&p,token);
2556 for (x=0; IsPoint(token) != MagickFalse; x++)
2558 GetMagickToken(p,&p,token);
2560 GetMagickToken(p,&p,token);
2562 graphic_context[n]->dash_pattern=(double *)
2563 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2564 sizeof(*graphic_context[n]->dash_pattern));
2565 if (graphic_context[n]->dash_pattern == (double *) NULL)
2567 (void) ThrowMagickException(&image->exception,
2568 GetMagickModule(),ResourceLimitError,
2569 "MemoryAllocationFailed","`%s'",image->filename);
2572 for (j=0; j < x; j++)
2574 GetMagickToken(q,&q,token);
2576 GetMagickToken(q,&q,token);
2577 graphic_context[n]->dash_pattern[j]=InterpretLocaleValue(
2578 token,(char **) NULL);
2580 if ((x & 0x01) != 0)
2581 for ( ; j < (2*x); j++)
2582 graphic_context[n]->dash_pattern[j]=
2583 graphic_context[n]->dash_pattern[j-x];
2584 graphic_context[n]->dash_pattern[j]=0.0;
2587 GetMagickToken(q,&q,token);
2590 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2592 GetMagickToken(q,&q,token);
2593 graphic_context[n]->dash_offset=InterpretLocaleValue(token,
2597 if (LocaleCompare("stroke-linecap",keyword) == 0)
2602 GetMagickToken(q,&q,token);
2603 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2609 graphic_context[n]->linecap=(LineCap) linecap;
2612 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2617 GetMagickToken(q,&q,token);
2618 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,token);
2624 graphic_context[n]->linejoin=(LineJoin) linejoin;
2627 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2629 GetMagickToken(q,&q,token);
2630 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2633 if (LocaleCompare("stroke-opacity",keyword) == 0)
2635 GetMagickToken(q,&q,token);
2636 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2637 graphic_context[n]->stroke.alpha=ClampToQuantum((MagickRealType)
2638 QuantumRange*factor*InterpretLocaleValue(token,
2642 if (LocaleCompare("stroke-width",keyword) == 0)
2644 GetMagickToken(q,&q,token);
2645 graphic_context[n]->stroke_width=InterpretLocaleValue(token,
2655 if (LocaleCompare("text",keyword) == 0)
2657 primitive_type=TextPrimitive;
2660 if (LocaleCompare("text-align",keyword) == 0)
2665 GetMagickToken(q,&q,token);
2666 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2672 graphic_context[n]->align=(AlignType) align;
2675 if (LocaleCompare("text-anchor",keyword) == 0)
2680 GetMagickToken(q,&q,token);
2681 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2687 graphic_context[n]->align=(AlignType) align;
2690 if (LocaleCompare("text-antialias",keyword) == 0)
2692 GetMagickToken(q,&q,token);
2693 graphic_context[n]->text_antialias=
2694 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2697 if (LocaleCompare("text-undercolor",keyword) == 0)
2699 GetMagickToken(q,&q,token);
2700 (void) QueryColorDatabase(token,&graphic_context[n]->undercolor,
2704 if (LocaleCompare("translate",keyword) == 0)
2706 GetMagickToken(q,&q,token);
2707 affine.tx=InterpretLocaleValue(token,(char **) NULL);
2708 GetMagickToken(q,&q,token);
2710 GetMagickToken(q,&q,token);
2711 affine.ty=InterpretLocaleValue(token,(char **) NULL);
2720 if (LocaleCompare("viewbox",keyword) == 0)
2722 GetMagickToken(q,&q,token);
2723 graphic_context[n]->viewbox.x=(ssize_t) ceil(InterpretLocaleValue(
2724 token,(char **) NULL)-0.5);
2725 GetMagickToken(q,&q,token);
2727 GetMagickToken(q,&q,token);
2728 graphic_context[n]->viewbox.y=(ssize_t) ceil(InterpretLocaleValue(
2729 token,(char **) NULL)-0.5);
2730 GetMagickToken(q,&q,token);
2732 GetMagickToken(q,&q,token);
2733 graphic_context[n]->viewbox.width=(size_t) floor(
2734 InterpretLocaleValue(token,(char **) NULL)+0.5);
2735 GetMagickToken(q,&q,token);
2737 GetMagickToken(q,&q,token);
2738 graphic_context[n]->viewbox.height=(size_t) floor(
2739 InterpretLocaleValue(token,(char **) NULL)+0.5);
2751 if (status == MagickFalse)
2753 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2754 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2756 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2757 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2758 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2759 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2760 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2762 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2765 if (primitive_type == UndefinedPrimitive)
2767 if (image->debug != MagickFalse)
2768 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2773 Parse the primitive attributes.
2777 primitive_info[0].point.x=0.0;
2778 primitive_info[0].point.y=0.0;
2779 for (x=0; *q != '\0'; x++)
2784 if (IsPoint(q) == MagickFalse)
2786 GetMagickToken(q,&q,token);
2787 point.x=InterpretLocaleValue(token,(char **) NULL);
2788 GetMagickToken(q,&q,token);
2790 GetMagickToken(q,&q,token);
2791 point.y=InterpretLocaleValue(token,(char **) NULL);
2792 GetMagickToken(q,(const char **) NULL,token);
2794 GetMagickToken(q,&q,token);
2795 primitive_info[i].primitive=primitive_type;
2796 primitive_info[i].point=point;
2797 primitive_info[i].coordinates=0;
2798 primitive_info[i].method=FloodfillMethod;
2800 if (i < (ssize_t) number_points)
2803 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2804 (size_t) number_points,sizeof(*primitive_info));
2805 if (primitive_info == (PrimitiveInfo *) NULL)
2807 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2808 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2812 primitive_info[j].primitive=primitive_type;
2813 primitive_info[j].coordinates=(size_t) x;
2814 primitive_info[j].method=FloodfillMethod;
2815 primitive_info[j].text=(char *) NULL;
2817 Circumscribe primitive within a circle.
2819 bounds.x1=primitive_info[j].point.x;
2820 bounds.y1=primitive_info[j].point.y;
2821 bounds.x2=primitive_info[j].point.x;
2822 bounds.y2=primitive_info[j].point.y;
2823 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2825 point=primitive_info[j+k].point;
2826 if (point.x < bounds.x1)
2828 if (point.y < bounds.y1)
2830 if (point.x > bounds.x2)
2832 if (point.y > bounds.y2)
2836 Speculate how many points our primitive might consume.
2838 length=primitive_info[j].coordinates;
2839 switch (primitive_type)
2841 case RectanglePrimitive:
2846 case RoundRectanglePrimitive:
2848 length*=5+8*BezierQuantum;
2851 case BezierPrimitive:
2853 if (primitive_info[j].coordinates > 107)
2854 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2855 DrawError,"TooManyBezierCoordinates","`%s'",token);
2856 length=BezierQuantum*primitive_info[j].coordinates;
2865 GetMagickToken(q,&q,token);
2868 for (s=token; *s != '\0'; s=t)
2873 value=InterpretLocaleValue(s,&t);
2882 length=length*BezierQuantum/2;
2885 case CirclePrimitive:
2887 case EllipsePrimitive:
2894 alpha=bounds.x2-bounds.x1;
2895 beta=bounds.y2-bounds.y1;
2896 radius=hypot((double) alpha,(double) beta);
2897 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2903 if ((size_t) (i+length) >= number_points)
2906 Resize based on speculative points required by primitive.
2908 number_points+=length+1;
2909 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2910 (size_t) number_points,sizeof(*primitive_info));
2911 if (primitive_info == (PrimitiveInfo *) NULL)
2913 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2914 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2919 switch (primitive_type)
2921 case PointPrimitive:
2924 if (primitive_info[j].coordinates != 1)
2929 TracePoint(primitive_info+j,primitive_info[j].point);
2930 i=(ssize_t) (j+primitive_info[j].coordinates);
2935 if (primitive_info[j].coordinates != 2)
2940 TraceLine(primitive_info+j,primitive_info[j].point,
2941 primitive_info[j+1].point);
2942 i=(ssize_t) (j+primitive_info[j].coordinates);
2945 case RectanglePrimitive:
2947 if (primitive_info[j].coordinates != 2)
2952 TraceRectangle(primitive_info+j,primitive_info[j].point,
2953 primitive_info[j+1].point);
2954 i=(ssize_t) (j+primitive_info[j].coordinates);
2957 case RoundRectanglePrimitive:
2959 if (primitive_info[j].coordinates != 3)
2964 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2965 primitive_info[j+1].point,primitive_info[j+2].point);
2966 i=(ssize_t) (j+primitive_info[j].coordinates);
2971 if (primitive_info[j].coordinates != 3)
2973 primitive_type=UndefinedPrimitive;
2976 TraceArc(primitive_info+j,primitive_info[j].point,
2977 primitive_info[j+1].point,primitive_info[j+2].point);
2978 i=(ssize_t) (j+primitive_info[j].coordinates);
2981 case EllipsePrimitive:
2983 if (primitive_info[j].coordinates != 3)
2988 TraceEllipse(primitive_info+j,primitive_info[j].point,
2989 primitive_info[j+1].point,primitive_info[j+2].point);
2990 i=(ssize_t) (j+primitive_info[j].coordinates);
2993 case CirclePrimitive:
2995 if (primitive_info[j].coordinates != 2)
3000 TraceCircle(primitive_info+j,primitive_info[j].point,
3001 primitive_info[j+1].point);
3002 i=(ssize_t) (j+primitive_info[j].coordinates);
3005 case PolylinePrimitive:
3007 case PolygonPrimitive:
3009 primitive_info[i]=primitive_info[j];
3010 primitive_info[i].coordinates=0;
3011 primitive_info[j].coordinates++;
3015 case BezierPrimitive:
3017 if (primitive_info[j].coordinates < 3)
3022 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3023 i=(ssize_t) (j+primitive_info[j].coordinates);
3028 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3031 case ColorPrimitive:
3032 case MattePrimitive:
3037 if (primitive_info[j].coordinates != 1)
3042 GetMagickToken(q,&q,token);
3043 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3049 primitive_info[j].method=(PaintMethod) method;
3054 if (primitive_info[j].coordinates != 1)
3060 GetMagickToken(q,&q,token);
3061 primitive_info[j].text=AcquireString(token);
3064 case ImagePrimitive:
3066 if (primitive_info[j].coordinates != 2)
3071 GetMagickToken(q,&q,token);
3072 primitive_info[j].text=AcquireString(token);
3076 if (primitive_info == (PrimitiveInfo *) NULL)
3078 if (image->debug != MagickFalse)
3079 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3080 if (status == MagickFalse)
3082 primitive_info[i].primitive=UndefinedPrimitive;
3088 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3090 point=primitive_info[i].point;
3091 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3092 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3093 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3094 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3095 point=primitive_info[i].point;
3096 if (point.x < graphic_context[n]->bounds.x1)
3097 graphic_context[n]->bounds.x1=point.x;
3098 if (point.y < graphic_context[n]->bounds.y1)
3099 graphic_context[n]->bounds.y1=point.y;
3100 if (point.x > graphic_context[n]->bounds.x2)
3101 graphic_context[n]->bounds.x2=point.x;
3102 if (point.y > graphic_context[n]->bounds.y2)
3103 graphic_context[n]->bounds.y2=point.y;
3104 if (primitive_info[i].primitive == ImagePrimitive)
3106 if (i >= (ssize_t) number_points)
3107 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3109 if (graphic_context[n]->render != MagickFalse)
3111 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3112 (LocaleCompare(graphic_context[n]->clip_mask,
3113 graphic_context[n-1]->clip_mask) != 0))
3114 (void) DrawClipPath(image,graphic_context[n],
3115 graphic_context[n]->clip_mask,exception);
3116 (void) DrawPrimitive(image,graphic_context[n],primitive_info);
3118 if (primitive_info->text != (char *) NULL)
3119 primitive_info->text=(char *) RelinquishMagickMemory(
3120 primitive_info->text);
3121 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3123 if (proceed == MagickFalse)
3126 if (image->debug != MagickFalse)
3127 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3129 Relinquish resources.
3131 token=DestroyString(token);
3132 if (primitive_info != (PrimitiveInfo *) NULL)
3133 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3134 primitive=DestroyString(primitive);
3135 for ( ; n >= 0; n--)
3136 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3137 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3138 if (status == MagickFalse)
3139 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3149 % D r a w G r a d i e n t I m a g e %
3153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155 % DrawGradientImage() draws a linear gradient on the image.
3157 % The format of the DrawGradientImage method is:
3159 % MagickBooleanType DrawGradientImage(Image *image,
3160 % const DrawInfo *draw_info)
3162 % A description of each parameter follows:
3164 % o image: the image.
3166 % o _info: the draw info.
3170 static inline MagickRealType 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=1.0/(gamma <= MagickEpsilon ? 1.0 : 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)
3258 Draw linear or radial gradient on image.
3260 assert(image != (Image *) NULL);
3261 assert(image->signature == MagickSignature);
3262 if (image->debug != MagickFalse)
3263 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3264 assert(draw_info != (const DrawInfo *) NULL);
3265 gradient=(&draw_info->gradient);
3266 gradient_vector=(&gradient->gradient_vector);
3267 point.x=gradient_vector->x2-gradient_vector->x1;
3268 point.y=gradient_vector->y2-gradient_vector->y1;
3269 length=sqrt(point.x*point.x+point.y*point.y);
3270 bounding_box=gradient->bounding_box;
3272 exception=(&image->exception);
3273 GetPixelInfo(image,&zero);
3274 image_view=AcquireCacheView(image);
3275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3276 #pragma omp parallel for schedule(dynamic,4) shared(status)
3278 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3298 if (status == MagickFalse)
3300 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3301 if (q == (Quantum *) NULL)
3308 offset=GetStopColorOffset(gradient,0,y);
3309 if (gradient->type != RadialGradient)
3311 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3313 SetPixelInfo(image,q,&pixel);
3314 switch (gradient->spread)
3316 case UndefinedSpread:
3319 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3320 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3322 offset=GetStopColorOffset(gradient,x,y);
3323 if (gradient->type != RadialGradient)
3326 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3327 if (offset < gradient->stops[i].offset)
3329 if ((offset < 0.0) || (i == 0))
3330 composite=gradient->stops[0].color;
3332 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3333 composite=gradient->stops[gradient->number_stops-1].color;
3338 alpha=(offset-gradient->stops[i].offset)/
3339 (gradient->stops[j].offset-gradient->stops[i].offset);
3340 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3341 &gradient->stops[j].color,alpha,&composite);
3347 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3348 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3350 offset=GetStopColorOffset(gradient,x,y);
3351 if (gradient->type != RadialGradient)
3356 if ((ssize_t) fmod(offset,2.0) == 0)
3357 offset=fmod(offset,1.0);
3359 offset=1.0-fmod(offset,1.0);
3360 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3361 if (offset < gradient->stops[i].offset)
3364 composite=gradient->stops[0].color;
3366 if (i == (ssize_t) gradient->number_stops)
3367 composite=gradient->stops[gradient->number_stops-1].color;
3372 alpha=(offset-gradient->stops[i].offset)/
3373 (gradient->stops[j].offset-gradient->stops[i].offset);
3374 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3375 &gradient->stops[j].color,alpha,&composite);
3387 antialias=MagickFalse;
3389 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3390 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3392 offset=GetStopColorOffset(gradient,x,y);
3393 if (gradient->type == LinearGradient)
3395 repeat=fmod(offset,length);
3397 repeat=length-fmod(-repeat,length);
3399 repeat=fmod(offset,length);
3400 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3401 MagickTrue : MagickFalse;
3402 offset=repeat/length;
3406 repeat=fmod(offset,gradient->radius);
3408 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3410 repeat=fmod(offset,gradient->radius);
3411 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3413 offset=repeat/gradient->radius;
3416 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3417 if (offset < gradient->stops[i].offset)
3420 composite=gradient->stops[0].color;
3422 if (i == (ssize_t) gradient->number_stops)
3423 composite=gradient->stops[gradient->number_stops-1].color;
3428 alpha=(offset-gradient->stops[i].offset)/
3429 (gradient->stops[j].offset-gradient->stops[i].offset);
3430 if (antialias != MagickFalse)
3432 if (gradient->type == LinearGradient)
3433 alpha=length-repeat;
3435 alpha=gradient->radius-repeat;
3437 j=(ssize_t) gradient->number_stops-1L;
3439 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3440 &gradient->stops[j].color,alpha,&composite);
3445 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3447 SetPixelPixelInfo(image,&pixel,q);
3448 q+=GetPixelChannels(image);
3450 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3453 image_view=DestroyCacheView(image_view);
3458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3462 % D r a w P a t t e r n P a t h %
3466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3468 % DrawPatternPath() draws a pattern.
3470 % The format of the DrawPatternPath method is:
3472 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3473 % const char *name,Image **pattern,ExceptionInfo *exception)
3475 % A description of each parameter follows:
3477 % o image: the image.
3479 % o draw_info: the draw info.
3481 % o name: the pattern name.
3483 % o image: the image.
3485 % o exception: return any errors or warnings in this structure.
3488 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3489 const DrawInfo *draw_info,const char *name,Image **pattern,
3490 ExceptionInfo *exception)
3493 property[MaxTextExtent];
3508 assert(image != (Image *) NULL);
3509 assert(image->signature == MagickSignature);
3510 if (image->debug != MagickFalse)
3511 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3512 assert(draw_info != (const DrawInfo *) NULL);
3513 assert(name != (const char *) NULL);
3514 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3515 path=GetImageArtifact(image,property);
3516 if (path == (const char *) NULL)
3517 return(MagickFalse);
3518 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3519 geometry=GetImageArtifact(image,property);
3520 if (geometry == (const char *) NULL)
3521 return(MagickFalse);
3522 if ((*pattern) != (Image *) NULL)
3523 *pattern=DestroyImage(*pattern);
3524 image_info=AcquireImageInfo();
3525 image_info->size=AcquireString(geometry);
3526 *pattern=AcquireImage(image_info);
3527 image_info=DestroyImageInfo(image_info);
3528 (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
3530 (void) SetImageBackgroundColor(*pattern);
3531 if (image->debug != MagickFalse)
3532 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3533 "begin pattern-path %s %s",name,geometry);
3534 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3535 clone_info->fill_pattern=NewImageList();
3536 clone_info->stroke_pattern=NewImageList();
3537 (void) CloneString(&clone_info->primitive,path);
3538 status=DrawImage(*pattern,clone_info,exception);
3539 clone_info=DestroyDrawInfo(clone_info);
3540 if (image->debug != MagickFalse)
3541 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3550 + D r a w P o l y g o n P r i m i t i v e %
3554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3556 % DrawPolygonPrimitive() draws a polygon on the image.
3558 % The format of the DrawPolygonPrimitive method is:
3560 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3561 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3563 % A description of each parameter follows:
3565 % o image: the image.
3567 % o draw_info: the draw info.
3569 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3573 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3578 assert(polygon_info != (PolygonInfo **) NULL);
3579 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); 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=GetOpenMPMaximumThreads();
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,GetOpenMPMaximumThreads()*
3607 sizeof(*polygon_info));
3608 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3609 if (path_info == (PathInfo *) NULL)
3610 return(DestroyPolygonThreadSet(polygon_info));
3611 for (i=0; i < (ssize_t) number_threads; i++)
3613 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3614 if (polygon_info[i] == (PolygonInfo *) NULL)
3615 return(DestroyPolygonThreadSet(polygon_info));
3617 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3618 return(polygon_info);
3621 static MagickRealType GetPixelOpacity(PolygonInfo *polygon_info,
3622 const MagickRealType mid,const MagickBooleanType fill,
3623 const FillRule fill_rule,const double x,const double y,
3624 MagickRealType *stroke_opacity)
3638 register const PointInfo
3649 Compute fill & stroke opacity for this (x,y) point.
3651 *stroke_opacity=0.0;
3652 subpath_opacity=0.0;
3653 p=polygon_info->edges;
3654 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3656 if (y <= (p->bounds.y1-mid-0.5))
3658 if (y > (p->bounds.y2+mid+0.5))
3660 (void) DestroyEdge(polygon_info,(size_t) j);
3663 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3665 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3666 for ( ; i < (ssize_t) p->number_points; i++)
3668 if (y <= (p->points[i-1].y-mid-0.5))
3670 if (y > (p->points[i].y+mid+0.5))
3672 if (p->scanline != y)
3675 p->highwater=(size_t) i;
3678 Compute distance between a point and an edge.
3681 delta.x=(q+1)->x-q->x;
3682 delta.y=(q+1)->y-q->y;
3683 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3688 distance=delta.x*delta.x+delta.y*delta.y;
3692 alpha=delta.x*delta.x+delta.y*delta.y;
3697 distance=delta.x*delta.x+delta.y*delta.y;
3702 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3703 distance=alpha*beta*beta;
3707 Compute stroke & subpath opacity.
3710 if (p->ghostline == MagickFalse)
3713 if ((*stroke_opacity < 1.0) &&
3714 (distance <= ((alpha+0.25)*(alpha+0.25))))
3717 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3718 *stroke_opacity=1.0;
3722 if (distance != 1.0)
3723 beta=sqrt((double) distance);
3725 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3726 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3730 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3732 if (distance <= 0.0)
3734 subpath_opacity=1.0;
3742 if (distance != 1.0)
3743 beta=sqrt(distance);
3746 if (subpath_opacity < (alpha*alpha))
3747 subpath_opacity=alpha*alpha;
3751 Compute fill opacity.
3753 if (fill == MagickFalse)
3755 if (subpath_opacity >= 1.0)
3758 Determine winding number.
3761 p=polygon_info->edges;
3762 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3764 if (y <= p->bounds.y1)
3766 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3768 if (x > p->bounds.x2)
3770 winding_number+=p->direction ? 1 : -1;
3773 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3774 for ( ; i < (ssize_t) p->number_points; i++)
3775 if (y <= p->points[i].y)
3778 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3779 winding_number+=p->direction ? 1 : -1;
3781 if (fill_rule != NonZeroRule)
3783 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3787 if (MagickAbsoluteValue(winding_number) != 0)
3789 return(subpath_opacity);
3792 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3793 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3809 **restrict polygon_info;
3826 Compute bounding box.
3828 assert(image != (Image *) NULL);
3829 assert(image->signature == MagickSignature);
3830 if (image->debug != MagickFalse)
3831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3832 assert(draw_info != (DrawInfo *) NULL);
3833 assert(draw_info->signature == MagickSignature);
3834 assert(primitive_info != (PrimitiveInfo *) NULL);
3835 if (primitive_info->coordinates == 0)
3837 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3838 if (polygon_info == (PolygonInfo **) NULL)
3839 return(MagickFalse);
3841 DrawBoundingRectangles(image,draw_info,polygon_info[0]);
3842 if (image->debug != MagickFalse)
3843 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3844 fill=(primitive_info->method == FillToBorderMethod) ||
3845 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3846 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3847 bounds=polygon_info[0]->edges[0].bounds;
3848 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3850 p=polygon_info[0]->edges+i;
3851 if (p->bounds.x1 < bounds.x1)
3852 bounds.x1=p->bounds.x1;
3853 if (p->bounds.y1 < bounds.y1)
3854 bounds.y1=p->bounds.y1;
3855 if (p->bounds.x2 > bounds.x2)
3856 bounds.x2=p->bounds.x2;
3857 if (p->bounds.y2 > bounds.y2)
3858 bounds.y2=p->bounds.y2;
3860 bounds.x1-=(mid+1.0);
3861 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3862 image->columns ? (double) image->columns-1.0 : bounds.x1;
3863 bounds.y1-=(mid+1.0);
3864 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3865 image->rows ? (double) image->rows-1.0 : bounds.y1;
3866 bounds.x2+=(mid+1.0);
3867 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3868 image->columns ? (double) image->columns-1.0 : bounds.x2;
3869 bounds.y2+=(mid+1.0);
3870 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3871 image->rows ? (double) image->rows-1.0 : bounds.y2;
3873 exception=(&image->exception);
3874 start=(ssize_t) ceil(bounds.x1-0.5);
3875 stop=(ssize_t) floor(bounds.x2+0.5);
3876 image_view=AcquireCacheView(image);
3877 if (primitive_info->coordinates == 1)
3882 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3883 #pragma omp parallel for schedule(dynamic,4) shared(status)
3885 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
3899 if (status == MagickFalse)
3902 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
3904 if (q == (Quantum *) NULL)
3909 for ( ; x <= stop; x++)
3911 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3912 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3914 (void) GetStrokeColor(draw_info,x,y,&pixel);
3915 SetPixelPacket(image,&pixel,q);
3917 q+=GetPixelChannels(image);
3919 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3920 if (sync == MagickFalse)
3923 image_view=DestroyCacheView(image_view);
3924 polygon_info=DestroyPolygonThreadSet(polygon_info);
3925 if (image->debug != MagickFalse)
3926 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3927 " end draw-polygon");
3931 Draw polygon or line.
3933 if (image->matte == MagickFalse)
3934 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3935 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3936 #pragma omp parallel for schedule(dynamic,4) shared(status)
3938 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
3941 id = GetOpenMPThreadId();
3957 if (status == MagickFalse)
3959 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-
3960 start+1),1,exception);
3961 if (q == (Quantum *) NULL)
3966 for (x=start; x <= stop; x++)
3971 fill_opacity=GetPixelOpacity(polygon_info[id],mid,fill,
3972 draw_info->fill_rule,(double) x,(double) y,&stroke_opacity);
3973 if (draw_info->stroke_antialias == MagickFalse)
3975 fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
3976 stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
3978 (void) GetFillColor(draw_info,x,y,&fill_color);
3979 fill_opacity=fill_opacity*fill_color.alpha;
3980 CompositePixelOver(image,&fill_color,fill_opacity,q,(MagickRealType)
3981 GetPixelAlpha(image,q),q);
3982 (void) GetStrokeColor(draw_info,x,y,&stroke_color);
3983 stroke_opacity=stroke_opacity*stroke_color.alpha;
3984 CompositePixelOver(image,&stroke_color,stroke_opacity,q,(MagickRealType)
3985 GetPixelAlpha(image,q),q);
3986 q+=GetPixelChannels(image);
3988 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3991 image_view=DestroyCacheView(image_view);
3992 polygon_info=DestroyPolygonThreadSet(polygon_info);
3993 if (image->debug != MagickFalse)
3994 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4003 % D r a w P r i m i t i v e %
4007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4009 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4011 % The format of the DrawPrimitive method is:
4013 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4014 % PrimitiveInfo *primitive_info)
4016 % A description of each parameter follows:
4018 % o image: the image.
4020 % o draw_info: the draw info.
4022 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4026 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4052 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4053 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4054 switch (primitive_info->primitive)
4056 case PointPrimitive:
4058 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4059 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4060 methods[primitive_info->method]);
4063 case ColorPrimitive:
4065 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4066 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4067 methods[primitive_info->method]);
4070 case MattePrimitive:
4072 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4073 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4074 methods[primitive_info->method]);
4079 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4080 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4083 case ImagePrimitive:
4085 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4086 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4093 p=primitive_info[0].point;
4096 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4098 point=primitive_info[i].point;
4099 if (coordinates <= 0)
4101 coordinates=(ssize_t) primitive_info[i].coordinates;
4102 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4103 " begin open (%.20g)",(double) coordinates);
4106 point=primitive_info[i].point;
4107 if ((fabs(q.x-point.x) > MagickEpsilon) ||
4108 (fabs(q.y-point.y) > MagickEpsilon))
4109 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4110 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4112 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4113 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4116 if (coordinates > 0)
4118 if ((fabs(p.x-point.x) > MagickEpsilon) ||
4119 (fabs(p.y-point.y) > MagickEpsilon))
4120 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4121 (double) coordinates);
4123 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4124 (double) coordinates);
4128 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4129 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4147 if (image->debug != MagickFalse)
4149 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4150 " begin draw-primitive");
4151 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4152 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4153 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4154 draw_info->affine.tx,draw_info->affine.ty);
4157 exception=(&image->exception);
4158 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4159 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4160 image_view=AcquireCacheView(image);
4161 switch (primitive_info->primitive)
4163 case PointPrimitive:
4171 if ((y < 0) || (y >= (ssize_t) image->rows))
4173 if ((x < 0) || (x >= (ssize_t) image->columns))
4175 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4176 if (q == (Quantum *) NULL)
4178 (void) GetFillColor(draw_info,x,y,&fill_color);
4179 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4180 (MagickRealType) GetPixelAlpha(image,q),q);
4181 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4184 case ColorPrimitive:
4186 switch (primitive_info->method)
4197 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4198 if (q == (Quantum *) NULL)
4200 (void) GetFillColor(draw_info,x,y,&pixel);
4201 SetPixelPacket(image,&pixel,q);
4202 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4214 (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4215 for (y=0; y < (ssize_t) image->rows; y++)
4220 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4222 if (q == (Quantum *) NULL)
4224 for (x=0; x < (ssize_t) image->columns; x++)
4226 GetPixelPacket(image,q,&pixel);
4227 if (IsFuzzyEquivalencePixelPacket(image,&pixel,&target) == MagickFalse)
4229 q+=GetPixelChannels(image);
4232 (void) GetFillColor(draw_info,x,y,&pixel);
4233 SetPixelPacket(image,&pixel,q);
4234 q+=GetPixelChannels(image);
4236 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4237 if (sync == MagickFalse)
4242 case FloodfillMethod:
4243 case FillToBorderMethod:
4248 (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4249 if (primitive_info->method == FillToBorderMethod)
4251 target.red=(MagickRealType) draw_info->border_color.red;
4252 target.green=(MagickRealType) draw_info->border_color.green;
4253 target.blue=(MagickRealType) draw_info->border_color.blue;
4255 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4256 primitive_info->method == FloodfillMethod ? MagickFalse :
4257 MagickTrue,exception);
4268 for (y=0; y < (ssize_t) image->rows; y++)
4276 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4278 if (q == (Quantum *) NULL)
4280 for (x=0; x < (ssize_t) image->columns; x++)
4282 (void) GetFillColor(draw_info,x,y,&pixel);
4283 SetPixelPacket(image,&pixel,q);
4284 q+=GetPixelChannels(image);
4286 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4287 if (sync == MagickFalse)
4295 case MattePrimitive:
4297 if (image->matte == MagickFalse)
4298 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4299 switch (primitive_info->method)
4310 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4311 if (q == (Quantum *) NULL)
4313 (void) GetFillColor(draw_info,x,y,&pixel);
4314 SetPixelAlpha(image,pixel.alpha,q);
4315 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4327 (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4328 for (y=0; y < (ssize_t) image->rows; y++)
4336 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4338 if (q == (Quantum *) NULL)
4340 for (x=0; x < (ssize_t) image->columns; x++)
4342 GetPixelPacket(image,q,&pixel);
4343 if (IsFuzzyEquivalencePixelPacket(image,&pixel,&target) == MagickFalse)
4345 q+=GetPixelChannels(image);
4348 (void) GetFillColor(draw_info,x,y,&pixel);
4349 SetPixelAlpha(image,pixel.alpha,q);
4350 q+=GetPixelChannels(image);
4352 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4353 if (sync == MagickFalse)
4358 case FloodfillMethod:
4359 case FillToBorderMethod:
4367 (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4368 if (primitive_info->method == FillToBorderMethod)
4370 target.red=(MagickRealType) draw_info->border_color.red;
4371 target.green=(MagickRealType) draw_info->border_color.green;
4372 target.blue=(MagickRealType) draw_info->border_color.blue;
4374 channel_mask=SetPixelChannelMask(image,AlphaChannel);
4375 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4376 primitive_info->method == FloodfillMethod ? MagickFalse :
4377 MagickTrue,exception);
4378 (void) SetPixelChannelMask(image,channel_mask);
4389 for (y=0; y < (ssize_t) image->rows; y++)
4397 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4399 if (q == (Quantum *) NULL)
4401 for (x=0; x < (ssize_t) image->columns; x++)
4403 (void) GetFillColor(draw_info,x,y,&pixel);
4404 SetPixelAlpha(image,pixel.alpha,q);
4405 q+=GetPixelChannels(image);
4407 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4408 if (sync == MagickFalse)
4419 geometry[MaxTextExtent];
4424 if (primitive_info->text == (char *) NULL)
4426 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4427 (void) CloneString(&clone_info->text,primitive_info->text);
4428 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4429 primitive_info->point.x,primitive_info->point.y);
4430 (void) CloneString(&clone_info->geometry,geometry);
4431 status=AnnotateImage(image,clone_info,exception);
4432 clone_info=DestroyDrawInfo(clone_info);
4435 case ImagePrimitive:
4441 composite_geometry[MaxTextExtent];
4456 if (primitive_info->text == (char *) NULL)
4458 clone_info=AcquireImageInfo();
4459 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4460 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4464 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4466 composite_image=ReadImage(clone_info,&image->exception);
4468 clone_info=DestroyImageInfo(clone_info);
4469 if (composite_image == (Image *) NULL)
4471 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4472 NULL,(void *) NULL);
4473 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4474 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4475 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4476 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4479 geometry[MaxTextExtent];
4484 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4485 primitive_info[1].point.x,primitive_info[1].point.y);
4486 composite_image->filter=image->filter;
4487 (void) TransformImage(&composite_image,(char *) NULL,geometry);
4489 if (composite_image->matte == MagickFalse)
4490 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4492 if (draw_info->alpha != OpaqueAlpha)
4493 (void) SetImageOpacity(composite_image,draw_info->alpha);
4494 SetGeometry(image,&geometry);
4495 image->gravity=draw_info->gravity;
4498 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4499 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4500 composite_image->rows,(double) geometry.x,(double) geometry.y);
4501 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
4503 affine=draw_info->affine;
4504 affine.tx=(double) geometry.x;
4505 affine.ty=(double) geometry.y;
4506 composite_image->interpolate=image->interpolate;
4507 if (draw_info->compose == OverCompositeOp)
4508 (void) DrawAffineImage(image,composite_image,&affine);
4510 (void) CompositeImage(image,draw_info->compose,composite_image,
4511 geometry.x,geometry.y);
4512 composite_image=DestroyImage(composite_image);
4524 if (IsEventLogging() != MagickFalse)
4525 LogPrimitiveInfo(primitive_info);
4526 scale=ExpandAffine(&draw_info->affine);
4527 if ((draw_info->dash_pattern != (double *) NULL) &&
4528 (draw_info->dash_pattern[0] != 0.0) &&
4529 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
4530 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4535 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4536 clone_info->stroke_width=0.0;
4537 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4538 status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4539 clone_info=DestroyDrawInfo(clone_info);
4540 (void) DrawDashPolygon(draw_info,primitive_info,image);
4543 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4545 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4551 Draw strokes while respecting line cap/join attributes.
4553 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4555 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4556 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4557 MagickTrue : MagickFalse;
4558 i=(ssize_t) primitive_info[0].coordinates;
4559 if ((((draw_info->linecap == RoundCap) ||
4560 (closed_path != MagickFalse)) &&
4561 (draw_info->linejoin == RoundJoin)) ||
4562 (primitive_info[i].primitive != UndefinedPrimitive))
4564 (void) DrawPolygonPrimitive(image,draw_info,primitive_info);
4567 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4568 clone_info->stroke_width=0.0;
4569 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4570 status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4571 clone_info=DestroyDrawInfo(clone_info);
4572 status|=DrawStrokePolygon(image,draw_info,primitive_info);
4575 status=DrawPolygonPrimitive(image,draw_info,primitive_info);
4579 image_view=DestroyCacheView(image_view);
4580 if (image->debug != MagickFalse)
4581 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4582 return(status != 0 ? MagickTrue : MagickFalse);
4586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4590 + D r a w S t r o k e P o l y g o n %
4594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4596 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4597 % the image while respecting the line cap and join attributes.
4599 % The format of the DrawStrokePolygon method is:
4601 % MagickBooleanType DrawStrokePolygon(Image *image,
4602 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4604 % A description of each parameter follows:
4606 % o image: the image.
4608 % o draw_info: the draw info.
4610 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4615 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4616 const PrimitiveInfo *primitive_info)
4624 for (i=0; i < 4; i++)
4625 linecap[i]=(*primitive_info);
4626 linecap[0].coordinates=4;
4627 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4628 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4629 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4630 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4631 linecap[4].primitive=UndefinedPrimitive;
4632 (void) DrawPolygonPrimitive(image,draw_info,linecap);
4635 static MagickBooleanType DrawStrokePolygon(Image *image,
4636 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4648 register const PrimitiveInfo
4653 Draw stroked polygon.
4655 if (image->debug != MagickFalse)
4656 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4657 " begin draw-stroke-polygon");
4658 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4659 clone_info->fill=draw_info->stroke;
4660 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4661 clone_info->stroke_width=0.0;
4662 clone_info->fill_rule=NonZeroRule;
4664 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4666 stroke_polygon=TraceStrokePolygon(draw_info,p);
4667 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
4668 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4669 q=p+p->coordinates-1;
4670 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4671 MagickTrue : MagickFalse;
4672 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4674 DrawRoundLinecap(image,draw_info,p);
4675 DrawRoundLinecap(image,draw_info,q);
4678 clone_info=DestroyDrawInfo(clone_info);
4679 if (image->debug != MagickFalse)
4680 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4681 " end draw-stroke-polygon");
4686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4690 % G e t A f f i n e M a t r i x %
4694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4696 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4699 % The format of the GetAffineMatrix method is:
4701 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4703 % A description of each parameter follows:
4705 % o affine_matrix: the affine matrix.
4708 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4711 assert(affine_matrix != (AffineMatrix *) NULL);
4712 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4713 affine_matrix->sx=1.0;
4714 affine_matrix->sy=1.0;
4718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4722 + G e t D r a w I n f o %
4726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4728 % GetDrawInfo() initializes draw_info to default values.
4730 % The format of the GetDrawInfo method is:
4732 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4734 % A description of each parameter follows:
4736 % o image_info: the image info..
4738 % o draw_info: the draw info.
4741 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4753 Initialize draw attributes.
4755 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4756 assert(draw_info != (DrawInfo *) NULL);
4757 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4758 clone_info=CloneImageInfo(image_info);
4759 GetAffineMatrix(&draw_info->affine);
4760 exception=AcquireExceptionInfo();
4761 (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
4762 (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
4763 draw_info->stroke_antialias=clone_info->antialias;
4764 draw_info->stroke_width=1.0;
4765 draw_info->alpha=OpaqueAlpha;
4766 draw_info->fill_rule=EvenOddRule;
4767 draw_info->linecap=ButtCap;
4768 draw_info->linejoin=MiterJoin;
4769 draw_info->miterlimit=10;
4770 draw_info->decorate=NoDecoration;
4771 if (clone_info->font != (char *) NULL)
4772 draw_info->font=AcquireString(clone_info->font);
4773 if (clone_info->density != (char *) NULL)
4774 draw_info->density=AcquireString(clone_info->density);
4775 draw_info->text_antialias=clone_info->antialias;
4776 draw_info->pointsize=12.0;
4777 if (clone_info->pointsize != 0.0)
4778 draw_info->pointsize=clone_info->pointsize;
4779 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4780 draw_info->border_color=clone_info->border_color;
4781 draw_info->compose=OverCompositeOp;
4782 if (clone_info->server_name != (char *) NULL)
4783 draw_info->server_name=AcquireString(clone_info->server_name);
4784 draw_info->render=MagickTrue;
4785 draw_info->debug=IsEventLogging();
4786 option=GetImageOption(clone_info,"encoding");
4787 if (option != (const char *) NULL)
4788 (void) CloneString(&draw_info->encoding,option);
4789 option=GetImageOption(clone_info,"kerning");
4790 if (option != (const char *) NULL)
4791 draw_info->kerning=InterpretLocaleValue(option,(char **) NULL);
4792 option=GetImageOption(clone_info,"interline-spacing");
4793 if (option != (const char *) NULL)
4794 draw_info->interline_spacing=InterpretLocaleValue(option,(char **) NULL);
4795 draw_info->direction=UndefinedDirection;
4796 option=GetImageOption(clone_info,"interword-spacing");
4797 if (option != (const char *) NULL)
4798 draw_info->interword_spacing=InterpretLocaleValue(option,(char **) NULL);
4799 option=GetImageOption(clone_info,"direction");
4800 if (option != (const char *) NULL)
4801 draw_info->direction=(DirectionType) ParseCommandOption(
4802 MagickDirectionOptions,MagickFalse,option);
4803 option=GetImageOption(clone_info,"fill");
4804 if (option != (const char *) NULL)
4805 (void) QueryColorDatabase(option,&draw_info->fill,exception);
4806 option=GetImageOption(clone_info,"stroke");
4807 if (option != (const char *) NULL)
4808 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
4809 option=GetImageOption(clone_info,"strokewidth");
4810 if (option != (const char *) NULL)
4811 draw_info->stroke_width=InterpretLocaleValue(option,(char **) NULL);
4812 option=GetImageOption(clone_info,"undercolor");
4813 if (option != (const char *) NULL)
4814 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
4815 option=GetImageOption(clone_info,"gravity");
4816 if (option != (const char *) NULL)
4817 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4818 MagickFalse,option);
4819 exception=DestroyExceptionInfo(exception);
4820 draw_info->signature=MagickSignature;
4821 clone_info=DestroyImageInfo(clone_info);
4825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4829 + P e r m u t a t e %
4833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4835 % Permutate() returns the permuation of the (n,k).
4837 % The format of the Permutate method is:
4839 % void Permutate(ssize_t n,ssize_t k)
4841 % A description of each parameter follows:
4849 static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
4858 for (i=k+1; i <= n; i++)
4860 for (i=1; i <= (n-k); i++)
4866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4870 + T r a c e P r i m i t i v e %
4874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4876 % TracePrimitive is a collection of methods for generating graphic
4877 % primitives such as arcs, ellipses, paths, etc.
4881 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4882 const PointInfo end,const PointInfo degrees)
4888 center.x=0.5*(end.x+start.x);
4889 center.y=0.5*(end.y+start.y);
4890 radii.x=fabs(center.x-start.x);
4891 radii.y=fabs(center.y-start.y);
4892 TraceEllipse(primitive_info,center,radii,degrees);
4895 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4896 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4897 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4912 register MagickRealType
4916 register PrimitiveInfo
4925 if ((start.x == end.x) && (start.y == end.y))
4927 TracePoint(primitive_info,end);
4930 radii.x=fabs(arc.x);
4931 radii.y=fabs(arc.y);
4932 if ((radii.x == 0.0) || (radii.y == 0.0))
4934 TraceLine(primitive_info,start,end);
4937 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4938 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4939 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4940 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4941 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4943 if (delta < MagickEpsilon)
4945 TraceLine(primitive_info,start,end);
4950 radii.x*=sqrt((double) delta);
4951 radii.y*=sqrt((double) delta);
4953 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4954 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4955 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4956 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4957 alpha=points[1].x-points[0].x;
4958 beta=points[1].y-points[0].y;
4959 factor=1.0/(alpha*alpha+beta*beta)-0.25;
4964 factor=sqrt((double) factor);
4965 if (sweep == large_arc)
4968 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4969 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4970 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4971 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4972 if ((theta < 0.0) && (sweep != MagickFalse))
4973 theta+=(MagickRealType) (2.0*MagickPI);
4975 if ((theta > 0.0) && (sweep == MagickFalse))
4976 theta-=(MagickRealType) (2.0*MagickPI);
4977 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
4980 for (i=0; i < (ssize_t) arc_segments; i++)
4982 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4983 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4984 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4985 sin(fmod((double) beta,DegreesToRadians(360.0)));
4986 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
4987 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
4988 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4989 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
4990 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
4991 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4992 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
4993 theta/arc_segments),DegreesToRadians(360.0))));
4994 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
4995 theta/arc_segments),DegreesToRadians(360.0))));
4996 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
4997 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4998 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
4999 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5000 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5001 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5002 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5004 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5006 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5008 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5010 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5012 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5014 if (i == (ssize_t) (arc_segments-1))
5019 primitive_info->coordinates=(size_t) (p-primitive_info);
5020 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5022 p->primitive=primitive_info->primitive;
5027 static void TraceBezier(PrimitiveInfo *primitive_info,
5028 const size_t number_coordinates)
5040 register PrimitiveInfo
5052 Allocate coeficients.
5054 quantum=number_coordinates;
5055 for (i=0; i < (ssize_t) number_coordinates; i++)
5057 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5059 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5060 if (alpha > (MagickRealType) quantum)
5061 quantum=(size_t) alpha;
5062 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5063 if (alpha > (MagickRealType) quantum)
5064 quantum=(size_t) alpha;
5067 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5068 (double) BezierQuantum);
5069 control_points=quantum*number_coordinates;
5070 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5071 number_coordinates,sizeof(*coefficients));
5072 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5074 if ((coefficients == (MagickRealType *) NULL) ||
5075 (points == (PointInfo *) NULL))
5076 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5078 Compute bezier points.
5080 end=primitive_info[number_coordinates-1].point;
5081 for (i=0; i < (ssize_t) number_coordinates; i++)
5082 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5084 for (i=0; i < (ssize_t) control_points; i++)
5089 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5090 for (j=0; j < (ssize_t) number_coordinates; j++)
5092 point.x+=alpha*coefficients[j]*p->point.x;
5093 point.y+=alpha*coefficients[j]*p->point.y;
5094 alpha*=weight/(1.0-weight);
5098 weight+=1.0/control_points;
5101 Bezier curves are just short segmented polys.
5104 for (i=0; i < (ssize_t) control_points; i++)
5106 TracePoint(p,points[i]);
5111 primitive_info->coordinates=(size_t) (p-primitive_info);
5112 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5114 p->primitive=primitive_info->primitive;
5117 points=(PointInfo *) RelinquishMagickMemory(points);
5118 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5121 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5122 const PointInfo end)
5133 alpha=end.x-start.x;
5135 radius=hypot((double) alpha,(double) beta);
5136 offset.x=(double) radius;
5137 offset.y=(double) radius;
5140 TraceEllipse(primitive_info,start,offset,degrees);
5143 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5144 const PointInfo stop,const PointInfo degrees)
5155 register PrimitiveInfo
5162 Ellipses are just short segmented polys.
5164 if ((stop.x == 0.0) && (stop.y == 0.0))
5166 TracePoint(primitive_info,start);
5169 delta=2.0/MagickMax(stop.x,stop.y);
5170 step=(MagickRealType) (MagickPI/8.0);
5171 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5172 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5173 angle.x=DegreesToRadians(degrees.x);
5175 while (y < degrees.x)
5177 angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5178 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5180 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5181 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5182 TracePoint(p,point);
5185 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5186 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5187 TracePoint(p,point);
5189 primitive_info->coordinates=(size_t) (p-primitive_info);
5190 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5192 p->primitive=primitive_info->primitive;
5197 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5198 const PointInfo end)
5200 TracePoint(primitive_info,start);
5201 if ((fabs(start.x-end.x) <= MagickEpsilon) &&
5202 (fabs(start.y-end.y) <= MagickEpsilon))
5204 primitive_info->primitive=PointPrimitive;
5205 primitive_info->coordinates=1;
5208 TracePoint(primitive_info+1,end);
5209 (primitive_info+1)->primitive=primitive_info->primitive;
5210 primitive_info->coordinates=2;
5213 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5216 token[MaxTextExtent];
5238 register PrimitiveInfo
5253 number_coordinates=0;
5255 primitive_type=primitive_info->primitive;
5257 for (p=path; *p != '\0'; )
5259 while (isspace((int) ((unsigned char) *p)) != 0)
5263 last_attribute=attribute;
5264 attribute=(int) (*p++);
5285 GetMagickToken(p,&p,token);
5287 GetMagickToken(p,&p,token);
5288 arc.x=InterpretLocaleValue(token,(char **) NULL);
5289 GetMagickToken(p,&p,token);
5291 GetMagickToken(p,&p,token);
5292 arc.y=InterpretLocaleValue(token,(char **) NULL);
5293 GetMagickToken(p,&p,token);
5295 GetMagickToken(p,&p,token);
5296 angle=InterpretLocaleValue(token,(char **) NULL);
5297 GetMagickToken(p,&p,token);
5299 GetMagickToken(p,&p,token);
5300 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5301 GetMagickToken(p,&p,token);
5303 GetMagickToken(p,&p,token);
5304 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5305 GetMagickToken(p,&p,token);
5307 GetMagickToken(p,&p,token);
5308 x=InterpretLocaleValue(token,(char **) NULL);
5309 GetMagickToken(p,&p,token);
5311 GetMagickToken(p,&p,token);
5312 y=InterpretLocaleValue(token,(char **) NULL);
5313 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5314 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5315 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5318 } while (IsPoint(p) != MagickFalse);
5325 Compute bezier points.
5330 for (i=1; i < 4; i++)
5332 GetMagickToken(p,&p,token);
5334 GetMagickToken(p,&p,token);
5335 x=InterpretLocaleValue(token,(char **) NULL);
5336 GetMagickToken(p,&p,token);
5338 GetMagickToken(p,&p,token);
5339 y=InterpretLocaleValue(token,(char **) NULL);
5340 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5341 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5344 for (i=0; i < 4; i++)
5345 (q+i)->point=points[i];
5349 } while (IsPoint(p) != MagickFalse);
5357 GetMagickToken(p,&p,token);
5359 GetMagickToken(p,&p,token);
5360 x=InterpretLocaleValue(token,(char **) NULL);
5361 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5362 TracePoint(q,point);
5364 } while (IsPoint(p) != MagickFalse);
5372 GetMagickToken(p,&p,token);
5374 GetMagickToken(p,&p,token);
5375 x=InterpretLocaleValue(token,(char **) NULL);
5376 GetMagickToken(p,&p,token);
5378 GetMagickToken(p,&p,token);
5379 y=InterpretLocaleValue(token,(char **) NULL);
5380 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5381 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5382 TracePoint(q,point);
5384 } while (IsPoint(p) != MagickFalse);
5390 if (q != primitive_info)
5392 primitive_info->coordinates=(size_t) (q-primitive_info);
5393 number_coordinates+=primitive_info->coordinates;
5399 GetMagickToken(p,&p,token);
5401 GetMagickToken(p,&p,token);
5402 x=InterpretLocaleValue(token,(char **) NULL);
5403 GetMagickToken(p,&p,token);
5405 GetMagickToken(p,&p,token);
5406 y=InterpretLocaleValue(token,(char **) NULL);
5407 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5408 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5412 TracePoint(q,point);
5414 if ((i != 0) && (attribute == (int) 'M'))
5416 TracePoint(q,point);
5419 } while (IsPoint(p) != MagickFalse);
5426 Compute bezier points.
5431 for (i=1; i < 3; i++)
5433 GetMagickToken(p,&p,token);
5435 GetMagickToken(p,&p,token);
5436 x=InterpretLocaleValue(token,(char **) NULL);
5437 GetMagickToken(p,&p,token);
5439 GetMagickToken(p,&p,token);
5440 y=InterpretLocaleValue(token,(char **) NULL);
5443 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5444 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5447 for (i=0; i < 3; i++)
5448 (q+i)->point=points[i];
5452 } while (IsPoint(p) != MagickFalse);
5459 Compute bezier points.
5463 points[0]=points[3];
5464 points[1].x=2.0*points[3].x-points[2].x;
5465 points[1].y=2.0*points[3].y-points[2].y;
5466 for (i=2; i < 4; i++)
5468 GetMagickToken(p,&p,token);
5470 GetMagickToken(p,&p,token);
5471 x=InterpretLocaleValue(token,(char **) NULL);
5472 GetMagickToken(p,&p,token);
5474 GetMagickToken(p,&p,token);
5475 y=InterpretLocaleValue(token,(char **) NULL);
5478 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5479 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5482 if (strchr("CcSs",last_attribute) == (char *) NULL)
5484 points[0]=points[2];
5485 points[1]=points[3];
5487 for (i=0; i < 4; i++)
5488 (q+i)->point=points[i];
5492 } while (IsPoint(p) != MagickFalse);
5499 Compute bezier points.
5503 points[0]=points[2];
5504 points[1].x=2.0*points[2].x-points[1].x;
5505 points[1].y=2.0*points[2].y-points[1].y;
5506 for (i=2; i < 3; i++)
5508 GetMagickToken(p,&p,token);
5510 GetMagickToken(p,&p,token);
5511 x=InterpretLocaleValue(token,(char **) NULL);
5512 GetMagickToken(p,&p,token);
5514 GetMagickToken(p,&p,token);
5515 y=InterpretLocaleValue(token,(char **) NULL);
5516 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5517 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5520 if (strchr("QqTt",last_attribute) == (char *) NULL)
5522 points[0]=points[2];
5523 points[1]=points[3];
5525 for (i=0; i < 3; i++)
5526 (q+i)->point=points[i];
5530 } while (IsPoint(p) != MagickFalse);
5538 GetMagickToken(p,&p,token);
5540 GetMagickToken(p,&p,token);
5541 y=InterpretLocaleValue(token,(char **) NULL);
5542 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5543 TracePoint(q,point);
5545 } while (IsPoint(p) != MagickFalse);
5552 TracePoint(q,point);
5554 primitive_info->coordinates=(size_t) (q-primitive_info);
5555 number_coordinates+=primitive_info->coordinates;
5562 if (isalpha((int) ((unsigned char) attribute)) != 0)
5563 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5569 primitive_info->coordinates=(size_t) (q-primitive_info);
5570 number_coordinates+=primitive_info->coordinates;
5571 for (i=0; i < (ssize_t) number_coordinates; i++)
5574 q->primitive=primitive_type;
5576 q->method=FillToBorderMethod;
5579 return(number_coordinates);
5582 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5583 const PointInfo end)
5588 register PrimitiveInfo
5595 TracePoint(p,start);
5599 TracePoint(p,point);
5605 TracePoint(p,point);
5607 TracePoint(p,start);
5609 primitive_info->coordinates=(size_t) (p-primitive_info);
5610 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5612 p->primitive=primitive_info->primitive;
5617 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5618 const PointInfo start,const PointInfo end,PointInfo arc)
5625 register PrimitiveInfo
5632 offset.x=fabs(end.x-start.x);
5633 offset.y=fabs(end.y-start.y);
5634 if (arc.x > (0.5*offset.x))
5636 if (arc.y > (0.5*offset.y))
5638 point.x=start.x+offset.x-arc.x;
5639 point.y=start.y+arc.y;
5642 TraceEllipse(p,point,arc,degrees);
5644 point.x=start.x+offset.x-arc.x;
5645 point.y=start.y+offset.y-arc.y;
5648 TraceEllipse(p,point,arc,degrees);
5650 point.x=start.x+arc.x;
5651 point.y=start.y+offset.y-arc.y;
5654 TraceEllipse(p,point,arc,degrees);
5656 point.x=start.x+arc.x;
5657 point.y=start.y+arc.y;
5660 TraceEllipse(p,point,arc,degrees);
5662 TracePoint(p,primitive_info->point);
5664 primitive_info->coordinates=(size_t) (p-primitive_info);
5665 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5667 p->primitive=primitive_info->primitive;
5672 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5673 const size_t number_vertices,const MagickRealType offset)
5678 register MagickRealType
5690 for (i=1; i < (ssize_t) number_vertices; i++)
5692 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5693 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5694 if ((fabs((double) dx) >= MagickEpsilon) ||
5695 (fabs((double) dy) >= MagickEpsilon))
5698 if (i == (ssize_t) number_vertices)
5699 i=(ssize_t) number_vertices-1L;
5700 distance=hypot((double) dx,(double) dy);
5701 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5702 dx*(distance+offset)/distance);
5703 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5704 dy*(distance+offset)/distance);
5705 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5707 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5708 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5709 if ((fabs((double) dx) >= MagickEpsilon) ||
5710 (fabs((double) dy) >= MagickEpsilon))
5713 distance=hypot((double) dx,(double) dy);
5714 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5715 dx*(distance+offset)/distance);
5716 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5717 dy*(distance+offset)/distance);
5720 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5721 const PrimitiveInfo *primitive_info)
5723 typedef struct _LineSegment
5775 number_vertices=primitive_info->coordinates;
5776 max_strokes=2*number_vertices+6*BezierQuantum+360;
5777 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5779 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5781 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5782 number_vertices+2UL,sizeof(*polygon_primitive));
5783 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5784 (polygon_primitive == (PrimitiveInfo *) NULL))
5785 return((PrimitiveInfo *) NULL);
5786 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5787 number_vertices*sizeof(*polygon_primitive));
5789 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5790 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5791 MagickTrue : MagickFalse;
5792 if ((draw_info->linejoin == RoundJoin) ||
5793 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5795 polygon_primitive[number_vertices]=primitive_info[1];
5798 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5800 Compute the slope for the first line segment, p.
5804 for (n=1; n < (ssize_t) number_vertices; n++)
5806 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5807 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5808 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5811 if (n == (ssize_t) number_vertices)
5812 n=(ssize_t) number_vertices-1L;
5814 inverse_slope.p=0.0;
5815 if (fabs(dx.p) <= MagickEpsilon)
5818 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5820 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5823 if (fabs(dy.p) <= MagickEpsilon)
5826 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5828 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5833 inverse_slope.p=(-1.0/slope.p);
5835 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5836 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5838 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5839 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5840 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5841 offset.y=(double) (offset.x*inverse_slope.p);
5842 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5844 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5845 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5846 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5847 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5848 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5849 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5850 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5851 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5855 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5856 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5857 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5858 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5859 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5860 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5861 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5862 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5865 Create strokes for the line join attribute: bevel, miter, round.
5869 path_q[p++]=box_q[0];
5870 path_p[q++]=box_p[0];
5871 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5874 Compute the slope for this line segment, q.
5876 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5877 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5878 dot_product=dx.q*dx.q+dy.q*dy.q;
5879 if (dot_product < 0.25)
5882 inverse_slope.q=0.0;
5883 if (fabs(dx.q) < MagickEpsilon)
5886 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5888 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5891 if (fabs(dy.q) <= MagickEpsilon)
5894 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5896 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5901 inverse_slope.q=(-1.0/slope.q);
5903 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5904 offset.y=(double) (offset.x*inverse_slope.q);
5905 dot_product=dy.q*offset.x-dx.q*offset.y;
5906 if (dot_product > 0.0)
5908 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5909 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5910 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5911 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5912 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5913 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5914 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5915 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5919 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5920 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5921 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5922 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5923 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5924 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5925 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5926 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5928 if (fabs((double) (slope.p-slope.q)) <= MagickEpsilon)
5935 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5936 box_p[3].y)/(slope.p-slope.q));
5937 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5938 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5939 box_q[3].y)/(slope.p-slope.q));
5940 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5942 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5944 max_strokes+=6*BezierQuantum+360;
5945 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5947 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5949 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5951 polygon_primitive=(PrimitiveInfo *)
5952 RelinquishMagickMemory(polygon_primitive);
5953 return((PrimitiveInfo *) NULL);
5956 dot_product=dx.q*dy.p-dx.p*dy.q;
5957 if (dot_product <= 0.0)
5958 switch (draw_info->linejoin)
5962 path_q[q++]=box_q[1];
5963 path_q[q++]=box_q[2];
5964 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5965 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5966 if (dot_product <= miterlimit)
5967 path_p[p++]=box_p[4];
5970 path_p[p++]=box_p[1];
5971 path_p[p++]=box_p[2];
5977 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5978 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5979 if (dot_product <= miterlimit)
5981 path_q[q++]=box_q[4];
5982 path_p[p++]=box_p[4];
5986 path_q[q++]=box_q[1];
5987 path_q[q++]=box_q[2];
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)
5998 path_p[p++]=box_p[4];
6001 path_p[p++]=box_p[1];
6002 path_p[p++]=box_p[2];
6004 center=polygon_primitive[n].point;
6005 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6006 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6007 if (theta.q < theta.p)
6008 theta.q+=(MagickRealType) (2.0*MagickPI);
6009 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6010 (2.0*sqrt((double) (1.0/mid)))));
6011 path_q[q].x=box_q[1].x;
6012 path_q[q].y=box_q[1].y;
6014 for (j=1; j < (ssize_t) arc_segments; j++)
6016 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6017 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6018 (theta.p+delta_theta),DegreesToRadians(360.0))));
6019 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6020 (theta.p+delta_theta),DegreesToRadians(360.0))));
6023 path_q[q++]=box_q[2];
6030 switch (draw_info->linejoin)
6034 path_p[p++]=box_p[1];
6035 path_p[p++]=box_p[2];
6036 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6037 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6038 if (dot_product <= miterlimit)
6039 path_q[q++]=box_q[4];
6042 path_q[q++]=box_q[1];
6043 path_q[q++]=box_q[2];
6049 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6050 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6051 if (dot_product <= miterlimit)
6053 path_q[q++]=box_q[4];
6054 path_p[p++]=box_p[4];
6058 path_q[q++]=box_q[1];
6059 path_q[q++]=box_q[2];
6060 path_p[p++]=box_p[1];
6061 path_p[p++]=box_p[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)
6070 path_q[q++]=box_q[4];
6073 path_q[q++]=box_q[1];
6074 path_q[q++]=box_q[2];
6076 center=polygon_primitive[n].point;
6077 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6078 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6079 if (theta.p < theta.q)
6080 theta.p+=(MagickRealType) (2.0*MagickPI);
6081 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6082 (2.0*sqrt((double) (1.0/mid)))));
6083 path_p[p++]=box_p[1];
6084 for (j=1; j < (ssize_t) arc_segments; j++)
6086 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6087 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6088 (theta.p+delta_theta),DegreesToRadians(360.0))));
6089 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6090 (theta.p+delta_theta),DegreesToRadians(360.0))));
6093 path_p[p++]=box_p[2];
6100 inverse_slope.p=inverse_slope.q;
6109 path_p[p++]=box_p[1];
6110 path_q[q++]=box_q[1];
6112 Trace stroked polygon.
6114 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6115 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6116 if (stroke_polygon != (PrimitiveInfo *) NULL)
6118 for (i=0; i < (ssize_t) p; i++)
6120 stroke_polygon[i]=polygon_primitive[0];
6121 stroke_polygon[i].point=path_p[i];
6123 if (closed_path != MagickFalse)
6125 stroke_polygon[i]=polygon_primitive[0];
6126 stroke_polygon[i].point=stroke_polygon[0].point;
6129 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6131 stroke_polygon[i]=polygon_primitive[0];
6132 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6134 if (closed_path != MagickFalse)
6136 stroke_polygon[i]=polygon_primitive[0];
6137 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6140 stroke_polygon[i]=polygon_primitive[0];
6141 stroke_polygon[i].point=stroke_polygon[0].point;
6143 stroke_polygon[i].primitive=UndefinedPrimitive;
6144 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6146 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6147 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6148 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6149 return(stroke_polygon);