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; \
106static inline float MagickSafeReciprocal(
const float x) {
107 return PerceptibleReciprocal(x);
196 *DrawClippingMask(
Image *,
const DrawInfo *,
const char *,
const char *,
199static MagickBooleanType
205 const double,
const MagickBooleanType,
const MagickBooleanType),
206 TraceBezier(
MVGInfo *,
const size_t),
212 TraceSquareLinecap(
PrimitiveInfo *,
const size_t,
const double);
238MagickExport
DrawInfo *AcquireDrawInfo(
void)
243 draw_info=(
DrawInfo *) AcquireCriticalMemory(
sizeof(*draw_info));
244 GetDrawInfo((
ImageInfo *) NULL,draw_info);
284 clone_info=(
DrawInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
285 GetDrawInfo(image_info,clone_info);
288 exception=AcquireExceptionInfo();
289 if (draw_info->id != (
char *) NULL)
290 (void) CloneString(&clone_info->id,draw_info->id);
291 if (draw_info->primitive != (
char *) NULL)
292 (
void) CloneString(&clone_info->primitive,draw_info->primitive);
293 if (draw_info->geometry != (
char *) NULL)
294 (void) CloneString(&clone_info->geometry,draw_info->geometry);
295 clone_info->compliance=draw_info->compliance;
296 clone_info->viewbox=draw_info->viewbox;
297 clone_info->affine=draw_info->affine;
298 clone_info->gravity=draw_info->gravity;
299 clone_info->fill=draw_info->fill;
300 clone_info->stroke=draw_info->stroke;
301 clone_info->stroke_width=draw_info->stroke_width;
302 if (draw_info->fill_pattern != (
Image *) NULL)
303 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
305 if (draw_info->stroke_pattern != (
Image *) NULL)
306 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
307 MagickTrue,exception);
308 clone_info->stroke_antialias=draw_info->stroke_antialias;
309 clone_info->text_antialias=draw_info->text_antialias;
310 clone_info->fill_rule=draw_info->fill_rule;
311 clone_info->linecap=draw_info->linecap;
312 clone_info->linejoin=draw_info->linejoin;
313 clone_info->miterlimit=draw_info->miterlimit;
314 clone_info->dash_offset=draw_info->dash_offset;
315 clone_info->decorate=draw_info->decorate;
316 clone_info->compose=draw_info->compose;
317 if (draw_info->text != (
char *) NULL)
318 (
void) CloneString(&clone_info->text,draw_info->text);
319 if (draw_info->font != (
char *) NULL)
320 (void) CloneString(&clone_info->font,draw_info->font);
321 if (draw_info->metrics != (
char *) NULL)
322 (
void) CloneString(&clone_info->metrics,draw_info->metrics);
323 if (draw_info->family != (
char *) NULL)
324 (void) CloneString(&clone_info->family,draw_info->family);
325 clone_info->style=draw_info->style;
326 clone_info->stretch=draw_info->stretch;
327 clone_info->weight=draw_info->weight;
328 if (draw_info->encoding != (
char *) NULL)
329 (
void) CloneString(&clone_info->encoding,draw_info->encoding);
330 clone_info->pointsize=draw_info->pointsize;
331 clone_info->kerning=draw_info->kerning;
332 clone_info->interline_spacing=draw_info->interline_spacing;
333 clone_info->interword_spacing=draw_info->interword_spacing;
334 clone_info->direction=draw_info->direction;
335 clone_info->word_break=draw_info->word_break;
336 if (draw_info->density != (
char *) NULL)
337 (void) CloneString(&clone_info->density,draw_info->density);
338 clone_info->align=draw_info->align;
339 clone_info->undercolor=draw_info->undercolor;
340 clone_info->border_color=draw_info->border_color;
341 if (draw_info->server_name != (
char *) NULL)
342 (
void) CloneString(&clone_info->server_name,draw_info->server_name);
343 if (draw_info->dash_pattern != (
double *) NULL)
348 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
349 clone_info->dash_pattern=(
double *) AcquireQuantumMemory((
size_t) (x+1),
350 sizeof(*clone_info->dash_pattern));
351 if (clone_info->dash_pattern == (
double *) NULL)
352 ThrowFatalException(ResourceLimitFatalError,
353 "UnableToAllocateDashPattern");
354 (void) memset(clone_info->dash_pattern,0,(
size_t) (x+1)*
355 sizeof(*clone_info->dash_pattern));
356 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(
size_t)
357 x*
sizeof(*clone_info->dash_pattern));
359 clone_info->gradient=draw_info->gradient;
360 if (draw_info->gradient.stops != (
StopInfo *) NULL)
365 number_stops=clone_info->gradient.number_stops;
366 clone_info->gradient.stops=(
StopInfo *) AcquireQuantumMemory((
size_t)
367 number_stops,
sizeof(*clone_info->gradient.stops));
368 if (clone_info->gradient.stops == (
StopInfo *) NULL)
369 ThrowFatalException(ResourceLimitFatalError,
370 "UnableToAllocateDashPattern");
371 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
372 (
size_t) number_stops*
sizeof(*clone_info->gradient.stops));
374 clone_info->bounds=draw_info->bounds;
375 clone_info->fill_alpha=draw_info->fill_alpha;
376 clone_info->stroke_alpha=draw_info->stroke_alpha;
377 clone_info->element_reference=draw_info->element_reference;
378 clone_info->clip_path=draw_info->clip_path;
379 clone_info->clip_units=draw_info->clip_units;
380 if (draw_info->clip_mask != (
char *) NULL)
381 (
void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
382 if (draw_info->clipping_mask != (
Image *) NULL)
383 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
384 MagickTrue,exception);
385 if (draw_info->composite_mask != (
Image *) NULL)
386 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
387 MagickTrue,exception);
388 clone_info->render=draw_info->render;
389 clone_info->debug=draw_info->debug;
390 exception=DestroyExceptionInfo(exception);
430 if (polygon_info->edges != (
EdgeInfo *) NULL)
432 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
433 if (polygon_info->edges[i].points != (
PointInfo *) NULL)
434 polygon_info->edges[i].points=(
PointInfo *)
435 RelinquishMagickMemory(polygon_info->edges[i].points);
436 polygon_info->edges=(
EdgeInfo *) RelinquishMagickMemory(
437 polygon_info->edges);
439 return((
PolygonInfo *) RelinquishMagickMemory(polygon_info));
441#if defined(__cplusplus) || defined(c_plusplus)
445static int DrawCompareEdges(
const void *p_edge,
const void *q_edge)
447#define DrawCompareEdge(p,q) \
449 if (((p)-(q)) < 0.0) \
451 if (((p)-(q)) > 0.0) \
462 p=((
const EdgeInfo *) p_edge)->points;
463 q=((
const EdgeInfo *) q_edge)->points;
464 DrawCompareEdge(p[0].y,q[0].y);
465 DrawCompareEdge(p[0].x,q[0].x);
466 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
468 DrawCompareEdge(p[1].y,q[1].y);
469 DrawCompareEdge(p[1].x,q[1].x);
473#if defined(__cplusplus) || defined(c_plusplus)
477static void LogPolygonInfo(
const PolygonInfo *polygon_info)
486 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin active-edge");
487 p=polygon_info->edges;
488 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
490 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" edge %.20g:",
492 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" direction: %s",
493 p->direction != MagickFalse ?
"down" :
"up");
494 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" ghostline: %s",
495 p->ghostline != MagickFalse ?
"transparent" :
"opaque");
496 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
497 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
498 p->bounds.x2,p->bounds.y2);
499 for (j=0; j < (ssize_t) p->number_points; j++)
500 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %g,%g",
501 p->points[j].x,p->points[j].y);
504 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end active-edge");
507static void ReversePoints(
PointInfo *points,
const size_t number_points)
515 for (i=0; i < (number_points >> 1); i++)
518 points[i]=points[number_points-(i+1)];
519 points[number_points-(i+1)]=point;
555 polygon_info=(
PolygonInfo *) AcquireMagickMemory(
sizeof(*polygon_info));
558 (void) ThrowMagickException(exception,GetMagickModule(),
559 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
563 polygon_info->edges=(
EdgeInfo *) AcquireQuantumMemory(number_edges,
564 sizeof(*polygon_info->edges));
565 if (polygon_info->edges == (
EdgeInfo *) NULL)
567 (void) ThrowMagickException(exception,GetMagickModule(),
568 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
569 return(DestroyPolygonInfo(polygon_info));
571 (void) memset(polygon_info->edges,0,number_edges*
572 sizeof(*polygon_info->edges));
575 ghostline=MagickFalse;
579 (void) memset(&point,0,
sizeof(point));
580 (void) memset(&bounds,0,
sizeof(bounds));
581 polygon_info->edges[edge].number_points=(size_t) n;
582 polygon_info->edges[edge].scanline=0.0;
583 polygon_info->edges[edge].highwater=0;
584 polygon_info->edges[edge].ghostline=ghostline;
585 polygon_info->edges[edge].direction=(ssize_t) direction;
586 polygon_info->edges[edge].points=points;
587 polygon_info->edges[edge].bounds=bounds;
588 polygon_info->number_edges=0;
589 for (i=0; path_info[i].code != EndCode; i++)
591 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
592 (path_info[i].code == GhostlineCode))
597 if ((points != (
PointInfo *) NULL) && (n >= 2))
599 if (edge == number_edges)
602 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(
603 polygon_info->edges,(
size_t) number_edges,
604 sizeof(*polygon_info->edges));
605 if (polygon_info->edges == (
EdgeInfo *) NULL)
607 (void) ThrowMagickException(exception,GetMagickModule(),
608 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
609 points=(
PointInfo *) RelinquishMagickMemory(points);
610 return(DestroyPolygonInfo(polygon_info));
613 polygon_info->edges[edge].number_points=(size_t) n;
614 polygon_info->edges[edge].scanline=(-1.0);
615 polygon_info->edges[edge].highwater=0;
616 polygon_info->edges[edge].ghostline=ghostline;
617 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
619 ReversePoints(points,(
size_t) n);
620 polygon_info->edges[edge].points=points;
621 polygon_info->edges[edge].bounds=bounds;
622 polygon_info->edges[edge].bounds.y1=points[0].y;
623 polygon_info->edges[edge].bounds.y2=points[n-1].y;
625 ghostline=MagickFalse;
627 polygon_info->number_edges=edge;
632 points=(
PointInfo *) AcquireQuantumMemory((
size_t) number_points,
636 (void) ThrowMagickException(exception,GetMagickModule(),
637 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
638 return(DestroyPolygonInfo(polygon_info));
641 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
642 point=path_info[i].point;
653 next_direction=((path_info[i].point.y > point.y) ||
654 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
655 (path_info[i].point.x > point.x))) ? 1 : -1;
656 if ((points != (
PointInfo *) NULL) && (direction != 0) &&
657 (direction != next_direction))
663 if (edge == number_edges)
666 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(
667 polygon_info->edges,(
size_t) number_edges,
668 sizeof(*polygon_info->edges));
669 if (polygon_info->edges == (
EdgeInfo *) NULL)
671 (void) ThrowMagickException(exception,GetMagickModule(),
672 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
673 points=(
PointInfo *) RelinquishMagickMemory(points);
674 return(DestroyPolygonInfo(polygon_info));
677 polygon_info->edges[edge].number_points=(size_t) n;
678 polygon_info->edges[edge].scanline=(-1.0);
679 polygon_info->edges[edge].highwater=0;
680 polygon_info->edges[edge].ghostline=ghostline;
681 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
683 ReversePoints(points,(
size_t) n);
684 polygon_info->edges[edge].points=points;
685 polygon_info->edges[edge].bounds=bounds;
686 polygon_info->edges[edge].bounds.y1=points[0].y;
687 polygon_info->edges[edge].bounds.y2=points[n-1].y;
688 polygon_info->number_edges=edge+1;
691 points=(
PointInfo *) AcquireQuantumMemory((
size_t) number_points,
695 (void) ThrowMagickException(exception,GetMagickModule(),
696 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
697 return(DestroyPolygonInfo(polygon_info));
700 ghostline=MagickFalse;
706 direction=next_direction;
709 if (n == (ssize_t) number_points)
712 points=(
PointInfo *) ResizeQuantumMemory(points,(
size_t) number_points,
716 (void) ThrowMagickException(exception,GetMagickModule(),
717 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
718 return(DestroyPolygonInfo(polygon_info));
721 point=path_info[i].point;
723 if (point.x < bounds.x1)
725 if (point.x > bounds.x2)
732 points=(
PointInfo *) RelinquishMagickMemory(points);
735 if (edge == number_edges)
738 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(
739 polygon_info->edges,(
size_t) number_edges,
740 sizeof(*polygon_info->edges));
741 if (polygon_info->edges == (
EdgeInfo *) NULL)
743 (void) ThrowMagickException(exception,GetMagickModule(),
744 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
745 return(DestroyPolygonInfo(polygon_info));
748 polygon_info->edges[edge].number_points=(size_t) n;
749 polygon_info->edges[edge].scanline=(-1.0);
750 polygon_info->edges[edge].highwater=0;
751 polygon_info->edges[edge].ghostline=ghostline;
752 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
754 ReversePoints(points,(
size_t) n);
755 polygon_info->edges[edge].points=points;
756 polygon_info->edges[edge].bounds=bounds;
757 polygon_info->edges[edge].bounds.y1=points[0].y;
758 polygon_info->edges[edge].bounds.y2=points[n-1].y;
760 ghostline=MagickFalse;
762 polygon_info->number_edges=edge;
765 polygon_info->number_edges=edge;
766 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
767 polygon_info->number_edges,
sizeof(*polygon_info->edges));
768 if (polygon_info->edges == (
EdgeInfo *) NULL)
770 (void) ThrowMagickException(exception,GetMagickModule(),
771 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
772 return(DestroyPolygonInfo(polygon_info));
774 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
779 edge_info=polygon_info->edges+i;
780 edge_info->points=(
PointInfo *) ResizeQuantumMemory(edge_info->points,
781 edge_info->number_points,
sizeof(*edge_info->points));
782 if (edge_info->points == (
PointInfo *) NULL)
784 (void) ThrowMagickException(exception,GetMagickModule(),
785 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
786 return(DestroyPolygonInfo(polygon_info));
789 qsort(polygon_info->edges,(
size_t) polygon_info->number_edges,
790 sizeof(*polygon_info->edges),DrawCompareEdges);
791 if ((GetLogEventMask() & DrawEvent) != 0)
792 LogPolygonInfo(polygon_info);
793 return(polygon_info);
826static void LogPathInfo(
const PathInfo *path_info)
831 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin vector-path");
832 for (p=path_info; p->code != EndCode; p++)
833 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
834 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
835 "moveto ghostline" : p->code == OpenCode ?
"moveto open" :
836 p->code == MoveToCode ?
"moveto" : p->code == LineToCode ?
"lineto" :
838 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end vector-path");
868 switch (primitive_info->primitive)
879 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
880 path_info=(
PathInfo *) AcquireQuantumMemory((
size_t) (3UL*i+1UL),
884 (void) ThrowMagickException(exception,GetMagickModule(),
885 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
889 closed_subpath=MagickFalse;
896 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
899 if (coordinates <= 0)
904 coordinates=primitive_info[i].coordinates;
905 p=primitive_info[i].point;
908 closed_subpath=primitive_info[i].closed_subpath;
911 if ((code == MoveToCode) || (coordinates <= 0) ||
912 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
913 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
918 path_info[n].code=code;
919 path_info[n].point=primitive_info[i].point;
920 q=primitive_info[i].point;
925 if (closed_subpath != MagickFalse)
927 closed_subpath=MagickFalse;
933 path_info[start].code=OpenCode;
934 path_info[n].code=GhostlineCode;
935 path_info[n].point=primitive_info[i].point;
937 path_info[n].code=LineToCode;
938 path_info[n].point=p;
941 path_info[n].code=EndCode;
942 path_info[n].point.x=0.0;
943 path_info[n].point.y=0.0;
944 if (IsEventLogging() != MagickFalse)
945 LogPathInfo(path_info);
946 path_info=(
PathInfo *) ResizeQuantumMemory(path_info,(
size_t) (n+1),
975 assert(draw_info != (
DrawInfo *) NULL);
976 assert(draw_info->signature == MagickCoreSignature);
977 if (IsEventLogging() != MagickFalse)
978 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
979 if (draw_info->id != (
char *) NULL)
980 draw_info->id=DestroyString(draw_info->id);
981 if (draw_info->primitive != (
char *) NULL)
982 draw_info->primitive=DestroyString(draw_info->primitive);
983 if (draw_info->text != (
char *) NULL)
984 draw_info->text=DestroyString(draw_info->text);
985 if (draw_info->geometry != (
char *) NULL)
986 draw_info->geometry=DestroyString(draw_info->geometry);
987 if (draw_info->fill_pattern != (
Image *) NULL)
988 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
989 if (draw_info->stroke_pattern != (
Image *) NULL)
990 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
991 if (draw_info->font != (
char *) NULL)
992 draw_info->font=DestroyString(draw_info->font);
993 if (draw_info->metrics != (
char *) NULL)
994 draw_info->metrics=DestroyString(draw_info->metrics);
995 if (draw_info->family != (
char *) NULL)
996 draw_info->family=DestroyString(draw_info->family);
997 if (draw_info->encoding != (
char *) NULL)
998 draw_info->encoding=DestroyString(draw_info->encoding);
999 if (draw_info->density != (
char *) NULL)
1000 draw_info->density=DestroyString(draw_info->density);
1001 if (draw_info->server_name != (
char *) NULL)
1002 draw_info->server_name=(
char *)
1003 RelinquishMagickMemory(draw_info->server_name);
1004 if (draw_info->dash_pattern != (
double *) NULL)
1005 draw_info->dash_pattern=(
double *) RelinquishMagickMemory(
1006 draw_info->dash_pattern);
1007 if (draw_info->gradient.stops != (
StopInfo *) NULL)
1008 draw_info->gradient.stops=(
StopInfo *) RelinquishMagickMemory(
1009 draw_info->gradient.stops);
1010 if (draw_info->clip_mask != (
char *) NULL)
1011 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1012 if (draw_info->clipping_mask != (
Image *) NULL)
1013 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1014 if (draw_info->composite_mask != (
Image *) NULL)
1015 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1016 if (draw_info->image_info != (
ImageInfo *) NULL)
1017 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1018 draw_info->signature=(~MagickCoreSignature);
1019 draw_info=(
DrawInfo *) RelinquishMagickMemory(draw_info);
1070 inverse_edge.x1=edge->x1;
1071 inverse_edge.y1=edge->y1;
1072 inverse_edge.x2=edge->x2;
1073 inverse_edge.y2=edge->y2;
1074 z=affine->ry*y+affine->tx;
1075 if (affine->sx >= MagickEpsilon)
1077 intercept=(-z/affine->sx);
1079 if (x > inverse_edge.x1)
1081 intercept=(-z+(double) image->columns)/affine->sx;
1083 if (x < inverse_edge.x2)
1087 if (affine->sx < -MagickEpsilon)
1089 intercept=(-z+(double) image->columns)/affine->sx;
1091 if (x > inverse_edge.x1)
1093 intercept=(-z/affine->sx);
1095 if (x < inverse_edge.x2)
1099 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1101 inverse_edge.x2=edge->x1;
1102 return(inverse_edge);
1107 z=affine->sy*y+affine->ty;
1108 if (affine->rx >= MagickEpsilon)
1110 intercept=(-z/affine->rx);
1112 if (x > inverse_edge.x1)
1114 intercept=(-z+(double) image->rows)/affine->rx;
1116 if (x < inverse_edge.x2)
1120 if (affine->rx < -MagickEpsilon)
1122 intercept=(-z+(double) image->rows)/affine->rx;
1124 if (x > inverse_edge.x1)
1126 intercept=(-z/affine->rx);
1128 if (x < inverse_edge.x2)
1132 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1134 inverse_edge.x2=edge->x2;
1135 return(inverse_edge);
1137 return(inverse_edge);
1148 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1150 inverse_affine.sx=determinant*affine->sy;
1151 inverse_affine.rx=determinant*(-affine->rx);
1152 inverse_affine.ry=determinant*(-affine->ry);
1153 inverse_affine.sy=determinant*affine->sx;
1154 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1156 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1158 return(inverse_affine);
1161MagickExport MagickBooleanType DrawAffineImage(
Image *image,
1196 assert(image != (
Image *) NULL);
1197 assert(image->signature == MagickCoreSignature);
1198 if (IsEventLogging() != MagickFalse)
1199 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1200 assert(source != (
const Image *) NULL);
1201 assert(source->signature == MagickCoreSignature);
1205 extent[1].x=(double) source->columns;
1207 extent[2].x=(double) source->columns;
1208 extent[2].y=(double) source->rows;
1210 extent[3].y=(double) source->rows;
1211 for (i=0; i < 4; i++)
1217 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1218 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1222 for (i=1; i < 4; i++)
1224 if (min.x > extent[i].x)
1226 if (min.y > extent[i].y)
1228 if (max.x < extent[i].x)
1230 if (max.y < extent[i].y)
1236 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1237 return(MagickFalse);
1243 inverse_affine=InverseAffineMatrix(affine);
1246 if (edge.y2 > ((
double) image->rows-1.0))
1247 edge.y2=(double) image->rows-1.0;
1248 GetPixelInfo(image,&zero);
1249 start=CastDoubleToSsizeT(ceil(edge.y1-0.5));
1250 stop=CastDoubleToSsizeT(floor(edge.y2+0.5));
1251 source_view=AcquireVirtualCacheView(source,exception);
1252 image_view=AcquireAuthenticCacheView(image,exception);
1253#if defined(MAGICKCORE_OPENMP_SUPPORT)
1254 #pragma omp parallel for schedule(static) shared(status) \
1255 magick_number_threads(source,image,(size_t) (stop-start),2)
1257 for (y=start; y <= stop; y++)
1275 if (status == MagickFalse)
1277 inverse_edge=AffineEdge(source,&inverse_affine,(
double) y,&edge);
1278 if (inverse_edge.x2 < inverse_edge.x1)
1280 if (inverse_edge.x1 < 0.0)
1281 inverse_edge.x1=0.0;
1282 if (inverse_edge.x2 > ((
double) image->columns-1.0))
1283 inverse_edge.x2=(double) image->columns-1.0;
1284 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToSsizeT(
1285 ceil(inverse_edge.x1-0.5)),y,(
size_t) CastDoubleToSsizeT(floor(
1286 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1287 if (q == (Quantum *) NULL)
1291 for (x=CastDoubleToSsizeT(ceil(inverse_edge.x1-0.5));
1292 x <= CastDoubleToSsizeT(floor(inverse_edge.x2+0.5)); x++)
1294 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1296 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1298 status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1299 point.x,point.y,&pixel,exception);
1300 if (status == MagickFalse)
1302 GetPixelInfoPixel(image,q,&composite);
1303 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1305 SetPixelViaPixelInfo(image,&composite,q);
1306 q+=(ptrdiff_t) GetPixelChannels(image);
1308 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1311 source_view=DestroyCacheView(source_view);
1312 image_view=DestroyCacheView(image_view);
1348static MagickBooleanType DrawBoundingRectangles(
Image *image,
1378 (void) memset(primitive_info,0,
sizeof(primitive_info));
1379 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1380 status=QueryColorCompliance(
"#000F",AllCompliance,&clone_info->fill,
1382 if (status == MagickFalse)
1384 clone_info=DestroyDrawInfo(clone_info);
1385 return(MagickFalse);
1389 if (clone_info->density != (
char *) NULL)
1397 flags=ParseGeometry(clone_info->density,&geometry_info);
1398 if ((flags & RhoValue) != 0)
1399 resolution.x=geometry_info.rho;
1400 resolution.y=resolution.x;
1401 if ((flags & SigmaValue) != 0)
1402 resolution.y=geometry_info.sigma;
1404 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1405 clone_info->stroke_width/2.0;
1412 bounds=polygon_info->edges[0].bounds;
1413 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1415 if (polygon_info->edges[i].bounds.x1 < (
double) bounds.x1)
1416 bounds.x1=polygon_info->edges[i].bounds.x1;
1417 if (polygon_info->edges[i].bounds.y1 < (
double) bounds.y1)
1418 bounds.y1=polygon_info->edges[i].bounds.y1;
1419 if (polygon_info->edges[i].bounds.x2 > (
double) bounds.x2)
1420 bounds.x2=polygon_info->edges[i].bounds.x2;
1421 if (polygon_info->edges[i].bounds.y2 > (
double) bounds.y2)
1422 bounds.y2=polygon_info->edges[i].bounds.y2;
1425 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1426 image->columns ? (
double) image->columns-1 : bounds.x1;
1428 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1429 image->rows ? (
double) image->rows-1 : bounds.y1;
1431 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1432 image->columns ? (
double) image->columns-1 : bounds.x2;
1434 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1435 image->rows ? (
double) image->rows-1 : bounds.y2;
1436 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1438 if (polygon_info->edges[i].direction != 0)
1439 status=QueryColorCompliance(
"#f00",AllCompliance,&clone_info->stroke,
1442 status=QueryColorCompliance(
"#0f0",AllCompliance,&clone_info->stroke,
1444 if (status == MagickFalse)
1446 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1447 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1448 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1449 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1450 primitive_info[0].primitive=RectanglePrimitive;
1451 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1452 primitive_info[0].method=ReplaceMethod;
1453 coordinates=(ssize_t) primitive_info[0].coordinates;
1454 primitive_info[coordinates].primitive=UndefinedPrimitive;
1455 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1456 if (status == MagickFalse)
1459 if (i < (ssize_t) polygon_info->number_edges)
1461 clone_info=DestroyDrawInfo(clone_info);
1462 return(status == 0 ? MagickFalse : MagickTrue);
1465 status=QueryColorCompliance(
"#00f",AllCompliance,&clone_info->stroke,
1467 if (status == MagickFalse)
1469 clone_info=DestroyDrawInfo(clone_info);
1470 return(MagickFalse);
1472 start.x=(double) (bounds.x1-mid);
1473 start.y=(double) (bounds.y1-mid);
1474 end.x=(double) (bounds.x2+mid);
1475 end.y=(double) (bounds.y2+mid);
1476 primitive_info[0].primitive=RectanglePrimitive;
1477 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1478 primitive_info[0].method=ReplaceMethod;
1479 coordinates=(ssize_t) primitive_info[0].coordinates;
1480 primitive_info[coordinates].primitive=UndefinedPrimitive;
1481 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1482 clone_info=DestroyDrawInfo(clone_info);
1483 return(status == 0 ? MagickFalse : MagickTrue);
1515MagickExport MagickBooleanType DrawClipPath(
Image *image,
1527 clip_path=GetImageArtifact(image,
id);
1528 if (clip_path == (
const char *) NULL)
1529 return(MagickFalse);
1530 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1532 if (clipping_mask == (
Image *) NULL)
1533 return(MagickFalse);
1534 status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1535 clipping_mask=DestroyImage(clipping_mask);
1572 const char *
id,
const char *clip_path,
ExceptionInfo *exception)
1587 assert(image != (
Image *) NULL);
1588 assert(image->signature == MagickCoreSignature);
1589 assert(draw_info != (
const DrawInfo *) NULL);
1590 if (IsEventLogging() != MagickFalse)
1591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1592 clip_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1593 status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1594 if (status == MagickFalse)
1595 return(DestroyImage(clip_mask));
1596 status=SetImageMask(clip_mask,WritePixelMask,(
Image *) NULL,exception);
1597 status=QueryColorCompliance(
"#0000",AllCompliance,
1598 &clip_mask->background_color,exception);
1599 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1600 clip_mask->background_color.alpha_trait=BlendPixelTrait;
1601 status=SetImageBackgroundColor(clip_mask,exception);
1602 if (draw_info->debug != MagickFalse)
1603 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin clip-path %s",
1605 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1606 (void) CloneString(&clone_info->primitive,clip_path);
1607 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1609 if (clone_info->clip_mask != (
char *) NULL)
1610 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1611 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1613 clone_info->stroke_width=0.0;
1614 clone_info->alpha=OpaqueAlpha;
1615 clone_info->clip_path=MagickTrue;
1616 status=RenderMVGContent(clip_mask,clone_info,0,exception);
1617 clone_info=DestroyDrawInfo(clone_info);
1618 separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1619 if (separate_mask == (
Image *) NULL)
1623 clip_mask=DestroyImage(clip_mask);
1624 clip_mask=separate_mask;
1625 status&=(MagickStatusType) NegateImage(clip_mask,MagickFalse,exception);
1627 if (status == MagickFalse)
1628 clip_mask=DestroyImage(clip_mask);
1629 if (draw_info->debug != MagickFalse)
1630 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end clip-path");
1666 const char *
id,
const char *mask_path,
ExceptionInfo *exception)
1681 assert(image != (
Image *) NULL);
1682 assert(image->signature == MagickCoreSignature);
1683 assert(draw_info != (
const DrawInfo *) NULL);
1684 if (IsEventLogging() != MagickFalse)
1685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1686 composite_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1687 status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1688 if (status == MagickFalse)
1689 return(DestroyImage(composite_mask));
1690 status=SetImageMask(composite_mask,CompositePixelMask,(
Image *) NULL,
1692 status=QueryColorCompliance(
"#0000",AllCompliance,
1693 &composite_mask->background_color,exception);
1694 composite_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1695 composite_mask->background_color.alpha_trait=BlendPixelTrait;
1696 (void) SetImageBackgroundColor(composite_mask,exception);
1697 if (draw_info->debug != MagickFalse)
1698 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin mask-path %s",
1700 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1701 (void) CloneString(&clone_info->primitive,mask_path);
1702 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1704 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1706 clone_info->stroke_width=0.0;
1707 clone_info->alpha=OpaqueAlpha;
1708 status=RenderMVGContent(composite_mask,clone_info,0,exception);
1709 clone_info=DestroyDrawInfo(clone_info);
1710 separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1711 if (separate_mask != (
Image *) NULL)
1713 composite_mask=DestroyImage(composite_mask);
1714 composite_mask=separate_mask;
1715 status=NegateImage(composite_mask,MagickFalse,exception);
1716 if (status == MagickFalse)
1717 composite_mask=DestroyImage(composite_mask);
1719 if (status == MagickFalse)
1720 composite_mask=DestroyImage(composite_mask);
1721 if (draw_info->debug != MagickFalse)
1722 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end mask-path");
1723 return(composite_mask);
1757static MagickBooleanType DrawDashPolygon(
const DrawInfo *draw_info,
1788 assert(draw_info != (
const DrawInfo *) NULL);
1789 if (draw_info->debug != MagickFalse)
1790 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-dash");
1791 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1792 number_vertices=(size_t) i;
1793 dash_polygon=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
1794 (2UL*number_vertices+32UL),
sizeof(*dash_polygon));
1797 (void) ThrowMagickException(exception,GetMagickModule(),
1798 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1799 return(MagickFalse);
1801 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1802 sizeof(*dash_polygon));
1803 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1804 clone_info->miterlimit=0;
1805 dash_polygon[0]=primitive_info[0];
1806 dash_polygon[0].closed_subpath=MagickFalse;
1807 scale=ExpandAffine(&draw_info->affine);
1808 length=scale*draw_info->dash_pattern[0];
1809 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1810 scale*draw_info->dash_offset : 0.0;
1812 for (n=0; offset > 0.0; j=0)
1814 if (draw_info->dash_pattern[n] <= 0.0)
1816 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1817 if (offset > length)
1821 length=scale*draw_info->dash_pattern[n];
1824 if (offset < length)
1836 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1838 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1839 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1840 maximum_length=hypot(dx,dy);
1841 if (maximum_length > (
double) (MaxBezierCoordinates >> 2))
1843 if (fabs(length) < MagickEpsilon)
1845 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1847 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1849 length=scale*draw_info->dash_pattern[n];
1851 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1853 total_length+=length;
1854 if ((n & 0x01) != 0)
1856 dash_polygon[0]=primitive_info[0];
1857 dash_polygon[0].closed_subpath=MagickFalse;
1858 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1859 total_length*MagickSafeReciprocal(maximum_length));
1860 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1861 total_length*MagickSafeReciprocal(maximum_length));
1866 if ((j+1) > (ssize_t) number_vertices)
1868 dash_polygon[j]=primitive_info[i-1];
1869 dash_polygon[j].closed_subpath=MagickFalse;
1870 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1871 total_length*MagickSafeReciprocal(maximum_length));
1872 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1873 total_length*MagickSafeReciprocal(maximum_length));
1874 dash_polygon[j].coordinates=1;
1876 dash_polygon[0].coordinates=(size_t) j;
1877 dash_polygon[j].primitive=UndefinedPrimitive;
1878 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1879 dash_polygon,exception);
1880 if (status == MagickFalse)
1883 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1885 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1887 length=scale*draw_info->dash_pattern[n];
1889 length-=(maximum_length-total_length);
1890 if ((n & 0x01) != 0)
1892 dash_polygon[j]=primitive_info[i];
1893 dash_polygon[j].coordinates=1;
1896 if ((status != MagickFalse) && (total_length < maximum_length) &&
1897 ((n & 0x01) == 0) && (j > 1))
1899 dash_polygon[j]=primitive_info[i-1];
1900 dash_polygon[j].closed_subpath=MagickFalse;
1901 dash_polygon[j].point.x+=MagickEpsilon;
1902 dash_polygon[j].point.y+=MagickEpsilon;
1903 dash_polygon[j].coordinates=1;
1905 dash_polygon[0].coordinates=(size_t) j;
1906 dash_polygon[j].primitive=UndefinedPrimitive;
1907 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1908 dash_polygon,exception);
1910 dash_polygon=(
PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1911 clone_info=DestroyDrawInfo(clone_info);
1912 if (draw_info->debug != MagickFalse)
1913 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-dash");
1914 return(status != 0 ? MagickTrue : MagickFalse);
1945static inline double GetStopColorOffset(
const GradientInfo *gradient,
1946 const ssize_t x,
const ssize_t y)
1948 switch (gradient->type)
1950 case UndefinedGradient:
1951 case LinearGradient:
1966 gradient_vector=(&gradient->gradient_vector);
1967 p.x=gradient_vector->x2-gradient_vector->x1;
1968 p.y=gradient_vector->y2-gradient_vector->y1;
1969 q.x=(double) x-gradient_vector->x1;
1970 q.y=(double) y-gradient_vector->y1;
1971 length=sqrt(q.x*q.x+q.y*q.y);
1972 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1973 gamma=MagickSafeReciprocal(gamma);
1974 scale=p.x*q.x+p.y*q.y;
1975 offset=gamma*scale*length;
1978 case RadialGradient:
1983 if (gradient->spread == RepeatSpread)
1985 v.x=(double) x-gradient->center.x;
1986 v.y=(double) y-gradient->center.y;
1987 return(sqrt(v.x*v.x+v.y*v.y));
1989 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1990 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1991 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1992 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1993 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1994 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1995 return(sqrt(v.x*v.x+v.y*v.y));
2001static int StopInfoCompare(
const void *x,
const void *y)
2009 if (stop_1->offset > stop_2->offset)
2011 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
2016MagickExport MagickBooleanType DrawGradientImage(
Image *image,
2052 assert(image != (
Image *) NULL);
2053 assert(image->signature == MagickCoreSignature);
2054 assert(draw_info != (
const DrawInfo *) NULL);
2055 if (IsEventLogging() != MagickFalse)
2056 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2057 gradient=(&draw_info->gradient);
2058 qsort(gradient->stops,gradient->number_stops,
sizeof(
StopInfo),
2060 gradient_vector=(&gradient->gradient_vector);
2061 point.x=gradient_vector->x2-gradient_vector->x1;
2062 point.y=gradient_vector->y2-gradient_vector->y1;
2063 length=sqrt(point.x*point.x+point.y*point.y);
2064 bounding_box=gradient->bounding_box;
2066 GetPixelInfo(image,&zero);
2067 image_view=AcquireAuthenticCacheView(image,exception);
2068 height=(size_t) (bounding_box.y+(ssize_t) bounding_box.height);
2069#if defined(MAGICKCORE_OPENMP_SUPPORT)
2070 #pragma omp parallel for schedule(static) shared(status) \
2071 magick_number_threads(image,image,height,1)
2073 for (y=bounding_box.y; y < (ssize_t) height; y++)
2094 if (status == MagickFalse)
2096 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(
size_t)
2097 bounding_box.width,1,exception);
2098 if (q == (Quantum *) NULL)
2105 offset=GetStopColorOffset(gradient,0,y);
2106 if (gradient->type != RadialGradient)
2107 offset*=MagickSafeReciprocal(length);
2108 width=(size_t) (bounding_box.x+(ssize_t) bounding_box.width);
2109 for (x=bounding_box.x; x < (ssize_t) width; x++)
2111 GetPixelInfoPixel(image,q,&pixel);
2112 switch (gradient->spread)
2114 case UndefinedSpread:
2117 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2118 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2120 offset=GetStopColorOffset(gradient,x,y);
2121 if (gradient->type != RadialGradient)
2122 offset*=MagickSafeReciprocal(length);
2124 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2125 if (offset < gradient->stops[i].offset)
2127 if ((offset < 0.0) || (i == 0))
2128 composite=gradient->stops[0].color;
2130 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2131 composite=gradient->stops[gradient->number_stops-1].color;
2136 alpha=(offset-gradient->stops[i].offset)/
2137 (gradient->stops[j].offset-gradient->stops[i].offset);
2138 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2139 &gradient->stops[j].color,alpha,&composite);
2145 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2146 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2148 offset=GetStopColorOffset(gradient,x,y);
2149 if (gradient->type != RadialGradient)
2150 offset*=MagickSafeReciprocal(length);
2154 if ((ssize_t) fmod(offset,2.0) == 0)
2155 offset=fmod(offset,1.0);
2157 offset=1.0-fmod(offset,1.0);
2158 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2159 if (offset < gradient->stops[i].offset)
2162 composite=gradient->stops[0].color;
2164 if (i == (ssize_t) gradient->number_stops)
2165 composite=gradient->stops[gradient->number_stops-1].color;
2170 alpha=(offset-gradient->stops[i].offset)/
2171 (gradient->stops[j].offset-gradient->stops[i].offset);
2172 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2173 &gradient->stops[j].color,alpha,&composite);
2185 antialias=MagickFalse;
2187 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2188 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2190 offset=GetStopColorOffset(gradient,x,y);
2191 if (gradient->type == LinearGradient)
2193 repeat=fmod(offset,length);
2195 repeat=length-fmod(-repeat,length);
2197 repeat=fmod(offset,length);
2198 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2199 MagickTrue : MagickFalse;
2200 offset=MagickSafeReciprocal(length)*repeat;
2204 repeat=fmod(offset,gradient->radius);
2206 repeat=gradient->radius-fmod(-repeat,gradient->radius);
2208 repeat=fmod(offset,gradient->radius);
2209 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2211 offset=repeat*MagickSafeReciprocal(gradient->radius);
2214 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2215 if (offset < gradient->stops[i].offset)
2218 composite=gradient->stops[0].color;
2220 if (i == (ssize_t) gradient->number_stops)
2221 composite=gradient->stops[gradient->number_stops-1].color;
2226 alpha=(offset-gradient->stops[i].offset)/
2227 (gradient->stops[j].offset-gradient->stops[i].offset);
2228 if (antialias != MagickFalse)
2230 if (gradient->type == LinearGradient)
2231 alpha=length-repeat;
2233 alpha=gradient->radius-repeat;
2235 j=(ssize_t) gradient->number_stops-1L;
2237 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2238 &gradient->stops[j].color,alpha,&composite);
2243 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2245 SetPixelViaPixelInfo(image,&pixel,q);
2246 q+=(ptrdiff_t) GetPixelChannels(image);
2248 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2251 image_view=DestroyCacheView(image_view);
2287static inline MagickBooleanType CheckPrimitiveExtent(
MVGInfo *mvg_info,
2302 if ((mvg_info == (
MVGInfo *) NULL) ||
2305 (mvg_info->extent == (
size_t *) NULL))
2306 return(MagickFalse);
2307 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2308 if ((proposed_extent <= 0.0) || (proposed_extent > (
double) MAGICK_SIZE_MAX))
2309 return(MagickFalse);
2310 extent=CastDoubleToSizeT(ceil(proposed_extent));
2311 if (extent <= *mvg_info->extent)
2314 return(MagickFalse);
2322 extent=(size_t) PrimitiveExtentPad;
2323 primitive_info=(
PrimitiveInfo *) AcquireCriticalMemory(extent*
2324 sizeof(*primitive_info));
2325 (void) memset(primitive_info,0,extent*
sizeof(*primitive_info));
2326 *mvg_info->primitive_info=primitive_info;
2327 *mvg_info->extent=extent;
2329 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2330 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2331 return(MagickFalse);
2333 primitive_info[extent].primitive=UndefinedPrimitive;
2334 primitive_info[extent].text=(
char *) NULL;
2338 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2340 primitive_info[i].primitive=UndefinedPrimitive;
2341 primitive_info[i].text=(
char *) NULL;
2343 *mvg_info->primitive_info=primitive_info;
2344 *mvg_info->extent=extent;
2348static inline double GetDrawValue(
const char *magick_restrict
string,
2349 char **magick_restrict sentinel)
2352 **magick_restrict q;
2358 value=InterpretLocaleValue(
string,q);
2363static int MVGMacroCompare(
const void *target,
const void *source)
2369 p=(
const char *) target;
2370 q=(
const char *) source;
2371 return(strcmp(p,q));
2393 if (primitive == (
const char *) NULL)
2395 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2396 RelinquishMagickMemory);
2397 macro=AcquireString(primitive);
2398 token=AcquireString(primitive);
2399 extent=strlen(token)+MagickPathExtent;
2400 for (q=primitive; *q !=
'\0'; )
2402 if (GetNextToken(q,&q,extent,token) < 1)
2406 if (LocaleCompare(
"push",token) == 0)
2412 (void) GetNextToken(q,&q,extent,token);
2416 name[MagickPathExtent];
2427 (void) GetNextToken(q,&q,extent,token);
2430 (void) CopyMagickString(name,token,MagickPathExtent);
2432 for (p=q; *p !=
'\0'; )
2434 if (GetNextToken(p,&p,extent,token) < 1)
2438 if (LocaleCompare(token,
"pop") == 0)
2440 end=p-strlen(token)-1;
2443 if (LocaleCompare(token,
"push") == 0)
2445 if (n++ > MagickMaxRecursionDepth)
2447 (void) ThrowMagickException(exception,GetMagickModule(),
2448 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2452 if ((n == 0) && (end >= start))
2455 length=(size_t) (end-start);
2460 (void) GetNextToken(p,&p,extent,token);
2463 (void) CopyMagickString(macro,start,length);
2464 (void) AddValueToSplayTree(macros,ConstantString(name),
2465 ConstantString(macro));
2473 token=DestroyString(token);
2474 macro=DestroyString(macro);
2478static inline MagickBooleanType IsValidListChar(
int c)
2480 if ((c >=
'0') && (c <=
'9'))
2494 return(MagickFalse);
2499static inline MagickBooleanType IsValidPoint(
const char *point)
2507 value=GetDrawValue(point,&p);
2508 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2512static inline MagickBooleanType TracePoint(
PrimitiveInfo *primitive_info,
2515 primitive_info->point=point;
2516 primitive_info->coordinates=1;
2517 primitive_info->closed_subpath=MagickFalse;
2518 primitive_info->text=(
char *) NULL;
2522static MagickBooleanType RenderMVGContent(
Image *image,
2525#define RenderImageTag "Render/Image"
2532 keyword[MagickPathExtent],
2533 geometry[MagickPathExtent],
2535 pattern[MagickPathExtent],
2599 assert(image != (
Image *) NULL);
2600 assert(image->signature == MagickCoreSignature);
2601 assert(draw_info != (
DrawInfo *) NULL);
2602 assert(draw_info->signature == MagickCoreSignature);
2603 if (IsEventLogging() != MagickFalse)
2604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2605 if (depth > MagickMaxRecursionDepth)
2606 ThrowBinaryException(DrawError,
"VectorGraphicsNestedTooDeeply",
2608 if ((draw_info->primitive == (
char *) NULL) ||
2609 (*draw_info->primitive ==
'\0'))
2610 return(MagickFalse);
2611 if (draw_info->debug != MagickFalse)
2612 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"begin draw-image");
2613 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2614 return(MagickFalse);
2615 if ((image->alpha_trait & BlendPixelTrait) == 0)
2617 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2618 if (status == MagickFalse)
2619 return(MagickFalse);
2621 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2622 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2623 primitive=FileToString(draw_info->primitive,~0UL,exception);
2625 primitive=AcquireString(draw_info->primitive);
2626 if (primitive == (
char *) NULL)
2627 return(MagickFalse);
2628 primitive_extent=(double) strlen(primitive);
2629 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2636 graphic_context=(
DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2637 if (graphic_context == (
DrawInfo **) NULL)
2639 primitive=DestroyString(primitive);
2640 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2643 number_points=(size_t) PrimitiveExtentPad;
2644 primitive_info=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2645 (number_points+1),
sizeof(*primitive_info));
2648 primitive=DestroyString(primitive);
2649 for ( ; n >= 0; n--)
2650 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2651 graphic_context=(
DrawInfo **) RelinquishMagickMemory(graphic_context);
2652 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2655 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2656 sizeof(*primitive_info));
2657 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2658 mvg_info.primitive_info=(&primitive_info);
2659 mvg_info.extent=(&number_points);
2660 mvg_info.exception=exception;
2661 graphic_context[n]=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
2662 graphic_context[n]->viewbox=image->page;
2663 if ((image->page.width == 0) || (image->page.height == 0))
2665 graphic_context[n]->viewbox.width=image->columns;
2666 graphic_context[n]->viewbox.height=image->rows;
2668 token=AcquireString(primitive);
2669 extent=strlen(token)+MagickPathExtent;
2673 macros=GetMVGMacros(primitive,exception);
2675 for (q=primitive; *q !=
'\0'; )
2680 if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2682 if (*keyword ==
'\0')
2684 if (*keyword ==
'#')
2689 while ((*q !=
'\n') && (*q !=
'\0'))
2693 p=q-strlen(keyword)-1;
2694 primitive_type=UndefinedPrimitive;
2695 current=graphic_context[n]->affine;
2696 GetAffineMatrix(&affine);
2705 if (LocaleCompare(
"affine",keyword) == 0)
2707 (void) GetNextToken(q,&q,extent,token);
2708 affine.sx=GetDrawValue(token,&next_token);
2709 if (token == next_token)
2710 ThrowPointExpectedException(token,exception);
2711 (void) GetNextToken(q,&q,extent,token);
2712 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2713 ThrowPointExpectedException(token,exception);
2715 (void) GetNextToken(q,&q,extent,token);
2716 affine.ry=GetDrawValue(token,&next_token);
2717 if (token == next_token)
2718 ThrowPointExpectedException(token,exception);
2719 (void) GetNextToken(q,&q,extent,token);
2720 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2721 ThrowPointExpectedException(token,exception);
2723 (void) GetNextToken(q,&q,extent,token);
2724 affine.rx=GetDrawValue(token,&next_token);
2725 if (token == next_token)
2726 ThrowPointExpectedException(token,exception);
2727 (void) GetNextToken(q,&q,extent,token);
2728 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2729 ThrowPointExpectedException(token,exception);
2731 (void) GetNextToken(q,&q,extent,token);
2732 affine.sy=GetDrawValue(token,&next_token);
2733 if (token == next_token)
2734 ThrowPointExpectedException(token,exception);
2735 (void) GetNextToken(q,&q,extent,token);
2736 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2737 ThrowPointExpectedException(token,exception);
2739 (void) GetNextToken(q,&q,extent,token);
2740 affine.tx=GetDrawValue(token,&next_token);
2741 if (token == next_token)
2742 ThrowPointExpectedException(token,exception);
2743 (void) GetNextToken(q,&q,extent,token);
2744 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2745 ThrowPointExpectedException(token,exception);
2747 (void) GetNextToken(q,&q,extent,token);
2748 affine.ty=GetDrawValue(token,&next_token);
2749 if (token == next_token)
2750 ThrowPointExpectedException(token,exception);
2753 if (LocaleCompare(
"alpha",keyword) == 0)
2755 primitive_type=AlphaPrimitive;
2758 if (LocaleCompare(
"arc",keyword) == 0)
2760 primitive_type=ArcPrimitive;
2769 if (LocaleCompare(
"bezier",keyword) == 0)
2771 primitive_type=BezierPrimitive;
2774 if (LocaleCompare(
"border-color",keyword) == 0)
2776 (void) GetNextToken(q,&q,extent,token);
2777 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2778 &graphic_context[n]->border_color,exception);
2787 if (LocaleCompare(
"class",keyword) == 0)
2792 (void) GetNextToken(q,&q,extent,token);
2793 if ((*token ==
'\0') || (*token ==
';'))
2801 for (i=0; i <= n; i++)
2802 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2806 if (classDepth++ > MagickMaxRecursionDepth)
2808 (void) ThrowMagickException(exception,GetMagickModule(),
2809 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2813 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2814 if ((graphic_context[n]->render != MagickFalse) &&
2815 (mvg_class != (
const char *) NULL) && (p > primitive))
2826 (void) CloneString(&graphic_context[n]->
id,token);
2827 offset=(ssize_t) (p-primitive);
2828 elements=AcquireString(primitive);
2829 elements[offset]=
'\0';
2830 (void) ConcatenateString(&elements,mvg_class);
2831 (void) ConcatenateString(&elements,
"\n");
2832 (void) ConcatenateString(&elements,q);
2833 primitive=DestroyString(primitive);
2839 if (LocaleCompare(
"clip-path",keyword) == 0)
2847 (void) GetNextToken(q,&q,extent,token);
2853 (void) CloneString(&graphic_context[n]->clip_mask,token);
2854 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2855 if (clip_path != (
const char *) NULL)
2857 if (graphic_context[n]->clipping_mask != (
Image *) NULL)
2858 graphic_context[n]->clipping_mask=
2859 DestroyImage(graphic_context[n]->clipping_mask);
2860 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2861 graphic_context[n],token,clip_path,exception);
2862 if (graphic_context[n]->compliance != SVGCompliance)
2864 clip_path=(
const char *) GetValueFromSplayTree(macros,
2865 graphic_context[n]->clip_mask);
2866 if (clip_path != (
const char *) NULL)
2867 (
void) SetImageArtifact(image,
2868 graphic_context[n]->clip_mask,clip_path);
2869 status&=(MagickStatusType) DrawClipPath(image,
2870 graphic_context[n],graphic_context[n]->clip_mask,
2876 if (LocaleCompare(
"clip-rule",keyword) == 0)
2881 (void) GetNextToken(q,&q,extent,token);
2882 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2884 if (fill_rule == -1)
2889 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2892 if (LocaleCompare(
"clip-units",keyword) == 0)
2897 (void) GetNextToken(q,&q,extent,token);
2898 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2900 if (clip_units == -1)
2905 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2906 if (clip_units == ObjectBoundingBox)
2908 GetAffineMatrix(¤t);
2909 affine.sx=draw_info->bounds.x2;
2910 affine.sy=draw_info->bounds.y2;
2911 affine.tx=draw_info->bounds.x1;
2912 affine.ty=draw_info->bounds.y1;
2917 if (LocaleCompare(
"circle",keyword) == 0)
2919 primitive_type=CirclePrimitive;
2922 if (LocaleCompare(
"color",keyword) == 0)
2924 primitive_type=ColorPrimitive;
2927 if (LocaleCompare(
"compliance",keyword) == 0)
2933 (void) GetNextToken(q,&q,extent,token);
2934 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2935 MagickComplianceOptions,MagickFalse,token);
2938 if (LocaleCompare(
"currentColor",keyword) == 0)
2940 (void) GetNextToken(q,&q,extent,token);
2949 if (LocaleCompare(
"decorate",keyword) == 0)
2954 (void) GetNextToken(q,&q,extent,token);
2955 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2962 graphic_context[n]->decorate=(DecorationType) decorate;
2965 if (LocaleCompare(
"density",keyword) == 0)
2967 (void) GetNextToken(q,&q,extent,token);
2968 (void) CloneString(&graphic_context[n]->density,token);
2971 if (LocaleCompare(
"direction",keyword) == 0)
2976 (void) GetNextToken(q,&q,extent,token);
2977 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2979 if (direction == -1)
2982 graphic_context[n]->direction=(DirectionType) direction;
2991 if (LocaleCompare(
"ellipse",keyword) == 0)
2993 primitive_type=EllipsePrimitive;
2996 if (LocaleCompare(
"encoding",keyword) == 0)
2998 (void) GetNextToken(q,&q,extent,token);
2999 (void) CloneString(&graphic_context[n]->encoding,token);
3008 if (LocaleCompare(
"fill",keyword) == 0)
3013 (void) GetNextToken(q,&q,extent,token);
3014 if (graphic_context[n]->clip_path != MagickFalse)
3016 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3017 if (mvg_class != (
const char *) NULL)
3019 (void) DrawPatternPath(image,draw_info,mvg_class,
3020 &graphic_context[n]->fill_pattern,exception);
3023 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3024 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3026 (void) DrawPatternPath(image,draw_info,token,
3027 &graphic_context[n]->fill_pattern,exception);
3030 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3031 &graphic_context[n]->fill,exception);
3032 if (graphic_context[n]->fill_alpha != (
double) OpaqueAlpha)
3033 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3036 if (LocaleCompare(
"fill-opacity",keyword) == 0)
3041 (void) GetNextToken(q,&q,extent,token);
3042 if (graphic_context[n]->clip_path != MagickFalse)
3044 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3045 opacity=MagickMin(MagickMax(factor*
3046 GetDrawValue(token,&next_token),0.0),1.0);
3047 if (token == next_token)
3048 ThrowPointExpectedException(token,exception);
3049 if (graphic_context[n]->compliance == SVGCompliance)
3050 graphic_context[n]->fill_alpha*=opacity;
3052 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3053 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3054 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3056 graphic_context[n]->fill.alpha=(MagickRealType)
3057 ClampToQuantum((
double) QuantumRange*opacity);
3058 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3061 if (LocaleCompare(
"fill-rule",keyword) == 0)
3066 (void) GetNextToken(q,&q,extent,token);
3067 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3069 if (fill_rule == -1)
3074 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3077 if (LocaleCompare(
"font",keyword) == 0)
3079 (void) GetNextToken(q,&q,extent,token);
3080 (void) CloneString(&graphic_context[n]->font,token);
3081 if (LocaleCompare(
"none",token) == 0)
3082 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3083 graphic_context[n]->font);
3086 if (LocaleCompare(
"font-family",keyword) == 0)
3088 (void) GetNextToken(q,&q,extent,token);
3089 (void) CloneString(&graphic_context[n]->family,token);
3092 if (LocaleCompare(
"font-size",keyword) == 0)
3094 (void) GetNextToken(q,&q,extent,token);
3095 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3096 if (token == next_token)
3097 ThrowPointExpectedException(token,exception);
3100 if (LocaleCompare(
"font-stretch",keyword) == 0)
3105 (void) GetNextToken(q,&q,extent,token);
3106 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3112 graphic_context[n]->stretch=(StretchType) stretch;
3115 if (LocaleCompare(
"font-style",keyword) == 0)
3120 (void) GetNextToken(q,&q,extent,token);
3121 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3127 graphic_context[n]->style=(StyleType) style;
3130 if (LocaleCompare(
"font-weight",keyword) == 0)
3135 (void) GetNextToken(q,&q,extent,token);
3136 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3138 weight=(ssize_t) StringToUnsignedLong(token);
3139 graphic_context[n]->weight=(size_t) weight;
3148 if (LocaleCompare(
"gradient-units",keyword) == 0)
3150 (void) GetNextToken(q,&q,extent,token);
3153 if (LocaleCompare(
"gravity",keyword) == 0)
3158 (void) GetNextToken(q,&q,extent,token);
3159 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3165 graphic_context[n]->gravity=(GravityType) gravity;
3174 if (LocaleCompare(
"image",keyword) == 0)
3179 primitive_type=ImagePrimitive;
3180 (void) GetNextToken(q,&q,extent,token);
3181 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3187 graphic_context[n]->compose=(CompositeOperator) compose;
3190 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3192 (void) GetNextToken(q,&q,extent,token);
3193 graphic_context[n]->interline_spacing=GetDrawValue(token,
3195 if (token == next_token)
3196 ThrowPointExpectedException(token,exception);
3199 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3201 (void) GetNextToken(q,&q,extent,token);
3202 graphic_context[n]->interword_spacing=GetDrawValue(token,
3204 if (token == next_token)
3205 ThrowPointExpectedException(token,exception);
3214 if (LocaleCompare(
"kerning",keyword) == 0)
3216 (void) GetNextToken(q,&q,extent,token);
3217 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3218 if (token == next_token)
3219 ThrowPointExpectedException(token,exception);
3228 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3230 (void) GetNextToken(q,&q,extent,token);
3231 if (IsValidPoint(token) == MagickFalse)
3233 clone_info=CloneDrawInfo((
ImageInfo *) NULL,graphic_context[n]);
3234 clone_info->text=AcquireString(
" ");
3235 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3237 graphic_context[n]->kerning=metrics.width*
3238 GetDrawValue(token,&next_token);
3239 clone_info=DestroyDrawInfo(clone_info);
3240 if (token == next_token)
3241 ThrowPointExpectedException(token,exception);
3244 if (LocaleCompare(
"line",keyword) == 0)
3246 primitive_type=LinePrimitive;
3255 if (LocaleCompare(
"mask",keyword) == 0)
3263 (void) GetNextToken(q,&q,extent,token);
3264 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3265 if (mask_path != (
const char *) NULL)
3267 if (graphic_context[n]->composite_mask != (
Image *) NULL)
3268 graphic_context[n]->composite_mask=
3269 DestroyImage(graphic_context[n]->composite_mask);
3270 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3271 graphic_context[n],token,mask_path,exception);
3272 if (graphic_context[n]->compliance != SVGCompliance)
3273 status=SetImageMask(image,CompositePixelMask,
3274 graphic_context[n]->composite_mask,exception);
3284 if (LocaleCompare(
"offset",keyword) == 0)
3286 (void) GetNextToken(q,&q,extent,token);
3289 if (LocaleCompare(
"opacity",keyword) == 0)
3294 (void) GetNextToken(q,&q,extent,token);
3295 if (graphic_context[n]->clip_path != MagickFalse)
3297 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3298 opacity=MagickMin(MagickMax(factor*
3299 GetDrawValue(token,&next_token),0.0),1.0);
3300 if (token == next_token)
3301 ThrowPointExpectedException(token,exception);
3302 if (graphic_context[n]->compliance == SVGCompliance)
3304 graphic_context[n]->fill_alpha*=opacity;
3305 graphic_context[n]->stroke_alpha*=opacity;
3309 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3310 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3312 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3314 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3315 graphic_context[n]->stroke.alpha=
3316 graphic_context[n]->stroke_alpha;
3320 graphic_context[n]->fill.alpha=(MagickRealType)
3321 ClampToQuantum((
double) QuantumRange*opacity);
3322 graphic_context[n]->stroke.alpha=(MagickRealType)
3323 ClampToQuantum((
double) QuantumRange*opacity);
3325 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3326 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3335 if (LocaleCompare(
"path",keyword) == 0)
3337 primitive_type=PathPrimitive;
3340 if (LocaleCompare(
"point",keyword) == 0)
3342 primitive_type=PointPrimitive;
3345 if (LocaleCompare(
"polyline",keyword) == 0)
3347 primitive_type=PolylinePrimitive;
3350 if (LocaleCompare(
"polygon",keyword) == 0)
3352 primitive_type=PolygonPrimitive;
3355 if (LocaleCompare(
"pop",keyword) == 0)
3357 if (GetNextToken(q,&q,extent,token) < 1)
3359 if (LocaleCompare(
"class",token) == 0)
3361 if (LocaleCompare(
"clip-path",token) == 0)
3363 if (LocaleCompare(
"defs",token) == 0)
3366 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3370 if (LocaleCompare(
"gradient",token) == 0)
3372 if (LocaleCompare(
"graphic-context",token) == 0)
3376 (void) ThrowMagickException(exception,GetMagickModule(),
3377 DrawError,
"UnbalancedGraphicContextPushPop",
"`%s'",token);
3382 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3383 (graphic_context[n]->compliance != SVGCompliance))
3384 if (LocaleCompare(graphic_context[n]->clip_mask,
3385 graphic_context[n-1]->clip_mask) != 0)
3386 status=SetImageMask(image,WritePixelMask,(
Image *) NULL,
3388 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3392 if (LocaleCompare(
"mask",token) == 0)
3394 if (LocaleCompare(
"pattern",token) == 0)
3396 if (LocaleCompare(
"symbol",token) == 0)
3399 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3406 if (LocaleCompare(
"push",keyword) == 0)
3408 if (GetNextToken(q,&q,extent,token) < 1)
3410 if (LocaleCompare(
"class",token) == 0)
3415 for (p=q; *q !=
'\0'; )
3417 if (GetNextToken(q,&q,extent,token) < 1)
3419 if (LocaleCompare(token,
"pop") != 0)
3421 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3422 if (LocaleCompare(token,
"class") != 0)
3426 (void) GetNextToken(q,&q,extent,token);
3429 if (LocaleCompare(
"clip-path",token) == 0)
3431 (void) GetNextToken(q,&q,extent,token);
3432 for (p=q; *q !=
'\0'; )
3434 if (GetNextToken(q,&q,extent,token) < 1)
3436 if (LocaleCompare(token,
"pop") != 0)
3438 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3439 if (LocaleCompare(token,
"clip-path") != 0)
3443 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3448 (void) GetNextToken(q,&q,extent,token);
3451 if (LocaleCompare(
"defs",token) == 0)
3454 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3458 if (LocaleCompare(
"gradient",token) == 0)
3461 key[2*MagickPathExtent],
3462 name[MagickPathExtent],
3463 type[MagickPathExtent];
3468 (void) GetNextToken(q,&q,extent,token);
3469 (void) CopyMagickString(name,token,MagickPathExtent);
3470 (void) GetNextToken(q,&q,extent,token);
3471 (void) CopyMagickString(type,token,MagickPathExtent);
3472 (void) GetNextToken(q,&q,extent,token);
3473 segment.x1=GetDrawValue(token,&next_token);
3474 if (token == next_token)
3475 ThrowPointExpectedException(token,exception);
3476 (void) GetNextToken(q,&q,extent,token);
3477 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3478 ThrowPointExpectedException(token,exception);
3480 (void) GetNextToken(q,&q,extent,token);
3481 segment.y1=GetDrawValue(token,&next_token);
3482 if (token == next_token)
3483 ThrowPointExpectedException(token,exception);
3484 (void) GetNextToken(q,&q,extent,token);
3485 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3486 ThrowPointExpectedException(token,exception);
3488 (void) GetNextToken(q,&q,extent,token);
3489 segment.x2=GetDrawValue(token,&next_token);
3490 if (token == next_token)
3491 ThrowPointExpectedException(token,exception);
3492 (void) GetNextToken(q,&q,extent,token);
3493 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3494 ThrowPointExpectedException(token,exception);
3496 (void) GetNextToken(q,&q,extent,token);
3497 segment.y2=GetDrawValue(token,&next_token);
3498 if (token == next_token)
3499 ThrowPointExpectedException(token,exception);
3500 if (LocaleCompare(type,
"radial") == 0)
3502 (void) GetNextToken(q,&q,extent,token);
3503 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3504 ThrowPointExpectedException(token,exception);
3506 (void) GetNextToken(q,&q,extent,token);
3508 for (p=q; *q !=
'\0'; )
3510 if (GetNextToken(q,&q,extent,token) < 1)
3512 if (LocaleCompare(token,
"pop") != 0)
3514 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3515 if (LocaleCompare(token,
"gradient") != 0)
3519 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3520 (p == (
char *) NULL) || ((q-4) < p) ||
3521 ((size_t) (q-p+4+1) > extent))
3526 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3527 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3528 graphic_context[n]->affine.ry*segment.y1+
3529 graphic_context[n]->affine.tx;
3530 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3531 graphic_context[n]->affine.sy*segment.y1+
3532 graphic_context[n]->affine.ty;
3533 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3534 graphic_context[n]->affine.ry*segment.y2+
3535 graphic_context[n]->affine.tx;
3536 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3537 graphic_context[n]->affine.sy*segment.y2+
3538 graphic_context[n]->affine.ty;
3539 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3540 (void) SetImageArtifact(image,key,token);
3541 (void) FormatLocaleString(key,MagickPathExtent,
"%s-type",name);
3542 (void) SetImageArtifact(image,key,type);
3543 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3545 (void) FormatLocaleString(geometry,MagickPathExtent,
3546 "%gx%g%+.15g%+.15g",
3547 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3548 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3549 bounds.x1,bounds.y1);
3550 (void) SetImageArtifact(image,key,geometry);
3551 (void) GetNextToken(q,&q,extent,token);
3554 if (LocaleCompare(
"graphic-context",token) == 0)
3557 graphic_context=(
DrawInfo **) ResizeQuantumMemory(
3558 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3559 if (graphic_context == (
DrawInfo **) NULL)
3561 (void) ThrowMagickException(exception,GetMagickModule(),
3562 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3567 graphic_context[n]=CloneDrawInfo((
ImageInfo *) NULL,
3568 graphic_context[n-1]);
3571 (void) GetNextToken(q,&q,extent,token);
3572 (void) CloneString(&graphic_context[n]->
id,token);
3574 if (n > MagickMaxRecursionDepth)
3576 (void) ThrowMagickException(exception,GetMagickModule(),
3577 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",
3583 if (LocaleCompare(
"mask",token) == 0)
3585 (void) GetNextToken(q,&q,extent,token);
3588 if (LocaleCompare(
"pattern",token) == 0)
3591 key[2*MagickPathExtent],
3592 name[MagickPathExtent];
3597 (void) GetNextToken(q,&q,extent,token);
3598 (void) CopyMagickString(name,token,MagickPathExtent);
3599 (void) GetNextToken(q,&q,extent,token);
3600 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3602 if (token == next_token)
3603 ThrowPointExpectedException(token,exception);
3604 (void) GetNextToken(q,&q,extent,token);
3605 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3606 ThrowPointExpectedException(token,exception);
3608 (void) GetNextToken(q,&q,extent,token);
3609 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3611 if (token == next_token)
3612 ThrowPointExpectedException(token,exception);
3613 (void) GetNextToken(q,&q,extent,token);
3614 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3615 ThrowPointExpectedException(token,exception);
3617 (void) GetNextToken(q,&q,extent,token);
3618 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3620 if (token == next_token)
3621 ThrowPointExpectedException(token,exception);
3622 (void) GetNextToken(q,&q,extent,token);
3623 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3624 ThrowPointExpectedException(token,exception);
3626 (void) GetNextToken(q,&q,extent,token);
3627 region.height=CastDoubleToSizeT(GetDrawValue(token,
3629 if (token == next_token)
3630 ThrowPointExpectedException(token,exception);
3631 for (p=q; *q !=
'\0'; )
3633 if (GetNextToken(q,&q,extent,token) < 1)
3635 if (LocaleCompare(token,
"pop") != 0)
3637 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3638 if (LocaleCompare(token,
"pattern") != 0)
3642 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3643 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3648 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3649 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3650 (void) SetImageArtifact(image,key,token);
3651 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3653 (void) FormatLocaleString(geometry,MagickPathExtent,
3654 "%.20gx%.20g%+.20g%+.20g",(
double) region.width,(double)
3655 region.height,(
double) region.x,(double) region.y);
3656 (void) SetImageArtifact(image,key,geometry);
3657 (void) GetNextToken(q,&q,extent,token);
3660 if (LocaleCompare(
"symbol",token) == 0)
3663 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3676 if (LocaleCompare(
"rectangle",keyword) == 0)
3678 primitive_type=RectanglePrimitive;
3681 if (LocaleCompare(
"rotate",keyword) == 0)
3683 (void) GetNextToken(q,&q,extent,token);
3684 angle=GetDrawValue(token,&next_token);
3685 if (token == next_token)
3686 ThrowPointExpectedException(token,exception);
3687 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3688 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3689 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3690 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3693 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3695 primitive_type=RoundRectanglePrimitive;
3704 if (LocaleCompare(
"scale",keyword) == 0)
3706 (void) GetNextToken(q,&q,extent,token);
3707 affine.sx=GetDrawValue(token,&next_token);
3708 if (token == next_token)
3709 ThrowPointExpectedException(token,exception);
3710 (void) GetNextToken(q,&q,extent,token);
3711 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3712 ThrowPointExpectedException(token,exception);
3714 (void) GetNextToken(q,&q,extent,token);
3715 affine.sy=GetDrawValue(token,&next_token);
3716 if (token == next_token)
3717 ThrowPointExpectedException(token,exception);
3720 if (LocaleCompare(
"skewX",keyword) == 0)
3722 (void) GetNextToken(q,&q,extent,token);
3723 angle=GetDrawValue(token,&next_token);
3724 if (token == next_token)
3725 ThrowPointExpectedException(token,exception);
3726 affine.ry=sin(DegreesToRadians(angle));
3729 if (LocaleCompare(
"skewY",keyword) == 0)
3731 (void) GetNextToken(q,&q,extent,token);
3732 angle=GetDrawValue(token,&next_token);
3733 if (token == next_token)
3734 ThrowPointExpectedException(token,exception);
3735 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3738 if (LocaleCompare(
"stop-color",keyword) == 0)
3744 if (number_stops == 1)
3745 stops=(
StopInfo *) AcquireQuantumMemory(2,
sizeof(*stops));
3747 if (number_stops > 2)
3748 stops=(
StopInfo *) ResizeQuantumMemory(stops,number_stops,
3752 (void) ThrowMagickException(exception,GetMagickModule(),
3753 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3758 (void) GetNextToken(q,&q,extent,token);
3759 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3760 &stop_color,exception);
3761 stops[number_stops-1].color=stop_color;
3762 (void) GetNextToken(q,&q,extent,token);
3763 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3764 stops[number_stops-1].offset=factor*GetDrawValue(token,
3766 if (token == next_token)
3767 ThrowPointExpectedException(token,exception);
3770 if (LocaleCompare(
"stroke",keyword) == 0)
3775 (void) GetNextToken(q,&q,extent,token);
3776 if (graphic_context[n]->clip_path != MagickFalse)
3778 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3779 if (mvg_class != (
const char *) NULL)
3781 (void) DrawPatternPath(image,draw_info,mvg_class,
3782 &graphic_context[n]->stroke_pattern,exception);
3785 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3786 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3788 (void) DrawPatternPath(image,draw_info,token,
3789 &graphic_context[n]->stroke_pattern,exception);
3792 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3793 &graphic_context[n]->stroke,exception);
3794 if (graphic_context[n]->stroke_alpha != (
double) OpaqueAlpha)
3795 graphic_context[n]->stroke.alpha=
3796 graphic_context[n]->stroke_alpha;
3799 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3801 (void) GetNextToken(q,&q,extent,token);
3802 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3803 MagickTrue : MagickFalse;
3806 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3808 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3809 graphic_context[n]->dash_pattern=(
double *)
3810 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3811 if (IsValidPoint(q) != MagickFalse)
3817 (void) GetNextToken(r,&r,extent,token);
3818 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3819 ThrowPointExpectedException(token,exception);
3821 (void) GetNextToken(r,&r,extent,token);
3822 for (x=0; IsValidPoint(token) != MagickFalse; x++)
3824 (void) GetNextToken(r,&r,extent,token);
3826 (void) GetNextToken(r,&r,extent,token);
3828 graphic_context[n]->dash_pattern=(
double *)
3829 AcquireQuantumMemory((
size_t) (2*x+2),
3830 sizeof(*graphic_context[n]->dash_pattern));
3831 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3833 (void) ThrowMagickException(exception,GetMagickModule(),
3834 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3839 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3840 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3841 for (j=0; j < x; j++)
3843 (void) GetNextToken(q,&q,extent,token);
3844 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3845 ThrowPointExpectedException(token,exception);
3847 (void) GetNextToken(q,&q,extent,token);
3848 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3850 if (token == next_token)
3851 ThrowPointExpectedException(token,exception);
3852 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3855 if (((x & 0x01) != 0) && (j == x))
3856 for ( ; j < (2*x); j++)
3857 graphic_context[n]->dash_pattern[j]=
3858 graphic_context[n]->dash_pattern[j-x];
3859 graphic_context[n]->dash_pattern[j]=0.0;
3862 (void) GetNextToken(q,&q,extent,token);
3865 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3867 (void) GetNextToken(q,&q,extent,token);
3868 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3869 if (token == next_token)
3870 ThrowPointExpectedException(token,exception);
3873 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3878 (void) GetNextToken(q,&q,extent,token);
3879 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3885 graphic_context[n]->linecap=(LineCap) linecap;
3888 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3893 (void) GetNextToken(q,&q,extent,token);
3894 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3901 graphic_context[n]->linejoin=(LineJoin) linejoin;
3904 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3906 (void) GetNextToken(q,&q,extent,token);
3907 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3910 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3915 (void) GetNextToken(q,&q,extent,token);
3916 if (graphic_context[n]->clip_path != MagickFalse)
3918 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3919 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3921 if (token == next_token)
3922 ThrowPointExpectedException(token,exception);
3923 if (graphic_context[n]->compliance == SVGCompliance)
3924 graphic_context[n]->stroke_alpha*=opacity;
3926 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3927 if (graphic_context[n]->stroke.alpha != (
double) TransparentAlpha)
3928 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3930 graphic_context[n]->stroke.alpha=(MagickRealType)
3931 ClampToQuantum((
double) QuantumRange*opacity);
3932 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3935 if (LocaleCompare(
"stroke-width",keyword) == 0)
3937 (void) GetNextToken(q,&q,extent,token);
3938 if (graphic_context[n]->clip_path != MagickFalse)
3940 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3941 if ((token == next_token) ||
3942 (graphic_context[n]->stroke_width < 0.0))
3943 ThrowPointExpectedException(token,exception);
3952 if (LocaleCompare(
"text",keyword) == 0)
3954 primitive_type=TextPrimitive;
3958 if (LocaleCompare(
"text-align",keyword) == 0)
3963 (void) GetNextToken(q,&q,extent,token);
3964 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3970 graphic_context[n]->align=(AlignType) align;
3973 if (LocaleCompare(
"text-anchor",keyword) == 0)
3978 (void) GetNextToken(q,&q,extent,token);
3979 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3985 graphic_context[n]->align=(AlignType) align;
3988 if (LocaleCompare(
"text-antialias",keyword) == 0)
3990 (void) GetNextToken(q,&q,extent,token);
3991 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3992 MagickTrue : MagickFalse;
3995 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3997 (void) GetNextToken(q,&q,extent,token);
3998 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3999 &graphic_context[n]->undercolor,exception);
4002 if (LocaleCompare(
"translate",keyword) == 0)
4004 (void) GetNextToken(q,&q,extent,token);
4005 affine.tx=GetDrawValue(token,&next_token);
4006 if (token == next_token)
4007 ThrowPointExpectedException(token,exception);
4008 (void) GetNextToken(q,&q,extent,token);
4009 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4010 ThrowPointExpectedException(token,exception);
4012 (void) GetNextToken(q,&q,extent,token);
4013 affine.ty=GetDrawValue(token,&next_token);
4014 if (token == next_token)
4015 ThrowPointExpectedException(token,exception);
4025 if (LocaleCompare(
"use",keyword) == 0)
4033 (void) GetNextToken(q,&q,extent,token);
4034 use=(
const char *) GetValueFromSplayTree(macros,token);
4035 if (use != (
const char *) NULL)
4037 clone_info=CloneDrawInfo((
ImageInfo *) NULL,graphic_context[n]);
4038 (void) CloneString(&clone_info->primitive,use);
4039 status=RenderMVGContent(image,clone_info,depth+1,exception);
4040 clone_info=DestroyDrawInfo(clone_info);
4050 if (LocaleCompare(
"viewbox",keyword) == 0)
4052 (void) GetNextToken(q,&q,extent,token);
4053 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
4054 GetDrawValue(token,&next_token)-0.5));
4055 if (token == next_token)
4056 ThrowPointExpectedException(token,exception);
4057 (void) GetNextToken(q,&q,extent,token);
4058 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4059 ThrowPointExpectedException(token,exception);
4061 (void) GetNextToken(q,&q,extent,token);
4062 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
4063 ceil(GetDrawValue(token,&next_token)-0.5));
4064 if (token == next_token)
4065 ThrowPointExpectedException(token,exception);
4066 (void) GetNextToken(q,&q,extent,token);
4067 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4068 ThrowPointExpectedException(token,exception);
4070 (void) GetNextToken(q,&q,extent,token);
4071 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
4072 GetDrawValue(token,&next_token)+0.5));
4073 if (token == next_token)
4074 ThrowPointExpectedException(token,exception);
4075 (void) GetNextToken(q,&q,extent,token);
4076 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4077 ThrowPointExpectedException(token,exception);
4079 (void) GetNextToken(q,&q,extent,token);
4080 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
4081 GetDrawValue(token,&next_token)+0.5));
4082 if (token == next_token)
4083 ThrowPointExpectedException(token,exception);
4092 if (LocaleCompare(
"word-spacing",keyword) == 0)
4094 (void) GetNextToken(q,&q,extent,token);
4095 graphic_context[n]->interword_spacing=GetDrawValue(token,
4097 if (token == next_token)
4098 ThrowPointExpectedException(token,exception);
4110 if (status == MagickFalse)
4112 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4113 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4114 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4115 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4117 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4118 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4119 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4120 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4121 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4123 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4126 if (primitive_type == UndefinedPrimitive)
4130 if (number_stops > 1)
4135 type=LinearGradient;
4136 if (draw_info->gradient.type == RadialGradient)
4137 type=RadialGradient;
4138 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4141 if (number_stops > 0)
4142 stops=(
StopInfo *) RelinquishMagickMemory(stops);
4144 if ((draw_info->debug != MagickFalse) && (q > p))
4145 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(int)
4152 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4153 if (primitive_info[i].text != (
char *) NULL)
4154 primitive_info[i].text=DestroyString(primitive_info[i].text);
4158 primitive_info[0].primitive=primitive_type;
4159 primitive_info[0].point.x=0.0;
4160 primitive_info[0].point.y=0.0;
4161 primitive_info[0].coordinates=0;
4162 primitive_info[0].method=FloodfillMethod;
4163 primitive_info[0].closed_subpath=MagickFalse;
4164 for (x=0; *q !=
'\0'; x++)
4169 if (IsValidPoint(q) == MagickFalse)
4171 (void) GetNextToken(q,&q,extent,token);
4172 point.x=GetDrawValue(token,&next_token);
4173 if (token == next_token)
4174 ThrowPointExpectedException(token,exception);
4175 (void) GetNextToken(q,&q,extent,token);
4176 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4177 ThrowPointExpectedException(token,exception);
4179 (void) GetNextToken(q,&q,extent,token);
4180 point.y=GetDrawValue(token,&next_token);
4181 if (token == next_token)
4182 ThrowPointExpectedException(token,exception);
4183 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4185 (void) GetNextToken(q,&q,extent,token);
4186 primitive_info[i].primitive=primitive_type;
4187 primitive_info[i].point=point;
4188 primitive_info[i].coordinates=0;
4189 primitive_info[i].method=FloodfillMethod;
4190 primitive_info[i].closed_subpath=MagickFalse;
4193 if (i < (ssize_t) number_points)
4195 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4197 primitive_info=(*mvg_info.primitive_info);
4198 if (status == MagickFalse)
4201 if (status == MagickFalse)
4203 if (primitive_info[j].text != (
char *) NULL)
4204 primitive_info[j].text=DestroyString(primitive_info[j].text);
4205 primitive_info[j].primitive=primitive_type;
4206 primitive_info[j].coordinates=(size_t) x;
4207 primitive_info[j].method=FloodfillMethod;
4208 primitive_info[j].closed_subpath=MagickFalse;
4212 bounds.x1=primitive_info[j].point.x;
4213 bounds.y1=primitive_info[j].point.y;
4214 bounds.x2=primitive_info[j].point.x;
4215 bounds.y2=primitive_info[j].point.y;
4216 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4218 point=primitive_info[j+k].point;
4219 if (point.x < bounds.x1)
4221 if (point.y < bounds.y1)
4223 if (point.x > bounds.x2)
4225 if (point.y > bounds.y2)
4231 coordinates=(double) primitive_info[j].coordinates;
4232 switch (primitive_type)
4234 case RectanglePrimitive:
4239 case RoundRectanglePrimitive:
4246 alpha=bounds.x2-bounds.x1;
4247 beta=bounds.y2-bounds.y1;
4248 radius=hypot(alpha,beta);
4250 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4251 BezierQuantum+360.0;
4254 case BezierPrimitive:
4256 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4265 (void) GetNextToken(q,&q,extent,token);
4268 for (s=token; *s !=
'\0'; s=t)
4273 value=GetDrawValue(s,&t);
4282 for (s=token; *s !=
'\0'; s++)
4283 if (strspn(s,
"AaCcQqSsTt") != 0)
4284 coordinates+=(20.0*BezierQuantum)+360.0;
4290 if (status == MagickFalse)
4292 if (((
size_t) (i+coordinates)) >= number_points)
4297 number_points+=(size_t) coordinates+1;
4298 if (number_points < (
size_t) coordinates)
4300 (void) ThrowMagickException(exception,GetMagickModule(),
4301 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4307 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4309 primitive_info=(*mvg_info.primitive_info);
4310 if (status == MagickFalse)
4313 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4314 PrimitiveExtentPad);
4315 primitive_info=(*mvg_info.primitive_info);
4316 if (status == MagickFalse)
4319 switch (primitive_type)
4321 case PointPrimitive:
4324 if (primitive_info[j].coordinates != 1)
4329 status&=(MagickStatusType) TracePoint(primitive_info+j,
4330 primitive_info[j].point);
4331 primitive_info=(*mvg_info.primitive_info);
4332 if (status == MagickFalse)
4334 i=j+(ssize_t) primitive_info[j].coordinates;
4339 if (primitive_info[j].coordinates != 2)
4344 status&=(MagickStatusType) TraceLine(primitive_info+j,
4345 primitive_info[j].point,primitive_info[j+1].point);
4346 primitive_info=(*mvg_info.primitive_info);
4347 if (status == MagickFalse)
4349 i=j+(ssize_t) primitive_info[j].coordinates;
4352 case RectanglePrimitive:
4354 if (primitive_info[j].coordinates != 2)
4359 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4360 primitive_info[j].point,primitive_info[j+1].point);
4361 primitive_info=(*mvg_info.primitive_info);
4362 if (status == MagickFalse)
4364 i=j+(ssize_t) primitive_info[j].coordinates;
4367 case RoundRectanglePrimitive:
4369 if (primitive_info[j].coordinates != 3)
4374 if ((primitive_info[j+2].point.x < 0.0) ||
4375 (primitive_info[j+2].point.y < 0.0))
4380 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4385 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4390 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4391 primitive_info[j].point,primitive_info[j+1].point,
4392 primitive_info[j+2].point);
4393 primitive_info=(*mvg_info.primitive_info);
4394 if (status == MagickFalse)
4396 i=j+(ssize_t) primitive_info[j].coordinates;
4401 if (primitive_info[j].coordinates != 3)
4406 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4407 primitive_info[j+1].point,primitive_info[j+2].point);
4408 primitive_info=(*mvg_info.primitive_info);
4409 if (status == MagickFalse)
4411 i=j+(ssize_t) primitive_info[j].coordinates;
4414 case EllipsePrimitive:
4416 if (primitive_info[j].coordinates != 3)
4421 if ((primitive_info[j+1].point.x < 0.0) ||
4422 (primitive_info[j+1].point.y < 0.0))
4427 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4428 primitive_info[j].point,primitive_info[j+1].point,
4429 primitive_info[j+2].point);
4430 primitive_info=(*mvg_info.primitive_info);
4431 if (status == MagickFalse)
4433 i=j+(ssize_t) primitive_info[j].coordinates;
4436 case CirclePrimitive:
4438 if (primitive_info[j].coordinates != 2)
4443 status&=(MagickStatusType) TraceCircle(&mvg_info,
4444 primitive_info[j].point,primitive_info[j+1].point);
4445 primitive_info=(*mvg_info.primitive_info);
4446 if (status == MagickFalse)
4448 i=j+(ssize_t) primitive_info[j].coordinates;
4451 case PolylinePrimitive:
4453 if (primitive_info[j].coordinates < 1)
4460 case PolygonPrimitive:
4462 if (primitive_info[j].coordinates < 3)
4467 primitive_info[i]=primitive_info[j];
4468 primitive_info[i].coordinates=0;
4469 primitive_info[j].coordinates++;
4470 primitive_info[j].closed_subpath=MagickTrue;
4474 case BezierPrimitive:
4476 if (primitive_info[j].coordinates < 3)
4481 status&=(MagickStatusType) TraceBezier(&mvg_info,
4482 primitive_info[j].coordinates);
4483 primitive_info=(*mvg_info.primitive_info);
4484 if (status == MagickFalse)
4486 i=j+(ssize_t) primitive_info[j].coordinates;
4491 coordinates=(double) TracePath(&mvg_info,token,exception);
4492 primitive_info=(*mvg_info.primitive_info);
4493 if (status == MagickFalse)
4495 if (coordinates < 0.0)
4500 i=(ssize_t) (j+coordinates);
4503 case AlphaPrimitive:
4504 case ColorPrimitive:
4509 if (primitive_info[j].coordinates != 1)
4514 (void) GetNextToken(q,&q,extent,token);
4515 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4521 primitive_info[j].method=(PaintMethod) method;
4526 if (primitive_info[j].coordinates != 1)
4532 (void) GetNextToken(q,&q,extent,token);
4533 (void) CloneString(&primitive_info[j].text,token);
4537 clone_info=CloneDrawInfo((
ImageInfo *) NULL,graphic_context[n]);
4538 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4539 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4541 mvg_info.point=primitive_info->point;
4542 primitive_info->point.x+=cursor;
4546 mvg_info.point=primitive_info->point;
4549 clone_info->render=MagickFalse;
4550 clone_info->text=AcquireString(token);
4551 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4552 &metrics,exception);
4553 clone_info=DestroyDrawInfo(clone_info);
4554 cursor+=metrics.width;
4555 if (graphic_context[n]->compliance != SVGCompliance)
4559 case ImagePrimitive:
4561 if (primitive_info[j].coordinates != 2)
4566 (void) GetNextToken(q,&q,extent,token);
4567 (void) CloneString(&primitive_info[j].text,token);
4574 primitive_info[i].primitive=UndefinedPrimitive;
4575 if ((draw_info->debug != MagickFalse) && (q > p))
4576 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(int) (q-p),p);
4580 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4581 &graphic_context[n]->affine));
4582 primitive_info=(*mvg_info.primitive_info);
4585 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4586 graphic_context[n]->stroke_width);
4587 primitive_info=(*mvg_info.primitive_info);
4595 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4597 point=primitive_info[i].point;
4598 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4599 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4600 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4601 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4602 point=primitive_info[i].point;
4603 if (point.x < graphic_context[n]->bounds.x1)
4604 graphic_context[n]->bounds.x1=point.x;
4605 if (point.y < graphic_context[n]->bounds.y1)
4606 graphic_context[n]->bounds.y1=point.y;
4607 if (point.x > graphic_context[n]->bounds.x2)
4608 graphic_context[n]->bounds.x2=point.x;
4609 if (point.y > graphic_context[n]->bounds.y2)
4610 graphic_context[n]->bounds.y2=point.y;
4611 if (primitive_info[i].primitive == ImagePrimitive)
4613 if (i >= (ssize_t) number_points)
4614 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4616 if (graphic_context[n]->render != MagickFalse)
4618 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4619 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4620 (LocaleCompare(graphic_context[n]->clip_mask,
4621 graphic_context[n-1]->clip_mask) != 0))
4626 clip_path=(
const char *) GetValueFromSplayTree(macros,
4627 graphic_context[n]->clip_mask);
4628 if (clip_path != (
const char *) NULL)
4629 (
void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4631 status&=(MagickStatusType) DrawClipPath(image,graphic_context[n],
4632 graphic_context[n]->clip_mask,exception);
4634 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4635 primitive_info,exception);
4637 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4639 if (proceed == MagickFalse)
4644 if (draw_info->debug != MagickFalse)
4645 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4649 macros=DestroySplayTree(macros);
4650 token=DestroyString(token);
4653 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4654 if (primitive_info[i].text != (
char *) NULL)
4655 primitive_info[i].text=DestroyString(primitive_info[i].text);
4656 primitive_info=(
PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4658 primitive=DestroyString(primitive);
4660 stops=(
StopInfo *) RelinquishMagickMemory(stops);
4661 for ( ; n >= 0; n--)
4662 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4663 graphic_context=(
DrawInfo **) RelinquishMagickMemory(graphic_context);
4664 if ((status == MagickFalse) && (exception->severity < ErrorException))
4665 ThrowBinaryException(DrawError,
"NonconformingDrawingPrimitiveDefinition",
4667 return(status != 0 ? MagickTrue : MagickFalse);
4670MagickExport MagickBooleanType DrawImage(
Image *image,
const DrawInfo *draw_info,
4673 return(RenderMVGContent(image,draw_info,0,exception));
4707MagickExport MagickBooleanType DrawPatternPath(
Image *image,
4712 property[MagickPathExtent];
4728 assert(image != (
Image *) NULL);
4729 assert(image->signature == MagickCoreSignature);
4730 assert(draw_info != (
const DrawInfo *) NULL);
4731 assert(name != (
const char *) NULL);
4732 if (IsEventLogging() != MagickFalse)
4733 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4734 (void) FormatLocaleString(property,MagickPathExtent,
"%s",name);
4735 path=GetImageArtifact(image,property);
4736 if (path == (
const char *) NULL)
4737 return(MagickFalse);
4738 (void) FormatLocaleString(property,MagickPathExtent,
"%s-geometry",name);
4739 geometry=GetImageArtifact(image,property);
4740 if (geometry == (
const char *) NULL)
4741 return(MagickFalse);
4742 if ((*pattern) != (
Image *) NULL)
4743 *pattern=DestroyImage(*pattern);
4744 image_info=AcquireImageInfo();
4745 image_info->size=AcquireString(geometry);
4746 *pattern=AcquireImage(image_info,exception);
4747 image_info=DestroyImageInfo(image_info);
4748 (void) QueryColorCompliance(
"#00000000",AllCompliance,
4749 &(*pattern)->background_color,exception);
4750 (void) SetImageBackgroundColor(*pattern,exception);
4751 if (draw_info->debug != MagickFalse)
4752 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4753 "begin pattern-path %s %s",name,geometry);
4754 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
4755 if (clone_info->fill_pattern != (
Image *) NULL)
4756 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4757 if (clone_info->stroke_pattern != (
Image *) NULL)
4758 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4759 (void) FormatLocaleString(property,MagickPathExtent,
"%s-type",name);
4760 type=GetImageArtifact(image,property);
4761 if (type != (
const char *) NULL)
4762 clone_info->gradient.type=(GradientType) ParseCommandOption(
4763 MagickGradientOptions,MagickFalse,type);
4764 (void) CloneString(&clone_info->primitive,path);
4765 status=RenderMVGContent(*pattern,clone_info,0,exception);
4766 clone_info=DestroyDrawInfo(clone_info);
4767 if (draw_info->debug != MagickFalse)
4768 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end pattern-path");
4809 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4811 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4812 polygon_info=(
PolygonInfo **) RelinquishMagickMemory(polygon_info);
4813 return(polygon_info);
4820 *magick_restrict path_info;
4828 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4829 polygon_info=(
PolygonInfo **) AcquireQuantumMemory(number_threads,
4830 sizeof(*polygon_info));
4833 (void) ThrowMagickException(exception,GetMagickModule(),
4834 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4837 (void) memset(polygon_info,0,number_threads*
sizeof(*polygon_info));
4838 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4839 if (path_info == (
PathInfo *) NULL)
4840 return(DestroyPolygonTLS(polygon_info));
4841 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4844 (void) ThrowMagickException(exception,GetMagickModule(),
4845 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4846 return(DestroyPolygonTLS(polygon_info));
4848 path_info=(
PathInfo *) RelinquishMagickMemory(path_info);
4849 return(polygon_info);
4852static MagickBooleanType ClonePolygonEdgesTLS(
PolygonInfo **polygon_info,
4858 for (i=1; i < (ssize_t) number_threads; i++)
4866 polygon_info[i]=(
PolygonInfo *) AcquireMagickMemory(
4867 sizeof(*polygon_info[i]));
4870 (void) ThrowMagickException(exception,GetMagickModule(),
4871 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4872 return(MagickFalse);
4874 polygon_info[i]->number_edges=0;
4875 edge_info=polygon_info[0]->edges;
4876 polygon_info[i]->edges=(
EdgeInfo *) AcquireQuantumMemory(
4877 polygon_info[0]->number_edges,
sizeof(*edge_info));
4878 if (polygon_info[i]->edges == (
EdgeInfo *) NULL)
4880 (void) ThrowMagickException(exception,GetMagickModule(),
4881 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4882 return(MagickFalse);
4884 (void) memcpy(polygon_info[i]->edges,edge_info,
4885 polygon_info[0]->number_edges*
sizeof(*edge_info));
4886 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4887 polygon_info[i]->edges[j].points=(
PointInfo *) NULL;
4888 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4889 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4891 edge_info=polygon_info[0]->edges+j;
4892 polygon_info[i]->edges[j].points=(
PointInfo *) AcquireQuantumMemory(
4893 edge_info->number_points,
sizeof(*edge_info));
4894 if (polygon_info[i]->edges[j].points == (
PointInfo *) NULL)
4896 (void) ThrowMagickException(exception,GetMagickModule(),
4897 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4898 return(MagickFalse);
4900 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4901 edge_info->number_points*
sizeof(*edge_info->points));
4907static size_t DestroyEdge(
PolygonInfo *polygon_info,
const ssize_t edge)
4909 assert(edge < (ssize_t) polygon_info->number_edges);
4910 polygon_info->edges[edge].points=(
PointInfo *) RelinquishMagickMemory(
4911 polygon_info->edges[edge].points);
4912 polygon_info->number_edges--;
4913 if (edge < (ssize_t) polygon_info->number_edges)
4914 (
void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4915 (polygon_info->number_edges-(
size_t) edge)*
sizeof(*polygon_info->edges));
4916 return(polygon_info->number_edges);
4919static double GetFillAlpha(
PolygonInfo *polygon_info,
const double mid,
4920 const MagickBooleanType fill,
const FillRule fill_rule,
const ssize_t x,
4921 const ssize_t y,
double *stroke_alpha)
4948 p=polygon_info->edges;
4949 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4951 if ((
double) y <= (p->bounds.y1-mid-0.5))
4953 if ((
double) y > (p->bounds.y2+mid+0.5))
4956 (void) DestroyEdge(polygon_info,j--);
4959 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4960 ((double) x > (p->bounds.x2+mid+0.5)))
4962 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4963 for ( ; i < (ssize_t) p->number_points; i++)
4965 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4967 if ((
double) y > (p->points[i].y+mid+0.5))
4969 if (p->scanline != (
double) y)
4971 p->scanline=(double) y;
4972 p->highwater=(size_t) i;
4978 delta.x=(q+1)->x-q->x;
4979 delta.y=(q+1)->y-q->y;
4980 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4986 delta.x=(double) x-q->x;
4987 delta.y=(double) y-q->y;
4988 distance=delta.x*delta.x+delta.y*delta.y;
4992 alpha=delta.x*delta.x+delta.y*delta.y;
4998 delta.x=(double) x-(q+1)->x;
4999 delta.y=(double) y-(q+1)->y;
5000 distance=delta.x*delta.x+delta.y*delta.y;
5007 alpha=MagickSafeReciprocal(alpha);
5008 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
5009 distance=alpha*beta*beta;
5016 if (p->ghostline == MagickFalse)
5019 if ((*stroke_alpha < 1.0) &&
5020 (distance <= ((alpha+0.25)*(alpha+0.25))))
5023 if (distance <= ((alpha+0.25)*(alpha+0.25)))
5028 if (fabs(distance-1.0) >= MagickEpsilon)
5029 beta=sqrt((
double) distance);
5031 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
5032 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
5036 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
5038 if (distance <= 0.0)
5045 if (fabs(beta) < MagickEpsilon)
5048 if (fabs(distance-1.0) >= MagickEpsilon)
5049 beta=sqrt(distance);
5052 if (subpath_alpha < (alpha*alpha))
5053 subpath_alpha=alpha*alpha;
5059 if (fill == MagickFalse)
5061 if (subpath_alpha >= 1.0)
5067 p=polygon_info->edges;
5068 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
5070 if ((
double) y <= p->bounds.y1)
5072 if (((
double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
5074 if ((
double) x > p->bounds.x2)
5076 winding_number+=p->direction != 0 ? 1 : -1;
5079 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
5080 for ( ; i < (ssize_t) (p->number_points-1); i++)
5081 if ((
double) y <= p->points[i].y)
5084 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
5085 winding_number+=p->direction != 0 ? 1 : -1;
5087 if (fill_rule != NonZeroRule)
5089 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
5093 if (MagickAbsoluteValue(winding_number) != 0)
5095 return(subpath_alpha);
5098static MagickBooleanType DrawPolygonPrimitive(
Image *image,
5102 typedef struct _ExtentInfo
5131 **magick_restrict polygon_info;
5143 assert(image != (
Image *) NULL);
5144 assert(image->signature == MagickCoreSignature);
5145 assert(draw_info != (
DrawInfo *) NULL);
5146 assert(draw_info->signature == MagickCoreSignature);
5148 if (IsEventLogging() != MagickFalse)
5149 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5150 if (primitive_info->coordinates <= 1)
5155 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5157 return(MagickFalse);
5158 if (draw_info->debug != MagickFalse)
5159 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5160 fill=(primitive_info->method == FillToBorderMethod) ||
5161 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5162 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5163 bounds=polygon_info[0]->edges[0].bounds;
5164 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5165 if (IsStringTrue(artifact) != MagickFalse)
5166 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5167 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5169 p=polygon_info[0]->edges+i;
5170 if (p->bounds.x1 < bounds.x1)
5171 bounds.x1=p->bounds.x1;
5172 if (p->bounds.y1 < bounds.y1)
5173 bounds.y1=p->bounds.y1;
5174 if (p->bounds.x2 > bounds.x2)
5175 bounds.x2=p->bounds.x2;
5176 if (p->bounds.y2 > bounds.y2)
5177 bounds.y2=p->bounds.y2;
5179 bounds.x1-=(mid+1.0);
5180 bounds.y1-=(mid+1.0);
5181 bounds.x2+=(mid+1.0);
5182 bounds.y2+=(mid+1.0);
5183 if ((bounds.x1 >= (
double) image->columns) ||
5184 (bounds.y1 >= (
double) image->rows) ||
5185 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5187 polygon_info=DestroyPolygonTLS(polygon_info);
5190 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5191 (
double) image->columns-1.0 : bounds.x1;
5192 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5193 (
double) image->rows-1.0 : bounds.y1;
5194 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5195 (
double) image->columns-1.0 : bounds.x2;
5196 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5197 (
double) image->rows-1.0 : bounds.y2;
5198 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5199 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5200 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5201 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5202 number_threads=(size_t) GetMagickNumberThreads(image,image,(
size_t)
5203 (poly_extent.y2-poly_extent.y1+1),1);
5204 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5205 if (status == MagickFalse)
5207 polygon_info=DestroyPolygonTLS(polygon_info);
5210 image_view=AcquireAuthenticCacheView(image,exception);
5211 if ((primitive_info->coordinates == 1) ||
5212 (polygon_info[0]->number_edges == 0))
5217#if defined(MAGICKCORE_OPENMP_SUPPORT)
5218 #pragma omp parallel for schedule(static) shared(status) \
5219 num_threads((int) number_threads)
5221 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5232 if (status == MagickFalse)
5235 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5237 if (q == (Quantum *) NULL)
5242 GetPixelInfo(image,&pixel);
5243 for ( ; x <= poly_extent.x2; x++)
5245 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5246 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5248 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5250 SetPixelViaPixelInfo(image,&pixel,q);
5252 q+=(ptrdiff_t) GetPixelChannels(image);
5254 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5257 image_view=DestroyCacheView(image_view);
5258 polygon_info=DestroyPolygonTLS(polygon_info);
5259 if (draw_info->debug != MagickFalse)
5260 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5261 " end draw-polygon");
5267#if defined(MAGICKCORE_OPENMP_SUPPORT)
5268 #pragma omp parallel for schedule(static) shared(status) \
5269 num_threads((int) number_threads)
5271 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5274 id = GetOpenMPThreadId();
5282 if (status == MagickFalse)
5284 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5285 (poly_extent.x2-poly_extent.x1+1),1,exception);
5286 if (q == (Quantum *) NULL)
5291 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5304 fill_alpha=GetFillAlpha(polygon_info[
id],mid,fill,draw_info->fill_rule,
5306 if (draw_info->stroke_antialias == MagickFalse)
5308 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5309 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5311 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5313 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5314 (
double) GetPixelAlpha(image,q),q);
5315 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5317 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5318 (
double) GetPixelAlpha(image,q),q);
5319 q+=(ptrdiff_t) GetPixelChannels(image);
5321 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5324 image_view=DestroyCacheView(image_view);
5325 polygon_info=DestroyPolygonTLS(polygon_info);
5326 if (draw_info->debug != MagickFalse)
5327 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5360static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5386 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5387 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5388 switch (primitive_info->primitive)
5390 case AlphaPrimitive:
5392 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5393 "AlphaPrimitive %.20g,%.20g %s",(double) x,(
double) y,
5394 methods[primitive_info->method]);
5397 case ColorPrimitive:
5399 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5400 "ColorPrimitive %.20g,%.20g %s",(double) x,(
double) y,
5401 methods[primitive_info->method]);
5404 case ImagePrimitive:
5406 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5407 "ImagePrimitive %.20g,%.20g",(double) x,(
double) y);
5410 case PointPrimitive:
5412 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5413 "PointPrimitive %.20g,%.20g %s",(double) x,(
double) y,
5414 methods[primitive_info->method]);
5419 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5420 "TextPrimitive %.20g,%.20g",(double) x,(
double) y);
5427 p=primitive_info[0].point;
5430 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5432 point=primitive_info[i].point;
5433 if (coordinates <= 0)
5435 coordinates=(ssize_t) primitive_info[i].coordinates;
5436 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5437 " begin open (%.20g)",(double) coordinates);
5440 point=primitive_info[i].point;
5441 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5442 (fabs(q.y-point.y) >= MagickEpsilon))
5443 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5444 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5446 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5447 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5450 if (coordinates > 0)
5452 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5453 (fabs(p.y-point.y) >= MagickEpsilon))
5454 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5455 (double) coordinates);
5457 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5458 (
double) coordinates);
5462MagickExport MagickBooleanType DrawPrimitive(
Image *image,
5479 if (draw_info->debug != MagickFalse)
5481 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5482 " begin draw-primitive");
5483 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5484 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5485 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5486 draw_info->affine.tx,draw_info->affine.ty);
5489 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5490 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5491 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5492 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5494 if (draw_info->compliance == SVGCompliance)
5496 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5497 draw_info->clipping_mask,exception);
5498 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5499 draw_info->composite_mask,exception);
5501 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5502 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5503 image_view=AcquireAuthenticCacheView(image,exception);
5504 switch (primitive_info->primitive)
5506 case AlphaPrimitive:
5508 if ((image->alpha_trait & BlendPixelTrait) == 0)
5509 status&=(MagickStatusType) SetImageAlphaChannel(image,
5510 OpaqueAlphaChannel,exception);
5511 switch (primitive_info->method)
5522 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5523 if (q == (Quantum *) NULL)
5525 GetFillColor(draw_info,x,y,&pixel,exception);
5526 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5527 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5537 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5538 x,y,&target,exception);
5539 GetPixelInfo(image,&pixel);
5540 for (y=0; y < (ssize_t) image->rows; y++)
5545 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5547 if (q == (Quantum *) NULL)
5549 for (x=0; x < (ssize_t) image->columns; x++)
5551 GetPixelInfoPixel(image,q,&pixel);
5552 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5554 q+=(ptrdiff_t) GetPixelChannels(image);
5557 GetFillColor(draw_info,x,y,&pixel,exception);
5558 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5559 q+=(ptrdiff_t) GetPixelChannels(image);
5561 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5563 if (status == MagickFalse)
5568 case FloodfillMethod:
5569 case FillToBorderMethod:
5577 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5578 TileVirtualPixelMethod,x,y,&target,exception);
5579 if (primitive_info->method == FillToBorderMethod)
5581 target.red=(double) draw_info->border_color.red;
5582 target.green=(double) draw_info->border_color.green;
5583 target.blue=(double) draw_info->border_color.blue;
5585 channel_mask=SetImageChannelMask(image,AlphaChannel);
5586 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5587 &target,x,y,primitive_info->method == FloodfillMethod ?
5588 MagickFalse : MagickTrue,exception);
5589 (void) SetImageChannelMask(image,channel_mask);
5597 for (y=0; y < (ssize_t) image->rows; y++)
5602 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5604 if (q == (Quantum *) NULL)
5606 for (x=0; x < (ssize_t) image->columns; x++)
5608 GetFillColor(draw_info,x,y,&pixel,exception);
5609 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5610 q+=(ptrdiff_t) GetPixelChannels(image);
5612 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5614 if (status == MagickFalse)
5622 case ColorPrimitive:
5624 switch (primitive_info->method)
5635 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5636 if (q == (Quantum *) NULL)
5638 GetPixelInfo(image,&pixel);
5639 GetFillColor(draw_info,x,y,&pixel,exception);
5640 SetPixelViaPixelInfo(image,&pixel,q);
5641 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5651 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5652 x,y,&target,exception);
5653 for (y=0; y < (ssize_t) image->rows; y++)
5658 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5660 if (q == (Quantum *) NULL)
5662 for (x=0; x < (ssize_t) image->columns; x++)
5664 GetPixelInfoPixel(image,q,&pixel);
5665 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5667 q+=(ptrdiff_t) GetPixelChannels(image);
5670 GetFillColor(draw_info,x,y,&pixel,exception);
5671 SetPixelViaPixelInfo(image,&pixel,q);
5672 q+=(ptrdiff_t) GetPixelChannels(image);
5674 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5676 if (status == MagickFalse)
5681 case FloodfillMethod:
5682 case FillToBorderMethod:
5687 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5688 TileVirtualPixelMethod,x,y,&target,exception);
5689 if (primitive_info->method == FillToBorderMethod)
5691 target.red=(double) draw_info->border_color.red;
5692 target.green=(double) draw_info->border_color.green;
5693 target.blue=(double) draw_info->border_color.blue;
5695 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5696 &target,x,y,primitive_info->method == FloodfillMethod ?
5697 MagickFalse : MagickTrue,exception);
5705 GetPixelInfo(image,&pixel);
5706 for (y=0; y < (ssize_t) image->rows; y++)
5711 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5713 if (q == (Quantum *) NULL)
5715 for (x=0; x < (ssize_t) image->columns; x++)
5717 GetFillColor(draw_info,x,y,&pixel,exception);
5718 SetPixelViaPixelInfo(image,&pixel,q);
5719 q+=(ptrdiff_t) GetPixelChannels(image);
5721 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5723 if (status == MagickFalse)
5731 case ImagePrimitive:
5737 composite_geometry[MagickPathExtent],
5738 magic[MagickPathExtent] = {
'\0'};
5754 if (primitive_info->text == (
char *) NULL)
5756 clone_info=AcquireImageInfo();
5757 composite_images=(
Image *) NULL;
5758 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5759 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5762 if (*primitive_info->text !=
'\0')
5767 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5769 (void) SetImageInfo(clone_info,1,exception);
5770 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5772 if (clone_info->size != (
char *) NULL)
5773 clone_info->size=DestroyString(clone_info->size);
5774 if (clone_info->extract != (
char *) NULL)
5775 clone_info->extract=DestroyString(clone_info->extract);
5776 GetPathComponent(clone_info->filename,MagickPath,magic);
5778 composite_images=ReadImage(clone_info,exception);
5780 (
void) ThrowMagickException(exception,GetMagickModule(),
5781 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5783 clone_info=DestroyImageInfo(clone_info);
5784 if (composite_images == (
Image *) NULL)
5789 composite_image=RemoveFirstImageFromList(&composite_images);
5790 composite_images=DestroyImageList(composite_images);
5791 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5792 NULL,(
void *) NULL);
5793 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5794 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5795 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5796 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5801 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5802 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5803 composite_image->filter=image->filter;
5804 status&=(MagickStatusType) TransformImage(&composite_image,
5805 (
char *) NULL,composite_geometry,exception);
5807 if (composite_image->alpha_trait == UndefinedPixelTrait)
5808 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5809 OpaqueAlphaChannel,exception);
5810 if (draw_info->alpha != OpaqueAlpha)
5811 status&=(MagickStatusType) SetImageAlpha(composite_image,
5812 draw_info->alpha,exception);
5813 SetGeometry(image,&geometry);
5814 image->gravity=draw_info->gravity;
5817 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5818 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(double)
5819 composite_image->rows,(
double) geometry.x,(double) geometry.y);
5820 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5821 affine=draw_info->affine;
5822 affine.tx=(double) geometry.x;
5823 affine.ty=(double) geometry.y;
5824 composite_image->interpolate=image->interpolate;
5825 if ((draw_info->compose == OverCompositeOp) ||
5826 (draw_info->compose == SrcOverCompositeOp))
5827 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5830 status&=(MagickStatusType) CompositeImage(image,composite_image,
5831 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5832 composite_image=DestroyImage(composite_image);
5835 case PointPrimitive:
5843 if ((y < 0) || (y >= (ssize_t) image->rows))
5845 if ((x < 0) || (x >= (ssize_t) image->columns))
5847 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5848 if (q == (Quantum *) NULL)
5850 GetFillColor(draw_info,x,y,&fill_color,exception);
5851 CompositePixelOver(image,&fill_color,(
double) fill_color.alpha,q,(
double)
5852 GetPixelAlpha(image,q),q);
5853 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5860 geometry[MagickPathExtent];
5865 if (primitive_info->text == (
char *) NULL)
5867 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
5868 (void) CloneString(&clone_info->text,primitive_info->text);
5869 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
5870 primitive_info->point.x,primitive_info->point.y);
5871 (void) CloneString(&clone_info->geometry,geometry);
5872 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5873 clone_info=DestroyDrawInfo(clone_info);
5885 if (IsEventLogging() != MagickFalse)
5886 LogPrimitiveInfo(primitive_info);
5887 scale=ExpandAffine(&draw_info->affine);
5888 if ((draw_info->dash_pattern != (
double *) NULL) &&
5889 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5890 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5891 (draw_info->stroke.alpha != (
double) TransparentAlpha))
5896 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
5897 clone_info->stroke_width=0.0;
5898 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5899 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5900 primitive_info,exception);
5901 clone_info=DestroyDrawInfo(clone_info);
5902 if (status != MagickFalse)
5903 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5907 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5909 ((draw_info->stroke.alpha != (
double) TransparentAlpha) ||
5910 (draw_info->stroke_pattern != (
Image *) NULL)))
5922 closed_path=primitive_info[0].closed_subpath;
5923 i=(ssize_t) primitive_info[0].coordinates;
5924 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5925 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5926 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5927 closed_path=MagickTrue;
5928 if ((((draw_info->linecap == RoundCap) ||
5929 (closed_path != MagickFalse)) &&
5930 (draw_info->linejoin == RoundJoin)) ||
5931 (primitive_info[i].primitive != UndefinedPrimitive))
5933 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5934 primitive_info,exception);
5937 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
5938 clone_info->stroke_width=0.0;
5939 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5940 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5941 primitive_info,exception);
5942 clone_info=DestroyDrawInfo(clone_info);
5943 if (status != MagickFalse)
5944 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5945 primitive_info,exception);
5948 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5949 primitive_info,exception);
5953 image_view=DestroyCacheView(image_view);
5954 if (draw_info->compliance == SVGCompliance)
5956 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5957 (
Image *) NULL,exception);
5958 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5959 (
Image *) NULL,exception);
5961 if (draw_info->debug != MagickFalse)
5962 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5963 return(status != 0 ? MagickTrue : MagickFalse);
5996static MagickBooleanType DrawRoundLinecap(
Image *image,
6006 if (primitive_info->coordinates < 1)
6007 return(MagickFalse);
6008 for (i=0; i < 4; i++)
6009 linecap[i]=(*primitive_info);
6010 linecap[0].coordinates=4;
6011 linecap[1].point.x+=2.0*MagickEpsilon;
6012 linecap[2].point.x+=2.0*MagickEpsilon;
6013 linecap[2].point.y+=2.0*MagickEpsilon;
6014 linecap[3].point.y+=2.0*MagickEpsilon;
6015 linecap[4].primitive=UndefinedPrimitive;
6016 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
6019static MagickBooleanType DrawStrokePolygon(
Image *image,
6042 if (draw_info->debug != MagickFalse)
6043 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6044 " begin draw-stroke-polygon");
6045 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
6046 clone_info->fill=draw_info->stroke;
6047 if (clone_info->fill_pattern != (
Image *) NULL)
6048 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
6049 if (clone_info->stroke_pattern != (
Image *) NULL)
6050 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
6051 MagickTrue,exception);
6052 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
6053 clone_info->stroke_width=0.0;
6054 clone_info->fill_rule=NonZeroRule;
6056 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
6058 if (p->coordinates == 1)
6060 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
6066 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
6067 stroke_polygon,exception);
6068 stroke_polygon=(
PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
6071 q=p+p->coordinates-1;
6072 closed_path=p->closed_subpath;
6073 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
6075 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
6077 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
6081 clone_info=DestroyDrawInfo(clone_info);
6082 if (draw_info->debug != MagickFalse)
6083 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6084 " end draw-stroke-polygon");
6085 return(status != 0 ? MagickTrue : MagickFalse);
6111MagickExport
void GetAffineMatrix(
AffineMatrix *affine_matrix)
6113 if (IsEventLogging() != MagickFalse)
6114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6116 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
6117 affine_matrix->sx=1.0;
6118 affine_matrix->sy=1.0;
6159 assert(draw_info != (
DrawInfo *) NULL);
6160 if (IsEventLogging() != MagickFalse)
6161 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6162 (void) memset(draw_info,0,
sizeof(*draw_info));
6163 draw_info->image_info=CloneImageInfo(image_info);
6164 GetAffineMatrix(&draw_info->affine);
6165 exception=AcquireExceptionInfo();
6166 (void) QueryColorCompliance(
"#000F",AllCompliance,&draw_info->fill,
6168 (void) QueryColorCompliance(
"#FFF0",AllCompliance,&draw_info->stroke,
6170 draw_info->stroke_antialias=draw_info->image_info->antialias;
6171 draw_info->stroke_width=1.0;
6172 draw_info->fill_rule=EvenOddRule;
6173 draw_info->alpha=OpaqueAlpha;
6174 draw_info->fill_alpha=OpaqueAlpha;
6175 draw_info->stroke_alpha=OpaqueAlpha;
6176 draw_info->linecap=ButtCap;
6177 draw_info->linejoin=MiterJoin;
6178 draw_info->miterlimit=10;
6179 draw_info->decorate=NoDecoration;
6180 draw_info->pointsize=12.0;
6181 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6182 draw_info->compose=OverCompositeOp;
6183 draw_info->render=MagickTrue;
6184 draw_info->clip_path=MagickFalse;
6185 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6186 MagickTrue : MagickFalse;
6187 if (draw_info->image_info->font != (
char *) NULL)
6188 draw_info->font=AcquireString(draw_info->image_info->font);
6189 if (draw_info->image_info->density != (
char *) NULL)
6190 draw_info->density=AcquireString(draw_info->image_info->density);
6191 draw_info->text_antialias=draw_info->image_info->antialias;
6192 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6193 draw_info->pointsize=draw_info->image_info->pointsize;
6194 draw_info->border_color=draw_info->image_info->border_color;
6195 if (draw_info->image_info->server_name != (
char *) NULL)
6196 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6197 option=GetImageOption(draw_info->image_info,
"direction");
6198 if (option != (
const char *) NULL)
6199 draw_info->direction=(DirectionType) ParseCommandOption(
6200 MagickDirectionOptions,MagickFalse,option);
6202 draw_info->direction=UndefinedDirection;
6203 option=GetImageOption(draw_info->image_info,
"encoding");
6204 if (option != (
const char *) NULL)
6205 (void) CloneString(&draw_info->encoding,option);
6206 option=GetImageOption(draw_info->image_info,
"family");
6207 if (option != (
const char *) NULL)
6208 (void) CloneString(&draw_info->family,option);
6209 option=GetImageOption(draw_info->image_info,
"fill");
6210 if (option != (
const char *) NULL)
6211 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6213 option=GetImageOption(draw_info->image_info,
"gravity");
6214 if (option != (
const char *) NULL)
6215 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6216 MagickFalse,option);
6217 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6218 if (option != (
const char *) NULL)
6219 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6220 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6221 if (option != (
const char *) NULL)
6222 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6223 option=GetImageOption(draw_info->image_info,
"kerning");
6224 if (option != (
const char *) NULL)
6225 draw_info->kerning=GetDrawValue(option,&next_token);
6226 option=GetImageOption(draw_info->image_info,
"stroke");
6227 if (option != (
const char *) NULL)
6228 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6230 option=GetImageOption(draw_info->image_info,
"strokewidth");
6231 if (option != (
const char *) NULL)
6232 draw_info->stroke_width=GetDrawValue(option,&next_token);
6233 option=GetImageOption(draw_info->image_info,
"style");
6234 if (option != (
const char *) NULL)
6235 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6236 MagickFalse,option);
6237 option=GetImageOption(draw_info->image_info,
"undercolor");
6238 if (option != (
const char *) NULL)
6239 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6241 option=GetImageOption(draw_info->image_info,
"weight");
6242 if (option != (
const char *) NULL)
6247 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6249 weight=(ssize_t) StringToUnsignedLong(option);
6250 draw_info->weight=(size_t) weight;
6252 option=GetImageOption(draw_info->image_info,
"word-break");
6253 if (option != (
const char *) NULL)
6254 draw_info->word_break=(WordBreakType) ParseCommandOption(
6255 MagickWordBreakOptions,MagickFalse,option);
6256 exception=DestroyExceptionInfo(exception);
6257 draw_info->signature=MagickCoreSignature;
6285static inline double Permutate(
const ssize_t n,
const ssize_t k)
6294 for (i=k+1; i <= n; i++)
6296 for (i=1; i <= (n-k); i++)
6324 center.x=0.5*(end.x+start.x);
6325 center.y=0.5*(end.y+start.y);
6326 radius.x=fabs(center.x-start.x);
6327 radius.y=fabs(center.y-start.y);
6328 return(TraceEllipse(mvg_info,center,radius,degrees));
6331static MagickBooleanType TraceArcPath(
MVGInfo *mvg_info,
const PointInfo start,
6333 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6370 offset=mvg_info->offset;
6371 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6372 primitive_info->coordinates=0;
6373 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6374 (fabs(start.y-end.y) < MagickEpsilon))
6375 return(TracePoint(primitive_info,end));
6376 radii.x=fabs(arc.x);
6377 radii.y=fabs(arc.y);
6378 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6379 return(TraceLine(primitive_info,start,end));
6380 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6381 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6382 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6383 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6384 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6386 if (delta < MagickEpsilon)
6387 return(TraceLine(primitive_info,start,end));
6390 radii.x*=sqrt((
double) delta);
6391 radii.y*=sqrt((
double) delta);
6393 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6394 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6395 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6396 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6397 alpha=points[1].x-points[0].x;
6398 beta=points[1].y-points[0].y;
6399 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6400 return(TraceLine(primitive_info,start,end));
6401 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6406 factor=sqrt((
double) factor);
6407 if (sweep == large_arc)
6410 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6411 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6412 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6413 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6414 if ((theta < 0.0) && (sweep != MagickFalse))
6415 theta+=2.0*MagickPI;
6417 if ((theta > 0.0) && (sweep == MagickFalse))
6418 theta-=2.0*MagickPI;
6419 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((
double) (theta/(0.5*
6420 MagickPI+MagickEpsilon)))));
6423 for (i=0; i < (ssize_t) arc_segments; i++)
6425 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6426 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6427 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6428 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6429 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6430 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6431 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6432 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6433 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6434 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6435 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(double) (i+1)*
6436 theta/arc_segments),DegreesToRadians(360.0))));
6437 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(double) (i+1)*
6438 theta/arc_segments),DegreesToRadians(360.0))));
6439 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(double)
6440 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6441 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(double)
6442 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6443 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6444 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6445 (p+1)->point.x=(
double) (cosine*radii.x*points[0].x-sine*radii.y*
6447 (p+1)->point.y=(
double) (sine*radii.x*points[0].x+cosine*radii.y*
6449 (p+2)->point.x=(
double) (cosine*radii.x*points[1].x-sine*radii.y*
6451 (p+2)->point.y=(
double) (sine*radii.x*points[1].x+cosine*radii.y*
6453 (p+3)->point.x=(
double) (cosine*radii.x*points[2].x-sine*radii.y*
6455 (p+3)->point.y=(
double) (sine*radii.x*points[2].x+cosine*radii.y*
6457 if (i == (ssize_t) (arc_segments-1))
6459 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6462 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6463 mvg_info->offset+=(ssize_t) p->coordinates;
6464 p+=(ptrdiff_t) p->coordinates;
6467 return(MagickFalse);
6468 mvg_info->offset=offset;
6469 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6470 primitive_info->coordinates=(size_t) (p-primitive_info);
6471 primitive_info->closed_subpath=MagickFalse;
6472 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6474 p->primitive=primitive_info->primitive;
6480static MagickBooleanType TraceBezier(
MVGInfo *mvg_info,
6481 const size_t number_coordinates)
6510 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6511 quantum=number_coordinates;
6512 for (i=0; i < (ssize_t) number_coordinates; i++)
6514 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6516 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6517 if (alpha > (
double) GetMaxMemoryRequest())
6519 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6520 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6521 return(MagickFalse);
6523 if (alpha > (
double) quantum)
6524 quantum=(
size_t) alpha;
6525 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6526 if (alpha > (
double) quantum)
6527 quantum=(size_t) alpha;
6530 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6531 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6532 if (quantum > (
double) GetMaxMemoryRequest())
6534 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6535 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6536 return(MagickFalse);
6538 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6539 sizeof(*coefficients));
6540 points=(
PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6542 if ((coefficients == (
double *) NULL) || (points == (
PointInfo *) NULL))
6545 points=(
PointInfo *) RelinquishMagickMemory(points);
6546 if (coefficients != (
double *) NULL)
6547 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6548 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6549 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6550 return(MagickFalse);
6552 control_points=quantum*number_coordinates;
6553 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6555 points=(
PointInfo *) RelinquishMagickMemory(points);
6556 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6557 return(MagickFalse);
6559 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6563 end=primitive_info[number_coordinates-1].point;
6564 for (i=0; i < (ssize_t) number_coordinates; i++)
6565 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6567 for (i=0; i < (ssize_t) control_points; i++)
6572 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6573 for (j=0; j < (ssize_t) number_coordinates; j++)
6575 point.x+=alpha*coefficients[j]*p->point.x;
6576 point.y+=alpha*coefficients[j]*p->point.y;
6577 alpha*=weight/(1.0-weight);
6581 weight+=1.0/control_points;
6587 for (i=0; i < (ssize_t) control_points; i++)
6589 if (TracePoint(p,points[i]) == MagickFalse)
6591 points=(
PointInfo *) RelinquishMagickMemory(points);
6592 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6593 return(MagickFalse);
6595 p+=(ptrdiff_t) p->coordinates;
6597 if (TracePoint(p,end) == MagickFalse)
6599 points=(
PointInfo *) RelinquishMagickMemory(points);
6600 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6601 return(MagickFalse);
6603 p+=(ptrdiff_t) p->coordinates;
6604 primitive_info->coordinates=(size_t) (p-primitive_info);
6605 primitive_info->closed_subpath=MagickFalse;
6606 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6608 p->primitive=primitive_info->primitive;
6611 points=(
PointInfo *) RelinquishMagickMemory(points);
6612 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6616static MagickBooleanType TraceCircle(
MVGInfo *mvg_info,
const PointInfo start,
6628 alpha=end.x-start.x;
6630 radius=hypot((
double) alpha,(
double) beta);
6631 offset.x=(double) radius;
6632 offset.y=(double) radius;
6635 return(TraceEllipse(mvg_info,start,offset,degrees));
6638static MagickBooleanType TraceEllipse(
MVGInfo *mvg_info,
const PointInfo center,
6664 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6665 primitive_info->coordinates=0;
6666 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6668 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6669 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6670 angle.x=DegreesToRadians(arc.x);
6674 angle.y=DegreesToRadians(y);
6675 coordinates=ceil((angle.y-angle.x)/step+1.0);
6676 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6677 return(MagickFalse);
6679 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6680 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6682 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6683 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6684 if (i++ >= (ssize_t) coordinates)
6686 if (TracePoint(p,point) == MagickFalse)
6687 return(MagickFalse);
6688 p+=(ptrdiff_t) p->coordinates;
6690 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6691 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6692 if (TracePoint(p,point) == MagickFalse)
6693 return(MagickFalse);
6694 p+=(ptrdiff_t) p->coordinates;
6695 primitive_info->coordinates=(size_t) (p-primitive_info);
6696 primitive_info->closed_subpath=MagickFalse;
6697 x=fabs(primitive_info[0].point.x-
6698 primitive_info[primitive_info->coordinates-1].point.x);
6699 y=fabs(primitive_info[0].point.y-
6700 primitive_info[primitive_info->coordinates-1].point.y);
6701 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6702 primitive_info->closed_subpath=MagickTrue;
6703 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6705 p->primitive=primitive_info->primitive;
6711static MagickBooleanType TraceLine(
PrimitiveInfo *primitive_info,
6714 if (TracePoint(primitive_info,start) == MagickFalse)
6715 return(MagickFalse);
6716 if (TracePoint(primitive_info+1,end) == MagickFalse)
6717 return(MagickFalse);
6718 (primitive_info+1)->primitive=primitive_info->primitive;
6719 primitive_info->coordinates=2;
6720 primitive_info->closed_subpath=MagickFalse;
6724static ssize_t TracePath(
MVGInfo *mvg_info,
const char *path,
6729 token[MagickPathExtent] =
"";
6747 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6770 subpath_offset=mvg_info->offset;
6771 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6774 number_coordinates=0;
6776 primitive_type=primitive_info->primitive;
6778 for (p=path; *p !=
'\0'; )
6780 if (status == MagickFalse)
6782 while (isspace((
int) ((
unsigned char) *p)) != 0)
6786 last_attribute=attribute;
6787 attribute=(int) (*p++);
6797 large_arc = MagickFalse,
6798 sweep = MagickFalse;
6808 (void) GetNextToken(p,&p,MagickPathExtent,token);
6809 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6810 ThrowPointExpectedException(token,exception);
6812 (void) GetNextToken(p,&p,MagickPathExtent,token);
6813 arc.x=GetDrawValue(token,&next_token);
6814 if (token == next_token)
6815 ThrowPointExpectedException(token,exception);
6816 (void) GetNextToken(p,&p,MagickPathExtent,token);
6817 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6818 ThrowPointExpectedException(token,exception);
6820 (void) GetNextToken(p,&p,MagickPathExtent,token);
6821 arc.y=GetDrawValue(token,&next_token);
6822 if (token == next_token)
6823 ThrowPointExpectedException(token,exception);
6824 (void) GetNextToken(p,&p,MagickPathExtent,token);
6825 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6826 ThrowPointExpectedException(token,exception);
6828 (void) GetNextToken(p,&p,MagickPathExtent,token);
6829 angle=GetDrawValue(token,&next_token);
6830 if (token == next_token)
6831 ThrowPointExpectedException(token,exception);
6832 (void) GetNextToken(p,&p,MagickPathExtent,token);
6833 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6834 ThrowPointExpectedException(token,exception);
6836 (void) GetNextToken(p,&p,MagickPathExtent,token);
6837 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6838 (void) GetNextToken(p,&p,MagickPathExtent,token);
6839 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6840 ThrowPointExpectedException(token,exception);
6842 (void) GetNextToken(p,&p,MagickPathExtent,token);
6843 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6844 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6845 ThrowPointExpectedException(token,exception);
6847 (void) GetNextToken(p,&p,MagickPathExtent,token);
6848 (void) GetNextToken(p,&p,MagickPathExtent,token);
6849 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6850 ThrowPointExpectedException(token,exception);
6852 (void) GetNextToken(p,&p,MagickPathExtent,token);
6853 x=GetDrawValue(token,&next_token);
6854 if (token == next_token)
6855 ThrowPointExpectedException(token,exception);
6856 (void) GetNextToken(p,&p,MagickPathExtent,token);
6857 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6858 ThrowPointExpectedException(token,exception);
6860 (void) GetNextToken(p,&p,MagickPathExtent,token);
6861 y=GetDrawValue(token,&next_token);
6862 if (token == next_token)
6863 ThrowPointExpectedException(token,exception);
6864 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6865 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6866 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6868 q=(*mvg_info->primitive_info)+mvg_info->offset;
6869 mvg_info->offset+=(ssize_t) q->coordinates;
6870 q+=(ptrdiff_t) q->coordinates;
6872 while (isspace((
int) ((
unsigned char) *p)) != 0)
6876 }
while (IsValidPoint(p) != MagickFalse);
6888 for (i=1; i < 4; i++)
6890 (void) GetNextToken(p,&p,MagickPathExtent,token);
6891 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6892 ThrowPointExpectedException(token,exception);
6894 (void) GetNextToken(p,&p,MagickPathExtent,token);
6895 x=GetDrawValue(token,&next_token);
6896 if (token == next_token)
6897 ThrowPointExpectedException(token,exception);
6898 (void) GetNextToken(p,&p,MagickPathExtent,token);
6899 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6900 ThrowPointExpectedException(token,exception);
6902 (void) GetNextToken(p,&p,MagickPathExtent,token);
6903 y=GetDrawValue(token,&next_token);
6904 if (token == next_token)
6905 ThrowPointExpectedException(token,exception);
6906 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6907 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6910 for (i=0; i < 4; i++)
6911 (q+i)->point=points[i];
6912 if (TraceBezier(mvg_info,4) == MagickFalse)
6914 q=(*mvg_info->primitive_info)+mvg_info->offset;
6915 mvg_info->offset+=(ssize_t) q->coordinates;
6916 q+=(ptrdiff_t) q->coordinates;
6918 while (isspace((
int) ((
unsigned char) *p)) != 0)
6922 }
while (IsValidPoint(p) != MagickFalse);
6930 (void) GetNextToken(p,&p,MagickPathExtent,token);
6931 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6932 ThrowPointExpectedException(token,exception);
6934 (void) GetNextToken(p,&p,MagickPathExtent,token);
6935 x=GetDrawValue(token,&next_token);
6936 if (token == next_token)
6937 ThrowPointExpectedException(token,exception);
6938 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6939 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6941 q=(*mvg_info->primitive_info)+mvg_info->offset;
6942 if (TracePoint(q,point) == MagickFalse)
6944 mvg_info->offset+=(ssize_t) q->coordinates;
6945 q+=(ptrdiff_t) q->coordinates;
6946 while (isspace((
int) ((
unsigned char) *p)) != 0)
6950 }
while (IsValidPoint(p) != MagickFalse);
6961 (void) GetNextToken(p,&p,MagickPathExtent,token);
6962 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6963 ThrowPointExpectedException(token,exception);
6965 (void) GetNextToken(p,&p,MagickPathExtent,token);
6966 x=GetDrawValue(token,&next_token);
6967 if (token == next_token)
6968 ThrowPointExpectedException(token,exception);
6969 (void) GetNextToken(p,&p,MagickPathExtent,token);
6970 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6971 ThrowPointExpectedException(token,exception);
6973 (void) GetNextToken(p,&p,MagickPathExtent,token);
6974 y=GetDrawValue(token,&next_token);
6975 if (token == next_token)
6976 ThrowPointExpectedException(token,exception);
6977 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
6978 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
6979 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6981 q=(*mvg_info->primitive_info)+mvg_info->offset;
6982 if (TracePoint(q,point) == MagickFalse)
6984 mvg_info->offset+=(ssize_t) q->coordinates;
6985 q+=(ptrdiff_t) q->coordinates;
6986 while (isspace((
int) ((
unsigned char) *p)) != 0)
6990 }
while (IsValidPoint(p) != MagickFalse);
6999 if (mvg_info->offset != subpath_offset)
7001 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7002 primitive_info->coordinates=(size_t) (q-primitive_info);
7003 number_coordinates+=primitive_info->coordinates;
7005 subpath_offset=mvg_info->offset;
7010 (void) GetNextToken(p,&p,MagickPathExtent,token);
7011 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7012 ThrowPointExpectedException(token,exception);
7014 (void) GetNextToken(p,&p,MagickPathExtent,token);
7015 x=GetDrawValue(token,&next_token);
7016 if (token == next_token)
7017 ThrowPointExpectedException(token,exception);
7018 (void) GetNextToken(p,&p,MagickPathExtent,token);
7019 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7020 ThrowPointExpectedException(token,exception);
7022 (void) GetNextToken(p,&p,MagickPathExtent,token);
7023 y=GetDrawValue(token,&next_token);
7024 if (token == next_token)
7025 ThrowPointExpectedException(token,exception);
7026 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
7027 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
7031 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7033 q=(*mvg_info->primitive_info)+mvg_info->offset;
7034 if (TracePoint(q,point) == MagickFalse)
7036 mvg_info->offset+=(ssize_t) q->coordinates;
7037 q+=(ptrdiff_t) q->coordinates;
7038 while (isspace((
int) ((
unsigned char) *p)) != 0)
7042 }
while (IsValidPoint(p) != MagickFalse);
7054 for (i=1; i < 3; i++)
7056 (void) GetNextToken(p,&p,MagickPathExtent,token);
7057 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7058 ThrowPointExpectedException(token,exception);
7060 (void) GetNextToken(p,&p,MagickPathExtent,token);
7061 x=GetDrawValue(token,&next_token);
7062 if (token == next_token)
7063 ThrowPointExpectedException(token,exception);
7064 (void) GetNextToken(p,&p,MagickPathExtent,token);
7065 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7066 ThrowPointExpectedException(token,exception);
7068 (void) GetNextToken(p,&p,MagickPathExtent,token);
7069 y=GetDrawValue(token,&next_token);
7070 if (token == next_token)
7071 ThrowPointExpectedException(token,exception);
7074 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
7075 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
7078 for (i=0; i < 3; i++)
7079 (q+i)->point=points[i];
7080 if (TraceBezier(mvg_info,3) == MagickFalse)
7082 q=(*mvg_info->primitive_info)+mvg_info->offset;
7083 mvg_info->offset+=(ssize_t) q->coordinates;
7084 q+=(ptrdiff_t) q->coordinates;
7086 while (isspace((
int) ((
unsigned char) *p)) != 0)
7090 }
while (IsValidPoint(p) != MagickFalse);
7101 points[0]=points[3];
7102 points[1].x=2.0*points[3].x-points[2].x;
7103 points[1].y=2.0*points[3].y-points[2].y;
7104 for (i=2; i < 4; i++)
7106 (void) GetNextToken(p,&p,MagickPathExtent,token);
7107 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7108 ThrowPointExpectedException(token,exception);
7110 (void) GetNextToken(p,&p,MagickPathExtent,token);
7111 x=GetDrawValue(token,&next_token);
7112 if (token == next_token)
7113 ThrowPointExpectedException(token,exception);
7114 (void) GetNextToken(p,&p,MagickPathExtent,token);
7115 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7116 ThrowPointExpectedException(token,exception);
7118 (void) GetNextToken(p,&p,MagickPathExtent,token);
7119 y=GetDrawValue(token,&next_token);
7120 if (token == next_token)
7121 ThrowPointExpectedException(token,exception);
7124 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
7125 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
7128 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
7133 for (i=0; i < 4; i++)
7134 (q+i)->point=points[i];
7135 if (TraceBezier(mvg_info,4) == MagickFalse)
7137 q=(*mvg_info->primitive_info)+mvg_info->offset;
7138 mvg_info->offset+=(ssize_t) q->coordinates;
7139 q+=(ptrdiff_t) q->coordinates;
7141 last_attribute=attribute;
7142 while (isspace((
int) ((
unsigned char) *p)) != 0)
7146 }
while (IsValidPoint(p) != MagickFalse);
7157 points[0]=points[2];
7158 points[1].x=2.0*points[2].x-points[1].x;
7159 points[1].y=2.0*points[2].y-points[1].y;
7160 for (i=2; i < 3; i++)
7162 (void) GetNextToken(p,&p,MagickPathExtent,token);
7163 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7164 ThrowPointExpectedException(token,exception);
7166 (void) GetNextToken(p,&p,MagickPathExtent,token);
7167 x=GetDrawValue(token,&next_token);
7168 if (token == next_token)
7169 ThrowPointExpectedException(token,exception);
7170 (void) GetNextToken(p,&p,MagickPathExtent,token);
7171 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7172 ThrowPointExpectedException(token,exception);
7174 (void) GetNextToken(p,&p,MagickPathExtent,token);
7175 y=GetDrawValue(token,&next_token);
7176 if (token == next_token)
7177 ThrowPointExpectedException(token,exception);
7178 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7179 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7182 if (status == MagickFalse)
7184 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7189 for (i=0; i < 3; i++)
7190 (q+i)->point=points[i];
7191 if (TraceBezier(mvg_info,3) == MagickFalse)
7193 q=(*mvg_info->primitive_info)+mvg_info->offset;
7194 mvg_info->offset+=(ssize_t) q->coordinates;
7195 q+=(ptrdiff_t) q->coordinates;
7197 last_attribute=attribute;
7198 while (isspace((
int) ((
unsigned char) *p)) != 0)
7202 }
while (IsValidPoint(p) != MagickFalse);
7213 (void) GetNextToken(p,&p,MagickPathExtent,token);
7214 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7215 ThrowPointExpectedException(token,exception);
7217 (void) GetNextToken(p,&p,MagickPathExtent,token);
7218 y=GetDrawValue(token,&next_token);
7219 if (token == next_token)
7220 ThrowPointExpectedException(token,exception);
7221 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7222 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7224 q=(*mvg_info->primitive_info)+mvg_info->offset;
7225 if (TracePoint(q,point) == MagickFalse)
7227 mvg_info->offset+=(ssize_t) q->coordinates;
7228 q+=(ptrdiff_t) q->coordinates;
7229 while (isspace((
int) ((
unsigned char) *p)) != 0)
7233 }
while (IsValidPoint(p) != MagickFalse);
7243 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7245 q=(*mvg_info->primitive_info)+mvg_info->offset;
7246 if (TracePoint(q,point) == MagickFalse)
7248 mvg_info->offset+=(ssize_t) q->coordinates;
7249 q+=(ptrdiff_t) q->coordinates;
7250 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7251 primitive_info->coordinates=(size_t) (q-primitive_info);
7252 primitive_info->closed_subpath=MagickTrue;
7253 number_coordinates+=primitive_info->coordinates;
7255 subpath_offset=mvg_info->offset;
7261 ThrowPointExpectedException(token,exception);
7266 if (status == MagickFalse)
7268 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7269 primitive_info->coordinates=(size_t) (q-primitive_info);
7270 number_coordinates+=primitive_info->coordinates;
7271 for (i=0; i < (ssize_t) number_coordinates; i++)
7274 q->primitive=primitive_type;
7276 q->method=FillToBorderMethod;
7279 return((ssize_t) number_coordinates);
7282static MagickBooleanType TraceRectangle(
PrimitiveInfo *primitive_info,
7295 if (TracePoint(p,start) == MagickFalse)
7296 return(MagickFalse);
7297 p+=(ptrdiff_t) p->coordinates;
7300 if (TracePoint(p,point) == MagickFalse)
7301 return(MagickFalse);
7302 p+=(ptrdiff_t) p->coordinates;
7303 if (TracePoint(p,end) == MagickFalse)
7304 return(MagickFalse);
7305 p+=(ptrdiff_t) p->coordinates;
7308 if (TracePoint(p,point) == MagickFalse)
7309 return(MagickFalse);
7310 p+=(ptrdiff_t) p->coordinates;
7311 if (TracePoint(p,start) == MagickFalse)
7312 return(MagickFalse);
7313 p+=(ptrdiff_t) p->coordinates;
7314 primitive_info->coordinates=(size_t) (p-primitive_info);
7315 primitive_info->closed_subpath=MagickTrue;
7316 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7318 p->primitive=primitive_info->primitive;
7324static MagickBooleanType TraceRoundRectangle(
MVGInfo *mvg_info,
7344 offset=mvg_info->offset;
7345 segment.x=fabs(end.x-start.x);
7346 segment.y=fabs(end.y-start.y);
7347 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7349 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7352 if (arc.x > (0.5*segment.x))
7353 arc.x=0.5*segment.x;
7354 if (arc.y > (0.5*segment.y))
7355 arc.y=0.5*segment.y;
7356 point.x=start.x+segment.x-arc.x;
7357 point.y=start.y+arc.y;
7360 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7361 return(MagickFalse);
7362 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7363 mvg_info->offset+=(ssize_t) p->coordinates;
7364 point.x=start.x+segment.x-arc.x;
7365 point.y=start.y+segment.y-arc.y;
7368 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7369 return(MagickFalse);
7370 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7371 mvg_info->offset+=(ssize_t) p->coordinates;
7372 point.x=start.x+arc.x;
7373 point.y=start.y+segment.y-arc.y;
7376 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7377 return(MagickFalse);
7378 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7379 mvg_info->offset+=(ssize_t) p->coordinates;
7380 point.x=start.x+arc.x;
7381 point.y=start.y+arc.y;
7384 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7385 return(MagickFalse);
7386 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7387 mvg_info->offset+=(ssize_t) p->coordinates;
7388 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7389 return(MagickFalse);
7390 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7391 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7392 return(MagickFalse);
7393 p+=(ptrdiff_t) p->coordinates;
7394 mvg_info->offset=offset;
7395 primitive_info=(*mvg_info->primitive_info)+offset;
7396 primitive_info->coordinates=(size_t) (p-primitive_info);
7397 primitive_info->closed_subpath=MagickTrue;
7398 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7400 p->primitive=primitive_info->primitive;
7406static MagickBooleanType TraceSquareLinecap(
PrimitiveInfo *primitive_info,
7407 const size_t number_vertices,
const double offset)
7424 for (i=1; i < (ssize_t) number_vertices; i++)
7426 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7427 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7428 if ((fabs((
double) dx) >= MagickEpsilon) ||
7429 (fabs((
double) dy) >= MagickEpsilon))
7432 if (i == (ssize_t) number_vertices)
7433 i=(ssize_t) number_vertices-1L;
7434 distance=hypot((
double) dx,(
double) dy);
7435 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7436 dx*(distance+offset)/distance);
7437 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7438 dy*(distance+offset)/distance);
7439 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7441 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7442 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7443 if ((fabs((
double) dx) >= MagickEpsilon) ||
7444 (fabs((
double) dy) >= MagickEpsilon))
7447 distance=hypot((
double) dx,(
double) dy);
7448 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7449 dx*(distance+offset)/distance);
7450 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7451 dy*(distance+offset)/distance);
7458#define MaxStrokePad (6*BezierQuantum+360)
7459#define CheckPathExtent(pad_p,pad_q) \
7461 if ((pad_p) > MaxBezierCoordinates) \
7462 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7464 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7466 if (~extent_p < (pad_p)) \
7467 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7470 extent_p+=(pad_p); \
7471 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7472 MaxStrokePad,sizeof(*stroke_p)); \
7475 if ((pad_q) > MaxBezierCoordinates) \
7476 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7478 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7480 if (~extent_q < (pad_q)) \
7481 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7484 extent_q+=(pad_q); \
7485 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7486 MaxStrokePad,sizeof(*stroke_q)); \
7489 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7491 if (stroke_p != (PointInfo *) NULL) \
7492 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7493 if (stroke_q != (PointInfo *) NULL) \
7494 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7495 polygon_primitive=(PrimitiveInfo *) \
7496 RelinquishMagickMemory(polygon_primitive); \
7497 (void) ThrowMagickException(exception,GetMagickModule(), \
7498 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7499 return((PrimitiveInfo *) NULL); \
7503 typedef struct _StrokeSegment
7549 inverse_slope = {0.0, 0.0},
7556 number_vertices=primitive_info->coordinates;
7557 polygon_primitive=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7558 number_vertices+2UL,
sizeof(*polygon_primitive));
7561 (void) ThrowMagickException(exception,GetMagickModule(),
7562 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7565 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7566 sizeof(*polygon_primitive));
7567 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7568 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7569 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7570 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7571 if ((draw_info->linejoin == RoundJoin) ||
7572 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7574 polygon_primitive[number_vertices]=primitive_info[1];
7577 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7581 closed_path=primitive_info[0].closed_subpath;
7584 for (n=1; n < (ssize_t) number_vertices; n++)
7586 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7587 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7588 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7591 if (n == (ssize_t) number_vertices)
7593 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7599 sizeof(*stroke_polygon));
7600 stroke_polygon[0]=polygon_primitive[0];
7601 stroke_polygon[0].coordinates=0;
7604 return(stroke_polygon);
7606 n=(ssize_t) number_vertices-1L;
7608 extent_p=2*number_vertices;
7609 extent_q=2*number_vertices;
7610 stroke_p=(
PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7612 stroke_q=(
PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7617 stroke_p=(
PointInfo *) RelinquishMagickMemory(stroke_p);
7619 stroke_q=(
PointInfo *) RelinquishMagickMemory(stroke_q);
7621 RelinquishMagickMemory(polygon_primitive);
7622 (void) ThrowMagickException(exception,GetMagickModule(),
7623 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7627 inverse_slope.p=0.0;
7628 if (fabs(dx.p) < MagickEpsilon)
7631 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7633 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7636 if (fabs(dy.p) < MagickEpsilon)
7639 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7641 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7646 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7648 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7649 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7650 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7651 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7652 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7653 offset.y=(double) (offset.x*inverse_slope.p);
7654 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7656 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7657 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7658 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7659 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7660 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7661 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7662 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7663 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7667 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7668 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7669 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7670 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7671 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7672 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7673 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7674 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7681 stroke_q[p++]=box_q[0];
7682 stroke_p[q++]=box_p[0];
7683 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7688 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7689 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7690 dot_product=dx.q*dx.q+dy.q*dy.q;
7691 if (dot_product < 0.25)
7694 inverse_slope.q=0.0;
7695 if (fabs(dx.q) < MagickEpsilon)
7698 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7700 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7703 if (fabs(dy.q) < MagickEpsilon)
7706 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7708 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7713 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7715 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7716 offset.y=(double) (offset.x*inverse_slope.q);
7717 dot_product=dy.q*offset.x-dx.q*offset.y;
7718 if (dot_product > 0.0)
7720 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7721 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7722 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7723 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7724 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7725 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7726 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7727 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7731 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7732 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7733 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7734 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7735 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7736 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7737 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7738 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7740 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7747 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7748 box_p[3].y)/(slope.p-slope.q));
7749 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7750 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7751 box_q[3].y)/(slope.p-slope.q));
7752 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7754 DisableMSCWarning(4127)
7755 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7757 dot_product=dx.q*dy.p-dx.p*dy.q;
7758 if (dot_product <= 0.0)
7759 switch (draw_info->linejoin)
7763 stroke_q[q++]=box_q[1];
7764 stroke_q[q++]=box_q[2];
7765 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7766 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7767 if (dot_product <= miterlimit)
7768 stroke_p[p++]=box_p[4];
7771 stroke_p[p++]=box_p[1];
7772 stroke_p[p++]=box_p[2];
7778 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7779 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7780 if (dot_product <= miterlimit)
7782 stroke_q[q++]=box_q[4];
7783 stroke_p[p++]=box_p[4];
7787 stroke_q[q++]=box_q[1];
7788 stroke_q[q++]=box_q[2];
7789 stroke_p[p++]=box_p[1];
7790 stroke_p[p++]=box_p[2];
7796 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7797 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7798 if (dot_product <= miterlimit)
7799 stroke_p[p++]=box_p[4];
7802 stroke_p[p++]=box_p[1];
7803 stroke_p[p++]=box_p[2];
7805 center=polygon_primitive[n].point;
7806 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7807 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7808 if (theta.q < theta.p)
7809 theta.q+=2.0*MagickPI;
7810 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.q-
7811 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7812 DisableMSCWarning(4127)
7813 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7815 stroke_q[q].x=box_q[1].x;
7816 stroke_q[q].y=box_q[1].y;
7818 for (j=1; j < (ssize_t) arc_segments; j++)
7820 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7821 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7822 (theta.p+delta_theta),DegreesToRadians(360.0))));
7823 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7824 (theta.p+delta_theta),DegreesToRadians(360.0))));
7827 stroke_q[q++]=box_q[2];
7834 switch (draw_info->linejoin)
7838 stroke_p[p++]=box_p[1];
7839 stroke_p[p++]=box_p[2];
7840 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7841 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7842 if (dot_product <= miterlimit)
7843 stroke_q[q++]=box_q[4];
7846 stroke_q[q++]=box_q[1];
7847 stroke_q[q++]=box_q[2];
7853 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7854 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7855 if (dot_product <= miterlimit)
7857 stroke_q[q++]=box_q[4];
7858 stroke_p[p++]=box_p[4];
7862 stroke_q[q++]=box_q[1];
7863 stroke_q[q++]=box_q[2];
7864 stroke_p[p++]=box_p[1];
7865 stroke_p[p++]=box_p[2];
7871 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7872 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7873 if (dot_product <= miterlimit)
7874 stroke_q[q++]=box_q[4];
7877 stroke_q[q++]=box_q[1];
7878 stroke_q[q++]=box_q[2];
7880 center=polygon_primitive[n].point;
7881 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7882 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7883 if (theta.p < theta.q)
7884 theta.p+=2.0*MagickPI;
7885 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.p-
7886 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7887 DisableMSCWarning(4127)
7888 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7890 stroke_p[p++]=box_p[1];
7891 for (j=1; j < (ssize_t) arc_segments; j++)
7893 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7894 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7895 (theta.p+delta_theta),DegreesToRadians(360.0))));
7896 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7897 (theta.p+delta_theta),DegreesToRadians(360.0))));
7900 stroke_p[p++]=box_p[2];
7907 inverse_slope.p=inverse_slope.q;
7916 stroke_p[p++]=box_p[1];
7917 stroke_q[q++]=box_q[1];
7921 stroke_polygon=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7922 (p+q+2L),(
size_t) (closed_path+2L)*
sizeof(*stroke_polygon));
7925 (void) ThrowMagickException(exception,GetMagickModule(),
7926 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7927 stroke_p=(
PointInfo *) RelinquishMagickMemory(stroke_p);
7928 stroke_q=(
PointInfo *) RelinquishMagickMemory(stroke_q);
7931 return(stroke_polygon);
7933 for (i=0; i < (ssize_t) p; i++)
7935 stroke_polygon[i]=polygon_primitive[0];
7936 stroke_polygon[i].point=stroke_p[i];
7938 if (closed_path != MagickFalse)
7940 stroke_polygon[i]=polygon_primitive[0];
7941 stroke_polygon[i].point=stroke_polygon[0].point;
7944 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7946 stroke_polygon[i]=polygon_primitive[0];
7947 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7949 if (closed_path != MagickFalse)
7951 stroke_polygon[i]=polygon_primitive[0];
7952 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7955 stroke_polygon[i]=polygon_primitive[0];
7956 stroke_polygon[i].point=stroke_polygon[0].point;
7958 stroke_polygon[i].primitive=UndefinedPrimitive;
7959 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7960 stroke_p=(
PointInfo *) RelinquishMagickMemory(stroke_p);
7961 stroke_q=(
PointInfo *) RelinquishMagickMemory(stroke_q);
7962 polygon_primitive=(
PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7963 return(stroke_polygon);