2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/channel.h"
55 #include "MagickCore/color.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/draw.h"
61 #include "MagickCore/draw-private.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/geometry.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/log.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/paint.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/pixel-private.h"
76 #include "MagickCore/property.h"
77 #include "MagickCore/resample.h"
78 #include "MagickCore/resample-private.h"
79 #include "MagickCore/resource_.h"
80 #include "MagickCore/string_.h"
81 #include "MagickCore/string-private.h"
82 #include "MagickCore/thread-private.h"
83 #include "MagickCore/token.h"
84 #include "MagickCore/transform.h"
85 #include "MagickCore/utility.h"
90 #define BezierQuantum 200
95 typedef struct _EdgeInfo
119 typedef struct _ElementInfo
129 typedef struct _PolygonInfo
147 typedef struct _PathInfo
157 Forward declarations.
159 static MagickBooleanType
160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
167 TracePath(PrimitiveInfo *,const char *);
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
172 const double,const MagickBooleanType,const MagickBooleanType),
173 TraceBezier(PrimitiveInfo *,const size_t),
174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,
177 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
179 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
181 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % A c q u i r e D r a w I n f o %
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
196 % The format of the AcquireDrawInfo method is:
198 % DrawInfo *AcquireDrawInfo(void)
201 MagickExport DrawInfo *AcquireDrawInfo(void)
206 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
207 if (draw_info == (DrawInfo *) NULL)
208 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
209 GetDrawInfo((ImageInfo *) NULL,draw_info);
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % C l o n e D r a w I n f o %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
225 % is specified, a new DrawInfo structure is created initialized to default
228 % The format of the CloneDrawInfo method is:
230 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
231 % const DrawInfo *draw_info)
233 % A description of each parameter follows:
235 % o image_info: the image info.
237 % o draw_info: the draw info.
240 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
241 const DrawInfo *draw_info)
249 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
250 if (clone_info == (DrawInfo *) NULL)
251 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
252 GetDrawInfo(image_info,clone_info);
253 if (draw_info == (DrawInfo *) NULL)
255 exception=AcquireExceptionInfo();
256 (void) CloneString(&clone_info->primitive,draw_info->primitive);
257 (void) CloneString(&clone_info->geometry,draw_info->geometry);
258 clone_info->viewbox=draw_info->viewbox;
259 clone_info->affine=draw_info->affine;
260 clone_info->gravity=draw_info->gravity;
261 clone_info->fill=draw_info->fill;
262 clone_info->stroke=draw_info->stroke;
263 clone_info->stroke_width=draw_info->stroke_width;
264 if (draw_info->fill_pattern != (Image *) NULL)
265 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
267 if (draw_info->stroke_pattern != (Image *) NULL)
268 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
269 MagickTrue,exception);
270 clone_info->stroke_antialias=draw_info->stroke_antialias;
271 clone_info->text_antialias=draw_info->text_antialias;
272 clone_info->fill_rule=draw_info->fill_rule;
273 clone_info->linecap=draw_info->linecap;
274 clone_info->linejoin=draw_info->linejoin;
275 clone_info->miterlimit=draw_info->miterlimit;
276 clone_info->dash_offset=draw_info->dash_offset;
277 clone_info->decorate=draw_info->decorate;
278 clone_info->compose=draw_info->compose;
279 (void) CloneString(&clone_info->text,draw_info->text);
280 (void) CloneString(&clone_info->font,draw_info->font);
281 (void) CloneString(&clone_info->metrics,draw_info->metrics);
282 (void) CloneString(&clone_info->family,draw_info->family);
283 clone_info->style=draw_info->style;
284 clone_info->stretch=draw_info->stretch;
285 clone_info->weight=draw_info->weight;
286 (void) CloneString(&clone_info->encoding,draw_info->encoding);
287 clone_info->pointsize=draw_info->pointsize;
288 clone_info->kerning=draw_info->kerning;
289 clone_info->interline_spacing=draw_info->interline_spacing;
290 clone_info->interword_spacing=draw_info->interword_spacing;
291 clone_info->direction=draw_info->direction;
292 (void) CloneString(&clone_info->density,draw_info->density);
293 clone_info->align=draw_info->align;
294 clone_info->undercolor=draw_info->undercolor;
295 clone_info->border_color=draw_info->border_color;
296 (void) CloneString(&clone_info->server_name,draw_info->server_name);
297 if (draw_info->dash_pattern != (double *) NULL)
302 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
303 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
304 sizeof(*clone_info->dash_pattern));
305 if (clone_info->dash_pattern == (double *) NULL)
306 ThrowFatalException(ResourceLimitFatalError,
307 "UnableToAllocateDashPattern");
308 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
309 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
311 clone_info->gradient=draw_info->gradient;
312 if (draw_info->gradient.stops != (StopInfo *) NULL)
317 number_stops=clone_info->gradient.number_stops;
318 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
319 number_stops,sizeof(*clone_info->gradient.stops));
320 if (clone_info->gradient.stops == (StopInfo *) NULL)
321 ThrowFatalException(ResourceLimitFatalError,
322 "UnableToAllocateDashPattern");
323 (void) CopyMagickMemory(clone_info->gradient.stops,
324 draw_info->gradient.stops,(size_t) number_stops*
325 sizeof(*clone_info->gradient.stops));
327 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
328 clone_info->bounds=draw_info->bounds;
329 clone_info->clip_units=draw_info->clip_units;
330 clone_info->render=draw_info->render;
331 clone_info->alpha=draw_info->alpha;
332 clone_info->element_reference=draw_info->element_reference;
333 clone_info->debug=IsEventLogging();
334 exception=DestroyExceptionInfo(exception);
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 + C o n v e r t P a t h T o P o l y g o n %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % ConvertPathToPolygon() converts a path to the more efficient sorted
352 % The format of the ConvertPathToPolygon method is:
354 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
355 % const PathInfo *path_info)
357 % A description of each parameter follows:
359 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
360 % rendering form of type PolygonInfo.
362 % o draw_info: Specifies a pointer to an DrawInfo structure.
364 % o path_info: Specifies a pointer to an PathInfo structure.
369 #if defined(__cplusplus) || defined(c_plusplus)
373 static int CompareEdges(const void *x,const void *y)
375 register const EdgeInfo
382 p=(const EdgeInfo *) x;
383 q=(const EdgeInfo *) y;
384 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
386 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
388 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
390 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
392 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
393 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
398 #if defined(__cplusplus) || defined(c_plusplus)
402 static void LogPolygonInfo(const PolygonInfo *polygon_info)
411 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
412 p=polygon_info->edges;
413 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
415 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
417 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
418 p->direction != MagickFalse ? "down" : "up");
419 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
420 p->ghostline != MagickFalse ? "transparent" : "opaque");
421 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
422 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
423 p->bounds.x2,p->bounds.y2);
424 for (j=0; j < (ssize_t) p->number_points; j++)
425 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
426 p->points[j].x,p->points[j].y);
429 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
432 static void ReversePoints(PointInfo *points,const size_t number_points)
440 for (i=0; i < (ssize_t) (number_points >> 1); i++)
443 points[i]=points[number_points-(i+1)];
444 points[number_points-(i+1)]=point;
448 static PolygonInfo *ConvertPathToPolygon(
449 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
478 Convert a path to the more efficient sorted rendering form.
480 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
481 if (polygon_info == (PolygonInfo *) NULL)
482 return((PolygonInfo *) NULL);
484 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
485 sizeof(*polygon_info->edges));
486 if (polygon_info->edges == (EdgeInfo *) NULL)
487 return((PolygonInfo *) NULL);
490 ghostline=MagickFalse;
493 points=(PointInfo *) NULL;
494 (void) ResetMagickMemory(&point,0,sizeof(point));
495 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
496 for (i=0; path_info[i].code != EndCode; i++)
498 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
499 (path_info[i].code == GhostlineCode))
504 if ((points != (PointInfo *) NULL) && (n >= 2))
506 if (edge == number_edges)
509 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
510 polygon_info->edges,(size_t) number_edges,
511 sizeof(*polygon_info->edges));
512 if (polygon_info->edges == (EdgeInfo *) NULL)
513 return((PolygonInfo *) NULL);
515 polygon_info->edges[edge].number_points=(size_t) n;
516 polygon_info->edges[edge].scanline=(-1.0);
517 polygon_info->edges[edge].highwater=0;
518 polygon_info->edges[edge].ghostline=ghostline;
519 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
521 ReversePoints(points,(size_t) n);
522 polygon_info->edges[edge].points=points;
523 polygon_info->edges[edge].bounds=bounds;
524 polygon_info->edges[edge].bounds.y1=points[0].y;
525 polygon_info->edges[edge].bounds.y2=points[n-1].y;
526 points=(PointInfo *) NULL;
527 ghostline=MagickFalse;
530 if (points == (PointInfo *) NULL)
533 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
535 if (points == (PointInfo *) NULL)
536 return((PolygonInfo *) NULL);
538 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
539 point=path_info[i].point;
550 next_direction=((path_info[i].point.y > point.y) ||
551 ((path_info[i].point.y == point.y) &&
552 (path_info[i].point.x > point.x))) ? 1 : -1;
553 if ((points != (PointInfo *) NULL) && (direction != 0) &&
554 (direction != next_direction))
560 if (edge == number_edges)
563 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
564 polygon_info->edges,(size_t) number_edges,
565 sizeof(*polygon_info->edges));
566 if (polygon_info->edges == (EdgeInfo *) NULL)
567 return((PolygonInfo *) NULL);
569 polygon_info->edges[edge].number_points=(size_t) n;
570 polygon_info->edges[edge].scanline=(-1.0);
571 polygon_info->edges[edge].highwater=0;
572 polygon_info->edges[edge].ghostline=ghostline;
573 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
575 ReversePoints(points,(size_t) n);
576 polygon_info->edges[edge].points=points;
577 polygon_info->edges[edge].bounds=bounds;
578 polygon_info->edges[edge].bounds.y1=points[0].y;
579 polygon_info->edges[edge].bounds.y2=points[n-1].y;
581 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
583 if (points == (PointInfo *) NULL)
584 return((PolygonInfo *) NULL);
586 ghostline=MagickFalse;
592 direction=next_direction;
593 if (points == (PointInfo *) NULL)
595 if (n == (ssize_t) number_points)
598 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
600 if (points == (PointInfo *) NULL)
601 return((PolygonInfo *) NULL);
603 point=path_info[i].point;
605 if (point.x < bounds.x1)
607 if (point.x > bounds.x2)
611 if (points != (PointInfo *) NULL)
614 points=(PointInfo *) RelinquishMagickMemory(points);
617 if (edge == number_edges)
620 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
621 polygon_info->edges,(size_t) number_edges,
622 sizeof(*polygon_info->edges));
623 if (polygon_info->edges == (EdgeInfo *) NULL)
624 return((PolygonInfo *) NULL);
626 polygon_info->edges[edge].number_points=(size_t) n;
627 polygon_info->edges[edge].scanline=(-1.0);
628 polygon_info->edges[edge].highwater=0;
629 polygon_info->edges[edge].ghostline=ghostline;
630 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
632 ReversePoints(points,(size_t) n);
633 polygon_info->edges[edge].points=points;
634 polygon_info->edges[edge].bounds=bounds;
635 polygon_info->edges[edge].bounds.y1=points[0].y;
636 polygon_info->edges[edge].bounds.y2=points[n-1].y;
637 ghostline=MagickFalse;
641 polygon_info->number_edges=edge;
642 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
643 sizeof(*polygon_info->edges),CompareEdges);
644 if (IsEventLogging() != MagickFalse)
645 LogPolygonInfo(polygon_info);
646 return(polygon_info);
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 + C o n v e r t P r i m i t i v e T o P a t h %
658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
663 % The format of the ConvertPrimitiveToPath method is:
665 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
666 % const PrimitiveInfo *primitive_info)
668 % A description of each parameter follows:
670 % o Method ConvertPrimitiveToPath returns a vector path structure of type
673 % o draw_info: a structure of type DrawInfo.
675 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
680 static void LogPathInfo(const PathInfo *path_info)
682 register const PathInfo
685 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
686 for (p=path_info; p->code != EndCode; p++)
687 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
688 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
689 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
690 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
692 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
695 static PathInfo *ConvertPrimitiveToPath(
696 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
717 Converts a PrimitiveInfo structure into a vector path structure.
719 switch (primitive_info->primitive)
726 return((PathInfo *) NULL);
730 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
731 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
733 if (path_info == (PathInfo *) NULL)
734 return((PathInfo *) NULL);
742 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
745 if (coordinates <= 0)
747 coordinates=(ssize_t) primitive_info[i].coordinates;
748 p=primitive_info[i].point;
754 Eliminate duplicate points.
756 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
757 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
759 path_info[n].code=code;
760 path_info[n].point=primitive_info[i].point;
761 q=primitive_info[i].point;
766 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
767 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
770 Mark the p point as open if it does not match the q.
772 path_info[start].code=OpenCode;
773 path_info[n].code=GhostlineCode;
774 path_info[n].point=primitive_info[i].point;
776 path_info[n].code=LineToCode;
777 path_info[n].point=p;
780 path_info[n].code=EndCode;
781 path_info[n].point.x=0.0;
782 path_info[n].point.y=0.0;
783 if (IsEventLogging() != MagickFalse)
784 LogPathInfo(path_info);
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % D e s t r o y D r a w I n f o %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
802 % The format of the DestroyDrawInfo method is:
804 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
806 % A description of each parameter follows:
808 % o draw_info: the draw info.
811 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
813 if (draw_info->debug != MagickFalse)
814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
815 assert(draw_info != (DrawInfo *) NULL);
816 assert(draw_info->signature == MagickSignature);
817 if (draw_info->primitive != (char *) NULL)
818 draw_info->primitive=DestroyString(draw_info->primitive);
819 if (draw_info->text != (char *) NULL)
820 draw_info->text=DestroyString(draw_info->text);
821 if (draw_info->geometry != (char *) NULL)
822 draw_info->geometry=DestroyString(draw_info->geometry);
823 if (draw_info->fill_pattern != (Image *) NULL)
824 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
825 if (draw_info->stroke_pattern != (Image *) NULL)
826 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
827 if (draw_info->font != (char *) NULL)
828 draw_info->font=DestroyString(draw_info->font);
829 if (draw_info->metrics != (char *) NULL)
830 draw_info->metrics=DestroyString(draw_info->metrics);
831 if (draw_info->family != (char *) NULL)
832 draw_info->family=DestroyString(draw_info->family);
833 if (draw_info->encoding != (char *) NULL)
834 draw_info->encoding=DestroyString(draw_info->encoding);
835 if (draw_info->density != (char *) NULL)
836 draw_info->density=DestroyString(draw_info->density);
837 if (draw_info->server_name != (char *) NULL)
838 draw_info->server_name=(char *)
839 RelinquishMagickMemory(draw_info->server_name);
840 if (draw_info->dash_pattern != (double *) NULL)
841 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
842 draw_info->dash_pattern);
843 if (draw_info->gradient.stops != (StopInfo *) NULL)
844 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
845 draw_info->gradient.stops);
846 if (draw_info->clip_mask != (char *) NULL)
847 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
848 draw_info->signature=(~MagickSignature);
849 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 + D e s t r o y E d g e %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % DestroyEdge() destroys the specified polygon edge.
866 % The format of the DestroyEdge method is:
868 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
870 % A description of each parameter follows:
872 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
874 % o edge: the polygon edge number to destroy.
877 static size_t DestroyEdge(PolygonInfo *polygon_info,
880 assert(edge < polygon_info->number_edges);
881 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
882 polygon_info->edges[edge].points);
883 polygon_info->number_edges--;
884 if (edge < polygon_info->number_edges)
885 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
886 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
887 return(polygon_info->number_edges);
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 + D e s t r o y P o l y g o n I n f o %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
903 % The format of the DestroyPolygonInfo method is:
905 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
907 % A description of each parameter follows:
909 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
912 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
917 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
918 polygon_info->edges[i].points=(PointInfo *)
919 RelinquishMagickMemory(polygon_info->edges[i].points);
920 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
921 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929 % D r a w A f f i n e I m a g e %
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 % DrawAffineImage() composites the source over the destination image as
936 % dictated by the affine transform.
938 % The format of the DrawAffineImage method is:
940 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
941 % const AffineMatrix *affine,ExceptionInfo *exception)
943 % A description of each parameter follows:
945 % o image: the image.
947 % o source: the source image.
949 % o affine: the affine transform.
951 % o exception: return any errors or warnings in this structure.
955 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
956 const double y,const SegmentInfo *edge)
969 Determine left and right edges.
971 inverse_edge.x1=edge->x1;
972 inverse_edge.y1=edge->y1;
973 inverse_edge.x2=edge->x2;
974 inverse_edge.y2=edge->y2;
975 z=affine->ry*y+affine->tx;
976 if (affine->sx >= MagickEpsilon)
978 intercept=(-z/affine->sx);
980 if (x > inverse_edge.x1)
982 intercept=(-z+(double) image->columns)/affine->sx;
984 if (x < inverse_edge.x2)
988 if (affine->sx < -MagickEpsilon)
990 intercept=(-z+(double) image->columns)/affine->sx;
992 if (x > inverse_edge.x1)
994 intercept=(-z/affine->sx);
996 if (x < inverse_edge.x2)
1000 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1002 inverse_edge.x2=edge->x1;
1003 return(inverse_edge);
1006 Determine top and bottom edges.
1008 z=affine->sy*y+affine->ty;
1009 if (affine->rx >= MagickEpsilon)
1011 intercept=(-z/affine->rx);
1013 if (x > inverse_edge.x1)
1015 intercept=(-z+(double) image->rows)/affine->rx;
1017 if (x < inverse_edge.x2)
1021 if (affine->rx < -MagickEpsilon)
1023 intercept=(-z+(double) image->rows)/affine->rx;
1025 if (x > inverse_edge.x1)
1027 intercept=(-z/affine->rx);
1029 if (x < inverse_edge.x2)
1033 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1035 inverse_edge.x2=edge->x2;
1036 return(inverse_edge);
1038 return(inverse_edge);
1041 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1049 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1051 inverse_affine.sx=determinant*affine->sy;
1052 inverse_affine.rx=determinant*(-affine->rx);
1053 inverse_affine.ry=determinant*(-affine->ry);
1054 inverse_affine.sy=determinant*affine->sx;
1055 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1057 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1059 return(inverse_affine);
1062 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1069 static inline double MagickMax(const double x,const double y)
1076 static inline double MagickMin(const double x,const double y)
1083 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1084 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1117 Determine bounding box.
1119 assert(image != (Image *) NULL);
1120 assert(image->signature == MagickSignature);
1121 if (image->debug != MagickFalse)
1122 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1123 assert(source != (const Image *) NULL);
1124 assert(source->signature == MagickSignature);
1125 assert(affine != (AffineMatrix *) NULL);
1128 extent[1].x=(double) source->columns-1.0;
1130 extent[2].x=(double) source->columns-1.0;
1131 extent[2].y=(double) source->rows-1.0;
1133 extent[3].y=(double) source->rows-1.0;
1134 for (i=0; i < 4; i++)
1137 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1138 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1142 for (i=1; i < 4; i++)
1144 if (min.x > extent[i].x)
1146 if (min.y > extent[i].y)
1148 if (max.x < extent[i].x)
1150 if (max.y < extent[i].y)
1154 Affine transform image.
1156 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1157 return(MagickFalse);
1159 edge.x1=MagickMax(min.x,0.0);
1160 edge.y1=MagickMax(min.y,0.0);
1161 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1162 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1163 inverse_affine=InverseAffineMatrix(affine);
1164 GetPixelInfo(image,&zero);
1165 start=(ssize_t) ceil(edge.y1-0.5);
1166 stop=(ssize_t) floor(edge.y2+0.5);
1167 source_view=AcquireVirtualCacheView(source,exception);
1168 image_view=AcquireAuthenticCacheView(image,exception);
1169 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1170 #pragma omp parallel for schedule(static,4) shared(status) \
1171 magick_threads(source,image,1,1)
1173 for (y=start; y <= stop; y++)
1194 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1195 if (inverse_edge.x2 < inverse_edge.x1)
1197 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1198 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1200 if (q == (Quantum *) NULL)
1205 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1207 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1209 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1211 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1212 point.x,point.y,&pixel,exception);
1213 GetPixelInfoPixel(image,q,&composite);
1214 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1216 SetPixelInfoPixel(image,&composite,q);
1218 q+=GetPixelChannels(image);
1220 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1223 source_view=DestroyCacheView(source_view);
1224 image_view=DestroyCacheView(image_view);
1229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 + D r a w B o u n d i n g R e c t a n g l e s %
1237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1240 % is only useful for developers debugging the rendering algorithm.
1242 % The format of the DrawBoundingRectangles method is:
1244 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1245 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1247 % A description of each parameter follows:
1249 % o image: the image.
1251 % o draw_info: the draw info.
1253 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1255 % o exception: return any errors or warnings in this structure.
1258 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1259 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1284 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1285 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1287 resolution.x=DefaultResolution;
1288 resolution.y=DefaultResolution;
1289 if (clone_info->density != (char *) NULL)
1297 flags=ParseGeometry(clone_info->density,&geometry_info);
1298 resolution.x=geometry_info.rho;
1299 resolution.y=geometry_info.sigma;
1300 if ((flags & SigmaValue) == MagickFalse)
1301 resolution.y=resolution.x;
1303 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1304 clone_info->stroke_width/2.0;
1309 if (polygon_info != (PolygonInfo *) NULL)
1311 bounds=polygon_info->edges[0].bounds;
1312 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1314 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1315 bounds.x1=polygon_info->edges[i].bounds.x1;
1316 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1317 bounds.y1=polygon_info->edges[i].bounds.y1;
1318 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1319 bounds.x2=polygon_info->edges[i].bounds.x2;
1320 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1321 bounds.y2=polygon_info->edges[i].bounds.y2;
1324 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1325 image->columns ? (double) image->columns-1 : bounds.x1;
1327 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1328 image->rows ? (double) image->rows-1 : bounds.y1;
1330 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1331 image->columns ? (double) image->columns-1 : bounds.x2;
1333 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1334 image->rows ? (double) image->rows-1 : bounds.y2;
1335 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1337 if (polygon_info->edges[i].direction != 0)
1338 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1341 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1343 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1344 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1345 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1346 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1347 primitive_info[0].primitive=RectanglePrimitive;
1348 TraceRectangle(primitive_info,start,end);
1349 primitive_info[0].method=ReplaceMethod;
1350 coordinates=(ssize_t) primitive_info[0].coordinates;
1351 primitive_info[coordinates].primitive=UndefinedPrimitive;
1352 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1355 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1357 start.x=(double) (bounds.x1-mid);
1358 start.y=(double) (bounds.y1-mid);
1359 end.x=(double) (bounds.x2+mid);
1360 end.y=(double) (bounds.y2+mid);
1361 primitive_info[0].primitive=RectanglePrimitive;
1362 TraceRectangle(primitive_info,start,end);
1363 primitive_info[0].method=ReplaceMethod;
1364 coordinates=(ssize_t) primitive_info[0].coordinates;
1365 primitive_info[coordinates].primitive=UndefinedPrimitive;
1366 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1367 clone_info=DestroyDrawInfo(clone_info);
1371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375 % D r a w C l i p P a t h %
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 % DrawClipPath() draws the clip path on the image mask.
1383 % The format of the DrawClipPath method is:
1385 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1386 % const char *name,ExceptionInfo *exception)
1388 % A description of each parameter follows:
1390 % o image: the image.
1392 % o draw_info: the draw info.
1394 % o name: the name of the clip path.
1396 % o exception: return any errors or warnings in this structure.
1399 MagickExport MagickBooleanType DrawClipPath(Image *image,
1400 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1403 filename[MaxTextExtent];
1417 assert(image != (Image *) NULL);
1418 assert(image->signature == MagickSignature);
1419 if (image->debug != MagickFalse)
1420 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1421 assert(draw_info != (const DrawInfo *) NULL);
1422 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1423 value=GetImageArtifact(image,filename);
1424 if (value == (const char *) NULL)
1425 return(MagickFalse);
1426 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1427 if (clip_mask == (Image *) NULL)
1428 return(MagickFalse);
1429 (void) QueryColorCompliance("#0000",AllCompliance,
1430 &clip_mask->background_color,exception);
1431 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1432 (void) SetImageBackgroundColor(clip_mask,exception);
1433 if (image->debug != MagickFalse)
1434 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1435 draw_info->clip_mask);
1436 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1437 (void) CloneString(&clone_info->primitive,value);
1438 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1440 clone_info->clip_mask=(char *) NULL;
1441 status=NegateImage(clip_mask,MagickFalse,exception);
1442 (void) SetImageMask(image,clip_mask,exception);
1443 clip_mask=DestroyImage(clip_mask);
1444 status&=DrawImage(image,clone_info,exception);
1445 clone_info=DestroyDrawInfo(clone_info);
1446 if (image->debug != MagickFalse)
1447 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1448 return(status != 0 ? MagickTrue : MagickFalse);
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456 + D r a w D a s h P o l y g o n %
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1463 % image while respecting the dash offset and dash pattern attributes.
1465 % The format of the DrawDashPolygon method is:
1467 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1468 % const PrimitiveInfo *primitive_info,Image *image,
1469 % ExceptionInfo *exception)
1471 % A description of each parameter follows:
1473 % o draw_info: the draw info.
1475 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1477 % o image: the image.
1479 % o exception: return any errors or warnings in this structure.
1482 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1483 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1515 assert(draw_info != (const DrawInfo *) NULL);
1516 if (image->debug != MagickFalse)
1517 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1518 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1519 clone_info->miterlimit=0;
1520 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1521 number_vertices=(size_t) i;
1522 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1523 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1524 if (dash_polygon == (PrimitiveInfo *) NULL)
1525 return(MagickFalse);
1526 dash_polygon[0]=primitive_info[0];
1527 scale=ExpandAffine(&draw_info->affine);
1528 length=scale*(draw_info->dash_pattern[0]-0.5);
1529 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1531 for (n=0; offset > 0.0; j=0)
1533 if (draw_info->dash_pattern[n] <= 0.0)
1535 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1536 if (offset > length)
1540 length=scale*(draw_info->dash_pattern[n]+0.5);
1543 if (offset < length)
1555 for (i=1; i < (ssize_t) number_vertices; i++)
1557 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1558 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1559 maximum_length=hypot((double) dx,dy);
1563 if (draw_info->dash_pattern[n] == 0.0)
1565 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1567 for (total_length=0.0; (total_length+length) <= maximum_length; )
1569 total_length+=length;
1570 if ((n & 0x01) != 0)
1572 dash_polygon[0]=primitive_info[0];
1573 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1574 total_length/maximum_length);
1575 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1576 total_length/maximum_length);
1581 if ((j+1) > (ssize_t) (2*number_vertices))
1583 dash_polygon[j]=primitive_info[i-1];
1584 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1585 total_length/maximum_length);
1586 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1587 total_length/maximum_length);
1588 dash_polygon[j].coordinates=1;
1590 dash_polygon[0].coordinates=(size_t) j;
1591 dash_polygon[j].primitive=UndefinedPrimitive;
1592 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1595 if (draw_info->dash_pattern[n] == 0.0)
1597 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1599 length-=(maximum_length-total_length);
1600 if ((n & 0x01) != 0)
1602 dash_polygon[j]=primitive_info[i];
1603 dash_polygon[j].coordinates=1;
1606 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1608 dash_polygon[j]=primitive_info[i-1];
1609 dash_polygon[j].point.x+=MagickEpsilon;
1610 dash_polygon[j].point.y+=MagickEpsilon;
1611 dash_polygon[j].coordinates=1;
1613 dash_polygon[0].coordinates=(size_t) j;
1614 dash_polygon[j].primitive=UndefinedPrimitive;
1615 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1617 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1618 clone_info=DestroyDrawInfo(clone_info);
1619 if (image->debug != MagickFalse)
1620 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1621 return(status != 0 ? MagickTrue : MagickFalse);
1625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629 % D r a w I m a g e %
1633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1635 % DrawImage() draws a graphic primitive on your image. The primitive
1636 % may be represented as a string or filename. Precede the filename with an
1637 % "at" sign (@) and the contents of the file are drawn on the image. You
1638 % can affect how text is drawn by setting one or more members of the draw
1641 % The format of the DrawImage method is:
1643 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1644 % ExceptionInfo *exception)
1646 % A description of each parameter follows:
1648 % o image: the image.
1650 % o draw_info: the draw info.
1652 % o exception: return any errors or warnings in this structure.
1656 static inline MagickBooleanType IsPoint(const char *point)
1664 value=StringToDouble(point,&p);
1665 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1668 static inline void TracePoint(PrimitiveInfo *primitive_info,
1669 const PointInfo point)
1671 primitive_info->coordinates=1;
1672 primitive_info->point=point;
1675 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1676 ExceptionInfo *exception)
1678 #define RenderImageTag "Render/Image"
1685 key[2*MaxTextExtent],
1686 keyword[MaxTextExtent],
1687 geometry[MaxTextExtent],
1688 name[MaxTextExtent],
1689 pattern[MaxTextExtent],
1742 Ensure the annotation info is valid.
1744 assert(image != (Image *) NULL);
1745 assert(image->signature == MagickSignature);
1746 if (image->debug != MagickFalse)
1747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1748 assert(draw_info != (DrawInfo *) NULL);
1749 assert(draw_info->signature == MagickSignature);
1750 if (image->debug != MagickFalse)
1751 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1752 if ((draw_info->primitive == (char *) NULL) ||
1753 (*draw_info->primitive == '\0'))
1754 return(MagickFalse);
1755 if (image->debug != MagickFalse)
1756 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1757 if (*draw_info->primitive != '@')
1758 primitive=AcquireString(draw_info->primitive);
1760 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1761 if (primitive == (char *) NULL)
1762 return(MagickFalse);
1763 primitive_extent=(double) strlen(primitive);
1764 (void) SetImageArtifact(image,"MVG",primitive);
1767 Allocate primitive info memory.
1769 graphic_context=(DrawInfo **) AcquireMagickMemory(
1770 sizeof(*graphic_context));
1771 if (graphic_context == (DrawInfo **) NULL)
1773 primitive=DestroyString(primitive);
1774 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1778 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1779 sizeof(*primitive_info));
1780 if (primitive_info == (PrimitiveInfo *) NULL)
1782 primitive=DestroyString(primitive);
1783 for ( ; n >= 0; n--)
1784 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1785 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1786 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1789 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1790 graphic_context[n]->viewbox=image->page;
1791 if ((image->page.width == 0) || (image->page.height == 0))
1793 graphic_context[n]->viewbox.width=image->columns;
1794 graphic_context[n]->viewbox.height=image->rows;
1796 token=AcquireString(primitive);
1797 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1799 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1800 return(MagickFalse);
1802 for (q=primitive; *q != '\0'; )
1805 Interpret graphic primitive.
1807 GetMagickToken(q,&q,keyword);
1808 if (*keyword == '\0')
1810 if (*keyword == '#')
1815 while ((*q != '\n') && (*q != '\0'))
1819 p=q-strlen(keyword)-1;
1820 primitive_type=UndefinedPrimitive;
1821 current=graphic_context[n]->affine;
1822 GetAffineMatrix(&affine);
1830 if (LocaleCompare("affine",keyword) == 0)
1832 GetMagickToken(q,&q,token);
1833 affine.sx=StringToDouble(token,(char **) NULL);
1834 GetMagickToken(q,&q,token);
1836 GetMagickToken(q,&q,token);
1837 affine.rx=StringToDouble(token,(char **) NULL);
1838 GetMagickToken(q,&q,token);
1840 GetMagickToken(q,&q,token);
1841 affine.ry=StringToDouble(token,(char **) NULL);
1842 GetMagickToken(q,&q,token);
1844 GetMagickToken(q,&q,token);
1845 affine.sy=StringToDouble(token,(char **) NULL);
1846 GetMagickToken(q,&q,token);
1848 GetMagickToken(q,&q,token);
1849 affine.tx=StringToDouble(token,(char **) NULL);
1850 GetMagickToken(q,&q,token);
1852 GetMagickToken(q,&q,token);
1853 affine.ty=StringToDouble(token,(char **) NULL);
1856 if (LocaleCompare("arc",keyword) == 0)
1858 primitive_type=ArcPrimitive;
1867 if (LocaleCompare("bezier",keyword) == 0)
1869 primitive_type=BezierPrimitive;
1872 if (LocaleCompare("border-color",keyword) == 0)
1874 GetMagickToken(q,&q,token);
1875 (void) QueryColorCompliance(token,AllCompliance,
1876 &graphic_context[n]->border_color,exception);
1885 if (LocaleCompare("clip-path",keyword) == 0)
1890 GetMagickToken(q,&q,token);
1891 (void) CloneString(&graphic_context[n]->clip_mask,token);
1892 (void) DrawClipPath(image,graphic_context[n],
1893 graphic_context[n]->clip_mask,exception);
1896 if (LocaleCompare("clip-rule",keyword) == 0)
1901 GetMagickToken(q,&q,token);
1902 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1904 if (fill_rule == -1)
1907 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1910 if (LocaleCompare("clip-units",keyword) == 0)
1915 GetMagickToken(q,&q,token);
1916 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1918 if (clip_units == -1)
1923 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1924 if (clip_units == ObjectBoundingBox)
1926 GetAffineMatrix(¤t);
1927 affine.sx=draw_info->bounds.x2;
1928 affine.sy=draw_info->bounds.y2;
1929 affine.tx=draw_info->bounds.x1;
1930 affine.ty=draw_info->bounds.y1;
1935 if (LocaleCompare("circle",keyword) == 0)
1937 primitive_type=CirclePrimitive;
1940 if (LocaleCompare("color",keyword) == 0)
1942 primitive_type=ColorPrimitive;
1951 if (LocaleCompare("decorate",keyword) == 0)
1956 GetMagickToken(q,&q,token);
1957 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1962 graphic_context[n]->decorate=(DecorationType) decorate;
1965 if (LocaleCompare("direction",keyword) == 0)
1970 GetMagickToken(q,&q,token);
1971 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1973 if (direction == -1)
1976 graphic_context[n]->direction=(DirectionType) direction;
1985 if (LocaleCompare("ellipse",keyword) == 0)
1987 primitive_type=EllipsePrimitive;
1990 if (LocaleCompare("encoding",keyword) == 0)
1992 GetMagickToken(q,&q,token);
1993 (void) CloneString(&graphic_context[n]->encoding,token);
2002 if (LocaleCompare("fill",keyword) == 0)
2004 GetMagickToken(q,&q,token);
2005 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2006 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2007 (void) DrawPatternPath(image,draw_info,token,
2008 &graphic_context[n]->fill_pattern,exception);
2011 status&=QueryColorCompliance(token,AllCompliance,
2012 &graphic_context[n]->fill,exception);
2013 if (status == MagickFalse)
2018 pattern_info=AcquireImageInfo();
2019 (void) CopyMagickString(pattern_info->filename,token,
2021 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2023 CatchException(exception);
2024 pattern_info=DestroyImageInfo(pattern_info);
2029 if (LocaleCompare("fill-alpha",keyword) == 0)
2031 GetMagickToken(q,&q,token);
2032 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2033 graphic_context[n]->fill.alpha=(double) QuantumRange*
2034 factor*StringToDouble(token,(char **) NULL);
2037 if (LocaleCompare("fill-rule",keyword) == 0)
2042 GetMagickToken(q,&q,token);
2043 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2045 if (fill_rule == -1)
2048 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2051 if (LocaleCompare("font",keyword) == 0)
2053 GetMagickToken(q,&q,token);
2054 (void) CloneString(&graphic_context[n]->font,token);
2055 if (LocaleCompare("none",token) == 0)
2056 graphic_context[n]->font=(char *)
2057 RelinquishMagickMemory(graphic_context[n]->font);
2060 if (LocaleCompare("font-family",keyword) == 0)
2062 GetMagickToken(q,&q,token);
2063 (void) CloneString(&graphic_context[n]->family,token);
2066 if (LocaleCompare("font-size",keyword) == 0)
2068 GetMagickToken(q,&q,token);
2069 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2072 if (LocaleCompare("font-stretch",keyword) == 0)
2077 GetMagickToken(q,&q,token);
2078 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2082 graphic_context[n]->stretch=(StretchType) stretch;
2085 if (LocaleCompare("font-style",keyword) == 0)
2090 GetMagickToken(q,&q,token);
2091 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2095 graphic_context[n]->style=(StyleType) style;
2098 if (LocaleCompare("font-weight",keyword) == 0)
2100 GetMagickToken(q,&q,token);
2101 graphic_context[n]->weight=StringToUnsignedLong(token);
2102 if (LocaleCompare(token,"all") == 0)
2103 graphic_context[n]->weight=0;
2104 if (LocaleCompare(token,"bold") == 0)
2105 graphic_context[n]->weight=700;
2106 if (LocaleCompare(token,"bolder") == 0)
2107 if (graphic_context[n]->weight <= 800)
2108 graphic_context[n]->weight+=100;
2109 if (LocaleCompare(token,"lighter") == 0)
2110 if (graphic_context[n]->weight >= 100)
2111 graphic_context[n]->weight-=100;
2112 if (LocaleCompare(token,"normal") == 0)
2113 graphic_context[n]->weight=400;
2122 if (LocaleCompare("gradient-units",keyword) == 0)
2124 GetMagickToken(q,&q,token);
2127 if (LocaleCompare("gravity",keyword) == 0)
2132 GetMagickToken(q,&q,token);
2133 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2137 graphic_context[n]->gravity=(GravityType) gravity;
2146 if (LocaleCompare("image",keyword) == 0)
2151 primitive_type=ImagePrimitive;
2152 GetMagickToken(q,&q,token);
2153 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2157 graphic_context[n]->compose=(CompositeOperator) compose;
2160 if (LocaleCompare("interline-spacing",keyword) == 0)
2162 GetMagickToken(q,&q,token);
2163 graphic_context[n]->interline_spacing=StringToDouble(token,
2167 if (LocaleCompare("interword-spacing",keyword) == 0)
2169 GetMagickToken(q,&q,token);
2170 graphic_context[n]->interword_spacing=StringToDouble(token,
2180 if (LocaleCompare("kerning",keyword) == 0)
2182 GetMagickToken(q,&q,token);
2183 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2192 if (LocaleCompare("line",keyword) == 0)
2193 primitive_type=LinePrimitive;
2201 if (LocaleCompare("matte",keyword) == 0)
2202 primitive_type=MattePrimitive;
2210 if (LocaleCompare("offset",keyword) == 0)
2212 GetMagickToken(q,&q,token);
2215 if (LocaleCompare("opacity",keyword) == 0)
2217 GetMagickToken(q,&q,token);
2218 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2219 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2220 QuantumScale*graphic_context[n]->alpha)*factor*
2221 StringToDouble(token,(char **) NULL))));
2222 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2223 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2232 if (LocaleCompare("path",keyword) == 0)
2234 primitive_type=PathPrimitive;
2237 if (LocaleCompare("point",keyword) == 0)
2239 primitive_type=PointPrimitive;
2242 if (LocaleCompare("polyline",keyword) == 0)
2244 primitive_type=PolylinePrimitive;
2247 if (LocaleCompare("polygon",keyword) == 0)
2249 primitive_type=PolygonPrimitive;
2252 if (LocaleCompare("pop",keyword) == 0)
2254 GetMagickToken(q,&q,token);
2255 if (LocaleCompare("clip-path",token) == 0)
2257 if (LocaleCompare("defs",token) == 0)
2259 if (LocaleCompare("gradient",token) == 0)
2261 if (LocaleCompare("graphic-context",token) == 0)
2265 (void) ThrowMagickException(exception,GetMagickModule(),
2266 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2270 if (graphic_context[n]->clip_mask != (char *) NULL)
2271 if (LocaleCompare(graphic_context[n]->clip_mask,
2272 graphic_context[n-1]->clip_mask) != 0)
2273 (void) SetImageMask(image,(Image *) NULL,exception);
2274 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2278 if (LocaleCompare("pattern",token) == 0)
2283 if (LocaleCompare("push",keyword) == 0)
2285 GetMagickToken(q,&q,token);
2286 if (LocaleCompare("clip-path",token) == 0)
2289 name[MaxTextExtent];
2291 GetMagickToken(q,&q,token);
2292 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2293 for (p=q; *q != '\0'; )
2295 GetMagickToken(q,&q,token);
2296 if (LocaleCompare(token,"pop") != 0)
2298 GetMagickToken(q,(const char **) NULL,token);
2299 if (LocaleCompare(token,"clip-path") != 0)
2303 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2304 (void) SetImageArtifact(image,name,token);
2305 GetMagickToken(q,&q,token);
2308 if (LocaleCompare("gradient",token) == 0)
2311 key[2*MaxTextExtent],
2312 name[MaxTextExtent],
2313 type[MaxTextExtent];
2318 GetMagickToken(q,&q,token);
2319 (void) CopyMagickString(name,token,MaxTextExtent);
2320 GetMagickToken(q,&q,token);
2321 (void) CopyMagickString(type,token,MaxTextExtent);
2322 GetMagickToken(q,&q,token);
2323 segment.x1=StringToDouble(token,(char **) NULL);
2324 GetMagickToken(q,&q,token);
2326 GetMagickToken(q,&q,token);
2327 segment.y1=StringToDouble(token,(char **) NULL);
2328 GetMagickToken(q,&q,token);
2330 GetMagickToken(q,&q,token);
2331 segment.x2=StringToDouble(token,(char **) NULL);
2332 GetMagickToken(q,&q,token);
2334 GetMagickToken(q,&q,token);
2335 segment.y2=StringToDouble(token,(char **) NULL);
2336 if (LocaleCompare(type,"radial") == 0)
2338 GetMagickToken(q,&q,token);
2340 GetMagickToken(q,&q,token);
2342 for (p=q; *q != '\0'; )
2344 GetMagickToken(q,&q,token);
2345 if (LocaleCompare(token,"pop") != 0)
2347 GetMagickToken(q,(const char **) NULL,token);
2348 if (LocaleCompare(token,"gradient") != 0)
2352 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2353 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2354 graphic_context[n]->affine.ry*segment.y1+
2355 graphic_context[n]->affine.tx;
2356 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2357 graphic_context[n]->affine.sy*segment.y1+
2358 graphic_context[n]->affine.ty;
2359 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2360 graphic_context[n]->affine.ry*segment.y2+
2361 graphic_context[n]->affine.tx;
2362 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2363 graphic_context[n]->affine.sy*segment.y2+
2364 graphic_context[n]->affine.ty;
2365 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2366 (void) SetImageArtifact(image,key,token);
2367 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2368 (void) FormatLocaleString(geometry,MaxTextExtent,
2369 "%gx%g%+.15g%+.15g",
2370 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2371 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2372 bounds.x1,bounds.y1);
2373 (void) SetImageArtifact(image,key,geometry);
2374 GetMagickToken(q,&q,token);
2377 if (LocaleCompare("pattern",token) == 0)
2382 GetMagickToken(q,&q,token);
2383 (void) CopyMagickString(name,token,MaxTextExtent);
2384 GetMagickToken(q,&q,token);
2385 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2387 GetMagickToken(q,&q,token);
2389 GetMagickToken(q,&q,token);
2390 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2392 GetMagickToken(q,&q,token);
2394 GetMagickToken(q,&q,token);
2395 bounds.width=(size_t) floor(StringToDouble(token,
2396 (char **) NULL)+0.5);
2397 GetMagickToken(q,&q,token);
2399 GetMagickToken(q,&q,token);
2400 bounds.height=(size_t) floor(StringToDouble(token,
2401 (char **) NULL)+0.5);
2402 for (p=q; *q != '\0'; )
2404 GetMagickToken(q,&q,token);
2405 if (LocaleCompare(token,"pop") != 0)
2407 GetMagickToken(q,(const char **) NULL,token);
2408 if (LocaleCompare(token,"pattern") != 0)
2412 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2413 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2414 (void) SetImageArtifact(image,key,token);
2415 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2416 (void) FormatLocaleString(geometry,MaxTextExtent,
2417 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2418 bounds.height,(double) bounds.x,(double) bounds.y);
2419 (void) SetImageArtifact(image,key,geometry);
2420 GetMagickToken(q,&q,token);
2423 if (LocaleCompare("graphic-context",token) == 0)
2426 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2427 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2428 if (graphic_context == (DrawInfo **) NULL)
2430 (void) ThrowMagickException(exception,GetMagickModule(),
2431 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2435 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2436 graphic_context[n-1]);
2439 if (LocaleCompare("defs",token) == 0)
2450 if (LocaleCompare("rectangle",keyword) == 0)
2452 primitive_type=RectanglePrimitive;
2455 if (LocaleCompare("rotate",keyword) == 0)
2457 GetMagickToken(q,&q,token);
2458 angle=StringToDouble(token,(char **) NULL);
2459 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2460 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2461 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2462 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2465 if (LocaleCompare("roundRectangle",keyword) == 0)
2467 primitive_type=RoundRectanglePrimitive;
2476 if (LocaleCompare("scale",keyword) == 0)
2478 GetMagickToken(q,&q,token);
2479 affine.sx=StringToDouble(token,(char **) NULL);
2480 GetMagickToken(q,&q,token);
2482 GetMagickToken(q,&q,token);
2483 affine.sy=StringToDouble(token,(char **) NULL);
2486 if (LocaleCompare("skewX",keyword) == 0)
2488 GetMagickToken(q,&q,token);
2489 angle=StringToDouble(token,(char **) NULL);
2490 affine.ry=sin(DegreesToRadians(angle));
2493 if (LocaleCompare("skewY",keyword) == 0)
2495 GetMagickToken(q,&q,token);
2496 angle=StringToDouble(token,(char **) NULL);
2497 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2500 if (LocaleCompare("stop-color",keyword) == 0)
2505 GetMagickToken(q,&q,token);
2506 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2508 (void) GradientImage(image,LinearGradient,ReflectSpread,
2509 &start_color,&stop_color,exception);
2510 start_color=stop_color;
2511 GetMagickToken(q,&q,token);
2514 if (LocaleCompare("stroke",keyword) == 0)
2516 GetMagickToken(q,&q,token);
2517 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2518 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2519 (void) DrawPatternPath(image,draw_info,token,
2520 &graphic_context[n]->stroke_pattern,exception);
2523 status&=QueryColorCompliance(token,AllCompliance,
2524 &graphic_context[n]->stroke,exception);
2525 if (status == MagickFalse)
2530 pattern_info=AcquireImageInfo();
2531 (void) CopyMagickString(pattern_info->filename,token,
2533 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2535 CatchException(exception);
2536 pattern_info=DestroyImageInfo(pattern_info);
2541 if (LocaleCompare("stroke-antialias",keyword) == 0)
2543 GetMagickToken(q,&q,token);
2544 graphic_context[n]->stroke_antialias=
2545 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2548 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2550 if (graphic_context[n]->dash_pattern != (double *) NULL)
2551 graphic_context[n]->dash_pattern=(double *)
2552 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2553 if (IsPoint(q) != MagickFalse)
2559 GetMagickToken(p,&p,token);
2561 GetMagickToken(p,&p,token);
2562 for (x=0; IsPoint(token) != MagickFalse; x++)
2564 GetMagickToken(p,&p,token);
2566 GetMagickToken(p,&p,token);
2568 graphic_context[n]->dash_pattern=(double *)
2569 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2570 sizeof(*graphic_context[n]->dash_pattern));
2571 if (graphic_context[n]->dash_pattern == (double *) NULL)
2573 (void) ThrowMagickException(exception,GetMagickModule(),
2574 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2578 for (j=0; j < x; j++)
2580 GetMagickToken(q,&q,token);
2582 GetMagickToken(q,&q,token);
2583 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2586 if ((x & 0x01) != 0)
2587 for ( ; j < (2*x); j++)
2588 graphic_context[n]->dash_pattern[j]=
2589 graphic_context[n]->dash_pattern[j-x];
2590 graphic_context[n]->dash_pattern[j]=0.0;
2593 GetMagickToken(q,&q,token);
2596 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2598 GetMagickToken(q,&q,token);
2599 graphic_context[n]->dash_offset=StringToDouble(token,
2603 if (LocaleCompare("stroke-linecap",keyword) == 0)
2608 GetMagickToken(q,&q,token);
2609 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2613 graphic_context[n]->linecap=(LineCap) linecap;
2616 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2621 GetMagickToken(q,&q,token);
2622 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2627 graphic_context[n]->linejoin=(LineJoin) linejoin;
2630 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2632 GetMagickToken(q,&q,token);
2633 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2636 if (LocaleCompare("stroke-opacity",keyword) == 0)
2638 GetMagickToken(q,&q,token);
2639 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2640 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2641 factor*StringToDouble(token,(char **) NULL);
2644 if (LocaleCompare("stroke-width",keyword) == 0)
2646 GetMagickToken(q,&q,token);
2647 graphic_context[n]->stroke_width=StringToDouble(token,
2657 if (LocaleCompare("text",keyword) == 0)
2659 primitive_type=TextPrimitive;
2662 if (LocaleCompare("text-align",keyword) == 0)
2667 GetMagickToken(q,&q,token);
2668 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2672 graphic_context[n]->align=(AlignType) align;
2675 if (LocaleCompare("text-anchor",keyword) == 0)
2680 GetMagickToken(q,&q,token);
2681 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2685 graphic_context[n]->align=(AlignType) align;
2688 if (LocaleCompare("text-antialias",keyword) == 0)
2690 GetMagickToken(q,&q,token);
2691 graphic_context[n]->text_antialias=
2692 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2695 if (LocaleCompare("text-undercolor",keyword) == 0)
2697 GetMagickToken(q,&q,token);
2698 (void) QueryColorCompliance(token,AllCompliance,
2699 &graphic_context[n]->undercolor,exception);
2702 if (LocaleCompare("translate",keyword) == 0)
2704 GetMagickToken(q,&q,token);
2705 affine.tx=StringToDouble(token,(char **) NULL);
2706 GetMagickToken(q,&q,token);
2708 GetMagickToken(q,&q,token);
2709 affine.ty=StringToDouble(token,(char **) NULL);
2718 if (LocaleCompare("viewbox",keyword) == 0)
2720 GetMagickToken(q,&q,token);
2721 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2722 (char **) NULL)-0.5);
2723 GetMagickToken(q,&q,token);
2725 GetMagickToken(q,&q,token);
2726 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2727 (char **) NULL)-0.5);
2728 GetMagickToken(q,&q,token);
2730 GetMagickToken(q,&q,token);
2731 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2732 token,(char **) NULL)+0.5);
2733 GetMagickToken(q,&q,token);
2735 GetMagickToken(q,&q,token);
2736 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2737 token,(char **) NULL)+0.5);
2749 if (status == MagickFalse)
2751 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2752 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2754 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2755 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2756 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2757 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2758 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2760 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2763 if (primitive_type == UndefinedPrimitive)
2765 if (image->debug != MagickFalse)
2766 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2771 Parse the primitive attributes.
2775 primitive_info[0].point.x=0.0;
2776 primitive_info[0].point.y=0.0;
2777 for (x=0; *q != '\0'; x++)
2782 if (IsPoint(q) == MagickFalse)
2784 GetMagickToken(q,&q,token);
2785 point.x=StringToDouble(token,(char **) NULL);
2786 GetMagickToken(q,&q,token);
2788 GetMagickToken(q,&q,token);
2789 point.y=StringToDouble(token,(char **) NULL);
2790 GetMagickToken(q,(const char **) NULL,token);
2792 GetMagickToken(q,&q,token);
2793 primitive_info[i].primitive=primitive_type;
2794 primitive_info[i].point=point;
2795 primitive_info[i].coordinates=0;
2796 primitive_info[i].method=FloodfillMethod;
2798 if (i < (ssize_t) number_points)
2801 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2802 (size_t) number_points,sizeof(*primitive_info));
2803 if (primitive_info == (PrimitiveInfo *) NULL)
2805 (void) ThrowMagickException(exception,GetMagickModule(),
2806 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2810 primitive_info[j].primitive=primitive_type;
2811 primitive_info[j].coordinates=(size_t) x;
2812 primitive_info[j].method=FloodfillMethod;
2813 primitive_info[j].text=(char *) NULL;
2815 Circumscribe primitive within a circle.
2817 bounds.x1=primitive_info[j].point.x;
2818 bounds.y1=primitive_info[j].point.y;
2819 bounds.x2=primitive_info[j].point.x;
2820 bounds.y2=primitive_info[j].point.y;
2821 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2823 point=primitive_info[j+k].point;
2824 if (point.x < bounds.x1)
2826 if (point.y < bounds.y1)
2828 if (point.x > bounds.x2)
2830 if (point.y > bounds.y2)
2834 Speculate how many points our primitive might consume.
2836 length=primitive_info[j].coordinates;
2837 switch (primitive_type)
2839 case RectanglePrimitive:
2844 case RoundRectanglePrimitive:
2851 alpha=bounds.x2-bounds.x1;
2852 beta=bounds.y2-bounds.y1;
2853 radius=hypot((double) alpha,(double) beta);
2855 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2858 case BezierPrimitive:
2860 if (primitive_info[j].coordinates > 107)
2861 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2862 "TooManyBezierCoordinates","`%s'",token);
2863 length=BezierQuantum*primitive_info[j].coordinates;
2872 GetMagickToken(q,&q,token);
2875 for (s=token; *s != '\0'; s=t)
2880 value=StringToDouble(s,&t);
2889 length=length*BezierQuantum/2;
2892 case CirclePrimitive:
2894 case EllipsePrimitive:
2901 alpha=bounds.x2-bounds.x1;
2902 beta=bounds.y2-bounds.y1;
2903 radius=hypot((double) alpha,(double) beta);
2904 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2910 if ((size_t) (i+length) >= number_points)
2913 Resize based on speculative points required by primitive.
2915 number_points+=length+1;
2916 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2917 (size_t) number_points,sizeof(*primitive_info));
2918 if (primitive_info == (PrimitiveInfo *) NULL)
2920 (void) ThrowMagickException(exception,GetMagickModule(),
2921 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2926 switch (primitive_type)
2928 case PointPrimitive:
2931 if (primitive_info[j].coordinates != 1)
2936 TracePoint(primitive_info+j,primitive_info[j].point);
2937 i=(ssize_t) (j+primitive_info[j].coordinates);
2942 if (primitive_info[j].coordinates != 2)
2947 TraceLine(primitive_info+j,primitive_info[j].point,
2948 primitive_info[j+1].point);
2949 i=(ssize_t) (j+primitive_info[j].coordinates);
2952 case RectanglePrimitive:
2954 if (primitive_info[j].coordinates != 2)
2959 TraceRectangle(primitive_info+j,primitive_info[j].point,
2960 primitive_info[j+1].point);
2961 i=(ssize_t) (j+primitive_info[j].coordinates);
2964 case RoundRectanglePrimitive:
2966 if (primitive_info[j].coordinates != 3)
2971 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2972 primitive_info[j+1].point,primitive_info[j+2].point);
2973 i=(ssize_t) (j+primitive_info[j].coordinates);
2978 if (primitive_info[j].coordinates != 3)
2980 primitive_type=UndefinedPrimitive;
2983 TraceArc(primitive_info+j,primitive_info[j].point,
2984 primitive_info[j+1].point,primitive_info[j+2].point);
2985 i=(ssize_t) (j+primitive_info[j].coordinates);
2988 case EllipsePrimitive:
2990 if (primitive_info[j].coordinates != 3)
2995 TraceEllipse(primitive_info+j,primitive_info[j].point,
2996 primitive_info[j+1].point,primitive_info[j+2].point);
2997 i=(ssize_t) (j+primitive_info[j].coordinates);
3000 case CirclePrimitive:
3002 if (primitive_info[j].coordinates != 2)
3007 TraceCircle(primitive_info+j,primitive_info[j].point,
3008 primitive_info[j+1].point);
3009 i=(ssize_t) (j+primitive_info[j].coordinates);
3012 case PolylinePrimitive:
3014 case PolygonPrimitive:
3016 primitive_info[i]=primitive_info[j];
3017 primitive_info[i].coordinates=0;
3018 primitive_info[j].coordinates++;
3022 case BezierPrimitive:
3024 if (primitive_info[j].coordinates < 3)
3029 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3030 i=(ssize_t) (j+primitive_info[j].coordinates);
3035 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3038 case ColorPrimitive:
3039 case MattePrimitive:
3044 if (primitive_info[j].coordinates != 1)
3049 GetMagickToken(q,&q,token);
3050 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3054 primitive_info[j].method=(PaintMethod) method;
3059 if (primitive_info[j].coordinates != 1)
3065 GetMagickToken(q,&q,token);
3066 primitive_info[j].text=AcquireString(token);
3069 case ImagePrimitive:
3071 if (primitive_info[j].coordinates != 2)
3076 GetMagickToken(q,&q,token);
3077 primitive_info[j].text=AcquireString(token);
3081 if (primitive_info == (PrimitiveInfo *) NULL)
3083 if (image->debug != MagickFalse)
3084 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3085 if (status == MagickFalse)
3087 primitive_info[i].primitive=UndefinedPrimitive;
3093 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3095 point=primitive_info[i].point;
3096 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3097 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3098 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3099 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3100 point=primitive_info[i].point;
3101 if (point.x < graphic_context[n]->bounds.x1)
3102 graphic_context[n]->bounds.x1=point.x;
3103 if (point.y < graphic_context[n]->bounds.y1)
3104 graphic_context[n]->bounds.y1=point.y;
3105 if (point.x > graphic_context[n]->bounds.x2)
3106 graphic_context[n]->bounds.x2=point.x;
3107 if (point.y > graphic_context[n]->bounds.y2)
3108 graphic_context[n]->bounds.y2=point.y;
3109 if (primitive_info[i].primitive == ImagePrimitive)
3111 if (i >= (ssize_t) number_points)
3112 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3114 if (graphic_context[n]->render != MagickFalse)
3116 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3117 (LocaleCompare(graphic_context[n]->clip_mask,
3118 graphic_context[n-1]->clip_mask) != 0))
3119 status&=DrawClipPath(image,graphic_context[n],
3120 graphic_context[n]->clip_mask,exception);
3121 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3124 if (primitive_info->text != (char *) NULL)
3125 primitive_info->text=(char *) RelinquishMagickMemory(
3126 primitive_info->text);
3127 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3129 if (proceed == MagickFalse)
3134 if (image->debug != MagickFalse)
3135 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3137 Relinquish resources.
3139 token=DestroyString(token);
3140 if (primitive_info != (PrimitiveInfo *) NULL)
3141 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3142 primitive=DestroyString(primitive);
3143 for ( ; n >= 0; n--)
3144 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3145 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3146 if (status == MagickFalse)
3147 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3149 return(status != 0 ? MagickTrue : MagickFalse);
3153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3157 % D r a w G r a d i e n t I m a g e %
3161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3163 % DrawGradientImage() draws a linear gradient on the image.
3165 % The format of the DrawGradientImage method is:
3167 % MagickBooleanType DrawGradientImage(Image *image,
3168 % const DrawInfo *draw_info,ExceptionInfo *exception)
3170 % A description of each parameter follows:
3172 % o image: the image.
3174 % o draw_info: the draw info.
3176 % o exception: return any errors or warnings in this structure.
3180 static inline double GetStopColorOffset(const GradientInfo *gradient,
3181 const ssize_t x,const ssize_t y)
3183 switch (gradient->type)
3185 case UndefinedGradient:
3186 case LinearGradient:
3201 gradient_vector=(&gradient->gradient_vector);
3202 p.x=gradient_vector->x2-gradient_vector->x1;
3203 p.y=gradient_vector->y2-gradient_vector->y1;
3204 q.x=(double) x-gradient_vector->x1;
3205 q.y=(double) y-gradient_vector->y1;
3206 length=sqrt(q.x*q.x+q.y*q.y);
3207 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3208 gamma=PerceptibleReciprocal(gamma);
3209 scale=p.x*q.x+p.y*q.y;
3210 offset=gamma*scale*length;
3213 case RadialGradient:
3222 v.x=(double) x-gradient->center.x;
3223 v.y=(double) y-gradient->center.y;
3224 length=sqrt(v.x*v.x+v.y*v.y);
3225 if (gradient->spread == RepeatSpread)
3227 offset=length/gradient->radius;
3234 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3235 const DrawInfo *draw_info,ExceptionInfo *exception)
3265 Draw linear or radial gradient on image.
3267 assert(image != (Image *) NULL);
3268 assert(image->signature == MagickSignature);
3269 if (image->debug != MagickFalse)
3270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3271 assert(draw_info != (const DrawInfo *) NULL);
3272 gradient=(&draw_info->gradient);
3273 gradient_vector=(&gradient->gradient_vector);
3274 point.x=gradient_vector->x2-gradient_vector->x1;
3275 point.y=gradient_vector->y2-gradient_vector->y1;
3276 length=sqrt(point.x*point.x+point.y*point.y);
3277 bounding_box=gradient->bounding_box;
3279 GetPixelInfo(image,&zero);
3280 image_view=AcquireAuthenticCacheView(image,exception);
3281 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3282 #pragma omp parallel for schedule(static,4) shared(status) \
3283 magick_threads(image,image,1,1)
3285 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3305 if (status == MagickFalse)
3307 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3308 if (q == (Quantum *) NULL)
3315 offset=GetStopColorOffset(gradient,0,y);
3316 if (gradient->type != RadialGradient)
3318 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3320 GetPixelInfoPixel(image,q,&pixel);
3321 switch (gradient->spread)
3323 case UndefinedSpread:
3326 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3327 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3329 offset=GetStopColorOffset(gradient,x,y);
3330 if (gradient->type != RadialGradient)
3333 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3334 if (offset < gradient->stops[i].offset)
3336 if ((offset < 0.0) || (i == 0))
3337 composite=gradient->stops[0].color;
3339 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3340 composite=gradient->stops[gradient->number_stops-1].color;
3345 alpha=(offset-gradient->stops[i].offset)/
3346 (gradient->stops[j].offset-gradient->stops[i].offset);
3347 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3348 &gradient->stops[j].color,alpha,&composite);
3354 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3355 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3357 offset=GetStopColorOffset(gradient,x,y);
3358 if (gradient->type != RadialGradient)
3363 if ((ssize_t) fmod(offset,2.0) == 0)
3364 offset=fmod(offset,1.0);
3366 offset=1.0-fmod(offset,1.0);
3367 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3368 if (offset < gradient->stops[i].offset)
3371 composite=gradient->stops[0].color;
3373 if (i == (ssize_t) gradient->number_stops)
3374 composite=gradient->stops[gradient->number_stops-1].color;
3379 alpha=(offset-gradient->stops[i].offset)/
3380 (gradient->stops[j].offset-gradient->stops[i].offset);
3381 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3382 &gradient->stops[j].color,alpha,&composite);
3394 antialias=MagickFalse;
3396 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3397 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3399 offset=GetStopColorOffset(gradient,x,y);
3400 if (gradient->type == LinearGradient)
3402 repeat=fmod(offset,length);
3404 repeat=length-fmod(-repeat,length);
3406 repeat=fmod(offset,length);
3407 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3408 MagickTrue : MagickFalse;
3409 offset=repeat/length;
3413 repeat=fmod(offset,gradient->radius);
3415 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3417 repeat=fmod(offset,gradient->radius);
3418 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3420 offset=repeat/gradient->radius;
3423 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3424 if (offset < gradient->stops[i].offset)
3427 composite=gradient->stops[0].color;
3429 if (i == (ssize_t) gradient->number_stops)
3430 composite=gradient->stops[gradient->number_stops-1].color;
3435 alpha=(offset-gradient->stops[i].offset)/
3436 (gradient->stops[j].offset-gradient->stops[i].offset);
3437 if (antialias != MagickFalse)
3439 if (gradient->type == LinearGradient)
3440 alpha=length-repeat;
3442 alpha=gradient->radius-repeat;
3444 j=(ssize_t) gradient->number_stops-1L;
3446 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3447 &gradient->stops[j].color,alpha,&composite);
3452 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3454 SetPixelInfoPixel(image,&pixel,q);
3455 q+=GetPixelChannels(image);
3457 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3460 image_view=DestroyCacheView(image_view);
3465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3469 % D r a w P a t t e r n P a t h %
3473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3475 % DrawPatternPath() draws a pattern.
3477 % The format of the DrawPatternPath method is:
3479 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3480 % const char *name,Image **pattern,ExceptionInfo *exception)
3482 % A description of each parameter follows:
3484 % o image: the image.
3486 % o draw_info: the draw info.
3488 % o name: the pattern name.
3490 % o image: the image.
3492 % o exception: return any errors or warnings in this structure.
3495 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3496 const DrawInfo *draw_info,const char *name,Image **pattern,
3497 ExceptionInfo *exception)
3500 property[MaxTextExtent];
3515 assert(image != (Image *) NULL);
3516 assert(image->signature == MagickSignature);
3517 if (image->debug != MagickFalse)
3518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3519 assert(draw_info != (const DrawInfo *) NULL);
3520 assert(name != (const char *) NULL);
3521 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3522 path=GetImageArtifact(image,property);
3523 if (path == (const char *) NULL)
3524 return(MagickFalse);
3525 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3526 geometry=GetImageArtifact(image,property);
3527 if (geometry == (const char *) NULL)
3528 return(MagickFalse);
3529 if ((*pattern) != (Image *) NULL)
3530 *pattern=DestroyImage(*pattern);
3531 image_info=AcquireImageInfo();
3532 image_info->size=AcquireString(geometry);
3533 *pattern=AcquireImage(image_info,exception);
3534 image_info=DestroyImageInfo(image_info);
3535 (void) QueryColorCompliance("#000000ff",AllCompliance,
3536 &(*pattern)->background_color,exception);
3537 (void) SetImageBackgroundColor(*pattern,exception);
3538 if (image->debug != MagickFalse)
3539 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3540 "begin pattern-path %s %s",name,geometry);
3541 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3542 clone_info->fill_pattern=NewImageList();
3543 clone_info->stroke_pattern=NewImageList();
3544 (void) CloneString(&clone_info->primitive,path);
3545 status=DrawImage(*pattern,clone_info,exception);
3546 clone_info=DestroyDrawInfo(clone_info);
3547 if (image->debug != MagickFalse)
3548 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3557 + D r a w P o l y g o n P r i m i t i v e %
3561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3563 % DrawPolygonPrimitive() draws a polygon on the image.
3565 % The format of the DrawPolygonPrimitive method is:
3567 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3568 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3569 % ExceptionInfo *exception)
3571 % A description of each parameter follows:
3573 % o image: the image.
3575 % o draw_info: the draw info.
3577 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3579 % o exception: return any errors or warnings in this structure.
3583 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3588 assert(polygon_info != (PolygonInfo **) NULL);
3589 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3590 if (polygon_info[i] != (PolygonInfo *) NULL)
3591 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3592 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3593 return(polygon_info);
3596 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3597 const PrimitiveInfo *primitive_info)
3600 *restrict path_info;
3611 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3612 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3613 sizeof(*polygon_info));
3614 if (polygon_info == (PolygonInfo **) NULL)
3615 return((PolygonInfo **) NULL);
3616 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3617 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3618 if (path_info == (PathInfo *) NULL)
3619 return(DestroyPolygonThreadSet(polygon_info));
3620 for (i=0; i < (ssize_t) number_threads; i++)
3622 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3623 if (polygon_info[i] == (PolygonInfo *) NULL)
3624 return(DestroyPolygonThreadSet(polygon_info));
3626 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3627 return(polygon_info);
3630 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3631 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3632 const ssize_t y,double *stroke_alpha)
3643 register const PointInfo
3657 Compute fill & stroke opacity for this (x,y) point.
3661 p=polygon_info->edges;
3662 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3664 if ((double) y <= (p->bounds.y1-mid-0.5))
3666 if ((double) y > (p->bounds.y2+mid+0.5))
3668 (void) DestroyEdge(polygon_info,(size_t) j);
3671 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3672 ((double) x > (p->bounds.x2+mid+0.5)))
3674 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3675 for ( ; i < (ssize_t) p->number_points; i++)
3677 if ((double) y <= (p->points[i-1].y-mid-0.5))
3679 if ((double) y > (p->points[i].y+mid+0.5))
3681 if (p->scanline != (double) y)
3683 p->scanline=(double) y;
3684 p->highwater=(size_t) i;
3687 Compute distance between a point and an edge.
3690 delta.x=(q+1)->x-q->x;
3691 delta.y=(q+1)->y-q->y;
3692 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3695 delta.x=(double) x-q->x;
3696 delta.y=(double) y-q->y;
3697 distance=delta.x*delta.x+delta.y*delta.y;
3701 alpha=delta.x*delta.x+delta.y*delta.y;
3704 delta.x=(double) x-(q+1)->x;
3705 delta.y=(double) y-(q+1)->y;
3706 distance=delta.x*delta.x+delta.y*delta.y;
3711 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3712 distance=alpha*beta*beta;
3716 Compute stroke & subpath opacity.
3719 if (p->ghostline == MagickFalse)
3722 if ((*stroke_alpha < 1.0) &&
3723 (distance <= ((alpha+0.25)*(alpha+0.25))))
3726 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3731 if (distance != 1.0)
3732 beta=sqrt((double) distance);
3734 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3735 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3739 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3741 if (distance <= 0.0)
3751 if (distance != 1.0)
3752 beta=sqrt(distance);
3755 if (subpath_alpha < (alpha*alpha))
3756 subpath_alpha=alpha*alpha;
3760 Compute fill opacity.
3762 if (fill == MagickFalse)
3764 if (subpath_alpha >= 1.0)
3767 Determine winding number.
3770 p=polygon_info->edges;
3771 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3773 if ((double) y <= p->bounds.y1)
3775 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3777 if ((double) x > p->bounds.x2)
3779 winding_number+=p->direction ? 1 : -1;
3782 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3783 for ( ; i < (ssize_t) p->number_points; i++)
3784 if ((double) y <= p->points[i].y)
3787 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3788 winding_number+=p->direction ? 1 : -1;
3790 if (fill_rule != NonZeroRule)
3792 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3796 if (MagickAbsoluteValue(winding_number) != 0)
3798 return(subpath_alpha);
3801 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3802 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3803 ExceptionInfo *exception)
3816 **restrict polygon_info;
3833 Compute bounding box.
3835 assert(image != (Image *) NULL);
3836 assert(image->signature == MagickSignature);
3837 if (image->debug != MagickFalse)
3838 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3839 assert(draw_info != (DrawInfo *) NULL);
3840 assert(draw_info->signature == MagickSignature);
3841 assert(primitive_info != (PrimitiveInfo *) NULL);
3842 if (primitive_info->coordinates == 0)
3844 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3845 if (polygon_info == (PolygonInfo **) NULL)
3846 return(MagickFalse);
3847 DisableMSCWarning(4127)
3849 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3851 if (image->debug != MagickFalse)
3852 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3853 fill=(primitive_info->method == FillToBorderMethod) ||
3854 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3855 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3856 bounds=polygon_info[0]->edges[0].bounds;
3857 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3859 p=polygon_info[0]->edges+i;
3860 if (p->bounds.x1 < bounds.x1)
3861 bounds.x1=p->bounds.x1;
3862 if (p->bounds.y1 < bounds.y1)
3863 bounds.y1=p->bounds.y1;
3864 if (p->bounds.x2 > bounds.x2)
3865 bounds.x2=p->bounds.x2;
3866 if (p->bounds.y2 > bounds.y2)
3867 bounds.y2=p->bounds.y2;
3869 bounds.x1-=(mid+1.0);
3870 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3871 image->columns ? (double) image->columns-1 : bounds.x1;
3872 bounds.y1-=(mid+1.0);
3873 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3874 image->rows ? (double) image->rows-1 : bounds.y1;
3875 bounds.x2+=(mid+1.0);
3876 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3877 image->columns ? (double) image->columns-1 : bounds.x2;
3878 bounds.y2+=(mid+1.0);
3879 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3880 image->rows ? (double) image->rows-1 : bounds.y2;
3882 image_view=AcquireAuthenticCacheView(image,exception);
3883 if (primitive_info->coordinates == 1)
3888 start=(ssize_t) ceil(bounds.y1-0.5);
3889 stop=(ssize_t) floor(bounds.y2+0.5);
3890 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3891 #pragma omp parallel for schedule(static,4) shared(status) \
3892 magick_threads(image,image,1,1)
3894 for (y=start; y <= stop; y++)
3912 if (status == MagickFalse)
3914 start=(ssize_t) ceil(bounds.x1-0.5);
3915 stop=(ssize_t) floor(bounds.x2+0.5);
3917 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3919 if (q == (Quantum *) NULL)
3924 GetPixelInfo(image,&pixel);
3925 for ( ; x <= stop; x++)
3927 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3928 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3930 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3931 SetPixelInfoPixel(image,&pixel,q);
3933 q+=GetPixelChannels(image);
3935 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3936 if (sync == MagickFalse)
3939 image_view=DestroyCacheView(image_view);
3940 polygon_info=DestroyPolygonThreadSet(polygon_info);
3941 if (image->debug != MagickFalse)
3942 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3943 " end draw-polygon");
3947 Draw polygon or line.
3949 if (image->alpha_trait != BlendPixelTrait)
3950 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3951 start=(ssize_t) ceil(bounds.y1-0.5);
3952 stop=(ssize_t) floor(bounds.y2+0.5);
3953 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3954 #pragma omp parallel for schedule(static,4) shared(status) \
3955 magick_threads(image,image,1,1)
3957 for (y=start; y <= stop; y++)
3960 id = GetOpenMPThreadId();
3980 if (status == MagickFalse)
3982 start=(ssize_t) ceil(bounds.x1-0.5);
3983 stop=(ssize_t) floor(bounds.x2+0.5);
3984 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3986 if (q == (Quantum *) NULL)
3991 for (x=start; x <= stop; x++)
3996 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3998 if (draw_info->stroke_antialias == MagickFalse)
4000 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4001 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4003 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4004 fill_alpha=fill_alpha*fill_color.alpha;
4005 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4006 GetPixelAlpha(image,q),q);
4007 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
4008 stroke_alpha=stroke_alpha*stroke_color.alpha;
4009 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4010 GetPixelAlpha(image,q),q);
4011 q+=GetPixelChannels(image);
4013 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4016 image_view=DestroyCacheView(image_view);
4017 polygon_info=DestroyPolygonThreadSet(polygon_info);
4018 if (image->debug != MagickFalse)
4019 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4028 % D r a w P r i m i t i v e %
4032 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4034 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4036 % The format of the DrawPrimitive method is:
4038 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4039 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4041 % A description of each parameter follows:
4043 % o image: the image.
4045 % o draw_info: the draw info.
4047 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4049 % o exception: return any errors or warnings in this structure.
4053 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4079 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4080 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4081 switch (primitive_info->primitive)
4083 case PointPrimitive:
4085 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4086 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4087 methods[primitive_info->method]);
4090 case ColorPrimitive:
4092 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4093 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4094 methods[primitive_info->method]);
4097 case MattePrimitive:
4099 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4100 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4101 methods[primitive_info->method]);
4106 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4107 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4110 case ImagePrimitive:
4112 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4113 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4120 p=primitive_info[0].point;
4123 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4125 point=primitive_info[i].point;
4126 if (coordinates <= 0)
4128 coordinates=(ssize_t) primitive_info[i].coordinates;
4129 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4130 " begin open (%.20g)",(double) coordinates);
4133 point=primitive_info[i].point;
4134 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4135 (fabs(q.y-point.y) >= MagickEpsilon))
4136 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4137 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4139 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4140 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4143 if (coordinates > 0)
4145 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4146 (fabs(p.y-point.y) >= MagickEpsilon))
4147 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4148 (double) coordinates);
4150 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4151 (double) coordinates);
4155 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4156 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4157 ExceptionInfo *exception)
4172 if (image->debug != MagickFalse)
4174 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4175 " begin draw-primitive");
4176 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4177 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4178 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4179 draw_info->affine.tx,draw_info->affine.ty);
4181 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4182 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4183 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4184 (void) SetImageColorspace(image,sRGBColorspace,exception);
4186 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4187 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4188 image_view=AcquireAuthenticCacheView(image,exception);
4189 switch (primitive_info->primitive)
4191 case PointPrimitive:
4199 if ((y < 0) || (y >= (ssize_t) image->rows))
4201 if ((x < 0) || (x >= (ssize_t) image->columns))
4203 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4204 if (q == (Quantum *) NULL)
4206 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4207 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4208 (double) GetPixelAlpha(image,q),q);
4209 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4212 case ColorPrimitive:
4214 switch (primitive_info->method)
4225 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4226 if (q == (Quantum *) NULL)
4228 GetPixelInfo(image,&pixel);
4229 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4230 SetPixelInfoPixel(image,&pixel,q);
4231 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4243 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4245 for (y=0; y < (ssize_t) image->rows; y++)
4250 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4252 if (q == (Quantum *) NULL)
4254 for (x=0; x < (ssize_t) image->columns; x++)
4256 GetPixelInfoPixel(image,q,&pixel);
4257 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4259 q+=GetPixelChannels(image);
4262 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4263 SetPixelInfoPixel(image,&pixel,q);
4264 q+=GetPixelChannels(image);
4266 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4267 if (sync == MagickFalse)
4272 case FloodfillMethod:
4273 case FillToBorderMethod:
4278 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4280 if (primitive_info->method == FillToBorderMethod)
4282 target.red=(double) draw_info->border_color.red;
4283 target.green=(double) draw_info->border_color.green;
4284 target.blue=(double) draw_info->border_color.blue;
4286 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4287 primitive_info->method == FloodfillMethod ? MagickFalse :
4288 MagickTrue,exception);
4299 GetPixelInfo(image,&pixel);
4300 for (y=0; y < (ssize_t) image->rows; y++)
4308 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4310 if (q == (Quantum *) NULL)
4312 for (x=0; x < (ssize_t) image->columns; x++)
4314 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4315 SetPixelInfoPixel(image,&pixel,q);
4316 q+=GetPixelChannels(image);
4318 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4319 if (sync == MagickFalse)
4327 case MattePrimitive:
4329 if (image->alpha_trait != BlendPixelTrait)
4330 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4331 switch (primitive_info->method)
4342 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4343 if (q == (Quantum *) NULL)
4345 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4346 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4347 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4359 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4361 GetPixelInfo(image,&pixel);
4362 for (y=0; y < (ssize_t) image->rows; y++)
4370 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4372 if (q == (Quantum *) NULL)
4374 for (x=0; x < (ssize_t) image->columns; x++)
4376 GetPixelInfoPixel(image,q,&pixel);
4377 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4379 q+=GetPixelChannels(image);
4382 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4383 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4384 q+=GetPixelChannels(image);
4386 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4387 if (sync == MagickFalse)
4392 case FloodfillMethod:
4393 case FillToBorderMethod:
4401 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4403 if (primitive_info->method == FillToBorderMethod)
4405 target.red=(double) draw_info->border_color.red;
4406 target.green=(double) draw_info->border_color.green;
4407 target.blue=(double) draw_info->border_color.blue;
4409 channel_mask=SetImageChannelMask(image,AlphaChannel);
4410 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4411 primitive_info->method == FloodfillMethod ? MagickFalse :
4412 MagickTrue,exception);
4413 (void) SetImageChannelMask(image,channel_mask);
4424 for (y=0; y < (ssize_t) image->rows; y++)
4432 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4434 if (q == (Quantum *) NULL)
4436 for (x=0; x < (ssize_t) image->columns; x++)
4438 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4439 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4440 q+=GetPixelChannels(image);
4442 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4443 if (sync == MagickFalse)
4454 geometry[MaxTextExtent];
4459 if (primitive_info->text == (char *) NULL)
4461 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4462 (void) CloneString(&clone_info->text,primitive_info->text);
4463 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4464 primitive_info->point.x,primitive_info->point.y);
4465 (void) CloneString(&clone_info->geometry,geometry);
4466 status&=AnnotateImage(image,clone_info,exception);
4467 clone_info=DestroyDrawInfo(clone_info);
4470 case ImagePrimitive:
4476 composite_geometry[MaxTextExtent];
4491 if (primitive_info->text == (char *) NULL)
4493 clone_info=AcquireImageInfo();
4494 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4495 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4499 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4501 composite_image=ReadImage(clone_info,exception);
4503 clone_info=DestroyImageInfo(clone_info);
4504 if (composite_image == (Image *) NULL)
4506 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4507 NULL,(void *) NULL);
4508 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4509 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4510 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4511 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4514 geometry[MaxTextExtent];
4519 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4520 primitive_info[1].point.x,primitive_info[1].point.y);
4521 composite_image->filter=image->filter;
4522 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4525 if (composite_image->alpha_trait != BlendPixelTrait)
4526 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4528 if (draw_info->alpha != OpaqueAlpha)
4529 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4530 SetGeometry(image,&geometry);
4531 image->gravity=draw_info->gravity;
4534 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4535 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4536 composite_image->rows,(double) geometry.x,(double) geometry.y);
4537 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4538 affine=draw_info->affine;
4539 affine.tx=(double) geometry.x;
4540 affine.ty=(double) geometry.y;
4541 composite_image->interpolate=image->interpolate;
4542 if (draw_info->compose == OverCompositeOp)
4543 (void) DrawAffineImage(image,composite_image,&affine,exception);
4545 (void) CompositeImage(image,composite_image,draw_info->compose,
4546 MagickTrue,geometry.x,geometry.y,exception);
4547 composite_image=DestroyImage(composite_image);
4559 if (IsEventLogging() != MagickFalse)
4560 LogPrimitiveInfo(primitive_info);
4561 scale=ExpandAffine(&draw_info->affine);
4562 if ((draw_info->dash_pattern != (double *) NULL) &&
4563 (draw_info->dash_pattern[0] != 0.0) &&
4564 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4565 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4570 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4571 clone_info->stroke_width=0.0;
4572 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4573 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4575 clone_info=DestroyDrawInfo(clone_info);
4576 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4579 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4581 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4587 Draw strokes while respecting line cap/join attributes.
4589 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4591 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4592 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4593 MagickTrue : MagickFalse;
4594 i=(ssize_t) primitive_info[0].coordinates;
4595 if ((((draw_info->linecap == RoundCap) ||
4596 (closed_path != MagickFalse)) &&
4597 (draw_info->linejoin == RoundJoin)) ||
4598 (primitive_info[i].primitive != UndefinedPrimitive))
4600 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4604 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4605 clone_info->stroke_width=0.0;
4606 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4607 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4609 clone_info=DestroyDrawInfo(clone_info);
4610 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4613 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4617 image_view=DestroyCacheView(image_view);
4618 if (image->debug != MagickFalse)
4619 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4620 return(status != 0 ? MagickTrue : MagickFalse);
4624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4628 + D r a w S t r o k e P o l y g o n %
4632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4634 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4635 % the image while respecting the line cap and join attributes.
4637 % The format of the DrawStrokePolygon method is:
4639 % MagickBooleanType DrawStrokePolygon(Image *image,
4640 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4642 % A description of each parameter follows:
4644 % o image: the image.
4646 % o draw_info: the draw info.
4648 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4653 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4654 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4662 for (i=0; i < 4; i++)
4663 linecap[i]=(*primitive_info);
4664 linecap[0].coordinates=4;
4665 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4666 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4667 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4668 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4669 linecap[4].primitive=UndefinedPrimitive;
4670 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4673 static MagickBooleanType DrawStrokePolygon(Image *image,
4674 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4675 ExceptionInfo *exception)
4689 register const PrimitiveInfo
4694 Draw stroked polygon.
4696 if (image->debug != MagickFalse)
4697 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4698 " begin draw-stroke-polygon");
4699 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4700 clone_info->fill=draw_info->stroke;
4701 if (clone_info->fill_pattern != (Image *) NULL)
4702 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4703 if (clone_info->stroke_pattern != (Image *) NULL)
4704 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4705 MagickTrue,exception);
4706 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4707 clone_info->stroke_width=0.0;
4708 clone_info->fill_rule=NonZeroRule;
4710 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4712 stroke_polygon=TraceStrokePolygon(draw_info,p);
4713 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4716 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4717 q=p+p->coordinates-1;
4718 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4719 MagickTrue : MagickFalse;
4720 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4722 DrawRoundLinecap(image,draw_info,p,exception);
4723 DrawRoundLinecap(image,draw_info,q,exception);
4726 clone_info=DestroyDrawInfo(clone_info);
4727 if (image->debug != MagickFalse)
4728 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4729 " end draw-stroke-polygon");
4730 return(status != 0 ? MagickTrue : MagickFalse);
4734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4738 % G e t A f f i n e M a t r i x %
4742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4744 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4747 % The format of the GetAffineMatrix method is:
4749 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4751 % A description of each parameter follows:
4753 % o affine_matrix: the affine matrix.
4756 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4758 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4759 assert(affine_matrix != (AffineMatrix *) NULL);
4760 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4761 affine_matrix->sx=1.0;
4762 affine_matrix->sy=1.0;
4766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4770 + G e t D r a w I n f o %
4774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4776 % GetDrawInfo() initializes draw_info to default values from image_info.
4778 % The format of the GetDrawInfo method is:
4780 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4782 % A description of each parameter follows:
4784 % o image_info: the image info..
4786 % o draw_info: the draw info.
4789 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4798 Initialize draw attributes.
4800 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4801 assert(draw_info != (DrawInfo *) NULL);
4802 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4803 GetAffineMatrix(&draw_info->affine);
4804 exception=AcquireExceptionInfo();
4805 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4807 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4809 draw_info->stroke_width=1.0;
4810 draw_info->alpha=OpaqueAlpha;
4811 draw_info->fill_rule=EvenOddRule;
4812 draw_info->linecap=ButtCap;
4813 draw_info->linejoin=MiterJoin;
4814 draw_info->miterlimit=10;
4815 draw_info->decorate=NoDecoration;
4816 draw_info->pointsize=12.0;
4817 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4818 draw_info->compose=OverCompositeOp;
4819 draw_info->render=MagickTrue;
4820 draw_info->debug=IsEventLogging();
4821 if (image_info != (ImageInfo *) NULL)
4823 draw_info->stroke_antialias=image_info->antialias;
4824 if (image_info->font != (char *) NULL)
4825 draw_info->font=AcquireString(image_info->font);
4826 if (image_info->density != (char *) NULL)
4827 draw_info->density=AcquireString(image_info->density);
4828 draw_info->text_antialias=image_info->antialias;
4829 if (image_info->pointsize != 0.0)
4830 draw_info->pointsize=image_info->pointsize;
4831 draw_info->border_color=image_info->border_color;
4832 if (image_info->server_name != (char *) NULL)
4833 draw_info->server_name=AcquireString(image_info->server_name);
4834 option=GetImageOption(image_info,"encoding");
4835 if (option != (const char *) NULL)
4836 (void) CloneString(&draw_info->encoding,option);
4837 option=GetImageOption(image_info,"kerning");
4838 if (option != (const char *) NULL)
4839 draw_info->kerning=StringToDouble(option,(char **) NULL);
4840 option=GetImageOption(image_info,"interline-spacing");
4841 if (option != (const char *) NULL)
4842 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4843 option=GetImageOption(image_info,"interword-spacing");
4844 if (option != (const char *) NULL)
4845 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4846 option=GetImageOption(image_info,"direction");
4847 if (option != (const char *) NULL)
4848 draw_info->direction=(DirectionType) ParseCommandOption(
4849 MagickDirectionOptions,MagickFalse,option);
4851 draw_info->direction=UndefinedDirection;
4852 option=GetImageOption(image_info,"fill");
4853 if (option != (const char *) NULL)
4854 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4856 option=GetImageOption(image_info,"stroke");
4857 if (option != (const char *) NULL)
4858 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4860 option=GetImageOption(image_info,"strokewidth");
4861 if (option != (const char *) NULL)
4862 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4863 option=GetImageOption(image_info,"undercolor");
4864 if (option != (const char *) NULL)
4865 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4867 option=GetImageOption(image_info,"gravity");
4868 if (option != (const char *) NULL)
4869 draw_info->gravity=(GravityType) ParseCommandOption(
4870 MagickGravityOptions,MagickFalse,option);
4872 exception=DestroyExceptionInfo(exception);
4873 draw_info->signature=MagickSignature;
4877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4881 + P e r m u t a t e %
4885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4887 % Permutate() returns the permuation of the (n,k).
4889 % The format of the Permutate method is:
4891 % void Permutate(ssize_t n,ssize_t k)
4893 % A description of each parameter follows:
4901 static inline double Permutate(const ssize_t n,const ssize_t k)
4910 for (i=k+1; i <= n; i++)
4912 for (i=1; i <= (n-k); i++)
4918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4922 + T r a c e P r i m i t i v e %
4926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4928 % TracePrimitive is a collection of methods for generating graphic
4929 % primitives such as arcs, ellipses, paths, etc.
4933 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4934 const PointInfo end,const PointInfo degrees)
4940 center.x=0.5*(end.x+start.x);
4941 center.y=0.5*(end.y+start.y);
4942 radii.x=fabs(center.x-start.x);
4943 radii.y=fabs(center.y-start.y);
4944 TraceEllipse(primitive_info,center,radii,degrees);
4947 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4948 const PointInfo end,const PointInfo arc,const double angle,
4949 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4968 register PrimitiveInfo
4977 if ((start.x == end.x) && (start.y == end.y))
4979 TracePoint(primitive_info,end);
4982 radii.x=fabs(arc.x);
4983 radii.y=fabs(arc.y);
4984 if ((radii.x == 0.0) || (radii.y == 0.0))
4986 TraceLine(primitive_info,start,end);
4989 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4990 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4991 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4992 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4993 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4995 if (delta < MagickEpsilon)
4997 TraceLine(primitive_info,start,end);
5002 radii.x*=sqrt((double) delta);
5003 radii.y*=sqrt((double) delta);
5005 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5006 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5007 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5008 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5009 alpha=points[1].x-points[0].x;
5010 beta=points[1].y-points[0].y;
5011 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5016 factor=sqrt((double) factor);
5017 if (sweep == large_arc)
5020 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5021 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5022 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5023 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5024 if ((theta < 0.0) && (sweep != MagickFalse))
5025 theta+=(double) (2.0*MagickPI);
5027 if ((theta > 0.0) && (sweep == MagickFalse))
5028 theta-=(double) (2.0*MagickPI);
5029 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5032 for (i=0; i < (ssize_t) arc_segments; i++)
5034 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5035 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5036 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5037 sin(fmod((double) beta,DegreesToRadians(360.0)));
5038 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5039 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5040 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5041 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5042 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5043 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5044 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5045 theta/arc_segments),DegreesToRadians(360.0))));
5046 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5047 theta/arc_segments),DegreesToRadians(360.0))));
5048 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5049 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5050 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5051 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5052 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5053 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5054 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5056 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5058 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5060 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5062 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5064 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5066 if (i == (ssize_t) (arc_segments-1))
5071 primitive_info->coordinates=(size_t) (p-primitive_info);
5072 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5074 p->primitive=primitive_info->primitive;
5079 static void TraceBezier(PrimitiveInfo *primitive_info,
5080 const size_t number_coordinates)
5092 register PrimitiveInfo
5104 Allocate coeficients.
5106 quantum=number_coordinates;
5107 for (i=0; i < (ssize_t) number_coordinates; i++)
5109 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5111 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5112 if (alpha > (double) quantum)
5113 quantum=(size_t) alpha;
5114 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5115 if (alpha > (double) quantum)
5116 quantum=(size_t) alpha;
5119 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5120 (double) BezierQuantum);
5121 control_points=quantum*number_coordinates;
5122 coefficients=(double *) AcquireQuantumMemory((size_t)
5123 number_coordinates,sizeof(*coefficients));
5124 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5126 if ((coefficients == (double *) NULL) ||
5127 (points == (PointInfo *) NULL))
5128 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5130 Compute bezier points.
5132 end=primitive_info[number_coordinates-1].point;
5133 for (i=0; i < (ssize_t) number_coordinates; i++)
5134 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5136 for (i=0; i < (ssize_t) control_points; i++)
5141 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5142 for (j=0; j < (ssize_t) number_coordinates; j++)
5144 point.x+=alpha*coefficients[j]*p->point.x;
5145 point.y+=alpha*coefficients[j]*p->point.y;
5146 alpha*=weight/(1.0-weight);
5150 weight+=1.0/control_points;
5153 Bezier curves are just short segmented polys.
5156 for (i=0; i < (ssize_t) control_points; i++)
5158 TracePoint(p,points[i]);
5163 primitive_info->coordinates=(size_t) (p-primitive_info);
5164 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5166 p->primitive=primitive_info->primitive;
5169 points=(PointInfo *) RelinquishMagickMemory(points);
5170 coefficients=(double *) RelinquishMagickMemory(coefficients);
5173 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5174 const PointInfo end)
5185 alpha=end.x-start.x;
5187 radius=hypot((double) alpha,(double) beta);
5188 offset.x=(double) radius;
5189 offset.y=(double) radius;
5192 TraceEllipse(primitive_info,start,offset,degrees);
5195 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5196 const PointInfo stop,const PointInfo degrees)
5207 register PrimitiveInfo
5214 Ellipses are just short segmented polys.
5216 if ((stop.x == 0.0) && (stop.y == 0.0))
5218 TracePoint(primitive_info,start);
5221 delta=2.0/MagickMax(stop.x,stop.y);
5222 step=(double) (MagickPI/8.0);
5223 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5224 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5225 angle.x=DegreesToRadians(degrees.x);
5227 while (y < degrees.x)
5229 angle.y=(double) DegreesToRadians(y);
5230 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5232 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5233 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5234 TracePoint(p,point);
5237 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5238 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5239 TracePoint(p,point);
5241 primitive_info->coordinates=(size_t) (p-primitive_info);
5242 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5244 p->primitive=primitive_info->primitive;
5249 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5250 const PointInfo end)
5252 TracePoint(primitive_info,start);
5253 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5254 (fabs(start.y-end.y) < MagickEpsilon))
5256 primitive_info->primitive=PointPrimitive;
5257 primitive_info->coordinates=1;
5260 TracePoint(primitive_info+1,end);
5261 (primitive_info+1)->primitive=primitive_info->primitive;
5262 primitive_info->coordinates=2;
5265 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5268 token[MaxTextExtent];
5290 register PrimitiveInfo
5307 number_coordinates=0;
5309 (void) ResetMagickMemory(points,0,sizeof(*points));
5310 primitive_type=primitive_info->primitive;
5312 for (p=path; *p != '\0'; )
5314 while (isspace((int) ((unsigned char) *p)) != 0)
5318 last_attribute=attribute;
5319 attribute=(int) (*p++);
5340 GetMagickToken(p,&p,token);
5342 GetMagickToken(p,&p,token);
5343 arc.x=StringToDouble(token,(char **) NULL);
5344 GetMagickToken(p,&p,token);
5346 GetMagickToken(p,&p,token);
5347 arc.y=StringToDouble(token,(char **) NULL);
5348 GetMagickToken(p,&p,token);
5350 GetMagickToken(p,&p,token);
5351 angle=StringToDouble(token,(char **) NULL);
5352 GetMagickToken(p,&p,token);
5354 GetMagickToken(p,&p,token);
5355 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5356 GetMagickToken(p,&p,token);
5358 GetMagickToken(p,&p,token);
5359 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5360 GetMagickToken(p,&p,token);
5362 GetMagickToken(p,&p,token);
5363 x=StringToDouble(token,(char **) NULL);
5364 GetMagickToken(p,&p,token);
5366 GetMagickToken(p,&p,token);
5367 y=StringToDouble(token,(char **) NULL);
5368 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5369 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5370 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5373 while (isspace((int) ((unsigned char) *p)) != 0)
5377 } while (IsPoint(p) != MagickFalse);
5384 Compute bezier points.
5389 for (i=1; i < 4; i++)
5391 GetMagickToken(p,&p,token);
5393 GetMagickToken(p,&p,token);
5394 x=StringToDouble(token,(char **) NULL);
5395 GetMagickToken(p,&p,token);
5397 GetMagickToken(p,&p,token);
5398 y=StringToDouble(token,(char **) NULL);
5399 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5400 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5403 for (i=0; i < 4; i++)
5404 (q+i)->point=points[i];
5408 } while (IsPoint(p) != MagickFalse);
5416 GetMagickToken(p,&p,token);
5418 GetMagickToken(p,&p,token);
5419 x=StringToDouble(token,(char **) NULL);
5420 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5421 TracePoint(q,point);
5423 } while (IsPoint(p) != MagickFalse);
5431 GetMagickToken(p,&p,token);
5433 GetMagickToken(p,&p,token);
5434 x=StringToDouble(token,(char **) NULL);
5435 GetMagickToken(p,&p,token);
5437 GetMagickToken(p,&p,token);
5438 y=StringToDouble(token,(char **) NULL);
5439 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5440 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5441 TracePoint(q,point);
5443 } while (IsPoint(p) != MagickFalse);
5449 if (q != primitive_info)
5451 primitive_info->coordinates=(size_t) (q-primitive_info);
5452 number_coordinates+=primitive_info->coordinates;
5458 GetMagickToken(p,&p,token);
5460 GetMagickToken(p,&p,token);
5461 x=StringToDouble(token,(char **) NULL);
5462 GetMagickToken(p,&p,token);
5464 GetMagickToken(p,&p,token);
5465 y=StringToDouble(token,(char **) NULL);
5466 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5467 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5471 TracePoint(q,point);
5473 if ((i != 0) && (attribute == (int) 'M'))
5475 TracePoint(q,point);
5478 } while (IsPoint(p) != MagickFalse);
5485 Compute bezier points.
5490 for (i=1; i < 3; i++)
5492 GetMagickToken(p,&p,token);
5494 GetMagickToken(p,&p,token);
5495 x=StringToDouble(token,(char **) NULL);
5496 GetMagickToken(p,&p,token);
5498 GetMagickToken(p,&p,token);
5499 y=StringToDouble(token,(char **) NULL);
5502 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5503 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5506 for (i=0; i < 3; i++)
5507 (q+i)->point=points[i];
5511 } while (IsPoint(p) != MagickFalse);
5518 Compute bezier points.
5522 points[0]=points[3];
5523 points[1].x=2.0*points[3].x-points[2].x;
5524 points[1].y=2.0*points[3].y-points[2].y;
5525 for (i=2; i < 4; i++)
5527 GetMagickToken(p,&p,token);
5529 GetMagickToken(p,&p,token);
5530 x=StringToDouble(token,(char **) NULL);
5531 GetMagickToken(p,&p,token);
5533 GetMagickToken(p,&p,token);
5534 y=StringToDouble(token,(char **) NULL);
5537 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5538 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5541 if (strchr("CcSs",last_attribute) == (char *) NULL)
5543 points[0]=points[2];
5544 points[1]=points[3];
5546 for (i=0; i < 4; i++)
5547 (q+i)->point=points[i];
5551 } while (IsPoint(p) != MagickFalse);
5558 Compute bezier points.
5562 points[0]=points[2];
5563 points[1].x=2.0*points[2].x-points[1].x;
5564 points[1].y=2.0*points[2].y-points[1].y;
5565 for (i=2; i < 3; i++)
5567 GetMagickToken(p,&p,token);
5569 GetMagickToken(p,&p,token);
5570 x=StringToDouble(token,(char **) NULL);
5571 GetMagickToken(p,&p,token);
5573 GetMagickToken(p,&p,token);
5574 y=StringToDouble(token,(char **) NULL);
5575 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5576 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5579 if (strchr("QqTt",last_attribute) == (char *) NULL)
5581 points[0]=points[2];
5582 points[1]=points[3];
5584 for (i=0; i < 3; i++)
5585 (q+i)->point=points[i];
5589 } while (IsPoint(p) != MagickFalse);
5597 GetMagickToken(p,&p,token);
5599 GetMagickToken(p,&p,token);
5600 y=StringToDouble(token,(char **) NULL);
5601 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5602 TracePoint(q,point);
5604 } while (IsPoint(p) != MagickFalse);
5611 TracePoint(q,point);
5613 primitive_info->coordinates=(size_t) (q-primitive_info);
5614 number_coordinates+=primitive_info->coordinates;
5621 if (isalpha((int) ((unsigned char) attribute)) != 0)
5622 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5628 primitive_info->coordinates=(size_t) (q-primitive_info);
5629 number_coordinates+=primitive_info->coordinates;
5630 for (i=0; i < (ssize_t) number_coordinates; i++)
5633 q->primitive=primitive_type;
5635 q->method=FillToBorderMethod;
5638 return(number_coordinates);
5641 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5642 const PointInfo end)
5647 register PrimitiveInfo
5654 TracePoint(p,start);
5658 TracePoint(p,point);
5664 TracePoint(p,point);
5666 TracePoint(p,start);
5668 primitive_info->coordinates=(size_t) (p-primitive_info);
5669 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5671 p->primitive=primitive_info->primitive;
5676 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5677 const PointInfo start,const PointInfo end,PointInfo arc)
5684 register PrimitiveInfo
5691 offset.x=fabs(end.x-start.x);
5692 offset.y=fabs(end.y-start.y);
5693 if (arc.x > (0.5*offset.x))
5695 if (arc.y > (0.5*offset.y))
5697 point.x=start.x+offset.x-arc.x;
5698 point.y=start.y+arc.y;
5701 TraceEllipse(p,point,arc,degrees);
5703 point.x=start.x+offset.x-arc.x;
5704 point.y=start.y+offset.y-arc.y;
5707 TraceEllipse(p,point,arc,degrees);
5709 point.x=start.x+arc.x;
5710 point.y=start.y+offset.y-arc.y;
5713 TraceEllipse(p,point,arc,degrees);
5715 point.x=start.x+arc.x;
5716 point.y=start.y+arc.y;
5719 TraceEllipse(p,point,arc,degrees);
5721 TracePoint(p,primitive_info->point);
5723 primitive_info->coordinates=(size_t) (p-primitive_info);
5724 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5726 p->primitive=primitive_info->primitive;
5731 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5732 const size_t number_vertices,const double offset)
5749 for (i=1; i < (ssize_t) number_vertices; i++)
5751 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5752 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5753 if ((fabs((double) dx) >= MagickEpsilon) ||
5754 (fabs((double) dy) >= MagickEpsilon))
5757 if (i == (ssize_t) number_vertices)
5758 i=(ssize_t) number_vertices-1L;
5759 distance=hypot((double) dx,(double) dy);
5760 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5761 dx*(distance+offset)/distance);
5762 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5763 dy*(distance+offset)/distance);
5764 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5766 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5767 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5768 if ((fabs((double) dx) >= MagickEpsilon) ||
5769 (fabs((double) dy) >= MagickEpsilon))
5772 distance=hypot((double) dx,(double) dy);
5773 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5774 dx*(distance+offset)/distance);
5775 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5776 dy*(distance+offset)/distance);
5779 static inline double DrawEpsilonReciprocal(const double x)
5781 #define DrawEpsilon ((double) 1.0e-10)
5783 double sign = x < (double) 0.0 ? (double) -1.0 :
5785 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5786 (double) 1.0/DrawEpsilon));
5789 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5790 const PrimitiveInfo *primitive_info)
5792 typedef struct _LineSegment
5844 number_vertices=primitive_info->coordinates;
5845 max_strokes=2*number_vertices+6*BezierQuantum+360;
5846 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5848 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5850 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5851 number_vertices+2UL,sizeof(*polygon_primitive));
5852 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5853 (polygon_primitive == (PrimitiveInfo *) NULL))
5854 return((PrimitiveInfo *) NULL);
5855 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5856 number_vertices*sizeof(*polygon_primitive));
5858 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5859 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5860 MagickTrue : MagickFalse;
5861 if ((draw_info->linejoin == RoundJoin) ||
5862 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5864 polygon_primitive[number_vertices]=primitive_info[1];
5867 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5869 Compute the slope for the first line segment, p.
5873 for (n=1; n < (ssize_t) number_vertices; n++)
5875 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5876 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5877 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5880 if (n == (ssize_t) number_vertices)
5881 n=(ssize_t) number_vertices-1L;
5882 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5883 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5884 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5885 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5887 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5888 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5889 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5890 offset.y=(double) (offset.x*inverse_slope.p);
5891 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5893 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5894 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5895 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5896 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5897 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5898 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5899 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5900 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5904 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5905 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5906 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5907 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5908 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5909 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5910 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5911 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5914 Create strokes for the line join attribute: bevel, miter, round.
5918 path_q[p++]=box_q[0];
5919 path_p[q++]=box_p[0];
5920 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5923 Compute the slope for this line segment, q.
5925 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5926 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5927 dot_product=dx.q*dx.q+dy.q*dy.q;
5928 if (dot_product < 0.25)
5930 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5931 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5932 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5933 offset.y=(double) (offset.x*inverse_slope.q);
5934 dot_product=dy.q*offset.x-dx.q*offset.y;
5935 if (dot_product > 0.0)
5937 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5938 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5939 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5940 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5941 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5942 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5943 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5944 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5948 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5949 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5950 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5951 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5952 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5953 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5954 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5955 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5957 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5964 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5965 box_p[3].y)/(slope.p-slope.q));
5966 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5967 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5968 box_q[3].y)/(slope.p-slope.q));
5969 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5971 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5973 max_strokes+=6*BezierQuantum+360;
5974 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5976 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5978 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5980 polygon_primitive=(PrimitiveInfo *)
5981 RelinquishMagickMemory(polygon_primitive);
5982 return((PrimitiveInfo *) NULL);
5985 dot_product=dx.q*dy.p-dx.p*dy.q;
5986 if (dot_product <= 0.0)
5987 switch (draw_info->linejoin)
5991 path_q[q++]=box_q[1];
5992 path_q[q++]=box_q[2];
5993 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5994 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5995 if (dot_product <= miterlimit)
5996 path_p[p++]=box_p[4];
5999 path_p[p++]=box_p[1];
6000 path_p[p++]=box_p[2];
6006 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6007 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6008 if (dot_product <= miterlimit)
6010 path_q[q++]=box_q[4];
6011 path_p[p++]=box_p[4];
6015 path_q[q++]=box_q[1];
6016 path_q[q++]=box_q[2];
6017 path_p[p++]=box_p[1];
6018 path_p[p++]=box_p[2];
6024 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6025 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6026 if (dot_product <= miterlimit)
6027 path_p[p++]=box_p[4];
6030 path_p[p++]=box_p[1];
6031 path_p[p++]=box_p[2];
6033 center=polygon_primitive[n].point;
6034 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6035 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6036 if (theta.q < theta.p)
6037 theta.q+=(double) (2.0*MagickPI);
6038 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6039 (2.0*sqrt((double) (1.0/mid)))));
6040 path_q[q].x=box_q[1].x;
6041 path_q[q].y=box_q[1].y;
6043 for (j=1; j < (ssize_t) arc_segments; j++)
6045 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6046 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6047 (theta.p+delta_theta),DegreesToRadians(360.0))));
6048 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6049 (theta.p+delta_theta),DegreesToRadians(360.0))));
6052 path_q[q++]=box_q[2];
6059 switch (draw_info->linejoin)
6063 path_p[p++]=box_p[1];
6064 path_p[p++]=box_p[2];
6065 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6066 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6067 if (dot_product <= miterlimit)
6068 path_q[q++]=box_q[4];
6071 path_q[q++]=box_q[1];
6072 path_q[q++]=box_q[2];
6078 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6079 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6080 if (dot_product <= miterlimit)
6082 path_q[q++]=box_q[4];
6083 path_p[p++]=box_p[4];
6087 path_q[q++]=box_q[1];
6088 path_q[q++]=box_q[2];
6089 path_p[p++]=box_p[1];
6090 path_p[p++]=box_p[2];
6096 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6097 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6098 if (dot_product <= miterlimit)
6099 path_q[q++]=box_q[4];
6102 path_q[q++]=box_q[1];
6103 path_q[q++]=box_q[2];
6105 center=polygon_primitive[n].point;
6106 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6107 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6108 if (theta.p < theta.q)
6109 theta.p+=(double) (2.0*MagickPI);
6110 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6111 (2.0*sqrt((double) (1.0/mid)))));
6112 path_p[p++]=box_p[1];
6113 for (j=1; j < (ssize_t) arc_segments; j++)
6115 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6116 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6117 (theta.p+delta_theta),DegreesToRadians(360.0))));
6118 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6119 (theta.p+delta_theta),DegreesToRadians(360.0))));
6122 path_p[p++]=box_p[2];
6129 inverse_slope.p=inverse_slope.q;
6138 path_p[p++]=box_p[1];
6139 path_q[q++]=box_q[1];
6141 Trace stroked polygon.
6143 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6144 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6145 if (stroke_polygon != (PrimitiveInfo *) NULL)
6147 for (i=0; i < (ssize_t) p; i++)
6149 stroke_polygon[i]=polygon_primitive[0];
6150 stroke_polygon[i].point=path_p[i];
6152 if (closed_path != MagickFalse)
6154 stroke_polygon[i]=polygon_primitive[0];
6155 stroke_polygon[i].point=stroke_polygon[0].point;
6158 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6160 stroke_polygon[i]=polygon_primitive[0];
6161 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6163 if (closed_path != MagickFalse)
6165 stroke_polygon[i]=polygon_primitive[0];
6166 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6169 stroke_polygon[i]=polygon_primitive[0];
6170 stroke_polygon[i].point=stroke_polygon[0].point;
6172 stroke_polygon[i].primitive=UndefinedPrimitive;
6173 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6175 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6176 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6177 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6178 return(stroke_polygon);