2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/channel.h"
55 #include "MagickCore/color.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/draw.h"
61 #include "MagickCore/draw-private.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/geometry.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/log.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/paint.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/pixel-private.h"
76 #include "MagickCore/property.h"
77 #include "MagickCore/resample.h"
78 #include "MagickCore/resample-private.h"
79 #include "MagickCore/resource_.h"
80 #include "MagickCore/string_.h"
81 #include "MagickCore/string-private.h"
82 #include "MagickCore/thread-private.h"
83 #include "MagickCore/token.h"
84 #include "MagickCore/transform.h"
85 #include "MagickCore/utility.h"
90 #define BezierQuantum 200
95 typedef struct _EdgeInfo
119 typedef struct _ElementInfo
129 typedef struct _PolygonInfo
147 typedef struct _PathInfo
157 Forward declarations.
159 static MagickBooleanType
160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
167 TracePath(PrimitiveInfo *,const char *);
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
172 const double,const MagickBooleanType,const MagickBooleanType),
173 TraceBezier(PrimitiveInfo *,const size_t),
174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,
177 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
179 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
181 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % A c q u i r e D r a w I n f o %
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
196 % The format of the AcquireDrawInfo method is:
198 % DrawInfo *AcquireDrawInfo(void)
201 MagickExport DrawInfo *AcquireDrawInfo(void)
206 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
207 if (draw_info == (DrawInfo *) NULL)
208 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
209 GetDrawInfo((ImageInfo *) NULL,draw_info);
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % C l o n e D r a w I n f o %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
225 % is specified, a new DrawInfo structure is created initialized to default
228 % The format of the CloneDrawInfo method is:
230 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
231 % const DrawInfo *draw_info)
233 % A description of each parameter follows:
235 % o image_info: the image info.
237 % o draw_info: the draw info.
240 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
241 const DrawInfo *draw_info)
249 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
250 if (clone_info == (DrawInfo *) NULL)
251 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
252 GetDrawInfo(image_info,clone_info);
253 if (draw_info == (DrawInfo *) NULL)
255 exception=AcquireExceptionInfo();
256 (void) CloneString(&clone_info->primitive,draw_info->primitive);
257 (void) CloneString(&clone_info->geometry,draw_info->geometry);
258 clone_info->viewbox=draw_info->viewbox;
259 clone_info->affine=draw_info->affine;
260 clone_info->gravity=draw_info->gravity;
261 clone_info->fill=draw_info->fill;
262 clone_info->stroke=draw_info->stroke;
263 clone_info->stroke_width=draw_info->stroke_width;
264 if (draw_info->fill_pattern != (Image *) NULL)
265 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
267 if (draw_info->stroke_pattern != (Image *) NULL)
268 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
269 MagickTrue,exception);
270 clone_info->stroke_antialias=draw_info->stroke_antialias;
271 clone_info->text_antialias=draw_info->text_antialias;
272 clone_info->fill_rule=draw_info->fill_rule;
273 clone_info->linecap=draw_info->linecap;
274 clone_info->linejoin=draw_info->linejoin;
275 clone_info->miterlimit=draw_info->miterlimit;
276 clone_info->dash_offset=draw_info->dash_offset;
277 clone_info->decorate=draw_info->decorate;
278 clone_info->compose=draw_info->compose;
279 (void) CloneString(&clone_info->text,draw_info->text);
280 (void) CloneString(&clone_info->font,draw_info->font);
281 (void) CloneString(&clone_info->metrics,draw_info->metrics);
282 (void) CloneString(&clone_info->family,draw_info->family);
283 clone_info->style=draw_info->style;
284 clone_info->stretch=draw_info->stretch;
285 clone_info->weight=draw_info->weight;
286 (void) CloneString(&clone_info->encoding,draw_info->encoding);
287 clone_info->pointsize=draw_info->pointsize;
288 clone_info->kerning=draw_info->kerning;
289 clone_info->interline_spacing=draw_info->interline_spacing;
290 clone_info->interword_spacing=draw_info->interword_spacing;
291 clone_info->direction=draw_info->direction;
292 (void) CloneString(&clone_info->density,draw_info->density);
293 clone_info->align=draw_info->align;
294 clone_info->undercolor=draw_info->undercolor;
295 clone_info->border_color=draw_info->border_color;
296 (void) CloneString(&clone_info->server_name,draw_info->server_name);
297 if (draw_info->dash_pattern != (double *) NULL)
302 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
303 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
304 sizeof(*clone_info->dash_pattern));
305 if (clone_info->dash_pattern == (double *) NULL)
306 ThrowFatalException(ResourceLimitFatalError,
307 "UnableToAllocateDashPattern");
308 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
309 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
311 clone_info->gradient=draw_info->gradient;
312 if (draw_info->gradient.stops != (StopInfo *) NULL)
317 number_stops=clone_info->gradient.number_stops;
318 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
319 number_stops,sizeof(*clone_info->gradient.stops));
320 if (clone_info->gradient.stops == (StopInfo *) NULL)
321 ThrowFatalException(ResourceLimitFatalError,
322 "UnableToAllocateDashPattern");
323 (void) CopyMagickMemory(clone_info->gradient.stops,
324 draw_info->gradient.stops,(size_t) number_stops*
325 sizeof(*clone_info->gradient.stops));
327 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
328 clone_info->bounds=draw_info->bounds;
329 clone_info->clip_units=draw_info->clip_units;
330 clone_info->render=draw_info->render;
331 clone_info->alpha=draw_info->alpha;
332 clone_info->element_reference=draw_info->element_reference;
333 clone_info->debug=IsEventLogging();
334 exception=DestroyExceptionInfo(exception);
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 + C o n v e r t P a t h T o P o l y g o n %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % ConvertPathToPolygon() converts a path to the more efficient sorted
352 % The format of the ConvertPathToPolygon method is:
354 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
355 % const PathInfo *path_info)
357 % A description of each parameter follows:
359 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
360 % rendering form of type PolygonInfo.
362 % o draw_info: Specifies a pointer to an DrawInfo structure.
364 % o path_info: Specifies a pointer to an PathInfo structure.
369 #if defined(__cplusplus) || defined(c_plusplus)
373 static int CompareEdges(const void *x,const void *y)
375 register const EdgeInfo
382 p=(const EdgeInfo *) x;
383 q=(const EdgeInfo *) y;
384 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
386 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
388 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
390 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
392 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
393 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
398 #if defined(__cplusplus) || defined(c_plusplus)
402 static void LogPolygonInfo(const PolygonInfo *polygon_info)
411 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
412 p=polygon_info->edges;
413 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
415 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
417 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
418 p->direction != MagickFalse ? "down" : "up");
419 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
420 p->ghostline != MagickFalse ? "transparent" : "opaque");
421 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
422 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
423 p->bounds.x2,p->bounds.y2);
424 for (j=0; j < (ssize_t) p->number_points; j++)
425 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
426 p->points[j].x,p->points[j].y);
429 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
432 static void ReversePoints(PointInfo *points,const size_t number_points)
440 for (i=0; i < (ssize_t) (number_points >> 1); i++)
443 points[i]=points[number_points-(i+1)];
444 points[number_points-(i+1)]=point;
448 static PolygonInfo *ConvertPathToPolygon(
449 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
478 Convert a path to the more efficient sorted rendering form.
480 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
481 if (polygon_info == (PolygonInfo *) NULL)
482 return((PolygonInfo *) NULL);
484 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
485 sizeof(*polygon_info->edges));
486 if (polygon_info->edges == (EdgeInfo *) NULL)
487 return((PolygonInfo *) NULL);
490 ghostline=MagickFalse;
493 points=(PointInfo *) NULL;
494 (void) ResetMagickMemory(&point,0,sizeof(point));
495 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
496 for (i=0; path_info[i].code != EndCode; i++)
498 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
499 (path_info[i].code == GhostlineCode))
504 if ((points != (PointInfo *) NULL) && (n >= 2))
506 if (edge == number_edges)
509 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
510 polygon_info->edges,(size_t) number_edges,
511 sizeof(*polygon_info->edges));
512 if (polygon_info->edges == (EdgeInfo *) NULL)
513 return((PolygonInfo *) NULL);
515 polygon_info->edges[edge].number_points=(size_t) n;
516 polygon_info->edges[edge].scanline=(-1.0);
517 polygon_info->edges[edge].highwater=0;
518 polygon_info->edges[edge].ghostline=ghostline;
519 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
521 ReversePoints(points,(size_t) n);
522 polygon_info->edges[edge].points=points;
523 polygon_info->edges[edge].bounds=bounds;
524 polygon_info->edges[edge].bounds.y1=points[0].y;
525 polygon_info->edges[edge].bounds.y2=points[n-1].y;
526 points=(PointInfo *) NULL;
527 ghostline=MagickFalse;
530 if (points == (PointInfo *) NULL)
533 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
535 if (points == (PointInfo *) NULL)
536 return((PolygonInfo *) NULL);
538 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
539 point=path_info[i].point;
550 next_direction=((path_info[i].point.y > point.y) ||
551 ((path_info[i].point.y == point.y) &&
552 (path_info[i].point.x > point.x))) ? 1 : -1;
553 if ((points != (PointInfo *) NULL) && (direction != 0) &&
554 (direction != next_direction))
560 if (edge == number_edges)
563 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
564 polygon_info->edges,(size_t) number_edges,
565 sizeof(*polygon_info->edges));
566 if (polygon_info->edges == (EdgeInfo *) NULL)
567 return((PolygonInfo *) NULL);
569 polygon_info->edges[edge].number_points=(size_t) n;
570 polygon_info->edges[edge].scanline=(-1.0);
571 polygon_info->edges[edge].highwater=0;
572 polygon_info->edges[edge].ghostline=ghostline;
573 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
575 ReversePoints(points,(size_t) n);
576 polygon_info->edges[edge].points=points;
577 polygon_info->edges[edge].bounds=bounds;
578 polygon_info->edges[edge].bounds.y1=points[0].y;
579 polygon_info->edges[edge].bounds.y2=points[n-1].y;
581 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
583 if (points == (PointInfo *) NULL)
584 return((PolygonInfo *) NULL);
586 ghostline=MagickFalse;
592 direction=next_direction;
593 if (points == (PointInfo *) NULL)
595 if (n == (ssize_t) number_points)
598 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
600 if (points == (PointInfo *) NULL)
601 return((PolygonInfo *) NULL);
603 point=path_info[i].point;
605 if (point.x < bounds.x1)
607 if (point.x > bounds.x2)
611 if (points != (PointInfo *) NULL)
614 points=(PointInfo *) RelinquishMagickMemory(points);
617 if (edge == number_edges)
620 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
621 polygon_info->edges,(size_t) number_edges,
622 sizeof(*polygon_info->edges));
623 if (polygon_info->edges == (EdgeInfo *) NULL)
624 return((PolygonInfo *) NULL);
626 polygon_info->edges[edge].number_points=(size_t) n;
627 polygon_info->edges[edge].scanline=(-1.0);
628 polygon_info->edges[edge].highwater=0;
629 polygon_info->edges[edge].ghostline=ghostline;
630 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
632 ReversePoints(points,(size_t) n);
633 polygon_info->edges[edge].points=points;
634 polygon_info->edges[edge].bounds=bounds;
635 polygon_info->edges[edge].bounds.y1=points[0].y;
636 polygon_info->edges[edge].bounds.y2=points[n-1].y;
637 ghostline=MagickFalse;
641 polygon_info->number_edges=edge;
642 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
643 sizeof(*polygon_info->edges),CompareEdges);
644 if (IsEventLogging() != MagickFalse)
645 LogPolygonInfo(polygon_info);
646 return(polygon_info);
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 + C o n v e r t P r i m i t i v e T o P a t h %
658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
663 % The format of the ConvertPrimitiveToPath method is:
665 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
666 % const PrimitiveInfo *primitive_info)
668 % A description of each parameter follows:
670 % o Method ConvertPrimitiveToPath returns a vector path structure of type
673 % o draw_info: a structure of type DrawInfo.
675 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
680 static void LogPathInfo(const PathInfo *path_info)
682 register const PathInfo
685 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
686 for (p=path_info; p->code != EndCode; p++)
687 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
688 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
689 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
690 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
692 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
695 static PathInfo *ConvertPrimitiveToPath(
696 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
717 Converts a PrimitiveInfo structure into a vector path structure.
719 switch (primitive_info->primitive)
726 return((PathInfo *) NULL);
730 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
731 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
733 if (path_info == (PathInfo *) NULL)
734 return((PathInfo *) NULL);
742 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
745 if (coordinates <= 0)
747 coordinates=(ssize_t) primitive_info[i].coordinates;
748 p=primitive_info[i].point;
754 Eliminate duplicate points.
756 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
757 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
759 path_info[n].code=code;
760 path_info[n].point=primitive_info[i].point;
761 q=primitive_info[i].point;
766 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
767 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
770 Mark the p point as open if it does not match the q.
772 path_info[start].code=OpenCode;
773 path_info[n].code=GhostlineCode;
774 path_info[n].point=primitive_info[i].point;
776 path_info[n].code=LineToCode;
777 path_info[n].point=p;
780 path_info[n].code=EndCode;
781 path_info[n].point.x=0.0;
782 path_info[n].point.y=0.0;
783 if (IsEventLogging() != MagickFalse)
784 LogPathInfo(path_info);
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % D e s t r o y D r a w I n f o %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
802 % The format of the DestroyDrawInfo method is:
804 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
806 % A description of each parameter follows:
808 % o draw_info: the draw info.
811 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
813 if (draw_info->debug != MagickFalse)
814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
815 assert(draw_info != (DrawInfo *) NULL);
816 assert(draw_info->signature == MagickSignature);
817 if (draw_info->primitive != (char *) NULL)
818 draw_info->primitive=DestroyString(draw_info->primitive);
819 if (draw_info->text != (char *) NULL)
820 draw_info->text=DestroyString(draw_info->text);
821 if (draw_info->geometry != (char *) NULL)
822 draw_info->geometry=DestroyString(draw_info->geometry);
823 if (draw_info->fill_pattern != (Image *) NULL)
824 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
825 if (draw_info->stroke_pattern != (Image *) NULL)
826 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
827 if (draw_info->font != (char *) NULL)
828 draw_info->font=DestroyString(draw_info->font);
829 if (draw_info->metrics != (char *) NULL)
830 draw_info->metrics=DestroyString(draw_info->metrics);
831 if (draw_info->family != (char *) NULL)
832 draw_info->family=DestroyString(draw_info->family);
833 if (draw_info->encoding != (char *) NULL)
834 draw_info->encoding=DestroyString(draw_info->encoding);
835 if (draw_info->density != (char *) NULL)
836 draw_info->density=DestroyString(draw_info->density);
837 if (draw_info->server_name != (char *) NULL)
838 draw_info->server_name=(char *)
839 RelinquishMagickMemory(draw_info->server_name);
840 if (draw_info->dash_pattern != (double *) NULL)
841 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
842 draw_info->dash_pattern);
843 if (draw_info->gradient.stops != (StopInfo *) NULL)
844 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
845 draw_info->gradient.stops);
846 if (draw_info->clip_mask != (char *) NULL)
847 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
848 draw_info->signature=(~MagickSignature);
849 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 + D e s t r o y E d g e %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % DestroyEdge() destroys the specified polygon edge.
866 % The format of the DestroyEdge method is:
868 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
870 % A description of each parameter follows:
872 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
874 % o edge: the polygon edge number to destroy.
877 static size_t DestroyEdge(PolygonInfo *polygon_info,
880 assert(edge < polygon_info->number_edges);
881 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
882 polygon_info->edges[edge].points);
883 polygon_info->number_edges--;
884 if (edge < polygon_info->number_edges)
885 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
886 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
887 return(polygon_info->number_edges);
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 + D e s t r o y P o l y g o n I n f o %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
903 % The format of the DestroyPolygonInfo method is:
905 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
907 % A description of each parameter follows:
909 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
912 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
917 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
918 polygon_info->edges[i].points=(PointInfo *)
919 RelinquishMagickMemory(polygon_info->edges[i].points);
920 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
921 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929 % D r a w A f f i n e I m a g e %
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 % DrawAffineImage() composites the source over the destination image as
936 % dictated by the affine transform.
938 % The format of the DrawAffineImage method is:
940 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
941 % const AffineMatrix *affine,ExceptionInfo *exception)
943 % A description of each parameter follows:
945 % o image: the image.
947 % o source: the source image.
949 % o affine: the affine transform.
951 % o exception: return any errors or warnings in this structure.
955 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
956 const double y,const SegmentInfo *edge)
969 Determine left and right edges.
971 inverse_edge.x1=edge->x1;
972 inverse_edge.y1=edge->y1;
973 inverse_edge.x2=edge->x2;
974 inverse_edge.y2=edge->y2;
975 z=affine->ry*y+affine->tx;
976 if (affine->sx >= MagickEpsilon)
978 intercept=(-z/affine->sx);
980 if (x > inverse_edge.x1)
982 intercept=(-z+(double) image->columns)/affine->sx;
984 if (x < inverse_edge.x2)
988 if (affine->sx < -MagickEpsilon)
990 intercept=(-z+(double) image->columns)/affine->sx;
992 if (x > inverse_edge.x1)
994 intercept=(-z/affine->sx);
996 if (x < inverse_edge.x2)
1000 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1002 inverse_edge.x2=edge->x1;
1003 return(inverse_edge);
1006 Determine top and bottom edges.
1008 z=affine->sy*y+affine->ty;
1009 if (affine->rx >= MagickEpsilon)
1011 intercept=(-z/affine->rx);
1013 if (x > inverse_edge.x1)
1015 intercept=(-z+(double) image->rows)/affine->rx;
1017 if (x < inverse_edge.x2)
1021 if (affine->rx < -MagickEpsilon)
1023 intercept=(-z+(double) image->rows)/affine->rx;
1025 if (x > inverse_edge.x1)
1027 intercept=(-z/affine->rx);
1029 if (x < inverse_edge.x2)
1033 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1035 inverse_edge.x2=edge->x2;
1036 return(inverse_edge);
1038 return(inverse_edge);
1041 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1049 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1051 inverse_affine.sx=determinant*affine->sy;
1052 inverse_affine.rx=determinant*(-affine->rx);
1053 inverse_affine.ry=determinant*(-affine->ry);
1054 inverse_affine.sy=determinant*affine->sx;
1055 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1057 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1059 return(inverse_affine);
1062 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1063 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1096 Determine bounding box.
1098 assert(image != (Image *) NULL);
1099 assert(image->signature == MagickSignature);
1100 if (image->debug != MagickFalse)
1101 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1102 assert(source != (const Image *) NULL);
1103 assert(source->signature == MagickSignature);
1104 assert(affine != (AffineMatrix *) NULL);
1107 extent[1].x=(double) source->columns-1.0;
1109 extent[2].x=(double) source->columns-1.0;
1110 extent[2].y=(double) source->rows-1.0;
1112 extent[3].y=(double) source->rows-1.0;
1113 for (i=0; i < 4; i++)
1116 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1117 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1121 for (i=1; i < 4; i++)
1123 if (min.x > extent[i].x)
1125 if (min.y > extent[i].y)
1127 if (max.x < extent[i].x)
1129 if (max.y < extent[i].y)
1133 Affine transform image.
1135 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1136 return(MagickFalse);
1138 edge.x1=MagickMax(min.x,0.0);
1139 edge.y1=MagickMax(min.y,0.0);
1140 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1141 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1142 inverse_affine=InverseAffineMatrix(affine);
1143 GetPixelInfo(image,&zero);
1144 start=(ssize_t) ceil(edge.y1-0.5);
1145 stop=(ssize_t) floor(edge.y2+0.5);
1146 source_view=AcquireVirtualCacheView(source,exception);
1147 image_view=AcquireAuthenticCacheView(image,exception);
1148 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1149 #pragma omp parallel for schedule(static,4) shared(status) \
1150 magick_threads(source,image,1,1)
1152 for (y=start; y <= stop; y++)
1173 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1174 if (inverse_edge.x2 < inverse_edge.x1)
1176 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1177 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1179 if (q == (Quantum *) NULL)
1184 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1186 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1188 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1190 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1191 point.x,point.y,&pixel,exception);
1192 GetPixelInfoPixel(image,q,&composite);
1193 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1195 SetPixelViaPixelInfo(image,&composite,q);
1197 q+=GetPixelChannels(image);
1199 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1202 source_view=DestroyCacheView(source_view);
1203 image_view=DestroyCacheView(image_view);
1208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1212 + D r a w B o u n d i n g R e c t a n g l e s %
1216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1218 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1219 % is only useful for developers debugging the rendering algorithm.
1221 % The format of the DrawBoundingRectangles method is:
1223 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1224 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1226 % A description of each parameter follows:
1228 % o image: the image.
1230 % o draw_info: the draw info.
1232 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1234 % o exception: return any errors or warnings in this structure.
1237 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1238 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1263 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1264 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1266 resolution.x=DefaultResolution;
1267 resolution.y=DefaultResolution;
1268 if (clone_info->density != (char *) NULL)
1276 flags=ParseGeometry(clone_info->density,&geometry_info);
1277 resolution.x=geometry_info.rho;
1278 resolution.y=geometry_info.sigma;
1279 if ((flags & SigmaValue) == MagickFalse)
1280 resolution.y=resolution.x;
1282 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1283 clone_info->stroke_width/2.0;
1288 if (polygon_info != (PolygonInfo *) NULL)
1290 bounds=polygon_info->edges[0].bounds;
1291 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1293 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1294 bounds.x1=polygon_info->edges[i].bounds.x1;
1295 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1296 bounds.y1=polygon_info->edges[i].bounds.y1;
1297 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1298 bounds.x2=polygon_info->edges[i].bounds.x2;
1299 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1300 bounds.y2=polygon_info->edges[i].bounds.y2;
1303 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1304 image->columns ? (double) image->columns-1 : bounds.x1;
1306 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1307 image->rows ? (double) image->rows-1 : bounds.y1;
1309 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1310 image->columns ? (double) image->columns-1 : bounds.x2;
1312 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1313 image->rows ? (double) image->rows-1 : bounds.y2;
1314 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1316 if (polygon_info->edges[i].direction != 0)
1317 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1320 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1322 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1323 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1324 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1325 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1326 primitive_info[0].primitive=RectanglePrimitive;
1327 TraceRectangle(primitive_info,start,end);
1328 primitive_info[0].method=ReplaceMethod;
1329 coordinates=(ssize_t) primitive_info[0].coordinates;
1330 primitive_info[coordinates].primitive=UndefinedPrimitive;
1331 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1334 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1336 start.x=(double) (bounds.x1-mid);
1337 start.y=(double) (bounds.y1-mid);
1338 end.x=(double) (bounds.x2+mid);
1339 end.y=(double) (bounds.y2+mid);
1340 primitive_info[0].primitive=RectanglePrimitive;
1341 TraceRectangle(primitive_info,start,end);
1342 primitive_info[0].method=ReplaceMethod;
1343 coordinates=(ssize_t) primitive_info[0].coordinates;
1344 primitive_info[coordinates].primitive=UndefinedPrimitive;
1345 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1346 clone_info=DestroyDrawInfo(clone_info);
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % D r a w C l i p P a t h %
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % DrawClipPath() draws the clip path on the image mask.
1362 % The format of the DrawClipPath method is:
1364 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1365 % const char *name,ExceptionInfo *exception)
1367 % A description of each parameter follows:
1369 % o image: the image.
1371 % o draw_info: the draw info.
1373 % o name: the name of the clip path.
1375 % o exception: return any errors or warnings in this structure.
1378 MagickExport MagickBooleanType DrawClipPath(Image *image,
1379 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1382 filename[MaxTextExtent];
1396 assert(image != (Image *) NULL);
1397 assert(image->signature == MagickSignature);
1398 if (image->debug != MagickFalse)
1399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1400 assert(draw_info != (const DrawInfo *) NULL);
1401 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1402 value=GetImageArtifact(image,filename);
1403 if (value == (const char *) NULL)
1404 return(MagickFalse);
1405 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1406 if (clip_mask == (Image *) NULL)
1407 return(MagickFalse);
1408 (void) QueryColorCompliance("#0000",AllCompliance,
1409 &clip_mask->background_color,exception);
1410 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1411 (void) SetImageBackgroundColor(clip_mask,exception);
1412 if (image->debug != MagickFalse)
1413 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1414 draw_info->clip_mask);
1415 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1416 (void) CloneString(&clone_info->primitive,value);
1417 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1419 clone_info->clip_mask=(char *) NULL;
1420 status=NegateImage(clip_mask,MagickFalse,exception);
1421 (void) SetImageMask(image,clip_mask,exception);
1422 clip_mask=DestroyImage(clip_mask);
1423 status&=DrawImage(image,clone_info,exception);
1424 clone_info=DestroyDrawInfo(clone_info);
1425 if (image->debug != MagickFalse)
1426 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1427 return(status != 0 ? MagickTrue : MagickFalse);
1431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435 + D r a w D a s h P o l y g o n %
1439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1441 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1442 % image while respecting the dash offset and dash pattern attributes.
1444 % The format of the DrawDashPolygon method is:
1446 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1447 % const PrimitiveInfo *primitive_info,Image *image,
1448 % ExceptionInfo *exception)
1450 % A description of each parameter follows:
1452 % o draw_info: the draw info.
1454 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1456 % o image: the image.
1458 % o exception: return any errors or warnings in this structure.
1461 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1462 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1494 assert(draw_info != (const DrawInfo *) NULL);
1495 if (image->debug != MagickFalse)
1496 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1497 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1498 clone_info->miterlimit=0;
1499 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1500 number_vertices=(size_t) i;
1501 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1502 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1503 if (dash_polygon == (PrimitiveInfo *) NULL)
1504 return(MagickFalse);
1505 dash_polygon[0]=primitive_info[0];
1506 scale=ExpandAffine(&draw_info->affine);
1507 length=scale*(draw_info->dash_pattern[0]-0.5);
1508 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1510 for (n=0; offset > 0.0; j=0)
1512 if (draw_info->dash_pattern[n] <= 0.0)
1514 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1515 if (offset > length)
1519 length=scale*(draw_info->dash_pattern[n]+0.5);
1522 if (offset < length)
1534 for (i=1; i < (ssize_t) number_vertices; i++)
1536 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1537 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1538 maximum_length=hypot((double) dx,dy);
1542 if (draw_info->dash_pattern[n] == 0.0)
1544 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1546 for (total_length=0.0; (total_length+length) <= maximum_length; )
1548 total_length+=length;
1549 if ((n & 0x01) != 0)
1551 dash_polygon[0]=primitive_info[0];
1552 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1553 total_length/maximum_length);
1554 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1555 total_length/maximum_length);
1560 if ((j+1) > (ssize_t) (2*number_vertices))
1562 dash_polygon[j]=primitive_info[i-1];
1563 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1564 total_length/maximum_length);
1565 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1566 total_length/maximum_length);
1567 dash_polygon[j].coordinates=1;
1569 dash_polygon[0].coordinates=(size_t) j;
1570 dash_polygon[j].primitive=UndefinedPrimitive;
1571 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1574 if (draw_info->dash_pattern[n] == 0.0)
1576 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1578 length-=(maximum_length-total_length);
1579 if ((n & 0x01) != 0)
1581 dash_polygon[j]=primitive_info[i];
1582 dash_polygon[j].coordinates=1;
1585 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1587 dash_polygon[j]=primitive_info[i-1];
1588 dash_polygon[j].point.x+=MagickEpsilon;
1589 dash_polygon[j].point.y+=MagickEpsilon;
1590 dash_polygon[j].coordinates=1;
1592 dash_polygon[0].coordinates=(size_t) j;
1593 dash_polygon[j].primitive=UndefinedPrimitive;
1594 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1596 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1597 clone_info=DestroyDrawInfo(clone_info);
1598 if (image->debug != MagickFalse)
1599 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1600 return(status != 0 ? MagickTrue : MagickFalse);
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608 % D r a w I m a g e %
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1614 % DrawImage() draws a graphic primitive on your image. The primitive
1615 % may be represented as a string or filename. Precede the filename with an
1616 % "at" sign (@) and the contents of the file are drawn on the image. You
1617 % can affect how text is drawn by setting one or more members of the draw
1620 % The format of the DrawImage method is:
1622 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1623 % ExceptionInfo *exception)
1625 % A description of each parameter follows:
1627 % o image: the image.
1629 % o draw_info: the draw info.
1631 % o exception: return any errors or warnings in this structure.
1635 static inline MagickBooleanType IsPoint(const char *point)
1643 value=StringToDouble(point,&p);
1644 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1647 static inline void TracePoint(PrimitiveInfo *primitive_info,
1648 const PointInfo point)
1650 primitive_info->coordinates=1;
1651 primitive_info->point=point;
1654 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1655 ExceptionInfo *exception)
1657 #define RenderImageTag "Render/Image"
1664 key[2*MaxTextExtent],
1665 keyword[MaxTextExtent],
1666 geometry[MaxTextExtent],
1667 name[MaxTextExtent],
1668 pattern[MaxTextExtent],
1721 Ensure the annotation info is valid.
1723 assert(image != (Image *) NULL);
1724 assert(image->signature == MagickSignature);
1725 if (image->debug != MagickFalse)
1726 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1727 assert(draw_info != (DrawInfo *) NULL);
1728 assert(draw_info->signature == MagickSignature);
1729 if (image->debug != MagickFalse)
1730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1731 if ((draw_info->primitive == (char *) NULL) ||
1732 (*draw_info->primitive == '\0'))
1733 return(MagickFalse);
1734 if (image->debug != MagickFalse)
1735 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1736 if (*draw_info->primitive != '@')
1737 primitive=AcquireString(draw_info->primitive);
1739 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1740 if (primitive == (char *) NULL)
1741 return(MagickFalse);
1742 primitive_extent=(double) strlen(primitive);
1743 (void) SetImageArtifact(image,"MVG",primitive);
1746 Allocate primitive info memory.
1748 graphic_context=(DrawInfo **) AcquireMagickMemory(
1749 sizeof(*graphic_context));
1750 if (graphic_context == (DrawInfo **) NULL)
1752 primitive=DestroyString(primitive);
1753 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1757 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1758 sizeof(*primitive_info));
1759 if (primitive_info == (PrimitiveInfo *) NULL)
1761 primitive=DestroyString(primitive);
1762 for ( ; n >= 0; n--)
1763 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1764 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1765 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1768 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1769 graphic_context[n]->viewbox=image->page;
1770 if ((image->page.width == 0) || (image->page.height == 0))
1772 graphic_context[n]->viewbox.width=image->columns;
1773 graphic_context[n]->viewbox.height=image->rows;
1775 token=AcquireString(primitive);
1776 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1778 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1779 return(MagickFalse);
1781 for (q=primitive; *q != '\0'; )
1784 Interpret graphic primitive.
1786 GetMagickToken(q,&q,keyword);
1787 if (*keyword == '\0')
1789 if (*keyword == '#')
1794 while ((*q != '\n') && (*q != '\0'))
1798 p=q-strlen(keyword)-1;
1799 primitive_type=UndefinedPrimitive;
1800 current=graphic_context[n]->affine;
1801 GetAffineMatrix(&affine);
1809 if (LocaleCompare("affine",keyword) == 0)
1811 GetMagickToken(q,&q,token);
1812 affine.sx=StringToDouble(token,(char **) NULL);
1813 GetMagickToken(q,&q,token);
1815 GetMagickToken(q,&q,token);
1816 affine.rx=StringToDouble(token,(char **) NULL);
1817 GetMagickToken(q,&q,token);
1819 GetMagickToken(q,&q,token);
1820 affine.ry=StringToDouble(token,(char **) NULL);
1821 GetMagickToken(q,&q,token);
1823 GetMagickToken(q,&q,token);
1824 affine.sy=StringToDouble(token,(char **) NULL);
1825 GetMagickToken(q,&q,token);
1827 GetMagickToken(q,&q,token);
1828 affine.tx=StringToDouble(token,(char **) NULL);
1829 GetMagickToken(q,&q,token);
1831 GetMagickToken(q,&q,token);
1832 affine.ty=StringToDouble(token,(char **) NULL);
1835 if (LocaleCompare("alpha",keyword) == 0)
1837 primitive_type=AlphaPrimitive;
1840 if (LocaleCompare("arc",keyword) == 0)
1842 primitive_type=ArcPrimitive;
1851 if (LocaleCompare("bezier",keyword) == 0)
1853 primitive_type=BezierPrimitive;
1856 if (LocaleCompare("border-color",keyword) == 0)
1858 GetMagickToken(q,&q,token);
1859 (void) QueryColorCompliance(token,AllCompliance,
1860 &graphic_context[n]->border_color,exception);
1869 if (LocaleCompare("clip-path",keyword) == 0)
1874 GetMagickToken(q,&q,token);
1875 (void) CloneString(&graphic_context[n]->clip_mask,token);
1876 (void) DrawClipPath(image,graphic_context[n],
1877 graphic_context[n]->clip_mask,exception);
1880 if (LocaleCompare("clip-rule",keyword) == 0)
1885 GetMagickToken(q,&q,token);
1886 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1888 if (fill_rule == -1)
1891 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1894 if (LocaleCompare("clip-units",keyword) == 0)
1899 GetMagickToken(q,&q,token);
1900 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1902 if (clip_units == -1)
1907 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1908 if (clip_units == ObjectBoundingBox)
1910 GetAffineMatrix(¤t);
1911 affine.sx=draw_info->bounds.x2;
1912 affine.sy=draw_info->bounds.y2;
1913 affine.tx=draw_info->bounds.x1;
1914 affine.ty=draw_info->bounds.y1;
1919 if (LocaleCompare("circle",keyword) == 0)
1921 primitive_type=CirclePrimitive;
1924 if (LocaleCompare("color",keyword) == 0)
1926 primitive_type=ColorPrimitive;
1935 if (LocaleCompare("decorate",keyword) == 0)
1940 GetMagickToken(q,&q,token);
1941 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1946 graphic_context[n]->decorate=(DecorationType) decorate;
1949 if (LocaleCompare("direction",keyword) == 0)
1954 GetMagickToken(q,&q,token);
1955 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1957 if (direction == -1)
1960 graphic_context[n]->direction=(DirectionType) direction;
1969 if (LocaleCompare("ellipse",keyword) == 0)
1971 primitive_type=EllipsePrimitive;
1974 if (LocaleCompare("encoding",keyword) == 0)
1976 GetMagickToken(q,&q,token);
1977 (void) CloneString(&graphic_context[n]->encoding,token);
1986 if (LocaleCompare("fill",keyword) == 0)
1988 GetMagickToken(q,&q,token);
1989 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1990 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1991 (void) DrawPatternPath(image,draw_info,token,
1992 &graphic_context[n]->fill_pattern,exception);
1995 status&=QueryColorCompliance(token,AllCompliance,
1996 &graphic_context[n]->fill,exception);
1997 if (status == MagickFalse)
2002 pattern_info=AcquireImageInfo();
2003 (void) CopyMagickString(pattern_info->filename,token,
2005 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2007 CatchException(exception);
2008 pattern_info=DestroyImageInfo(pattern_info);
2013 if (LocaleCompare("fill-opacity",keyword) == 0)
2015 GetMagickToken(q,&q,token);
2016 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2017 graphic_context[n]->fill.alpha=(double) QuantumRange*
2018 factor*StringToDouble(token,(char **) NULL);
2021 if (LocaleCompare("fill-rule",keyword) == 0)
2026 GetMagickToken(q,&q,token);
2027 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2029 if (fill_rule == -1)
2032 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2035 if (LocaleCompare("font",keyword) == 0)
2037 GetMagickToken(q,&q,token);
2038 (void) CloneString(&graphic_context[n]->font,token);
2039 if (LocaleCompare("none",token) == 0)
2040 graphic_context[n]->font=(char *)
2041 RelinquishMagickMemory(graphic_context[n]->font);
2044 if (LocaleCompare("font-family",keyword) == 0)
2046 GetMagickToken(q,&q,token);
2047 (void) CloneString(&graphic_context[n]->family,token);
2050 if (LocaleCompare("font-size",keyword) == 0)
2052 GetMagickToken(q,&q,token);
2053 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2056 if (LocaleCompare("font-stretch",keyword) == 0)
2061 GetMagickToken(q,&q,token);
2062 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2066 graphic_context[n]->stretch=(StretchType) stretch;
2069 if (LocaleCompare("font-style",keyword) == 0)
2074 GetMagickToken(q,&q,token);
2075 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2079 graphic_context[n]->style=(StyleType) style;
2082 if (LocaleCompare("font-weight",keyword) == 0)
2084 GetMagickToken(q,&q,token);
2085 graphic_context[n]->weight=StringToUnsignedLong(token);
2086 if (LocaleCompare(token,"all") == 0)
2087 graphic_context[n]->weight=0;
2088 if (LocaleCompare(token,"bold") == 0)
2089 graphic_context[n]->weight=700;
2090 if (LocaleCompare(token,"bolder") == 0)
2091 if (graphic_context[n]->weight <= 800)
2092 graphic_context[n]->weight+=100;
2093 if (LocaleCompare(token,"lighter") == 0)
2094 if (graphic_context[n]->weight >= 100)
2095 graphic_context[n]->weight-=100;
2096 if (LocaleCompare(token,"normal") == 0)
2097 graphic_context[n]->weight=400;
2106 if (LocaleCompare("gradient-units",keyword) == 0)
2108 GetMagickToken(q,&q,token);
2111 if (LocaleCompare("gravity",keyword) == 0)
2116 GetMagickToken(q,&q,token);
2117 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2121 graphic_context[n]->gravity=(GravityType) gravity;
2130 if (LocaleCompare("image",keyword) == 0)
2135 primitive_type=ImagePrimitive;
2136 GetMagickToken(q,&q,token);
2137 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2141 graphic_context[n]->compose=(CompositeOperator) compose;
2144 if (LocaleCompare("interline-spacing",keyword) == 0)
2146 GetMagickToken(q,&q,token);
2147 graphic_context[n]->interline_spacing=StringToDouble(token,
2151 if (LocaleCompare("interword-spacing",keyword) == 0)
2153 GetMagickToken(q,&q,token);
2154 graphic_context[n]->interword_spacing=StringToDouble(token,
2164 if (LocaleCompare("kerning",keyword) == 0)
2166 GetMagickToken(q,&q,token);
2167 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2176 if (LocaleCompare("line",keyword) == 0)
2177 primitive_type=LinePrimitive;
2185 if (LocaleCompare("offset",keyword) == 0)
2187 GetMagickToken(q,&q,token);
2190 if (LocaleCompare("opacity",keyword) == 0)
2192 GetMagickToken(q,&q,token);
2193 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2194 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2195 QuantumScale*graphic_context[n]->alpha)*factor*
2196 StringToDouble(token,(char **) NULL))));
2197 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2198 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2207 if (LocaleCompare("path",keyword) == 0)
2209 primitive_type=PathPrimitive;
2212 if (LocaleCompare("point",keyword) == 0)
2214 primitive_type=PointPrimitive;
2217 if (LocaleCompare("polyline",keyword) == 0)
2219 primitive_type=PolylinePrimitive;
2222 if (LocaleCompare("polygon",keyword) == 0)
2224 primitive_type=PolygonPrimitive;
2227 if (LocaleCompare("pop",keyword) == 0)
2229 GetMagickToken(q,&q,token);
2230 if (LocaleCompare("clip-path",token) == 0)
2232 if (LocaleCompare("defs",token) == 0)
2234 if (LocaleCompare("gradient",token) == 0)
2236 if (LocaleCompare("graphic-context",token) == 0)
2240 (void) ThrowMagickException(exception,GetMagickModule(),
2241 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2245 if (graphic_context[n]->clip_mask != (char *) NULL)
2246 if (LocaleCompare(graphic_context[n]->clip_mask,
2247 graphic_context[n-1]->clip_mask) != 0)
2248 (void) SetImageMask(image,(Image *) NULL,exception);
2249 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2253 if (LocaleCompare("pattern",token) == 0)
2258 if (LocaleCompare("push",keyword) == 0)
2260 GetMagickToken(q,&q,token);
2261 if (LocaleCompare("clip-path",token) == 0)
2264 name[MaxTextExtent];
2266 GetMagickToken(q,&q,token);
2267 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2268 for (p=q; *q != '\0'; )
2270 GetMagickToken(q,&q,token);
2271 if (LocaleCompare(token,"pop") != 0)
2273 GetMagickToken(q,(const char **) NULL,token);
2274 if (LocaleCompare(token,"clip-path") != 0)
2278 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2279 (void) SetImageArtifact(image,name,token);
2280 GetMagickToken(q,&q,token);
2283 if (LocaleCompare("gradient",token) == 0)
2286 key[2*MaxTextExtent],
2287 name[MaxTextExtent],
2288 type[MaxTextExtent];
2293 GetMagickToken(q,&q,token);
2294 (void) CopyMagickString(name,token,MaxTextExtent);
2295 GetMagickToken(q,&q,token);
2296 (void) CopyMagickString(type,token,MaxTextExtent);
2297 GetMagickToken(q,&q,token);
2298 segment.x1=StringToDouble(token,(char **) NULL);
2299 GetMagickToken(q,&q,token);
2301 GetMagickToken(q,&q,token);
2302 segment.y1=StringToDouble(token,(char **) NULL);
2303 GetMagickToken(q,&q,token);
2305 GetMagickToken(q,&q,token);
2306 segment.x2=StringToDouble(token,(char **) NULL);
2307 GetMagickToken(q,&q,token);
2309 GetMagickToken(q,&q,token);
2310 segment.y2=StringToDouble(token,(char **) NULL);
2311 if (LocaleCompare(type,"radial") == 0)
2313 GetMagickToken(q,&q,token);
2315 GetMagickToken(q,&q,token);
2317 for (p=q; *q != '\0'; )
2319 GetMagickToken(q,&q,token);
2320 if (LocaleCompare(token,"pop") != 0)
2322 GetMagickToken(q,(const char **) NULL,token);
2323 if (LocaleCompare(token,"gradient") != 0)
2327 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2328 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2329 graphic_context[n]->affine.ry*segment.y1+
2330 graphic_context[n]->affine.tx;
2331 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2332 graphic_context[n]->affine.sy*segment.y1+
2333 graphic_context[n]->affine.ty;
2334 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2335 graphic_context[n]->affine.ry*segment.y2+
2336 graphic_context[n]->affine.tx;
2337 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2338 graphic_context[n]->affine.sy*segment.y2+
2339 graphic_context[n]->affine.ty;
2340 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2341 (void) SetImageArtifact(image,key,token);
2342 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2343 (void) FormatLocaleString(geometry,MaxTextExtent,
2344 "%gx%g%+.15g%+.15g",
2345 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2346 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2347 bounds.x1,bounds.y1);
2348 (void) SetImageArtifact(image,key,geometry);
2349 GetMagickToken(q,&q,token);
2352 if (LocaleCompare("pattern",token) == 0)
2357 GetMagickToken(q,&q,token);
2358 (void) CopyMagickString(name,token,MaxTextExtent);
2359 GetMagickToken(q,&q,token);
2360 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2362 GetMagickToken(q,&q,token);
2364 GetMagickToken(q,&q,token);
2365 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2367 GetMagickToken(q,&q,token);
2369 GetMagickToken(q,&q,token);
2370 bounds.width=(size_t) floor(StringToDouble(token,
2371 (char **) NULL)+0.5);
2372 GetMagickToken(q,&q,token);
2374 GetMagickToken(q,&q,token);
2375 bounds.height=(size_t) floor(StringToDouble(token,
2376 (char **) NULL)+0.5);
2377 for (p=q; *q != '\0'; )
2379 GetMagickToken(q,&q,token);
2380 if (LocaleCompare(token,"pop") != 0)
2382 GetMagickToken(q,(const char **) NULL,token);
2383 if (LocaleCompare(token,"pattern") != 0)
2387 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2388 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2389 (void) SetImageArtifact(image,key,token);
2390 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2391 (void) FormatLocaleString(geometry,MaxTextExtent,
2392 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2393 bounds.height,(double) bounds.x,(double) bounds.y);
2394 (void) SetImageArtifact(image,key,geometry);
2395 GetMagickToken(q,&q,token);
2398 if (LocaleCompare("graphic-context",token) == 0)
2401 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2402 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2403 if (graphic_context == (DrawInfo **) NULL)
2405 (void) ThrowMagickException(exception,GetMagickModule(),
2406 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2410 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2411 graphic_context[n-1]);
2414 if (LocaleCompare("defs",token) == 0)
2425 if (LocaleCompare("rectangle",keyword) == 0)
2427 primitive_type=RectanglePrimitive;
2430 if (LocaleCompare("rotate",keyword) == 0)
2432 GetMagickToken(q,&q,token);
2433 angle=StringToDouble(token,(char **) NULL);
2434 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2435 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2436 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2437 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2440 if (LocaleCompare("roundRectangle",keyword) == 0)
2442 primitive_type=RoundRectanglePrimitive;
2451 if (LocaleCompare("scale",keyword) == 0)
2453 GetMagickToken(q,&q,token);
2454 affine.sx=StringToDouble(token,(char **) NULL);
2455 GetMagickToken(q,&q,token);
2457 GetMagickToken(q,&q,token);
2458 affine.sy=StringToDouble(token,(char **) NULL);
2461 if (LocaleCompare("skewX",keyword) == 0)
2463 GetMagickToken(q,&q,token);
2464 angle=StringToDouble(token,(char **) NULL);
2465 affine.ry=sin(DegreesToRadians(angle));
2468 if (LocaleCompare("skewY",keyword) == 0)
2470 GetMagickToken(q,&q,token);
2471 angle=StringToDouble(token,(char **) NULL);
2472 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2475 if (LocaleCompare("stop-color",keyword) == 0)
2480 GetMagickToken(q,&q,token);
2481 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2483 (void) GradientImage(image,LinearGradient,ReflectSpread,
2484 &start_color,&stop_color,exception);
2485 start_color=stop_color;
2486 GetMagickToken(q,&q,token);
2489 if (LocaleCompare("stroke",keyword) == 0)
2491 GetMagickToken(q,&q,token);
2492 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2493 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2494 (void) DrawPatternPath(image,draw_info,token,
2495 &graphic_context[n]->stroke_pattern,exception);
2498 status&=QueryColorCompliance(token,AllCompliance,
2499 &graphic_context[n]->stroke,exception);
2500 if (status == MagickFalse)
2505 pattern_info=AcquireImageInfo();
2506 (void) CopyMagickString(pattern_info->filename,token,
2508 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2510 CatchException(exception);
2511 pattern_info=DestroyImageInfo(pattern_info);
2516 if (LocaleCompare("stroke-antialias",keyword) == 0)
2518 GetMagickToken(q,&q,token);
2519 graphic_context[n]->stroke_antialias=
2520 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2523 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2525 if (graphic_context[n]->dash_pattern != (double *) NULL)
2526 graphic_context[n]->dash_pattern=(double *)
2527 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2528 if (IsPoint(q) != MagickFalse)
2534 GetMagickToken(p,&p,token);
2536 GetMagickToken(p,&p,token);
2537 for (x=0; IsPoint(token) != MagickFalse; x++)
2539 GetMagickToken(p,&p,token);
2541 GetMagickToken(p,&p,token);
2543 graphic_context[n]->dash_pattern=(double *)
2544 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2545 sizeof(*graphic_context[n]->dash_pattern));
2546 if (graphic_context[n]->dash_pattern == (double *) NULL)
2548 (void) ThrowMagickException(exception,GetMagickModule(),
2549 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2553 for (j=0; j < x; j++)
2555 GetMagickToken(q,&q,token);
2557 GetMagickToken(q,&q,token);
2558 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2561 if ((x & 0x01) != 0)
2562 for ( ; j < (2*x); j++)
2563 graphic_context[n]->dash_pattern[j]=
2564 graphic_context[n]->dash_pattern[j-x];
2565 graphic_context[n]->dash_pattern[j]=0.0;
2568 GetMagickToken(q,&q,token);
2571 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2573 GetMagickToken(q,&q,token);
2574 graphic_context[n]->dash_offset=StringToDouble(token,
2578 if (LocaleCompare("stroke-linecap",keyword) == 0)
2583 GetMagickToken(q,&q,token);
2584 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2588 graphic_context[n]->linecap=(LineCap) linecap;
2591 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2596 GetMagickToken(q,&q,token);
2597 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2602 graphic_context[n]->linejoin=(LineJoin) linejoin;
2605 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2607 GetMagickToken(q,&q,token);
2608 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2611 if (LocaleCompare("stroke-opacity",keyword) == 0)
2613 GetMagickToken(q,&q,token);
2614 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2615 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2616 factor*StringToDouble(token,(char **) NULL);
2619 if (LocaleCompare("stroke-width",keyword) == 0)
2621 GetMagickToken(q,&q,token);
2622 graphic_context[n]->stroke_width=StringToDouble(token,
2632 if (LocaleCompare("text",keyword) == 0)
2634 primitive_type=TextPrimitive;
2637 if (LocaleCompare("text-align",keyword) == 0)
2642 GetMagickToken(q,&q,token);
2643 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2647 graphic_context[n]->align=(AlignType) align;
2650 if (LocaleCompare("text-anchor",keyword) == 0)
2655 GetMagickToken(q,&q,token);
2656 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2660 graphic_context[n]->align=(AlignType) align;
2663 if (LocaleCompare("text-antialias",keyword) == 0)
2665 GetMagickToken(q,&q,token);
2666 graphic_context[n]->text_antialias=
2667 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2670 if (LocaleCompare("text-undercolor",keyword) == 0)
2672 GetMagickToken(q,&q,token);
2673 (void) QueryColorCompliance(token,AllCompliance,
2674 &graphic_context[n]->undercolor,exception);
2677 if (LocaleCompare("translate",keyword) == 0)
2679 GetMagickToken(q,&q,token);
2680 affine.tx=StringToDouble(token,(char **) NULL);
2681 GetMagickToken(q,&q,token);
2683 GetMagickToken(q,&q,token);
2684 affine.ty=StringToDouble(token,(char **) NULL);
2693 if (LocaleCompare("viewbox",keyword) == 0)
2695 GetMagickToken(q,&q,token);
2696 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2697 (char **) NULL)-0.5);
2698 GetMagickToken(q,&q,token);
2700 GetMagickToken(q,&q,token);
2701 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2702 (char **) NULL)-0.5);
2703 GetMagickToken(q,&q,token);
2705 GetMagickToken(q,&q,token);
2706 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2707 token,(char **) NULL)+0.5);
2708 GetMagickToken(q,&q,token);
2710 GetMagickToken(q,&q,token);
2711 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2712 token,(char **) NULL)+0.5);
2724 if (status == MagickFalse)
2726 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2727 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2729 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2730 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2731 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2732 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2733 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2735 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2738 if (primitive_type == UndefinedPrimitive)
2740 if (image->debug != MagickFalse)
2741 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2746 Parse the primitive attributes.
2750 primitive_info[0].point.x=0.0;
2751 primitive_info[0].point.y=0.0;
2752 for (x=0; *q != '\0'; x++)
2757 if (IsPoint(q) == MagickFalse)
2759 GetMagickToken(q,&q,token);
2760 point.x=StringToDouble(token,(char **) NULL);
2761 GetMagickToken(q,&q,token);
2763 GetMagickToken(q,&q,token);
2764 point.y=StringToDouble(token,(char **) NULL);
2765 GetMagickToken(q,(const char **) NULL,token);
2767 GetMagickToken(q,&q,token);
2768 primitive_info[i].primitive=primitive_type;
2769 primitive_info[i].point=point;
2770 primitive_info[i].coordinates=0;
2771 primitive_info[i].method=FloodfillMethod;
2773 if (i < (ssize_t) number_points)
2776 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2777 (size_t) number_points,sizeof(*primitive_info));
2778 if (primitive_info == (PrimitiveInfo *) NULL)
2780 (void) ThrowMagickException(exception,GetMagickModule(),
2781 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2785 primitive_info[j].primitive=primitive_type;
2786 primitive_info[j].coordinates=(size_t) x;
2787 primitive_info[j].method=FloodfillMethod;
2788 primitive_info[j].text=(char *) NULL;
2790 Circumscribe primitive within a circle.
2792 bounds.x1=primitive_info[j].point.x;
2793 bounds.y1=primitive_info[j].point.y;
2794 bounds.x2=primitive_info[j].point.x;
2795 bounds.y2=primitive_info[j].point.y;
2796 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2798 point=primitive_info[j+k].point;
2799 if (point.x < bounds.x1)
2801 if (point.y < bounds.y1)
2803 if (point.x > bounds.x2)
2805 if (point.y > bounds.y2)
2809 Speculate how many points our primitive might consume.
2811 length=primitive_info[j].coordinates;
2812 switch (primitive_type)
2814 case RectanglePrimitive:
2819 case RoundRectanglePrimitive:
2826 alpha=bounds.x2-bounds.x1;
2827 beta=bounds.y2-bounds.y1;
2828 radius=hypot((double) alpha,(double) beta);
2830 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2833 case BezierPrimitive:
2835 if (primitive_info[j].coordinates > 107)
2836 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2837 "TooManyBezierCoordinates","`%s'",token);
2838 length=BezierQuantum*primitive_info[j].coordinates;
2847 GetMagickToken(q,&q,token);
2850 for (s=token; *s != '\0'; s=t)
2855 value=StringToDouble(s,&t);
2864 length=length*BezierQuantum/2;
2867 case CirclePrimitive:
2869 case EllipsePrimitive:
2876 alpha=bounds.x2-bounds.x1;
2877 beta=bounds.y2-bounds.y1;
2878 radius=hypot((double) alpha,(double) beta);
2879 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2885 if ((size_t) (i+length) >= number_points)
2888 Resize based on speculative points required by primitive.
2890 number_points+=length+1;
2891 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2892 (size_t) number_points,sizeof(*primitive_info));
2893 if (primitive_info == (PrimitiveInfo *) NULL)
2895 (void) ThrowMagickException(exception,GetMagickModule(),
2896 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2901 switch (primitive_type)
2903 case PointPrimitive:
2906 if (primitive_info[j].coordinates != 1)
2911 TracePoint(primitive_info+j,primitive_info[j].point);
2912 i=(ssize_t) (j+primitive_info[j].coordinates);
2917 if (primitive_info[j].coordinates != 2)
2922 TraceLine(primitive_info+j,primitive_info[j].point,
2923 primitive_info[j+1].point);
2924 i=(ssize_t) (j+primitive_info[j].coordinates);
2927 case RectanglePrimitive:
2929 if (primitive_info[j].coordinates != 2)
2934 TraceRectangle(primitive_info+j,primitive_info[j].point,
2935 primitive_info[j+1].point);
2936 i=(ssize_t) (j+primitive_info[j].coordinates);
2939 case RoundRectanglePrimitive:
2941 if (primitive_info[j].coordinates != 3)
2946 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2947 primitive_info[j+1].point,primitive_info[j+2].point);
2948 i=(ssize_t) (j+primitive_info[j].coordinates);
2953 if (primitive_info[j].coordinates != 3)
2955 primitive_type=UndefinedPrimitive;
2958 TraceArc(primitive_info+j,primitive_info[j].point,
2959 primitive_info[j+1].point,primitive_info[j+2].point);
2960 i=(ssize_t) (j+primitive_info[j].coordinates);
2963 case EllipsePrimitive:
2965 if (primitive_info[j].coordinates != 3)
2970 TraceEllipse(primitive_info+j,primitive_info[j].point,
2971 primitive_info[j+1].point,primitive_info[j+2].point);
2972 i=(ssize_t) (j+primitive_info[j].coordinates);
2975 case CirclePrimitive:
2977 if (primitive_info[j].coordinates != 2)
2982 TraceCircle(primitive_info+j,primitive_info[j].point,
2983 primitive_info[j+1].point);
2984 i=(ssize_t) (j+primitive_info[j].coordinates);
2987 case PolylinePrimitive:
2989 case PolygonPrimitive:
2991 primitive_info[i]=primitive_info[j];
2992 primitive_info[i].coordinates=0;
2993 primitive_info[j].coordinates++;
2997 case BezierPrimitive:
2999 if (primitive_info[j].coordinates < 3)
3004 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3005 i=(ssize_t) (j+primitive_info[j].coordinates);
3010 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3013 case AlphaPrimitive:
3014 case ColorPrimitive:
3019 if (primitive_info[j].coordinates != 1)
3024 GetMagickToken(q,&q,token);
3025 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3029 primitive_info[j].method=(PaintMethod) method;
3034 if (primitive_info[j].coordinates != 1)
3040 GetMagickToken(q,&q,token);
3041 primitive_info[j].text=AcquireString(token);
3044 case ImagePrimitive:
3046 if (primitive_info[j].coordinates != 2)
3051 GetMagickToken(q,&q,token);
3052 primitive_info[j].text=AcquireString(token);
3056 if (primitive_info == (PrimitiveInfo *) NULL)
3058 if (image->debug != MagickFalse)
3059 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3060 if (status == MagickFalse)
3062 primitive_info[i].primitive=UndefinedPrimitive;
3068 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3070 point=primitive_info[i].point;
3071 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3072 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3073 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3074 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3075 point=primitive_info[i].point;
3076 if (point.x < graphic_context[n]->bounds.x1)
3077 graphic_context[n]->bounds.x1=point.x;
3078 if (point.y < graphic_context[n]->bounds.y1)
3079 graphic_context[n]->bounds.y1=point.y;
3080 if (point.x > graphic_context[n]->bounds.x2)
3081 graphic_context[n]->bounds.x2=point.x;
3082 if (point.y > graphic_context[n]->bounds.y2)
3083 graphic_context[n]->bounds.y2=point.y;
3084 if (primitive_info[i].primitive == ImagePrimitive)
3086 if (i >= (ssize_t) number_points)
3087 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3089 if (graphic_context[n]->render != MagickFalse)
3091 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3092 (LocaleCompare(graphic_context[n]->clip_mask,
3093 graphic_context[n-1]->clip_mask) != 0))
3094 status&=DrawClipPath(image,graphic_context[n],
3095 graphic_context[n]->clip_mask,exception);
3096 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3099 if (primitive_info->text != (char *) NULL)
3100 primitive_info->text=(char *) RelinquishMagickMemory(
3101 primitive_info->text);
3102 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3104 if (proceed == MagickFalse)
3109 if (image->debug != MagickFalse)
3110 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3112 Relinquish resources.
3114 token=DestroyString(token);
3115 if (primitive_info != (PrimitiveInfo *) NULL)
3116 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3117 primitive=DestroyString(primitive);
3118 for ( ; n >= 0; n--)
3119 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3120 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3121 if (status == MagickFalse)
3122 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3124 return(status != 0 ? MagickTrue : MagickFalse);
3128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3132 % D r a w G r a d i e n t I m a g e %
3136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3138 % DrawGradientImage() draws a linear gradient on the image.
3140 % The format of the DrawGradientImage method is:
3142 % MagickBooleanType DrawGradientImage(Image *image,
3143 % const DrawInfo *draw_info,ExceptionInfo *exception)
3145 % A description of each parameter follows:
3147 % o image: the image.
3149 % o draw_info: the draw info.
3151 % o exception: return any errors or warnings in this structure.
3155 static inline double GetStopColorOffset(const GradientInfo *gradient,
3156 const ssize_t x,const ssize_t y)
3158 switch (gradient->type)
3160 case UndefinedGradient:
3161 case LinearGradient:
3176 gradient_vector=(&gradient->gradient_vector);
3177 p.x=gradient_vector->x2-gradient_vector->x1;
3178 p.y=gradient_vector->y2-gradient_vector->y1;
3179 q.x=(double) x-gradient_vector->x1;
3180 q.y=(double) y-gradient_vector->y1;
3181 length=sqrt(q.x*q.x+q.y*q.y);
3182 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3183 gamma=PerceptibleReciprocal(gamma);
3184 scale=p.x*q.x+p.y*q.y;
3185 offset=gamma*scale*length;
3188 case RadialGradient:
3197 v.x=(double) x-gradient->center.x;
3198 v.y=(double) y-gradient->center.y;
3199 length=sqrt(v.x*v.x+v.y*v.y);
3200 if (gradient->spread == RepeatSpread)
3202 offset=length/gradient->radius;
3209 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3210 const DrawInfo *draw_info,ExceptionInfo *exception)
3240 Draw linear or radial gradient on image.
3242 assert(image != (Image *) NULL);
3243 assert(image->signature == MagickSignature);
3244 if (image->debug != MagickFalse)
3245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3246 assert(draw_info != (const DrawInfo *) NULL);
3247 gradient=(&draw_info->gradient);
3248 gradient_vector=(&gradient->gradient_vector);
3249 point.x=gradient_vector->x2-gradient_vector->x1;
3250 point.y=gradient_vector->y2-gradient_vector->y1;
3251 length=sqrt(point.x*point.x+point.y*point.y);
3252 bounding_box=gradient->bounding_box;
3254 GetPixelInfo(image,&zero);
3255 image_view=AcquireAuthenticCacheView(image,exception);
3256 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3257 #pragma omp parallel for schedule(static,4) shared(status) \
3258 magick_threads(image,image,1,1)
3260 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3280 if (status == MagickFalse)
3282 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3283 if (q == (Quantum *) NULL)
3290 offset=GetStopColorOffset(gradient,0,y);
3291 if (gradient->type != RadialGradient)
3293 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3295 GetPixelInfoPixel(image,q,&pixel);
3296 switch (gradient->spread)
3298 case UndefinedSpread:
3301 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3302 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3304 offset=GetStopColorOffset(gradient,x,y);
3305 if (gradient->type != RadialGradient)
3308 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3309 if (offset < gradient->stops[i].offset)
3311 if ((offset < 0.0) || (i == 0))
3312 composite=gradient->stops[0].color;
3314 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3315 composite=gradient->stops[gradient->number_stops-1].color;
3320 alpha=(offset-gradient->stops[i].offset)/
3321 (gradient->stops[j].offset-gradient->stops[i].offset);
3322 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3323 &gradient->stops[j].color,alpha,&composite);
3329 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3330 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3332 offset=GetStopColorOffset(gradient,x,y);
3333 if (gradient->type != RadialGradient)
3338 if ((ssize_t) fmod(offset,2.0) == 0)
3339 offset=fmod(offset,1.0);
3341 offset=1.0-fmod(offset,1.0);
3342 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3343 if (offset < gradient->stops[i].offset)
3346 composite=gradient->stops[0].color;
3348 if (i == (ssize_t) gradient->number_stops)
3349 composite=gradient->stops[gradient->number_stops-1].color;
3354 alpha=(offset-gradient->stops[i].offset)/
3355 (gradient->stops[j].offset-gradient->stops[i].offset);
3356 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3357 &gradient->stops[j].color,alpha,&composite);
3369 antialias=MagickFalse;
3371 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3372 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3374 offset=GetStopColorOffset(gradient,x,y);
3375 if (gradient->type == LinearGradient)
3377 repeat=fmod(offset,length);
3379 repeat=length-fmod(-repeat,length);
3381 repeat=fmod(offset,length);
3382 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3383 MagickTrue : MagickFalse;
3384 offset=repeat/length;
3388 repeat=fmod(offset,gradient->radius);
3390 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3392 repeat=fmod(offset,gradient->radius);
3393 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3395 offset=repeat/gradient->radius;
3398 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3399 if (offset < gradient->stops[i].offset)
3402 composite=gradient->stops[0].color;
3404 if (i == (ssize_t) gradient->number_stops)
3405 composite=gradient->stops[gradient->number_stops-1].color;
3410 alpha=(offset-gradient->stops[i].offset)/
3411 (gradient->stops[j].offset-gradient->stops[i].offset);
3412 if (antialias != MagickFalse)
3414 if (gradient->type == LinearGradient)
3415 alpha=length-repeat;
3417 alpha=gradient->radius-repeat;
3419 j=(ssize_t) gradient->number_stops-1L;
3421 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3422 &gradient->stops[j].color,alpha,&composite);
3427 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3429 SetPixelViaPixelInfo(image,&pixel,q);
3430 q+=GetPixelChannels(image);
3432 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3435 image_view=DestroyCacheView(image_view);
3440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3444 % D r a w P a t t e r n P a t h %
3448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3450 % DrawPatternPath() draws a pattern.
3452 % The format of the DrawPatternPath method is:
3454 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3455 % const char *name,Image **pattern,ExceptionInfo *exception)
3457 % A description of each parameter follows:
3459 % o image: the image.
3461 % o draw_info: the draw info.
3463 % o name: the pattern name.
3465 % o image: the image.
3467 % o exception: return any errors or warnings in this structure.
3470 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3471 const DrawInfo *draw_info,const char *name,Image **pattern,
3472 ExceptionInfo *exception)
3475 property[MaxTextExtent];
3490 assert(image != (Image *) NULL);
3491 assert(image->signature == MagickSignature);
3492 if (image->debug != MagickFalse)
3493 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3494 assert(draw_info != (const DrawInfo *) NULL);
3495 assert(name != (const char *) NULL);
3496 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3497 path=GetImageArtifact(image,property);
3498 if (path == (const char *) NULL)
3499 return(MagickFalse);
3500 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3501 geometry=GetImageArtifact(image,property);
3502 if (geometry == (const char *) NULL)
3503 return(MagickFalse);
3504 if ((*pattern) != (Image *) NULL)
3505 *pattern=DestroyImage(*pattern);
3506 image_info=AcquireImageInfo();
3507 image_info->size=AcquireString(geometry);
3508 *pattern=AcquireImage(image_info,exception);
3509 image_info=DestroyImageInfo(image_info);
3510 (void) QueryColorCompliance("#000000ff",AllCompliance,
3511 &(*pattern)->background_color,exception);
3512 (void) SetImageBackgroundColor(*pattern,exception);
3513 if (image->debug != MagickFalse)
3514 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3515 "begin pattern-path %s %s",name,geometry);
3516 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3517 clone_info->fill_pattern=NewImageList();
3518 clone_info->stroke_pattern=NewImageList();
3519 (void) CloneString(&clone_info->primitive,path);
3520 status=DrawImage(*pattern,clone_info,exception);
3521 clone_info=DestroyDrawInfo(clone_info);
3522 if (image->debug != MagickFalse)
3523 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3532 + D r a w P o l y g o n P r i m i t i v e %
3536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3538 % DrawPolygonPrimitive() draws a polygon on the image.
3540 % The format of the DrawPolygonPrimitive method is:
3542 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3543 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3544 % ExceptionInfo *exception)
3546 % A description of each parameter follows:
3548 % o image: the image.
3550 % o draw_info: the draw info.
3552 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3554 % o exception: return any errors or warnings in this structure.
3558 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3563 assert(polygon_info != (PolygonInfo **) NULL);
3564 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3565 if (polygon_info[i] != (PolygonInfo *) NULL)
3566 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3567 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3568 return(polygon_info);
3571 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3572 const PrimitiveInfo *primitive_info)
3575 *restrict path_info;
3586 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3587 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3588 sizeof(*polygon_info));
3589 if (polygon_info == (PolygonInfo **) NULL)
3590 return((PolygonInfo **) NULL);
3591 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3592 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3593 if (path_info == (PathInfo *) NULL)
3594 return(DestroyPolygonThreadSet(polygon_info));
3595 for (i=0; i < (ssize_t) number_threads; i++)
3597 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3598 if (polygon_info[i] == (PolygonInfo *) NULL)
3599 return(DestroyPolygonThreadSet(polygon_info));
3601 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3602 return(polygon_info);
3605 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3606 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3607 const ssize_t y,double *stroke_alpha)
3618 register const PointInfo
3632 Compute fill & stroke opacity for this (x,y) point.
3636 p=polygon_info->edges;
3637 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3639 if ((double) y <= (p->bounds.y1-mid-0.5))
3641 if ((double) y > (p->bounds.y2+mid+0.5))
3643 (void) DestroyEdge(polygon_info,(size_t) j);
3646 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3647 ((double) x > (p->bounds.x2+mid+0.5)))
3649 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3650 for ( ; i < (ssize_t) p->number_points; i++)
3652 if ((double) y <= (p->points[i-1].y-mid-0.5))
3654 if ((double) y > (p->points[i].y+mid+0.5))
3656 if (p->scanline != (double) y)
3658 p->scanline=(double) y;
3659 p->highwater=(size_t) i;
3662 Compute distance between a point and an edge.
3665 delta.x=(q+1)->x-q->x;
3666 delta.y=(q+1)->y-q->y;
3667 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3670 delta.x=(double) x-q->x;
3671 delta.y=(double) y-q->y;
3672 distance=delta.x*delta.x+delta.y*delta.y;
3676 alpha=delta.x*delta.x+delta.y*delta.y;
3679 delta.x=(double) x-(q+1)->x;
3680 delta.y=(double) y-(q+1)->y;
3681 distance=delta.x*delta.x+delta.y*delta.y;
3686 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3687 distance=alpha*beta*beta;
3691 Compute stroke & subpath opacity.
3694 if (p->ghostline == MagickFalse)
3697 if ((*stroke_alpha < 1.0) &&
3698 (distance <= ((alpha+0.25)*(alpha+0.25))))
3701 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3706 if (distance != 1.0)
3707 beta=sqrt((double) distance);
3709 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3710 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3714 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3716 if (distance <= 0.0)
3726 if (distance != 1.0)
3727 beta=sqrt(distance);
3730 if (subpath_alpha < (alpha*alpha))
3731 subpath_alpha=alpha*alpha;
3735 Compute fill opacity.
3737 if (fill == MagickFalse)
3739 if (subpath_alpha >= 1.0)
3742 Determine winding number.
3745 p=polygon_info->edges;
3746 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3748 if ((double) y <= p->bounds.y1)
3750 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3752 if ((double) x > p->bounds.x2)
3754 winding_number+=p->direction ? 1 : -1;
3757 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3758 for ( ; i < (ssize_t) p->number_points; i++)
3759 if ((double) y <= p->points[i].y)
3762 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3763 winding_number+=p->direction ? 1 : -1;
3765 if (fill_rule != NonZeroRule)
3767 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3771 if (MagickAbsoluteValue(winding_number) != 0)
3773 return(subpath_alpha);
3776 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3777 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3778 ExceptionInfo *exception)
3791 **restrict polygon_info;
3808 Compute bounding box.
3810 assert(image != (Image *) NULL);
3811 assert(image->signature == MagickSignature);
3812 if (image->debug != MagickFalse)
3813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3814 assert(draw_info != (DrawInfo *) NULL);
3815 assert(draw_info->signature == MagickSignature);
3816 assert(primitive_info != (PrimitiveInfo *) NULL);
3817 if (primitive_info->coordinates == 0)
3819 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3820 if (polygon_info == (PolygonInfo **) NULL)
3821 return(MagickFalse);
3822 DisableMSCWarning(4127)
3824 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3826 if (image->debug != MagickFalse)
3827 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3828 fill=(primitive_info->method == FillToBorderMethod) ||
3829 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3830 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3831 bounds=polygon_info[0]->edges[0].bounds;
3832 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3834 p=polygon_info[0]->edges+i;
3835 if (p->bounds.x1 < bounds.x1)
3836 bounds.x1=p->bounds.x1;
3837 if (p->bounds.y1 < bounds.y1)
3838 bounds.y1=p->bounds.y1;
3839 if (p->bounds.x2 > bounds.x2)
3840 bounds.x2=p->bounds.x2;
3841 if (p->bounds.y2 > bounds.y2)
3842 bounds.y2=p->bounds.y2;
3844 bounds.x1-=(mid+1.0);
3845 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3846 image->columns ? (double) image->columns-1 : bounds.x1;
3847 bounds.y1-=(mid+1.0);
3848 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3849 image->rows ? (double) image->rows-1 : bounds.y1;
3850 bounds.x2+=(mid+1.0);
3851 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3852 image->columns ? (double) image->columns-1 : bounds.x2;
3853 bounds.y2+=(mid+1.0);
3854 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3855 image->rows ? (double) image->rows-1 : bounds.y2;
3857 image_view=AcquireAuthenticCacheView(image,exception);
3858 if (primitive_info->coordinates == 1)
3863 start=(ssize_t) ceil(bounds.y1-0.5);
3864 stop=(ssize_t) floor(bounds.y2+0.5);
3865 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3866 #pragma omp parallel for schedule(static,4) shared(status) \
3867 magick_threads(image,image,1,1)
3869 for (y=start; y <= stop; y++)
3887 if (status == MagickFalse)
3889 start=(ssize_t) ceil(bounds.x1-0.5);
3890 stop=(ssize_t) floor(bounds.x2+0.5);
3892 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3894 if (q == (Quantum *) NULL)
3899 GetPixelInfo(image,&pixel);
3900 for ( ; x <= stop; x++)
3902 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3903 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3905 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3906 SetPixelViaPixelInfo(image,&pixel,q);
3908 q+=GetPixelChannels(image);
3910 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3911 if (sync == MagickFalse)
3914 image_view=DestroyCacheView(image_view);
3915 polygon_info=DestroyPolygonThreadSet(polygon_info);
3916 if (image->debug != MagickFalse)
3917 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3918 " end draw-polygon");
3922 Draw polygon or line.
3924 if (image->alpha_trait == UndefinedPixelTrait)
3925 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3926 start=(ssize_t) ceil(bounds.y1-0.5);
3927 stop=(ssize_t) floor(bounds.y2+0.5);
3928 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3929 #pragma omp parallel for schedule(static,4) shared(status) \
3930 magick_threads(image,image,1,1)
3932 for (y=start; y <= stop; y++)
3935 id = GetOpenMPThreadId();
3955 if (status == MagickFalse)
3957 start=(ssize_t) ceil(bounds.x1-0.5);
3958 stop=(ssize_t) floor(bounds.x2+0.5);
3959 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3961 if (q == (Quantum *) NULL)
3966 for (x=start; x <= stop; x++)
3971 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3973 if (draw_info->stroke_antialias == MagickFalse)
3975 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3976 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3978 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3979 fill_alpha=fill_alpha*fill_color.alpha;
3980 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
3981 GetPixelAlpha(image,q),q);
3982 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3983 stroke_alpha=stroke_alpha*stroke_color.alpha;
3984 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
3985 GetPixelAlpha(image,q),q);
3986 q+=GetPixelChannels(image);
3988 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3991 image_view=DestroyCacheView(image_view);
3992 polygon_info=DestroyPolygonThreadSet(polygon_info);
3993 if (image->debug != MagickFalse)
3994 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4003 % D r a w P r i m i t i v e %
4007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4009 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4011 % The format of the DrawPrimitive method is:
4013 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4014 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4016 % A description of each parameter follows:
4018 % o image: the image.
4020 % o draw_info: the draw info.
4022 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4024 % o exception: return any errors or warnings in this structure.
4028 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4054 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4055 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4056 switch (primitive_info->primitive)
4058 case AlphaPrimitive:
4060 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4061 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4062 methods[primitive_info->method]);
4065 case ColorPrimitive:
4067 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4068 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4069 methods[primitive_info->method]);
4072 case ImagePrimitive:
4074 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4075 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4078 case PointPrimitive:
4080 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4081 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4082 methods[primitive_info->method]);
4087 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4088 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4095 p=primitive_info[0].point;
4098 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4100 point=primitive_info[i].point;
4101 if (coordinates <= 0)
4103 coordinates=(ssize_t) primitive_info[i].coordinates;
4104 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4105 " begin open (%.20g)",(double) coordinates);
4108 point=primitive_info[i].point;
4109 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4110 (fabs(q.y-point.y) >= MagickEpsilon))
4111 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4112 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4114 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4115 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4118 if (coordinates > 0)
4120 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4121 (fabs(p.y-point.y) >= MagickEpsilon))
4122 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4123 (double) coordinates);
4125 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4126 (double) coordinates);
4130 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4131 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4132 ExceptionInfo *exception)
4147 if (image->debug != MagickFalse)
4149 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4150 " begin draw-primitive");
4151 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4152 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4153 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4154 draw_info->affine.tx,draw_info->affine.ty);
4156 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4157 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4158 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4159 (void) SetImageColorspace(image,sRGBColorspace,exception);
4161 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4162 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4163 image_view=AcquireAuthenticCacheView(image,exception);
4164 switch (primitive_info->primitive)
4166 case AlphaPrimitive:
4168 if (image->alpha_trait == UndefinedPixelTrait)
4169 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4170 switch (primitive_info->method)
4181 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4182 if (q == (Quantum *) NULL)
4184 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4185 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4186 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4198 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4200 GetPixelInfo(image,&pixel);
4201 for (y=0; y < (ssize_t) image->rows; y++)
4209 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4211 if (q == (Quantum *) NULL)
4213 for (x=0; x < (ssize_t) image->columns; x++)
4215 GetPixelInfoPixel(image,q,&pixel);
4216 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4218 q+=GetPixelChannels(image);
4221 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4222 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4223 q+=GetPixelChannels(image);
4225 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4226 if (sync == MagickFalse)
4231 case FloodfillMethod:
4232 case FillToBorderMethod:
4240 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4242 if (primitive_info->method == FillToBorderMethod)
4244 target.red=(double) draw_info->border_color.red;
4245 target.green=(double) draw_info->border_color.green;
4246 target.blue=(double) draw_info->border_color.blue;
4248 channel_mask=SetImageChannelMask(image,AlphaChannel);
4249 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4250 primitive_info->method == FloodfillMethod ? MagickFalse :
4251 MagickTrue,exception);
4252 (void) SetImageChannelMask(image,channel_mask);
4263 for (y=0; y < (ssize_t) image->rows; y++)
4271 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4273 if (q == (Quantum *) NULL)
4275 for (x=0; x < (ssize_t) image->columns; x++)
4277 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4278 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4279 q+=GetPixelChannels(image);
4281 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4282 if (sync == MagickFalse)
4290 case ColorPrimitive:
4292 switch (primitive_info->method)
4303 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4304 if (q == (Quantum *) NULL)
4306 GetPixelInfo(image,&pixel);
4307 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4308 SetPixelViaPixelInfo(image,&pixel,q);
4309 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4321 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4323 for (y=0; y < (ssize_t) image->rows; y++)
4328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4330 if (q == (Quantum *) NULL)
4332 for (x=0; x < (ssize_t) image->columns; x++)
4334 GetPixelInfoPixel(image,q,&pixel);
4335 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4337 q+=GetPixelChannels(image);
4340 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4341 SetPixelViaPixelInfo(image,&pixel,q);
4342 q+=GetPixelChannels(image);
4344 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4345 if (sync == MagickFalse)
4350 case FloodfillMethod:
4351 case FillToBorderMethod:
4356 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4358 if (primitive_info->method == FillToBorderMethod)
4360 target.red=(double) draw_info->border_color.red;
4361 target.green=(double) draw_info->border_color.green;
4362 target.blue=(double) draw_info->border_color.blue;
4364 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4365 primitive_info->method == FloodfillMethod ? MagickFalse :
4366 MagickTrue,exception);
4377 GetPixelInfo(image,&pixel);
4378 for (y=0; y < (ssize_t) image->rows; y++)
4386 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4388 if (q == (Quantum *) NULL)
4390 for (x=0; x < (ssize_t) image->columns; x++)
4392 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4393 SetPixelViaPixelInfo(image,&pixel,q);
4394 q+=GetPixelChannels(image);
4396 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4397 if (sync == MagickFalse)
4405 case ImagePrimitive:
4411 composite_geometry[MaxTextExtent];
4426 if (primitive_info->text == (char *) NULL)
4428 clone_info=AcquireImageInfo();
4429 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4430 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4434 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4436 composite_image=ReadImage(clone_info,exception);
4438 clone_info=DestroyImageInfo(clone_info);
4439 if (composite_image == (Image *) NULL)
4441 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4442 NULL,(void *) NULL);
4443 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4444 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4445 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4446 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4449 geometry[MaxTextExtent];
4454 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4455 primitive_info[1].point.x,primitive_info[1].point.y);
4456 composite_image->filter=image->filter;
4457 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4460 if (composite_image->alpha_trait == UndefinedPixelTrait)
4461 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4463 if (draw_info->alpha != OpaqueAlpha)
4464 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4465 SetGeometry(image,&geometry);
4466 image->gravity=draw_info->gravity;
4469 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4470 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4471 composite_image->rows,(double) geometry.x,(double) geometry.y);
4472 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4473 affine=draw_info->affine;
4474 affine.tx=(double) geometry.x;
4475 affine.ty=(double) geometry.y;
4476 composite_image->interpolate=image->interpolate;
4477 if (draw_info->compose == OverCompositeOp)
4478 (void) DrawAffineImage(image,composite_image,&affine,exception);
4480 (void) CompositeImage(image,composite_image,draw_info->compose,
4481 MagickTrue,geometry.x,geometry.y,exception);
4482 composite_image=DestroyImage(composite_image);
4485 case PointPrimitive:
4493 if ((y < 0) || (y >= (ssize_t) image->rows))
4495 if ((x < 0) || (x >= (ssize_t) image->columns))
4497 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4498 if (q == (Quantum *) NULL)
4500 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4501 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4502 (double) GetPixelAlpha(image,q),q);
4503 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4509 geometry[MaxTextExtent];
4514 if (primitive_info->text == (char *) NULL)
4516 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4517 (void) CloneString(&clone_info->text,primitive_info->text);
4518 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4519 primitive_info->point.x,primitive_info->point.y);
4520 (void) CloneString(&clone_info->geometry,geometry);
4521 status&=AnnotateImage(image,clone_info,exception);
4522 clone_info=DestroyDrawInfo(clone_info);
4534 if (IsEventLogging() != MagickFalse)
4535 LogPrimitiveInfo(primitive_info);
4536 scale=ExpandAffine(&draw_info->affine);
4537 if ((draw_info->dash_pattern != (double *) NULL) &&
4538 (draw_info->dash_pattern[0] != 0.0) &&
4539 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4540 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4545 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4546 clone_info->stroke_width=0.0;
4547 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4548 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4550 clone_info=DestroyDrawInfo(clone_info);
4551 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4554 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4556 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4562 Draw strokes while respecting line cap/join attributes.
4564 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4566 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4567 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4568 MagickTrue : MagickFalse;
4569 i=(ssize_t) primitive_info[0].coordinates;
4570 if ((((draw_info->linecap == RoundCap) ||
4571 (closed_path != MagickFalse)) &&
4572 (draw_info->linejoin == RoundJoin)) ||
4573 (primitive_info[i].primitive != UndefinedPrimitive))
4575 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4579 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4580 clone_info->stroke_width=0.0;
4581 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4582 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4584 clone_info=DestroyDrawInfo(clone_info);
4585 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4588 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4592 image_view=DestroyCacheView(image_view);
4593 if (image->debug != MagickFalse)
4594 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4595 return(status != 0 ? MagickTrue : MagickFalse);
4599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4603 + D r a w S t r o k e P o l y g o n %
4607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4609 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4610 % the image while respecting the line cap and join attributes.
4612 % The format of the DrawStrokePolygon method is:
4614 % MagickBooleanType DrawStrokePolygon(Image *image,
4615 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4617 % A description of each parameter follows:
4619 % o image: the image.
4621 % o draw_info: the draw info.
4623 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4628 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4629 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4637 for (i=0; i < 4; i++)
4638 linecap[i]=(*primitive_info);
4639 linecap[0].coordinates=4;
4640 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4641 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4642 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4643 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4644 linecap[4].primitive=UndefinedPrimitive;
4645 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4648 static MagickBooleanType DrawStrokePolygon(Image *image,
4649 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4650 ExceptionInfo *exception)
4664 register const PrimitiveInfo
4669 Draw stroked polygon.
4671 if (image->debug != MagickFalse)
4672 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4673 " begin draw-stroke-polygon");
4674 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4675 clone_info->fill=draw_info->stroke;
4676 if (clone_info->fill_pattern != (Image *) NULL)
4677 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4678 if (clone_info->stroke_pattern != (Image *) NULL)
4679 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4680 MagickTrue,exception);
4681 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4682 clone_info->stroke_width=0.0;
4683 clone_info->fill_rule=NonZeroRule;
4685 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4687 stroke_polygon=TraceStrokePolygon(draw_info,p);
4688 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4691 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4692 q=p+p->coordinates-1;
4693 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4694 MagickTrue : MagickFalse;
4695 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4697 DrawRoundLinecap(image,draw_info,p,exception);
4698 DrawRoundLinecap(image,draw_info,q,exception);
4701 clone_info=DestroyDrawInfo(clone_info);
4702 if (image->debug != MagickFalse)
4703 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4704 " end draw-stroke-polygon");
4705 return(status != 0 ? MagickTrue : MagickFalse);
4709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4713 % G e t A f f i n e M a t r i x %
4717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4719 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4722 % The format of the GetAffineMatrix method is:
4724 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4726 % A description of each parameter follows:
4728 % o affine_matrix: the affine matrix.
4731 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4733 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4734 assert(affine_matrix != (AffineMatrix *) NULL);
4735 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4736 affine_matrix->sx=1.0;
4737 affine_matrix->sy=1.0;
4741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4745 + G e t D r a w I n f o %
4749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4751 % GetDrawInfo() initializes draw_info to default values from image_info.
4753 % The format of the GetDrawInfo method is:
4755 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4757 % A description of each parameter follows:
4759 % o image_info: the image info..
4761 % o draw_info: the draw info.
4764 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4773 Initialize draw attributes.
4775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4776 assert(draw_info != (DrawInfo *) NULL);
4777 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4778 GetAffineMatrix(&draw_info->affine);
4779 exception=AcquireExceptionInfo();
4780 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4782 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4784 draw_info->stroke_width=1.0;
4785 draw_info->alpha=OpaqueAlpha;
4786 draw_info->fill_rule=EvenOddRule;
4787 draw_info->linecap=ButtCap;
4788 draw_info->linejoin=MiterJoin;
4789 draw_info->miterlimit=10;
4790 draw_info->decorate=NoDecoration;
4791 draw_info->pointsize=12.0;
4792 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4793 draw_info->compose=OverCompositeOp;
4794 draw_info->render=MagickTrue;
4795 draw_info->debug=IsEventLogging();
4796 if (image_info != (ImageInfo *) NULL)
4798 draw_info->stroke_antialias=image_info->antialias;
4799 if (image_info->font != (char *) NULL)
4800 draw_info->font=AcquireString(image_info->font);
4801 if (image_info->density != (char *) NULL)
4802 draw_info->density=AcquireString(image_info->density);
4803 draw_info->text_antialias=image_info->antialias;
4804 if (image_info->pointsize != 0.0)
4805 draw_info->pointsize=image_info->pointsize;
4806 draw_info->border_color=image_info->border_color;
4807 if (image_info->server_name != (char *) NULL)
4808 draw_info->server_name=AcquireString(image_info->server_name);
4809 option=GetImageOption(image_info,"encoding");
4810 if (option != (const char *) NULL)
4811 (void) CloneString(&draw_info->encoding,option);
4812 option=GetImageOption(image_info,"kerning");
4813 if (option != (const char *) NULL)
4814 draw_info->kerning=StringToDouble(option,(char **) NULL);
4815 option=GetImageOption(image_info,"interline-spacing");
4816 if (option != (const char *) NULL)
4817 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4818 option=GetImageOption(image_info,"interword-spacing");
4819 if (option != (const char *) NULL)
4820 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4821 option=GetImageOption(image_info,"direction");
4822 if (option != (const char *) NULL)
4823 draw_info->direction=(DirectionType) ParseCommandOption(
4824 MagickDirectionOptions,MagickFalse,option);
4826 draw_info->direction=UndefinedDirection;
4827 option=GetImageOption(image_info,"fill");
4828 if (option != (const char *) NULL)
4829 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4831 option=GetImageOption(image_info,"stroke");
4832 if (option != (const char *) NULL)
4833 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4835 option=GetImageOption(image_info,"strokewidth");
4836 if (option != (const char *) NULL)
4837 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4838 option=GetImageOption(image_info,"undercolor");
4839 if (option != (const char *) NULL)
4840 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4842 option=GetImageOption(image_info,"gravity");
4843 if (option != (const char *) NULL)
4844 draw_info->gravity=(GravityType) ParseCommandOption(
4845 MagickGravityOptions,MagickFalse,option);
4847 exception=DestroyExceptionInfo(exception);
4848 draw_info->signature=MagickSignature;
4852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4856 + P e r m u t a t e %
4860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4862 % Permutate() returns the permuation of the (n,k).
4864 % The format of the Permutate method is:
4866 % void Permutate(ssize_t n,ssize_t k)
4868 % A description of each parameter follows:
4876 static inline double Permutate(const ssize_t n,const ssize_t k)
4885 for (i=k+1; i <= n; i++)
4887 for (i=1; i <= (n-k); i++)
4893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4897 + T r a c e P r i m i t i v e %
4901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4903 % TracePrimitive is a collection of methods for generating graphic
4904 % primitives such as arcs, ellipses, paths, etc.
4908 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4909 const PointInfo end,const PointInfo degrees)
4915 center.x=0.5*(end.x+start.x);
4916 center.y=0.5*(end.y+start.y);
4917 radii.x=fabs(center.x-start.x);
4918 radii.y=fabs(center.y-start.y);
4919 TraceEllipse(primitive_info,center,radii,degrees);
4922 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4923 const PointInfo end,const PointInfo arc,const double angle,
4924 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4943 register PrimitiveInfo
4952 if ((start.x == end.x) && (start.y == end.y))
4954 TracePoint(primitive_info,end);
4957 radii.x=fabs(arc.x);
4958 radii.y=fabs(arc.y);
4959 if ((radii.x == 0.0) || (radii.y == 0.0))
4961 TraceLine(primitive_info,start,end);
4964 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4965 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4966 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4967 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4968 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4970 if (delta < MagickEpsilon)
4972 TraceLine(primitive_info,start,end);
4977 radii.x*=sqrt((double) delta);
4978 radii.y*=sqrt((double) delta);
4980 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4981 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4982 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4983 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4984 alpha=points[1].x-points[0].x;
4985 beta=points[1].y-points[0].y;
4986 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
4991 factor=sqrt((double) factor);
4992 if (sweep == large_arc)
4995 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4996 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4997 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4998 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4999 if ((theta < 0.0) && (sweep != MagickFalse))
5000 theta+=(double) (2.0*MagickPI);
5002 if ((theta > 0.0) && (sweep == MagickFalse))
5003 theta-=(double) (2.0*MagickPI);
5004 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5007 for (i=0; i < (ssize_t) arc_segments; i++)
5009 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5010 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5011 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5012 sin(fmod((double) beta,DegreesToRadians(360.0)));
5013 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5014 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5015 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5016 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5017 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5018 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5019 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5020 theta/arc_segments),DegreesToRadians(360.0))));
5021 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5022 theta/arc_segments),DegreesToRadians(360.0))));
5023 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5024 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5025 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5026 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5027 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5028 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5029 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5031 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5033 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5035 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5037 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5039 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5041 if (i == (ssize_t) (arc_segments-1))
5046 primitive_info->coordinates=(size_t) (p-primitive_info);
5047 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5049 p->primitive=primitive_info->primitive;
5054 static void TraceBezier(PrimitiveInfo *primitive_info,
5055 const size_t number_coordinates)
5067 register PrimitiveInfo
5079 Allocate coeficients.
5081 quantum=number_coordinates;
5082 for (i=0; i < (ssize_t) number_coordinates; i++)
5084 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5086 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5087 if (alpha > (double) quantum)
5088 quantum=(size_t) alpha;
5089 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5090 if (alpha > (double) quantum)
5091 quantum=(size_t) alpha;
5094 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5095 (double) BezierQuantum);
5096 control_points=quantum*number_coordinates;
5097 coefficients=(double *) AcquireQuantumMemory((size_t)
5098 number_coordinates,sizeof(*coefficients));
5099 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5101 if ((coefficients == (double *) NULL) ||
5102 (points == (PointInfo *) NULL))
5103 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5105 Compute bezier points.
5107 end=primitive_info[number_coordinates-1].point;
5108 for (i=0; i < (ssize_t) number_coordinates; i++)
5109 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5111 for (i=0; i < (ssize_t) control_points; i++)
5116 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5117 for (j=0; j < (ssize_t) number_coordinates; j++)
5119 point.x+=alpha*coefficients[j]*p->point.x;
5120 point.y+=alpha*coefficients[j]*p->point.y;
5121 alpha*=weight/(1.0-weight);
5125 weight+=1.0/control_points;
5128 Bezier curves are just short segmented polys.
5131 for (i=0; i < (ssize_t) control_points; i++)
5133 TracePoint(p,points[i]);
5138 primitive_info->coordinates=(size_t) (p-primitive_info);
5139 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5141 p->primitive=primitive_info->primitive;
5144 points=(PointInfo *) RelinquishMagickMemory(points);
5145 coefficients=(double *) RelinquishMagickMemory(coefficients);
5148 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5149 const PointInfo end)
5160 alpha=end.x-start.x;
5162 radius=hypot((double) alpha,(double) beta);
5163 offset.x=(double) radius;
5164 offset.y=(double) radius;
5167 TraceEllipse(primitive_info,start,offset,degrees);
5170 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5171 const PointInfo stop,const PointInfo degrees)
5182 register PrimitiveInfo
5189 Ellipses are just short segmented polys.
5191 if ((stop.x == 0.0) && (stop.y == 0.0))
5193 TracePoint(primitive_info,start);
5196 delta=2.0/MagickMax(stop.x,stop.y);
5197 step=(double) (MagickPI/8.0);
5198 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5199 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5200 angle.x=DegreesToRadians(degrees.x);
5202 while (y < degrees.x)
5204 angle.y=(double) DegreesToRadians(y);
5205 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5207 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5208 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5209 TracePoint(p,point);
5212 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5213 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5214 TracePoint(p,point);
5216 primitive_info->coordinates=(size_t) (p-primitive_info);
5217 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5219 p->primitive=primitive_info->primitive;
5224 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5225 const PointInfo end)
5227 TracePoint(primitive_info,start);
5228 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5229 (fabs(start.y-end.y) < MagickEpsilon))
5231 primitive_info->primitive=PointPrimitive;
5232 primitive_info->coordinates=1;
5235 TracePoint(primitive_info+1,end);
5236 (primitive_info+1)->primitive=primitive_info->primitive;
5237 primitive_info->coordinates=2;
5240 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5243 token[MaxTextExtent];
5258 points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5265 register PrimitiveInfo
5276 number_coordinates=0;
5278 primitive_type=primitive_info->primitive;
5280 for (p=path; *p != '\0'; )
5282 while (isspace((int) ((unsigned char) *p)) != 0)
5286 last_attribute=attribute;
5287 attribute=(int) (*p++);
5308 GetMagickToken(p,&p,token);
5310 GetMagickToken(p,&p,token);
5311 arc.x=StringToDouble(token,(char **) NULL);
5312 GetMagickToken(p,&p,token);
5314 GetMagickToken(p,&p,token);
5315 arc.y=StringToDouble(token,(char **) NULL);
5316 GetMagickToken(p,&p,token);
5318 GetMagickToken(p,&p,token);
5319 angle=StringToDouble(token,(char **) NULL);
5320 GetMagickToken(p,&p,token);
5322 GetMagickToken(p,&p,token);
5323 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5324 GetMagickToken(p,&p,token);
5326 GetMagickToken(p,&p,token);
5327 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5328 GetMagickToken(p,&p,token);
5330 GetMagickToken(p,&p,token);
5331 x=StringToDouble(token,(char **) NULL);
5332 GetMagickToken(p,&p,token);
5334 GetMagickToken(p,&p,token);
5335 y=StringToDouble(token,(char **) NULL);
5336 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5337 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5338 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5341 while (isspace((int) ((unsigned char) *p)) != 0)
5345 } while (IsPoint(p) != MagickFalse);
5352 Compute bezier points.
5357 for (i=1; i < 4; i++)
5359 GetMagickToken(p,&p,token);
5361 GetMagickToken(p,&p,token);
5362 x=StringToDouble(token,(char **) NULL);
5363 GetMagickToken(p,&p,token);
5365 GetMagickToken(p,&p,token);
5366 y=StringToDouble(token,(char **) NULL);
5367 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5368 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5371 for (i=0; i < 4; i++)
5372 (q+i)->point=points[i];
5376 } while (IsPoint(p) != MagickFalse);
5384 GetMagickToken(p,&p,token);
5386 GetMagickToken(p,&p,token);
5387 x=StringToDouble(token,(char **) NULL);
5388 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5389 TracePoint(q,point);
5391 } while (IsPoint(p) != MagickFalse);
5399 GetMagickToken(p,&p,token);
5401 GetMagickToken(p,&p,token);
5402 x=StringToDouble(token,(char **) NULL);
5403 GetMagickToken(p,&p,token);
5405 GetMagickToken(p,&p,token);
5406 y=StringToDouble(token,(char **) NULL);
5407 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5408 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5409 TracePoint(q,point);
5411 } while (IsPoint(p) != MagickFalse);
5417 if (q != primitive_info)
5419 primitive_info->coordinates=(size_t) (q-primitive_info);
5420 number_coordinates+=primitive_info->coordinates;
5426 GetMagickToken(p,&p,token);
5428 GetMagickToken(p,&p,token);
5429 x=StringToDouble(token,(char **) NULL);
5430 GetMagickToken(p,&p,token);
5432 GetMagickToken(p,&p,token);
5433 y=StringToDouble(token,(char **) NULL);
5434 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5435 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5439 TracePoint(q,point);
5441 if ((i != 0) && (attribute == (int) 'M'))
5443 TracePoint(q,point);
5446 } while (IsPoint(p) != MagickFalse);
5453 Compute bezier points.
5458 for (i=1; i < 3; i++)
5460 GetMagickToken(p,&p,token);
5462 GetMagickToken(p,&p,token);
5463 x=StringToDouble(token,(char **) NULL);
5464 GetMagickToken(p,&p,token);
5466 GetMagickToken(p,&p,token);
5467 y=StringToDouble(token,(char **) NULL);
5470 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5471 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5474 for (i=0; i < 3; i++)
5475 (q+i)->point=points[i];
5479 } while (IsPoint(p) != MagickFalse);
5486 Compute bezier points.
5490 points[0]=points[3];
5491 points[1].x=2.0*points[3].x-points[2].x;
5492 points[1].y=2.0*points[3].y-points[2].y;
5493 for (i=2; i < 4; i++)
5495 GetMagickToken(p,&p,token);
5497 GetMagickToken(p,&p,token);
5498 x=StringToDouble(token,(char **) NULL);
5499 GetMagickToken(p,&p,token);
5501 GetMagickToken(p,&p,token);
5502 y=StringToDouble(token,(char **) NULL);
5505 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5506 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5509 if (strchr("CcSs",last_attribute) == (char *) NULL)
5514 for (i=0; i < 4; i++)
5515 (q+i)->point=points[i];
5519 } while (IsPoint(p) != MagickFalse);
5526 Compute bezier points.
5530 points[0]=points[2];
5531 points[1].x=2.0*points[2].x-points[1].x;
5532 points[1].y=2.0*points[2].y-points[1].y;
5533 for (i=2; i < 3; i++)
5535 GetMagickToken(p,&p,token);
5537 GetMagickToken(p,&p,token);
5538 x=StringToDouble(token,(char **) NULL);
5539 GetMagickToken(p,&p,token);
5541 GetMagickToken(p,&p,token);
5542 y=StringToDouble(token,(char **) NULL);
5543 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5544 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5547 if (strchr("QqTt",last_attribute) == (char *) NULL)
5552 for (i=0; i < 3; i++)
5553 (q+i)->point=points[i];
5557 } while (IsPoint(p) != MagickFalse);
5565 GetMagickToken(p,&p,token);
5567 GetMagickToken(p,&p,token);
5568 y=StringToDouble(token,(char **) NULL);
5569 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5570 TracePoint(q,point);
5572 } while (IsPoint(p) != MagickFalse);
5579 TracePoint(q,point);
5581 primitive_info->coordinates=(size_t) (q-primitive_info);
5582 number_coordinates+=primitive_info->coordinates;
5589 if (isalpha((int) ((unsigned char) attribute)) != 0)
5590 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5596 primitive_info->coordinates=(size_t) (q-primitive_info);
5597 number_coordinates+=primitive_info->coordinates;
5598 for (i=0; i < (ssize_t) number_coordinates; i++)
5601 q->primitive=primitive_type;
5603 q->method=FillToBorderMethod;
5606 return(number_coordinates);
5609 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5610 const PointInfo end)
5615 register PrimitiveInfo
5622 TracePoint(p,start);
5626 TracePoint(p,point);
5632 TracePoint(p,point);
5634 TracePoint(p,start);
5636 primitive_info->coordinates=(size_t) (p-primitive_info);
5637 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5639 p->primitive=primitive_info->primitive;
5644 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5645 const PointInfo start,const PointInfo end,PointInfo arc)
5652 register PrimitiveInfo
5659 offset.x=fabs(end.x-start.x);
5660 offset.y=fabs(end.y-start.y);
5661 if (arc.x > (0.5*offset.x))
5663 if (arc.y > (0.5*offset.y))
5665 point.x=start.x+offset.x-arc.x;
5666 point.y=start.y+arc.y;
5669 TraceEllipse(p,point,arc,degrees);
5671 point.x=start.x+offset.x-arc.x;
5672 point.y=start.y+offset.y-arc.y;
5675 TraceEllipse(p,point,arc,degrees);
5677 point.x=start.x+arc.x;
5678 point.y=start.y+offset.y-arc.y;
5681 TraceEllipse(p,point,arc,degrees);
5683 point.x=start.x+arc.x;
5684 point.y=start.y+arc.y;
5687 TraceEllipse(p,point,arc,degrees);
5689 TracePoint(p,primitive_info->point);
5691 primitive_info->coordinates=(size_t) (p-primitive_info);
5692 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5694 p->primitive=primitive_info->primitive;
5699 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5700 const size_t number_vertices,const double offset)
5717 for (i=1; i < (ssize_t) number_vertices; i++)
5719 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5720 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5721 if ((fabs((double) dx) >= MagickEpsilon) ||
5722 (fabs((double) dy) >= MagickEpsilon))
5725 if (i == (ssize_t) number_vertices)
5726 i=(ssize_t) number_vertices-1L;
5727 distance=hypot((double) dx,(double) dy);
5728 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5729 dx*(distance+offset)/distance);
5730 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5731 dy*(distance+offset)/distance);
5732 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5734 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5735 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5736 if ((fabs((double) dx) >= MagickEpsilon) ||
5737 (fabs((double) dy) >= MagickEpsilon))
5740 distance=hypot((double) dx,(double) dy);
5741 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5742 dx*(distance+offset)/distance);
5743 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5744 dy*(distance+offset)/distance);
5747 static inline double DrawEpsilonReciprocal(const double x)
5749 #define DrawEpsilon (1.0e-6)
5751 double sign = x < 0.0 ? -1.0 : 1.0;
5752 return((sign*x) >= DrawEpsilon ? 1.0/x : sign*(1.0/DrawEpsilon));
5755 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5756 const PrimitiveInfo *primitive_info)
5758 typedef struct _LineSegment
5810 number_vertices=primitive_info->coordinates;
5811 max_strokes=2*number_vertices+6*BezierQuantum+360;
5812 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5814 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5816 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5817 number_vertices+2UL,sizeof(*polygon_primitive));
5818 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5819 (polygon_primitive == (PrimitiveInfo *) NULL))
5820 return((PrimitiveInfo *) NULL);
5821 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5822 number_vertices*sizeof(*polygon_primitive));
5824 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5825 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5826 MagickTrue : MagickFalse;
5827 if ((draw_info->linejoin == RoundJoin) ||
5828 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5830 polygon_primitive[number_vertices]=primitive_info[1];
5833 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5835 Compute the slope for the first line segment, p.
5839 for (n=1; n < (ssize_t) number_vertices; n++)
5841 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5842 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5843 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5846 if (n == (ssize_t) number_vertices)
5847 n=(ssize_t) number_vertices-1L;
5848 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5849 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5850 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5851 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5853 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5854 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5855 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5856 offset.y=(double) (offset.x*inverse_slope.p);
5857 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5859 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5860 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5861 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5862 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5863 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5864 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5865 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5866 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5870 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5871 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5872 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5873 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5874 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5875 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5876 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5877 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5880 Create strokes for the line join attribute: bevel, miter, round.
5884 path_q[p++]=box_q[0];
5885 path_p[q++]=box_p[0];
5886 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5889 Compute the slope for this line segment, q.
5891 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5892 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5893 dot_product=dx.q*dx.q+dy.q*dy.q;
5894 if (dot_product < 0.25)
5896 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5897 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5898 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5899 offset.y=(double) (offset.x*inverse_slope.q);
5900 dot_product=dy.q*offset.x-dx.q*offset.y;
5901 if (dot_product > 0.0)
5903 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5904 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5905 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5906 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5907 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5908 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5909 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5910 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5914 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5915 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5916 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5917 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5918 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5919 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5920 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5921 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5923 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
5930 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5931 box_p[3].y)/(slope.p-slope.q));
5932 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5933 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5934 box_q[3].y)/(slope.p-slope.q));
5935 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5937 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5939 max_strokes+=6*BezierQuantum+360;
5940 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5942 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5944 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5946 polygon_primitive=(PrimitiveInfo *)
5947 RelinquishMagickMemory(polygon_primitive);
5948 return((PrimitiveInfo *) NULL);
5951 dot_product=dx.q*dy.p-dx.p*dy.q;
5952 if (dot_product <= 0.0)
5953 switch (draw_info->linejoin)
5957 path_q[q++]=box_q[1];
5958 path_q[q++]=box_q[2];
5959 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5960 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5961 if (dot_product <= miterlimit)
5962 path_p[p++]=box_p[4];
5965 path_p[p++]=box_p[1];
5966 path_p[p++]=box_p[2];
5972 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5973 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5974 if (dot_product <= miterlimit)
5976 path_q[q++]=box_q[4];
5977 path_p[p++]=box_p[4];
5981 path_q[q++]=box_q[1];
5982 path_q[q++]=box_q[2];
5983 path_p[p++]=box_p[1];
5984 path_p[p++]=box_p[2];
5990 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5991 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5992 if (dot_product <= miterlimit)
5993 path_p[p++]=box_p[4];
5996 path_p[p++]=box_p[1];
5997 path_p[p++]=box_p[2];
5999 center=polygon_primitive[n].point;
6000 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6001 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6002 if (theta.q < theta.p)
6003 theta.q+=(double) (2.0*MagickPI);
6004 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6005 (2.0*sqrt((double) (1.0/mid)))));
6006 path_q[q].x=box_q[1].x;
6007 path_q[q].y=box_q[1].y;
6009 for (j=1; j < (ssize_t) arc_segments; j++)
6011 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6012 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6013 (theta.p+delta_theta),DegreesToRadians(360.0))));
6014 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6015 (theta.p+delta_theta),DegreesToRadians(360.0))));
6018 path_q[q++]=box_q[2];
6025 switch (draw_info->linejoin)
6029 path_p[p++]=box_p[1];
6030 path_p[p++]=box_p[2];
6031 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6032 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6033 if (dot_product <= miterlimit)
6034 path_q[q++]=box_q[4];
6037 path_q[q++]=box_q[1];
6038 path_q[q++]=box_q[2];
6044 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6045 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6046 if (dot_product <= miterlimit)
6048 path_q[q++]=box_q[4];
6049 path_p[p++]=box_p[4];
6053 path_q[q++]=box_q[1];
6054 path_q[q++]=box_q[2];
6055 path_p[p++]=box_p[1];
6056 path_p[p++]=box_p[2];
6062 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6063 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6064 if (dot_product <= miterlimit)
6065 path_q[q++]=box_q[4];
6068 path_q[q++]=box_q[1];
6069 path_q[q++]=box_q[2];
6071 center=polygon_primitive[n].point;
6072 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6073 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6074 if (theta.p < theta.q)
6075 theta.p+=(double) (2.0*MagickPI);
6076 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6077 (2.0*sqrt((double) (1.0/mid)))));
6078 path_p[p++]=box_p[1];
6079 for (j=1; j < (ssize_t) arc_segments; j++)
6081 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6082 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6083 (theta.p+delta_theta),DegreesToRadians(360.0))));
6084 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6085 (theta.p+delta_theta),DegreesToRadians(360.0))));
6088 path_p[p++]=box_p[2];
6095 inverse_slope.p=inverse_slope.q;
6104 path_p[p++]=box_p[1];
6105 path_q[q++]=box_q[1];
6107 Trace stroked polygon.
6109 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6110 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6111 if (stroke_polygon != (PrimitiveInfo *) NULL)
6113 for (i=0; i < (ssize_t) p; i++)
6115 stroke_polygon[i]=polygon_primitive[0];
6116 stroke_polygon[i].point=path_p[i];
6118 if (closed_path != MagickFalse)
6120 stroke_polygon[i]=polygon_primitive[0];
6121 stroke_polygon[i].point=stroke_polygon[0].point;
6124 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6126 stroke_polygon[i]=polygon_primitive[0];
6127 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6129 if (closed_path != MagickFalse)
6131 stroke_polygon[i]=polygon_primitive[0];
6132 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6135 stroke_polygon[i]=polygon_primitive[0];
6136 stroke_polygon[i].point=stroke_polygon[0].point;
6138 stroke_polygon[i].primitive=UndefinedPrimitive;
6139 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6141 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6142 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6143 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6144 return(stroke_polygon);