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-private.h"
54#include "MagickCore/cache-view.h"
55#include "MagickCore/channel.h"
56#include "MagickCore/color.h"
57#include "MagickCore/colorspace-private.h"
58#include "MagickCore/composite.h"
59#include "MagickCore/composite-private.h"
60#include "MagickCore/constitute.h"
61#include "MagickCore/draw.h"
62#include "MagickCore/draw-private.h"
63#include "MagickCore/enhance.h"
64#include "MagickCore/exception.h"
65#include "MagickCore/exception-private.h"
66#include "MagickCore/gem.h"
67#include "MagickCore/geometry.h"
68#include "MagickCore/image-private.h"
69#include "MagickCore/list.h"
70#include "MagickCore/log.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory-private.h"
73#include "MagickCore/monitor.h"
74#include "MagickCore/monitor-private.h"
75#include "MagickCore/option.h"
76#include "MagickCore/paint.h"
77#include "MagickCore/pixel-accessor.h"
78#include "MagickCore/property.h"
79#include "MagickCore/resample.h"
80#include "MagickCore/resample-private.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/splay-tree.h"
83#include "MagickCore/string_.h"
84#include "MagickCore/string-private.h"
85#include "MagickCore/thread-private.h"
86#include "MagickCore/token.h"
87#include "MagickCore/transform-private.h"
88#include "MagickCore/utility.h"
93#define AntialiasThreshold (1.0/3.0)
94#define BezierQuantum 200
95#define PrimitiveExtentPad 4296.0
96#define MaxBezierCoordinates 67108864
97#define ThrowPointExpectedException(token,exception) \
99 (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
100 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
101 status=MagickFalse; \
191 *DrawClippingMask(Image *,
const DrawInfo *,
const char *,
const char *,
194static MagickBooleanType
195 DrawStrokePolygon(Image *,
const DrawInfo *,
const PrimitiveInfo *,
197 RenderMVGContent(Image *,
const DrawInfo *,
const size_t,ExceptionInfo *),
198 TraceArc(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
199 TraceArcPath(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo,
200 const double,
const MagickBooleanType,
const MagickBooleanType),
201 TraceBezier(MVGInfo *,
const size_t),
202 TraceCircle(MVGInfo *,
const PointInfo,
const PointInfo),
203 TraceEllipse(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
204 TraceLine(PrimitiveInfo *,
const PointInfo,
const PointInfo),
205 TraceRectangle(PrimitiveInfo *,
const PointInfo,
const PointInfo),
206 TraceRoundRectangle(MVGInfo *,
const PointInfo,
const PointInfo,PointInfo),
207 TraceSquareLinecap(PrimitiveInfo *,
const size_t,
const double);
210 *TraceStrokePolygon(
const DrawInfo *,
const PrimitiveInfo *,ExceptionInfo *);
213 TracePath(MVGInfo *,
const char *,ExceptionInfo *);
233MagickExport DrawInfo *AcquireDrawInfo(
void)
238 draw_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*draw_info));
239 GetDrawInfo((ImageInfo *) NULL,draw_info);
270MagickExport DrawInfo *CloneDrawInfo(
const ImageInfo *image_info,
271 const DrawInfo *draw_info)
279 clone_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
280 GetDrawInfo(image_info,clone_info);
281 if (draw_info == (DrawInfo *) NULL)
283 exception=AcquireExceptionInfo();
284 if (draw_info->id != (
char *) NULL)
285 (void) CloneString(&clone_info->id,draw_info->id);
286 if (draw_info->primitive != (
char *) NULL)
287 (void) CloneString(&clone_info->primitive,draw_info->primitive);
288 if (draw_info->geometry != (
char *) NULL)
289 (void) CloneString(&clone_info->geometry,draw_info->geometry);
290 clone_info->compliance=draw_info->compliance;
291 clone_info->viewbox=draw_info->viewbox;
292 clone_info->affine=draw_info->affine;
293 clone_info->gravity=draw_info->gravity;
294 clone_info->fill=draw_info->fill;
295 clone_info->stroke=draw_info->stroke;
296 clone_info->stroke_width=draw_info->stroke_width;
297 if (draw_info->fill_pattern != (Image *) NULL)
298 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
300 if (draw_info->stroke_pattern != (Image *) NULL)
301 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
302 MagickTrue,exception);
303 clone_info->stroke_antialias=draw_info->stroke_antialias;
304 clone_info->text_antialias=draw_info->text_antialias;
305 clone_info->fill_rule=draw_info->fill_rule;
306 clone_info->linecap=draw_info->linecap;
307 clone_info->linejoin=draw_info->linejoin;
308 clone_info->miterlimit=draw_info->miterlimit;
309 clone_info->dash_offset=draw_info->dash_offset;
310 clone_info->decorate=draw_info->decorate;
311 clone_info->compose=draw_info->compose;
312 if (draw_info->text != (
char *) NULL)
313 (void) CloneString(&clone_info->text,draw_info->text);
314 if (draw_info->font != (
char *) NULL)
315 (void) CloneString(&clone_info->font,draw_info->font);
316 if (draw_info->metrics != (
char *) NULL)
317 (void) CloneString(&clone_info->metrics,draw_info->metrics);
318 if (draw_info->family != (
char *) NULL)
319 (void) CloneString(&clone_info->family,draw_info->family);
320 clone_info->style=draw_info->style;
321 clone_info->stretch=draw_info->stretch;
322 clone_info->weight=draw_info->weight;
323 if (draw_info->encoding != (
char *) NULL)
324 (void) CloneString(&clone_info->encoding,draw_info->encoding);
325 clone_info->pointsize=draw_info->pointsize;
326 clone_info->kerning=draw_info->kerning;
327 clone_info->interline_spacing=draw_info->interline_spacing;
328 clone_info->interword_spacing=draw_info->interword_spacing;
329 clone_info->direction=draw_info->direction;
330 clone_info->word_break=draw_info->word_break;
331 if (draw_info->density != (
char *) NULL)
332 (void) CloneString(&clone_info->density,draw_info->density);
333 clone_info->align=draw_info->align;
334 clone_info->undercolor=draw_info->undercolor;
335 clone_info->border_color=draw_info->border_color;
336 if (draw_info->server_name != (
char *) NULL)
337 (void) CloneString(&clone_info->server_name,draw_info->server_name);
338 if (draw_info->dash_pattern != (
double *) NULL)
343 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
344 clone_info->dash_pattern=(
double *) AcquireQuantumMemory((
size_t) (x+1),
345 sizeof(*clone_info->dash_pattern));
346 if (clone_info->dash_pattern == (
double *) NULL)
347 ThrowFatalException(ResourceLimitFatalError,
348 "UnableToAllocateDashPattern");
349 (void) memset(clone_info->dash_pattern,0,(
size_t) (x+1)*
350 sizeof(*clone_info->dash_pattern));
351 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(
size_t)
352 x*
sizeof(*clone_info->dash_pattern));
354 clone_info->gradient=draw_info->gradient;
355 if (draw_info->gradient.stops != (StopInfo *) NULL)
360 number_stops=clone_info->gradient.number_stops;
361 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((
size_t)
362 number_stops,
sizeof(*clone_info->gradient.stops));
363 if (clone_info->gradient.stops == (StopInfo *) NULL)
364 ThrowFatalException(ResourceLimitFatalError,
365 "UnableToAllocateDashPattern");
366 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
367 (
size_t) number_stops*
sizeof(*clone_info->gradient.stops));
369 clone_info->bounds=draw_info->bounds;
370 clone_info->fill_alpha=draw_info->fill_alpha;
371 clone_info->stroke_alpha=draw_info->stroke_alpha;
372 clone_info->element_reference=draw_info->element_reference;
373 clone_info->clip_path=draw_info->clip_path;
374 clone_info->clip_units=draw_info->clip_units;
375 if (draw_info->clip_mask != (
char *) NULL)
376 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
377 if (draw_info->clipping_mask != (Image *) NULL)
378 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
379 MagickTrue,exception);
380 if (draw_info->composite_mask != (Image *) NULL)
381 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
382 MagickTrue,exception);
383 clone_info->render=draw_info->render;
384 clone_info->debug=draw_info->debug;
385 exception=DestroyExceptionInfo(exception);
420static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
425 if (polygon_info->edges != (EdgeInfo *) NULL)
427 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
428 if (polygon_info->edges[i].points != (PointInfo *) NULL)
429 polygon_info->edges[i].points=(PointInfo *)
430 RelinquishMagickMemory(polygon_info->edges[i].points);
431 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
432 polygon_info->edges);
434 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
436#if defined(__cplusplus) || defined(c_plusplus)
440static int DrawCompareEdges(
const void *p_edge,
const void *q_edge)
442#define DrawCompareEdge(p,q) \
444 if (((p)-(q)) < 0.0) \
446 if (((p)-(q)) > 0.0) \
457 p=((
const EdgeInfo *) p_edge)->points;
458 q=((
const EdgeInfo *) q_edge)->points;
459 DrawCompareEdge(p[0].y,q[0].y);
460 DrawCompareEdge(p[0].x,q[0].x);
461 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
463 DrawCompareEdge(p[1].y,q[1].y);
464 DrawCompareEdge(p[1].x,q[1].x);
468#if defined(__cplusplus) || defined(c_plusplus)
472static void LogPolygonInfo(
const PolygonInfo *polygon_info)
481 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin active-edge");
482 p=polygon_info->edges;
483 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
485 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" edge %.20g:",
487 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" direction: %s",
488 p->direction != MagickFalse ?
"down" :
"up");
489 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" ghostline: %s",
490 p->ghostline != MagickFalse ?
"transparent" :
"opaque");
491 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
492 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
493 p->bounds.x2,p->bounds.y2);
494 for (j=0; j < (ssize_t) p->number_points; j++)
495 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %g,%g",
496 p->points[j].x,p->points[j].y);
499 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end active-edge");
502static void ReversePoints(PointInfo *points,
const size_t number_points)
510 for (i=0; i < (number_points >> 1); i++)
513 points[i]=points[number_points-(i+1)];
514 points[number_points-(i+1)]=point;
518static PolygonInfo *ConvertPathToPolygon(
const PathInfo *path_info,
519 ExceptionInfo *exception)
550 polygon_info=(PolygonInfo *) AcquireMagickMemory(
sizeof(*polygon_info));
551 if (polygon_info == (PolygonInfo *) NULL)
553 (void) ThrowMagickException(exception,GetMagickModule(),
554 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
555 return((PolygonInfo *) NULL);
558 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
559 sizeof(*polygon_info->edges));
560 if (polygon_info->edges == (EdgeInfo *) NULL)
562 (void) ThrowMagickException(exception,GetMagickModule(),
563 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
564 return(DestroyPolygonInfo(polygon_info));
566 (void) memset(polygon_info->edges,0,number_edges*
567 sizeof(*polygon_info->edges));
570 ghostline=MagickFalse;
573 points=(PointInfo *) NULL;
574 (void) memset(&point,0,
sizeof(point));
575 (void) memset(&bounds,0,
sizeof(bounds));
576 polygon_info->edges[edge].number_points=(size_t) n;
577 polygon_info->edges[edge].scanline=0.0;
578 polygon_info->edges[edge].highwater=0;
579 polygon_info->edges[edge].ghostline=ghostline;
580 polygon_info->edges[edge].direction=(ssize_t) direction;
581 polygon_info->edges[edge].points=points;
582 polygon_info->edges[edge].bounds=bounds;
583 polygon_info->number_edges=0;
584 for (i=0; path_info[i].code != EndCode; i++)
586 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
587 (path_info[i].code == GhostlineCode))
592 if ((points != (PointInfo *) NULL) && (n >= 2))
594 if (edge == number_edges)
597 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
598 polygon_info->edges,(
size_t) number_edges,
599 sizeof(*polygon_info->edges));
600 if (polygon_info->edges == (EdgeInfo *) NULL)
602 (void) ThrowMagickException(exception,GetMagickModule(),
603 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
604 points=(PointInfo *) RelinquishMagickMemory(points);
605 return(DestroyPolygonInfo(polygon_info));
608 polygon_info->edges[edge].number_points=(size_t) n;
609 polygon_info->edges[edge].scanline=(-1.0);
610 polygon_info->edges[edge].highwater=0;
611 polygon_info->edges[edge].ghostline=ghostline;
612 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
614 ReversePoints(points,(
size_t) n);
615 polygon_info->edges[edge].points=points;
616 polygon_info->edges[edge].bounds=bounds;
617 polygon_info->edges[edge].bounds.y1=points[0].y;
618 polygon_info->edges[edge].bounds.y2=points[n-1].y;
619 points=(PointInfo *) NULL;
620 ghostline=MagickFalse;
622 polygon_info->number_edges=edge;
624 if (points == (PointInfo *) NULL)
627 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
629 if (points == (PointInfo *) NULL)
631 (void) ThrowMagickException(exception,GetMagickModule(),
632 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
633 return(DestroyPolygonInfo(polygon_info));
636 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
637 point=path_info[i].point;
648 next_direction=((path_info[i].point.y > point.y) ||
649 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
650 (path_info[i].point.x > point.x))) ? 1 : -1;
651 if ((points != (PointInfo *) NULL) && (direction != 0) &&
652 (direction != next_direction))
658 if (edge == number_edges)
661 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
662 polygon_info->edges,(
size_t) number_edges,
663 sizeof(*polygon_info->edges));
664 if (polygon_info->edges == (EdgeInfo *) NULL)
666 (void) ThrowMagickException(exception,GetMagickModule(),
667 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
668 points=(PointInfo *) RelinquishMagickMemory(points);
669 return(DestroyPolygonInfo(polygon_info));
672 polygon_info->edges[edge].number_points=(size_t) n;
673 polygon_info->edges[edge].scanline=(-1.0);
674 polygon_info->edges[edge].highwater=0;
675 polygon_info->edges[edge].ghostline=ghostline;
676 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
678 ReversePoints(points,(
size_t) n);
679 polygon_info->edges[edge].points=points;
680 polygon_info->edges[edge].bounds=bounds;
681 polygon_info->edges[edge].bounds.y1=points[0].y;
682 polygon_info->edges[edge].bounds.y2=points[n-1].y;
683 polygon_info->number_edges=edge+1;
684 points=(PointInfo *) NULL;
686 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
688 if (points == (PointInfo *) NULL)
690 (void) ThrowMagickException(exception,GetMagickModule(),
691 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
692 return(DestroyPolygonInfo(polygon_info));
695 ghostline=MagickFalse;
701 direction=next_direction;
702 if (points == (PointInfo *) NULL)
704 if (n == (ssize_t) number_points)
707 points=(PointInfo *) ResizeQuantumMemory(points,(
size_t) number_points,
709 if (points == (PointInfo *) NULL)
711 (void) ThrowMagickException(exception,GetMagickModule(),
712 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
713 return(DestroyPolygonInfo(polygon_info));
716 point=path_info[i].point;
718 if (point.x < bounds.x1)
720 if (point.x > bounds.x2)
724 if (points != (PointInfo *) NULL)
727 points=(PointInfo *) RelinquishMagickMemory(points);
730 if (edge == number_edges)
733 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
734 polygon_info->edges,(
size_t) number_edges,
735 sizeof(*polygon_info->edges));
736 if (polygon_info->edges == (EdgeInfo *) NULL)
738 (void) ThrowMagickException(exception,GetMagickModule(),
739 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
740 return(DestroyPolygonInfo(polygon_info));
743 polygon_info->edges[edge].number_points=(size_t) n;
744 polygon_info->edges[edge].scanline=(-1.0);
745 polygon_info->edges[edge].highwater=0;
746 polygon_info->edges[edge].ghostline=ghostline;
747 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
749 ReversePoints(points,(
size_t) n);
750 polygon_info->edges[edge].points=points;
751 polygon_info->edges[edge].bounds=bounds;
752 polygon_info->edges[edge].bounds.y1=points[0].y;
753 polygon_info->edges[edge].bounds.y2=points[n-1].y;
754 points=(PointInfo *) NULL;
755 ghostline=MagickFalse;
757 polygon_info->number_edges=edge;
760 polygon_info->number_edges=edge;
761 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
762 polygon_info->number_edges,
sizeof(*polygon_info->edges));
763 if (polygon_info->edges == (EdgeInfo *) NULL)
765 (void) ThrowMagickException(exception,GetMagickModule(),
766 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
767 return(DestroyPolygonInfo(polygon_info));
769 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
774 edge_info=polygon_info->edges+i;
775 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
776 edge_info->number_points,
sizeof(*edge_info->points));
777 if (edge_info->points == (PointInfo *) NULL)
779 (void) ThrowMagickException(exception,GetMagickModule(),
780 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
781 return(DestroyPolygonInfo(polygon_info));
784 qsort(polygon_info->edges,(
size_t) polygon_info->number_edges,
785 sizeof(*polygon_info->edges),DrawCompareEdges);
786 if ((GetLogEventMask() & DrawEvent) != 0)
787 LogPolygonInfo(polygon_info);
788 return(polygon_info);
821static void LogPathInfo(
const PathInfo *path_info)
826 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin vector-path");
827 for (p=path_info; p->code != EndCode; p++)
828 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
829 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
830 "moveto ghostline" : p->code == OpenCode ?
"moveto open" :
831 p->code == MoveToCode ?
"moveto" : p->code == LineToCode ?
"lineto" :
833 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end vector-path");
836static PathInfo *ConvertPrimitiveToPath(
const PrimitiveInfo *primitive_info,
837 ExceptionInfo *exception)
863 switch (primitive_info->primitive)
870 return((PathInfo *) NULL);
874 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
875 path_info=(PathInfo *) AcquireQuantumMemory((
size_t) (3UL*i+1UL),
877 if (path_info == (PathInfo *) NULL)
879 (void) ThrowMagickException(exception,GetMagickModule(),
880 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
881 return((PathInfo *) NULL);
884 closed_subpath=MagickFalse;
891 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
894 if (coordinates <= 0)
899 coordinates=primitive_info[i].coordinates;
900 p=primitive_info[i].point;
903 closed_subpath=primitive_info[i].closed_subpath;
906 if ((code == MoveToCode) || (coordinates <= 0) ||
907 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
908 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
913 path_info[n].code=code;
914 path_info[n].point=primitive_info[i].point;
915 q=primitive_info[i].point;
920 if (closed_subpath != MagickFalse)
922 closed_subpath=MagickFalse;
928 path_info[start].code=OpenCode;
929 path_info[n].code=GhostlineCode;
930 path_info[n].point=primitive_info[i].point;
932 path_info[n].code=LineToCode;
933 path_info[n].point=p;
936 path_info[n].code=EndCode;
937 path_info[n].point.x=0.0;
938 path_info[n].point.y=0.0;
939 if (IsEventLogging() != MagickFalse)
940 LogPathInfo(path_info);
941 path_info=(PathInfo *) ResizeQuantumMemory(path_info,(
size_t) (n+1),
968MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
970 assert(draw_info != (DrawInfo *) NULL);
971 assert(draw_info->signature == MagickCoreSignature);
972 if (IsEventLogging() != MagickFalse)
973 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
974 if (draw_info->id != (
char *) NULL)
975 draw_info->id=DestroyString(draw_info->id);
976 if (draw_info->primitive != (
char *) NULL)
977 draw_info->primitive=DestroyString(draw_info->primitive);
978 if (draw_info->text != (
char *) NULL)
979 draw_info->text=DestroyString(draw_info->text);
980 if (draw_info->geometry != (
char *) NULL)
981 draw_info->geometry=DestroyString(draw_info->geometry);
982 if (draw_info->fill_pattern != (Image *) NULL)
983 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
984 if (draw_info->stroke_pattern != (Image *) NULL)
985 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
986 if (draw_info->font != (
char *) NULL)
987 draw_info->font=DestroyString(draw_info->font);
988 if (draw_info->metrics != (
char *) NULL)
989 draw_info->metrics=DestroyString(draw_info->metrics);
990 if (draw_info->family != (
char *) NULL)
991 draw_info->family=DestroyString(draw_info->family);
992 if (draw_info->encoding != (
char *) NULL)
993 draw_info->encoding=DestroyString(draw_info->encoding);
994 if (draw_info->density != (
char *) NULL)
995 draw_info->density=DestroyString(draw_info->density);
996 if (draw_info->server_name != (
char *) NULL)
997 draw_info->server_name=(
char *)
998 RelinquishMagickMemory(draw_info->server_name);
999 if (draw_info->dash_pattern != (
double *) NULL)
1000 draw_info->dash_pattern=(
double *) RelinquishMagickMemory(
1001 draw_info->dash_pattern);
1002 if (draw_info->gradient.stops != (StopInfo *) NULL)
1003 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1004 draw_info->gradient.stops);
1005 if (draw_info->clip_mask != (
char *) NULL)
1006 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1007 if (draw_info->clipping_mask != (Image *) NULL)
1008 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1009 if (draw_info->composite_mask != (Image *) NULL)
1010 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1011 if (draw_info->image_info != (ImageInfo *) NULL)
1012 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1013 draw_info->signature=(~MagickCoreSignature);
1014 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1049static SegmentInfo AffineEdge(
const Image *image,
const AffineMatrix *affine,
1050 const double y,
const SegmentInfo *edge)
1065 inverse_edge.x1=edge->x1;
1066 inverse_edge.y1=edge->y1;
1067 inverse_edge.x2=edge->x2;
1068 inverse_edge.y2=edge->y2;
1069 z=affine->ry*y+affine->tx;
1070 if (affine->sx >= MagickEpsilon)
1072 intercept=(-z/affine->sx);
1074 if (x > inverse_edge.x1)
1076 intercept=(-z+(double) image->columns)/affine->sx;
1078 if (x < inverse_edge.x2)
1082 if (affine->sx < -MagickEpsilon)
1084 intercept=(-z+(double) image->columns)/affine->sx;
1086 if (x > inverse_edge.x1)
1088 intercept=(-z/affine->sx);
1090 if (x < inverse_edge.x2)
1094 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->columns))
1096 inverse_edge.x2=edge->x1;
1097 return(inverse_edge);
1102 z=affine->sy*y+affine->ty;
1103 if (affine->rx >= MagickEpsilon)
1105 intercept=(-z/affine->rx);
1107 if (x > inverse_edge.x1)
1109 intercept=(-z+(double) image->rows)/affine->rx;
1111 if (x < inverse_edge.x2)
1115 if (affine->rx < -MagickEpsilon)
1117 intercept=(-z+(double) image->rows)/affine->rx;
1119 if (x > inverse_edge.x1)
1121 intercept=(-z/affine->rx);
1123 if (x < inverse_edge.x2)
1127 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->rows))
1129 inverse_edge.x2=edge->x2;
1130 return(inverse_edge);
1132 return(inverse_edge);
1135static AffineMatrix InverseAffineMatrix(
const AffineMatrix *affine)
1143 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1145 inverse_affine.sx=determinant*affine->sy;
1146 inverse_affine.rx=determinant*(-affine->rx);
1147 inverse_affine.ry=determinant*(-affine->ry);
1148 inverse_affine.sy=determinant*affine->sx;
1149 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1151 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1153 return(inverse_affine);
1156MagickExport MagickBooleanType DrawAffineImage(Image *image,
1157 const Image *source,
const AffineMatrix *affine,ExceptionInfo *exception)
1191 assert(image != (Image *) NULL);
1192 assert(image->signature == MagickCoreSignature);
1193 if (IsEventLogging() != MagickFalse)
1194 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1195 assert(source != (
const Image *) NULL);
1196 assert(source->signature == MagickCoreSignature);
1197 assert(affine != (AffineMatrix *) NULL);
1200 extent[1].x=(double) source->columns;
1202 extent[2].x=(double) source->columns;
1203 extent[2].y=(double) source->rows;
1205 extent[3].y=(double) source->rows;
1206 for (i=0; i < 4; i++)
1212 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1213 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1217 for (i=1; i < 4; i++)
1219 if (min.x > extent[i].x)
1221 if (min.y > extent[i].y)
1223 if (max.x < extent[i].x)
1225 if (max.y < extent[i].y)
1231 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1232 return(MagickFalse);
1238 inverse_affine=InverseAffineMatrix(affine);
1241 if (edge.y2 > ((
double) image->rows-1.0))
1242 edge.y2=(double) image->rows-1.0;
1243 GetPixelInfo(image,&zero);
1244 start=CastDoubleToSsizeT(ceil(edge.y1-0.5));
1245 stop=CastDoubleToSsizeT(floor(edge.y2+0.5));
1246 source_view=AcquireVirtualCacheView(source,exception);
1247 image_view=AcquireAuthenticCacheView(image,exception);
1248#if defined(MAGICKCORE_OPENMP_SUPPORT)
1249 #pragma omp parallel for schedule(static) shared(status) \
1250 magick_number_threads(source,image,(size_t) (stop-start),2)
1252 for (y=start; y <= stop; y++)
1270 if (status == MagickFalse)
1272 inverse_edge=AffineEdge(source,&inverse_affine,(
double) y,&edge);
1273 if (inverse_edge.x2 < inverse_edge.x1)
1275 if (inverse_edge.x1 < 0.0)
1276 inverse_edge.x1=0.0;
1277 if (inverse_edge.x2 > ((
double) image->columns-1.0))
1278 inverse_edge.x2=(double) image->columns-1.0;
1279 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToSsizeT(
1280 ceil(inverse_edge.x1-0.5)),y,(
size_t) CastDoubleToSsizeT(floor(
1281 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1282 if (q == (Quantum *) NULL)
1286 for (x=CastDoubleToSsizeT(ceil(inverse_edge.x1-0.5));
1287 x <= CastDoubleToSsizeT(floor(inverse_edge.x2+0.5)); x++)
1289 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1291 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1293 status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1294 point.x,point.y,&pixel,exception);
1295 if (status == MagickFalse)
1297 GetPixelInfoPixel(image,q,&composite);
1298 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1300 SetPixelViaPixelInfo(image,&composite,q);
1301 q+=(ptrdiff_t) GetPixelChannels(image);
1303 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1306 source_view=DestroyCacheView(source_view);
1307 image_view=DestroyCacheView(image_view);
1343static MagickBooleanType DrawBoundingRectangles(Image *image,
1344 const DrawInfo *draw_info,
const PolygonInfo *polygon_info,
1345 ExceptionInfo *exception)
1373 (void) memset(primitive_info,0,
sizeof(primitive_info));
1374 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1375 status=QueryColorCompliance(
"#000F",AllCompliance,&clone_info->fill,
1377 if (status == MagickFalse)
1379 clone_info=DestroyDrawInfo(clone_info);
1380 return(MagickFalse);
1384 if (clone_info->density != (
char *) NULL)
1392 flags=ParseGeometry(clone_info->density,&geometry_info);
1393 if ((flags & RhoValue) != 0)
1394 resolution.x=geometry_info.rho;
1395 resolution.y=resolution.x;
1396 if ((flags & SigmaValue) != 0)
1397 resolution.y=geometry_info.sigma;
1399 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1400 clone_info->stroke_width/2.0;
1405 if (polygon_info != (PolygonInfo *) NULL)
1407 bounds=polygon_info->edges[0].bounds;
1408 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1410 if (polygon_info->edges[i].bounds.x1 < (
double) bounds.x1)
1411 bounds.x1=polygon_info->edges[i].bounds.x1;
1412 if (polygon_info->edges[i].bounds.y1 < (
double) bounds.y1)
1413 bounds.y1=polygon_info->edges[i].bounds.y1;
1414 if (polygon_info->edges[i].bounds.x2 > (
double) bounds.x2)
1415 bounds.x2=polygon_info->edges[i].bounds.x2;
1416 if (polygon_info->edges[i].bounds.y2 > (
double) bounds.y2)
1417 bounds.y2=polygon_info->edges[i].bounds.y2;
1420 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1421 image->columns ? (
double) image->columns-1 : bounds.x1;
1423 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1424 image->rows ? (
double) image->rows-1 : bounds.y1;
1426 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1427 image->columns ? (
double) image->columns-1 : bounds.x2;
1429 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1430 image->rows ? (
double) image->rows-1 : bounds.y2;
1431 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1433 if (polygon_info->edges[i].direction != 0)
1434 status=QueryColorCompliance(
"#f00",AllCompliance,&clone_info->stroke,
1437 status=QueryColorCompliance(
"#0f0",AllCompliance,&clone_info->stroke,
1439 if (status == MagickFalse)
1441 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1442 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1443 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1444 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1445 primitive_info[0].primitive=RectanglePrimitive;
1446 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1447 primitive_info[0].method=ReplaceMethod;
1448 coordinates=(ssize_t) primitive_info[0].coordinates;
1449 primitive_info[coordinates].primitive=UndefinedPrimitive;
1450 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1451 if (status == MagickFalse)
1454 if (i < (ssize_t) polygon_info->number_edges)
1456 clone_info=DestroyDrawInfo(clone_info);
1457 return(status == 0 ? MagickFalse : MagickTrue);
1460 status=QueryColorCompliance(
"#00f",AllCompliance,&clone_info->stroke,
1462 if (status == MagickFalse)
1464 clone_info=DestroyDrawInfo(clone_info);
1465 return(MagickFalse);
1467 start.x=(double) (bounds.x1-mid);
1468 start.y=(double) (bounds.y1-mid);
1469 end.x=(double) (bounds.x2+mid);
1470 end.y=(double) (bounds.y2+mid);
1471 primitive_info[0].primitive=RectanglePrimitive;
1472 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1473 primitive_info[0].method=ReplaceMethod;
1474 coordinates=(ssize_t) primitive_info[0].coordinates;
1475 primitive_info[coordinates].primitive=UndefinedPrimitive;
1476 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1477 clone_info=DestroyDrawInfo(clone_info);
1478 return(status == 0 ? MagickFalse : MagickTrue);
1510MagickExport MagickBooleanType DrawClipPath(Image *image,
1511 const DrawInfo *draw_info,
const char *
id,ExceptionInfo *exception)
1522 clip_path=GetImageArtifact(image,
id);
1523 if (clip_path == (
const char *) NULL)
1524 return(MagickFalse);
1525 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1527 if (clipping_mask == (Image *) NULL)
1528 return(MagickFalse);
1529 status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1530 clipping_mask=DestroyImage(clipping_mask);
1566static Image *DrawClippingMask(Image *image,
const DrawInfo *draw_info,
1567 const char *
id,
const char *clip_path,ExceptionInfo *exception)
1582 assert(image != (Image *) NULL);
1583 assert(image->signature == MagickCoreSignature);
1584 assert(draw_info != (
const DrawInfo *) NULL);
1585 if (IsEventLogging() != MagickFalse)
1586 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1587 clip_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1588 status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1589 if (status == MagickFalse)
1590 return(DestroyImage(clip_mask));
1591 status=SetImageMask(clip_mask,WritePixelMask,(Image *) NULL,exception);
1592 status=QueryColorCompliance(
"#0000",AllCompliance,
1593 &clip_mask->background_color,exception);
1594 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1595 clip_mask->background_color.alpha_trait=BlendPixelTrait;
1596 status=SetImageBackgroundColor(clip_mask,exception);
1597 if (draw_info->debug != MagickFalse)
1598 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin clip-path %s",
1600 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1601 (void) CloneString(&clone_info->primitive,clip_path);
1602 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1604 if (clone_info->clip_mask != (
char *) NULL)
1605 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1606 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1608 clone_info->stroke_width=0.0;
1609 clone_info->alpha=OpaqueAlpha;
1610 clone_info->clip_path=MagickTrue;
1611 status=RenderMVGContent(clip_mask,clone_info,0,exception);
1612 clone_info=DestroyDrawInfo(clone_info);
1613 separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1614 if (separate_mask == (Image *) NULL)
1618 clip_mask=DestroyImage(clip_mask);
1619 clip_mask=separate_mask;
1620 status&=(MagickStatusType) NegateImage(clip_mask,MagickFalse,exception);
1622 if (status == MagickFalse)
1623 clip_mask=DestroyImage(clip_mask);
1624 if (draw_info->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end clip-path");
1660static Image *DrawCompositeMask(Image *image,
const DrawInfo *draw_info,
1661 const char *
id,
const char *mask_path,ExceptionInfo *exception)
1676 assert(image != (Image *) NULL);
1677 assert(image->signature == MagickCoreSignature);
1678 assert(draw_info != (
const DrawInfo *) NULL);
1679 if (IsEventLogging() != MagickFalse)
1680 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1681 composite_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1682 status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1683 if (status == MagickFalse)
1684 return(DestroyImage(composite_mask));
1685 status=SetImageMask(composite_mask,CompositePixelMask,(Image *) NULL,
1687 status=QueryColorCompliance(
"#0000",AllCompliance,
1688 &composite_mask->background_color,exception);
1689 composite_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1690 composite_mask->background_color.alpha_trait=BlendPixelTrait;
1691 (void) SetImageBackgroundColor(composite_mask,exception);
1692 if (draw_info->debug != MagickFalse)
1693 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin mask-path %s",
1695 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1696 (void) CloneString(&clone_info->primitive,mask_path);
1697 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1699 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1701 clone_info->stroke_width=0.0;
1702 clone_info->alpha=OpaqueAlpha;
1703 status=RenderMVGContent(composite_mask,clone_info,0,exception);
1704 clone_info=DestroyDrawInfo(clone_info);
1705 separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1706 if (separate_mask != (Image *) NULL)
1708 composite_mask=DestroyImage(composite_mask);
1709 composite_mask=separate_mask;
1710 status=NegateImage(composite_mask,MagickFalse,exception);
1711 if (status == MagickFalse)
1712 composite_mask=DestroyImage(composite_mask);
1714 if (status == MagickFalse)
1715 composite_mask=DestroyImage(composite_mask);
1716 if (draw_info->debug != MagickFalse)
1717 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end mask-path");
1718 return(composite_mask);
1752static MagickBooleanType DrawDashPolygon(
const DrawInfo *draw_info,
1753 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1783 assert(draw_info != (
const DrawInfo *) NULL);
1784 if (draw_info->debug != MagickFalse)
1785 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-dash");
1786 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1787 number_vertices=(size_t) i;
1788 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
1789 (2UL*number_vertices+32UL),
sizeof(*dash_polygon));
1790 if (dash_polygon == (PrimitiveInfo *) NULL)
1792 (void) ThrowMagickException(exception,GetMagickModule(),
1793 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1794 return(MagickFalse);
1796 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1797 sizeof(*dash_polygon));
1798 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1799 clone_info->miterlimit=0;
1800 dash_polygon[0]=primitive_info[0];
1801 dash_polygon[0].closed_subpath=MagickFalse;
1802 scale=ExpandAffine(&draw_info->affine);
1803 length=scale*draw_info->dash_pattern[0];
1804 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1805 scale*draw_info->dash_offset : 0.0;
1807 for (n=0; offset > 0.0; j=0)
1809 if (draw_info->dash_pattern[n] <= 0.0)
1811 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1812 if (offset > length)
1816 length=scale*draw_info->dash_pattern[n];
1819 if (offset < length)
1831 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1833 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1834 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1835 maximum_length=hypot(dx,dy);
1836 if (maximum_length > (
double) (MaxBezierCoordinates >> 2))
1838 if (fabs(length) < MagickEpsilon)
1840 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1842 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1844 length=scale*draw_info->dash_pattern[n];
1846 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1848 total_length+=length;
1849 if ((n & 0x01) != 0)
1851 dash_polygon[0]=primitive_info[0];
1852 dash_polygon[0].closed_subpath=MagickFalse;
1853 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1854 total_length*MagickSafeReciprocal(maximum_length));
1855 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1856 total_length*MagickSafeReciprocal(maximum_length));
1861 if ((j+1) > (ssize_t) number_vertices)
1863 dash_polygon[j]=primitive_info[i-1];
1864 dash_polygon[j].closed_subpath=MagickFalse;
1865 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1866 total_length*MagickSafeReciprocal(maximum_length));
1867 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1868 total_length*MagickSafeReciprocal(maximum_length));
1869 dash_polygon[j].coordinates=1;
1871 dash_polygon[0].coordinates=(size_t) j;
1872 dash_polygon[j].primitive=UndefinedPrimitive;
1873 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1874 dash_polygon,exception);
1875 if (status == MagickFalse)
1878 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1880 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1882 length=scale*draw_info->dash_pattern[n];
1884 length-=(maximum_length-total_length);
1885 if ((n & 0x01) != 0)
1887 dash_polygon[j]=primitive_info[i];
1888 dash_polygon[j].coordinates=1;
1891 if ((status != MagickFalse) && (total_length < maximum_length) &&
1892 ((n & 0x01) == 0) && (j > 1))
1894 dash_polygon[j]=primitive_info[i-1];
1895 dash_polygon[j].closed_subpath=MagickFalse;
1896 dash_polygon[j].point.x+=MagickEpsilon;
1897 dash_polygon[j].point.y+=MagickEpsilon;
1898 dash_polygon[j].coordinates=1;
1900 dash_polygon[0].coordinates=(size_t) j;
1901 dash_polygon[j].primitive=UndefinedPrimitive;
1902 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1903 dash_polygon,exception);
1905 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1906 clone_info=DestroyDrawInfo(clone_info);
1907 if (draw_info->debug != MagickFalse)
1908 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-dash");
1909 return(status != 0 ? MagickTrue : MagickFalse);
1940static inline double GetStopColorOffset(
const GradientInfo *gradient,
1941 const ssize_t x,
const ssize_t y)
1943 switch (gradient->type)
1945 case UndefinedGradient:
1946 case LinearGradient:
1961 gradient_vector=(&gradient->gradient_vector);
1962 p.x=gradient_vector->x2-gradient_vector->x1;
1963 p.y=gradient_vector->y2-gradient_vector->y1;
1964 q.x=(double) x-gradient_vector->x1;
1965 q.y=(double) y-gradient_vector->y1;
1966 length=sqrt(q.x*q.x+q.y*q.y);
1967 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1968 gamma=MagickSafeReciprocal(gamma);
1969 scale=p.x*q.x+p.y*q.y;
1970 offset=gamma*scale*length;
1973 case RadialGradient:
1978 if (gradient->spread == RepeatSpread)
1980 v.x=(double) x-gradient->center.x;
1981 v.y=(double) y-gradient->center.y;
1982 return(sqrt(v.x*v.x+v.y*v.y));
1984 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1985 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1986 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1987 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1988 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1989 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1990 return(sqrt(v.x*v.x+v.y*v.y));
1996static int StopInfoCompare(
const void *x,
const void *y)
2002 stop_1=(StopInfo *) x;
2003 stop_2=(StopInfo *) y;
2004 if (stop_1->offset > stop_2->offset)
2006 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
2011MagickExport MagickBooleanType DrawGradientImage(Image *image,
2012 const DrawInfo *draw_info,ExceptionInfo *exception)
2047 assert(image != (Image *) NULL);
2048 assert(image->signature == MagickCoreSignature);
2049 assert(draw_info != (
const DrawInfo *) NULL);
2050 if (IsEventLogging() != MagickFalse)
2051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2052 gradient=(&draw_info->gradient);
2053 qsort(gradient->stops,gradient->number_stops,
sizeof(StopInfo),
2055 gradient_vector=(&gradient->gradient_vector);
2056 point.x=gradient_vector->x2-gradient_vector->x1;
2057 point.y=gradient_vector->y2-gradient_vector->y1;
2058 length=sqrt(point.x*point.x+point.y*point.y);
2059 bounding_box=gradient->bounding_box;
2061 GetPixelInfo(image,&zero);
2062 image_view=AcquireAuthenticCacheView(image,exception);
2063 height=(size_t) (bounding_box.y+(ssize_t) bounding_box.height);
2064#if defined(MAGICKCORE_OPENMP_SUPPORT)
2065 #pragma omp parallel for schedule(static) shared(status) \
2066 magick_number_threads(image,image,height,1)
2068 for (y=bounding_box.y; y < (ssize_t) height; y++)
2089 if (status == MagickFalse)
2091 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(
size_t)
2092 bounding_box.width,1,exception);
2093 if (q == (Quantum *) NULL)
2100 offset=GetStopColorOffset(gradient,0,y);
2101 if (gradient->type != RadialGradient)
2102 offset*=MagickSafeReciprocal(length);
2103 width=(size_t) (bounding_box.x+(ssize_t) bounding_box.width);
2104 for (x=bounding_box.x; x < (ssize_t) width; x++)
2106 GetPixelInfoPixel(image,q,&pixel);
2107 switch (gradient->spread)
2109 case UndefinedSpread:
2112 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2113 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2115 offset=GetStopColorOffset(gradient,x,y);
2116 if (gradient->type != RadialGradient)
2117 offset*=MagickSafeReciprocal(length);
2119 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2120 if (offset < gradient->stops[i].offset)
2122 if ((offset < 0.0) || (i == 0))
2123 composite=gradient->stops[0].color;
2125 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2126 composite=gradient->stops[gradient->number_stops-1].color;
2131 alpha=(offset-gradient->stops[i].offset)/
2132 (gradient->stops[j].offset-gradient->stops[i].offset);
2133 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2134 &gradient->stops[j].color,alpha,&composite);
2140 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2141 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2143 offset=GetStopColorOffset(gradient,x,y);
2144 if (gradient->type != RadialGradient)
2145 offset*=MagickSafeReciprocal(length);
2149 if ((ssize_t) fmod(offset,2.0) == 0)
2150 offset=fmod(offset,1.0);
2152 offset=1.0-fmod(offset,1.0);
2153 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2154 if (offset < gradient->stops[i].offset)
2157 composite=gradient->stops[0].color;
2159 if (i == (ssize_t) gradient->number_stops)
2160 composite=gradient->stops[gradient->number_stops-1].color;
2165 alpha=(offset-gradient->stops[i].offset)/
2166 (gradient->stops[j].offset-gradient->stops[i].offset);
2167 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2168 &gradient->stops[j].color,alpha,&composite);
2180 antialias=MagickFalse;
2182 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2183 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2185 offset=GetStopColorOffset(gradient,x,y);
2186 if (gradient->type == LinearGradient)
2188 repeat=fmod(offset,length);
2190 repeat=length-fmod(-repeat,length);
2192 repeat=fmod(offset,length);
2193 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2194 MagickTrue : MagickFalse;
2195 offset=MagickSafeReciprocal(length)*repeat;
2199 repeat=fmod(offset,gradient->radius);
2201 repeat=gradient->radius-fmod(-repeat,gradient->radius);
2203 repeat=fmod(offset,gradient->radius);
2204 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2206 offset=repeat*MagickSafeReciprocal(gradient->radius);
2209 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2210 if (offset < gradient->stops[i].offset)
2213 composite=gradient->stops[0].color;
2215 if (i == (ssize_t) gradient->number_stops)
2216 composite=gradient->stops[gradient->number_stops-1].color;
2221 alpha=(offset-gradient->stops[i].offset)/
2222 (gradient->stops[j].offset-gradient->stops[i].offset);
2223 if (antialias != MagickFalse)
2225 if (gradient->type == LinearGradient)
2226 alpha=length-repeat;
2228 alpha=gradient->radius-repeat;
2230 j=(ssize_t) gradient->number_stops-1L;
2232 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2233 &gradient->stops[j].color,alpha,&composite);
2238 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2240 SetPixelViaPixelInfo(image,&pixel,q);
2241 q+=(ptrdiff_t) GetPixelChannels(image);
2243 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2246 image_view=DestroyCacheView(image_view);
2282static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2297 if ((mvg_info == (MVGInfo *) NULL) ||
2298 (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2299 (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2300 (mvg_info->extent == (
size_t *) NULL))
2301 return(MagickFalse);
2302 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2303 if ((proposed_extent <= 0.0) || (proposed_extent > (
double) MAGICK_SIZE_MAX))
2304 return(MagickFalse);
2305 extent=CastDoubleToSizeT(ceil(proposed_extent));
2306 if (extent <= *mvg_info->extent)
2308 if (extent > (GetMaxMemoryRequest()/
sizeof(PrimitiveInfo)))
2309 return(MagickFalse);
2310 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2311 *mvg_info->primitive_info,extent,
sizeof(PrimitiveInfo));
2312 if (primitive_info == (PrimitiveInfo *) NULL)
2317 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2318 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2319 return(MagickFalse);
2324 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2326 primitive_info[i].primitive=UndefinedPrimitive;
2327 primitive_info[i].text=(
char *) NULL;
2329 *mvg_info->primitive_info=primitive_info;
2330 *mvg_info->extent=extent;
2334static inline double GetDrawValue(
const char *magick_restrict
string,
2335 char **magick_restrict sentinel)
2338 **magick_restrict q;
2344 value=InterpretLocaleValue(
string,q);
2349static int MVGMacroCompare(
const void *target,
const void *source)
2355 p=(
const char *) target;
2356 q=(
const char *) source;
2357 return(strcmp(p,q));
2360static SplayTreeInfo *GetMVGMacros(
const char *primitive)
2378 if (primitive == (
const char *) NULL)
2379 return((SplayTreeInfo *) NULL);
2380 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2381 RelinquishMagickMemory);
2382 macro=AcquireString(primitive);
2383 token=AcquireString(primitive);
2384 extent=strlen(token)+MagickPathExtent;
2385 for (q=primitive; *q !=
'\0'; )
2387 if (GetNextToken(q,&q,extent,token) < 1)
2391 if (LocaleCompare(
"push",token) == 0)
2397 (void) GetNextToken(q,&q,extent,token);
2401 name[MagickPathExtent];
2412 (void) GetNextToken(q,&q,extent,token);
2415 (void) CopyMagickString(name,token,MagickPathExtent);
2417 for (p=q; *p !=
'\0'; )
2419 if (GetNextToken(p,&p,extent,token) < 1)
2423 if (LocaleCompare(token,
"pop") == 0)
2425 end=p-strlen(token)-1;
2428 if (LocaleCompare(token,
"push") == 0)
2430 if ((n == 0) && (end >= start))
2433 length=(size_t) (end-start);
2438 (void) GetNextToken(p,&p,extent,token);
2441 (void) CopyMagickString(macro,start,length);
2442 (void) AddValueToSplayTree(macros,ConstantString(name),
2443 ConstantString(macro));
2451 token=DestroyString(token);
2452 macro=DestroyString(macro);
2456static inline MagickBooleanType IsPoint(
const char *point)
2464 value=GetDrawValue(point,&p);
2465 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2469static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2470 const PointInfo point)
2472 primitive_info->point=point;
2473 primitive_info->coordinates=1;
2474 primitive_info->closed_subpath=MagickFalse;
2475 primitive_info->text=(
char *) NULL;
2479static MagickBooleanType RenderMVGContent(Image *image,
2480 const DrawInfo *draw_info,
const size_t depth,ExceptionInfo *exception)
2482#define RenderImageTag "Render/Image"
2489 keyword[MagickPathExtent],
2490 geometry[MagickPathExtent],
2492 pattern[MagickPathExtent],
2556 assert(image != (Image *) NULL);
2557 assert(image->signature == MagickCoreSignature);
2558 assert(draw_info != (DrawInfo *) NULL);
2559 assert(draw_info->signature == MagickCoreSignature);
2560 if (IsEventLogging() != MagickFalse)
2561 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2562 if (depth > MagickMaxRecursionDepth)
2563 ThrowBinaryException(DrawError,
"VectorGraphicsNestedTooDeeply",
2565 if ((draw_info->primitive == (
char *) NULL) ||
2566 (*draw_info->primitive ==
'\0'))
2567 return(MagickFalse);
2568 if (draw_info->debug != MagickFalse)
2569 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"begin draw-image");
2570 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2571 return(MagickFalse);
2572 if ((image->alpha_trait & BlendPixelTrait) == 0)
2574 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2575 if (status == MagickFalse)
2576 return(MagickFalse);
2578 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2579 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2580 primitive=FileToString(draw_info->primitive,~0UL,exception);
2582 primitive=AcquireString(draw_info->primitive);
2583 if (primitive == (
char *) NULL)
2584 return(MagickFalse);
2585 primitive_extent=(double) strlen(primitive);
2586 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2589 stops=(StopInfo *) NULL;
2593 graphic_context=(DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2594 if (graphic_context == (DrawInfo **) NULL)
2596 primitive=DestroyString(primitive);
2597 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2600 number_points=(size_t) PrimitiveExtentPad;
2601 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2602 (number_points+1),
sizeof(*primitive_info));
2603 if (primitive_info == (PrimitiveInfo *) NULL)
2605 primitive=DestroyString(primitive);
2606 for ( ; n >= 0; n--)
2607 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2608 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2609 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2612 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2613 sizeof(*primitive_info));
2614 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2615 mvg_info.primitive_info=(&primitive_info);
2616 mvg_info.extent=(&number_points);
2617 mvg_info.exception=exception;
2618 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2619 graphic_context[n]->viewbox=image->page;
2620 if ((image->page.width == 0) || (image->page.height == 0))
2622 graphic_context[n]->viewbox.width=image->columns;
2623 graphic_context[n]->viewbox.height=image->rows;
2625 token=AcquireString(primitive);
2626 extent=strlen(token)+MagickPathExtent;
2630 macros=GetMVGMacros(primitive);
2632 for (q=primitive; *q !=
'\0'; )
2637 if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2639 if (*keyword ==
'\0')
2641 if (*keyword ==
'#')
2646 while ((*q !=
'\n') && (*q !=
'\0'))
2650 p=q-strlen(keyword)-1;
2651 primitive_type=UndefinedPrimitive;
2652 current=graphic_context[n]->affine;
2653 GetAffineMatrix(&affine);
2662 if (LocaleCompare(
"affine",keyword) == 0)
2664 (void) GetNextToken(q,&q,extent,token);
2665 affine.sx=GetDrawValue(token,&next_token);
2666 if (token == next_token)
2667 ThrowPointExpectedException(token,exception);
2668 (void) GetNextToken(q,&q,extent,token);
2670 (void) GetNextToken(q,&q,extent,token);
2671 affine.ry=GetDrawValue(token,&next_token);
2672 if (token == next_token)
2673 ThrowPointExpectedException(token,exception);
2674 (void) GetNextToken(q,&q,extent,token);
2676 (void) GetNextToken(q,&q,extent,token);
2677 affine.rx=GetDrawValue(token,&next_token);
2678 if (token == next_token)
2679 ThrowPointExpectedException(token,exception);
2680 (void) GetNextToken(q,&q,extent,token);
2682 (void) GetNextToken(q,&q,extent,token);
2683 affine.sy=GetDrawValue(token,&next_token);
2684 if (token == next_token)
2685 ThrowPointExpectedException(token,exception);
2686 (void) GetNextToken(q,&q,extent,token);
2688 (void) GetNextToken(q,&q,extent,token);
2689 affine.tx=GetDrawValue(token,&next_token);
2690 if (token == next_token)
2691 ThrowPointExpectedException(token,exception);
2692 (void) GetNextToken(q,&q,extent,token);
2694 (void) GetNextToken(q,&q,extent,token);
2695 affine.ty=GetDrawValue(token,&next_token);
2696 if (token == next_token)
2697 ThrowPointExpectedException(token,exception);
2700 if (LocaleCompare(
"alpha",keyword) == 0)
2702 primitive_type=AlphaPrimitive;
2705 if (LocaleCompare(
"arc",keyword) == 0)
2707 primitive_type=ArcPrimitive;
2716 if (LocaleCompare(
"bezier",keyword) == 0)
2718 primitive_type=BezierPrimitive;
2721 if (LocaleCompare(
"border-color",keyword) == 0)
2723 (void) GetNextToken(q,&q,extent,token);
2724 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2725 &graphic_context[n]->border_color,exception);
2734 if (LocaleCompare(
"class",keyword) == 0)
2739 (void) GetNextToken(q,&q,extent,token);
2740 if ((*token ==
'\0') || (*token ==
';'))
2748 for (i=0; i <= n; i++)
2749 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2753 if (classDepth++ > MagickMaxRecursionDepth)
2755 (void) ThrowMagickException(exception,GetMagickModule(),
2756 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2760 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2761 if ((graphic_context[n]->render != MagickFalse) &&
2762 (mvg_class != (
const char *) NULL) && (p > primitive))
2773 (void) CloneString(&graphic_context[n]->
id,token);
2774 offset=(ssize_t) (p-primitive);
2775 elements=AcquireString(primitive);
2776 elements[offset]=
'\0';
2777 (void) ConcatenateString(&elements,mvg_class);
2778 (void) ConcatenateString(&elements,
"\n");
2779 (void) ConcatenateString(&elements,q);
2780 primitive=DestroyString(primitive);
2786 if (LocaleCompare(
"clip-path",keyword) == 0)
2794 (void) GetNextToken(q,&q,extent,token);
2800 (void) CloneString(&graphic_context[n]->clip_mask,token);
2801 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2802 if (clip_path != (
const char *) NULL)
2804 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2805 graphic_context[n]->clipping_mask=
2806 DestroyImage(graphic_context[n]->clipping_mask);
2807 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2808 graphic_context[n],token,clip_path,exception);
2809 if (graphic_context[n]->compliance != SVGCompliance)
2811 clip_path=(
const char *) GetValueFromSplayTree(macros,
2812 graphic_context[n]->clip_mask);
2813 if (clip_path != (
const char *) NULL)
2814 (void) SetImageArtifact(image,
2815 graphic_context[n]->clip_mask,clip_path);
2816 status&=(MagickStatusType) DrawClipPath(image,
2817 graphic_context[n],graphic_context[n]->clip_mask,
2823 if (LocaleCompare(
"clip-rule",keyword) == 0)
2828 (void) GetNextToken(q,&q,extent,token);
2829 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2831 if (fill_rule == -1)
2836 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2839 if (LocaleCompare(
"clip-units",keyword) == 0)
2844 (void) GetNextToken(q,&q,extent,token);
2845 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2847 if (clip_units == -1)
2852 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2853 if (clip_units == ObjectBoundingBox)
2855 GetAffineMatrix(¤t);
2856 affine.sx=draw_info->bounds.x2;
2857 affine.sy=draw_info->bounds.y2;
2858 affine.tx=draw_info->bounds.x1;
2859 affine.ty=draw_info->bounds.y1;
2864 if (LocaleCompare(
"circle",keyword) == 0)
2866 primitive_type=CirclePrimitive;
2869 if (LocaleCompare(
"color",keyword) == 0)
2871 primitive_type=ColorPrimitive;
2874 if (LocaleCompare(
"compliance",keyword) == 0)
2880 (void) GetNextToken(q,&q,extent,token);
2881 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2882 MagickComplianceOptions,MagickFalse,token);
2885 if (LocaleCompare(
"currentColor",keyword) == 0)
2887 (void) GetNextToken(q,&q,extent,token);
2896 if (LocaleCompare(
"decorate",keyword) == 0)
2901 (void) GetNextToken(q,&q,extent,token);
2902 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2909 graphic_context[n]->decorate=(DecorationType) decorate;
2912 if (LocaleCompare(
"density",keyword) == 0)
2914 (void) GetNextToken(q,&q,extent,token);
2915 (void) CloneString(&graphic_context[n]->density,token);
2918 if (LocaleCompare(
"direction",keyword) == 0)
2923 (void) GetNextToken(q,&q,extent,token);
2924 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2926 if (direction == -1)
2929 graphic_context[n]->direction=(DirectionType) direction;
2938 if (LocaleCompare(
"ellipse",keyword) == 0)
2940 primitive_type=EllipsePrimitive;
2943 if (LocaleCompare(
"encoding",keyword) == 0)
2945 (void) GetNextToken(q,&q,extent,token);
2946 (void) CloneString(&graphic_context[n]->encoding,token);
2955 if (LocaleCompare(
"fill",keyword) == 0)
2960 (void) GetNextToken(q,&q,extent,token);
2961 if (graphic_context[n]->clip_path != MagickFalse)
2963 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2964 if (mvg_class != (
const char *) NULL)
2966 (void) DrawPatternPath(image,draw_info,mvg_class,
2967 &graphic_context[n]->fill_pattern,exception);
2970 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
2971 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
2973 (void) DrawPatternPath(image,draw_info,token,
2974 &graphic_context[n]->fill_pattern,exception);
2977 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2978 &graphic_context[n]->fill,exception);
2979 if (graphic_context[n]->fill_alpha != (
double) OpaqueAlpha)
2980 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2983 if (LocaleCompare(
"fill-opacity",keyword) == 0)
2988 (void) GetNextToken(q,&q,extent,token);
2989 if (graphic_context[n]->clip_path != MagickFalse)
2991 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
2992 opacity=MagickMin(MagickMax(factor*
2993 GetDrawValue(token,&next_token),0.0),1.0);
2994 if (token == next_token)
2995 ThrowPointExpectedException(token,exception);
2996 if (graphic_context[n]->compliance == SVGCompliance)
2997 graphic_context[n]->fill_alpha*=opacity;
2999 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3000 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3001 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3003 graphic_context[n]->fill.alpha=(MagickRealType)
3004 ClampToQuantum((
double) QuantumRange*opacity);
3005 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3008 if (LocaleCompare(
"fill-rule",keyword) == 0)
3013 (void) GetNextToken(q,&q,extent,token);
3014 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3016 if (fill_rule == -1)
3021 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3024 if (LocaleCompare(
"font",keyword) == 0)
3026 (void) GetNextToken(q,&q,extent,token);
3027 (void) CloneString(&graphic_context[n]->font,token);
3028 if (LocaleCompare(
"none",token) == 0)
3029 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3030 graphic_context[n]->font);
3033 if (LocaleCompare(
"font-family",keyword) == 0)
3035 (void) GetNextToken(q,&q,extent,token);
3036 (void) CloneString(&graphic_context[n]->family,token);
3039 if (LocaleCompare(
"font-size",keyword) == 0)
3041 (void) GetNextToken(q,&q,extent,token);
3042 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3043 if (token == next_token)
3044 ThrowPointExpectedException(token,exception);
3047 if (LocaleCompare(
"font-stretch",keyword) == 0)
3052 (void) GetNextToken(q,&q,extent,token);
3053 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3059 graphic_context[n]->stretch=(StretchType) stretch;
3062 if (LocaleCompare(
"font-style",keyword) == 0)
3067 (void) GetNextToken(q,&q,extent,token);
3068 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3074 graphic_context[n]->style=(StyleType) style;
3077 if (LocaleCompare(
"font-weight",keyword) == 0)
3082 (void) GetNextToken(q,&q,extent,token);
3083 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3085 weight=(ssize_t) StringToUnsignedLong(token);
3086 graphic_context[n]->weight=(size_t) weight;
3095 if (LocaleCompare(
"gradient-units",keyword) == 0)
3097 (void) GetNextToken(q,&q,extent,token);
3100 if (LocaleCompare(
"gravity",keyword) == 0)
3105 (void) GetNextToken(q,&q,extent,token);
3106 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3112 graphic_context[n]->gravity=(GravityType) gravity;
3121 if (LocaleCompare(
"image",keyword) == 0)
3126 primitive_type=ImagePrimitive;
3127 (void) GetNextToken(q,&q,extent,token);
3128 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3134 graphic_context[n]->compose=(CompositeOperator) compose;
3137 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3139 (void) GetNextToken(q,&q,extent,token);
3140 graphic_context[n]->interline_spacing=GetDrawValue(token,
3142 if (token == next_token)
3143 ThrowPointExpectedException(token,exception);
3146 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3148 (void) GetNextToken(q,&q,extent,token);
3149 graphic_context[n]->interword_spacing=GetDrawValue(token,
3151 if (token == next_token)
3152 ThrowPointExpectedException(token,exception);
3161 if (LocaleCompare(
"kerning",keyword) == 0)
3163 (void) GetNextToken(q,&q,extent,token);
3164 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3165 if (token == next_token)
3166 ThrowPointExpectedException(token,exception);
3175 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3177 (void) GetNextToken(q,&q,extent,token);
3178 if (IsPoint(token) == MagickFalse)
3180 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3181 clone_info->text=AcquireString(
" ");
3182 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3184 graphic_context[n]->kerning=metrics.width*
3185 GetDrawValue(token,&next_token);
3186 clone_info=DestroyDrawInfo(clone_info);
3187 if (token == next_token)
3188 ThrowPointExpectedException(token,exception);
3191 if (LocaleCompare(
"line",keyword) == 0)
3193 primitive_type=LinePrimitive;
3202 if (LocaleCompare(
"mask",keyword) == 0)
3210 (void) GetNextToken(q,&q,extent,token);
3211 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3212 if (mask_path != (
const char *) NULL)
3214 if (graphic_context[n]->composite_mask != (Image *) NULL)
3215 graphic_context[n]->composite_mask=
3216 DestroyImage(graphic_context[n]->composite_mask);
3217 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3218 graphic_context[n],token,mask_path,exception);
3219 if (graphic_context[n]->compliance != SVGCompliance)
3220 status=SetImageMask(image,CompositePixelMask,
3221 graphic_context[n]->composite_mask,exception);
3231 if (LocaleCompare(
"offset",keyword) == 0)
3233 (void) GetNextToken(q,&q,extent,token);
3236 if (LocaleCompare(
"opacity",keyword) == 0)
3241 (void) GetNextToken(q,&q,extent,token);
3242 if (graphic_context[n]->clip_path != MagickFalse)
3244 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3245 opacity=MagickMin(MagickMax(factor*
3246 GetDrawValue(token,&next_token),0.0),1.0);
3247 if (token == next_token)
3248 ThrowPointExpectedException(token,exception);
3249 if (graphic_context[n]->compliance == SVGCompliance)
3251 graphic_context[n]->fill_alpha*=opacity;
3252 graphic_context[n]->stroke_alpha*=opacity;
3256 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3257 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3259 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3261 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3262 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3266 graphic_context[n]->fill.alpha=(MagickRealType)
3267 ClampToQuantum((
double) QuantumRange*opacity);
3268 graphic_context[n]->stroke.alpha=(MagickRealType)
3269 ClampToQuantum((
double) QuantumRange*opacity);
3271 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3272 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3281 if (LocaleCompare(
"path",keyword) == 0)
3283 primitive_type=PathPrimitive;
3286 if (LocaleCompare(
"point",keyword) == 0)
3288 primitive_type=PointPrimitive;
3291 if (LocaleCompare(
"polyline",keyword) == 0)
3293 primitive_type=PolylinePrimitive;
3296 if (LocaleCompare(
"polygon",keyword) == 0)
3298 primitive_type=PolygonPrimitive;
3301 if (LocaleCompare(
"pop",keyword) == 0)
3303 if (GetNextToken(q,&q,extent,token) < 1)
3305 if (LocaleCompare(
"class",token) == 0)
3307 if (LocaleCompare(
"clip-path",token) == 0)
3309 if (LocaleCompare(
"defs",token) == 0)
3312 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3316 if (LocaleCompare(
"gradient",token) == 0)
3318 if (LocaleCompare(
"graphic-context",token) == 0)
3322 (void) ThrowMagickException(exception,GetMagickModule(),
3323 DrawError,
"UnbalancedGraphicContextPushPop",
"`%s'",token);
3328 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3329 (graphic_context[n]->compliance != SVGCompliance))
3330 if (LocaleCompare(graphic_context[n]->clip_mask,
3331 graphic_context[n-1]->clip_mask) != 0)
3332 status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3334 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3338 if (LocaleCompare(
"mask",token) == 0)
3340 if (LocaleCompare(
"pattern",token) == 0)
3342 if (LocaleCompare(
"symbol",token) == 0)
3345 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3352 if (LocaleCompare(
"push",keyword) == 0)
3354 if (GetNextToken(q,&q,extent,token) < 1)
3356 if (LocaleCompare(
"class",token) == 0)
3361 for (p=q; *q !=
'\0'; )
3363 if (GetNextToken(q,&q,extent,token) < 1)
3365 if (LocaleCompare(token,
"pop") != 0)
3367 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3368 if (LocaleCompare(token,
"class") != 0)
3372 (void) GetNextToken(q,&q,extent,token);
3375 if (LocaleCompare(
"clip-path",token) == 0)
3377 (void) GetNextToken(q,&q,extent,token);
3378 for (p=q; *q !=
'\0'; )
3380 if (GetNextToken(q,&q,extent,token) < 1)
3382 if (LocaleCompare(token,
"pop") != 0)
3384 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3385 if (LocaleCompare(token,
"clip-path") != 0)
3389 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3394 (void) GetNextToken(q,&q,extent,token);
3397 if (LocaleCompare(
"defs",token) == 0)
3400 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3404 if (LocaleCompare(
"gradient",token) == 0)
3407 key[2*MagickPathExtent],
3408 name[MagickPathExtent],
3409 type[MagickPathExtent];
3414 (void) GetNextToken(q,&q,extent,token);
3415 (void) CopyMagickString(name,token,MagickPathExtent);
3416 (void) GetNextToken(q,&q,extent,token);
3417 (void) CopyMagickString(type,token,MagickPathExtent);
3418 (void) GetNextToken(q,&q,extent,token);
3419 segment.x1=GetDrawValue(token,&next_token);
3420 if (token == next_token)
3421 ThrowPointExpectedException(token,exception);
3422 (void) GetNextToken(q,&q,extent,token);
3424 (void) GetNextToken(q,&q,extent,token);
3425 segment.y1=GetDrawValue(token,&next_token);
3426 if (token == next_token)
3427 ThrowPointExpectedException(token,exception);
3428 (void) GetNextToken(q,&q,extent,token);
3430 (void) GetNextToken(q,&q,extent,token);
3431 segment.x2=GetDrawValue(token,&next_token);
3432 if (token == next_token)
3433 ThrowPointExpectedException(token,exception);
3434 (void) GetNextToken(q,&q,extent,token);
3436 (void) GetNextToken(q,&q,extent,token);
3437 segment.y2=GetDrawValue(token,&next_token);
3438 if (token == next_token)
3439 ThrowPointExpectedException(token,exception);
3440 if (LocaleCompare(type,
"radial") == 0)
3442 (void) GetNextToken(q,&q,extent,token);
3444 (void) GetNextToken(q,&q,extent,token);
3446 for (p=q; *q !=
'\0'; )
3448 if (GetNextToken(q,&q,extent,token) < 1)
3450 if (LocaleCompare(token,
"pop") != 0)
3452 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3453 if (LocaleCompare(token,
"gradient") != 0)
3457 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3458 (p == (
char *) NULL) || ((q-4) < p) ||
3459 ((
size_t) (q-p+4+1) > extent))
3464 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3465 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3466 graphic_context[n]->affine.ry*segment.y1+
3467 graphic_context[n]->affine.tx;
3468 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3469 graphic_context[n]->affine.sy*segment.y1+
3470 graphic_context[n]->affine.ty;
3471 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3472 graphic_context[n]->affine.ry*segment.y2+
3473 graphic_context[n]->affine.tx;
3474 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3475 graphic_context[n]->affine.sy*segment.y2+
3476 graphic_context[n]->affine.ty;
3477 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3478 (void) SetImageArtifact(image,key,token);
3479 (void) FormatLocaleString(key,MagickPathExtent,
"%s-type",name);
3480 (void) SetImageArtifact(image,key,type);
3481 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3483 (void) FormatLocaleString(geometry,MagickPathExtent,
3484 "%gx%g%+.15g%+.15g",
3485 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3486 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3487 bounds.x1,bounds.y1);
3488 (void) SetImageArtifact(image,key,geometry);
3489 (void) GetNextToken(q,&q,extent,token);
3492 if (LocaleCompare(
"graphic-context",token) == 0)
3495 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3496 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3497 if (graphic_context == (DrawInfo **) NULL)
3499 (void) ThrowMagickException(exception,GetMagickModule(),
3500 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3505 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3506 graphic_context[n-1]);
3509 (void) GetNextToken(q,&q,extent,token);
3510 (void) CloneString(&graphic_context[n]->
id,token);
3512 if (n > MagickMaxRecursionDepth)
3514 (void) ThrowMagickException(exception,GetMagickModule(),
3515 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",
3521 if (LocaleCompare(
"mask",token) == 0)
3523 (void) GetNextToken(q,&q,extent,token);
3526 if (LocaleCompare(
"pattern",token) == 0)
3529 key[2*MagickPathExtent],
3530 name[MagickPathExtent];
3535 (void) GetNextToken(q,&q,extent,token);
3536 (void) CopyMagickString(name,token,MagickPathExtent);
3537 (void) GetNextToken(q,&q,extent,token);
3538 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3540 if (token == next_token)
3541 ThrowPointExpectedException(token,exception);
3542 (void) GetNextToken(q,&q,extent,token);
3544 (void) GetNextToken(q,&q,extent,token);
3545 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3547 if (token == next_token)
3548 ThrowPointExpectedException(token,exception);
3549 (void) GetNextToken(q,&q,extent,token);
3551 (void) GetNextToken(q,&q,extent,token);
3552 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3554 if (token == next_token)
3555 ThrowPointExpectedException(token,exception);
3556 (void) GetNextToken(q,&q,extent,token);
3558 (void) GetNextToken(q,&q,extent,token);
3559 region.height=CastDoubleToSizeT(GetDrawValue(token,
3561 if (token == next_token)
3562 ThrowPointExpectedException(token,exception);
3563 for (p=q; *q !=
'\0'; )
3565 if (GetNextToken(q,&q,extent,token) < 1)
3567 if (LocaleCompare(token,
"pop") != 0)
3569 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3570 if (LocaleCompare(token,
"pattern") != 0)
3574 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3575 ((q-4) < p) || ((
size_t) (q-p+4+1) > extent))
3580 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3581 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3582 (void) SetImageArtifact(image,key,token);
3583 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3585 (void) FormatLocaleString(geometry,MagickPathExtent,
3586 "%.20gx%.20g%+.20g%+.20g",(
double) region.width,(
double)
3587 region.height,(
double) region.x,(
double) region.y);
3588 (void) SetImageArtifact(image,key,geometry);
3589 (void) GetNextToken(q,&q,extent,token);
3592 if (LocaleCompare(
"symbol",token) == 0)
3595 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3608 if (LocaleCompare(
"rectangle",keyword) == 0)
3610 primitive_type=RectanglePrimitive;
3613 if (LocaleCompare(
"rotate",keyword) == 0)
3615 (void) GetNextToken(q,&q,extent,token);
3616 angle=GetDrawValue(token,&next_token);
3617 if (token == next_token)
3618 ThrowPointExpectedException(token,exception);
3619 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3620 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3621 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3622 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3625 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3627 primitive_type=RoundRectanglePrimitive;
3636 if (LocaleCompare(
"scale",keyword) == 0)
3638 (void) GetNextToken(q,&q,extent,token);
3639 affine.sx=GetDrawValue(token,&next_token);
3640 if (token == next_token)
3641 ThrowPointExpectedException(token,exception);
3642 (void) GetNextToken(q,&q,extent,token);
3644 (void) GetNextToken(q,&q,extent,token);
3645 affine.sy=GetDrawValue(token,&next_token);
3646 if (token == next_token)
3647 ThrowPointExpectedException(token,exception);
3650 if (LocaleCompare(
"skewX",keyword) == 0)
3652 (void) GetNextToken(q,&q,extent,token);
3653 angle=GetDrawValue(token,&next_token);
3654 if (token == next_token)
3655 ThrowPointExpectedException(token,exception);
3656 affine.ry=sin(DegreesToRadians(angle));
3659 if (LocaleCompare(
"skewY",keyword) == 0)
3661 (void) GetNextToken(q,&q,extent,token);
3662 angle=GetDrawValue(token,&next_token);
3663 if (token == next_token)
3664 ThrowPointExpectedException(token,exception);
3665 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3668 if (LocaleCompare(
"stop-color",keyword) == 0)
3674 if (number_stops == 1)
3675 stops=(StopInfo *) AcquireQuantumMemory(2,
sizeof(*stops));
3677 if (number_stops > 2)
3678 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3680 if (stops == (StopInfo *) NULL)
3682 (void) ThrowMagickException(exception,GetMagickModule(),
3683 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3688 (void) GetNextToken(q,&q,extent,token);
3689 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3690 &stop_color,exception);
3691 stops[number_stops-1].color=stop_color;
3692 (void) GetNextToken(q,&q,extent,token);
3693 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3694 stops[number_stops-1].offset=factor*GetDrawValue(token,
3696 if (token == next_token)
3697 ThrowPointExpectedException(token,exception);
3700 if (LocaleCompare(
"stroke",keyword) == 0)
3705 (void) GetNextToken(q,&q,extent,token);
3706 if (graphic_context[n]->clip_path != MagickFalse)
3708 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3709 if (mvg_class != (
const char *) NULL)
3711 (void) DrawPatternPath(image,draw_info,mvg_class,
3712 &graphic_context[n]->stroke_pattern,exception);
3715 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3716 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3718 (void) DrawPatternPath(image,draw_info,token,
3719 &graphic_context[n]->stroke_pattern,exception);
3722 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3723 &graphic_context[n]->stroke,exception);
3724 if (graphic_context[n]->stroke_alpha != (
double) OpaqueAlpha)
3725 graphic_context[n]->stroke.alpha=
3726 graphic_context[n]->stroke_alpha;
3729 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3731 (void) GetNextToken(q,&q,extent,token);
3732 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3733 MagickTrue : MagickFalse;
3736 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3738 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3739 graphic_context[n]->dash_pattern=(
double *)
3740 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3741 if (IsPoint(q) != MagickFalse)
3747 (void) GetNextToken(r,&r,extent,token);
3749 (void) GetNextToken(r,&r,extent,token);
3750 for (x=0; IsPoint(token) != MagickFalse; x++)
3752 (void) GetNextToken(r,&r,extent,token);
3754 (void) GetNextToken(r,&r,extent,token);
3756 graphic_context[n]->dash_pattern=(
double *)
3757 AcquireQuantumMemory((
size_t) (2*x+2),
3758 sizeof(*graphic_context[n]->dash_pattern));
3759 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3761 (void) ThrowMagickException(exception,GetMagickModule(),
3762 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3767 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3768 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3769 for (j=0; j < x; j++)
3771 (void) GetNextToken(q,&q,extent,token);
3773 (void) GetNextToken(q,&q,extent,token);
3774 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3776 if (token == next_token)
3777 ThrowPointExpectedException(token,exception);
3778 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3781 if ((x & 0x01) != 0)
3782 for ( ; j < (2*x); j++)
3783 graphic_context[n]->dash_pattern[j]=
3784 graphic_context[n]->dash_pattern[j-x];
3785 graphic_context[n]->dash_pattern[j]=0.0;
3788 (void) GetNextToken(q,&q,extent,token);
3791 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3793 (void) GetNextToken(q,&q,extent,token);
3794 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3795 if (token == next_token)
3796 ThrowPointExpectedException(token,exception);
3799 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3804 (void) GetNextToken(q,&q,extent,token);
3805 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3811 graphic_context[n]->linecap=(LineCap) linecap;
3814 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3819 (void) GetNextToken(q,&q,extent,token);
3820 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3827 graphic_context[n]->linejoin=(LineJoin) linejoin;
3830 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3832 (void) GetNextToken(q,&q,extent,token);
3833 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3836 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3841 (void) GetNextToken(q,&q,extent,token);
3842 if (graphic_context[n]->clip_path != MagickFalse)
3844 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3845 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3847 if (token == next_token)
3848 ThrowPointExpectedException(token,exception);
3849 if (graphic_context[n]->compliance == SVGCompliance)
3850 graphic_context[n]->stroke_alpha*=opacity;
3852 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3853 if (graphic_context[n]->stroke.alpha != (
double) TransparentAlpha)
3854 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3856 graphic_context[n]->stroke.alpha=(MagickRealType)
3857 ClampToQuantum((
double) QuantumRange*opacity);
3858 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3861 if (LocaleCompare(
"stroke-width",keyword) == 0)
3863 (void) GetNextToken(q,&q,extent,token);
3864 if (graphic_context[n]->clip_path != MagickFalse)
3866 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3867 if ((token == next_token) ||
3868 (graphic_context[n]->stroke_width < 0.0))
3869 ThrowPointExpectedException(token,exception);
3878 if (LocaleCompare(
"text",keyword) == 0)
3880 primitive_type=TextPrimitive;
3884 if (LocaleCompare(
"text-align",keyword) == 0)
3889 (void) GetNextToken(q,&q,extent,token);
3890 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3896 graphic_context[n]->align=(AlignType) align;
3899 if (LocaleCompare(
"text-anchor",keyword) == 0)
3904 (void) GetNextToken(q,&q,extent,token);
3905 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3911 graphic_context[n]->align=(AlignType) align;
3914 if (LocaleCompare(
"text-antialias",keyword) == 0)
3916 (void) GetNextToken(q,&q,extent,token);
3917 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3918 MagickTrue : MagickFalse;
3921 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3923 (void) GetNextToken(q,&q,extent,token);
3924 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3925 &graphic_context[n]->undercolor,exception);
3928 if (LocaleCompare(
"translate",keyword) == 0)
3930 (void) GetNextToken(q,&q,extent,token);
3931 affine.tx=GetDrawValue(token,&next_token);
3932 if (token == next_token)
3933 ThrowPointExpectedException(token,exception);
3934 (void) GetNextToken(q,&q,extent,token);
3936 (void) GetNextToken(q,&q,extent,token);
3937 affine.ty=GetDrawValue(token,&next_token);
3938 if (token == next_token)
3939 ThrowPointExpectedException(token,exception);
3949 if (LocaleCompare(
"use",keyword) == 0)
3957 (void) GetNextToken(q,&q,extent,token);
3958 use=(
const char *) GetValueFromSplayTree(macros,token);
3959 if (use != (
const char *) NULL)
3961 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3962 (void) CloneString(&clone_info->primitive,use);
3963 status=RenderMVGContent(image,clone_info,depth+1,exception);
3964 clone_info=DestroyDrawInfo(clone_info);
3974 if (LocaleCompare(
"viewbox",keyword) == 0)
3976 (void) GetNextToken(q,&q,extent,token);
3977 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
3978 GetDrawValue(token,&next_token)-0.5));
3979 if (token == next_token)
3980 ThrowPointExpectedException(token,exception);
3981 (void) GetNextToken(q,&q,extent,token);
3983 (void) GetNextToken(q,&q,extent,token);
3984 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
3985 ceil(GetDrawValue(token,&next_token)-0.5));
3986 if (token == next_token)
3987 ThrowPointExpectedException(token,exception);
3988 (void) GetNextToken(q,&q,extent,token);
3990 (void) GetNextToken(q,&q,extent,token);
3991 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
3992 GetDrawValue(token,&next_token)+0.5));
3993 if (token == next_token)
3994 ThrowPointExpectedException(token,exception);
3995 (void) GetNextToken(q,&q,extent,token);
3997 (void) GetNextToken(q,&q,extent,token);
3998 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
3999 GetDrawValue(token,&next_token)+0.5));
4000 if (token == next_token)
4001 ThrowPointExpectedException(token,exception);
4010 if (LocaleCompare(
"word-spacing",keyword) == 0)
4012 (void) GetNextToken(q,&q,extent,token);
4013 graphic_context[n]->interword_spacing=GetDrawValue(token,
4015 if (token == next_token)
4016 ThrowPointExpectedException(token,exception);
4028 if (status == MagickFalse)
4030 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4031 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4032 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4033 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4035 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4036 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4037 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4038 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4039 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4041 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4044 if (primitive_type == UndefinedPrimitive)
4048 if (number_stops > 1)
4053 type=LinearGradient;
4054 if (draw_info->gradient.type == RadialGradient)
4055 type=RadialGradient;
4056 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4059 if (number_stops > 0)
4060 stops=(StopInfo *) RelinquishMagickMemory(stops);
4062 if ((draw_info->debug != MagickFalse) && (q > p))
4063 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int)
4070 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4071 if (primitive_info[i].text != (
char *) NULL)
4072 primitive_info[i].text=DestroyString(primitive_info[i].text);
4076 primitive_info[0].primitive=primitive_type;
4077 primitive_info[0].point.x=0.0;
4078 primitive_info[0].point.y=0.0;
4079 primitive_info[0].coordinates=0;
4080 primitive_info[0].method=FloodfillMethod;
4081 primitive_info[0].closed_subpath=MagickFalse;
4082 for (x=0; *q !=
'\0'; x++)
4087 if (IsPoint(q) == MagickFalse)
4089 (void) GetNextToken(q,&q,extent,token);
4090 point.x=GetDrawValue(token,&next_token);
4091 if (token == next_token)
4092 ThrowPointExpectedException(token,exception);
4093 (void) GetNextToken(q,&q,extent,token);
4095 (void) GetNextToken(q,&q,extent,token);
4096 point.y=GetDrawValue(token,&next_token);
4097 if (token == next_token)
4098 ThrowPointExpectedException(token,exception);
4099 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4101 (void) GetNextToken(q,&q,extent,token);
4102 primitive_info[i].primitive=primitive_type;
4103 primitive_info[i].point=point;
4104 primitive_info[i].coordinates=0;
4105 primitive_info[i].method=FloodfillMethod;
4106 primitive_info[i].closed_subpath=MagickFalse;
4109 if (i < (ssize_t) number_points)
4111 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4113 primitive_info=(*mvg_info.primitive_info);
4115 if (status == MagickFalse)
4117 if (primitive_info[j].text != (
char *) NULL)
4118 primitive_info[j].text=DestroyString(primitive_info[j].text);
4119 primitive_info[j].primitive=primitive_type;
4120 primitive_info[j].coordinates=(size_t) x;
4121 primitive_info[j].method=FloodfillMethod;
4122 primitive_info[j].closed_subpath=MagickFalse;
4126 bounds.x1=primitive_info[j].point.x;
4127 bounds.y1=primitive_info[j].point.y;
4128 bounds.x2=primitive_info[j].point.x;
4129 bounds.y2=primitive_info[j].point.y;
4130 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4132 point=primitive_info[j+k].point;
4133 if (point.x < bounds.x1)
4135 if (point.y < bounds.y1)
4137 if (point.x > bounds.x2)
4139 if (point.y > bounds.y2)
4145 coordinates=(double) primitive_info[j].coordinates;
4146 switch (primitive_type)
4148 case RectanglePrimitive:
4153 case RoundRectanglePrimitive:
4160 alpha=bounds.x2-bounds.x1;
4161 beta=bounds.y2-bounds.y1;
4162 radius=hypot(alpha,beta);
4164 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4165 BezierQuantum+360.0;
4168 case BezierPrimitive:
4170 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4179 (void) GetNextToken(q,&q,extent,token);
4182 for (s=token; *s !=
'\0'; s=t)
4187 value=GetDrawValue(s,&t);
4196 for (s=token; *s !=
'\0'; s++)
4197 if (strspn(s,
"AaCcQqSsTt") != 0)
4198 coordinates+=(20.0*BezierQuantum)+360.0;
4204 if (status == MagickFalse)
4206 if (((
size_t) (i+coordinates)) >= number_points)
4211 number_points+=(size_t) coordinates+1;
4212 if (number_points < (
size_t) coordinates)
4214 (void) ThrowMagickException(exception,GetMagickModule(),
4215 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4221 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4223 primitive_info=(*mvg_info.primitive_info);
4225 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4226 PrimitiveExtentPad);
4227 primitive_info=(*mvg_info.primitive_info);
4228 if (status == MagickFalse)
4231 switch (primitive_type)
4233 case PointPrimitive:
4236 if (primitive_info[j].coordinates != 1)
4241 status&=(MagickStatusType) TracePoint(primitive_info+j,
4242 primitive_info[j].point);
4243 primitive_info=(*mvg_info.primitive_info);
4244 i=j+(ssize_t) primitive_info[j].coordinates;
4249 if (primitive_info[j].coordinates != 2)
4254 status&=(MagickStatusType) TraceLine(primitive_info+j,
4255 primitive_info[j].point,primitive_info[j+1].point);
4256 primitive_info=(*mvg_info.primitive_info);
4257 i=j+(ssize_t) primitive_info[j].coordinates;
4260 case RectanglePrimitive:
4262 if (primitive_info[j].coordinates != 2)
4267 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4268 primitive_info[j].point,primitive_info[j+1].point);
4269 primitive_info=(*mvg_info.primitive_info);
4270 i=j+(ssize_t) primitive_info[j].coordinates;
4273 case RoundRectanglePrimitive:
4275 if (primitive_info[j].coordinates != 3)
4280 if ((primitive_info[j+2].point.x < 0.0) ||
4281 (primitive_info[j+2].point.y < 0.0))
4286 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4291 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4296 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4297 primitive_info[j].point,primitive_info[j+1].point,
4298 primitive_info[j+2].point);
4299 primitive_info=(*mvg_info.primitive_info);
4300 i=j+(ssize_t) primitive_info[j].coordinates;
4305 if (primitive_info[j].coordinates != 3)
4310 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4311 primitive_info[j+1].point,primitive_info[j+2].point);
4312 primitive_info=(*mvg_info.primitive_info);
4313 i=j+(ssize_t) primitive_info[j].coordinates;
4316 case EllipsePrimitive:
4318 if (primitive_info[j].coordinates != 3)
4323 if ((primitive_info[j+1].point.x < 0.0) ||
4324 (primitive_info[j+1].point.y < 0.0))
4329 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4330 primitive_info[j].point,primitive_info[j+1].point,
4331 primitive_info[j+2].point);
4332 primitive_info=(*mvg_info.primitive_info);
4333 i=j+(ssize_t) primitive_info[j].coordinates;
4336 case CirclePrimitive:
4338 if (primitive_info[j].coordinates != 2)
4343 status&=(MagickStatusType) TraceCircle(&mvg_info,
4344 primitive_info[j].point,primitive_info[j+1].point);
4345 primitive_info=(*mvg_info.primitive_info);
4346 i=j+(ssize_t) primitive_info[j].coordinates;
4349 case PolylinePrimitive:
4351 if (primitive_info[j].coordinates < 1)
4358 case PolygonPrimitive:
4360 if (primitive_info[j].coordinates < 3)
4365 primitive_info[i]=primitive_info[j];
4366 primitive_info[i].coordinates=0;
4367 primitive_info[j].coordinates++;
4368 primitive_info[j].closed_subpath=MagickTrue;
4372 case BezierPrimitive:
4374 if (primitive_info[j].coordinates < 3)
4379 status&=(MagickStatusType) TraceBezier(&mvg_info,
4380 primitive_info[j].coordinates);
4381 primitive_info=(*mvg_info.primitive_info);
4382 i=j+(ssize_t) primitive_info[j].coordinates;
4387 coordinates=(double) TracePath(&mvg_info,token,exception);
4388 primitive_info=(*mvg_info.primitive_info);
4389 if (coordinates < 0.0)
4394 i=(ssize_t) (j+coordinates);
4397 case AlphaPrimitive:
4398 case ColorPrimitive:
4403 if (primitive_info[j].coordinates != 1)
4408 (void) GetNextToken(q,&q,extent,token);
4409 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4415 primitive_info[j].method=(PaintMethod) method;
4420 if (primitive_info[j].coordinates != 1)
4426 (void) GetNextToken(q,&q,extent,token);
4427 (void) CloneString(&primitive_info[j].text,token);
4431 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4432 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4433 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4435 mvg_info.point=primitive_info->point;
4436 primitive_info->point.x+=cursor;
4440 mvg_info.point=primitive_info->point;
4443 clone_info->render=MagickFalse;
4444 clone_info->text=AcquireString(token);
4445 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4446 &metrics,exception);
4447 clone_info=DestroyDrawInfo(clone_info);
4448 cursor+=metrics.width;
4449 if (graphic_context[n]->compliance != SVGCompliance)
4453 case ImagePrimitive:
4455 if (primitive_info[j].coordinates != 2)
4460 (void) GetNextToken(q,&q,extent,token);
4461 (void) CloneString(&primitive_info[j].text,token);
4468 primitive_info[i].primitive=UndefinedPrimitive;
4469 if ((draw_info->debug != MagickFalse) && (q > p))
4470 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int) (q-p),p);
4474 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4475 &graphic_context[n]->affine));
4476 primitive_info=(*mvg_info.primitive_info);
4479 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4480 graphic_context[n]->stroke_width);
4481 primitive_info=(*mvg_info.primitive_info);
4489 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4491 point=primitive_info[i].point;
4492 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4493 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4494 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4495 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4496 point=primitive_info[i].point;
4497 if (point.x < graphic_context[n]->bounds.x1)
4498 graphic_context[n]->bounds.x1=point.x;
4499 if (point.y < graphic_context[n]->bounds.y1)
4500 graphic_context[n]->bounds.y1=point.y;
4501 if (point.x > graphic_context[n]->bounds.x2)
4502 graphic_context[n]->bounds.x2=point.x;
4503 if (point.y > graphic_context[n]->bounds.y2)
4504 graphic_context[n]->bounds.y2=point.y;
4505 if (primitive_info[i].primitive == ImagePrimitive)
4507 if (i >= (ssize_t) number_points)
4508 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4510 if (graphic_context[n]->render != MagickFalse)
4512 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4513 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4514 (LocaleCompare(graphic_context[n]->clip_mask,
4515 graphic_context[n-1]->clip_mask) != 0))
4520 clip_path=(
const char *) GetValueFromSplayTree(macros,
4521 graphic_context[n]->clip_mask);
4522 if (clip_path != (
const char *) NULL)
4523 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4525 status&=(MagickStatusType) DrawClipPath(image,graphic_context[n],
4526 graphic_context[n]->clip_mask,exception);
4528 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4529 primitive_info,exception);
4531 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4533 if (proceed == MagickFalse)
4538 if (draw_info->debug != MagickFalse)
4539 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4543 macros=DestroySplayTree(macros);
4544 token=DestroyString(token);
4545 if (primitive_info != (PrimitiveInfo *) NULL)
4547 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4548 if (primitive_info[i].text != (
char *) NULL)
4549 primitive_info[i].text=DestroyString(primitive_info[i].text);
4550 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4552 primitive=DestroyString(primitive);
4553 if (stops != (StopInfo *) NULL)
4554 stops=(StopInfo *) RelinquishMagickMemory(stops);
4555 for ( ; n >= 0; n--)
4556 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4557 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4558 if ((status == MagickFalse) && (exception->severity < ErrorException))
4559 ThrowBinaryException(DrawError,
"NonconformingDrawingPrimitiveDefinition",
4561 return(status != 0 ? MagickTrue : MagickFalse);
4564MagickExport MagickBooleanType DrawImage(Image *image,
const DrawInfo *draw_info,
4565 ExceptionInfo *exception)
4567 return(RenderMVGContent(image,draw_info,0,exception));
4601MagickExport MagickBooleanType DrawPatternPath(Image *image,
4602 const DrawInfo *draw_info,
const char *name,Image **pattern,
4603 ExceptionInfo *exception)
4606 property[MagickPathExtent];
4622 assert(image != (Image *) NULL);
4623 assert(image->signature == MagickCoreSignature);
4624 assert(draw_info != (
const DrawInfo *) NULL);
4625 assert(name != (
const char *) NULL);
4626 if (IsEventLogging() != MagickFalse)
4627 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4628 (void) FormatLocaleString(property,MagickPathExtent,
"%s",name);
4629 path=GetImageArtifact(image,property);
4630 if (path == (
const char *) NULL)
4631 return(MagickFalse);
4632 (void) FormatLocaleString(property,MagickPathExtent,
"%s-geometry",name);
4633 geometry=GetImageArtifact(image,property);
4634 if (geometry == (
const char *) NULL)
4635 return(MagickFalse);
4636 if ((*pattern) != (Image *) NULL)
4637 *pattern=DestroyImage(*pattern);
4638 image_info=AcquireImageInfo();
4639 image_info->size=AcquireString(geometry);
4640 *pattern=AcquireImage(image_info,exception);
4641 image_info=DestroyImageInfo(image_info);
4642 (void) QueryColorCompliance(
"#00000000",AllCompliance,
4643 &(*pattern)->background_color,exception);
4644 (void) SetImageBackgroundColor(*pattern,exception);
4645 if (draw_info->debug != MagickFalse)
4646 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4647 "begin pattern-path %s %s",name,geometry);
4648 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4649 if (clone_info->fill_pattern != (Image *) NULL)
4650 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4651 if (clone_info->stroke_pattern != (Image *) NULL)
4652 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4653 (void) FormatLocaleString(property,MagickPathExtent,
"%s-type",name);
4654 type=GetImageArtifact(image,property);
4655 if (type != (
const char *) NULL)
4656 clone_info->gradient.type=(GradientType) ParseCommandOption(
4657 MagickGradientOptions,MagickFalse,type);
4658 (void) CloneString(&clone_info->primitive,path);
4659 status=RenderMVGContent(*pattern,clone_info,0,exception);
4660 clone_info=DestroyDrawInfo(clone_info);
4661 if (draw_info->debug != MagickFalse)
4662 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end pattern-path");
4697static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4702 assert(polygon_info != (PolygonInfo **) NULL);
4703 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4704 if (polygon_info[i] != (PolygonInfo *) NULL)
4705 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4706 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4707 return(polygon_info);
4710static PolygonInfo **AcquirePolygonTLS(
const PrimitiveInfo *primitive_info,
4711 ExceptionInfo *exception)
4714 *magick_restrict path_info;
4722 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4723 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4724 sizeof(*polygon_info));
4725 if (polygon_info == (PolygonInfo **) NULL)
4727 (void) ThrowMagickException(exception,GetMagickModule(),
4728 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4729 return((PolygonInfo **) NULL);
4731 (void) memset(polygon_info,0,number_threads*
sizeof(*polygon_info));
4732 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4733 if (path_info == (PathInfo *) NULL)
4734 return(DestroyPolygonTLS(polygon_info));
4735 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4736 if (polygon_info[0] == (PolygonInfo *) NULL)
4738 (void) ThrowMagickException(exception,GetMagickModule(),
4739 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4740 return(DestroyPolygonTLS(polygon_info));
4742 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4743 return(polygon_info);
4746static MagickBooleanType ClonePolygonEdgesTLS(PolygonInfo **polygon_info,
4747 const size_t number_threads,ExceptionInfo *exception)
4752 for (i=1; i < (ssize_t) number_threads; i++)
4760 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4761 sizeof(*polygon_info[i]));
4762 if (polygon_info[i] == (PolygonInfo *) NULL)
4764 (void) ThrowMagickException(exception,GetMagickModule(),
4765 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4766 return(MagickFalse);
4768 polygon_info[i]->number_edges=0;
4769 edge_info=polygon_info[0]->edges;
4770 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4771 polygon_info[0]->number_edges,
sizeof(*edge_info));
4772 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4774 (void) ThrowMagickException(exception,GetMagickModule(),
4775 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4776 return(MagickFalse);
4778 (void) memcpy(polygon_info[i]->edges,edge_info,
4779 polygon_info[0]->number_edges*
sizeof(*edge_info));
4780 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4781 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4782 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4783 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4785 edge_info=polygon_info[0]->edges+j;
4786 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4787 edge_info->number_points,
sizeof(*edge_info));
4788 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4790 (void) ThrowMagickException(exception,GetMagickModule(),
4791 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4792 return(MagickFalse);
4794 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4795 edge_info->number_points*
sizeof(*edge_info->points));
4801static size_t DestroyEdge(PolygonInfo *polygon_info,
const ssize_t edge)
4803 assert(edge < (ssize_t) polygon_info->number_edges);
4804 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4805 polygon_info->edges[edge].points);
4806 polygon_info->number_edges--;
4807 if (edge < (ssize_t) polygon_info->number_edges)
4808 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4809 (polygon_info->number_edges-(
size_t) edge)*
sizeof(*polygon_info->edges));
4810 return(polygon_info->number_edges);
4813static double GetFillAlpha(PolygonInfo *polygon_info,
const double mid,
4814 const MagickBooleanType fill,
const FillRule fill_rule,
const ssize_t x,
4815 const ssize_t y,
double *stroke_alpha)
4842 p=polygon_info->edges;
4843 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4845 if ((
double) y <= (p->bounds.y1-mid-0.5))
4847 if ((
double) y > (p->bounds.y2+mid+0.5))
4850 (void) DestroyEdge(polygon_info,j--);
4853 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4854 ((
double) x > (p->bounds.x2+mid+0.5)))
4856 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4857 for ( ; i < (ssize_t) p->number_points; i++)
4859 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4861 if ((
double) y > (p->points[i].y+mid+0.5))
4863 if (p->scanline != (
double) y)
4865 p->scanline=(double) y;
4866 p->highwater=(size_t) i;
4872 delta.x=(q+1)->x-q->x;
4873 delta.y=(q+1)->y-q->y;
4874 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4880 delta.x=(double) x-q->x;
4881 delta.y=(double) y-q->y;
4882 distance=delta.x*delta.x+delta.y*delta.y;
4886 alpha=delta.x*delta.x+delta.y*delta.y;
4892 delta.x=(double) x-(q+1)->x;
4893 delta.y=(double) y-(q+1)->y;
4894 distance=delta.x*delta.x+delta.y*delta.y;
4901 alpha=MagickSafeReciprocal(alpha);
4902 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4903 distance=alpha*beta*beta;
4910 if (p->ghostline == MagickFalse)
4913 if ((*stroke_alpha < 1.0) &&
4914 (distance <= ((alpha+0.25)*(alpha+0.25))))
4917 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4922 if (fabs(distance-1.0) >= MagickEpsilon)
4923 beta=sqrt((
double) distance);
4925 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4926 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4930 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4932 if (distance <= 0.0)
4939 if (fabs(beta) < MagickEpsilon)
4942 if (fabs(distance-1.0) >= MagickEpsilon)
4943 beta=sqrt(distance);
4946 if (subpath_alpha < (alpha*alpha))
4947 subpath_alpha=alpha*alpha;
4953 if (fill == MagickFalse)
4955 if (subpath_alpha >= 1.0)
4961 p=polygon_info->edges;
4962 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4964 if ((
double) y <= p->bounds.y1)
4966 if (((
double) y > p->bounds.y2) || ((
double) x <= p->bounds.x1))
4968 if ((
double) x > p->bounds.x2)
4970 winding_number+=p->direction != 0 ? 1 : -1;
4973 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4974 for ( ; i < (ssize_t) (p->number_points-1); i++)
4975 if ((
double) y <= p->points[i].y)
4978 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4979 winding_number+=p->direction != 0 ? 1 : -1;
4981 if (fill_rule != NonZeroRule)
4983 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4987 if (MagickAbsoluteValue(winding_number) != 0)
4989 return(subpath_alpha);
4992static MagickBooleanType DrawPolygonPrimitive(Image *image,
4993 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
4994 ExceptionInfo *exception)
4996 typedef struct _ExtentInfo
5025 **magick_restrict polygon_info;
5037 assert(image != (Image *) NULL);
5038 assert(image->signature == MagickCoreSignature);
5039 assert(draw_info != (DrawInfo *) NULL);
5040 assert(draw_info->signature == MagickCoreSignature);
5041 assert(primitive_info != (PrimitiveInfo *) NULL);
5042 if (IsEventLogging() != MagickFalse)
5043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5044 if (primitive_info->coordinates <= 1)
5049 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5050 if (polygon_info == (PolygonInfo **) NULL)
5051 return(MagickFalse);
5052 if (draw_info->debug != MagickFalse)
5053 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5054 fill=(primitive_info->method == FillToBorderMethod) ||
5055 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5056 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5057 bounds=polygon_info[0]->edges[0].bounds;
5058 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5059 if (IsStringTrue(artifact) != MagickFalse)
5060 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5061 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5063 p=polygon_info[0]->edges+i;
5064 if (p->bounds.x1 < bounds.x1)
5065 bounds.x1=p->bounds.x1;
5066 if (p->bounds.y1 < bounds.y1)
5067 bounds.y1=p->bounds.y1;
5068 if (p->bounds.x2 > bounds.x2)
5069 bounds.x2=p->bounds.x2;
5070 if (p->bounds.y2 > bounds.y2)
5071 bounds.y2=p->bounds.y2;
5073 bounds.x1-=(mid+1.0);
5074 bounds.y1-=(mid+1.0);
5075 bounds.x2+=(mid+1.0);
5076 bounds.y2+=(mid+1.0);
5077 if ((bounds.x1 >= (
double) image->columns) ||
5078 (bounds.y1 >= (
double) image->rows) ||
5079 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5081 polygon_info=DestroyPolygonTLS(polygon_info);
5084 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5085 (
double) image->columns-1.0 : bounds.x1;
5086 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5087 (
double) image->rows-1.0 : bounds.y1;
5088 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5089 (
double) image->columns-1.0 : bounds.x2;
5090 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5091 (
double) image->rows-1.0 : bounds.y2;
5092 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5093 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5094 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5095 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5096 number_threads=(size_t) GetMagickNumberThreads(image,image,(
size_t)
5097 (poly_extent.y2-poly_extent.y1+1),1);
5098 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5099 if (status == MagickFalse)
5101 polygon_info=DestroyPolygonTLS(polygon_info);
5104 image_view=AcquireAuthenticCacheView(image,exception);
5105 if ((primitive_info->coordinates == 1) ||
5106 (polygon_info[0]->number_edges == 0))
5111#if defined(MAGICKCORE_OPENMP_SUPPORT)
5112 #pragma omp parallel for schedule(static) shared(status) \
5113 num_threads((int) number_threads)
5115 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5126 if (status == MagickFalse)
5129 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5131 if (q == (Quantum *) NULL)
5136 GetPixelInfo(image,&pixel);
5137 for ( ; x <= poly_extent.x2; x++)
5139 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5140 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5142 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5144 SetPixelViaPixelInfo(image,&pixel,q);
5146 q+=(ptrdiff_t) GetPixelChannels(image);
5148 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5151 image_view=DestroyCacheView(image_view);
5152 polygon_info=DestroyPolygonTLS(polygon_info);
5153 if (draw_info->debug != MagickFalse)
5154 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5155 " end draw-polygon");
5161#if defined(MAGICKCORE_OPENMP_SUPPORT)
5162 #pragma omp parallel for schedule(static) shared(status) \
5163 num_threads((int) number_threads)
5165 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5168 id = GetOpenMPThreadId();
5176 if (status == MagickFalse)
5178 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5179 (poly_extent.x2-poly_extent.x1+1),1,exception);
5180 if (q == (Quantum *) NULL)
5185 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5198 fill_alpha=GetFillAlpha(polygon_info[
id],mid,fill,draw_info->fill_rule,
5200 if (draw_info->stroke_antialias == MagickFalse)
5202 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5203 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5205 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5207 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5208 (
double) GetPixelAlpha(image,q),q);
5209 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5211 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5212 (
double) GetPixelAlpha(image,q),q);
5213 q+=(ptrdiff_t) GetPixelChannels(image);
5215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5218 image_view=DestroyCacheView(image_view);
5219 polygon_info=DestroyPolygonTLS(polygon_info);
5220 if (draw_info->debug != MagickFalse)
5221 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5254static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5280 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5281 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5282 switch (primitive_info->primitive)
5284 case AlphaPrimitive:
5286 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5287 "AlphaPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5288 methods[primitive_info->method]);
5291 case ColorPrimitive:
5293 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5294 "ColorPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5295 methods[primitive_info->method]);
5298 case ImagePrimitive:
5300 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5301 "ImagePrimitive %.20g,%.20g",(
double) x,(
double) y);
5304 case PointPrimitive:
5306 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5307 "PointPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5308 methods[primitive_info->method]);
5313 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5314 "TextPrimitive %.20g,%.20g",(
double) x,(
double) y);
5321 p=primitive_info[0].point;
5324 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5326 point=primitive_info[i].point;
5327 if (coordinates <= 0)
5329 coordinates=(ssize_t) primitive_info[i].coordinates;
5330 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5331 " begin open (%.20g)",(
double) coordinates);
5334 point=primitive_info[i].point;
5335 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5336 (fabs(q.y-point.y) >= MagickEpsilon))
5337 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5338 " %.20g: %.18g,%.18g",(
double) coordinates,point.x,point.y);
5340 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5341 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5344 if (coordinates > 0)
5346 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5347 (fabs(p.y-point.y) >= MagickEpsilon))
5348 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5349 (
double) coordinates);
5351 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5352 (
double) coordinates);
5356MagickExport MagickBooleanType DrawPrimitive(Image *image,
5357 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5358 ExceptionInfo *exception)
5373 if (draw_info->debug != MagickFalse)
5375 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5376 " begin draw-primitive");
5377 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5378 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5379 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5380 draw_info->affine.tx,draw_info->affine.ty);
5383 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5384 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5385 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5386 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5388 if (draw_info->compliance == SVGCompliance)
5390 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5391 draw_info->clipping_mask,exception);
5392 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5393 draw_info->composite_mask,exception);
5395 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5396 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5397 image_view=AcquireAuthenticCacheView(image,exception);
5398 switch (primitive_info->primitive)
5400 case AlphaPrimitive:
5402 if ((image->alpha_trait & BlendPixelTrait) == 0)
5403 status&=(MagickStatusType) SetImageAlphaChannel(image,
5404 OpaqueAlphaChannel,exception);
5405 switch (primitive_info->method)
5416 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5417 if (q == (Quantum *) NULL)
5419 GetFillColor(draw_info,x,y,&pixel,exception);
5420 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5421 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5431 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5432 x,y,&target,exception);
5433 GetPixelInfo(image,&pixel);
5434 for (y=0; y < (ssize_t) image->rows; y++)
5439 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5441 if (q == (Quantum *) NULL)
5443 for (x=0; x < (ssize_t) image->columns; x++)
5445 GetPixelInfoPixel(image,q,&pixel);
5446 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5448 q+=(ptrdiff_t) GetPixelChannels(image);
5451 GetFillColor(draw_info,x,y,&pixel,exception);
5452 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5453 q+=(ptrdiff_t) GetPixelChannels(image);
5455 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5457 if (status == MagickFalse)
5462 case FloodfillMethod:
5463 case FillToBorderMethod:
5471 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5472 TileVirtualPixelMethod,x,y,&target,exception);
5473 if (primitive_info->method == FillToBorderMethod)
5475 target.red=(double) draw_info->border_color.red;
5476 target.green=(double) draw_info->border_color.green;
5477 target.blue=(double) draw_info->border_color.blue;
5479 channel_mask=SetImageChannelMask(image,AlphaChannel);
5480 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5481 &target,x,y,primitive_info->method == FloodfillMethod ?
5482 MagickFalse : MagickTrue,exception);
5483 (void) SetImageChannelMask(image,channel_mask);
5491 for (y=0; y < (ssize_t) image->rows; y++)
5496 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5498 if (q == (Quantum *) NULL)
5500 for (x=0; x < (ssize_t) image->columns; x++)
5502 GetFillColor(draw_info,x,y,&pixel,exception);
5503 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5504 q+=(ptrdiff_t) GetPixelChannels(image);
5506 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5508 if (status == MagickFalse)
5516 case ColorPrimitive:
5518 switch (primitive_info->method)
5529 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5530 if (q == (Quantum *) NULL)
5532 GetPixelInfo(image,&pixel);
5533 GetFillColor(draw_info,x,y,&pixel,exception);
5534 SetPixelViaPixelInfo(image,&pixel,q);
5535 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5545 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5546 x,y,&target,exception);
5547 for (y=0; y < (ssize_t) image->rows; y++)
5552 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5554 if (q == (Quantum *) NULL)
5556 for (x=0; x < (ssize_t) image->columns; x++)
5558 GetPixelInfoPixel(image,q,&pixel);
5559 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5561 q+=(ptrdiff_t) GetPixelChannels(image);
5564 GetFillColor(draw_info,x,y,&pixel,exception);
5565 SetPixelViaPixelInfo(image,&pixel,q);
5566 q+=(ptrdiff_t) GetPixelChannels(image);
5568 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5570 if (status == MagickFalse)
5575 case FloodfillMethod:
5576 case FillToBorderMethod:
5581 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5582 TileVirtualPixelMethod,x,y,&target,exception);
5583 if (primitive_info->method == FillToBorderMethod)
5585 target.red=(double) draw_info->border_color.red;
5586 target.green=(double) draw_info->border_color.green;
5587 target.blue=(double) draw_info->border_color.blue;
5589 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5590 &target,x,y,primitive_info->method == FloodfillMethod ?
5591 MagickFalse : MagickTrue,exception);
5599 GetPixelInfo(image,&pixel);
5600 for (y=0; y < (ssize_t) image->rows; y++)
5605 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5607 if (q == (Quantum *) NULL)
5609 for (x=0; x < (ssize_t) image->columns; x++)
5611 GetFillColor(draw_info,x,y,&pixel,exception);
5612 SetPixelViaPixelInfo(image,&pixel,q);
5613 q+=(ptrdiff_t) GetPixelChannels(image);
5615 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5617 if (status == MagickFalse)
5625 case ImagePrimitive:
5631 composite_geometry[MagickPathExtent];
5647 if (primitive_info->text == (
char *) NULL)
5649 clone_info=AcquireImageInfo();
5650 composite_images=(Image *) NULL;
5651 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5652 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5655 if (*primitive_info->text !=
'\0')
5660 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5662 (void) SetImageInfo(clone_info,1,exception);
5663 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5665 if (clone_info->size != (
char *) NULL)
5666 clone_info->size=DestroyString(clone_info->size);
5667 if (clone_info->extract != (
char *) NULL)
5668 clone_info->extract=DestroyString(clone_info->extract);
5669 if ((LocaleCompare(clone_info->magick,
"ftp") != 0) &&
5670 (LocaleCompare(clone_info->magick,
"http") != 0) &&
5671 (LocaleCompare(clone_info->magick,
"https") != 0) &&
5672 (LocaleCompare(clone_info->magick,
"mvg") != 0) &&
5673 (LocaleCompare(clone_info->magick,
"vid") != 0))
5674 composite_images=ReadImage(clone_info,exception);
5676 (
void) ThrowMagickException(exception,GetMagickModule(),
5677 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5679 clone_info=DestroyImageInfo(clone_info);
5680 if (composite_images == (Image *) NULL)
5685 composite_image=RemoveFirstImageFromList(&composite_images);
5686 composite_images=DestroyImageList(composite_images);
5687 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5688 NULL,(
void *) NULL);
5689 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5690 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5691 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5692 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5697 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5698 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5699 composite_image->filter=image->filter;
5700 status&=(MagickStatusType) TransformImage(&composite_image,
5701 (
char *) NULL,composite_geometry,exception);
5703 if (composite_image->alpha_trait == UndefinedPixelTrait)
5704 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5705 OpaqueAlphaChannel,exception);
5706 if (draw_info->alpha != OpaqueAlpha)
5707 status&=(MagickStatusType) SetImageAlpha(composite_image,
5708 draw_info->alpha,exception);
5709 SetGeometry(image,&geometry);
5710 image->gravity=draw_info->gravity;
5713 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5714 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(
double)
5715 composite_image->rows,(
double) geometry.x,(
double) geometry.y);
5716 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5717 affine=draw_info->affine;
5718 affine.tx=(double) geometry.x;
5719 affine.ty=(double) geometry.y;
5720 composite_image->interpolate=image->interpolate;
5721 if ((draw_info->compose == OverCompositeOp) ||
5722 (draw_info->compose == SrcOverCompositeOp))
5723 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5726 status&=(MagickStatusType) CompositeImage(image,composite_image,
5727 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5728 composite_image=DestroyImage(composite_image);
5731 case PointPrimitive:
5739 if ((y < 0) || (y >= (ssize_t) image->rows))
5741 if ((x < 0) || (x >= (ssize_t) image->columns))
5743 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5744 if (q == (Quantum *) NULL)
5746 GetFillColor(draw_info,x,y,&fill_color,exception);
5747 CompositePixelOver(image,&fill_color,(
double) fill_color.alpha,q,(
double)
5748 GetPixelAlpha(image,q),q);
5749 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5756 geometry[MagickPathExtent];
5761 if (primitive_info->text == (
char *) NULL)
5763 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5764 (void) CloneString(&clone_info->text,primitive_info->text);
5765 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
5766 primitive_info->point.x,primitive_info->point.y);
5767 (void) CloneString(&clone_info->geometry,geometry);
5768 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5769 clone_info=DestroyDrawInfo(clone_info);
5781 if (IsEventLogging() != MagickFalse)
5782 LogPrimitiveInfo(primitive_info);
5783 scale=ExpandAffine(&draw_info->affine);
5784 if ((draw_info->dash_pattern != (
double *) NULL) &&
5785 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5786 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5787 (draw_info->stroke.alpha != (
double) TransparentAlpha))
5792 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5793 clone_info->stroke_width=0.0;
5794 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5795 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5796 primitive_info,exception);
5797 clone_info=DestroyDrawInfo(clone_info);
5798 if (status != MagickFalse)
5799 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5803 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5805 ((draw_info->stroke.alpha != (
double) TransparentAlpha) ||
5806 (draw_info->stroke_pattern != (Image *) NULL)))
5818 closed_path=primitive_info[0].closed_subpath;
5819 i=(ssize_t) primitive_info[0].coordinates;
5820 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5821 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5822 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5823 closed_path=MagickTrue;
5824 if ((((draw_info->linecap == RoundCap) ||
5825 (closed_path != MagickFalse)) &&
5826 (draw_info->linejoin == RoundJoin)) ||
5827 (primitive_info[i].primitive != UndefinedPrimitive))
5829 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5830 primitive_info,exception);
5833 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5834 clone_info->stroke_width=0.0;
5835 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5836 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5837 primitive_info,exception);
5838 clone_info=DestroyDrawInfo(clone_info);
5839 if (status != MagickFalse)
5840 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5841 primitive_info,exception);
5844 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5845 primitive_info,exception);
5849 image_view=DestroyCacheView(image_view);
5850 if (draw_info->compliance == SVGCompliance)
5852 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5853 (Image *) NULL,exception);
5854 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5855 (Image *) NULL,exception);
5857 if (draw_info->debug != MagickFalse)
5858 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5859 return(status != 0 ? MagickTrue : MagickFalse);
5892static MagickBooleanType DrawRoundLinecap(Image *image,
5893 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5894 ExceptionInfo *exception)
5902 if (primitive_info->coordinates < 1)
5903 return(MagickFalse);
5904 for (i=0; i < 4; i++)
5905 linecap[i]=(*primitive_info);
5906 linecap[0].coordinates=4;
5907 linecap[1].point.x+=2.0*MagickEpsilon;
5908 linecap[2].point.x+=2.0*MagickEpsilon;
5909 linecap[2].point.y+=2.0*MagickEpsilon;
5910 linecap[3].point.y+=2.0*MagickEpsilon;
5911 linecap[4].primitive=UndefinedPrimitive;
5912 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
5915static MagickBooleanType DrawStrokePolygon(Image *image,
5916 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5917 ExceptionInfo *exception)
5938 if (draw_info->debug != MagickFalse)
5939 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5940 " begin draw-stroke-polygon");
5941 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5942 clone_info->fill=draw_info->stroke;
5943 if (clone_info->fill_pattern != (Image *) NULL)
5944 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5945 if (clone_info->stroke_pattern != (Image *) NULL)
5946 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5947 MagickTrue,exception);
5948 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5949 clone_info->stroke_width=0.0;
5950 clone_info->fill_rule=NonZeroRule;
5952 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5954 if (p->coordinates == 1)
5956 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
5957 if (stroke_polygon == (PrimitiveInfo *) NULL)
5962 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5963 stroke_polygon,exception);
5964 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5967 q=p+p->coordinates-1;
5968 closed_path=p->closed_subpath;
5969 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5971 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
5973 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
5977 clone_info=DestroyDrawInfo(clone_info);
5978 if (draw_info->debug != MagickFalse)
5979 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5980 " end draw-stroke-polygon");
5981 return(status != 0 ? MagickTrue : MagickFalse);
6007MagickExport
void GetAffineMatrix(AffineMatrix *affine_matrix)
6009 if (IsEventLogging() != MagickFalse)
6010 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6011 assert(affine_matrix != (AffineMatrix *) NULL);
6012 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
6013 affine_matrix->sx=1.0;
6014 affine_matrix->sy=1.0;
6041MagickExport
void GetDrawInfo(
const ImageInfo *image_info,DrawInfo *draw_info)
6055 assert(draw_info != (DrawInfo *) NULL);
6056 if (IsEventLogging() != MagickFalse)
6057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6058 (void) memset(draw_info,0,
sizeof(*draw_info));
6059 draw_info->image_info=CloneImageInfo(image_info);
6060 GetAffineMatrix(&draw_info->affine);
6061 exception=AcquireExceptionInfo();
6062 (void) QueryColorCompliance(
"#000F",AllCompliance,&draw_info->fill,
6064 (void) QueryColorCompliance(
"#FFF0",AllCompliance,&draw_info->stroke,
6066 draw_info->stroke_antialias=draw_info->image_info->antialias;
6067 draw_info->stroke_width=1.0;
6068 draw_info->fill_rule=EvenOddRule;
6069 draw_info->alpha=OpaqueAlpha;
6070 draw_info->fill_alpha=OpaqueAlpha;
6071 draw_info->stroke_alpha=OpaqueAlpha;
6072 draw_info->linecap=ButtCap;
6073 draw_info->linejoin=MiterJoin;
6074 draw_info->miterlimit=10;
6075 draw_info->decorate=NoDecoration;
6076 draw_info->pointsize=12.0;
6077 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6078 draw_info->compose=OverCompositeOp;
6079 draw_info->render=MagickTrue;
6080 draw_info->clip_path=MagickFalse;
6081 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6082 MagickTrue : MagickFalse;
6083 if (draw_info->image_info->font != (
char *) NULL)
6084 draw_info->font=AcquireString(draw_info->image_info->font);
6085 if (draw_info->image_info->density != (
char *) NULL)
6086 draw_info->density=AcquireString(draw_info->image_info->density);
6087 draw_info->text_antialias=draw_info->image_info->antialias;
6088 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6089 draw_info->pointsize=draw_info->image_info->pointsize;
6090 draw_info->border_color=draw_info->image_info->border_color;
6091 if (draw_info->image_info->server_name != (
char *) NULL)
6092 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6093 option=GetImageOption(draw_info->image_info,
"direction");
6094 if (option != (
const char *) NULL)
6095 draw_info->direction=(DirectionType) ParseCommandOption(
6096 MagickDirectionOptions,MagickFalse,option);
6098 draw_info->direction=UndefinedDirection;
6099 option=GetImageOption(draw_info->image_info,
"encoding");
6100 if (option != (
const char *) NULL)
6101 (void) CloneString(&draw_info->encoding,option);
6102 option=GetImageOption(draw_info->image_info,
"family");
6103 if (option != (
const char *) NULL)
6104 (void) CloneString(&draw_info->family,option);
6105 option=GetImageOption(draw_info->image_info,
"fill");
6106 if (option != (
const char *) NULL)
6107 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6109 option=GetImageOption(draw_info->image_info,
"gravity");
6110 if (option != (
const char *) NULL)
6111 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6112 MagickFalse,option);
6113 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6114 if (option != (
const char *) NULL)
6115 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6116 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6117 if (option != (
const char *) NULL)
6118 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6119 option=GetImageOption(draw_info->image_info,
"kerning");
6120 if (option != (
const char *) NULL)
6121 draw_info->kerning=GetDrawValue(option,&next_token);
6122 option=GetImageOption(draw_info->image_info,
"stroke");
6123 if (option != (
const char *) NULL)
6124 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6126 option=GetImageOption(draw_info->image_info,
"strokewidth");
6127 if (option != (
const char *) NULL)
6128 draw_info->stroke_width=GetDrawValue(option,&next_token);
6129 option=GetImageOption(draw_info->image_info,
"style");
6130 if (option != (
const char *) NULL)
6131 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6132 MagickFalse,option);
6133 option=GetImageOption(draw_info->image_info,
"undercolor");
6134 if (option != (
const char *) NULL)
6135 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6137 option=GetImageOption(draw_info->image_info,
"weight");
6138 if (option != (
const char *) NULL)
6143 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6145 weight=(ssize_t) StringToUnsignedLong(option);
6146 draw_info->weight=(size_t) weight;
6148 option=GetImageOption(draw_info->image_info,
"word-break");
6149 if (option != (
const char *) NULL)
6150 draw_info->word_break=(WordBreakType) ParseCommandOption(
6151 MagickWordBreakOptions,MagickFalse,option);
6152 exception=DestroyExceptionInfo(exception);
6153 draw_info->signature=MagickCoreSignature;
6181static inline double Permutate(
const ssize_t n,
const ssize_t k)
6190 for (i=k+1; i <= n; i++)
6192 for (i=1; i <= (n-k); i++)
6213static MagickBooleanType TraceArc(MVGInfo *mvg_info,
const PointInfo start,
6214 const PointInfo end,
const PointInfo degrees)
6220 center.x=0.5*(end.x+start.x);
6221 center.y=0.5*(end.y+start.y);
6222 radius.x=fabs(center.x-start.x);
6223 radius.y=fabs(center.y-start.y);
6224 return(TraceEllipse(mvg_info,center,radius,degrees));
6227static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,
const PointInfo start,
6228 const PointInfo end,
const PointInfo arc,
const double angle,
6229 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6266 offset=mvg_info->offset;
6267 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6268 primitive_info->coordinates=0;
6269 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6270 (fabs(start.y-end.y) < MagickEpsilon))
6271 return(TracePoint(primitive_info,end));
6272 radii.x=fabs(arc.x);
6273 radii.y=fabs(arc.y);
6274 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6275 return(TraceLine(primitive_info,start,end));
6276 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6277 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6278 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6279 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6280 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6282 if (delta < MagickEpsilon)
6283 return(TraceLine(primitive_info,start,end));
6286 radii.x*=sqrt((
double) delta);
6287 radii.y*=sqrt((
double) delta);
6289 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6290 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6291 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6292 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6293 alpha=points[1].x-points[0].x;
6294 beta=points[1].y-points[0].y;
6295 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6296 return(TraceLine(primitive_info,start,end));
6297 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6302 factor=sqrt((
double) factor);
6303 if (sweep == large_arc)
6306 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6307 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6308 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6309 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6310 if ((theta < 0.0) && (sweep != MagickFalse))
6311 theta+=2.0*MagickPI;
6313 if ((theta > 0.0) && (sweep == MagickFalse))
6314 theta-=2.0*MagickPI;
6315 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((
double) (theta/(0.5*
6316 MagickPI+MagickEpsilon)))));
6319 for (i=0; i < (ssize_t) arc_segments; i++)
6321 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6322 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6323 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6324 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6325 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6326 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6327 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6328 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6329 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6330 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6331 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) (i+1)*
6332 theta/arc_segments),DegreesToRadians(360.0))));
6333 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) (i+1)*
6334 theta/arc_segments),DegreesToRadians(360.0))));
6335 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(
double)
6336 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6337 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(
double)
6338 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6339 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6340 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6341 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6343 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6345 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6347 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6349 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6351 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6353 if (i == (ssize_t) (arc_segments-1))
6355 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6358 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6359 mvg_info->offset+=(ssize_t) p->coordinates;
6360 p+=(ptrdiff_t) p->coordinates;
6363 return(MagickFalse);
6364 mvg_info->offset=offset;
6365 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6366 primitive_info->coordinates=(size_t) (p-primitive_info);
6367 primitive_info->closed_subpath=MagickFalse;
6368 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6370 p->primitive=primitive_info->primitive;
6376static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6377 const size_t number_coordinates)
6406 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6407 quantum=number_coordinates;
6408 for (i=0; i < (ssize_t) number_coordinates; i++)
6410 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6412 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6413 if (alpha > (
double) GetMaxMemoryRequest())
6415 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6416 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6417 return(MagickFalse);
6419 if (alpha > (
double) quantum)
6420 quantum=(size_t) alpha;
6421 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6422 if (alpha > (
double) quantum)
6423 quantum=(size_t) alpha;
6426 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6427 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6428 if (quantum > (
double) GetMaxMemoryRequest())
6430 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6431 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6432 return(MagickFalse);
6434 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6435 sizeof(*coefficients));
6436 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6438 if ((coefficients == (
double *) NULL) || (points == (PointInfo *) NULL))
6440 if (points != (PointInfo *) NULL)
6441 points=(PointInfo *) RelinquishMagickMemory(points);
6442 if (coefficients != (
double *) NULL)
6443 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6444 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6445 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6446 return(MagickFalse);
6448 control_points=quantum*number_coordinates;
6449 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6451 points=(PointInfo *) RelinquishMagickMemory(points);
6452 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6453 return(MagickFalse);
6455 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6459 end=primitive_info[number_coordinates-1].point;
6460 for (i=0; i < (ssize_t) number_coordinates; i++)
6461 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6463 for (i=0; i < (ssize_t) control_points; i++)
6468 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6469 for (j=0; j < (ssize_t) number_coordinates; j++)
6471 point.x+=alpha*coefficients[j]*p->point.x;
6472 point.y+=alpha*coefficients[j]*p->point.y;
6473 alpha*=weight/(1.0-weight);
6477 weight+=1.0/control_points;
6483 for (i=0; i < (ssize_t) control_points; i++)
6485 if (TracePoint(p,points[i]) == MagickFalse)
6487 points=(PointInfo *) RelinquishMagickMemory(points);
6488 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6489 return(MagickFalse);
6491 p+=(ptrdiff_t) p->coordinates;
6493 if (TracePoint(p,end) == MagickFalse)
6495 points=(PointInfo *) RelinquishMagickMemory(points);
6496 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6497 return(MagickFalse);
6499 p+=(ptrdiff_t) p->coordinates;
6500 primitive_info->coordinates=(size_t) (p-primitive_info);
6501 primitive_info->closed_subpath=MagickFalse;
6502 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6504 p->primitive=primitive_info->primitive;
6507 points=(PointInfo *) RelinquishMagickMemory(points);
6508 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6512static MagickBooleanType TraceCircle(MVGInfo *mvg_info,
const PointInfo start,
6513 const PointInfo end)
6524 alpha=end.x-start.x;
6526 radius=hypot((
double) alpha,(
double) beta);
6527 offset.x=(double) radius;
6528 offset.y=(double) radius;
6531 return(TraceEllipse(mvg_info,start,offset,degrees));
6534static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,
const PointInfo center,
6535 const PointInfo radii,
const PointInfo arc)
6560 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6561 primitive_info->coordinates=0;
6562 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6564 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6565 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6566 angle.x=DegreesToRadians(arc.x);
6570 angle.y=DegreesToRadians(y);
6571 coordinates=ceil((angle.y-angle.x)/step+1.0);
6572 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6573 return(MagickFalse);
6575 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6576 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6578 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6579 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6580 if (i++ >= (ssize_t) coordinates)
6582 if (TracePoint(p,point) == MagickFalse)
6583 return(MagickFalse);
6584 p+=(ptrdiff_t) p->coordinates;
6586 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6587 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6588 if (TracePoint(p,point) == MagickFalse)
6589 return(MagickFalse);
6590 p+=(ptrdiff_t) p->coordinates;
6591 primitive_info->coordinates=(size_t) (p-primitive_info);
6592 primitive_info->closed_subpath=MagickFalse;
6593 x=fabs(primitive_info[0].point.x-
6594 primitive_info[primitive_info->coordinates-1].point.x);
6595 y=fabs(primitive_info[0].point.y-
6596 primitive_info[primitive_info->coordinates-1].point.y);
6597 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6598 primitive_info->closed_subpath=MagickTrue;
6599 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6601 p->primitive=primitive_info->primitive;
6607static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6608 const PointInfo start,
const PointInfo end)
6610 if (TracePoint(primitive_info,start) == MagickFalse)
6611 return(MagickFalse);
6612 if (TracePoint(primitive_info+1,end) == MagickFalse)
6613 return(MagickFalse);
6614 (primitive_info+1)->primitive=primitive_info->primitive;
6615 primitive_info->coordinates=2;
6616 primitive_info->closed_subpath=MagickFalse;
6620static ssize_t TracePath(MVGInfo *mvg_info,
const char *path,
6621 ExceptionInfo *exception)
6625 token[MagickPathExtent] =
"";
6643 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6666 subpath_offset=mvg_info->offset;
6667 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6670 number_coordinates=0;
6672 primitive_type=primitive_info->primitive;
6674 for (p=path; *p !=
'\0'; )
6676 if (status == MagickFalse)
6678 while (isspace((
int) ((
unsigned char) *p)) != 0)
6682 last_attribute=attribute;
6683 attribute=(int) (*p++);
6693 large_arc = MagickFalse,
6694 sweep = MagickFalse;
6704 (void) GetNextToken(p,&p,MagickPathExtent,token);
6706 (void) GetNextToken(p,&p,MagickPathExtent,token);
6707 arc.x=GetDrawValue(token,&next_token);
6708 if (token == next_token)
6709 ThrowPointExpectedException(token,exception);
6710 (void) GetNextToken(p,&p,MagickPathExtent,token);
6712 (void) GetNextToken(p,&p,MagickPathExtent,token);
6713 arc.y=GetDrawValue(token,&next_token);
6714 if (token == next_token)
6715 ThrowPointExpectedException(token,exception);
6716 (void) GetNextToken(p,&p,MagickPathExtent,token);
6718 (void) GetNextToken(p,&p,MagickPathExtent,token);
6719 angle=GetDrawValue(token,&next_token);
6720 if (token == next_token)
6721 ThrowPointExpectedException(token,exception);
6722 (void) GetNextToken(p,&p,MagickPathExtent,token);
6724 (void) GetNextToken(p,&p,MagickPathExtent,token);
6725 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6726 (void) GetNextToken(p,&p,MagickPathExtent,token);
6728 (void) GetNextToken(p,&p,MagickPathExtent,token);
6729 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6731 (void) GetNextToken(p,&p,MagickPathExtent,token);
6732 (void) GetNextToken(p,&p,MagickPathExtent,token);
6734 (void) GetNextToken(p,&p,MagickPathExtent,token);
6735 x=GetDrawValue(token,&next_token);
6736 if (token == next_token)
6737 ThrowPointExpectedException(token,exception);
6738 (void) GetNextToken(p,&p,MagickPathExtent,token);
6740 (void) GetNextToken(p,&p,MagickPathExtent,token);
6741 y=GetDrawValue(token,&next_token);
6742 if (token == next_token)
6743 ThrowPointExpectedException(token,exception);
6744 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6745 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6746 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6748 q=(*mvg_info->primitive_info)+mvg_info->offset;
6749 mvg_info->offset+=(ssize_t) q->coordinates;
6750 q+=(ptrdiff_t) q->coordinates;
6752 while (isspace((
int) ((
unsigned char) *p)) != 0)
6756 }
while (IsPoint(p) != MagickFalse);
6768 for (i=1; i < 4; i++)
6770 (void) GetNextToken(p,&p,MagickPathExtent,token);
6772 (void) GetNextToken(p,&p,MagickPathExtent,token);
6773 x=GetDrawValue(token,&next_token);
6774 if (token == next_token)
6775 ThrowPointExpectedException(token,exception);
6776 (void) GetNextToken(p,&p,MagickPathExtent,token);
6778 (void) GetNextToken(p,&p,MagickPathExtent,token);
6779 y=GetDrawValue(token,&next_token);
6780 if (token == next_token)
6781 ThrowPointExpectedException(token,exception);
6782 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6783 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6786 for (i=0; i < 4; i++)
6787 (q+i)->point=points[i];
6788 if (TraceBezier(mvg_info,4) == MagickFalse)
6790 q=(*mvg_info->primitive_info)+mvg_info->offset;
6791 mvg_info->offset+=(ssize_t) q->coordinates;
6792 q+=(ptrdiff_t) q->coordinates;
6794 while (isspace((
int) ((
unsigned char) *p)) != 0)
6798 }
while (IsPoint(p) != MagickFalse);
6806 (void) GetNextToken(p,&p,MagickPathExtent,token);
6808 (void) GetNextToken(p,&p,MagickPathExtent,token);
6809 x=GetDrawValue(token,&next_token);
6810 if (token == next_token)
6811 ThrowPointExpectedException(token,exception);
6812 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6813 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6815 q=(*mvg_info->primitive_info)+mvg_info->offset;
6816 if (TracePoint(q,point) == MagickFalse)
6818 mvg_info->offset+=(ssize_t) q->coordinates;
6819 q+=(ptrdiff_t) q->coordinates;
6820 while (isspace((
int) ((
unsigned char) *p)) != 0)
6824 }
while (IsPoint(p) != MagickFalse);
6835 (void) GetNextToken(p,&p,MagickPathExtent,token);
6837 (void) GetNextToken(p,&p,MagickPathExtent,token);
6838 x=GetDrawValue(token,&next_token);
6839 if (token == next_token)
6840 ThrowPointExpectedException(token,exception);
6841 (void) GetNextToken(p,&p,MagickPathExtent,token);
6843 (void) GetNextToken(p,&p,MagickPathExtent,token);
6844 y=GetDrawValue(token,&next_token);
6845 if (token == next_token)
6846 ThrowPointExpectedException(token,exception);
6847 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
6848 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
6849 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6851 q=(*mvg_info->primitive_info)+mvg_info->offset;
6852 if (TracePoint(q,point) == MagickFalse)
6854 mvg_info->offset+=(ssize_t) q->coordinates;
6855 q+=(ptrdiff_t) q->coordinates;
6856 while (isspace((
int) ((
unsigned char) *p)) != 0)
6860 }
while (IsPoint(p) != MagickFalse);
6869 if (mvg_info->offset != subpath_offset)
6871 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6872 primitive_info->coordinates=(size_t) (q-primitive_info);
6873 number_coordinates+=primitive_info->coordinates;
6875 subpath_offset=mvg_info->offset;
6880 (void) GetNextToken(p,&p,MagickPathExtent,token);
6882 (void) GetNextToken(p,&p,MagickPathExtent,token);
6883 x=GetDrawValue(token,&next_token);
6884 if (token == next_token)
6885 ThrowPointExpectedException(token,exception);
6886 (void) GetNextToken(p,&p,MagickPathExtent,token);
6888 (void) GetNextToken(p,&p,MagickPathExtent,token);
6889 y=GetDrawValue(token,&next_token);
6890 if (token == next_token)
6891 ThrowPointExpectedException(token,exception);
6892 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
6893 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
6897 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6899 q=(*mvg_info->primitive_info)+mvg_info->offset;
6900 if (TracePoint(q,point) == MagickFalse)
6902 mvg_info->offset+=(ssize_t) q->coordinates;
6903 q+=(ptrdiff_t) q->coordinates;
6904 while (isspace((
int) ((
unsigned char) *p)) != 0)
6908 }
while (IsPoint(p) != MagickFalse);
6920 for (i=1; i < 3; i++)
6922 (void) GetNextToken(p,&p,MagickPathExtent,token);
6924 (void) GetNextToken(p,&p,MagickPathExtent,token);
6925 x=GetDrawValue(token,&next_token);
6926 if (token == next_token)
6927 ThrowPointExpectedException(token,exception);
6928 (void) GetNextToken(p,&p,MagickPathExtent,token);
6930 (void) GetNextToken(p,&p,MagickPathExtent,token);
6931 y=GetDrawValue(token,&next_token);
6932 if (token == next_token)
6933 ThrowPointExpectedException(token,exception);
6936 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
6937 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
6940 for (i=0; i < 3; i++)
6941 (q+i)->point=points[i];
6942 if (TraceBezier(mvg_info,3) == MagickFalse)
6944 q=(*mvg_info->primitive_info)+mvg_info->offset;
6945 mvg_info->offset+=(ssize_t) q->coordinates;
6946 q+=(ptrdiff_t) q->coordinates;
6948 while (isspace((
int) ((
unsigned char) *p)) != 0)
6952 }
while (IsPoint(p) != MagickFalse);
6963 points[0]=points[3];
6964 points[1].x=2.0*points[3].x-points[2].x;
6965 points[1].y=2.0*points[3].y-points[2].y;
6966 for (i=2; i < 4; i++)
6968 (void) GetNextToken(p,&p,MagickPathExtent,token);
6970 (void) GetNextToken(p,&p,MagickPathExtent,token);
6971 x=GetDrawValue(token,&next_token);
6972 if (token == next_token)
6973 ThrowPointExpectedException(token,exception);
6974 (void) GetNextToken(p,&p,MagickPathExtent,token);
6976 (void) GetNextToken(p,&p,MagickPathExtent,token);
6977 y=GetDrawValue(token,&next_token);
6978 if (token == next_token)
6979 ThrowPointExpectedException(token,exception);
6982 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
6983 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
6986 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
6991 for (i=0; i < 4; i++)
6992 (q+i)->point=points[i];
6993 if (TraceBezier(mvg_info,4) == MagickFalse)
6995 q=(*mvg_info->primitive_info)+mvg_info->offset;
6996 mvg_info->offset+=(ssize_t) q->coordinates;
6997 q+=(ptrdiff_t) q->coordinates;
6999 last_attribute=attribute;
7000 while (isspace((
int) ((
unsigned char) *p)) != 0)
7004 }
while (IsPoint(p) != MagickFalse);
7015 points[0]=points[2];
7016 points[1].x=2.0*points[2].x-points[1].x;
7017 points[1].y=2.0*points[2].y-points[1].y;
7018 for (i=2; i < 3; i++)
7020 (void) GetNextToken(p,&p,MagickPathExtent,token);
7022 (void) GetNextToken(p,&p,MagickPathExtent,token);
7023 x=GetDrawValue(token,&next_token);
7024 if (token == next_token)
7025 ThrowPointExpectedException(token,exception);
7026 (void) GetNextToken(p,&p,MagickPathExtent,token);
7028 (void) GetNextToken(p,&p,MagickPathExtent,token);
7029 y=GetDrawValue(token,&next_token);
7030 if (token == next_token)
7031 ThrowPointExpectedException(token,exception);
7032 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7033 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7036 if (status == MagickFalse)
7038 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7043 for (i=0; i < 3; i++)
7044 (q+i)->point=points[i];
7045 if (TraceBezier(mvg_info,3) == MagickFalse)
7047 q=(*mvg_info->primitive_info)+mvg_info->offset;
7048 mvg_info->offset+=(ssize_t) q->coordinates;
7049 q+=(ptrdiff_t) q->coordinates;
7051 last_attribute=attribute;
7052 while (isspace((
int) ((
unsigned char) *p)) != 0)
7056 }
while (IsPoint(p) != MagickFalse);
7067 (void) GetNextToken(p,&p,MagickPathExtent,token);
7069 (void) GetNextToken(p,&p,MagickPathExtent,token);
7070 y=GetDrawValue(token,&next_token);
7071 if (token == next_token)
7072 ThrowPointExpectedException(token,exception);
7073 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7074 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7076 q=(*mvg_info->primitive_info)+mvg_info->offset;
7077 if (TracePoint(q,point) == MagickFalse)
7079 mvg_info->offset+=(ssize_t) q->coordinates;
7080 q+=(ptrdiff_t) q->coordinates;
7081 while (isspace((
int) ((
unsigned char) *p)) != 0)
7085 }
while (IsPoint(p) != MagickFalse);
7095 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7097 q=(*mvg_info->primitive_info)+mvg_info->offset;
7098 if (TracePoint(q,point) == MagickFalse)
7100 mvg_info->offset+=(ssize_t) q->coordinates;
7101 q+=(ptrdiff_t) q->coordinates;
7102 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7103 primitive_info->coordinates=(size_t) (q-primitive_info);
7104 primitive_info->closed_subpath=MagickTrue;
7105 number_coordinates+=primitive_info->coordinates;
7107 subpath_offset=mvg_info->offset;
7113 ThrowPointExpectedException(token,exception);
7118 if (status == MagickFalse)
7120 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7121 primitive_info->coordinates=(size_t) (q-primitive_info);
7122 number_coordinates+=primitive_info->coordinates;
7123 for (i=0; i < (ssize_t) number_coordinates; i++)
7126 q->primitive=primitive_type;
7128 q->method=FillToBorderMethod;
7131 return((ssize_t) number_coordinates);
7134static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7135 const PointInfo start,
const PointInfo end)
7147 if (TracePoint(p,start) == MagickFalse)
7148 return(MagickFalse);
7149 p+=(ptrdiff_t) p->coordinates;
7152 if (TracePoint(p,point) == MagickFalse)
7153 return(MagickFalse);
7154 p+=(ptrdiff_t) p->coordinates;
7155 if (TracePoint(p,end) == MagickFalse)
7156 return(MagickFalse);
7157 p+=(ptrdiff_t) p->coordinates;
7160 if (TracePoint(p,point) == MagickFalse)
7161 return(MagickFalse);
7162 p+=(ptrdiff_t) p->coordinates;
7163 if (TracePoint(p,start) == MagickFalse)
7164 return(MagickFalse);
7165 p+=(ptrdiff_t) p->coordinates;
7166 primitive_info->coordinates=(size_t) (p-primitive_info);
7167 primitive_info->closed_subpath=MagickTrue;
7168 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7170 p->primitive=primitive_info->primitive;
7176static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7177 const PointInfo start,
const PointInfo end,PointInfo arc)
7196 offset=mvg_info->offset;
7197 segment.x=fabs(end.x-start.x);
7198 segment.y=fabs(end.y-start.y);
7199 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7201 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7204 if (arc.x > (0.5*segment.x))
7205 arc.x=0.5*segment.x;
7206 if (arc.y > (0.5*segment.y))
7207 arc.y=0.5*segment.y;
7208 point.x=start.x+segment.x-arc.x;
7209 point.y=start.y+arc.y;
7212 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7213 return(MagickFalse);
7214 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7215 mvg_info->offset+=(ssize_t) p->coordinates;
7216 point.x=start.x+segment.x-arc.x;
7217 point.y=start.y+segment.y-arc.y;
7220 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7221 return(MagickFalse);
7222 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7223 mvg_info->offset+=(ssize_t) p->coordinates;
7224 point.x=start.x+arc.x;
7225 point.y=start.y+segment.y-arc.y;
7228 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7229 return(MagickFalse);
7230 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7231 mvg_info->offset+=(ssize_t) p->coordinates;
7232 point.x=start.x+arc.x;
7233 point.y=start.y+arc.y;
7236 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7237 return(MagickFalse);
7238 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7239 mvg_info->offset+=(ssize_t) p->coordinates;
7240 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7241 return(MagickFalse);
7242 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7243 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7244 return(MagickFalse);
7245 p+=(ptrdiff_t) p->coordinates;
7246 mvg_info->offset=offset;
7247 primitive_info=(*mvg_info->primitive_info)+offset;
7248 primitive_info->coordinates=(size_t) (p-primitive_info);
7249 primitive_info->closed_subpath=MagickTrue;
7250 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7252 p->primitive=primitive_info->primitive;
7258static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7259 const size_t number_vertices,
const double offset)
7276 for (i=1; i < (ssize_t) number_vertices; i++)
7278 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7279 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7280 if ((fabs((
double) dx) >= MagickEpsilon) ||
7281 (fabs((
double) dy) >= MagickEpsilon))
7284 if (i == (ssize_t) number_vertices)
7285 i=(ssize_t) number_vertices-1L;
7286 distance=hypot((
double) dx,(
double) dy);
7287 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7288 dx*(distance+offset)/distance);
7289 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7290 dy*(distance+offset)/distance);
7291 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7293 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7294 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7295 if ((fabs((
double) dx) >= MagickEpsilon) ||
7296 (fabs((
double) dy) >= MagickEpsilon))
7299 distance=hypot((
double) dx,(
double) dy);
7300 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7301 dx*(distance+offset)/distance);
7302 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7303 dy*(distance+offset)/distance);
7307static PrimitiveInfo *TraceStrokePolygon(
const DrawInfo *draw_info,
7308 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7310#define MaxStrokePad (6*BezierQuantum+360)
7311#define CheckPathExtent(pad_p,pad_q) \
7313 if ((pad_p) > MaxBezierCoordinates) \
7314 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7316 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7318 if (~extent_p < (pad_p)) \
7319 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7322 extent_p+=(pad_p); \
7323 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7324 MaxStrokePad,sizeof(*stroke_p)); \
7327 if ((pad_q) > MaxBezierCoordinates) \
7328 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7330 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7332 if (~extent_q < (pad_q)) \
7333 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7336 extent_q+=(pad_q); \
7337 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7338 MaxStrokePad,sizeof(*stroke_q)); \
7341 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7343 if (stroke_p != (PointInfo *) NULL) \
7344 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7345 if (stroke_q != (PointInfo *) NULL) \
7346 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7347 polygon_primitive=(PrimitiveInfo *) \
7348 RelinquishMagickMemory(polygon_primitive); \
7349 (void) ThrowMagickException(exception,GetMagickModule(), \
7350 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7351 return((PrimitiveInfo *) NULL); \
7355 typedef struct _StrokeSegment
7401 inverse_slope = {0.0, 0.0},
7408 number_vertices=primitive_info->coordinates;
7409 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7410 number_vertices+2UL,
sizeof(*polygon_primitive));
7411 if (polygon_primitive == (PrimitiveInfo *) NULL)
7413 (void) ThrowMagickException(exception,GetMagickModule(),
7414 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7415 return((PrimitiveInfo *) NULL);
7417 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7418 sizeof(*polygon_primitive));
7419 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7420 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7421 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7422 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7423 if ((draw_info->linejoin == RoundJoin) ||
7424 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7426 polygon_primitive[number_vertices]=primitive_info[1];
7429 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7433 closed_path=primitive_info[0].closed_subpath;
7436 for (n=1; n < (ssize_t) number_vertices; n++)
7438 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7439 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7440 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7443 if (n == (ssize_t) number_vertices)
7445 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7450 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7451 sizeof(*stroke_polygon));
7452 stroke_polygon[0]=polygon_primitive[0];
7453 stroke_polygon[0].coordinates=0;
7454 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7456 return(stroke_polygon);
7458 n=(ssize_t) number_vertices-1L;
7460 extent_p=2*number_vertices;
7461 extent_q=2*number_vertices;
7462 stroke_p=(PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7464 stroke_q=(PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7466 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7468 if (stroke_p != (PointInfo *) NULL)
7469 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7470 if (stroke_q != (PointInfo *) NULL)
7471 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7472 polygon_primitive=(PrimitiveInfo *)
7473 RelinquishMagickMemory(polygon_primitive);
7474 (void) ThrowMagickException(exception,GetMagickModule(),
7475 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7476 return((PrimitiveInfo *) NULL);
7479 inverse_slope.p=0.0;
7480 if (fabs(dx.p) < MagickEpsilon)
7483 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7485 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7488 if (fabs(dy.p) < MagickEpsilon)
7491 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7493 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7498 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7500 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7501 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7502 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7503 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7504 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7505 offset.y=(double) (offset.x*inverse_slope.p);
7506 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7508 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7509 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7510 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7511 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7512 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7513 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7514 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7515 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7519 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7520 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7521 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7522 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7523 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7524 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7525 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7526 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7533 stroke_q[p++]=box_q[0];
7534 stroke_p[q++]=box_p[0];
7535 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7540 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7541 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7542 dot_product=dx.q*dx.q+dy.q*dy.q;
7543 if (dot_product < 0.25)
7546 inverse_slope.q=0.0;
7547 if (fabs(dx.q) < MagickEpsilon)
7550 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7552 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7555 if (fabs(dy.q) < MagickEpsilon)
7558 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7560 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7565 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7567 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7568 offset.y=(double) (offset.x*inverse_slope.q);
7569 dot_product=dy.q*offset.x-dx.q*offset.y;
7570 if (dot_product > 0.0)
7572 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7573 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7574 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7575 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7576 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7577 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7578 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7579 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7583 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7584 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7585 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7586 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7587 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7588 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7589 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7590 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7592 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7599 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7600 box_p[3].y)/(slope.p-slope.q));
7601 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7602 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7603 box_q[3].y)/(slope.p-slope.q));
7604 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7606 DisableMSCWarning(4127)
7607 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7609 dot_product=dx.q*dy.p-dx.p*dy.q;
7610 if (dot_product <= 0.0)
7611 switch (draw_info->linejoin)
7615 stroke_q[q++]=box_q[1];
7616 stroke_q[q++]=box_q[2];
7617 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7618 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7619 if (dot_product <= miterlimit)
7620 stroke_p[p++]=box_p[4];
7623 stroke_p[p++]=box_p[1];
7624 stroke_p[p++]=box_p[2];
7630 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7631 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7632 if (dot_product <= miterlimit)
7634 stroke_q[q++]=box_q[4];
7635 stroke_p[p++]=box_p[4];
7639 stroke_q[q++]=box_q[1];
7640 stroke_q[q++]=box_q[2];
7641 stroke_p[p++]=box_p[1];
7642 stroke_p[p++]=box_p[2];
7648 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7649 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7650 if (dot_product <= miterlimit)
7651 stroke_p[p++]=box_p[4];
7654 stroke_p[p++]=box_p[1];
7655 stroke_p[p++]=box_p[2];
7657 center=polygon_primitive[n].point;
7658 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7659 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7660 if (theta.q < theta.p)
7661 theta.q+=2.0*MagickPI;
7662 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.q-
7663 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7664 DisableMSCWarning(4127)
7665 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7667 stroke_q[q].x=box_q[1].x;
7668 stroke_q[q].y=box_q[1].y;
7670 for (j=1; j < (ssize_t) arc_segments; j++)
7672 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7673 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7674 (theta.p+delta_theta),DegreesToRadians(360.0))));
7675 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7676 (theta.p+delta_theta),DegreesToRadians(360.0))));
7679 stroke_q[q++]=box_q[2];
7686 switch (draw_info->linejoin)
7690 stroke_p[p++]=box_p[1];
7691 stroke_p[p++]=box_p[2];
7692 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7693 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7694 if (dot_product <= miterlimit)
7695 stroke_q[q++]=box_q[4];
7698 stroke_q[q++]=box_q[1];
7699 stroke_q[q++]=box_q[2];
7705 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7706 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7707 if (dot_product <= miterlimit)
7709 stroke_q[q++]=box_q[4];
7710 stroke_p[p++]=box_p[4];
7714 stroke_q[q++]=box_q[1];
7715 stroke_q[q++]=box_q[2];
7716 stroke_p[p++]=box_p[1];
7717 stroke_p[p++]=box_p[2];
7723 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7724 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7725 if (dot_product <= miterlimit)
7726 stroke_q[q++]=box_q[4];
7729 stroke_q[q++]=box_q[1];
7730 stroke_q[q++]=box_q[2];
7732 center=polygon_primitive[n].point;
7733 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7734 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7735 if (theta.p < theta.q)
7736 theta.p+=2.0*MagickPI;
7737 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.p-
7738 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7739 DisableMSCWarning(4127)
7740 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7742 stroke_p[p++]=box_p[1];
7743 for (j=1; j < (ssize_t) arc_segments; j++)
7745 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7746 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7747 (theta.p+delta_theta),DegreesToRadians(360.0))));
7748 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7749 (theta.p+delta_theta),DegreesToRadians(360.0))));
7752 stroke_p[p++]=box_p[2];
7759 inverse_slope.p=inverse_slope.q;
7768 stroke_p[p++]=box_p[1];
7769 stroke_q[q++]=box_q[1];
7773 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7774 (p+q+2L),(
size_t) (closed_path+2L)*
sizeof(*stroke_polygon));
7775 if (stroke_polygon == (PrimitiveInfo *) NULL)
7777 (void) ThrowMagickException(exception,GetMagickModule(),
7778 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7779 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7780 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7781 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7783 return(stroke_polygon);
7785 for (i=0; i < (ssize_t) p; i++)
7787 stroke_polygon[i]=polygon_primitive[0];
7788 stroke_polygon[i].point=stroke_p[i];
7790 if (closed_path != MagickFalse)
7792 stroke_polygon[i]=polygon_primitive[0];
7793 stroke_polygon[i].point=stroke_polygon[0].point;
7796 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7798 stroke_polygon[i]=polygon_primitive[0];
7799 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7801 if (closed_path != MagickFalse)
7803 stroke_polygon[i]=polygon_primitive[0];
7804 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7807 stroke_polygon[i]=polygon_primitive[0];
7808 stroke_polygon[i].point=stroke_polygon[0].point;
7810 stroke_polygon[i].primitive=UndefinedPrimitive;
7811 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7812 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7813 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7814 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7815 return(stroke_polygon);