2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2016 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 if (clone_info->primitive != (char *) NULL)
257 (void) CloneString(&clone_info->primitive,draw_info->primitive);
258 if (draw_info->geometry != (char *) NULL)
259 (void) CloneString(&clone_info->geometry,draw_info->geometry);
260 clone_info->viewbox=draw_info->viewbox;
261 clone_info->affine=draw_info->affine;
262 clone_info->gravity=draw_info->gravity;
263 clone_info->fill=draw_info->fill;
264 clone_info->stroke=draw_info->stroke;
265 clone_info->stroke_width=draw_info->stroke_width;
266 if (draw_info->fill_pattern != (Image *) NULL)
267 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
269 if (draw_info->stroke_pattern != (Image *) NULL)
270 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
271 MagickTrue,exception);
272 clone_info->stroke_antialias=draw_info->stroke_antialias;
273 clone_info->text_antialias=draw_info->text_antialias;
274 clone_info->fill_rule=draw_info->fill_rule;
275 clone_info->linecap=draw_info->linecap;
276 clone_info->linejoin=draw_info->linejoin;
277 clone_info->miterlimit=draw_info->miterlimit;
278 clone_info->dash_offset=draw_info->dash_offset;
279 clone_info->decorate=draw_info->decorate;
280 clone_info->compose=draw_info->compose;
281 if (draw_info->text != (char *) NULL)
282 (void) CloneString(&clone_info->text,draw_info->text);
283 if (draw_info->font != (char *) NULL)
284 (void) CloneString(&clone_info->font,draw_info->font);
285 if (draw_info->font_features != (char *) NULL)
286 (void) CloneString(&clone_info->font_features,draw_info->font_features);
287 if (draw_info->metrics != (char *) NULL)
288 (void) CloneString(&clone_info->metrics,draw_info->metrics);
289 if (draw_info->family != (char *) NULL)
290 (void) CloneString(&clone_info->family,draw_info->family);
291 clone_info->style=draw_info->style;
292 clone_info->stretch=draw_info->stretch;
293 clone_info->weight=draw_info->weight;
294 if (draw_info->encoding != (char *) NULL)
295 (void) CloneString(&clone_info->encoding,draw_info->encoding);
296 clone_info->pointsize=draw_info->pointsize;
297 clone_info->kerning=draw_info->kerning;
298 clone_info->interline_spacing=draw_info->interline_spacing;
299 clone_info->interword_spacing=draw_info->interword_spacing;
300 clone_info->direction=draw_info->direction;
301 if (draw_info->density != (char *) NULL)
302 (void) CloneString(&clone_info->density,draw_info->density);
303 clone_info->align=draw_info->align;
304 clone_info->undercolor=draw_info->undercolor;
305 clone_info->border_color=draw_info->border_color;
306 if (draw_info->server_name != (char *) NULL)
307 (void) CloneString(&clone_info->server_name,draw_info->server_name);
308 if (draw_info->dash_pattern != (double *) NULL)
313 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
314 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
315 sizeof(*clone_info->dash_pattern));
316 if (clone_info->dash_pattern == (double *) NULL)
317 ThrowFatalException(ResourceLimitFatalError,
318 "UnableToAllocateDashPattern");
319 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
320 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
322 clone_info->gradient=draw_info->gradient;
323 if (draw_info->gradient.stops != (StopInfo *) NULL)
328 number_stops=clone_info->gradient.number_stops;
329 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
330 number_stops,sizeof(*clone_info->gradient.stops));
331 if (clone_info->gradient.stops == (StopInfo *) NULL)
332 ThrowFatalException(ResourceLimitFatalError,
333 "UnableToAllocateDashPattern");
334 (void) CopyMagickMemory(clone_info->gradient.stops,
335 draw_info->gradient.stops,(size_t) number_stops*
336 sizeof(*clone_info->gradient.stops));
338 if (draw_info->clip_mask != (char *) NULL)
339 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
340 clone_info->bounds=draw_info->bounds;
341 clone_info->clip_units=draw_info->clip_units;
342 clone_info->render=draw_info->render;
343 clone_info->alpha=draw_info->alpha;
344 clone_info->element_reference=draw_info->element_reference;
345 clone_info->debug=IsEventLogging();
346 exception=DestroyExceptionInfo(exception);
351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355 + C o n v e r t P a t h T o P o l y g o n %
359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 % ConvertPathToPolygon() converts a path to the more efficient sorted
364 % The format of the ConvertPathToPolygon method is:
366 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
367 % const PathInfo *path_info)
369 % A description of each parameter follows:
371 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
372 % rendering form of type PolygonInfo.
374 % o draw_info: Specifies a pointer to an DrawInfo structure.
376 % o path_info: Specifies a pointer to an PathInfo structure.
381 #if defined(__cplusplus) || defined(c_plusplus)
385 static int CompareEdges(const void *x,const void *y)
387 register const EdgeInfo
394 p=(const EdgeInfo *) x;
395 q=(const EdgeInfo *) y;
396 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
398 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
400 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
402 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
404 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
405 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
410 #if defined(__cplusplus) || defined(c_plusplus)
414 static void LogPolygonInfo(const PolygonInfo *polygon_info)
423 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
424 p=polygon_info->edges;
425 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
427 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
429 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
430 p->direction != MagickFalse ? "down" : "up");
431 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
432 p->ghostline != MagickFalse ? "transparent" : "opaque");
433 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
434 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
435 p->bounds.x2,p->bounds.y2);
436 for (j=0; j < (ssize_t) p->number_points; j++)
437 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
438 p->points[j].x,p->points[j].y);
441 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
444 static void ReversePoints(PointInfo *points,const size_t number_points)
452 for (i=0; i < (ssize_t) (number_points >> 1); i++)
455 points[i]=points[number_points-(i+1)];
456 points[number_points-(i+1)]=point;
460 static PolygonInfo *ConvertPathToPolygon(
461 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
490 Convert a path to the more efficient sorted rendering form.
492 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
493 if (polygon_info == (PolygonInfo *) NULL)
494 return((PolygonInfo *) NULL);
496 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
497 sizeof(*polygon_info->edges));
498 if (polygon_info->edges == (EdgeInfo *) NULL)
499 return((PolygonInfo *) NULL);
502 ghostline=MagickFalse;
505 points=(PointInfo *) NULL;
506 (void) ResetMagickMemory(&point,0,sizeof(point));
507 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
508 for (i=0; path_info[i].code != EndCode; i++)
510 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
511 (path_info[i].code == GhostlineCode))
516 if ((points != (PointInfo *) NULL) && (n >= 2))
518 if (edge == number_edges)
521 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
522 polygon_info->edges,(size_t) number_edges,
523 sizeof(*polygon_info->edges));
524 if (polygon_info->edges == (EdgeInfo *) NULL)
525 return((PolygonInfo *) NULL);
527 polygon_info->edges[edge].number_points=(size_t) n;
528 polygon_info->edges[edge].scanline=(-1.0);
529 polygon_info->edges[edge].highwater=0;
530 polygon_info->edges[edge].ghostline=ghostline;
531 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
533 ReversePoints(points,(size_t) n);
534 polygon_info->edges[edge].points=points;
535 polygon_info->edges[edge].bounds=bounds;
536 polygon_info->edges[edge].bounds.y1=points[0].y;
537 polygon_info->edges[edge].bounds.y2=points[n-1].y;
538 points=(PointInfo *) NULL;
539 ghostline=MagickFalse;
542 if (points == (PointInfo *) NULL)
545 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
547 if (points == (PointInfo *) NULL)
548 return((PolygonInfo *) NULL);
550 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
551 point=path_info[i].point;
562 next_direction=((path_info[i].point.y > point.y) ||
563 ((path_info[i].point.y == point.y) &&
564 (path_info[i].point.x > point.x))) ? 1 : -1;
565 if ((points != (PointInfo *) NULL) && (direction != 0) &&
566 (direction != next_direction))
572 if (edge == number_edges)
575 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
576 polygon_info->edges,(size_t) number_edges,
577 sizeof(*polygon_info->edges));
578 if (polygon_info->edges == (EdgeInfo *) NULL)
579 return((PolygonInfo *) NULL);
581 polygon_info->edges[edge].number_points=(size_t) n;
582 polygon_info->edges[edge].scanline=(-1.0);
583 polygon_info->edges[edge].highwater=0;
584 polygon_info->edges[edge].ghostline=ghostline;
585 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
587 ReversePoints(points,(size_t) n);
588 polygon_info->edges[edge].points=points;
589 polygon_info->edges[edge].bounds=bounds;
590 polygon_info->edges[edge].bounds.y1=points[0].y;
591 polygon_info->edges[edge].bounds.y2=points[n-1].y;
593 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
595 if (points == (PointInfo *) NULL)
596 return((PolygonInfo *) NULL);
598 ghostline=MagickFalse;
604 direction=next_direction;
605 if (points == (PointInfo *) NULL)
607 if (n == (ssize_t) number_points)
610 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
612 if (points == (PointInfo *) NULL)
613 return((PolygonInfo *) NULL);
615 point=path_info[i].point;
617 if (point.x < bounds.x1)
619 if (point.x > bounds.x2)
623 if (points != (PointInfo *) NULL)
626 points=(PointInfo *) RelinquishMagickMemory(points);
629 if (edge == number_edges)
632 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
633 polygon_info->edges,(size_t) number_edges,
634 sizeof(*polygon_info->edges));
635 if (polygon_info->edges == (EdgeInfo *) NULL)
636 return((PolygonInfo *) NULL);
638 polygon_info->edges[edge].number_points=(size_t) n;
639 polygon_info->edges[edge].scanline=(-1.0);
640 polygon_info->edges[edge].highwater=0;
641 polygon_info->edges[edge].ghostline=ghostline;
642 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
644 ReversePoints(points,(size_t) n);
645 polygon_info->edges[edge].points=points;
646 polygon_info->edges[edge].bounds=bounds;
647 polygon_info->edges[edge].bounds.y1=points[0].y;
648 polygon_info->edges[edge].bounds.y2=points[n-1].y;
649 ghostline=MagickFalse;
653 polygon_info->number_edges=edge;
654 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
655 sizeof(*polygon_info->edges),CompareEdges);
656 if (IsEventLogging() != MagickFalse)
657 LogPolygonInfo(polygon_info);
658 return(polygon_info);
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666 + C o n v e r t P r i m i t i v e T o P a t h %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
675 % The format of the ConvertPrimitiveToPath method is:
677 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
678 % const PrimitiveInfo *primitive_info)
680 % A description of each parameter follows:
682 % o Method ConvertPrimitiveToPath returns a vector path structure of type
685 % o draw_info: a structure of type DrawInfo.
687 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
692 static void LogPathInfo(const PathInfo *path_info)
694 register const PathInfo
697 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
698 for (p=path_info; p->code != EndCode; p++)
699 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
700 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
701 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
702 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
704 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
707 static PathInfo *ConvertPrimitiveToPath(
708 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
729 Converts a PrimitiveInfo structure into a vector path structure.
731 switch (primitive_info->primitive)
738 return((PathInfo *) NULL);
742 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
743 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
745 if (path_info == (PathInfo *) NULL)
746 return((PathInfo *) NULL);
754 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
757 if (coordinates <= 0)
759 coordinates=(ssize_t) primitive_info[i].coordinates;
760 p=primitive_info[i].point;
766 Eliminate duplicate points.
768 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
769 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
771 path_info[n].code=code;
772 path_info[n].point=primitive_info[i].point;
773 q=primitive_info[i].point;
778 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
779 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
782 Mark the p point as open if it does not match the q.
784 path_info[start].code=OpenCode;
785 path_info[n].code=GhostlineCode;
786 path_info[n].point=primitive_info[i].point;
788 path_info[n].code=LineToCode;
789 path_info[n].point=p;
792 path_info[n].code=EndCode;
793 path_info[n].point.x=0.0;
794 path_info[n].point.y=0.0;
795 if (IsEventLogging() != MagickFalse)
796 LogPathInfo(path_info);
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
805 % D e s t r o y D r a w I n f o %
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
814 % The format of the DestroyDrawInfo method is:
816 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
818 % A description of each parameter follows:
820 % o draw_info: the draw info.
823 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
825 if (draw_info->debug != MagickFalse)
826 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
827 assert(draw_info != (DrawInfo *) NULL);
828 assert(draw_info->signature == MagickCoreSignature);
829 if (draw_info->primitive != (char *) NULL)
830 draw_info->primitive=DestroyString(draw_info->primitive);
831 if (draw_info->text != (char *) NULL)
832 draw_info->text=DestroyString(draw_info->text);
833 if (draw_info->geometry != (char *) NULL)
834 draw_info->geometry=DestroyString(draw_info->geometry);
835 if (draw_info->fill_pattern != (Image *) NULL)
836 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
837 if (draw_info->stroke_pattern != (Image *) NULL)
838 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
839 if (draw_info->font != (char *) NULL)
840 draw_info->font=DestroyString(draw_info->font);
841 if (draw_info->font_features != (char *) NULL)
842 draw_info->font_features=DestroyString(draw_info->font_features);
843 if (draw_info->metrics != (char *) NULL)
844 draw_info->metrics=DestroyString(draw_info->metrics);
845 if (draw_info->family != (char *) NULL)
846 draw_info->family=DestroyString(draw_info->family);
847 if (draw_info->encoding != (char *) NULL)
848 draw_info->encoding=DestroyString(draw_info->encoding);
849 if (draw_info->density != (char *) NULL)
850 draw_info->density=DestroyString(draw_info->density);
851 if (draw_info->server_name != (char *) NULL)
852 draw_info->server_name=(char *)
853 RelinquishMagickMemory(draw_info->server_name);
854 if (draw_info->dash_pattern != (double *) NULL)
855 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
856 draw_info->dash_pattern);
857 if (draw_info->gradient.stops != (StopInfo *) NULL)
858 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
859 draw_info->gradient.stops);
860 if (draw_info->clip_mask != (char *) NULL)
861 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
862 draw_info->signature=(~MagickCoreSignature);
863 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
872 + D e s t r o y E d g e %
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % DestroyEdge() destroys the specified polygon edge.
880 % The format of the DestroyEdge method is:
882 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
884 % A description of each parameter follows:
886 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
888 % o edge: the polygon edge number to destroy.
891 static size_t DestroyEdge(PolygonInfo *polygon_info,
894 assert(edge < polygon_info->number_edges);
895 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
896 polygon_info->edges[edge].points);
897 polygon_info->number_edges--;
898 if (edge < polygon_info->number_edges)
899 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
900 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
901 return(polygon_info->number_edges);
905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 + D e s t r o y P o l y g o n I n f o %
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
917 % The format of the DestroyPolygonInfo method is:
919 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
921 % A description of each parameter follows:
923 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
926 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
931 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
932 polygon_info->edges[i].points=(PointInfo *)
933 RelinquishMagickMemory(polygon_info->edges[i].points);
934 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
935 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 % D r a w A f f i n e I m a g e %
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % DrawAffineImage() composites the source over the destination image as
950 % dictated by the affine transform.
952 % The format of the DrawAffineImage method is:
954 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
955 % const AffineMatrix *affine,ExceptionInfo *exception)
957 % A description of each parameter follows:
959 % o image: the image.
961 % o source: the source image.
963 % o affine: the affine transform.
965 % o exception: return any errors or warnings in this structure.
969 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
970 const double y,const SegmentInfo *edge)
983 Determine left and right edges.
985 inverse_edge.x1=edge->x1;
986 inverse_edge.y1=edge->y1;
987 inverse_edge.x2=edge->x2;
988 inverse_edge.y2=edge->y2;
989 z=affine->ry*y+affine->tx;
990 if (affine->sx >= MagickEpsilon)
992 intercept=(-z/affine->sx);
994 if (x > inverse_edge.x1)
996 intercept=(-z+(double) image->columns)/affine->sx;
998 if (x < inverse_edge.x2)
1002 if (affine->sx < -MagickEpsilon)
1004 intercept=(-z+(double) image->columns)/affine->sx;
1006 if (x > inverse_edge.x1)
1008 intercept=(-z/affine->sx);
1010 if (x < inverse_edge.x2)
1014 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1016 inverse_edge.x2=edge->x1;
1017 return(inverse_edge);
1020 Determine top and bottom edges.
1022 z=affine->sy*y+affine->ty;
1023 if (affine->rx >= MagickEpsilon)
1025 intercept=(-z/affine->rx);
1027 if (x > inverse_edge.x1)
1029 intercept=(-z+(double) image->rows)/affine->rx;
1031 if (x < inverse_edge.x2)
1035 if (affine->rx < -MagickEpsilon)
1037 intercept=(-z+(double) image->rows)/affine->rx;
1039 if (x > inverse_edge.x1)
1041 intercept=(-z/affine->rx);
1043 if (x < inverse_edge.x2)
1047 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1049 inverse_edge.x2=edge->x2;
1050 return(inverse_edge);
1052 return(inverse_edge);
1055 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1063 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1065 inverse_affine.sx=determinant*affine->sy;
1066 inverse_affine.rx=determinant*(-affine->rx);
1067 inverse_affine.ry=determinant*(-affine->ry);
1068 inverse_affine.sy=determinant*affine->sx;
1069 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1071 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1073 return(inverse_affine);
1076 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1077 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1109 Determine bounding box.
1111 assert(image != (Image *) NULL);
1112 assert(image->signature == MagickCoreSignature);
1113 if (image->debug != MagickFalse)
1114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1115 assert(source != (const Image *) NULL);
1116 assert(source->signature == MagickCoreSignature);
1117 assert(affine != (AffineMatrix *) NULL);
1120 extent[1].x=(double) source->columns-1.0;
1122 extent[2].x=(double) source->columns-1.0;
1123 extent[2].y=(double) source->rows-1.0;
1125 extent[3].y=(double) source->rows-1.0;
1126 for (i=0; i < 4; i++)
1132 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1133 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1137 for (i=1; i < 4; i++)
1139 if (min.x > extent[i].x)
1141 if (min.y > extent[i].y)
1143 if (max.x < extent[i].x)
1145 if (max.y < extent[i].y)
1149 Affine transform image.
1151 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1152 return(MagickFalse);
1154 edge.x1=MagickMax(min.x,0.0);
1155 edge.y1=MagickMax(min.y,0.0);
1156 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1157 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1158 inverse_affine=InverseAffineMatrix(affine);
1159 GetPixelInfo(image,&zero);
1160 start=(ssize_t) ceil(edge.y1-0.5);
1161 stop=(ssize_t) floor(edge.y2+0.5);
1162 source_view=AcquireVirtualCacheView(source,exception);
1163 image_view=AcquireAuthenticCacheView(image,exception);
1164 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1165 #pragma omp parallel for schedule(static,4) shared(status) \
1166 magick_threads(source,image,1,1)
1168 for (y=start; y <= stop; y++)
1189 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1190 if (inverse_edge.x2 < inverse_edge.x1)
1192 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1193 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1195 if (q == (Quantum *) NULL)
1200 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1202 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1204 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1206 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1207 point.x,point.y,&pixel,exception);
1208 GetPixelInfoPixel(image,q,&composite);
1209 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1211 SetPixelViaPixelInfo(image,&composite,q);
1213 q+=GetPixelChannels(image);
1215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1218 source_view=DestroyCacheView(source_view);
1219 image_view=DestroyCacheView(image_view);
1224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228 + D r a w B o u n d i n g R e c t a n g l e s %
1232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1234 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1235 % is only useful for developers debugging the rendering algorithm.
1237 % The format of the DrawBoundingRectangles method is:
1239 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1240 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1242 % A description of each parameter follows:
1244 % o image: the image.
1246 % o draw_info: the draw info.
1248 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1250 % o exception: return any errors or warnings in this structure.
1253 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1254 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1279 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1280 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1282 resolution.x=DefaultResolution;
1283 resolution.y=DefaultResolution;
1284 if (clone_info->density != (char *) NULL)
1292 flags=ParseGeometry(clone_info->density,&geometry_info);
1293 resolution.x=geometry_info.rho;
1294 resolution.y=geometry_info.sigma;
1295 if ((flags & SigmaValue) == MagickFalse)
1296 resolution.y=resolution.x;
1298 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1299 clone_info->stroke_width/2.0;
1304 if (polygon_info != (PolygonInfo *) NULL)
1306 bounds=polygon_info->edges[0].bounds;
1307 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1309 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1310 bounds.x1=polygon_info->edges[i].bounds.x1;
1311 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1312 bounds.y1=polygon_info->edges[i].bounds.y1;
1313 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1314 bounds.x2=polygon_info->edges[i].bounds.x2;
1315 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1316 bounds.y2=polygon_info->edges[i].bounds.y2;
1319 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1320 image->columns ? (double) image->columns-1 : bounds.x1;
1322 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1323 image->rows ? (double) image->rows-1 : bounds.y1;
1325 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1326 image->columns ? (double) image->columns-1 : bounds.x2;
1328 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1329 image->rows ? (double) image->rows-1 : bounds.y2;
1330 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1332 if (polygon_info->edges[i].direction != 0)
1333 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1336 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1338 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1339 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1340 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1341 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1342 primitive_info[0].primitive=RectanglePrimitive;
1343 TraceRectangle(primitive_info,start,end);
1344 primitive_info[0].method=ReplaceMethod;
1345 coordinates=(ssize_t) primitive_info[0].coordinates;
1346 primitive_info[coordinates].primitive=UndefinedPrimitive;
1347 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1350 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1352 start.x=(double) (bounds.x1-mid);
1353 start.y=(double) (bounds.y1-mid);
1354 end.x=(double) (bounds.x2+mid);
1355 end.y=(double) (bounds.y2+mid);
1356 primitive_info[0].primitive=RectanglePrimitive;
1357 TraceRectangle(primitive_info,start,end);
1358 primitive_info[0].method=ReplaceMethod;
1359 coordinates=(ssize_t) primitive_info[0].coordinates;
1360 primitive_info[coordinates].primitive=UndefinedPrimitive;
1361 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1362 clone_info=DestroyDrawInfo(clone_info);
1366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % D r a w C l i p P a t h %
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 % DrawClipPath() draws the clip path on the image mask.
1378 % The format of the DrawClipPath method is:
1380 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1381 % const char *name,ExceptionInfo *exception)
1383 % A description of each parameter follows:
1385 % o image: the image.
1387 % o draw_info: the draw info.
1389 % o name: the name of the clip path.
1391 % o exception: return any errors or warnings in this structure.
1394 MagickExport MagickBooleanType DrawClipPath(Image *image,
1395 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1398 filename[MagickPathExtent];
1412 assert(image != (Image *) NULL);
1413 assert(image->signature == MagickCoreSignature);
1414 if (image->debug != MagickFalse)
1415 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1416 assert(draw_info != (const DrawInfo *) NULL);
1417 (void) FormatLocaleString(filename,MagickPathExtent,"%s",name);
1418 value=GetImageArtifact(image,filename);
1419 if (value == (const char *) NULL)
1420 return(MagickFalse);
1421 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1422 if (clip_mask == (Image *) NULL)
1423 return(MagickFalse);
1424 (void) QueryColorCompliance("#0000",AllCompliance,
1425 &clip_mask->background_color,exception);
1426 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1427 (void) SetImageBackgroundColor(clip_mask,exception);
1428 if (image->debug != MagickFalse)
1429 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1430 draw_info->clip_mask);
1431 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1432 (void) CloneString(&clone_info->primitive,value);
1433 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1435 clone_info->clip_mask=(char *) NULL;
1436 status=NegateImage(clip_mask,MagickFalse,exception);
1437 (void) SetImageMask(image,ReadPixelMask,clip_mask,exception);
1438 clip_mask=DestroyImage(clip_mask);
1439 status&=DrawImage(image,clone_info,exception);
1440 clone_info=DestroyDrawInfo(clone_info);
1441 if (image->debug != MagickFalse)
1442 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1443 return(status != 0 ? MagickTrue : MagickFalse);
1447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451 + D r a w D a s h P o l y g o n %
1455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1458 % image while respecting the dash offset and dash pattern attributes.
1460 % The format of the DrawDashPolygon method is:
1462 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1463 % const PrimitiveInfo *primitive_info,Image *image,
1464 % ExceptionInfo *exception)
1466 % A description of each parameter follows:
1468 % o draw_info: the draw info.
1470 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1472 % o image: the image.
1474 % o exception: return any errors or warnings in this structure.
1477 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1478 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1510 assert(draw_info != (const DrawInfo *) NULL);
1511 if (image->debug != MagickFalse)
1512 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1513 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1514 clone_info->miterlimit=0;
1515 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1516 number_vertices=(size_t) i;
1517 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1518 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1519 if (dash_polygon == (PrimitiveInfo *) NULL)
1520 return(MagickFalse);
1521 dash_polygon[0]=primitive_info[0];
1522 scale=ExpandAffine(&draw_info->affine);
1523 length=scale*(draw_info->dash_pattern[0]-0.5);
1524 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1526 for (n=0; offset > 0.0; j=0)
1528 if (draw_info->dash_pattern[n] <= 0.0)
1530 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1531 if (offset > length)
1535 length=scale*(draw_info->dash_pattern[n]+0.5);
1538 if (offset < length)
1550 for (i=1; i < (ssize_t) number_vertices; i++)
1552 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1553 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1554 maximum_length=hypot((double) dx,dy);
1558 if (draw_info->dash_pattern[n] == 0.0)
1560 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1562 for (total_length=0.0; (total_length+length) <= maximum_length; )
1564 total_length+=length;
1565 if ((n & 0x01) != 0)
1567 dash_polygon[0]=primitive_info[0];
1568 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1569 total_length/maximum_length);
1570 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1571 total_length/maximum_length);
1576 if ((j+1) > (ssize_t) (2*number_vertices))
1578 dash_polygon[j]=primitive_info[i-1];
1579 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1580 total_length/maximum_length);
1581 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1582 total_length/maximum_length);
1583 dash_polygon[j].coordinates=1;
1585 dash_polygon[0].coordinates=(size_t) j;
1586 dash_polygon[j].primitive=UndefinedPrimitive;
1587 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1590 if (draw_info->dash_pattern[n] == 0.0)
1592 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1594 length-=(maximum_length-total_length);
1595 if ((n & 0x01) != 0)
1597 dash_polygon[j]=primitive_info[i];
1598 dash_polygon[j].coordinates=1;
1601 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1603 dash_polygon[j]=primitive_info[i-1];
1604 dash_polygon[j].point.x+=MagickEpsilon;
1605 dash_polygon[j].point.y+=MagickEpsilon;
1606 dash_polygon[j].coordinates=1;
1608 dash_polygon[0].coordinates=(size_t) j;
1609 dash_polygon[j].primitive=UndefinedPrimitive;
1610 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1612 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1613 clone_info=DestroyDrawInfo(clone_info);
1614 if (image->debug != MagickFalse)
1615 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1616 return(status != 0 ? MagickTrue : MagickFalse);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % D r a w I m a g e %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % DrawImage() draws a graphic primitive on your image. The primitive
1631 % may be represented as a string or filename. Precede the filename with an
1632 % "at" sign (@) and the contents of the file are drawn on the image. You
1633 % can affect how text is drawn by setting one or more members of the draw
1636 % The format of the DrawImage method is:
1638 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1639 % ExceptionInfo *exception)
1641 % A description of each parameter follows:
1643 % o image: the image.
1645 % o draw_info: the draw info.
1647 % o exception: return any errors or warnings in this structure.
1651 static inline MagickBooleanType IsPoint(const char *point)
1659 value=StringToDouble(point,&p);
1660 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1663 static inline void TracePoint(PrimitiveInfo *primitive_info,
1664 const PointInfo point)
1666 primitive_info->coordinates=1;
1667 primitive_info->point=point;
1670 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1671 ExceptionInfo *exception)
1673 #define RenderImageTag "Render/Image"
1680 keyword[MagickPathExtent],
1681 geometry[MagickPathExtent],
1682 pattern[MagickPathExtent],
1736 Ensure the annotation info is valid.
1738 assert(image != (Image *) NULL);
1739 assert(image->signature == MagickCoreSignature);
1740 if (image->debug != MagickFalse)
1741 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1742 assert(draw_info != (DrawInfo *) NULL);
1743 assert(draw_info->signature == MagickCoreSignature);
1744 if (image->debug != MagickFalse)
1745 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1746 if ((draw_info->primitive == (char *) NULL) ||
1747 (*draw_info->primitive == '\0'))
1748 return(MagickFalse);
1749 if (image->debug != MagickFalse)
1750 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1751 if (*draw_info->primitive != '@')
1752 primitive=AcquireString(draw_info->primitive);
1754 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1755 if (primitive == (char *) NULL)
1756 return(MagickFalse);
1757 primitive_extent=(double) strlen(primitive);
1758 (void) SetImageArtifact(image,"MVG",primitive);
1761 stops=(StopInfo *) NULL;
1763 Allocate primitive info memory.
1765 graphic_context=(DrawInfo **) AcquireMagickMemory(
1766 sizeof(*graphic_context));
1767 if (graphic_context == (DrawInfo **) NULL)
1769 primitive=DestroyString(primitive);
1770 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1774 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1775 sizeof(*primitive_info));
1776 if (primitive_info == (PrimitiveInfo *) NULL)
1778 primitive=DestroyString(primitive);
1779 for ( ; n >= 0; n--)
1780 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1781 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1782 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1785 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1786 graphic_context[n]->viewbox=image->page;
1787 if ((image->page.width == 0) || (image->page.height == 0))
1789 graphic_context[n]->viewbox.width=image->columns;
1790 graphic_context[n]->viewbox.height=image->rows;
1792 token=AcquireString(primitive);
1793 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1794 return(MagickFalse);
1796 for (q=primitive; *q != '\0'; )
1799 Interpret graphic primitive.
1801 GetMagickToken(q,&q,keyword);
1802 if (*keyword == '\0')
1804 if (*keyword == '#')
1809 while ((*q != '\n') && (*q != '\0'))
1813 p=q-strlen(keyword)-1;
1814 primitive_type=UndefinedPrimitive;
1815 current=graphic_context[n]->affine;
1816 GetAffineMatrix(&affine);
1824 if (LocaleCompare("affine",keyword) == 0)
1826 GetMagickToken(q,&q,token);
1827 affine.sx=StringToDouble(token,(char **) NULL);
1828 GetMagickToken(q,&q,token);
1830 GetMagickToken(q,&q,token);
1831 affine.rx=StringToDouble(token,(char **) NULL);
1832 GetMagickToken(q,&q,token);
1834 GetMagickToken(q,&q,token);
1835 affine.ry=StringToDouble(token,(char **) NULL);
1836 GetMagickToken(q,&q,token);
1838 GetMagickToken(q,&q,token);
1839 affine.sy=StringToDouble(token,(char **) NULL);
1840 GetMagickToken(q,&q,token);
1842 GetMagickToken(q,&q,token);
1843 affine.tx=StringToDouble(token,(char **) NULL);
1844 GetMagickToken(q,&q,token);
1846 GetMagickToken(q,&q,token);
1847 affine.ty=StringToDouble(token,(char **) NULL);
1850 if (LocaleCompare("alpha",keyword) == 0)
1852 primitive_type=AlphaPrimitive;
1855 if (LocaleCompare("arc",keyword) == 0)
1857 primitive_type=ArcPrimitive;
1866 if (LocaleCompare("bezier",keyword) == 0)
1868 primitive_type=BezierPrimitive;
1871 if (LocaleCompare("border-color",keyword) == 0)
1873 GetMagickToken(q,&q,token);
1874 (void) QueryColorCompliance(token,AllCompliance,
1875 &graphic_context[n]->border_color,exception);
1884 if (LocaleCompare("clip-path",keyword) == 0)
1889 GetMagickToken(q,&q,token);
1890 (void) CloneString(&graphic_context[n]->clip_mask,token);
1891 (void) DrawClipPath(image,graphic_context[n],
1892 graphic_context[n]->clip_mask,exception);
1895 if (LocaleCompare("clip-rule",keyword) == 0)
1900 GetMagickToken(q,&q,token);
1901 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1903 if (fill_rule == -1)
1906 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1909 if (LocaleCompare("clip-units",keyword) == 0)
1914 GetMagickToken(q,&q,token);
1915 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1917 if (clip_units == -1)
1922 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1923 if (clip_units == ObjectBoundingBox)
1925 GetAffineMatrix(¤t);
1926 affine.sx=draw_info->bounds.x2;
1927 affine.sy=draw_info->bounds.y2;
1928 affine.tx=draw_info->bounds.x1;
1929 affine.ty=draw_info->bounds.y1;
1934 if (LocaleCompare("circle",keyword) == 0)
1936 primitive_type=CirclePrimitive;
1939 if (LocaleCompare("color",keyword) == 0)
1941 primitive_type=ColorPrimitive;
1950 if (LocaleCompare("decorate",keyword) == 0)
1955 GetMagickToken(q,&q,token);
1956 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1961 graphic_context[n]->decorate=(DecorationType) decorate;
1964 if (LocaleCompare("density",keyword) == 0)
1966 GetMagickToken(q,&q,token);
1967 (void) CloneString(&graphic_context[n]->density,token);
1970 if (LocaleCompare("direction",keyword) == 0)
1975 GetMagickToken(q,&q,token);
1976 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1978 if (direction == -1)
1981 graphic_context[n]->direction=(DirectionType) direction;
1990 if (LocaleCompare("ellipse",keyword) == 0)
1992 primitive_type=EllipsePrimitive;
1995 if (LocaleCompare("encoding",keyword) == 0)
1997 GetMagickToken(q,&q,token);
1998 (void) CloneString(&graphic_context[n]->encoding,token);
2007 if (LocaleCompare("fill",keyword) == 0)
2009 GetMagickToken(q,&q,token);
2010 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2011 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2012 (void) DrawPatternPath(image,draw_info,token,
2013 &graphic_context[n]->fill_pattern,exception);
2016 status&=QueryColorCompliance(token,AllCompliance,
2017 &graphic_context[n]->fill,exception);
2018 if (status == MagickFalse)
2023 pattern_info=AcquireImageInfo();
2024 (void) CopyMagickString(pattern_info->filename,token,
2026 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2028 CatchException(exception);
2029 pattern_info=DestroyImageInfo(pattern_info);
2034 if (LocaleCompare("fill-opacity",keyword) == 0)
2036 GetMagickToken(q,&q,token);
2037 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2038 graphic_context[n]->fill.alpha=(double) QuantumRange*
2039 factor*StringToDouble(token,(char **) NULL);
2042 if (LocaleCompare("fill-rule",keyword) == 0)
2047 GetMagickToken(q,&q,token);
2048 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2050 if (fill_rule == -1)
2053 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2056 if (LocaleCompare("font",keyword) == 0)
2058 GetMagickToken(q,&q,token);
2059 (void) CloneString(&graphic_context[n]->font,token);
2060 if (LocaleCompare("none",token) == 0)
2061 graphic_context[n]->font=(char *)
2062 RelinquishMagickMemory(graphic_context[n]->font);
2065 if (LocaleCompare("font-family",keyword) == 0)
2067 GetMagickToken(q,&q,token);
2068 (void) CloneString(&graphic_context[n]->family,token);
2071 if (LocaleCompare("font-features",keyword) == 0)
2073 GetMagickToken(q,&q,token);
2074 (void) CloneString(&graphic_context[n]->font_features,token);
2075 if (LocaleCompare("none",token) == 0)
2076 graphic_context[n]->font_features=(char *)
2077 RelinquishMagickMemory(graphic_context[n]->font_features);
2080 if (LocaleCompare("font-size",keyword) == 0)
2082 GetMagickToken(q,&q,token);
2083 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2086 if (LocaleCompare("font-stretch",keyword) == 0)
2091 GetMagickToken(q,&q,token);
2092 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2096 graphic_context[n]->stretch=(StretchType) stretch;
2099 if (LocaleCompare("font-style",keyword) == 0)
2104 GetMagickToken(q,&q,token);
2105 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2109 graphic_context[n]->style=(StyleType) style;
2112 if (LocaleCompare("font-weight",keyword) == 0)
2117 GetMagickToken(q,&q,token);
2118 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,
2121 weight=StringToUnsignedLong(token);
2122 graphic_context[n]->weight=(size_t) weight;
2131 if (LocaleCompare("gradient-units",keyword) == 0)
2133 GetMagickToken(q,&q,token);
2136 if (LocaleCompare("gravity",keyword) == 0)
2141 GetMagickToken(q,&q,token);
2142 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2146 graphic_context[n]->gravity=(GravityType) gravity;
2155 if (LocaleCompare("image",keyword) == 0)
2160 primitive_type=ImagePrimitive;
2161 GetMagickToken(q,&q,token);
2162 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2166 graphic_context[n]->compose=(CompositeOperator) compose;
2169 if (LocaleCompare("interline-spacing",keyword) == 0)
2171 GetMagickToken(q,&q,token);
2172 graphic_context[n]->interline_spacing=StringToDouble(token,
2176 if (LocaleCompare("interword-spacing",keyword) == 0)
2178 GetMagickToken(q,&q,token);
2179 graphic_context[n]->interword_spacing=StringToDouble(token,
2189 if (LocaleCompare("kerning",keyword) == 0)
2191 GetMagickToken(q,&q,token);
2192 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2201 if (LocaleCompare("line",keyword) == 0)
2202 primitive_type=LinePrimitive;
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,ReadPixelMask,(Image *) NULL,
2275 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2279 if (LocaleCompare("pattern",token) == 0)
2284 if (LocaleCompare("push",keyword) == 0)
2286 GetMagickToken(q,&q,token);
2287 if (LocaleCompare("clip-path",token) == 0)
2290 name[MagickPathExtent];
2292 GetMagickToken(q,&q,token);
2293 (void) FormatLocaleString(name,MagickPathExtent,"%s",token);
2294 for (p=q; *q != '\0'; )
2296 GetMagickToken(q,&q,token);
2297 if (LocaleCompare(token,"pop") != 0)
2299 GetMagickToken(q,(const char **) NULL,token);
2300 if (LocaleCompare(token,"clip-path") != 0)
2304 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2305 (void) SetImageArtifact(image,name,token);
2306 GetMagickToken(q,&q,token);
2309 if (LocaleCompare("gradient",token) == 0)
2312 key[2*MagickPathExtent],
2313 name[MagickPathExtent],
2314 type[MagickPathExtent];
2319 GetMagickToken(q,&q,token);
2320 (void) CopyMagickString(name,token,MagickPathExtent);
2321 GetMagickToken(q,&q,token);
2322 (void) CopyMagickString(type,token,MagickPathExtent);
2323 GetMagickToken(q,&q,token);
2324 segment.x1=StringToDouble(token,(char **) NULL);
2325 GetMagickToken(q,&q,token);
2327 GetMagickToken(q,&q,token);
2328 segment.y1=StringToDouble(token,(char **) NULL);
2329 GetMagickToken(q,&q,token);
2331 GetMagickToken(q,&q,token);
2332 segment.x2=StringToDouble(token,(char **) NULL);
2333 GetMagickToken(q,&q,token);
2335 GetMagickToken(q,&q,token);
2336 segment.y2=StringToDouble(token,(char **) NULL);
2337 if (LocaleCompare(type,"radial") == 0)
2339 GetMagickToken(q,&q,token);
2341 GetMagickToken(q,&q,token);
2343 for (p=q; *q != '\0'; )
2345 GetMagickToken(q,&q,token);
2346 if (LocaleCompare(token,"pop") != 0)
2348 GetMagickToken(q,(const char **) NULL,token);
2349 if (LocaleCompare(token,"gradient") != 0)
2353 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2354 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2355 graphic_context[n]->affine.ry*segment.y1+
2356 graphic_context[n]->affine.tx;
2357 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2358 graphic_context[n]->affine.sy*segment.y1+
2359 graphic_context[n]->affine.ty;
2360 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2361 graphic_context[n]->affine.ry*segment.y2+
2362 graphic_context[n]->affine.tx;
2363 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2364 graphic_context[n]->affine.sy*segment.y2+
2365 graphic_context[n]->affine.ty;
2366 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2367 (void) SetImageArtifact(image,key,token);
2368 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
2369 (void) SetImageArtifact(image,key,type);
2370 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",name);
2371 (void) FormatLocaleString(geometry,MagickPathExtent,
2372 "%gx%g%+.15g%+.15g",
2373 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2374 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2375 bounds.x1,bounds.y1);
2376 (void) SetImageArtifact(image,key,geometry);
2377 GetMagickToken(q,&q,token);
2380 if (LocaleCompare("pattern",token) == 0)
2383 key[2*MagickPathExtent],
2384 name[MagickPathExtent];
2389 GetMagickToken(q,&q,token);
2390 (void) CopyMagickString(name,token,MagickPathExtent);
2391 GetMagickToken(q,&q,token);
2392 pattern_bounds.x=(ssize_t) ceil(StringToDouble(token,
2393 (char **) NULL)-0.5);
2394 GetMagickToken(q,&q,token);
2396 GetMagickToken(q,&q,token);
2397 pattern_bounds.y=(ssize_t) ceil(StringToDouble(token,
2398 (char **) NULL)-0.5);
2399 GetMagickToken(q,&q,token);
2401 GetMagickToken(q,&q,token);
2402 pattern_bounds.width=(size_t) floor(StringToDouble(token,
2403 (char **) NULL)+0.5);
2404 GetMagickToken(q,&q,token);
2406 GetMagickToken(q,&q,token);
2407 pattern_bounds.height=(size_t) floor(StringToDouble(token,
2408 (char **) NULL)+0.5);
2409 for (p=q; *q != '\0'; )
2411 GetMagickToken(q,&q,token);
2412 if (LocaleCompare(token,"pop") != 0)
2414 GetMagickToken(q,(const char **) NULL,token);
2415 if (LocaleCompare(token,"pattern") != 0)
2419 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2420 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2421 (void) SetImageArtifact(image,key,token);
2422 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2424 (void) FormatLocaleString(geometry,MagickPathExtent,
2425 "%.20gx%.20g%+.20g%+.20g",(double)pattern_bounds.width,
2426 (double)pattern_bounds.height,(double)pattern_bounds.x,
2427 (double)pattern_bounds.y);
2428 (void) SetImageArtifact(image,key,geometry);
2429 GetMagickToken(q,&q,token);
2432 if (LocaleCompare("graphic-context",token) == 0)
2435 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2436 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2437 if (graphic_context == (DrawInfo **) NULL)
2439 (void) ThrowMagickException(exception,GetMagickModule(),
2440 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2444 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2445 graphic_context[n-1]);
2448 if (LocaleCompare("defs",token) == 0)
2459 if (LocaleCompare("rectangle",keyword) == 0)
2461 primitive_type=RectanglePrimitive;
2464 if (LocaleCompare("rotate",keyword) == 0)
2466 GetMagickToken(q,&q,token);
2467 angle=StringToDouble(token,(char **) NULL);
2468 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2469 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2470 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2471 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2474 if (LocaleCompare("roundRectangle",keyword) == 0)
2476 primitive_type=RoundRectanglePrimitive;
2485 if (LocaleCompare("scale",keyword) == 0)
2487 GetMagickToken(q,&q,token);
2488 affine.sx=StringToDouble(token,(char **) NULL);
2489 GetMagickToken(q,&q,token);
2491 GetMagickToken(q,&q,token);
2492 affine.sy=StringToDouble(token,(char **) NULL);
2495 if (LocaleCompare("skewX",keyword) == 0)
2497 GetMagickToken(q,&q,token);
2498 angle=StringToDouble(token,(char **) NULL);
2499 affine.ry=sin(DegreesToRadians(angle));
2502 if (LocaleCompare("skewY",keyword) == 0)
2504 GetMagickToken(q,&q,token);
2505 angle=StringToDouble(token,(char **) NULL);
2506 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2509 if (LocaleCompare("stop-color",keyword) == 0)
2515 if (number_stops == 1)
2516 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
2517 else if (number_stops > 2)
2518 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
2520 if (stops == (StopInfo *) NULL)
2522 (void) ThrowMagickException(exception,GetMagickModule(),
2523 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2527 GetMagickToken(q,&q,token);
2528 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2530 stops[number_stops-1].color=stop_color;
2531 GetMagickToken(q,&q,token);
2532 stops[number_stops-1].offset=StringToDouble(token,(char **) NULL);
2535 if (LocaleCompare("stroke",keyword) == 0)
2537 GetMagickToken(q,&q,token);
2538 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2539 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2540 (void) DrawPatternPath(image,draw_info,token,
2541 &graphic_context[n]->stroke_pattern,exception);
2544 status&=QueryColorCompliance(token,AllCompliance,
2545 &graphic_context[n]->stroke,exception);
2546 if (status == MagickFalse)
2551 pattern_info=AcquireImageInfo();
2552 (void) CopyMagickString(pattern_info->filename,token,
2554 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2556 CatchException(exception);
2557 pattern_info=DestroyImageInfo(pattern_info);
2562 if (LocaleCompare("stroke-antialias",keyword) == 0)
2564 GetMagickToken(q,&q,token);
2565 graphic_context[n]->stroke_antialias=
2566 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2569 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2571 if (graphic_context[n]->dash_pattern != (double *) NULL)
2572 graphic_context[n]->dash_pattern=(double *)
2573 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2574 if (IsPoint(q) != MagickFalse)
2580 GetMagickToken(r,&r,token);
2582 GetMagickToken(r,&r,token);
2583 for (x=0; IsPoint(token) != MagickFalse; x++)
2585 GetMagickToken(r,&r,token);
2587 GetMagickToken(r,&r,token);
2589 graphic_context[n]->dash_pattern=(double *)
2590 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2591 sizeof(*graphic_context[n]->dash_pattern));
2592 if (graphic_context[n]->dash_pattern == (double *) NULL)
2594 (void) ThrowMagickException(exception,GetMagickModule(),
2595 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2599 for (j=0; j < x; j++)
2601 GetMagickToken(q,&q,token);
2603 GetMagickToken(q,&q,token);
2604 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2607 if ((x & 0x01) != 0)
2608 for ( ; j < (2*x); j++)
2609 graphic_context[n]->dash_pattern[j]=
2610 graphic_context[n]->dash_pattern[j-x];
2611 graphic_context[n]->dash_pattern[j]=0.0;
2614 GetMagickToken(q,&q,token);
2617 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2619 GetMagickToken(q,&q,token);
2620 graphic_context[n]->dash_offset=StringToDouble(token,
2624 if (LocaleCompare("stroke-linecap",keyword) == 0)
2629 GetMagickToken(q,&q,token);
2630 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2634 graphic_context[n]->linecap=(LineCap) linecap;
2637 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2642 GetMagickToken(q,&q,token);
2643 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2648 graphic_context[n]->linejoin=(LineJoin) linejoin;
2651 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2653 GetMagickToken(q,&q,token);
2654 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2657 if (LocaleCompare("stroke-opacity",keyword) == 0)
2659 GetMagickToken(q,&q,token);
2660 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2661 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2662 factor*StringToDouble(token,(char **) NULL);
2665 if (LocaleCompare("stroke-width",keyword) == 0)
2667 GetMagickToken(q,&q,token);
2668 graphic_context[n]->stroke_width=StringToDouble(token,
2678 if (LocaleCompare("text",keyword) == 0)
2680 primitive_type=TextPrimitive;
2683 if (LocaleCompare("text-align",keyword) == 0)
2688 GetMagickToken(q,&q,token);
2689 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2693 graphic_context[n]->align=(AlignType) align;
2696 if (LocaleCompare("text-anchor",keyword) == 0)
2701 GetMagickToken(q,&q,token);
2702 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2706 graphic_context[n]->align=(AlignType) align;
2709 if (LocaleCompare("text-antialias",keyword) == 0)
2711 GetMagickToken(q,&q,token);
2712 graphic_context[n]->text_antialias=
2713 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2716 if (LocaleCompare("text-undercolor",keyword) == 0)
2718 GetMagickToken(q,&q,token);
2719 (void) QueryColorCompliance(token,AllCompliance,
2720 &graphic_context[n]->undercolor,exception);
2723 if (LocaleCompare("translate",keyword) == 0)
2725 GetMagickToken(q,&q,token);
2726 affine.tx=StringToDouble(token,(char **) NULL);
2727 GetMagickToken(q,&q,token);
2729 GetMagickToken(q,&q,token);
2730 affine.ty=StringToDouble(token,(char **) NULL);
2739 if (LocaleCompare("viewbox",keyword) == 0)
2741 GetMagickToken(q,&q,token);
2742 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2743 (char **) NULL)-0.5);
2744 GetMagickToken(q,&q,token);
2746 GetMagickToken(q,&q,token);
2747 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2748 (char **) NULL)-0.5);
2749 GetMagickToken(q,&q,token);
2751 GetMagickToken(q,&q,token);
2752 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2753 token,(char **) NULL)+0.5);
2754 GetMagickToken(q,&q,token);
2756 GetMagickToken(q,&q,token);
2757 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2758 token,(char **) NULL)+0.5);
2770 if (status == MagickFalse)
2772 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2773 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2775 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2776 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2777 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2778 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2779 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2781 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2784 if (primitive_type == UndefinedPrimitive)
2788 if (number_stops > 1)
2793 type=LinearGradient;
2794 if (draw_info->gradient.type == RadialGradient)
2795 type=RadialGradient;
2796 (void) GradientImage(image,type,PadSpread,stops,number_stops,
2799 if (number_stops > 0)
2800 stops=(StopInfo *) RelinquishMagickMemory(stops);
2802 if (image->debug != MagickFalse)
2803 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2808 Parse the primitive attributes.
2812 primitive_info[0].point.x=0.0;
2813 primitive_info[0].point.y=0.0;
2814 for (x=0; *q != '\0'; x++)
2819 if (IsPoint(q) == MagickFalse)
2821 GetMagickToken(q,&q,token);
2822 point.x=StringToDouble(token,(char **) NULL);
2823 GetMagickToken(q,&q,token);
2825 GetMagickToken(q,&q,token);
2826 point.y=StringToDouble(token,(char **) NULL);
2827 GetMagickToken(q,(const char **) NULL,token);
2829 GetMagickToken(q,&q,token);
2830 primitive_info[i].primitive=primitive_type;
2831 primitive_info[i].point=point;
2832 primitive_info[i].coordinates=0;
2833 primitive_info[i].method=FloodfillMethod;
2835 if (i < (ssize_t) number_points)
2838 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2839 (size_t) number_points,sizeof(*primitive_info));
2840 if (primitive_info == (PrimitiveInfo *) NULL)
2842 (void) ThrowMagickException(exception,GetMagickModule(),
2843 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2847 primitive_info[j].primitive=primitive_type;
2848 primitive_info[j].coordinates=(size_t) x;
2849 primitive_info[j].method=FloodfillMethod;
2850 primitive_info[j].text=(char *) NULL;
2852 Circumscribe primitive within a circle.
2854 bounds.x1=primitive_info[j].point.x;
2855 bounds.y1=primitive_info[j].point.y;
2856 bounds.x2=primitive_info[j].point.x;
2857 bounds.y2=primitive_info[j].point.y;
2858 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2860 point=primitive_info[j+k].point;
2861 if (point.x < bounds.x1)
2863 if (point.y < bounds.y1)
2865 if (point.x > bounds.x2)
2867 if (point.y > bounds.y2)
2871 Speculate how many points our primitive might consume.
2873 length=primitive_info[j].coordinates;
2874 switch (primitive_type)
2876 case RectanglePrimitive:
2881 case RoundRectanglePrimitive:
2888 alpha=bounds.x2-bounds.x1;
2889 beta=bounds.y2-bounds.y1;
2890 radius=hypot((double) alpha,(double) beta);
2892 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2895 case BezierPrimitive:
2897 if (primitive_info[j].coordinates > 107)
2898 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2899 "TooManyBezierCoordinates","`%s'",token);
2900 length=BezierQuantum*primitive_info[j].coordinates;
2909 GetMagickToken(q,&q,token);
2912 for (s=token; *s != '\0'; s=t)
2917 value=StringToDouble(s,&t);
2926 length=length*BezierQuantum/2;
2929 case CirclePrimitive:
2931 case EllipsePrimitive:
2938 alpha=bounds.x2-bounds.x1;
2939 beta=bounds.y2-bounds.y1;
2940 radius=hypot((double) alpha,(double) beta);
2941 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2947 if ((size_t) (i+length) >= number_points)
2950 Resize based on speculative points required by primitive.
2952 number_points+=length+1;
2953 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2954 (size_t) number_points,sizeof(*primitive_info));
2955 if (primitive_info == (PrimitiveInfo *) NULL)
2957 (void) ThrowMagickException(exception,GetMagickModule(),
2958 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2963 switch (primitive_type)
2965 case PointPrimitive:
2968 if (primitive_info[j].coordinates != 1)
2973 TracePoint(primitive_info+j,primitive_info[j].point);
2974 i=(ssize_t) (j+primitive_info[j].coordinates);
2979 if (primitive_info[j].coordinates != 2)
2984 TraceLine(primitive_info+j,primitive_info[j].point,
2985 primitive_info[j+1].point);
2986 i=(ssize_t) (j+primitive_info[j].coordinates);
2989 case RectanglePrimitive:
2991 if (primitive_info[j].coordinates != 2)
2996 TraceRectangle(primitive_info+j,primitive_info[j].point,
2997 primitive_info[j+1].point);
2998 i=(ssize_t) (j+primitive_info[j].coordinates);
3001 case RoundRectanglePrimitive:
3003 if (primitive_info[j].coordinates != 3)
3008 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
3009 primitive_info[j+1].point,primitive_info[j+2].point);
3010 i=(ssize_t) (j+primitive_info[j].coordinates);
3015 if (primitive_info[j].coordinates != 3)
3017 primitive_type=UndefinedPrimitive;
3020 TraceArc(primitive_info+j,primitive_info[j].point,
3021 primitive_info[j+1].point,primitive_info[j+2].point);
3022 i=(ssize_t) (j+primitive_info[j].coordinates);
3025 case EllipsePrimitive:
3027 if (primitive_info[j].coordinates != 3)
3032 TraceEllipse(primitive_info+j,primitive_info[j].point,
3033 primitive_info[j+1].point,primitive_info[j+2].point);
3034 i=(ssize_t) (j+primitive_info[j].coordinates);
3037 case CirclePrimitive:
3039 if (primitive_info[j].coordinates != 2)
3044 TraceCircle(primitive_info+j,primitive_info[j].point,
3045 primitive_info[j+1].point);
3046 i=(ssize_t) (j+primitive_info[j].coordinates);
3049 case PolylinePrimitive:
3051 case PolygonPrimitive:
3053 primitive_info[i]=primitive_info[j];
3054 primitive_info[i].coordinates=0;
3055 primitive_info[j].coordinates++;
3059 case BezierPrimitive:
3061 if (primitive_info[j].coordinates < 3)
3066 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3067 i=(ssize_t) (j+primitive_info[j].coordinates);
3072 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3075 case AlphaPrimitive:
3076 case ColorPrimitive:
3081 if (primitive_info[j].coordinates != 1)
3086 GetMagickToken(q,&q,token);
3087 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3091 primitive_info[j].method=(PaintMethod) method;
3096 if (primitive_info[j].coordinates != 1)
3102 GetMagickToken(q,&q,token);
3103 primitive_info[j].text=AcquireString(token);
3106 case ImagePrimitive:
3108 if (primitive_info[j].coordinates != 2)
3113 GetMagickToken(q,&q,token);
3114 primitive_info[j].text=AcquireString(token);
3118 if (primitive_info == (PrimitiveInfo *) NULL)
3120 if (image->debug != MagickFalse)
3121 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3122 if (status == MagickFalse)
3124 primitive_info[i].primitive=UndefinedPrimitive;
3130 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3132 point=primitive_info[i].point;
3133 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3134 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3135 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3136 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3137 point=primitive_info[i].point;
3138 if (point.x < graphic_context[n]->bounds.x1)
3139 graphic_context[n]->bounds.x1=point.x;
3140 if (point.y < graphic_context[n]->bounds.y1)
3141 graphic_context[n]->bounds.y1=point.y;
3142 if (point.x > graphic_context[n]->bounds.x2)
3143 graphic_context[n]->bounds.x2=point.x;
3144 if (point.y > graphic_context[n]->bounds.y2)
3145 graphic_context[n]->bounds.y2=point.y;
3146 if (primitive_info[i].primitive == ImagePrimitive)
3148 if (i >= (ssize_t) number_points)
3149 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3151 if (graphic_context[n]->render != MagickFalse)
3153 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3154 (LocaleCompare(graphic_context[n]->clip_mask,
3155 graphic_context[n-1]->clip_mask) != 0))
3156 status&=DrawClipPath(image,graphic_context[n],
3157 graphic_context[n]->clip_mask,exception);
3158 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3161 if (primitive_info->text != (char *) NULL)
3162 primitive_info->text=(char *) RelinquishMagickMemory(
3163 primitive_info->text);
3164 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3166 if (proceed == MagickFalse)
3171 if (image->debug != MagickFalse)
3172 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3174 Relinquish resources.
3176 token=DestroyString(token);
3177 if (primitive_info != (PrimitiveInfo *) NULL)
3178 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3179 primitive=DestroyString(primitive);
3180 for ( ; n >= 0; n--)
3181 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3182 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3183 if (status == MagickFalse)
3184 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3186 return(status != 0 ? MagickTrue : MagickFalse);
3190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3194 % D r a w G r a d i e n t I m a g e %
3198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3200 % DrawGradientImage() draws a linear gradient on the image.
3202 % The format of the DrawGradientImage method is:
3204 % MagickBooleanType DrawGradientImage(Image *image,
3205 % const DrawInfo *draw_info,ExceptionInfo *exception)
3207 % A description of each parameter follows:
3209 % o image: the image.
3211 % o draw_info: the draw info.
3213 % o exception: return any errors or warnings in this structure.
3217 static inline double GetStopColorOffset(const GradientInfo *gradient,
3218 const ssize_t x,const ssize_t y)
3220 switch (gradient->type)
3222 case UndefinedGradient:
3223 case LinearGradient:
3238 gradient_vector=(&gradient->gradient_vector);
3239 p.x=gradient_vector->x2-gradient_vector->x1;
3240 p.y=gradient_vector->y2-gradient_vector->y1;
3241 q.x=(double) x-gradient_vector->x1;
3242 q.y=(double) y-gradient_vector->y1;
3243 length=sqrt(q.x*q.x+q.y*q.y);
3244 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3245 gamma=PerceptibleReciprocal(gamma);
3246 scale=p.x*q.x+p.y*q.y;
3247 offset=gamma*scale*length;
3250 case RadialGradient:
3255 if (gradient->spread == RepeatSpread)
3257 v.x=(double) x-gradient->center.x;
3258 v.y=(double) y-gradient->center.y;
3259 return(sqrt(v.x*v.x+v.y*v.y));
3261 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3262 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3263 gradient->angle))))/gradient->radii.x;
3264 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3265 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3266 gradient->angle))))/gradient->radii.y;
3267 return(sqrt(v.x*v.x+v.y*v.y));
3273 static int StopInfoCompare(const void *x,const void *y)
3279 stop_1=(StopInfo *) x;
3280 stop_2=(StopInfo *) y;
3281 if (stop_1->offset > stop_2->offset)
3283 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
3288 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3289 const DrawInfo *draw_info,ExceptionInfo *exception)
3319 Draw linear or radial gradient on image.
3321 assert(image != (Image *) NULL);
3322 assert(image->signature == MagickCoreSignature);
3323 if (image->debug != MagickFalse)
3324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3325 assert(draw_info != (const DrawInfo *) NULL);
3326 gradient=(&draw_info->gradient);
3327 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
3329 gradient_vector=(&gradient->gradient_vector);
3330 point.x=gradient_vector->x2-gradient_vector->x1;
3331 point.y=gradient_vector->y2-gradient_vector->y1;
3332 length=sqrt(point.x*point.x+point.y*point.y);
3333 bounding_box=gradient->bounding_box;
3335 GetPixelInfo(image,&zero);
3336 image_view=AcquireAuthenticCacheView(image,exception);
3337 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3338 #pragma omp parallel for schedule(static,4) shared(status) \
3339 magick_threads(image,image,1,1)
3341 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3361 if (status == MagickFalse)
3363 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3364 if (q == (Quantum *) NULL)
3371 offset=GetStopColorOffset(gradient,0,y);
3372 if (gradient->type != RadialGradient)
3374 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3376 GetPixelInfoPixel(image,q,&pixel);
3377 switch (gradient->spread)
3379 case UndefinedSpread:
3382 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3383 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3385 offset=GetStopColorOffset(gradient,x,y);
3386 if (gradient->type != RadialGradient)
3389 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3390 if (offset < gradient->stops[i].offset)
3392 if ((offset < 0.0) || (i == 0))
3393 composite=gradient->stops[0].color;
3395 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3396 composite=gradient->stops[gradient->number_stops-1].color;
3401 alpha=(offset-gradient->stops[i].offset)/
3402 (gradient->stops[j].offset-gradient->stops[i].offset);
3403 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3404 &gradient->stops[j].color,alpha,&composite);
3410 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3411 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3413 offset=GetStopColorOffset(gradient,x,y);
3414 if (gradient->type != RadialGradient)
3419 if ((ssize_t) fmod(offset,2.0) == 0)
3420 offset=fmod(offset,1.0);
3422 offset=1.0-fmod(offset,1.0);
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 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3438 &gradient->stops[j].color,alpha,&composite);
3450 antialias=MagickFalse;
3452 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3453 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3455 offset=GetStopColorOffset(gradient,x,y);
3456 if (gradient->type == LinearGradient)
3458 repeat=fmod(offset,length);
3460 repeat=length-fmod(-repeat,length);
3462 repeat=fmod(offset,length);
3463 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3464 MagickTrue : MagickFalse;
3465 offset=repeat/length;
3469 repeat=fmod(offset,gradient->radius);
3471 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3473 repeat=fmod(offset,gradient->radius);
3474 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3476 offset=repeat/gradient->radius;
3479 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3480 if (offset < gradient->stops[i].offset)
3483 composite=gradient->stops[0].color;
3485 if (i == (ssize_t) gradient->number_stops)
3486 composite=gradient->stops[gradient->number_stops-1].color;
3491 alpha=(offset-gradient->stops[i].offset)/
3492 (gradient->stops[j].offset-gradient->stops[i].offset);
3493 if (antialias != MagickFalse)
3495 if (gradient->type == LinearGradient)
3496 alpha=length-repeat;
3498 alpha=gradient->radius-repeat;
3500 j=(ssize_t) gradient->number_stops-1L;
3502 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3503 &gradient->stops[j].color,alpha,&composite);
3508 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3510 SetPixelViaPixelInfo(image,&pixel,q);
3511 q+=GetPixelChannels(image);
3513 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3516 image_view=DestroyCacheView(image_view);
3521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3525 % D r a w P a t t e r n P a t h %
3529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3531 % DrawPatternPath() draws a pattern.
3533 % The format of the DrawPatternPath method is:
3535 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3536 % const char *name,Image **pattern,ExceptionInfo *exception)
3538 % A description of each parameter follows:
3540 % o image: the image.
3542 % o draw_info: the draw info.
3544 % o name: the pattern name.
3546 % o image: the image.
3548 % o exception: return any errors or warnings in this structure.
3551 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3552 const DrawInfo *draw_info,const char *name,Image **pattern,
3553 ExceptionInfo *exception)
3556 property[MagickPathExtent];
3572 assert(image != (Image *) NULL);
3573 assert(image->signature == MagickCoreSignature);
3574 if (image->debug != MagickFalse)
3575 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3576 assert(draw_info != (const DrawInfo *) NULL);
3577 assert(name != (const char *) NULL);
3578 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
3579 path=GetImageArtifact(image,property);
3580 if (path == (const char *) NULL)
3581 return(MagickFalse);
3582 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
3583 geometry=GetImageArtifact(image,property);
3584 if (geometry == (const char *) NULL)
3585 return(MagickFalse);
3586 if ((*pattern) != (Image *) NULL)
3587 *pattern=DestroyImage(*pattern);
3588 image_info=AcquireImageInfo();
3589 image_info->size=AcquireString(geometry);
3590 *pattern=AcquireImage(image_info,exception);
3591 image_info=DestroyImageInfo(image_info);
3592 (void) QueryColorCompliance("#000000ff",AllCompliance,
3593 &(*pattern)->background_color,exception);
3594 (void) SetImageBackgroundColor(*pattern,exception);
3595 if (image->debug != MagickFalse)
3596 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3597 "begin pattern-path %s %s",name,geometry);
3598 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3599 clone_info->fill_pattern=NewImageList();
3600 clone_info->stroke_pattern=NewImageList();
3601 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
3602 type=GetImageArtifact(image,property);
3603 if (type != (const char *) NULL)
3604 clone_info->gradient.type=(GradientType) ParseCommandOption(
3605 MagickGradientOptions,MagickFalse,type);
3606 (void) CloneString(&clone_info->primitive,path);
3607 status=DrawImage(*pattern,clone_info,exception);
3608 clone_info=DestroyDrawInfo(clone_info);
3609 if (image->debug != MagickFalse)
3610 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3619 + D r a w P o l y g o n P r i m i t i v e %
3623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3625 % DrawPolygonPrimitive() draws a polygon on the image.
3627 % The format of the DrawPolygonPrimitive method is:
3629 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3630 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3631 % ExceptionInfo *exception)
3633 % A description of each parameter follows:
3635 % o image: the image.
3637 % o draw_info: the draw info.
3639 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3641 % o exception: return any errors or warnings in this structure.
3645 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3650 assert(polygon_info != (PolygonInfo **) NULL);
3651 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3652 if (polygon_info[i] != (PolygonInfo *) NULL)
3653 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3654 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3655 return(polygon_info);
3658 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3659 const PrimitiveInfo *primitive_info)
3662 *magick_restrict path_info;
3673 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3674 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3675 sizeof(*polygon_info));
3676 if (polygon_info == (PolygonInfo **) NULL)
3677 return((PolygonInfo **) NULL);
3678 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3679 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3680 if (path_info == (PathInfo *) NULL)
3681 return(DestroyPolygonThreadSet(polygon_info));
3682 for (i=0; i < (ssize_t) number_threads; i++)
3684 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3685 if (polygon_info[i] == (PolygonInfo *) NULL)
3686 return(DestroyPolygonThreadSet(polygon_info));
3688 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3689 return(polygon_info);
3692 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3693 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3694 const ssize_t y,double *stroke_alpha)
3705 register const PointInfo
3719 Compute fill & stroke opacity for this (x,y) point.
3723 p=polygon_info->edges;
3724 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3726 if ((double) y <= (p->bounds.y1-mid-0.5))
3728 if ((double) y > (p->bounds.y2+mid+0.5))
3730 (void) DestroyEdge(polygon_info,(size_t) j);
3733 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3734 ((double) x > (p->bounds.x2+mid+0.5)))
3736 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3737 for ( ; i < (ssize_t) p->number_points; i++)
3739 if ((double) y <= (p->points[i-1].y-mid-0.5))
3741 if ((double) y > (p->points[i].y+mid+0.5))
3743 if (p->scanline != (double) y)
3745 p->scanline=(double) y;
3746 p->highwater=(size_t) i;
3749 Compute distance between a point and an edge.
3752 delta.x=(q+1)->x-q->x;
3753 delta.y=(q+1)->y-q->y;
3754 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3757 delta.x=(double) x-q->x;
3758 delta.y=(double) y-q->y;
3759 distance=delta.x*delta.x+delta.y*delta.y;
3763 alpha=delta.x*delta.x+delta.y*delta.y;
3766 delta.x=(double) x-(q+1)->x;
3767 delta.y=(double) y-(q+1)->y;
3768 distance=delta.x*delta.x+delta.y*delta.y;
3773 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3774 distance=alpha*beta*beta;
3778 Compute stroke & subpath opacity.
3781 if (p->ghostline == MagickFalse)
3784 if ((*stroke_alpha < 1.0) &&
3785 (distance <= ((alpha+0.25)*(alpha+0.25))))
3788 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3793 if (distance != 1.0)
3794 beta=sqrt((double) distance);
3796 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3797 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3801 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3803 if (distance <= 0.0)
3813 if (distance != 1.0)
3814 beta=sqrt(distance);
3817 if (subpath_alpha < (alpha*alpha))
3818 subpath_alpha=alpha*alpha;
3822 Compute fill opacity.
3824 if (fill == MagickFalse)
3826 if (subpath_alpha >= 1.0)
3829 Determine winding number.
3832 p=polygon_info->edges;
3833 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3835 if ((double) y <= p->bounds.y1)
3837 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3839 if ((double) x > p->bounds.x2)
3841 winding_number+=p->direction ? 1 : -1;
3844 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3845 for ( ; i < (ssize_t) p->number_points; i++)
3846 if ((double) y <= p->points[i].y)
3849 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3850 winding_number+=p->direction ? 1 : -1;
3852 if (fill_rule != NonZeroRule)
3854 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3858 if (MagickAbsoluteValue(winding_number) != 0)
3860 return(subpath_alpha);
3863 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3864 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3865 ExceptionInfo *exception)
3878 **magick_restrict polygon_info;
3895 Compute bounding box.
3897 assert(image != (Image *) NULL);
3898 assert(image->signature == MagickCoreSignature);
3899 if (image->debug != MagickFalse)
3900 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3901 assert(draw_info != (DrawInfo *) NULL);
3902 assert(draw_info->signature == MagickCoreSignature);
3903 assert(primitive_info != (PrimitiveInfo *) NULL);
3904 if (primitive_info->coordinates == 0)
3906 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3907 if (polygon_info == (PolygonInfo **) NULL)
3908 return(MagickFalse);
3909 DisableMSCWarning(4127)
3911 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3913 if (image->debug != MagickFalse)
3914 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3915 fill=(primitive_info->method == FillToBorderMethod) ||
3916 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3917 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3918 bounds=polygon_info[0]->edges[0].bounds;
3919 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3921 p=polygon_info[0]->edges+i;
3922 if (p->bounds.x1 < bounds.x1)
3923 bounds.x1=p->bounds.x1;
3924 if (p->bounds.y1 < bounds.y1)
3925 bounds.y1=p->bounds.y1;
3926 if (p->bounds.x2 > bounds.x2)
3927 bounds.x2=p->bounds.x2;
3928 if (p->bounds.y2 > bounds.y2)
3929 bounds.y2=p->bounds.y2;
3931 bounds.x1-=(mid+1.0);
3932 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3933 image->columns ? (double) image->columns-1 : bounds.x1;
3934 bounds.y1-=(mid+1.0);
3935 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3936 image->rows ? (double) image->rows-1 : bounds.y1;
3937 bounds.x2+=(mid+1.0);
3938 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3939 image->columns ? (double) image->columns-1 : bounds.x2;
3940 bounds.y2+=(mid+1.0);
3941 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3942 image->rows ? (double) image->rows-1 : bounds.y2;
3944 image_view=AcquireAuthenticCacheView(image,exception);
3945 if ((primitive_info->coordinates == 1) ||
3946 (polygon_info[0]->number_edges == 0))
3951 start_y=(ssize_t) ceil(bounds.y1-0.5);
3952 stop_y=(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; y <= stop_y; y++)
3975 if (status == MagickFalse)
3977 start_x=(ssize_t) ceil(bounds.x1-0.5);
3978 stop_x=(ssize_t) floor(bounds.x2+0.5);
3980 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
3982 if (q == (Quantum *) NULL)
3987 GetPixelInfo(image,&pixel);
3988 for ( ; x <= stop_x; x++)
3990 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3991 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3993 (void) GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
3994 SetPixelViaPixelInfo(image,&pixel,q);
3996 q+=GetPixelChannels(image);
3998 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3999 if (sync == MagickFalse)
4002 image_view=DestroyCacheView(image_view);
4003 polygon_info=DestroyPolygonThreadSet(polygon_info);
4004 if (image->debug != MagickFalse)
4005 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4006 " end draw-polygon");
4010 Draw polygon or line.
4012 if (image->alpha_trait == UndefinedPixelTrait)
4013 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4014 start_y=(ssize_t) ceil(bounds.y1-0.5);
4015 stop_y=(ssize_t) floor(bounds.y2+0.5);
4016 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4017 #pragma omp parallel for schedule(static,4) shared(status) \
4018 magick_threads(image,image,1,1)
4020 for (y=start_y; y <= stop_y; y++)
4023 id = GetOpenMPThreadId();
4043 if (status == MagickFalse)
4045 start_x=(ssize_t) ceil(bounds.x1-0.5);
4046 stop_x=(ssize_t) floor(bounds.x2+0.5);
4047 q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+1),1,
4049 if (q == (Quantum *) NULL)
4054 for (x=start_x; x <= stop_x; x++)
4059 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4061 if (draw_info->stroke_antialias == MagickFalse)
4063 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4064 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4066 (void) GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4067 fill_alpha=fill_alpha*fill_color.alpha;
4068 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4069 GetPixelAlpha(image,q),q);
4070 (void) GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4071 stroke_alpha=stroke_alpha*stroke_color.alpha;
4072 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4073 GetPixelAlpha(image,q),q);
4074 q+=GetPixelChannels(image);
4076 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4079 image_view=DestroyCacheView(image_view);
4080 polygon_info=DestroyPolygonThreadSet(polygon_info);
4081 if (image->debug != MagickFalse)
4082 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4091 % D r a w P r i m i t i v e %
4095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4097 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4099 % The format of the DrawPrimitive method is:
4101 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4102 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4104 % A description of each parameter follows:
4106 % o image: the image.
4108 % o draw_info: the draw info.
4110 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4112 % o exception: return any errors or warnings in this structure.
4116 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4142 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4143 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4144 switch (primitive_info->primitive)
4146 case AlphaPrimitive:
4148 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4149 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4150 methods[primitive_info->method]);
4153 case ColorPrimitive:
4155 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4156 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4157 methods[primitive_info->method]);
4160 case ImagePrimitive:
4162 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4163 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4166 case PointPrimitive:
4168 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4169 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4170 methods[primitive_info->method]);
4175 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4176 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4183 p=primitive_info[0].point;
4186 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4188 point=primitive_info[i].point;
4189 if (coordinates <= 0)
4191 coordinates=(ssize_t) primitive_info[i].coordinates;
4192 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4193 " begin open (%.20g)",(double) coordinates);
4196 point=primitive_info[i].point;
4197 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4198 (fabs(q.y-point.y) >= MagickEpsilon))
4199 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4200 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4202 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4203 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4206 if (coordinates > 0)
4208 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4209 (fabs(p.y-point.y) >= MagickEpsilon))
4210 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4211 (double) coordinates);
4213 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4214 (double) coordinates);
4218 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4219 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4220 ExceptionInfo *exception)
4235 if (image->debug != MagickFalse)
4237 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4238 " begin draw-primitive");
4239 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4240 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4241 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4242 draw_info->affine.tx,draw_info->affine.ty);
4244 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4245 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4246 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4247 (void) SetImageColorspace(image,sRGBColorspace,exception);
4249 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4250 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4251 image_view=AcquireAuthenticCacheView(image,exception);
4252 switch (primitive_info->primitive)
4254 case AlphaPrimitive:
4256 if (image->alpha_trait == UndefinedPixelTrait)
4257 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4258 switch (primitive_info->method)
4269 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4270 if (q == (Quantum *) NULL)
4272 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4273 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4274 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4286 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4288 GetPixelInfo(image,&pixel);
4289 for (y=0; y < (ssize_t) image->rows; y++)
4294 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4296 if (q == (Quantum *) NULL)
4298 for (x=0; x < (ssize_t) image->columns; x++)
4300 GetPixelInfoPixel(image,q,&pixel);
4301 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4303 q+=GetPixelChannels(image);
4306 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4307 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4308 q+=GetPixelChannels(image);
4310 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4311 if (sync == MagickFalse)
4316 case FloodfillMethod:
4317 case FillToBorderMethod:
4325 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4327 if (primitive_info->method == FillToBorderMethod)
4329 target.red=(double) draw_info->border_color.red;
4330 target.green=(double) draw_info->border_color.green;
4331 target.blue=(double) draw_info->border_color.blue;
4333 channel_mask=SetImageChannelMask(image,AlphaChannel);
4334 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4335 primitive_info->method == FloodfillMethod ? MagickFalse :
4336 MagickTrue,exception);
4337 (void) SetImageChannelMask(image,channel_mask);
4348 for (y=0; y < (ssize_t) image->rows; y++)
4353 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4355 if (q == (Quantum *) NULL)
4357 for (x=0; x < (ssize_t) image->columns; x++)
4359 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4360 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4361 q+=GetPixelChannels(image);
4363 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4364 if (sync == MagickFalse)
4372 case ColorPrimitive:
4374 switch (primitive_info->method)
4385 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4386 if (q == (Quantum *) NULL)
4388 GetPixelInfo(image,&pixel);
4389 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4390 SetPixelViaPixelInfo(image,&pixel,q);
4391 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4403 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4405 for (y=0; y < (ssize_t) image->rows; y++)
4410 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4412 if (q == (Quantum *) NULL)
4414 for (x=0; x < (ssize_t) image->columns; x++)
4416 GetPixelInfoPixel(image,q,&pixel);
4417 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4419 q+=GetPixelChannels(image);
4422 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4423 SetPixelViaPixelInfo(image,&pixel,q);
4424 q+=GetPixelChannels(image);
4426 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4427 if (sync == MagickFalse)
4432 case FloodfillMethod:
4433 case FillToBorderMethod:
4438 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4440 if (primitive_info->method == FillToBorderMethod)
4442 target.red=(double) draw_info->border_color.red;
4443 target.green=(double) draw_info->border_color.green;
4444 target.blue=(double) draw_info->border_color.blue;
4446 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4447 primitive_info->method == FloodfillMethod ? MagickFalse :
4448 MagickTrue,exception);
4459 GetPixelInfo(image,&pixel);
4460 for (y=0; y < (ssize_t) image->rows; y++)
4465 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4467 if (q == (Quantum *) NULL)
4469 for (x=0; x < (ssize_t) image->columns; x++)
4471 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4472 SetPixelViaPixelInfo(image,&pixel,q);
4473 q+=GetPixelChannels(image);
4475 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4476 if (sync == MagickFalse)
4484 case ImagePrimitive:
4490 composite_geometry[MagickPathExtent];
4505 if (primitive_info->text == (char *) NULL)
4507 clone_info=AcquireImageInfo();
4508 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4509 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4513 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4515 composite_image=ReadImage(clone_info,exception);
4517 clone_info=DestroyImageInfo(clone_info);
4518 if (composite_image == (Image *) NULL)
4520 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4521 NULL,(void *) NULL);
4522 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4523 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4524 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4525 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4530 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4531 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
4532 composite_image->filter=image->filter;
4533 (void) TransformImage(&composite_image,(char *) NULL,
4534 composite_geometry,exception);
4536 if (composite_image->alpha_trait == UndefinedPixelTrait)
4537 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4539 if (draw_info->alpha != OpaqueAlpha)
4540 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4541 SetGeometry(image,&geometry);
4542 image->gravity=draw_info->gravity;
4545 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4546 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4547 composite_image->rows,(double) geometry.x,(double) geometry.y);
4548 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4549 affine=draw_info->affine;
4550 affine.tx=(double) geometry.x;
4551 affine.ty=(double) geometry.y;
4552 composite_image->interpolate=image->interpolate;
4553 if (draw_info->compose == OverCompositeOp)
4554 (void) DrawAffineImage(image,composite_image,&affine,exception);
4556 (void) CompositeImage(image,composite_image,draw_info->compose,
4557 MagickTrue,geometry.x,geometry.y,exception);
4558 composite_image=DestroyImage(composite_image);
4561 case PointPrimitive:
4569 if ((y < 0) || (y >= (ssize_t) image->rows))
4571 if ((x < 0) || (x >= (ssize_t) image->columns))
4573 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4574 if (q == (Quantum *) NULL)
4576 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4577 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4578 (double) GetPixelAlpha(image,q),q);
4579 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4585 geometry[MagickPathExtent];
4590 if (primitive_info->text == (char *) NULL)
4592 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4593 (void) CloneString(&clone_info->text,primitive_info->text);
4594 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4595 primitive_info->point.x,primitive_info->point.y);
4596 (void) CloneString(&clone_info->geometry,geometry);
4597 status&=AnnotateImage(image,clone_info,exception);
4598 clone_info=DestroyDrawInfo(clone_info);
4610 if (IsEventLogging() != MagickFalse)
4611 LogPrimitiveInfo(primitive_info);
4612 scale=ExpandAffine(&draw_info->affine);
4613 if ((draw_info->dash_pattern != (double *) NULL) &&
4614 (draw_info->dash_pattern[0] != 0.0) &&
4615 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4616 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4621 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4622 clone_info->stroke_width=0.0;
4623 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4624 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4626 clone_info=DestroyDrawInfo(clone_info);
4627 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4630 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4632 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4638 Draw strokes while respecting line cap/join attributes.
4640 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4642 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4643 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4644 MagickTrue : MagickFalse;
4645 i=(ssize_t) primitive_info[0].coordinates;
4646 if ((((draw_info->linecap == RoundCap) ||
4647 (closed_path != MagickFalse)) &&
4648 (draw_info->linejoin == RoundJoin)) ||
4649 (primitive_info[i].primitive != UndefinedPrimitive))
4651 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4655 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4656 clone_info->stroke_width=0.0;
4657 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4658 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4660 clone_info=DestroyDrawInfo(clone_info);
4661 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4664 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4668 image_view=DestroyCacheView(image_view);
4669 if (image->debug != MagickFalse)
4670 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4671 return(status != 0 ? MagickTrue : MagickFalse);
4675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4679 + D r a w S t r o k e P o l y g o n %
4683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4685 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4686 % the image while respecting the line cap and join attributes.
4688 % The format of the DrawStrokePolygon method is:
4690 % MagickBooleanType DrawStrokePolygon(Image *image,
4691 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4693 % A description of each parameter follows:
4695 % o image: the image.
4697 % o draw_info: the draw info.
4699 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4704 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4705 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4713 for (i=0; i < 4; i++)
4714 linecap[i]=(*primitive_info);
4715 linecap[0].coordinates=4;
4716 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4717 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4718 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4719 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4720 linecap[4].primitive=UndefinedPrimitive;
4721 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4724 static MagickBooleanType DrawStrokePolygon(Image *image,
4725 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4726 ExceptionInfo *exception)
4740 register const PrimitiveInfo
4745 Draw stroked polygon.
4747 if (image->debug != MagickFalse)
4748 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4749 " begin draw-stroke-polygon");
4750 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4751 clone_info->fill=draw_info->stroke;
4752 if (clone_info->fill_pattern != (Image *) NULL)
4753 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4754 if (clone_info->stroke_pattern != (Image *) NULL)
4755 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4756 MagickTrue,exception);
4757 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4758 clone_info->stroke_width=0.0;
4759 clone_info->fill_rule=NonZeroRule;
4761 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4763 stroke_polygon=TraceStrokePolygon(draw_info,p);
4764 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4767 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4768 q=p+p->coordinates-1;
4769 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4770 MagickTrue : MagickFalse;
4771 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4773 DrawRoundLinecap(image,draw_info,p,exception);
4774 DrawRoundLinecap(image,draw_info,q,exception);
4777 clone_info=DestroyDrawInfo(clone_info);
4778 if (image->debug != MagickFalse)
4779 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4780 " end draw-stroke-polygon");
4781 return(status != 0 ? MagickTrue : MagickFalse);
4785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4789 % G e t A f f i n e M a t r i x %
4793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4795 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4798 % The format of the GetAffineMatrix method is:
4800 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4802 % A description of each parameter follows:
4804 % o affine_matrix: the affine matrix.
4807 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4809 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4810 assert(affine_matrix != (AffineMatrix *) NULL);
4811 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4812 affine_matrix->sx=1.0;
4813 affine_matrix->sy=1.0;
4817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4821 + G e t D r a w I n f o %
4825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4827 % GetDrawInfo() initializes draw_info to default values from image_info.
4829 % The format of the GetDrawInfo method is:
4831 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4833 % A description of each parameter follows:
4835 % o image_info: the image info..
4837 % o draw_info: the draw info.
4840 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4852 Initialize draw attributes.
4854 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4855 assert(draw_info != (DrawInfo *) NULL);
4856 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4857 clone_info=CloneImageInfo(image_info);
4858 GetAffineMatrix(&draw_info->affine);
4859 exception=AcquireExceptionInfo();
4860 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4862 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4864 draw_info->stroke_width=1.0;
4865 draw_info->alpha=OpaqueAlpha;
4866 draw_info->fill_rule=EvenOddRule;
4867 draw_info->linecap=ButtCap;
4868 draw_info->linejoin=MiterJoin;
4869 draw_info->miterlimit=10;
4870 draw_info->decorate=NoDecoration;
4871 draw_info->pointsize=12.0;
4872 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4873 draw_info->compose=OverCompositeOp;
4874 draw_info->render=MagickTrue;
4875 draw_info->debug=IsEventLogging();
4876 draw_info->stroke_antialias=clone_info->antialias;
4877 if (clone_info->font != (char *) NULL)
4878 draw_info->font=AcquireString(clone_info->font);
4879 option=GetImageOption(clone_info,"font-features");
4880 if (option != (char *) NULL)
4881 draw_info->font_features=AcquireString(option);
4882 if (clone_info->density != (char *) NULL)
4883 draw_info->density=AcquireString(clone_info->density);
4884 draw_info->text_antialias=clone_info->antialias;
4885 if (clone_info->pointsize != 0.0)
4886 draw_info->pointsize=clone_info->pointsize;
4887 draw_info->border_color=clone_info->border_color;
4888 if (clone_info->server_name != (char *) NULL)
4889 draw_info->server_name=AcquireString(clone_info->server_name);
4890 option=GetImageOption(clone_info,"direction");
4891 if (option != (const char *) NULL)
4892 draw_info->direction=(DirectionType) ParseCommandOption(
4893 MagickDirectionOptions,MagickFalse,option);
4895 draw_info->direction=UndefinedDirection;
4896 option=GetImageOption(clone_info,"encoding");
4897 if (option != (const char *) NULL)
4898 (void) CloneString(&draw_info->encoding,option);
4899 option=GetImageOption(clone_info,"family");
4900 if (option != (const char *) NULL)
4901 (void) CloneString(&draw_info->family,option);
4902 option=GetImageOption(clone_info,"fill");
4903 if (option != (const char *) NULL)
4904 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4906 option=GetImageOption(clone_info,"gravity");
4907 if (option != (const char *) NULL)
4908 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4909 MagickFalse,option);
4910 option=GetImageOption(clone_info,"interline-spacing");
4911 if (option != (const char *) NULL)
4912 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4913 option=GetImageOption(clone_info,"interword-spacing");
4914 if (option != (const char *) NULL)
4915 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4916 option=GetImageOption(clone_info,"kerning");
4917 if (option != (const char *) NULL)
4918 draw_info->kerning=StringToDouble(option,(char **) NULL);
4919 option=GetImageOption(clone_info,"stroke");
4920 if (option != (const char *) NULL)
4921 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4923 option=GetImageOption(clone_info,"strokewidth");
4924 if (option != (const char *) NULL)
4925 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4926 option=GetImageOption(clone_info,"style");
4927 if (option != (const char *) NULL)
4928 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
4929 MagickFalse,option);
4930 option=GetImageOption(clone_info,"undercolor");
4931 if (option != (const char *) NULL)
4932 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4934 option=GetImageOption(clone_info,"weight");
4935 if (option != (const char *) NULL)
4940 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
4942 weight=StringToUnsignedLong(option);
4943 draw_info->weight=(size_t) weight;
4945 exception=DestroyExceptionInfo(exception);
4946 draw_info->signature=MagickCoreSignature;
4947 clone_info=DestroyImageInfo(clone_info);
4951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4955 + P e r m u t a t e %
4959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4961 % Permutate() returns the permuation of the (n,k).
4963 % The format of the Permutate method is:
4965 % void Permutate(ssize_t n,ssize_t k)
4967 % A description of each parameter follows:
4975 static inline double Permutate(const ssize_t n,const ssize_t k)
4984 for (i=k+1; i <= n; i++)
4986 for (i=1; i <= (n-k); i++)
4992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4996 + T r a c e P r i m i t i v e %
5000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5002 % TracePrimitive is a collection of methods for generating graphic
5003 % primitives such as arcs, ellipses, paths, etc.
5007 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
5008 const PointInfo end,const PointInfo degrees)
5014 center.x=0.5*(end.x+start.x);
5015 center.y=0.5*(end.y+start.y);
5016 radii.x=fabs(center.x-start.x);
5017 radii.y=fabs(center.y-start.y);
5018 TraceEllipse(primitive_info,center,radii,degrees);
5021 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
5022 const PointInfo end,const PointInfo arc,const double angle,
5023 const MagickBooleanType large_arc,const MagickBooleanType sweep)
5042 register PrimitiveInfo
5051 if ((start.x == end.x) && (start.y == end.y))
5053 TracePoint(primitive_info,end);
5056 radii.x=fabs(arc.x);
5057 radii.y=fabs(arc.y);
5058 if ((radii.x == 0.0) || (radii.y == 0.0))
5060 TraceLine(primitive_info,start,end);
5063 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5064 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5065 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5066 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5067 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5069 if (delta < MagickEpsilon)
5071 TraceLine(primitive_info,start,end);
5076 radii.x*=sqrt((double) delta);
5077 radii.y*=sqrt((double) delta);
5079 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5080 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5081 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5082 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5083 alpha=points[1].x-points[0].x;
5084 beta=points[1].y-points[0].y;
5085 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5090 factor=sqrt((double) factor);
5091 if (sweep == large_arc)
5094 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5095 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5096 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5097 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5098 if ((theta < 0.0) && (sweep != MagickFalse))
5099 theta+=(double) (2.0*MagickPI);
5101 if ((theta > 0.0) && (sweep == MagickFalse))
5102 theta-=(double) (2.0*MagickPI);
5103 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5106 for (i=0; i < (ssize_t) arc_segments; i++)
5108 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5109 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5110 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5111 sin(fmod((double) beta,DegreesToRadians(360.0)));
5112 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5113 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5114 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5115 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5116 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5117 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5118 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5119 theta/arc_segments),DegreesToRadians(360.0))));
5120 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5121 theta/arc_segments),DegreesToRadians(360.0))));
5122 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5123 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5124 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5125 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5126 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5127 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5128 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5130 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5132 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5134 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5136 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5138 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5140 if (i == (ssize_t) (arc_segments-1))
5145 primitive_info->coordinates=(size_t) (p-primitive_info);
5146 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5148 p->primitive=primitive_info->primitive;
5153 static void TraceBezier(PrimitiveInfo *primitive_info,
5154 const size_t number_coordinates)
5166 register PrimitiveInfo
5178 Allocate coeficients.
5180 quantum=number_coordinates;
5181 for (i=0; i < (ssize_t) number_coordinates; i++)
5183 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5185 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5186 if (alpha > (double) quantum)
5187 quantum=(size_t) alpha;
5188 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5189 if (alpha > (double) quantum)
5190 quantum=(size_t) alpha;
5193 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5194 (double) BezierQuantum);
5195 control_points=quantum*number_coordinates;
5196 coefficients=(double *) AcquireQuantumMemory((size_t)
5197 number_coordinates,sizeof(*coefficients));
5198 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5200 if ((coefficients == (double *) NULL) ||
5201 (points == (PointInfo *) NULL))
5202 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5204 Compute bezier points.
5206 end=primitive_info[number_coordinates-1].point;
5207 for (i=0; i < (ssize_t) number_coordinates; i++)
5208 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5210 for (i=0; i < (ssize_t) control_points; i++)
5215 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5216 for (j=0; j < (ssize_t) number_coordinates; j++)
5218 point.x+=alpha*coefficients[j]*p->point.x;
5219 point.y+=alpha*coefficients[j]*p->point.y;
5220 alpha*=weight/(1.0-weight);
5224 weight+=1.0/control_points;
5227 Bezier curves are just short segmented polys.
5230 for (i=0; i < (ssize_t) control_points; i++)
5232 TracePoint(p,points[i]);
5237 primitive_info->coordinates=(size_t) (p-primitive_info);
5238 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5240 p->primitive=primitive_info->primitive;
5243 points=(PointInfo *) RelinquishMagickMemory(points);
5244 coefficients=(double *) RelinquishMagickMemory(coefficients);
5247 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5248 const PointInfo end)
5259 alpha=end.x-start.x;
5261 radius=hypot((double) alpha,(double) beta);
5262 offset.x=(double) radius;
5263 offset.y=(double) radius;
5266 TraceEllipse(primitive_info,start,offset,degrees);
5269 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5270 const PointInfo stop,const PointInfo degrees)
5281 register PrimitiveInfo
5288 Ellipses are just short segmented polys.
5290 if ((stop.x == 0.0) && (stop.y == 0.0))
5292 TracePoint(primitive_info,start);
5295 delta=2.0/MagickMax(stop.x,stop.y);
5296 step=(double) (MagickPI/8.0);
5297 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5298 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5299 angle.x=DegreesToRadians(degrees.x);
5301 while (y < degrees.x)
5303 angle.y=(double) DegreesToRadians(y);
5304 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5306 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5307 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5308 TracePoint(p,point);
5311 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5312 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5313 TracePoint(p,point);
5315 primitive_info->coordinates=(size_t) (p-primitive_info);
5316 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5318 p->primitive=primitive_info->primitive;
5323 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5324 const PointInfo end)
5326 TracePoint(primitive_info,start);
5327 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5328 (fabs(start.y-end.y) < MagickEpsilon))
5330 primitive_info->primitive=PointPrimitive;
5331 primitive_info->coordinates=1;
5334 TracePoint(primitive_info+1,end);
5335 (primitive_info+1)->primitive=primitive_info->primitive;
5336 primitive_info->coordinates=2;
5339 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5342 token[MagickPathExtent];
5357 points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5364 register PrimitiveInfo
5375 number_coordinates=0;
5377 primitive_type=primitive_info->primitive;
5379 for (p=path; *p != '\0'; )
5381 while (isspace((int) ((unsigned char) *p)) != 0)
5385 last_attribute=attribute;
5386 attribute=(int) (*p++);
5407 GetMagickToken(p,&p,token);
5409 GetMagickToken(p,&p,token);
5410 arc.x=StringToDouble(token,(char **) NULL);
5411 GetMagickToken(p,&p,token);
5413 GetMagickToken(p,&p,token);
5414 arc.y=StringToDouble(token,(char **) NULL);
5415 GetMagickToken(p,&p,token);
5417 GetMagickToken(p,&p,token);
5418 angle=StringToDouble(token,(char **) NULL);
5419 GetMagickToken(p,&p,token);
5421 GetMagickToken(p,&p,token);
5422 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5423 GetMagickToken(p,&p,token);
5425 GetMagickToken(p,&p,token);
5426 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5427 GetMagickToken(p,&p,token);
5429 GetMagickToken(p,&p,token);
5430 x=StringToDouble(token,(char **) NULL);
5431 GetMagickToken(p,&p,token);
5433 GetMagickToken(p,&p,token);
5434 y=StringToDouble(token,(char **) NULL);
5435 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5436 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5437 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5440 while (isspace((int) ((unsigned char) *p)) != 0)
5444 } while (IsPoint(p) != MagickFalse);
5451 Compute bezier points.
5456 for (i=1; i < 4; i++)
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 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5467 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5470 for (i=0; i < 4; i++)
5471 (q+i)->point=points[i];
5475 } while (IsPoint(p) != MagickFalse);
5483 GetMagickToken(p,&p,token);
5485 GetMagickToken(p,&p,token);
5486 x=StringToDouble(token,(char **) NULL);
5487 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5488 TracePoint(q,point);
5490 } while (IsPoint(p) != MagickFalse);
5498 GetMagickToken(p,&p,token);
5500 GetMagickToken(p,&p,token);
5501 x=StringToDouble(token,(char **) NULL);
5502 GetMagickToken(p,&p,token);
5504 GetMagickToken(p,&p,token);
5505 y=StringToDouble(token,(char **) NULL);
5506 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5507 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5508 TracePoint(q,point);
5510 } while (IsPoint(p) != MagickFalse);
5516 if (q != primitive_info)
5518 primitive_info->coordinates=(size_t) (q-primitive_info);
5519 number_coordinates+=primitive_info->coordinates;
5525 GetMagickToken(p,&p,token);
5527 GetMagickToken(p,&p,token);
5528 x=StringToDouble(token,(char **) NULL);
5529 GetMagickToken(p,&p,token);
5531 GetMagickToken(p,&p,token);
5532 y=StringToDouble(token,(char **) NULL);
5533 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5534 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5538 TracePoint(q,point);
5540 if ((i != 0) && (attribute == (int) 'M'))
5542 TracePoint(q,point);
5545 } while (IsPoint(p) != MagickFalse);
5552 Compute bezier points.
5557 for (i=1; i < 3; i++)
5559 GetMagickToken(p,&p,token);
5561 GetMagickToken(p,&p,token);
5562 x=StringToDouble(token,(char **) NULL);
5563 GetMagickToken(p,&p,token);
5565 GetMagickToken(p,&p,token);
5566 y=StringToDouble(token,(char **) NULL);
5569 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5570 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5573 for (i=0; i < 3; i++)
5574 (q+i)->point=points[i];
5578 } while (IsPoint(p) != MagickFalse);
5585 Compute bezier points.
5589 points[0]=points[3];
5590 points[1].x=2.0*points[3].x-points[2].x;
5591 points[1].y=2.0*points[3].y-points[2].y;
5592 for (i=2; i < 4; i++)
5594 GetMagickToken(p,&p,token);
5596 GetMagickToken(p,&p,token);
5597 x=StringToDouble(token,(char **) NULL);
5598 GetMagickToken(p,&p,token);
5600 GetMagickToken(p,&p,token);
5601 y=StringToDouble(token,(char **) NULL);
5604 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5605 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5608 if (strchr("CcSs",last_attribute) == (char *) NULL)
5613 for (i=0; i < 4; i++)
5614 (q+i)->point=points[i];
5618 } while (IsPoint(p) != MagickFalse);
5625 Compute bezier points.
5629 points[0]=points[2];
5630 points[1].x=2.0*points[2].x-points[1].x;
5631 points[1].y=2.0*points[2].y-points[1].y;
5632 for (i=2; i < 3; i++)
5634 GetMagickToken(p,&p,token);
5636 GetMagickToken(p,&p,token);
5637 x=StringToDouble(token,(char **) NULL);
5638 GetMagickToken(p,&p,token);
5640 GetMagickToken(p,&p,token);
5641 y=StringToDouble(token,(char **) NULL);
5642 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5643 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5646 if (strchr("QqTt",last_attribute) == (char *) NULL)
5651 for (i=0; i < 3; i++)
5652 (q+i)->point=points[i];
5656 } while (IsPoint(p) != MagickFalse);
5664 GetMagickToken(p,&p,token);
5666 GetMagickToken(p,&p,token);
5667 y=StringToDouble(token,(char **) NULL);
5668 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5669 TracePoint(q,point);
5671 } while (IsPoint(p) != MagickFalse);
5678 TracePoint(q,point);
5680 primitive_info->coordinates=(size_t) (q-primitive_info);
5681 number_coordinates+=primitive_info->coordinates;
5688 if (isalpha((int) ((unsigned char) attribute)) != 0)
5689 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5695 primitive_info->coordinates=(size_t) (q-primitive_info);
5696 number_coordinates+=primitive_info->coordinates;
5697 for (i=0; i < (ssize_t) number_coordinates; i++)
5700 q->primitive=primitive_type;
5702 q->method=FillToBorderMethod;
5705 return(number_coordinates);
5708 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5709 const PointInfo end)
5714 register PrimitiveInfo
5721 TracePoint(p,start);
5725 TracePoint(p,point);
5731 TracePoint(p,point);
5733 TracePoint(p,start);
5735 primitive_info->coordinates=(size_t) (p-primitive_info);
5736 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5738 p->primitive=primitive_info->primitive;
5743 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5744 const PointInfo start,const PointInfo end,PointInfo arc)
5751 register PrimitiveInfo
5758 offset.x=fabs(end.x-start.x);
5759 offset.y=fabs(end.y-start.y);
5760 if (arc.x > (0.5*offset.x))
5762 if (arc.y > (0.5*offset.y))
5764 point.x=start.x+offset.x-arc.x;
5765 point.y=start.y+arc.y;
5768 TraceEllipse(p,point,arc,degrees);
5770 point.x=start.x+offset.x-arc.x;
5771 point.y=start.y+offset.y-arc.y;
5774 TraceEllipse(p,point,arc,degrees);
5776 point.x=start.x+arc.x;
5777 point.y=start.y+offset.y-arc.y;
5780 TraceEllipse(p,point,arc,degrees);
5782 point.x=start.x+arc.x;
5783 point.y=start.y+arc.y;
5786 TraceEllipse(p,point,arc,degrees);
5788 TracePoint(p,primitive_info->point);
5790 primitive_info->coordinates=(size_t) (p-primitive_info);
5791 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5793 p->primitive=primitive_info->primitive;
5798 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5799 const size_t number_vertices,const double offset)
5816 for (i=1; i < (ssize_t) number_vertices; i++)
5818 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5819 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5820 if ((fabs((double) dx) >= MagickEpsilon) ||
5821 (fabs((double) dy) >= MagickEpsilon))
5824 if (i == (ssize_t) number_vertices)
5825 i=(ssize_t) number_vertices-1L;
5826 distance=hypot((double) dx,(double) dy);
5827 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5828 dx*(distance+offset)/distance);
5829 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5830 dy*(distance+offset)/distance);
5831 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5833 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5834 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5835 if ((fabs((double) dx) >= MagickEpsilon) ||
5836 (fabs((double) dy) >= MagickEpsilon))
5839 distance=hypot((double) dx,(double) dy);
5840 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5841 dx*(distance+offset)/distance);
5842 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5843 dy*(distance+offset)/distance);
5846 static inline double DrawEpsilonReciprocal(const double x)
5848 #define DrawEpsilon (1.0e-6)
5850 double sign = x < 0.0 ? -1.0 : 1.0;
5851 return((sign*x) >= DrawEpsilon ? 1.0/x : sign*(1.0/DrawEpsilon));
5854 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5855 const PrimitiveInfo *primitive_info)
5857 typedef struct _LineSegment
5909 number_vertices=primitive_info->coordinates;
5910 max_strokes=2*number_vertices+6*BezierQuantum+360;
5911 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5913 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5915 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5916 number_vertices+2UL,sizeof(*polygon_primitive));
5917 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5918 (polygon_primitive == (PrimitiveInfo *) NULL))
5919 return((PrimitiveInfo *) NULL);
5920 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5921 number_vertices*sizeof(*polygon_primitive));
5923 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5924 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5925 MagickTrue : MagickFalse;
5926 if ((draw_info->linejoin == RoundJoin) ||
5927 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5929 polygon_primitive[number_vertices]=primitive_info[1];
5932 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5934 Compute the slope for the first line segment, p.
5938 for (n=1; n < (ssize_t) number_vertices; n++)
5940 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5941 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5942 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5945 if (n == (ssize_t) number_vertices)
5946 n=(ssize_t) number_vertices-1L;
5947 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5948 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5949 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5950 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5952 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5953 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5954 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5955 offset.y=(double) (offset.x*inverse_slope.p);
5956 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5958 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5959 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5960 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5961 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5962 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5963 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5964 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5965 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5969 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5970 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5971 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5972 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5973 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5974 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5975 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5976 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5979 Create strokes for the line join attribute: bevel, miter, round.
5983 path_q[p++]=box_q[0];
5984 path_p[q++]=box_p[0];
5985 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5988 Compute the slope for this line segment, q.
5990 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5991 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5992 dot_product=dx.q*dx.q+dy.q*dy.q;
5993 if (dot_product < 0.25)
5995 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5996 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5997 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5998 offset.y=(double) (offset.x*inverse_slope.q);
5999 dot_product=dy.q*offset.x-dx.q*offset.y;
6000 if (dot_product > 0.0)
6002 box_p[2].x=polygon_primitive[n].point.x-offset.x;
6003 box_p[2].y=polygon_primitive[n].point.y-offset.y;
6004 box_p[3].x=polygon_primitive[i].point.x-offset.x;
6005 box_p[3].y=polygon_primitive[i].point.y-offset.y;
6006 box_q[2].x=polygon_primitive[n].point.x+offset.x;
6007 box_q[2].y=polygon_primitive[n].point.y+offset.y;
6008 box_q[3].x=polygon_primitive[i].point.x+offset.x;
6009 box_q[3].y=polygon_primitive[i].point.y+offset.y;
6013 box_p[2].x=polygon_primitive[n].point.x+offset.x;
6014 box_p[2].y=polygon_primitive[n].point.y+offset.y;
6015 box_p[3].x=polygon_primitive[i].point.x+offset.x;
6016 box_p[3].y=polygon_primitive[i].point.y+offset.y;
6017 box_q[2].x=polygon_primitive[n].point.x-offset.x;
6018 box_q[2].y=polygon_primitive[n].point.y-offset.y;
6019 box_q[3].x=polygon_primitive[i].point.x-offset.x;
6020 box_q[3].y=polygon_primitive[i].point.y-offset.y;
6022 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
6029 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
6030 box_p[3].y)/(slope.p-slope.q));
6031 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
6032 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
6033 box_q[3].y)/(slope.p-slope.q));
6034 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
6036 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
6038 max_strokes+=6*BezierQuantum+360;
6039 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
6041 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
6043 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
6045 polygon_primitive=(PrimitiveInfo *)
6046 RelinquishMagickMemory(polygon_primitive);
6047 return((PrimitiveInfo *) NULL);
6050 dot_product=dx.q*dy.p-dx.p*dy.q;
6051 if (dot_product <= 0.0)
6052 switch (draw_info->linejoin)
6056 path_q[q++]=box_q[1];
6057 path_q[q++]=box_q[2];
6058 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6059 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6060 if (dot_product <= miterlimit)
6061 path_p[p++]=box_p[4];
6064 path_p[p++]=box_p[1];
6065 path_p[p++]=box_p[2];
6071 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6072 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6073 if (dot_product <= miterlimit)
6075 path_q[q++]=box_q[4];
6076 path_p[p++]=box_p[4];
6080 path_q[q++]=box_q[1];
6081 path_q[q++]=box_q[2];
6082 path_p[p++]=box_p[1];
6083 path_p[p++]=box_p[2];
6089 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6090 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6091 if (dot_product <= miterlimit)
6092 path_p[p++]=box_p[4];
6095 path_p[p++]=box_p[1];
6096 path_p[p++]=box_p[2];
6098 center=polygon_primitive[n].point;
6099 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6100 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6101 if (theta.q < theta.p)
6102 theta.q+=(double) (2.0*MagickPI);
6103 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6104 (2.0*sqrt((double) (1.0/mid)))));
6105 path_q[q].x=box_q[1].x;
6106 path_q[q].y=box_q[1].y;
6108 for (j=1; j < (ssize_t) arc_segments; j++)
6110 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6111 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6112 (theta.p+delta_theta),DegreesToRadians(360.0))));
6113 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6114 (theta.p+delta_theta),DegreesToRadians(360.0))));
6117 path_q[q++]=box_q[2];
6124 switch (draw_info->linejoin)
6128 path_p[p++]=box_p[1];
6129 path_p[p++]=box_p[2];
6130 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6131 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6132 if (dot_product <= miterlimit)
6133 path_q[q++]=box_q[4];
6136 path_q[q++]=box_q[1];
6137 path_q[q++]=box_q[2];
6143 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6144 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6145 if (dot_product <= miterlimit)
6147 path_q[q++]=box_q[4];
6148 path_p[p++]=box_p[4];
6152 path_q[q++]=box_q[1];
6153 path_q[q++]=box_q[2];
6154 path_p[p++]=box_p[1];
6155 path_p[p++]=box_p[2];
6161 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6162 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6163 if (dot_product <= miterlimit)
6164 path_q[q++]=box_q[4];
6167 path_q[q++]=box_q[1];
6168 path_q[q++]=box_q[2];
6170 center=polygon_primitive[n].point;
6171 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6172 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6173 if (theta.p < theta.q)
6174 theta.p+=(double) (2.0*MagickPI);
6175 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6176 (2.0*sqrt((double) (1.0/mid)))));
6177 path_p[p++]=box_p[1];
6178 for (j=1; j < (ssize_t) arc_segments; j++)
6180 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6181 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6182 (theta.p+delta_theta),DegreesToRadians(360.0))));
6183 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6184 (theta.p+delta_theta),DegreesToRadians(360.0))));
6187 path_p[p++]=box_p[2];
6194 inverse_slope.p=inverse_slope.q;
6203 path_p[p++]=box_p[1];
6204 path_q[q++]=box_q[1];
6206 Trace stroked polygon.
6208 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6209 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6210 if (stroke_polygon != (PrimitiveInfo *) NULL)
6212 for (i=0; i < (ssize_t) p; i++)
6214 stroke_polygon[i]=polygon_primitive[0];
6215 stroke_polygon[i].point=path_p[i];
6217 if (closed_path != MagickFalse)
6219 stroke_polygon[i]=polygon_primitive[0];
6220 stroke_polygon[i].point=stroke_polygon[0].point;
6223 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6225 stroke_polygon[i]=polygon_primitive[0];
6226 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6228 if (closed_path != MagickFalse)
6230 stroke_polygon[i]=polygon_primitive[0];
6231 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6234 stroke_polygon[i]=polygon_primitive[0];
6235 stroke_polygon[i].point=stroke_polygon[0].point;
6237 stroke_polygon[i].primitive=UndefinedPrimitive;
6238 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6240 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6241 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6242 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6243 return(stroke_polygon);