2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
53 #include "MagickCore/cache-view.h"
54 #include "MagickCore/color.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/constitute.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/draw-private.h"
61 #include "MagickCore/enhance.h"
62 #include "MagickCore/exception.h"
63 #include "MagickCore/exception-private.h"
64 #include "MagickCore/gem.h"
65 #include "MagickCore/geometry.h"
66 #include "MagickCore/image-private.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/log.h"
69 #include "MagickCore/monitor.h"
70 #include "MagickCore/monitor-private.h"
71 #include "MagickCore/option.h"
72 #include "MagickCore/paint.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/property.h"
75 #include "MagickCore/resample.h"
76 #include "MagickCore/resample-private.h"
77 #include "MagickCore/string_.h"
78 #include "MagickCore/string-private.h"
79 #include "MagickCore/thread-private.h"
80 #include "MagickCore/token.h"
81 #include "MagickCore/transform.h"
82 #include "MagickCore/utility.h"
87 #define BezierQuantum 200
92 typedef struct _EdgeInfo
116 typedef struct _ElementInfo
126 typedef struct _PolygonInfo
144 typedef struct _PathInfo
154 Forward declarations.
156 static MagickBooleanType
157 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
161 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
164 TracePath(PrimitiveInfo *,const char *);
167 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
168 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
169 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
170 TraceBezier(PrimitiveInfo *,const size_t),
171 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
172 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
173 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
174 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
177 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % A c q u i r e D r a w I n f o %
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
192 % The format of the AcquireDrawInfo method is:
194 % DrawInfo *AcquireDrawInfo(void)
197 MagickExport DrawInfo *AcquireDrawInfo(void)
202 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
203 if (draw_info == (DrawInfo *) NULL)
204 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
205 GetDrawInfo((ImageInfo *) NULL,draw_info);
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 % C l o n e D r a w I n f o %
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
221 % is specified, a new draw_info structure is created initialized to
222 % default values, according to the given image_info.
224 % The format of the CloneDrawInfo method is:
226 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
227 % const DrawInfo *draw_info)
229 % A description of each parameter follows:
231 % o image_info: the image info.
233 % o draw_info: the draw info.
236 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
237 const DrawInfo *draw_info)
245 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
246 if (clone_info == (DrawInfo *) NULL)
247 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
248 GetDrawInfo(image_info,clone_info);
249 if (draw_info == (DrawInfo *) NULL)
251 exception=AcquireExceptionInfo();
252 (void) CloneString(&clone_info->primitive,draw_info->primitive);
253 (void) CloneString(&clone_info->geometry,draw_info->geometry);
254 clone_info->viewbox=draw_info->viewbox;
255 clone_info->affine=draw_info->affine;
256 clone_info->gravity=draw_info->gravity;
257 clone_info->fill=draw_info->fill;
258 clone_info->stroke=draw_info->stroke;
259 clone_info->stroke_width=draw_info->stroke_width;
260 if (draw_info->fill_pattern != (Image *) NULL)
261 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
263 if (draw_info->stroke_pattern != (Image *) NULL)
264 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
265 MagickTrue,exception);
266 clone_info->stroke_antialias=draw_info->stroke_antialias;
267 clone_info->text_antialias=draw_info->text_antialias;
268 clone_info->fill_rule=draw_info->fill_rule;
269 clone_info->linecap=draw_info->linecap;
270 clone_info->linejoin=draw_info->linejoin;
271 clone_info->miterlimit=draw_info->miterlimit;
272 clone_info->dash_offset=draw_info->dash_offset;
273 clone_info->decorate=draw_info->decorate;
274 clone_info->compose=draw_info->compose;
275 (void) CloneString(&clone_info->text,draw_info->text);
276 (void) CloneString(&clone_info->font,draw_info->font);
277 (void) CloneString(&clone_info->metrics,draw_info->metrics);
278 (void) CloneString(&clone_info->family,draw_info->family);
279 clone_info->style=draw_info->style;
280 clone_info->stretch=draw_info->stretch;
281 clone_info->weight=draw_info->weight;
282 (void) CloneString(&clone_info->encoding,draw_info->encoding);
283 clone_info->pointsize=draw_info->pointsize;
284 clone_info->kerning=draw_info->kerning;
285 clone_info->interline_spacing=draw_info->interline_spacing;
286 clone_info->interword_spacing=draw_info->interword_spacing;
287 clone_info->direction=draw_info->direction;
288 (void) CloneString(&clone_info->density,draw_info->density);
289 clone_info->align=draw_info->align;
290 clone_info->undercolor=draw_info->undercolor;
291 clone_info->border_color=draw_info->border_color;
292 (void) CloneString(&clone_info->server_name,draw_info->server_name);
293 if (draw_info->dash_pattern != (double *) NULL)
298 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
299 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
300 sizeof(*clone_info->dash_pattern));
301 if (clone_info->dash_pattern == (double *) NULL)
302 ThrowFatalException(ResourceLimitFatalError,
303 "UnableToAllocateDashPattern");
304 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
305 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
307 clone_info->gradient=draw_info->gradient;
308 if (draw_info->gradient.stops != (StopInfo *) NULL)
313 number_stops=clone_info->gradient.number_stops;
314 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
315 number_stops,sizeof(*clone_info->gradient.stops));
316 if (clone_info->gradient.stops == (StopInfo *) NULL)
317 ThrowFatalException(ResourceLimitFatalError,
318 "UnableToAllocateDashPattern");
319 (void) CopyMagickMemory(clone_info->gradient.stops,
320 draw_info->gradient.stops,(size_t) number_stops*
321 sizeof(*clone_info->gradient.stops));
323 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
324 clone_info->bounds=draw_info->bounds;
325 clone_info->clip_units=draw_info->clip_units;
326 clone_info->render=draw_info->render;
327 clone_info->alpha=draw_info->alpha;
328 clone_info->element_reference=draw_info->element_reference;
329 clone_info->debug=IsEventLogging();
330 exception=DestroyExceptionInfo(exception);
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 + C o n v e r t P a t h T o P o l y g o n %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 % ConvertPathToPolygon() converts a path to the more efficient sorted
348 % The format of the ConvertPathToPolygon method is:
350 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
351 % const PathInfo *path_info)
353 % A description of each parameter follows:
355 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
356 % rendering form of type PolygonInfo.
358 % o draw_info: Specifies a pointer to an DrawInfo structure.
360 % o path_info: Specifies a pointer to an PathInfo structure.
365 #if defined(__cplusplus) || defined(c_plusplus)
369 static int CompareEdges(const void *x,const void *y)
371 register const EdgeInfo
378 p=(const EdgeInfo *) x;
379 q=(const EdgeInfo *) y;
380 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
382 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
384 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
386 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
388 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
389 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
394 #if defined(__cplusplus) || defined(c_plusplus)
398 static void LogPolygonInfo(const PolygonInfo *polygon_info)
407 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
408 p=polygon_info->edges;
409 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
411 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
413 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
414 p->direction != MagickFalse ? "down" : "up");
415 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
416 p->ghostline != MagickFalse ? "transparent" : "opaque");
417 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
418 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
419 p->bounds.x2,p->bounds.y2);
420 for (j=0; j < (ssize_t) p->number_points; j++)
421 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
422 p->points[j].x,p->points[j].y);
425 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
428 static void ReversePoints(PointInfo *points,const size_t number_points)
436 for (i=0; i < (ssize_t) (number_points >> 1); i++)
439 points[i]=points[number_points-(i+1)];
440 points[number_points-(i+1)]=point;
444 static PolygonInfo *ConvertPathToPolygon(
445 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
474 Convert a path to the more efficient sorted rendering form.
476 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
477 if (polygon_info == (PolygonInfo *) NULL)
478 return((PolygonInfo *) NULL);
480 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
481 sizeof(*polygon_info->edges));
482 if (polygon_info->edges == (EdgeInfo *) NULL)
483 return((PolygonInfo *) NULL);
486 ghostline=MagickFalse;
489 points=(PointInfo *) NULL;
490 (void) ResetMagickMemory(&point,0,sizeof(point));
491 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
492 for (i=0; path_info[i].code != EndCode; i++)
494 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
495 (path_info[i].code == GhostlineCode))
500 if ((points != (PointInfo *) NULL) && (n >= 2))
502 if (edge == number_edges)
505 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
506 polygon_info->edges,(size_t) number_edges,
507 sizeof(*polygon_info->edges));
508 if (polygon_info->edges == (EdgeInfo *) NULL)
509 return((PolygonInfo *) NULL);
511 polygon_info->edges[edge].number_points=(size_t) n;
512 polygon_info->edges[edge].scanline=(-1.0);
513 polygon_info->edges[edge].highwater=0;
514 polygon_info->edges[edge].ghostline=ghostline;
515 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
517 ReversePoints(points,(size_t) n);
518 polygon_info->edges[edge].points=points;
519 polygon_info->edges[edge].bounds=bounds;
520 polygon_info->edges[edge].bounds.y1=points[0].y;
521 polygon_info->edges[edge].bounds.y2=points[n-1].y;
522 points=(PointInfo *) NULL;
523 ghostline=MagickFalse;
526 if (points == (PointInfo *) NULL)
529 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
531 if (points == (PointInfo *) NULL)
532 return((PolygonInfo *) NULL);
534 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
535 point=path_info[i].point;
546 next_direction=((path_info[i].point.y > point.y) ||
547 ((path_info[i].point.y == point.y) &&
548 (path_info[i].point.x > point.x))) ? 1 : -1;
549 if ((direction != 0) && (direction != next_direction))
555 if (edge == number_edges)
558 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
559 polygon_info->edges,(size_t) number_edges,
560 sizeof(*polygon_info->edges));
561 if (polygon_info->edges == (EdgeInfo *) NULL)
562 return((PolygonInfo *) NULL);
564 polygon_info->edges[edge].number_points=(size_t) n;
565 polygon_info->edges[edge].scanline=(-1.0);
566 polygon_info->edges[edge].highwater=0;
567 polygon_info->edges[edge].ghostline=ghostline;
568 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
570 ReversePoints(points,(size_t) n);
571 polygon_info->edges[edge].points=points;
572 polygon_info->edges[edge].bounds=bounds;
573 polygon_info->edges[edge].bounds.y1=points[0].y;
574 polygon_info->edges[edge].bounds.y2=points[n-1].y;
576 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
578 if (points == (PointInfo *) NULL)
579 return((PolygonInfo *) NULL);
581 ghostline=MagickFalse;
587 direction=next_direction;
588 if (points == (PointInfo *) NULL)
590 if (n == (ssize_t) number_points)
593 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
595 if (points == (PointInfo *) NULL)
596 return((PolygonInfo *) NULL);
598 point=path_info[i].point;
600 if (point.x < bounds.x1)
602 if (point.x > bounds.x2)
606 if (points != (PointInfo *) NULL)
609 points=(PointInfo *) RelinquishMagickMemory(points);
612 if (edge == number_edges)
615 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
616 polygon_info->edges,(size_t) number_edges,
617 sizeof(*polygon_info->edges));
618 if (polygon_info->edges == (EdgeInfo *) NULL)
619 return((PolygonInfo *) NULL);
621 polygon_info->edges[edge].number_points=(size_t) n;
622 polygon_info->edges[edge].scanline=(-1.0);
623 polygon_info->edges[edge].highwater=0;
624 polygon_info->edges[edge].ghostline=ghostline;
625 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
627 ReversePoints(points,(size_t) n);
628 polygon_info->edges[edge].points=points;
629 polygon_info->edges[edge].bounds=bounds;
630 polygon_info->edges[edge].bounds.y1=points[0].y;
631 polygon_info->edges[edge].bounds.y2=points[n-1].y;
632 ghostline=MagickFalse;
636 polygon_info->number_edges=edge;
637 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
638 sizeof(*polygon_info->edges),CompareEdges);
639 if (IsEventLogging() != MagickFalse)
640 LogPolygonInfo(polygon_info);
641 return(polygon_info);
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 + C o n v e r t P r i m i t i v e T o P a t h %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
658 % The format of the ConvertPrimitiveToPath method is:
660 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
661 % const PrimitiveInfo *primitive_info)
663 % A description of each parameter follows:
665 % o Method ConvertPrimitiveToPath returns a vector path structure of type
668 % o draw_info: a structure of type DrawInfo.
670 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
675 static void LogPathInfo(const PathInfo *path_info)
677 register const PathInfo
680 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
681 for (p=path_info; p->code != EndCode; p++)
682 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
683 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
684 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
685 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
687 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
690 static PathInfo *ConvertPrimitiveToPath(
691 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
712 Converts a PrimitiveInfo structure into a vector path structure.
714 switch (primitive_info->primitive)
721 return((PathInfo *) NULL);
725 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
726 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
728 if (path_info == (PathInfo *) NULL)
729 return((PathInfo *) NULL);
737 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
740 if (coordinates <= 0)
742 coordinates=(ssize_t) primitive_info[i].coordinates;
743 p=primitive_info[i].point;
749 Eliminate duplicate points.
751 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) > MagickEpsilon) ||
752 (fabs(q.y-primitive_info[i].point.y) > MagickEpsilon))
754 path_info[n].code=code;
755 path_info[n].point=primitive_info[i].point;
756 q=primitive_info[i].point;
761 if ((fabs(p.x-primitive_info[i].point.x) <= MagickEpsilon) &&
762 (fabs(p.y-primitive_info[i].point.y) <= MagickEpsilon))
765 Mark the p point as open if it does not match the q.
767 path_info[start].code=OpenCode;
768 path_info[n].code=GhostlineCode;
769 path_info[n].point=primitive_info[i].point;
771 path_info[n].code=LineToCode;
772 path_info[n].point=p;
775 path_info[n].code=EndCode;
776 path_info[n].point.x=0.0;
777 path_info[n].point.y=0.0;
778 if (IsEventLogging() != MagickFalse)
779 LogPathInfo(path_info);
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 % D e s t r o y D r a w I n f o %
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
797 % The format of the DestroyDrawInfo method is:
799 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
801 % A description of each parameter follows:
803 % o draw_info: the draw info.
806 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
808 if (draw_info->debug != MagickFalse)
809 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
810 assert(draw_info != (DrawInfo *) NULL);
811 assert(draw_info->signature == MagickSignature);
812 if (draw_info->primitive != (char *) NULL)
813 draw_info->primitive=DestroyString(draw_info->primitive);
814 if (draw_info->text != (char *) NULL)
815 draw_info->text=DestroyString(draw_info->text);
816 if (draw_info->geometry != (char *) NULL)
817 draw_info->geometry=DestroyString(draw_info->geometry);
818 if (draw_info->fill_pattern != (Image *) NULL)
819 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
820 if (draw_info->stroke_pattern != (Image *) NULL)
821 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
822 if (draw_info->font != (char *) NULL)
823 draw_info->font=DestroyString(draw_info->font);
824 if (draw_info->metrics != (char *) NULL)
825 draw_info->metrics=DestroyString(draw_info->metrics);
826 if (draw_info->family != (char *) NULL)
827 draw_info->family=DestroyString(draw_info->family);
828 if (draw_info->encoding != (char *) NULL)
829 draw_info->encoding=DestroyString(draw_info->encoding);
830 if (draw_info->density != (char *) NULL)
831 draw_info->density=DestroyString(draw_info->density);
832 if (draw_info->server_name != (char *) NULL)
833 draw_info->server_name=(char *)
834 RelinquishMagickMemory(draw_info->server_name);
835 if (draw_info->dash_pattern != (double *) NULL)
836 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
837 draw_info->dash_pattern);
838 if (draw_info->gradient.stops != (StopInfo *) NULL)
839 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
840 draw_info->gradient.stops);
841 if (draw_info->clip_mask != (char *) NULL)
842 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
843 draw_info->signature=(~MagickSignature);
844 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 + D e s t r o y E d g e %
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 % DestroyEdge() destroys the specified polygon edge.
861 % The format of the DestroyEdge method is:
863 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
865 % A description of each parameter follows:
867 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
869 % o edge: the polygon edge number to destroy.
872 static size_t DestroyEdge(PolygonInfo *polygon_info,
875 assert(edge < polygon_info->number_edges);
876 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
877 polygon_info->edges[edge].points);
878 polygon_info->number_edges--;
879 if (edge < polygon_info->number_edges)
880 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
881 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
882 return(polygon_info->number_edges);
886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 + D e s t r o y P o l y g o n I n f o %
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
898 % The format of the DestroyPolygonInfo method is:
900 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
902 % A description of each parameter follows:
904 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
907 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
912 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
913 polygon_info->edges[i].points=(PointInfo *)
914 RelinquishMagickMemory(polygon_info->edges[i].points);
915 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
916 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % D r a w A f f i n e I m a g e %
928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930 % DrawAffineImage() composites the source over the destination image as
931 % dictated by the affine transform.
933 % The format of the DrawAffineImage method is:
935 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
936 % const AffineMatrix *affine,ExceptionInfo *exception)
938 % A description of each parameter follows:
940 % o image: the image.
942 % o source: the source image.
944 % o affine: the affine transform.
946 % o exception: return any errors or warnings in this structure.
950 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
951 const double y,const SegmentInfo *edge)
964 Determine left and right edges.
966 inverse_edge.x1=edge->x1;
967 inverse_edge.y1=edge->y1;
968 inverse_edge.x2=edge->x2;
969 inverse_edge.y2=edge->y2;
970 z=affine->ry*y+affine->tx;
971 if (affine->sx > MagickEpsilon)
973 intercept=(-z/affine->sx);
975 if (x > inverse_edge.x1)
977 intercept=(-z+(double) image->columns)/affine->sx;
979 if (x < inverse_edge.x2)
983 if (affine->sx < -MagickEpsilon)
985 intercept=(-z+(double) image->columns)/affine->sx;
987 if (x > inverse_edge.x1)
989 intercept=(-z/affine->sx);
991 if (x < inverse_edge.x2)
995 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
997 inverse_edge.x2=edge->x1;
998 return(inverse_edge);
1001 Determine top and bottom edges.
1003 z=affine->sy*y+affine->ty;
1004 if (affine->rx > MagickEpsilon)
1006 intercept=(-z/affine->rx);
1008 if (x > inverse_edge.x1)
1010 intercept=(-z+(double) image->rows)/affine->rx;
1012 if (x < inverse_edge.x2)
1016 if (affine->rx < -MagickEpsilon)
1018 intercept=(-z+(double) image->rows)/affine->rx;
1020 if (x > inverse_edge.x1)
1022 intercept=(-z/affine->rx);
1024 if (x < inverse_edge.x2)
1028 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1030 inverse_edge.x2=edge->x2;
1031 return(inverse_edge);
1033 return(inverse_edge);
1036 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1044 determinant=1.0/(affine->sx*affine->sy-affine->rx*affine->ry);
1045 inverse_affine.sx=determinant*affine->sy;
1046 inverse_affine.rx=determinant*(-affine->rx);
1047 inverse_affine.ry=determinant*(-affine->ry);
1048 inverse_affine.sy=determinant*affine->sx;
1049 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1051 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1053 return(inverse_affine);
1056 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
1063 static inline double MagickMax(const double x,const double y)
1070 static inline double MagickMin(const double x,const double y)
1077 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1078 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1111 Determine bounding box.
1113 assert(image != (Image *) NULL);
1114 assert(image->signature == MagickSignature);
1115 if (image->debug != MagickFalse)
1116 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1117 assert(source != (const Image *) NULL);
1118 assert(source->signature == MagickSignature);
1119 assert(affine != (AffineMatrix *) NULL);
1122 extent[1].x=(double) source->columns-1.0;
1124 extent[2].x=(double) source->columns-1.0;
1125 extent[2].y=(double) source->rows-1.0;
1127 extent[3].y=(double) source->rows-1.0;
1128 for (i=0; i < 4; i++)
1131 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1132 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1136 for (i=1; i < 4; i++)
1138 if (min.x > extent[i].x)
1140 if (min.y > extent[i].y)
1142 if (max.x < extent[i].x)
1144 if (max.y < extent[i].y)
1148 Affine transform image.
1150 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1151 return(MagickFalse);
1153 edge.x1=MagickMax(min.x,0.0);
1154 edge.y1=MagickMax(min.y,0.0);
1155 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1156 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1157 inverse_affine=InverseAffineMatrix(affine);
1158 GetPixelInfo(image,&zero);
1159 start=(ssize_t) ceil(edge.y1-0.5);
1160 stop=(ssize_t) ceil(edge.y2-0.5);
1161 source_view=AcquireVirtualCacheView(source,exception);
1162 image_view=AcquireAuthenticCacheView(image,exception);
1163 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1164 #pragma omp parallel for schedule(static) shared(status)
1166 for (y=start; y <= stop; y++)
1187 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1188 if (inverse_edge.x2 < inverse_edge.x1)
1190 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1191 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
1192 inverse_edge.x1+0.5)+1),1,exception);
1193 if (q == (Quantum *) NULL)
1198 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1200 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1202 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1204 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1205 point.x,point.y,&pixel,exception);
1206 GetPixelInfoPixel(image,q,&composite);
1207 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1209 SetPixelInfoPixel(image,&composite,q);
1211 q+=GetPixelChannels(image);
1213 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1216 source_view=DestroyCacheView(source_view);
1217 image_view=DestroyCacheView(image_view);
1222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226 + D r a w B o u n d i n g R e c t a n g l e s %
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1233 % is only useful for developers debugging the rendering algorithm.
1235 % The format of the DrawBoundingRectangles method is:
1237 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1238 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1240 % A description of each parameter follows:
1242 % o image: the image.
1244 % o draw_info: the draw info.
1246 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1248 % o exception: return any errors or warnings in this structure.
1251 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1252 const PolygonInfo *polygon_info,ExceptionInfo *exception)
1277 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1278 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1280 resolution.x=DefaultResolution;
1281 resolution.y=DefaultResolution;
1282 if (clone_info->density != (char *) NULL)
1290 flags=ParseGeometry(clone_info->density,&geometry_info);
1291 resolution.x=geometry_info.rho;
1292 resolution.y=geometry_info.sigma;
1293 if ((flags & SigmaValue) == MagickFalse)
1294 resolution.y=resolution.x;
1296 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1297 clone_info->stroke_width/2.0;
1302 if (polygon_info != (PolygonInfo *) NULL)
1304 bounds=polygon_info->edges[0].bounds;
1305 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1307 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1308 bounds.x1=polygon_info->edges[i].bounds.x1;
1309 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1310 bounds.y1=polygon_info->edges[i].bounds.y1;
1311 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1312 bounds.x2=polygon_info->edges[i].bounds.x2;
1313 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1314 bounds.y2=polygon_info->edges[i].bounds.y2;
1317 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1318 image->columns ? (double) image->columns-1 : bounds.x1;
1320 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1321 image->rows ? (double) image->rows-1 : bounds.y1;
1323 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1324 image->columns ? (double) image->columns-1 : bounds.x2;
1326 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1327 image->rows ? (double) image->rows-1 : bounds.y2;
1328 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1330 if (polygon_info->edges[i].direction != 0)
1331 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1334 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1336 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1337 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1338 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1339 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1340 primitive_info[0].primitive=RectanglePrimitive;
1341 TraceRectangle(primitive_info,start,end);
1342 primitive_info[0].method=ReplaceMethod;
1343 coordinates=(ssize_t) primitive_info[0].coordinates;
1344 primitive_info[coordinates].primitive=UndefinedPrimitive;
1345 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1348 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1350 start.x=(double) (bounds.x1-mid);
1351 start.y=(double) (bounds.y1-mid);
1352 end.x=(double) (bounds.x2+mid);
1353 end.y=(double) (bounds.y2+mid);
1354 primitive_info[0].primitive=RectanglePrimitive;
1355 TraceRectangle(primitive_info,start,end);
1356 primitive_info[0].method=ReplaceMethod;
1357 coordinates=(ssize_t) primitive_info[0].coordinates;
1358 primitive_info[coordinates].primitive=UndefinedPrimitive;
1359 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1360 clone_info=DestroyDrawInfo(clone_info);
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 % D r a w C l i p P a t h %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % DrawClipPath() draws the clip path on the image mask.
1376 % The format of the DrawClipPath method is:
1378 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1379 % const char *name,ExceptionInfo *exception)
1381 % A description of each parameter follows:
1383 % o image: the image.
1385 % o draw_info: the draw info.
1387 % o name: the name of the clip path.
1389 % o exception: return any errors or warnings in this structure.
1392 MagickExport MagickBooleanType DrawClipPath(Image *image,
1393 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1396 filename[MaxTextExtent];
1410 assert(image != (Image *) NULL);
1411 assert(image->signature == MagickSignature);
1412 if (image->debug != MagickFalse)
1413 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1414 assert(draw_info != (const DrawInfo *) NULL);
1415 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1416 value=GetImageArtifact(image,filename);
1417 if (value == (const char *) NULL)
1418 return(MagickFalse);
1419 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1420 if (clip_mask == (Image *) NULL)
1421 return(MagickFalse);
1422 (void) QueryColorCompliance("#0000",AllCompliance,
1423 &clip_mask->background_color,exception);
1424 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1425 (void) SetImageBackgroundColor(clip_mask,exception);
1426 if (image->debug != MagickFalse)
1427 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1428 draw_info->clip_mask);
1429 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1430 (void) CloneString(&clone_info->primitive,value);
1431 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1433 clone_info->clip_mask=(char *) NULL;
1434 status=NegateImage(clip_mask,MagickFalse,exception);
1435 (void) SetImageMask(image,clip_mask,exception);
1436 clip_mask=DestroyImage(clip_mask);
1437 clone_info=DestroyDrawInfo(clone_info);
1438 status=DrawImage(image,clone_info,exception);
1439 if (image->debug != MagickFalse)
1440 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1441 return(status != 0 ? MagickTrue : MagickFalse);
1445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1449 + D r a w D a s h P o l y g o n %
1453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1455 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1456 % image while respecting the dash offset and dash pattern attributes.
1458 % The format of the DrawDashPolygon method is:
1460 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1461 % const PrimitiveInfo *primitive_info,Image *image,
1462 % ExceptionInfo *exception)
1464 % A description of each parameter follows:
1466 % o draw_info: the draw info.
1468 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1470 % o image: the image.
1472 % o exception: return any errors or warnings in this structure.
1475 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1476 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1497 register MagickRealType
1508 assert(draw_info != (const DrawInfo *) NULL);
1509 if (image->debug != MagickFalse)
1510 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1511 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1512 clone_info->miterlimit=0;
1513 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1514 number_vertices=(size_t) i;
1515 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1516 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1517 if (dash_polygon == (PrimitiveInfo *) NULL)
1518 return(MagickFalse);
1519 dash_polygon[0]=primitive_info[0];
1520 scale=ExpandAffine(&draw_info->affine);
1521 length=scale*(draw_info->dash_pattern[0]-0.5);
1522 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1524 for (n=0; offset > 0.0; j=0)
1526 if (draw_info->dash_pattern[n] <= 0.0)
1528 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1529 if (offset > length)
1533 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1536 if (offset < length)
1548 for (i=1; i < (ssize_t) number_vertices; i++)
1550 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1551 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1552 maximum_length=hypot((double) dx,dy);
1556 if (draw_info->dash_pattern[n] == 0.0)
1558 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1560 for (total_length=0.0; (total_length+length) < maximum_length; )
1562 total_length+=length;
1563 if ((n & 0x01) != 0)
1565 dash_polygon[0]=primitive_info[0];
1566 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1567 total_length/maximum_length);
1568 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1569 total_length/maximum_length);
1574 if ((j+1) > (ssize_t) (2*number_vertices))
1576 dash_polygon[j]=primitive_info[i-1];
1577 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1578 total_length/maximum_length);
1579 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1580 total_length/maximum_length);
1581 dash_polygon[j].coordinates=1;
1583 dash_polygon[0].coordinates=(size_t) j;
1584 dash_polygon[j].primitive=UndefinedPrimitive;
1585 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1588 if (draw_info->dash_pattern[n] == 0.0)
1590 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1592 length-=(maximum_length-total_length);
1593 if ((n & 0x01) != 0)
1595 dash_polygon[j]=primitive_info[i];
1596 dash_polygon[j].coordinates=1;
1599 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1601 dash_polygon[j]=primitive_info[i-1];
1602 dash_polygon[j].point.x+=MagickEpsilon;
1603 dash_polygon[j].point.y+=MagickEpsilon;
1604 dash_polygon[j].coordinates=1;
1606 dash_polygon[0].coordinates=(size_t) j;
1607 dash_polygon[j].primitive=UndefinedPrimitive;
1608 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1610 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1611 clone_info=DestroyDrawInfo(clone_info);
1612 if (image->debug != MagickFalse)
1613 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1614 return(status != 0 ? MagickTrue : MagickFalse);
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1622 % D r a w I m a g e %
1626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 % DrawImage() draws a graphic primitive on your image. The primitive
1629 % may be represented as a string or filename. Precede the filename with an
1630 % "at" sign (@) and the contents of the file are drawn on the image. You
1631 % can affect how text is drawn by setting one or more members of the draw
1634 % The format of the DrawImage method is:
1636 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1637 % ExceptionInfo *exception)
1639 % A description of each parameter follows:
1641 % o image: the image.
1643 % o draw_info: the draw info.
1645 % o exception: return any errors or warnings in this structure.
1649 static inline MagickBooleanType IsPoint(const char *point)
1657 value=StringToDouble(point,&p);
1658 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1661 static inline void TracePoint(PrimitiveInfo *primitive_info,
1662 const PointInfo point)
1664 primitive_info->coordinates=1;
1665 primitive_info->point=point;
1668 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1669 ExceptionInfo *exception)
1671 #define RenderImageTag "Render/Image"
1678 key[2*MaxTextExtent],
1679 keyword[MaxTextExtent],
1680 geometry[MaxTextExtent],
1681 name[MaxTextExtent],
1682 pattern[MaxTextExtent],
1733 Ensure the annotation info is valid.
1735 assert(image != (Image *) NULL);
1736 assert(image->signature == MagickSignature);
1737 if (image->debug != MagickFalse)
1738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1739 assert(draw_info != (DrawInfo *) NULL);
1740 assert(draw_info->signature == MagickSignature);
1741 if (image->debug != MagickFalse)
1742 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1743 if ((draw_info->primitive == (char *) NULL) ||
1744 (*draw_info->primitive == '\0'))
1745 return(MagickFalse);
1746 if (image->debug != MagickFalse)
1747 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1748 if (*draw_info->primitive != '@')
1749 primitive=AcquireString(draw_info->primitive);
1751 primitive=FileToString(draw_info->primitive+1,~0,exception);
1752 if (primitive == (char *) NULL)
1753 return(MagickFalse);
1754 primitive_extent=(MagickRealType) strlen(primitive);
1755 (void) SetImageArtifact(image,"MVG",primitive);
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 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1790 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1791 return(MagickFalse);
1792 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1793 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1795 for (q=primitive; *q != '\0'; )
1798 Interpret graphic primitive.
1800 GetMagickToken(q,&q,keyword);
1801 if (*keyword == '\0')
1803 if (*keyword == '#')
1808 while ((*q != '\n') && (*q != '\0'))
1812 p=q-strlen(keyword)-1;
1813 primitive_type=UndefinedPrimitive;
1814 current=graphic_context[n]->affine;
1815 GetAffineMatrix(&affine);
1823 if (LocaleCompare("affine",keyword) == 0)
1825 GetMagickToken(q,&q,token);
1826 affine.sx=StringToDouble(token,(char **) NULL);
1827 GetMagickToken(q,&q,token);
1829 GetMagickToken(q,&q,token);
1830 affine.rx=StringToDouble(token,(char **) NULL);
1831 GetMagickToken(q,&q,token);
1833 GetMagickToken(q,&q,token);
1834 affine.ry=StringToDouble(token,(char **) NULL);
1835 GetMagickToken(q,&q,token);
1837 GetMagickToken(q,&q,token);
1838 affine.sy=StringToDouble(token,(char **) NULL);
1839 GetMagickToken(q,&q,token);
1841 GetMagickToken(q,&q,token);
1842 affine.tx=StringToDouble(token,(char **) NULL);
1843 GetMagickToken(q,&q,token);
1845 GetMagickToken(q,&q,token);
1846 affine.ty=StringToDouble(token,(char **) NULL);
1849 if (LocaleCompare("arc",keyword) == 0)
1851 primitive_type=ArcPrimitive;
1860 if (LocaleCompare("bezier",keyword) == 0)
1862 primitive_type=BezierPrimitive;
1865 if (LocaleCompare("border-color",keyword) == 0)
1867 GetMagickToken(q,&q,token);
1868 (void) QueryColorCompliance(token,AllCompliance,
1869 &graphic_context[n]->border_color,exception);
1878 if (LocaleCompare("clip-path",keyword) == 0)
1883 GetMagickToken(q,&q,token);
1884 (void) CloneString(&graphic_context[n]->clip_mask,token);
1885 (void) DrawClipPath(image,graphic_context[n],
1886 graphic_context[n]->clip_mask,exception);
1889 if (LocaleCompare("clip-rule",keyword) == 0)
1894 GetMagickToken(q,&q,token);
1895 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
1897 if (fill_rule == -1)
1900 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1903 if (LocaleCompare("clip-units",keyword) == 0)
1908 GetMagickToken(q,&q,token);
1909 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
1911 if (clip_units == -1)
1916 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1917 if (clip_units == ObjectBoundingBox)
1919 GetAffineMatrix(¤t);
1920 affine.sx=draw_info->bounds.x2;
1921 affine.sy=draw_info->bounds.y2;
1922 affine.tx=draw_info->bounds.x1;
1923 affine.ty=draw_info->bounds.y1;
1928 if (LocaleCompare("circle",keyword) == 0)
1930 primitive_type=CirclePrimitive;
1933 if (LocaleCompare("color",keyword) == 0)
1935 primitive_type=ColorPrimitive;
1944 if (LocaleCompare("decorate",keyword) == 0)
1949 GetMagickToken(q,&q,token);
1950 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
1955 graphic_context[n]->decorate=(DecorationType) decorate;
1964 if (LocaleCompare("ellipse",keyword) == 0)
1966 primitive_type=EllipsePrimitive;
1969 if (LocaleCompare("encoding",keyword) == 0)
1971 GetMagickToken(q,&q,token);
1972 (void) CloneString(&graphic_context[n]->encoding,token);
1981 if (LocaleCompare("fill",keyword) == 0)
1983 GetMagickToken(q,&q,token);
1984 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
1985 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1986 (void) DrawPatternPath(image,draw_info,token,
1987 &graphic_context[n]->fill_pattern,exception);
1990 status=QueryColorCompliance(token,AllCompliance,
1991 &graphic_context[n]->fill,exception);
1992 if (status == MagickFalse)
1997 pattern_info=AcquireImageInfo();
1998 (void) CopyMagickString(pattern_info->filename,token,
2000 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2002 CatchException(exception);
2003 pattern_info=DestroyImageInfo(pattern_info);
2008 if (LocaleCompare("fill-opacity",keyword) == 0)
2010 GetMagickToken(q,&q,token);
2011 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2012 graphic_context[n]->fill.alpha=(MagickRealType) QuantumRange*
2013 factor*StringToDouble(token,(char **) NULL);
2016 if (LocaleCompare("fill-rule",keyword) == 0)
2021 GetMagickToken(q,&q,token);
2022 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2024 if (fill_rule == -1)
2027 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2030 if (LocaleCompare("font",keyword) == 0)
2032 GetMagickToken(q,&q,token);
2033 (void) CloneString(&graphic_context[n]->font,token);
2034 if (LocaleCompare("none",token) == 0)
2035 graphic_context[n]->font=(char *)
2036 RelinquishMagickMemory(graphic_context[n]->font);
2039 if (LocaleCompare("font-family",keyword) == 0)
2041 GetMagickToken(q,&q,token);
2042 (void) CloneString(&graphic_context[n]->family,token);
2045 if (LocaleCompare("font-size",keyword) == 0)
2047 GetMagickToken(q,&q,token);
2048 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
2051 if (LocaleCompare("font-stretch",keyword) == 0)
2056 GetMagickToken(q,&q,token);
2057 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2061 graphic_context[n]->stretch=(StretchType) stretch;
2064 if (LocaleCompare("font-style",keyword) == 0)
2069 GetMagickToken(q,&q,token);
2070 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2074 graphic_context[n]->style=(StyleType) style;
2077 if (LocaleCompare("font-weight",keyword) == 0)
2079 GetMagickToken(q,&q,token);
2080 graphic_context[n]->weight=StringToUnsignedLong(token);
2081 if (LocaleCompare(token,"all") == 0)
2082 graphic_context[n]->weight=0;
2083 if (LocaleCompare(token,"bold") == 0)
2084 graphic_context[n]->weight=700;
2085 if (LocaleCompare(token,"bolder") == 0)
2086 if (graphic_context[n]->weight <= 800)
2087 graphic_context[n]->weight+=100;
2088 if (LocaleCompare(token,"lighter") == 0)
2089 if (graphic_context[n]->weight >= 100)
2090 graphic_context[n]->weight-=100;
2091 if (LocaleCompare(token,"normal") == 0)
2092 graphic_context[n]->weight=400;
2101 if (LocaleCompare("gradient-units",keyword) == 0)
2103 GetMagickToken(q,&q,token);
2106 if (LocaleCompare("gravity",keyword) == 0)
2111 GetMagickToken(q,&q,token);
2112 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2116 graphic_context[n]->gravity=(GravityType) gravity;
2125 if (LocaleCompare("image",keyword) == 0)
2130 primitive_type=ImagePrimitive;
2131 GetMagickToken(q,&q,token);
2132 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
2136 graphic_context[n]->compose=(CompositeOperator) compose;
2139 if (LocaleCompare("interline-spacing",keyword) == 0)
2141 GetMagickToken(q,&q,token);
2142 graphic_context[n]->interline_spacing=StringToDouble(token,
2146 if (LocaleCompare("interword-spacing",keyword) == 0)
2148 GetMagickToken(q,&q,token);
2149 graphic_context[n]->interword_spacing=StringToDouble(token,
2159 if (LocaleCompare("kerning",keyword) == 0)
2161 GetMagickToken(q,&q,token);
2162 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2171 if (LocaleCompare("line",keyword) == 0)
2172 primitive_type=LinePrimitive;
2180 if (LocaleCompare("matte",keyword) == 0)
2181 primitive_type=MattePrimitive;
2189 if (LocaleCompare("offset",keyword) == 0)
2191 GetMagickToken(q,&q,token);
2194 if (LocaleCompare("opacity",keyword) == 0)
2196 GetMagickToken(q,&q,token);
2197 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2198 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2199 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
2200 factor*StringToDouble(token,(char **) NULL))));
2201 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2202 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
2211 if (LocaleCompare("path",keyword) == 0)
2213 primitive_type=PathPrimitive;
2216 if (LocaleCompare("point",keyword) == 0)
2218 primitive_type=PointPrimitive;
2221 if (LocaleCompare("polyline",keyword) == 0)
2223 primitive_type=PolylinePrimitive;
2226 if (LocaleCompare("polygon",keyword) == 0)
2228 primitive_type=PolygonPrimitive;
2231 if (LocaleCompare("pop",keyword) == 0)
2233 GetMagickToken(q,&q,token);
2234 if (LocaleCompare("clip-path",token) == 0)
2236 if (LocaleCompare("defs",token) == 0)
2238 if (LocaleCompare("gradient",token) == 0)
2240 if (LocaleCompare("graphic-context",token) == 0)
2244 (void) ThrowMagickException(exception,GetMagickModule(),
2245 DrawError,"UnbalancedGraphicContextPushPop","'%s'",token);
2249 if (graphic_context[n]->clip_mask != (char *) NULL)
2250 if (LocaleCompare(graphic_context[n]->clip_mask,
2251 graphic_context[n-1]->clip_mask) != 0)
2252 (void) SetImageMask(image,(Image *) NULL,exception);
2253 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2257 if (LocaleCompare("pattern",token) == 0)
2262 if (LocaleCompare("push",keyword) == 0)
2264 GetMagickToken(q,&q,token);
2265 if (LocaleCompare("clip-path",token) == 0)
2268 name[MaxTextExtent];
2270 GetMagickToken(q,&q,token);
2271 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2272 for (p=q; *q != '\0'; )
2274 GetMagickToken(q,&q,token);
2275 if (LocaleCompare(token,"pop") != 0)
2277 GetMagickToken(q,(const char **) NULL,token);
2278 if (LocaleCompare(token,"clip-path") != 0)
2282 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2283 (void) SetImageArtifact(image,name,token);
2284 GetMagickToken(q,&q,token);
2287 if (LocaleCompare("gradient",token) == 0)
2290 key[2*MaxTextExtent],
2291 name[MaxTextExtent],
2292 type[MaxTextExtent];
2297 GetMagickToken(q,&q,token);
2298 (void) CopyMagickString(name,token,MaxTextExtent);
2299 GetMagickToken(q,&q,token);
2300 (void) CopyMagickString(type,token,MaxTextExtent);
2301 GetMagickToken(q,&q,token);
2302 segment.x1=StringToDouble(token,(char **) NULL);
2303 GetMagickToken(q,&q,token);
2305 GetMagickToken(q,&q,token);
2306 segment.y1=StringToDouble(token,(char **) NULL);
2307 GetMagickToken(q,&q,token);
2309 GetMagickToken(q,&q,token);
2310 segment.x2=StringToDouble(token,(char **) NULL);
2311 GetMagickToken(q,&q,token);
2313 GetMagickToken(q,&q,token);
2314 segment.y2=StringToDouble(token,(char **) NULL);
2315 if (LocaleCompare(type,"radial") == 0)
2317 GetMagickToken(q,&q,token);
2319 GetMagickToken(q,&q,token);
2321 for (p=q; *q != '\0'; )
2323 GetMagickToken(q,&q,token);
2324 if (LocaleCompare(token,"pop") != 0)
2326 GetMagickToken(q,(const char **) NULL,token);
2327 if (LocaleCompare(token,"gradient") != 0)
2331 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2332 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2333 graphic_context[n]->affine.ry*segment.y1+
2334 graphic_context[n]->affine.tx;
2335 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2336 graphic_context[n]->affine.sy*segment.y1+
2337 graphic_context[n]->affine.ty;
2338 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2339 graphic_context[n]->affine.ry*segment.y2+
2340 graphic_context[n]->affine.tx;
2341 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2342 graphic_context[n]->affine.sy*segment.y2+
2343 graphic_context[n]->affine.ty;
2344 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2345 (void) SetImageArtifact(image,key,token);
2346 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2347 (void) FormatLocaleString(geometry,MaxTextExtent,
2348 "%gx%g%+.15g%+.15g",
2349 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2350 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2351 bounds.x1,bounds.y1);
2352 (void) SetImageArtifact(image,key,geometry);
2353 GetMagickToken(q,&q,token);
2356 if (LocaleCompare("pattern",token) == 0)
2361 GetMagickToken(q,&q,token);
2362 (void) CopyMagickString(name,token,MaxTextExtent);
2363 GetMagickToken(q,&q,token);
2364 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2366 GetMagickToken(q,&q,token);
2368 GetMagickToken(q,&q,token);
2369 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2371 GetMagickToken(q,&q,token);
2373 GetMagickToken(q,&q,token);
2374 bounds.width=(size_t) floor(StringToDouble(token,
2375 (char **) NULL)+0.5);
2376 GetMagickToken(q,&q,token);
2378 GetMagickToken(q,&q,token);
2379 bounds.height=(size_t) floor(StringToDouble(token,
2380 (char **) NULL)+0.5);
2381 for (p=q; *q != '\0'; )
2383 GetMagickToken(q,&q,token);
2384 if (LocaleCompare(token,"pop") != 0)
2386 GetMagickToken(q,(const char **) NULL,token);
2387 if (LocaleCompare(token,"pattern") != 0)
2391 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2392 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2393 (void) SetImageArtifact(image,key,token);
2394 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2395 (void) FormatLocaleString(geometry,MaxTextExtent,
2396 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2397 bounds.height,(double) bounds.x,(double) bounds.y);
2398 (void) SetImageArtifact(image,key,geometry);
2399 GetMagickToken(q,&q,token);
2402 if (LocaleCompare("graphic-context",token) == 0)
2405 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2406 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2407 if (graphic_context == (DrawInfo **) NULL)
2409 (void) ThrowMagickException(exception,GetMagickModule(),
2410 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2414 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2415 graphic_context[n-1]);
2418 if (LocaleCompare("defs",token) == 0)
2429 if (LocaleCompare("rectangle",keyword) == 0)
2431 primitive_type=RectanglePrimitive;
2434 if (LocaleCompare("rotate",keyword) == 0)
2436 GetMagickToken(q,&q,token);
2437 angle=StringToDouble(token,(char **) NULL);
2438 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2439 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2440 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2441 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2444 if (LocaleCompare("roundRectangle",keyword) == 0)
2446 primitive_type=RoundRectanglePrimitive;
2455 if (LocaleCompare("scale",keyword) == 0)
2457 GetMagickToken(q,&q,token);
2458 affine.sx=StringToDouble(token,(char **) NULL);
2459 GetMagickToken(q,&q,token);
2461 GetMagickToken(q,&q,token);
2462 affine.sy=StringToDouble(token,(char **) NULL);
2465 if (LocaleCompare("skewX",keyword) == 0)
2467 GetMagickToken(q,&q,token);
2468 angle=StringToDouble(token,(char **) NULL);
2469 affine.ry=sin(DegreesToRadians(angle));
2472 if (LocaleCompare("skewY",keyword) == 0)
2474 GetMagickToken(q,&q,token);
2475 angle=StringToDouble(token,(char **) NULL);
2476 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2479 if (LocaleCompare("stop-color",keyword) == 0)
2484 GetMagickToken(q,&q,token);
2485 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2487 (void) GradientImage(image,LinearGradient,ReflectSpread,
2488 &start_color,&stop_color,exception);
2489 start_color=stop_color;
2490 GetMagickToken(q,&q,token);
2493 if (LocaleCompare("stroke",keyword) == 0)
2495 GetMagickToken(q,&q,token);
2496 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2497 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2498 (void) DrawPatternPath(image,draw_info,token,
2499 &graphic_context[n]->stroke_pattern,exception);
2502 status=QueryColorCompliance(token,AllCompliance,
2503 &graphic_context[n]->stroke,exception);
2504 if (status == MagickFalse)
2509 pattern_info=AcquireImageInfo();
2510 (void) CopyMagickString(pattern_info->filename,token,
2512 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2514 CatchException(exception);
2515 pattern_info=DestroyImageInfo(pattern_info);
2520 if (LocaleCompare("stroke-antialias",keyword) == 0)
2522 GetMagickToken(q,&q,token);
2523 graphic_context[n]->stroke_antialias=
2524 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2527 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2529 if (graphic_context[n]->dash_pattern != (double *) NULL)
2530 graphic_context[n]->dash_pattern=(double *)
2531 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2532 if (IsPoint(q) != MagickFalse)
2538 GetMagickToken(p,&p,token);
2540 GetMagickToken(p,&p,token);
2541 for (x=0; IsPoint(token) != MagickFalse; x++)
2543 GetMagickToken(p,&p,token);
2545 GetMagickToken(p,&p,token);
2547 graphic_context[n]->dash_pattern=(double *)
2548 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2549 sizeof(*graphic_context[n]->dash_pattern));
2550 if (graphic_context[n]->dash_pattern == (double *) NULL)
2552 (void) ThrowMagickException(exception,GetMagickModule(),
2553 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2557 for (j=0; j < x; j++)
2559 GetMagickToken(q,&q,token);
2561 GetMagickToken(q,&q,token);
2562 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2565 if ((x & 0x01) != 0)
2566 for ( ; j < (2*x); j++)
2567 graphic_context[n]->dash_pattern[j]=
2568 graphic_context[n]->dash_pattern[j-x];
2569 graphic_context[n]->dash_pattern[j]=0.0;
2572 GetMagickToken(q,&q,token);
2575 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2577 GetMagickToken(q,&q,token);
2578 graphic_context[n]->dash_offset=StringToDouble(token,
2582 if (LocaleCompare("stroke-linecap",keyword) == 0)
2587 GetMagickToken(q,&q,token);
2588 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
2592 graphic_context[n]->linecap=(LineCap) linecap;
2595 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2600 GetMagickToken(q,&q,token);
2601 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2606 graphic_context[n]->linejoin=(LineJoin) linejoin;
2609 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2611 GetMagickToken(q,&q,token);
2612 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2615 if (LocaleCompare("stroke-opacity",keyword) == 0)
2617 GetMagickToken(q,&q,token);
2618 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2619 graphic_context[n]->stroke.alpha=(MagickRealType) QuantumRange*
2620 factor*StringToDouble(token,(char **) NULL);
2623 if (LocaleCompare("stroke-width",keyword) == 0)
2625 GetMagickToken(q,&q,token);
2626 graphic_context[n]->stroke_width=StringToDouble(token,
2636 if (LocaleCompare("text",keyword) == 0)
2638 primitive_type=TextPrimitive;
2641 if (LocaleCompare("text-align",keyword) == 0)
2646 GetMagickToken(q,&q,token);
2647 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2651 graphic_context[n]->align=(AlignType) align;
2654 if (LocaleCompare("text-anchor",keyword) == 0)
2659 GetMagickToken(q,&q,token);
2660 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
2664 graphic_context[n]->align=(AlignType) align;
2667 if (LocaleCompare("text-antialias",keyword) == 0)
2669 GetMagickToken(q,&q,token);
2670 graphic_context[n]->text_antialias=
2671 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2674 if (LocaleCompare("text-undercolor",keyword) == 0)
2676 GetMagickToken(q,&q,token);
2677 (void) QueryColorCompliance(token,AllCompliance,
2678 &graphic_context[n]->undercolor,exception);
2681 if (LocaleCompare("translate",keyword) == 0)
2683 GetMagickToken(q,&q,token);
2684 affine.tx=StringToDouble(token,(char **) NULL);
2685 GetMagickToken(q,&q,token);
2687 GetMagickToken(q,&q,token);
2688 affine.ty=StringToDouble(token,(char **) NULL);
2697 if (LocaleCompare("viewbox",keyword) == 0)
2699 GetMagickToken(q,&q,token);
2700 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2701 (char **) NULL)-0.5);
2702 GetMagickToken(q,&q,token);
2704 GetMagickToken(q,&q,token);
2705 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2706 (char **) NULL)-0.5);
2707 GetMagickToken(q,&q,token);
2709 GetMagickToken(q,&q,token);
2710 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2711 token,(char **) NULL)+0.5);
2712 GetMagickToken(q,&q,token);
2714 GetMagickToken(q,&q,token);
2715 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2716 token,(char **) NULL)+0.5);
2728 if (status == MagickFalse)
2730 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2731 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2733 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2734 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2735 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2736 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2737 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2739 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2742 if (primitive_type == UndefinedPrimitive)
2744 if (image->debug != MagickFalse)
2745 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2750 Parse the primitive attributes.
2754 primitive_info[0].point.x=0.0;
2755 primitive_info[0].point.y=0.0;
2756 for (x=0; *q != '\0'; x++)
2761 if (IsPoint(q) == MagickFalse)
2763 GetMagickToken(q,&q,token);
2764 point.x=StringToDouble(token,(char **) NULL);
2765 GetMagickToken(q,&q,token);
2767 GetMagickToken(q,&q,token);
2768 point.y=StringToDouble(token,(char **) NULL);
2769 GetMagickToken(q,(const char **) NULL,token);
2771 GetMagickToken(q,&q,token);
2772 primitive_info[i].primitive=primitive_type;
2773 primitive_info[i].point=point;
2774 primitive_info[i].coordinates=0;
2775 primitive_info[i].method=FloodfillMethod;
2777 if (i < (ssize_t) number_points)
2780 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2781 (size_t) number_points,sizeof(*primitive_info));
2782 if (primitive_info == (PrimitiveInfo *) NULL)
2784 (void) ThrowMagickException(exception,GetMagickModule(),
2785 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
2789 primitive_info[j].primitive=primitive_type;
2790 primitive_info[j].coordinates=(size_t) x;
2791 primitive_info[j].method=FloodfillMethod;
2792 primitive_info[j].text=(char *) NULL;
2794 Circumscribe primitive within a circle.
2796 bounds.x1=primitive_info[j].point.x;
2797 bounds.y1=primitive_info[j].point.y;
2798 bounds.x2=primitive_info[j].point.x;
2799 bounds.y2=primitive_info[j].point.y;
2800 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2802 point=primitive_info[j+k].point;
2803 if (point.x < bounds.x1)
2805 if (point.y < bounds.y1)
2807 if (point.x > bounds.x2)
2809 if (point.y > bounds.y2)
2813 Speculate how many points our primitive might consume.
2815 length=primitive_info[j].coordinates;
2816 switch (primitive_type)
2818 case RectanglePrimitive:
2823 case RoundRectanglePrimitive:
2825 length*=5+8*BezierQuantum;
2828 case BezierPrimitive:
2830 if (primitive_info[j].coordinates > 107)
2831 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2832 "TooManyBezierCoordinates","'%s'",token);
2833 length=BezierQuantum*primitive_info[j].coordinates;
2842 GetMagickToken(q,&q,token);
2845 for (s=token; *s != '\0'; s=t)
2850 value=StringToDouble(s,&t);
2859 length=length*BezierQuantum/2;
2862 case CirclePrimitive:
2864 case EllipsePrimitive:
2871 alpha=bounds.x2-bounds.x1;
2872 beta=bounds.y2-bounds.y1;
2873 radius=hypot((double) alpha,(double) beta);
2874 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2880 if ((size_t) (i+length) >= number_points)
2883 Resize based on speculative points required by primitive.
2885 number_points+=length+1;
2886 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2887 (size_t) number_points,sizeof(*primitive_info));
2888 if (primitive_info == (PrimitiveInfo *) NULL)
2890 (void) ThrowMagickException(exception,GetMagickModule(),
2891 ResourceLimitError,"MemoryAllocationFailed","'%s'",
2896 switch (primitive_type)
2898 case PointPrimitive:
2901 if (primitive_info[j].coordinates != 1)
2906 TracePoint(primitive_info+j,primitive_info[j].point);
2907 i=(ssize_t) (j+primitive_info[j].coordinates);
2912 if (primitive_info[j].coordinates != 2)
2917 TraceLine(primitive_info+j,primitive_info[j].point,
2918 primitive_info[j+1].point);
2919 i=(ssize_t) (j+primitive_info[j].coordinates);
2922 case RectanglePrimitive:
2924 if (primitive_info[j].coordinates != 2)
2929 TraceRectangle(primitive_info+j,primitive_info[j].point,
2930 primitive_info[j+1].point);
2931 i=(ssize_t) (j+primitive_info[j].coordinates);
2934 case RoundRectanglePrimitive:
2936 if (primitive_info[j].coordinates != 3)
2941 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2942 primitive_info[j+1].point,primitive_info[j+2].point);
2943 i=(ssize_t) (j+primitive_info[j].coordinates);
2948 if (primitive_info[j].coordinates != 3)
2950 primitive_type=UndefinedPrimitive;
2953 TraceArc(primitive_info+j,primitive_info[j].point,
2954 primitive_info[j+1].point,primitive_info[j+2].point);
2955 i=(ssize_t) (j+primitive_info[j].coordinates);
2958 case EllipsePrimitive:
2960 if (primitive_info[j].coordinates != 3)
2965 TraceEllipse(primitive_info+j,primitive_info[j].point,
2966 primitive_info[j+1].point,primitive_info[j+2].point);
2967 i=(ssize_t) (j+primitive_info[j].coordinates);
2970 case CirclePrimitive:
2972 if (primitive_info[j].coordinates != 2)
2977 TraceCircle(primitive_info+j,primitive_info[j].point,
2978 primitive_info[j+1].point);
2979 i=(ssize_t) (j+primitive_info[j].coordinates);
2982 case PolylinePrimitive:
2984 case PolygonPrimitive:
2986 primitive_info[i]=primitive_info[j];
2987 primitive_info[i].coordinates=0;
2988 primitive_info[j].coordinates++;
2992 case BezierPrimitive:
2994 if (primitive_info[j].coordinates < 3)
2999 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3000 i=(ssize_t) (j+primitive_info[j].coordinates);
3005 i=(ssize_t) (j+TracePath(primitive_info+j,token));
3008 case ColorPrimitive:
3009 case MattePrimitive:
3014 if (primitive_info[j].coordinates != 1)
3019 GetMagickToken(q,&q,token);
3020 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
3024 primitive_info[j].method=(PaintMethod) method;
3029 if (primitive_info[j].coordinates != 1)
3035 GetMagickToken(q,&q,token);
3036 primitive_info[j].text=AcquireString(token);
3039 case ImagePrimitive:
3041 if (primitive_info[j].coordinates != 2)
3046 GetMagickToken(q,&q,token);
3047 primitive_info[j].text=AcquireString(token);
3051 if (primitive_info == (PrimitiveInfo *) NULL)
3053 if (image->debug != MagickFalse)
3054 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3055 if (status == MagickFalse)
3057 primitive_info[i].primitive=UndefinedPrimitive;
3063 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3065 point=primitive_info[i].point;
3066 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3067 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3068 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3069 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3070 point=primitive_info[i].point;
3071 if (point.x < graphic_context[n]->bounds.x1)
3072 graphic_context[n]->bounds.x1=point.x;
3073 if (point.y < graphic_context[n]->bounds.y1)
3074 graphic_context[n]->bounds.y1=point.y;
3075 if (point.x > graphic_context[n]->bounds.x2)
3076 graphic_context[n]->bounds.x2=point.x;
3077 if (point.y > graphic_context[n]->bounds.y2)
3078 graphic_context[n]->bounds.y2=point.y;
3079 if (primitive_info[i].primitive == ImagePrimitive)
3081 if (i >= (ssize_t) number_points)
3082 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3084 if (graphic_context[n]->render != MagickFalse)
3086 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3087 (LocaleCompare(graphic_context[n]->clip_mask,
3088 graphic_context[n-1]->clip_mask) != 0))
3089 (void) DrawClipPath(image,graphic_context[n],
3090 graphic_context[n]->clip_mask,exception);
3091 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
3093 if (primitive_info->text != (char *) NULL)
3094 primitive_info->text=(char *) RelinquishMagickMemory(
3095 primitive_info->text);
3096 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3098 if (proceed == MagickFalse)
3101 if (image->debug != MagickFalse)
3102 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3104 Relinquish resources.
3106 token=DestroyString(token);
3107 if (primitive_info != (PrimitiveInfo *) NULL)
3108 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3109 primitive=DestroyString(primitive);
3110 for ( ; n >= 0; n--)
3111 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3112 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3113 if (status == MagickFalse)
3114 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3124 % D r a w G r a d i e n t I m a g e %
3128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3130 % DrawGradientImage() draws a linear gradient on the image.
3132 % The format of the DrawGradientImage method is:
3134 % MagickBooleanType DrawGradientImage(Image *image,
3135 % const DrawInfo *draw_info,ExceptionInfo *exception)
3137 % A description of each parameter follows:
3139 % o image: the image.
3141 % o draw_info: the draw info.
3143 % o exception: return any errors or warnings in this structure.
3147 static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
3148 const ssize_t x,const ssize_t y)
3150 switch (gradient->type)
3152 case UndefinedGradient:
3153 case LinearGradient:
3168 gradient_vector=(&gradient->gradient_vector);
3169 p.x=gradient_vector->x2-gradient_vector->x1;
3170 p.y=gradient_vector->y2-gradient_vector->y1;
3171 q.x=(double) x-gradient_vector->x1;
3172 q.y=(double) y-gradient_vector->y1;
3173 length=sqrt(q.x*q.x+q.y*q.y);
3174 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3175 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3176 scale=p.x*q.x+p.y*q.y;
3177 offset=gamma*scale*length;
3180 case RadialGradient:
3189 v.x=(double) x-gradient->center.x;
3190 v.y=(double) y-gradient->center.y;
3191 length=sqrt(v.x*v.x+v.y*v.y);
3192 if (gradient->spread == RepeatSpread)
3194 offset=length/gradient->radius;
3201 MagickExport MagickBooleanType DrawGradientImage(Image *image,
3202 const DrawInfo *draw_info,ExceptionInfo *exception)
3232 Draw linear or radial gradient on image.
3234 assert(image != (Image *) NULL);
3235 assert(image->signature == MagickSignature);
3236 if (image->debug != MagickFalse)
3237 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3238 assert(draw_info != (const DrawInfo *) NULL);
3239 gradient=(&draw_info->gradient);
3240 gradient_vector=(&gradient->gradient_vector);
3241 point.x=gradient_vector->x2-gradient_vector->x1;
3242 point.y=gradient_vector->y2-gradient_vector->y1;
3243 length=sqrt(point.x*point.x+point.y*point.y);
3244 bounding_box=gradient->bounding_box;
3246 GetPixelInfo(image,&zero);
3247 image_view=AcquireAuthenticCacheView(image,exception);
3248 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3249 #pragma omp parallel for schedule(static) shared(status)
3251 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3271 if (status == MagickFalse)
3273 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3274 if (q == (Quantum *) NULL)
3281 offset=GetStopColorOffset(gradient,0,y);
3282 if (gradient->type != RadialGradient)
3284 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3286 GetPixelInfoPixel(image,q,&pixel);
3287 switch (gradient->spread)
3289 case UndefinedSpread:
3292 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3293 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3295 offset=GetStopColorOffset(gradient,x,y);
3296 if (gradient->type != RadialGradient)
3299 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3300 if (offset < gradient->stops[i].offset)
3302 if ((offset < 0.0) || (i == 0))
3303 composite=gradient->stops[0].color;
3305 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3306 composite=gradient->stops[gradient->number_stops-1].color;
3311 alpha=(offset-gradient->stops[i].offset)/
3312 (gradient->stops[j].offset-gradient->stops[i].offset);
3313 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3314 &gradient->stops[j].color,alpha,&composite);
3320 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3321 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3323 offset=GetStopColorOffset(gradient,x,y);
3324 if (gradient->type != RadialGradient)
3329 if ((ssize_t) fmod(offset,2.0) == 0)
3330 offset=fmod(offset,1.0);
3332 offset=1.0-fmod(offset,1.0);
3333 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3334 if (offset < gradient->stops[i].offset)
3337 composite=gradient->stops[0].color;
3339 if (i == (ssize_t) gradient->number_stops)
3340 composite=gradient->stops[gradient->number_stops-1].color;
3345 alpha=(offset-gradient->stops[i].offset)/
3346 (gradient->stops[j].offset-gradient->stops[i].offset);
3347 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3348 &gradient->stops[j].color,alpha,&composite);
3360 antialias=MagickFalse;
3362 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3363 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3365 offset=GetStopColorOffset(gradient,x,y);
3366 if (gradient->type == LinearGradient)
3368 repeat=fmod(offset,length);
3370 repeat=length-fmod(-repeat,length);
3372 repeat=fmod(offset,length);
3373 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3374 MagickTrue : MagickFalse;
3375 offset=repeat/length;
3379 repeat=fmod(offset,gradient->radius);
3381 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3383 repeat=fmod(offset,gradient->radius);
3384 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3386 offset=repeat/gradient->radius;
3389 for (i=0; i < (ssize_t) gradient->number_stops; i++)
3390 if (offset < gradient->stops[i].offset)
3393 composite=gradient->stops[0].color;
3395 if (i == (ssize_t) gradient->number_stops)
3396 composite=gradient->stops[gradient->number_stops-1].color;
3401 alpha=(offset-gradient->stops[i].offset)/
3402 (gradient->stops[j].offset-gradient->stops[i].offset);
3403 if (antialias != MagickFalse)
3405 if (gradient->type == LinearGradient)
3406 alpha=length-repeat;
3408 alpha=gradient->radius-repeat;
3410 j=(ssize_t) gradient->number_stops-1L;
3412 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3413 &gradient->stops[j].color,alpha,&composite);
3418 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3420 SetPixelInfoPixel(image,&pixel,q);
3421 q+=GetPixelChannels(image);
3423 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3426 image_view=DestroyCacheView(image_view);
3431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3435 % D r a w P a t t e r n P a t h %
3439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3441 % DrawPatternPath() draws a pattern.
3443 % The format of the DrawPatternPath method is:
3445 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3446 % const char *name,Image **pattern,ExceptionInfo *exception)
3448 % A description of each parameter follows:
3450 % o image: the image.
3452 % o draw_info: the draw info.
3454 % o name: the pattern name.
3456 % o image: the image.
3458 % o exception: return any errors or warnings in this structure.
3461 MagickExport MagickBooleanType DrawPatternPath(Image *image,
3462 const DrawInfo *draw_info,const char *name,Image **pattern,
3463 ExceptionInfo *exception)
3466 property[MaxTextExtent];
3481 assert(image != (Image *) NULL);
3482 assert(image->signature == MagickSignature);
3483 if (image->debug != MagickFalse)
3484 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3485 assert(draw_info != (const DrawInfo *) NULL);
3486 assert(name != (const char *) NULL);
3487 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3488 path=GetImageArtifact(image,property);
3489 if (path == (const char *) NULL)
3490 return(MagickFalse);
3491 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3492 geometry=GetImageArtifact(image,property);
3493 if (geometry == (const char *) NULL)
3494 return(MagickFalse);
3495 if ((*pattern) != (Image *) NULL)
3496 *pattern=DestroyImage(*pattern);
3497 image_info=AcquireImageInfo();
3498 image_info->size=AcquireString(geometry);
3499 *pattern=AcquireImage(image_info,exception);
3500 image_info=DestroyImageInfo(image_info);
3501 (void) QueryColorCompliance("#00000000",AllCompliance,
3502 &(*pattern)->background_color,exception);
3503 (void) SetImageBackgroundColor(*pattern,exception);
3504 if (image->debug != MagickFalse)
3505 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3506 "begin pattern-path %s %s",name,geometry);
3507 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3508 clone_info->fill_pattern=NewImageList();
3509 clone_info->stroke_pattern=NewImageList();
3510 (void) CloneString(&clone_info->primitive,path);
3511 status=DrawImage(*pattern,clone_info,exception);
3512 clone_info=DestroyDrawInfo(clone_info);
3513 if (image->debug != MagickFalse)
3514 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3523 + D r a w P o l y g o n P r i m i t i v e %
3527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3529 % DrawPolygonPrimitive() draws a polygon on the image.
3531 % The format of the DrawPolygonPrimitive method is:
3533 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3534 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3535 % ExceptionInfo *exception)
3537 % A description of each parameter follows:
3539 % o image: the image.
3541 % o draw_info: the draw info.
3543 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3545 % o exception: return any errors or warnings in this structure.
3549 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3554 assert(polygon_info != (PolygonInfo **) NULL);
3555 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3556 if (polygon_info[i] != (PolygonInfo *) NULL)
3557 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3558 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3559 return(polygon_info);
3562 static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3563 const PrimitiveInfo *primitive_info)
3566 *restrict path_info;
3577 number_threads=GetOpenMPMaximumThreads();
3578 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3579 sizeof(*polygon_info));
3580 if (polygon_info == (PolygonInfo **) NULL)
3581 return((PolygonInfo **) NULL);
3582 (void) ResetMagickMemory(polygon_info,0,GetOpenMPMaximumThreads()*
3583 sizeof(*polygon_info));
3584 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3585 if (path_info == (PathInfo *) NULL)
3586 return(DestroyPolygonThreadSet(polygon_info));
3587 for (i=0; i < (ssize_t) number_threads; i++)
3589 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3590 if (polygon_info[i] == (PolygonInfo *) NULL)
3591 return(DestroyPolygonThreadSet(polygon_info));
3593 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3594 return(polygon_info);
3597 static MagickRealType GetFillAlpha(PolygonInfo *polygon_info,
3598 const MagickRealType mid,const MagickBooleanType fill,
3599 const FillRule fill_rule,const double x,const double y,
3600 MagickRealType *stroke_alpha)
3614 register const PointInfo
3625 Compute fill & stroke opacity for this (x,y) point.
3629 p=polygon_info->edges;
3630 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3632 if (y <= (p->bounds.y1-mid-0.5))
3634 if (y > (p->bounds.y2+mid+0.5))
3636 (void) DestroyEdge(polygon_info,(size_t) j);
3639 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
3641 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3642 for ( ; i < (ssize_t) p->number_points; i++)
3644 if (y <= (p->points[i-1].y-mid-0.5))
3646 if (y > (p->points[i].y+mid+0.5))
3648 if (p->scanline != y)
3651 p->highwater=(size_t) i;
3654 Compute distance between a point and an edge.
3657 delta.x=(q+1)->x-q->x;
3658 delta.y=(q+1)->y-q->y;
3659 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3664 distance=delta.x*delta.x+delta.y*delta.y;
3668 alpha=delta.x*delta.x+delta.y*delta.y;
3673 distance=delta.x*delta.x+delta.y*delta.y;
3678 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3679 distance=alpha*beta*beta;
3683 Compute stroke & subpath opacity.
3686 if (p->ghostline == MagickFalse)
3689 if ((*stroke_alpha < 1.0) &&
3690 (distance <= ((alpha+0.25)*(alpha+0.25))))
3693 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3698 if (distance != 1.0)
3699 beta=sqrt((double) distance);
3701 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3702 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3706 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3708 if (distance <= 0.0)
3718 if (distance != 1.0)
3719 beta=sqrt(distance);
3722 if (subpath_alpha < (alpha*alpha))
3723 subpath_alpha=alpha*alpha;
3727 Compute fill opacity.
3729 if (fill == MagickFalse)
3731 if (subpath_alpha >= 1.0)
3734 Determine winding number.
3737 p=polygon_info->edges;
3738 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3740 if (y <= p->bounds.y1)
3742 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3744 if (x > p->bounds.x2)
3746 winding_number+=p->direction ? 1 : -1;
3749 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3750 for ( ; i < (ssize_t) p->number_points; i++)
3751 if (y <= p->points[i].y)
3754 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3755 winding_number+=p->direction ? 1 : -1;
3757 if (fill_rule != NonZeroRule)
3759 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3763 if (MagickAbsoluteValue(winding_number) != 0)
3765 return(subpath_alpha);
3768 static MagickBooleanType DrawPolygonPrimitive(Image *image,
3769 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3770 ExceptionInfo *exception)
3783 **restrict polygon_info;
3800 Compute bounding box.
3802 assert(image != (Image *) NULL);
3803 assert(image->signature == MagickSignature);
3804 if (image->debug != MagickFalse)
3805 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3806 assert(draw_info != (DrawInfo *) NULL);
3807 assert(draw_info->signature == MagickSignature);
3808 assert(primitive_info != (PrimitiveInfo *) NULL);
3809 if (primitive_info->coordinates == 0)
3811 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3812 if (polygon_info == (PolygonInfo **) NULL)
3813 return(MagickFalse);
3815 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
3816 if (image->debug != MagickFalse)
3817 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3818 fill=(primitive_info->method == FillToBorderMethod) ||
3819 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3820 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3821 bounds=polygon_info[0]->edges[0].bounds;
3822 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3824 p=polygon_info[0]->edges+i;
3825 if (p->bounds.x1 < bounds.x1)
3826 bounds.x1=p->bounds.x1;
3827 if (p->bounds.y1 < bounds.y1)
3828 bounds.y1=p->bounds.y1;
3829 if (p->bounds.x2 > bounds.x2)
3830 bounds.x2=p->bounds.x2;
3831 if (p->bounds.y2 > bounds.y2)
3832 bounds.y2=p->bounds.y2;
3834 bounds.x1-=(mid+1.0);
3835 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3836 image->columns ? (double) image->columns-1.0 : bounds.x1;
3837 bounds.y1-=(mid+1.0);
3838 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3839 image->rows ? (double) image->rows-1.0 : bounds.y1;
3840 bounds.x2+=(mid+1.0);
3841 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3842 image->columns ? (double) image->columns-1.0 : bounds.x2;
3843 bounds.y2+=(mid+1.0);
3844 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3845 image->rows ? (double) image->rows-1.0 : bounds.y2;
3847 image_view=AcquireAuthenticCacheView(image,exception);
3848 if (primitive_info->coordinates == 1)
3853 start=(ssize_t) ceil(bounds.y1-0.5);
3854 stop=(ssize_t) floor(bounds.y2+0.5);
3855 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3856 #pragma omp parallel for schedule(static) shared(status)
3858 for (y=start; y <= stop; y++)
3876 if (status == MagickFalse)
3878 start=(ssize_t) ceil(bounds.x1-0.5);
3879 stop=(ssize_t) floor(bounds.x2+0.5);
3881 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
3883 if (q == (Quantum *) NULL)
3888 GetPixelInfo(image,&pixel);
3889 for ( ; x <= stop; x++)
3891 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3892 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3894 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
3895 SetPixelInfoPixel(image,&pixel,q);
3897 q+=GetPixelChannels(image);
3899 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3900 if (sync == MagickFalse)
3903 image_view=DestroyCacheView(image_view);
3904 polygon_info=DestroyPolygonThreadSet(polygon_info);
3905 if (image->debug != MagickFalse)
3906 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3907 " end draw-polygon");
3911 Draw polygon or line.
3913 if (image->matte == MagickFalse)
3914 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3915 start=(ssize_t) ceil(bounds.y1-0.5);
3916 stop=(ssize_t) floor(bounds.y2+0.5);
3917 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3918 #pragma omp parallel for schedule(static) shared(status)
3920 for (y=start; y <= stop; y++)
3923 id = GetOpenMPThreadId();
3943 if (status == MagickFalse)
3945 start=(ssize_t) ceil(bounds.x1-0.5);
3946 stop=(ssize_t) floor(bounds.x2+0.5);
3947 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3949 if (q == (Quantum *) NULL)
3954 for (x=start; x <= stop; x++)
3959 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
3960 (double) x,(double) y,&stroke_alpha);
3961 if (draw_info->stroke_antialias == MagickFalse)
3963 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3964 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
3966 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
3967 fill_alpha=fill_alpha*fill_color.alpha;
3968 CompositePixelOver(image,&fill_color,fill_alpha,q,(MagickRealType)
3969 GetPixelAlpha(image,q),q);
3970 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
3971 stroke_alpha=stroke_alpha*stroke_color.alpha;
3972 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(MagickRealType)
3973 GetPixelAlpha(image,q),q);
3974 q+=GetPixelChannels(image);
3976 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3979 image_view=DestroyCacheView(image_view);
3980 polygon_info=DestroyPolygonThreadSet(polygon_info);
3981 if (image->debug != MagickFalse)
3982 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3991 % D r a w P r i m i t i v e %
3995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3997 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
3999 % The format of the DrawPrimitive method is:
4001 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4002 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4004 % A description of each parameter follows:
4006 % o image: the image.
4008 % o draw_info: the draw info.
4010 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4012 % o exception: return any errors or warnings in this structure.
4016 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4042 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4043 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4044 switch (primitive_info->primitive)
4046 case PointPrimitive:
4048 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4049 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4050 methods[primitive_info->method]);
4053 case ColorPrimitive:
4055 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4056 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4057 methods[primitive_info->method]);
4060 case MattePrimitive:
4062 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4063 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4064 methods[primitive_info->method]);
4069 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4070 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4073 case ImagePrimitive:
4075 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4076 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4083 p=primitive_info[0].point;
4086 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4088 point=primitive_info[i].point;
4089 if (coordinates <= 0)
4091 coordinates=(ssize_t) primitive_info[i].coordinates;
4092 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4093 " begin open (%.20g)",(double) coordinates);
4096 point=primitive_info[i].point;
4097 if ((fabs(q.x-point.x) > MagickEpsilon) ||
4098 (fabs(q.y-point.y) > MagickEpsilon))
4099 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4100 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4102 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4103 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4106 if (coordinates > 0)
4108 if ((fabs(p.x-point.x) > MagickEpsilon) ||
4109 (fabs(p.y-point.y) > MagickEpsilon))
4110 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4111 (double) coordinates);
4113 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4114 (double) coordinates);
4118 MagickExport MagickBooleanType DrawPrimitive(Image *image,
4119 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4120 ExceptionInfo *exception)
4135 if (image->debug != MagickFalse)
4137 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4138 " begin draw-primitive");
4139 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4140 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4141 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4142 draw_info->affine.tx,draw_info->affine.ty);
4145 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4146 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4147 image_view=AcquireAuthenticCacheView(image,exception);
4148 switch (primitive_info->primitive)
4150 case PointPrimitive:
4158 if ((y < 0) || (y >= (ssize_t) image->rows))
4160 if ((x < 0) || (x >= (ssize_t) image->columns))
4162 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4163 if (q == (Quantum *) NULL)
4165 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
4166 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4167 (MagickRealType) GetPixelAlpha(image,q),q);
4168 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4171 case ColorPrimitive:
4173 switch (primitive_info->method)
4184 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4185 if (q == (Quantum *) NULL)
4187 GetPixelInfo(image,&pixel);
4188 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4189 SetPixelInfoPixel(image,&pixel,q);
4190 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4202 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4204 for (y=0; y < (ssize_t) image->rows; y++)
4209 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4211 if (q == (Quantum *) NULL)
4213 for (x=0; x < (ssize_t) image->columns; x++)
4215 GetPixelInfoPixel(image,q,&pixel);
4216 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4218 q+=GetPixelChannels(image);
4221 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4222 SetPixelInfoPixel(image,&pixel,q);
4223 q+=GetPixelChannels(image);
4225 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4226 if (sync == MagickFalse)
4231 case FloodfillMethod:
4232 case FillToBorderMethod:
4237 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4239 if (primitive_info->method == FillToBorderMethod)
4241 target.red=(MagickRealType) draw_info->border_color.red;
4242 target.green=(MagickRealType) draw_info->border_color.green;
4243 target.blue=(MagickRealType) draw_info->border_color.blue;
4245 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4246 primitive_info->method == FloodfillMethod ? MagickFalse :
4247 MagickTrue,exception);
4258 GetPixelInfo(image,&pixel);
4259 for (y=0; y < (ssize_t) image->rows; y++)
4267 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4269 if (q == (Quantum *) NULL)
4271 for (x=0; x < (ssize_t) image->columns; x++)
4273 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4274 SetPixelInfoPixel(image,&pixel,q);
4275 q+=GetPixelChannels(image);
4277 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4278 if (sync == MagickFalse)
4286 case MattePrimitive:
4288 if (image->matte == MagickFalse)
4289 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4290 switch (primitive_info->method)
4301 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4302 if (q == (Quantum *) NULL)
4304 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4305 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4306 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4318 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4320 for (y=0; y < (ssize_t) image->rows; y++)
4328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4330 if (q == (Quantum *) NULL)
4332 for (x=0; x < (ssize_t) image->columns; x++)
4334 GetPixelInfoPixel(image,q,&pixel);
4335 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4337 q+=GetPixelChannels(image);
4340 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4341 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4342 q+=GetPixelChannels(image);
4344 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4345 if (sync == MagickFalse)
4350 case FloodfillMethod:
4351 case FillToBorderMethod:
4359 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
4361 if (primitive_info->method == FillToBorderMethod)
4363 target.red=(MagickRealType) draw_info->border_color.red;
4364 target.green=(MagickRealType) draw_info->border_color.green;
4365 target.blue=(MagickRealType) draw_info->border_color.blue;
4367 channel_mask=SetPixelChannelMask(image,AlphaChannel);
4368 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4369 primitive_info->method == FloodfillMethod ? MagickFalse :
4370 MagickTrue,exception);
4371 (void) SetPixelChannelMask(image,channel_mask);
4382 for (y=0; y < (ssize_t) image->rows; y++)
4390 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4392 if (q == (Quantum *) NULL)
4394 for (x=0; x < (ssize_t) image->columns; x++)
4396 (void) GetFillColor(draw_info,x,y,&pixel,exception);
4397 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4398 q+=GetPixelChannels(image);
4400 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4401 if (sync == MagickFalse)
4412 geometry[MaxTextExtent];
4417 if (primitive_info->text == (char *) NULL)
4419 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4420 (void) CloneString(&clone_info->text,primitive_info->text);
4421 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4422 primitive_info->point.x,primitive_info->point.y);
4423 (void) CloneString(&clone_info->geometry,geometry);
4424 status=AnnotateImage(image,clone_info,exception);
4425 clone_info=DestroyDrawInfo(clone_info);
4428 case ImagePrimitive:
4434 composite_geometry[MaxTextExtent];
4449 if (primitive_info->text == (char *) NULL)
4451 clone_info=AcquireImageInfo();
4452 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4453 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4457 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4459 composite_image=ReadImage(clone_info,exception);
4461 clone_info=DestroyImageInfo(clone_info);
4462 if (composite_image == (Image *) NULL)
4464 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4465 NULL,(void *) NULL);
4466 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4467 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4468 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4469 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4472 geometry[MaxTextExtent];
4477 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4478 primitive_info[1].point.x,primitive_info[1].point.y);
4479 composite_image->filter=image->filter;
4480 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4483 if (composite_image->matte == MagickFalse)
4484 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4486 if (draw_info->alpha != OpaqueAlpha)
4487 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4488 SetGeometry(image,&geometry);
4489 image->gravity=draw_info->gravity;
4492 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4493 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4494 composite_image->rows,(double) geometry.x,(double) geometry.y);
4495 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4496 affine=draw_info->affine;
4497 affine.tx=(double) geometry.x;
4498 affine.ty=(double) geometry.y;
4499 composite_image->interpolate=image->interpolate;
4500 if (draw_info->compose == OverCompositeOp)
4501 (void) DrawAffineImage(image,composite_image,&affine,exception);
4503 (void) CompositeImage(image,composite_image,draw_info->compose,
4504 MagickTrue,geometry.x,geometry.y,exception);
4505 composite_image=DestroyImage(composite_image);
4517 if (IsEventLogging() != MagickFalse)
4518 LogPrimitiveInfo(primitive_info);
4519 scale=ExpandAffine(&draw_info->affine);
4520 if ((draw_info->dash_pattern != (double *) NULL) &&
4521 (draw_info->dash_pattern[0] != 0.0) &&
4522 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
4523 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4528 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4529 clone_info->stroke_width=0.0;
4530 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4531 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4533 clone_info=DestroyDrawInfo(clone_info);
4534 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4537 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4539 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4545 Draw strokes while respecting line cap/join attributes.
4547 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4549 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4550 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4551 MagickTrue : MagickFalse;
4552 i=(ssize_t) primitive_info[0].coordinates;
4553 if ((((draw_info->linecap == RoundCap) ||
4554 (closed_path != MagickFalse)) &&
4555 (draw_info->linejoin == RoundJoin)) ||
4556 (primitive_info[i].primitive != UndefinedPrimitive))
4558 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4562 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4563 clone_info->stroke_width=0.0;
4564 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4565 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4567 clone_info=DestroyDrawInfo(clone_info);
4568 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4571 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4575 image_view=DestroyCacheView(image_view);
4576 if (image->debug != MagickFalse)
4577 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4578 return(status != 0 ? MagickTrue : MagickFalse);
4582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4586 + D r a w S t r o k e P o l y g o n %
4590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4592 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4593 % the image while respecting the line cap and join attributes.
4595 % The format of the DrawStrokePolygon method is:
4597 % MagickBooleanType DrawStrokePolygon(Image *image,
4598 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4600 % A description of each parameter follows:
4602 % o image: the image.
4604 % o draw_info: the draw info.
4606 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4611 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4612 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4620 for (i=0; i < 4; i++)
4621 linecap[i]=(*primitive_info);
4622 linecap[0].coordinates=4;
4623 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4624 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4625 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4626 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4627 linecap[4].primitive=UndefinedPrimitive;
4628 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4631 static MagickBooleanType DrawStrokePolygon(Image *image,
4632 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4633 ExceptionInfo *exception)
4645 register const PrimitiveInfo
4650 Draw stroked polygon.
4652 if (image->debug != MagickFalse)
4653 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4654 " begin draw-stroke-polygon");
4655 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4656 clone_info->fill=draw_info->stroke;
4657 if (clone_info->fill_pattern != (Image *) NULL)
4658 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4659 if (clone_info->stroke_pattern != (Image *) NULL)
4660 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4661 MagickTrue,exception);
4662 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
4663 clone_info->stroke_width=0.0;
4664 clone_info->fill_rule=NonZeroRule;
4666 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4668 stroke_polygon=TraceStrokePolygon(draw_info,p);
4669 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4670 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4671 q=p+p->coordinates-1;
4672 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4673 MagickTrue : MagickFalse;
4674 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4676 DrawRoundLinecap(image,draw_info,p,exception);
4677 DrawRoundLinecap(image,draw_info,q,exception);
4680 clone_info=DestroyDrawInfo(clone_info);
4681 if (image->debug != MagickFalse)
4682 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4683 " end draw-stroke-polygon");
4688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4692 % G e t A f f i n e M a t r i x %
4696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4698 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4701 % The format of the GetAffineMatrix method is:
4703 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4705 % A description of each parameter follows:
4707 % o affine_matrix: the affine matrix.
4710 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4713 assert(affine_matrix != (AffineMatrix *) NULL);
4714 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4715 affine_matrix->sx=1.0;
4716 affine_matrix->sy=1.0;
4720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4724 + G e t D r a w I n f o %
4728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4730 % GetDrawInfo() initializes draw_info to default values from image_info.
4732 % The format of the GetDrawInfo method is:
4734 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4736 % A description of each parameter follows:
4738 % o image_info: the image info..
4740 % o draw_info: the draw info.
4743 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4752 Initialize draw attributes.
4754 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4755 assert(draw_info != (DrawInfo *) NULL);
4756 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4757 GetAffineMatrix(&draw_info->affine);
4758 exception=AcquireExceptionInfo();
4759 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4761 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4763 draw_info->stroke_width=1.0;
4764 draw_info->alpha=OpaqueAlpha;
4765 draw_info->fill_rule=EvenOddRule;
4766 draw_info->linecap=ButtCap;
4767 draw_info->linejoin=MiterJoin;
4768 draw_info->miterlimit=10;
4769 draw_info->decorate=NoDecoration;
4770 draw_info->pointsize=12.0;
4771 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
4772 draw_info->compose=OverCompositeOp;
4773 draw_info->render=MagickTrue;
4774 draw_info->debug=IsEventLogging();
4775 if (image_info != (ImageInfo *) NULL)
4777 draw_info->stroke_antialias=image_info->antialias;
4778 if (image_info->font != (char *) NULL)
4779 draw_info->font=AcquireString(image_info->font);
4780 if (image_info->density != (char *) NULL)
4781 draw_info->density=AcquireString(image_info->density);
4782 draw_info->text_antialias=image_info->antialias;
4783 if (image_info->pointsize != 0.0)
4784 draw_info->pointsize=image_info->pointsize;
4785 draw_info->border_color=image_info->border_color;
4786 if (image_info->server_name != (char *) NULL)
4787 draw_info->server_name=AcquireString(image_info->server_name);
4788 option=GetImageOption(image_info,"encoding");
4789 if (option != (const char *) NULL)
4790 (void) CloneString(&draw_info->encoding,option);
4791 option=GetImageOption(image_info,"kerning");
4792 if (option != (const char *) NULL)
4793 draw_info->kerning=StringToDouble(option,(char **) NULL);
4794 option=GetImageOption(image_info,"interline-spacing");
4795 if (option != (const char *) NULL)
4796 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4797 option=GetImageOption(image_info,"interword-spacing");
4798 if (option != (const char *) NULL)
4799 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4800 option=GetImageOption(image_info,"direction");
4801 if (option != (const char *) NULL)
4802 draw_info->direction=(DirectionType) ParseCommandOption(
4803 MagickDirectionOptions,MagickFalse,option);
4805 draw_info->direction=UndefinedDirection;
4806 option=GetImageOption(image_info,"fill");
4807 if (option != (const char *) NULL)
4808 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4810 option=GetImageOption(image_info,"stroke");
4811 if (option != (const char *) NULL)
4812 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4814 option=GetImageOption(image_info,"strokewidth");
4815 if (option != (const char *) NULL)
4816 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4817 option=GetImageOption(image_info,"undercolor");
4818 if (option != (const char *) NULL)
4819 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4821 option=GetImageOption(image_info,"gravity");
4822 if (option != (const char *) NULL)
4823 draw_info->gravity=(GravityType) ParseCommandOption(
4824 MagickGravityOptions,MagickFalse,option);
4826 exception=DestroyExceptionInfo(exception);
4827 draw_info->signature=MagickSignature;
4831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4835 + P e r m u t a t e %
4839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4841 % Permutate() returns the permuation of the (n,k).
4843 % The format of the Permutate method is:
4845 % void Permutate(ssize_t n,ssize_t k)
4847 % A description of each parameter follows:
4855 static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
4864 for (i=k+1; i <= n; i++)
4866 for (i=1; i <= (n-k); i++)
4872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4876 + T r a c e P r i m i t i v e %
4880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4882 % TracePrimitive is a collection of methods for generating graphic
4883 % primitives such as arcs, ellipses, paths, etc.
4887 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4888 const PointInfo end,const PointInfo degrees)
4894 center.x=0.5*(end.x+start.x);
4895 center.y=0.5*(end.y+start.y);
4896 radii.x=fabs(center.x-start.x);
4897 radii.y=fabs(center.y-start.y);
4898 TraceEllipse(primitive_info,center,radii,degrees);
4901 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4902 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4903 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4918 register MagickRealType
4922 register PrimitiveInfo
4931 if ((start.x == end.x) && (start.y == end.y))
4933 TracePoint(primitive_info,end);
4936 radii.x=fabs(arc.x);
4937 radii.y=fabs(arc.y);
4938 if ((radii.x == 0.0) || (radii.y == 0.0))
4940 TraceLine(primitive_info,start,end);
4943 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4944 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4945 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4946 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4947 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4949 if (delta < MagickEpsilon)
4951 TraceLine(primitive_info,start,end);
4956 radii.x*=sqrt((double) delta);
4957 radii.y*=sqrt((double) delta);
4959 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4960 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4961 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4962 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4963 alpha=points[1].x-points[0].x;
4964 beta=points[1].y-points[0].y;
4965 factor=1.0/(alpha*alpha+beta*beta)-0.25;
4970 factor=sqrt((double) factor);
4971 if (sweep == large_arc)
4974 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4975 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4976 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4977 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4978 if ((theta < 0.0) && (sweep != MagickFalse))
4979 theta+=(MagickRealType) (2.0*MagickPI);
4981 if ((theta > 0.0) && (sweep == MagickFalse))
4982 theta-=(MagickRealType) (2.0*MagickPI);
4983 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
4986 for (i=0; i < (ssize_t) arc_segments; i++)
4988 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4989 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4990 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4991 sin(fmod((double) beta,DegreesToRadians(360.0)));
4992 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
4993 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
4994 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4995 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
4996 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
4997 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4998 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
4999 theta/arc_segments),DegreesToRadians(360.0))));
5000 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5001 theta/arc_segments),DegreesToRadians(360.0))));
5002 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5003 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5004 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5005 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5006 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5007 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5008 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5010 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5012 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5014 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5016 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5018 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5020 if (i == (ssize_t) (arc_segments-1))
5025 primitive_info->coordinates=(size_t) (p-primitive_info);
5026 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5028 p->primitive=primitive_info->primitive;
5033 static void TraceBezier(PrimitiveInfo *primitive_info,
5034 const size_t number_coordinates)
5046 register PrimitiveInfo
5058 Allocate coeficients.
5060 quantum=number_coordinates;
5061 for (i=0; i < (ssize_t) number_coordinates; i++)
5063 for (j=i+1; j < (ssize_t) number_coordinates; j++)
5065 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5066 if (alpha > (MagickRealType) quantum)
5067 quantum=(size_t) alpha;
5068 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5069 if (alpha > (MagickRealType) quantum)
5070 quantum=(size_t) alpha;
5073 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5074 (double) BezierQuantum);
5075 control_points=quantum*number_coordinates;
5076 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5077 number_coordinates,sizeof(*coefficients));
5078 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5080 if ((coefficients == (MagickRealType *) NULL) ||
5081 (points == (PointInfo *) NULL))
5082 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5084 Compute bezier points.
5086 end=primitive_info[number_coordinates-1].point;
5087 for (i=0; i < (ssize_t) number_coordinates; i++)
5088 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5090 for (i=0; i < (ssize_t) control_points; i++)
5095 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5096 for (j=0; j < (ssize_t) number_coordinates; j++)
5098 point.x+=alpha*coefficients[j]*p->point.x;
5099 point.y+=alpha*coefficients[j]*p->point.y;
5100 alpha*=weight/(1.0-weight);
5104 weight+=1.0/control_points;
5107 Bezier curves are just short segmented polys.
5110 for (i=0; i < (ssize_t) control_points; i++)
5112 TracePoint(p,points[i]);
5117 primitive_info->coordinates=(size_t) (p-primitive_info);
5118 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5120 p->primitive=primitive_info->primitive;
5123 points=(PointInfo *) RelinquishMagickMemory(points);
5124 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5127 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5128 const PointInfo end)
5139 alpha=end.x-start.x;
5141 radius=hypot((double) alpha,(double) beta);
5142 offset.x=(double) radius;
5143 offset.y=(double) radius;
5146 TraceEllipse(primitive_info,start,offset,degrees);
5149 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5150 const PointInfo stop,const PointInfo degrees)
5161 register PrimitiveInfo
5168 Ellipses are just short segmented polys.
5170 if ((stop.x == 0.0) && (stop.y == 0.0))
5172 TracePoint(primitive_info,start);
5175 delta=2.0/MagickMax(stop.x,stop.y);
5176 step=(MagickRealType) (MagickPI/8.0);
5177 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5178 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
5179 angle.x=DegreesToRadians(degrees.x);
5181 while (y < degrees.x)
5183 angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5184 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5186 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5187 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5188 TracePoint(p,point);
5191 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5192 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5193 TracePoint(p,point);
5195 primitive_info->coordinates=(size_t) (p-primitive_info);
5196 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5198 p->primitive=primitive_info->primitive;
5203 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5204 const PointInfo end)
5206 TracePoint(primitive_info,start);
5207 if ((fabs(start.x-end.x) <= MagickEpsilon) &&
5208 (fabs(start.y-end.y) <= MagickEpsilon))
5210 primitive_info->primitive=PointPrimitive;
5211 primitive_info->coordinates=1;
5214 TracePoint(primitive_info+1,end);
5215 (primitive_info+1)->primitive=primitive_info->primitive;
5216 primitive_info->coordinates=2;
5219 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5222 token[MaxTextExtent];
5244 register PrimitiveInfo
5259 number_coordinates=0;
5261 primitive_type=primitive_info->primitive;
5263 for (p=path; *p != '\0'; )
5265 while (isspace((int) ((unsigned char) *p)) != 0)
5269 last_attribute=attribute;
5270 attribute=(int) (*p++);
5291 GetMagickToken(p,&p,token);
5293 GetMagickToken(p,&p,token);
5294 arc.x=StringToDouble(token,(char **) NULL);
5295 GetMagickToken(p,&p,token);
5297 GetMagickToken(p,&p,token);
5298 arc.y=StringToDouble(token,(char **) NULL);
5299 GetMagickToken(p,&p,token);
5301 GetMagickToken(p,&p,token);
5302 angle=StringToDouble(token,(char **) NULL);
5303 GetMagickToken(p,&p,token);
5305 GetMagickToken(p,&p,token);
5306 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5307 GetMagickToken(p,&p,token);
5309 GetMagickToken(p,&p,token);
5310 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5311 GetMagickToken(p,&p,token);
5313 GetMagickToken(p,&p,token);
5314 x=StringToDouble(token,(char **) NULL);
5315 GetMagickToken(p,&p,token);
5317 GetMagickToken(p,&p,token);
5318 y=StringToDouble(token,(char **) NULL);
5319 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5320 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5321 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5324 while (isspace((int) ((unsigned char) *p)) != 0)
5328 } while (IsPoint(p) != MagickFalse);
5335 Compute bezier points.
5340 for (i=1; i < 4; i++)
5342 GetMagickToken(p,&p,token);
5344 GetMagickToken(p,&p,token);
5345 x=StringToDouble(token,(char **) NULL);
5346 GetMagickToken(p,&p,token);
5348 GetMagickToken(p,&p,token);
5349 y=StringToDouble(token,(char **) NULL);
5350 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5351 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5354 for (i=0; i < 4; i++)
5355 (q+i)->point=points[i];
5359 } while (IsPoint(p) != MagickFalse);
5367 GetMagickToken(p,&p,token);
5369 GetMagickToken(p,&p,token);
5370 x=StringToDouble(token,(char **) NULL);
5371 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5372 TracePoint(q,point);
5374 } while (IsPoint(p) != MagickFalse);
5382 GetMagickToken(p,&p,token);
5384 GetMagickToken(p,&p,token);
5385 x=StringToDouble(token,(char **) NULL);
5386 GetMagickToken(p,&p,token);
5388 GetMagickToken(p,&p,token);
5389 y=StringToDouble(token,(char **) NULL);
5390 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5391 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5392 TracePoint(q,point);
5394 } while (IsPoint(p) != MagickFalse);
5400 if (q != primitive_info)
5402 primitive_info->coordinates=(size_t) (q-primitive_info);
5403 number_coordinates+=primitive_info->coordinates;
5409 GetMagickToken(p,&p,token);
5411 GetMagickToken(p,&p,token);
5412 x=StringToDouble(token,(char **) NULL);
5413 GetMagickToken(p,&p,token);
5415 GetMagickToken(p,&p,token);
5416 y=StringToDouble(token,(char **) NULL);
5417 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5418 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5422 TracePoint(q,point);
5424 if ((i != 0) && (attribute == (int) 'M'))
5426 TracePoint(q,point);
5429 } while (IsPoint(p) != MagickFalse);
5436 Compute bezier points.
5441 for (i=1; i < 3; i++)
5443 GetMagickToken(p,&p,token);
5445 GetMagickToken(p,&p,token);
5446 x=StringToDouble(token,(char **) NULL);
5447 GetMagickToken(p,&p,token);
5449 GetMagickToken(p,&p,token);
5450 y=StringToDouble(token,(char **) NULL);
5453 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5454 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5457 for (i=0; i < 3; i++)
5458 (q+i)->point=points[i];
5462 } while (IsPoint(p) != MagickFalse);
5469 Compute bezier points.
5473 points[0]=points[3];
5474 points[1].x=2.0*points[3].x-points[2].x;
5475 points[1].y=2.0*points[3].y-points[2].y;
5476 for (i=2; i < 4; i++)
5478 GetMagickToken(p,&p,token);
5480 GetMagickToken(p,&p,token);
5481 x=StringToDouble(token,(char **) NULL);
5482 GetMagickToken(p,&p,token);
5484 GetMagickToken(p,&p,token);
5485 y=StringToDouble(token,(char **) NULL);
5488 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5489 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5492 if (strchr("CcSs",last_attribute) == (char *) NULL)
5494 points[0]=points[2];
5495 points[1]=points[3];
5497 for (i=0; i < 4; i++)
5498 (q+i)->point=points[i];
5502 } while (IsPoint(p) != MagickFalse);
5509 Compute bezier points.
5513 points[0]=points[2];
5514 points[1].x=2.0*points[2].x-points[1].x;
5515 points[1].y=2.0*points[2].y-points[1].y;
5516 for (i=2; i < 3; i++)
5518 GetMagickToken(p,&p,token);
5520 GetMagickToken(p,&p,token);
5521 x=StringToDouble(token,(char **) NULL);
5522 GetMagickToken(p,&p,token);
5524 GetMagickToken(p,&p,token);
5525 y=StringToDouble(token,(char **) NULL);
5526 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5527 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5530 if (strchr("QqTt",last_attribute) == (char *) NULL)
5532 points[0]=points[2];
5533 points[1]=points[3];
5535 for (i=0; i < 3; i++)
5536 (q+i)->point=points[i];
5540 } while (IsPoint(p) != MagickFalse);
5548 GetMagickToken(p,&p,token);
5550 GetMagickToken(p,&p,token);
5551 y=StringToDouble(token,(char **) NULL);
5552 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5553 TracePoint(q,point);
5555 } while (IsPoint(p) != MagickFalse);
5562 TracePoint(q,point);
5564 primitive_info->coordinates=(size_t) (q-primitive_info);
5565 number_coordinates+=primitive_info->coordinates;
5572 if (isalpha((int) ((unsigned char) attribute)) != 0)
5573 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5579 primitive_info->coordinates=(size_t) (q-primitive_info);
5580 number_coordinates+=primitive_info->coordinates;
5581 for (i=0; i < (ssize_t) number_coordinates; i++)
5584 q->primitive=primitive_type;
5586 q->method=FillToBorderMethod;
5589 return(number_coordinates);
5592 static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5593 const PointInfo end)
5598 register PrimitiveInfo
5605 TracePoint(p,start);
5609 TracePoint(p,point);
5615 TracePoint(p,point);
5617 TracePoint(p,start);
5619 primitive_info->coordinates=(size_t) (p-primitive_info);
5620 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5622 p->primitive=primitive_info->primitive;
5627 static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5628 const PointInfo start,const PointInfo end,PointInfo arc)
5635 register PrimitiveInfo
5642 offset.x=fabs(end.x-start.x);
5643 offset.y=fabs(end.y-start.y);
5644 if (arc.x > (0.5*offset.x))
5646 if (arc.y > (0.5*offset.y))
5648 point.x=start.x+offset.x-arc.x;
5649 point.y=start.y+arc.y;
5652 TraceEllipse(p,point,arc,degrees);
5654 point.x=start.x+offset.x-arc.x;
5655 point.y=start.y+offset.y-arc.y;
5658 TraceEllipse(p,point,arc,degrees);
5660 point.x=start.x+arc.x;
5661 point.y=start.y+offset.y-arc.y;
5664 TraceEllipse(p,point,arc,degrees);
5666 point.x=start.x+arc.x;
5667 point.y=start.y+arc.y;
5670 TraceEllipse(p,point,arc,degrees);
5672 TracePoint(p,primitive_info->point);
5674 primitive_info->coordinates=(size_t) (p-primitive_info);
5675 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5677 p->primitive=primitive_info->primitive;
5682 static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
5683 const size_t number_vertices,const MagickRealType offset)
5688 register MagickRealType
5700 for (i=1; i < (ssize_t) number_vertices; i++)
5702 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5703 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5704 if ((fabs((double) dx) >= MagickEpsilon) ||
5705 (fabs((double) dy) >= MagickEpsilon))
5708 if (i == (ssize_t) number_vertices)
5709 i=(ssize_t) number_vertices-1L;
5710 distance=hypot((double) dx,(double) dy);
5711 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5712 dx*(distance+offset)/distance);
5713 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5714 dy*(distance+offset)/distance);
5715 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
5717 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5718 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5719 if ((fabs((double) dx) >= MagickEpsilon) ||
5720 (fabs((double) dy) >= MagickEpsilon))
5723 distance=hypot((double) dx,(double) dy);
5724 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5725 dx*(distance+offset)/distance);
5726 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5727 dy*(distance+offset)/distance);
5730 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5731 const PrimitiveInfo *primitive_info)
5733 typedef struct _LineSegment
5785 number_vertices=primitive_info->coordinates;
5786 max_strokes=2*number_vertices+6*BezierQuantum+360;
5787 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5789 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5791 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5792 number_vertices+2UL,sizeof(*polygon_primitive));
5793 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5794 (polygon_primitive == (PrimitiveInfo *) NULL))
5795 return((PrimitiveInfo *) NULL);
5796 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5797 number_vertices*sizeof(*polygon_primitive));
5799 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5800 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5801 MagickTrue : MagickFalse;
5802 if ((draw_info->linejoin == RoundJoin) ||
5803 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5805 polygon_primitive[number_vertices]=primitive_info[1];
5808 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5810 Compute the slope for the first line segment, p.
5814 for (n=1; n < (ssize_t) number_vertices; n++)
5816 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5817 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5818 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5821 if (n == (ssize_t) number_vertices)
5822 n=(ssize_t) number_vertices-1L;
5824 inverse_slope.p=0.0;
5825 if (fabs(dx.p) <= MagickEpsilon)
5828 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5830 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5833 if (fabs(dy.p) <= MagickEpsilon)
5836 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5838 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5843 inverse_slope.p=(-1.0/slope.p);
5845 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5846 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5848 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5849 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5850 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5851 offset.y=(double) (offset.x*inverse_slope.p);
5852 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5854 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5855 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5856 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5857 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5858 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5859 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5860 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5861 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5865 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5866 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5867 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5868 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5869 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5870 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5871 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5872 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5875 Create strokes for the line join attribute: bevel, miter, round.
5879 path_q[p++]=box_q[0];
5880 path_p[q++]=box_p[0];
5881 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
5884 Compute the slope for this line segment, q.
5886 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5887 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5888 dot_product=dx.q*dx.q+dy.q*dy.q;
5889 if (dot_product < 0.25)
5892 inverse_slope.q=0.0;
5893 if (fabs(dx.q) < MagickEpsilon)
5896 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5898 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5901 if (fabs(dy.q) <= MagickEpsilon)
5904 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5906 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5911 inverse_slope.q=(-1.0/slope.q);
5913 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5914 offset.y=(double) (offset.x*inverse_slope.q);
5915 dot_product=dy.q*offset.x-dx.q*offset.y;
5916 if (dot_product > 0.0)
5918 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5919 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5920 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5921 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5922 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5923 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5924 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5925 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5929 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5930 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5931 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5932 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5933 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5934 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5935 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5936 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5938 if (fabs((double) (slope.p-slope.q)) <= MagickEpsilon)
5945 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5946 box_p[3].y)/(slope.p-slope.q));
5947 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5948 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5949 box_q[3].y)/(slope.p-slope.q));
5950 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5952 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
5954 max_strokes+=6*BezierQuantum+360;
5955 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5957 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5959 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5961 polygon_primitive=(PrimitiveInfo *)
5962 RelinquishMagickMemory(polygon_primitive);
5963 return((PrimitiveInfo *) NULL);
5966 dot_product=dx.q*dy.p-dx.p*dy.q;
5967 if (dot_product <= 0.0)
5968 switch (draw_info->linejoin)
5972 path_q[q++]=box_q[1];
5973 path_q[q++]=box_q[2];
5974 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5975 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5976 if (dot_product <= miterlimit)
5977 path_p[p++]=box_p[4];
5980 path_p[p++]=box_p[1];
5981 path_p[p++]=box_p[2];
5987 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5988 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5989 if (dot_product <= miterlimit)
5991 path_q[q++]=box_q[4];
5992 path_p[p++]=box_p[4];
5996 path_q[q++]=box_q[1];
5997 path_q[q++]=box_q[2];
5998 path_p[p++]=box_p[1];
5999 path_p[p++]=box_p[2];
6005 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6006 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6007 if (dot_product <= miterlimit)
6008 path_p[p++]=box_p[4];
6011 path_p[p++]=box_p[1];
6012 path_p[p++]=box_p[2];
6014 center=polygon_primitive[n].point;
6015 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6016 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6017 if (theta.q < theta.p)
6018 theta.q+=(MagickRealType) (2.0*MagickPI);
6019 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
6020 (2.0*sqrt((double) (1.0/mid)))));
6021 path_q[q].x=box_q[1].x;
6022 path_q[q].y=box_q[1].y;
6024 for (j=1; j < (ssize_t) arc_segments; j++)
6026 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6027 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6028 (theta.p+delta_theta),DegreesToRadians(360.0))));
6029 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6030 (theta.p+delta_theta),DegreesToRadians(360.0))));
6033 path_q[q++]=box_q[2];
6040 switch (draw_info->linejoin)
6044 path_p[p++]=box_p[1];
6045 path_p[p++]=box_p[2];
6046 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6047 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6048 if (dot_product <= miterlimit)
6049 path_q[q++]=box_q[4];
6052 path_q[q++]=box_q[1];
6053 path_q[q++]=box_q[2];
6059 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6060 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6061 if (dot_product <= miterlimit)
6063 path_q[q++]=box_q[4];
6064 path_p[p++]=box_p[4];
6068 path_q[q++]=box_q[1];
6069 path_q[q++]=box_q[2];
6070 path_p[p++]=box_p[1];
6071 path_p[p++]=box_p[2];
6077 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6078 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6079 if (dot_product <= miterlimit)
6080 path_q[q++]=box_q[4];
6083 path_q[q++]=box_q[1];
6084 path_q[q++]=box_q[2];
6086 center=polygon_primitive[n].point;
6087 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6088 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6089 if (theta.p < theta.q)
6090 theta.p+=(MagickRealType) (2.0*MagickPI);
6091 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
6092 (2.0*sqrt((double) (1.0/mid)))));
6093 path_p[p++]=box_p[1];
6094 for (j=1; j < (ssize_t) arc_segments; j++)
6096 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6097 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6098 (theta.p+delta_theta),DegreesToRadians(360.0))));
6099 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6100 (theta.p+delta_theta),DegreesToRadians(360.0))));
6103 path_p[p++]=box_p[2];
6110 inverse_slope.p=inverse_slope.q;
6119 path_p[p++]=box_p[1];
6120 path_q[q++]=box_q[1];
6122 Trace stroked polygon.
6124 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6125 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6126 if (stroke_polygon != (PrimitiveInfo *) NULL)
6128 for (i=0; i < (ssize_t) p; i++)
6130 stroke_polygon[i]=polygon_primitive[0];
6131 stroke_polygon[i].point=path_p[i];
6133 if (closed_path != MagickFalse)
6135 stroke_polygon[i]=polygon_primitive[0];
6136 stroke_polygon[i].point=stroke_polygon[0].point;
6139 for ( ; i < (ssize_t) (p+q+closed_path); i++)
6141 stroke_polygon[i]=polygon_primitive[0];
6142 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6144 if (closed_path != MagickFalse)
6146 stroke_polygon[i]=polygon_primitive[0];
6147 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6150 stroke_polygon[i]=polygon_primitive[0];
6151 stroke_polygon[i].point=stroke_polygon[0].point;
6153 stroke_polygon[i].primitive=UndefinedPrimitive;
6154 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
6156 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6157 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6158 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6159 return(stroke_polygon);