2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2012 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 *,
160 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
163 TracePath(PrimitiveInfo *,const char *);
166 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
167 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
168 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
169 TraceBezier(PrimitiveInfo *,const size_t),
170 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
171 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
172 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
173 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
174 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
176 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % A c q u i r e D r a w I n f o %
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
191 % The format of the AcquireDrawInfo method is:
193 % DrawInfo *AcquireDrawInfo(void)
196 MagickExport DrawInfo *AcquireDrawInfo(void)
201 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
202 if (draw_info == (DrawInfo *) NULL)
203 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
204 GetDrawInfo((ImageInfo *) NULL,draw_info);
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 % C l o n e D r a w I n f o %
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
220 % is specified, a new draw_info structure is created initialized to
221 % default values, according to the given image_info.
223 % The format of the CloneDrawInfo method is:
225 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
226 % const DrawInfo *draw_info)
228 % A description of each parameter follows:
230 % o image_info: the image info.
232 % o draw_info: the draw info.
235 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
236 const DrawInfo *draw_info)
244 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
245 if (clone_info == (DrawInfo *) NULL)
246 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
247 GetDrawInfo(image_info,clone_info);
248 if (draw_info == (DrawInfo *) NULL)
250 exception=AcquireExceptionInfo();
251 (void) CloneString(&clone_info->primitive,draw_info->primitive);
252 (void) CloneString(&clone_info->geometry,draw_info->geometry);
253 clone_info->viewbox=draw_info->viewbox;
254 clone_info->affine=draw_info->affine;
255 clone_info->gravity=draw_info->gravity;
256 clone_info->fill=draw_info->fill;
257 clone_info->stroke=draw_info->stroke;
258 clone_info->stroke_width=draw_info->stroke_width;
259 if (draw_info->fill_pattern != (Image *) NULL)
260 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
262 if (draw_info->stroke_pattern != (Image *) NULL)
263 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
264 MagickTrue,exception);
265 clone_info->stroke_antialias=draw_info->stroke_antialias;
266 clone_info->text_antialias=draw_info->text_antialias;
267 clone_info->fill_rule=draw_info->fill_rule;
268 clone_info->linecap=draw_info->linecap;
269 clone_info->linejoin=draw_info->linejoin;
270 clone_info->miterlimit=draw_info->miterlimit;
271 clone_info->dash_offset=draw_info->dash_offset;
272 clone_info->decorate=draw_info->decorate;
273 clone_info->compose=draw_info->compose;
274 (void) CloneString(&clone_info->text,draw_info->text);
275 (void) CloneString(&clone_info->font,draw_info->font);
276 (void) CloneString(&clone_info->metrics,draw_info->metrics);
277 (void) CloneString(&clone_info->family,draw_info->family);
278 clone_info->style=draw_info->style;
279 clone_info->stretch=draw_info->stretch;
280 clone_info->weight=draw_info->weight;
281 (void) CloneString(&clone_info->encoding,draw_info->encoding);
282 clone_info->pointsize=draw_info->pointsize;
283 clone_info->kerning=draw_info->kerning;
284 clone_info->interline_spacing=draw_info->interline_spacing;
285 clone_info->interword_spacing=draw_info->interword_spacing;
286 clone_info->direction=draw_info->direction;
287 (void) CloneString(&clone_info->density,draw_info->density);
288 clone_info->align=draw_info->align;
289 clone_info->undercolor=draw_info->undercolor;
290 clone_info->border_color=draw_info->border_color;
291 (void) CloneString(&clone_info->server_name,draw_info->server_name);
292 if (draw_info->dash_pattern != (double *) NULL)
297 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
298 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
299 sizeof(*clone_info->dash_pattern));
300 if (clone_info->dash_pattern == (double *) NULL)
301 ThrowFatalException(ResourceLimitFatalError,
302 "UnableToAllocateDashPattern");
303 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
304 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
306 clone_info->gradient=draw_info->gradient;
307 if (draw_info->gradient.stops != (StopInfo *) NULL)
312 number_stops=clone_info->gradient.number_stops;
313 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
314 number_stops,sizeof(*clone_info->gradient.stops));
315 if (clone_info->gradient.stops == (StopInfo *) NULL)
316 ThrowFatalException(ResourceLimitFatalError,
317 "UnableToAllocateDashPattern");
318 (void) CopyMagickMemory(clone_info->gradient.stops,
319 draw_info->gradient.stops,(size_t) number_stops*
320 sizeof(*clone_info->gradient.stops));
322 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
323 clone_info->bounds=draw_info->bounds;
324 clone_info->clip_units=draw_info->clip_units;
325 clone_info->render=draw_info->render;
326 clone_info->alpha=draw_info->alpha;
327 clone_info->element_reference=draw_info->element_reference;
328 clone_info->debug=IsEventLogging();
329 exception=DestroyExceptionInfo(exception);
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 + C o n v e r t P a t h T o P o l y g o n %
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % ConvertPathToPolygon() converts a path to the more efficient sorted
347 % The format of the ConvertPathToPolygon method is:
349 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
350 % const PathInfo *path_info)
352 % A description of each parameter follows:
354 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
355 % rendering form of type PolygonInfo.
357 % o draw_info: Specifies a pointer to an DrawInfo structure.
359 % o path_info: Specifies a pointer to an PathInfo structure.
364 #if defined(__cplusplus) || defined(c_plusplus)
368 static int CompareEdges(const void *x,const void *y)
370 register const EdgeInfo
377 p=(const EdgeInfo *) x;
378 q=(const EdgeInfo *) y;
379 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
381 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
383 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
385 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
387 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
388 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
393 #if defined(__cplusplus) || defined(c_plusplus)
397 static void LogPolygonInfo(const PolygonInfo *polygon_info)
406 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
407 p=polygon_info->edges;
408 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
410 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
412 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
413 p->direction != MagickFalse ? "down" : "up");
414 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
415 p->ghostline != MagickFalse ? "transparent" : "opaque");
416 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
417 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
418 p->bounds.x2,p->bounds.y2);
419 for (j=0; j < (ssize_t) p->number_points; j++)
420 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
421 p->points[j].x,p->points[j].y);
424 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
427 static void ReversePoints(PointInfo *points,const size_t number_points)
435 for (i=0; i < (ssize_t) (number_points >> 1); i++)
438 points[i]=points[number_points-(i+1)];
439 points[number_points-(i+1)]=point;
443 static PolygonInfo *ConvertPathToPolygon(
444 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
473 Convert a path to the more efficient sorted rendering form.
475 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
476 if (polygon_info == (PolygonInfo *) NULL)
477 return((PolygonInfo *) NULL);
479 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
480 sizeof(*polygon_info->edges));
481 if (polygon_info->edges == (EdgeInfo *) NULL)
482 return((PolygonInfo *) NULL);
485 ghostline=MagickFalse;
488 points=(PointInfo *) NULL;
489 (void) ResetMagickMemory(&point,0,sizeof(point));
490 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
491 for (i=0; path_info[i].code != EndCode; i++)
493 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
494 (path_info[i].code == GhostlineCode))
499 if ((points != (PointInfo *) NULL) && (n >= 2))
501 if (edge == number_edges)
504 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
505 polygon_info->edges,(size_t) number_edges,
506 sizeof(*polygon_info->edges));
507 if (polygon_info->edges == (EdgeInfo *) NULL)
508 return((PolygonInfo *) NULL);
510 polygon_info->edges[edge].number_points=(size_t) n;
511 polygon_info->edges[edge].scanline=(-1.0);
512 polygon_info->edges[edge].highwater=0;
513 polygon_info->edges[edge].ghostline=ghostline;
514 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
516 ReversePoints(points,(size_t) n);
517 polygon_info->edges[edge].points=points;
518 polygon_info->edges[edge].bounds=bounds;
519 polygon_info->edges[edge].bounds.y1=points[0].y;
520 polygon_info->edges[edge].bounds.y2=points[n-1].y;
521 points=(PointInfo *) NULL;
522 ghostline=MagickFalse;
525 if (points == (PointInfo *) NULL)
528 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
530 if (points == (PointInfo *) NULL)
531 return((PolygonInfo *) NULL);
533 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
534 point=path_info[i].point;
545 next_direction=((path_info[i].point.y > point.y) ||
546 ((path_info[i].point.y == point.y) &&
547 (path_info[i].point.x > point.x))) ? 1 : -1;
548 if ((direction != 0) && (direction != next_direction))
554 if (edge == number_edges)
557 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
558 polygon_info->edges,(size_t) number_edges,
559 sizeof(*polygon_info->edges));
560 if (polygon_info->edges == (EdgeInfo *) NULL)
561 return((PolygonInfo *) NULL);
563 polygon_info->edges[edge].number_points=(size_t) n;
564 polygon_info->edges[edge].scanline=(-1.0);
565 polygon_info->edges[edge].highwater=0;
566 polygon_info->edges[edge].ghostline=ghostline;
567 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
569 ReversePoints(points,(size_t) n);
570 polygon_info->edges[edge].points=points;
571 polygon_info->edges[edge].bounds=bounds;
572 polygon_info->edges[edge].bounds.y1=points[0].y;
573 polygon_info->edges[edge].bounds.y2=points[n-1].y;
575 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
577 if (points == (PointInfo *) NULL)
578 return((PolygonInfo *) NULL);
580 ghostline=MagickFalse;
586 direction=next_direction;
587 if (points == (PointInfo *) NULL)
589 if (n == (ssize_t) number_points)
592 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
594 if (points == (PointInfo *) NULL)
595 return((PolygonInfo *) NULL);
597 point=path_info[i].point;
599 if (point.x < bounds.x1)
601 if (point.x > bounds.x2)
605 if (points != (PointInfo *) NULL)
608 points=(PointInfo *) RelinquishMagickMemory(points);
611 if (edge == number_edges)
614 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
615 polygon_info->edges,(size_t) number_edges,
616 sizeof(*polygon_info->edges));
617 if (polygon_info->edges == (EdgeInfo *) NULL)
618 return((PolygonInfo *) NULL);
620 polygon_info->edges[edge].number_points=(size_t) n;
621 polygon_info->edges[edge].scanline=(-1.0);
622 polygon_info->edges[edge].highwater=0;
623 polygon_info->edges[edge].ghostline=ghostline;
624 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
626 ReversePoints(points,(size_t) n);
627 polygon_info->edges[edge].points=points;
628 polygon_info->edges[edge].bounds=bounds;
629 polygon_info->edges[edge].bounds.y1=points[0].y;
630 polygon_info->edges[edge].bounds.y2=points[n-1].y;
631 ghostline=MagickFalse;
635 polygon_info->number_edges=edge;
636 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
637 sizeof(*polygon_info->edges),CompareEdges);
638 if (IsEventLogging() != MagickFalse)
639 LogPolygonInfo(polygon_info);
640 return(polygon_info);
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 + C o n v e r t P r i m i t i v e T o P a t h %
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
657 % The format of the ConvertPrimitiveToPath method is:
659 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
660 % const PrimitiveInfo *primitive_info)
662 % A description of each parameter follows:
664 % o Method ConvertPrimitiveToPath returns a vector path structure of type
667 % o draw_info: a structure of type DrawInfo.
669 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
674 static void LogPathInfo(const PathInfo *path_info)
676 register const PathInfo
679 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
680 for (p=path_info; p->code != EndCode; p++)
681 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
682 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
683 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
684 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
686 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
689 static PathInfo *ConvertPrimitiveToPath(
690 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
711 Converts a PrimitiveInfo structure into a vector path structure.
713 switch (primitive_info->primitive)
720 return((PathInfo *) NULL);
724 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
725 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
727 if (path_info == (PathInfo *) NULL)
728 return((PathInfo *) NULL);
736 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
739 if (coordinates <= 0)
741 coordinates=(ssize_t) primitive_info[i].coordinates;
742 p=primitive_info[i].point;
748 Eliminate duplicate points.
750 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) > MagickEpsilon) ||
751 (fabs(q.y-primitive_info[i].point.y) > MagickEpsilon))
753 path_info[n].code=code;
754 path_info[n].point=primitive_info[i].point;
755 q=primitive_info[i].point;
760 if ((fabs(p.x-primitive_info[i].point.x) <= MagickEpsilon) &&
761 (fabs(p.y-primitive_info[i].point.y) <= MagickEpsilon))
764 Mark the p point as open if it does not match the q.
766 path_info[start].code=OpenCode;
767 path_info[n].code=GhostlineCode;
768 path_info[n].point=primitive_info[i].point;
770 path_info[n].code=LineToCode;
771 path_info[n].point=p;
774 path_info[n].code=EndCode;
775 path_info[n].point.x=0.0;
776 path_info[n].point.y=0.0;
777 if (IsEventLogging() != MagickFalse)
778 LogPathInfo(path_info);
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787 % D e s t r o y D r a w I n f o %
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
796 % The format of the DestroyDrawInfo method is:
798 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
800 % A description of each parameter follows:
802 % o draw_info: the draw info.
805 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
807 if (draw_info->debug != MagickFalse)
808 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
809 assert(draw_info != (DrawInfo *) NULL);
810 assert(draw_info->signature == MagickSignature);
811 if (draw_info->primitive != (char *) NULL)
812 draw_info->primitive=DestroyString(draw_info->primitive);
813 if (draw_info->text != (char *) NULL)
814 draw_info->text=DestroyString(draw_info->text);
815 if (draw_info->geometry != (char *) NULL)
816 draw_info->geometry=DestroyString(draw_info->geometry);
817 if (draw_info->fill_pattern != (Image *) NULL)
818 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
819 if (draw_info->stroke_pattern != (Image *) NULL)
820 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
821 if (draw_info->font != (char *) NULL)
822 draw_info->font=DestroyString(draw_info->font);
823 if (draw_info->metrics != (char *) NULL)
824 draw_info->metrics=DestroyString(draw_info->metrics);
825 if (draw_info->family != (char *) NULL)
826 draw_info->family=DestroyString(draw_info->family);
827 if (draw_info->encoding != (char *) NULL)
828 draw_info->encoding=DestroyString(draw_info->encoding);
829 if (draw_info->density != (char *) NULL)
830 draw_info->density=DestroyString(draw_info->density);
831 if (draw_info->server_name != (char *) NULL)
832 draw_info->server_name=(char *)
833 RelinquishMagickMemory(draw_info->server_name);
834 if (draw_info->dash_pattern != (double *) NULL)
835 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
836 draw_info->dash_pattern);
837 if (draw_info->gradient.stops != (StopInfo *) NULL)
838 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
839 draw_info->gradient.stops);
840 if (draw_info->clip_mask != (char *) NULL)
841 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
842 draw_info->signature=(~MagickSignature);
843 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 + D e s t r o y E d g e %
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % DestroyEdge() destroys the specified polygon edge.
860 % The format of the DestroyEdge method is:
862 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
864 % A description of each parameter follows:
866 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
868 % o edge: the polygon edge number to destroy.
871 static size_t DestroyEdge(PolygonInfo *polygon_info,
874 assert(edge < polygon_info->number_edges);
875 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
876 polygon_info->edges[edge].points);
877 polygon_info->number_edges--;
878 if (edge < polygon_info->number_edges)
879 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
880 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
881 return(polygon_info->number_edges);
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889 + D e s t r o y P o l y g o n I n f o %
893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
897 % The format of the DestroyPolygonInfo method is:
899 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
901 % A description of each parameter follows:
903 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
906 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
911 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
912 polygon_info->edges[i].points=(PointInfo *)
913 RelinquishMagickMemory(polygon_info->edges[i].points);
914 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
915 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
919 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923 % D r a w A f f i n e I m a g e %
927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929 % DrawAffineImage() composites the source over the destination image as
930 % dictated by the affine transform.
932 % The format of the DrawAffineImage method is:
934 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
935 % const AffineMatrix *affine,ExceptionInfo *exception)
937 % A description of each parameter follows:
939 % o image: the image.
941 % o source: the source image.
943 % o affine: the affine transform.
945 % o exception: return any errors or warnings in this structure.
949 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
950 const double y,const SegmentInfo *edge)
963 Determine left and right edges.
965 inverse_edge.x1=edge->x1;
966 inverse_edge.y1=edge->y1;
967 inverse_edge.x2=edge->x2;
968 inverse_edge.y2=edge->y2;
969 z=affine->ry*y+affine->tx;
970 if (affine->sx > MagickEpsilon)
972 intercept=(-z/affine->sx);
974 if (x > inverse_edge.x1)
976 intercept=(-z+(double) image->columns)/affine->sx;
978 if (x < inverse_edge.x2)
982 if (affine->sx < -MagickEpsilon)
984 intercept=(-z+(double) image->columns)/affine->sx;
986 if (x > inverse_edge.x1)
988 intercept=(-z/affine->sx);
990 if (x < inverse_edge.x2)
994 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
996 inverse_edge.x2=edge->x1;
997 return(inverse_edge);
1000 Determine top and bottom edges.
1002 z=affine->sy*y+affine->ty;
1003 if (affine->rx > MagickEpsilon)
1005 intercept=(-z/affine->rx);
1007 if (x > inverse_edge.x1)
1009 intercept=(-z+(double) image->rows)/affine->rx;
1011 if (x < inverse_edge.x2)
1015 if (affine->rx < -MagickEpsilon)
1017 intercept=(-z+(double) image->rows)/affine->rx;
1019 if (x > inverse_edge.x1)
1021 intercept=(-z/affine->rx);
1023 if (x < inverse_edge.x2)
1027 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1029 inverse_edge.x2=edge->x2;
1030 return(inverse_edge);
1032 return(inverse_edge);
1035 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1043 determinant=1.0/(affine->sx*affine->sy-affine->rx*affine->ry);
1044 inverse_affine.sx=determinant*affine->sy;
1045 inverse_affine.rx=determinant*(-affine->rx);
1046 inverse_affine.ry=determinant*(-affine->ry);
1047 inverse_affine.sy=determinant*affine->sx;
1048 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1050 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1052 return(inverse_affine);
1055 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1062 static inline double MagickMax(const double x,const double y)
1069 static inline double MagickMin(const double x,const double y)
1076 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1077 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1108 Determine bounding box.
1110 assert(image != (Image *) NULL);
1111 assert(image->signature == MagickSignature);
1112 if (image->debug != MagickFalse)
1113 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1114 assert(source != (const Image *) NULL);
1115 assert(source->signature == MagickSignature);
1116 assert(affine != (AffineMatrix *) NULL);
1119 extent[1].x=(double) source->columns-1.0;
1121 extent[2].x=(double) source->columns-1.0;
1122 extent[2].y=(double) source->rows-1.0;
1124 extent[3].y=(double) source->rows-1.0;
1125 for (i=0; i < 4; i++)
1128 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1129 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1133 for (i=1; i < 4; i++)
1135 if (min.x > extent[i].x)
1137 if (min.y > extent[i].y)
1139 if (max.x < extent[i].x)
1141 if (max.y < extent[i].y)
1145 Affine transform image.
1147 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1148 return(MagickFalse);
1150 edge.x1=MagickMax(min.x,0.0);
1151 edge.y1=MagickMax(min.y,0.0);
1152 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1153 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1154 inverse_affine=InverseAffineMatrix(affine);
1155 GetPixelInfo(image,&zero);
1156 image_view=AcquireCacheView(image);
1157 source_view=AcquireCacheView(source);
1158 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1159 #pragma omp parallel for schedule(dynamic,4) shared(status)
1161 for (y=(ssize_t) ceil(edge.y1-0.5); y <= (ssize_t) floor(edge.y2+0.5); y++)
1182 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1183 if (inverse_edge.x2 < inverse_edge.x1)
1185 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1186 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
1187 inverse_edge.x1+0.5)+1),1,exception);
1188 if (q == (Quantum *) NULL)
1193 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1195 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1197 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1199 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1200 point.x,point.y,&pixel,exception);
1201 GetPixelInfoPixel(image,q,&composite);
1202 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1204 SetPixelInfoPixel(image,&composite,q);
1206 q+=GetPixelChannels(image);
1208 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1211 source_view=DestroyCacheView(source_view);
1212 image_view=DestroyCacheView(image_view);
1217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1221 + D r a w B o u n d i n g R e c t a n g l e s %
1225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1228 % is only useful for developers debugging the rendering algorithm.
1230 % The format of the DrawBoundingRectangles method is:
1232 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1233 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1235 % A description of each parameter follows:
1237 % o image: the image.
1239 % o draw_info: the draw info.
1241 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1243 % o exception: return any errors or warnings in this structure.
1246 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1247 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1272 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1273 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1275 resolution.x=DefaultResolution;
1276 resolution.y=DefaultResolution;
1277 if (clone_info->density != (char *) NULL)
1285 flags=ParseGeometry(clone_info->density,&geometry_info);
1286 resolution.x=geometry_info.rho;
1287 resolution.y=geometry_info.sigma;
1288 if ((flags & SigmaValue) == MagickFalse)
1289 resolution.y=resolution.x;
1291 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1292 clone_info->stroke_width/2.0;
1297 if (polygon_info != (PolygonInfo *) NULL)
1299 bounds=polygon_info->edges[0].bounds;
1300 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1302 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1303 bounds.x1=polygon_info->edges[i].bounds.x1;
1304 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1305 bounds.y1=polygon_info->edges[i].bounds.y1;
1306 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1307 bounds.x2=polygon_info->edges[i].bounds.x2;
1308 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1309 bounds.y2=polygon_info->edges[i].bounds.y2;
1312 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1313 image->columns ? (double) image->columns-1 : bounds.x1;
1315 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1316 image->rows ? (double) image->rows-1 : bounds.y1;
1318 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1319 image->columns ? (double) image->columns-1 : bounds.x2;
1321 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1322 image->rows ? (double) image->rows-1 : bounds.y2;
1323 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1325 if (polygon_info->edges[i].direction != 0)
1326 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1329 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1331 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1332 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1333 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1334 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1335 primitive_info[0].primitive=RectanglePrimitive;
1336 TraceRectangle(primitive_info,start,end);
1337 primitive_info[0].method=ReplaceMethod;
1338 coordinates=(ssize_t) primitive_info[0].coordinates;
1339 primitive_info[coordinates].primitive=UndefinedPrimitive;
1340 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1343 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1345 start.x=(double) (bounds.x1-mid);
1346 start.y=(double) (bounds.y1-mid);
1347 end.x=(double) (bounds.x2+mid);
1348 end.y=(double) (bounds.y2+mid);
1349 primitive_info[0].primitive=RectanglePrimitive;
1350 TraceRectangle(primitive_info,start,end);
1351 primitive_info[0].method=ReplaceMethod;
1352 coordinates=(ssize_t) primitive_info[0].coordinates;
1353 primitive_info[coordinates].primitive=UndefinedPrimitive;
1354 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1355 clone_info=DestroyDrawInfo(clone_info);
1359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363 % D r a w C l i p P a t h %
1367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 % DrawClipPath() draws the clip path on the image mask.
1371 % The format of the DrawClipPath method is:
1373 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1374 % const char *name,ExceptionInfo *exception)
1376 % A description of each parameter follows:
1378 % o image: the image.
1380 % o draw_info: the draw info.
1382 % o name: the name of the clip path.
1384 % o exception: return any errors or warnings in this structure.
1387 MagickExport MagickBooleanType DrawClipPath(Image *image,
1388 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1391 clip_mask[MaxTextExtent];
1402 assert(image != (Image *) NULL);
1403 assert(image->signature == MagickSignature);
1404 if (image->debug != MagickFalse)
1405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1406 assert(draw_info != (const DrawInfo *) NULL);
1407 (void) FormatLocaleString(clip_mask,MaxTextExtent,"%s",name);
1408 value=GetImageArtifact(image,clip_mask);
1409 if (value == (const char *) NULL)
1410 return(MagickFalse);
1411 if (image->clip_mask == (Image *) NULL)
1416 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,
1418 if (clip_mask == (Image *) NULL)
1419 return(MagickFalse);
1420 (void) SetImageClipMask(image,clip_mask,exception);
1421 clip_mask=DestroyImage(clip_mask);
1423 (void) QueryColorCompliance("#00000000",AllCompliance,
1424 &image->clip_mask->background_color,exception);
1425 image->clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1426 (void) SetImageBackgroundColor(image->clip_mask,exception);
1427 if (image->debug != MagickFalse)
1428 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1429 draw_info->clip_mask);
1430 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1431 (void) CloneString(&clone_info->primitive,value);
1432 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1434 clone_info->clip_mask=(char *) NULL;
1435 status=DrawImage(image->clip_mask,clone_info,exception);
1436 status|=NegateImage(image->clip_mask,MagickFalse,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,
1461 % ExceptionInfo *exception)
1463 % A description of each parameter follows:
1465 % o draw_info: the draw info.
1467 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1469 % o image: the image.
1471 % o exception: return any errors or warnings in this structure.
1474 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1475 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1496 register MagickRealType
1507 assert(draw_info != (const DrawInfo *) NULL);
1508 if (image->debug != MagickFalse)
1509 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1510 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1511 clone_info->miterlimit=0;
1512 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1513 number_vertices=(size_t) i;
1514 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1515 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1516 if (dash_polygon == (PrimitiveInfo *) NULL)
1517 return(MagickFalse);
1518 dash_polygon[0]=primitive_info[0];
1519 scale=ExpandAffine(&draw_info->affine);
1520 length=scale*(draw_info->dash_pattern[0]-0.5);
1521 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1523 for (n=0; offset > 0.0; j=0)
1525 if (draw_info->dash_pattern[n] <= 0.0)
1527 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1528 if (offset > length)
1532 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1535 if (offset < length)
1547 for (i=1; i < (ssize_t) number_vertices; i++)
1549 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1550 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1551 maximum_length=hypot((double) dx,dy);
1555 if (draw_info->dash_pattern[n] == 0.0)
1557 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1559 for (total_length=0.0; (total_length+length) < maximum_length; )
1561 total_length+=length;
1562 if ((n & 0x01) != 0)
1564 dash_polygon[0]=primitive_info[0];
1565 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1566 total_length/maximum_length);
1567 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1568 total_length/maximum_length);
1573 if ((j+1) > (ssize_t) (2*number_vertices))
1575 dash_polygon[j]=primitive_info[i-1];
1576 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1577 total_length/maximum_length);
1578 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1579 total_length/maximum_length);
1580 dash_polygon[j].coordinates=1;
1582 dash_polygon[0].coordinates=(size_t) j;
1583 dash_polygon[j].primitive=UndefinedPrimitive;
1584 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1587 if (draw_info->dash_pattern[n] == 0.0)
1589 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1591 length-=(maximum_length-total_length);
1592 if ((n & 0x01) != 0)
1594 dash_polygon[j]=primitive_info[i];
1595 dash_polygon[j].coordinates=1;
1598 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1600 dash_polygon[j]=primitive_info[i-1];
1601 dash_polygon[j].point.x+=MagickEpsilon;
1602 dash_polygon[j].point.y+=MagickEpsilon;
1603 dash_polygon[j].coordinates=1;
1605 dash_polygon[0].coordinates=(size_t) j;
1606 dash_polygon[j].primitive=UndefinedPrimitive;
1607 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1609 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1610 clone_info=DestroyDrawInfo(clone_info);
1611 if (image->debug != MagickFalse)
1612 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1613 return(status != 0 ? MagickTrue : MagickFalse);
1617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 % D r a w I m a g e %
1625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 % DrawImage() draws a graphic primitive on your image. The primitive
1628 % may be represented as a string or filename. Precede the filename with an
1629 % "at" sign (@) and the contents of the file are drawn on the image. You
1630 % can affect how text is drawn by setting one or more members of the draw
1633 % The format of the DrawImage method is:
1635 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1636 % ExceptionInfo *exception)
1638 % A description of each parameter follows:
1640 % o image: the image.
1642 % o draw_info: the draw info.
1644 % o exception: return any errors or warnings in this structure.
1648 static inline MagickBooleanType IsPoint(const char *point)
1656 value=StringToDouble(point,&p);
1657 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1660 static inline void TracePoint(PrimitiveInfo *primitive_info,
1661 const PointInfo point)
1663 primitive_info->coordinates=1;
1664 primitive_info->point=point;
1667 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1668 ExceptionInfo *exception)
1670 #define RenderImageTag "Render/Image"
1677 key[2*MaxTextExtent],
1678 keyword[MaxTextExtent],
1679 geometry[MaxTextExtent],
1680 name[MaxTextExtent],
1681 pattern[MaxTextExtent],
1732 Ensure the annotation info is valid.
1734 assert(image != (Image *) NULL);
1735 assert(image->signature == MagickSignature);
1736 if (image->debug != MagickFalse)
1737 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1738 assert(draw_info != (DrawInfo *) NULL);
1739 assert(draw_info->signature == MagickSignature);
1740 if (image->debug != MagickFalse)
1741 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1742 if ((draw_info->primitive == (char *) NULL) ||
1743 (*draw_info->primitive == '\0'))
1744 return(MagickFalse);
1745 if (image->debug != MagickFalse)
1746 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1747 if (*draw_info->primitive != '@')
1748 primitive=AcquireString(draw_info->primitive);
1750 primitive=FileToString(draw_info->primitive+1,~0,exception);
1751 if (primitive == (char *) NULL)
1752 return(MagickFalse);
1753 primitive_extent=(MagickRealType) strlen(primitive);
1754 (void) SetImageArtifact(image,"MVG",primitive);
1757 Allocate primitive info memory.
1759 graphic_context=(DrawInfo **) AcquireMagickMemory(
1760 sizeof(*graphic_context));
1761 if (graphic_context == (DrawInfo **) NULL)
1763 primitive=DestroyString(primitive);
1764 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1768 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1769 sizeof(*primitive_info));
1770 if (primitive_info == (PrimitiveInfo *) NULL)
1772 primitive=DestroyString(primitive);
1773 for ( ; n >= 0; n--)
1774 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1775 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1776 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1779 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1780 graphic_context[n]->viewbox=image->page;
1781 if ((image->page.width == 0) || (image->page.height == 0))
1783 graphic_context[n]->viewbox.width=image->columns;
1784 graphic_context[n]->viewbox.height=image->rows;
1786 token=AcquireString(primitive);
1787 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1789 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1790 return(MagickFalse);
1792 for (q=primitive; *q != '\0'; )
1795 Interpret graphic primitive.
1797 GetMagickToken(q,&q,keyword);
1798 if (*keyword == '\0')
1800 if (*keyword == '#')
1805 while ((*q != '\n') && (*q != '\0'))
1809 p=q-strlen(keyword)-1;
1810 primitive_type=UndefinedPrimitive;
1811 current=graphic_context[n]->affine;
1812 GetAffineMatrix(&affine);
1820 if (LocaleCompare("affine",keyword) == 0)
1822 GetMagickToken(q,&q,token);
1823 affine.sx=StringToDouble(token,(char **) NULL);
1824 GetMagickToken(q,&q,token);
1826 GetMagickToken(q,&q,token);
1827 affine.rx=StringToDouble(token,(char **) NULL);
1828 GetMagickToken(q,&q,token);
1830 GetMagickToken(q,&q,token);
1831 affine.ry=StringToDouble(token,(char **) NULL);
1832 GetMagickToken(q,&q,token);
1834 GetMagickToken(q,&q,token);
1835 affine.sy=StringToDouble(token,(char **) NULL);
1836 GetMagickToken(q,&q,token);
1838 GetMagickToken(q,&q,token);
1839 affine.tx=StringToDouble(token,(char **) NULL);
1840 GetMagickToken(q,&q,token);
1842 GetMagickToken(q,&q,token);
1843 affine.ty=StringToDouble(token,(char **) NULL);
1846 if (LocaleCompare("arc",keyword) == 0)
1848 primitive_type=ArcPrimitive;
1857 if (LocaleCompare("bezier",keyword) == 0)
1859 primitive_type=BezierPrimitive;
1862 if (LocaleCompare("border-color",keyword) == 0)
1864 GetMagickToken(q,&q,token);
1865 (void) QueryColorCompliance(token,AllCompliance,
1866 &graphic_context[n]->border_color,exception);
1875 if (LocaleCompare("clip-path",keyword) == 0)
1880 GetMagickToken(q,&q,token);
1881 (void) CloneString(&graphic_context[n]->clip_mask,token);
1882 (void) DrawClipPath(image,graphic_context[n],
1883 graphic_context[n]->clip_mask,exception);
1886 if (LocaleCompare("clip-rule",keyword) == 0)
1891 GetMagickToken(q,&q,token);
1892 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1894 if (fill_rule == -1)
1897 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1900 if (LocaleCompare("clip-units",keyword) == 0)
1905 GetMagickToken(q,&q,token);
1906 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1908 if (clip_units == -1)
1913 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1914 if (clip_units == ObjectBoundingBox)
1916 GetAffineMatrix(¤t);
1917 affine.sx=draw_info->bounds.x2;
1918 affine.sy=draw_info->bounds.y2;
1919 affine.tx=draw_info->bounds.x1;
1920 affine.ty=draw_info->bounds.y1;
1925 if (LocaleCompare("circle",keyword) == 0)
1927 primitive_type=CirclePrimitive;
1930 if (LocaleCompare("color",keyword) == 0)
1932 primitive_type=ColorPrimitive;
1941 if (LocaleCompare("decorate",keyword) == 0)
1946 GetMagickToken(q,&q,token);
1947 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1952 graphic_context[n]->decorate=(DecorationType) decorate;
1961 if (LocaleCompare("ellipse",keyword) == 0)
1963 primitive_type=EllipsePrimitive;
1966 if (LocaleCompare("encoding",keyword) == 0)
1968 GetMagickToken(q,&q,token);
1969 (void) CloneString(&graphic_context[n]->encoding,token);
1978 if (LocaleCompare("fill",keyword) == 0)
1980 GetMagickToken(q,&q,token);
1981 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1982 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1983 (void) DrawPatternPath(image,draw_info,token,
1984 &graphic_context[n]->fill_pattern,exception);
1987 status=QueryColorCompliance(token,AllCompliance,
1988 &graphic_context[n]->fill,exception);
1989 if (status == MagickFalse)
1994 pattern_info=AcquireImageInfo();
1995 (void) CopyMagickString(pattern_info->filename,token,
1997 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
1999 CatchException(exception);
2000 pattern_info=DestroyImageInfo(pattern_info);
2005 if (LocaleCompare("fill-opacity",keyword) == 0)
2007 GetMagickToken(q,&q,token);
2008 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2009 graphic_context[n]->fill.alpha=(MagickRealType) QuantumRange*
2010 factor*StringToDouble(token,(char **) NULL);
2013 if (LocaleCompare("fill-rule",keyword) == 0)
2018 GetMagickToken(q,&q,token);
2019 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2021 if (fill_rule == -1)
2024 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2027 if (LocaleCompare("font",keyword) == 0)
2029 GetMagickToken(q,&q,token);
2030 (void) CloneString(&graphic_context[n]->font,token);
2031 if (LocaleCompare("none",token) == 0)
2032 graphic_context[n]->font=(char *)
2033 RelinquishMagickMemory(graphic_context[n]->font);
2036 if (LocaleCompare("font-family",keyword) == 0)
2038 GetMagickToken(q,&q,token);
2039 (void) CloneString(&graphic_context[n]->family,token);
2042 if (LocaleCompare("font-size",keyword) == 0)
2044 GetMagickToken(q,&q,token);
2045 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2048 if (LocaleCompare("font-stretch",keyword) == 0)
2053 GetMagickToken(q,&q,token);
2054 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2058 graphic_context[n]->stretch=(StretchType) stretch;
2061 if (LocaleCompare("font-style",keyword) == 0)
2066 GetMagickToken(q,&q,token);
2067 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2071 graphic_context[n]->style=(StyleType) style;
2074 if (LocaleCompare("font-weight",keyword) == 0)
2076 GetMagickToken(q,&q,token);
2077 graphic_context[n]->weight=StringToUnsignedLong(token);
2078 if (LocaleCompare(token,"all") == 0)
2079 graphic_context[n]->weight=0;
2080 if (LocaleCompare(token,"bold") == 0)
2081 graphic_context[n]->weight=700;
2082 if (LocaleCompare(token,"bolder") == 0)
2083 if (graphic_context[n]->weight <= 800)
2084 graphic_context[n]->weight+=100;
2085 if (LocaleCompare(token,"lighter") == 0)
2086 if (graphic_context[n]->weight >= 100)
2087 graphic_context[n]->weight-=100;
2088 if (LocaleCompare(token,"normal") == 0)
2089 graphic_context[n]->weight=400;
2098 if (LocaleCompare("gradient-units",keyword) == 0)
2100 GetMagickToken(q,&q,token);
2103 if (LocaleCompare("gravity",keyword) == 0)
2108 GetMagickToken(q,&q,token);
2109 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2113 graphic_context[n]->gravity=(GravityType) gravity;
2122 if (LocaleCompare("image",keyword) == 0)
2127 primitive_type=ImagePrimitive;
2128 GetMagickToken(q,&q,token);
2129 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2133 graphic_context[n]->compose=(CompositeOperator) compose;
2136 if (LocaleCompare("interline-spacing",keyword) == 0)
2138 GetMagickToken(q,&q,token);
2139 graphic_context[n]->interline_spacing=StringToDouble(token,
2143 if (LocaleCompare("interword-spacing",keyword) == 0)
2145 GetMagickToken(q,&q,token);
2146 graphic_context[n]->interword_spacing=StringToDouble(token,
2156 if (LocaleCompare("kerning",keyword) == 0)
2158 GetMagickToken(q,&q,token);
2159 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2168 if (LocaleCompare("line",keyword) == 0)
2169 primitive_type=LinePrimitive;
2177 if (LocaleCompare("matte",keyword) == 0)
2178 primitive_type=MattePrimitive;
2186 if (LocaleCompare("offset",keyword) == 0)
2188 GetMagickToken(q,&q,token);
2191 if (LocaleCompare("opacity",keyword) == 0)
2193 GetMagickToken(q,&q,token);
2194 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2195 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2196 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
2197 factor*StringToDouble(token,(char **) NULL))));
2198 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2199 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2208 if (LocaleCompare("path",keyword) == 0)
2210 primitive_type=PathPrimitive;
2213 if (LocaleCompare("point",keyword) == 0)
2215 primitive_type=PointPrimitive;
2218 if (LocaleCompare("polyline",keyword) == 0)
2220 primitive_type=PolylinePrimitive;
2223 if (LocaleCompare("polygon",keyword) == 0)
2225 primitive_type=PolygonPrimitive;
2228 if (LocaleCompare("pop",keyword) == 0)
2230 GetMagickToken(q,&q,token);
2231 if (LocaleCompare("clip-path",token) == 0)
2233 if (LocaleCompare("defs",token) == 0)
2235 if (LocaleCompare("gradient",token) == 0)
2237 if (LocaleCompare("graphic-context",token) == 0)
2241 (void) ThrowMagickException(exception,GetMagickModule(),
2242 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2246 if (graphic_context[n]->clip_mask != (char *) NULL)
2247 if (LocaleCompare(graphic_context[n]->clip_mask,
2248 graphic_context[n-1]->clip_mask) != 0)
2249 (void) SetImageClipMask(image,(Image *) NULL,exception);
2250 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2254 if (LocaleCompare("pattern",token) == 0)
2259 if (LocaleCompare("push",keyword) == 0)
2261 GetMagickToken(q,&q,token);
2262 if (LocaleCompare("clip-path",token) == 0)
2265 name[MaxTextExtent];
2267 GetMagickToken(q,&q,token);
2268 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2269 for (p=q; *q != '\0'; )
2271 GetMagickToken(q,&q,token);
2272 if (LocaleCompare(token,"pop") != 0)
2274 GetMagickToken(q,(const char **) NULL,token);
2275 if (LocaleCompare(token,"clip-path") != 0)
2279 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2280 (void) SetImageArtifact(image,name,token);
2281 GetMagickToken(q,&q,token);
2284 if (LocaleCompare("gradient",token) == 0)
2287 key[2*MaxTextExtent],
2288 name[MaxTextExtent],
2289 type[MaxTextExtent];
2294 GetMagickToken(q,&q,token);
2295 (void) CopyMagickString(name,token,MaxTextExtent);
2296 GetMagickToken(q,&q,token);
2297 (void) CopyMagickString(type,token,MaxTextExtent);
2298 GetMagickToken(q,&q,token);
2299 segment.x1=StringToDouble(token,(char **) NULL);
2300 GetMagickToken(q,&q,token);
2302 GetMagickToken(q,&q,token);
2303 segment.y1=StringToDouble(token,(char **) NULL);
2304 GetMagickToken(q,&q,token);
2306 GetMagickToken(q,&q,token);
2307 segment.x2=StringToDouble(token,(char **) NULL);
2308 GetMagickToken(q,&q,token);
2310 GetMagickToken(q,&q,token);
2311 segment.y2=StringToDouble(token,(char **) NULL);
2312 if (LocaleCompare(type,"radial") == 0)
2314 GetMagickToken(q,&q,token);
2316 GetMagickToken(q,&q,token);
2318 for (p=q; *q != '\0'; )
2320 GetMagickToken(q,&q,token);
2321 if (LocaleCompare(token,"pop") != 0)
2323 GetMagickToken(q,(const char **) NULL,token);
2324 if (LocaleCompare(token,"gradient") != 0)
2328 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2329 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2330 graphic_context[n]->affine.ry*segment.y1+
2331 graphic_context[n]->affine.tx;
2332 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2333 graphic_context[n]->affine.sy*segment.y1+
2334 graphic_context[n]->affine.ty;
2335 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2336 graphic_context[n]->affine.ry*segment.y2+
2337 graphic_context[n]->affine.tx;
2338 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2339 graphic_context[n]->affine.sy*segment.y2+
2340 graphic_context[n]->affine.ty;
2341 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2342 (void) SetImageArtifact(image,key,token);
2343 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2344 (void) FormatLocaleString(geometry,MaxTextExtent,
2345 "%gx%g%+.15g%+.15g",
2346 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2347 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2348 bounds.x1,bounds.y1);
2349 (void) SetImageArtifact(image,key,geometry);
2350 GetMagickToken(q,&q,token);
2353 if (LocaleCompare("pattern",token) == 0)
2358 GetMagickToken(q,&q,token);
2359 (void) CopyMagickString(name,token,MaxTextExtent);
2360 GetMagickToken(q,&q,token);
2361 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2363 GetMagickToken(q,&q,token);
2365 GetMagickToken(q,&q,token);
2366 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2368 GetMagickToken(q,&q,token);
2370 GetMagickToken(q,&q,token);
2371 bounds.width=(size_t) floor(StringToDouble(token,
2372 (char **) NULL)+0.5);
2373 GetMagickToken(q,&q,token);
2375 GetMagickToken(q,&q,token);
2376 bounds.height=(size_t) floor(StringToDouble(token,
2377 (char **) NULL)+0.5);
2378 for (p=q; *q != '\0'; )
2380 GetMagickToken(q,&q,token);
2381 if (LocaleCompare(token,"pop") != 0)
2383 GetMagickToken(q,(const char **) NULL,token);
2384 if (LocaleCompare(token,"pattern") != 0)
2388 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2389 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2390 (void) SetImageArtifact(image,key,token);
2391 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2392 (void) FormatLocaleString(geometry,MaxTextExtent,
2393 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2394 bounds.height,(double) bounds.x,(double) bounds.y);
2395 (void) SetImageArtifact(image,key,geometry);
2396 GetMagickToken(q,&q,token);
2399 if (LocaleCompare("graphic-context",token) == 0)
2402 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2403 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2404 if (graphic_context == (DrawInfo **) NULL)
2406 (void) ThrowMagickException(exception,GetMagickModule(),
2407 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2411 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2412 graphic_context[n-1]);
2415 if (LocaleCompare("defs",token) == 0)
2426 if (LocaleCompare("rectangle",keyword) == 0)
2428 primitive_type=RectanglePrimitive;
2431 if (LocaleCompare("rotate",keyword) == 0)
2433 GetMagickToken(q,&q,token);
2434 angle=StringToDouble(token,(char **) NULL);
2435 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2436 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2437 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2438 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2441 if (LocaleCompare("roundRectangle",keyword) == 0)
2443 primitive_type=RoundRectanglePrimitive;
2452 if (LocaleCompare("scale",keyword) == 0)
2454 GetMagickToken(q,&q,token);
2455 affine.sx=StringToDouble(token,(char **) NULL);
2456 GetMagickToken(q,&q,token);
2458 GetMagickToken(q,&q,token);
2459 affine.sy=StringToDouble(token,(char **) NULL);
2462 if (LocaleCompare("skewX",keyword) == 0)
2464 GetMagickToken(q,&q,token);
2465 angle=StringToDouble(token,(char **) NULL);
2466 affine.ry=sin(DegreesToRadians(angle));
2469 if (LocaleCompare("skewY",keyword) == 0)
2471 GetMagickToken(q,&q,token);
2472 angle=StringToDouble(token,(char **) NULL);
2473 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2476 if (LocaleCompare("stop-color",keyword) == 0)
2481 GetMagickToken(q,&q,token);
2482 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2484 (void) GradientImage(image,LinearGradient,ReflectSpread,
2485 &start_color,&stop_color,exception);
2486 start_color=stop_color;
2487 GetMagickToken(q,&q,token);
2490 if (LocaleCompare("stroke",keyword) == 0)
2492 GetMagickToken(q,&q,token);
2493 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2494 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2495 (void) DrawPatternPath(image,draw_info,token,
2496 &graphic_context[n]->stroke_pattern,exception);
2499 status=QueryColorCompliance(token,AllCompliance,
2500 &graphic_context[n]->stroke,exception);
2501 if (status == MagickFalse)
2506 pattern_info=AcquireImageInfo();
2507 (void) CopyMagickString(pattern_info->filename,token,
2509 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2511 CatchException(exception);
2512 pattern_info=DestroyImageInfo(pattern_info);
2517 if (LocaleCompare("stroke-antialias",keyword) == 0)
2519 GetMagickToken(q,&q,token);
2520 graphic_context[n]->stroke_antialias=
2521 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2524 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2526 if (graphic_context[n]->dash_pattern != (double *) NULL)
2527 graphic_context[n]->dash_pattern=(double *)
2528 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2529 if (IsPoint(q) != MagickFalse)
2535 GetMagickToken(p,&p,token);
2537 GetMagickToken(p,&p,token);
2538 for (x=0; IsPoint(token) != MagickFalse; x++)
2540 GetMagickToken(p,&p,token);
2542 GetMagickToken(p,&p,token);
2544 graphic_context[n]->dash_pattern=(double *)
2545 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2546 sizeof(*graphic_context[n]->dash_pattern));
2547 if (graphic_context[n]->dash_pattern == (double *) NULL)
2549 (void) ThrowMagickException(exception,GetMagickModule(),
2550 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2554 for (j=0; j < x; j++)
2556 GetMagickToken(q,&q,token);
2558 GetMagickToken(q,&q,token);
2559 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2562 if ((x & 0x01) != 0)
2563 for ( ; j < (2*x); j++)
2564 graphic_context[n]->dash_pattern[j]=
2565 graphic_context[n]->dash_pattern[j-x];
2566 graphic_context[n]->dash_pattern[j]=0.0;
2569 GetMagickToken(q,&q,token);
2572 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2574 GetMagickToken(q,&q,token);
2575 graphic_context[n]->dash_offset=StringToDouble(token,
2579 if (LocaleCompare("stroke-linecap",keyword) == 0)
2584 GetMagickToken(q,&q,token);
2585 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2589 graphic_context[n]->linecap=(LineCap) linecap;
2592 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2597 GetMagickToken(q,&q,token);
2598 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2603 graphic_context[n]->linejoin=(LineJoin) linejoin;
2606 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2608 GetMagickToken(q,&q,token);
2609 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2612 if (LocaleCompare("stroke-opacity",keyword) == 0)
2614 GetMagickToken(q,&q,token);
2615 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2616 graphic_context[n]->stroke.alpha=(MagickRealType) QuantumRange*
2617 factor*StringToDouble(token,(char **) NULL);
2620 if (LocaleCompare("stroke-width",keyword) == 0)
2622 GetMagickToken(q,&q,token);
2623 graphic_context[n]->stroke_width=StringToDouble(token,
2633 if (LocaleCompare("text",keyword) == 0)
2635 primitive_type=TextPrimitive;
2638 if (LocaleCompare("text-align",keyword) == 0)
2643 GetMagickToken(q,&q,token);
2644 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2648 graphic_context[n]->align=(AlignType) align;
2651 if (LocaleCompare("text-anchor",keyword) == 0)
2656 GetMagickToken(q,&q,token);
2657 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2661 graphic_context[n]->align=(AlignType) align;
2664 if (LocaleCompare("text-antialias",keyword) == 0)
2666 GetMagickToken(q,&q,token);
2667 graphic_context[n]->text_antialias=
2668 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2671 if (LocaleCompare("text-undercolor",keyword) == 0)
2673 GetMagickToken(q,&q,token);
2674 (void) QueryColorCompliance(token,AllCompliance,
2675 &graphic_context[n]->undercolor,exception);
2678 if (LocaleCompare("translate",keyword) == 0)
2680 GetMagickToken(q,&q,token);
2681 affine.tx=StringToDouble(token,(char **) NULL);
2682 GetMagickToken(q,&q,token);
2684 GetMagickToken(q,&q,token);
2685 affine.ty=StringToDouble(token,(char **) NULL);
2694 if (LocaleCompare("viewbox",keyword) == 0)
2696 GetMagickToken(q,&q,token);
2697 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2698 (char **) NULL)-0.5);
2699 GetMagickToken(q,&q,token);
2701 GetMagickToken(q,&q,token);
2702 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2703 (char **) NULL)-0.5);
2704 GetMagickToken(q,&q,token);
2706 GetMagickToken(q,&q,token);
2707 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2708 token,(char **) NULL)+0.5);
2709 GetMagickToken(q,&q,token);
2711 GetMagickToken(q,&q,token);
2712 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2713 token,(char **) NULL)+0.5);
2725 if (status == MagickFalse)
2727 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2728 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2730 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2731 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2732 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2733 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2734 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2736 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2739 if (primitive_type == UndefinedPrimitive)
2741 if (image->debug != MagickFalse)
2742 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2747 Parse the primitive attributes.
2751 primitive_info[0].point.x=0.0;
2752 primitive_info[0].point.y=0.0;
2753 for (x=0; *q != '\0'; x++)
2758 if (IsPoint(q) == MagickFalse)
2760 GetMagickToken(q,&q,token);
2761 point.x=StringToDouble(token,(char **) NULL);
2762 GetMagickToken(q,&q,token);
2764 GetMagickToken(q,&q,token);
2765 point.y=StringToDouble(token,(char **) NULL);
2766 GetMagickToken(q,(const char **) NULL,token);
2768 GetMagickToken(q,&q,token);
2769 primitive_info[i].primitive=primitive_type;
2770 primitive_info[i].point=point;
2771 primitive_info[i].coordinates=0;
2772 primitive_info[i].method=FloodfillMethod;
2774 if (i < (ssize_t) number_points)
2777 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2778 (size_t) number_points,sizeof(*primitive_info));
2779 if (primitive_info == (PrimitiveInfo *) NULL)
2781 (void) ThrowMagickException(exception,GetMagickModule(),
2782 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2786 primitive_info[j].primitive=primitive_type;
2787 primitive_info[j].coordinates=(size_t) x;
2788 primitive_info[j].method=FloodfillMethod;
2789 primitive_info[j].text=(char *) NULL;
2791 Circumscribe primitive within a circle.
2793 bounds.x1=primitive_info[j].point.x;
2794 bounds.y1=primitive_info[j].point.y;
2795 bounds.x2=primitive_info[j].point.x;
2796 bounds.y2=primitive_info[j].point.y;
2797 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2799 point=primitive_info[j+k].point;
2800 if (point.x < bounds.x1)
2802 if (point.y < bounds.y1)
2804 if (point.x > bounds.x2)
2806 if (point.y > bounds.y2)
2810 Speculate how many points our primitive might consume.
2812 length=primitive_info[j].coordinates;
2813 switch (primitive_type)
2815 case RectanglePrimitive:
2820 case RoundRectanglePrimitive:
2822 length*=5+8*BezierQuantum;
2825 case BezierPrimitive:
2827 if (primitive_info[j].coordinates > 107)
2828 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2829 "TooManyBezierCoordinates","`%s'",token);
2830 length=BezierQuantum*primitive_info[j].coordinates;
2839 GetMagickToken(q,&q,token);
2842 for (s=token; *s != '\0'; s=t)
2847 value=StringToDouble(s,&t);
2856 length=length*BezierQuantum/2;
2859 case CirclePrimitive:
2861 case EllipsePrimitive:
2868 alpha=bounds.x2-bounds.x1;
2869 beta=bounds.y2-bounds.y1;
2870 radius=hypot((double) alpha,(double) beta);
2871 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2877 if ((size_t) (i+length) >= number_points)
2880 Resize based on speculative points required by primitive.
2882 number_points+=length+1;
2883 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2884 (size_t) number_points,sizeof(*primitive_info));
2885 if (primitive_info == (PrimitiveInfo *) NULL)
2887 (void) ThrowMagickException(exception,GetMagickModule(),
2888 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2893 switch (primitive_type)
2895 case PointPrimitive:
2898 if (primitive_info[j].coordinates != 1)
2903 TracePoint(primitive_info+j,primitive_info[j].point);
2904 i=(ssize_t) (j+primitive_info[j].coordinates);
2909 if (primitive_info[j].coordinates != 2)
2914 TraceLine(primitive_info+j,primitive_info[j].point,
2915 primitive_info[j+1].point);
2916 i=(ssize_t) (j+primitive_info[j].coordinates);
2919 case RectanglePrimitive:
2921 if (primitive_info[j].coordinates != 2)
2926 TraceRectangle(primitive_info+j,primitive_info[j].point,
2927 primitive_info[j+1].point);
2928 i=(ssize_t) (j+primitive_info[j].coordinates);
2931 case RoundRectanglePrimitive:
2933 if (primitive_info[j].coordinates != 3)
2938 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2939 primitive_info[j+1].point,primitive_info[j+2].point);
2940 i=(ssize_t) (j+primitive_info[j].coordinates);
2945 if (primitive_info[j].coordinates != 3)
2947 primitive_type=UndefinedPrimitive;
2950 TraceArc(primitive_info+j,primitive_info[j].point,
2951 primitive_info[j+1].point,primitive_info[j+2].point);
2952 i=(ssize_t) (j+primitive_info[j].coordinates);
2955 case EllipsePrimitive:
2957 if (primitive_info[j].coordinates != 3)
2962 TraceEllipse(primitive_info+j,primitive_info[j].point,
2963 primitive_info[j+1].point,primitive_info[j+2].point);
2964 i=(ssize_t) (j+primitive_info[j].coordinates);
2967 case CirclePrimitive:
2969 if (primitive_info[j].coordinates != 2)
2974 TraceCircle(primitive_info+j,primitive_info[j].point,
2975 primitive_info[j+1].point);
2976 i=(ssize_t) (j+primitive_info[j].coordinates);
2979 case PolylinePrimitive:
2981 case PolygonPrimitive:
2983 primitive_info[i]=primitive_info[j];
2984 primitive_info[i].coordinates=0;
2985 primitive_info[j].coordinates++;
2989 case BezierPrimitive:
2991 if (primitive_info[j].coordinates < 3)
2996 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
2997 i=(ssize_t) (j+primitive_info[j].coordinates);
3002 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3005 case ColorPrimitive:
3006 case MattePrimitive:
3011 if (primitive_info[j].coordinates != 1)
3016 GetMagickToken(q,&q,token);
3017 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3021 primitive_info[j].method=(PaintMethod) method;
3026 if (primitive_info[j].coordinates != 1)
3032 GetMagickToken(q,&q,token);
3033 primitive_info[j].text=AcquireString(token);
3036 case ImagePrimitive:
3038 if (primitive_info[j].coordinates != 2)
3043 GetMagickToken(q,&q,token);
3044 primitive_info[j].text=AcquireString(token);
3048 if (primitive_info == (PrimitiveInfo *) NULL)
3050 if (image->debug != MagickFalse)
3051 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3052 if (status == MagickFalse)
3054 primitive_info[i].primitive=UndefinedPrimitive;
3060 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3062 point=primitive_info[i].point;
3063 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3064 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3065 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3066 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3067 point=primitive_info[i].point;
3068 if (point.x < graphic_context[n]->bounds.x1)
3069 graphic_context[n]->bounds.x1=point.x;
3070 if (point.y < graphic_context[n]->bounds.y1)
3071 graphic_context[n]->bounds.y1=point.y;
3072 if (point.x > graphic_context[n]->bounds.x2)
3073 graphic_context[n]->bounds.x2=point.x;
3074 if (point.y > graphic_context[n]->bounds.y2)
3075 graphic_context[n]->bounds.y2=point.y;
3076 if (primitive_info[i].primitive == ImagePrimitive)
3078 if (i >= (ssize_t) number_points)
3079 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3081 if (graphic_context[n]->render != MagickFalse)
3083 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3084 (LocaleCompare(graphic_context[n]->clip_mask,
3085 graphic_context[n-1]->clip_mask) != 0))
3086 (void) DrawClipPath(image,graphic_context[n],
3087 graphic_context[n]->clip_mask,exception);
3088 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3090 if (primitive_info->text != (char *) NULL)
3091 primitive_info->text=(char *) RelinquishMagickMemory(
3092 primitive_info->text);
3093 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3095 if (proceed == MagickFalse)
3098 if (image->debug != MagickFalse)
3099 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3101 Relinquish resources.
3103 token=DestroyString(token);
3104 if (primitive_info != (PrimitiveInfo *) NULL)
3105 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3106 primitive=DestroyString(primitive);
3107 for ( ; n >= 0; n--)
3108 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3109 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3110 if (status == MagickFalse)
3111 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121 % D r a w G r a d i e n t I m a g e %
3125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3127 % DrawGradientImage() draws a linear gradient on the image.
3129 % The format of the DrawGradientImage method is:
3131 % MagickBooleanType DrawGradientImage(Image *image,
3132 % const DrawInfo *draw_info,ExceptionInfo *exception)
3134 % A description of each parameter follows:
3136 % o image: the image.
3138 % o draw_info: the draw info.
3140 % o exception: return any errors or warnings in this structure.
3144 static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
3145 const ssize_t x,const ssize_t y)
3147 switch (gradient->type)
3149 case UndefinedGradient:
3150 case LinearGradient:
3165 gradient_vector=(&gradient->gradient_vector);
3166 p.x=gradient_vector->x2-gradient_vector->x1;
3167 p.y=gradient_vector->y2-gradient_vector->y1;
3168 q.x=(double) x-gradient_vector->x1;
3169 q.y=(double) y-gradient_vector->y1;
3170 length=sqrt(q.x*q.x+q.y*q.y);
3171 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3172 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3173 scale=p.x*q.x+p.y*q.y;
3174 offset=gamma*scale*length;
3177 case RadialGradient:
3186 v.x=(double) x-gradient->center.x;
3187 v.y=(double) y-gradient->center.y;
3188 length=sqrt(v.x*v.x+v.y*v.y);
3189 if (gradient->spread == RepeatSpread)
3191 offset=length/gradient->radius;
3198 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3199 const DrawInfo *draw_info,ExceptionInfo *exception)
3229 Draw linear or radial gradient on image.
3231 assert(image != (Image *) NULL);
3232 assert(image->signature == MagickSignature);
3233 if (image->debug != MagickFalse)
3234 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3235 assert(draw_info != (const DrawInfo *) NULL);
3236 gradient=(&draw_info->gradient);
3237 gradient_vector=(&gradient->gradient_vector);
3238 point.x=gradient_vector->x2-gradient_vector->x1;
3239 point.y=gradient_vector->y2-gradient_vector->y1;
3240 length=sqrt(point.x*point.x+point.y*point.y);
3241 bounding_box=gradient->bounding_box;
3243 GetPixelInfo(image,&zero);
3244 image_view=AcquireCacheView(image);
3245 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3246 #pragma omp parallel for schedule(dynamic,4) shared(status)
3248 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3268 if (status == MagickFalse)
3270 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3271 if (q == (Quantum *) NULL)
3278 offset=GetStopColorOffset(gradient,0,y);
3279 if (gradient->type != RadialGradient)
3281 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3283 GetPixelInfoPixel(image,q,&pixel);
3284 switch (gradient->spread)
3286 case UndefinedSpread:
3289 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3290 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3292 offset=GetStopColorOffset(gradient,x,y);
3293 if (gradient->type != RadialGradient)
3296 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3297 if (offset < gradient->stops[i].offset)
3299 if ((offset < 0.0) || (i == 0))
3300 composite=gradient->stops[0].color;
3302 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3303 composite=gradient->stops[gradient->number_stops-1].color;
3308 alpha=(offset-gradient->stops[i].offset)/
3309 (gradient->stops[j].offset-gradient->stops[i].offset);
3310 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3311 &gradient->stops[j].color,alpha,&composite);
3317 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3318 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3320 offset=GetStopColorOffset(gradient,x,y);
3321 if (gradient->type != RadialGradient)
3326 if ((ssize_t) fmod(offset,2.0) == 0)
3327 offset=fmod(offset,1.0);
3329 offset=1.0-fmod(offset,1.0);
3330 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3331 if (offset < gradient->stops[i].offset)
3334 composite=gradient->stops[0].color;
3336 if (i == (ssize_t) gradient->number_stops)
3337 composite=gradient->stops[gradient->number_stops-1].color;
3342 alpha=(offset-gradient->stops[i].offset)/
3343 (gradient->stops[j].offset-gradient->stops[i].offset);
3344 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3345 &gradient->stops[j].color,alpha,&composite);
3357 antialias=MagickFalse;
3359 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3360 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3362 offset=GetStopColorOffset(gradient,x,y);
3363 if (gradient->type == LinearGradient)
3365 repeat=fmod(offset,length);
3367 repeat=length-fmod(-repeat,length);
3369 repeat=fmod(offset,length);
3370 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3371 MagickTrue : MagickFalse;
3372 offset=repeat/length;
3376 repeat=fmod(offset,gradient->radius);
3378 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3380 repeat=fmod(offset,gradient->radius);
3381 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3383 offset=repeat/gradient->radius;
3386 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3387 if (offset < gradient->stops[i].offset)
3390 composite=gradient->stops[0].color;
3392 if (i == (ssize_t) gradient->number_stops)
3393 composite=gradient->stops[gradient->number_stops-1].color;
3398 alpha=(offset-gradient->stops[i].offset)/
3399 (gradient->stops[j].offset-gradient->stops[i].offset);
3400 if (antialias != MagickFalse)
3402 if (gradient->type == LinearGradient)
3403 alpha=length-repeat;
3405 alpha=gradient->radius-repeat;
3407 j=(ssize_t) gradient->number_stops-1L;
3409 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3410 &gradient->stops[j].color,alpha,&composite);
3415 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3417 SetPixelInfoPixel(image,&pixel,q);
3418 q+=GetPixelChannels(image);
3420 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3423 image_view=DestroyCacheView(image_view);
3428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3432 % D r a w P a t t e r n P a t h %
3436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3438 % DrawPatternPath() draws a pattern.
3440 % The format of the DrawPatternPath method is:
3442 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3443 % const char *name,Image **pattern,ExceptionInfo *exception)
3445 % A description of each parameter follows:
3447 % o image: the image.
3449 % o draw_info: the draw info.
3451 % o name: the pattern name.
3453 % o image: the image.
3455 % o exception: return any errors or warnings in this structure.
3458 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3459 const DrawInfo *draw_info,const char *name,Image **pattern,
3460 ExceptionInfo *exception)
3463 property[MaxTextExtent];
3478 assert(image != (Image *) NULL);
3479 assert(image->signature == MagickSignature);
3480 if (image->debug != MagickFalse)
3481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3482 assert(draw_info != (const DrawInfo *) NULL);
3483 assert(name != (const char *) NULL);
3484 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3485 path=GetImageArtifact(image,property);
3486 if (path == (const char *) NULL)
3487 return(MagickFalse);
3488 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3489 geometry=GetImageArtifact(image,property);
3490 if (geometry == (const char *) NULL)
3491 return(MagickFalse);
3492 if ((*pattern) != (Image *) NULL)
3493 *pattern=DestroyImage(*pattern);
3494 image_info=AcquireImageInfo();
3495 image_info->size=AcquireString(geometry);
3496 *pattern=AcquireImage(image_info,exception);
3497 image_info=DestroyImageInfo(image_info);
3498 (void) QueryColorCompliance("#00000000",AllCompliance,
3499 &(*pattern)->background_color,exception);
3500 (void) SetImageBackgroundColor(*pattern,exception);
3501 if (image->debug != MagickFalse)
3502 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3503 "begin pattern-path %s %s",name,geometry);
3504 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3505 clone_info->fill_pattern=NewImageList();
3506 clone_info->stroke_pattern=NewImageList();
3507 (void) CloneString(&clone_info->primitive,path);
3508 status=DrawImage(*pattern,clone_info,exception);
3509 clone_info=DestroyDrawInfo(clone_info);
3510 if (image->debug != MagickFalse)
3511 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3520 + D r a w P o l y g o n P r i m i t i v e %
3524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3526 % DrawPolygonPrimitive() draws a polygon on the image.
3528 % The format of the DrawPolygonPrimitive method is:
3530 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3531 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3532 % ExceptionInfo *exception)
3534 % A description of each parameter follows:
3536 % o image: the image.
3538 % o draw_info: the draw info.
3540 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3542 % o exception: return any errors or warnings in this structure.
3546 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3551 assert(polygon_info != (PolygonInfo **) NULL);
3552 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3553 if (polygon_info[i] != (PolygonInfo *) NULL)
3554 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3555 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3556 return(polygon_info);
3559 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3560 const PrimitiveInfo *primitive_info)
3563 *restrict path_info;
3574 number_threads=GetOpenMPMaximumThreads();
3575 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3576 sizeof(*polygon_info));
3577 if (polygon_info == (PolygonInfo **) NULL)
3578 return((PolygonInfo **) NULL);
3579 (void) ResetMagickMemory(polygon_info,0,GetOpenMPMaximumThreads()*
3580 sizeof(*polygon_info));
3581 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3582 if (path_info == (PathInfo *) NULL)
3583 return(DestroyPolygonThreadSet(polygon_info));
3584 for (i=0; i < (ssize_t) number_threads; i++)
3586 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3587 if (polygon_info[i] == (PolygonInfo *) NULL)
3588 return(DestroyPolygonThreadSet(polygon_info));
3590 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3591 return(polygon_info);
3594 static MagickRealType GetFillAlpha(PolygonInfo *polygon_info,
3595 const MagickRealType mid,const MagickBooleanType fill,
3596 const FillRule fill_rule,const double x,const double y,
3597 MagickRealType *stroke_alpha)
3611 register const PointInfo
3622 Compute fill & stroke opacity for this (x,y) point.
3626 p=polygon_info->edges;
3627 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3629 if (y <= (p->bounds.y1-mid-0.5))
3631 if (y > (p->bounds.y2+mid+0.5))
3633 (void) DestroyEdge(polygon_info,(size_t) j);
3636 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3638 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3639 for ( ; i < (ssize_t) p->number_points; i++)
3641 if (y <= (p->points[i-1].y-mid-0.5))
3643 if (y > (p->points[i].y+mid+0.5))
3645 if (p->scanline != y)
3648 p->highwater=(size_t) i;
3651 Compute distance between a point and an edge.
3654 delta.x=(q+1)->x-q->x;
3655 delta.y=(q+1)->y-q->y;
3656 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3661 distance=delta.x*delta.x+delta.y*delta.y;
3665 alpha=delta.x*delta.x+delta.y*delta.y;
3670 distance=delta.x*delta.x+delta.y*delta.y;
3675 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3676 distance=alpha*beta*beta;
3680 Compute stroke & subpath opacity.
3683 if (p->ghostline == MagickFalse)
3686 if ((*stroke_alpha < 1.0) &&
3687 (distance <= ((alpha+0.25)*(alpha+0.25))))
3690 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3695 if (distance != 1.0)
3696 beta=sqrt((double) distance);
3698 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3699 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3703 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3705 if (distance <= 0.0)
3715 if (distance != 1.0)
3716 beta=sqrt(distance);
3719 if (subpath_alpha < (alpha*alpha))
3720 subpath_alpha=alpha*alpha;
3724 Compute fill opacity.
3726 if (fill == MagickFalse)
3728 if (subpath_alpha >= 1.0)
3731 Determine winding number.
3734 p=polygon_info->edges;
3735 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3737 if (y <= p->bounds.y1)
3739 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3741 if (x > p->bounds.x2)
3743 winding_number+=p->direction ? 1 : -1;
3746 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3747 for ( ; i < (ssize_t) p->number_points; i++)
3748 if (y <= p->points[i].y)
3751 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3752 winding_number+=p->direction ? 1 : -1;
3754 if (fill_rule != NonZeroRule)
3756 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3760 if (MagickAbsoluteValue(winding_number) != 0)
3762 return(subpath_alpha);
3765 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3766 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3767 ExceptionInfo *exception)
3780 **restrict polygon_info;
3797 Compute bounding box.
3799 assert(image != (Image *) NULL);
3800 assert(image->signature == MagickSignature);
3801 if (image->debug != MagickFalse)
3802 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3803 assert(draw_info != (DrawInfo *) NULL);
3804 assert(draw_info->signature == MagickSignature);
3805 assert(primitive_info != (PrimitiveInfo *) NULL);
3806 if (primitive_info->coordinates == 0)
3808 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3809 if (polygon_info == (PolygonInfo **) NULL)
3810 return(MagickFalse);
3812 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3813 if (image->debug != MagickFalse)
3814 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3815 fill=(primitive_info->method == FillToBorderMethod) ||
3816 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3817 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3818 bounds=polygon_info[0]->edges[0].bounds;
3819 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3821 p=polygon_info[0]->edges+i;
3822 if (p->bounds.x1 < bounds.x1)
3823 bounds.x1=p->bounds.x1;
3824 if (p->bounds.y1 < bounds.y1)
3825 bounds.y1=p->bounds.y1;
3826 if (p->bounds.x2 > bounds.x2)
3827 bounds.x2=p->bounds.x2;
3828 if (p->bounds.y2 > bounds.y2)
3829 bounds.y2=p->bounds.y2;
3831 bounds.x1-=(mid+1.0);
3832 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3833 image->columns ? (double) image->columns-1.0 : bounds.x1;
3834 bounds.y1-=(mid+1.0);
3835 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3836 image->rows ? (double) image->rows-1.0 : bounds.y1;
3837 bounds.x2+=(mid+1.0);
3838 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3839 image->columns ? (double) image->columns-1.0 : bounds.x2;
3840 bounds.y2+=(mid+1.0);
3841 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3842 image->rows ? (double) image->rows-1.0 : bounds.y2;
3844 start=(ssize_t) ceil(bounds.x1-0.5);
3845 stop=(ssize_t) floor(bounds.x2+0.5);
3846 image_view=AcquireCacheView(image);
3847 if (primitive_info->coordinates == 1)
3852 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3853 #pragma omp parallel for schedule(dynamic,4) shared(status)
3855 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
3869 if (status == MagickFalse)
3872 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
3874 if (q == (Quantum *) NULL)
3879 GetPixelInfo(image,&pixel);
3880 for ( ; x <= stop; x++)
3882 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3883 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3885 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3886 SetPixelInfoPixel(image,&pixel,q);
3888 q+=GetPixelChannels(image);
3890 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3891 if (sync == MagickFalse)
3894 image_view=DestroyCacheView(image_view);
3895 polygon_info=DestroyPolygonThreadSet(polygon_info);
3896 if (image->debug != MagickFalse)
3897 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3898 " end draw-polygon");
3902 Draw polygon or line.
3904 if (image->matte == MagickFalse)
3905 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3906 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3907 #pragma omp parallel for schedule(dynamic,4) shared(status)
3909 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
3912 id = GetOpenMPThreadId();
3928 if (status == MagickFalse)
3930 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3932 if (q == (Quantum *) NULL)
3937 for (x=start; x <= stop; x++)
3942 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3943 (double) x,(double) y,&stroke_alpha);
3944 if (draw_info->stroke_antialias == MagickFalse)
3946 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3947 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3949 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3950 fill_alpha=fill_alpha*fill_color.alpha;
3951 CompositePixelOver(image,&fill_color,fill_alpha,q,(MagickRealType)
3952 GetPixelAlpha(image,q),q);
3953 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3954 stroke_alpha=stroke_alpha*stroke_color.alpha;
3955 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(MagickRealType)
3956 GetPixelAlpha(image,q),q);
3957 q+=GetPixelChannels(image);
3959 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3962 image_view=DestroyCacheView(image_view);
3963 polygon_info=DestroyPolygonThreadSet(polygon_info);
3964 if (image->debug != MagickFalse)
3965 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3974 % D r a w P r i m i t i v e %
3978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3980 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
3982 % The format of the DrawPrimitive method is:
3984 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
3985 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
3987 % A description of each parameter follows:
3989 % o image: the image.
3991 % o draw_info: the draw info.
3993 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3995 % o exception: return any errors or warnings in this structure.
3999 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4025 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4026 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4027 switch (primitive_info->primitive)
4029 case PointPrimitive:
4031 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4032 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4033 methods[primitive_info->method]);
4036 case ColorPrimitive:
4038 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4039 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4040 methods[primitive_info->method]);
4043 case MattePrimitive:
4045 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4046 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4047 methods[primitive_info->method]);
4052 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4053 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4056 case ImagePrimitive:
4058 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4059 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4066 p=primitive_info[0].point;
4069 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4071 point=primitive_info[i].point;
4072 if (coordinates <= 0)
4074 coordinates=(ssize_t) primitive_info[i].coordinates;
4075 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4076 " begin open (%.20g)",(double) coordinates);
4079 point=primitive_info[i].point;
4080 if ((fabs(q.x-point.x) > MagickEpsilon) ||
4081 (fabs(q.y-point.y) > MagickEpsilon))
4082 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4083 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4085 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4086 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4089 if (coordinates > 0)
4091 if ((fabs(p.x-point.x) > MagickEpsilon) ||
4092 (fabs(p.y-point.y) > MagickEpsilon))
4093 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4094 (double) coordinates);
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4097 (double) coordinates);
4101 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4102 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4103 ExceptionInfo *exception)
4118 if (image->debug != MagickFalse)
4120 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4121 " begin draw-primitive");
4122 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4123 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4124 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4125 draw_info->affine.tx,draw_info->affine.ty);
4128 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4129 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4130 image_view=AcquireCacheView(image);
4131 switch (primitive_info->primitive)
4133 case PointPrimitive:
4141 if ((y < 0) || (y >= (ssize_t) image->rows))
4143 if ((x < 0) || (x >= (ssize_t) image->columns))
4145 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4146 if (q == (Quantum *) NULL)
4148 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4149 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4150 (MagickRealType) GetPixelAlpha(image,q),q);
4151 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4154 case ColorPrimitive:
4156 switch (primitive_info->method)
4167 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4168 if (q == (Quantum *) NULL)
4170 GetPixelInfo(image,&pixel);
4171 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4172 SetPixelInfoPixel(image,&pixel,q);
4173 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4186 virtual_pixel[CompositePixelChannel];
4188 (void) GetOneCacheViewVirtualPixel(image_view,x,y,virtual_pixel,
4190 target.red=(double) virtual_pixel[RedPixelChannel];
4191 target.green=(double) virtual_pixel[GreenPixelChannel];
4192 target.blue=(double) virtual_pixel[BluePixelChannel];
4193 target.alpha=(double) virtual_pixel[AlphaPixelChannel];
4194 for (y=0; y < (ssize_t) image->rows; y++)
4199 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4201 if (q == (Quantum *) NULL)
4203 for (x=0; x < (ssize_t) image->columns; x++)
4205 GetPixelInfoPixel(image,q,&pixel);
4206 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4208 q+=GetPixelChannels(image);
4211 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4212 SetPixelInfoPixel(image,&pixel,q);
4213 q+=GetPixelChannels(image);
4215 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4216 if (sync == MagickFalse)
4221 case FloodfillMethod:
4222 case FillToBorderMethod:
4227 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4229 if (primitive_info->method == FillToBorderMethod)
4231 target.red=(MagickRealType) draw_info->border_color.red;
4232 target.green=(MagickRealType) draw_info->border_color.green;
4233 target.blue=(MagickRealType) draw_info->border_color.blue;
4235 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4236 primitive_info->method == FloodfillMethod ? MagickFalse :
4237 MagickTrue,exception);
4248 GetPixelInfo(image,&pixel);
4249 for (y=0; y < (ssize_t) image->rows; y++)
4257 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4259 if (q == (Quantum *) NULL)
4261 for (x=0; x < (ssize_t) image->columns; x++)
4263 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4264 SetPixelInfoPixel(image,&pixel,q);
4265 q+=GetPixelChannels(image);
4267 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4268 if (sync == MagickFalse)
4276 case MattePrimitive:
4278 if (image->matte == MagickFalse)
4279 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4280 switch (primitive_info->method)
4291 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4292 if (q == (Quantum *) NULL)
4294 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4295 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4296 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4309 virtual_pixel[CompositePixelChannel];
4311 (void) GetOneCacheViewVirtualPixel(image_view,x,y,virtual_pixel,
4313 target.red=(double) virtual_pixel[RedPixelChannel];
4314 target.green=(double) virtual_pixel[GreenPixelChannel];
4315 target.blue=(double) virtual_pixel[BluePixelChannel];
4316 target.alpha=(double) virtual_pixel[AlphaPixelChannel];
4317 for (y=0; y < (ssize_t) image->rows; y++)
4325 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4327 if (q == (Quantum *) NULL)
4329 for (x=0; x < (ssize_t) image->columns; x++)
4331 GetPixelInfoPixel(image,q,&pixel);
4332 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4334 q+=GetPixelChannels(image);
4337 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4338 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4339 q+=GetPixelChannels(image);
4341 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4342 if (sync == MagickFalse)
4347 case FloodfillMethod:
4348 case FillToBorderMethod:
4356 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4358 if (primitive_info->method == FillToBorderMethod)
4360 target.red=(MagickRealType) draw_info->border_color.red;
4361 target.green=(MagickRealType) draw_info->border_color.green;
4362 target.blue=(MagickRealType) draw_info->border_color.blue;
4364 channel_mask=SetPixelChannelMask(image,AlphaChannel);
4365 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4366 primitive_info->method == FloodfillMethod ? MagickFalse :
4367 MagickTrue,exception);
4368 (void) SetPixelChannelMask(image,channel_mask);
4379 for (y=0; y < (ssize_t) image->rows; y++)
4387 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4389 if (q == (Quantum *) NULL)
4391 for (x=0; x < (ssize_t) image->columns; x++)
4393 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4394 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4395 q+=GetPixelChannels(image);
4397 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4398 if (sync == MagickFalse)
4409 geometry[MaxTextExtent];
4414 if (primitive_info->text == (char *) NULL)
4416 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4417 (void) CloneString(&clone_info->text,primitive_info->text);
4418 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4419 primitive_info->point.x,primitive_info->point.y);
4420 (void) CloneString(&clone_info->geometry,geometry);
4421 status=AnnotateImage(image,clone_info,exception);
4422 clone_info=DestroyDrawInfo(clone_info);
4425 case ImagePrimitive:
4431 composite_geometry[MaxTextExtent];
4446 if (primitive_info->text == (char *) NULL)
4448 clone_info=AcquireImageInfo();
4449 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4450 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4454 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4456 composite_image=ReadImage(clone_info,exception);
4458 clone_info=DestroyImageInfo(clone_info);
4459 if (composite_image == (Image *) NULL)
4461 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4462 NULL,(void *) NULL);
4463 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4464 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4465 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4466 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4469 geometry[MaxTextExtent];
4474 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4475 primitive_info[1].point.x,primitive_info[1].point.y);
4476 composite_image->filter=image->filter;
4477 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4480 if (composite_image->matte == MagickFalse)
4481 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4483 if (draw_info->alpha != OpaqueAlpha)
4484 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4485 SetGeometry(image,&geometry);
4486 image->gravity=draw_info->gravity;
4489 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4490 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4491 composite_image->rows,(double) geometry.x,(double) geometry.y);
4492 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4493 affine=draw_info->affine;
4494 affine.tx=(double) geometry.x;
4495 affine.ty=(double) geometry.y;
4496 composite_image->interpolate=image->interpolate;
4497 if (draw_info->compose == OverCompositeOp)
4498 (void) DrawAffineImage(image,composite_image,&affine,exception);
4500 (void) CompositeImage(image,draw_info->compose,composite_image,
4501 geometry.x,geometry.y,exception);
4502 composite_image=DestroyImage(composite_image);
4514 if (IsEventLogging() != MagickFalse)
4515 LogPrimitiveInfo(primitive_info);
4516 scale=ExpandAffine(&draw_info->affine);
4517 if ((draw_info->dash_pattern != (double *) NULL) &&
4518 (draw_info->dash_pattern[0] != 0.0) &&
4519 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
4520 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4525 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4526 clone_info->stroke_width=0.0;
4527 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4528 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4530 clone_info=DestroyDrawInfo(clone_info);
4531 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4534 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4536 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4542 Draw strokes while respecting line cap/join attributes.
4544 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4546 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4547 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4548 MagickTrue : MagickFalse;
4549 i=(ssize_t) primitive_info[0].coordinates;
4550 if ((((draw_info->linecap == RoundCap) ||
4551 (closed_path != MagickFalse)) &&
4552 (draw_info->linejoin == RoundJoin)) ||
4553 (primitive_info[i].primitive != UndefinedPrimitive))
4555 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4559 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4560 clone_info->stroke_width=0.0;
4561 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4562 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4564 clone_info=DestroyDrawInfo(clone_info);
4565 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4568 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4572 image_view=DestroyCacheView(image_view);
4573 if (image->debug != MagickFalse)
4574 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4575 return(status != 0 ? MagickTrue : MagickFalse);
4579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4583 + D r a w S t r o k e P o l y g o n %
4587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4589 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4590 % the image while respecting the line cap and join attributes.
4592 % The format of the DrawStrokePolygon method is:
4594 % MagickBooleanType DrawStrokePolygon(Image *image,
4595 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4597 % A description of each parameter follows:
4599 % o image: the image.
4601 % o draw_info: the draw info.
4603 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4608 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4609 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4617 for (i=0; i < 4; i++)
4618 linecap[i]=(*primitive_info);
4619 linecap[0].coordinates=4;
4620 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4621 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4622 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4623 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4624 linecap[4].primitive=UndefinedPrimitive;
4625 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4628 static MagickBooleanType DrawStrokePolygon(Image *image,
4629 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4630 ExceptionInfo *exception)
4642 register const PrimitiveInfo
4647 Draw stroked polygon.
4649 if (image->debug != MagickFalse)
4650 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4651 " begin draw-stroke-polygon");
4652 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4653 clone_info->fill=draw_info->stroke;
4654 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4655 clone_info->stroke_width=0.0;
4656 clone_info->fill_rule=NonZeroRule;
4658 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4660 stroke_polygon=TraceStrokePolygon(draw_info,p);
4661 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4662 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4663 q=p+p->coordinates-1;
4664 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4665 MagickTrue : MagickFalse;
4666 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4668 DrawRoundLinecap(image,draw_info,p,exception);
4669 DrawRoundLinecap(image,draw_info,q,exception);
4672 clone_info=DestroyDrawInfo(clone_info);
4673 if (image->debug != MagickFalse)
4674 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4675 " end draw-stroke-polygon");
4680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4684 % G e t A f f i n e M a t r i x %
4688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4690 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4693 % The format of the GetAffineMatrix method is:
4695 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4697 % A description of each parameter follows:
4699 % o affine_matrix: the affine matrix.
4702 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4705 assert(affine_matrix != (AffineMatrix *) NULL);
4706 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4707 affine_matrix->sx=1.0;
4708 affine_matrix->sy=1.0;
4712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4716 + G e t D r a w I n f o %
4720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4722 % GetDrawInfo() initializes draw_info to default values from image_info.
4724 % The format of the GetDrawInfo method is:
4726 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4728 % A description of each parameter follows:
4730 % o image_info: the image info..
4732 % o draw_info: the draw info.
4735 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4744 Initialize draw attributes.
4746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4747 assert(draw_info != (DrawInfo *) NULL);
4748 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4749 GetAffineMatrix(&draw_info->affine);
4750 exception=AcquireExceptionInfo();
4751 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4753 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4755 draw_info->stroke_width=1.0;
4756 draw_info->alpha=OpaqueAlpha;
4757 draw_info->fill_rule=EvenOddRule;
4758 draw_info->linecap=ButtCap;
4759 draw_info->linejoin=MiterJoin;
4760 draw_info->miterlimit=10;
4761 draw_info->decorate=NoDecoration;
4762 draw_info->pointsize=12.0;
4763 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4764 draw_info->compose=OverCompositeOp;
4765 draw_info->render=MagickTrue;
4766 draw_info->debug=IsEventLogging();
4767 if (image_info != (ImageInfo *) NULL)
4769 draw_info->stroke_antialias=image_info->antialias;
4770 if (image_info->font != (char *) NULL)
4771 draw_info->font=AcquireString(image_info->font);
4772 if (image_info->density != (char *) NULL)
4773 draw_info->density=AcquireString(image_info->density);
4774 draw_info->text_antialias=image_info->antialias;
4775 if (image_info->pointsize != 0.0)
4776 draw_info->pointsize=image_info->pointsize;
4777 draw_info->border_color=image_info->border_color;
4778 if (image_info->server_name != (char *) NULL)
4779 draw_info->server_name=AcquireString(image_info->server_name);
4780 option=GetImageOption(image_info,"encoding");
4781 if (option != (const char *) NULL)
4782 (void) CloneString(&draw_info->encoding,option);
4783 option=GetImageOption(image_info,"kerning");
4784 if (option != (const char *) NULL)
4785 draw_info->kerning=StringToDouble(option,(char **) NULL);
4786 option=GetImageOption(image_info,"interline-spacing");
4787 if (option != (const char *) NULL)
4788 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4789 option=GetImageOption(image_info,"interword-spacing");
4790 if (option != (const char *) NULL)
4791 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4792 option=GetImageOption(image_info,"direction");
4793 if (option != (const char *) NULL)
4794 draw_info->direction=(DirectionType) ParseCommandOption(
4795 MagickDirectionOptions,MagickFalse,option);
4797 draw_info->direction=UndefinedDirection;
4798 option=GetImageOption(image_info,"fill");
4799 if (option != (const char *) NULL)
4800 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4802 option=GetImageOption(image_info,"stroke");
4803 if (option != (const char *) NULL)
4804 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4806 option=GetImageOption(image_info,"strokewidth");
4807 if (option != (const char *) NULL)
4808 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4809 option=GetImageOption(image_info,"undercolor");
4810 if (option != (const char *) NULL)
4811 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4813 option=GetImageOption(image_info,"gravity");
4814 if (option != (const char *) NULL)
4815 draw_info->gravity=(GravityType) ParseCommandOption(
4816 MagickGravityOptions,MagickFalse,option);
4818 exception=DestroyExceptionInfo(exception);
4819 draw_info->signature=MagickSignature;
4823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4827 + P e r m u t a t e %
4831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4833 % Permutate() returns the permuation of the (n,k).
4835 % The format of the Permutate method is:
4837 % void Permutate(ssize_t n,ssize_t k)
4839 % A description of each parameter follows:
4847 static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
4856 for (i=k+1; i <= n; i++)
4858 for (i=1; i <= (n-k); i++)
4864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4868 + T r a c e P r i m i t i v e %
4872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4874 % TracePrimitive is a collection of methods for generating graphic
4875 % primitives such as arcs, ellipses, paths, etc.
4879 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4880 const PointInfo end,const PointInfo degrees)
4886 center.x=0.5*(end.x+start.x);
4887 center.y=0.5*(end.y+start.y);
4888 radii.x=fabs(center.x-start.x);
4889 radii.y=fabs(center.y-start.y);
4890 TraceEllipse(primitive_info,center,radii,degrees);
4893 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4894 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4895 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4910 register MagickRealType
4914 register PrimitiveInfo
4923 if ((start.x == end.x) && (start.y == end.y))
4925 TracePoint(primitive_info,end);
4928 radii.x=fabs(arc.x);
4929 radii.y=fabs(arc.y);
4930 if ((radii.x == 0.0) || (radii.y == 0.0))
4932 TraceLine(primitive_info,start,end);
4935 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4936 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4937 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4938 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4939 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4941 if (delta < MagickEpsilon)
4943 TraceLine(primitive_info,start,end);
4948 radii.x*=sqrt((double) delta);
4949 radii.y*=sqrt((double) delta);
4951 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4952 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4953 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4954 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4955 alpha=points[1].x-points[0].x;
4956 beta=points[1].y-points[0].y;
4957 factor=1.0/(alpha*alpha+beta*beta)-0.25;
4962 factor=sqrt((double) factor);
4963 if (sweep == large_arc)
4966 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4967 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4968 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4969 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4970 if ((theta < 0.0) && (sweep != MagickFalse))
4971 theta+=(MagickRealType) (2.0*MagickPI);
4973 if ((theta > 0.0) && (sweep == MagickFalse))
4974 theta-=(MagickRealType) (2.0*MagickPI);
4975 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
4978 for (i=0; i < (ssize_t) arc_segments; i++)
4980 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4981 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4982 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4983 sin(fmod((double) beta,DegreesToRadians(360.0)));
4984 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
4985 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
4986 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4987 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
4988 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
4989 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4990 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
4991 theta/arc_segments),DegreesToRadians(360.0))));
4992 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
4993 theta/arc_segments),DegreesToRadians(360.0))));
4994 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
4995 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4996 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
4997 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
4998 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
4999 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5000 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5002 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5004 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5006 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5008 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5010 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5012 if (i == (ssize_t) (arc_segments-1))
5017 primitive_info->coordinates=(size_t) (p-primitive_info);
5018 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5020 p->primitive=primitive_info->primitive;
5025 static void TraceBezier(PrimitiveInfo *primitive_info,
5026 const size_t number_coordinates)
5038 register PrimitiveInfo
5050 Allocate coeficients.
5052 quantum=number_coordinates;
5053 for (i=0; i < (ssize_t) number_coordinates; i++)
5055 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5057 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5058 if (alpha > (MagickRealType) quantum)
5059 quantum=(size_t) alpha;
5060 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5061 if (alpha > (MagickRealType) quantum)
5062 quantum=(size_t) alpha;
5065 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5066 (double) BezierQuantum);
5067 control_points=quantum*number_coordinates;
5068 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5069 number_coordinates,sizeof(*coefficients));
5070 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5072 if ((coefficients == (MagickRealType *) NULL) ||
5073 (points == (PointInfo *) NULL))
5074 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5076 Compute bezier points.
5078 end=primitive_info[number_coordinates-1].point;
5079 for (i=0; i < (ssize_t) number_coordinates; i++)
5080 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5082 for (i=0; i < (ssize_t) control_points; i++)
5087 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5088 for (j=0; j < (ssize_t) number_coordinates; j++)
5090 point.x+=alpha*coefficients[j]*p->point.x;
5091 point.y+=alpha*coefficients[j]*p->point.y;
5092 alpha*=weight/(1.0-weight);
5096 weight+=1.0/control_points;
5099 Bezier curves are just short segmented polys.
5102 for (i=0; i < (ssize_t) control_points; i++)
5104 TracePoint(p,points[i]);
5109 primitive_info->coordinates=(size_t) (p-primitive_info);
5110 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5112 p->primitive=primitive_info->primitive;
5115 points=(PointInfo *) RelinquishMagickMemory(points);
5116 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5119 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5120 const PointInfo end)
5131 alpha=end.x-start.x;
5133 radius=hypot((double) alpha,(double) beta);
5134 offset.x=(double) radius;
5135 offset.y=(double) radius;
5138 TraceEllipse(primitive_info,start,offset,degrees);
5141 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5142 const PointInfo stop,const PointInfo degrees)
5153 register PrimitiveInfo
5160 Ellipses are just short segmented polys.
5162 if ((stop.x == 0.0) && (stop.y == 0.0))
5164 TracePoint(primitive_info,start);
5167 delta=2.0/MagickMax(stop.x,stop.y);
5168 step=(MagickRealType) (MagickPI/8.0);
5169 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5170 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5171 angle.x=DegreesToRadians(degrees.x);
5173 while (y < degrees.x)
5175 angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5176 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5178 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5179 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5180 TracePoint(p,point);
5183 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5184 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5185 TracePoint(p,point);
5187 primitive_info->coordinates=(size_t) (p-primitive_info);
5188 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5190 p->primitive=primitive_info->primitive;
5195 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5196 const PointInfo end)
5198 TracePoint(primitive_info,start);
5199 if ((fabs(start.x-end.x) <= MagickEpsilon) &&
5200 (fabs(start.y-end.y) <= MagickEpsilon))
5202 primitive_info->primitive=PointPrimitive;
5203 primitive_info->coordinates=1;
5206 TracePoint(primitive_info+1,end);
5207 (primitive_info+1)->primitive=primitive_info->primitive;
5208 primitive_info->coordinates=2;
5211 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5214 token[MaxTextExtent];
5236 register PrimitiveInfo
5251 number_coordinates=0;
5253 primitive_type=primitive_info->primitive;
5255 for (p=path; *p != '\0'; )
5257 while (isspace((int) ((unsigned char) *p)) != 0)
5261 last_attribute=attribute;
5262 attribute=(int) (*p++);
5283 GetMagickToken(p,&p,token);
5285 GetMagickToken(p,&p,token);
5286 arc.x=StringToDouble(token,(char **) NULL);
5287 GetMagickToken(p,&p,token);
5289 GetMagickToken(p,&p,token);
5290 arc.y=StringToDouble(token,(char **) NULL);
5291 GetMagickToken(p,&p,token);
5293 GetMagickToken(p,&p,token);
5294 angle=StringToDouble(token,(char **) NULL);
5295 GetMagickToken(p,&p,token);
5297 GetMagickToken(p,&p,token);
5298 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5299 GetMagickToken(p,&p,token);
5301 GetMagickToken(p,&p,token);
5302 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5303 GetMagickToken(p,&p,token);
5305 GetMagickToken(p,&p,token);
5306 x=StringToDouble(token,(char **) NULL);
5307 GetMagickToken(p,&p,token);
5309 GetMagickToken(p,&p,token);
5310 y=StringToDouble(token,(char **) NULL);
5311 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5312 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5313 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5316 while (isspace((int) ((unsigned char) *p)) != 0)
5320 } while (IsPoint(p) != MagickFalse);
5327 Compute bezier points.
5332 for (i=1; i < 4; i++)
5334 GetMagickToken(p,&p,token);
5336 GetMagickToken(p,&p,token);
5337 x=StringToDouble(token,(char **) NULL);
5338 GetMagickToken(p,&p,token);
5340 GetMagickToken(p,&p,token);
5341 y=StringToDouble(token,(char **) NULL);
5342 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5343 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5346 for (i=0; i < 4; i++)
5347 (q+i)->point=points[i];
5351 } while (IsPoint(p) != MagickFalse);
5359 GetMagickToken(p,&p,token);
5361 GetMagickToken(p,&p,token);
5362 x=StringToDouble(token,(char **) NULL);
5363 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5364 TracePoint(q,point);
5366 } while (IsPoint(p) != MagickFalse);
5374 GetMagickToken(p,&p,token);
5376 GetMagickToken(p,&p,token);
5377 x=StringToDouble(token,(char **) NULL);
5378 GetMagickToken(p,&p,token);
5380 GetMagickToken(p,&p,token);
5381 y=StringToDouble(token,(char **) NULL);
5382 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5383 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5384 TracePoint(q,point);
5386 } while (IsPoint(p) != MagickFalse);
5392 if (q != primitive_info)
5394 primitive_info->coordinates=(size_t) (q-primitive_info);
5395 number_coordinates+=primitive_info->coordinates;
5401 GetMagickToken(p,&p,token);
5403 GetMagickToken(p,&p,token);
5404 x=StringToDouble(token,(char **) NULL);
5405 GetMagickToken(p,&p,token);
5407 GetMagickToken(p,&p,token);
5408 y=StringToDouble(token,(char **) NULL);
5409 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5410 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5414 TracePoint(q,point);
5416 if ((i != 0) && (attribute == (int) 'M'))
5418 TracePoint(q,point);
5421 } while (IsPoint(p) != MagickFalse);
5428 Compute bezier points.
5433 for (i=1; i < 3; i++)
5435 GetMagickToken(p,&p,token);
5437 GetMagickToken(p,&p,token);
5438 x=StringToDouble(token,(char **) NULL);
5439 GetMagickToken(p,&p,token);
5441 GetMagickToken(p,&p,token);
5442 y=StringToDouble(token,(char **) NULL);
5445 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5446 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5449 for (i=0; i < 3; i++)
5450 (q+i)->point=points[i];
5454 } while (IsPoint(p) != MagickFalse);
5461 Compute bezier points.
5465 points[0]=points[3];
5466 points[1].x=2.0*points[3].x-points[2].x;
5467 points[1].y=2.0*points[3].y-points[2].y;
5468 for (i=2; i < 4; i++)
5470 GetMagickToken(p,&p,token);
5472 GetMagickToken(p,&p,token);
5473 x=StringToDouble(token,(char **) NULL);
5474 GetMagickToken(p,&p,token);
5476 GetMagickToken(p,&p,token);
5477 y=StringToDouble(token,(char **) NULL);
5480 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5481 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5484 if (strchr("CcSs",last_attribute) == (char *) NULL)
5486 points[0]=points[2];
5487 points[1]=points[3];
5489 for (i=0; i < 4; i++)
5490 (q+i)->point=points[i];
5494 } while (IsPoint(p) != MagickFalse);
5501 Compute bezier points.
5505 points[0]=points[2];
5506 points[1].x=2.0*points[2].x-points[1].x;
5507 points[1].y=2.0*points[2].y-points[1].y;
5508 for (i=2; i < 3; i++)
5510 GetMagickToken(p,&p,token);
5512 GetMagickToken(p,&p,token);
5513 x=StringToDouble(token,(char **) NULL);
5514 GetMagickToken(p,&p,token);
5516 GetMagickToken(p,&p,token);
5517 y=StringToDouble(token,(char **) NULL);
5518 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5519 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5522 if (strchr("QqTt",last_attribute) == (char *) NULL)
5524 points[0]=points[2];
5525 points[1]=points[3];
5527 for (i=0; i < 3; i++)
5528 (q+i)->point=points[i];
5532 } while (IsPoint(p) != MagickFalse);
5540 GetMagickToken(p,&p,token);
5542 GetMagickToken(p,&p,token);
5543 y=StringToDouble(token,(char **) NULL);
5544 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5545 TracePoint(q,point);
5547 } while (IsPoint(p) != MagickFalse);
5554 TracePoint(q,point);
5556 primitive_info->coordinates=(size_t) (q-primitive_info);
5557 number_coordinates+=primitive_info->coordinates;
5564 if (isalpha((int) ((unsigned char) attribute)) != 0)
5565 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5571 primitive_info->coordinates=(size_t) (q-primitive_info);
5572 number_coordinates+=primitive_info->coordinates;
5573 for (i=0; i < (ssize_t) number_coordinates; i++)
5576 q->primitive=primitive_type;
5578 q->method=FillToBorderMethod;
5581 return(number_coordinates);
5584 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5585 const PointInfo end)
5590 register PrimitiveInfo
5597 TracePoint(p,start);
5601 TracePoint(p,point);
5607 TracePoint(p,point);
5609 TracePoint(p,start);
5611 primitive_info->coordinates=(size_t) (p-primitive_info);
5612 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5614 p->primitive=primitive_info->primitive;
5619 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5620 const PointInfo start,const PointInfo end,PointInfo arc)
5627 register PrimitiveInfo
5634 offset.x=fabs(end.x-start.x);
5635 offset.y=fabs(end.y-start.y);
5636 if (arc.x > (0.5*offset.x))
5638 if (arc.y > (0.5*offset.y))
5640 point.x=start.x+offset.x-arc.x;
5641 point.y=start.y+arc.y;
5644 TraceEllipse(p,point,arc,degrees);
5646 point.x=start.x+offset.x-arc.x;
5647 point.y=start.y+offset.y-arc.y;
5650 TraceEllipse(p,point,arc,degrees);
5652 point.x=start.x+arc.x;
5653 point.y=start.y+offset.y-arc.y;
5656 TraceEllipse(p,point,arc,degrees);
5658 point.x=start.x+arc.x;
5659 point.y=start.y+arc.y;
5662 TraceEllipse(p,point,arc,degrees);
5664 TracePoint(p,primitive_info->point);
5666 primitive_info->coordinates=(size_t) (p-primitive_info);
5667 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5669 p->primitive=primitive_info->primitive;
5674 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5675 const size_t number_vertices,const MagickRealType offset)
5680 register MagickRealType
5692 for (i=1; i < (ssize_t) number_vertices; i++)
5694 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5695 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5696 if ((fabs((double) dx) >= MagickEpsilon) ||
5697 (fabs((double) dy) >= MagickEpsilon))
5700 if (i == (ssize_t) number_vertices)
5701 i=(ssize_t) number_vertices-1L;
5702 distance=hypot((double) dx,(double) dy);
5703 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5704 dx*(distance+offset)/distance);
5705 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5706 dy*(distance+offset)/distance);
5707 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5709 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5710 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5711 if ((fabs((double) dx) >= MagickEpsilon) ||
5712 (fabs((double) dy) >= MagickEpsilon))
5715 distance=hypot((double) dx,(double) dy);
5716 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5717 dx*(distance+offset)/distance);
5718 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5719 dy*(distance+offset)/distance);
5722 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5723 const PrimitiveInfo *primitive_info)
5725 typedef struct _LineSegment
5777 number_vertices=primitive_info->coordinates;
5778 max_strokes=2*number_vertices+6*BezierQuantum+360;
5779 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5781 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5783 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5784 number_vertices+2UL,sizeof(*polygon_primitive));
5785 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5786 (polygon_primitive == (PrimitiveInfo *) NULL))
5787 return((PrimitiveInfo *) NULL);
5788 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5789 number_vertices*sizeof(*polygon_primitive));
5791 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5792 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5793 MagickTrue : MagickFalse;
5794 if ((draw_info->linejoin == RoundJoin) ||
5795 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5797 polygon_primitive[number_vertices]=primitive_info[1];
5800 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5802 Compute the slope for the first line segment, p.
5806 for (n=1; n < (ssize_t) number_vertices; n++)
5808 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5809 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5810 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5813 if (n == (ssize_t) number_vertices)
5814 n=(ssize_t) number_vertices-1L;
5816 inverse_slope.p=0.0;
5817 if (fabs(dx.p) <= MagickEpsilon)
5820 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5822 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5825 if (fabs(dy.p) <= MagickEpsilon)
5828 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5830 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5835 inverse_slope.p=(-1.0/slope.p);
5837 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5838 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5840 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5841 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5842 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5843 offset.y=(double) (offset.x*inverse_slope.p);
5844 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5846 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5847 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5848 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5849 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5850 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5851 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5852 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5853 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5857 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5858 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5859 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5860 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5861 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5862 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5863 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5864 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5867 Create strokes for the line join attribute: bevel, miter, round.
5871 path_q[p++]=box_q[0];
5872 path_p[q++]=box_p[0];
5873 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5876 Compute the slope for this line segment, q.
5878 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5879 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5880 dot_product=dx.q*dx.q+dy.q*dy.q;
5881 if (dot_product < 0.25)
5884 inverse_slope.q=0.0;
5885 if (fabs(dx.q) < MagickEpsilon)
5888 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5890 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5893 if (fabs(dy.q) <= MagickEpsilon)
5896 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5898 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5903 inverse_slope.q=(-1.0/slope.q);
5905 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5906 offset.y=(double) (offset.x*inverse_slope.q);
5907 dot_product=dy.q*offset.x-dx.q*offset.y;
5908 if (dot_product > 0.0)
5910 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5911 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5912 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5913 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5914 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5915 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5916 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5917 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5921 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5922 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5923 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5924 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5925 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5926 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5927 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5928 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5930 if (fabs((double) (slope.p-slope.q)) <= MagickEpsilon)
5937 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5938 box_p[3].y)/(slope.p-slope.q));
5939 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5940 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5941 box_q[3].y)/(slope.p-slope.q));
5942 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5944 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5946 max_strokes+=6*BezierQuantum+360;
5947 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5949 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5951 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5953 polygon_primitive=(PrimitiveInfo *)
5954 RelinquishMagickMemory(polygon_primitive);
5955 return((PrimitiveInfo *) NULL);
5958 dot_product=dx.q*dy.p-dx.p*dy.q;
5959 if (dot_product <= 0.0)
5960 switch (draw_info->linejoin)
5964 path_q[q++]=box_q[1];
5965 path_q[q++]=box_q[2];
5966 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5967 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5968 if (dot_product <= miterlimit)
5969 path_p[p++]=box_p[4];
5972 path_p[p++]=box_p[1];
5973 path_p[p++]=box_p[2];
5979 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5980 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5981 if (dot_product <= miterlimit)
5983 path_q[q++]=box_q[4];
5984 path_p[p++]=box_p[4];
5988 path_q[q++]=box_q[1];
5989 path_q[q++]=box_q[2];
5990 path_p[p++]=box_p[1];
5991 path_p[p++]=box_p[2];
5997 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5998 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5999 if (dot_product <= miterlimit)
6000 path_p[p++]=box_p[4];
6003 path_p[p++]=box_p[1];
6004 path_p[p++]=box_p[2];
6006 center=polygon_primitive[n].point;
6007 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6008 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6009 if (theta.q < theta.p)
6010 theta.q+=(MagickRealType) (2.0*MagickPI);
6011 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6012 (2.0*sqrt((double) (1.0/mid)))));
6013 path_q[q].x=box_q[1].x;
6014 path_q[q].y=box_q[1].y;
6016 for (j=1; j < (ssize_t) arc_segments; j++)
6018 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6019 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6020 (theta.p+delta_theta),DegreesToRadians(360.0))));
6021 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6022 (theta.p+delta_theta),DegreesToRadians(360.0))));
6025 path_q[q++]=box_q[2];
6032 switch (draw_info->linejoin)
6036 path_p[p++]=box_p[1];
6037 path_p[p++]=box_p[2];
6038 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6039 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6040 if (dot_product <= miterlimit)
6041 path_q[q++]=box_q[4];
6044 path_q[q++]=box_q[1];
6045 path_q[q++]=box_q[2];
6051 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6052 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6053 if (dot_product <= miterlimit)
6055 path_q[q++]=box_q[4];
6056 path_p[p++]=box_p[4];
6060 path_q[q++]=box_q[1];
6061 path_q[q++]=box_q[2];
6062 path_p[p++]=box_p[1];
6063 path_p[p++]=box_p[2];
6069 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6070 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6071 if (dot_product <= miterlimit)
6072 path_q[q++]=box_q[4];
6075 path_q[q++]=box_q[1];
6076 path_q[q++]=box_q[2];
6078 center=polygon_primitive[n].point;
6079 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6080 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6081 if (theta.p < theta.q)
6082 theta.p+=(MagickRealType) (2.0*MagickPI);
6083 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6084 (2.0*sqrt((double) (1.0/mid)))));
6085 path_p[p++]=box_p[1];
6086 for (j=1; j < (ssize_t) arc_segments; j++)
6088 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6089 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6090 (theta.p+delta_theta),DegreesToRadians(360.0))));
6091 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6092 (theta.p+delta_theta),DegreesToRadians(360.0))));
6095 path_p[p++]=box_p[2];
6102 inverse_slope.p=inverse_slope.q;
6111 path_p[p++]=box_p[1];
6112 path_q[q++]=box_q[1];
6114 Trace stroked polygon.
6116 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6117 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6118 if (stroke_polygon != (PrimitiveInfo *) NULL)
6120 for (i=0; i < (ssize_t) p; i++)
6122 stroke_polygon[i]=polygon_primitive[0];
6123 stroke_polygon[i].point=path_p[i];
6125 if (closed_path != MagickFalse)
6127 stroke_polygon[i]=polygon_primitive[0];
6128 stroke_polygon[i].point=stroke_polygon[0].point;
6131 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6133 stroke_polygon[i]=polygon_primitive[0];
6134 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6136 if (closed_path != MagickFalse)
6138 stroke_polygon[i]=polygon_primitive[0];
6139 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6142 stroke_polygon[i]=polygon_primitive[0];
6143 stroke_polygon[i].point=stroke_polygon[0].point;
6145 stroke_polygon[i].primitive=UndefinedPrimitive;
6146 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6148 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6149 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6150 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6151 return(stroke_polygon);