2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/channel.h"
55 #include "MagickCore/color.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/draw.h"
61 #include "MagickCore/draw-private.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/geometry.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/log.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/paint.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/pixel-private.h"
76 #include "MagickCore/property.h"
77 #include "MagickCore/resample.h"
78 #include "MagickCore/resample-private.h"
79 #include "MagickCore/resource_.h"
80 #include "MagickCore/string_.h"
81 #include "MagickCore/string-private.h"
82 #include "MagickCore/thread-private.h"
83 #include "MagickCore/token.h"
84 #include "MagickCore/transform-private.h"
85 #include "MagickCore/utility.h"
90 #define BezierQuantum 200
95 typedef struct _EdgeInfo
119 typedef struct _ElementInfo
129 typedef struct _PolygonInfo
147 typedef struct _PathInfo
157 Forward declarations.
159 static MagickBooleanType
160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
167 TracePath(PrimitiveInfo *,const char *);
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
172 const double,const MagickBooleanType,const MagickBooleanType),
173 TraceBezier(PrimitiveInfo *,const size_t),
174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,
177 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
179 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
181 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % A c q u i r e D r a w I n f o %
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
196 % The format of the AcquireDrawInfo method is:
198 % DrawInfo *AcquireDrawInfo(void)
201 MagickExport DrawInfo *AcquireDrawInfo(void)
206 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
207 if (draw_info == (DrawInfo *) NULL)
208 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
209 GetDrawInfo((ImageInfo *) NULL,draw_info);
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % C l o n e D r a w I n f o %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
225 % is specified, a new DrawInfo structure is created initialized to default
228 % The format of the CloneDrawInfo method is:
230 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
231 % const DrawInfo *draw_info)
233 % A description of each parameter follows:
235 % o image_info: the image info.
237 % o draw_info: the draw info.
240 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
241 const DrawInfo *draw_info)
249 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
250 if (clone_info == (DrawInfo *) NULL)
251 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
252 GetDrawInfo(image_info,clone_info);
253 if (draw_info == (DrawInfo *) NULL)
255 exception=AcquireExceptionInfo();
256 if (clone_info->primitive != (char *) NULL)
257 (void) CloneString(&clone_info->primitive,draw_info->primitive);
258 if (draw_info->geometry != (char *) NULL)
259 (void) CloneString(&clone_info->geometry,draw_info->geometry);
260 clone_info->viewbox=draw_info->viewbox;
261 clone_info->affine=draw_info->affine;
262 clone_info->gravity=draw_info->gravity;
263 clone_info->fill=draw_info->fill;
264 clone_info->stroke=draw_info->stroke;
265 clone_info->stroke_width=draw_info->stroke_width;
266 if (draw_info->fill_pattern != (Image *) NULL)
267 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
269 if (draw_info->stroke_pattern != (Image *) NULL)
270 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
271 MagickTrue,exception);
272 clone_info->stroke_antialias=draw_info->stroke_antialias;
273 clone_info->text_antialias=draw_info->text_antialias;
274 clone_info->fill_rule=draw_info->fill_rule;
275 clone_info->linecap=draw_info->linecap;
276 clone_info->linejoin=draw_info->linejoin;
277 clone_info->miterlimit=draw_info->miterlimit;
278 clone_info->dash_offset=draw_info->dash_offset;
279 clone_info->decorate=draw_info->decorate;
280 clone_info->compose=draw_info->compose;
281 if (draw_info->text != (char *) NULL)
282 (void) CloneString(&clone_info->text,draw_info->text);
283 if (draw_info->font != (char *) NULL)
284 (void) CloneString(&clone_info->font,draw_info->font);
285 if (draw_info->metrics != (char *) NULL)
286 (void) CloneString(&clone_info->metrics,draw_info->metrics);
287 if (draw_info->family != (char *) NULL)
288 (void) CloneString(&clone_info->family,draw_info->family);
289 clone_info->style=draw_info->style;
290 clone_info->stretch=draw_info->stretch;
291 clone_info->weight=draw_info->weight;
292 if (draw_info->encoding != (char *) NULL)
293 (void) CloneString(&clone_info->encoding,draw_info->encoding);
294 clone_info->pointsize=draw_info->pointsize;
295 clone_info->kerning=draw_info->kerning;
296 clone_info->interline_spacing=draw_info->interline_spacing;
297 clone_info->interword_spacing=draw_info->interword_spacing;
298 clone_info->direction=draw_info->direction;
299 if (draw_info->density != (char *) NULL)
300 (void) CloneString(&clone_info->density,draw_info->density);
301 clone_info->align=draw_info->align;
302 clone_info->undercolor=draw_info->undercolor;
303 clone_info->border_color=draw_info->border_color;
304 if (draw_info->server_name != (char *) NULL)
305 (void) CloneString(&clone_info->server_name,draw_info->server_name);
306 if (draw_info->dash_pattern != (double *) NULL)
311 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
312 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
313 sizeof(*clone_info->dash_pattern));
314 if (clone_info->dash_pattern == (double *) NULL)
315 ThrowFatalException(ResourceLimitFatalError,
316 "UnableToAllocateDashPattern");
317 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
318 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
320 clone_info->gradient=draw_info->gradient;
321 if (draw_info->gradient.stops != (StopInfo *) NULL)
326 number_stops=clone_info->gradient.number_stops;
327 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
328 number_stops,sizeof(*clone_info->gradient.stops));
329 if (clone_info->gradient.stops == (StopInfo *) NULL)
330 ThrowFatalException(ResourceLimitFatalError,
331 "UnableToAllocateDashPattern");
332 (void) CopyMagickMemory(clone_info->gradient.stops,
333 draw_info->gradient.stops,(size_t) number_stops*
334 sizeof(*clone_info->gradient.stops));
336 if (draw_info->clip_mask != (char *) NULL)
337 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
338 clone_info->bounds=draw_info->bounds;
339 clone_info->clip_units=draw_info->clip_units;
340 clone_info->render=draw_info->render;
341 clone_info->alpha=draw_info->alpha;
342 clone_info->element_reference=draw_info->element_reference;
343 clone_info->debug=IsEventLogging();
344 exception=DestroyExceptionInfo(exception);
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 + C o n v e r t P a t h T o P o l y g o n %
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 % ConvertPathToPolygon() converts a path to the more efficient sorted
362 % The format of the ConvertPathToPolygon method is:
364 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
365 % const PathInfo *path_info)
367 % A description of each parameter follows:
369 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
370 % rendering form of type PolygonInfo.
372 % o draw_info: Specifies a pointer to an DrawInfo structure.
374 % o path_info: Specifies a pointer to an PathInfo structure.
379 #if defined(__cplusplus) || defined(c_plusplus)
383 static int CompareEdges(const void *x,const void *y)
385 register const EdgeInfo
392 p=(const EdgeInfo *) x;
393 q=(const EdgeInfo *) y;
394 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
396 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
398 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
400 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
402 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
403 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
408 #if defined(__cplusplus) || defined(c_plusplus)
412 static void LogPolygonInfo(const PolygonInfo *polygon_info)
421 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
422 p=polygon_info->edges;
423 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
425 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
427 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
428 p->direction != MagickFalse ? "down" : "up");
429 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
430 p->ghostline != MagickFalse ? "transparent" : "opaque");
431 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
432 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
433 p->bounds.x2,p->bounds.y2);
434 for (j=0; j < (ssize_t) p->number_points; j++)
435 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
436 p->points[j].x,p->points[j].y);
439 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
442 static void ReversePoints(PointInfo *points,const size_t number_points)
450 for (i=0; i < (ssize_t) (number_points >> 1); i++)
453 points[i]=points[number_points-(i+1)];
454 points[number_points-(i+1)]=point;
458 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
487 Convert a path to the more efficient sorted rendering form.
489 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
490 if (polygon_info == (PolygonInfo *) NULL)
491 return((PolygonInfo *) NULL);
493 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
494 sizeof(*polygon_info->edges));
495 if (polygon_info->edges == (EdgeInfo *) NULL)
496 return((PolygonInfo *) NULL);
499 ghostline=MagickFalse;
502 points=(PointInfo *) NULL;
503 (void) ResetMagickMemory(&point,0,sizeof(point));
504 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
505 for (i=0; path_info[i].code != EndCode; i++)
507 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
508 (path_info[i].code == GhostlineCode))
513 if ((points != (PointInfo *) NULL) && (n >= 2))
515 if (edge == number_edges)
518 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
519 polygon_info->edges,(size_t) number_edges,
520 sizeof(*polygon_info->edges));
521 if (polygon_info->edges == (EdgeInfo *) NULL)
522 return((PolygonInfo *) NULL);
524 polygon_info->edges[edge].number_points=(size_t) n;
525 polygon_info->edges[edge].scanline=(-1.0);
526 polygon_info->edges[edge].highwater=0;
527 polygon_info->edges[edge].ghostline=ghostline;
528 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
530 ReversePoints(points,(size_t) n);
531 polygon_info->edges[edge].points=points;
532 polygon_info->edges[edge].bounds=bounds;
533 polygon_info->edges[edge].bounds.y1=points[0].y;
534 polygon_info->edges[edge].bounds.y2=points[n-1].y;
535 points=(PointInfo *) NULL;
536 ghostline=MagickFalse;
539 if (points == (PointInfo *) NULL)
542 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
544 if (points == (PointInfo *) NULL)
545 return((PolygonInfo *) NULL);
547 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
548 point=path_info[i].point;
559 next_direction=((path_info[i].point.y > point.y) ||
560 ((path_info[i].point.y == point.y) &&
561 (path_info[i].point.x > point.x))) ? 1 : -1;
562 if ((points != (PointInfo *) NULL) && (direction != 0) &&
563 (direction != next_direction))
569 if (edge == number_edges)
572 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
573 polygon_info->edges,(size_t) number_edges,
574 sizeof(*polygon_info->edges));
575 if (polygon_info->edges == (EdgeInfo *) NULL)
576 return((PolygonInfo *) NULL);
578 polygon_info->edges[edge].number_points=(size_t) n;
579 polygon_info->edges[edge].scanline=(-1.0);
580 polygon_info->edges[edge].highwater=0;
581 polygon_info->edges[edge].ghostline=ghostline;
582 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
584 ReversePoints(points,(size_t) n);
585 polygon_info->edges[edge].points=points;
586 polygon_info->edges[edge].bounds=bounds;
587 polygon_info->edges[edge].bounds.y1=points[0].y;
588 polygon_info->edges[edge].bounds.y2=points[n-1].y;
590 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
592 if (points == (PointInfo *) NULL)
593 return((PolygonInfo *) NULL);
595 ghostline=MagickFalse;
601 direction=next_direction;
602 if (points == (PointInfo *) NULL)
604 if (n == (ssize_t) number_points)
607 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
609 if (points == (PointInfo *) NULL)
610 return((PolygonInfo *) NULL);
612 point=path_info[i].point;
614 if (point.x < bounds.x1)
616 if (point.x > bounds.x2)
620 if (points != (PointInfo *) NULL)
623 points=(PointInfo *) RelinquishMagickMemory(points);
626 if (edge == number_edges)
629 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
630 polygon_info->edges,(size_t) number_edges,
631 sizeof(*polygon_info->edges));
632 if (polygon_info->edges == (EdgeInfo *) NULL)
633 return((PolygonInfo *) NULL);
635 polygon_info->edges[edge].number_points=(size_t) n;
636 polygon_info->edges[edge].scanline=(-1.0);
637 polygon_info->edges[edge].highwater=0;
638 polygon_info->edges[edge].ghostline=ghostline;
639 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
641 ReversePoints(points,(size_t) n);
642 polygon_info->edges[edge].points=points;
643 polygon_info->edges[edge].bounds=bounds;
644 polygon_info->edges[edge].bounds.y1=points[0].y;
645 polygon_info->edges[edge].bounds.y2=points[n-1].y;
646 ghostline=MagickFalse;
650 polygon_info->number_edges=edge;
651 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
652 sizeof(*polygon_info->edges),CompareEdges);
653 if (IsEventLogging() != MagickFalse)
654 LogPolygonInfo(polygon_info);
655 return(polygon_info);
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 + C o n v e r t P r i m i t i v e T o P a t h %
667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
672 % The format of the ConvertPrimitiveToPath method is:
674 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
675 % const PrimitiveInfo *primitive_info)
677 % A description of each parameter follows:
679 % o Method ConvertPrimitiveToPath returns a vector path structure of type
682 % o draw_info: a structure of type DrawInfo.
684 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
689 static void LogPathInfo(const PathInfo *path_info)
691 register const PathInfo
694 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
695 for (p=path_info; p->code != EndCode; p++)
696 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
697 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
698 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
699 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
701 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
704 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
725 Converts a PrimitiveInfo structure into a vector path structure.
727 switch (primitive_info->primitive)
734 return((PathInfo *) NULL);
738 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
739 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
741 if (path_info == (PathInfo *) NULL)
742 return((PathInfo *) NULL);
750 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
753 if (coordinates <= 0)
755 coordinates=(ssize_t) primitive_info[i].coordinates;
756 p=primitive_info[i].point;
762 Eliminate duplicate points.
764 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
765 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
767 path_info[n].code=code;
768 path_info[n].point=primitive_info[i].point;
769 q=primitive_info[i].point;
774 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
775 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
778 Mark the p point as open if it does not match the q.
780 path_info[start].code=OpenCode;
781 path_info[n].code=GhostlineCode;
782 path_info[n].point=primitive_info[i].point;
784 path_info[n].code=LineToCode;
785 path_info[n].point=p;
788 path_info[n].code=EndCode;
789 path_info[n].point.x=0.0;
790 path_info[n].point.y=0.0;
791 if (IsEventLogging() != MagickFalse)
792 LogPathInfo(path_info);
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % D e s t r o y D r a w I n f o %
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
807 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
810 % The format of the DestroyDrawInfo method is:
812 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
814 % A description of each parameter follows:
816 % o draw_info: the draw info.
819 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
821 if (draw_info->debug != MagickFalse)
822 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
823 assert(draw_info != (DrawInfo *) NULL);
824 assert(draw_info->signature == MagickCoreSignature);
825 if (draw_info->primitive != (char *) NULL)
826 draw_info->primitive=DestroyString(draw_info->primitive);
827 if (draw_info->text != (char *) NULL)
828 draw_info->text=DestroyString(draw_info->text);
829 if (draw_info->geometry != (char *) NULL)
830 draw_info->geometry=DestroyString(draw_info->geometry);
831 if (draw_info->fill_pattern != (Image *) NULL)
832 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
833 if (draw_info->stroke_pattern != (Image *) NULL)
834 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
835 if (draw_info->font != (char *) NULL)
836 draw_info->font=DestroyString(draw_info->font);
837 if (draw_info->metrics != (char *) NULL)
838 draw_info->metrics=DestroyString(draw_info->metrics);
839 if (draw_info->family != (char *) NULL)
840 draw_info->family=DestroyString(draw_info->family);
841 if (draw_info->encoding != (char *) NULL)
842 draw_info->encoding=DestroyString(draw_info->encoding);
843 if (draw_info->density != (char *) NULL)
844 draw_info->density=DestroyString(draw_info->density);
845 if (draw_info->server_name != (char *) NULL)
846 draw_info->server_name=(char *)
847 RelinquishMagickMemory(draw_info->server_name);
848 if (draw_info->dash_pattern != (double *) NULL)
849 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
850 draw_info->dash_pattern);
851 if (draw_info->gradient.stops != (StopInfo *) NULL)
852 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
853 draw_info->gradient.stops);
854 if (draw_info->clip_mask != (char *) NULL)
855 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
856 draw_info->signature=(~MagickCoreSignature);
857 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866 + D e s t r o y E d g e %
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
872 % DestroyEdge() destroys the specified polygon edge.
874 % The format of the DestroyEdge method is:
876 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
878 % A description of each parameter follows:
880 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
882 % o edge: the polygon edge number to destroy.
885 static size_t DestroyEdge(PolygonInfo *polygon_info,
888 assert(edge < polygon_info->number_edges);
889 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
890 polygon_info->edges[edge].points);
891 polygon_info->number_edges--;
892 if (edge < polygon_info->number_edges)
893 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
894 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
895 return(polygon_info->number_edges);
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 + D e s t r o y P o l y g o n I n f o %
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
911 % The format of the DestroyPolygonInfo method is:
913 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
915 % A description of each parameter follows:
917 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
920 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
925 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
926 polygon_info->edges[i].points=(PointInfo *)
927 RelinquishMagickMemory(polygon_info->edges[i].points);
928 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
929 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 % D r a w A f f i n e I m a g e %
941 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 % DrawAffineImage() composites the source over the destination image as
944 % dictated by the affine transform.
946 % The format of the DrawAffineImage method is:
948 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
949 % const AffineMatrix *affine,ExceptionInfo *exception)
951 % A description of each parameter follows:
953 % o image: the image.
955 % o source: the source image.
957 % o affine: the affine transform.
959 % o exception: return any errors or warnings in this structure.
963 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
964 const double y,const SegmentInfo *edge)
977 Determine left and right edges.
979 inverse_edge.x1=edge->x1;
980 inverse_edge.y1=edge->y1;
981 inverse_edge.x2=edge->x2;
982 inverse_edge.y2=edge->y2;
983 z=affine->ry*y+affine->tx;
984 if (affine->sx >= MagickEpsilon)
986 intercept=(-z/affine->sx);
988 if (x > inverse_edge.x1)
990 intercept=(-z+(double) image->columns)/affine->sx;
992 if (x < inverse_edge.x2)
996 if (affine->sx < -MagickEpsilon)
998 intercept=(-z+(double) image->columns)/affine->sx;
1000 if (x > inverse_edge.x1)
1002 intercept=(-z/affine->sx);
1004 if (x < inverse_edge.x2)
1008 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1010 inverse_edge.x2=edge->x1;
1011 return(inverse_edge);
1014 Determine top and bottom edges.
1016 z=affine->sy*y+affine->ty;
1017 if (affine->rx >= MagickEpsilon)
1019 intercept=(-z/affine->rx);
1021 if (x > inverse_edge.x1)
1023 intercept=(-z+(double) image->rows)/affine->rx;
1025 if (x < inverse_edge.x2)
1029 if (affine->rx < -MagickEpsilon)
1031 intercept=(-z+(double) image->rows)/affine->rx;
1033 if (x > inverse_edge.x1)
1035 intercept=(-z/affine->rx);
1037 if (x < inverse_edge.x2)
1041 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1043 inverse_edge.x2=edge->x2;
1044 return(inverse_edge);
1046 return(inverse_edge);
1049 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1057 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1059 inverse_affine.sx=determinant*affine->sy;
1060 inverse_affine.rx=determinant*(-affine->rx);
1061 inverse_affine.ry=determinant*(-affine->ry);
1062 inverse_affine.sy=determinant*affine->sx;
1063 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1065 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1067 return(inverse_affine);
1070 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1071 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1103 Determine bounding box.
1105 assert(image != (Image *) NULL);
1106 assert(image->signature == MagickCoreSignature);
1107 if (image->debug != MagickFalse)
1108 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1109 assert(source != (const Image *) NULL);
1110 assert(source->signature == MagickCoreSignature);
1111 assert(affine != (AffineMatrix *) NULL);
1114 extent[1].x=(double) source->columns-1.0;
1116 extent[2].x=(double) source->columns-1.0;
1117 extent[2].y=(double) source->rows-1.0;
1119 extent[3].y=(double) source->rows-1.0;
1120 for (i=0; i < 4; i++)
1126 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1127 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1131 for (i=1; i < 4; i++)
1133 if (min.x > extent[i].x)
1135 if (min.y > extent[i].y)
1137 if (max.x < extent[i].x)
1139 if (max.y < extent[i].y)
1143 Affine transform image.
1145 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1146 return(MagickFalse);
1148 edge.x1=MagickMax(min.x,0.0);
1149 edge.y1=MagickMax(min.y,0.0);
1150 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1151 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1152 inverse_affine=InverseAffineMatrix(affine);
1153 GetPixelInfo(image,&zero);
1154 start=(ssize_t) ceil(edge.y1-0.5);
1155 stop=(ssize_t) floor(edge.y2+0.5);
1156 source_view=AcquireVirtualCacheView(source,exception);
1157 image_view=AcquireAuthenticCacheView(image,exception);
1158 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1159 #pragma omp parallel for schedule(static,4) shared(status) \
1160 magick_threads(source,image,1,1)
1162 for (y=start; y <= stop; y++)
1183 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1184 if (inverse_edge.x2 < inverse_edge.x1)
1186 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1187 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1189 if (q == (Quantum *) NULL)
1194 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1196 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1198 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1200 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1201 point.x,point.y,&pixel,exception);
1202 GetPixelInfoPixel(image,q,&composite);
1203 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1205 SetPixelViaPixelInfo(image,&composite,q);
1207 q+=GetPixelChannels(image);
1209 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1212 source_view=DestroyCacheView(source_view);
1213 image_view=DestroyCacheView(image_view);
1218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222 + D r a w B o u n d i n g R e c t a n g l e s %
1226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1229 % is only useful for developers debugging the rendering algorithm.
1231 % The format of the DrawBoundingRectangles method is:
1233 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1234 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1236 % A description of each parameter follows:
1238 % o image: the image.
1240 % o draw_info: the draw info.
1242 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1244 % o exception: return any errors or warnings in this structure.
1247 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1248 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1273 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1274 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1276 resolution.x=DefaultResolution;
1277 resolution.y=DefaultResolution;
1278 if (clone_info->density != (char *) NULL)
1286 flags=ParseGeometry(clone_info->density,&geometry_info);
1287 resolution.x=geometry_info.rho;
1288 resolution.y=geometry_info.sigma;
1289 if ((flags & SigmaValue) == MagickFalse)
1290 resolution.y=resolution.x;
1292 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1293 clone_info->stroke_width/2.0;
1298 if (polygon_info != (PolygonInfo *) NULL)
1300 bounds=polygon_info->edges[0].bounds;
1301 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1303 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1304 bounds.x1=polygon_info->edges[i].bounds.x1;
1305 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1306 bounds.y1=polygon_info->edges[i].bounds.y1;
1307 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1308 bounds.x2=polygon_info->edges[i].bounds.x2;
1309 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1310 bounds.y2=polygon_info->edges[i].bounds.y2;
1313 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1314 image->columns ? (double) image->columns-1 : bounds.x1;
1316 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1317 image->rows ? (double) image->rows-1 : bounds.y1;
1319 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1320 image->columns ? (double) image->columns-1 : bounds.x2;
1322 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1323 image->rows ? (double) image->rows-1 : bounds.y2;
1324 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1326 if (polygon_info->edges[i].direction != 0)
1327 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1330 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1332 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1333 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1334 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1335 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1336 primitive_info[0].primitive=RectanglePrimitive;
1337 TraceRectangle(primitive_info,start,end);
1338 primitive_info[0].method=ReplaceMethod;
1339 coordinates=(ssize_t) primitive_info[0].coordinates;
1340 primitive_info[coordinates].primitive=UndefinedPrimitive;
1341 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1344 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1346 start.x=(double) (bounds.x1-mid);
1347 start.y=(double) (bounds.y1-mid);
1348 end.x=(double) (bounds.x2+mid);
1349 end.y=(double) (bounds.y2+mid);
1350 primitive_info[0].primitive=RectanglePrimitive;
1351 TraceRectangle(primitive_info,start,end);
1352 primitive_info[0].method=ReplaceMethod;
1353 coordinates=(ssize_t) primitive_info[0].coordinates;
1354 primitive_info[coordinates].primitive=UndefinedPrimitive;
1355 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1356 clone_info=DestroyDrawInfo(clone_info);
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 % D r a w C l i p P a t h %
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 % DrawClipPath() draws the clip path on the image mask.
1372 % The format of the DrawClipPath method is:
1374 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1375 % const char *name,ExceptionInfo *exception)
1377 % A description of each parameter follows:
1379 % o image: the image.
1381 % o draw_info: the draw info.
1383 % o name: the name of the clip path.
1385 % o exception: return any errors or warnings in this structure.
1388 MagickExport MagickBooleanType DrawClipPath(Image *image,
1389 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1392 filename[MagickPathExtent];
1406 assert(image != (Image *) NULL);
1407 assert(image->signature == MagickCoreSignature);
1408 if (image->debug != MagickFalse)
1409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1410 assert(draw_info != (const DrawInfo *) NULL);
1411 (void) FormatLocaleString(filename,MagickPathExtent,"%s",name);
1412 value=GetImageArtifact(image,filename);
1413 if (value == (const char *) NULL)
1414 return(MagickFalse);
1415 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1416 if (clip_mask == (Image *) NULL)
1417 return(MagickFalse);
1418 (void) QueryColorCompliance("#0000",AllCompliance,
1419 &clip_mask->background_color,exception);
1420 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1421 (void) SetImageBackgroundColor(clip_mask,exception);
1422 if (image->debug != MagickFalse)
1423 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1424 draw_info->clip_mask);
1425 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1426 (void) CloneString(&clone_info->primitive,value);
1427 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1429 clone_info->clip_mask=(char *) NULL;
1430 status=NegateImage(clip_mask,MagickFalse,exception);
1431 (void) SetImageMask(image,ReadPixelMask,clip_mask,exception);
1432 clip_mask=DestroyImage(clip_mask);
1433 status&=DrawImage(image,clone_info,exception);
1434 clone_info=DestroyDrawInfo(clone_info);
1435 if (image->debug != MagickFalse)
1436 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1437 return(status != 0 ? MagickTrue : MagickFalse);
1441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1445 + D r a w D a s h P o l y g o n %
1449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1452 % image while respecting the dash offset and dash pattern attributes.
1454 % The format of the DrawDashPolygon method is:
1456 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1457 % const PrimitiveInfo *primitive_info,Image *image,
1458 % ExceptionInfo *exception)
1460 % A description of each parameter follows:
1462 % o draw_info: the draw info.
1464 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1466 % o image: the image.
1468 % o exception: return any errors or warnings in this structure.
1471 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1472 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1504 assert(draw_info != (const DrawInfo *) NULL);
1505 if (image->debug != MagickFalse)
1506 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1507 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1508 clone_info->miterlimit=0;
1509 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1510 number_vertices=(size_t) i;
1511 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1512 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1513 if (dash_polygon == (PrimitiveInfo *) NULL)
1514 return(MagickFalse);
1515 dash_polygon[0]=primitive_info[0];
1516 scale=ExpandAffine(&draw_info->affine);
1517 length=scale*(draw_info->dash_pattern[0]-0.5);
1518 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1520 for (n=0; offset > 0.0; j=0)
1522 if (draw_info->dash_pattern[n] <= 0.0)
1524 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1525 if (offset > length)
1529 length=scale*(draw_info->dash_pattern[n]+0.5);
1532 if (offset < length)
1544 for (i=1; i < (ssize_t) number_vertices; i++)
1546 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1547 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1548 maximum_length=hypot((double) dx,dy);
1552 if (draw_info->dash_pattern[n] == 0.0)
1554 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1556 for (total_length=0.0; (total_length+length) <= maximum_length; )
1558 total_length+=length;
1559 if ((n & 0x01) != 0)
1561 dash_polygon[0]=primitive_info[0];
1562 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1563 total_length/maximum_length);
1564 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1565 total_length/maximum_length);
1570 if ((j+1) > (ssize_t) (2*number_vertices))
1572 dash_polygon[j]=primitive_info[i-1];
1573 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1574 total_length/maximum_length);
1575 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1576 total_length/maximum_length);
1577 dash_polygon[j].coordinates=1;
1579 dash_polygon[0].coordinates=(size_t) j;
1580 dash_polygon[j].primitive=UndefinedPrimitive;
1581 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1584 if (draw_info->dash_pattern[n] == 0.0)
1586 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1588 length-=(maximum_length-total_length);
1589 if ((n & 0x01) != 0)
1591 dash_polygon[j]=primitive_info[i];
1592 dash_polygon[j].coordinates=1;
1595 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1597 dash_polygon[j]=primitive_info[i-1];
1598 dash_polygon[j].point.x+=MagickEpsilon;
1599 dash_polygon[j].point.y+=MagickEpsilon;
1600 dash_polygon[j].coordinates=1;
1602 dash_polygon[0].coordinates=(size_t) j;
1603 dash_polygon[j].primitive=UndefinedPrimitive;
1604 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1606 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1607 clone_info=DestroyDrawInfo(clone_info);
1608 if (image->debug != MagickFalse)
1609 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1610 return(status != 0 ? MagickTrue : MagickFalse);
1614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1618 % D r a w I m a g e %
1622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % DrawImage() draws a graphic primitive on your image. The primitive
1625 % may be represented as a string or filename. Precede the filename with an
1626 % "at" sign (@) and the contents of the file are drawn on the image. You
1627 % can affect how text is drawn by setting one or more members of the draw
1630 % The format of the DrawImage method is:
1632 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1633 % ExceptionInfo *exception)
1635 % A description of each parameter follows:
1637 % o image: the image.
1639 % o draw_info: the draw info.
1641 % o exception: return any errors or warnings in this structure.
1645 static inline MagickBooleanType IsPoint(const char *point)
1653 value=StringToDouble(point,&p);
1654 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1657 static inline void TracePoint(PrimitiveInfo *primitive_info,
1658 const PointInfo point)
1660 primitive_info->coordinates=1;
1661 primitive_info->point=point;
1664 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1665 ExceptionInfo *exception)
1667 #define RenderImageTag "Render/Image"
1674 keyword[MagickPathExtent],
1675 geometry[MagickPathExtent],
1676 pattern[MagickPathExtent],
1731 Ensure the annotation info is valid.
1733 assert(image != (Image *) NULL);
1734 assert(image->signature == MagickCoreSignature);
1735 if (image->debug != MagickFalse)
1736 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1737 assert(draw_info != (DrawInfo *) NULL);
1738 assert(draw_info->signature == MagickCoreSignature);
1739 if (image->debug != MagickFalse)
1740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1741 if ((draw_info->primitive == (char *) NULL) ||
1742 (*draw_info->primitive == '\0'))
1743 return(MagickFalse);
1744 if (image->debug != MagickFalse)
1745 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1746 if (*draw_info->primitive != '@')
1747 primitive=AcquireString(draw_info->primitive);
1749 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1750 if (primitive == (char *) NULL)
1751 return(MagickFalse);
1752 primitive_extent=(double) strlen(primitive);
1753 (void) SetImageArtifact(image,"MVG",primitive);
1756 stops=(StopInfo *) NULL;
1758 Allocate primitive info memory.
1760 graphic_context=(DrawInfo **) AcquireMagickMemory(
1761 sizeof(*graphic_context));
1762 if (graphic_context == (DrawInfo **) NULL)
1764 primitive=DestroyString(primitive);
1765 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1769 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1770 sizeof(*primitive_info));
1771 if (primitive_info == (PrimitiveInfo *) NULL)
1773 primitive=DestroyString(primitive);
1774 for ( ; n >= 0; n--)
1775 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1776 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1777 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1780 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1781 graphic_context[n]->viewbox=image->page;
1782 if ((image->page.width == 0) || (image->page.height == 0))
1784 graphic_context[n]->viewbox.width=image->columns;
1785 graphic_context[n]->viewbox.height=image->rows;
1787 token=AcquireString(primitive);
1788 extent=strlen(token)+MagickPathExtent;
1789 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1790 return(MagickFalse);
1792 for (q=primitive; *q != '\0'; )
1795 Interpret graphic primitive.
1797 GetMagickToken(q,&q,extent,keyword);
1798 if (*keyword == '\0')
1800 if (*keyword == '#')
1805 while ((*q != '\n') && (*q != '\0'))
1809 p=q-strlen(keyword)-1;
1810 primitive_type=UndefinedPrimitive;
1811 current=graphic_context[n]->affine;
1812 GetAffineMatrix(&affine);
1820 if (LocaleCompare("affine",keyword) == 0)
1822 GetMagickToken(q,&q,extent,token);
1823 affine.sx=StringToDouble(token,(char **) NULL);
1824 GetMagickToken(q,&q,extent,token);
1826 GetMagickToken(q,&q,extent,token);
1827 affine.rx=StringToDouble(token,(char **) NULL);
1828 GetMagickToken(q,&q,extent,token);
1830 GetMagickToken(q,&q,extent,token);
1831 affine.ry=StringToDouble(token,(char **) NULL);
1832 GetMagickToken(q,&q,extent,token);
1834 GetMagickToken(q,&q,extent,token);
1835 affine.sy=StringToDouble(token,(char **) NULL);
1836 GetMagickToken(q,&q,extent,token);
1838 GetMagickToken(q,&q,extent,token);
1839 affine.tx=StringToDouble(token,(char **) NULL);
1840 GetMagickToken(q,&q,extent,token);
1842 GetMagickToken(q,&q,extent,token);
1843 affine.ty=StringToDouble(token,(char **) NULL);
1846 if (LocaleCompare("alpha",keyword) == 0)
1848 primitive_type=AlphaPrimitive;
1851 if (LocaleCompare("arc",keyword) == 0)
1853 primitive_type=ArcPrimitive;
1862 if (LocaleCompare("bezier",keyword) == 0)
1864 primitive_type=BezierPrimitive;
1867 if (LocaleCompare("border-color",keyword) == 0)
1869 GetMagickToken(q,&q,extent,token);
1870 (void) QueryColorCompliance(token,AllCompliance,
1871 &graphic_context[n]->border_color,exception);
1880 if (LocaleCompare("clip-path",keyword) == 0)
1885 GetMagickToken(q,&q,extent,token);
1886 (void) CloneString(&graphic_context[n]->clip_mask,token);
1887 (void) DrawClipPath(image,graphic_context[n],
1888 graphic_context[n]->clip_mask,exception);
1891 if (LocaleCompare("clip-rule",keyword) == 0)
1896 GetMagickToken(q,&q,extent,token);
1897 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1899 if (fill_rule == -1)
1902 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1905 if (LocaleCompare("clip-units",keyword) == 0)
1910 GetMagickToken(q,&q,extent,token);
1911 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1913 if (clip_units == -1)
1918 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1919 if (clip_units == ObjectBoundingBox)
1921 GetAffineMatrix(¤t);
1922 affine.sx=draw_info->bounds.x2;
1923 affine.sy=draw_info->bounds.y2;
1924 affine.tx=draw_info->bounds.x1;
1925 affine.ty=draw_info->bounds.y1;
1930 if (LocaleCompare("circle",keyword) == 0)
1932 primitive_type=CirclePrimitive;
1935 if (LocaleCompare("color",keyword) == 0)
1937 primitive_type=ColorPrimitive;
1946 if (LocaleCompare("decorate",keyword) == 0)
1951 GetMagickToken(q,&q,extent,token);
1952 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1957 graphic_context[n]->decorate=(DecorationType) decorate;
1960 if (LocaleCompare("density",keyword) == 0)
1962 GetMagickToken(q,&q,extent,token);
1963 (void) CloneString(&graphic_context[n]->density,token);
1966 if (LocaleCompare("direction",keyword) == 0)
1971 GetMagickToken(q,&q,extent,token);
1972 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
1974 if (direction == -1)
1977 graphic_context[n]->direction=(DirectionType) direction;
1986 if (LocaleCompare("ellipse",keyword) == 0)
1988 primitive_type=EllipsePrimitive;
1991 if (LocaleCompare("encoding",keyword) == 0)
1993 GetMagickToken(q,&q,extent,token);
1994 (void) CloneString(&graphic_context[n]->encoding,token);
2003 if (LocaleCompare("fill",keyword) == 0)
2005 GetMagickToken(q,&q,extent,token);
2006 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2007 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2008 (void) DrawPatternPath(image,draw_info,token,
2009 &graphic_context[n]->fill_pattern,exception);
2012 status&=QueryColorCompliance(token,AllCompliance,
2013 &graphic_context[n]->fill,exception);
2014 if (status == MagickFalse)
2019 pattern_info=AcquireImageInfo();
2020 (void) CopyMagickString(pattern_info->filename,token,
2022 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2024 CatchException(exception);
2025 pattern_info=DestroyImageInfo(pattern_info);
2030 if (LocaleCompare("fill-opacity",keyword) == 0)
2032 GetMagickToken(q,&q,extent,token);
2033 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2034 graphic_context[n]->fill.alpha=(double) QuantumRange*
2035 factor*StringToDouble(token,(char **) NULL);
2038 if (LocaleCompare("fill-rule",keyword) == 0)
2043 GetMagickToken(q,&q,extent,token);
2044 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2046 if (fill_rule == -1)
2049 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2052 if (LocaleCompare("font",keyword) == 0)
2054 GetMagickToken(q,&q,extent,token);
2055 (void) CloneString(&graphic_context[n]->font,token);
2056 if (LocaleCompare("none",token) == 0)
2057 graphic_context[n]->font=(char *)
2058 RelinquishMagickMemory(graphic_context[n]->font);
2061 if (LocaleCompare("font-family",keyword) == 0)
2063 GetMagickToken(q,&q,extent,token);
2064 (void) CloneString(&graphic_context[n]->family,token);
2067 if (LocaleCompare("font-size",keyword) == 0)
2069 GetMagickToken(q,&q,extent,token);
2070 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2073 if (LocaleCompare("font-stretch",keyword) == 0)
2078 GetMagickToken(q,&q,extent,token);
2079 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2083 graphic_context[n]->stretch=(StretchType) stretch;
2086 if (LocaleCompare("font-style",keyword) == 0)
2091 GetMagickToken(q,&q,extent,token);
2092 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2096 graphic_context[n]->style=(StyleType) style;
2099 if (LocaleCompare("font-weight",keyword) == 0)
2104 GetMagickToken(q,&q,extent,token);
2105 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,
2108 weight=StringToUnsignedLong(token);
2109 graphic_context[n]->weight=(size_t) weight;
2118 if (LocaleCompare("gradient-units",keyword) == 0)
2120 GetMagickToken(q,&q,extent,token);
2123 if (LocaleCompare("gravity",keyword) == 0)
2128 GetMagickToken(q,&q,extent,token);
2129 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2133 graphic_context[n]->gravity=(GravityType) gravity;
2142 if (LocaleCompare("image",keyword) == 0)
2147 primitive_type=ImagePrimitive;
2148 GetMagickToken(q,&q,extent,token);
2149 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2153 graphic_context[n]->compose=(CompositeOperator) compose;
2156 if (LocaleCompare("interline-spacing",keyword) == 0)
2158 GetMagickToken(q,&q,extent,token);
2159 graphic_context[n]->interline_spacing=StringToDouble(token,
2163 if (LocaleCompare("interword-spacing",keyword) == 0)
2165 GetMagickToken(q,&q,extent,token);
2166 graphic_context[n]->interword_spacing=StringToDouble(token,
2176 if (LocaleCompare("kerning",keyword) == 0)
2178 GetMagickToken(q,&q,extent,token);
2179 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2188 if (LocaleCompare("line",keyword) == 0)
2189 primitive_type=LinePrimitive;
2197 if (LocaleCompare("offset",keyword) == 0)
2199 GetMagickToken(q,&q,extent,token);
2202 if (LocaleCompare("opacity",keyword) == 0)
2204 GetMagickToken(q,&q,extent,token);
2205 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2206 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2207 QuantumScale*graphic_context[n]->alpha)*factor*
2208 StringToDouble(token,(char **) NULL))));
2209 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2210 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2219 if (LocaleCompare("path",keyword) == 0)
2221 primitive_type=PathPrimitive;
2224 if (LocaleCompare("point",keyword) == 0)
2226 primitive_type=PointPrimitive;
2229 if (LocaleCompare("polyline",keyword) == 0)
2231 primitive_type=PolylinePrimitive;
2234 if (LocaleCompare("polygon",keyword) == 0)
2236 primitive_type=PolygonPrimitive;
2239 if (LocaleCompare("pop",keyword) == 0)
2241 GetMagickToken(q,&q,extent,token);
2242 if (LocaleCompare("clip-path",token) == 0)
2244 if (LocaleCompare("defs",token) == 0)
2246 if (LocaleCompare("gradient",token) == 0)
2248 if (LocaleCompare("graphic-context",token) == 0)
2252 (void) ThrowMagickException(exception,GetMagickModule(),
2253 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2257 if (graphic_context[n]->clip_mask != (char *) NULL)
2258 if (LocaleCompare(graphic_context[n]->clip_mask,
2259 graphic_context[n-1]->clip_mask) != 0)
2260 (void) SetImageMask(image,ReadPixelMask,(Image *) NULL,
2262 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2266 if (LocaleCompare("pattern",token) == 0)
2271 if (LocaleCompare("push",keyword) == 0)
2273 GetMagickToken(q,&q,extent,token);
2274 if (LocaleCompare("clip-path",token) == 0)
2277 name[MagickPathExtent];
2279 GetMagickToken(q,&q,extent,token);
2280 (void) FormatLocaleString(name,MagickPathExtent,"%s",token);
2281 for (p=q; *q != '\0'; )
2283 GetMagickToken(q,&q,extent,token);
2284 if (LocaleCompare(token,"pop") != 0)
2286 GetMagickToken(q,(const char **) NULL,extent,token);
2287 if (LocaleCompare(token,"clip-path") != 0)
2291 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2292 (void) SetImageArtifact(image,name,token);
2293 GetMagickToken(q,&q,extent,token);
2296 if (LocaleCompare("gradient",token) == 0)
2299 key[2*MagickPathExtent],
2300 name[MagickPathExtent],
2301 type[MagickPathExtent];
2306 GetMagickToken(q,&q,extent,token);
2307 (void) CopyMagickString(name,token,MagickPathExtent);
2308 GetMagickToken(q,&q,extent,token);
2309 (void) CopyMagickString(type,token,MagickPathExtent);
2310 GetMagickToken(q,&q,extent,token);
2311 segment.x1=StringToDouble(token,(char **) NULL);
2312 GetMagickToken(q,&q,extent,token);
2314 GetMagickToken(q,&q,extent,token);
2315 segment.y1=StringToDouble(token,(char **) NULL);
2316 GetMagickToken(q,&q,extent,token);
2318 GetMagickToken(q,&q,extent,token);
2319 segment.x2=StringToDouble(token,(char **) NULL);
2320 GetMagickToken(q,&q,extent,token);
2322 GetMagickToken(q,&q,extent,token);
2323 segment.y2=StringToDouble(token,(char **) NULL);
2324 if (LocaleCompare(type,"radial") == 0)
2326 GetMagickToken(q,&q,extent,token);
2328 GetMagickToken(q,&q,extent,token);
2330 for (p=q; *q != '\0'; )
2332 GetMagickToken(q,&q,extent,token);
2333 if (LocaleCompare(token,"pop") != 0)
2335 GetMagickToken(q,(const char **) NULL,extent,token);
2336 if (LocaleCompare(token,"gradient") != 0)
2340 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2341 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2342 graphic_context[n]->affine.ry*segment.y1+
2343 graphic_context[n]->affine.tx;
2344 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2345 graphic_context[n]->affine.sy*segment.y1+
2346 graphic_context[n]->affine.ty;
2347 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2348 graphic_context[n]->affine.ry*segment.y2+
2349 graphic_context[n]->affine.tx;
2350 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2351 graphic_context[n]->affine.sy*segment.y2+
2352 graphic_context[n]->affine.ty;
2353 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2354 (void) SetImageArtifact(image,key,token);
2355 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
2356 (void) SetImageArtifact(image,key,type);
2357 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",name);
2358 (void) FormatLocaleString(geometry,MagickPathExtent,
2359 "%gx%g%+.15g%+.15g",
2360 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2361 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2362 bounds.x1,bounds.y1);
2363 (void) SetImageArtifact(image,key,geometry);
2364 GetMagickToken(q,&q,extent,token);
2367 if (LocaleCompare("pattern",token) == 0)
2370 key[2*MagickPathExtent],
2371 name[MagickPathExtent];
2376 GetMagickToken(q,&q,extent,token);
2377 (void) CopyMagickString(name,token,MagickPathExtent);
2378 GetMagickToken(q,&q,extent,token);
2379 pattern_bounds.x=(ssize_t) ceil(StringToDouble(token,
2380 (char **) NULL)-0.5);
2381 GetMagickToken(q,&q,extent,token);
2383 GetMagickToken(q,&q,extent,token);
2384 pattern_bounds.y=(ssize_t) ceil(StringToDouble(token,
2385 (char **) NULL)-0.5);
2386 GetMagickToken(q,&q,extent,token);
2388 GetMagickToken(q,&q,extent,token);
2389 pattern_bounds.width=(size_t) floor(StringToDouble(token,
2390 (char **) NULL)+0.5);
2391 GetMagickToken(q,&q,extent,token);
2393 GetMagickToken(q,&q,extent,token);
2394 pattern_bounds.height=(size_t) floor(StringToDouble(token,
2395 (char **) NULL)+0.5);
2396 for (p=q; *q != '\0'; )
2398 GetMagickToken(q,&q,extent,token);
2399 if (LocaleCompare(token,"pop") != 0)
2401 GetMagickToken(q,(const char **) NULL,extent,token);
2402 if (LocaleCompare(token,"pattern") != 0)
2406 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2407 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2408 (void) SetImageArtifact(image,key,token);
2409 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
2411 (void) FormatLocaleString(geometry,MagickPathExtent,
2412 "%.20gx%.20g%+.20g%+.20g",(double)pattern_bounds.width,
2413 (double)pattern_bounds.height,(double)pattern_bounds.x,
2414 (double)pattern_bounds.y);
2415 (void) SetImageArtifact(image,key,geometry);
2416 GetMagickToken(q,&q,extent,token);
2419 if (LocaleCompare("graphic-context",token) == 0)
2422 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2423 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2424 if (graphic_context == (DrawInfo **) NULL)
2426 (void) ThrowMagickException(exception,GetMagickModule(),
2427 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2431 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2432 graphic_context[n-1]);
2435 if (LocaleCompare("defs",token) == 0)
2446 if (LocaleCompare("rectangle",keyword) == 0)
2448 primitive_type=RectanglePrimitive;
2451 if (LocaleCompare("rotate",keyword) == 0)
2453 GetMagickToken(q,&q,extent,token);
2454 angle=StringToDouble(token,(char **) NULL);
2455 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2456 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2457 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2458 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2461 if (LocaleCompare("roundRectangle",keyword) == 0)
2463 primitive_type=RoundRectanglePrimitive;
2472 if (LocaleCompare("scale",keyword) == 0)
2474 GetMagickToken(q,&q,extent,token);
2475 affine.sx=StringToDouble(token,(char **) NULL);
2476 GetMagickToken(q,&q,extent,token);
2478 GetMagickToken(q,&q,extent,token);
2479 affine.sy=StringToDouble(token,(char **) NULL);
2482 if (LocaleCompare("skewX",keyword) == 0)
2484 GetMagickToken(q,&q,extent,token);
2485 angle=StringToDouble(token,(char **) NULL);
2486 affine.ry=sin(DegreesToRadians(angle));
2489 if (LocaleCompare("skewY",keyword) == 0)
2491 GetMagickToken(q,&q,extent,token);
2492 angle=StringToDouble(token,(char **) NULL);
2493 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2496 if (LocaleCompare("stop-color",keyword) == 0)
2502 if (number_stops == 1)
2503 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
2504 else if (number_stops > 2)
2505 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
2507 if (stops == (StopInfo *) NULL)
2509 (void) ThrowMagickException(exception,GetMagickModule(),
2510 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2514 GetMagickToken(q,&q,extent,token);
2515 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2517 stops[number_stops-1].color=stop_color;
2518 GetMagickToken(q,&q,extent,token);
2519 stops[number_stops-1].offset=StringToDouble(token,(char **) NULL);
2522 if (LocaleCompare("stroke",keyword) == 0)
2524 GetMagickToken(q,&q,extent,token);
2525 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2526 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2527 (void) DrawPatternPath(image,draw_info,token,
2528 &graphic_context[n]->stroke_pattern,exception);
2531 status&=QueryColorCompliance(token,AllCompliance,
2532 &graphic_context[n]->stroke,exception);
2533 if (status == MagickFalse)
2538 pattern_info=AcquireImageInfo();
2539 (void) CopyMagickString(pattern_info->filename,token,
2541 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2543 CatchException(exception);
2544 pattern_info=DestroyImageInfo(pattern_info);
2549 if (LocaleCompare("stroke-antialias",keyword) == 0)
2551 GetMagickToken(q,&q,extent,token);
2552 graphic_context[n]->stroke_antialias=
2553 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2556 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2558 if (graphic_context[n]->dash_pattern != (double *) NULL)
2559 graphic_context[n]->dash_pattern=(double *)
2560 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2561 if (IsPoint(q) != MagickFalse)
2567 GetMagickToken(r,&r,extent,token);
2569 GetMagickToken(r,&r,extent,token);
2570 for (x=0; IsPoint(token) != MagickFalse; x++)
2572 GetMagickToken(r,&r,extent,token);
2574 GetMagickToken(r,&r,extent,token);
2576 graphic_context[n]->dash_pattern=(double *)
2577 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2578 sizeof(*graphic_context[n]->dash_pattern));
2579 if (graphic_context[n]->dash_pattern == (double *) NULL)
2581 (void) ThrowMagickException(exception,GetMagickModule(),
2582 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2586 for (j=0; j < x; j++)
2588 GetMagickToken(q,&q,extent,token);
2590 GetMagickToken(q,&q,extent,token);
2591 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2594 if ((x & 0x01) != 0)
2595 for ( ; j < (2*x); j++)
2596 graphic_context[n]->dash_pattern[j]=
2597 graphic_context[n]->dash_pattern[j-x];
2598 graphic_context[n]->dash_pattern[j]=0.0;
2601 GetMagickToken(q,&q,extent,token);
2604 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2606 GetMagickToken(q,&q,extent,token);
2607 graphic_context[n]->dash_offset=StringToDouble(token,
2611 if (LocaleCompare("stroke-linecap",keyword) == 0)
2616 GetMagickToken(q,&q,extent,token);
2617 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2621 graphic_context[n]->linecap=(LineCap) linecap;
2624 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2629 GetMagickToken(q,&q,extent,token);
2630 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2635 graphic_context[n]->linejoin=(LineJoin) linejoin;
2638 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2640 GetMagickToken(q,&q,extent,token);
2641 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2644 if (LocaleCompare("stroke-opacity",keyword) == 0)
2646 GetMagickToken(q,&q,extent,token);
2647 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2648 graphic_context[n]->stroke.alpha=(double) QuantumRange*
2649 factor*StringToDouble(token,(char **) NULL);
2652 if (LocaleCompare("stroke-width",keyword) == 0)
2654 GetMagickToken(q,&q,extent,token);
2655 graphic_context[n]->stroke_width=StringToDouble(token,
2665 if (LocaleCompare("text",keyword) == 0)
2667 primitive_type=TextPrimitive;
2670 if (LocaleCompare("text-align",keyword) == 0)
2675 GetMagickToken(q,&q,extent,token);
2676 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2680 graphic_context[n]->align=(AlignType) align;
2683 if (LocaleCompare("text-anchor",keyword) == 0)
2688 GetMagickToken(q,&q,extent,token);
2689 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2693 graphic_context[n]->align=(AlignType) align;
2696 if (LocaleCompare("text-antialias",keyword) == 0)
2698 GetMagickToken(q,&q,extent,token);
2699 graphic_context[n]->text_antialias=
2700 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2703 if (LocaleCompare("text-undercolor",keyword) == 0)
2705 GetMagickToken(q,&q,extent,token);
2706 (void) QueryColorCompliance(token,AllCompliance,
2707 &graphic_context[n]->undercolor,exception);
2710 if (LocaleCompare("translate",keyword) == 0)
2712 GetMagickToken(q,&q,extent,token);
2713 affine.tx=StringToDouble(token,(char **) NULL);
2714 GetMagickToken(q,&q,extent,token);
2716 GetMagickToken(q,&q,extent,token);
2717 affine.ty=StringToDouble(token,(char **) NULL);
2726 if (LocaleCompare("viewbox",keyword) == 0)
2728 GetMagickToken(q,&q,extent,token);
2729 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2730 (char **) NULL)-0.5);
2731 GetMagickToken(q,&q,extent,token);
2733 GetMagickToken(q,&q,extent,token);
2734 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2735 (char **) NULL)-0.5);
2736 GetMagickToken(q,&q,extent,token);
2738 GetMagickToken(q,&q,extent,token);
2739 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2740 token,(char **) NULL)+0.5);
2741 GetMagickToken(q,&q,extent,token);
2743 GetMagickToken(q,&q,extent,token);
2744 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2745 token,(char **) NULL)+0.5);
2757 if (status == MagickFalse)
2759 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2760 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2762 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2763 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2764 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2765 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2766 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2768 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2771 if (primitive_type == UndefinedPrimitive)
2775 if (number_stops > 1)
2780 type=LinearGradient;
2781 if (draw_info->gradient.type == RadialGradient)
2782 type=RadialGradient;
2783 (void) GradientImage(image,type,PadSpread,stops,number_stops,
2786 if (number_stops > 0)
2787 stops=(StopInfo *) RelinquishMagickMemory(stops);
2789 if (image->debug != MagickFalse)
2790 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2795 Parse the primitive attributes.
2799 primitive_info[0].point.x=0.0;
2800 primitive_info[0].point.y=0.0;
2801 for (x=0; *q != '\0'; x++)
2806 if (IsPoint(q) == MagickFalse)
2808 GetMagickToken(q,&q,extent,token);
2809 point.x=StringToDouble(token,(char **) NULL);
2810 GetMagickToken(q,&q,extent,token);
2812 GetMagickToken(q,&q,extent,token);
2813 point.y=StringToDouble(token,(char **) NULL);
2814 GetMagickToken(q,(const char **) NULL,extent,token);
2816 GetMagickToken(q,&q,extent,token);
2817 primitive_info[i].primitive=primitive_type;
2818 primitive_info[i].point=point;
2819 primitive_info[i].coordinates=0;
2820 primitive_info[i].method=FloodfillMethod;
2822 if (i < (ssize_t) number_points)
2825 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2826 (size_t) number_points,sizeof(*primitive_info));
2827 if (primitive_info == (PrimitiveInfo *) NULL)
2829 (void) ThrowMagickException(exception,GetMagickModule(),
2830 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2834 primitive_info[j].primitive=primitive_type;
2835 primitive_info[j].coordinates=(size_t) x;
2836 primitive_info[j].method=FloodfillMethod;
2837 primitive_info[j].text=(char *) NULL;
2839 Circumscribe primitive within a circle.
2841 bounds.x1=primitive_info[j].point.x;
2842 bounds.y1=primitive_info[j].point.y;
2843 bounds.x2=primitive_info[j].point.x;
2844 bounds.y2=primitive_info[j].point.y;
2845 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2847 point=primitive_info[j+k].point;
2848 if (point.x < bounds.x1)
2850 if (point.y < bounds.y1)
2852 if (point.x > bounds.x2)
2854 if (point.y > bounds.y2)
2858 Speculate how many points our primitive might consume.
2860 length=primitive_info[j].coordinates;
2861 switch (primitive_type)
2863 case RectanglePrimitive:
2868 case RoundRectanglePrimitive:
2875 alpha=bounds.x2-bounds.x1;
2876 beta=bounds.y2-bounds.y1;
2877 radius=hypot((double) alpha,(double) beta);
2879 length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2882 case BezierPrimitive:
2884 if (primitive_info[j].coordinates > 107)
2885 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2886 "TooManyBezierCoordinates","`%s'",token);
2887 length=BezierQuantum*primitive_info[j].coordinates;
2896 GetMagickToken(q,&q,extent,token);
2899 for (s=token; *s != '\0'; s=t)
2904 value=StringToDouble(s,&t);
2913 length=length*BezierQuantum/2;
2916 case CirclePrimitive:
2918 case EllipsePrimitive:
2925 alpha=bounds.x2-bounds.x1;
2926 beta=bounds.y2-bounds.y1;
2927 radius=hypot((double) alpha,(double) beta);
2928 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2934 if ((size_t) (i+length) >= number_points)
2937 Resize based on speculative points required by primitive.
2939 number_points+=length+1;
2940 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2941 (size_t) number_points,sizeof(*primitive_info));
2942 if (primitive_info == (PrimitiveInfo *) NULL)
2944 (void) ThrowMagickException(exception,GetMagickModule(),
2945 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2950 switch (primitive_type)
2952 case PointPrimitive:
2955 if (primitive_info[j].coordinates != 1)
2960 TracePoint(primitive_info+j,primitive_info[j].point);
2961 i=(ssize_t) (j+primitive_info[j].coordinates);
2966 if (primitive_info[j].coordinates != 2)
2971 TraceLine(primitive_info+j,primitive_info[j].point,
2972 primitive_info[j+1].point);
2973 i=(ssize_t) (j+primitive_info[j].coordinates);
2976 case RectanglePrimitive:
2978 if (primitive_info[j].coordinates != 2)
2983 TraceRectangle(primitive_info+j,primitive_info[j].point,
2984 primitive_info[j+1].point);
2985 i=(ssize_t) (j+primitive_info[j].coordinates);
2988 case RoundRectanglePrimitive:
2990 if (primitive_info[j].coordinates != 3)
2995 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2996 primitive_info[j+1].point,primitive_info[j+2].point);
2997 i=(ssize_t) (j+primitive_info[j].coordinates);
3002 if (primitive_info[j].coordinates != 3)
3004 primitive_type=UndefinedPrimitive;
3007 TraceArc(primitive_info+j,primitive_info[j].point,
3008 primitive_info[j+1].point,primitive_info[j+2].point);
3009 i=(ssize_t) (j+primitive_info[j].coordinates);
3012 case EllipsePrimitive:
3014 if (primitive_info[j].coordinates != 3)
3019 TraceEllipse(primitive_info+j,primitive_info[j].point,
3020 primitive_info[j+1].point,primitive_info[j+2].point);
3021 i=(ssize_t) (j+primitive_info[j].coordinates);
3024 case CirclePrimitive:
3026 if (primitive_info[j].coordinates != 2)
3031 TraceCircle(primitive_info+j,primitive_info[j].point,
3032 primitive_info[j+1].point);
3033 i=(ssize_t) (j+primitive_info[j].coordinates);
3036 case PolylinePrimitive:
3038 case PolygonPrimitive:
3040 primitive_info[i]=primitive_info[j];
3041 primitive_info[i].coordinates=0;
3042 primitive_info[j].coordinates++;
3046 case BezierPrimitive:
3048 if (primitive_info[j].coordinates < 3)
3053 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3054 i=(ssize_t) (j+primitive_info[j].coordinates);
3059 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3062 case AlphaPrimitive:
3063 case ColorPrimitive:
3068 if (primitive_info[j].coordinates != 1)
3073 GetMagickToken(q,&q,extent,token);
3074 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3078 primitive_info[j].method=(PaintMethod) method;
3083 if (primitive_info[j].coordinates != 1)
3089 GetMagickToken(q,&q,extent,token);
3090 primitive_info[j].text=AcquireString(token);
3093 case ImagePrimitive:
3095 if (primitive_info[j].coordinates != 2)
3100 GetMagickToken(q,&q,extent,token);
3101 primitive_info[j].text=AcquireString(token);
3105 if (primitive_info == (PrimitiveInfo *) NULL)
3107 if (image->debug != MagickFalse)
3108 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3109 if (status == MagickFalse)
3111 primitive_info[i].primitive=UndefinedPrimitive;
3117 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3119 point=primitive_info[i].point;
3120 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3121 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3122 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3123 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3124 point=primitive_info[i].point;
3125 if (point.x < graphic_context[n]->bounds.x1)
3126 graphic_context[n]->bounds.x1=point.x;
3127 if (point.y < graphic_context[n]->bounds.y1)
3128 graphic_context[n]->bounds.y1=point.y;
3129 if (point.x > graphic_context[n]->bounds.x2)
3130 graphic_context[n]->bounds.x2=point.x;
3131 if (point.y > graphic_context[n]->bounds.y2)
3132 graphic_context[n]->bounds.y2=point.y;
3133 if (primitive_info[i].primitive == ImagePrimitive)
3135 if (i >= (ssize_t) number_points)
3136 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3138 if (graphic_context[n]->render != MagickFalse)
3140 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3141 (LocaleCompare(graphic_context[n]->clip_mask,
3142 graphic_context[n-1]->clip_mask) != 0))
3143 status&=DrawClipPath(image,graphic_context[n],
3144 graphic_context[n]->clip_mask,exception);
3145 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3148 if (primitive_info->text != (char *) NULL)
3149 primitive_info->text=(char *) RelinquishMagickMemory(
3150 primitive_info->text);
3151 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3153 if (proceed == MagickFalse)
3158 if (image->debug != MagickFalse)
3159 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3161 Relinquish resources.
3163 token=DestroyString(token);
3164 if (primitive_info != (PrimitiveInfo *) NULL)
3165 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3166 primitive=DestroyString(primitive);
3167 for ( ; n >= 0; n--)
3168 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3169 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3170 if (status == MagickFalse)
3171 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3173 return(status != 0 ? MagickTrue : MagickFalse);
3177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3181 % D r a w G r a d i e n t I m a g e %
3185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3187 % DrawGradientImage() draws a linear gradient on the image.
3189 % The format of the DrawGradientImage method is:
3191 % MagickBooleanType DrawGradientImage(Image *image,
3192 % const DrawInfo *draw_info,ExceptionInfo *exception)
3194 % A description of each parameter follows:
3196 % o image: the image.
3198 % o draw_info: the draw info.
3200 % o exception: return any errors or warnings in this structure.
3204 static inline double GetStopColorOffset(const GradientInfo *gradient,
3205 const ssize_t x,const ssize_t y)
3207 switch (gradient->type)
3209 case UndefinedGradient:
3210 case LinearGradient:
3225 gradient_vector=(&gradient->gradient_vector);
3226 p.x=gradient_vector->x2-gradient_vector->x1;
3227 p.y=gradient_vector->y2-gradient_vector->y1;
3228 q.x=(double) x-gradient_vector->x1;
3229 q.y=(double) y-gradient_vector->y1;
3230 length=sqrt(q.x*q.x+q.y*q.y);
3231 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3232 gamma=PerceptibleReciprocal(gamma);
3233 scale=p.x*q.x+p.y*q.y;
3234 offset=gamma*scale*length;
3237 case RadialGradient:
3242 if (gradient->spread == RepeatSpread)
3244 v.x=(double) x-gradient->center.x;
3245 v.y=(double) y-gradient->center.y;
3246 return(sqrt(v.x*v.x+v.y*v.y));
3248 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3249 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3250 gradient->angle))))/gradient->radii.x;
3251 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3252 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3253 gradient->angle))))/gradient->radii.y;
3254 return(sqrt(v.x*v.x+v.y*v.y));
3260 static int StopInfoCompare(const void *x,const void *y)
3266 stop_1=(StopInfo *) x;
3267 stop_2=(StopInfo *) y;
3268 if (stop_1->offset > stop_2->offset)
3270 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
3275 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3276 const DrawInfo *draw_info,ExceptionInfo *exception)
3306 Draw linear or radial gradient on image.
3308 assert(image != (Image *) NULL);
3309 assert(image->signature == MagickCoreSignature);
3310 if (image->debug != MagickFalse)
3311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3312 assert(draw_info != (const DrawInfo *) NULL);
3313 gradient=(&draw_info->gradient);
3314 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
3316 gradient_vector=(&gradient->gradient_vector);
3317 point.x=gradient_vector->x2-gradient_vector->x1;
3318 point.y=gradient_vector->y2-gradient_vector->y1;
3319 length=sqrt(point.x*point.x+point.y*point.y);
3320 bounding_box=gradient->bounding_box;
3322 GetPixelInfo(image,&zero);
3323 image_view=AcquireAuthenticCacheView(image,exception);
3324 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3325 #pragma omp parallel for schedule(static,4) shared(status) \
3326 magick_threads(image,image,1,1)
3328 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3348 if (status == MagickFalse)
3350 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3351 if (q == (Quantum *) NULL)
3358 offset=GetStopColorOffset(gradient,0,y);
3359 if (gradient->type != RadialGradient)
3361 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3363 GetPixelInfoPixel(image,q,&pixel);
3364 switch (gradient->spread)
3366 case UndefinedSpread:
3369 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3370 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3372 offset=GetStopColorOffset(gradient,x,y);
3373 if (gradient->type != RadialGradient)
3376 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3377 if (offset < gradient->stops[i].offset)
3379 if ((offset < 0.0) || (i == 0))
3380 composite=gradient->stops[0].color;
3382 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3383 composite=gradient->stops[gradient->number_stops-1].color;
3388 alpha=(offset-gradient->stops[i].offset)/
3389 (gradient->stops[j].offset-gradient->stops[i].offset);
3390 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3391 &gradient->stops[j].color,alpha,&composite);
3397 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3398 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3400 offset=GetStopColorOffset(gradient,x,y);
3401 if (gradient->type != RadialGradient)
3406 if ((ssize_t) fmod(offset,2.0) == 0)
3407 offset=fmod(offset,1.0);
3409 offset=1.0-fmod(offset,1.0);
3410 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3411 if (offset < gradient->stops[i].offset)
3414 composite=gradient->stops[0].color;
3416 if (i == (ssize_t) gradient->number_stops)
3417 composite=gradient->stops[gradient->number_stops-1].color;
3422 alpha=(offset-gradient->stops[i].offset)/
3423 (gradient->stops[j].offset-gradient->stops[i].offset);
3424 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3425 &gradient->stops[j].color,alpha,&composite);
3437 antialias=MagickFalse;
3439 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3440 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3442 offset=GetStopColorOffset(gradient,x,y);
3443 if (gradient->type == LinearGradient)
3445 repeat=fmod(offset,length);
3447 repeat=length-fmod(-repeat,length);
3449 repeat=fmod(offset,length);
3450 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3451 MagickTrue : MagickFalse;
3452 offset=repeat/length;
3456 repeat=fmod(offset,gradient->radius);
3458 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3460 repeat=fmod(offset,gradient->radius);
3461 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3463 offset=repeat/gradient->radius;
3466 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3467 if (offset < gradient->stops[i].offset)
3470 composite=gradient->stops[0].color;
3472 if (i == (ssize_t) gradient->number_stops)
3473 composite=gradient->stops[gradient->number_stops-1].color;
3478 alpha=(offset-gradient->stops[i].offset)/
3479 (gradient->stops[j].offset-gradient->stops[i].offset);
3480 if (antialias != MagickFalse)
3482 if (gradient->type == LinearGradient)
3483 alpha=length-repeat;
3485 alpha=gradient->radius-repeat;
3487 j=(ssize_t) gradient->number_stops-1L;
3489 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3490 &gradient->stops[j].color,alpha,&composite);
3495 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3497 SetPixelViaPixelInfo(image,&pixel,q);
3498 q+=GetPixelChannels(image);
3500 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3503 image_view=DestroyCacheView(image_view);
3508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3512 % D r a w P a t t e r n P a t h %
3516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3518 % DrawPatternPath() draws a pattern.
3520 % The format of the DrawPatternPath method is:
3522 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3523 % const char *name,Image **pattern,ExceptionInfo *exception)
3525 % A description of each parameter follows:
3527 % o image: the image.
3529 % o draw_info: the draw info.
3531 % o name: the pattern name.
3533 % o image: the image.
3535 % o exception: return any errors or warnings in this structure.
3538 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3539 const DrawInfo *draw_info,const char *name,Image **pattern,
3540 ExceptionInfo *exception)
3543 property[MagickPathExtent];
3559 assert(image != (Image *) NULL);
3560 assert(image->signature == MagickCoreSignature);
3561 if (image->debug != MagickFalse)
3562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3563 assert(draw_info != (const DrawInfo *) NULL);
3564 assert(name != (const char *) NULL);
3565 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
3566 path=GetImageArtifact(image,property);
3567 if (path == (const char *) NULL)
3568 return(MagickFalse);
3569 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
3570 geometry=GetImageArtifact(image,property);
3571 if (geometry == (const char *) NULL)
3572 return(MagickFalse);
3573 if ((*pattern) != (Image *) NULL)
3574 *pattern=DestroyImage(*pattern);
3575 image_info=AcquireImageInfo();
3576 image_info->size=AcquireString(geometry);
3577 *pattern=AcquireImage(image_info,exception);
3578 image_info=DestroyImageInfo(image_info);
3579 (void) QueryColorCompliance("#000000ff",AllCompliance,
3580 &(*pattern)->background_color,exception);
3581 (void) SetImageBackgroundColor(*pattern,exception);
3582 if (image->debug != MagickFalse)
3583 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3584 "begin pattern-path %s %s",name,geometry);
3585 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3586 clone_info->fill_pattern=NewImageList();
3587 clone_info->stroke_pattern=NewImageList();
3588 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
3589 type=GetImageArtifact(image,property);
3590 if (type != (const char *) NULL)
3591 clone_info->gradient.type=(GradientType) ParseCommandOption(
3592 MagickGradientOptions,MagickFalse,type);
3593 (void) CloneString(&clone_info->primitive,path);
3594 status=DrawImage(*pattern,clone_info,exception);
3595 clone_info=DestroyDrawInfo(clone_info);
3596 if (image->debug != MagickFalse)
3597 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3606 + D r a w P o l y g o n P r i m i t i v e %
3610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3612 % DrawPolygonPrimitive() draws a polygon on the image.
3614 % The format of the DrawPolygonPrimitive method is:
3616 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3617 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3618 % ExceptionInfo *exception)
3620 % A description of each parameter follows:
3622 % o image: the image.
3624 % o draw_info: the draw info.
3626 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3628 % o exception: return any errors or warnings in this structure.
3632 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3637 assert(polygon_info != (PolygonInfo **) NULL);
3638 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3639 if (polygon_info[i] != (PolygonInfo *) NULL)
3640 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3641 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3642 return(polygon_info);
3645 static PolygonInfo **AcquirePolygonThreadSet(
3646 const PrimitiveInfo *primitive_info)
3649 *magick_restrict path_info;
3660 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3661 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3662 sizeof(*polygon_info));
3663 if (polygon_info == (PolygonInfo **) NULL)
3664 return((PolygonInfo **) NULL);
3665 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3666 path_info=ConvertPrimitiveToPath(primitive_info);
3667 if (path_info == (PathInfo *) NULL)
3668 return(DestroyPolygonThreadSet(polygon_info));
3669 for (i=0; i < (ssize_t) number_threads; i++)
3671 polygon_info[i]=ConvertPathToPolygon(path_info);
3672 if (polygon_info[i] == (PolygonInfo *) NULL)
3673 return(DestroyPolygonThreadSet(polygon_info));
3675 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3676 return(polygon_info);
3679 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3680 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3681 const ssize_t y,double *stroke_alpha)
3692 register const PointInfo
3706 Compute fill & stroke opacity for this (x,y) point.
3710 p=polygon_info->edges;
3711 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3713 if ((double) y <= (p->bounds.y1-mid-0.5))
3715 if ((double) y > (p->bounds.y2+mid+0.5))
3717 (void) DestroyEdge(polygon_info,(size_t) j);
3720 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3721 ((double) x > (p->bounds.x2+mid+0.5)))
3723 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3724 for ( ; i < (ssize_t) p->number_points; i++)
3726 if ((double) y <= (p->points[i-1].y-mid-0.5))
3728 if ((double) y > (p->points[i].y+mid+0.5))
3730 if (p->scanline != (double) y)
3732 p->scanline=(double) y;
3733 p->highwater=(size_t) i;
3736 Compute distance between a point and an edge.
3739 delta.x=(q+1)->x-q->x;
3740 delta.y=(q+1)->y-q->y;
3741 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3744 delta.x=(double) x-q->x;
3745 delta.y=(double) y-q->y;
3746 distance=delta.x*delta.x+delta.y*delta.y;
3750 alpha=delta.x*delta.x+delta.y*delta.y;
3753 delta.x=(double) x-(q+1)->x;
3754 delta.y=(double) y-(q+1)->y;
3755 distance=delta.x*delta.x+delta.y*delta.y;
3760 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3761 distance=alpha*beta*beta;
3765 Compute stroke & subpath opacity.
3768 if (p->ghostline == MagickFalse)
3771 if ((*stroke_alpha < 1.0) &&
3772 (distance <= ((alpha+0.25)*(alpha+0.25))))
3775 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3780 if (distance != 1.0)
3781 beta=sqrt((double) distance);
3783 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3784 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3788 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3790 if (distance <= 0.0)
3800 if (distance != 1.0)
3801 beta=sqrt(distance);
3804 if (subpath_alpha < (alpha*alpha))
3805 subpath_alpha=alpha*alpha;
3809 Compute fill opacity.
3811 if (fill == MagickFalse)
3813 if (subpath_alpha >= 1.0)
3816 Determine winding number.
3819 p=polygon_info->edges;
3820 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3822 if ((double) y <= p->bounds.y1)
3824 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3826 if ((double) x > p->bounds.x2)
3828 winding_number+=p->direction ? 1 : -1;
3831 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3832 for ( ; i < (ssize_t) p->number_points; i++)
3833 if ((double) y <= p->points[i].y)
3836 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3837 winding_number+=p->direction ? 1 : -1;
3839 if (fill_rule != NonZeroRule)
3841 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3845 if (MagickAbsoluteValue(winding_number) != 0)
3847 return(subpath_alpha);
3850 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3851 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3852 ExceptionInfo *exception)
3865 **magick_restrict polygon_info;
3882 Compute bounding box.
3884 assert(image != (Image *) NULL);
3885 assert(image->signature == MagickCoreSignature);
3886 if (image->debug != MagickFalse)
3887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3888 assert(draw_info != (DrawInfo *) NULL);
3889 assert(draw_info->signature == MagickCoreSignature);
3890 assert(primitive_info != (PrimitiveInfo *) NULL);
3891 if (primitive_info->coordinates == 0)
3893 polygon_info=AcquirePolygonThreadSet(primitive_info);
3894 if (polygon_info == (PolygonInfo **) NULL)
3895 return(MagickFalse);
3896 DisableMSCWarning(4127)
3898 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3900 if (image->debug != MagickFalse)
3901 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3902 fill=(primitive_info->method == FillToBorderMethod) ||
3903 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3904 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3905 bounds=polygon_info[0]->edges[0].bounds;
3906 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3908 p=polygon_info[0]->edges+i;
3909 if (p->bounds.x1 < bounds.x1)
3910 bounds.x1=p->bounds.x1;
3911 if (p->bounds.y1 < bounds.y1)
3912 bounds.y1=p->bounds.y1;
3913 if (p->bounds.x2 > bounds.x2)
3914 bounds.x2=p->bounds.x2;
3915 if (p->bounds.y2 > bounds.y2)
3916 bounds.y2=p->bounds.y2;
3918 bounds.x1-=(mid+1.0);
3919 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3920 image->columns ? (double) image->columns-1 : bounds.x1;
3921 bounds.y1-=(mid+1.0);
3922 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3923 image->rows ? (double) image->rows-1 : bounds.y1;
3924 bounds.x2+=(mid+1.0);
3925 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3926 image->columns ? (double) image->columns-1 : bounds.x2;
3927 bounds.y2+=(mid+1.0);
3928 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3929 image->rows ? (double) image->rows-1 : bounds.y2;
3931 image_view=AcquireAuthenticCacheView(image,exception);
3932 if ((primitive_info->coordinates == 1) ||
3933 (polygon_info[0]->number_edges == 0))
3938 start_y=(ssize_t) ceil(bounds.y1-0.5);
3939 stop_y=(ssize_t) floor(bounds.y2+0.5);
3940 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3941 #pragma omp parallel for schedule(static,4) shared(status) \
3942 magick_threads(image,image,1,1)
3944 for (y=start_y; y <= stop_y; y++)
3962 if (status == MagickFalse)
3964 start_x=(ssize_t) ceil(bounds.x1-0.5);
3965 stop_x=(ssize_t) floor(bounds.x2+0.5);
3967 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
3969 if (q == (Quantum *) NULL)
3974 GetPixelInfo(image,&pixel);
3975 for ( ; x <= stop_x; x++)
3977 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3978 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3980 GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
3981 SetPixelViaPixelInfo(image,&pixel,q);
3983 q+=GetPixelChannels(image);
3985 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3986 if (sync == MagickFalse)
3989 image_view=DestroyCacheView(image_view);
3990 polygon_info=DestroyPolygonThreadSet(polygon_info);
3991 if (image->debug != MagickFalse)
3992 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3993 " end draw-polygon");
3997 Draw polygon or line.
3999 if (image->alpha_trait == UndefinedPixelTrait)
4000 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4001 start_y=(ssize_t) ceil(bounds.y1-0.5);
4002 stop_y=(ssize_t) floor(bounds.y2+0.5);
4003 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4004 #pragma omp parallel for schedule(static,4) shared(status) \
4005 magick_threads(image,image,1,1)
4007 for (y=start_y; y <= stop_y; y++)
4010 id = GetOpenMPThreadId();
4030 if (status == MagickFalse)
4032 start_x=(ssize_t) ceil(bounds.x1-0.5);
4033 stop_x=(ssize_t) floor(bounds.x2+0.5);
4034 q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+1),1,
4036 if (q == (Quantum *) NULL)
4041 for (x=start_x; x <= stop_x; x++)
4046 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4048 if (draw_info->stroke_antialias == MagickFalse)
4050 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4051 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4053 GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4054 fill_alpha=fill_alpha*fill_color.alpha;
4055 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4056 GetPixelAlpha(image,q),q);
4057 GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4058 stroke_alpha=stroke_alpha*stroke_color.alpha;
4059 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4060 GetPixelAlpha(image,q),q);
4061 q+=GetPixelChannels(image);
4063 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4066 image_view=DestroyCacheView(image_view);
4067 polygon_info=DestroyPolygonThreadSet(polygon_info);
4068 if (image->debug != MagickFalse)
4069 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4078 % D r a w P r i m i t i v e %
4082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4084 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4086 % The format of the DrawPrimitive method is:
4088 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4089 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4091 % A description of each parameter follows:
4093 % o image: the image.
4095 % o draw_info: the draw info.
4097 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4099 % o exception: return any errors or warnings in this structure.
4103 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4129 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4130 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4131 switch (primitive_info->primitive)
4133 case AlphaPrimitive:
4135 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4136 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4137 methods[primitive_info->method]);
4140 case ColorPrimitive:
4142 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4143 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4144 methods[primitive_info->method]);
4147 case ImagePrimitive:
4149 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4150 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4153 case PointPrimitive:
4155 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4156 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4157 methods[primitive_info->method]);
4162 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4163 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4170 p=primitive_info[0].point;
4173 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4175 point=primitive_info[i].point;
4176 if (coordinates <= 0)
4178 coordinates=(ssize_t) primitive_info[i].coordinates;
4179 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4180 " begin open (%.20g)",(double) coordinates);
4183 point=primitive_info[i].point;
4184 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4185 (fabs(q.y-point.y) >= MagickEpsilon))
4186 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4187 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4189 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4190 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4193 if (coordinates > 0)
4195 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4196 (fabs(p.y-point.y) >= MagickEpsilon))
4197 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4198 (double) coordinates);
4200 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4201 (double) coordinates);
4205 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4206 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4207 ExceptionInfo *exception)
4222 if (image->debug != MagickFalse)
4224 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4225 " begin draw-primitive");
4226 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4227 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4228 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4229 draw_info->affine.tx,draw_info->affine.ty);
4231 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4232 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4233 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4234 (void) SetImageColorspace(image,sRGBColorspace,exception);
4236 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4237 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4238 image_view=AcquireAuthenticCacheView(image,exception);
4239 switch (primitive_info->primitive)
4241 case AlphaPrimitive:
4243 if (image->alpha_trait == UndefinedPixelTrait)
4244 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4245 switch (primitive_info->method)
4256 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4257 if (q == (Quantum *) NULL)
4259 GetFillColor(draw_info,x,y,&pixel,exception);
4260 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4261 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4273 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4275 GetPixelInfo(image,&pixel);
4276 for (y=0; y < (ssize_t) image->rows; y++)
4281 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4283 if (q == (Quantum *) NULL)
4285 for (x=0; x < (ssize_t) image->columns; x++)
4287 GetPixelInfoPixel(image,q,&pixel);
4288 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4290 q+=GetPixelChannels(image);
4293 GetFillColor(draw_info,x,y,&pixel,exception);
4294 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4295 q+=GetPixelChannels(image);
4297 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4298 if (sync == MagickFalse)
4303 case FloodfillMethod:
4304 case FillToBorderMethod:
4312 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4314 if (primitive_info->method == FillToBorderMethod)
4316 target.red=(double) draw_info->border_color.red;
4317 target.green=(double) draw_info->border_color.green;
4318 target.blue=(double) draw_info->border_color.blue;
4320 channel_mask=SetImageChannelMask(image,AlphaChannel);
4321 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4322 primitive_info->method == FloodfillMethod ? MagickFalse :
4323 MagickTrue,exception);
4324 (void) SetImageChannelMask(image,channel_mask);
4335 for (y=0; y < (ssize_t) image->rows; y++)
4340 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4342 if (q == (Quantum *) NULL)
4344 for (x=0; x < (ssize_t) image->columns; x++)
4346 GetFillColor(draw_info,x,y,&pixel,exception);
4347 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4348 q+=GetPixelChannels(image);
4350 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4351 if (sync == MagickFalse)
4359 case ColorPrimitive:
4361 switch (primitive_info->method)
4372 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4373 if (q == (Quantum *) NULL)
4375 GetPixelInfo(image,&pixel);
4376 GetFillColor(draw_info,x,y,&pixel,exception);
4377 SetPixelViaPixelInfo(image,&pixel,q);
4378 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4390 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4392 for (y=0; y < (ssize_t) image->rows; y++)
4397 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4399 if (q == (Quantum *) NULL)
4401 for (x=0; x < (ssize_t) image->columns; x++)
4403 GetPixelInfoPixel(image,q,&pixel);
4404 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4406 q+=GetPixelChannels(image);
4409 GetFillColor(draw_info,x,y,&pixel,exception);
4410 SetPixelViaPixelInfo(image,&pixel,q);
4411 q+=GetPixelChannels(image);
4413 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4414 if (sync == MagickFalse)
4419 case FloodfillMethod:
4420 case FillToBorderMethod:
4425 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4427 if (primitive_info->method == FillToBorderMethod)
4429 target.red=(double) draw_info->border_color.red;
4430 target.green=(double) draw_info->border_color.green;
4431 target.blue=(double) draw_info->border_color.blue;
4433 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4434 primitive_info->method == FloodfillMethod ? MagickFalse :
4435 MagickTrue,exception);
4446 GetPixelInfo(image,&pixel);
4447 for (y=0; y < (ssize_t) image->rows; y++)
4452 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4454 if (q == (Quantum *) NULL)
4456 for (x=0; x < (ssize_t) image->columns; x++)
4458 GetFillColor(draw_info,x,y,&pixel,exception);
4459 SetPixelViaPixelInfo(image,&pixel,q);
4460 q+=GetPixelChannels(image);
4462 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4463 if (sync == MagickFalse)
4471 case ImagePrimitive:
4477 composite_geometry[MagickPathExtent];
4492 if (primitive_info->text == (char *) NULL)
4494 clone_info=AcquireImageInfo();
4495 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4496 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4500 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4502 composite_image=ReadImage(clone_info,exception);
4504 clone_info=DestroyImageInfo(clone_info);
4505 if (composite_image == (Image *) NULL)
4507 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4508 NULL,(void *) NULL);
4509 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4510 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4511 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4512 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4517 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4518 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
4519 composite_image->filter=image->filter;
4520 (void) TransformImage(&composite_image,(char *) NULL,
4521 composite_geometry,exception);
4523 if (composite_image->alpha_trait == UndefinedPixelTrait)
4524 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4526 if (draw_info->alpha != OpaqueAlpha)
4527 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4528 SetGeometry(image,&geometry);
4529 image->gravity=draw_info->gravity;
4532 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4533 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4534 composite_image->rows,(double) geometry.x,(double) geometry.y);
4535 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4536 affine=draw_info->affine;
4537 affine.tx=(double) geometry.x;
4538 affine.ty=(double) geometry.y;
4539 composite_image->interpolate=image->interpolate;
4540 if (draw_info->compose == OverCompositeOp)
4541 (void) DrawAffineImage(image,composite_image,&affine,exception);
4543 (void) CompositeImage(image,composite_image,draw_info->compose,
4544 MagickTrue,geometry.x,geometry.y,exception);
4545 composite_image=DestroyImage(composite_image);
4548 case PointPrimitive:
4556 if ((y < 0) || (y >= (ssize_t) image->rows))
4558 if ((x < 0) || (x >= (ssize_t) image->columns))
4560 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4561 if (q == (Quantum *) NULL)
4563 GetFillColor(draw_info,x,y,&fill_color,exception);
4564 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4565 (double) GetPixelAlpha(image,q),q);
4566 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4572 geometry[MagickPathExtent];
4577 if (primitive_info->text == (char *) NULL)
4579 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4580 (void) CloneString(&clone_info->text,primitive_info->text);
4581 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4582 primitive_info->point.x,primitive_info->point.y);
4583 (void) CloneString(&clone_info->geometry,geometry);
4584 status&=AnnotateImage(image,clone_info,exception);
4585 clone_info=DestroyDrawInfo(clone_info);
4597 if (IsEventLogging() != MagickFalse)
4598 LogPrimitiveInfo(primitive_info);
4599 scale=ExpandAffine(&draw_info->affine);
4600 if ((draw_info->dash_pattern != (double *) NULL) &&
4601 (draw_info->dash_pattern[0] != 0.0) &&
4602 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4603 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4608 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4609 clone_info->stroke_width=0.0;
4610 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4611 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4613 clone_info=DestroyDrawInfo(clone_info);
4614 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4617 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4619 ((draw_info->stroke.alpha != (Quantum) TransparentAlpha) ||
4620 (draw_info->stroke_pattern != (Image *) NULL)))
4626 Draw strokes while respecting line cap/join attributes.
4628 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4630 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4631 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4632 MagickTrue : MagickFalse;
4633 i=(ssize_t) primitive_info[0].coordinates;
4634 if ((((draw_info->linecap == RoundCap) ||
4635 (closed_path != MagickFalse)) &&
4636 (draw_info->linejoin == RoundJoin)) ||
4637 (primitive_info[i].primitive != UndefinedPrimitive))
4639 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4643 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4644 clone_info->stroke_width=0.0;
4645 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4646 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4648 clone_info=DestroyDrawInfo(clone_info);
4649 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4652 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4656 image_view=DestroyCacheView(image_view);
4657 if (image->debug != MagickFalse)
4658 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4659 return(status != 0 ? MagickTrue : MagickFalse);
4663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4667 + D r a w S t r o k e P o l y g o n %
4671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4673 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4674 % the image while respecting the line cap and join attributes.
4676 % The format of the DrawStrokePolygon method is:
4678 % MagickBooleanType DrawStrokePolygon(Image *image,
4679 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4681 % A description of each parameter follows:
4683 % o image: the image.
4685 % o draw_info: the draw info.
4687 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4692 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4693 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4701 for (i=0; i < 4; i++)
4702 linecap[i]=(*primitive_info);
4703 linecap[0].coordinates=4;
4704 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4705 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4706 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4707 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4708 linecap[4].primitive=UndefinedPrimitive;
4709 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4712 static MagickBooleanType DrawStrokePolygon(Image *image,
4713 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4714 ExceptionInfo *exception)
4728 register const PrimitiveInfo
4733 Draw stroked polygon.
4735 if (image->debug != MagickFalse)
4736 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4737 " begin draw-stroke-polygon");
4738 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4739 clone_info->fill=draw_info->stroke;
4740 if (clone_info->fill_pattern != (Image *) NULL)
4741 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4742 if (clone_info->stroke_pattern != (Image *) NULL)
4743 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4744 MagickTrue,exception);
4745 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4746 clone_info->stroke_width=0.0;
4747 clone_info->fill_rule=NonZeroRule;
4749 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4751 stroke_polygon=TraceStrokePolygon(draw_info,p);
4752 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4755 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4756 q=p+p->coordinates-1;
4757 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4758 MagickTrue : MagickFalse;
4759 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4761 DrawRoundLinecap(image,draw_info,p,exception);
4762 DrawRoundLinecap(image,draw_info,q,exception);
4765 clone_info=DestroyDrawInfo(clone_info);
4766 if (image->debug != MagickFalse)
4767 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4768 " end draw-stroke-polygon");
4769 return(status != 0 ? MagickTrue : MagickFalse);
4773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4777 % G e t A f f i n e M a t r i x %
4781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4783 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4786 % The format of the GetAffineMatrix method is:
4788 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4790 % A description of each parameter follows:
4792 % o affine_matrix: the affine matrix.
4795 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4798 assert(affine_matrix != (AffineMatrix *) NULL);
4799 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4800 affine_matrix->sx=1.0;
4801 affine_matrix->sy=1.0;
4805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4809 + G e t D r a w I n f o %
4813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4815 % GetDrawInfo() initializes draw_info to default values from image_info.
4817 % The format of the GetDrawInfo method is:
4819 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4821 % A description of each parameter follows:
4823 % o image_info: the image info..
4825 % o draw_info: the draw info.
4828 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4840 Initialize draw attributes.
4842 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4843 assert(draw_info != (DrawInfo *) NULL);
4844 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4845 clone_info=CloneImageInfo(image_info);
4846 GetAffineMatrix(&draw_info->affine);
4847 exception=AcquireExceptionInfo();
4848 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4850 (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4852 draw_info->stroke_width=1.0;
4853 draw_info->alpha=OpaqueAlpha;
4854 draw_info->fill_rule=EvenOddRule;
4855 draw_info->linecap=ButtCap;
4856 draw_info->linejoin=MiterJoin;
4857 draw_info->miterlimit=10;
4858 draw_info->decorate=NoDecoration;
4859 draw_info->pointsize=12.0;
4860 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4861 draw_info->compose=OverCompositeOp;
4862 draw_info->render=MagickTrue;
4863 draw_info->debug=IsEventLogging();
4864 draw_info->stroke_antialias=clone_info->antialias;
4865 if (clone_info->font != (char *) NULL)
4866 draw_info->font=AcquireString(clone_info->font);
4867 if (clone_info->density != (char *) NULL)
4868 draw_info->density=AcquireString(clone_info->density);
4869 draw_info->text_antialias=clone_info->antialias;
4870 if (clone_info->pointsize != 0.0)
4871 draw_info->pointsize=clone_info->pointsize;
4872 draw_info->border_color=clone_info->border_color;
4873 if (clone_info->server_name != (char *) NULL)
4874 draw_info->server_name=AcquireString(clone_info->server_name);
4875 option=GetImageOption(clone_info,"direction");
4876 if (option != (const char *) NULL)
4877 draw_info->direction=(DirectionType) ParseCommandOption(
4878 MagickDirectionOptions,MagickFalse,option);
4880 draw_info->direction=UndefinedDirection;
4881 option=GetImageOption(clone_info,"encoding");
4882 if (option != (const char *) NULL)
4883 (void) CloneString(&draw_info->encoding,option);
4884 option=GetImageOption(clone_info,"family");
4885 if (option != (const char *) NULL)
4886 (void) CloneString(&draw_info->family,option);
4887 option=GetImageOption(clone_info,"fill");
4888 if (option != (const char *) NULL)
4889 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4891 option=GetImageOption(clone_info,"gravity");
4892 if (option != (const char *) NULL)
4893 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4894 MagickFalse,option);
4895 option=GetImageOption(clone_info,"interline-spacing");
4896 if (option != (const char *) NULL)
4897 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4898 option=GetImageOption(clone_info,"interword-spacing");
4899 if (option != (const char *) NULL)
4900 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4901 option=GetImageOption(clone_info,"kerning");
4902 if (option != (const char *) NULL)
4903 draw_info->kerning=StringToDouble(option,(char **) NULL);
4904 option=GetImageOption(clone_info,"stroke");
4905 if (option != (const char *) NULL)
4906 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4908 option=GetImageOption(clone_info,"strokewidth");
4909 if (option != (const char *) NULL)
4910 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4911 option=GetImageOption(clone_info,"style");
4912 if (option != (const char *) NULL)
4913 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
4914 MagickFalse,option);
4915 option=GetImageOption(clone_info,"undercolor");
4916 if (option != (const char *) NULL)
4917 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4919 option=GetImageOption(clone_info,"weight");
4920 if (option != (const char *) NULL)
4925 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
4927 weight=StringToUnsignedLong(option);
4928 draw_info->weight=(size_t) weight;
4930 exception=DestroyExceptionInfo(exception);
4931 draw_info->signature=MagickCoreSignature;
4932 clone_info=DestroyImageInfo(clone_info);
4936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4940 + P e r m u t a t e %
4944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4946 % Permutate() returns the permuation of the (n,k).
4948 % The format of the Permutate method is:
4950 % void Permutate(ssize_t n,ssize_t k)
4952 % A description of each parameter follows:
4960 static inline double Permutate(const ssize_t n,const ssize_t k)
4969 for (i=k+1; i <= n; i++)
4971 for (i=1; i <= (n-k); i++)
4977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4981 + T r a c e P r i m i t i v e %
4985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4987 % TracePrimitive is a collection of methods for generating graphic
4988 % primitives such as arcs, ellipses, paths, etc.
4992 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4993 const PointInfo end,const PointInfo degrees)
4999 center.x=0.5*(end.x+start.x);
5000 center.y=0.5*(end.y+start.y);
5001 radii.x=fabs(center.x-start.x);
5002 radii.y=fabs(center.y-start.y);
5003 TraceEllipse(primitive_info,center,radii,degrees);
5006 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
5007 const PointInfo end,const PointInfo arc,const double angle,
5008 const MagickBooleanType large_arc,const MagickBooleanType sweep)
5027 register PrimitiveInfo
5036 if ((start.x == end.x) && (start.y == end.y))
5038 TracePoint(primitive_info,end);
5041 radii.x=fabs(arc.x);
5042 radii.y=fabs(arc.y);
5043 if ((radii.x == 0.0) || (radii.y == 0.0))
5045 TraceLine(primitive_info,start,end);
5048 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5049 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5050 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5051 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5052 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5054 if (delta < MagickEpsilon)
5056 TraceLine(primitive_info,start,end);
5061 radii.x*=sqrt((double) delta);
5062 radii.y*=sqrt((double) delta);
5064 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5065 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5066 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5067 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5068 alpha=points[1].x-points[0].x;
5069 beta=points[1].y-points[0].y;
5070 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5075 factor=sqrt((double) factor);
5076 if (sweep == large_arc)
5079 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5080 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5081 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5082 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5083 if ((theta < 0.0) && (sweep != MagickFalse))
5084 theta+=(double) (2.0*MagickPI);
5086 if ((theta > 0.0) && (sweep == MagickFalse))
5087 theta-=(double) (2.0*MagickPI);
5088 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5091 for (i=0; i < (ssize_t) arc_segments; i++)
5093 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5094 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5095 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5096 sin(fmod((double) beta,DegreesToRadians(360.0)));
5097 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5098 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5099 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5100 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5101 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5102 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5103 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5104 theta/arc_segments),DegreesToRadians(360.0))));
5105 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5106 theta/arc_segments),DegreesToRadians(360.0))));
5107 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5108 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5109 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5110 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5111 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5112 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5113 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5115 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5117 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5119 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5121 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5123 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5125 if (i == (ssize_t) (arc_segments-1))
5130 primitive_info->coordinates=(size_t) (p-primitive_info);
5131 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5133 p->primitive=primitive_info->primitive;
5138 static void TraceBezier(PrimitiveInfo *primitive_info,
5139 const size_t number_coordinates)
5151 register PrimitiveInfo
5163 Allocate coeficients.
5165 quantum=number_coordinates;
5166 for (i=0; i < (ssize_t) number_coordinates; i++)
5168 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5170 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5171 if (alpha > (double) quantum)
5172 quantum=(size_t) alpha;
5173 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5174 if (alpha > (double) quantum)
5175 quantum=(size_t) alpha;
5178 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5179 (double) BezierQuantum);
5180 control_points=quantum*number_coordinates;
5181 coefficients=(double *) AcquireQuantumMemory((size_t)
5182 number_coordinates,sizeof(*coefficients));
5183 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5185 if ((coefficients == (double *) NULL) ||
5186 (points == (PointInfo *) NULL))
5187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5189 Compute bezier points.
5191 end=primitive_info[number_coordinates-1].point;
5192 for (i=0; i < (ssize_t) number_coordinates; i++)
5193 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5195 for (i=0; i < (ssize_t) control_points; i++)
5200 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5201 for (j=0; j < (ssize_t) number_coordinates; j++)
5203 point.x+=alpha*coefficients[j]*p->point.x;
5204 point.y+=alpha*coefficients[j]*p->point.y;
5205 alpha*=weight/(1.0-weight);
5209 weight+=1.0/control_points;
5212 Bezier curves are just short segmented polys.
5215 for (i=0; i < (ssize_t) control_points; i++)
5217 TracePoint(p,points[i]);
5222 primitive_info->coordinates=(size_t) (p-primitive_info);
5223 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5225 p->primitive=primitive_info->primitive;
5228 points=(PointInfo *) RelinquishMagickMemory(points);
5229 coefficients=(double *) RelinquishMagickMemory(coefficients);
5232 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5233 const PointInfo end)
5244 alpha=end.x-start.x;
5246 radius=hypot((double) alpha,(double) beta);
5247 offset.x=(double) radius;
5248 offset.y=(double) radius;
5251 TraceEllipse(primitive_info,start,offset,degrees);
5254 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5255 const PointInfo stop,const PointInfo degrees)
5266 register PrimitiveInfo
5273 Ellipses are just short segmented polys.
5275 if ((stop.x == 0.0) && (stop.y == 0.0))
5277 TracePoint(primitive_info,start);
5280 delta=2.0/MagickMax(stop.x,stop.y);
5281 step=(double) (MagickPI/8.0);
5282 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5283 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5284 angle.x=DegreesToRadians(degrees.x);
5286 while (y < degrees.x)
5288 angle.y=(double) DegreesToRadians(y);
5289 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5291 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5292 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5293 TracePoint(p,point);
5296 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5297 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5298 TracePoint(p,point);
5300 primitive_info->coordinates=(size_t) (p-primitive_info);
5301 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5303 p->primitive=primitive_info->primitive;
5308 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5309 const PointInfo end)
5311 TracePoint(primitive_info,start);
5312 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5313 (fabs(start.y-end.y) < MagickEpsilon))
5315 primitive_info->primitive=PointPrimitive;
5316 primitive_info->coordinates=1;
5319 TracePoint(primitive_info+1,end);
5320 (primitive_info+1)->primitive=primitive_info->primitive;
5321 primitive_info->coordinates=2;
5324 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5327 token[MagickPathExtent];
5342 points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5349 register PrimitiveInfo
5360 number_coordinates=0;
5362 primitive_type=primitive_info->primitive;
5364 for (p=path; *p != '\0'; )
5366 while (isspace((int) ((unsigned char) *p)) != 0)
5370 last_attribute=attribute;
5371 attribute=(int) (*p++);
5392 GetMagickToken(p,&p,MagickPathExtent,token);
5394 GetMagickToken(p,&p,MagickPathExtent,token);
5395 arc.x=StringToDouble(token,(char **) NULL);
5396 GetMagickToken(p,&p,MagickPathExtent,token);
5398 GetMagickToken(p,&p,MagickPathExtent,token);
5399 arc.y=StringToDouble(token,(char **) NULL);
5400 GetMagickToken(p,&p,MagickPathExtent,token);
5402 GetMagickToken(p,&p,MagickPathExtent,token);
5403 angle=StringToDouble(token,(char **) NULL);
5404 GetMagickToken(p,&p,MagickPathExtent,token);
5406 GetMagickToken(p,&p,MagickPathExtent,token);
5407 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5408 GetMagickToken(p,&p,MagickPathExtent,token);
5410 GetMagickToken(p,&p,MagickPathExtent,token);
5411 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5412 GetMagickToken(p,&p,MagickPathExtent,token);
5414 GetMagickToken(p,&p,MagickPathExtent,token);
5415 x=StringToDouble(token,(char **) NULL);
5416 GetMagickToken(p,&p,MagickPathExtent,token);
5418 GetMagickToken(p,&p,MagickPathExtent,token);
5419 y=StringToDouble(token,(char **) NULL);
5420 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5421 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5422 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5425 while (isspace((int) ((unsigned char) *p)) != 0)
5429 } while (IsPoint(p) != MagickFalse);
5436 Compute bezier points.
5441 for (i=1; i < 4; i++)
5443 GetMagickToken(p,&p,MagickPathExtent,token);
5445 GetMagickToken(p,&p,MagickPathExtent,token);
5446 x=StringToDouble(token,(char **) NULL);
5447 GetMagickToken(p,&p,MagickPathExtent,token);
5449 GetMagickToken(p,&p,MagickPathExtent,token);
5450 y=StringToDouble(token,(char **) NULL);
5451 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5452 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5455 for (i=0; i < 4; i++)
5456 (q+i)->point=points[i];
5460 } while (IsPoint(p) != MagickFalse);
5468 GetMagickToken(p,&p,MagickPathExtent,token);
5470 GetMagickToken(p,&p,MagickPathExtent,token);
5471 x=StringToDouble(token,(char **) NULL);
5472 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5473 TracePoint(q,point);
5475 } while (IsPoint(p) != MagickFalse);
5483 GetMagickToken(p,&p,MagickPathExtent,token);
5485 GetMagickToken(p,&p,MagickPathExtent,token);
5486 x=StringToDouble(token,(char **) NULL);
5487 GetMagickToken(p,&p,MagickPathExtent,token);
5489 GetMagickToken(p,&p,MagickPathExtent,token);
5490 y=StringToDouble(token,(char **) NULL);
5491 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5492 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5493 TracePoint(q,point);
5495 } while (IsPoint(p) != MagickFalse);
5501 if (q != primitive_info)
5503 primitive_info->coordinates=(size_t) (q-primitive_info);
5504 number_coordinates+=primitive_info->coordinates;
5510 GetMagickToken(p,&p,MagickPathExtent,token);
5512 GetMagickToken(p,&p,MagickPathExtent,token);
5513 x=StringToDouble(token,(char **) NULL);
5514 GetMagickToken(p,&p,MagickPathExtent,token);
5516 GetMagickToken(p,&p,MagickPathExtent,token);
5517 y=StringToDouble(token,(char **) NULL);
5518 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5519 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5523 TracePoint(q,point);
5525 if ((i != 0) && (attribute == (int) 'M'))
5527 TracePoint(q,point);
5530 } while (IsPoint(p) != MagickFalse);
5537 Compute bezier points.
5542 for (i=1; i < 3; i++)
5544 GetMagickToken(p,&p,MagickPathExtent,token);
5546 GetMagickToken(p,&p,MagickPathExtent,token);
5547 x=StringToDouble(token,(char **) NULL);
5548 GetMagickToken(p,&p,MagickPathExtent,token);
5550 GetMagickToken(p,&p,MagickPathExtent,token);
5551 y=StringToDouble(token,(char **) NULL);
5554 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5555 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5558 for (i=0; i < 3; i++)
5559 (q+i)->point=points[i];
5563 } while (IsPoint(p) != MagickFalse);
5570 Compute bezier points.
5574 points[0]=points[3];
5575 points[1].x=2.0*points[3].x-points[2].x;
5576 points[1].y=2.0*points[3].y-points[2].y;
5577 for (i=2; i < 4; i++)
5579 GetMagickToken(p,&p,MagickPathExtent,token);
5581 GetMagickToken(p,&p,MagickPathExtent,token);
5582 x=StringToDouble(token,(char **) NULL);
5583 GetMagickToken(p,&p,MagickPathExtent,token);
5585 GetMagickToken(p,&p,MagickPathExtent,token);
5586 y=StringToDouble(token,(char **) NULL);
5589 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5590 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5593 if (strchr("CcSs",last_attribute) == (char *) NULL)
5598 for (i=0; i < 4; i++)
5599 (q+i)->point=points[i];
5603 } while (IsPoint(p) != MagickFalse);
5610 Compute bezier points.
5614 points[0]=points[2];
5615 points[1].x=2.0*points[2].x-points[1].x;
5616 points[1].y=2.0*points[2].y-points[1].y;
5617 for (i=2; i < 3; i++)
5619 GetMagickToken(p,&p,MagickPathExtent,token);
5621 GetMagickToken(p,&p,MagickPathExtent,token);
5622 x=StringToDouble(token,(char **) NULL);
5623 GetMagickToken(p,&p,MagickPathExtent,token);
5625 GetMagickToken(p,&p,MagickPathExtent,token);
5626 y=StringToDouble(token,(char **) NULL);
5627 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5628 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5631 if (strchr("QqTt",last_attribute) == (char *) NULL)
5636 for (i=0; i < 3; i++)
5637 (q+i)->point=points[i];
5641 } while (IsPoint(p) != MagickFalse);
5649 GetMagickToken(p,&p,MagickPathExtent,token);
5651 GetMagickToken(p,&p,MagickPathExtent,token);
5652 y=StringToDouble(token,(char **) NULL);
5653 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5654 TracePoint(q,point);
5656 } while (IsPoint(p) != MagickFalse);
5663 TracePoint(q,point);
5665 primitive_info->coordinates=(size_t) (q-primitive_info);
5666 number_coordinates+=primitive_info->coordinates;
5673 if (isalpha((int) ((unsigned char) attribute)) != 0)
5674 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5680 primitive_info->coordinates=(size_t) (q-primitive_info);
5681 number_coordinates+=primitive_info->coordinates;
5682 for (i=0; i < (ssize_t) number_coordinates; i++)
5685 q->primitive=primitive_type;
5687 q->method=FillToBorderMethod;
5690 return(number_coordinates);
5693 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5694 const PointInfo end)
5699 register PrimitiveInfo
5706 TracePoint(p,start);
5710 TracePoint(p,point);
5716 TracePoint(p,point);
5718 TracePoint(p,start);
5720 primitive_info->coordinates=(size_t) (p-primitive_info);
5721 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5723 p->primitive=primitive_info->primitive;
5728 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5729 const PointInfo start,const PointInfo end,PointInfo arc)
5736 register PrimitiveInfo
5743 offset.x=fabs(end.x-start.x);
5744 offset.y=fabs(end.y-start.y);
5745 if (arc.x > (0.5*offset.x))
5747 if (arc.y > (0.5*offset.y))
5749 point.x=start.x+offset.x-arc.x;
5750 point.y=start.y+arc.y;
5753 TraceEllipse(p,point,arc,degrees);
5755 point.x=start.x+offset.x-arc.x;
5756 point.y=start.y+offset.y-arc.y;
5759 TraceEllipse(p,point,arc,degrees);
5761 point.x=start.x+arc.x;
5762 point.y=start.y+offset.y-arc.y;
5765 TraceEllipse(p,point,arc,degrees);
5767 point.x=start.x+arc.x;
5768 point.y=start.y+arc.y;
5771 TraceEllipse(p,point,arc,degrees);
5773 TracePoint(p,primitive_info->point);
5775 primitive_info->coordinates=(size_t) (p-primitive_info);
5776 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5778 p->primitive=primitive_info->primitive;
5783 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5784 const size_t number_vertices,const double offset)
5801 for (i=1; i < (ssize_t) number_vertices; i++)
5803 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5804 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5805 if ((fabs((double) dx) >= MagickEpsilon) ||
5806 (fabs((double) dy) >= MagickEpsilon))
5809 if (i == (ssize_t) number_vertices)
5810 i=(ssize_t) number_vertices-1L;
5811 distance=hypot((double) dx,(double) dy);
5812 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5813 dx*(distance+offset)/distance);
5814 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5815 dy*(distance+offset)/distance);
5816 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5818 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5819 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5820 if ((fabs((double) dx) >= MagickEpsilon) ||
5821 (fabs((double) dy) >= MagickEpsilon))
5824 distance=hypot((double) dx,(double) dy);
5825 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5826 dx*(distance+offset)/distance);
5827 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5828 dy*(distance+offset)/distance);
5831 static inline double DrawEpsilonReciprocal(const double x)
5833 #define DrawEpsilon (1.0e-6)
5835 double sign = x < 0.0 ? -1.0 : 1.0;
5836 return((sign*x) >= DrawEpsilon ? 1.0/x : sign*(1.0/DrawEpsilon));
5839 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5840 const PrimitiveInfo *primitive_info)
5842 typedef struct _LineSegment
5894 number_vertices=primitive_info->coordinates;
5895 max_strokes=2*number_vertices+6*BezierQuantum+360;
5896 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5898 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5900 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5901 number_vertices+2UL,sizeof(*polygon_primitive));
5902 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5903 (polygon_primitive == (PrimitiveInfo *) NULL))
5904 return((PrimitiveInfo *) NULL);
5905 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5906 number_vertices*sizeof(*polygon_primitive));
5908 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5909 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5910 MagickTrue : MagickFalse;
5911 if ((draw_info->linejoin == RoundJoin) ||
5912 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5914 polygon_primitive[number_vertices]=primitive_info[1];
5917 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5919 Compute the slope for the first line segment, p.
5923 for (n=1; n < (ssize_t) number_vertices; n++)
5925 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5926 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5927 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5930 if (n == (ssize_t) number_vertices)
5931 n=(ssize_t) number_vertices-1L;
5932 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5933 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
5934 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5935 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
5937 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5938 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5939 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5940 offset.y=(double) (offset.x*inverse_slope.p);
5941 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5943 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5944 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5945 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5946 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5947 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5948 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5949 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5950 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5954 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5955 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5956 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5957 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5958 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5959 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5960 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5961 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5964 Create strokes for the line join attribute: bevel, miter, round.
5968 path_q[p++]=box_q[0];
5969 path_p[q++]=box_p[0];
5970 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5973 Compute the slope for this line segment, q.
5975 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5976 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5977 dot_product=dx.q*dx.q+dy.q*dy.q;
5978 if (dot_product < 0.25)
5980 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5981 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
5982 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5983 offset.y=(double) (offset.x*inverse_slope.q);
5984 dot_product=dy.q*offset.x-dx.q*offset.y;
5985 if (dot_product > 0.0)
5987 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5988 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5989 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5990 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5991 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5992 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5993 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5994 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5998 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5999 box_p[2].y=polygon_primitive[n].point.y+offset.y;
6000 box_p[3].x=polygon_primitive[i].point.x+offset.x;
6001 box_p[3].y=polygon_primitive[i].point.y+offset.y;
6002 box_q[2].x=polygon_primitive[n].point.x-offset.x;
6003 box_q[2].y=polygon_primitive[n].point.y-offset.y;
6004 box_q[3].x=polygon_primitive[i].point.x-offset.x;
6005 box_q[3].y=polygon_primitive[i].point.y-offset.y;
6007 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
6014 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
6015 box_p[3].y)/(slope.p-slope.q));
6016 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
6017 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
6018 box_q[3].y)/(slope.p-slope.q));
6019 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
6021 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
6023 max_strokes+=6*BezierQuantum+360;
6024 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
6026 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
6028 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
6030 polygon_primitive=(PrimitiveInfo *)
6031 RelinquishMagickMemory(polygon_primitive);
6032 return((PrimitiveInfo *) NULL);
6035 dot_product=dx.q*dy.p-dx.p*dy.q;
6036 if (dot_product <= 0.0)
6037 switch (draw_info->linejoin)
6041 path_q[q++]=box_q[1];
6042 path_q[q++]=box_q[2];
6043 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6044 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6045 if (dot_product <= miterlimit)
6046 path_p[p++]=box_p[4];
6049 path_p[p++]=box_p[1];
6050 path_p[p++]=box_p[2];
6056 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6057 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6058 if (dot_product <= miterlimit)
6060 path_q[q++]=box_q[4];
6061 path_p[p++]=box_p[4];
6065 path_q[q++]=box_q[1];
6066 path_q[q++]=box_q[2];
6067 path_p[p++]=box_p[1];
6068 path_p[p++]=box_p[2];
6074 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6075 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6076 if (dot_product <= miterlimit)
6077 path_p[p++]=box_p[4];
6080 path_p[p++]=box_p[1];
6081 path_p[p++]=box_p[2];
6083 center=polygon_primitive[n].point;
6084 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6085 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6086 if (theta.q < theta.p)
6087 theta.q+=(double) (2.0*MagickPI);
6088 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6089 (2.0*sqrt((double) (1.0/mid)))));
6090 path_q[q].x=box_q[1].x;
6091 path_q[q].y=box_q[1].y;
6093 for (j=1; j < (ssize_t) arc_segments; j++)
6095 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6096 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6097 (theta.p+delta_theta),DegreesToRadians(360.0))));
6098 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6099 (theta.p+delta_theta),DegreesToRadians(360.0))));
6102 path_q[q++]=box_q[2];
6109 switch (draw_info->linejoin)
6113 path_p[p++]=box_p[1];
6114 path_p[p++]=box_p[2];
6115 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6116 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6117 if (dot_product <= miterlimit)
6118 path_q[q++]=box_q[4];
6121 path_q[q++]=box_q[1];
6122 path_q[q++]=box_q[2];
6128 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6129 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6130 if (dot_product <= miterlimit)
6132 path_q[q++]=box_q[4];
6133 path_p[p++]=box_p[4];
6137 path_q[q++]=box_q[1];
6138 path_q[q++]=box_q[2];
6139 path_p[p++]=box_p[1];
6140 path_p[p++]=box_p[2];
6146 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6147 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6148 if (dot_product <= miterlimit)
6149 path_q[q++]=box_q[4];
6152 path_q[q++]=box_q[1];
6153 path_q[q++]=box_q[2];
6155 center=polygon_primitive[n].point;
6156 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6157 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6158 if (theta.p < theta.q)
6159 theta.p+=(double) (2.0*MagickPI);
6160 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6161 (2.0*sqrt((double) (1.0/mid)))));
6162 path_p[p++]=box_p[1];
6163 for (j=1; j < (ssize_t) arc_segments; j++)
6165 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
6166 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6167 (theta.p+delta_theta),DegreesToRadians(360.0))));
6168 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6169 (theta.p+delta_theta),DegreesToRadians(360.0))));
6172 path_p[p++]=box_p[2];
6179 inverse_slope.p=inverse_slope.q;
6188 path_p[p++]=box_p[1];
6189 path_q[q++]=box_q[1];
6191 Trace stroked polygon.
6193 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6194 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6195 if (stroke_polygon != (PrimitiveInfo *) NULL)
6197 for (i=0; i < (ssize_t) p; i++)
6199 stroke_polygon[i]=polygon_primitive[0];
6200 stroke_polygon[i].point=path_p[i];
6202 if (closed_path != MagickFalse)
6204 stroke_polygon[i]=polygon_primitive[0];
6205 stroke_polygon[i].point=stroke_polygon[0].point;
6208 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6210 stroke_polygon[i]=polygon_primitive[0];
6211 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6213 if (closed_path != MagickFalse)
6215 stroke_polygon[i]=polygon_primitive[0];
6216 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6219 stroke_polygon[i]=polygon_primitive[0];
6220 stroke_polygon[i].point=stroke_polygon[0].point;
6222 stroke_polygon[i].primitive=UndefinedPrimitive;
6223 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6225 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6226 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6227 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6228 return(stroke_polygon);